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