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 unsigned int fnic_max_qdepth
= FNIC_DFLT_QUEUE_DEPTH
;
78 module_param(fnic_max_qdepth
, uint
, S_IRUGO
|S_IWUSR
);
79 MODULE_PARM_DESC(fnic_max_qdepth
, "Queue depth to report for each LUN");
81 static struct libfc_function_template fnic_transport_template
= {
82 .frame_send
= fnic_send
,
83 .lport_set_port_id
= fnic_set_port_id
,
84 .fcp_abort_io
= fnic_empty_scsi_cleanup
,
85 .fcp_cleanup
= fnic_empty_scsi_cleanup
,
86 .exch_mgr_reset
= fnic_exch_mgr_reset
89 static int fnic_slave_alloc(struct scsi_device
*sdev
)
91 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
93 sdev
->tagged_supported
= 1;
95 if (!rport
|| fc_remote_port_chkready(rport
))
98 scsi_activate_tcq(sdev
, fnic_max_qdepth
);
102 static struct scsi_host_template fnic_host_template
= {
103 .module
= THIS_MODULE
,
105 .queuecommand
= fnic_queuecommand
,
106 .eh_abort_handler
= fnic_abort_cmd
,
107 .eh_device_reset_handler
= fnic_device_reset
,
108 .eh_host_reset_handler
= fnic_host_reset
,
109 .slave_alloc
= fnic_slave_alloc
,
110 .change_queue_depth
= fc_change_queue_depth
,
111 .change_queue_type
= fc_change_queue_type
,
114 .can_queue
= FNIC_MAX_IO_REQ
,
115 .use_clustering
= ENABLE_CLUSTERING
,
116 .sg_tablesize
= FNIC_MAX_SG_DESC_CNT
,
117 .max_sectors
= 0xffff,
118 .shost_attrs
= fnic_attrs
,
122 fnic_set_rport_dev_loss_tmo(struct fc_rport
*rport
, u32 timeout
)
125 rport
->dev_loss_tmo
= timeout
;
127 rport
->dev_loss_tmo
= 1;
130 static void fnic_get_host_speed(struct Scsi_Host
*shost
);
131 static struct scsi_transport_template
*fnic_fc_transport
;
132 static struct fc_host_statistics
*fnic_get_stats(struct Scsi_Host
*);
133 static void fnic_reset_host_stats(struct Scsi_Host
*);
135 static struct fc_function_template fnic_fc_functions
= {
137 .show_host_node_name
= 1,
138 .show_host_port_name
= 1,
139 .show_host_supported_classes
= 1,
140 .show_host_supported_fc4s
= 1,
141 .show_host_active_fc4s
= 1,
142 .show_host_maxframe_size
= 1,
143 .show_host_port_id
= 1,
144 .show_host_supported_speeds
= 1,
145 .get_host_speed
= fnic_get_host_speed
,
146 .show_host_speed
= 1,
147 .show_host_port_type
= 1,
148 .get_host_port_state
= fc_get_host_port_state
,
149 .show_host_port_state
= 1,
150 .show_host_symbolic_name
= 1,
151 .show_rport_maxframe_size
= 1,
152 .show_rport_supported_classes
= 1,
153 .show_host_fabric_name
= 1,
154 .show_starget_node_name
= 1,
155 .show_starget_port_name
= 1,
156 .show_starget_port_id
= 1,
157 .show_rport_dev_loss_tmo
= 1,
158 .set_rport_dev_loss_tmo
= fnic_set_rport_dev_loss_tmo
,
159 .issue_fc_host_lip
= fnic_reset
,
160 .get_fc_host_stats
= fnic_get_stats
,
161 .reset_fc_host_stats
= fnic_reset_host_stats
,
162 .dd_fcrport_size
= sizeof(struct fc_rport_libfc_priv
),
163 .terminate_rport_io
= fnic_terminate_rport_io
,
164 .bsg_request
= fc_lport_bsg_request
,
167 static void fnic_get_host_speed(struct Scsi_Host
*shost
)
169 struct fc_lport
*lp
= shost_priv(shost
);
170 struct fnic
*fnic
= lport_priv(lp
);
171 u32 port_speed
= vnic_dev_port_speed(fnic
->vdev
);
173 /* Add in other values as they get defined in fw */
174 switch (port_speed
) {
176 fc_host_speed(shost
) = FC_PORTSPEED_10GBIT
;
179 fc_host_speed(shost
) = FC_PORTSPEED_10GBIT
;
184 static struct fc_host_statistics
*fnic_get_stats(struct Scsi_Host
*host
)
187 struct fc_lport
*lp
= shost_priv(host
);
188 struct fnic
*fnic
= lport_priv(lp
);
189 struct fc_host_statistics
*stats
= &lp
->host_stats
;
190 struct vnic_stats
*vs
;
193 if (time_before(jiffies
, fnic
->stats_time
+ HZ
/ FNIC_STATS_RATE_LIMIT
))
195 fnic
->stats_time
= jiffies
;
197 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
198 ret
= vnic_dev_stats_dump(fnic
->vdev
, &fnic
->stats
);
199 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
202 FNIC_MAIN_DBG(KERN_DEBUG
, fnic
->lport
->host
,
203 "fnic: Get vnic stats failed"
208 stats
->tx_frames
= vs
->tx
.tx_unicast_frames_ok
;
209 stats
->tx_words
= vs
->tx
.tx_unicast_bytes_ok
/ 4;
210 stats
->rx_frames
= vs
->rx
.rx_unicast_frames_ok
;
211 stats
->rx_words
= vs
->rx
.rx_unicast_bytes_ok
/ 4;
212 stats
->error_frames
= vs
->tx
.tx_errors
+ vs
->rx
.rx_errors
;
213 stats
->dumped_frames
= vs
->tx
.tx_drops
+ vs
->rx
.rx_drop
;
214 stats
->invalid_crc_count
= vs
->rx
.rx_crc_errors
;
215 stats
->seconds_since_last_reset
=
216 (jiffies
- fnic
->stats_reset_time
) / HZ
;
217 stats
->fcp_input_megabytes
= div_u64(fnic
->fcp_input_bytes
, 1000000);
218 stats
->fcp_output_megabytes
= div_u64(fnic
->fcp_output_bytes
, 1000000);
224 * fnic_dump_fchost_stats
225 * note : dumps fc_statistics into system logs
227 void fnic_dump_fchost_stats(struct Scsi_Host
*host
,
228 struct fc_host_statistics
*stats
)
230 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
231 "fnic: seconds since last reset = %llu\n",
232 stats
->seconds_since_last_reset
);
233 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
234 "fnic: tx frames = %llu\n",
236 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
237 "fnic: tx words = %llu\n",
239 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
240 "fnic: rx frames = %llu\n",
242 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
243 "fnic: rx words = %llu\n",
245 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
246 "fnic: lip count = %llu\n",
248 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
249 "fnic: nos count = %llu\n",
251 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
252 "fnic: error frames = %llu\n",
253 stats
->error_frames
);
254 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
255 "fnic: dumped frames = %llu\n",
256 stats
->dumped_frames
);
257 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
258 "fnic: link failure count = %llu\n",
259 stats
->link_failure_count
);
260 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
261 "fnic: loss of sync count = %llu\n",
262 stats
->loss_of_sync_count
);
263 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
264 "fnic: loss of signal count = %llu\n",
265 stats
->loss_of_signal_count
);
266 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
267 "fnic: prim seq protocol err count = %llu\n",
268 stats
->prim_seq_protocol_err_count
);
269 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
270 "fnic: invalid tx word count= %llu\n",
271 stats
->invalid_tx_word_count
);
272 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
273 "fnic: invalid crc count = %llu\n",
274 stats
->invalid_crc_count
);
275 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
276 "fnic: fcp input requests = %llu\n",
277 stats
->fcp_input_requests
);
278 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
279 "fnic: fcp output requests = %llu\n",
280 stats
->fcp_output_requests
);
281 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
282 "fnic: fcp control requests = %llu\n",
283 stats
->fcp_control_requests
);
284 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
285 "fnic: fcp input megabytes = %llu\n",
286 stats
->fcp_input_megabytes
);
287 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
288 "fnic: fcp output megabytes = %llu\n",
289 stats
->fcp_output_megabytes
);
294 * fnic_reset_host_stats : clears host stats
295 * note : called when reset_statistics set under sysfs dir
297 static void fnic_reset_host_stats(struct Scsi_Host
*host
)
300 struct fc_lport
*lp
= shost_priv(host
);
301 struct fnic
*fnic
= lport_priv(lp
);
302 struct fc_host_statistics
*stats
;
305 /* dump current stats, before clearing them */
306 stats
= fnic_get_stats(host
);
307 fnic_dump_fchost_stats(host
, stats
);
309 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
310 ret
= vnic_dev_stats_clear(fnic
->vdev
);
311 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
314 FNIC_MAIN_DBG(KERN_DEBUG
, fnic
->lport
->host
,
315 "fnic: Reset vnic stats failed"
319 fnic
->stats_reset_time
= jiffies
;
320 memset(stats
, 0, sizeof(*stats
));
325 void fnic_log_q_error(struct fnic
*fnic
)
330 for (i
= 0; i
< fnic
->raw_wq_count
; i
++) {
331 error_status
= ioread32(&fnic
->wq
[i
].ctrl
->error_status
);
333 shost_printk(KERN_ERR
, fnic
->lport
->host
,
334 "WQ[%d] error_status"
335 " %d\n", i
, error_status
);
338 for (i
= 0; i
< fnic
->rq_count
; i
++) {
339 error_status
= ioread32(&fnic
->rq
[i
].ctrl
->error_status
);
341 shost_printk(KERN_ERR
, fnic
->lport
->host
,
342 "RQ[%d] error_status"
343 " %d\n", i
, error_status
);
346 for (i
= 0; i
< fnic
->wq_copy_count
; i
++) {
347 error_status
= ioread32(&fnic
->wq_copy
[i
].ctrl
->error_status
);
349 shost_printk(KERN_ERR
, fnic
->lport
->host
,
350 "CWQ[%d] error_status"
351 " %d\n", i
, error_status
);
355 void fnic_handle_link_event(struct fnic
*fnic
)
359 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
360 if (fnic
->stop_rx_link_events
) {
361 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
364 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
366 queue_work(fnic_event_queue
, &fnic
->link_work
);
370 static int fnic_notify_set(struct fnic
*fnic
)
374 switch (vnic_dev_get_intr_mode(fnic
->vdev
)) {
375 case VNIC_DEV_INTR_MODE_INTX
:
376 err
= vnic_dev_notify_set(fnic
->vdev
, FNIC_INTX_NOTIFY
);
378 case VNIC_DEV_INTR_MODE_MSI
:
379 err
= vnic_dev_notify_set(fnic
->vdev
, -1);
381 case VNIC_DEV_INTR_MODE_MSIX
:
382 err
= vnic_dev_notify_set(fnic
->vdev
, FNIC_MSIX_ERR_NOTIFY
);
385 shost_printk(KERN_ERR
, fnic
->lport
->host
,
386 "Interrupt mode should be set up"
387 " before devcmd notify set %d\n",
388 vnic_dev_get_intr_mode(fnic
->vdev
));
396 static void fnic_notify_timer(unsigned long data
)
398 struct fnic
*fnic
= (struct fnic
*)data
;
400 fnic_handle_link_event(fnic
);
401 mod_timer(&fnic
->notify_timer
,
402 round_jiffies(jiffies
+ FNIC_NOTIFY_TIMER_PERIOD
));
405 static void fnic_fip_notify_timer(unsigned long data
)
407 struct fnic
*fnic
= (struct fnic
*)data
;
409 fnic_handle_fip_timer(fnic
);
412 static void fnic_notify_timer_start(struct fnic
*fnic
)
414 switch (vnic_dev_get_intr_mode(fnic
->vdev
)) {
415 case VNIC_DEV_INTR_MODE_MSI
:
417 * Schedule first timeout immediately. The driver is
418 * initiatialized and ready to look for link up notification
420 mod_timer(&fnic
->notify_timer
, jiffies
);
423 /* Using intr for notification for INTx/MSI-X */
428 static int fnic_dev_wait(struct vnic_dev
*vdev
,
429 int (*start
)(struct vnic_dev
*, int),
430 int (*finished
)(struct vnic_dev
*, int *),
437 err
= start(vdev
, arg
);
441 /* Wait for func to complete...2 seconds max */
442 time
= jiffies
+ (HZ
* 2);
444 err
= finished(vdev
, &done
);
449 schedule_timeout_uninterruptible(HZ
/ 10);
450 } while (time_after(time
, jiffies
));
455 static int fnic_cleanup(struct fnic
*fnic
)
460 vnic_dev_disable(fnic
->vdev
);
461 for (i
= 0; i
< fnic
->intr_count
; i
++)
462 vnic_intr_mask(&fnic
->intr
[i
]);
464 for (i
= 0; i
< fnic
->rq_count
; i
++) {
465 err
= vnic_rq_disable(&fnic
->rq
[i
]);
469 for (i
= 0; i
< fnic
->raw_wq_count
; i
++) {
470 err
= vnic_wq_disable(&fnic
->wq
[i
]);
474 for (i
= 0; i
< fnic
->wq_copy_count
; i
++) {
475 err
= vnic_wq_copy_disable(&fnic
->wq_copy
[i
]);
480 /* Clean up completed IOs and FCS frames */
481 fnic_wq_copy_cmpl_handler(fnic
, -1);
482 fnic_wq_cmpl_handler(fnic
, -1);
483 fnic_rq_cmpl_handler(fnic
, -1);
485 /* Clean up the IOs and FCS frames that have not completed */
486 for (i
= 0; i
< fnic
->raw_wq_count
; i
++)
487 vnic_wq_clean(&fnic
->wq
[i
], fnic_free_wq_buf
);
488 for (i
= 0; i
< fnic
->rq_count
; i
++)
489 vnic_rq_clean(&fnic
->rq
[i
], fnic_free_rq_buf
);
490 for (i
= 0; i
< fnic
->wq_copy_count
; i
++)
491 vnic_wq_copy_clean(&fnic
->wq_copy
[i
],
492 fnic_wq_copy_cleanup_handler
);
494 for (i
= 0; i
< fnic
->cq_count
; i
++)
495 vnic_cq_clean(&fnic
->cq
[i
]);
496 for (i
= 0; i
< fnic
->intr_count
; i
++)
497 vnic_intr_clean(&fnic
->intr
[i
]);
499 mempool_destroy(fnic
->io_req_pool
);
500 for (i
= 0; i
< FNIC_SGL_NUM_CACHES
; i
++)
501 mempool_destroy(fnic
->io_sgl_pool
[i
]);
506 static void fnic_iounmap(struct fnic
*fnic
)
508 if (fnic
->bar0
.vaddr
)
509 iounmap(fnic
->bar0
.vaddr
);
513 * fnic_get_mac() - get assigned data MAC address for FIP code.
514 * @lport: local port.
516 static u8
*fnic_get_mac(struct fc_lport
*lport
)
518 struct fnic
*fnic
= lport_priv(lport
);
520 return fnic
->data_src_addr
;
523 static void fnic_set_vlan(struct fnic
*fnic
, u16 vlan_id
)
526 old_vlan
= vnic_dev_set_default_vlan(fnic
->vdev
, vlan_id
);
529 static int fnic_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
531 struct Scsi_Host
*host
;
540 * Allocate SCSI Host and set up association between host,
541 * local port, and fnic
543 lp
= libfc_host_alloc(&fnic_host_template
, sizeof(struct fnic
));
545 printk(KERN_ERR PFX
"Unable to alloc libfc local port\n");
550 fnic
= lport_priv(lp
);
554 snprintf(fnic
->name
, sizeof(fnic
->name
) - 1, "%s%d", DRV_NAME
,
557 host
->transportt
= fnic_fc_transport
;
559 /* Setup PCI resources */
560 pci_set_drvdata(pdev
, fnic
);
564 err
= pci_enable_device(pdev
);
566 shost_printk(KERN_ERR
, fnic
->lport
->host
,
567 "Cannot enable PCI device, aborting.\n");
568 goto err_out_free_hba
;
571 err
= pci_request_regions(pdev
, DRV_NAME
);
573 shost_printk(KERN_ERR
, fnic
->lport
->host
,
574 "Cannot enable PCI resources, aborting\n");
575 goto err_out_disable_device
;
578 pci_set_master(pdev
);
580 /* Query PCI controller on system for DMA addressing
581 * limitation for the device. Try 64-bit first, and
584 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
586 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
588 shost_printk(KERN_ERR
, fnic
->lport
->host
,
589 "No usable DMA configuration "
591 goto err_out_release_regions
;
593 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
595 shost_printk(KERN_ERR
, fnic
->lport
->host
,
596 "Unable to obtain 32-bit DMA "
597 "for consistent allocations, aborting.\n");
598 goto err_out_release_regions
;
601 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
603 shost_printk(KERN_ERR
, fnic
->lport
->host
,
604 "Unable to obtain 64-bit DMA "
605 "for consistent allocations, aborting.\n");
606 goto err_out_release_regions
;
610 /* Map vNIC resources from BAR0 */
611 if (!(pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
)) {
612 shost_printk(KERN_ERR
, fnic
->lport
->host
,
613 "BAR0 not memory-map'able, aborting.\n");
615 goto err_out_release_regions
;
618 fnic
->bar0
.vaddr
= pci_iomap(pdev
, 0, 0);
619 fnic
->bar0
.bus_addr
= pci_resource_start(pdev
, 0);
620 fnic
->bar0
.len
= pci_resource_len(pdev
, 0);
622 if (!fnic
->bar0
.vaddr
) {
623 shost_printk(KERN_ERR
, fnic
->lport
->host
,
624 "Cannot memory-map BAR0 res hdr, "
627 goto err_out_release_regions
;
630 fnic
->vdev
= vnic_dev_register(NULL
, fnic
, pdev
, &fnic
->bar0
);
632 shost_printk(KERN_ERR
, fnic
->lport
->host
,
633 "vNIC registration failed, "
636 goto err_out_iounmap
;
639 err
= fnic_dev_wait(fnic
->vdev
, vnic_dev_open
,
640 vnic_dev_open_done
, 0);
642 shost_printk(KERN_ERR
, fnic
->lport
->host
,
643 "vNIC dev open failed, aborting.\n");
644 goto err_out_vnic_unregister
;
647 err
= vnic_dev_init(fnic
->vdev
, 0);
649 shost_printk(KERN_ERR
, fnic
->lport
->host
,
650 "vNIC dev init failed, aborting.\n");
651 goto err_out_dev_close
;
654 err
= vnic_dev_mac_addr(fnic
->vdev
, fnic
->ctlr
.ctl_src_addr
);
656 shost_printk(KERN_ERR
, fnic
->lport
->host
,
657 "vNIC get MAC addr failed \n");
658 goto err_out_dev_close
;
660 /* set data_src for point-to-point mode and to keep it non-zero */
661 memcpy(fnic
->data_src_addr
, fnic
->ctlr
.ctl_src_addr
, ETH_ALEN
);
663 /* Get vNIC configuration */
664 err
= fnic_get_vnic_config(fnic
);
666 shost_printk(KERN_ERR
, fnic
->lport
->host
,
667 "Get vNIC configuration failed, "
669 goto err_out_dev_close
;
672 /* Configure Maximum Outstanding IO reqs*/
673 if (fnic
->config
.io_throttle_count
!= FNIC_UCSM_DFLT_THROTTLE_CNT_BLD
) {
674 host
->can_queue
= min_t(u32
, FNIC_MAX_IO_REQ
,
675 max_t(u32
, FNIC_MIN_IO_REQ
,
676 fnic
->config
.io_throttle_count
));
678 fnic
->fnic_max_tag_id
= host
->can_queue
;
680 err
= scsi_init_shared_tag_map(host
, fnic
->fnic_max_tag_id
);
682 shost_printk(KERN_ERR
, fnic
->lport
->host
,
683 "Unable to alloc shared tag map\n");
684 goto err_out_dev_close
;
687 host
->max_lun
= fnic
->config
.luns_per_tgt
;
688 host
->max_id
= FNIC_MAX_FCP_TARGET
;
689 host
->max_cmd_len
= FCOE_MAX_CMD_LEN
;
691 fnic_get_res_counts(fnic
);
693 err
= fnic_set_intr_mode(fnic
);
695 shost_printk(KERN_ERR
, fnic
->lport
->host
,
696 "Failed to set intr mode, "
698 goto err_out_dev_close
;
701 err
= fnic_alloc_vnic_resources(fnic
);
703 shost_printk(KERN_ERR
, fnic
->lport
->host
,
704 "Failed to alloc vNIC resources, "
706 goto err_out_clear_intr
;
710 /* initialize all fnic locks */
711 spin_lock_init(&fnic
->fnic_lock
);
713 for (i
= 0; i
< FNIC_WQ_MAX
; i
++)
714 spin_lock_init(&fnic
->wq_lock
[i
]);
716 for (i
= 0; i
< FNIC_WQ_COPY_MAX
; i
++) {
717 spin_lock_init(&fnic
->wq_copy_lock
[i
]);
718 fnic
->wq_copy_desc_low
[i
] = DESC_CLEAN_LOW_WATERMARK
;
719 fnic
->fw_ack_recd
[i
] = 0;
720 fnic
->fw_ack_index
[i
] = -1;
723 for (i
= 0; i
< FNIC_IO_LOCKS
; i
++)
724 spin_lock_init(&fnic
->io_req_lock
[i
]);
726 fnic
->io_req_pool
= mempool_create_slab_pool(2, fnic_io_req_cache
);
727 if (!fnic
->io_req_pool
)
728 goto err_out_free_resources
;
730 pool
= mempool_create_slab_pool(2, fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
732 goto err_out_free_ioreq_pool
;
733 fnic
->io_sgl_pool
[FNIC_SGL_CACHE_DFLT
] = pool
;
735 pool
= mempool_create_slab_pool(2, fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
737 goto err_out_free_dflt_pool
;
738 fnic
->io_sgl_pool
[FNIC_SGL_CACHE_MAX
] = pool
;
740 /* setup vlan config, hw inserts vlan header */
741 fnic
->vlan_hw_insert
= 1;
744 /* Initialize the FIP fcoe_ctrl struct */
745 fnic
->ctlr
.send
= fnic_eth_send
;
746 fnic
->ctlr
.update_mac
= fnic_update_mac
;
747 fnic
->ctlr
.get_src_addr
= fnic_get_mac
;
748 if (fnic
->config
.flags
& VFCF_FIP_CAPABLE
) {
749 shost_printk(KERN_INFO
, fnic
->lport
->host
,
750 "firmware supports FIP\n");
751 /* enable directed and multicast */
752 vnic_dev_packet_filter(fnic
->vdev
, 1, 1, 0, 0, 0);
753 vnic_dev_add_addr(fnic
->vdev
, FIP_ALL_ENODE_MACS
);
754 vnic_dev_add_addr(fnic
->vdev
, fnic
->ctlr
.ctl_src_addr
);
755 fnic
->set_vlan
= fnic_set_vlan
;
756 fcoe_ctlr_init(&fnic
->ctlr
, FIP_MODE_AUTO
);
757 setup_timer(&fnic
->fip_timer
, fnic_fip_notify_timer
,
758 (unsigned long)fnic
);
759 spin_lock_init(&fnic
->vlans_lock
);
760 INIT_WORK(&fnic
->fip_frame_work
, fnic_handle_fip_frame
);
761 INIT_WORK(&fnic
->event_work
, fnic_handle_event
);
762 skb_queue_head_init(&fnic
->fip_frame_queue
);
763 INIT_LIST_HEAD(&fnic
->evlist
);
764 INIT_LIST_HEAD(&fnic
->vlans
);
766 shost_printk(KERN_INFO
, fnic
->lport
->host
,
767 "firmware uses non-FIP mode\n");
768 fcoe_ctlr_init(&fnic
->ctlr
, FIP_MODE_NON_FIP
);
770 fnic
->state
= FNIC_IN_FC_MODE
;
772 atomic_set(&fnic
->in_flight
, 0);
773 fnic
->state_flags
= FNIC_FLAGS_NONE
;
775 /* Enable hardware stripping of vlan header on ingress */
776 fnic_set_nic_config(fnic
, 0, 0, 0, 0, 0, 0, 1);
778 /* Setup notification buffer area */
779 err
= fnic_notify_set(fnic
);
781 shost_printk(KERN_ERR
, fnic
->lport
->host
,
782 "Failed to alloc notify buffer, aborting.\n");
783 goto err_out_free_max_pool
;
786 /* Setup notify timer when using MSI interrupts */
787 if (vnic_dev_get_intr_mode(fnic
->vdev
) == VNIC_DEV_INTR_MODE_MSI
)
788 setup_timer(&fnic
->notify_timer
,
789 fnic_notify_timer
, (unsigned long)fnic
);
791 /* allocate RQ buffers and post them to RQ*/
792 for (i
= 0; i
< fnic
->rq_count
; i
++) {
793 err
= vnic_rq_fill(&fnic
->rq
[i
], fnic_alloc_rq_frame
);
795 shost_printk(KERN_ERR
, fnic
->lport
->host
,
796 "fnic_alloc_rq_frame can't alloc "
798 goto err_out_free_rq_buf
;
803 * Initialization done with PCI system, hardware, firmware.
806 err
= scsi_add_host(lp
->host
, &pdev
->dev
);
808 shost_printk(KERN_ERR
, fnic
->lport
->host
,
809 "fnic: scsi_add_host failed...exiting\n");
810 goto err_out_free_rq_buf
;
813 /* Start local port initiatialization */
817 lp
->max_retry_count
= fnic
->config
.flogi_retries
;
818 lp
->max_rport_retry_count
= fnic
->config
.plogi_retries
;
819 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
820 FCP_SPPF_CONF_COMPL
);
821 if (fnic
->config
.flags
& VFCF_FCP_SEQ_LVL_ERR
)
822 lp
->service_params
|= FCP_SPPF_RETRY
;
824 lp
->boot_time
= jiffies
;
825 lp
->e_d_tov
= fnic
->config
.ed_tov
;
826 lp
->r_a_tov
= fnic
->config
.ra_tov
;
827 lp
->link_supported_speeds
= FC_PORTSPEED_10GBIT
;
828 fc_set_wwnn(lp
, fnic
->config
.node_wwn
);
829 fc_set_wwpn(lp
, fnic
->config
.port_wwn
);
831 fcoe_libfc_config(lp
, &fnic
->ctlr
, &fnic_transport_template
, 0);
833 if (!fc_exch_mgr_alloc(lp
, FC_CLASS_3
, FCPIO_HOST_EXCH_RANGE_START
,
834 FCPIO_HOST_EXCH_RANGE_END
, NULL
)) {
836 goto err_out_remove_scsi_host
;
839 fc_lport_init_stats(lp
);
840 fnic
->stats_reset_time
= jiffies
;
844 if (fc_set_mfs(lp
, fnic
->config
.maxdatafieldsize
+
845 sizeof(struct fc_frame_header
))) {
847 goto err_out_free_exch_mgr
;
849 fc_host_maxframe_size(lp
->host
) = lp
->mfs
;
850 fc_host_dev_loss_tmo(lp
->host
) = fnic
->config
.port_down_timeout
/ 1000;
852 sprintf(fc_host_symbolic_name(lp
->host
),
853 DRV_NAME
" v" DRV_VERSION
" over %s", fnic
->name
);
855 spin_lock_irqsave(&fnic_list_lock
, flags
);
856 list_add_tail(&fnic
->list
, &fnic_list
);
857 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
859 INIT_WORK(&fnic
->link_work
, fnic_handle_link
);
860 INIT_WORK(&fnic
->frame_work
, fnic_handle_frame
);
861 skb_queue_head_init(&fnic
->frame_queue
);
862 skb_queue_head_init(&fnic
->tx_queue
);
864 /* Enable all queues */
865 for (i
= 0; i
< fnic
->raw_wq_count
; i
++)
866 vnic_wq_enable(&fnic
->wq
[i
]);
867 for (i
= 0; i
< fnic
->rq_count
; i
++)
868 vnic_rq_enable(&fnic
->rq
[i
]);
869 for (i
= 0; i
< fnic
->wq_copy_count
; i
++)
870 vnic_wq_copy_enable(&fnic
->wq_copy
[i
]);
874 vnic_dev_enable(fnic
->vdev
);
876 err
= fnic_request_intr(fnic
);
878 shost_printk(KERN_ERR
, fnic
->lport
->host
,
879 "Unable to request irq.\n");
880 goto err_out_free_exch_mgr
;
883 for (i
= 0; i
< fnic
->intr_count
; i
++)
884 vnic_intr_unmask(&fnic
->intr
[i
]);
886 fnic_notify_timer_start(fnic
);
890 err_out_free_exch_mgr
:
891 fc_exch_mgr_free(lp
);
892 err_out_remove_scsi_host
:
893 fc_remove_host(lp
->host
);
894 scsi_remove_host(lp
->host
);
896 for (i
= 0; i
< fnic
->rq_count
; i
++)
897 vnic_rq_clean(&fnic
->rq
[i
], fnic_free_rq_buf
);
898 vnic_dev_notify_unset(fnic
->vdev
);
899 err_out_free_max_pool
:
900 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_MAX
]);
901 err_out_free_dflt_pool
:
902 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_DFLT
]);
903 err_out_free_ioreq_pool
:
904 mempool_destroy(fnic
->io_req_pool
);
905 err_out_free_resources
:
906 fnic_free_vnic_resources(fnic
);
908 fnic_clear_intr_mode(fnic
);
910 vnic_dev_close(fnic
->vdev
);
911 err_out_vnic_unregister
:
912 vnic_dev_unregister(fnic
->vdev
);
915 err_out_release_regions
:
916 pci_release_regions(pdev
);
917 err_out_disable_device
:
918 pci_disable_device(pdev
);
920 scsi_host_put(lp
->host
);
925 static void fnic_remove(struct pci_dev
*pdev
)
927 struct fnic
*fnic
= pci_get_drvdata(pdev
);
928 struct fc_lport
*lp
= fnic
->lport
;
932 * Mark state so that the workqueue thread stops forwarding
933 * received frames and link events to the local port. ISR and
934 * other threads that can queue work items will also stop
935 * creating work items on the fnic workqueue
937 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
938 fnic
->stop_rx_link_events
= 1;
939 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
941 if (vnic_dev_get_intr_mode(fnic
->vdev
) == VNIC_DEV_INTR_MODE_MSI
)
942 del_timer_sync(&fnic
->notify_timer
);
945 * Flush the fnic event queue. After this call, there should
946 * be no event queued for this fnic device in the workqueue
948 flush_workqueue(fnic_event_queue
);
949 skb_queue_purge(&fnic
->frame_queue
);
950 skb_queue_purge(&fnic
->tx_queue
);
952 if (fnic
->config
.flags
& VFCF_FIP_CAPABLE
) {
953 del_timer_sync(&fnic
->fip_timer
);
954 skb_queue_purge(&fnic
->fip_frame_queue
);
955 fnic_fcoe_reset_vlans(fnic
);
956 fnic_fcoe_evlist_free(fnic
);
960 * Log off the fabric. This stops all remote ports, dns port,
961 * logs off the fabric. This flushes all rport, disc, lport work
964 fc_fabric_logoff(fnic
->lport
);
966 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
968 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
970 fcoe_ctlr_destroy(&fnic
->ctlr
);
971 fc_lport_destroy(lp
);
974 * This stops the fnic device, masks all interrupts. Completed
975 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
980 BUG_ON(!skb_queue_empty(&fnic
->frame_queue
));
981 BUG_ON(!skb_queue_empty(&fnic
->tx_queue
));
983 spin_lock_irqsave(&fnic_list_lock
, flags
);
984 list_del(&fnic
->list
);
985 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
987 fc_remove_host(fnic
->lport
->host
);
988 scsi_remove_host(fnic
->lport
->host
);
989 fc_exch_mgr_free(fnic
->lport
);
990 vnic_dev_notify_unset(fnic
->vdev
);
991 fnic_free_intr(fnic
);
992 fnic_free_vnic_resources(fnic
);
993 fnic_clear_intr_mode(fnic
);
994 vnic_dev_close(fnic
->vdev
);
995 vnic_dev_unregister(fnic
->vdev
);
997 pci_release_regions(pdev
);
998 pci_disable_device(pdev
);
999 pci_set_drvdata(pdev
, NULL
);
1000 scsi_host_put(lp
->host
);
1003 static struct pci_driver fnic_driver
= {
1005 .id_table
= fnic_id_table
,
1006 .probe
= fnic_probe
,
1007 .remove
= fnic_remove
,
1010 static int __init
fnic_init_module(void)
1015 printk(KERN_INFO PFX
"%s, ver %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
1017 /* Allocate memory for trace buffer */
1018 err
= fnic_trace_buf_init();
1020 printk(KERN_ERR PFX
"Trace buffer initialization Failed "
1021 "Fnic Tracing utility is disabled\n");
1025 /* Create a cache for allocation of default size sgls */
1026 len
= sizeof(struct fnic_dflt_sgl_list
);
1027 fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
] = kmem_cache_create
1028 ("fnic_sgl_dflt", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
1031 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]) {
1032 printk(KERN_ERR PFX
"failed to create fnic dflt sgl slab\n");
1034 goto err_create_fnic_sgl_slab_dflt
;
1037 /* Create a cache for allocation of max size sgls*/
1038 len
= sizeof(struct fnic_sgl_list
);
1039 fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
] = kmem_cache_create
1040 ("fnic_sgl_max", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
1043 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]) {
1044 printk(KERN_ERR PFX
"failed to create fnic max sgl slab\n");
1046 goto err_create_fnic_sgl_slab_max
;
1049 /* Create a cache of io_req structs for use via mempool */
1050 fnic_io_req_cache
= kmem_cache_create("fnic_io_req",
1051 sizeof(struct fnic_io_req
),
1052 0, SLAB_HWCACHE_ALIGN
, NULL
);
1053 if (!fnic_io_req_cache
) {
1054 printk(KERN_ERR PFX
"failed to create fnic io_req slab\n");
1056 goto err_create_fnic_ioreq_slab
;
1059 fnic_event_queue
= create_singlethread_workqueue("fnic_event_wq");
1060 if (!fnic_event_queue
) {
1061 printk(KERN_ERR PFX
"fnic work queue create failed\n");
1063 goto err_create_fnic_workq
;
1066 spin_lock_init(&fnic_list_lock
);
1067 INIT_LIST_HEAD(&fnic_list
);
1069 fnic_fip_queue
= create_singlethread_workqueue("fnic_fip_q");
1070 if (!fnic_fip_queue
) {
1071 printk(KERN_ERR PFX
"fnic FIP work queue create failed\n");
1073 goto err_create_fip_workq
;
1076 fnic_fc_transport
= fc_attach_transport(&fnic_fc_functions
);
1077 if (!fnic_fc_transport
) {
1078 printk(KERN_ERR PFX
"fc_attach_transport error\n");
1080 goto err_fc_transport
;
1083 /* register the driver with PCI system */
1084 err
= pci_register_driver(&fnic_driver
);
1086 printk(KERN_ERR PFX
"pci register error\n");
1087 goto err_pci_register
;
1092 fc_release_transport(fnic_fc_transport
);
1094 destroy_workqueue(fnic_fip_queue
);
1095 err_create_fip_workq
:
1096 destroy_workqueue(fnic_event_queue
);
1097 err_create_fnic_workq
:
1098 kmem_cache_destroy(fnic_io_req_cache
);
1099 err_create_fnic_ioreq_slab
:
1100 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
1101 err_create_fnic_sgl_slab_max
:
1102 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
1103 err_create_fnic_sgl_slab_dflt
:
1108 static void __exit
fnic_cleanup_module(void)
1110 pci_unregister_driver(&fnic_driver
);
1111 destroy_workqueue(fnic_event_queue
);
1112 if (fnic_fip_queue
) {
1113 flush_workqueue(fnic_fip_queue
);
1114 destroy_workqueue(fnic_fip_queue
);
1116 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
1117 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
1118 kmem_cache_destroy(fnic_io_req_cache
);
1119 fc_release_transport(fnic_fc_transport
);
1123 module_init(fnic_init_module
);
1124 module_exit(fnic_cleanup_module
);