PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / scsi / fnic / fnic_main.c
blob33e4ec2bfe734eefda1cd049bfde7f1112cf0ea2
1 /*
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
16 * SOFTWARE.
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>
38 #include "vnic_dev.h"
39 #include "vnic_intr.h"
40 #include "vnic_stats.h"
41 #include "fnic_io.h"
42 #include "fnic_fip.h"
43 #include "fnic.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;
52 LIST_HEAD(fnic_list);
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) },
58 { 0, }
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))
96 return -ENXIO;
98 scsi_activate_tcq(sdev, fnic_max_qdepth);
99 return 0;
102 static struct scsi_host_template fnic_host_template = {
103 .module = THIS_MODULE,
104 .name = DRV_NAME,
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,
112 .this_id = -1,
113 .cmd_per_lun = 3,
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,
121 static void
122 fnic_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
124 if (timeout)
125 rport->dev_loss_tmo = timeout;
126 else
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) {
175 case 10000:
176 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
177 break;
178 default:
179 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
180 break;
184 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host)
186 int ret;
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;
191 unsigned long flags;
193 if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT))
194 return stats;
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);
201 if (ret) {
202 FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host,
203 "fnic: Get vnic stats failed"
204 " 0x%x", ret);
205 return stats;
207 vs = fnic->stats;
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);
220 return stats;
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",
235 stats->tx_frames);
236 FNIC_MAIN_NOTE(KERN_NOTICE, host,
237 "fnic: tx words = %llu\n",
238 stats->tx_words);
239 FNIC_MAIN_NOTE(KERN_NOTICE, host,
240 "fnic: rx frames = %llu\n",
241 stats->rx_frames);
242 FNIC_MAIN_NOTE(KERN_NOTICE, host,
243 "fnic: rx words = %llu\n",
244 stats->rx_words);
245 FNIC_MAIN_NOTE(KERN_NOTICE, host,
246 "fnic: lip count = %llu\n",
247 stats->lip_count);
248 FNIC_MAIN_NOTE(KERN_NOTICE, host,
249 "fnic: nos count = %llu\n",
250 stats->nos_count);
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);
290 return;
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)
299 int ret;
300 struct fc_lport *lp = shost_priv(host);
301 struct fnic *fnic = lport_priv(lp);
302 struct fc_host_statistics *stats;
303 unsigned long flags;
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);
313 if (ret) {
314 FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host,
315 "fnic: Reset vnic stats failed"
316 " 0x%x", ret);
317 return;
319 fnic->stats_reset_time = jiffies;
320 memset(stats, 0, sizeof(*stats));
322 return;
325 void fnic_log_q_error(struct fnic *fnic)
327 unsigned int i;
328 u32 error_status;
330 for (i = 0; i < fnic->raw_wq_count; i++) {
331 error_status = ioread32(&fnic->wq[i].ctrl->error_status);
332 if (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);
340 if (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);
348 if (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)
357 unsigned long flags;
359 spin_lock_irqsave(&fnic->fnic_lock, flags);
360 if (fnic->stop_rx_link_events) {
361 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
362 return;
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)
372 int err;
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);
377 break;
378 case VNIC_DEV_INTR_MODE_MSI:
379 err = vnic_dev_notify_set(fnic->vdev, -1);
380 break;
381 case VNIC_DEV_INTR_MODE_MSIX:
382 err = vnic_dev_notify_set(fnic->vdev, FNIC_MSIX_ERR_NOTIFY);
383 break;
384 default:
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));
389 err = -1;
390 break;
393 return err;
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);
421 break;
422 default:
423 /* Using intr for notification for INTx/MSI-X */
424 break;
428 static int fnic_dev_wait(struct vnic_dev *vdev,
429 int (*start)(struct vnic_dev *, int),
430 int (*finished)(struct vnic_dev *, int *),
431 int arg)
433 unsigned long time;
434 int done;
435 int err;
437 err = start(vdev, arg);
438 if (err)
439 return err;
441 /* Wait for func to complete...2 seconds max */
442 time = jiffies + (HZ * 2);
443 do {
444 err = finished(vdev, &done);
445 if (err)
446 return err;
447 if (done)
448 return 0;
449 schedule_timeout_uninterruptible(HZ / 10);
450 } while (time_after(time, jiffies));
452 return -ETIMEDOUT;
455 static int fnic_cleanup(struct fnic *fnic)
457 unsigned int i;
458 int err;
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]);
466 if (err)
467 return err;
469 for (i = 0; i < fnic->raw_wq_count; i++) {
470 err = vnic_wq_disable(&fnic->wq[i]);
471 if (err)
472 return err;
474 for (i = 0; i < fnic->wq_copy_count; i++) {
475 err = vnic_wq_copy_disable(&fnic->wq_copy[i]);
476 if (err)
477 return err;
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]);
503 return 0;
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)
525 u16 old_vlan;
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;
532 struct fc_lport *lp;
533 struct fnic *fnic;
534 mempool_t *pool;
535 int err;
536 int i;
537 unsigned long flags;
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));
544 if (!lp) {
545 printk(KERN_ERR PFX "Unable to alloc libfc local port\n");
546 err = -ENOMEM;
547 goto err_out;
549 host = lp->host;
550 fnic = lport_priv(lp);
551 fnic->lport = lp;
552 fnic->ctlr.lp = lp;
554 snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME,
555 host->host_no);
557 host->transportt = fnic_fc_transport;
559 err = fnic_stats_debugfs_init(fnic);
560 if (err) {
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);
569 fnic->pdev = pdev;
571 err = pci_enable_device(pdev);
572 if (err) {
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);
579 if (err) {
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
589 * fail to 32-bit.
591 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
592 if (err) {
593 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
594 if (err) {
595 shost_printk(KERN_ERR, fnic->lport->host,
596 "No usable DMA configuration "
597 "aborting\n");
598 goto err_out_release_regions;
600 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
601 if (err) {
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;
607 } else {
608 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
609 if (err) {
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");
621 err = -ENODEV;
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, "
632 "aborting.\n");
633 err = -ENODEV;
634 goto err_out_release_regions;
637 fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0);
638 if (!fnic->vdev) {
639 shost_printk(KERN_ERR, fnic->lport->host,
640 "vNIC registration failed, "
641 "aborting.\n");
642 err = -ENODEV;
643 goto err_out_iounmap;
646 err = fnic_dev_wait(fnic->vdev, vnic_dev_open,
647 vnic_dev_open_done, 0);
648 if (err) {
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);
655 if (err) {
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);
662 if (err) {
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);
672 if (err) {
673 shost_printk(KERN_ERR, fnic->lport->host,
674 "Get vNIC configuration failed, "
675 "aborting.\n");
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);
688 if (err) {
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);
701 if (err) {
702 shost_printk(KERN_ERR, fnic->lport->host,
703 "Failed to set intr mode, "
704 "aborting.\n");
705 goto err_out_dev_close;
708 err = fnic_alloc_vnic_resources(fnic);
709 if (err) {
710 shost_printk(KERN_ERR, fnic->lport->host,
711 "Failed to alloc vNIC resources, "
712 "aborting.\n");
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]);
738 if (!pool)
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]);
743 if (!pool)
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;
749 fnic->vlan_id = 0;
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);
772 } else {
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);
787 if (err) {
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);
801 if (err) {
802 shost_printk(KERN_ERR, fnic->lport->host,
803 "fnic_alloc_rq_frame can't alloc "
804 "frame\n");
805 goto err_out_free_rq_buf;
810 * Initialization done with PCI system, hardware, firmware.
811 * Add host to SCSI
813 err = scsi_add_host(lp->host, &pdev->dev);
814 if (err) {
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 */
822 lp->link_up = 0;
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)) {
842 err = -ENOMEM;
843 goto err_out_remove_scsi_host;
846 fc_lport_init_stats(lp);
847 fnic->stats_reset_time = jiffies;
849 fc_lport_config(lp);
851 if (fc_set_mfs(lp, fnic->config.maxdatafieldsize +
852 sizeof(struct fc_frame_header))) {
853 err = -EINVAL;
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]);
879 fc_fabric_login(lp);
881 vnic_dev_enable(fnic->vdev);
883 err = fnic_request_intr(fnic);
884 if (err) {
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);
895 return 0;
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);
902 err_out_free_rq_buf:
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);
914 err_out_clear_intr:
915 fnic_clear_intr_mode(fnic);
916 err_out_dev_close:
917 vnic_dev_close(fnic->vdev);
918 err_out_vnic_unregister:
919 vnic_dev_unregister(fnic->vdev);
920 err_out_iounmap:
921 fnic_iounmap(fnic);
922 err_out_release_regions:
923 pci_release_regions(pdev);
924 err_out_disable_device:
925 pci_disable_device(pdev);
926 err_out_free_hba:
927 fnic_stats_debugfs_remove(fnic);
928 scsi_host_put(lp->host);
929 err_out:
930 return err;
933 static void fnic_remove(struct pci_dev *pdev)
935 struct fnic *fnic = pci_get_drvdata(pdev);
936 struct fc_lport *lp = fnic->lport;
937 unsigned long flags;
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
970 * before returning
972 fc_fabric_logoff(fnic->lport);
974 spin_lock_irqsave(&fnic->fnic_lock, flags);
975 fnic->in_remove = 1;
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
985 * cleaned up
987 fnic_cleanup(fnic);
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);
1005 fnic_iounmap(fnic);
1006 pci_release_regions(pdev);
1007 pci_disable_device(pdev);
1008 scsi_host_put(lp->host);
1011 static struct pci_driver fnic_driver = {
1012 .name = DRV_NAME,
1013 .id_table = fnic_id_table,
1014 .probe = fnic_probe,
1015 .remove = fnic_remove,
1018 static int __init fnic_init_module(void)
1020 size_t len;
1021 int err = 0;
1023 printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
1025 /* Create debugfs entries for fnic */
1026 err = fnic_debugfs_init();
1027 if (err < 0) {
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();
1035 if (err < 0) {
1036 printk(KERN_ERR PFX "Trace buffer initialization Failed "
1037 "Fnic Tracing utility is disabled\n");
1038 fnic_trace_free();
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,
1045 SLAB_HWCACHE_ALIGN,
1046 NULL);
1047 if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) {
1048 printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n");
1049 err = -ENOMEM;
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,
1057 SLAB_HWCACHE_ALIGN,
1058 NULL);
1059 if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) {
1060 printk(KERN_ERR PFX "failed to create fnic max sgl slab\n");
1061 err = -ENOMEM;
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");
1071 err = -ENOMEM;
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");
1078 err = -ENOMEM;
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");
1088 err = -ENOMEM;
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");
1095 err = -ENOMEM;
1096 goto err_fc_transport;
1099 /* register the driver with PCI system */
1100 err = pci_register_driver(&fnic_driver);
1101 if (err < 0) {
1102 printk(KERN_ERR PFX "pci register error\n");
1103 goto err_pci_register;
1105 return err;
1107 err_pci_register:
1108 fc_release_transport(fnic_fc_transport);
1109 err_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:
1120 fnic_trace_free();
1121 fnic_debugfs_terminate();
1122 return err;
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);
1137 fnic_trace_free();
1138 fnic_debugfs_terminate();
1141 module_init(fnic_init_module);
1142 module_exit(fnic_cleanup_module);