]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/fnic/fnic_main.c
8c56fdc3a4560adf69a82332545a1ed768bde1e7
[karo-tx-linux.git] / drivers / scsi / fnic / fnic_main.c
1 /*
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>
21 #include <linux/slab.h>
22 #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>
29 #include <linux/if_ether.h>
30 #include <scsi/fc/fc_fip.h>
31 #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_fip.h"
43 #include "fnic.h"
44
45 #define PCI_DEVICE_ID_CISCO_FNIC        0x0045
46
47 /* Timer to poll notification area for events. Used for MSI interrupts */
48 #define FNIC_NOTIFY_TIMER_PERIOD        (2 * HZ)
49
50 static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES];
51 static struct kmem_cache *fnic_io_req_cache;
52 LIST_HEAD(fnic_list);
53 DEFINE_SPINLOCK(fnic_list_lock);
54
55 /* Supported devices by fnic module */
56 static struct pci_device_id fnic_id_table[] = {
57         { PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_FNIC) },
58         { 0, }
59 };
60
61 MODULE_DESCRIPTION(DRV_DESCRIPTION);
62 MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, "
63               "Joseph R. Eykholt <jeykholt@cisco.com>");
64 MODULE_LICENSE("GPL v2");
65 MODULE_VERSION(DRV_VERSION);
66 MODULE_DEVICE_TABLE(pci, fnic_id_table);
67
68 unsigned int fnic_log_level;
69 module_param(fnic_log_level, int, S_IRUGO|S_IWUSR);
70 MODULE_PARM_DESC(fnic_log_level, "bit mask of fnic logging levels");
71
72 unsigned int fnic_trace_max_pages = 16;
73 module_param(fnic_trace_max_pages, uint, S_IRUGO|S_IWUSR);
74 MODULE_PARM_DESC(fnic_trace_max_pages, "Total allocated memory pages "
75                                         "for fnic trace buffer");
76
77 unsigned int fnic_fc_trace_max_pages = 64;
78 module_param(fnic_fc_trace_max_pages, uint, S_IRUGO|S_IWUSR);
79 MODULE_PARM_DESC(fnic_fc_trace_max_pages,
80                  "Total allocated memory pages for fc trace buffer");
81
82 static unsigned int fnic_max_qdepth = FNIC_DFLT_QUEUE_DEPTH;
83 module_param(fnic_max_qdepth, uint, S_IRUGO|S_IWUSR);
84 MODULE_PARM_DESC(fnic_max_qdepth, "Queue depth to report for each LUN");
85
86 static struct libfc_function_template fnic_transport_template = {
87         .frame_send = fnic_send,
88         .lport_set_port_id = fnic_set_port_id,
89         .fcp_abort_io = fnic_empty_scsi_cleanup,
90         .fcp_cleanup = fnic_empty_scsi_cleanup,
91         .exch_mgr_reset = fnic_exch_mgr_reset
92 };
93
94 static int fnic_slave_alloc(struct scsi_device *sdev)
95 {
96         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
97
98         sdev->tagged_supported = 1;
99
100         if (!rport || fc_remote_port_chkready(rport))
101                 return -ENXIO;
102
103         scsi_activate_tcq(sdev, fnic_max_qdepth);
104         return 0;
105 }
106
107 static struct scsi_host_template fnic_host_template = {
108         .module = THIS_MODULE,
109         .name = DRV_NAME,
110         .queuecommand = fnic_queuecommand,
111         .eh_abort_handler = fnic_abort_cmd,
112         .eh_device_reset_handler = fnic_device_reset,
113         .eh_host_reset_handler = fnic_host_reset,
114         .slave_alloc = fnic_slave_alloc,
115         .change_queue_depth = fc_change_queue_depth,
116         .change_queue_type = fc_change_queue_type,
117         .this_id = -1,
118         .cmd_per_lun = 3,
119         .can_queue = FNIC_DFLT_IO_REQ,
120         .use_clustering = ENABLE_CLUSTERING,
121         .sg_tablesize = FNIC_MAX_SG_DESC_CNT,
122         .max_sectors = 0xffff,
123         .shost_attrs = fnic_attrs,
124 };
125
126 static void
127 fnic_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
128 {
129         if (timeout)
130                 rport->dev_loss_tmo = timeout;
131         else
132                 rport->dev_loss_tmo = 1;
133 }
134
135 static void fnic_get_host_speed(struct Scsi_Host *shost);
136 static struct scsi_transport_template *fnic_fc_transport;
137 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *);
138 static void fnic_reset_host_stats(struct Scsi_Host *);
139
140 static struct fc_function_template fnic_fc_functions = {
141
142         .show_host_node_name = 1,
143         .show_host_port_name = 1,
144         .show_host_supported_classes = 1,
145         .show_host_supported_fc4s = 1,
146         .show_host_active_fc4s = 1,
147         .show_host_maxframe_size = 1,
148         .show_host_port_id = 1,
149         .show_host_supported_speeds = 1,
150         .get_host_speed = fnic_get_host_speed,
151         .show_host_speed = 1,
152         .show_host_port_type = 1,
153         .get_host_port_state = fc_get_host_port_state,
154         .show_host_port_state = 1,
155         .show_host_symbolic_name = 1,
156         .show_rport_maxframe_size = 1,
157         .show_rport_supported_classes = 1,
158         .show_host_fabric_name = 1,
159         .show_starget_node_name = 1,
160         .show_starget_port_name = 1,
161         .show_starget_port_id = 1,
162         .show_rport_dev_loss_tmo = 1,
163         .set_rport_dev_loss_tmo = fnic_set_rport_dev_loss_tmo,
164         .issue_fc_host_lip = fnic_reset,
165         .get_fc_host_stats = fnic_get_stats,
166         .reset_fc_host_stats = fnic_reset_host_stats,
167         .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
168         .terminate_rport_io = fnic_terminate_rport_io,
169         .bsg_request = fc_lport_bsg_request,
170 };
171
172 static void fnic_get_host_speed(struct Scsi_Host *shost)
173 {
174         struct fc_lport *lp = shost_priv(shost);
175         struct fnic *fnic = lport_priv(lp);
176         u32 port_speed = vnic_dev_port_speed(fnic->vdev);
177
178         /* Add in other values as they get defined in fw */
179         switch (port_speed) {
180         case 10000:
181                 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
182                 break;
183         default:
184                 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
185                 break;
186         }
187 }
188
189 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host)
190 {
191         int ret;
192         struct fc_lport *lp = shost_priv(host);
193         struct fnic *fnic = lport_priv(lp);
194         struct fc_host_statistics *stats = &lp->host_stats;
195         struct vnic_stats *vs;
196         unsigned long flags;
197
198         if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT))
199                 return stats;
200         fnic->stats_time = jiffies;
201
202         spin_lock_irqsave(&fnic->fnic_lock, flags);
203         ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
204         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
205
206         if (ret) {
207                 FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host,
208                               "fnic: Get vnic stats failed"
209                               " 0x%x", ret);
210                 return stats;
211         }
212         vs = fnic->stats;
213         stats->tx_frames = vs->tx.tx_unicast_frames_ok;
214         stats->tx_words  = vs->tx.tx_unicast_bytes_ok / 4;
215         stats->rx_frames = vs->rx.rx_unicast_frames_ok;
216         stats->rx_words  = vs->rx.rx_unicast_bytes_ok / 4;
217         stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors;
218         stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop;
219         stats->invalid_crc_count = vs->rx.rx_crc_errors;
220         stats->seconds_since_last_reset =
221                         (jiffies - fnic->stats_reset_time) / HZ;
222         stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000);
223         stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000);
224
225         return stats;
226 }
227
228 /*
229  * fnic_dump_fchost_stats
230  * note : dumps fc_statistics into system logs
231  */
232 void fnic_dump_fchost_stats(struct Scsi_Host *host,
233                                 struct fc_host_statistics *stats)
234 {
235         FNIC_MAIN_NOTE(KERN_NOTICE, host,
236                         "fnic: seconds since last reset = %llu\n",
237                         stats->seconds_since_last_reset);
238         FNIC_MAIN_NOTE(KERN_NOTICE, host,
239                         "fnic: tx frames                = %llu\n",
240                         stats->tx_frames);
241         FNIC_MAIN_NOTE(KERN_NOTICE, host,
242                         "fnic: tx words         = %llu\n",
243                         stats->tx_words);
244         FNIC_MAIN_NOTE(KERN_NOTICE, host,
245                         "fnic: rx frames                = %llu\n",
246                         stats->rx_frames);
247         FNIC_MAIN_NOTE(KERN_NOTICE, host,
248                         "fnic: rx words         = %llu\n",
249                         stats->rx_words);
250         FNIC_MAIN_NOTE(KERN_NOTICE, host,
251                         "fnic: lip count                = %llu\n",
252                         stats->lip_count);
253         FNIC_MAIN_NOTE(KERN_NOTICE, host,
254                         "fnic: nos count                = %llu\n",
255                         stats->nos_count);
256         FNIC_MAIN_NOTE(KERN_NOTICE, host,
257                         "fnic: error frames             = %llu\n",
258                         stats->error_frames);
259         FNIC_MAIN_NOTE(KERN_NOTICE, host,
260                         "fnic: dumped frames    = %llu\n",
261                         stats->dumped_frames);
262         FNIC_MAIN_NOTE(KERN_NOTICE, host,
263                         "fnic: link failure count       = %llu\n",
264                         stats->link_failure_count);
265         FNIC_MAIN_NOTE(KERN_NOTICE, host,
266                         "fnic: loss of sync count       = %llu\n",
267                         stats->loss_of_sync_count);
268         FNIC_MAIN_NOTE(KERN_NOTICE, host,
269                         "fnic: loss of signal count     = %llu\n",
270                         stats->loss_of_signal_count);
271         FNIC_MAIN_NOTE(KERN_NOTICE, host,
272                         "fnic: prim seq protocol err count = %llu\n",
273                         stats->prim_seq_protocol_err_count);
274         FNIC_MAIN_NOTE(KERN_NOTICE, host,
275                         "fnic: invalid tx word count= %llu\n",
276                         stats->invalid_tx_word_count);
277         FNIC_MAIN_NOTE(KERN_NOTICE, host,
278                         "fnic: invalid crc count        = %llu\n",
279                         stats->invalid_crc_count);
280         FNIC_MAIN_NOTE(KERN_NOTICE, host,
281                         "fnic: fcp input requests       = %llu\n",
282                         stats->fcp_input_requests);
283         FNIC_MAIN_NOTE(KERN_NOTICE, host,
284                         "fnic: fcp output requests      = %llu\n",
285                         stats->fcp_output_requests);
286         FNIC_MAIN_NOTE(KERN_NOTICE, host,
287                         "fnic: fcp control requests     = %llu\n",
288                         stats->fcp_control_requests);
289         FNIC_MAIN_NOTE(KERN_NOTICE, host,
290                         "fnic: fcp input megabytes      = %llu\n",
291                         stats->fcp_input_megabytes);
292         FNIC_MAIN_NOTE(KERN_NOTICE, host,
293                         "fnic: fcp output megabytes     = %llu\n",
294                         stats->fcp_output_megabytes);
295         return;
296 }
297
298 /*
299  * fnic_reset_host_stats : clears host stats
300  * note : called when reset_statistics set under sysfs dir
301  */
302 static void fnic_reset_host_stats(struct Scsi_Host *host)
303 {
304         int ret;
305         struct fc_lport *lp = shost_priv(host);
306         struct fnic *fnic = lport_priv(lp);
307         struct fc_host_statistics *stats;
308         unsigned long flags;
309
310         /* dump current stats, before clearing them */
311         stats = fnic_get_stats(host);
312         fnic_dump_fchost_stats(host, stats);
313
314         spin_lock_irqsave(&fnic->fnic_lock, flags);
315         ret = vnic_dev_stats_clear(fnic->vdev);
316         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
317
318         if (ret) {
319                 FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host,
320                                 "fnic: Reset vnic stats failed"
321                                 " 0x%x", ret);
322                 return;
323         }
324         fnic->stats_reset_time = jiffies;
325         memset(stats, 0, sizeof(*stats));
326
327         return;
328 }
329
330 void fnic_log_q_error(struct fnic *fnic)
331 {
332         unsigned int i;
333         u32 error_status;
334
335         for (i = 0; i < fnic->raw_wq_count; i++) {
336                 error_status = ioread32(&fnic->wq[i].ctrl->error_status);
337                 if (error_status)
338                         shost_printk(KERN_ERR, fnic->lport->host,
339                                      "WQ[%d] error_status"
340                                      " %d\n", i, error_status);
341         }
342
343         for (i = 0; i < fnic->rq_count; i++) {
344                 error_status = ioread32(&fnic->rq[i].ctrl->error_status);
345                 if (error_status)
346                         shost_printk(KERN_ERR, fnic->lport->host,
347                                      "RQ[%d] error_status"
348                                      " %d\n", i, error_status);
349         }
350
351         for (i = 0; i < fnic->wq_copy_count; i++) {
352                 error_status = ioread32(&fnic->wq_copy[i].ctrl->error_status);
353                 if (error_status)
354                         shost_printk(KERN_ERR, fnic->lport->host,
355                                      "CWQ[%d] error_status"
356                                      " %d\n", i, error_status);
357         }
358 }
359
360 void fnic_handle_link_event(struct fnic *fnic)
361 {
362         unsigned long flags;
363
364         spin_lock_irqsave(&fnic->fnic_lock, flags);
365         if (fnic->stop_rx_link_events) {
366                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
367                 return;
368         }
369         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
370
371         queue_work(fnic_event_queue, &fnic->link_work);
372
373 }
374
375 static int fnic_notify_set(struct fnic *fnic)
376 {
377         int err;
378
379         switch (vnic_dev_get_intr_mode(fnic->vdev)) {
380         case VNIC_DEV_INTR_MODE_INTX:
381                 err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY);
382                 break;
383         case VNIC_DEV_INTR_MODE_MSI:
384                 err = vnic_dev_notify_set(fnic->vdev, -1);
385                 break;
386         case VNIC_DEV_INTR_MODE_MSIX:
387                 err = vnic_dev_notify_set(fnic->vdev, FNIC_MSIX_ERR_NOTIFY);
388                 break;
389         default:
390                 shost_printk(KERN_ERR, fnic->lport->host,
391                              "Interrupt mode should be set up"
392                              " before devcmd notify set %d\n",
393                              vnic_dev_get_intr_mode(fnic->vdev));
394                 err = -1;
395                 break;
396         }
397
398         return err;
399 }
400
401 static void fnic_notify_timer(unsigned long data)
402 {
403         struct fnic *fnic = (struct fnic *)data;
404
405         fnic_handle_link_event(fnic);
406         mod_timer(&fnic->notify_timer,
407                   round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD));
408 }
409
410 static void fnic_fip_notify_timer(unsigned long data)
411 {
412         struct fnic *fnic = (struct fnic *)data;
413
414         fnic_handle_fip_timer(fnic);
415 }
416
417 static void fnic_notify_timer_start(struct fnic *fnic)
418 {
419         switch (vnic_dev_get_intr_mode(fnic->vdev)) {
420         case VNIC_DEV_INTR_MODE_MSI:
421                 /*
422                  * Schedule first timeout immediately. The driver is
423                  * initiatialized and ready to look for link up notification
424                  */
425                 mod_timer(&fnic->notify_timer, jiffies);
426                 break;
427         default:
428                 /* Using intr for notification for INTx/MSI-X */
429                 break;
430         };
431 }
432
433 static int fnic_dev_wait(struct vnic_dev *vdev,
434                          int (*start)(struct vnic_dev *, int),
435                          int (*finished)(struct vnic_dev *, int *),
436                          int arg)
437 {
438         unsigned long time;
439         int done;
440         int err;
441
442         err = start(vdev, arg);
443         if (err)
444                 return err;
445
446         /* Wait for func to complete...2 seconds max */
447         time = jiffies + (HZ * 2);
448         do {
449                 err = finished(vdev, &done);
450                 if (err)
451                         return err;
452                 if (done)
453                         return 0;
454                 schedule_timeout_uninterruptible(HZ / 10);
455         } while (time_after(time, jiffies));
456
457         return -ETIMEDOUT;
458 }
459
460 static int fnic_cleanup(struct fnic *fnic)
461 {
462         unsigned int i;
463         int err;
464
465         vnic_dev_disable(fnic->vdev);
466         for (i = 0; i < fnic->intr_count; i++)
467                 vnic_intr_mask(&fnic->intr[i]);
468
469         for (i = 0; i < fnic->rq_count; i++) {
470                 err = vnic_rq_disable(&fnic->rq[i]);
471                 if (err)
472                         return err;
473         }
474         for (i = 0; i < fnic->raw_wq_count; i++) {
475                 err = vnic_wq_disable(&fnic->wq[i]);
476                 if (err)
477                         return err;
478         }
479         for (i = 0; i < fnic->wq_copy_count; i++) {
480                 err = vnic_wq_copy_disable(&fnic->wq_copy[i]);
481                 if (err)
482                         return err;
483         }
484
485         /* Clean up completed IOs and FCS frames */
486         fnic_wq_copy_cmpl_handler(fnic, -1);
487         fnic_wq_cmpl_handler(fnic, -1);
488         fnic_rq_cmpl_handler(fnic, -1);
489
490         /* Clean up the IOs and FCS frames that have not completed */
491         for (i = 0; i < fnic->raw_wq_count; i++)
492                 vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf);
493         for (i = 0; i < fnic->rq_count; i++)
494                 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
495         for (i = 0; i < fnic->wq_copy_count; i++)
496                 vnic_wq_copy_clean(&fnic->wq_copy[i],
497                                    fnic_wq_copy_cleanup_handler);
498
499         for (i = 0; i < fnic->cq_count; i++)
500                 vnic_cq_clean(&fnic->cq[i]);
501         for (i = 0; i < fnic->intr_count; i++)
502                 vnic_intr_clean(&fnic->intr[i]);
503
504         mempool_destroy(fnic->io_req_pool);
505         for (i = 0; i < FNIC_SGL_NUM_CACHES; i++)
506                 mempool_destroy(fnic->io_sgl_pool[i]);
507
508         return 0;
509 }
510
511 static void fnic_iounmap(struct fnic *fnic)
512 {
513         if (fnic->bar0.vaddr)
514                 iounmap(fnic->bar0.vaddr);
515 }
516
517 /**
518  * fnic_get_mac() - get assigned data MAC address for FIP code.
519  * @lport:      local port.
520  */
521 static u8 *fnic_get_mac(struct fc_lport *lport)
522 {
523         struct fnic *fnic = lport_priv(lport);
524
525         return fnic->data_src_addr;
526 }
527
528 static void fnic_set_vlan(struct fnic *fnic, u16 vlan_id)
529 {
530         u16 old_vlan;
531         old_vlan = vnic_dev_set_default_vlan(fnic->vdev, vlan_id);
532 }
533
534 static int fnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
535 {
536         struct Scsi_Host *host;
537         struct fc_lport *lp;
538         struct fnic *fnic;
539         mempool_t *pool;
540         int err;
541         int i;
542         unsigned long flags;
543
544         /*
545          * Allocate SCSI Host and set up association between host,
546          * local port, and fnic
547          */
548         lp = libfc_host_alloc(&fnic_host_template, sizeof(struct fnic));
549         if (!lp) {
550                 printk(KERN_ERR PFX "Unable to alloc libfc local port\n");
551                 err = -ENOMEM;
552                 goto err_out;
553         }
554         host = lp->host;
555         fnic = lport_priv(lp);
556         fnic->lport = lp;
557         fnic->ctlr.lp = lp;
558
559         snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME,
560                  host->host_no);
561
562         host->transportt = fnic_fc_transport;
563
564         err = fnic_stats_debugfs_init(fnic);
565         if (err) {
566                 shost_printk(KERN_ERR, fnic->lport->host,
567                                 "Failed to initialize debugfs for stats\n");
568                 fnic_stats_debugfs_remove(fnic);
569         }
570
571         /* Setup PCI resources */
572         pci_set_drvdata(pdev, fnic);
573
574         fnic->pdev = pdev;
575
576         err = pci_enable_device(pdev);
577         if (err) {
578                 shost_printk(KERN_ERR, fnic->lport->host,
579                              "Cannot enable PCI device, aborting.\n");
580                 goto err_out_free_hba;
581         }
582
583         err = pci_request_regions(pdev, DRV_NAME);
584         if (err) {
585                 shost_printk(KERN_ERR, fnic->lport->host,
586                              "Cannot enable PCI resources, aborting\n");
587                 goto err_out_disable_device;
588         }
589
590         pci_set_master(pdev);
591
592         /* Query PCI controller on system for DMA addressing
593          * limitation for the device.  Try 64-bit first, and
594          * fail to 32-bit.
595          */
596         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
597         if (err) {
598                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
599                 if (err) {
600                         shost_printk(KERN_ERR, fnic->lport->host,
601                                      "No usable DMA configuration "
602                                      "aborting\n");
603                         goto err_out_release_regions;
604                 }
605                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
606                 if (err) {
607                         shost_printk(KERN_ERR, fnic->lport->host,
608                                      "Unable to obtain 32-bit DMA "
609                                      "for consistent allocations, aborting.\n");
610                         goto err_out_release_regions;
611                 }
612         } else {
613                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
614                 if (err) {
615                         shost_printk(KERN_ERR, fnic->lport->host,
616                                      "Unable to obtain 64-bit DMA "
617                                      "for consistent allocations, aborting.\n");
618                         goto err_out_release_regions;
619                 }
620         }
621
622         /* Map vNIC resources from BAR0 */
623         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
624                 shost_printk(KERN_ERR, fnic->lport->host,
625                              "BAR0 not memory-map'able, aborting.\n");
626                 err = -ENODEV;
627                 goto err_out_release_regions;
628         }
629
630         fnic->bar0.vaddr = pci_iomap(pdev, 0, 0);
631         fnic->bar0.bus_addr = pci_resource_start(pdev, 0);
632         fnic->bar0.len = pci_resource_len(pdev, 0);
633
634         if (!fnic->bar0.vaddr) {
635                 shost_printk(KERN_ERR, fnic->lport->host,
636                              "Cannot memory-map BAR0 res hdr, "
637                              "aborting.\n");
638                 err = -ENODEV;
639                 goto err_out_release_regions;
640         }
641
642         fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0);
643         if (!fnic->vdev) {
644                 shost_printk(KERN_ERR, fnic->lport->host,
645                              "vNIC registration failed, "
646                              "aborting.\n");
647                 err = -ENODEV;
648                 goto err_out_iounmap;
649         }
650
651         err = fnic_dev_wait(fnic->vdev, vnic_dev_open,
652                             vnic_dev_open_done, 0);
653         if (err) {
654                 shost_printk(KERN_ERR, fnic->lport->host,
655                              "vNIC dev open failed, aborting.\n");
656                 goto err_out_vnic_unregister;
657         }
658
659         err = vnic_dev_init(fnic->vdev, 0);
660         if (err) {
661                 shost_printk(KERN_ERR, fnic->lport->host,
662                              "vNIC dev init failed, aborting.\n");
663                 goto err_out_dev_close;
664         }
665
666         err = vnic_dev_mac_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
667         if (err) {
668                 shost_printk(KERN_ERR, fnic->lport->host,
669                              "vNIC get MAC addr failed \n");
670                 goto err_out_dev_close;
671         }
672         /* set data_src for point-to-point mode and to keep it non-zero */
673         memcpy(fnic->data_src_addr, fnic->ctlr.ctl_src_addr, ETH_ALEN);
674
675         /* Get vNIC configuration */
676         err = fnic_get_vnic_config(fnic);
677         if (err) {
678                 shost_printk(KERN_ERR, fnic->lport->host,
679                              "Get vNIC configuration failed, "
680                              "aborting.\n");
681                 goto err_out_dev_close;
682         }
683
684         /* Configure Maximum Outstanding IO reqs*/
685         if (fnic->config.io_throttle_count != FNIC_UCSM_DFLT_THROTTLE_CNT_BLD) {
686                 host->can_queue = min_t(u32, FNIC_MAX_IO_REQ,
687                                         max_t(u32, FNIC_MIN_IO_REQ,
688                                         fnic->config.io_throttle_count));
689         }
690         fnic->fnic_max_tag_id = host->can_queue;
691
692         err = scsi_init_shared_tag_map(host, fnic->fnic_max_tag_id);
693         if (err) {
694                 shost_printk(KERN_ERR, fnic->lport->host,
695                           "Unable to alloc shared tag map\n");
696                 goto err_out_dev_close;
697         }
698
699         host->max_lun = fnic->config.luns_per_tgt;
700         host->max_id = FNIC_MAX_FCP_TARGET;
701         host->max_cmd_len = FCOE_MAX_CMD_LEN;
702
703         fnic_get_res_counts(fnic);
704
705         err = fnic_set_intr_mode(fnic);
706         if (err) {
707                 shost_printk(KERN_ERR, fnic->lport->host,
708                              "Failed to set intr mode, "
709                              "aborting.\n");
710                 goto err_out_dev_close;
711         }
712
713         err = fnic_alloc_vnic_resources(fnic);
714         if (err) {
715                 shost_printk(KERN_ERR, fnic->lport->host,
716                              "Failed to alloc vNIC resources, "
717                              "aborting.\n");
718                 goto err_out_clear_intr;
719         }
720
721
722         /* initialize all fnic locks */
723         spin_lock_init(&fnic->fnic_lock);
724
725         for (i = 0; i < FNIC_WQ_MAX; i++)
726                 spin_lock_init(&fnic->wq_lock[i]);
727
728         for (i = 0; i < FNIC_WQ_COPY_MAX; i++) {
729                 spin_lock_init(&fnic->wq_copy_lock[i]);
730                 fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK;
731                 fnic->fw_ack_recd[i] = 0;
732                 fnic->fw_ack_index[i] = -1;
733         }
734
735         for (i = 0; i < FNIC_IO_LOCKS; i++)
736                 spin_lock_init(&fnic->io_req_lock[i]);
737
738         fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache);
739         if (!fnic->io_req_pool)
740                 goto err_out_free_resources;
741
742         pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
743         if (!pool)
744                 goto err_out_free_ioreq_pool;
745         fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool;
746
747         pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
748         if (!pool)
749                 goto err_out_free_dflt_pool;
750         fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool;
751
752         /* setup vlan config, hw inserts vlan header */
753         fnic->vlan_hw_insert = 1;
754         fnic->vlan_id = 0;
755
756         /* Initialize the FIP fcoe_ctrl struct */
757         fnic->ctlr.send = fnic_eth_send;
758         fnic->ctlr.update_mac = fnic_update_mac;
759         fnic->ctlr.get_src_addr = fnic_get_mac;
760         if (fnic->config.flags & VFCF_FIP_CAPABLE) {
761                 shost_printk(KERN_INFO, fnic->lport->host,
762                              "firmware supports FIP\n");
763                 /* enable directed and multicast */
764                 vnic_dev_packet_filter(fnic->vdev, 1, 1, 0, 0, 0);
765                 vnic_dev_add_addr(fnic->vdev, FIP_ALL_ENODE_MACS);
766                 vnic_dev_add_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
767                 fnic->set_vlan = fnic_set_vlan;
768                 fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_AUTO);
769                 setup_timer(&fnic->fip_timer, fnic_fip_notify_timer,
770                                                         (unsigned long)fnic);
771                 spin_lock_init(&fnic->vlans_lock);
772                 INIT_WORK(&fnic->fip_frame_work, fnic_handle_fip_frame);
773                 INIT_WORK(&fnic->event_work, fnic_handle_event);
774                 skb_queue_head_init(&fnic->fip_frame_queue);
775                 INIT_LIST_HEAD(&fnic->evlist);
776                 INIT_LIST_HEAD(&fnic->vlans);
777         } else {
778                 shost_printk(KERN_INFO, fnic->lport->host,
779                              "firmware uses non-FIP mode\n");
780                 fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_NON_FIP);
781                 fnic->ctlr.state = FIP_ST_NON_FIP;
782         }
783         fnic->state = FNIC_IN_FC_MODE;
784
785         atomic_set(&fnic->in_flight, 0);
786         fnic->state_flags = FNIC_FLAGS_NONE;
787
788         /* Enable hardware stripping of vlan header on ingress */
789         fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1);
790
791         /* Setup notification buffer area */
792         err = fnic_notify_set(fnic);
793         if (err) {
794                 shost_printk(KERN_ERR, fnic->lport->host,
795                              "Failed to alloc notify buffer, aborting.\n");
796                 goto err_out_free_max_pool;
797         }
798
799         /* Setup notify timer when using MSI interrupts */
800         if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
801                 setup_timer(&fnic->notify_timer,
802                             fnic_notify_timer, (unsigned long)fnic);
803
804         /* allocate RQ buffers and post them to RQ*/
805         for (i = 0; i < fnic->rq_count; i++) {
806                 err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
807                 if (err) {
808                         shost_printk(KERN_ERR, fnic->lport->host,
809                                      "fnic_alloc_rq_frame can't alloc "
810                                      "frame\n");
811                         goto err_out_free_rq_buf;
812                 }
813         }
814
815         /*
816          * Initialization done with PCI system, hardware, firmware.
817          * Add host to SCSI
818          */
819         err = scsi_add_host(lp->host, &pdev->dev);
820         if (err) {
821                 shost_printk(KERN_ERR, fnic->lport->host,
822                              "fnic: scsi_add_host failed...exiting\n");
823                 goto err_out_free_rq_buf;
824         }
825
826         /* Start local port initiatialization */
827
828         lp->link_up = 0;
829
830         lp->max_retry_count = fnic->config.flogi_retries;
831         lp->max_rport_retry_count = fnic->config.plogi_retries;
832         lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
833                               FCP_SPPF_CONF_COMPL);
834         if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR)
835                 lp->service_params |= FCP_SPPF_RETRY;
836
837         lp->boot_time = jiffies;
838         lp->e_d_tov = fnic->config.ed_tov;
839         lp->r_a_tov = fnic->config.ra_tov;
840         lp->link_supported_speeds = FC_PORTSPEED_10GBIT;
841         fc_set_wwnn(lp, fnic->config.node_wwn);
842         fc_set_wwpn(lp, fnic->config.port_wwn);
843
844         fcoe_libfc_config(lp, &fnic->ctlr, &fnic_transport_template, 0);
845
846         if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCPIO_HOST_EXCH_RANGE_START,
847                                FCPIO_HOST_EXCH_RANGE_END, NULL)) {
848                 err = -ENOMEM;
849                 goto err_out_remove_scsi_host;
850         }
851
852         fc_lport_init_stats(lp);
853         fnic->stats_reset_time = jiffies;
854
855         fc_lport_config(lp);
856
857         if (fc_set_mfs(lp, fnic->config.maxdatafieldsize +
858                        sizeof(struct fc_frame_header))) {
859                 err = -EINVAL;
860                 goto err_out_free_exch_mgr;
861         }
862         fc_host_maxframe_size(lp->host) = lp->mfs;
863         fc_host_dev_loss_tmo(lp->host) = fnic->config.port_down_timeout / 1000;
864
865         sprintf(fc_host_symbolic_name(lp->host),
866                 DRV_NAME " v" DRV_VERSION " over %s", fnic->name);
867
868         spin_lock_irqsave(&fnic_list_lock, flags);
869         list_add_tail(&fnic->list, &fnic_list);
870         spin_unlock_irqrestore(&fnic_list_lock, flags);
871
872         INIT_WORK(&fnic->link_work, fnic_handle_link);
873         INIT_WORK(&fnic->frame_work, fnic_handle_frame);
874         skb_queue_head_init(&fnic->frame_queue);
875         skb_queue_head_init(&fnic->tx_queue);
876
877         /* Enable all queues */
878         for (i = 0; i < fnic->raw_wq_count; i++)
879                 vnic_wq_enable(&fnic->wq[i]);
880         for (i = 0; i < fnic->rq_count; i++)
881                 vnic_rq_enable(&fnic->rq[i]);
882         for (i = 0; i < fnic->wq_copy_count; i++)
883                 vnic_wq_copy_enable(&fnic->wq_copy[i]);
884
885         fc_fabric_login(lp);
886
887         vnic_dev_enable(fnic->vdev);
888
889         err = fnic_request_intr(fnic);
890         if (err) {
891                 shost_printk(KERN_ERR, fnic->lport->host,
892                              "Unable to request irq.\n");
893                 goto err_out_free_exch_mgr;
894         }
895
896         for (i = 0; i < fnic->intr_count; i++)
897                 vnic_intr_unmask(&fnic->intr[i]);
898
899         fnic_notify_timer_start(fnic);
900
901         return 0;
902
903 err_out_free_exch_mgr:
904         fc_exch_mgr_free(lp);
905 err_out_remove_scsi_host:
906         fc_remove_host(lp->host);
907         scsi_remove_host(lp->host);
908 err_out_free_rq_buf:
909         for (i = 0; i < fnic->rq_count; i++)
910                 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
911         vnic_dev_notify_unset(fnic->vdev);
912 err_out_free_max_pool:
913         mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]);
914 err_out_free_dflt_pool:
915         mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]);
916 err_out_free_ioreq_pool:
917         mempool_destroy(fnic->io_req_pool);
918 err_out_free_resources:
919         fnic_free_vnic_resources(fnic);
920 err_out_clear_intr:
921         fnic_clear_intr_mode(fnic);
922 err_out_dev_close:
923         vnic_dev_close(fnic->vdev);
924 err_out_vnic_unregister:
925         vnic_dev_unregister(fnic->vdev);
926 err_out_iounmap:
927         fnic_iounmap(fnic);
928 err_out_release_regions:
929         pci_release_regions(pdev);
930 err_out_disable_device:
931         pci_disable_device(pdev);
932 err_out_free_hba:
933         fnic_stats_debugfs_remove(fnic);
934         scsi_host_put(lp->host);
935 err_out:
936         return err;
937 }
938
939 static void fnic_remove(struct pci_dev *pdev)
940 {
941         struct fnic *fnic = pci_get_drvdata(pdev);
942         struct fc_lport *lp = fnic->lport;
943         unsigned long flags;
944
945         /*
946          * Mark state so that the workqueue thread stops forwarding
947          * received frames and link events to the local port. ISR and
948          * other threads that can queue work items will also stop
949          * creating work items on the fnic workqueue
950          */
951         spin_lock_irqsave(&fnic->fnic_lock, flags);
952         fnic->stop_rx_link_events = 1;
953         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
954
955         if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
956                 del_timer_sync(&fnic->notify_timer);
957
958         /*
959          * Flush the fnic event queue. After this call, there should
960          * be no event queued for this fnic device in the workqueue
961          */
962         flush_workqueue(fnic_event_queue);
963         skb_queue_purge(&fnic->frame_queue);
964         skb_queue_purge(&fnic->tx_queue);
965
966         if (fnic->config.flags & VFCF_FIP_CAPABLE) {
967                 del_timer_sync(&fnic->fip_timer);
968                 skb_queue_purge(&fnic->fip_frame_queue);
969                 fnic_fcoe_reset_vlans(fnic);
970                 fnic_fcoe_evlist_free(fnic);
971         }
972
973         /*
974          * Log off the fabric. This stops all remote ports, dns port,
975          * logs off the fabric. This flushes all rport, disc, lport work
976          * before returning
977          */
978         fc_fabric_logoff(fnic->lport);
979
980         spin_lock_irqsave(&fnic->fnic_lock, flags);
981         fnic->in_remove = 1;
982         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
983
984         fcoe_ctlr_destroy(&fnic->ctlr);
985         fc_lport_destroy(lp);
986         fnic_stats_debugfs_remove(fnic);
987
988         /*
989          * This stops the fnic device, masks all interrupts. Completed
990          * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
991          * cleaned up
992          */
993         fnic_cleanup(fnic);
994
995         BUG_ON(!skb_queue_empty(&fnic->frame_queue));
996         BUG_ON(!skb_queue_empty(&fnic->tx_queue));
997
998         spin_lock_irqsave(&fnic_list_lock, flags);
999         list_del(&fnic->list);
1000         spin_unlock_irqrestore(&fnic_list_lock, flags);
1001
1002         fc_remove_host(fnic->lport->host);
1003         scsi_remove_host(fnic->lport->host);
1004         fc_exch_mgr_free(fnic->lport);
1005         vnic_dev_notify_unset(fnic->vdev);
1006         fnic_free_intr(fnic);
1007         fnic_free_vnic_resources(fnic);
1008         fnic_clear_intr_mode(fnic);
1009         vnic_dev_close(fnic->vdev);
1010         vnic_dev_unregister(fnic->vdev);
1011         fnic_iounmap(fnic);
1012         pci_release_regions(pdev);
1013         pci_disable_device(pdev);
1014         scsi_host_put(lp->host);
1015 }
1016
1017 static struct pci_driver fnic_driver = {
1018         .name = DRV_NAME,
1019         .id_table = fnic_id_table,
1020         .probe = fnic_probe,
1021         .remove = fnic_remove,
1022 };
1023
1024 static int __init fnic_init_module(void)
1025 {
1026         size_t len;
1027         int err = 0;
1028
1029         printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
1030
1031         /* Create debugfs entries for fnic */
1032         err = fnic_debugfs_init();
1033         if (err < 0) {
1034                 printk(KERN_ERR PFX "Failed to create fnic directory "
1035                                 "for tracing and stats logging\n");
1036                 fnic_debugfs_terminate();
1037         }
1038
1039         /* Allocate memory for trace buffer */
1040         err = fnic_trace_buf_init();
1041         if (err < 0) {
1042                 printk(KERN_ERR PFX
1043                        "Trace buffer initialization Failed. "
1044                        "Fnic Tracing utility is disabled\n");
1045                 fnic_trace_free();
1046         }
1047
1048     /* Allocate memory for fc trace buffer */
1049         err = fnic_fc_trace_init();
1050         if (err < 0) {
1051                 printk(KERN_ERR PFX "FC trace buffer initialization Failed "
1052                        "FC frame tracing utility is disabled\n");
1053                 fnic_fc_trace_free();
1054         }
1055
1056         /* Create a cache for allocation of default size sgls */
1057         len = sizeof(struct fnic_dflt_sgl_list);
1058         fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create
1059                 ("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
1060                  SLAB_HWCACHE_ALIGN,
1061                  NULL);
1062         if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) {
1063                 printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n");
1064                 err = -ENOMEM;
1065                 goto err_create_fnic_sgl_slab_dflt;
1066         }
1067
1068         /* Create a cache for allocation of max size sgls*/
1069         len = sizeof(struct fnic_sgl_list);
1070         fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create
1071                 ("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
1072                   SLAB_HWCACHE_ALIGN,
1073                   NULL);
1074         if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) {
1075                 printk(KERN_ERR PFX "failed to create fnic max sgl slab\n");
1076                 err = -ENOMEM;
1077                 goto err_create_fnic_sgl_slab_max;
1078         }
1079
1080         /* Create a cache of io_req structs for use via mempool */
1081         fnic_io_req_cache = kmem_cache_create("fnic_io_req",
1082                                               sizeof(struct fnic_io_req),
1083                                               0, SLAB_HWCACHE_ALIGN, NULL);
1084         if (!fnic_io_req_cache) {
1085                 printk(KERN_ERR PFX "failed to create fnic io_req slab\n");
1086                 err = -ENOMEM;
1087                 goto err_create_fnic_ioreq_slab;
1088         }
1089
1090         fnic_event_queue = create_singlethread_workqueue("fnic_event_wq");
1091         if (!fnic_event_queue) {
1092                 printk(KERN_ERR PFX "fnic work queue create failed\n");
1093                 err = -ENOMEM;
1094                 goto err_create_fnic_workq;
1095         }
1096
1097         spin_lock_init(&fnic_list_lock);
1098         INIT_LIST_HEAD(&fnic_list);
1099
1100         fnic_fip_queue = create_singlethread_workqueue("fnic_fip_q");
1101         if (!fnic_fip_queue) {
1102                 printk(KERN_ERR PFX "fnic FIP work queue create failed\n");
1103                 err = -ENOMEM;
1104                 goto err_create_fip_workq;
1105         }
1106
1107         fnic_fc_transport = fc_attach_transport(&fnic_fc_functions);
1108         if (!fnic_fc_transport) {
1109                 printk(KERN_ERR PFX "fc_attach_transport error\n");
1110                 err = -ENOMEM;
1111                 goto err_fc_transport;
1112         }
1113
1114         /* register the driver with PCI system */
1115         err = pci_register_driver(&fnic_driver);
1116         if (err < 0) {
1117                 printk(KERN_ERR PFX "pci register error\n");
1118                 goto err_pci_register;
1119         }
1120         return err;
1121
1122 err_pci_register:
1123         fc_release_transport(fnic_fc_transport);
1124 err_fc_transport:
1125         destroy_workqueue(fnic_fip_queue);
1126 err_create_fip_workq:
1127         destroy_workqueue(fnic_event_queue);
1128 err_create_fnic_workq:
1129         kmem_cache_destroy(fnic_io_req_cache);
1130 err_create_fnic_ioreq_slab:
1131         kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
1132 err_create_fnic_sgl_slab_max:
1133         kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
1134 err_create_fnic_sgl_slab_dflt:
1135         fnic_trace_free();
1136         fnic_fc_trace_free();
1137         fnic_debugfs_terminate();
1138         return err;
1139 }
1140
1141 static void __exit fnic_cleanup_module(void)
1142 {
1143         pci_unregister_driver(&fnic_driver);
1144         destroy_workqueue(fnic_event_queue);
1145         if (fnic_fip_queue) {
1146                 flush_workqueue(fnic_fip_queue);
1147                 destroy_workqueue(fnic_fip_queue);
1148         }
1149         kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
1150         kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
1151         kmem_cache_destroy(fnic_io_req_cache);
1152         fc_release_transport(fnic_fc_transport);
1153         fnic_trace_free();
1154         fnic_fc_trace_free();
1155         fnic_debugfs_terminate();
1156 }
1157
1158 module_init(fnic_init_module);
1159 module_exit(fnic_cleanup_module);
1160