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