2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
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.
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
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>
39 #include "vnic_intr.h"
40 #include "vnic_stats.h"
45 #define PCI_DEVICE_ID_CISCO_FNIC 0x0045
47 /* Timer to poll notification area for events. Used for MSI interrupts */
48 #define FNIC_NOTIFY_TIMER_PERIOD (2 * HZ)
50 static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES];
51 static struct kmem_cache *fnic_io_req_cache;
53 DEFINE_SPINLOCK(fnic_list_lock);
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) },
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);
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");
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");
77 static struct libfc_function_template fnic_transport_template = {
78 .frame_send = fnic_send,
79 .lport_set_port_id = fnic_set_port_id,
80 .fcp_abort_io = fnic_empty_scsi_cleanup,
81 .fcp_cleanup = fnic_empty_scsi_cleanup,
82 .exch_mgr_reset = fnic_exch_mgr_reset
85 static int fnic_slave_alloc(struct scsi_device *sdev)
87 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
89 sdev->tagged_supported = 1;
91 if (!rport || fc_remote_port_chkready(rport))
94 scsi_activate_tcq(sdev, FNIC_DFLT_QUEUE_DEPTH);
98 static struct scsi_host_template fnic_host_template = {
99 .module = THIS_MODULE,
101 .queuecommand = fnic_queuecommand,
102 .eh_abort_handler = fnic_abort_cmd,
103 .eh_device_reset_handler = fnic_device_reset,
104 .eh_host_reset_handler = fnic_host_reset,
105 .slave_alloc = fnic_slave_alloc,
106 .change_queue_depth = fc_change_queue_depth,
107 .change_queue_type = fc_change_queue_type,
110 .can_queue = FNIC_MAX_IO_REQ,
111 .use_clustering = ENABLE_CLUSTERING,
112 .sg_tablesize = FNIC_MAX_SG_DESC_CNT,
113 .max_sectors = 0xffff,
114 .shost_attrs = fnic_attrs,
118 fnic_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
121 rport->dev_loss_tmo = timeout;
123 rport->dev_loss_tmo = 1;
126 static void fnic_get_host_speed(struct Scsi_Host *shost);
127 static struct scsi_transport_template *fnic_fc_transport;
128 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *);
130 static struct fc_function_template fnic_fc_functions = {
132 .show_host_node_name = 1,
133 .show_host_port_name = 1,
134 .show_host_supported_classes = 1,
135 .show_host_supported_fc4s = 1,
136 .show_host_active_fc4s = 1,
137 .show_host_maxframe_size = 1,
138 .show_host_port_id = 1,
139 .show_host_supported_speeds = 1,
140 .get_host_speed = fnic_get_host_speed,
141 .show_host_speed = 1,
142 .show_host_port_type = 1,
143 .get_host_port_state = fc_get_host_port_state,
144 .show_host_port_state = 1,
145 .show_host_symbolic_name = 1,
146 .show_rport_maxframe_size = 1,
147 .show_rport_supported_classes = 1,
148 .show_host_fabric_name = 1,
149 .show_starget_node_name = 1,
150 .show_starget_port_name = 1,
151 .show_starget_port_id = 1,
152 .show_rport_dev_loss_tmo = 1,
153 .set_rport_dev_loss_tmo = fnic_set_rport_dev_loss_tmo,
154 .issue_fc_host_lip = fnic_reset,
155 .get_fc_host_stats = fnic_get_stats,
156 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
157 .terminate_rport_io = fnic_terminate_rport_io,
158 .bsg_request = fc_lport_bsg_request,
161 static void fnic_get_host_speed(struct Scsi_Host *shost)
163 struct fc_lport *lp = shost_priv(shost);
164 struct fnic *fnic = lport_priv(lp);
165 u32 port_speed = vnic_dev_port_speed(fnic->vdev);
167 /* Add in other values as they get defined in fw */
168 switch (port_speed) {
170 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
173 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
178 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host)
181 struct fc_lport *lp = shost_priv(host);
182 struct fnic *fnic = lport_priv(lp);
183 struct fc_host_statistics *stats = &lp->host_stats;
184 struct vnic_stats *vs;
187 if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT))
189 fnic->stats_time = jiffies;
191 spin_lock_irqsave(&fnic->fnic_lock, flags);
192 ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
193 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
196 FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host,
197 "fnic: Get vnic stats failed"
202 stats->tx_frames = vs->tx.tx_unicast_frames_ok;
203 stats->tx_words = vs->tx.tx_unicast_bytes_ok / 4;
204 stats->rx_frames = vs->rx.rx_unicast_frames_ok;
205 stats->rx_words = vs->rx.rx_unicast_bytes_ok / 4;
206 stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors;
207 stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop;
208 stats->invalid_crc_count = vs->rx.rx_crc_errors;
209 stats->seconds_since_last_reset = (jiffies - lp->boot_time) / HZ;
210 stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000);
211 stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000);
216 void fnic_log_q_error(struct fnic *fnic)
221 for (i = 0; i < fnic->raw_wq_count; i++) {
222 error_status = ioread32(&fnic->wq[i].ctrl->error_status);
224 shost_printk(KERN_ERR, fnic->lport->host,
225 "WQ[%d] error_status"
226 " %d\n", i, error_status);
229 for (i = 0; i < fnic->rq_count; i++) {
230 error_status = ioread32(&fnic->rq[i].ctrl->error_status);
232 shost_printk(KERN_ERR, fnic->lport->host,
233 "RQ[%d] error_status"
234 " %d\n", i, error_status);
237 for (i = 0; i < fnic->wq_copy_count; i++) {
238 error_status = ioread32(&fnic->wq_copy[i].ctrl->error_status);
240 shost_printk(KERN_ERR, fnic->lport->host,
241 "CWQ[%d] error_status"
242 " %d\n", i, error_status);
246 void fnic_handle_link_event(struct fnic *fnic)
250 spin_lock_irqsave(&fnic->fnic_lock, flags);
251 if (fnic->stop_rx_link_events) {
252 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
255 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
257 queue_work(fnic_event_queue, &fnic->link_work);
261 static int fnic_notify_set(struct fnic *fnic)
265 switch (vnic_dev_get_intr_mode(fnic->vdev)) {
266 case VNIC_DEV_INTR_MODE_INTX:
267 err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY);
269 case VNIC_DEV_INTR_MODE_MSI:
270 err = vnic_dev_notify_set(fnic->vdev, -1);
272 case VNIC_DEV_INTR_MODE_MSIX:
273 err = vnic_dev_notify_set(fnic->vdev, FNIC_MSIX_ERR_NOTIFY);
276 shost_printk(KERN_ERR, fnic->lport->host,
277 "Interrupt mode should be set up"
278 " before devcmd notify set %d\n",
279 vnic_dev_get_intr_mode(fnic->vdev));
287 static void fnic_notify_timer(unsigned long data)
289 struct fnic *fnic = (struct fnic *)data;
291 fnic_handle_link_event(fnic);
292 mod_timer(&fnic->notify_timer,
293 round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD));
296 static void fnic_fip_notify_timer(unsigned long data)
298 struct fnic *fnic = (struct fnic *)data;
300 fnic_handle_fip_timer(fnic);
303 static void fnic_notify_timer_start(struct fnic *fnic)
305 switch (vnic_dev_get_intr_mode(fnic->vdev)) {
306 case VNIC_DEV_INTR_MODE_MSI:
308 * Schedule first timeout immediately. The driver is
309 * initiatialized and ready to look for link up notification
311 mod_timer(&fnic->notify_timer, jiffies);
314 /* Using intr for notification for INTx/MSI-X */
319 static int fnic_dev_wait(struct vnic_dev *vdev,
320 int (*start)(struct vnic_dev *, int),
321 int (*finished)(struct vnic_dev *, int *),
328 err = start(vdev, arg);
332 /* Wait for func to complete...2 seconds max */
333 time = jiffies + (HZ * 2);
335 err = finished(vdev, &done);
340 schedule_timeout_uninterruptible(HZ / 10);
341 } while (time_after(time, jiffies));
346 static int fnic_cleanup(struct fnic *fnic)
351 vnic_dev_disable(fnic->vdev);
352 for (i = 0; i < fnic->intr_count; i++)
353 vnic_intr_mask(&fnic->intr[i]);
355 for (i = 0; i < fnic->rq_count; i++) {
356 err = vnic_rq_disable(&fnic->rq[i]);
360 for (i = 0; i < fnic->raw_wq_count; i++) {
361 err = vnic_wq_disable(&fnic->wq[i]);
365 for (i = 0; i < fnic->wq_copy_count; i++) {
366 err = vnic_wq_copy_disable(&fnic->wq_copy[i]);
371 /* Clean up completed IOs and FCS frames */
372 fnic_wq_copy_cmpl_handler(fnic, -1);
373 fnic_wq_cmpl_handler(fnic, -1);
374 fnic_rq_cmpl_handler(fnic, -1);
376 /* Clean up the IOs and FCS frames that have not completed */
377 for (i = 0; i < fnic->raw_wq_count; i++)
378 vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf);
379 for (i = 0; i < fnic->rq_count; i++)
380 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
381 for (i = 0; i < fnic->wq_copy_count; i++)
382 vnic_wq_copy_clean(&fnic->wq_copy[i],
383 fnic_wq_copy_cleanup_handler);
385 for (i = 0; i < fnic->cq_count; i++)
386 vnic_cq_clean(&fnic->cq[i]);
387 for (i = 0; i < fnic->intr_count; i++)
388 vnic_intr_clean(&fnic->intr[i]);
390 mempool_destroy(fnic->io_req_pool);
391 for (i = 0; i < FNIC_SGL_NUM_CACHES; i++)
392 mempool_destroy(fnic->io_sgl_pool[i]);
397 static void fnic_iounmap(struct fnic *fnic)
399 if (fnic->bar0.vaddr)
400 iounmap(fnic->bar0.vaddr);
404 * fnic_get_mac() - get assigned data MAC address for FIP code.
405 * @lport: local port.
407 static u8 *fnic_get_mac(struct fc_lport *lport)
409 struct fnic *fnic = lport_priv(lport);
411 return fnic->data_src_addr;
414 static void fnic_set_vlan(struct fnic *fnic, u16 vlan_id)
417 old_vlan = vnic_dev_set_default_vlan(fnic->vdev, vlan_id);
420 static int fnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
422 struct Scsi_Host *host;
431 * Allocate SCSI Host and set up association between host,
432 * local port, and fnic
434 lp = libfc_host_alloc(&fnic_host_template, sizeof(struct fnic));
436 printk(KERN_ERR PFX "Unable to alloc libfc local port\n");
441 fnic = lport_priv(lp);
445 snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME,
448 host->transportt = fnic_fc_transport;
450 err = scsi_init_shared_tag_map(host, FNIC_MAX_IO_REQ);
452 shost_printk(KERN_ERR, fnic->lport->host,
453 "Unable to alloc shared tag map\n");
454 goto err_out_free_hba;
457 /* Setup PCI resources */
458 pci_set_drvdata(pdev, fnic);
462 err = pci_enable_device(pdev);
464 shost_printk(KERN_ERR, fnic->lport->host,
465 "Cannot enable PCI device, aborting.\n");
466 goto err_out_free_hba;
469 err = pci_request_regions(pdev, DRV_NAME);
471 shost_printk(KERN_ERR, fnic->lport->host,
472 "Cannot enable PCI resources, aborting\n");
473 goto err_out_disable_device;
476 pci_set_master(pdev);
478 /* Query PCI controller on system for DMA addressing
479 * limitation for the device. Try 40-bit first, and
482 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
484 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
486 shost_printk(KERN_ERR, fnic->lport->host,
487 "No usable DMA configuration "
489 goto err_out_release_regions;
491 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
493 shost_printk(KERN_ERR, fnic->lport->host,
494 "Unable to obtain 32-bit DMA "
495 "for consistent allocations, aborting.\n");
496 goto err_out_release_regions;
499 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
501 shost_printk(KERN_ERR, fnic->lport->host,
502 "Unable to obtain 40-bit DMA "
503 "for consistent allocations, aborting.\n");
504 goto err_out_release_regions;
508 /* Map vNIC resources from BAR0 */
509 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
510 shost_printk(KERN_ERR, fnic->lport->host,
511 "BAR0 not memory-map'able, aborting.\n");
513 goto err_out_release_regions;
516 fnic->bar0.vaddr = pci_iomap(pdev, 0, 0);
517 fnic->bar0.bus_addr = pci_resource_start(pdev, 0);
518 fnic->bar0.len = pci_resource_len(pdev, 0);
520 if (!fnic->bar0.vaddr) {
521 shost_printk(KERN_ERR, fnic->lport->host,
522 "Cannot memory-map BAR0 res hdr, "
525 goto err_out_release_regions;
528 fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0);
530 shost_printk(KERN_ERR, fnic->lport->host,
531 "vNIC registration failed, "
534 goto err_out_iounmap;
537 err = fnic_dev_wait(fnic->vdev, vnic_dev_open,
538 vnic_dev_open_done, 0);
540 shost_printk(KERN_ERR, fnic->lport->host,
541 "vNIC dev open failed, aborting.\n");
542 goto err_out_vnic_unregister;
545 err = vnic_dev_init(fnic->vdev, 0);
547 shost_printk(KERN_ERR, fnic->lport->host,
548 "vNIC dev init failed, aborting.\n");
549 goto err_out_dev_close;
552 err = vnic_dev_mac_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
554 shost_printk(KERN_ERR, fnic->lport->host,
555 "vNIC get MAC addr failed \n");
556 goto err_out_dev_close;
558 /* set data_src for point-to-point mode and to keep it non-zero */
559 memcpy(fnic->data_src_addr, fnic->ctlr.ctl_src_addr, ETH_ALEN);
561 /* Get vNIC configuration */
562 err = fnic_get_vnic_config(fnic);
564 shost_printk(KERN_ERR, fnic->lport->host,
565 "Get vNIC configuration failed, "
567 goto err_out_dev_close;
569 host->max_lun = fnic->config.luns_per_tgt;
570 host->max_id = FNIC_MAX_FCP_TARGET;
571 host->max_cmd_len = FCOE_MAX_CMD_LEN;
573 fnic_get_res_counts(fnic);
575 err = fnic_set_intr_mode(fnic);
577 shost_printk(KERN_ERR, fnic->lport->host,
578 "Failed to set intr mode, "
580 goto err_out_dev_close;
583 err = fnic_alloc_vnic_resources(fnic);
585 shost_printk(KERN_ERR, fnic->lport->host,
586 "Failed to alloc vNIC resources, "
588 goto err_out_clear_intr;
592 /* initialize all fnic locks */
593 spin_lock_init(&fnic->fnic_lock);
595 for (i = 0; i < FNIC_WQ_MAX; i++)
596 spin_lock_init(&fnic->wq_lock[i]);
598 for (i = 0; i < FNIC_WQ_COPY_MAX; i++) {
599 spin_lock_init(&fnic->wq_copy_lock[i]);
600 fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK;
601 fnic->fw_ack_recd[i] = 0;
602 fnic->fw_ack_index[i] = -1;
605 for (i = 0; i < FNIC_IO_LOCKS; i++)
606 spin_lock_init(&fnic->io_req_lock[i]);
608 fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache);
609 if (!fnic->io_req_pool)
610 goto err_out_free_resources;
612 pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
614 goto err_out_free_ioreq_pool;
615 fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool;
617 pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
619 goto err_out_free_dflt_pool;
620 fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool;
622 /* setup vlan config, hw inserts vlan header */
623 fnic->vlan_hw_insert = 1;
626 /* Initialize the FIP fcoe_ctrl struct */
627 fnic->ctlr.send = fnic_eth_send;
628 fnic->ctlr.update_mac = fnic_update_mac;
629 fnic->ctlr.get_src_addr = fnic_get_mac;
630 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
631 shost_printk(KERN_INFO, fnic->lport->host,
632 "firmware supports FIP\n");
633 /* enable directed and multicast */
634 vnic_dev_packet_filter(fnic->vdev, 1, 1, 0, 0, 0);
635 vnic_dev_add_addr(fnic->vdev, FIP_ALL_ENODE_MACS);
636 vnic_dev_add_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
637 fnic->set_vlan = fnic_set_vlan;
638 fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_AUTO);
639 setup_timer(&fnic->fip_timer, fnic_fip_notify_timer,
640 (unsigned long)fnic);
641 spin_lock_init(&fnic->vlans_lock);
642 INIT_WORK(&fnic->fip_frame_work, fnic_handle_fip_frame);
643 INIT_WORK(&fnic->event_work, fnic_handle_event);
644 skb_queue_head_init(&fnic->fip_frame_queue);
645 spin_lock_irqsave(&fnic_list_lock, flags);
646 if (!fnic_fip_queue) {
648 create_singlethread_workqueue("fnic_fip_q");
649 if (!fnic_fip_queue) {
650 spin_unlock_irqrestore(&fnic_list_lock, flags);
651 printk(KERN_ERR PFX "fnic FIP work queue "
654 goto err_out_free_max_pool;
657 spin_unlock_irqrestore(&fnic_list_lock, flags);
658 INIT_LIST_HEAD(&fnic->evlist);
659 INIT_LIST_HEAD(&fnic->vlans);
661 shost_printk(KERN_INFO, fnic->lport->host,
662 "firmware uses non-FIP mode\n");
663 fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_NON_FIP);
665 fnic->state = FNIC_IN_FC_MODE;
667 atomic_set(&fnic->in_flight, 0);
668 fnic->state_flags = FNIC_FLAGS_NONE;
670 /* Enable hardware stripping of vlan header on ingress */
671 fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1);
673 /* Setup notification buffer area */
674 err = fnic_notify_set(fnic);
676 shost_printk(KERN_ERR, fnic->lport->host,
677 "Failed to alloc notify buffer, aborting.\n");
678 goto err_out_free_max_pool;
681 /* Setup notify timer when using MSI interrupts */
682 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
683 setup_timer(&fnic->notify_timer,
684 fnic_notify_timer, (unsigned long)fnic);
686 /* allocate RQ buffers and post them to RQ*/
687 for (i = 0; i < fnic->rq_count; i++) {
688 err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
690 shost_printk(KERN_ERR, fnic->lport->host,
691 "fnic_alloc_rq_frame can't alloc "
693 goto err_out_free_rq_buf;
698 * Initialization done with PCI system, hardware, firmware.
701 err = scsi_add_host(lp->host, &pdev->dev);
703 shost_printk(KERN_ERR, fnic->lport->host,
704 "fnic: scsi_add_host failed...exiting\n");
705 goto err_out_free_rq_buf;
708 /* Start local port initiatialization */
712 lp->max_retry_count = fnic->config.flogi_retries;
713 lp->max_rport_retry_count = fnic->config.plogi_retries;
714 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
715 FCP_SPPF_CONF_COMPL);
716 if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR)
717 lp->service_params |= FCP_SPPF_RETRY;
719 lp->boot_time = jiffies;
720 lp->e_d_tov = fnic->config.ed_tov;
721 lp->r_a_tov = fnic->config.ra_tov;
722 lp->link_supported_speeds = FC_PORTSPEED_10GBIT;
723 fc_set_wwnn(lp, fnic->config.node_wwn);
724 fc_set_wwpn(lp, fnic->config.port_wwn);
726 fcoe_libfc_config(lp, &fnic->ctlr, &fnic_transport_template, 0);
728 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCPIO_HOST_EXCH_RANGE_START,
729 FCPIO_HOST_EXCH_RANGE_END, NULL)) {
731 goto err_out_remove_scsi_host;
734 fc_lport_init_stats(lp);
738 if (fc_set_mfs(lp, fnic->config.maxdatafieldsize +
739 sizeof(struct fc_frame_header))) {
741 goto err_out_free_exch_mgr;
743 fc_host_maxframe_size(lp->host) = lp->mfs;
744 fc_host_dev_loss_tmo(lp->host) = fnic->config.port_down_timeout / 1000;
746 sprintf(fc_host_symbolic_name(lp->host),
747 DRV_NAME " v" DRV_VERSION " over %s", fnic->name);
749 spin_lock_irqsave(&fnic_list_lock, flags);
750 list_add_tail(&fnic->list, &fnic_list);
751 spin_unlock_irqrestore(&fnic_list_lock, flags);
753 INIT_WORK(&fnic->link_work, fnic_handle_link);
754 INIT_WORK(&fnic->frame_work, fnic_handle_frame);
755 skb_queue_head_init(&fnic->frame_queue);
756 skb_queue_head_init(&fnic->tx_queue);
758 /* Enable all queues */
759 for (i = 0; i < fnic->raw_wq_count; i++)
760 vnic_wq_enable(&fnic->wq[i]);
761 for (i = 0; i < fnic->rq_count; i++)
762 vnic_rq_enable(&fnic->rq[i]);
763 for (i = 0; i < fnic->wq_copy_count; i++)
764 vnic_wq_copy_enable(&fnic->wq_copy[i]);
768 vnic_dev_enable(fnic->vdev);
770 err = fnic_request_intr(fnic);
772 shost_printk(KERN_ERR, fnic->lport->host,
773 "Unable to request irq.\n");
774 goto err_out_free_exch_mgr;
777 for (i = 0; i < fnic->intr_count; i++)
778 vnic_intr_unmask(&fnic->intr[i]);
780 fnic_notify_timer_start(fnic);
784 err_out_free_exch_mgr:
785 fc_exch_mgr_free(lp);
786 err_out_remove_scsi_host:
787 fc_remove_host(lp->host);
788 scsi_remove_host(lp->host);
790 for (i = 0; i < fnic->rq_count; i++)
791 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
792 vnic_dev_notify_unset(fnic->vdev);
793 err_out_free_max_pool:
794 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]);
795 err_out_free_dflt_pool:
796 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]);
797 err_out_free_ioreq_pool:
798 mempool_destroy(fnic->io_req_pool);
799 err_out_free_resources:
800 fnic_free_vnic_resources(fnic);
802 fnic_clear_intr_mode(fnic);
804 vnic_dev_close(fnic->vdev);
805 err_out_vnic_unregister:
806 vnic_dev_unregister(fnic->vdev);
809 err_out_release_regions:
810 pci_release_regions(pdev);
811 err_out_disable_device:
812 pci_disable_device(pdev);
814 scsi_host_put(lp->host);
819 static void fnic_remove(struct pci_dev *pdev)
821 struct fnic *fnic = pci_get_drvdata(pdev);
822 struct fc_lport *lp = fnic->lport;
826 * Mark state so that the workqueue thread stops forwarding
827 * received frames and link events to the local port. ISR and
828 * other threads that can queue work items will also stop
829 * creating work items on the fnic workqueue
831 spin_lock_irqsave(&fnic->fnic_lock, flags);
832 fnic->stop_rx_link_events = 1;
833 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
835 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
836 del_timer_sync(&fnic->notify_timer);
839 * Flush the fnic event queue. After this call, there should
840 * be no event queued for this fnic device in the workqueue
842 flush_workqueue(fnic_event_queue);
843 skb_queue_purge(&fnic->frame_queue);
844 skb_queue_purge(&fnic->tx_queue);
846 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
847 del_timer_sync(&fnic->fip_timer);
848 skb_queue_purge(&fnic->fip_frame_queue);
849 fnic_fcoe_reset_vlans(fnic);
850 fnic_fcoe_evlist_free(fnic);
854 * Log off the fabric. This stops all remote ports, dns port,
855 * logs off the fabric. This flushes all rport, disc, lport work
858 fc_fabric_logoff(fnic->lport);
860 spin_lock_irqsave(&fnic->fnic_lock, flags);
862 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
864 fcoe_ctlr_destroy(&fnic->ctlr);
865 fc_lport_destroy(lp);
868 * This stops the fnic device, masks all interrupts. Completed
869 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
874 BUG_ON(!skb_queue_empty(&fnic->frame_queue));
875 BUG_ON(!skb_queue_empty(&fnic->tx_queue));
877 spin_lock_irqsave(&fnic_list_lock, flags);
878 list_del(&fnic->list);
879 spin_unlock_irqrestore(&fnic_list_lock, flags);
881 fc_remove_host(fnic->lport->host);
882 scsi_remove_host(fnic->lport->host);
883 fc_exch_mgr_free(fnic->lport);
884 vnic_dev_notify_unset(fnic->vdev);
885 fnic_free_intr(fnic);
886 fnic_free_vnic_resources(fnic);
887 fnic_clear_intr_mode(fnic);
888 vnic_dev_close(fnic->vdev);
889 vnic_dev_unregister(fnic->vdev);
891 pci_release_regions(pdev);
892 pci_disable_device(pdev);
893 pci_set_drvdata(pdev, NULL);
894 scsi_host_put(lp->host);
897 static struct pci_driver fnic_driver = {
899 .id_table = fnic_id_table,
901 .remove = fnic_remove,
904 static int __init fnic_init_module(void)
909 printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
911 /* Allocate memory for trace buffer */
912 err = fnic_trace_buf_init();
914 printk(KERN_ERR PFX "Trace buffer initialization Failed "
915 "Fnic Tracing utility is disabled\n");
919 /* Create a cache for allocation of default size sgls */
920 len = sizeof(struct fnic_dflt_sgl_list);
921 fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create
922 ("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
925 if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) {
926 printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n");
928 goto err_create_fnic_sgl_slab_dflt;
931 /* Create a cache for allocation of max size sgls*/
932 len = sizeof(struct fnic_sgl_list);
933 fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create
934 ("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
937 if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) {
938 printk(KERN_ERR PFX "failed to create fnic max sgl slab\n");
940 goto err_create_fnic_sgl_slab_max;
943 /* Create a cache of io_req structs for use via mempool */
944 fnic_io_req_cache = kmem_cache_create("fnic_io_req",
945 sizeof(struct fnic_io_req),
946 0, SLAB_HWCACHE_ALIGN, NULL);
947 if (!fnic_io_req_cache) {
948 printk(KERN_ERR PFX "failed to create fnic io_req slab\n");
950 goto err_create_fnic_ioreq_slab;
953 fnic_event_queue = create_singlethread_workqueue("fnic_event_wq");
954 if (!fnic_event_queue) {
955 printk(KERN_ERR PFX "fnic work queue create failed\n");
957 goto err_create_fnic_workq;
960 spin_lock_init(&fnic_list_lock);
961 INIT_LIST_HEAD(&fnic_list);
963 fnic_fc_transport = fc_attach_transport(&fnic_fc_functions);
964 if (!fnic_fc_transport) {
965 printk(KERN_ERR PFX "fc_attach_transport error\n");
967 goto err_fc_transport;
970 /* register the driver with PCI system */
971 err = pci_register_driver(&fnic_driver);
973 printk(KERN_ERR PFX "pci register error\n");
974 goto err_pci_register;
979 fc_release_transport(fnic_fc_transport);
981 destroy_workqueue(fnic_event_queue);
982 err_create_fnic_workq:
983 kmem_cache_destroy(fnic_io_req_cache);
984 err_create_fnic_ioreq_slab:
985 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
986 err_create_fnic_sgl_slab_max:
987 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
988 err_create_fnic_sgl_slab_dflt:
993 static void __exit fnic_cleanup_module(void)
995 pci_unregister_driver(&fnic_driver);
996 destroy_workqueue(fnic_event_queue);
997 if (fnic_fip_queue) {
998 flush_workqueue(fnic_fip_queue);
999 destroy_workqueue(fnic_fip_queue);
1001 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
1002 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
1003 kmem_cache_destroy(fnic_io_req_cache);
1004 fc_release_transport(fnic_fc_transport);
1008 module_init(fnic_init_module);
1009 module_exit(fnic_cleanup_module);