1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
4 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
6 #include <linux/module.h>
7 #include <linux/mempool.h>
8 #include <linux/string.h>
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/pci.h>
13 #include <linux/skbuff.h>
14 #include <linux/interrupt.h>
15 #include <linux/spinlock.h>
16 #include <linux/workqueue.h>
17 #include <linux/if_ether.h>
18 #include <scsi/fc/fc_fip.h>
19 #include <scsi/scsi_host.h>
20 #include <scsi/scsi_transport.h>
21 #include <scsi/scsi_transport_fc.h>
22 #include <scsi/scsi_tcq.h>
23 #include <scsi/libfc.h>
24 #include <scsi/fc_frame.h>
27 #include "vnic_intr.h"
28 #include "vnic_stats.h"
33 #define PCI_DEVICE_ID_CISCO_FNIC 0x0045
35 /* Timer to poll notification area for events. Used for MSI interrupts */
36 #define FNIC_NOTIFY_TIMER_PERIOD (2 * HZ)
38 static struct kmem_cache
*fnic_sgl_cache
[FNIC_SGL_NUM_CACHES
];
39 static struct kmem_cache
*fnic_io_req_cache
;
40 static LIST_HEAD(fnic_list
);
41 static DEFINE_SPINLOCK(fnic_list_lock
);
43 /* Supported devices by fnic module */
44 static struct pci_device_id fnic_id_table
[] = {
45 { PCI_DEVICE(PCI_VENDOR_ID_CISCO
, PCI_DEVICE_ID_CISCO_FNIC
) },
49 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
50 MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, "
51 "Joseph R. Eykholt <jeykholt@cisco.com>");
52 MODULE_LICENSE("GPL v2");
53 MODULE_VERSION(DRV_VERSION
);
54 MODULE_DEVICE_TABLE(pci
, fnic_id_table
);
56 unsigned int fnic_log_level
;
57 module_param(fnic_log_level
, int, S_IRUGO
|S_IWUSR
);
58 MODULE_PARM_DESC(fnic_log_level
, "bit mask of fnic logging levels");
61 unsigned int io_completions
= FNIC_DFLT_IO_COMPLETIONS
;
62 module_param(io_completions
, int, S_IRUGO
|S_IWUSR
);
63 MODULE_PARM_DESC(io_completions
, "Max CQ entries to process at a time");
65 unsigned int fnic_trace_max_pages
= 16;
66 module_param(fnic_trace_max_pages
, uint
, S_IRUGO
|S_IWUSR
);
67 MODULE_PARM_DESC(fnic_trace_max_pages
, "Total allocated memory pages "
68 "for fnic trace buffer");
70 unsigned int fnic_fc_trace_max_pages
= 64;
71 module_param(fnic_fc_trace_max_pages
, uint
, S_IRUGO
|S_IWUSR
);
72 MODULE_PARM_DESC(fnic_fc_trace_max_pages
,
73 "Total allocated memory pages for fc trace buffer");
75 static unsigned int fnic_max_qdepth
= FNIC_DFLT_QUEUE_DEPTH
;
76 module_param(fnic_max_qdepth
, uint
, S_IRUGO
|S_IWUSR
);
77 MODULE_PARM_DESC(fnic_max_qdepth
, "Queue depth to report for each LUN");
79 static struct libfc_function_template fnic_transport_template
= {
80 .frame_send
= fnic_send
,
81 .lport_set_port_id
= fnic_set_port_id
,
82 .fcp_abort_io
= fnic_empty_scsi_cleanup
,
83 .fcp_cleanup
= fnic_empty_scsi_cleanup
,
84 .exch_mgr_reset
= fnic_exch_mgr_reset
87 static int fnic_slave_alloc(struct scsi_device
*sdev
)
89 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
91 if (!rport
|| fc_remote_port_chkready(rport
))
94 scsi_change_queue_depth(sdev
, fnic_max_qdepth
);
98 static const struct scsi_host_template fnic_host_template
= {
99 .module
= THIS_MODULE
,
101 .queuecommand
= fnic_queuecommand
,
102 .eh_timed_out
= fc_eh_timed_out
,
103 .eh_abort_handler
= fnic_abort_cmd
,
104 .eh_device_reset_handler
= fnic_device_reset
,
105 .eh_host_reset_handler
= fnic_host_reset
,
106 .slave_alloc
= fnic_slave_alloc
,
107 .change_queue_depth
= scsi_change_queue_depth
,
110 .can_queue
= FNIC_DFLT_IO_REQ
,
111 .sg_tablesize
= FNIC_MAX_SG_DESC_CNT
,
112 .max_sectors
= 0xffff,
113 .shost_groups
= fnic_host_groups
,
114 .track_queue_depth
= 1,
115 .cmd_size
= sizeof(struct fnic_cmd_priv
),
119 fnic_set_rport_dev_loss_tmo(struct fc_rport
*rport
, u32 timeout
)
122 rport
->dev_loss_tmo
= timeout
;
124 rport
->dev_loss_tmo
= 1;
127 static void fnic_get_host_speed(struct Scsi_Host
*shost
);
128 static struct scsi_transport_template
*fnic_fc_transport
;
129 static struct fc_host_statistics
*fnic_get_stats(struct Scsi_Host
*);
130 static void fnic_reset_host_stats(struct Scsi_Host
*);
132 static struct fc_function_template fnic_fc_functions
= {
134 .show_host_node_name
= 1,
135 .show_host_port_name
= 1,
136 .show_host_supported_classes
= 1,
137 .show_host_supported_fc4s
= 1,
138 .show_host_active_fc4s
= 1,
139 .show_host_maxframe_size
= 1,
140 .show_host_port_id
= 1,
141 .show_host_supported_speeds
= 1,
142 .get_host_speed
= fnic_get_host_speed
,
143 .show_host_speed
= 1,
144 .show_host_port_type
= 1,
145 .get_host_port_state
= fc_get_host_port_state
,
146 .show_host_port_state
= 1,
147 .show_host_symbolic_name
= 1,
148 .show_rport_maxframe_size
= 1,
149 .show_rport_supported_classes
= 1,
150 .show_host_fabric_name
= 1,
151 .show_starget_node_name
= 1,
152 .show_starget_port_name
= 1,
153 .show_starget_port_id
= 1,
154 .show_rport_dev_loss_tmo
= 1,
155 .set_rport_dev_loss_tmo
= fnic_set_rport_dev_loss_tmo
,
156 .issue_fc_host_lip
= fnic_reset
,
157 .get_fc_host_stats
= fnic_get_stats
,
158 .reset_fc_host_stats
= fnic_reset_host_stats
,
159 .dd_fcrport_size
= sizeof(struct fc_rport_libfc_priv
),
160 .terminate_rport_io
= fnic_terminate_rport_io
,
161 .bsg_request
= fc_lport_bsg_request
,
164 static void fnic_get_host_speed(struct Scsi_Host
*shost
)
166 struct fc_lport
*lp
= shost_priv(shost
);
167 struct fnic
*fnic
= lport_priv(lp
);
168 u32 port_speed
= vnic_dev_port_speed(fnic
->vdev
);
170 /* Add in other values as they get defined in fw */
171 switch (port_speed
) {
172 case DCEM_PORTSPEED_10G
:
173 fc_host_speed(shost
) = FC_PORTSPEED_10GBIT
;
175 case DCEM_PORTSPEED_20G
:
176 fc_host_speed(shost
) = FC_PORTSPEED_20GBIT
;
178 case DCEM_PORTSPEED_25G
:
179 fc_host_speed(shost
) = FC_PORTSPEED_25GBIT
;
181 case DCEM_PORTSPEED_40G
:
182 case DCEM_PORTSPEED_4x10G
:
183 fc_host_speed(shost
) = FC_PORTSPEED_40GBIT
;
185 case DCEM_PORTSPEED_100G
:
186 fc_host_speed(shost
) = FC_PORTSPEED_100GBIT
;
189 fc_host_speed(shost
) = FC_PORTSPEED_UNKNOWN
;
194 static struct fc_host_statistics
*fnic_get_stats(struct Scsi_Host
*host
)
197 struct fc_lport
*lp
= shost_priv(host
);
198 struct fnic
*fnic
= lport_priv(lp
);
199 struct fc_host_statistics
*stats
= &lp
->host_stats
;
200 struct vnic_stats
*vs
;
203 if (time_before(jiffies
, fnic
->stats_time
+ HZ
/ FNIC_STATS_RATE_LIMIT
))
205 fnic
->stats_time
= jiffies
;
207 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
208 ret
= vnic_dev_stats_dump(fnic
->vdev
, &fnic
->stats
);
209 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
212 FNIC_MAIN_DBG(KERN_DEBUG
, fnic
->lport
->host
,
213 "fnic: Get vnic stats failed"
218 stats
->tx_frames
= vs
->tx
.tx_unicast_frames_ok
;
219 stats
->tx_words
= vs
->tx
.tx_unicast_bytes_ok
/ 4;
220 stats
->rx_frames
= vs
->rx
.rx_unicast_frames_ok
;
221 stats
->rx_words
= vs
->rx
.rx_unicast_bytes_ok
/ 4;
222 stats
->error_frames
= vs
->tx
.tx_errors
+ vs
->rx
.rx_errors
;
223 stats
->dumped_frames
= vs
->tx
.tx_drops
+ vs
->rx
.rx_drop
;
224 stats
->invalid_crc_count
= vs
->rx
.rx_crc_errors
;
225 stats
->seconds_since_last_reset
=
226 (jiffies
- fnic
->stats_reset_time
) / HZ
;
227 stats
->fcp_input_megabytes
= div_u64(fnic
->fcp_input_bytes
, 1000000);
228 stats
->fcp_output_megabytes
= div_u64(fnic
->fcp_output_bytes
, 1000000);
234 * fnic_dump_fchost_stats
235 * note : dumps fc_statistics into system logs
237 void fnic_dump_fchost_stats(struct Scsi_Host
*host
,
238 struct fc_host_statistics
*stats
)
240 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
241 "fnic: seconds since last reset = %llu\n",
242 stats
->seconds_since_last_reset
);
243 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
244 "fnic: tx frames = %llu\n",
246 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
247 "fnic: tx words = %llu\n",
249 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
250 "fnic: rx frames = %llu\n",
252 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
253 "fnic: rx words = %llu\n",
255 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
256 "fnic: lip count = %llu\n",
258 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
259 "fnic: nos count = %llu\n",
261 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
262 "fnic: error frames = %llu\n",
263 stats
->error_frames
);
264 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
265 "fnic: dumped frames = %llu\n",
266 stats
->dumped_frames
);
267 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
268 "fnic: link failure count = %llu\n",
269 stats
->link_failure_count
);
270 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
271 "fnic: loss of sync count = %llu\n",
272 stats
->loss_of_sync_count
);
273 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
274 "fnic: loss of signal count = %llu\n",
275 stats
->loss_of_signal_count
);
276 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
277 "fnic: prim seq protocol err count = %llu\n",
278 stats
->prim_seq_protocol_err_count
);
279 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
280 "fnic: invalid tx word count= %llu\n",
281 stats
->invalid_tx_word_count
);
282 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
283 "fnic: invalid crc count = %llu\n",
284 stats
->invalid_crc_count
);
285 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
286 "fnic: fcp input requests = %llu\n",
287 stats
->fcp_input_requests
);
288 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
289 "fnic: fcp output requests = %llu\n",
290 stats
->fcp_output_requests
);
291 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
292 "fnic: fcp control requests = %llu\n",
293 stats
->fcp_control_requests
);
294 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
295 "fnic: fcp input megabytes = %llu\n",
296 stats
->fcp_input_megabytes
);
297 FNIC_MAIN_NOTE(KERN_NOTICE
, host
,
298 "fnic: fcp output megabytes = %llu\n",
299 stats
->fcp_output_megabytes
);
304 * fnic_reset_host_stats : clears host stats
305 * note : called when reset_statistics set under sysfs dir
307 static void fnic_reset_host_stats(struct Scsi_Host
*host
)
310 struct fc_lport
*lp
= shost_priv(host
);
311 struct fnic
*fnic
= lport_priv(lp
);
312 struct fc_host_statistics
*stats
;
315 /* dump current stats, before clearing them */
316 stats
= fnic_get_stats(host
);
317 fnic_dump_fchost_stats(host
, stats
);
319 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
320 ret
= vnic_dev_stats_clear(fnic
->vdev
);
321 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
324 FNIC_MAIN_DBG(KERN_DEBUG
, fnic
->lport
->host
,
325 "fnic: Reset vnic stats failed"
329 fnic
->stats_reset_time
= jiffies
;
330 memset(stats
, 0, sizeof(*stats
));
335 void fnic_log_q_error(struct fnic
*fnic
)
340 for (i
= 0; i
< fnic
->raw_wq_count
; i
++) {
341 error_status
= ioread32(&fnic
->wq
[i
].ctrl
->error_status
);
343 shost_printk(KERN_ERR
, fnic
->lport
->host
,
344 "WQ[%d] error_status"
345 " %d\n", i
, error_status
);
348 for (i
= 0; i
< fnic
->rq_count
; i
++) {
349 error_status
= ioread32(&fnic
->rq
[i
].ctrl
->error_status
);
351 shost_printk(KERN_ERR
, fnic
->lport
->host
,
352 "RQ[%d] error_status"
353 " %d\n", i
, error_status
);
356 for (i
= 0; i
< fnic
->wq_copy_count
; i
++) {
357 error_status
= ioread32(&fnic
->wq_copy
[i
].ctrl
->error_status
);
359 shost_printk(KERN_ERR
, fnic
->lport
->host
,
360 "CWQ[%d] error_status"
361 " %d\n", i
, error_status
);
365 void fnic_handle_link_event(struct fnic
*fnic
)
369 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
370 if (fnic
->stop_rx_link_events
) {
371 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
374 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
376 queue_work(fnic_event_queue
, &fnic
->link_work
);
380 static int fnic_notify_set(struct fnic
*fnic
)
384 switch (vnic_dev_get_intr_mode(fnic
->vdev
)) {
385 case VNIC_DEV_INTR_MODE_INTX
:
386 err
= vnic_dev_notify_set(fnic
->vdev
, FNIC_INTX_NOTIFY
);
388 case VNIC_DEV_INTR_MODE_MSI
:
389 err
= vnic_dev_notify_set(fnic
->vdev
, -1);
391 case VNIC_DEV_INTR_MODE_MSIX
:
392 err
= vnic_dev_notify_set(fnic
->vdev
, FNIC_MSIX_ERR_NOTIFY
);
395 shost_printk(KERN_ERR
, fnic
->lport
->host
,
396 "Interrupt mode should be set up"
397 " before devcmd notify set %d\n",
398 vnic_dev_get_intr_mode(fnic
->vdev
));
406 static void fnic_notify_timer(struct timer_list
*t
)
408 struct fnic
*fnic
= from_timer(fnic
, t
, notify_timer
);
410 fnic_handle_link_event(fnic
);
411 mod_timer(&fnic
->notify_timer
,
412 round_jiffies(jiffies
+ FNIC_NOTIFY_TIMER_PERIOD
));
415 static void fnic_fip_notify_timer(struct timer_list
*t
)
417 struct fnic
*fnic
= from_timer(fnic
, t
, fip_timer
);
419 fnic_handle_fip_timer(fnic
);
422 static void fnic_notify_timer_start(struct fnic
*fnic
)
424 switch (vnic_dev_get_intr_mode(fnic
->vdev
)) {
425 case VNIC_DEV_INTR_MODE_MSI
:
427 * Schedule first timeout immediately. The driver is
428 * initiatialized and ready to look for link up notification
430 mod_timer(&fnic
->notify_timer
, jiffies
);
433 /* Using intr for notification for INTx/MSI-X */
438 static int fnic_dev_wait(struct vnic_dev
*vdev
,
439 int (*start
)(struct vnic_dev
*, int),
440 int (*finished
)(struct vnic_dev
*, int *),
450 err
= start(vdev
, arg
);
454 /* Wait for func to complete.
455 * Sometime schedule_timeout_uninterruptible take long time
456 * to wake up so we do not retry as we are only waiting for
457 * 2 seconds in while loop. By adding count, we make sure
458 * we try atleast three times before returning -ETIMEDOUT
460 time
= jiffies
+ (HZ
* 2);
462 err
= finished(vdev
, &done
);
468 schedule_timeout_uninterruptible(HZ
/ 10);
469 } while (time_after(time
, jiffies
) || (count
< 3));
474 static int fnic_cleanup(struct fnic
*fnic
)
479 vnic_dev_disable(fnic
->vdev
);
480 for (i
= 0; i
< fnic
->intr_count
; i
++)
481 vnic_intr_mask(&fnic
->intr
[i
]);
483 for (i
= 0; i
< fnic
->rq_count
; i
++) {
484 err
= vnic_rq_disable(&fnic
->rq
[i
]);
488 for (i
= 0; i
< fnic
->raw_wq_count
; i
++) {
489 err
= vnic_wq_disable(&fnic
->wq
[i
]);
493 for (i
= 0; i
< fnic
->wq_copy_count
; i
++) {
494 err
= vnic_wq_copy_disable(&fnic
->wq_copy
[i
]);
499 /* Clean up completed IOs and FCS frames */
500 fnic_wq_copy_cmpl_handler(fnic
, io_completions
);
501 fnic_wq_cmpl_handler(fnic
, -1);
502 fnic_rq_cmpl_handler(fnic
, -1);
504 /* Clean up the IOs and FCS frames that have not completed */
505 for (i
= 0; i
< fnic
->raw_wq_count
; i
++)
506 vnic_wq_clean(&fnic
->wq
[i
], fnic_free_wq_buf
);
507 for (i
= 0; i
< fnic
->rq_count
; i
++)
508 vnic_rq_clean(&fnic
->rq
[i
], fnic_free_rq_buf
);
509 for (i
= 0; i
< fnic
->wq_copy_count
; i
++)
510 vnic_wq_copy_clean(&fnic
->wq_copy
[i
],
511 fnic_wq_copy_cleanup_handler
);
513 for (i
= 0; i
< fnic
->cq_count
; i
++)
514 vnic_cq_clean(&fnic
->cq
[i
]);
515 for (i
= 0; i
< fnic
->intr_count
; i
++)
516 vnic_intr_clean(&fnic
->intr
[i
]);
518 mempool_destroy(fnic
->io_req_pool
);
519 for (i
= 0; i
< FNIC_SGL_NUM_CACHES
; i
++)
520 mempool_destroy(fnic
->io_sgl_pool
[i
]);
525 static void fnic_iounmap(struct fnic
*fnic
)
527 if (fnic
->bar0
.vaddr
)
528 iounmap(fnic
->bar0
.vaddr
);
532 * fnic_get_mac() - get assigned data MAC address for FIP code.
533 * @lport: local port.
535 static u8
*fnic_get_mac(struct fc_lport
*lport
)
537 struct fnic
*fnic
= lport_priv(lport
);
539 return fnic
->data_src_addr
;
542 static void fnic_set_vlan(struct fnic
*fnic
, u16 vlan_id
)
544 vnic_dev_set_default_vlan(fnic
->vdev
, vlan_id
);
547 static int fnic_scsi_drv_init(struct fnic
*fnic
)
549 struct Scsi_Host
*host
= fnic
->lport
->host
;
551 /* Configure maximum outstanding IO reqs*/
552 if (fnic
->config
.io_throttle_count
!= FNIC_UCSM_DFLT_THROTTLE_CNT_BLD
)
553 host
->can_queue
= min_t(u32
, FNIC_MAX_IO_REQ
,
554 max_t(u32
, FNIC_MIN_IO_REQ
,
555 fnic
->config
.io_throttle_count
));
557 fnic
->fnic_max_tag_id
= host
->can_queue
;
558 host
->max_lun
= fnic
->config
.luns_per_tgt
;
559 host
->max_id
= FNIC_MAX_FCP_TARGET
;
560 host
->max_cmd_len
= FCOE_MAX_CMD_LEN
;
562 host
->nr_hw_queues
= fnic
->wq_copy_count
;
563 if (host
->nr_hw_queues
> 1)
564 shost_printk(KERN_ERR
, host
,
565 "fnic: blk-mq is not supported");
567 host
->nr_hw_queues
= fnic
->wq_copy_count
= 1;
569 shost_printk(KERN_INFO
, host
,
570 "fnic: can_queue: %d max_lun: %llu",
571 host
->can_queue
, host
->max_lun
);
573 shost_printk(KERN_INFO
, host
,
574 "fnic: max_id: %d max_cmd_len: %d nr_hw_queues: %d",
575 host
->max_id
, host
->max_cmd_len
, host
->nr_hw_queues
);
580 static int fnic_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
582 struct Scsi_Host
*host
;
591 * Allocate SCSI Host and set up association between host,
592 * local port, and fnic
594 lp
= libfc_host_alloc(&fnic_host_template
, sizeof(struct fnic
));
596 printk(KERN_ERR PFX
"Unable to alloc libfc local port\n");
601 fnic
= lport_priv(lp
);
605 fnic
->link_events
= 0;
607 snprintf(fnic
->name
, sizeof(fnic
->name
) - 1, "%s%d", DRV_NAME
,
610 host
->transportt
= fnic_fc_transport
;
612 fnic_stats_debugfs_init(fnic
);
614 /* Setup PCI resources */
615 pci_set_drvdata(pdev
, fnic
);
619 err
= pci_enable_device(pdev
);
621 shost_printk(KERN_ERR
, fnic
->lport
->host
,
622 "Cannot enable PCI device, aborting.\n");
623 goto err_out_free_hba
;
626 err
= pci_request_regions(pdev
, DRV_NAME
);
628 shost_printk(KERN_ERR
, fnic
->lport
->host
,
629 "Cannot enable PCI resources, aborting\n");
630 goto err_out_disable_device
;
633 pci_set_master(pdev
);
635 /* Query PCI controller on system for DMA addressing
636 * limitation for the device. Try 47-bit first, and
637 * fail to 32-bit. Cisco VIC supports 47 bits only.
639 err
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(47));
641 err
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(32));
643 shost_printk(KERN_ERR
, fnic
->lport
->host
,
644 "No usable DMA configuration "
646 goto err_out_release_regions
;
650 /* Map vNIC resources from BAR0 */
651 if (!(pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
)) {
652 shost_printk(KERN_ERR
, fnic
->lport
->host
,
653 "BAR0 not memory-map'able, aborting.\n");
655 goto err_out_release_regions
;
658 fnic
->bar0
.vaddr
= pci_iomap(pdev
, 0, 0);
659 fnic
->bar0
.bus_addr
= pci_resource_start(pdev
, 0);
660 fnic
->bar0
.len
= pci_resource_len(pdev
, 0);
662 if (!fnic
->bar0
.vaddr
) {
663 shost_printk(KERN_ERR
, fnic
->lport
->host
,
664 "Cannot memory-map BAR0 res hdr, "
667 goto err_out_release_regions
;
670 fnic
->vdev
= vnic_dev_register(NULL
, fnic
, pdev
, &fnic
->bar0
);
672 shost_printk(KERN_ERR
, fnic
->lport
->host
,
673 "vNIC registration failed, "
676 goto err_out_iounmap
;
679 err
= vnic_dev_cmd_init(fnic
->vdev
);
681 shost_printk(KERN_ERR
, fnic
->lport
->host
,
682 "vnic_dev_cmd_init() returns %d, aborting\n",
684 goto err_out_vnic_unregister
;
687 err
= fnic_dev_wait(fnic
->vdev
, vnic_dev_open
,
688 vnic_dev_open_done
, CMD_OPENF_RQ_ENABLE_THEN_POST
);
690 shost_printk(KERN_ERR
, fnic
->lport
->host
,
691 "vNIC dev open failed, aborting.\n");
692 goto err_out_dev_cmd_deinit
;
695 err
= vnic_dev_init(fnic
->vdev
, 0);
697 shost_printk(KERN_ERR
, fnic
->lport
->host
,
698 "vNIC dev init failed, aborting.\n");
699 goto err_out_dev_close
;
702 err
= vnic_dev_mac_addr(fnic
->vdev
, fnic
->ctlr
.ctl_src_addr
);
704 shost_printk(KERN_ERR
, fnic
->lport
->host
,
705 "vNIC get MAC addr failed \n");
706 goto err_out_dev_close
;
708 /* set data_src for point-to-point mode and to keep it non-zero */
709 memcpy(fnic
->data_src_addr
, fnic
->ctlr
.ctl_src_addr
, ETH_ALEN
);
711 /* Get vNIC configuration */
712 err
= fnic_get_vnic_config(fnic
);
714 shost_printk(KERN_ERR
, fnic
->lport
->host
,
715 "Get vNIC configuration failed, "
717 goto err_out_dev_close
;
720 fnic_scsi_drv_init(fnic
);
722 fnic_get_res_counts(fnic
);
724 err
= fnic_set_intr_mode(fnic
);
726 shost_printk(KERN_ERR
, fnic
->lport
->host
,
727 "Failed to set intr mode, "
729 goto err_out_dev_close
;
732 err
= fnic_alloc_vnic_resources(fnic
);
734 shost_printk(KERN_ERR
, fnic
->lport
->host
,
735 "Failed to alloc vNIC resources, "
737 goto err_out_clear_intr
;
741 /* initialize all fnic locks */
742 spin_lock_init(&fnic
->fnic_lock
);
744 for (i
= 0; i
< FNIC_WQ_MAX
; i
++)
745 spin_lock_init(&fnic
->wq_lock
[i
]);
747 for (i
= 0; i
< FNIC_WQ_COPY_MAX
; i
++) {
748 spin_lock_init(&fnic
->wq_copy_lock
[i
]);
749 fnic
->wq_copy_desc_low
[i
] = DESC_CLEAN_LOW_WATERMARK
;
750 fnic
->fw_ack_recd
[i
] = 0;
751 fnic
->fw_ack_index
[i
] = -1;
754 for (i
= 0; i
< FNIC_IO_LOCKS
; i
++)
755 spin_lock_init(&fnic
->io_req_lock
[i
]);
757 spin_lock_init(&fnic
->sgreset_lock
);
760 fnic
->io_req_pool
= mempool_create_slab_pool(2, fnic_io_req_cache
);
761 if (!fnic
->io_req_pool
)
762 goto err_out_free_resources
;
764 pool
= mempool_create_slab_pool(2, fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
766 goto err_out_free_ioreq_pool
;
767 fnic
->io_sgl_pool
[FNIC_SGL_CACHE_DFLT
] = pool
;
769 pool
= mempool_create_slab_pool(2, fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
771 goto err_out_free_dflt_pool
;
772 fnic
->io_sgl_pool
[FNIC_SGL_CACHE_MAX
] = pool
;
774 /* setup vlan config, hw inserts vlan header */
775 fnic
->vlan_hw_insert
= 1;
778 /* Initialize the FIP fcoe_ctrl struct */
779 fnic
->ctlr
.send
= fnic_eth_send
;
780 fnic
->ctlr
.update_mac
= fnic_update_mac
;
781 fnic
->ctlr
.get_src_addr
= fnic_get_mac
;
782 if (fnic
->config
.flags
& VFCF_FIP_CAPABLE
) {
783 shost_printk(KERN_INFO
, fnic
->lport
->host
,
784 "firmware supports FIP\n");
785 /* enable directed and multicast */
786 vnic_dev_packet_filter(fnic
->vdev
, 1, 1, 0, 0, 0);
787 vnic_dev_add_addr(fnic
->vdev
, FIP_ALL_ENODE_MACS
);
788 vnic_dev_add_addr(fnic
->vdev
, fnic
->ctlr
.ctl_src_addr
);
789 fnic
->set_vlan
= fnic_set_vlan
;
790 fcoe_ctlr_init(&fnic
->ctlr
, FIP_MODE_AUTO
);
791 timer_setup(&fnic
->fip_timer
, fnic_fip_notify_timer
, 0);
792 spin_lock_init(&fnic
->vlans_lock
);
793 INIT_WORK(&fnic
->fip_frame_work
, fnic_handle_fip_frame
);
794 INIT_WORK(&fnic
->event_work
, fnic_handle_event
);
795 skb_queue_head_init(&fnic
->fip_frame_queue
);
796 INIT_LIST_HEAD(&fnic
->evlist
);
797 INIT_LIST_HEAD(&fnic
->vlans
);
799 shost_printk(KERN_INFO
, fnic
->lport
->host
,
800 "firmware uses non-FIP mode\n");
801 fcoe_ctlr_init(&fnic
->ctlr
, FIP_MODE_NON_FIP
);
802 fnic
->ctlr
.state
= FIP_ST_NON_FIP
;
804 fnic
->state
= FNIC_IN_FC_MODE
;
806 atomic_set(&fnic
->in_flight
, 0);
807 fnic
->state_flags
= FNIC_FLAGS_NONE
;
809 /* Enable hardware stripping of vlan header on ingress */
810 fnic_set_nic_config(fnic
, 0, 0, 0, 0, 0, 0, 1);
812 /* Setup notification buffer area */
813 err
= fnic_notify_set(fnic
);
815 shost_printk(KERN_ERR
, fnic
->lport
->host
,
816 "Failed to alloc notify buffer, aborting.\n");
817 goto err_out_free_max_pool
;
820 /* Setup notify timer when using MSI interrupts */
821 if (vnic_dev_get_intr_mode(fnic
->vdev
) == VNIC_DEV_INTR_MODE_MSI
)
822 timer_setup(&fnic
->notify_timer
, fnic_notify_timer
, 0);
824 /* allocate RQ buffers and post them to RQ*/
825 for (i
= 0; i
< fnic
->rq_count
; i
++) {
826 vnic_rq_enable(&fnic
->rq
[i
]);
827 err
= vnic_rq_fill(&fnic
->rq
[i
], fnic_alloc_rq_frame
);
829 shost_printk(KERN_ERR
, fnic
->lport
->host
,
830 "fnic_alloc_rq_frame can't alloc "
832 goto err_out_free_rq_buf
;
837 * Initialization done with PCI system, hardware, firmware.
840 err
= scsi_add_host(lp
->host
, &pdev
->dev
);
842 shost_printk(KERN_ERR
, fnic
->lport
->host
,
843 "fnic: scsi_add_host failed...exiting\n");
844 goto err_out_free_rq_buf
;
847 /* Start local port initiatialization */
851 lp
->max_retry_count
= fnic
->config
.flogi_retries
;
852 lp
->max_rport_retry_count
= fnic
->config
.plogi_retries
;
853 lp
->service_params
= (FCP_SPPF_INIT_FCN
| FCP_SPPF_RD_XRDY_DIS
|
854 FCP_SPPF_CONF_COMPL
);
855 if (fnic
->config
.flags
& VFCF_FCP_SEQ_LVL_ERR
)
856 lp
->service_params
|= FCP_SPPF_RETRY
;
858 lp
->boot_time
= jiffies
;
859 lp
->e_d_tov
= fnic
->config
.ed_tov
;
860 lp
->r_a_tov
= fnic
->config
.ra_tov
;
861 lp
->link_supported_speeds
= FC_PORTSPEED_10GBIT
;
862 fc_set_wwnn(lp
, fnic
->config
.node_wwn
);
863 fc_set_wwpn(lp
, fnic
->config
.port_wwn
);
865 fcoe_libfc_config(lp
, &fnic
->ctlr
, &fnic_transport_template
, 0);
867 if (!fc_exch_mgr_alloc(lp
, FC_CLASS_3
, FCPIO_HOST_EXCH_RANGE_START
,
868 FCPIO_HOST_EXCH_RANGE_END
, NULL
)) {
870 goto err_out_remove_scsi_host
;
873 fc_lport_init_stats(lp
);
874 fnic
->stats_reset_time
= jiffies
;
878 if (fc_set_mfs(lp
, fnic
->config
.maxdatafieldsize
+
879 sizeof(struct fc_frame_header
))) {
881 goto err_out_free_exch_mgr
;
883 fc_host_maxframe_size(lp
->host
) = lp
->mfs
;
884 fc_host_dev_loss_tmo(lp
->host
) = fnic
->config
.port_down_timeout
/ 1000;
886 sprintf(fc_host_symbolic_name(lp
->host
),
887 DRV_NAME
" v" DRV_VERSION
" over %s", fnic
->name
);
889 spin_lock_irqsave(&fnic_list_lock
, flags
);
890 list_add_tail(&fnic
->list
, &fnic_list
);
891 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
893 INIT_WORK(&fnic
->link_work
, fnic_handle_link
);
894 INIT_WORK(&fnic
->frame_work
, fnic_handle_frame
);
895 skb_queue_head_init(&fnic
->frame_queue
);
896 skb_queue_head_init(&fnic
->tx_queue
);
898 /* Enable all queues */
899 for (i
= 0; i
< fnic
->raw_wq_count
; i
++)
900 vnic_wq_enable(&fnic
->wq
[i
]);
901 for (i
= 0; i
< fnic
->wq_copy_count
; i
++)
902 vnic_wq_copy_enable(&fnic
->wq_copy
[i
]);
906 err
= fnic_request_intr(fnic
);
908 shost_printk(KERN_ERR
, fnic
->lport
->host
,
909 "Unable to request irq.\n");
910 goto err_out_free_exch_mgr
;
913 vnic_dev_enable(fnic
->vdev
);
915 for (i
= 0; i
< fnic
->intr_count
; i
++)
916 vnic_intr_unmask(&fnic
->intr
[i
]);
918 fnic_notify_timer_start(fnic
);
922 err_out_free_exch_mgr
:
923 fc_exch_mgr_free(lp
);
924 err_out_remove_scsi_host
:
925 fc_remove_host(lp
->host
);
926 scsi_remove_host(lp
->host
);
928 for (i
= 0; i
< fnic
->rq_count
; i
++)
929 vnic_rq_clean(&fnic
->rq
[i
], fnic_free_rq_buf
);
930 vnic_dev_notify_unset(fnic
->vdev
);
931 err_out_free_max_pool
:
932 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_MAX
]);
933 err_out_free_dflt_pool
:
934 mempool_destroy(fnic
->io_sgl_pool
[FNIC_SGL_CACHE_DFLT
]);
935 err_out_free_ioreq_pool
:
936 mempool_destroy(fnic
->io_req_pool
);
937 err_out_free_resources
:
938 fnic_free_vnic_resources(fnic
);
940 fnic_clear_intr_mode(fnic
);
942 vnic_dev_close(fnic
->vdev
);
943 err_out_dev_cmd_deinit
:
944 err_out_vnic_unregister
:
945 vnic_dev_unregister(fnic
->vdev
);
948 err_out_release_regions
:
949 pci_release_regions(pdev
);
950 err_out_disable_device
:
951 pci_disable_device(pdev
);
953 fnic_stats_debugfs_remove(fnic
);
954 scsi_host_put(lp
->host
);
959 static void fnic_remove(struct pci_dev
*pdev
)
961 struct fnic
*fnic
= pci_get_drvdata(pdev
);
962 struct fc_lport
*lp
= fnic
->lport
;
966 * Mark state so that the workqueue thread stops forwarding
967 * received frames and link events to the local port. ISR and
968 * other threads that can queue work items will also stop
969 * creating work items on the fnic workqueue
971 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
972 fnic
->stop_rx_link_events
= 1;
973 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
975 if (vnic_dev_get_intr_mode(fnic
->vdev
) == VNIC_DEV_INTR_MODE_MSI
)
976 del_timer_sync(&fnic
->notify_timer
);
979 * Flush the fnic event queue. After this call, there should
980 * be no event queued for this fnic device in the workqueue
982 flush_workqueue(fnic_event_queue
);
983 skb_queue_purge(&fnic
->frame_queue
);
984 skb_queue_purge(&fnic
->tx_queue
);
986 if (fnic
->config
.flags
& VFCF_FIP_CAPABLE
) {
987 del_timer_sync(&fnic
->fip_timer
);
988 skb_queue_purge(&fnic
->fip_frame_queue
);
989 fnic_fcoe_reset_vlans(fnic
);
990 fnic_fcoe_evlist_free(fnic
);
994 * Log off the fabric. This stops all remote ports, dns port,
995 * logs off the fabric. This flushes all rport, disc, lport work
998 fc_fabric_logoff(fnic
->lport
);
1000 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
1001 fnic
->in_remove
= 1;
1002 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
1004 fcoe_ctlr_destroy(&fnic
->ctlr
);
1005 fc_lport_destroy(lp
);
1006 fnic_stats_debugfs_remove(fnic
);
1009 * This stops the fnic device, masks all interrupts. Completed
1010 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
1015 BUG_ON(!skb_queue_empty(&fnic
->frame_queue
));
1016 BUG_ON(!skb_queue_empty(&fnic
->tx_queue
));
1018 spin_lock_irqsave(&fnic_list_lock
, flags
);
1019 list_del(&fnic
->list
);
1020 spin_unlock_irqrestore(&fnic_list_lock
, flags
);
1022 fc_remove_host(fnic
->lport
->host
);
1023 scsi_remove_host(fnic
->lport
->host
);
1024 fc_exch_mgr_free(fnic
->lport
);
1025 vnic_dev_notify_unset(fnic
->vdev
);
1026 fnic_free_intr(fnic
);
1027 fnic_free_vnic_resources(fnic
);
1028 fnic_clear_intr_mode(fnic
);
1029 vnic_dev_close(fnic
->vdev
);
1030 vnic_dev_unregister(fnic
->vdev
);
1032 pci_release_regions(pdev
);
1033 pci_disable_device(pdev
);
1034 scsi_host_put(lp
->host
);
1037 static struct pci_driver fnic_driver
= {
1039 .id_table
= fnic_id_table
,
1040 .probe
= fnic_probe
,
1041 .remove
= fnic_remove
,
1044 static int __init
fnic_init_module(void)
1049 printk(KERN_INFO PFX
"%s, ver %s\n", DRV_DESCRIPTION
, DRV_VERSION
);
1051 /* Create debugfs entries for fnic */
1052 err
= fnic_debugfs_init();
1054 printk(KERN_ERR PFX
"Failed to create fnic directory "
1055 "for tracing and stats logging\n");
1056 fnic_debugfs_terminate();
1059 /* Allocate memory for trace buffer */
1060 err
= fnic_trace_buf_init();
1063 "Trace buffer initialization Failed. "
1064 "Fnic Tracing utility is disabled\n");
1068 /* Allocate memory for fc trace buffer */
1069 err
= fnic_fc_trace_init();
1071 printk(KERN_ERR PFX
"FC trace buffer initialization Failed "
1072 "FC frame tracing utility is disabled\n");
1073 fnic_fc_trace_free();
1076 /* Create a cache for allocation of default size sgls */
1077 len
= sizeof(struct fnic_dflt_sgl_list
);
1078 fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
] = kmem_cache_create
1079 ("fnic_sgl_dflt", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
1082 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]) {
1083 printk(KERN_ERR PFX
"failed to create fnic dflt sgl slab\n");
1085 goto err_create_fnic_sgl_slab_dflt
;
1088 /* Create a cache for allocation of max size sgls*/
1089 len
= sizeof(struct fnic_sgl_list
);
1090 fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
] = kmem_cache_create
1091 ("fnic_sgl_max", len
+ FNIC_SG_DESC_ALIGN
, FNIC_SG_DESC_ALIGN
,
1094 if (!fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]) {
1095 printk(KERN_ERR PFX
"failed to create fnic max sgl slab\n");
1097 goto err_create_fnic_sgl_slab_max
;
1100 /* Create a cache of io_req structs for use via mempool */
1101 fnic_io_req_cache
= kmem_cache_create("fnic_io_req",
1102 sizeof(struct fnic_io_req
),
1103 0, SLAB_HWCACHE_ALIGN
, NULL
);
1104 if (!fnic_io_req_cache
) {
1105 printk(KERN_ERR PFX
"failed to create fnic io_req slab\n");
1107 goto err_create_fnic_ioreq_slab
;
1110 fnic_event_queue
= create_singlethread_workqueue("fnic_event_wq");
1111 if (!fnic_event_queue
) {
1112 printk(KERN_ERR PFX
"fnic work queue create failed\n");
1114 goto err_create_fnic_workq
;
1117 fnic_fip_queue
= create_singlethread_workqueue("fnic_fip_q");
1118 if (!fnic_fip_queue
) {
1119 printk(KERN_ERR PFX
"fnic FIP work queue create failed\n");
1121 goto err_create_fip_workq
;
1124 fnic_fc_transport
= fc_attach_transport(&fnic_fc_functions
);
1125 if (!fnic_fc_transport
) {
1126 printk(KERN_ERR PFX
"fc_attach_transport error\n");
1128 goto err_fc_transport
;
1131 /* register the driver with PCI system */
1132 err
= pci_register_driver(&fnic_driver
);
1134 printk(KERN_ERR PFX
"pci register error\n");
1135 goto err_pci_register
;
1140 fc_release_transport(fnic_fc_transport
);
1142 destroy_workqueue(fnic_fip_queue
);
1143 err_create_fip_workq
:
1144 destroy_workqueue(fnic_event_queue
);
1145 err_create_fnic_workq
:
1146 kmem_cache_destroy(fnic_io_req_cache
);
1147 err_create_fnic_ioreq_slab
:
1148 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
1149 err_create_fnic_sgl_slab_max
:
1150 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
1151 err_create_fnic_sgl_slab_dflt
:
1153 fnic_fc_trace_free();
1154 fnic_debugfs_terminate();
1158 static void __exit
fnic_cleanup_module(void)
1160 pci_unregister_driver(&fnic_driver
);
1161 destroy_workqueue(fnic_event_queue
);
1163 destroy_workqueue(fnic_fip_queue
);
1164 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_MAX
]);
1165 kmem_cache_destroy(fnic_sgl_cache
[FNIC_SGL_CACHE_DFLT
]);
1166 kmem_cache_destroy(fnic_io_req_cache
);
1167 fc_release_transport(fnic_fc_transport
);
1169 fnic_fc_trace_free();
1170 fnic_debugfs_terminate();
1173 module_init(fnic_init_module
);
1174 module_exit(fnic_cleanup_module
);