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