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