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 unsigned int fnic_fc_trace_max_pages
= 64;
78 module_param(fnic_fc_trace_max_pages
, uint
, S_IRUGO
|S_IWUSR
);
79 MODULE_PARM_DESC(fnic_fc_trace_max_pages
,
80 "Total allocated memory pages for fc trace buffer");
82 static unsigned int fnic_max_qdepth
= FNIC_DFLT_QUEUE_DEPTH
;
83 module_param(fnic_max_qdepth
, uint
, S_IRUGO
|S_IWUSR
);
84 MODULE_PARM_DESC(fnic_max_qdepth
, "Queue depth to report for each LUN");
86 static struct libfc_function_template fnic_transport_template
= {
87 .frame_send
= fnic_send
,
88 .lport_set_port_id
= fnic_set_port_id
,
89 .fcp_abort_io
= fnic_empty_scsi_cleanup
,
90 .fcp_cleanup
= fnic_empty_scsi_cleanup
,
91 .exch_mgr_reset
= fnic_exch_mgr_reset
94 static int fnic_slave_alloc(struct scsi_device
*sdev
)
96 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
98 if (!rport
|| fc_remote_port_chkready(rport
))
101 scsi_change_queue_depth(sdev
, fnic_max_qdepth
);
105 static struct scsi_host_template fnic_host_template
= {
106 .module
= THIS_MODULE
,
108 .queuecommand
= fnic_queuecommand
,
109 .eh_timed_out
= fc_eh_timed_out
,
110 .eh_abort_handler
= fnic_abort_cmd
,
111 .eh_device_reset_handler
= fnic_device_reset
,
112 .eh_host_reset_handler
= fnic_host_reset
,
113 .slave_alloc
= fnic_slave_alloc
,
114 .change_queue_depth
= scsi_change_queue_depth
,
117 .can_queue
= FNIC_DFLT_IO_REQ
,
118 .use_clustering
= ENABLE_CLUSTERING
,
119 .sg_tablesize
= FNIC_MAX_SG_DESC_CNT
,
120 .max_sectors
= 0xffff,
121 .shost_attrs
= fnic_attrs
,
122 .track_queue_depth
= 1,
126 fnic_set_rport_dev_loss_tmo(struct fc_rport
*rport
, u32 timeout
)
129 rport
->dev_loss_tmo
= timeout
;
131 rport
->dev_loss_tmo
= 1;
134 static void fnic_get_host_speed(struct Scsi_Host
*shost
);
135 static struct scsi_transport_template
*fnic_fc_transport
;
136 static struct fc_host_statistics
*fnic_get_stats(struct Scsi_Host
*);
137 static void fnic_reset_host_stats(struct Scsi_Host
*);
139 static struct fc_function_template fnic_fc_functions
= {
141 .show_host_node_name
= 1,
142 .show_host_port_name
= 1,
143 .show_host_supported_classes
= 1,
144 .show_host_supported_fc4s
= 1,
145 .show_host_active_fc4s
= 1,
146 .show_host_maxframe_size
= 1,
147 .show_host_port_id
= 1,
148 .show_host_supported_speeds
= 1,
149 .get_host_speed
= fnic_get_host_speed
,
150 .show_host_speed
= 1,
151 .show_host_port_type
= 1,
152 .get_host_port_state
= fc_get_host_port_state
,
153 .show_host_port_state
= 1,
154 .show_host_symbolic_name
= 1,
155 .show_rport_maxframe_size
= 1,
156 .show_rport_supported_classes
= 1,
157 .show_host_fabric_name
= 1,
158 .show_starget_node_name
= 1,
159 .show_starget_port_name
= 1,
160 .show_starget_port_id
= 1,
161 .show_rport_dev_loss_tmo
= 1,
162 .set_rport_dev_loss_tmo
= fnic_set_rport_dev_loss_tmo
,
163 .issue_fc_host_lip
= fnic_reset
,
164 .get_fc_host_stats
= fnic_get_stats
,
165 .reset_fc_host_stats
= fnic_reset_host_stats
,
166 .dd_fcrport_size
= sizeof(struct fc_rport_libfc_priv
),
167 .terminate_rport_io
= fnic_terminate_rport_io
,
168 .bsg_request
= fc_lport_bsg_request
,
171 static void fnic_get_host_speed(struct Scsi_Host
*shost
)
173 struct fc_lport
*lp
= shost_priv(shost
);
174 struct fnic
*fnic
= lport_priv(lp
);
175 u32 port_speed
= vnic_dev_port_speed(fnic
->vdev
);
177 /* Add in other values as they get defined in fw */
178 switch (port_speed
) {
179 case DCEM_PORTSPEED_10G
:
180 fc_host_speed(shost
) = FC_PORTSPEED_10GBIT
;
182 case DCEM_PORTSPEED_25G
:
183 fc_host_speed(shost
) = FC_PORTSPEED_25GBIT
;
185 case DCEM_PORTSPEED_40G
:
186 case DCEM_PORTSPEED_4x10G
:
187 fc_host_speed(shost
) = FC_PORTSPEED_40GBIT
;
189 case DCEM_PORTSPEED_100G
:
190 fc_host_speed(shost
) = FC_PORTSPEED_100GBIT
;
193 fc_host_speed(shost
) = FC_PORTSPEED_UNKNOWN
;
198 static struct fc_host_statistics
*fnic_get_stats(struct Scsi_Host
*host
)
201 struct fc_lport
*lp
= shost_priv(host
);
202 struct fnic
*fnic
= lport_priv(lp
);
203 struct fc_host_statistics
*stats
= &lp
->host_stats
;
204 struct vnic_stats
*vs
;
207 if (time_before(jiffies
, fnic
->stats_time
+ HZ
/ FNIC_STATS_RATE_LIMIT
))
209 fnic
->stats_time
= jiffies
;
211 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
212 ret
= vnic_dev_stats_dump(fnic
->vdev
, &fnic
->stats
);
213 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
216 FNIC_MAIN_DBG(KERN_DEBUG
, fnic
->lport
->host
,
217 "fnic: Get vnic stats failed"
222 stats
->tx_frames
= vs
->tx
.tx_unicast_frames_ok
;
223 stats
->tx_words
= vs
->tx
.tx_unicast_bytes_ok
/ 4;
224 stats
->rx_frames
= vs
->rx
.rx_unicast_frames_ok
;
225 stats
->rx_words
= vs
->rx
.rx_unicast_bytes_ok
/ 4;
226 stats
->error_frames
= vs
->tx
.tx_errors
+ vs
->rx
.rx_errors
;
227 stats
->dumped_frames
= vs
->tx
.tx_drops
+ vs
->rx
.rx_drop
;
228 stats
->invalid_crc_count
= vs
->rx
.rx_crc_errors
;
229 stats
->seconds_since_last_reset
=
230 (jiffies
- fnic
->stats_reset_time
) / HZ
;
231 stats
->fcp_input_megabytes
= div_u64(fnic
->fcp_input_bytes
, 1000000);
232 stats
->fcp_output_megabytes
= div_u64(fnic
->fcp_output_bytes
, 1000000);
238 * fnic_dump_fchost_stats
239 * note : dumps fc_statistics into system logs
241 void fnic_dump_fchost_stats(struct Scsi_Host
*host
,
242 struct fc_host_statistics
*stats
)
244 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
245 "fnic: seconds since last reset = %llu\n",
246 stats
->seconds_since_last_reset
);
247 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
248 "fnic: tx frames = %llu\n",
250 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
251 "fnic: tx words = %llu\n",
253 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
254 "fnic: rx frames = %llu\n",
256 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
257 "fnic: rx words = %llu\n",
259 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
260 "fnic: lip count = %llu\n",
262 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
263 "fnic: nos count = %llu\n",
265 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
266 "fnic: error frames = %llu\n",
267 stats
->error_frames
);
268 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
269 "fnic: dumped frames = %llu\n",
270 stats
->dumped_frames
);
271 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
272 "fnic: link failure count = %llu\n",
273 stats
->link_failure_count
);
274 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
275 "fnic: loss of sync count = %llu\n",
276 stats
->loss_of_sync_count
);
277 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
278 "fnic: loss of signal count = %llu\n",
279 stats
->loss_of_signal_count
);
280 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
281 "fnic: prim seq protocol err count = %llu\n",
282 stats
->prim_seq_protocol_err_count
);
283 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
284 "fnic: invalid tx word count= %llu\n",
285 stats
->invalid_tx_word_count
);
286 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
287 "fnic: invalid crc count = %llu\n",
288 stats
->invalid_crc_count
);
289 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
290 "fnic: fcp input requests = %llu\n",
291 stats
->fcp_input_requests
);
292 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
293 "fnic: fcp output requests = %llu\n",
294 stats
->fcp_output_requests
);
295 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
296 "fnic: fcp control requests = %llu\n",
297 stats
->fcp_control_requests
);
298 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
299 "fnic: fcp input megabytes = %llu\n",
300 stats
->fcp_input_megabytes
);
301 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
302 "fnic: fcp output megabytes = %llu\n",
303 stats
->fcp_output_megabytes
);
308 * fnic_reset_host_stats : clears host stats
309 * note : called when reset_statistics set under sysfs dir
311 static void fnic_reset_host_stats(struct Scsi_Host
*host
)
314 struct fc_lport
*lp
= shost_priv(host
);
315 struct fnic
*fnic
= lport_priv(lp
);
316 struct fc_host_statistics
*stats
;
319 /* dump current stats, before clearing them */
320 stats
= fnic_get_stats(host
);
321 fnic_dump_fchost_stats(host
, stats
);
323 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
324 ret
= vnic_dev_stats_clear(fnic
->vdev
);
325 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
328 FNIC_MAIN_DBG(KERN_DEBUG
, fnic
->lport
->host
,
329 "fnic: Reset vnic stats failed"
333 fnic
->stats_reset_time
= jiffies
;
334 memset(stats
, 0, sizeof(*stats
));
339 void fnic_log_q_error(struct fnic
*fnic
)
344 for (i
= 0; i
< fnic
->raw_wq_count
; i
++) {
345 error_status
= ioread32(&fnic
->wq
[i
].ctrl
->error_status
);
347 shost_printk(KERN_ERR
, fnic
->lport
->host
,
348 "WQ[%d] error_status"
349 " %d\n", i
, error_status
);
352 for (i
= 0; i
< fnic
->rq_count
; i
++) {
353 error_status
= ioread32(&fnic
->rq
[i
].ctrl
->error_status
);
355 shost_printk(KERN_ERR
, fnic
->lport
->host
,
356 "RQ[%d] error_status"
357 " %d\n", i
, error_status
);
360 for (i
= 0; i
< fnic
->wq_copy_count
; i
++) {
361 error_status
= ioread32(&fnic
->wq_copy
[i
].ctrl
->error_status
);
363 shost_printk(KERN_ERR
, fnic
->lport
->host
,
364 "CWQ[%d] error_status"
365 " %d\n", i
, error_status
);
369 void fnic_handle_link_event(struct fnic
*fnic
)
373 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
374 if (fnic
->stop_rx_link_events
) {
375 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
378 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
380 queue_work(fnic_event_queue
, &fnic
->link_work
);
384 static int fnic_notify_set(struct fnic
*fnic
)
388 switch (vnic_dev_get_intr_mode(fnic
->vdev
)) {
389 case VNIC_DEV_INTR_MODE_INTX
:
390 err
= vnic_dev_notify_set(fnic
->vdev
, FNIC_INTX_NOTIFY
);
392 case VNIC_DEV_INTR_MODE_MSI
:
393 err
= vnic_dev_notify_set(fnic
->vdev
, -1);
395 case VNIC_DEV_INTR_MODE_MSIX
:
396 err
= vnic_dev_notify_set(fnic
->vdev
, FNIC_MSIX_ERR_NOTIFY
);
399 shost_printk(KERN_ERR
, fnic
->lport
->host
,
400 "Interrupt mode should be set up"
401 " before devcmd notify set %d\n",
402 vnic_dev_get_intr_mode(fnic
->vdev
));
410 static void fnic_notify_timer(struct timer_list
*t
)
412 struct fnic
*fnic
= from_timer(fnic
, t
, notify_timer
);
414 fnic_handle_link_event(fnic
);
415 mod_timer(&fnic
->notify_timer
,
416 round_jiffies(jiffies
+ FNIC_NOTIFY_TIMER_PERIOD
));
419 static void fnic_fip_notify_timer(struct timer_list
*t
)
421 struct fnic
*fnic
= from_timer(fnic
, t
, fip_timer
);
423 fnic_handle_fip_timer(fnic
);
426 static void fnic_notify_timer_start(struct fnic
*fnic
)
428 switch (vnic_dev_get_intr_mode(fnic
->vdev
)) {
429 case VNIC_DEV_INTR_MODE_MSI
:
431 * Schedule first timeout immediately. The driver is
432 * initiatialized and ready to look for link up notification
434 mod_timer(&fnic
->notify_timer
, jiffies
);
437 /* Using intr for notification for INTx/MSI-X */
442 static int fnic_dev_wait(struct vnic_dev
*vdev
,
443 int (*start
)(struct vnic_dev
*, int),
444 int (*finished
)(struct vnic_dev
*, int *),
454 err
= start(vdev
, arg
);
458 /* Wait for func to complete.
459 * Sometime schedule_timeout_uninterruptible take long time
460 * to wake up so we do not retry as we are only waiting for
461 * 2 seconds in while loop. By adding count, we make sure
462 * we try atleast three times before returning -ETIMEDOUT
464 time
= jiffies
+ (HZ
* 2);
466 err
= finished(vdev
, &done
);
472 schedule_timeout_uninterruptible(HZ
/ 10);
473 } while (time_after(time
, jiffies
) || (count
< 3));
478 static int fnic_cleanup(struct fnic
*fnic
)
483 vnic_dev_disable(fnic
->vdev
);
484 for (i
= 0; i
< fnic
->intr_count
; i
++)
485 vnic_intr_mask(&fnic
->intr
[i
]);
487 for (i
= 0; i
< fnic
->rq_count
; i
++) {
488 err
= vnic_rq_disable(&fnic
->rq
[i
]);
492 for (i
= 0; i
< fnic
->raw_wq_count
; i
++) {
493 err
= vnic_wq_disable(&fnic
->wq
[i
]);
497 for (i
= 0; i
< fnic
->wq_copy_count
; i
++) {
498 err
= vnic_wq_copy_disable(&fnic
->wq_copy
[i
]);
503 /* Clean up completed IOs and FCS frames */
504 fnic_wq_copy_cmpl_handler(fnic
, -1);
505 fnic_wq_cmpl_handler(fnic
, -1);
506 fnic_rq_cmpl_handler(fnic
, -1);
508 /* Clean up the IOs and FCS frames that have not completed */
509 for (i
= 0; i
< fnic
->raw_wq_count
; i
++)
510 vnic_wq_clean(&fnic
->wq
[i
], fnic_free_wq_buf
);
511 for (i
= 0; i
< fnic
->rq_count
; i
++)
512 vnic_rq_clean(&fnic
->rq
[i
], fnic_free_rq_buf
);
513 for (i
= 0; i
< fnic
->wq_copy_count
; i
++)
514 vnic_wq_copy_clean(&fnic
->wq_copy
[i
],
515 fnic_wq_copy_cleanup_handler
);
517 for (i
= 0; i
< fnic
->cq_count
; i
++)
518 vnic_cq_clean(&fnic
->cq
[i
]);
519 for (i
= 0; i
< fnic
->intr_count
; i
++)
520 vnic_intr_clean(&fnic
->intr
[i
]);
522 mempool_destroy(fnic
->io_req_pool
);
523 for (i
= 0; i
< FNIC_SGL_NUM_CACHES
; i
++)
524 mempool_destroy(fnic
->io_sgl_pool
[i
]);
529 static void fnic_iounmap(struct fnic
*fnic
)
531 if (fnic
->bar0
.vaddr
)
532 iounmap(fnic
->bar0
.vaddr
);
536 * fnic_get_mac() - get assigned data MAC address for FIP code.
537 * @lport: local port.
539 static u8
*fnic_get_mac(struct fc_lport
*lport
)
541 struct fnic
*fnic
= lport_priv(lport
);
543 return fnic
->data_src_addr
;
546 static void fnic_set_vlan(struct fnic
*fnic
, u16 vlan_id
)
549 old_vlan
= vnic_dev_set_default_vlan(fnic
->vdev
, vlan_id
);
552 static int fnic_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
554 struct Scsi_Host
*host
;
563 * Allocate SCSI Host and set up association between host,
564 * local port, and fnic
566 lp
= libfc_host_alloc(&fnic_host_template
, sizeof(struct fnic
));
568 printk(KERN_ERR PFX
"Unable to alloc libfc local port\n");
573 fnic
= lport_priv(lp
);
577 snprintf(fnic
->name
, sizeof(fnic
->name
) - 1, "%s%d", DRV_NAME
,
580 host
->transportt
= fnic_fc_transport
;
582 err
= fnic_stats_debugfs_init(fnic
);
584 shost_printk(KERN_ERR
, fnic
->lport
->host
,
585 "Failed to initialize debugfs for stats\n");
586 fnic_stats_debugfs_remove(fnic
);
589 /* Setup PCI resources */
590 pci_set_drvdata(pdev
, fnic
);
594 err
= pci_enable_device(pdev
);
596 shost_printk(KERN_ERR
, fnic
->lport
->host
,
597 "Cannot enable PCI device, aborting.\n");
598 goto err_out_free_hba
;
601 err
= pci_request_regions(pdev
, DRV_NAME
);
603 shost_printk(KERN_ERR
, fnic
->lport
->host
,
604 "Cannot enable PCI resources, aborting\n");
605 goto err_out_disable_device
;
608 pci_set_master(pdev
);
610 /* Query PCI controller on system for DMA addressing
611 * limitation for the device. Try 64-bit first, and
614 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
616 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
618 shost_printk(KERN_ERR
, fnic
->lport
->host
,
619 "No usable DMA configuration "
621 goto err_out_release_regions
;
623 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
625 shost_printk(KERN_ERR
, fnic
->lport
->host
,
626 "Unable to obtain 32-bit DMA "
627 "for consistent allocations, aborting.\n");
628 goto err_out_release_regions
;
631 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
633 shost_printk(KERN_ERR
, fnic
->lport
->host
,
634 "Unable to obtain 64-bit DMA "
635 "for consistent allocations, aborting.\n");
636 goto err_out_release_regions
;
640 /* Map vNIC resources from BAR0 */
641 if (!(pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
)) {
642 shost_printk(KERN_ERR
, fnic
->lport
->host
,
643 "BAR0 not memory-map'able, aborting.\n");
645 goto err_out_release_regions
;
648 fnic
->bar0
.vaddr
= pci_iomap(pdev
, 0, 0);
649 fnic
->bar0
.bus_addr
= pci_resource_start(pdev
, 0);
650 fnic
->bar0
.len
= pci_resource_len(pdev
, 0);
652 if (!fnic
->bar0
.vaddr
) {
653 shost_printk(KERN_ERR
, fnic
->lport
->host
,
654 "Cannot memory-map BAR0 res hdr, "
657 goto err_out_release_regions
;
660 fnic
->vdev
= vnic_dev_register(NULL
, fnic
, pdev
, &fnic
->bar0
);
662 shost_printk(KERN_ERR
, fnic
->lport
->host
,
663 "vNIC registration failed, "
666 goto err_out_iounmap
;
669 err
= fnic_dev_wait(fnic
->vdev
, vnic_dev_open
,
670 vnic_dev_open_done
, 0);
672 shost_printk(KERN_ERR
, fnic
->lport
->host
,
673 "vNIC dev open failed, aborting.\n");
674 goto err_out_vnic_unregister
;
677 err
= vnic_dev_init(fnic
->vdev
, 0);
679 shost_printk(KERN_ERR
, fnic
->lport
->host
,
680 "vNIC dev init failed, aborting.\n");
681 goto err_out_dev_close
;
684 err
= vnic_dev_mac_addr(fnic
->vdev
, fnic
->ctlr
.ctl_src_addr
);
686 shost_printk(KERN_ERR
, fnic
->lport
->host
,
687 "vNIC get MAC addr failed \n");
688 goto err_out_dev_close
;
690 /* set data_src for point-to-point mode and to keep it non-zero */
691 memcpy(fnic
->data_src_addr
, fnic
->ctlr
.ctl_src_addr
, ETH_ALEN
);
693 /* Get vNIC configuration */
694 err
= fnic_get_vnic_config(fnic
);
696 shost_printk(KERN_ERR
, fnic
->lport
->host
,
697 "Get vNIC configuration failed, "
699 goto err_out_dev_close
;
702 /* Configure Maximum Outstanding IO reqs*/
703 if (fnic
->config
.io_throttle_count
!= FNIC_UCSM_DFLT_THROTTLE_CNT_BLD
) {
704 host
->can_queue
= min_t(u32
, FNIC_MAX_IO_REQ
,
705 max_t(u32
, FNIC_MIN_IO_REQ
,
706 fnic
->config
.io_throttle_count
));
708 fnic
->fnic_max_tag_id
= host
->can_queue
;
710 host
->max_lun
= fnic
->config
.luns_per_tgt
;
711 host
->max_id
= FNIC_MAX_FCP_TARGET
;
712 host
->max_cmd_len
= FCOE_MAX_CMD_LEN
;
714 fnic_get_res_counts(fnic
);
716 err
= fnic_set_intr_mode(fnic
);
718 shost_printk(KERN_ERR
, fnic
->lport
->host
,
719 "Failed to set intr mode, "
721 goto err_out_dev_close
;
724 err
= fnic_alloc_vnic_resources(fnic
);
726 shost_printk(KERN_ERR
, fnic
->lport
->host
,
727 "Failed to alloc vNIC resources, "
729 goto err_out_clear_intr
;
733 /* initialize all fnic locks */
734 spin_lock_init(&fnic
->fnic_lock
);
736 for (i
= 0; i
< FNIC_WQ_MAX
; i
++)
737 spin_lock_init(&fnic
->wq_lock
[i
]);
739 for (i
= 0; i
< FNIC_WQ_COPY_MAX
; i
++) {
740 spin_lock_init(&fnic
->wq_copy_lock
[i
]);
741 fnic
->wq_copy_desc_low
[i
] = DESC_CLEAN_LOW_WATERMARK
;
742 fnic
->fw_ack_recd
[i
] = 0;
743 fnic
->fw_ack_index
[i
] = -1;
746 for (i
= 0; i
< FNIC_IO_LOCKS
; i
++)
747 spin_lock_init(&fnic
->io_req_lock
[i
]);
749 fnic
->io_req_pool
= mempool_create_slab_pool(2, fnic_io_req_cache
);
750 if (!fnic
->io_req_pool
)
751 goto err_out_free_resources
;
753 pool
= mempool_create_slab_pool(2, fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
755 goto err_out_free_ioreq_pool
;
756 fnic
->io_sgl_pool
[FNIC_SGL_CACHE_DFLT
] = pool
;
758 pool
= mempool_create_slab_pool(2, fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
760 goto err_out_free_dflt_pool
;
761 fnic
->io_sgl_pool
[FNIC_SGL_CACHE_MAX
] = pool
;
763 /* setup vlan config, hw inserts vlan header */
764 fnic
->vlan_hw_insert
= 1;
767 /* Initialize the FIP fcoe_ctrl struct */
768 fnic
->ctlr
.send
= fnic_eth_send
;
769 fnic
->ctlr
.update_mac
= fnic_update_mac
;
770 fnic
->ctlr
.get_src_addr
= fnic_get_mac
;
771 if (fnic
->config
.flags
& VFCF_FIP_CAPABLE
) {
772 shost_printk(KERN_INFO
, fnic
->lport
->host
,
773 "firmware supports FIP\n");
774 /* enable directed and multicast */
775 vnic_dev_packet_filter(fnic
->vdev
, 1, 1, 0, 0, 0);
776 vnic_dev_add_addr(fnic
->vdev
, FIP_ALL_ENODE_MACS
);
777 vnic_dev_add_addr(fnic
->vdev
, fnic
->ctlr
.ctl_src_addr
);
778 fnic
->set_vlan
= fnic_set_vlan
;
779 fcoe_ctlr_init(&fnic
->ctlr
, FIP_MODE_AUTO
);
780 timer_setup(&fnic
->fip_timer
, fnic_fip_notify_timer
, 0);
781 spin_lock_init(&fnic
->vlans_lock
);
782 INIT_WORK(&fnic
->fip_frame_work
, fnic_handle_fip_frame
);
783 INIT_WORK(&fnic
->event_work
, fnic_handle_event
);
784 skb_queue_head_init(&fnic
->fip_frame_queue
);
785 INIT_LIST_HEAD(&fnic
->evlist
);
786 INIT_LIST_HEAD(&fnic
->vlans
);
788 shost_printk(KERN_INFO
, fnic
->lport
->host
,
789 "firmware uses non-FIP mode\n");
790 fcoe_ctlr_init(&fnic
->ctlr
, FIP_MODE_NON_FIP
);
791 fnic
->ctlr
.state
= FIP_ST_NON_FIP
;
793 fnic
->state
= FNIC_IN_FC_MODE
;
795 atomic_set(&fnic
->in_flight
, 0);
796 fnic
->state_flags
= FNIC_FLAGS_NONE
;
798 /* Enable hardware stripping of vlan header on ingress */
799 fnic_set_nic_config(fnic
, 0, 0, 0, 0, 0, 0, 1);
801 /* Setup notification buffer area */
802 err
= fnic_notify_set(fnic
);
804 shost_printk(KERN_ERR
, fnic
->lport
->host
,
805 "Failed to alloc notify buffer, aborting.\n");
806 goto err_out_free_max_pool
;
809 /* Setup notify timer when using MSI interrupts */
810 if (vnic_dev_get_intr_mode(fnic
->vdev
) == VNIC_DEV_INTR_MODE_MSI
)
811 timer_setup(&fnic
->notify_timer
, fnic_notify_timer
, 0);
813 /* allocate RQ buffers and post them to RQ*/
814 for (i
= 0; i
< fnic
->rq_count
; i
++) {
815 err
= vnic_rq_fill(&fnic
->rq
[i
], fnic_alloc_rq_frame
);
817 shost_printk(KERN_ERR
, fnic
->lport
->host
,
818 "fnic_alloc_rq_frame can't alloc "
820 goto err_out_free_rq_buf
;
825 * Initialization done with PCI system, hardware, firmware.
828 err
= scsi_add_host(lp
->host
, &pdev
->dev
);
830 shost_printk(KERN_ERR
, fnic
->lport
->host
,
831 "fnic: scsi_add_host failed...exiting\n");
832 goto err_out_free_rq_buf
;
835 /* Start local port initiatialization */
839 lp
->max_retry_count
= fnic
->config
.flogi_retries
;
840 lp
->max_rport_retry_count
= fnic
->config
.plogi_retries
;
841 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
842 FCP_SPPF_CONF_COMPL
);
843 if (fnic
->config
.flags
& VFCF_FCP_SEQ_LVL_ERR
)
844 lp
->service_params
|= FCP_SPPF_RETRY
;
846 lp
->boot_time
= jiffies
;
847 lp
->e_d_tov
= fnic
->config
.ed_tov
;
848 lp
->r_a_tov
= fnic
->config
.ra_tov
;
849 lp
->link_supported_speeds
= FC_PORTSPEED_10GBIT
;
850 fc_set_wwnn(lp
, fnic
->config
.node_wwn
);
851 fc_set_wwpn(lp
, fnic
->config
.port_wwn
);
853 fcoe_libfc_config(lp
, &fnic
->ctlr
, &fnic_transport_template
, 0);
855 if (!fc_exch_mgr_alloc(lp
, FC_CLASS_3
, FCPIO_HOST_EXCH_RANGE_START
,
856 FCPIO_HOST_EXCH_RANGE_END
, NULL
)) {
858 goto err_out_remove_scsi_host
;
861 fc_lport_init_stats(lp
);
862 fnic
->stats_reset_time
= jiffies
;
866 if (fc_set_mfs(lp
, fnic
->config
.maxdatafieldsize
+
867 sizeof(struct fc_frame_header
))) {
869 goto err_out_free_exch_mgr
;
871 fc_host_maxframe_size(lp
->host
) = lp
->mfs
;
872 fc_host_dev_loss_tmo(lp
->host
) = fnic
->config
.port_down_timeout
/ 1000;
874 sprintf(fc_host_symbolic_name(lp
->host
),
875 DRV_NAME
" v" DRV_VERSION
" over %s", fnic
->name
);
877 spin_lock_irqsave(&fnic_list_lock
, flags
);
878 list_add_tail(&fnic
->list
, &fnic_list
);
879 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
881 INIT_WORK(&fnic
->link_work
, fnic_handle_link
);
882 INIT_WORK(&fnic
->frame_work
, fnic_handle_frame
);
883 skb_queue_head_init(&fnic
->frame_queue
);
884 skb_queue_head_init(&fnic
->tx_queue
);
886 /* Enable all queues */
887 for (i
= 0; i
< fnic
->raw_wq_count
; i
++)
888 vnic_wq_enable(&fnic
->wq
[i
]);
889 for (i
= 0; i
< fnic
->rq_count
; i
++)
890 vnic_rq_enable(&fnic
->rq
[i
]);
891 for (i
= 0; i
< fnic
->wq_copy_count
; i
++)
892 vnic_wq_copy_enable(&fnic
->wq_copy
[i
]);
896 vnic_dev_enable(fnic
->vdev
);
898 err
= fnic_request_intr(fnic
);
900 shost_printk(KERN_ERR
, fnic
->lport
->host
,
901 "Unable to request irq.\n");
902 goto err_out_free_exch_mgr
;
905 for (i
= 0; i
< fnic
->intr_count
; i
++)
906 vnic_intr_unmask(&fnic
->intr
[i
]);
908 fnic_notify_timer_start(fnic
);
912 err_out_free_exch_mgr
:
913 fc_exch_mgr_free(lp
);
914 err_out_remove_scsi_host
:
915 fc_remove_host(lp
->host
);
916 scsi_remove_host(lp
->host
);
918 for (i
= 0; i
< fnic
->rq_count
; i
++)
919 vnic_rq_clean(&fnic
->rq
[i
], fnic_free_rq_buf
);
920 vnic_dev_notify_unset(fnic
->vdev
);
921 err_out_free_max_pool
:
922 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_MAX
]);
923 err_out_free_dflt_pool
:
924 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_DFLT
]);
925 err_out_free_ioreq_pool
:
926 mempool_destroy(fnic
->io_req_pool
);
927 err_out_free_resources
:
928 fnic_free_vnic_resources(fnic
);
930 fnic_clear_intr_mode(fnic
);
932 vnic_dev_close(fnic
->vdev
);
933 err_out_vnic_unregister
:
934 vnic_dev_unregister(fnic
->vdev
);
937 err_out_release_regions
:
938 pci_release_regions(pdev
);
939 err_out_disable_device
:
940 pci_disable_device(pdev
);
942 fnic_stats_debugfs_remove(fnic
);
943 scsi_host_put(lp
->host
);
948 static void fnic_remove(struct pci_dev
*pdev
)
950 struct fnic
*fnic
= pci_get_drvdata(pdev
);
951 struct fc_lport
*lp
= fnic
->lport
;
955 * Mark state so that the workqueue thread stops forwarding
956 * received frames and link events to the local port. ISR and
957 * other threads that can queue work items will also stop
958 * creating work items on the fnic workqueue
960 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
961 fnic
->stop_rx_link_events
= 1;
962 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
964 if (vnic_dev_get_intr_mode(fnic
->vdev
) == VNIC_DEV_INTR_MODE_MSI
)
965 del_timer_sync(&fnic
->notify_timer
);
968 * Flush the fnic event queue. After this call, there should
969 * be no event queued for this fnic device in the workqueue
971 flush_workqueue(fnic_event_queue
);
972 skb_queue_purge(&fnic
->frame_queue
);
973 skb_queue_purge(&fnic
->tx_queue
);
975 if (fnic
->config
.flags
& VFCF_FIP_CAPABLE
) {
976 del_timer_sync(&fnic
->fip_timer
);
977 skb_queue_purge(&fnic
->fip_frame_queue
);
978 fnic_fcoe_reset_vlans(fnic
);
979 fnic_fcoe_evlist_free(fnic
);
983 * Log off the fabric. This stops all remote ports, dns port,
984 * logs off the fabric. This flushes all rport, disc, lport work
987 fc_fabric_logoff(fnic
->lport
);
989 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
991 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
993 fcoe_ctlr_destroy(&fnic
->ctlr
);
994 fc_lport_destroy(lp
);
995 fnic_stats_debugfs_remove(fnic
);
998 * This stops the fnic device, masks all interrupts. Completed
999 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
1004 BUG_ON(!skb_queue_empty(&fnic
->frame_queue
));
1005 BUG_ON(!skb_queue_empty(&fnic
->tx_queue
));
1007 spin_lock_irqsave(&fnic_list_lock
, flags
);
1008 list_del(&fnic
->list
);
1009 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
1011 fc_remove_host(fnic
->lport
->host
);
1012 scsi_remove_host(fnic
->lport
->host
);
1013 fc_exch_mgr_free(fnic
->lport
);
1014 vnic_dev_notify_unset(fnic
->vdev
);
1015 fnic_free_intr(fnic
);
1016 fnic_free_vnic_resources(fnic
);
1017 fnic_clear_intr_mode(fnic
);
1018 vnic_dev_close(fnic
->vdev
);
1019 vnic_dev_unregister(fnic
->vdev
);
1021 pci_release_regions(pdev
);
1022 pci_disable_device(pdev
);
1023 scsi_host_put(lp
->host
);
1026 static struct pci_driver fnic_driver
= {
1028 .id_table
= fnic_id_table
,
1029 .probe
= fnic_probe
,
1030 .remove
= fnic_remove
,
1033 static int __init
fnic_init_module(void)
1038 printk(KERN_INFO PFX
"%s, ver %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
1040 /* Create debugfs entries for fnic */
1041 err
= fnic_debugfs_init();
1043 printk(KERN_ERR PFX
"Failed to create fnic directory "
1044 "for tracing and stats logging\n");
1045 fnic_debugfs_terminate();
1048 /* Allocate memory for trace buffer */
1049 err
= fnic_trace_buf_init();
1052 "Trace buffer initialization Failed. "
1053 "Fnic Tracing utility is disabled\n");
1057 /* Allocate memory for fc trace buffer */
1058 err
= fnic_fc_trace_init();
1060 printk(KERN_ERR PFX
"FC trace buffer initialization Failed "
1061 "FC frame tracing utility is disabled\n");
1062 fnic_fc_trace_free();
1065 /* Create a cache for allocation of default size sgls */
1066 len
= sizeof(struct fnic_dflt_sgl_list
);
1067 fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
] = kmem_cache_create
1068 ("fnic_sgl_dflt", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
1071 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]) {
1072 printk(KERN_ERR PFX
"failed to create fnic dflt sgl slab\n");
1074 goto err_create_fnic_sgl_slab_dflt
;
1077 /* Create a cache for allocation of max size sgls*/
1078 len
= sizeof(struct fnic_sgl_list
);
1079 fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
] = kmem_cache_create
1080 ("fnic_sgl_max", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
1083 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]) {
1084 printk(KERN_ERR PFX
"failed to create fnic max sgl slab\n");
1086 goto err_create_fnic_sgl_slab_max
;
1089 /* Create a cache of io_req structs for use via mempool */
1090 fnic_io_req_cache
= kmem_cache_create("fnic_io_req",
1091 sizeof(struct fnic_io_req
),
1092 0, SLAB_HWCACHE_ALIGN
, NULL
);
1093 if (!fnic_io_req_cache
) {
1094 printk(KERN_ERR PFX
"failed to create fnic io_req slab\n");
1096 goto err_create_fnic_ioreq_slab
;
1099 fnic_event_queue
= create_singlethread_workqueue("fnic_event_wq");
1100 if (!fnic_event_queue
) {
1101 printk(KERN_ERR PFX
"fnic work queue create failed\n");
1103 goto err_create_fnic_workq
;
1106 spin_lock_init(&fnic_list_lock
);
1107 INIT_LIST_HEAD(&fnic_list
);
1109 fnic_fip_queue
= create_singlethread_workqueue("fnic_fip_q");
1110 if (!fnic_fip_queue
) {
1111 printk(KERN_ERR PFX
"fnic FIP work queue create failed\n");
1113 goto err_create_fip_workq
;
1116 fnic_fc_transport
= fc_attach_transport(&fnic_fc_functions
);
1117 if (!fnic_fc_transport
) {
1118 printk(KERN_ERR PFX
"fc_attach_transport error\n");
1120 goto err_fc_transport
;
1123 /* register the driver with PCI system */
1124 err
= pci_register_driver(&fnic_driver
);
1126 printk(KERN_ERR PFX
"pci register error\n");
1127 goto err_pci_register
;
1132 fc_release_transport(fnic_fc_transport
);
1134 destroy_workqueue(fnic_fip_queue
);
1135 err_create_fip_workq
:
1136 destroy_workqueue(fnic_event_queue
);
1137 err_create_fnic_workq
:
1138 kmem_cache_destroy(fnic_io_req_cache
);
1139 err_create_fnic_ioreq_slab
:
1140 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
1141 err_create_fnic_sgl_slab_max
:
1142 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
1143 err_create_fnic_sgl_slab_dflt
:
1145 fnic_fc_trace_free();
1146 fnic_debugfs_terminate();
1150 static void __exit
fnic_cleanup_module(void)
1152 pci_unregister_driver(&fnic_driver
);
1153 destroy_workqueue(fnic_event_queue
);
1154 if (fnic_fip_queue
) {
1155 flush_workqueue(fnic_fip_queue
);
1156 destroy_workqueue(fnic_fip_queue
);
1158 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
1159 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
1160 kmem_cache_destroy(fnic_io_req_cache
);
1161 fc_release_transport(fnic_fc_transport
);
1163 fnic_fc_trace_free();
1164 fnic_debugfs_terminate();
1167 module_init(fnic_init_module
);
1168 module_exit(fnic_cleanup_module
);