xref: /linux/drivers/scsi/fnic/fnic_main.c (revision e7602bb4f3a1234df8b75728ac3260bcb8242612)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
4  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
5  */
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/irq.h>
16 #include <linux/spinlock.h>
17 #include <linux/workqueue.h>
18 #include <linux/if_ether.h>
19 #include <scsi/fc/fc_fip.h>
20 #include <scsi/scsi_host.h>
21 #include <scsi/scsi_transport.h>
22 #include <scsi/scsi_transport_fc.h>
23 #include <scsi/scsi_tcq.h>
24 #include <scsi/libfc.h>
25 #include <scsi/fc_frame.h>
26 
27 #include "vnic_dev.h"
28 #include "vnic_intr.h"
29 #include "vnic_stats.h"
30 #include "fnic_io.h"
31 #include "fnic_fip.h"
32 #include "fnic.h"
33 
34 #define PCI_DEVICE_ID_CISCO_FNIC	0x0045
35 
36 /* Timer to poll notification area for events. Used for MSI interrupts */
37 #define FNIC_NOTIFY_TIMER_PERIOD	(2 * HZ)
38 
39 static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES];
40 static struct kmem_cache *fnic_io_req_cache;
41 static LIST_HEAD(fnic_list);
42 static DEFINE_SPINLOCK(fnic_list_lock);
43 static DEFINE_IDA(fnic_ida);
44 
45 /* Supported devices by fnic module */
46 static struct pci_device_id fnic_id_table[] = {
47 	{ PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_FNIC) },
48 	{ 0, }
49 };
50 
51 MODULE_DESCRIPTION(DRV_DESCRIPTION);
52 MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, "
53 	      "Joseph R. Eykholt <jeykholt@cisco.com>");
54 MODULE_LICENSE("GPL v2");
55 MODULE_VERSION(DRV_VERSION);
56 MODULE_DEVICE_TABLE(pci, fnic_id_table);
57 
58 unsigned int fnic_log_level;
59 module_param(fnic_log_level, int, S_IRUGO|S_IWUSR);
60 MODULE_PARM_DESC(fnic_log_level, "bit mask of fnic logging levels");
61 
62 
63 unsigned int io_completions = FNIC_DFLT_IO_COMPLETIONS;
64 module_param(io_completions, int, S_IRUGO|S_IWUSR);
65 MODULE_PARM_DESC(io_completions, "Max CQ entries to process at a time");
66 
67 unsigned int fnic_trace_max_pages = 16;
68 module_param(fnic_trace_max_pages, uint, S_IRUGO|S_IWUSR);
69 MODULE_PARM_DESC(fnic_trace_max_pages, "Total allocated memory pages "
70 					"for fnic trace buffer");
71 
72 unsigned int fnic_fc_trace_max_pages = 64;
73 module_param(fnic_fc_trace_max_pages, uint, S_IRUGO|S_IWUSR);
74 MODULE_PARM_DESC(fnic_fc_trace_max_pages,
75 		 "Total allocated memory pages for fc trace buffer");
76 
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");
80 
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
87 };
88 
89 static int fnic_slave_alloc(struct scsi_device *sdev)
90 {
91 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
92 
93 	if (!rport || fc_remote_port_chkready(rport))
94 		return -ENXIO;
95 
96 	scsi_change_queue_depth(sdev, fnic_max_qdepth);
97 	return 0;
98 }
99 
100 static const struct scsi_host_template fnic_host_template = {
101 	.module = THIS_MODULE,
102 	.name = DRV_NAME,
103 	.queuecommand = fnic_queuecommand,
104 	.eh_timed_out = fc_eh_timed_out,
105 	.eh_abort_handler = fnic_abort_cmd,
106 	.eh_device_reset_handler = fnic_device_reset,
107 	.eh_host_reset_handler = fnic_host_reset,
108 	.slave_alloc = fnic_slave_alloc,
109 	.change_queue_depth = scsi_change_queue_depth,
110 	.this_id = -1,
111 	.cmd_per_lun = 3,
112 	.can_queue = FNIC_DFLT_IO_REQ,
113 	.sg_tablesize = FNIC_MAX_SG_DESC_CNT,
114 	.max_sectors = 0xffff,
115 	.shost_groups = fnic_host_groups,
116 	.track_queue_depth = 1,
117 	.cmd_size = sizeof(struct fnic_cmd_priv),
118 	.map_queues = fnic_mq_map_queues_cpus,
119 };
120 
121 static void
122 fnic_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
123 {
124 	if (timeout)
125 		rport->dev_loss_tmo = timeout;
126 	else
127 		rport->dev_loss_tmo = 1;
128 }
129 
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 *);
134 
135 static struct fc_function_template fnic_fc_functions = {
136 
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,
165 };
166 
167 static void fnic_get_host_speed(struct Scsi_Host *shost)
168 {
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);
172 
173 	/* Add in other values as they get defined in fw */
174 	switch (port_speed) {
175 	case DCEM_PORTSPEED_10G:
176 		fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
177 		break;
178 	case DCEM_PORTSPEED_20G:
179 		fc_host_speed(shost) = FC_PORTSPEED_20GBIT;
180 		break;
181 	case DCEM_PORTSPEED_25G:
182 		fc_host_speed(shost) = FC_PORTSPEED_25GBIT;
183 		break;
184 	case DCEM_PORTSPEED_40G:
185 	case DCEM_PORTSPEED_4x10G:
186 		fc_host_speed(shost) = FC_PORTSPEED_40GBIT;
187 		break;
188 	case DCEM_PORTSPEED_100G:
189 		fc_host_speed(shost) = FC_PORTSPEED_100GBIT;
190 		break;
191 	default:
192 		fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
193 		break;
194 	}
195 }
196 
197 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host)
198 {
199 	int ret;
200 	struct fc_lport *lp = shost_priv(host);
201 	struct fnic *fnic = lport_priv(lp);
202 	struct fc_host_statistics *stats = &lp->host_stats;
203 	struct vnic_stats *vs;
204 	unsigned long flags;
205 
206 	if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT))
207 		return stats;
208 	fnic->stats_time = jiffies;
209 
210 	spin_lock_irqsave(&fnic->fnic_lock, flags);
211 	ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
212 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
213 
214 	if (ret) {
215 		FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host, fnic->fnic_num,
216 			      "fnic: Get vnic stats failed"
217 			      " 0x%x", ret);
218 		return stats;
219 	}
220 	vs = fnic->stats;
221 	stats->tx_frames = vs->tx.tx_unicast_frames_ok;
222 	stats->tx_words  = vs->tx.tx_unicast_bytes_ok / 4;
223 	stats->rx_frames = vs->rx.rx_unicast_frames_ok;
224 	stats->rx_words  = vs->rx.rx_unicast_bytes_ok / 4;
225 	stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors;
226 	stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop;
227 	stats->invalid_crc_count = vs->rx.rx_crc_errors;
228 	stats->seconds_since_last_reset =
229 			(jiffies - fnic->stats_reset_time) / HZ;
230 	stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000);
231 	stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000);
232 
233 	return stats;
234 }
235 
236 /*
237  * fnic_dump_fchost_stats
238  * note : dumps fc_statistics into system logs
239  */
240 void fnic_dump_fchost_stats(struct Scsi_Host *host,
241 				struct fc_host_statistics *stats)
242 {
243 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
244 			"fnic: seconds since last reset = %llu\n",
245 			stats->seconds_since_last_reset);
246 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
247 			"fnic: tx frames		= %llu\n",
248 			stats->tx_frames);
249 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
250 			"fnic: tx words		= %llu\n",
251 			stats->tx_words);
252 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
253 			"fnic: rx frames		= %llu\n",
254 			stats->rx_frames);
255 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
256 			"fnic: rx words		= %llu\n",
257 			stats->rx_words);
258 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
259 			"fnic: lip count		= %llu\n",
260 			stats->lip_count);
261 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
262 			"fnic: nos count		= %llu\n",
263 			stats->nos_count);
264 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
265 			"fnic: error frames		= %llu\n",
266 			stats->error_frames);
267 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
268 			"fnic: dumped frames	= %llu\n",
269 			stats->dumped_frames);
270 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
271 			"fnic: link failure count	= %llu\n",
272 			stats->link_failure_count);
273 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
274 			"fnic: loss of sync count	= %llu\n",
275 			stats->loss_of_sync_count);
276 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
277 			"fnic: loss of signal count	= %llu\n",
278 			stats->loss_of_signal_count);
279 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
280 			"fnic: prim seq protocol err count = %llu\n",
281 			stats->prim_seq_protocol_err_count);
282 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
283 			"fnic: invalid tx word count= %llu\n",
284 			stats->invalid_tx_word_count);
285 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
286 			"fnic: invalid crc count	= %llu\n",
287 			stats->invalid_crc_count);
288 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
289 			"fnic: fcp input requests	= %llu\n",
290 			stats->fcp_input_requests);
291 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
292 			"fnic: fcp output requests	= %llu\n",
293 			stats->fcp_output_requests);
294 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
295 			"fnic: fcp control requests	= %llu\n",
296 			stats->fcp_control_requests);
297 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
298 			"fnic: fcp input megabytes	= %llu\n",
299 			stats->fcp_input_megabytes);
300 	FNIC_MAIN_NOTE(KERN_NOTICE, host,
301 			"fnic: fcp output megabytes	= %llu\n",
302 			stats->fcp_output_megabytes);
303 	return;
304 }
305 
306 /*
307  * fnic_reset_host_stats : clears host stats
308  * note : called when reset_statistics set under sysfs dir
309  */
310 static void fnic_reset_host_stats(struct Scsi_Host *host)
311 {
312 	int ret;
313 	struct fc_lport *lp = shost_priv(host);
314 	struct fnic *fnic = lport_priv(lp);
315 	struct fc_host_statistics *stats;
316 	unsigned long flags;
317 
318 	/* dump current stats, before clearing them */
319 	stats = fnic_get_stats(host);
320 	fnic_dump_fchost_stats(host, stats);
321 
322 	spin_lock_irqsave(&fnic->fnic_lock, flags);
323 	ret = vnic_dev_stats_clear(fnic->vdev);
324 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
325 
326 	if (ret) {
327 		FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host, fnic->fnic_num,
328 				"fnic: Reset vnic stats failed"
329 				" 0x%x", ret);
330 		return;
331 	}
332 	fnic->stats_reset_time = jiffies;
333 	memset(stats, 0, sizeof(*stats));
334 
335 	return;
336 }
337 
338 void fnic_log_q_error(struct fnic *fnic)
339 {
340 	unsigned int i;
341 	u32 error_status;
342 
343 	for (i = 0; i < fnic->raw_wq_count; i++) {
344 		error_status = ioread32(&fnic->wq[i].ctrl->error_status);
345 		if (error_status)
346 			shost_printk(KERN_ERR, fnic->lport->host,
347 				     "WQ[%d] error_status"
348 				     " %d\n", i, error_status);
349 	}
350 
351 	for (i = 0; i < fnic->rq_count; i++) {
352 		error_status = ioread32(&fnic->rq[i].ctrl->error_status);
353 		if (error_status)
354 			shost_printk(KERN_ERR, fnic->lport->host,
355 				     "RQ[%d] error_status"
356 				     " %d\n", i, error_status);
357 	}
358 
359 	for (i = 0; i < fnic->wq_copy_count; i++) {
360 		error_status = ioread32(&fnic->hw_copy_wq[i].ctrl->error_status);
361 		if (error_status)
362 			shost_printk(KERN_ERR, fnic->lport->host,
363 				     "CWQ[%d] error_status"
364 				     " %d\n", i, error_status);
365 	}
366 }
367 
368 void fnic_handle_link_event(struct fnic *fnic)
369 {
370 	unsigned long flags;
371 
372 	spin_lock_irqsave(&fnic->fnic_lock, flags);
373 	if (fnic->stop_rx_link_events) {
374 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
375 		return;
376 	}
377 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
378 
379 	queue_work(fnic_event_queue, &fnic->link_work);
380 
381 }
382 
383 static int fnic_notify_set(struct fnic *fnic)
384 {
385 	int err;
386 
387 	switch (vnic_dev_get_intr_mode(fnic->vdev)) {
388 	case VNIC_DEV_INTR_MODE_INTX:
389 		err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY);
390 		break;
391 	case VNIC_DEV_INTR_MODE_MSI:
392 		err = vnic_dev_notify_set(fnic->vdev, -1);
393 		break;
394 	case VNIC_DEV_INTR_MODE_MSIX:
395 		err = vnic_dev_notify_set(fnic->vdev, fnic->wq_copy_count + fnic->copy_wq_base);
396 		break;
397 	default:
398 		shost_printk(KERN_ERR, fnic->lport->host,
399 			     "Interrupt mode should be set up"
400 			     " before devcmd notify set %d\n",
401 			     vnic_dev_get_intr_mode(fnic->vdev));
402 		err = -1;
403 		break;
404 	}
405 
406 	return err;
407 }
408 
409 static void fnic_notify_timer(struct timer_list *t)
410 {
411 	struct fnic *fnic = from_timer(fnic, t, notify_timer);
412 
413 	fnic_handle_link_event(fnic);
414 	mod_timer(&fnic->notify_timer,
415 		  round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD));
416 }
417 
418 static void fnic_fip_notify_timer(struct timer_list *t)
419 {
420 	struct fnic *fnic = from_timer(fnic, t, fip_timer);
421 
422 	fnic_handle_fip_timer(fnic);
423 }
424 
425 static void fnic_notify_timer_start(struct fnic *fnic)
426 {
427 	switch (vnic_dev_get_intr_mode(fnic->vdev)) {
428 	case VNIC_DEV_INTR_MODE_MSI:
429 		/*
430 		 * Schedule first timeout immediately. The driver is
431 		 * initiatialized and ready to look for link up notification
432 		 */
433 		mod_timer(&fnic->notify_timer, jiffies);
434 		break;
435 	default:
436 		/* Using intr for notification for INTx/MSI-X */
437 		break;
438 	}
439 }
440 
441 static int fnic_dev_wait(struct vnic_dev *vdev,
442 			 int (*start)(struct vnic_dev *, int),
443 			 int (*finished)(struct vnic_dev *, int *),
444 			 int arg)
445 {
446 	unsigned long time;
447 	int done;
448 	int err;
449 	int count;
450 
451 	count = 0;
452 
453 	err = start(vdev, arg);
454 	if (err)
455 		return err;
456 
457 	/* Wait for func to complete.
458 	* Sometime schedule_timeout_uninterruptible take long time
459 	* to wake up so we do not retry as we are only waiting for
460 	* 2 seconds in while loop. By adding count, we make sure
461 	* we try atleast three times before returning -ETIMEDOUT
462 	*/
463 	time = jiffies + (HZ * 2);
464 	do {
465 		err = finished(vdev, &done);
466 		count++;
467 		if (err)
468 			return err;
469 		if (done)
470 			return 0;
471 		schedule_timeout_uninterruptible(HZ / 10);
472 	} while (time_after(time, jiffies) || (count < 3));
473 
474 	return -ETIMEDOUT;
475 }
476 
477 static int fnic_cleanup(struct fnic *fnic)
478 {
479 	unsigned int i;
480 	int err;
481 	int raw_wq_rq_counts;
482 
483 	vnic_dev_disable(fnic->vdev);
484 	for (i = 0; i < fnic->intr_count; i++)
485 		vnic_intr_mask(&fnic->intr[i]);
486 
487 	for (i = 0; i < fnic->rq_count; i++) {
488 		err = vnic_rq_disable(&fnic->rq[i]);
489 		if (err)
490 			return err;
491 	}
492 	for (i = 0; i < fnic->raw_wq_count; i++) {
493 		err = vnic_wq_disable(&fnic->wq[i]);
494 		if (err)
495 			return err;
496 	}
497 	for (i = 0; i < fnic->wq_copy_count; i++) {
498 		err = vnic_wq_copy_disable(&fnic->hw_copy_wq[i]);
499 		if (err)
500 			return err;
501 		raw_wq_rq_counts = fnic->raw_wq_count + fnic->rq_count;
502 		fnic_wq_copy_cmpl_handler(fnic, -1, i + raw_wq_rq_counts);
503 	}
504 
505 	/* Clean up completed IOs and FCS frames */
506 	fnic_wq_cmpl_handler(fnic, -1);
507 	fnic_rq_cmpl_handler(fnic, -1);
508 
509 	/* Clean up the IOs and FCS frames that have not completed */
510 	for (i = 0; i < fnic->raw_wq_count; i++)
511 		vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf);
512 	for (i = 0; i < fnic->rq_count; i++)
513 		vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
514 	for (i = 0; i < fnic->wq_copy_count; i++)
515 		vnic_wq_copy_clean(&fnic->hw_copy_wq[i],
516 				   fnic_wq_copy_cleanup_handler);
517 
518 	for (i = 0; i < fnic->cq_count; i++)
519 		vnic_cq_clean(&fnic->cq[i]);
520 	for (i = 0; i < fnic->intr_count; i++)
521 		vnic_intr_clean(&fnic->intr[i]);
522 
523 	mempool_destroy(fnic->io_req_pool);
524 	for (i = 0; i < FNIC_SGL_NUM_CACHES; i++)
525 		mempool_destroy(fnic->io_sgl_pool[i]);
526 
527 	return 0;
528 }
529 
530 static void fnic_iounmap(struct fnic *fnic)
531 {
532 	if (fnic->bar0.vaddr)
533 		iounmap(fnic->bar0.vaddr);
534 }
535 
536 /**
537  * fnic_get_mac() - get assigned data MAC address for FIP code.
538  * @lport: 	local port.
539  */
540 static u8 *fnic_get_mac(struct fc_lport *lport)
541 {
542 	struct fnic *fnic = lport_priv(lport);
543 
544 	return fnic->data_src_addr;
545 }
546 
547 static void fnic_set_vlan(struct fnic *fnic, u16 vlan_id)
548 {
549 	vnic_dev_set_default_vlan(fnic->vdev, vlan_id);
550 }
551 
552 static int fnic_scsi_drv_init(struct fnic *fnic)
553 {
554 	struct Scsi_Host *host = fnic->lport->host;
555 
556 	/* Configure maximum outstanding IO reqs*/
557 	if (fnic->config.io_throttle_count != FNIC_UCSM_DFLT_THROTTLE_CNT_BLD)
558 		host->can_queue = min_t(u32, FNIC_MAX_IO_REQ,
559 					max_t(u32, FNIC_MIN_IO_REQ,
560 					fnic->config.io_throttle_count));
561 
562 	fnic->fnic_max_tag_id = host->can_queue;
563 	host->max_lun = fnic->config.luns_per_tgt;
564 	host->max_id = FNIC_MAX_FCP_TARGET;
565 	host->max_cmd_len = FCOE_MAX_CMD_LEN;
566 
567 	host->nr_hw_queues = fnic->wq_copy_count;
568 
569 	shost_printk(KERN_INFO, host,
570 			"fnic: can_queue: %d max_lun: %llu",
571 			host->can_queue, host->max_lun);
572 
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);
576 
577 	return 0;
578 }
579 
580 void fnic_mq_map_queues_cpus(struct Scsi_Host *host)
581 {
582 	struct fc_lport *lp = shost_priv(host);
583 	struct fnic *fnic = lport_priv(lp);
584 	struct pci_dev *l_pdev = fnic->pdev;
585 	int intr_mode = fnic->config.intr_mode;
586 	struct blk_mq_queue_map *qmap = &host->tag_set.map[HCTX_TYPE_DEFAULT];
587 
588 	if (intr_mode == VNIC_DEV_INTR_MODE_MSI || intr_mode == VNIC_DEV_INTR_MODE_INTX) {
589 		FNIC_MAIN_DBG(KERN_ERR, fnic->lport->host, fnic->fnic_num,
590 			"intr_mode is not msix\n");
591 		return;
592 	}
593 
594 	FNIC_MAIN_DBG(KERN_INFO, fnic->lport->host, fnic->fnic_num,
595 			"qmap->nr_queues: %d\n", qmap->nr_queues);
596 
597 	if (l_pdev == NULL) {
598 		FNIC_MAIN_DBG(KERN_ERR, fnic->lport->host, fnic->fnic_num,
599 						"l_pdev is null\n");
600 		return;
601 	}
602 
603 	blk_mq_map_hw_queues(qmap, &l_pdev->dev, FNIC_PCI_OFFSET);
604 }
605 
606 static int fnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
607 {
608 	struct Scsi_Host *host;
609 	struct fc_lport *lp;
610 	struct fnic *fnic;
611 	mempool_t *pool;
612 	int err = 0;
613 	int fnic_id = 0;
614 	int i;
615 	unsigned long flags;
616 	int hwq;
617 
618 	/*
619 	 * Allocate SCSI Host and set up association between host,
620 	 * local port, and fnic
621 	 */
622 	lp = libfc_host_alloc(&fnic_host_template, sizeof(struct fnic));
623 	if (!lp) {
624 		printk(KERN_ERR PFX "Unable to alloc libfc local port\n");
625 		err = -ENOMEM;
626 		goto err_out;
627 	}
628 
629 	host = lp->host;
630 	fnic = lport_priv(lp);
631 
632 	fnic_id = ida_alloc(&fnic_ida, GFP_KERNEL);
633 	if (fnic_id < 0) {
634 		pr_err("Unable to alloc fnic ID\n");
635 		err = fnic_id;
636 		goto err_out_ida_alloc;
637 	}
638 	fnic->lport = lp;
639 	fnic->ctlr.lp = lp;
640 	fnic->link_events = 0;
641 	fnic->pdev = pdev;
642 
643 	snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME,
644 		 host->host_no);
645 
646 	host->transportt = fnic_fc_transport;
647 	fnic->fnic_num = fnic_id;
648 	fnic_stats_debugfs_init(fnic);
649 
650 	err = pci_enable_device(pdev);
651 	if (err) {
652 		shost_printk(KERN_ERR, fnic->lport->host,
653 			     "Cannot enable PCI device, aborting.\n");
654 		goto err_out_free_hba;
655 	}
656 
657 	err = pci_request_regions(pdev, DRV_NAME);
658 	if (err) {
659 		shost_printk(KERN_ERR, fnic->lport->host,
660 			     "Cannot enable PCI resources, aborting\n");
661 		goto err_out_disable_device;
662 	}
663 
664 	pci_set_master(pdev);
665 
666 	/* Query PCI controller on system for DMA addressing
667 	 * limitation for the device.  Try 47-bit first, and
668 	 * fail to 32-bit. Cisco VIC supports 47 bits only.
669 	 */
670 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(47));
671 	if (err) {
672 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
673 		if (err) {
674 			shost_printk(KERN_ERR, fnic->lport->host,
675 				     "No usable DMA configuration "
676 				     "aborting\n");
677 			goto err_out_release_regions;
678 		}
679 	}
680 
681 	/* Map vNIC resources from BAR0 */
682 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
683 		shost_printk(KERN_ERR, fnic->lport->host,
684 			     "BAR0 not memory-map'able, aborting.\n");
685 		err = -ENODEV;
686 		goto err_out_release_regions;
687 	}
688 
689 	fnic->bar0.vaddr = pci_iomap(pdev, 0, 0);
690 	fnic->bar0.bus_addr = pci_resource_start(pdev, 0);
691 	fnic->bar0.len = pci_resource_len(pdev, 0);
692 
693 	if (!fnic->bar0.vaddr) {
694 		shost_printk(KERN_ERR, fnic->lport->host,
695 			     "Cannot memory-map BAR0 res hdr, "
696 			     "aborting.\n");
697 		err = -ENODEV;
698 		goto err_out_release_regions;
699 	}
700 
701 	fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0);
702 	if (!fnic->vdev) {
703 		shost_printk(KERN_ERR, fnic->lport->host,
704 			     "vNIC registration failed, "
705 			     "aborting.\n");
706 		err = -ENODEV;
707 		goto err_out_iounmap;
708 	}
709 
710 	err = vnic_dev_cmd_init(fnic->vdev);
711 	if (err) {
712 		shost_printk(KERN_ERR, fnic->lport->host,
713 				"vnic_dev_cmd_init() returns %d, aborting\n",
714 				err);
715 		goto err_out_vnic_unregister;
716 	}
717 
718 	err = fnic_dev_wait(fnic->vdev, vnic_dev_open,
719 			    vnic_dev_open_done, CMD_OPENF_RQ_ENABLE_THEN_POST);
720 	if (err) {
721 		shost_printk(KERN_ERR, fnic->lport->host,
722 			     "vNIC dev open failed, aborting.\n");
723 		goto err_out_dev_cmd_deinit;
724 	}
725 
726 	err = vnic_dev_init(fnic->vdev, 0);
727 	if (err) {
728 		shost_printk(KERN_ERR, fnic->lport->host,
729 			     "vNIC dev init failed, aborting.\n");
730 		goto err_out_dev_close;
731 	}
732 
733 	err = vnic_dev_mac_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
734 	if (err) {
735 		shost_printk(KERN_ERR, fnic->lport->host,
736 			     "vNIC get MAC addr failed \n");
737 		goto err_out_dev_close;
738 	}
739 	/* set data_src for point-to-point mode and to keep it non-zero */
740 	memcpy(fnic->data_src_addr, fnic->ctlr.ctl_src_addr, ETH_ALEN);
741 
742 	/* Get vNIC configuration */
743 	err = fnic_get_vnic_config(fnic);
744 	if (err) {
745 		shost_printk(KERN_ERR, fnic->lport->host,
746 			     "Get vNIC configuration failed, "
747 			     "aborting.\n");
748 		goto err_out_dev_close;
749 	}
750 
751 	/* Setup PCI resources */
752 	pci_set_drvdata(pdev, fnic);
753 
754 	fnic_get_res_counts(fnic);
755 
756 	err = fnic_set_intr_mode(fnic);
757 	if (err) {
758 		shost_printk(KERN_ERR, fnic->lport->host,
759 			     "Failed to set intr mode, "
760 			     "aborting.\n");
761 		goto err_out_dev_close;
762 	}
763 
764 	err = fnic_alloc_vnic_resources(fnic);
765 	if (err) {
766 		shost_printk(KERN_ERR, fnic->lport->host,
767 			     "Failed to alloc vNIC resources, "
768 			     "aborting.\n");
769 		goto err_out_clear_intr;
770 	}
771 
772 	fnic_scsi_drv_init(fnic);
773 
774 	for (hwq = 0; hwq < fnic->wq_copy_count; hwq++) {
775 		fnic->sw_copy_wq[hwq].ioreq_table_size = fnic->fnic_max_tag_id;
776 		fnic->sw_copy_wq[hwq].io_req_table =
777 					kzalloc((fnic->sw_copy_wq[hwq].ioreq_table_size + 1) *
778 					sizeof(struct fnic_io_req *), GFP_KERNEL);
779 	}
780 	shost_printk(KERN_INFO, fnic->lport->host, "fnic copy wqs: %d, Q0 ioreq table size: %d\n",
781 			fnic->wq_copy_count, fnic->sw_copy_wq[0].ioreq_table_size);
782 
783 	/* initialize all fnic locks */
784 	spin_lock_init(&fnic->fnic_lock);
785 
786 	for (i = 0; i < FNIC_WQ_MAX; i++)
787 		spin_lock_init(&fnic->wq_lock[i]);
788 
789 	for (i = 0; i < FNIC_WQ_COPY_MAX; i++) {
790 		spin_lock_init(&fnic->wq_copy_lock[i]);
791 		fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK;
792 		fnic->fw_ack_recd[i] = 0;
793 		fnic->fw_ack_index[i] = -1;
794 	}
795 
796 	err = -ENOMEM;
797 	fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache);
798 	if (!fnic->io_req_pool)
799 		goto err_out_free_resources;
800 
801 	pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
802 	if (!pool)
803 		goto err_out_free_ioreq_pool;
804 	fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool;
805 
806 	pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
807 	if (!pool)
808 		goto err_out_free_dflt_pool;
809 	fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool;
810 
811 	/* setup vlan config, hw inserts vlan header */
812 	fnic->vlan_hw_insert = 1;
813 	fnic->vlan_id = 0;
814 
815 	/* Initialize the FIP fcoe_ctrl struct */
816 	fnic->ctlr.send = fnic_eth_send;
817 	fnic->ctlr.update_mac = fnic_update_mac;
818 	fnic->ctlr.get_src_addr = fnic_get_mac;
819 	if (fnic->config.flags & VFCF_FIP_CAPABLE) {
820 		shost_printk(KERN_INFO, fnic->lport->host,
821 			     "firmware supports FIP\n");
822 		/* enable directed and multicast */
823 		vnic_dev_packet_filter(fnic->vdev, 1, 1, 0, 0, 0);
824 		vnic_dev_add_addr(fnic->vdev, FIP_ALL_ENODE_MACS);
825 		vnic_dev_add_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
826 		fnic->set_vlan = fnic_set_vlan;
827 		fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_AUTO);
828 		timer_setup(&fnic->fip_timer, fnic_fip_notify_timer, 0);
829 		spin_lock_init(&fnic->vlans_lock);
830 		INIT_WORK(&fnic->fip_frame_work, fnic_handle_fip_frame);
831 		INIT_WORK(&fnic->event_work, fnic_handle_event);
832 		skb_queue_head_init(&fnic->fip_frame_queue);
833 		INIT_LIST_HEAD(&fnic->evlist);
834 		INIT_LIST_HEAD(&fnic->vlans);
835 	} else {
836 		shost_printk(KERN_INFO, fnic->lport->host,
837 			     "firmware uses non-FIP mode\n");
838 		fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_NON_FIP);
839 		fnic->ctlr.state = FIP_ST_NON_FIP;
840 	}
841 	fnic->state = FNIC_IN_FC_MODE;
842 
843 	atomic_set(&fnic->in_flight, 0);
844 	fnic->state_flags = FNIC_FLAGS_NONE;
845 
846 	/* Enable hardware stripping of vlan header on ingress */
847 	fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1);
848 
849 	/* Setup notification buffer area */
850 	err = fnic_notify_set(fnic);
851 	if (err) {
852 		shost_printk(KERN_ERR, fnic->lport->host,
853 			     "Failed to alloc notify buffer, aborting.\n");
854 		goto err_out_free_max_pool;
855 	}
856 
857 	/* Setup notify timer when using MSI interrupts */
858 	if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
859 		timer_setup(&fnic->notify_timer, fnic_notify_timer, 0);
860 
861 	/* allocate RQ buffers and post them to RQ*/
862 	for (i = 0; i < fnic->rq_count; i++) {
863 		err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
864 		if (err) {
865 			shost_printk(KERN_ERR, fnic->lport->host,
866 				     "fnic_alloc_rq_frame can't alloc "
867 				     "frame\n");
868 			goto err_out_rq_buf;
869 		}
870 	}
871 
872 	/* Enable all queues */
873 	for (i = 0; i < fnic->raw_wq_count; i++)
874 		vnic_wq_enable(&fnic->wq[i]);
875 	for (i = 0; i < fnic->rq_count; i++) {
876 		if (!ioread32(&fnic->rq[i].ctrl->enable))
877 			vnic_rq_enable(&fnic->rq[i]);
878 	}
879 	for (i = 0; i < fnic->wq_copy_count; i++)
880 		vnic_wq_copy_enable(&fnic->hw_copy_wq[i]);
881 
882 	err = fnic_request_intr(fnic);
883 	if (err) {
884 		shost_printk(KERN_ERR, fnic->lport->host,
885 			     "Unable to request irq.\n");
886 		goto err_out_request_intr;
887 	}
888 
889 	/*
890 	 * Initialization done with PCI system, hardware, firmware.
891 	 * Add host to SCSI
892 	 */
893 	err = scsi_add_host(lp->host, &pdev->dev);
894 	if (err) {
895 		shost_printk(KERN_ERR, fnic->lport->host,
896 			     "fnic: scsi_add_host failed...exiting\n");
897 		goto err_out_scsi_add_host;
898 	}
899 
900 
901 	/* Start local port initiatialization */
902 
903 	lp->link_up = 0;
904 
905 	lp->max_retry_count = fnic->config.flogi_retries;
906 	lp->max_rport_retry_count = fnic->config.plogi_retries;
907 	lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
908 			      FCP_SPPF_CONF_COMPL);
909 	if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR)
910 		lp->service_params |= FCP_SPPF_RETRY;
911 
912 	lp->boot_time = jiffies;
913 	lp->e_d_tov = fnic->config.ed_tov;
914 	lp->r_a_tov = fnic->config.ra_tov;
915 	lp->link_supported_speeds = FC_PORTSPEED_10GBIT;
916 	fc_set_wwnn(lp, fnic->config.node_wwn);
917 	fc_set_wwpn(lp, fnic->config.port_wwn);
918 
919 	fcoe_libfc_config(lp, &fnic->ctlr, &fnic_transport_template, 0);
920 
921 	if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCPIO_HOST_EXCH_RANGE_START,
922 			       FCPIO_HOST_EXCH_RANGE_END, NULL)) {
923 		err = -ENOMEM;
924 		goto err_out_fc_exch_mgr_alloc;
925 	}
926 
927 	fc_lport_init_stats(lp);
928 	fnic->stats_reset_time = jiffies;
929 
930 	fc_lport_config(lp);
931 
932 	if (fc_set_mfs(lp, fnic->config.maxdatafieldsize +
933 		       sizeof(struct fc_frame_header))) {
934 		err = -EINVAL;
935 		goto err_out_free_exch_mgr;
936 	}
937 	fc_host_maxframe_size(lp->host) = lp->mfs;
938 	fc_host_dev_loss_tmo(lp->host) = fnic->config.port_down_timeout / 1000;
939 
940 	sprintf(fc_host_symbolic_name(lp->host),
941 		DRV_NAME " v" DRV_VERSION " over %s", fnic->name);
942 
943 	spin_lock_irqsave(&fnic_list_lock, flags);
944 	list_add_tail(&fnic->list, &fnic_list);
945 	spin_unlock_irqrestore(&fnic_list_lock, flags);
946 
947 	INIT_WORK(&fnic->link_work, fnic_handle_link);
948 	INIT_WORK(&fnic->frame_work, fnic_handle_frame);
949 	INIT_WORK(&fnic->flush_work, fnic_flush_tx);
950 	skb_queue_head_init(&fnic->frame_queue);
951 	skb_queue_head_init(&fnic->tx_queue);
952 
953 	fc_fabric_login(lp);
954 
955 	vnic_dev_enable(fnic->vdev);
956 
957 	for (i = 0; i < fnic->intr_count; i++)
958 		vnic_intr_unmask(&fnic->intr[i]);
959 
960 	fnic_notify_timer_start(fnic);
961 
962 	return 0;
963 
964 err_out_free_exch_mgr:
965 	fc_exch_mgr_free(lp);
966 err_out_fc_exch_mgr_alloc:
967 	fc_remove_host(lp->host);
968 	scsi_remove_host(lp->host);
969 err_out_scsi_add_host:
970 	fnic_free_intr(fnic);
971 err_out_request_intr:
972 	for (i = 0; i < fnic->rq_count; i++)
973 		vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
974 err_out_rq_buf:
975 	vnic_dev_notify_unset(fnic->vdev);
976 err_out_free_max_pool:
977 	mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]);
978 err_out_free_dflt_pool:
979 	mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]);
980 err_out_free_ioreq_pool:
981 	mempool_destroy(fnic->io_req_pool);
982 err_out_free_resources:
983 	for (hwq = 0; hwq < fnic->wq_copy_count; hwq++)
984 		kfree(fnic->sw_copy_wq[hwq].io_req_table);
985 	fnic_free_vnic_resources(fnic);
986 err_out_clear_intr:
987 	fnic_clear_intr_mode(fnic);
988 err_out_dev_close:
989 	vnic_dev_close(fnic->vdev);
990 err_out_dev_cmd_deinit:
991 err_out_vnic_unregister:
992 	vnic_dev_unregister(fnic->vdev);
993 err_out_iounmap:
994 	fnic_iounmap(fnic);
995 err_out_release_regions:
996 	pci_release_regions(pdev);
997 err_out_disable_device:
998 	pci_disable_device(pdev);
999 err_out_free_hba:
1000 	fnic_stats_debugfs_remove(fnic);
1001 	ida_free(&fnic_ida, fnic->fnic_num);
1002 err_out_ida_alloc:
1003 	scsi_host_put(lp->host);
1004 err_out:
1005 	return err;
1006 }
1007 
1008 static void fnic_remove(struct pci_dev *pdev)
1009 {
1010 	struct fnic *fnic = pci_get_drvdata(pdev);
1011 	struct fc_lport *lp = fnic->lport;
1012 	unsigned long flags;
1013 	int hwq;
1014 
1015 	/*
1016 	 * Mark state so that the workqueue thread stops forwarding
1017 	 * received frames and link events to the local port. ISR and
1018 	 * other threads that can queue work items will also stop
1019 	 * creating work items on the fnic workqueue
1020 	 */
1021 	spin_lock_irqsave(&fnic->fnic_lock, flags);
1022 	fnic->stop_rx_link_events = 1;
1023 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1024 
1025 	if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
1026 		del_timer_sync(&fnic->notify_timer);
1027 
1028 	/*
1029 	 * Flush the fnic event queue. After this call, there should
1030 	 * be no event queued for this fnic device in the workqueue
1031 	 */
1032 	flush_workqueue(fnic_event_queue);
1033 	skb_queue_purge(&fnic->frame_queue);
1034 	skb_queue_purge(&fnic->tx_queue);
1035 
1036 	if (fnic->config.flags & VFCF_FIP_CAPABLE) {
1037 		del_timer_sync(&fnic->fip_timer);
1038 		skb_queue_purge(&fnic->fip_frame_queue);
1039 		fnic_fcoe_reset_vlans(fnic);
1040 		fnic_fcoe_evlist_free(fnic);
1041 	}
1042 
1043 	/*
1044 	 * Log off the fabric. This stops all remote ports, dns port,
1045 	 * logs off the fabric. This flushes all rport, disc, lport work
1046 	 * before returning
1047 	 */
1048 	fc_fabric_logoff(fnic->lport);
1049 
1050 	spin_lock_irqsave(&fnic->fnic_lock, flags);
1051 	fnic->in_remove = 1;
1052 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1053 
1054 	fcoe_ctlr_destroy(&fnic->ctlr);
1055 	fc_lport_destroy(lp);
1056 	fnic_stats_debugfs_remove(fnic);
1057 
1058 	/*
1059 	 * This stops the fnic device, masks all interrupts. Completed
1060 	 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
1061 	 * cleaned up
1062 	 */
1063 	fnic_cleanup(fnic);
1064 
1065 	BUG_ON(!skb_queue_empty(&fnic->frame_queue));
1066 	BUG_ON(!skb_queue_empty(&fnic->tx_queue));
1067 
1068 	spin_lock_irqsave(&fnic_list_lock, flags);
1069 	list_del(&fnic->list);
1070 	spin_unlock_irqrestore(&fnic_list_lock, flags);
1071 
1072 	fc_remove_host(fnic->lport->host);
1073 	scsi_remove_host(fnic->lport->host);
1074 	for (hwq = 0; hwq < fnic->wq_copy_count; hwq++)
1075 		kfree(fnic->sw_copy_wq[hwq].io_req_table);
1076 	fc_exch_mgr_free(fnic->lport);
1077 	vnic_dev_notify_unset(fnic->vdev);
1078 	fnic_free_intr(fnic);
1079 	fnic_free_vnic_resources(fnic);
1080 	fnic_clear_intr_mode(fnic);
1081 	vnic_dev_close(fnic->vdev);
1082 	vnic_dev_unregister(fnic->vdev);
1083 	fnic_iounmap(fnic);
1084 	pci_release_regions(pdev);
1085 	pci_disable_device(pdev);
1086 	ida_free(&fnic_ida, fnic->fnic_num);
1087 	scsi_host_put(lp->host);
1088 }
1089 
1090 static struct pci_driver fnic_driver = {
1091 	.name = DRV_NAME,
1092 	.id_table = fnic_id_table,
1093 	.probe = fnic_probe,
1094 	.remove = fnic_remove,
1095 };
1096 
1097 static int __init fnic_init_module(void)
1098 {
1099 	size_t len;
1100 	int err = 0;
1101 
1102 	printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
1103 
1104 	/* Create debugfs entries for fnic */
1105 	err = fnic_debugfs_init();
1106 	if (err < 0) {
1107 		printk(KERN_ERR PFX "Failed to create fnic directory "
1108 				"for tracing and stats logging\n");
1109 		fnic_debugfs_terminate();
1110 	}
1111 
1112 	/* Allocate memory for trace buffer */
1113 	err = fnic_trace_buf_init();
1114 	if (err < 0) {
1115 		printk(KERN_ERR PFX
1116 		       "Trace buffer initialization Failed. "
1117 		       "Fnic Tracing utility is disabled\n");
1118 		fnic_trace_free();
1119 	}
1120 
1121     /* Allocate memory for fc trace buffer */
1122 	err = fnic_fc_trace_init();
1123 	if (err < 0) {
1124 		printk(KERN_ERR PFX "FC trace buffer initialization Failed "
1125 		       "FC frame tracing utility is disabled\n");
1126 		fnic_fc_trace_free();
1127 	}
1128 
1129 	/* Create a cache for allocation of default size sgls */
1130 	len = sizeof(struct fnic_dflt_sgl_list);
1131 	fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create
1132 		("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
1133 		 SLAB_HWCACHE_ALIGN,
1134 		 NULL);
1135 	if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) {
1136 		printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n");
1137 		err = -ENOMEM;
1138 		goto err_create_fnic_sgl_slab_dflt;
1139 	}
1140 
1141 	/* Create a cache for allocation of max size sgls*/
1142 	len = sizeof(struct fnic_sgl_list);
1143 	fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create
1144 		("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
1145 		  SLAB_HWCACHE_ALIGN,
1146 		  NULL);
1147 	if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) {
1148 		printk(KERN_ERR PFX "failed to create fnic max sgl slab\n");
1149 		err = -ENOMEM;
1150 		goto err_create_fnic_sgl_slab_max;
1151 	}
1152 
1153 	/* Create a cache of io_req structs for use via mempool */
1154 	fnic_io_req_cache = kmem_cache_create("fnic_io_req",
1155 					      sizeof(struct fnic_io_req),
1156 					      0, SLAB_HWCACHE_ALIGN, NULL);
1157 	if (!fnic_io_req_cache) {
1158 		printk(KERN_ERR PFX "failed to create fnic io_req slab\n");
1159 		err = -ENOMEM;
1160 		goto err_create_fnic_ioreq_slab;
1161 	}
1162 
1163 	fnic_event_queue =
1164 		alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, "fnic_event_wq");
1165 	if (!fnic_event_queue) {
1166 		printk(KERN_ERR PFX "fnic work queue create failed\n");
1167 		err = -ENOMEM;
1168 		goto err_create_fnic_workq;
1169 	}
1170 
1171 	fnic_fip_queue =
1172 		alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, "fnic_fip_q");
1173 	if (!fnic_fip_queue) {
1174 		printk(KERN_ERR PFX "fnic FIP work queue create failed\n");
1175 		err = -ENOMEM;
1176 		goto err_create_fip_workq;
1177 	}
1178 
1179 	fnic_fc_transport = fc_attach_transport(&fnic_fc_functions);
1180 	if (!fnic_fc_transport) {
1181 		printk(KERN_ERR PFX "fc_attach_transport error\n");
1182 		err = -ENOMEM;
1183 		goto err_fc_transport;
1184 	}
1185 
1186 	/* register the driver with PCI system */
1187 	err = pci_register_driver(&fnic_driver);
1188 	if (err < 0) {
1189 		printk(KERN_ERR PFX "pci register error\n");
1190 		goto err_pci_register;
1191 	}
1192 	return err;
1193 
1194 err_pci_register:
1195 	fc_release_transport(fnic_fc_transport);
1196 err_fc_transport:
1197 	destroy_workqueue(fnic_fip_queue);
1198 err_create_fip_workq:
1199 	destroy_workqueue(fnic_event_queue);
1200 err_create_fnic_workq:
1201 	kmem_cache_destroy(fnic_io_req_cache);
1202 err_create_fnic_ioreq_slab:
1203 	kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
1204 err_create_fnic_sgl_slab_max:
1205 	kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
1206 err_create_fnic_sgl_slab_dflt:
1207 	fnic_trace_free();
1208 	fnic_fc_trace_free();
1209 	fnic_debugfs_terminate();
1210 	return err;
1211 }
1212 
1213 static void __exit fnic_cleanup_module(void)
1214 {
1215 	pci_unregister_driver(&fnic_driver);
1216 	destroy_workqueue(fnic_event_queue);
1217 	if (fnic_fip_queue)
1218 		destroy_workqueue(fnic_fip_queue);
1219 	kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
1220 	kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
1221 	kmem_cache_destroy(fnic_io_req_cache);
1222 	fc_release_transport(fnic_fc_transport);
1223 	fnic_trace_free();
1224 	fnic_fc_trace_free();
1225 	fnic_debugfs_terminate();
1226 	ida_destroy(&fnic_ida);
1227 }
1228 
1229 module_init(fnic_init_module);
1230 module_exit(fnic_cleanup_module);
1231