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 err
= fnic_stats_debugfs_init(fnic
);
561 shost_printk(KERN_ERR
, fnic
->lport
->host
,
562 "Failed to initialize debugfs for stats\n");
563 fnic_stats_debugfs_remove(fnic
);
566 /* Setup PCI resources */
567 pci_set_drvdata(pdev
, fnic
);
571 err
= pci_enable_device(pdev
);
573 shost_printk(KERN_ERR
, fnic
->lport
->host
,
574 "Cannot enable PCI device, aborting.\n");
575 goto err_out_free_hba
;
578 err
= pci_request_regions(pdev
, DRV_NAME
);
580 shost_printk(KERN_ERR
, fnic
->lport
->host
,
581 "Cannot enable PCI resources, aborting\n");
582 goto err_out_disable_device
;
585 pci_set_master(pdev
);
587 /* Query PCI controller on system for DMA addressing
588 * limitation for the device. Try 64-bit first, and
591 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
593 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
595 shost_printk(KERN_ERR
, fnic
->lport
->host
,
596 "No usable DMA configuration "
598 goto err_out_release_regions
;
600 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
602 shost_printk(KERN_ERR
, fnic
->lport
->host
,
603 "Unable to obtain 32-bit DMA "
604 "for consistent allocations, aborting.\n");
605 goto err_out_release_regions
;
608 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
610 shost_printk(KERN_ERR
, fnic
->lport
->host
,
611 "Unable to obtain 64-bit DMA "
612 "for consistent allocations, aborting.\n");
613 goto err_out_release_regions
;
617 /* Map vNIC resources from BAR0 */
618 if (!(pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
)) {
619 shost_printk(KERN_ERR
, fnic
->lport
->host
,
620 "BAR0 not memory-map'able, aborting.\n");
622 goto err_out_release_regions
;
625 fnic
->bar0
.vaddr
= pci_iomap(pdev
, 0, 0);
626 fnic
->bar0
.bus_addr
= pci_resource_start(pdev
, 0);
627 fnic
->bar0
.len
= pci_resource_len(pdev
, 0);
629 if (!fnic
->bar0
.vaddr
) {
630 shost_printk(KERN_ERR
, fnic
->lport
->host
,
631 "Cannot memory-map BAR0 res hdr, "
634 goto err_out_release_regions
;
637 fnic
->vdev
= vnic_dev_register(NULL
, fnic
, pdev
, &fnic
->bar0
);
639 shost_printk(KERN_ERR
, fnic
->lport
->host
,
640 "vNIC registration failed, "
643 goto err_out_iounmap
;
646 err
= fnic_dev_wait(fnic
->vdev
, vnic_dev_open
,
647 vnic_dev_open_done
, 0);
649 shost_printk(KERN_ERR
, fnic
->lport
->host
,
650 "vNIC dev open failed, aborting.\n");
651 goto err_out_vnic_unregister
;
654 err
= vnic_dev_init(fnic
->vdev
, 0);
656 shost_printk(KERN_ERR
, fnic
->lport
->host
,
657 "vNIC dev init failed, aborting.\n");
658 goto err_out_dev_close
;
661 err
= vnic_dev_mac_addr(fnic
->vdev
, fnic
->ctlr
.ctl_src_addr
);
663 shost_printk(KERN_ERR
, fnic
->lport
->host
,
664 "vNIC get MAC addr failed \n");
665 goto err_out_dev_close
;
667 /* set data_src for point-to-point mode and to keep it non-zero */
668 memcpy(fnic
->data_src_addr
, fnic
->ctlr
.ctl_src_addr
, ETH_ALEN
);
670 /* Get vNIC configuration */
671 err
= fnic_get_vnic_config(fnic
);
673 shost_printk(KERN_ERR
, fnic
->lport
->host
,
674 "Get vNIC configuration failed, "
676 goto err_out_dev_close
;
679 /* Configure Maximum Outstanding IO reqs*/
680 if (fnic
->config
.io_throttle_count
!= FNIC_UCSM_DFLT_THROTTLE_CNT_BLD
) {
681 host
->can_queue
= min_t(u32
, FNIC_MAX_IO_REQ
,
682 max_t(u32
, FNIC_MIN_IO_REQ
,
683 fnic
->config
.io_throttle_count
));
685 fnic
->fnic_max_tag_id
= host
->can_queue
;
687 err
= scsi_init_shared_tag_map(host
, fnic
->fnic_max_tag_id
);
689 shost_printk(KERN_ERR
, fnic
->lport
->host
,
690 "Unable to alloc shared tag map\n");
691 goto err_out_dev_close
;
694 host
->max_lun
= fnic
->config
.luns_per_tgt
;
695 host
->max_id
= FNIC_MAX_FCP_TARGET
;
696 host
->max_cmd_len
= FCOE_MAX_CMD_LEN
;
698 fnic_get_res_counts(fnic
);
700 err
= fnic_set_intr_mode(fnic
);
702 shost_printk(KERN_ERR
, fnic
->lport
->host
,
703 "Failed to set intr mode, "
705 goto err_out_dev_close
;
708 err
= fnic_alloc_vnic_resources(fnic
);
710 shost_printk(KERN_ERR
, fnic
->lport
->host
,
711 "Failed to alloc vNIC resources, "
713 goto err_out_clear_intr
;
717 /* initialize all fnic locks */
718 spin_lock_init(&fnic
->fnic_lock
);
720 for (i
= 0; i
< FNIC_WQ_MAX
; i
++)
721 spin_lock_init(&fnic
->wq_lock
[i
]);
723 for (i
= 0; i
< FNIC_WQ_COPY_MAX
; i
++) {
724 spin_lock_init(&fnic
->wq_copy_lock
[i
]);
725 fnic
->wq_copy_desc_low
[i
] = DESC_CLEAN_LOW_WATERMARK
;
726 fnic
->fw_ack_recd
[i
] = 0;
727 fnic
->fw_ack_index
[i
] = -1;
730 for (i
= 0; i
< FNIC_IO_LOCKS
; i
++)
731 spin_lock_init(&fnic
->io_req_lock
[i
]);
733 fnic
->io_req_pool
= mempool_create_slab_pool(2, fnic_io_req_cache
);
734 if (!fnic
->io_req_pool
)
735 goto err_out_free_resources
;
737 pool
= mempool_create_slab_pool(2, fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
739 goto err_out_free_ioreq_pool
;
740 fnic
->io_sgl_pool
[FNIC_SGL_CACHE_DFLT
] = pool
;
742 pool
= mempool_create_slab_pool(2, fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
744 goto err_out_free_dflt_pool
;
745 fnic
->io_sgl_pool
[FNIC_SGL_CACHE_MAX
] = pool
;
747 /* setup vlan config, hw inserts vlan header */
748 fnic
->vlan_hw_insert
= 1;
751 /* Initialize the FIP fcoe_ctrl struct */
752 fnic
->ctlr
.send
= fnic_eth_send
;
753 fnic
->ctlr
.update_mac
= fnic_update_mac
;
754 fnic
->ctlr
.get_src_addr
= fnic_get_mac
;
755 if (fnic
->config
.flags
& VFCF_FIP_CAPABLE
) {
756 shost_printk(KERN_INFO
, fnic
->lport
->host
,
757 "firmware supports FIP\n");
758 /* enable directed and multicast */
759 vnic_dev_packet_filter(fnic
->vdev
, 1, 1, 0, 0, 0);
760 vnic_dev_add_addr(fnic
->vdev
, FIP_ALL_ENODE_MACS
);
761 vnic_dev_add_addr(fnic
->vdev
, fnic
->ctlr
.ctl_src_addr
);
762 fnic
->set_vlan
= fnic_set_vlan
;
763 fcoe_ctlr_init(&fnic
->ctlr
, FIP_MODE_AUTO
);
764 setup_timer(&fnic
->fip_timer
, fnic_fip_notify_timer
,
765 (unsigned long)fnic
);
766 spin_lock_init(&fnic
->vlans_lock
);
767 INIT_WORK(&fnic
->fip_frame_work
, fnic_handle_fip_frame
);
768 INIT_WORK(&fnic
->event_work
, fnic_handle_event
);
769 skb_queue_head_init(&fnic
->fip_frame_queue
);
770 INIT_LIST_HEAD(&fnic
->evlist
);
771 INIT_LIST_HEAD(&fnic
->vlans
);
773 shost_printk(KERN_INFO
, fnic
->lport
->host
,
774 "firmware uses non-FIP mode\n");
775 fcoe_ctlr_init(&fnic
->ctlr
, FIP_MODE_NON_FIP
);
777 fnic
->state
= FNIC_IN_FC_MODE
;
779 atomic_set(&fnic
->in_flight
, 0);
780 fnic
->state_flags
= FNIC_FLAGS_NONE
;
782 /* Enable hardware stripping of vlan header on ingress */
783 fnic_set_nic_config(fnic
, 0, 0, 0, 0, 0, 0, 1);
785 /* Setup notification buffer area */
786 err
= fnic_notify_set(fnic
);
788 shost_printk(KERN_ERR
, fnic
->lport
->host
,
789 "Failed to alloc notify buffer, aborting.\n");
790 goto err_out_free_max_pool
;
793 /* Setup notify timer when using MSI interrupts */
794 if (vnic_dev_get_intr_mode(fnic
->vdev
) == VNIC_DEV_INTR_MODE_MSI
)
795 setup_timer(&fnic
->notify_timer
,
796 fnic_notify_timer
, (unsigned long)fnic
);
798 /* allocate RQ buffers and post them to RQ*/
799 for (i
= 0; i
< fnic
->rq_count
; i
++) {
800 err
= vnic_rq_fill(&fnic
->rq
[i
], fnic_alloc_rq_frame
);
802 shost_printk(KERN_ERR
, fnic
->lport
->host
,
803 "fnic_alloc_rq_frame can't alloc "
805 goto err_out_free_rq_buf
;
810 * Initialization done with PCI system, hardware, firmware.
813 err
= scsi_add_host(lp
->host
, &pdev
->dev
);
815 shost_printk(KERN_ERR
, fnic
->lport
->host
,
816 "fnic: scsi_add_host failed...exiting\n");
817 goto err_out_free_rq_buf
;
820 /* Start local port initiatialization */
824 lp
->max_retry_count
= fnic
->config
.flogi_retries
;
825 lp
->max_rport_retry_count
= fnic
->config
.plogi_retries
;
826 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
827 FCP_SPPF_CONF_COMPL
);
828 if (fnic
->config
.flags
& VFCF_FCP_SEQ_LVL_ERR
)
829 lp
->service_params
|= FCP_SPPF_RETRY
;
831 lp
->boot_time
= jiffies
;
832 lp
->e_d_tov
= fnic
->config
.ed_tov
;
833 lp
->r_a_tov
= fnic
->config
.ra_tov
;
834 lp
->link_supported_speeds
= FC_PORTSPEED_10GBIT
;
835 fc_set_wwnn(lp
, fnic
->config
.node_wwn
);
836 fc_set_wwpn(lp
, fnic
->config
.port_wwn
);
838 fcoe_libfc_config(lp
, &fnic
->ctlr
, &fnic_transport_template
, 0);
840 if (!fc_exch_mgr_alloc(lp
, FC_CLASS_3
, FCPIO_HOST_EXCH_RANGE_START
,
841 FCPIO_HOST_EXCH_RANGE_END
, NULL
)) {
843 goto err_out_remove_scsi_host
;
846 fc_lport_init_stats(lp
);
847 fnic
->stats_reset_time
= jiffies
;
851 if (fc_set_mfs(lp
, fnic
->config
.maxdatafieldsize
+
852 sizeof(struct fc_frame_header
))) {
854 goto err_out_free_exch_mgr
;
856 fc_host_maxframe_size(lp
->host
) = lp
->mfs
;
857 fc_host_dev_loss_tmo(lp
->host
) = fnic
->config
.port_down_timeout
/ 1000;
859 sprintf(fc_host_symbolic_name(lp
->host
),
860 DRV_NAME
" v" DRV_VERSION
" over %s", fnic
->name
);
862 spin_lock_irqsave(&fnic_list_lock
, flags
);
863 list_add_tail(&fnic
->list
, &fnic_list
);
864 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
866 INIT_WORK(&fnic
->link_work
, fnic_handle_link
);
867 INIT_WORK(&fnic
->frame_work
, fnic_handle_frame
);
868 skb_queue_head_init(&fnic
->frame_queue
);
869 skb_queue_head_init(&fnic
->tx_queue
);
871 /* Enable all queues */
872 for (i
= 0; i
< fnic
->raw_wq_count
; i
++)
873 vnic_wq_enable(&fnic
->wq
[i
]);
874 for (i
= 0; i
< fnic
->rq_count
; i
++)
875 vnic_rq_enable(&fnic
->rq
[i
]);
876 for (i
= 0; i
< fnic
->wq_copy_count
; i
++)
877 vnic_wq_copy_enable(&fnic
->wq_copy
[i
]);
881 vnic_dev_enable(fnic
->vdev
);
883 err
= fnic_request_intr(fnic
);
885 shost_printk(KERN_ERR
, fnic
->lport
->host
,
886 "Unable to request irq.\n");
887 goto err_out_free_exch_mgr
;
890 for (i
= 0; i
< fnic
->intr_count
; i
++)
891 vnic_intr_unmask(&fnic
->intr
[i
]);
893 fnic_notify_timer_start(fnic
);
897 err_out_free_exch_mgr
:
898 fc_exch_mgr_free(lp
);
899 err_out_remove_scsi_host
:
900 fc_remove_host(lp
->host
);
901 scsi_remove_host(lp
->host
);
903 for (i
= 0; i
< fnic
->rq_count
; i
++)
904 vnic_rq_clean(&fnic
->rq
[i
], fnic_free_rq_buf
);
905 vnic_dev_notify_unset(fnic
->vdev
);
906 err_out_free_max_pool
:
907 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_MAX
]);
908 err_out_free_dflt_pool
:
909 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_DFLT
]);
910 err_out_free_ioreq_pool
:
911 mempool_destroy(fnic
->io_req_pool
);
912 err_out_free_resources
:
913 fnic_free_vnic_resources(fnic
);
915 fnic_clear_intr_mode(fnic
);
917 vnic_dev_close(fnic
->vdev
);
918 err_out_vnic_unregister
:
919 vnic_dev_unregister(fnic
->vdev
);
922 err_out_release_regions
:
923 pci_release_regions(pdev
);
924 err_out_disable_device
:
925 pci_disable_device(pdev
);
927 fnic_stats_debugfs_remove(fnic
);
928 scsi_host_put(lp
->host
);
933 static void fnic_remove(struct pci_dev
*pdev
)
935 struct fnic
*fnic
= pci_get_drvdata(pdev
);
936 struct fc_lport
*lp
= fnic
->lport
;
940 * Mark state so that the workqueue thread stops forwarding
941 * received frames and link events to the local port. ISR and
942 * other threads that can queue work items will also stop
943 * creating work items on the fnic workqueue
945 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
946 fnic
->stop_rx_link_events
= 1;
947 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
949 if (vnic_dev_get_intr_mode(fnic
->vdev
) == VNIC_DEV_INTR_MODE_MSI
)
950 del_timer_sync(&fnic
->notify_timer
);
953 * Flush the fnic event queue. After this call, there should
954 * be no event queued for this fnic device in the workqueue
956 flush_workqueue(fnic_event_queue
);
957 skb_queue_purge(&fnic
->frame_queue
);
958 skb_queue_purge(&fnic
->tx_queue
);
960 if (fnic
->config
.flags
& VFCF_FIP_CAPABLE
) {
961 del_timer_sync(&fnic
->fip_timer
);
962 skb_queue_purge(&fnic
->fip_frame_queue
);
963 fnic_fcoe_reset_vlans(fnic
);
964 fnic_fcoe_evlist_free(fnic
);
968 * Log off the fabric. This stops all remote ports, dns port,
969 * logs off the fabric. This flushes all rport, disc, lport work
972 fc_fabric_logoff(fnic
->lport
);
974 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
976 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
978 fcoe_ctlr_destroy(&fnic
->ctlr
);
979 fc_lport_destroy(lp
);
980 fnic_stats_debugfs_remove(fnic
);
983 * This stops the fnic device, masks all interrupts. Completed
984 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
989 BUG_ON(!skb_queue_empty(&fnic
->frame_queue
));
990 BUG_ON(!skb_queue_empty(&fnic
->tx_queue
));
992 spin_lock_irqsave(&fnic_list_lock
, flags
);
993 list_del(&fnic
->list
);
994 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
996 fc_remove_host(fnic
->lport
->host
);
997 scsi_remove_host(fnic
->lport
->host
);
998 fc_exch_mgr_free(fnic
->lport
);
999 vnic_dev_notify_unset(fnic
->vdev
);
1000 fnic_free_intr(fnic
);
1001 fnic_free_vnic_resources(fnic
);
1002 fnic_clear_intr_mode(fnic
);
1003 vnic_dev_close(fnic
->vdev
);
1004 vnic_dev_unregister(fnic
->vdev
);
1006 pci_release_regions(pdev
);
1007 pci_disable_device(pdev
);
1008 scsi_host_put(lp
->host
);
1011 static struct pci_driver fnic_driver
= {
1013 .id_table
= fnic_id_table
,
1014 .probe
= fnic_probe
,
1015 .remove
= fnic_remove
,
1018 static int __init
fnic_init_module(void)
1023 printk(KERN_INFO PFX
"%s, ver %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
1025 /* Create debugfs entries for fnic */
1026 err
= fnic_debugfs_init();
1028 printk(KERN_ERR PFX
"Failed to create fnic directory "
1029 "for tracing and stats logging\n");
1030 fnic_debugfs_terminate();
1033 /* Allocate memory for trace buffer */
1034 err
= fnic_trace_buf_init();
1036 printk(KERN_ERR PFX
"Trace buffer initialization Failed "
1037 "Fnic Tracing utility is disabled\n");
1041 /* Create a cache for allocation of default size sgls */
1042 len
= sizeof(struct fnic_dflt_sgl_list
);
1043 fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
] = kmem_cache_create
1044 ("fnic_sgl_dflt", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
1047 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]) {
1048 printk(KERN_ERR PFX
"failed to create fnic dflt sgl slab\n");
1050 goto err_create_fnic_sgl_slab_dflt
;
1053 /* Create a cache for allocation of max size sgls*/
1054 len
= sizeof(struct fnic_sgl_list
);
1055 fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
] = kmem_cache_create
1056 ("fnic_sgl_max", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
1059 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]) {
1060 printk(KERN_ERR PFX
"failed to create fnic max sgl slab\n");
1062 goto err_create_fnic_sgl_slab_max
;
1065 /* Create a cache of io_req structs for use via mempool */
1066 fnic_io_req_cache
= kmem_cache_create("fnic_io_req",
1067 sizeof(struct fnic_io_req
),
1068 0, SLAB_HWCACHE_ALIGN
, NULL
);
1069 if (!fnic_io_req_cache
) {
1070 printk(KERN_ERR PFX
"failed to create fnic io_req slab\n");
1072 goto err_create_fnic_ioreq_slab
;
1075 fnic_event_queue
= create_singlethread_workqueue("fnic_event_wq");
1076 if (!fnic_event_queue
) {
1077 printk(KERN_ERR PFX
"fnic work queue create failed\n");
1079 goto err_create_fnic_workq
;
1082 spin_lock_init(&fnic_list_lock
);
1083 INIT_LIST_HEAD(&fnic_list
);
1085 fnic_fip_queue
= create_singlethread_workqueue("fnic_fip_q");
1086 if (!fnic_fip_queue
) {
1087 printk(KERN_ERR PFX
"fnic FIP work queue create failed\n");
1089 goto err_create_fip_workq
;
1092 fnic_fc_transport
= fc_attach_transport(&fnic_fc_functions
);
1093 if (!fnic_fc_transport
) {
1094 printk(KERN_ERR PFX
"fc_attach_transport error\n");
1096 goto err_fc_transport
;
1099 /* register the driver with PCI system */
1100 err
= pci_register_driver(&fnic_driver
);
1102 printk(KERN_ERR PFX
"pci register error\n");
1103 goto err_pci_register
;
1108 fc_release_transport(fnic_fc_transport
);
1110 destroy_workqueue(fnic_fip_queue
);
1111 err_create_fip_workq
:
1112 destroy_workqueue(fnic_event_queue
);
1113 err_create_fnic_workq
:
1114 kmem_cache_destroy(fnic_io_req_cache
);
1115 err_create_fnic_ioreq_slab
:
1116 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
1117 err_create_fnic_sgl_slab_max
:
1118 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
1119 err_create_fnic_sgl_slab_dflt
:
1121 fnic_debugfs_terminate();
1125 static void __exit
fnic_cleanup_module(void)
1127 pci_unregister_driver(&fnic_driver
);
1128 destroy_workqueue(fnic_event_queue
);
1129 if (fnic_fip_queue
) {
1130 flush_workqueue(fnic_fip_queue
);
1131 destroy_workqueue(fnic_fip_queue
);
1133 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
1134 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
1135 kmem_cache_destroy(fnic_io_req_cache
);
1136 fc_release_transport(fnic_fc_transport
);
1138 fnic_debugfs_terminate();
1141 module_init(fnic_init_module
);
1142 module_exit(fnic_cleanup_module
);