blob: 97b212570bcc67f2431d86b17740e1051812f031 [file] [log] [blame]
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001/*
2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
4 *
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16 * SOFTWARE.
17 */
18#include <linux/module.h>
19#include <linux/mempool.h>
20#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070022#include <linux/errno.h>
23#include <linux/init.h>
24#include <linux/pci.h>
25#include <linux/skbuff.h>
26#include <linux/interrupt.h>
27#include <linux/spinlock.h>
28#include <linux/workqueue.h>
Joe Eykholt78112e52009-11-03 11:49:22 -080029#include <linux/if_ether.h>
30#include <scsi/fc/fc_fip.h>
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070031#include <scsi/scsi_host.h>
32#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_fc.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/libfc.h>
36#include <scsi/fc_frame.h>
37
38#include "vnic_dev.h"
39#include "vnic_intr.h"
40#include "vnic_stats.h"
41#include "fnic_io.h"
42#include "fnic.h"
43
44#define PCI_DEVICE_ID_CISCO_FNIC 0x0045
45
46/* Timer to poll notification area for events. Used for MSI interrupts */
47#define FNIC_NOTIFY_TIMER_PERIOD (2 * HZ)
48
49static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES];
50static struct kmem_cache *fnic_io_req_cache;
51LIST_HEAD(fnic_list);
52DEFINE_SPINLOCK(fnic_list_lock);
53
54/* Supported devices by fnic module */
55static struct pci_device_id fnic_id_table[] = {
56 { PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_FNIC) },
57 { 0, }
58};
59
60MODULE_DESCRIPTION(DRV_DESCRIPTION);
61MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, "
62 "Joseph R. Eykholt <jeykholt@cisco.com>");
63MODULE_LICENSE("GPL v2");
64MODULE_VERSION(DRV_VERSION);
65MODULE_DEVICE_TABLE(pci, fnic_id_table);
66
67unsigned int fnic_log_level;
68module_param(fnic_log_level, int, S_IRUGO|S_IWUSR);
69MODULE_PARM_DESC(fnic_log_level, "bit mask of fnic logging levels");
70
71
72static struct libfc_function_template fnic_transport_template = {
73 .frame_send = fnic_send,
Joe Eykholt78112e52009-11-03 11:49:22 -080074 .lport_set_port_id = fnic_set_port_id,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070075 .fcp_abort_io = fnic_empty_scsi_cleanup,
76 .fcp_cleanup = fnic_empty_scsi_cleanup,
77 .exch_mgr_reset = fnic_exch_mgr_reset
78};
79
80static int fnic_slave_alloc(struct scsi_device *sdev)
81{
82 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
83 struct fc_lport *lp = shost_priv(sdev->host);
84 struct fnic *fnic = lport_priv(lp);
85
86 sdev->tagged_supported = 1;
87
88 if (!rport || fc_remote_port_chkready(rport))
89 return -ENXIO;
90
91 scsi_activate_tcq(sdev, FNIC_DFLT_QUEUE_DEPTH);
92 rport->dev_loss_tmo = fnic->config.port_down_timeout / 1000;
93
94 return 0;
95}
96
97static struct scsi_host_template fnic_host_template = {
98 .module = THIS_MODULE,
99 .name = DRV_NAME,
100 .queuecommand = fnic_queuecommand,
101 .eh_abort_handler = fnic_abort_cmd,
102 .eh_device_reset_handler = fnic_device_reset,
103 .eh_host_reset_handler = fnic_host_reset,
104 .slave_alloc = fnic_slave_alloc,
105 .change_queue_depth = fc_change_queue_depth,
106 .change_queue_type = fc_change_queue_type,
107 .this_id = -1,
108 .cmd_per_lun = 3,
109 .can_queue = FNIC_MAX_IO_REQ,
110 .use_clustering = ENABLE_CLUSTERING,
111 .sg_tablesize = FNIC_MAX_SG_DESC_CNT,
112 .max_sectors = 0xffff,
113 .shost_attrs = fnic_attrs,
114};
115
116static void fnic_get_host_speed(struct Scsi_Host *shost);
117static struct scsi_transport_template *fnic_fc_transport;
118static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *);
119
120static struct fc_function_template fnic_fc_functions = {
121
122 .show_host_node_name = 1,
123 .show_host_port_name = 1,
124 .show_host_supported_classes = 1,
125 .show_host_supported_fc4s = 1,
126 .show_host_active_fc4s = 1,
127 .show_host_maxframe_size = 1,
128 .show_host_port_id = 1,
129 .show_host_supported_speeds = 1,
130 .get_host_speed = fnic_get_host_speed,
131 .show_host_speed = 1,
132 .show_host_port_type = 1,
133 .get_host_port_state = fc_get_host_port_state,
134 .show_host_port_state = 1,
135 .show_host_symbolic_name = 1,
136 .show_rport_maxframe_size = 1,
137 .show_rport_supported_classes = 1,
138 .show_host_fabric_name = 1,
139 .show_starget_node_name = 1,
140 .show_starget_port_name = 1,
141 .show_starget_port_id = 1,
142 .show_rport_dev_loss_tmo = 1,
143 .issue_fc_host_lip = fnic_reset,
144 .get_fc_host_stats = fnic_get_stats,
145 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
146 .terminate_rport_io = fnic_terminate_rport_io,
Joe Eykholt76d87372009-11-03 11:49:32 -0800147 .bsg_request = fc_lport_bsg_request,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700148};
149
150static void fnic_get_host_speed(struct Scsi_Host *shost)
151{
152 struct fc_lport *lp = shost_priv(shost);
153 struct fnic *fnic = lport_priv(lp);
154 u32 port_speed = vnic_dev_port_speed(fnic->vdev);
155
156 /* Add in other values as they get defined in fw */
157 switch (port_speed) {
158 case 10000:
159 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
160 break;
161 default:
162 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
163 break;
164 }
165}
166
167static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host)
168{
169 int ret;
170 struct fc_lport *lp = shost_priv(host);
171 struct fnic *fnic = lport_priv(lp);
172 struct fc_host_statistics *stats = &lp->host_stats;
173 struct vnic_stats *vs;
174 unsigned long flags;
175
176 if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT))
177 return stats;
178 fnic->stats_time = jiffies;
179
180 spin_lock_irqsave(&fnic->fnic_lock, flags);
181 ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
182 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
183
184 if (ret) {
185 FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host,
186 "fnic: Get vnic stats failed"
187 " 0x%x", ret);
188 return stats;
189 }
190 vs = fnic->stats;
191 stats->tx_frames = vs->tx.tx_unicast_frames_ok;
192 stats->tx_words = vs->tx.tx_unicast_bytes_ok / 4;
193 stats->rx_frames = vs->rx.rx_unicast_frames_ok;
194 stats->rx_words = vs->rx.rx_unicast_bytes_ok / 4;
195 stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors;
196 stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop;
197 stats->invalid_crc_count = vs->rx.rx_crc_errors;
198 stats->seconds_since_last_reset = (jiffies - lp->boot_time) / HZ;
199 stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000);
200 stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000);
201
202 return stats;
203}
204
205void fnic_log_q_error(struct fnic *fnic)
206{
207 unsigned int i;
208 u32 error_status;
209
210 for (i = 0; i < fnic->raw_wq_count; i++) {
211 error_status = ioread32(&fnic->wq[i].ctrl->error_status);
212 if (error_status)
213 shost_printk(KERN_ERR, fnic->lport->host,
214 "WQ[%d] error_status"
215 " %d\n", i, error_status);
216 }
217
218 for (i = 0; i < fnic->rq_count; i++) {
219 error_status = ioread32(&fnic->rq[i].ctrl->error_status);
220 if (error_status)
221 shost_printk(KERN_ERR, fnic->lport->host,
222 "RQ[%d] error_status"
223 " %d\n", i, error_status);
224 }
225
226 for (i = 0; i < fnic->wq_copy_count; i++) {
227 error_status = ioread32(&fnic->wq_copy[i].ctrl->error_status);
228 if (error_status)
229 shost_printk(KERN_ERR, fnic->lport->host,
230 "CWQ[%d] error_status"
231 " %d\n", i, error_status);
232 }
233}
234
235void fnic_handle_link_event(struct fnic *fnic)
236{
237 unsigned long flags;
238
239 spin_lock_irqsave(&fnic->fnic_lock, flags);
240 if (fnic->stop_rx_link_events) {
241 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
242 return;
243 }
244 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
245
246 queue_work(fnic_event_queue, &fnic->link_work);
247
248}
249
250static int fnic_notify_set(struct fnic *fnic)
251{
252 int err;
253
254 switch (vnic_dev_get_intr_mode(fnic->vdev)) {
255 case VNIC_DEV_INTR_MODE_INTX:
256 err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY);
257 break;
258 case VNIC_DEV_INTR_MODE_MSI:
259 err = vnic_dev_notify_set(fnic->vdev, -1);
260 break;
261 case VNIC_DEV_INTR_MODE_MSIX:
262 err = vnic_dev_notify_set(fnic->vdev, FNIC_MSIX_ERR_NOTIFY);
263 break;
264 default:
265 shost_printk(KERN_ERR, fnic->lport->host,
266 "Interrupt mode should be set up"
267 " before devcmd notify set %d\n",
268 vnic_dev_get_intr_mode(fnic->vdev));
269 err = -1;
270 break;
271 }
272
273 return err;
274}
275
276static void fnic_notify_timer(unsigned long data)
277{
278 struct fnic *fnic = (struct fnic *)data;
279
280 fnic_handle_link_event(fnic);
281 mod_timer(&fnic->notify_timer,
282 round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD));
283}
284
285static void fnic_notify_timer_start(struct fnic *fnic)
286{
287 switch (vnic_dev_get_intr_mode(fnic->vdev)) {
288 case VNIC_DEV_INTR_MODE_MSI:
289 /*
290 * Schedule first timeout immediately. The driver is
291 * initiatialized and ready to look for link up notification
292 */
293 mod_timer(&fnic->notify_timer, jiffies);
294 break;
295 default:
296 /* Using intr for notification for INTx/MSI-X */
297 break;
298 };
299}
300
301static int fnic_dev_wait(struct vnic_dev *vdev,
302 int (*start)(struct vnic_dev *, int),
303 int (*finished)(struct vnic_dev *, int *),
304 int arg)
305{
306 unsigned long time;
307 int done;
308 int err;
309
310 err = start(vdev, arg);
311 if (err)
312 return err;
313
314 /* Wait for func to complete...2 seconds max */
315 time = jiffies + (HZ * 2);
316 do {
317 err = finished(vdev, &done);
318 if (err)
319 return err;
320 if (done)
321 return 0;
322 schedule_timeout_uninterruptible(HZ / 10);
323 } while (time_after(time, jiffies));
324
325 return -ETIMEDOUT;
326}
327
328static int fnic_cleanup(struct fnic *fnic)
329{
330 unsigned int i;
331 int err;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700332
333 vnic_dev_disable(fnic->vdev);
334 for (i = 0; i < fnic->intr_count; i++)
335 vnic_intr_mask(&fnic->intr[i]);
336
337 for (i = 0; i < fnic->rq_count; i++) {
338 err = vnic_rq_disable(&fnic->rq[i]);
339 if (err)
340 return err;
341 }
342 for (i = 0; i < fnic->raw_wq_count; i++) {
343 err = vnic_wq_disable(&fnic->wq[i]);
344 if (err)
345 return err;
346 }
347 for (i = 0; i < fnic->wq_copy_count; i++) {
348 err = vnic_wq_copy_disable(&fnic->wq_copy[i]);
349 if (err)
350 return err;
351 }
352
353 /* Clean up completed IOs and FCS frames */
354 fnic_wq_copy_cmpl_handler(fnic, -1);
355 fnic_wq_cmpl_handler(fnic, -1);
356 fnic_rq_cmpl_handler(fnic, -1);
357
358 /* Clean up the IOs and FCS frames that have not completed */
359 for (i = 0; i < fnic->raw_wq_count; i++)
360 vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf);
361 for (i = 0; i < fnic->rq_count; i++)
362 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
363 for (i = 0; i < fnic->wq_copy_count; i++)
364 vnic_wq_copy_clean(&fnic->wq_copy[i],
365 fnic_wq_copy_cleanup_handler);
366
367 for (i = 0; i < fnic->cq_count; i++)
368 vnic_cq_clean(&fnic->cq[i]);
369 for (i = 0; i < fnic->intr_count; i++)
370 vnic_intr_clean(&fnic->intr[i]);
371
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700372 mempool_destroy(fnic->io_req_pool);
373 for (i = 0; i < FNIC_SGL_NUM_CACHES; i++)
374 mempool_destroy(fnic->io_sgl_pool[i]);
375
376 return 0;
377}
378
379static void fnic_iounmap(struct fnic *fnic)
380{
381 if (fnic->bar0.vaddr)
382 iounmap(fnic->bar0.vaddr);
383}
384
385/*
386 * Allocate element for mempools requiring GFP_DMA flag.
387 * Otherwise, checks in kmem_flagcheck() hit BUG_ON().
388 */
389static void *fnic_alloc_slab_dma(gfp_t gfp_mask, void *pool_data)
390{
391 struct kmem_cache *mem = pool_data;
392
393 return kmem_cache_alloc(mem, gfp_mask | GFP_ATOMIC | GFP_DMA);
394}
395
Joe Eykholt78112e52009-11-03 11:49:22 -0800396/**
397 * fnic_get_mac() - get assigned data MAC address for FIP code.
398 * @lport: local port.
399 */
400static u8 *fnic_get_mac(struct fc_lport *lport)
401{
402 struct fnic *fnic = lport_priv(lport);
403
404 return fnic->data_src_addr;
405}
406
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700407static int __devinit fnic_probe(struct pci_dev *pdev,
408 const struct pci_device_id *ent)
409{
410 struct Scsi_Host *host;
411 struct fc_lport *lp;
412 struct fnic *fnic;
413 mempool_t *pool;
414 int err;
415 int i;
416 unsigned long flags;
417
418 /*
419 * Allocate SCSI Host and set up association between host,
420 * local port, and fnic
421 */
Chris Leech86221962009-11-03 11:46:08 -0800422 lp = libfc_host_alloc(&fnic_host_template, sizeof(struct fnic));
423 if (!lp) {
424 printk(KERN_ERR PFX "Unable to alloc libfc local port\n");
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700425 err = -ENOMEM;
426 goto err_out;
427 }
Chris Leech86221962009-11-03 11:46:08 -0800428 host = lp->host;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700429 fnic = lport_priv(lp);
430 fnic->lport = lp;
Joe Eykholt78112e52009-11-03 11:49:22 -0800431 fnic->ctlr.lp = lp;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700432
433 snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME,
434 host->host_no);
435
436 host->transportt = fnic_fc_transport;
437
438 err = scsi_init_shared_tag_map(host, FNIC_MAX_IO_REQ);
439 if (err) {
440 shost_printk(KERN_ERR, fnic->lport->host,
441 "Unable to alloc shared tag map\n");
442 goto err_out_free_hba;
443 }
444
445 /* Setup PCI resources */
446 pci_set_drvdata(pdev, fnic);
447
448 fnic->pdev = pdev;
449
450 err = pci_enable_device(pdev);
451 if (err) {
452 shost_printk(KERN_ERR, fnic->lport->host,
453 "Cannot enable PCI device, aborting.\n");
454 goto err_out_free_hba;
455 }
456
457 err = pci_request_regions(pdev, DRV_NAME);
458 if (err) {
459 shost_printk(KERN_ERR, fnic->lport->host,
460 "Cannot enable PCI resources, aborting\n");
461 goto err_out_disable_device;
462 }
463
464 pci_set_master(pdev);
465
466 /* Query PCI controller on system for DMA addressing
467 * limitation for the device. Try 40-bit first, and
468 * fail to 32-bit.
469 */
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700470 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700471 if (err) {
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700472 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700473 if (err) {
474 shost_printk(KERN_ERR, fnic->lport->host,
475 "No usable DMA configuration "
476 "aborting\n");
477 goto err_out_release_regions;
478 }
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700479 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700480 if (err) {
481 shost_printk(KERN_ERR, fnic->lport->host,
482 "Unable to obtain 32-bit DMA "
483 "for consistent allocations, aborting.\n");
484 goto err_out_release_regions;
485 }
486 } else {
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700487 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700488 if (err) {
489 shost_printk(KERN_ERR, fnic->lport->host,
490 "Unable to obtain 40-bit DMA "
491 "for consistent allocations, aborting.\n");
492 goto err_out_release_regions;
493 }
494 }
495
496 /* Map vNIC resources from BAR0 */
497 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
498 shost_printk(KERN_ERR, fnic->lport->host,
499 "BAR0 not memory-map'able, aborting.\n");
500 err = -ENODEV;
501 goto err_out_release_regions;
502 }
503
504 fnic->bar0.vaddr = pci_iomap(pdev, 0, 0);
505 fnic->bar0.bus_addr = pci_resource_start(pdev, 0);
506 fnic->bar0.len = pci_resource_len(pdev, 0);
507
508 if (!fnic->bar0.vaddr) {
509 shost_printk(KERN_ERR, fnic->lport->host,
510 "Cannot memory-map BAR0 res hdr, "
511 "aborting.\n");
512 err = -ENODEV;
513 goto err_out_release_regions;
514 }
515
516 fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0);
517 if (!fnic->vdev) {
518 shost_printk(KERN_ERR, fnic->lport->host,
519 "vNIC registration failed, "
520 "aborting.\n");
521 err = -ENODEV;
522 goto err_out_iounmap;
523 }
524
525 err = fnic_dev_wait(fnic->vdev, vnic_dev_open,
526 vnic_dev_open_done, 0);
527 if (err) {
528 shost_printk(KERN_ERR, fnic->lport->host,
529 "vNIC dev open failed, aborting.\n");
530 goto err_out_vnic_unregister;
531 }
532
533 err = vnic_dev_init(fnic->vdev, 0);
534 if (err) {
535 shost_printk(KERN_ERR, fnic->lport->host,
536 "vNIC dev init failed, aborting.\n");
537 goto err_out_dev_close;
538 }
539
Joe Eykholt78112e52009-11-03 11:49:22 -0800540 err = vnic_dev_mac_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700541 if (err) {
542 shost_printk(KERN_ERR, fnic->lport->host,
543 "vNIC get MAC addr failed \n");
544 goto err_out_dev_close;
545 }
Joe Eykholt78112e52009-11-03 11:49:22 -0800546 /* set data_src for point-to-point mode and to keep it non-zero */
547 memcpy(fnic->data_src_addr, fnic->ctlr.ctl_src_addr, ETH_ALEN);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700548
549 /* Get vNIC configuration */
550 err = fnic_get_vnic_config(fnic);
551 if (err) {
552 shost_printk(KERN_ERR, fnic->lport->host,
553 "Get vNIC configuration failed, "
554 "aborting.\n");
555 goto err_out_dev_close;
556 }
557 host->max_lun = fnic->config.luns_per_tgt;
558 host->max_id = FNIC_MAX_FCP_TARGET;
Abhijeet Joglekarf9bdc3d2009-10-21 16:28:19 -0700559 host->max_cmd_len = FNIC_MAX_CMD_LEN;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700560
561 fnic_get_res_counts(fnic);
562
563 err = fnic_set_intr_mode(fnic);
564 if (err) {
565 shost_printk(KERN_ERR, fnic->lport->host,
566 "Failed to set intr mode, "
567 "aborting.\n");
568 goto err_out_dev_close;
569 }
570
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700571 err = fnic_alloc_vnic_resources(fnic);
572 if (err) {
573 shost_printk(KERN_ERR, fnic->lport->host,
574 "Failed to alloc vNIC resources, "
575 "aborting.\n");
Abhijeet Joglekar2e76f762009-11-03 11:45:37 -0800576 goto err_out_clear_intr;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700577 }
578
579
580 /* initialize all fnic locks */
581 spin_lock_init(&fnic->fnic_lock);
582
583 for (i = 0; i < FNIC_WQ_MAX; i++)
584 spin_lock_init(&fnic->wq_lock[i]);
585
586 for (i = 0; i < FNIC_WQ_COPY_MAX; i++) {
587 spin_lock_init(&fnic->wq_copy_lock[i]);
588 fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK;
589 fnic->fw_ack_recd[i] = 0;
590 fnic->fw_ack_index[i] = -1;
591 }
592
593 for (i = 0; i < FNIC_IO_LOCKS; i++)
594 spin_lock_init(&fnic->io_req_lock[i]);
595
596 fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache);
597 if (!fnic->io_req_pool)
598 goto err_out_free_resources;
599
600 pool = mempool_create(2, fnic_alloc_slab_dma, mempool_free_slab,
601 fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
602 if (!pool)
603 goto err_out_free_ioreq_pool;
604 fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool;
605
606 pool = mempool_create(2, fnic_alloc_slab_dma, mempool_free_slab,
607 fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
608 if (!pool)
609 goto err_out_free_dflt_pool;
610 fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool;
611
612 /* setup vlan config, hw inserts vlan header */
613 fnic->vlan_hw_insert = 1;
614 fnic->vlan_id = 0;
615
Joe Eykholt78112e52009-11-03 11:49:22 -0800616 /* Initialize the FIP fcoe_ctrl struct */
617 fnic->ctlr.send = fnic_eth_send;
618 fnic->ctlr.update_mac = fnic_update_mac;
619 fnic->ctlr.get_src_addr = fnic_get_mac;
620 fcoe_ctlr_init(&fnic->ctlr);
621 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
622 shost_printk(KERN_INFO, fnic->lport->host,
623 "firmware supports FIP\n");
Venkata Siva Vijayendra Bhamidipatiaaa5e562010-02-16 12:15:44 -0800624 /* enable directed and multicast */
625 vnic_dev_packet_filter(fnic->vdev, 1, 1, 0, 0, 0);
Joe Eykholt78112e52009-11-03 11:49:22 -0800626 vnic_dev_add_addr(fnic->vdev, FIP_ALL_ENODE_MACS);
627 vnic_dev_add_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
628 } else {
629 shost_printk(KERN_INFO, fnic->lport->host,
630 "firmware uses non-FIP mode\n");
631 fnic->ctlr.mode = FIP_ST_NON_FIP;
632 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700633 fnic->state = FNIC_IN_FC_MODE;
634
635 /* Enable hardware stripping of vlan header on ingress */
636 fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1);
637
638 /* Setup notification buffer area */
639 err = fnic_notify_set(fnic);
640 if (err) {
641 shost_printk(KERN_ERR, fnic->lport->host,
642 "Failed to alloc notify buffer, aborting.\n");
643 goto err_out_free_max_pool;
644 }
645
646 /* Setup notify timer when using MSI interrupts */
647 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
648 setup_timer(&fnic->notify_timer,
649 fnic_notify_timer, (unsigned long)fnic);
650
651 /* allocate RQ buffers and post them to RQ*/
652 for (i = 0; i < fnic->rq_count; i++) {
653 err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
654 if (err) {
655 shost_printk(KERN_ERR, fnic->lport->host,
656 "fnic_alloc_rq_frame can't alloc "
657 "frame\n");
658 goto err_out_free_rq_buf;
659 }
660 }
661
662 /*
663 * Initialization done with PCI system, hardware, firmware.
664 * Add host to SCSI
665 */
666 err = scsi_add_host(lp->host, &pdev->dev);
667 if (err) {
668 shost_printk(KERN_ERR, fnic->lport->host,
669 "fnic: scsi_add_host failed...exiting\n");
670 goto err_out_free_rq_buf;
671 }
672
673 /* Start local port initiatialization */
674
675 lp->link_up = 0;
676 lp->tt = fnic_transport_template;
677
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700678 lp->max_retry_count = fnic->config.flogi_retries;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700679 lp->max_rport_retry_count = fnic->config.plogi_retries;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700680 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
681 FCP_SPPF_CONF_COMPL);
682 if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR)
683 lp->service_params |= FCP_SPPF_RETRY;
684
685 lp->boot_time = jiffies;
686 lp->e_d_tov = fnic->config.ed_tov;
687 lp->r_a_tov = fnic->config.ra_tov;
688 lp->link_supported_speeds = FC_PORTSPEED_10GBIT;
689 fc_set_wwnn(lp, fnic->config.node_wwn);
690 fc_set_wwpn(lp, fnic->config.port_wwn);
691
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700692 fc_lport_init(lp);
Vasu Dev52ff8782009-07-29 17:05:10 -0700693 fc_exch_init(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700694 fc_elsct_init(lp);
695 fc_rport_init(lp);
696 fc_disc_init(lp);
697
Vasu Dev52ff8782009-07-29 17:05:10 -0700698 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCPIO_HOST_EXCH_RANGE_START,
699 FCPIO_HOST_EXCH_RANGE_END, NULL)) {
700 err = -ENOMEM;
701 goto err_out_remove_scsi_host;
702 }
703
Venkata Siva Vijayendra Bhamidipatic693a712010-02-16 12:15:50 -0800704 fc_lport_init_stats(lp);
705
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700706 fc_lport_config(lp);
707
708 if (fc_set_mfs(lp, fnic->config.maxdatafieldsize +
709 sizeof(struct fc_frame_header))) {
710 err = -EINVAL;
711 goto err_out_free_exch_mgr;
712 }
713 fc_host_maxframe_size(lp->host) = lp->mfs;
714
715 sprintf(fc_host_symbolic_name(lp->host),
716 DRV_NAME " v" DRV_VERSION " over %s", fnic->name);
717
718 spin_lock_irqsave(&fnic_list_lock, flags);
719 list_add_tail(&fnic->list, &fnic_list);
720 spin_unlock_irqrestore(&fnic_list_lock, flags);
721
722 INIT_WORK(&fnic->link_work, fnic_handle_link);
723 INIT_WORK(&fnic->frame_work, fnic_handle_frame);
724 skb_queue_head_init(&fnic->frame_queue);
Joe Eykholt78112e52009-11-03 11:49:22 -0800725 skb_queue_head_init(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700726
727 /* Enable all queues */
728 for (i = 0; i < fnic->raw_wq_count; i++)
729 vnic_wq_enable(&fnic->wq[i]);
730 for (i = 0; i < fnic->rq_count; i++)
731 vnic_rq_enable(&fnic->rq[i]);
732 for (i = 0; i < fnic->wq_copy_count; i++)
733 vnic_wq_copy_enable(&fnic->wq_copy[i]);
734
735 fc_fabric_login(lp);
736
737 vnic_dev_enable(fnic->vdev);
Abhijeet Joglekar2e76f762009-11-03 11:45:37 -0800738
739 err = fnic_request_intr(fnic);
740 if (err) {
741 shost_printk(KERN_ERR, fnic->lport->host,
742 "Unable to request irq.\n");
743 goto err_out_free_exch_mgr;
744 }
745
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700746 for (i = 0; i < fnic->intr_count; i++)
747 vnic_intr_unmask(&fnic->intr[i]);
748
749 fnic_notify_timer_start(fnic);
750
751 return 0;
752
753err_out_free_exch_mgr:
Vasu Dev52ff8782009-07-29 17:05:10 -0700754 fc_exch_mgr_free(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700755err_out_remove_scsi_host:
Joe Eykholt78112e52009-11-03 11:49:22 -0800756 fc_remove_host(lp->host);
757 scsi_remove_host(lp->host);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700758err_out_free_rq_buf:
759 for (i = 0; i < fnic->rq_count; i++)
760 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
761 vnic_dev_notify_unset(fnic->vdev);
762err_out_free_max_pool:
763 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]);
764err_out_free_dflt_pool:
765 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]);
766err_out_free_ioreq_pool:
767 mempool_destroy(fnic->io_req_pool);
768err_out_free_resources:
769 fnic_free_vnic_resources(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700770err_out_clear_intr:
771 fnic_clear_intr_mode(fnic);
772err_out_dev_close:
773 vnic_dev_close(fnic->vdev);
774err_out_vnic_unregister:
775 vnic_dev_unregister(fnic->vdev);
776err_out_iounmap:
777 fnic_iounmap(fnic);
778err_out_release_regions:
779 pci_release_regions(pdev);
780err_out_disable_device:
781 pci_disable_device(pdev);
782err_out_free_hba:
783 scsi_host_put(lp->host);
784err_out:
785 return err;
786}
787
788static void __devexit fnic_remove(struct pci_dev *pdev)
789{
790 struct fnic *fnic = pci_get_drvdata(pdev);
Joe Eykholt78112e52009-11-03 11:49:22 -0800791 struct fc_lport *lp = fnic->lport;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700792 unsigned long flags;
793
794 /*
795 * Mark state so that the workqueue thread stops forwarding
796 * received frames and link events to the local port. ISR and
797 * other threads that can queue work items will also stop
798 * creating work items on the fnic workqueue
799 */
800 spin_lock_irqsave(&fnic->fnic_lock, flags);
801 fnic->stop_rx_link_events = 1;
802 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
803
804 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
805 del_timer_sync(&fnic->notify_timer);
806
807 /*
808 * Flush the fnic event queue. After this call, there should
809 * be no event queued for this fnic device in the workqueue
810 */
811 flush_workqueue(fnic_event_queue);
812 skb_queue_purge(&fnic->frame_queue);
Joe Eykholt78112e52009-11-03 11:49:22 -0800813 skb_queue_purge(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700814
815 /*
816 * Log off the fabric. This stops all remote ports, dns port,
817 * logs off the fabric. This flushes all rport, disc, lport work
818 * before returning
819 */
820 fc_fabric_logoff(fnic->lport);
821
822 spin_lock_irqsave(&fnic->fnic_lock, flags);
823 fnic->in_remove = 1;
824 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
825
Joe Eykholt78112e52009-11-03 11:49:22 -0800826 fcoe_ctlr_destroy(&fnic->ctlr);
827 fc_lport_destroy(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700828
829 /*
830 * This stops the fnic device, masks all interrupts. Completed
831 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
832 * cleaned up
833 */
834 fnic_cleanup(fnic);
835
836 BUG_ON(!skb_queue_empty(&fnic->frame_queue));
Joe Eykholt78112e52009-11-03 11:49:22 -0800837 BUG_ON(!skb_queue_empty(&fnic->tx_queue));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700838
839 spin_lock_irqsave(&fnic_list_lock, flags);
840 list_del(&fnic->list);
841 spin_unlock_irqrestore(&fnic_list_lock, flags);
842
843 fc_remove_host(fnic->lport->host);
844 scsi_remove_host(fnic->lport->host);
Vasu Dev52ff8782009-07-29 17:05:10 -0700845 fc_exch_mgr_free(fnic->lport);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700846 vnic_dev_notify_unset(fnic->vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700847 fnic_free_intr(fnic);
Abhijeet Joglekar2e76f762009-11-03 11:45:37 -0800848 fnic_free_vnic_resources(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700849 fnic_clear_intr_mode(fnic);
850 vnic_dev_close(fnic->vdev);
851 vnic_dev_unregister(fnic->vdev);
852 fnic_iounmap(fnic);
853 pci_release_regions(pdev);
854 pci_disable_device(pdev);
855 pci_set_drvdata(pdev, NULL);
Joe Eykholt78112e52009-11-03 11:49:22 -0800856 scsi_host_put(lp->host);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700857}
858
859static struct pci_driver fnic_driver = {
860 .name = DRV_NAME,
861 .id_table = fnic_id_table,
862 .probe = fnic_probe,
863 .remove = __devexit_p(fnic_remove),
864};
865
866static int __init fnic_init_module(void)
867{
868 size_t len;
869 int err = 0;
870
871 printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
872
873 /* Create a cache for allocation of default size sgls */
874 len = sizeof(struct fnic_dflt_sgl_list);
875 fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create
876 ("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
877 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA,
878 NULL);
879 if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) {
880 printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n");
881 err = -ENOMEM;
882 goto err_create_fnic_sgl_slab_dflt;
883 }
884
885 /* Create a cache for allocation of max size sgls*/
886 len = sizeof(struct fnic_sgl_list);
887 fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create
888 ("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
889 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA,
890 NULL);
891 if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) {
892 printk(KERN_ERR PFX "failed to create fnic max sgl slab\n");
893 err = -ENOMEM;
894 goto err_create_fnic_sgl_slab_max;
895 }
896
897 /* Create a cache of io_req structs for use via mempool */
898 fnic_io_req_cache = kmem_cache_create("fnic_io_req",
899 sizeof(struct fnic_io_req),
900 0, SLAB_HWCACHE_ALIGN, NULL);
901 if (!fnic_io_req_cache) {
902 printk(KERN_ERR PFX "failed to create fnic io_req slab\n");
903 err = -ENOMEM;
904 goto err_create_fnic_ioreq_slab;
905 }
906
907 fnic_event_queue = create_singlethread_workqueue("fnic_event_wq");
908 if (!fnic_event_queue) {
909 printk(KERN_ERR PFX "fnic work queue create failed\n");
910 err = -ENOMEM;
911 goto err_create_fnic_workq;
912 }
913
914 spin_lock_init(&fnic_list_lock);
915 INIT_LIST_HEAD(&fnic_list);
916
917 fnic_fc_transport = fc_attach_transport(&fnic_fc_functions);
918 if (!fnic_fc_transport) {
919 printk(KERN_ERR PFX "fc_attach_transport error\n");
920 err = -ENOMEM;
921 goto err_fc_transport;
922 }
923
924 /* register the driver with PCI system */
925 err = pci_register_driver(&fnic_driver);
926 if (err < 0) {
927 printk(KERN_ERR PFX "pci register error\n");
928 goto err_pci_register;
929 }
930 return err;
931
932err_pci_register:
933 fc_release_transport(fnic_fc_transport);
934err_fc_transport:
935 destroy_workqueue(fnic_event_queue);
936err_create_fnic_workq:
937 kmem_cache_destroy(fnic_io_req_cache);
938err_create_fnic_ioreq_slab:
939 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
940err_create_fnic_sgl_slab_max:
941 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
942err_create_fnic_sgl_slab_dflt:
943 return err;
944}
945
946static void __exit fnic_cleanup_module(void)
947{
948 pci_unregister_driver(&fnic_driver);
949 destroy_workqueue(fnic_event_queue);
950 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
951 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
952 kmem_cache_destroy(fnic_io_req_cache);
953 fc_release_transport(fnic_fc_transport);
954}
955
956module_init(fnic_init_module);
957module_exit(fnic_cleanup_module);
958