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"
44 #define PCI_DEVICE_ID_CISCO_FNIC 0x0045
46 /* Timer to poll notification area for events. Used for MSI interrupts */
47 #define FNIC_NOTIFY_TIMER_PERIOD (2 * HZ)
49 static struct kmem_cache
*fnic_sgl_cache
[FNIC_SGL_NUM_CACHES
];
50 static struct kmem_cache
*fnic_io_req_cache
;
52 DEFINE_SPINLOCK(fnic_list_lock
);
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
) },
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
);
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");
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
80 static int fnic_slave_alloc(struct scsi_device
*sdev
)
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
);
86 sdev
->tagged_supported
= 1;
88 if (!rport
|| fc_remote_port_chkready(rport
))
91 scsi_activate_tcq(sdev
, FNIC_DFLT_QUEUE_DEPTH
);
92 rport
->dev_loss_tmo
= fnic
->config
.port_down_timeout
/ 1000;
97 static struct scsi_host_template fnic_host_template
= {
98 .module
= THIS_MODULE
,
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
,
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
,
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
*);
120 static struct fc_function_template fnic_fc_functions
= {
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
,
150 static void fnic_get_host_speed(struct Scsi_Host
*shost
)
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
);
156 /* Add in other values as they get defined in fw */
157 switch (port_speed
) {
159 fc_host_speed(shost
) = FC_PORTSPEED_10GBIT
;
162 fc_host_speed(shost
) = FC_PORTSPEED_10GBIT
;
167 static struct fc_host_statistics
*fnic_get_stats(struct Scsi_Host
*host
)
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
;
176 if (time_before(jiffies
, fnic
->stats_time
+ HZ
/ FNIC_STATS_RATE_LIMIT
))
178 fnic
->stats_time
= jiffies
;
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
);
185 FNIC_MAIN_DBG(KERN_DEBUG
, fnic
->lport
->host
,
186 "fnic: Get vnic stats failed"
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);
205 void fnic_log_q_error(struct fnic
*fnic
)
210 for (i
= 0; i
< fnic
->raw_wq_count
; i
++) {
211 error_status
= ioread32(&fnic
->wq
[i
].ctrl
->error_status
);
213 shost_printk(KERN_ERR
, fnic
->lport
->host
,
214 "WQ[%d] error_status"
215 " %d\n", i
, error_status
);
218 for (i
= 0; i
< fnic
->rq_count
; i
++) {
219 error_status
= ioread32(&fnic
->rq
[i
].ctrl
->error_status
);
221 shost_printk(KERN_ERR
, fnic
->lport
->host
,
222 "RQ[%d] error_status"
223 " %d\n", i
, error_status
);
226 for (i
= 0; i
< fnic
->wq_copy_count
; i
++) {
227 error_status
= ioread32(&fnic
->wq_copy
[i
].ctrl
->error_status
);
229 shost_printk(KERN_ERR
, fnic
->lport
->host
,
230 "CWQ[%d] error_status"
231 " %d\n", i
, error_status
);
235 void fnic_handle_link_event(struct fnic
*fnic
)
239 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
240 if (fnic
->stop_rx_link_events
) {
241 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
244 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
246 queue_work(fnic_event_queue
, &fnic
->link_work
);
250 static int fnic_notify_set(struct fnic
*fnic
)
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
);
258 case VNIC_DEV_INTR_MODE_MSI
:
259 err
= vnic_dev_notify_set(fnic
->vdev
, -1);
261 case VNIC_DEV_INTR_MODE_MSIX
:
262 err
= vnic_dev_notify_set(fnic
->vdev
, FNIC_MSIX_ERR_NOTIFY
);
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
));
276 static void fnic_notify_timer(unsigned long data
)
278 struct fnic
*fnic
= (struct fnic
*)data
;
280 fnic_handle_link_event(fnic
);
281 mod_timer(&fnic
->notify_timer
,
282 round_jiffies(jiffies
+ FNIC_NOTIFY_TIMER_PERIOD
));
285 static void fnic_notify_timer_start(struct fnic
*fnic
)
287 switch (vnic_dev_get_intr_mode(fnic
->vdev
)) {
288 case VNIC_DEV_INTR_MODE_MSI
:
290 * Schedule first timeout immediately. The driver is
291 * initiatialized and ready to look for link up notification
293 mod_timer(&fnic
->notify_timer
, jiffies
);
296 /* Using intr for notification for INTx/MSI-X */
301 static int fnic_dev_wait(struct vnic_dev
*vdev
,
302 int (*start
)(struct vnic_dev
*, int),
303 int (*finished
)(struct vnic_dev
*, int *),
310 err
= start(vdev
, arg
);
314 /* Wait for func to complete...2 seconds max */
315 time
= jiffies
+ (HZ
* 2);
317 err
= finished(vdev
, &done
);
322 schedule_timeout_uninterruptible(HZ
/ 10);
323 } while (time_after(time
, jiffies
));
328 static int fnic_cleanup(struct fnic
*fnic
)
333 vnic_dev_disable(fnic
->vdev
);
334 for (i
= 0; i
< fnic
->intr_count
; i
++)
335 vnic_intr_mask(&fnic
->intr
[i
]);
337 for (i
= 0; i
< fnic
->rq_count
; i
++) {
338 err
= vnic_rq_disable(&fnic
->rq
[i
]);
342 for (i
= 0; i
< fnic
->raw_wq_count
; i
++) {
343 err
= vnic_wq_disable(&fnic
->wq
[i
]);
347 for (i
= 0; i
< fnic
->wq_copy_count
; i
++) {
348 err
= vnic_wq_copy_disable(&fnic
->wq_copy
[i
]);
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);
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
);
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
]);
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
]);
379 static void fnic_iounmap(struct fnic
*fnic
)
381 if (fnic
->bar0
.vaddr
)
382 iounmap(fnic
->bar0
.vaddr
);
386 * Allocate element for mempools requiring GFP_DMA flag.
387 * Otherwise, checks in kmem_flagcheck() hit BUG_ON().
389 static void *fnic_alloc_slab_dma(gfp_t gfp_mask
, void *pool_data
)
391 struct kmem_cache
*mem
= pool_data
;
393 return kmem_cache_alloc(mem
, gfp_mask
| GFP_ATOMIC
| GFP_DMA
);
397 * fnic_get_mac() - get assigned data MAC address for FIP code.
398 * @lport: local port.
400 static u8
*fnic_get_mac(struct fc_lport
*lport
)
402 struct fnic
*fnic
= lport_priv(lport
);
404 return fnic
->data_src_addr
;
407 static int __devinit
fnic_probe(struct pci_dev
*pdev
,
408 const struct pci_device_id
*ent
)
410 struct Scsi_Host
*host
;
419 * Allocate SCSI Host and set up association between host,
420 * local port, and fnic
422 lp
= libfc_host_alloc(&fnic_host_template
, sizeof(struct fnic
));
424 printk(KERN_ERR PFX
"Unable to alloc libfc local port\n");
429 fnic
= lport_priv(lp
);
433 snprintf(fnic
->name
, sizeof(fnic
->name
) - 1, "%s%d", DRV_NAME
,
436 host
->transportt
= fnic_fc_transport
;
438 err
= scsi_init_shared_tag_map(host
, FNIC_MAX_IO_REQ
);
440 shost_printk(KERN_ERR
, fnic
->lport
->host
,
441 "Unable to alloc shared tag map\n");
442 goto err_out_free_hba
;
445 /* Setup PCI resources */
446 pci_set_drvdata(pdev
, fnic
);
450 err
= pci_enable_device(pdev
);
452 shost_printk(KERN_ERR
, fnic
->lport
->host
,
453 "Cannot enable PCI device, aborting.\n");
454 goto err_out_free_hba
;
457 err
= pci_request_regions(pdev
, DRV_NAME
);
459 shost_printk(KERN_ERR
, fnic
->lport
->host
,
460 "Cannot enable PCI resources, aborting\n");
461 goto err_out_disable_device
;
464 pci_set_master(pdev
);
466 /* Query PCI controller on system for DMA addressing
467 * limitation for the device. Try 40-bit first, and
470 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(40));
472 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
474 shost_printk(KERN_ERR
, fnic
->lport
->host
,
475 "No usable DMA configuration "
477 goto err_out_release_regions
;
479 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
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
;
487 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(40));
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
;
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");
501 goto err_out_release_regions
;
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);
508 if (!fnic
->bar0
.vaddr
) {
509 shost_printk(KERN_ERR
, fnic
->lport
->host
,
510 "Cannot memory-map BAR0 res hdr, "
513 goto err_out_release_regions
;
516 fnic
->vdev
= vnic_dev_register(NULL
, fnic
, pdev
, &fnic
->bar0
);
518 shost_printk(KERN_ERR
, fnic
->lport
->host
,
519 "vNIC registration failed, "
522 goto err_out_iounmap
;
525 err
= fnic_dev_wait(fnic
->vdev
, vnic_dev_open
,
526 vnic_dev_open_done
, 0);
528 shost_printk(KERN_ERR
, fnic
->lport
->host
,
529 "vNIC dev open failed, aborting.\n");
530 goto err_out_vnic_unregister
;
533 err
= vnic_dev_init(fnic
->vdev
, 0);
535 shost_printk(KERN_ERR
, fnic
->lport
->host
,
536 "vNIC dev init failed, aborting.\n");
537 goto err_out_dev_close
;
540 err
= vnic_dev_mac_addr(fnic
->vdev
, fnic
->ctlr
.ctl_src_addr
);
542 shost_printk(KERN_ERR
, fnic
->lport
->host
,
543 "vNIC get MAC addr failed \n");
544 goto err_out_dev_close
;
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
);
549 /* Get vNIC configuration */
550 err
= fnic_get_vnic_config(fnic
);
552 shost_printk(KERN_ERR
, fnic
->lport
->host
,
553 "Get vNIC configuration failed, "
555 goto err_out_dev_close
;
557 host
->max_lun
= fnic
->config
.luns_per_tgt
;
558 host
->max_id
= FNIC_MAX_FCP_TARGET
;
559 host
->max_cmd_len
= FNIC_MAX_CMD_LEN
;
561 fnic_get_res_counts(fnic
);
563 err
= fnic_set_intr_mode(fnic
);
565 shost_printk(KERN_ERR
, fnic
->lport
->host
,
566 "Failed to set intr mode, "
568 goto err_out_dev_close
;
571 err
= fnic_alloc_vnic_resources(fnic
);
573 shost_printk(KERN_ERR
, fnic
->lport
->host
,
574 "Failed to alloc vNIC resources, "
576 goto err_out_clear_intr
;
580 /* initialize all fnic locks */
581 spin_lock_init(&fnic
->fnic_lock
);
583 for (i
= 0; i
< FNIC_WQ_MAX
; i
++)
584 spin_lock_init(&fnic
->wq_lock
[i
]);
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;
593 for (i
= 0; i
< FNIC_IO_LOCKS
; i
++)
594 spin_lock_init(&fnic
->io_req_lock
[i
]);
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
;
600 pool
= mempool_create(2, fnic_alloc_slab_dma
, mempool_free_slab
,
601 fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
603 goto err_out_free_ioreq_pool
;
604 fnic
->io_sgl_pool
[FNIC_SGL_CACHE_DFLT
] = pool
;
606 pool
= mempool_create(2, fnic_alloc_slab_dma
, mempool_free_slab
,
607 fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
609 goto err_out_free_dflt_pool
;
610 fnic
->io_sgl_pool
[FNIC_SGL_CACHE_MAX
] = pool
;
612 /* setup vlan config, hw inserts vlan header */
613 fnic
->vlan_hw_insert
= 1;
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 fcoe_ctlr_init(&fnic
->ctlr
);
621 if (fnic
->config
.flags
& VFCF_FIP_CAPABLE
) {
622 shost_printk(KERN_INFO
, fnic
->lport
->host
,
623 "firmware supports FIP\n");
624 /* enable directed and multicast */
625 vnic_dev_packet_filter(fnic
->vdev
, 1, 1, 0, 0, 0);
626 vnic_dev_add_addr(fnic
->vdev
, FIP_ALL_ENODE_MACS
);
627 vnic_dev_add_addr(fnic
->vdev
, fnic
->ctlr
.ctl_src_addr
);
629 shost_printk(KERN_INFO
, fnic
->lport
->host
,
630 "firmware uses non-FIP mode\n");
631 fnic
->ctlr
.mode
= FIP_ST_NON_FIP
;
633 fnic
->state
= FNIC_IN_FC_MODE
;
635 /* Enable hardware stripping of vlan header on ingress */
636 fnic_set_nic_config(fnic
, 0, 0, 0, 0, 0, 0, 1);
638 /* Setup notification buffer area */
639 err
= fnic_notify_set(fnic
);
641 shost_printk(KERN_ERR
, fnic
->lport
->host
,
642 "Failed to alloc notify buffer, aborting.\n");
643 goto err_out_free_max_pool
;
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
);
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
);
655 shost_printk(KERN_ERR
, fnic
->lport
->host
,
656 "fnic_alloc_rq_frame can't alloc "
658 goto err_out_free_rq_buf
;
663 * Initialization done with PCI system, hardware, firmware.
666 err
= scsi_add_host(lp
->host
, &pdev
->dev
);
668 shost_printk(KERN_ERR
, fnic
->lport
->host
,
669 "fnic: scsi_add_host failed...exiting\n");
670 goto err_out_free_rq_buf
;
673 /* Start local port initiatialization */
676 lp
->tt
= fnic_transport_template
;
678 lp
->max_retry_count
= fnic
->config
.flogi_retries
;
679 lp
->max_rport_retry_count
= fnic
->config
.plogi_retries
;
680 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
681 FCP_SPPF_CONF_COMPL
);
682 if (fnic
->config
.flags
& VFCF_FCP_SEQ_LVL_ERR
)
683 lp
->service_params
|= FCP_SPPF_RETRY
;
685 lp
->boot_time
= jiffies
;
686 lp
->e_d_tov
= fnic
->config
.ed_tov
;
687 lp
->r_a_tov
= fnic
->config
.ra_tov
;
688 lp
->link_supported_speeds
= FC_PORTSPEED_10GBIT
;
689 fc_set_wwnn(lp
, fnic
->config
.node_wwn
);
690 fc_set_wwpn(lp
, fnic
->config
.port_wwn
);
698 if (!fc_exch_mgr_alloc(lp
, FC_CLASS_3
, FCPIO_HOST_EXCH_RANGE_START
,
699 FCPIO_HOST_EXCH_RANGE_END
, NULL
)) {
701 goto err_out_remove_scsi_host
;
704 fc_lport_init_stats(lp
);
708 if (fc_set_mfs(lp
, fnic
->config
.maxdatafieldsize
+
709 sizeof(struct fc_frame_header
))) {
711 goto err_out_free_exch_mgr
;
713 fc_host_maxframe_size(lp
->host
) = lp
->mfs
;
715 sprintf(fc_host_symbolic_name(lp
->host
),
716 DRV_NAME
" v" DRV_VERSION
" over %s", fnic
->name
);
718 spin_lock_irqsave(&fnic_list_lock
, flags
);
719 list_add_tail(&fnic
->list
, &fnic_list
);
720 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
722 INIT_WORK(&fnic
->link_work
, fnic_handle_link
);
723 INIT_WORK(&fnic
->frame_work
, fnic_handle_frame
);
724 skb_queue_head_init(&fnic
->frame_queue
);
725 skb_queue_head_init(&fnic
->tx_queue
);
727 /* Enable all queues */
728 for (i
= 0; i
< fnic
->raw_wq_count
; i
++)
729 vnic_wq_enable(&fnic
->wq
[i
]);
730 for (i
= 0; i
< fnic
->rq_count
; i
++)
731 vnic_rq_enable(&fnic
->rq
[i
]);
732 for (i
= 0; i
< fnic
->wq_copy_count
; i
++)
733 vnic_wq_copy_enable(&fnic
->wq_copy
[i
]);
737 vnic_dev_enable(fnic
->vdev
);
739 err
= fnic_request_intr(fnic
);
741 shost_printk(KERN_ERR
, fnic
->lport
->host
,
742 "Unable to request irq.\n");
743 goto err_out_free_exch_mgr
;
746 for (i
= 0; i
< fnic
->intr_count
; i
++)
747 vnic_intr_unmask(&fnic
->intr
[i
]);
749 fnic_notify_timer_start(fnic
);
753 err_out_free_exch_mgr
:
754 fc_exch_mgr_free(lp
);
755 err_out_remove_scsi_host
:
756 fc_remove_host(lp
->host
);
757 scsi_remove_host(lp
->host
);
759 for (i
= 0; i
< fnic
->rq_count
; i
++)
760 vnic_rq_clean(&fnic
->rq
[i
], fnic_free_rq_buf
);
761 vnic_dev_notify_unset(fnic
->vdev
);
762 err_out_free_max_pool
:
763 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_MAX
]);
764 err_out_free_dflt_pool
:
765 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_DFLT
]);
766 err_out_free_ioreq_pool
:
767 mempool_destroy(fnic
->io_req_pool
);
768 err_out_free_resources
:
769 fnic_free_vnic_resources(fnic
);
771 fnic_clear_intr_mode(fnic
);
773 vnic_dev_close(fnic
->vdev
);
774 err_out_vnic_unregister
:
775 vnic_dev_unregister(fnic
->vdev
);
778 err_out_release_regions
:
779 pci_release_regions(pdev
);
780 err_out_disable_device
:
781 pci_disable_device(pdev
);
783 scsi_host_put(lp
->host
);
788 static void __devexit
fnic_remove(struct pci_dev
*pdev
)
790 struct fnic
*fnic
= pci_get_drvdata(pdev
);
791 struct fc_lport
*lp
= fnic
->lport
;
795 * Mark state so that the workqueue thread stops forwarding
796 * received frames and link events to the local port. ISR and
797 * other threads that can queue work items will also stop
798 * creating work items on the fnic workqueue
800 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
801 fnic
->stop_rx_link_events
= 1;
802 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
804 if (vnic_dev_get_intr_mode(fnic
->vdev
) == VNIC_DEV_INTR_MODE_MSI
)
805 del_timer_sync(&fnic
->notify_timer
);
808 * Flush the fnic event queue. After this call, there should
809 * be no event queued for this fnic device in the workqueue
811 flush_workqueue(fnic_event_queue
);
812 skb_queue_purge(&fnic
->frame_queue
);
813 skb_queue_purge(&fnic
->tx_queue
);
816 * Log off the fabric. This stops all remote ports, dns port,
817 * logs off the fabric. This flushes all rport, disc, lport work
820 fc_fabric_logoff(fnic
->lport
);
822 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
824 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
826 fcoe_ctlr_destroy(&fnic
->ctlr
);
827 fc_lport_destroy(lp
);
830 * This stops the fnic device, masks all interrupts. Completed
831 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
836 BUG_ON(!skb_queue_empty(&fnic
->frame_queue
));
837 BUG_ON(!skb_queue_empty(&fnic
->tx_queue
));
839 spin_lock_irqsave(&fnic_list_lock
, flags
);
840 list_del(&fnic
->list
);
841 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
843 fc_remove_host(fnic
->lport
->host
);
844 scsi_remove_host(fnic
->lport
->host
);
845 fc_exch_mgr_free(fnic
->lport
);
846 vnic_dev_notify_unset(fnic
->vdev
);
847 fnic_free_intr(fnic
);
848 fnic_free_vnic_resources(fnic
);
849 fnic_clear_intr_mode(fnic
);
850 vnic_dev_close(fnic
->vdev
);
851 vnic_dev_unregister(fnic
->vdev
);
853 pci_release_regions(pdev
);
854 pci_disable_device(pdev
);
855 pci_set_drvdata(pdev
, NULL
);
856 scsi_host_put(lp
->host
);
859 static struct pci_driver fnic_driver
= {
861 .id_table
= fnic_id_table
,
863 .remove
= __devexit_p(fnic_remove
),
866 static int __init
fnic_init_module(void)
871 printk(KERN_INFO PFX
"%s, ver %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
873 /* Create a cache for allocation of default size sgls */
874 len
= sizeof(struct fnic_dflt_sgl_list
);
875 fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
] = kmem_cache_create
876 ("fnic_sgl_dflt", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
877 SLAB_HWCACHE_ALIGN
| SLAB_CACHE_DMA
,
879 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]) {
880 printk(KERN_ERR PFX
"failed to create fnic dflt sgl slab\n");
882 goto err_create_fnic_sgl_slab_dflt
;
885 /* Create a cache for allocation of max size sgls*/
886 len
= sizeof(struct fnic_sgl_list
);
887 fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
] = kmem_cache_create
888 ("fnic_sgl_max", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
889 SLAB_HWCACHE_ALIGN
| SLAB_CACHE_DMA
,
891 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]) {
892 printk(KERN_ERR PFX
"failed to create fnic max sgl slab\n");
894 goto err_create_fnic_sgl_slab_max
;
897 /* Create a cache of io_req structs for use via mempool */
898 fnic_io_req_cache
= kmem_cache_create("fnic_io_req",
899 sizeof(struct fnic_io_req
),
900 0, SLAB_HWCACHE_ALIGN
, NULL
);
901 if (!fnic_io_req_cache
) {
902 printk(KERN_ERR PFX
"failed to create fnic io_req slab\n");
904 goto err_create_fnic_ioreq_slab
;
907 fnic_event_queue
= create_singlethread_workqueue("fnic_event_wq");
908 if (!fnic_event_queue
) {
909 printk(KERN_ERR PFX
"fnic work queue create failed\n");
911 goto err_create_fnic_workq
;
914 spin_lock_init(&fnic_list_lock
);
915 INIT_LIST_HEAD(&fnic_list
);
917 fnic_fc_transport
= fc_attach_transport(&fnic_fc_functions
);
918 if (!fnic_fc_transport
) {
919 printk(KERN_ERR PFX
"fc_attach_transport error\n");
921 goto err_fc_transport
;
924 /* register the driver with PCI system */
925 err
= pci_register_driver(&fnic_driver
);
927 printk(KERN_ERR PFX
"pci register error\n");
928 goto err_pci_register
;
933 fc_release_transport(fnic_fc_transport
);
935 destroy_workqueue(fnic_event_queue
);
936 err_create_fnic_workq
:
937 kmem_cache_destroy(fnic_io_req_cache
);
938 err_create_fnic_ioreq_slab
:
939 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
940 err_create_fnic_sgl_slab_max
:
941 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
942 err_create_fnic_sgl_slab_dflt
:
946 static void __exit
fnic_cleanup_module(void)
948 pci_unregister_driver(&fnic_driver
);
949 destroy_workqueue(fnic_event_queue
);
950 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
951 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
952 kmem_cache_destroy(fnic_io_req_cache
);
953 fc_release_transport(fnic_fc_transport
);
956 module_init(fnic_init_module
);
957 module_exit(fnic_cleanup_module
);