xref: /openbmc/linux/drivers/scsi/fnic/fnic_main.c (revision aaa5e569)
1 /*
2  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
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.
8  *
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.
17  */
18 #include <linux/module.h>
19 #include <linux/mempool.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/pci.h>
24 #include <linux/skbuff.h>
25 #include <linux/interrupt.h>
26 #include <linux/spinlock.h>
27 #include <linux/workqueue.h>
28 #include <linux/if_ether.h>
29 #include <scsi/fc/fc_fip.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_transport.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/libfc.h>
35 #include <scsi/fc_frame.h>
36 
37 #include "vnic_dev.h"
38 #include "vnic_intr.h"
39 #include "vnic_stats.h"
40 #include "fnic_io.h"
41 #include "fnic.h"
42 
43 #define PCI_DEVICE_ID_CISCO_FNIC	0x0045
44 
45 /* Timer to poll notification area for events. Used for MSI interrupts */
46 #define FNIC_NOTIFY_TIMER_PERIOD	(2 * HZ)
47 
48 static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES];
49 static struct kmem_cache *fnic_io_req_cache;
50 LIST_HEAD(fnic_list);
51 DEFINE_SPINLOCK(fnic_list_lock);
52 
53 /* Supported devices by fnic module */
54 static struct pci_device_id fnic_id_table[] = {
55 	{ PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_FNIC) },
56 	{ 0, }
57 };
58 
59 MODULE_DESCRIPTION(DRV_DESCRIPTION);
60 MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, "
61 	      "Joseph R. Eykholt <jeykholt@cisco.com>");
62 MODULE_LICENSE("GPL v2");
63 MODULE_VERSION(DRV_VERSION);
64 MODULE_DEVICE_TABLE(pci, fnic_id_table);
65 
66 unsigned int fnic_log_level;
67 module_param(fnic_log_level, int, S_IRUGO|S_IWUSR);
68 MODULE_PARM_DESC(fnic_log_level, "bit mask of fnic logging levels");
69 
70 
71 static struct libfc_function_template fnic_transport_template = {
72 	.frame_send = fnic_send,
73 	.lport_set_port_id = fnic_set_port_id,
74 	.fcp_abort_io = fnic_empty_scsi_cleanup,
75 	.fcp_cleanup = fnic_empty_scsi_cleanup,
76 	.exch_mgr_reset = fnic_exch_mgr_reset
77 };
78 
79 static int fnic_slave_alloc(struct scsi_device *sdev)
80 {
81 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
82 	struct fc_lport *lp = shost_priv(sdev->host);
83 	struct fnic *fnic = lport_priv(lp);
84 
85 	sdev->tagged_supported = 1;
86 
87 	if (!rport || fc_remote_port_chkready(rport))
88 		return -ENXIO;
89 
90 	scsi_activate_tcq(sdev, FNIC_DFLT_QUEUE_DEPTH);
91 	rport->dev_loss_tmo = fnic->config.port_down_timeout / 1000;
92 
93 	return 0;
94 }
95 
96 static struct scsi_host_template fnic_host_template = {
97 	.module = THIS_MODULE,
98 	.name = DRV_NAME,
99 	.queuecommand = fnic_queuecommand,
100 	.eh_abort_handler = fnic_abort_cmd,
101 	.eh_device_reset_handler = fnic_device_reset,
102 	.eh_host_reset_handler = fnic_host_reset,
103 	.slave_alloc = fnic_slave_alloc,
104 	.change_queue_depth = fc_change_queue_depth,
105 	.change_queue_type = fc_change_queue_type,
106 	.this_id = -1,
107 	.cmd_per_lun = 3,
108 	.can_queue = FNIC_MAX_IO_REQ,
109 	.use_clustering = ENABLE_CLUSTERING,
110 	.sg_tablesize = FNIC_MAX_SG_DESC_CNT,
111 	.max_sectors = 0xffff,
112 	.shost_attrs = fnic_attrs,
113 };
114 
115 static void fnic_get_host_speed(struct Scsi_Host *shost);
116 static struct scsi_transport_template *fnic_fc_transport;
117 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *);
118 
119 static struct fc_function_template fnic_fc_functions = {
120 
121 	.show_host_node_name = 1,
122 	.show_host_port_name = 1,
123 	.show_host_supported_classes = 1,
124 	.show_host_supported_fc4s = 1,
125 	.show_host_active_fc4s = 1,
126 	.show_host_maxframe_size = 1,
127 	.show_host_port_id = 1,
128 	.show_host_supported_speeds = 1,
129 	.get_host_speed = fnic_get_host_speed,
130 	.show_host_speed = 1,
131 	.show_host_port_type = 1,
132 	.get_host_port_state = fc_get_host_port_state,
133 	.show_host_port_state = 1,
134 	.show_host_symbolic_name = 1,
135 	.show_rport_maxframe_size = 1,
136 	.show_rport_supported_classes = 1,
137 	.show_host_fabric_name = 1,
138 	.show_starget_node_name = 1,
139 	.show_starget_port_name = 1,
140 	.show_starget_port_id = 1,
141 	.show_rport_dev_loss_tmo = 1,
142 	.issue_fc_host_lip = fnic_reset,
143 	.get_fc_host_stats = fnic_get_stats,
144 	.dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
145 	.terminate_rport_io = fnic_terminate_rport_io,
146 	.bsg_request = fc_lport_bsg_request,
147 };
148 
149 static void fnic_get_host_speed(struct Scsi_Host *shost)
150 {
151 	struct fc_lport *lp = shost_priv(shost);
152 	struct fnic *fnic = lport_priv(lp);
153 	u32 port_speed = vnic_dev_port_speed(fnic->vdev);
154 
155 	/* Add in other values as they get defined in fw */
156 	switch (port_speed) {
157 	case 10000:
158 		fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
159 		break;
160 	default:
161 		fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
162 		break;
163 	}
164 }
165 
166 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host)
167 {
168 	int ret;
169 	struct fc_lport *lp = shost_priv(host);
170 	struct fnic *fnic = lport_priv(lp);
171 	struct fc_host_statistics *stats = &lp->host_stats;
172 	struct vnic_stats *vs;
173 	unsigned long flags;
174 
175 	if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT))
176 		return stats;
177 	fnic->stats_time = jiffies;
178 
179 	spin_lock_irqsave(&fnic->fnic_lock, flags);
180 	ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
181 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
182 
183 	if (ret) {
184 		FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host,
185 			      "fnic: Get vnic stats failed"
186 			      " 0x%x", ret);
187 		return stats;
188 	}
189 	vs = fnic->stats;
190 	stats->tx_frames = vs->tx.tx_unicast_frames_ok;
191 	stats->tx_words  = vs->tx.tx_unicast_bytes_ok / 4;
192 	stats->rx_frames = vs->rx.rx_unicast_frames_ok;
193 	stats->rx_words  = vs->rx.rx_unicast_bytes_ok / 4;
194 	stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors;
195 	stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop;
196 	stats->invalid_crc_count = vs->rx.rx_crc_errors;
197 	stats->seconds_since_last_reset = (jiffies - lp->boot_time) / HZ;
198 	stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000);
199 	stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000);
200 
201 	return stats;
202 }
203 
204 void fnic_log_q_error(struct fnic *fnic)
205 {
206 	unsigned int i;
207 	u32 error_status;
208 
209 	for (i = 0; i < fnic->raw_wq_count; i++) {
210 		error_status = ioread32(&fnic->wq[i].ctrl->error_status);
211 		if (error_status)
212 			shost_printk(KERN_ERR, fnic->lport->host,
213 				     "WQ[%d] error_status"
214 				     " %d\n", i, error_status);
215 	}
216 
217 	for (i = 0; i < fnic->rq_count; i++) {
218 		error_status = ioread32(&fnic->rq[i].ctrl->error_status);
219 		if (error_status)
220 			shost_printk(KERN_ERR, fnic->lport->host,
221 				     "RQ[%d] error_status"
222 				     " %d\n", i, error_status);
223 	}
224 
225 	for (i = 0; i < fnic->wq_copy_count; i++) {
226 		error_status = ioread32(&fnic->wq_copy[i].ctrl->error_status);
227 		if (error_status)
228 			shost_printk(KERN_ERR, fnic->lport->host,
229 				     "CWQ[%d] error_status"
230 				     " %d\n", i, error_status);
231 	}
232 }
233 
234 void fnic_handle_link_event(struct fnic *fnic)
235 {
236 	unsigned long flags;
237 
238 	spin_lock_irqsave(&fnic->fnic_lock, flags);
239 	if (fnic->stop_rx_link_events) {
240 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
241 		return;
242 	}
243 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
244 
245 	queue_work(fnic_event_queue, &fnic->link_work);
246 
247 }
248 
249 static int fnic_notify_set(struct fnic *fnic)
250 {
251 	int err;
252 
253 	switch (vnic_dev_get_intr_mode(fnic->vdev)) {
254 	case VNIC_DEV_INTR_MODE_INTX:
255 		err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY);
256 		break;
257 	case VNIC_DEV_INTR_MODE_MSI:
258 		err = vnic_dev_notify_set(fnic->vdev, -1);
259 		break;
260 	case VNIC_DEV_INTR_MODE_MSIX:
261 		err = vnic_dev_notify_set(fnic->vdev, FNIC_MSIX_ERR_NOTIFY);
262 		break;
263 	default:
264 		shost_printk(KERN_ERR, fnic->lport->host,
265 			     "Interrupt mode should be set up"
266 			     " before devcmd notify set %d\n",
267 			     vnic_dev_get_intr_mode(fnic->vdev));
268 		err = -1;
269 		break;
270 	}
271 
272 	return err;
273 }
274 
275 static void fnic_notify_timer(unsigned long data)
276 {
277 	struct fnic *fnic = (struct fnic *)data;
278 
279 	fnic_handle_link_event(fnic);
280 	mod_timer(&fnic->notify_timer,
281 		  round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD));
282 }
283 
284 static void fnic_notify_timer_start(struct fnic *fnic)
285 {
286 	switch (vnic_dev_get_intr_mode(fnic->vdev)) {
287 	case VNIC_DEV_INTR_MODE_MSI:
288 		/*
289 		 * Schedule first timeout immediately. The driver is
290 		 * initiatialized and ready to look for link up notification
291 		 */
292 		mod_timer(&fnic->notify_timer, jiffies);
293 		break;
294 	default:
295 		/* Using intr for notification for INTx/MSI-X */
296 		break;
297 	};
298 }
299 
300 static int fnic_dev_wait(struct vnic_dev *vdev,
301 			 int (*start)(struct vnic_dev *, int),
302 			 int (*finished)(struct vnic_dev *, int *),
303 			 int arg)
304 {
305 	unsigned long time;
306 	int done;
307 	int err;
308 
309 	err = start(vdev, arg);
310 	if (err)
311 		return err;
312 
313 	/* Wait for func to complete...2 seconds max */
314 	time = jiffies + (HZ * 2);
315 	do {
316 		err = finished(vdev, &done);
317 		if (err)
318 			return err;
319 		if (done)
320 			return 0;
321 		schedule_timeout_uninterruptible(HZ / 10);
322 	} while (time_after(time, jiffies));
323 
324 	return -ETIMEDOUT;
325 }
326 
327 static int fnic_cleanup(struct fnic *fnic)
328 {
329 	unsigned int i;
330 	int err;
331 
332 	vnic_dev_disable(fnic->vdev);
333 	for (i = 0; i < fnic->intr_count; i++)
334 		vnic_intr_mask(&fnic->intr[i]);
335 
336 	for (i = 0; i < fnic->rq_count; i++) {
337 		err = vnic_rq_disable(&fnic->rq[i]);
338 		if (err)
339 			return err;
340 	}
341 	for (i = 0; i < fnic->raw_wq_count; i++) {
342 		err = vnic_wq_disable(&fnic->wq[i]);
343 		if (err)
344 			return err;
345 	}
346 	for (i = 0; i < fnic->wq_copy_count; i++) {
347 		err = vnic_wq_copy_disable(&fnic->wq_copy[i]);
348 		if (err)
349 			return err;
350 	}
351 
352 	/* Clean up completed IOs and FCS frames */
353 	fnic_wq_copy_cmpl_handler(fnic, -1);
354 	fnic_wq_cmpl_handler(fnic, -1);
355 	fnic_rq_cmpl_handler(fnic, -1);
356 
357 	/* Clean up the IOs and FCS frames that have not completed */
358 	for (i = 0; i < fnic->raw_wq_count; i++)
359 		vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf);
360 	for (i = 0; i < fnic->rq_count; i++)
361 		vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
362 	for (i = 0; i < fnic->wq_copy_count; i++)
363 		vnic_wq_copy_clean(&fnic->wq_copy[i],
364 				   fnic_wq_copy_cleanup_handler);
365 
366 	for (i = 0; i < fnic->cq_count; i++)
367 		vnic_cq_clean(&fnic->cq[i]);
368 	for (i = 0; i < fnic->intr_count; i++)
369 		vnic_intr_clean(&fnic->intr[i]);
370 
371 	mempool_destroy(fnic->io_req_pool);
372 	for (i = 0; i < FNIC_SGL_NUM_CACHES; i++)
373 		mempool_destroy(fnic->io_sgl_pool[i]);
374 
375 	return 0;
376 }
377 
378 static void fnic_iounmap(struct fnic *fnic)
379 {
380 	if (fnic->bar0.vaddr)
381 		iounmap(fnic->bar0.vaddr);
382 }
383 
384 /*
385  * Allocate element for mempools requiring GFP_DMA flag.
386  * Otherwise, checks in kmem_flagcheck() hit BUG_ON().
387  */
388 static void *fnic_alloc_slab_dma(gfp_t gfp_mask, void *pool_data)
389 {
390 	struct kmem_cache *mem = pool_data;
391 
392 	return kmem_cache_alloc(mem, gfp_mask | GFP_ATOMIC | GFP_DMA);
393 }
394 
395 /**
396  * fnic_get_mac() - get assigned data MAC address for FIP code.
397  * @lport: 	local port.
398  */
399 static u8 *fnic_get_mac(struct fc_lport *lport)
400 {
401 	struct fnic *fnic = lport_priv(lport);
402 
403 	return fnic->data_src_addr;
404 }
405 
406 static int __devinit fnic_probe(struct pci_dev *pdev,
407 				const struct pci_device_id *ent)
408 {
409 	struct Scsi_Host *host;
410 	struct fc_lport *lp;
411 	struct fnic *fnic;
412 	mempool_t *pool;
413 	int err;
414 	int i;
415 	unsigned long flags;
416 
417 	/*
418 	 * Allocate SCSI Host and set up association between host,
419 	 * local port, and fnic
420 	 */
421 	lp = libfc_host_alloc(&fnic_host_template, sizeof(struct fnic));
422 	if (!lp) {
423 		printk(KERN_ERR PFX "Unable to alloc libfc local port\n");
424 		err = -ENOMEM;
425 		goto err_out;
426 	}
427 	host = lp->host;
428 	fnic = lport_priv(lp);
429 	fnic->lport = lp;
430 	fnic->ctlr.lp = lp;
431 
432 	snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME,
433 		 host->host_no);
434 
435 	host->transportt = fnic_fc_transport;
436 
437 	err = scsi_init_shared_tag_map(host, FNIC_MAX_IO_REQ);
438 	if (err) {
439 		shost_printk(KERN_ERR, fnic->lport->host,
440 			     "Unable to alloc shared tag map\n");
441 		goto err_out_free_hba;
442 	}
443 
444 	/* Setup PCI resources */
445 	pci_set_drvdata(pdev, fnic);
446 
447 	fnic->pdev = pdev;
448 
449 	err = pci_enable_device(pdev);
450 	if (err) {
451 		shost_printk(KERN_ERR, fnic->lport->host,
452 			     "Cannot enable PCI device, aborting.\n");
453 		goto err_out_free_hba;
454 	}
455 
456 	err = pci_request_regions(pdev, DRV_NAME);
457 	if (err) {
458 		shost_printk(KERN_ERR, fnic->lport->host,
459 			     "Cannot enable PCI resources, aborting\n");
460 		goto err_out_disable_device;
461 	}
462 
463 	pci_set_master(pdev);
464 
465 	/* Query PCI controller on system for DMA addressing
466 	 * limitation for the device.  Try 40-bit first, and
467 	 * fail to 32-bit.
468 	 */
469 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
470 	if (err) {
471 		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
472 		if (err) {
473 			shost_printk(KERN_ERR, fnic->lport->host,
474 				     "No usable DMA configuration "
475 				     "aborting\n");
476 			goto err_out_release_regions;
477 		}
478 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
479 		if (err) {
480 			shost_printk(KERN_ERR, fnic->lport->host,
481 				     "Unable to obtain 32-bit DMA "
482 				     "for consistent allocations, aborting.\n");
483 			goto err_out_release_regions;
484 		}
485 	} else {
486 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
487 		if (err) {
488 			shost_printk(KERN_ERR, fnic->lport->host,
489 				     "Unable to obtain 40-bit DMA "
490 				     "for consistent allocations, aborting.\n");
491 			goto err_out_release_regions;
492 		}
493 	}
494 
495 	/* Map vNIC resources from BAR0 */
496 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
497 		shost_printk(KERN_ERR, fnic->lport->host,
498 			     "BAR0 not memory-map'able, aborting.\n");
499 		err = -ENODEV;
500 		goto err_out_release_regions;
501 	}
502 
503 	fnic->bar0.vaddr = pci_iomap(pdev, 0, 0);
504 	fnic->bar0.bus_addr = pci_resource_start(pdev, 0);
505 	fnic->bar0.len = pci_resource_len(pdev, 0);
506 
507 	if (!fnic->bar0.vaddr) {
508 		shost_printk(KERN_ERR, fnic->lport->host,
509 			     "Cannot memory-map BAR0 res hdr, "
510 			     "aborting.\n");
511 		err = -ENODEV;
512 		goto err_out_release_regions;
513 	}
514 
515 	fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0);
516 	if (!fnic->vdev) {
517 		shost_printk(KERN_ERR, fnic->lport->host,
518 			     "vNIC registration failed, "
519 			     "aborting.\n");
520 		err = -ENODEV;
521 		goto err_out_iounmap;
522 	}
523 
524 	err = fnic_dev_wait(fnic->vdev, vnic_dev_open,
525 			    vnic_dev_open_done, 0);
526 	if (err) {
527 		shost_printk(KERN_ERR, fnic->lport->host,
528 			     "vNIC dev open failed, aborting.\n");
529 		goto err_out_vnic_unregister;
530 	}
531 
532 	err = vnic_dev_init(fnic->vdev, 0);
533 	if (err) {
534 		shost_printk(KERN_ERR, fnic->lport->host,
535 			     "vNIC dev init failed, aborting.\n");
536 		goto err_out_dev_close;
537 	}
538 
539 	err = vnic_dev_mac_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
540 	if (err) {
541 		shost_printk(KERN_ERR, fnic->lport->host,
542 			     "vNIC get MAC addr failed \n");
543 		goto err_out_dev_close;
544 	}
545 	/* set data_src for point-to-point mode and to keep it non-zero */
546 	memcpy(fnic->data_src_addr, fnic->ctlr.ctl_src_addr, ETH_ALEN);
547 
548 	/* Get vNIC configuration */
549 	err = fnic_get_vnic_config(fnic);
550 	if (err) {
551 		shost_printk(KERN_ERR, fnic->lport->host,
552 			     "Get vNIC configuration failed, "
553 			     "aborting.\n");
554 		goto err_out_dev_close;
555 	}
556 	host->max_lun = fnic->config.luns_per_tgt;
557 	host->max_id = FNIC_MAX_FCP_TARGET;
558 	host->max_cmd_len = FNIC_MAX_CMD_LEN;
559 
560 	fnic_get_res_counts(fnic);
561 
562 	err = fnic_set_intr_mode(fnic);
563 	if (err) {
564 		shost_printk(KERN_ERR, fnic->lport->host,
565 			     "Failed to set intr mode, "
566 			     "aborting.\n");
567 		goto err_out_dev_close;
568 	}
569 
570 	err = fnic_alloc_vnic_resources(fnic);
571 	if (err) {
572 		shost_printk(KERN_ERR, fnic->lport->host,
573 			     "Failed to alloc vNIC resources, "
574 			     "aborting.\n");
575 		goto err_out_clear_intr;
576 	}
577 
578 
579 	/* initialize all fnic locks */
580 	spin_lock_init(&fnic->fnic_lock);
581 
582 	for (i = 0; i < FNIC_WQ_MAX; i++)
583 		spin_lock_init(&fnic->wq_lock[i]);
584 
585 	for (i = 0; i < FNIC_WQ_COPY_MAX; i++) {
586 		spin_lock_init(&fnic->wq_copy_lock[i]);
587 		fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK;
588 		fnic->fw_ack_recd[i] = 0;
589 		fnic->fw_ack_index[i] = -1;
590 	}
591 
592 	for (i = 0; i < FNIC_IO_LOCKS; i++)
593 		spin_lock_init(&fnic->io_req_lock[i]);
594 
595 	fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache);
596 	if (!fnic->io_req_pool)
597 		goto err_out_free_resources;
598 
599 	pool = mempool_create(2, fnic_alloc_slab_dma, mempool_free_slab,
600 			      fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
601 	if (!pool)
602 		goto err_out_free_ioreq_pool;
603 	fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool;
604 
605 	pool = mempool_create(2, fnic_alloc_slab_dma, mempool_free_slab,
606 			      fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
607 	if (!pool)
608 		goto err_out_free_dflt_pool;
609 	fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool;
610 
611 	/* setup vlan config, hw inserts vlan header */
612 	fnic->vlan_hw_insert = 1;
613 	fnic->vlan_id = 0;
614 
615 	/* Initialize the FIP fcoe_ctrl struct */
616 	fnic->ctlr.send = fnic_eth_send;
617 	fnic->ctlr.update_mac = fnic_update_mac;
618 	fnic->ctlr.get_src_addr = fnic_get_mac;
619 	fcoe_ctlr_init(&fnic->ctlr);
620 	if (fnic->config.flags & VFCF_FIP_CAPABLE) {
621 		shost_printk(KERN_INFO, fnic->lport->host,
622 			     "firmware supports FIP\n");
623 		/* enable directed and multicast */
624 		vnic_dev_packet_filter(fnic->vdev, 1, 1, 0, 0, 0);
625 		vnic_dev_add_addr(fnic->vdev, FIP_ALL_ENODE_MACS);
626 		vnic_dev_add_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
627 	} else {
628 		shost_printk(KERN_INFO, fnic->lport->host,
629 			     "firmware uses non-FIP mode\n");
630 		fnic->ctlr.mode = FIP_ST_NON_FIP;
631 	}
632 	fnic->state = FNIC_IN_FC_MODE;
633 
634 	/* Enable hardware stripping of vlan header on ingress */
635 	fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1);
636 
637 	/* Setup notification buffer area */
638 	err = fnic_notify_set(fnic);
639 	if (err) {
640 		shost_printk(KERN_ERR, fnic->lport->host,
641 			     "Failed to alloc notify buffer, aborting.\n");
642 		goto err_out_free_max_pool;
643 	}
644 
645 	/* Setup notify timer when using MSI interrupts */
646 	if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
647 		setup_timer(&fnic->notify_timer,
648 			    fnic_notify_timer, (unsigned long)fnic);
649 
650 	/* allocate RQ buffers and post them to RQ*/
651 	for (i = 0; i < fnic->rq_count; i++) {
652 		err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
653 		if (err) {
654 			shost_printk(KERN_ERR, fnic->lport->host,
655 				     "fnic_alloc_rq_frame can't alloc "
656 				     "frame\n");
657 			goto err_out_free_rq_buf;
658 		}
659 	}
660 
661 	/*
662 	 * Initialization done with PCI system, hardware, firmware.
663 	 * Add host to SCSI
664 	 */
665 	err = scsi_add_host(lp->host, &pdev->dev);
666 	if (err) {
667 		shost_printk(KERN_ERR, fnic->lport->host,
668 			     "fnic: scsi_add_host failed...exiting\n");
669 		goto err_out_free_rq_buf;
670 	}
671 
672 	/* Start local port initiatialization */
673 
674 	lp->link_up = 0;
675 	lp->tt = fnic_transport_template;
676 
677 	lp->max_retry_count = fnic->config.flogi_retries;
678 	lp->max_rport_retry_count = fnic->config.plogi_retries;
679 	lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
680 			      FCP_SPPF_CONF_COMPL);
681 	if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR)
682 		lp->service_params |= FCP_SPPF_RETRY;
683 
684 	lp->boot_time = jiffies;
685 	lp->e_d_tov = fnic->config.ed_tov;
686 	lp->r_a_tov = fnic->config.ra_tov;
687 	lp->link_supported_speeds = FC_PORTSPEED_10GBIT;
688 	fc_set_wwnn(lp, fnic->config.node_wwn);
689 	fc_set_wwpn(lp, fnic->config.port_wwn);
690 
691 	fc_lport_init(lp);
692 	fc_exch_init(lp);
693 	fc_elsct_init(lp);
694 	fc_rport_init(lp);
695 	fc_disc_init(lp);
696 
697 	if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCPIO_HOST_EXCH_RANGE_START,
698 			       FCPIO_HOST_EXCH_RANGE_END, NULL)) {
699 		err = -ENOMEM;
700 		goto err_out_remove_scsi_host;
701 	}
702 
703 	fc_lport_config(lp);
704 
705 	if (fc_set_mfs(lp, fnic->config.maxdatafieldsize +
706 		       sizeof(struct fc_frame_header))) {
707 		err = -EINVAL;
708 		goto err_out_free_exch_mgr;
709 	}
710 	fc_host_maxframe_size(lp->host) = lp->mfs;
711 
712 	sprintf(fc_host_symbolic_name(lp->host),
713 		DRV_NAME " v" DRV_VERSION " over %s", fnic->name);
714 
715 	spin_lock_irqsave(&fnic_list_lock, flags);
716 	list_add_tail(&fnic->list, &fnic_list);
717 	spin_unlock_irqrestore(&fnic_list_lock, flags);
718 
719 	INIT_WORK(&fnic->link_work, fnic_handle_link);
720 	INIT_WORK(&fnic->frame_work, fnic_handle_frame);
721 	skb_queue_head_init(&fnic->frame_queue);
722 	skb_queue_head_init(&fnic->tx_queue);
723 
724 	/* Enable all queues */
725 	for (i = 0; i < fnic->raw_wq_count; i++)
726 		vnic_wq_enable(&fnic->wq[i]);
727 	for (i = 0; i < fnic->rq_count; i++)
728 		vnic_rq_enable(&fnic->rq[i]);
729 	for (i = 0; i < fnic->wq_copy_count; i++)
730 		vnic_wq_copy_enable(&fnic->wq_copy[i]);
731 
732 	fc_fabric_login(lp);
733 
734 	vnic_dev_enable(fnic->vdev);
735 
736 	err = fnic_request_intr(fnic);
737 	if (err) {
738 		shost_printk(KERN_ERR, fnic->lport->host,
739 			     "Unable to request irq.\n");
740 		goto err_out_free_exch_mgr;
741 	}
742 
743 	for (i = 0; i < fnic->intr_count; i++)
744 		vnic_intr_unmask(&fnic->intr[i]);
745 
746 	fnic_notify_timer_start(fnic);
747 
748 	return 0;
749 
750 err_out_free_exch_mgr:
751 	fc_exch_mgr_free(lp);
752 err_out_remove_scsi_host:
753 	fc_remove_host(lp->host);
754 	scsi_remove_host(lp->host);
755 err_out_free_rq_buf:
756 	for (i = 0; i < fnic->rq_count; i++)
757 		vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
758 	vnic_dev_notify_unset(fnic->vdev);
759 err_out_free_max_pool:
760 	mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]);
761 err_out_free_dflt_pool:
762 	mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]);
763 err_out_free_ioreq_pool:
764 	mempool_destroy(fnic->io_req_pool);
765 err_out_free_resources:
766 	fnic_free_vnic_resources(fnic);
767 err_out_clear_intr:
768 	fnic_clear_intr_mode(fnic);
769 err_out_dev_close:
770 	vnic_dev_close(fnic->vdev);
771 err_out_vnic_unregister:
772 	vnic_dev_unregister(fnic->vdev);
773 err_out_iounmap:
774 	fnic_iounmap(fnic);
775 err_out_release_regions:
776 	pci_release_regions(pdev);
777 err_out_disable_device:
778 	pci_disable_device(pdev);
779 err_out_free_hba:
780 	scsi_host_put(lp->host);
781 err_out:
782 	return err;
783 }
784 
785 static void __devexit fnic_remove(struct pci_dev *pdev)
786 {
787 	struct fnic *fnic = pci_get_drvdata(pdev);
788 	struct fc_lport *lp = fnic->lport;
789 	unsigned long flags;
790 
791 	/*
792 	 * Mark state so that the workqueue thread stops forwarding
793 	 * received frames and link events to the local port. ISR and
794 	 * other threads that can queue work items will also stop
795 	 * creating work items on the fnic workqueue
796 	 */
797 	spin_lock_irqsave(&fnic->fnic_lock, flags);
798 	fnic->stop_rx_link_events = 1;
799 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
800 
801 	if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
802 		del_timer_sync(&fnic->notify_timer);
803 
804 	/*
805 	 * Flush the fnic event queue. After this call, there should
806 	 * be no event queued for this fnic device in the workqueue
807 	 */
808 	flush_workqueue(fnic_event_queue);
809 	skb_queue_purge(&fnic->frame_queue);
810 	skb_queue_purge(&fnic->tx_queue);
811 
812 	/*
813 	 * Log off the fabric. This stops all remote ports, dns port,
814 	 * logs off the fabric. This flushes all rport, disc, lport work
815 	 * before returning
816 	 */
817 	fc_fabric_logoff(fnic->lport);
818 
819 	spin_lock_irqsave(&fnic->fnic_lock, flags);
820 	fnic->in_remove = 1;
821 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
822 
823 	fcoe_ctlr_destroy(&fnic->ctlr);
824 	fc_lport_destroy(lp);
825 
826 	/*
827 	 * This stops the fnic device, masks all interrupts. Completed
828 	 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
829 	 * cleaned up
830 	 */
831 	fnic_cleanup(fnic);
832 
833 	BUG_ON(!skb_queue_empty(&fnic->frame_queue));
834 	BUG_ON(!skb_queue_empty(&fnic->tx_queue));
835 
836 	spin_lock_irqsave(&fnic_list_lock, flags);
837 	list_del(&fnic->list);
838 	spin_unlock_irqrestore(&fnic_list_lock, flags);
839 
840 	fc_remove_host(fnic->lport->host);
841 	scsi_remove_host(fnic->lport->host);
842 	fc_exch_mgr_free(fnic->lport);
843 	vnic_dev_notify_unset(fnic->vdev);
844 	fnic_free_intr(fnic);
845 	fnic_free_vnic_resources(fnic);
846 	fnic_clear_intr_mode(fnic);
847 	vnic_dev_close(fnic->vdev);
848 	vnic_dev_unregister(fnic->vdev);
849 	fnic_iounmap(fnic);
850 	pci_release_regions(pdev);
851 	pci_disable_device(pdev);
852 	pci_set_drvdata(pdev, NULL);
853 	scsi_host_put(lp->host);
854 }
855 
856 static struct pci_driver fnic_driver = {
857 	.name = DRV_NAME,
858 	.id_table = fnic_id_table,
859 	.probe = fnic_probe,
860 	.remove = __devexit_p(fnic_remove),
861 };
862 
863 static int __init fnic_init_module(void)
864 {
865 	size_t len;
866 	int err = 0;
867 
868 	printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
869 
870 	/* Create a cache for allocation of default size sgls */
871 	len = sizeof(struct fnic_dflt_sgl_list);
872 	fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create
873 		("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
874 		 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA,
875 		 NULL);
876 	if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) {
877 		printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n");
878 		err = -ENOMEM;
879 		goto err_create_fnic_sgl_slab_dflt;
880 	}
881 
882 	/* Create a cache for allocation of max size sgls*/
883 	len = sizeof(struct fnic_sgl_list);
884 	fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create
885 		("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
886 		 SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA,
887 		 NULL);
888 	if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) {
889 		printk(KERN_ERR PFX "failed to create fnic max sgl slab\n");
890 		err = -ENOMEM;
891 		goto err_create_fnic_sgl_slab_max;
892 	}
893 
894 	/* Create a cache of io_req structs for use via mempool */
895 	fnic_io_req_cache = kmem_cache_create("fnic_io_req",
896 					      sizeof(struct fnic_io_req),
897 					      0, SLAB_HWCACHE_ALIGN, NULL);
898 	if (!fnic_io_req_cache) {
899 		printk(KERN_ERR PFX "failed to create fnic io_req slab\n");
900 		err = -ENOMEM;
901 		goto err_create_fnic_ioreq_slab;
902 	}
903 
904 	fnic_event_queue = create_singlethread_workqueue("fnic_event_wq");
905 	if (!fnic_event_queue) {
906 		printk(KERN_ERR PFX "fnic work queue create failed\n");
907 		err = -ENOMEM;
908 		goto err_create_fnic_workq;
909 	}
910 
911 	spin_lock_init(&fnic_list_lock);
912 	INIT_LIST_HEAD(&fnic_list);
913 
914 	fnic_fc_transport = fc_attach_transport(&fnic_fc_functions);
915 	if (!fnic_fc_transport) {
916 		printk(KERN_ERR PFX "fc_attach_transport error\n");
917 		err = -ENOMEM;
918 		goto err_fc_transport;
919 	}
920 
921 	/* register the driver with PCI system */
922 	err = pci_register_driver(&fnic_driver);
923 	if (err < 0) {
924 		printk(KERN_ERR PFX "pci register error\n");
925 		goto err_pci_register;
926 	}
927 	return err;
928 
929 err_pci_register:
930 	fc_release_transport(fnic_fc_transport);
931 err_fc_transport:
932 	destroy_workqueue(fnic_event_queue);
933 err_create_fnic_workq:
934 	kmem_cache_destroy(fnic_io_req_cache);
935 err_create_fnic_ioreq_slab:
936 	kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
937 err_create_fnic_sgl_slab_max:
938 	kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
939 err_create_fnic_sgl_slab_dflt:
940 	return err;
941 }
942 
943 static void __exit fnic_cleanup_module(void)
944 {
945 	pci_unregister_driver(&fnic_driver);
946 	destroy_workqueue(fnic_event_queue);
947 	kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
948 	kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
949 	kmem_cache_destroy(fnic_io_req_cache);
950 	fc_release_transport(fnic_fc_transport);
951 }
952 
953 module_init(fnic_init_module);
954 module_exit(fnic_cleanup_module);
955 
956