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