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 INIT_LIST_HEAD(&fnic
->evlist
);
646 INIT_LIST_HEAD(&fnic
->vlans
);
648 shost_printk(KERN_INFO
, fnic
->lport
->host
,
649 "firmware uses non-FIP mode\n");
650 fcoe_ctlr_init(&fnic
->ctlr
, FIP_MODE_NON_FIP
);
652 fnic
->state
= FNIC_IN_FC_MODE
;
654 atomic_set(&fnic
->in_flight
, 0);
655 fnic
->state_flags
= FNIC_FLAGS_NONE
;
657 /* Enable hardware stripping of vlan header on ingress */
658 fnic_set_nic_config(fnic
, 0, 0, 0, 0, 0, 0, 1);
660 /* Setup notification buffer area */
661 err
= fnic_notify_set(fnic
);
663 shost_printk(KERN_ERR
, fnic
->lport
->host
,
664 "Failed to alloc notify buffer, aborting.\n");
665 goto err_out_free_max_pool
;
668 /* Setup notify timer when using MSI interrupts */
669 if (vnic_dev_get_intr_mode(fnic
->vdev
) == VNIC_DEV_INTR_MODE_MSI
)
670 setup_timer(&fnic
->notify_timer
,
671 fnic_notify_timer
, (unsigned long)fnic
);
673 /* allocate RQ buffers and post them to RQ*/
674 for (i
= 0; i
< fnic
->rq_count
; i
++) {
675 err
= vnic_rq_fill(&fnic
->rq
[i
], fnic_alloc_rq_frame
);
677 shost_printk(KERN_ERR
, fnic
->lport
->host
,
678 "fnic_alloc_rq_frame can't alloc "
680 goto err_out_free_rq_buf
;
685 * Initialization done with PCI system, hardware, firmware.
688 err
= scsi_add_host(lp
->host
, &pdev
->dev
);
690 shost_printk(KERN_ERR
, fnic
->lport
->host
,
691 "fnic: scsi_add_host failed...exiting\n");
692 goto err_out_free_rq_buf
;
695 /* Start local port initiatialization */
699 lp
->max_retry_count
= fnic
->config
.flogi_retries
;
700 lp
->max_rport_retry_count
= fnic
->config
.plogi_retries
;
701 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
702 FCP_SPPF_CONF_COMPL
);
703 if (fnic
->config
.flags
& VFCF_FCP_SEQ_LVL_ERR
)
704 lp
->service_params
|= FCP_SPPF_RETRY
;
706 lp
->boot_time
= jiffies
;
707 lp
->e_d_tov
= fnic
->config
.ed_tov
;
708 lp
->r_a_tov
= fnic
->config
.ra_tov
;
709 lp
->link_supported_speeds
= FC_PORTSPEED_10GBIT
;
710 fc_set_wwnn(lp
, fnic
->config
.node_wwn
);
711 fc_set_wwpn(lp
, fnic
->config
.port_wwn
);
713 fcoe_libfc_config(lp
, &fnic
->ctlr
, &fnic_transport_template
, 0);
715 if (!fc_exch_mgr_alloc(lp
, FC_CLASS_3
, FCPIO_HOST_EXCH_RANGE_START
,
716 FCPIO_HOST_EXCH_RANGE_END
, NULL
)) {
718 goto err_out_remove_scsi_host
;
721 fc_lport_init_stats(lp
);
725 if (fc_set_mfs(lp
, fnic
->config
.maxdatafieldsize
+
726 sizeof(struct fc_frame_header
))) {
728 goto err_out_free_exch_mgr
;
730 fc_host_maxframe_size(lp
->host
) = lp
->mfs
;
731 fc_host_dev_loss_tmo(lp
->host
) = fnic
->config
.port_down_timeout
/ 1000;
733 sprintf(fc_host_symbolic_name(lp
->host
),
734 DRV_NAME
" v" DRV_VERSION
" over %s", fnic
->name
);
736 spin_lock_irqsave(&fnic_list_lock
, flags
);
737 list_add_tail(&fnic
->list
, &fnic_list
);
738 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
740 INIT_WORK(&fnic
->link_work
, fnic_handle_link
);
741 INIT_WORK(&fnic
->frame_work
, fnic_handle_frame
);
742 skb_queue_head_init(&fnic
->frame_queue
);
743 skb_queue_head_init(&fnic
->tx_queue
);
745 /* Enable all queues */
746 for (i
= 0; i
< fnic
->raw_wq_count
; i
++)
747 vnic_wq_enable(&fnic
->wq
[i
]);
748 for (i
= 0; i
< fnic
->rq_count
; i
++)
749 vnic_rq_enable(&fnic
->rq
[i
]);
750 for (i
= 0; i
< fnic
->wq_copy_count
; i
++)
751 vnic_wq_copy_enable(&fnic
->wq_copy
[i
]);
755 vnic_dev_enable(fnic
->vdev
);
757 err
= fnic_request_intr(fnic
);
759 shost_printk(KERN_ERR
, fnic
->lport
->host
,
760 "Unable to request irq.\n");
761 goto err_out_free_exch_mgr
;
764 for (i
= 0; i
< fnic
->intr_count
; i
++)
765 vnic_intr_unmask(&fnic
->intr
[i
]);
767 fnic_notify_timer_start(fnic
);
771 err_out_free_exch_mgr
:
772 fc_exch_mgr_free(lp
);
773 err_out_remove_scsi_host
:
774 fc_remove_host(lp
->host
);
775 scsi_remove_host(lp
->host
);
777 for (i
= 0; i
< fnic
->rq_count
; i
++)
778 vnic_rq_clean(&fnic
->rq
[i
], fnic_free_rq_buf
);
779 vnic_dev_notify_unset(fnic
->vdev
);
780 err_out_free_max_pool
:
781 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_MAX
]);
782 err_out_free_dflt_pool
:
783 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_DFLT
]);
784 err_out_free_ioreq_pool
:
785 mempool_destroy(fnic
->io_req_pool
);
786 err_out_free_resources
:
787 fnic_free_vnic_resources(fnic
);
789 fnic_clear_intr_mode(fnic
);
791 vnic_dev_close(fnic
->vdev
);
792 err_out_vnic_unregister
:
793 vnic_dev_unregister(fnic
->vdev
);
796 err_out_release_regions
:
797 pci_release_regions(pdev
);
798 err_out_disable_device
:
799 pci_disable_device(pdev
);
801 scsi_host_put(lp
->host
);
806 static void fnic_remove(struct pci_dev
*pdev
)
808 struct fnic
*fnic
= pci_get_drvdata(pdev
);
809 struct fc_lport
*lp
= fnic
->lport
;
813 * Mark state so that the workqueue thread stops forwarding
814 * received frames and link events to the local port. ISR and
815 * other threads that can queue work items will also stop
816 * creating work items on the fnic workqueue
818 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
819 fnic
->stop_rx_link_events
= 1;
820 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
822 if (vnic_dev_get_intr_mode(fnic
->vdev
) == VNIC_DEV_INTR_MODE_MSI
)
823 del_timer_sync(&fnic
->notify_timer
);
826 * Flush the fnic event queue. After this call, there should
827 * be no event queued for this fnic device in the workqueue
829 flush_workqueue(fnic_event_queue
);
830 skb_queue_purge(&fnic
->frame_queue
);
831 skb_queue_purge(&fnic
->tx_queue
);
833 if (fnic
->config
.flags
& VFCF_FIP_CAPABLE
) {
834 del_timer_sync(&fnic
->fip_timer
);
835 skb_queue_purge(&fnic
->fip_frame_queue
);
836 fnic_fcoe_reset_vlans(fnic
);
837 fnic_fcoe_evlist_free(fnic
);
841 * Log off the fabric. This stops all remote ports, dns port,
842 * logs off the fabric. This flushes all rport, disc, lport work
845 fc_fabric_logoff(fnic
->lport
);
847 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
849 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
851 fcoe_ctlr_destroy(&fnic
->ctlr
);
852 fc_lport_destroy(lp
);
855 * This stops the fnic device, masks all interrupts. Completed
856 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
861 BUG_ON(!skb_queue_empty(&fnic
->frame_queue
));
862 BUG_ON(!skb_queue_empty(&fnic
->tx_queue
));
864 spin_lock_irqsave(&fnic_list_lock
, flags
);
865 list_del(&fnic
->list
);
866 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
868 fc_remove_host(fnic
->lport
->host
);
869 scsi_remove_host(fnic
->lport
->host
);
870 fc_exch_mgr_free(fnic
->lport
);
871 vnic_dev_notify_unset(fnic
->vdev
);
872 fnic_free_intr(fnic
);
873 fnic_free_vnic_resources(fnic
);
874 fnic_clear_intr_mode(fnic
);
875 vnic_dev_close(fnic
->vdev
);
876 vnic_dev_unregister(fnic
->vdev
);
878 pci_release_regions(pdev
);
879 pci_disable_device(pdev
);
880 pci_set_drvdata(pdev
, NULL
);
881 scsi_host_put(lp
->host
);
884 static struct pci_driver fnic_driver
= {
886 .id_table
= fnic_id_table
,
888 .remove
= fnic_remove
,
891 static int __init
fnic_init_module(void)
896 printk(KERN_INFO PFX
"%s, ver %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
898 /* Allocate memory for trace buffer */
899 err
= fnic_trace_buf_init();
901 printk(KERN_ERR PFX
"Trace buffer initialization Failed "
902 "Fnic Tracing utility is disabled\n");
906 /* Create a cache for allocation of default size sgls */
907 len
= sizeof(struct fnic_dflt_sgl_list
);
908 fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
] = kmem_cache_create
909 ("fnic_sgl_dflt", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
912 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]) {
913 printk(KERN_ERR PFX
"failed to create fnic dflt sgl slab\n");
915 goto err_create_fnic_sgl_slab_dflt
;
918 /* Create a cache for allocation of max size sgls*/
919 len
= sizeof(struct fnic_sgl_list
);
920 fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
] = kmem_cache_create
921 ("fnic_sgl_max", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
924 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]) {
925 printk(KERN_ERR PFX
"failed to create fnic max sgl slab\n");
927 goto err_create_fnic_sgl_slab_max
;
930 /* Create a cache of io_req structs for use via mempool */
931 fnic_io_req_cache
= kmem_cache_create("fnic_io_req",
932 sizeof(struct fnic_io_req
),
933 0, SLAB_HWCACHE_ALIGN
, NULL
);
934 if (!fnic_io_req_cache
) {
935 printk(KERN_ERR PFX
"failed to create fnic io_req slab\n");
937 goto err_create_fnic_ioreq_slab
;
940 fnic_event_queue
= create_singlethread_workqueue("fnic_event_wq");
941 if (!fnic_event_queue
) {
942 printk(KERN_ERR PFX
"fnic work queue create failed\n");
944 goto err_create_fnic_workq
;
947 spin_lock_init(&fnic_list_lock
);
948 INIT_LIST_HEAD(&fnic_list
);
950 fnic_fip_queue
= create_singlethread_workqueue("fnic_fip_q");
951 if (!fnic_fip_queue
) {
952 printk(KERN_ERR PFX
"fnic FIP work queue create failed\n");
954 goto err_create_fip_workq
;
957 fnic_fc_transport
= fc_attach_transport(&fnic_fc_functions
);
958 if (!fnic_fc_transport
) {
959 printk(KERN_ERR PFX
"fc_attach_transport error\n");
961 goto err_fc_transport
;
964 /* register the driver with PCI system */
965 err
= pci_register_driver(&fnic_driver
);
967 printk(KERN_ERR PFX
"pci register error\n");
968 goto err_pci_register
;
973 fc_release_transport(fnic_fc_transport
);
975 destroy_workqueue(fnic_fip_queue
);
976 err_create_fip_workq
:
977 destroy_workqueue(fnic_event_queue
);
978 err_create_fnic_workq
:
979 kmem_cache_destroy(fnic_io_req_cache
);
980 err_create_fnic_ioreq_slab
:
981 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
982 err_create_fnic_sgl_slab_max
:
983 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
984 err_create_fnic_sgl_slab_dflt
:
989 static void __exit
fnic_cleanup_module(void)
991 pci_unregister_driver(&fnic_driver
);
992 destroy_workqueue(fnic_event_queue
);
993 if (fnic_fip_queue
) {
994 flush_workqueue(fnic_fip_queue
);
995 destroy_workqueue(fnic_fip_queue
);
997 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
998 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
999 kmem_cache_destroy(fnic_io_req_cache
);
1000 fc_release_transport(fnic_fc_transport
);
1004 module_init(fnic_init_module
);
1005 module_exit(fnic_cleanup_module
);