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