xref: /openbmc/linux/drivers/scsi/qedf/qedf_main.c (revision d2c43ff1)
1 /*
2  *  QLogic FCoE Offload Driver
3  *  Copyright (c) 2016-2017 Cavium Inc.
4  *
5  *  This software is available under the terms of the GNU General Public License
6  *  (GPL) Version 2, available from the file COPYING in the main directory of
7  *  this source tree.
8  */
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/device.h>
14 #include <linux/highmem.h>
15 #include <linux/crc32.h>
16 #include <linux/interrupt.h>
17 #include <linux/list.h>
18 #include <linux/kthread.h>
19 #include <scsi/libfc.h>
20 #include <scsi/scsi_host.h>
21 #include <linux/if_ether.h>
22 #include <linux/if_vlan.h>
23 #include <linux/cpu.h>
24 #include "qedf.h"
25 #include <uapi/linux/pci_regs.h>
26 
27 const struct qed_fcoe_ops *qed_ops;
28 
29 static int qedf_probe(struct pci_dev *pdev, const struct pci_device_id *id);
30 static void qedf_remove(struct pci_dev *pdev);
31 
32 extern struct qedf_debugfs_ops qedf_debugfs_ops;
33 extern struct file_operations qedf_dbg_fops;
34 
35 /*
36  * Driver module parameters.
37  */
38 static unsigned int qedf_dev_loss_tmo = 60;
39 module_param_named(dev_loss_tmo, qedf_dev_loss_tmo, int, S_IRUGO);
40 MODULE_PARM_DESC(dev_loss_tmo,  " dev_loss_tmo setting for attached "
41 	"remote ports (default 60)");
42 
43 uint qedf_debug = QEDF_LOG_INFO;
44 module_param_named(debug, qedf_debug, uint, S_IRUGO);
45 MODULE_PARM_DESC(qedf_debug, " Debug mask. Pass '1' to enable default debugging"
46 	" mask");
47 
48 static uint qedf_fipvlan_retries = 30;
49 module_param_named(fipvlan_retries, qedf_fipvlan_retries, int, S_IRUGO);
50 MODULE_PARM_DESC(fipvlan_retries, " Number of FIP VLAN requests to attempt "
51 	"before giving up (default 30)");
52 
53 static uint qedf_fallback_vlan = QEDF_FALLBACK_VLAN;
54 module_param_named(fallback_vlan, qedf_fallback_vlan, int, S_IRUGO);
55 MODULE_PARM_DESC(fallback_vlan, " VLAN ID to try if fip vlan request fails "
56 	"(default 1002).");
57 
58 static uint qedf_default_prio = QEDF_DEFAULT_PRIO;
59 module_param_named(default_prio, qedf_default_prio, int, S_IRUGO);
60 MODULE_PARM_DESC(default_prio, " Default 802.1q priority for FIP and FCoE"
61 	" traffic (default 3).");
62 
63 uint qedf_dump_frames;
64 module_param_named(dump_frames, qedf_dump_frames, int, S_IRUGO | S_IWUSR);
65 MODULE_PARM_DESC(dump_frames, " Print the skb data of FIP and FCoE frames "
66 	"(default off)");
67 
68 static uint qedf_queue_depth;
69 module_param_named(queue_depth, qedf_queue_depth, int, S_IRUGO);
70 MODULE_PARM_DESC(queue_depth, " Sets the queue depth for all LUNs discovered "
71 	"by the qedf driver. Default is 0 (use OS default).");
72 
73 uint qedf_io_tracing;
74 module_param_named(io_tracing, qedf_io_tracing, int, S_IRUGO | S_IWUSR);
75 MODULE_PARM_DESC(io_tracing, " Enable logging of SCSI requests/completions "
76 	"into trace buffer. (default off).");
77 
78 static uint qedf_max_lun = MAX_FIBRE_LUNS;
79 module_param_named(max_lun, qedf_max_lun, int, S_IRUGO);
80 MODULE_PARM_DESC(max_lun, " Sets the maximum luns per target that the driver "
81 	"supports. (default 0xffffffff)");
82 
83 uint qedf_link_down_tmo;
84 module_param_named(link_down_tmo, qedf_link_down_tmo, int, S_IRUGO);
85 MODULE_PARM_DESC(link_down_tmo, " Delays informing the fcoe transport that the "
86 	"link is down by N seconds.");
87 
88 bool qedf_retry_delay;
89 module_param_named(retry_delay, qedf_retry_delay, bool, S_IRUGO | S_IWUSR);
90 MODULE_PARM_DESC(retry_delay, " Enable/disable handling of FCP_RSP IU retry "
91 	"delay handling (default off).");
92 
93 static uint qedf_dp_module;
94 module_param_named(dp_module, qedf_dp_module, uint, S_IRUGO);
95 MODULE_PARM_DESC(dp_module, " bit flags control for verbose printk passed "
96 	"qed module during probe.");
97 
98 static uint qedf_dp_level = QED_LEVEL_NOTICE;
99 module_param_named(dp_level, qedf_dp_level, uint, S_IRUGO);
100 MODULE_PARM_DESC(dp_level, " printk verbosity control passed to qed module  "
101 	"during probe (0-3: 0 more verbose).");
102 
103 struct workqueue_struct *qedf_io_wq;
104 
105 static struct fcoe_percpu_s qedf_global;
106 static DEFINE_SPINLOCK(qedf_global_lock);
107 
108 static struct kmem_cache *qedf_io_work_cache;
109 
110 void qedf_set_vlan_id(struct qedf_ctx *qedf, int vlan_id)
111 {
112 	qedf->vlan_id = vlan_id;
113 	qedf->vlan_id |= qedf_default_prio << VLAN_PRIO_SHIFT;
114 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Setting vlan_id=%04x "
115 		   "prio=%d.\n", vlan_id, qedf_default_prio);
116 }
117 
118 /* Returns true if we have a valid vlan, false otherwise */
119 static bool qedf_initiate_fipvlan_req(struct qedf_ctx *qedf)
120 {
121 	int rc;
122 
123 	if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
124 		QEDF_ERR(&(qedf->dbg_ctx), "Link not up.\n");
125 		return  false;
126 	}
127 
128 	while (qedf->fipvlan_retries--) {
129 		if (qedf->vlan_id > 0)
130 			return true;
131 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
132 			   "Retry %d.\n", qedf->fipvlan_retries);
133 		init_completion(&qedf->fipvlan_compl);
134 		qedf_fcoe_send_vlan_req(qedf);
135 		rc = wait_for_completion_timeout(&qedf->fipvlan_compl,
136 		    1 * HZ);
137 		if (rc > 0) {
138 			fcoe_ctlr_link_up(&qedf->ctlr);
139 			return true;
140 		}
141 	}
142 
143 	return false;
144 }
145 
146 static void qedf_handle_link_update(struct work_struct *work)
147 {
148 	struct qedf_ctx *qedf =
149 	    container_of(work, struct qedf_ctx, link_update.work);
150 	int rc;
151 
152 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Entered.\n");
153 
154 	if (atomic_read(&qedf->link_state) == QEDF_LINK_UP) {
155 		rc = qedf_initiate_fipvlan_req(qedf);
156 		if (rc)
157 			return;
158 		/*
159 		 * If we get here then we never received a repsonse to our
160 		 * fip vlan request so set the vlan_id to the default and
161 		 * tell FCoE that the link is up
162 		 */
163 		QEDF_WARN(&(qedf->dbg_ctx), "Did not receive FIP VLAN "
164 			   "response, falling back to default VLAN %d.\n",
165 			   qedf_fallback_vlan);
166 		qedf_set_vlan_id(qedf, QEDF_FALLBACK_VLAN);
167 
168 		/*
169 		 * Zero out data_src_addr so we'll update it with the new
170 		 * lport port_id
171 		 */
172 		eth_zero_addr(qedf->data_src_addr);
173 		fcoe_ctlr_link_up(&qedf->ctlr);
174 	} else if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
175 		/*
176 		 * If we hit here and link_down_tmo_valid is still 1 it means
177 		 * that link_down_tmo timed out so set it to 0 to make sure any
178 		 * other readers have accurate state.
179 		 */
180 		atomic_set(&qedf->link_down_tmo_valid, 0);
181 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
182 		    "Calling fcoe_ctlr_link_down().\n");
183 		fcoe_ctlr_link_down(&qedf->ctlr);
184 		qedf_wait_for_upload(qedf);
185 		/* Reset the number of FIP VLAN retries */
186 		qedf->fipvlan_retries = qedf_fipvlan_retries;
187 	}
188 }
189 
190 static void qedf_flogi_resp(struct fc_seq *seq, struct fc_frame *fp,
191 	void *arg)
192 {
193 	struct fc_exch *exch = fc_seq_exch(seq);
194 	struct fc_lport *lport = exch->lp;
195 	struct qedf_ctx *qedf = lport_priv(lport);
196 
197 	if (!qedf) {
198 		QEDF_ERR(NULL, "qedf is NULL.\n");
199 		return;
200 	}
201 
202 	/*
203 	 * If ERR_PTR is set then don't try to stat anything as it will cause
204 	 * a crash when we access fp.
205 	 */
206 	if (IS_ERR(fp)) {
207 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
208 		    "fp has IS_ERR() set.\n");
209 		goto skip_stat;
210 	}
211 
212 	/* Log stats for FLOGI reject */
213 	if (fc_frame_payload_op(fp) == ELS_LS_RJT)
214 		qedf->flogi_failed++;
215 
216 	/* Complete flogi_compl so we can proceed to sending ADISCs */
217 	complete(&qedf->flogi_compl);
218 
219 skip_stat:
220 	/* Report response to libfc */
221 	fc_lport_flogi_resp(seq, fp, lport);
222 }
223 
224 static struct fc_seq *qedf_elsct_send(struct fc_lport *lport, u32 did,
225 	struct fc_frame *fp, unsigned int op,
226 	void (*resp)(struct fc_seq *,
227 	struct fc_frame *,
228 	void *),
229 	void *arg, u32 timeout)
230 {
231 	struct qedf_ctx *qedf = lport_priv(lport);
232 
233 	/*
234 	 * Intercept FLOGI for statistic purposes. Note we use the resp
235 	 * callback to tell if this is really a flogi.
236 	 */
237 	if (resp == fc_lport_flogi_resp) {
238 		qedf->flogi_cnt++;
239 		return fc_elsct_send(lport, did, fp, op, qedf_flogi_resp,
240 		    arg, timeout);
241 	}
242 
243 	return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
244 }
245 
246 int qedf_send_flogi(struct qedf_ctx *qedf)
247 {
248 	struct fc_lport *lport;
249 	struct fc_frame *fp;
250 
251 	lport = qedf->lport;
252 
253 	if (!lport->tt.elsct_send)
254 		return -EINVAL;
255 
256 	fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
257 	if (!fp) {
258 		QEDF_ERR(&(qedf->dbg_ctx), "fc_frame_alloc failed.\n");
259 		return -ENOMEM;
260 	}
261 
262 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
263 	    "Sending FLOGI to reestablish session with switch.\n");
264 	lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
265 	    ELS_FLOGI, qedf_flogi_resp, lport, lport->r_a_tov);
266 
267 	init_completion(&qedf->flogi_compl);
268 
269 	return 0;
270 }
271 
272 struct qedf_tmp_rdata_item {
273 	struct fc_rport_priv *rdata;
274 	struct list_head list;
275 };
276 
277 /*
278  * This function is called if link_down_tmo is in use.  If we get a link up and
279  * link_down_tmo has not expired then use just FLOGI/ADISC to recover our
280  * sessions with targets.  Otherwise, just call fcoe_ctlr_link_up().
281  */
282 static void qedf_link_recovery(struct work_struct *work)
283 {
284 	struct qedf_ctx *qedf =
285 	    container_of(work, struct qedf_ctx, link_recovery.work);
286 	struct qedf_rport *fcport;
287 	struct fc_rport_priv *rdata;
288 	struct qedf_tmp_rdata_item *rdata_item, *tmp_rdata_item;
289 	bool rc;
290 	int retries = 30;
291 	int rval, i;
292 	struct list_head rdata_login_list;
293 
294 	INIT_LIST_HEAD(&rdata_login_list);
295 
296 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
297 	    "Link down tmo did not expire.\n");
298 
299 	/*
300 	 * Essentially reset the fcoe_ctlr here without affecting the state
301 	 * of the libfc structs.
302 	 */
303 	qedf->ctlr.state = FIP_ST_LINK_WAIT;
304 	fcoe_ctlr_link_down(&qedf->ctlr);
305 
306 	/*
307 	 * Bring the link up before we send the fipvlan request so libfcoe
308 	 * can select a new fcf in parallel
309 	 */
310 	fcoe_ctlr_link_up(&qedf->ctlr);
311 
312 	/* Since the link when down and up to verify which vlan we're on */
313 	qedf->fipvlan_retries = qedf_fipvlan_retries;
314 	rc = qedf_initiate_fipvlan_req(qedf);
315 	if (!rc)
316 		return;
317 
318 	/*
319 	 * We need to wait for an FCF to be selected due to the
320 	 * fcoe_ctlr_link_up other the FLOGI will be rejected.
321 	 */
322 	while (retries > 0) {
323 		if (qedf->ctlr.sel_fcf) {
324 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
325 			    "FCF reselected, proceeding with FLOGI.\n");
326 			break;
327 		}
328 		msleep(500);
329 		retries--;
330 	}
331 
332 	if (retries < 1) {
333 		QEDF_ERR(&(qedf->dbg_ctx), "Exhausted retries waiting for "
334 		    "FCF selection.\n");
335 		return;
336 	}
337 
338 	rval = qedf_send_flogi(qedf);
339 	if (rval)
340 		return;
341 
342 	/* Wait for FLOGI completion before proceeding with sending ADISCs */
343 	i = wait_for_completion_timeout(&qedf->flogi_compl,
344 	    qedf->lport->r_a_tov);
345 	if (i == 0) {
346 		QEDF_ERR(&(qedf->dbg_ctx), "FLOGI timed out.\n");
347 		return;
348 	}
349 
350 	/*
351 	 * Call lport->tt.rport_login which will cause libfc to send an
352 	 * ADISC since the rport is in state ready.
353 	 */
354 	rcu_read_lock();
355 	list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
356 		rdata = fcport->rdata;
357 		if (rdata == NULL)
358 			continue;
359 		rdata_item = kzalloc(sizeof(struct qedf_tmp_rdata_item),
360 		    GFP_ATOMIC);
361 		if (!rdata_item)
362 			continue;
363 		if (kref_get_unless_zero(&rdata->kref)) {
364 			rdata_item->rdata = rdata;
365 			list_add(&rdata_item->list, &rdata_login_list);
366 		} else
367 			kfree(rdata_item);
368 	}
369 	rcu_read_unlock();
370 	/*
371 	 * Do the fc_rport_login outside of the rcu lock so we don't take a
372 	 * mutex in an atomic context.
373 	 */
374 	list_for_each_entry_safe(rdata_item, tmp_rdata_item, &rdata_login_list,
375 	    list) {
376 		list_del(&rdata_item->list);
377 		fc_rport_login(rdata_item->rdata);
378 		kref_put(&rdata_item->rdata->kref, fc_rport_destroy);
379 		kfree(rdata_item);
380 	}
381 }
382 
383 static void qedf_update_link_speed(struct qedf_ctx *qedf,
384 	struct qed_link_output *link)
385 {
386 	struct fc_lport *lport = qedf->lport;
387 
388 	lport->link_speed = FC_PORTSPEED_UNKNOWN;
389 	lport->link_supported_speeds = FC_PORTSPEED_UNKNOWN;
390 
391 	/* Set fc_host link speed */
392 	switch (link->speed) {
393 	case 10000:
394 		lport->link_speed = FC_PORTSPEED_10GBIT;
395 		break;
396 	case 25000:
397 		lport->link_speed = FC_PORTSPEED_25GBIT;
398 		break;
399 	case 40000:
400 		lport->link_speed = FC_PORTSPEED_40GBIT;
401 		break;
402 	case 50000:
403 		lport->link_speed = FC_PORTSPEED_50GBIT;
404 		break;
405 	case 100000:
406 		lport->link_speed = FC_PORTSPEED_100GBIT;
407 		break;
408 	default:
409 		lport->link_speed = FC_PORTSPEED_UNKNOWN;
410 		break;
411 	}
412 
413 	/*
414 	 * Set supported link speed by querying the supported
415 	 * capabilities of the link.
416 	 */
417 	if (link->supported_caps & SUPPORTED_10000baseKR_Full)
418 		lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
419 	if (link->supported_caps & SUPPORTED_25000baseKR_Full)
420 		lport->link_supported_speeds |= FC_PORTSPEED_25GBIT;
421 	if (link->supported_caps & SUPPORTED_40000baseLR4_Full)
422 		lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
423 	if (link->supported_caps & SUPPORTED_50000baseKR2_Full)
424 		lport->link_supported_speeds |= FC_PORTSPEED_50GBIT;
425 	if (link->supported_caps & SUPPORTED_100000baseKR4_Full)
426 		lport->link_supported_speeds |= FC_PORTSPEED_100GBIT;
427 	fc_host_supported_speeds(lport->host) = lport->link_supported_speeds;
428 }
429 
430 static void qedf_link_update(void *dev, struct qed_link_output *link)
431 {
432 	struct qedf_ctx *qedf = (struct qedf_ctx *)dev;
433 
434 	if (link->link_up) {
435 		QEDF_ERR(&(qedf->dbg_ctx), "LINK UP (%d GB/s).\n",
436 		    link->speed / 1000);
437 
438 		/* Cancel any pending link down work */
439 		cancel_delayed_work(&qedf->link_update);
440 
441 		atomic_set(&qedf->link_state, QEDF_LINK_UP);
442 		qedf_update_link_speed(qedf, link);
443 
444 		if (atomic_read(&qedf->dcbx) == QEDF_DCBX_DONE) {
445 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
446 			     "DCBx done.\n");
447 			if (atomic_read(&qedf->link_down_tmo_valid) > 0)
448 				queue_delayed_work(qedf->link_update_wq,
449 				    &qedf->link_recovery, 0);
450 			else
451 				queue_delayed_work(qedf->link_update_wq,
452 				    &qedf->link_update, 0);
453 			atomic_set(&qedf->link_down_tmo_valid, 0);
454 		}
455 
456 	} else {
457 		QEDF_ERR(&(qedf->dbg_ctx), "LINK DOWN.\n");
458 
459 		atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
460 		atomic_set(&qedf->dcbx, QEDF_DCBX_PENDING);
461 		/*
462 		 * Flag that we're waiting for the link to come back up before
463 		 * informing the fcoe layer of the event.
464 		 */
465 		if (qedf_link_down_tmo > 0) {
466 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
467 			    "Starting link down tmo.\n");
468 			atomic_set(&qedf->link_down_tmo_valid, 1);
469 		}
470 		qedf->vlan_id  = 0;
471 		qedf_update_link_speed(qedf, link);
472 		queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
473 		    qedf_link_down_tmo * HZ);
474 	}
475 }
476 
477 
478 static void qedf_dcbx_handler(void *dev, struct qed_dcbx_get *get, u32 mib_type)
479 {
480 	struct qedf_ctx *qedf = (struct qedf_ctx *)dev;
481 
482 	QEDF_ERR(&(qedf->dbg_ctx), "DCBx event valid=%d enabled=%d fcoe "
483 	    "prio=%d.\n", get->operational.valid, get->operational.enabled,
484 	    get->operational.app_prio.fcoe);
485 
486 	if (get->operational.enabled && get->operational.valid) {
487 		/* If DCBX was already negotiated on link up then just exit */
488 		if (atomic_read(&qedf->dcbx) == QEDF_DCBX_DONE) {
489 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
490 			    "DCBX already set on link up.\n");
491 			return;
492 		}
493 
494 		atomic_set(&qedf->dcbx, QEDF_DCBX_DONE);
495 
496 		if (atomic_read(&qedf->link_state) == QEDF_LINK_UP) {
497 			if (atomic_read(&qedf->link_down_tmo_valid) > 0)
498 				queue_delayed_work(qedf->link_update_wq,
499 				    &qedf->link_recovery, 0);
500 			else
501 				queue_delayed_work(qedf->link_update_wq,
502 				    &qedf->link_update, 0);
503 			atomic_set(&qedf->link_down_tmo_valid, 0);
504 		}
505 	}
506 
507 }
508 
509 static u32 qedf_get_login_failures(void *cookie)
510 {
511 	struct qedf_ctx *qedf;
512 
513 	qedf = (struct qedf_ctx *)cookie;
514 	return qedf->flogi_failed;
515 }
516 
517 static struct qed_fcoe_cb_ops qedf_cb_ops = {
518 	{
519 		.link_update = qedf_link_update,
520 		.dcbx_aen = qedf_dcbx_handler,
521 	}
522 };
523 
524 /*
525  * Various transport templates.
526  */
527 
528 static struct scsi_transport_template *qedf_fc_transport_template;
529 static struct scsi_transport_template *qedf_fc_vport_transport_template;
530 
531 /*
532  * SCSI EH handlers
533  */
534 static int qedf_eh_abort(struct scsi_cmnd *sc_cmd)
535 {
536 	struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
537 	struct fc_rport_libfc_priv *rp = rport->dd_data;
538 	struct qedf_rport *fcport;
539 	struct fc_lport *lport;
540 	struct qedf_ctx *qedf;
541 	struct qedf_ioreq *io_req;
542 	int rc = FAILED;
543 	int rval;
544 
545 	if (fc_remote_port_chkready(rport)) {
546 		QEDF_ERR(NULL, "rport not ready\n");
547 		goto out;
548 	}
549 
550 	lport = shost_priv(sc_cmd->device->host);
551 	qedf = (struct qedf_ctx *)lport_priv(lport);
552 
553 	if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
554 		QEDF_ERR(&(qedf->dbg_ctx), "link not ready.\n");
555 		goto out;
556 	}
557 
558 	fcport = (struct qedf_rport *)&rp[1];
559 
560 	io_req = (struct qedf_ioreq *)sc_cmd->SCp.ptr;
561 	if (!io_req) {
562 		QEDF_ERR(&(qedf->dbg_ctx), "io_req is NULL.\n");
563 		rc = SUCCESS;
564 		goto out;
565 	}
566 
567 	if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
568 	    test_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags) ||
569 	    test_bit(QEDF_CMD_IN_ABORT, &io_req->flags)) {
570 		QEDF_ERR(&(qedf->dbg_ctx), "io_req xid=0x%x already in "
571 			  "cleanup or abort processing or already "
572 			  "completed.\n", io_req->xid);
573 		rc = SUCCESS;
574 		goto out;
575 	}
576 
577 	QEDF_ERR(&(qedf->dbg_ctx), "Aborting io_req sc_cmd=%p xid=0x%x "
578 		  "fp_idx=%d.\n", sc_cmd, io_req->xid, io_req->fp_idx);
579 
580 	if (qedf->stop_io_on_error) {
581 		qedf_stop_all_io(qedf);
582 		rc = SUCCESS;
583 		goto out;
584 	}
585 
586 	init_completion(&io_req->abts_done);
587 	rval = qedf_initiate_abts(io_req, true);
588 	if (rval) {
589 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
590 		goto out;
591 	}
592 
593 	wait_for_completion(&io_req->abts_done);
594 
595 	if (io_req->event == QEDF_IOREQ_EV_ABORT_SUCCESS ||
596 	    io_req->event == QEDF_IOREQ_EV_ABORT_FAILED ||
597 	    io_req->event == QEDF_IOREQ_EV_CLEANUP_SUCCESS) {
598 		/*
599 		 * If we get a reponse to the abort this is success from
600 		 * the perspective that all references to the command have
601 		 * been removed from the driver and firmware
602 		 */
603 		rc = SUCCESS;
604 	} else {
605 		/* If the abort and cleanup failed then return a failure */
606 		rc = FAILED;
607 	}
608 
609 	if (rc == SUCCESS)
610 		QEDF_ERR(&(qedf->dbg_ctx), "ABTS succeeded, xid=0x%x.\n",
611 			  io_req->xid);
612 	else
613 		QEDF_ERR(&(qedf->dbg_ctx), "ABTS failed, xid=0x%x.\n",
614 			  io_req->xid);
615 
616 out:
617 	return rc;
618 }
619 
620 static int qedf_eh_target_reset(struct scsi_cmnd *sc_cmd)
621 {
622 	QEDF_ERR(NULL, "TARGET RESET Issued...");
623 	return qedf_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
624 }
625 
626 static int qedf_eh_device_reset(struct scsi_cmnd *sc_cmd)
627 {
628 	QEDF_ERR(NULL, "LUN RESET Issued...\n");
629 	return qedf_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
630 }
631 
632 static int qedf_eh_bus_reset(struct scsi_cmnd *sc_cmd)
633 {
634 	QEDF_ERR(NULL, "BUS RESET Issued...\n");
635 	/*
636 	 * Essentially a no-op but return SUCCESS to prevent
637 	 * unnecessary escalation to the host reset handler.
638 	 */
639 	return SUCCESS;
640 }
641 
642 void qedf_wait_for_upload(struct qedf_ctx *qedf)
643 {
644 	while (1) {
645 		if (atomic_read(&qedf->num_offloads))
646 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
647 			    "Waiting for all uploads to complete.\n");
648 		else
649 			break;
650 		msleep(500);
651 	}
652 }
653 
654 /* Performs soft reset of qedf_ctx by simulating a link down/up */
655 static void qedf_ctx_soft_reset(struct fc_lport *lport)
656 {
657 	struct qedf_ctx *qedf;
658 
659 	if (lport->vport) {
660 		QEDF_ERR(NULL, "Cannot issue host reset on NPIV port.\n");
661 		return;
662 	}
663 
664 	qedf = lport_priv(lport);
665 
666 	/* For host reset, essentially do a soft link up/down */
667 	atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
668 	atomic_set(&qedf->dcbx, QEDF_DCBX_PENDING);
669 	queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
670 	    0);
671 	qedf_wait_for_upload(qedf);
672 	atomic_set(&qedf->link_state, QEDF_LINK_UP);
673 	qedf->vlan_id  = 0;
674 	queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
675 	    0);
676 }
677 
678 /* Reset the host by gracefully logging out and then logging back in */
679 static int qedf_eh_host_reset(struct scsi_cmnd *sc_cmd)
680 {
681 	struct fc_lport *lport;
682 	struct qedf_ctx *qedf;
683 
684 	lport = shost_priv(sc_cmd->device->host);
685 	qedf = lport_priv(lport);
686 
687 	if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN ||
688 	    test_bit(QEDF_UNLOADING, &qedf->flags))
689 		return FAILED;
690 
691 	QEDF_ERR(&(qedf->dbg_ctx), "HOST RESET Issued...");
692 
693 	qedf_ctx_soft_reset(lport);
694 
695 	return SUCCESS;
696 }
697 
698 static int qedf_slave_configure(struct scsi_device *sdev)
699 {
700 	if (qedf_queue_depth) {
701 		scsi_change_queue_depth(sdev, qedf_queue_depth);
702 	}
703 
704 	return 0;
705 }
706 
707 static struct scsi_host_template qedf_host_template = {
708 	.module 	= THIS_MODULE,
709 	.name 		= QEDF_MODULE_NAME,
710 	.this_id 	= -1,
711 	.cmd_per_lun	= 32,
712 	.use_clustering = ENABLE_CLUSTERING,
713 	.max_sectors 	= 0xffff,
714 	.queuecommand 	= qedf_queuecommand,
715 	.shost_attrs	= qedf_host_attrs,
716 	.eh_abort_handler	= qedf_eh_abort,
717 	.eh_device_reset_handler = qedf_eh_device_reset, /* lun reset */
718 	.eh_target_reset_handler = qedf_eh_target_reset, /* target reset */
719 	.eh_bus_reset_handler = qedf_eh_bus_reset,
720 	.eh_host_reset_handler  = qedf_eh_host_reset,
721 	.slave_configure	= qedf_slave_configure,
722 	.dma_boundary = QED_HW_DMA_BOUNDARY,
723 	.sg_tablesize = QEDF_MAX_BDS_PER_CMD,
724 	.can_queue = FCOE_PARAMS_NUM_TASKS,
725 	.change_queue_depth = scsi_change_queue_depth,
726 };
727 
728 static int qedf_get_paged_crc_eof(struct sk_buff *skb, int tlen)
729 {
730 	int rc;
731 
732 	spin_lock(&qedf_global_lock);
733 	rc = fcoe_get_paged_crc_eof(skb, tlen, &qedf_global);
734 	spin_unlock(&qedf_global_lock);
735 
736 	return rc;
737 }
738 
739 static struct qedf_rport *qedf_fcport_lookup(struct qedf_ctx *qedf, u32 port_id)
740 {
741 	struct qedf_rport *fcport;
742 	struct fc_rport_priv *rdata;
743 
744 	rcu_read_lock();
745 	list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
746 		rdata = fcport->rdata;
747 		if (rdata == NULL)
748 			continue;
749 		if (rdata->ids.port_id == port_id) {
750 			rcu_read_unlock();
751 			return fcport;
752 		}
753 	}
754 	rcu_read_unlock();
755 
756 	/* Return NULL to caller to let them know fcport was not found */
757 	return NULL;
758 }
759 
760 /* Transmits an ELS frame over an offloaded session */
761 static int qedf_xmit_l2_frame(struct qedf_rport *fcport, struct fc_frame *fp)
762 {
763 	struct fc_frame_header *fh;
764 	int rc = 0;
765 
766 	fh = fc_frame_header_get(fp);
767 	if ((fh->fh_type == FC_TYPE_ELS) &&
768 	    (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
769 		switch (fc_frame_payload_op(fp)) {
770 		case ELS_ADISC:
771 			qedf_send_adisc(fcport, fp);
772 			rc = 1;
773 			break;
774 		}
775 	}
776 
777 	return rc;
778 }
779 
780 /**
781  * qedf_xmit - qedf FCoE frame transmit function
782  *
783  */
784 static int qedf_xmit(struct fc_lport *lport, struct fc_frame *fp)
785 {
786 	struct fc_lport		*base_lport;
787 	struct qedf_ctx		*qedf;
788 	struct ethhdr		*eh;
789 	struct fcoe_crc_eof	*cp;
790 	struct sk_buff		*skb;
791 	struct fc_frame_header	*fh;
792 	struct fcoe_hdr		*hp;
793 	u8			sof, eof;
794 	u32			crc;
795 	unsigned int		hlen, tlen, elen;
796 	int			wlen;
797 	struct fc_stats		*stats;
798 	struct fc_lport *tmp_lport;
799 	struct fc_lport *vn_port = NULL;
800 	struct qedf_rport *fcport;
801 	int rc;
802 	u16 vlan_tci = 0;
803 
804 	qedf = (struct qedf_ctx *)lport_priv(lport);
805 
806 	fh = fc_frame_header_get(fp);
807 	skb = fp_skb(fp);
808 
809 	/* Filter out traffic to other NPIV ports on the same host */
810 	if (lport->vport)
811 		base_lport = shost_priv(vport_to_shost(lport->vport));
812 	else
813 		base_lport = lport;
814 
815 	/* Flag if the destination is the base port */
816 	if (base_lport->port_id == ntoh24(fh->fh_d_id)) {
817 		vn_port = base_lport;
818 	} else {
819 		/* Got through the list of vports attached to the base_lport
820 		 * and see if we have a match with the destination address.
821 		 */
822 		list_for_each_entry(tmp_lport, &base_lport->vports, list) {
823 			if (tmp_lport->port_id == ntoh24(fh->fh_d_id)) {
824 				vn_port = tmp_lport;
825 				break;
826 			}
827 		}
828 	}
829 	if (vn_port && ntoh24(fh->fh_d_id) != FC_FID_FLOGI) {
830 		struct fc_rport_priv *rdata = NULL;
831 
832 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
833 		    "Dropping FCoE frame to %06x.\n", ntoh24(fh->fh_d_id));
834 		kfree_skb(skb);
835 		rdata = fc_rport_lookup(lport, ntoh24(fh->fh_d_id));
836 		if (rdata)
837 			rdata->retries = lport->max_rport_retry_count;
838 		return -EINVAL;
839 	}
840 	/* End NPIV filtering */
841 
842 	if (!qedf->ctlr.sel_fcf) {
843 		kfree_skb(skb);
844 		return 0;
845 	}
846 
847 	if (!test_bit(QEDF_LL2_STARTED, &qedf->flags)) {
848 		QEDF_WARN(&(qedf->dbg_ctx), "LL2 not started\n");
849 		kfree_skb(skb);
850 		return 0;
851 	}
852 
853 	if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
854 		QEDF_WARN(&(qedf->dbg_ctx), "qedf link down\n");
855 		kfree_skb(skb);
856 		return 0;
857 	}
858 
859 	if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
860 		if (fcoe_ctlr_els_send(&qedf->ctlr, lport, skb))
861 			return 0;
862 	}
863 
864 	/* Check to see if this needs to be sent on an offloaded session */
865 	fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
866 
867 	if (fcport && test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
868 		rc = qedf_xmit_l2_frame(fcport, fp);
869 		/*
870 		 * If the frame was successfully sent over the middle path
871 		 * then do not try to also send it over the LL2 path
872 		 */
873 		if (rc)
874 			return 0;
875 	}
876 
877 	sof = fr_sof(fp);
878 	eof = fr_eof(fp);
879 
880 	elen = sizeof(struct ethhdr);
881 	hlen = sizeof(struct fcoe_hdr);
882 	tlen = sizeof(struct fcoe_crc_eof);
883 	wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
884 
885 	skb->ip_summed = CHECKSUM_NONE;
886 	crc = fcoe_fc_crc(fp);
887 
888 	/* copy port crc and eof to the skb buff */
889 	if (skb_is_nonlinear(skb)) {
890 		skb_frag_t *frag;
891 
892 		if (qedf_get_paged_crc_eof(skb, tlen)) {
893 			kfree_skb(skb);
894 			return -ENOMEM;
895 		}
896 		frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
897 		cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
898 	} else {
899 		cp = skb_put(skb, tlen);
900 	}
901 
902 	memset(cp, 0, sizeof(*cp));
903 	cp->fcoe_eof = eof;
904 	cp->fcoe_crc32 = cpu_to_le32(~crc);
905 	if (skb_is_nonlinear(skb)) {
906 		kunmap_atomic(cp);
907 		cp = NULL;
908 	}
909 
910 
911 	/* adjust skb network/transport offsets to match mac/fcoe/port */
912 	skb_push(skb, elen + hlen);
913 	skb_reset_mac_header(skb);
914 	skb_reset_network_header(skb);
915 	skb->mac_len = elen;
916 	skb->protocol = htons(ETH_P_FCOE);
917 
918 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), qedf->vlan_id);
919 
920 	/* fill up mac and fcoe headers */
921 	eh = eth_hdr(skb);
922 	eh->h_proto = htons(ETH_P_FCOE);
923 	if (qedf->ctlr.map_dest)
924 		fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
925 	else
926 		/* insert GW address */
927 		ether_addr_copy(eh->h_dest, qedf->ctlr.dest_addr);
928 
929 	/* Set the source MAC address */
930 	fc_fcoe_set_mac(eh->h_source, fh->fh_s_id);
931 
932 	hp = (struct fcoe_hdr *)(eh + 1);
933 	memset(hp, 0, sizeof(*hp));
934 	if (FC_FCOE_VER)
935 		FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
936 	hp->fcoe_sof = sof;
937 
938 	/*update tx stats */
939 	stats = per_cpu_ptr(lport->stats, get_cpu());
940 	stats->TxFrames++;
941 	stats->TxWords += wlen;
942 	put_cpu();
943 
944 	/* Get VLAN ID from skb for printing purposes */
945 	__vlan_hwaccel_get_tag(skb, &vlan_tci);
946 
947 	/* send down to lld */
948 	fr_dev(fp) = lport;
949 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame send: "
950 	    "src=%06x dest=%06x r_ctl=%x type=%x vlan=%04x.\n",
951 	    ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl, fh->fh_type,
952 	    vlan_tci);
953 	if (qedf_dump_frames)
954 		print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
955 		    1, skb->data, skb->len, false);
956 	qed_ops->ll2->start_xmit(qedf->cdev, skb);
957 
958 	return 0;
959 }
960 
961 static int qedf_alloc_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
962 {
963 	int rval = 0;
964 	u32 *pbl;
965 	dma_addr_t page;
966 	int num_pages;
967 
968 	/* Calculate appropriate queue and PBL sizes */
969 	fcport->sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
970 	fcport->sq_mem_size = ALIGN(fcport->sq_mem_size, QEDF_PAGE_SIZE);
971 	fcport->sq_pbl_size = (fcport->sq_mem_size / QEDF_PAGE_SIZE) *
972 	    sizeof(void *);
973 	fcport->sq_pbl_size = fcport->sq_pbl_size + QEDF_PAGE_SIZE;
974 
975 	fcport->sq = dma_zalloc_coherent(&qedf->pdev->dev,
976 	    fcport->sq_mem_size, &fcport->sq_dma, GFP_KERNEL);
977 	if (!fcport->sq) {
978 		QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue.\n");
979 		rval = 1;
980 		goto out;
981 	}
982 
983 	fcport->sq_pbl = dma_zalloc_coherent(&qedf->pdev->dev,
984 	    fcport->sq_pbl_size, &fcport->sq_pbl_dma, GFP_KERNEL);
985 	if (!fcport->sq_pbl) {
986 		QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue PBL.\n");
987 		rval = 1;
988 		goto out_free_sq;
989 	}
990 
991 	/* Create PBL */
992 	num_pages = fcport->sq_mem_size / QEDF_PAGE_SIZE;
993 	page = fcport->sq_dma;
994 	pbl = (u32 *)fcport->sq_pbl;
995 
996 	while (num_pages--) {
997 		*pbl = U64_LO(page);
998 		pbl++;
999 		*pbl = U64_HI(page);
1000 		pbl++;
1001 		page += QEDF_PAGE_SIZE;
1002 	}
1003 
1004 	return rval;
1005 
1006 out_free_sq:
1007 	dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size, fcport->sq,
1008 	    fcport->sq_dma);
1009 out:
1010 	return rval;
1011 }
1012 
1013 static void qedf_free_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
1014 {
1015 	if (fcport->sq_pbl)
1016 		dma_free_coherent(&qedf->pdev->dev, fcport->sq_pbl_size,
1017 		    fcport->sq_pbl, fcport->sq_pbl_dma);
1018 	if (fcport->sq)
1019 		dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size,
1020 		    fcport->sq, fcport->sq_dma);
1021 }
1022 
1023 static int qedf_offload_connection(struct qedf_ctx *qedf,
1024 	struct qedf_rport *fcport)
1025 {
1026 	struct qed_fcoe_params_offload conn_info;
1027 	u32 port_id;
1028 	u8 lport_src_id[3];
1029 	int rval;
1030 	uint16_t total_sqe = (fcport->sq_mem_size / sizeof(struct fcoe_wqe));
1031 
1032 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offloading connection "
1033 		   "portid=%06x.\n", fcport->rdata->ids.port_id);
1034 	rval = qed_ops->acquire_conn(qedf->cdev, &fcport->handle,
1035 	    &fcport->fw_cid, &fcport->p_doorbell);
1036 	if (rval) {
1037 		QEDF_WARN(&(qedf->dbg_ctx), "Could not acquire connection "
1038 			   "for portid=%06x.\n", fcport->rdata->ids.port_id);
1039 		rval = 1; /* For some reason qed returns 0 on failure here */
1040 		goto out;
1041 	}
1042 
1043 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "portid=%06x "
1044 		   "fw_cid=%08x handle=%d.\n", fcport->rdata->ids.port_id,
1045 		   fcport->fw_cid, fcport->handle);
1046 
1047 	memset(&conn_info, 0, sizeof(struct qed_fcoe_params_offload));
1048 
1049 	/* Fill in the offload connection info */
1050 	conn_info.sq_pbl_addr = fcport->sq_pbl_dma;
1051 
1052 	conn_info.sq_curr_page_addr = (dma_addr_t)(*(u64 *)fcport->sq_pbl);
1053 	conn_info.sq_next_page_addr =
1054 	    (dma_addr_t)(*(u64 *)(fcport->sq_pbl + 8));
1055 
1056 	/* Need to use our FCoE MAC for the offload session */
1057 	port_id = fc_host_port_id(qedf->lport->host);
1058 	lport_src_id[2] = (port_id & 0x000000FF);
1059 	lport_src_id[1] = (port_id & 0x0000FF00) >> 8;
1060 	lport_src_id[0] = (port_id & 0x00FF0000) >> 16;
1061 	fc_fcoe_set_mac(conn_info.src_mac, lport_src_id);
1062 
1063 	ether_addr_copy(conn_info.dst_mac, qedf->ctlr.dest_addr);
1064 
1065 	conn_info.tx_max_fc_pay_len = fcport->rdata->maxframe_size;
1066 	conn_info.e_d_tov_timer_val = qedf->lport->e_d_tov / 20;
1067 	conn_info.rec_tov_timer_val = 3; /* I think this is what E3 was */
1068 	conn_info.rx_max_fc_pay_len = fcport->rdata->maxframe_size;
1069 
1070 	/* Set VLAN data */
1071 	conn_info.vlan_tag = qedf->vlan_id <<
1072 	    FCOE_CONN_OFFLOAD_RAMROD_DATA_VLAN_ID_SHIFT;
1073 	conn_info.vlan_tag |=
1074 	    qedf_default_prio << FCOE_CONN_OFFLOAD_RAMROD_DATA_PRIORITY_SHIFT;
1075 	conn_info.flags |= (FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_MASK <<
1076 	    FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_SHIFT);
1077 
1078 	/* Set host port source id */
1079 	port_id = fc_host_port_id(qedf->lport->host);
1080 	fcport->sid = port_id;
1081 	conn_info.s_id.addr_hi = (port_id & 0x000000FF);
1082 	conn_info.s_id.addr_mid = (port_id & 0x0000FF00) >> 8;
1083 	conn_info.s_id.addr_lo = (port_id & 0x00FF0000) >> 16;
1084 
1085 	conn_info.max_conc_seqs_c3 = fcport->rdata->max_seq;
1086 
1087 	/* Set remote port destination id */
1088 	port_id = fcport->rdata->rport->port_id;
1089 	conn_info.d_id.addr_hi = (port_id & 0x000000FF);
1090 	conn_info.d_id.addr_mid = (port_id & 0x0000FF00) >> 8;
1091 	conn_info.d_id.addr_lo = (port_id & 0x00FF0000) >> 16;
1092 
1093 	conn_info.def_q_idx = 0; /* Default index for send queue? */
1094 
1095 	/* Set FC-TAPE specific flags if needed */
1096 	if (fcport->dev_type == QEDF_RPORT_TYPE_TAPE) {
1097 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN,
1098 		    "Enable CONF, REC for portid=%06x.\n",
1099 		    fcport->rdata->ids.port_id);
1100 		conn_info.flags |= 1 <<
1101 		    FCOE_CONN_OFFLOAD_RAMROD_DATA_B_CONF_REQ_SHIFT;
1102 		conn_info.flags |=
1103 		    ((fcport->rdata->sp_features & FC_SP_FT_SEQC) ? 1 : 0) <<
1104 		    FCOE_CONN_OFFLOAD_RAMROD_DATA_B_REC_VALID_SHIFT;
1105 	}
1106 
1107 	rval = qed_ops->offload_conn(qedf->cdev, fcport->handle, &conn_info);
1108 	if (rval) {
1109 		QEDF_WARN(&(qedf->dbg_ctx), "Could not offload connection "
1110 			   "for portid=%06x.\n", fcport->rdata->ids.port_id);
1111 		goto out_free_conn;
1112 	} else
1113 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offload "
1114 			   "succeeded portid=%06x total_sqe=%d.\n",
1115 			   fcport->rdata->ids.port_id, total_sqe);
1116 
1117 	spin_lock_init(&fcport->rport_lock);
1118 	atomic_set(&fcport->free_sqes, total_sqe);
1119 	return 0;
1120 out_free_conn:
1121 	qed_ops->release_conn(qedf->cdev, fcport->handle);
1122 out:
1123 	return rval;
1124 }
1125 
1126 #define QEDF_TERM_BUFF_SIZE		10
1127 static void qedf_upload_connection(struct qedf_ctx *qedf,
1128 	struct qedf_rport *fcport)
1129 {
1130 	void *term_params;
1131 	dma_addr_t term_params_dma;
1132 
1133 	/* Term params needs to be a DMA coherent buffer as qed shared the
1134 	 * physical DMA address with the firmware. The buffer may be used in
1135 	 * the receive path so we may eventually have to move this.
1136 	 */
1137 	term_params = dma_alloc_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE,
1138 		&term_params_dma, GFP_KERNEL);
1139 
1140 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Uploading connection "
1141 		   "port_id=%06x.\n", fcport->rdata->ids.port_id);
1142 
1143 	qed_ops->destroy_conn(qedf->cdev, fcport->handle, term_params_dma);
1144 	qed_ops->release_conn(qedf->cdev, fcport->handle);
1145 
1146 	dma_free_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE, term_params,
1147 	    term_params_dma);
1148 }
1149 
1150 static void qedf_cleanup_fcport(struct qedf_ctx *qedf,
1151 	struct qedf_rport *fcport)
1152 {
1153 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Cleaning up portid=%06x.\n",
1154 	    fcport->rdata->ids.port_id);
1155 
1156 	/* Flush any remaining i/o's before we upload the connection */
1157 	qedf_flush_active_ios(fcport, -1);
1158 
1159 	if (test_and_clear_bit(QEDF_RPORT_SESSION_READY, &fcport->flags))
1160 		qedf_upload_connection(qedf, fcport);
1161 	qedf_free_sq(qedf, fcport);
1162 	fcport->rdata = NULL;
1163 	fcport->qedf = NULL;
1164 }
1165 
1166 /**
1167  * This event_callback is called after successful completion of libfc
1168  * initiated target login. qedf can proceed with initiating the session
1169  * establishment.
1170  */
1171 static void qedf_rport_event_handler(struct fc_lport *lport,
1172 				struct fc_rport_priv *rdata,
1173 				enum fc_rport_event event)
1174 {
1175 	struct qedf_ctx *qedf = lport_priv(lport);
1176 	struct fc_rport *rport = rdata->rport;
1177 	struct fc_rport_libfc_priv *rp;
1178 	struct qedf_rport *fcport;
1179 	u32 port_id;
1180 	int rval;
1181 	unsigned long flags;
1182 
1183 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "event = %d, "
1184 		   "port_id = 0x%x\n", event, rdata->ids.port_id);
1185 
1186 	switch (event) {
1187 	case RPORT_EV_READY:
1188 		if (!rport) {
1189 			QEDF_WARN(&(qedf->dbg_ctx), "rport is NULL.\n");
1190 			break;
1191 		}
1192 
1193 		rp = rport->dd_data;
1194 		fcport = (struct qedf_rport *)&rp[1];
1195 		fcport->qedf = qedf;
1196 
1197 		if (atomic_read(&qedf->num_offloads) >= QEDF_MAX_SESSIONS) {
1198 			QEDF_ERR(&(qedf->dbg_ctx), "Not offloading "
1199 			    "portid=0x%x as max number of offloaded sessions "
1200 			    "reached.\n", rdata->ids.port_id);
1201 			return;
1202 		}
1203 
1204 		/*
1205 		 * Don't try to offload the session again. Can happen when we
1206 		 * get an ADISC
1207 		 */
1208 		if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1209 			QEDF_WARN(&(qedf->dbg_ctx), "Session already "
1210 				   "offloaded, portid=0x%x.\n",
1211 				   rdata->ids.port_id);
1212 			return;
1213 		}
1214 
1215 		if (rport->port_id == FC_FID_DIR_SERV) {
1216 			/*
1217 			 * qedf_rport structure doesn't exist for
1218 			 * directory server.
1219 			 * We should not come here, as lport will
1220 			 * take care of fabric login
1221 			 */
1222 			QEDF_WARN(&(qedf->dbg_ctx), "rport struct does not "
1223 			    "exist for dir server port_id=%x\n",
1224 			    rdata->ids.port_id);
1225 			break;
1226 		}
1227 
1228 		if (rdata->spp_type != FC_TYPE_FCP) {
1229 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1230 			    "Not offloading since spp type isn't FCP\n");
1231 			break;
1232 		}
1233 		if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
1234 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1235 			    "Not FCP target so not offloading\n");
1236 			break;
1237 		}
1238 
1239 		fcport->rdata = rdata;
1240 		fcport->rport = rport;
1241 
1242 		rval = qedf_alloc_sq(qedf, fcport);
1243 		if (rval) {
1244 			qedf_cleanup_fcport(qedf, fcport);
1245 			break;
1246 		}
1247 
1248 		/* Set device type */
1249 		if (rdata->flags & FC_RP_FLAGS_RETRY &&
1250 		    rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET &&
1251 		    !(rdata->ids.roles & FC_RPORT_ROLE_FCP_INITIATOR)) {
1252 			fcport->dev_type = QEDF_RPORT_TYPE_TAPE;
1253 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1254 			    "portid=%06x is a TAPE device.\n",
1255 			    rdata->ids.port_id);
1256 		} else {
1257 			fcport->dev_type = QEDF_RPORT_TYPE_DISK;
1258 		}
1259 
1260 		rval = qedf_offload_connection(qedf, fcport);
1261 		if (rval) {
1262 			qedf_cleanup_fcport(qedf, fcport);
1263 			break;
1264 		}
1265 
1266 		/* Add fcport to list of qedf_ctx list of offloaded ports */
1267 		spin_lock_irqsave(&qedf->hba_lock, flags);
1268 		list_add_rcu(&fcport->peers, &qedf->fcports);
1269 		spin_unlock_irqrestore(&qedf->hba_lock, flags);
1270 
1271 		/*
1272 		 * Set the session ready bit to let everyone know that this
1273 		 * connection is ready for I/O
1274 		 */
1275 		set_bit(QEDF_RPORT_SESSION_READY, &fcport->flags);
1276 		atomic_inc(&qedf->num_offloads);
1277 
1278 		break;
1279 	case RPORT_EV_LOGO:
1280 	case RPORT_EV_FAILED:
1281 	case RPORT_EV_STOP:
1282 		port_id = rdata->ids.port_id;
1283 		if (port_id == FC_FID_DIR_SERV)
1284 			break;
1285 
1286 		if (!rport) {
1287 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1288 			    "port_id=%x - rport notcreated Yet!!\n", port_id);
1289 			break;
1290 		}
1291 		rp = rport->dd_data;
1292 		/*
1293 		 * Perform session upload. Note that rdata->peers is already
1294 		 * removed from disc->rports list before we get this event.
1295 		 */
1296 		fcport = (struct qedf_rport *)&rp[1];
1297 
1298 		/* Only free this fcport if it is offloaded already */
1299 		if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1300 			set_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags);
1301 			qedf_cleanup_fcport(qedf, fcport);
1302 
1303 			/*
1304 			 * Remove fcport to list of qedf_ctx list of offloaded
1305 			 * ports
1306 			 */
1307 			spin_lock_irqsave(&qedf->hba_lock, flags);
1308 			list_del_rcu(&fcport->peers);
1309 			spin_unlock_irqrestore(&qedf->hba_lock, flags);
1310 
1311 			clear_bit(QEDF_RPORT_UPLOADING_CONNECTION,
1312 			    &fcport->flags);
1313 			atomic_dec(&qedf->num_offloads);
1314 		}
1315 
1316 		break;
1317 
1318 	case RPORT_EV_NONE:
1319 		break;
1320 	}
1321 }
1322 
1323 static void qedf_abort_io(struct fc_lport *lport)
1324 {
1325 	/* NO-OP but need to fill in the template */
1326 }
1327 
1328 static void qedf_fcp_cleanup(struct fc_lport *lport)
1329 {
1330 	/*
1331 	 * NO-OP but need to fill in template to prevent a NULL
1332 	 * function pointer dereference during link down. I/Os
1333 	 * will be flushed when port is uploaded.
1334 	 */
1335 }
1336 
1337 static struct libfc_function_template qedf_lport_template = {
1338 	.frame_send		= qedf_xmit,
1339 	.fcp_abort_io		= qedf_abort_io,
1340 	.fcp_cleanup		= qedf_fcp_cleanup,
1341 	.rport_event_callback	= qedf_rport_event_handler,
1342 	.elsct_send		= qedf_elsct_send,
1343 };
1344 
1345 static void qedf_fcoe_ctlr_setup(struct qedf_ctx *qedf)
1346 {
1347 	fcoe_ctlr_init(&qedf->ctlr, FIP_ST_AUTO);
1348 
1349 	qedf->ctlr.send = qedf_fip_send;
1350 	qedf->ctlr.update_mac = qedf_update_src_mac;
1351 	qedf->ctlr.get_src_addr = qedf_get_src_mac;
1352 	ether_addr_copy(qedf->ctlr.ctl_src_addr, qedf->mac);
1353 }
1354 
1355 static void qedf_setup_fdmi(struct qedf_ctx *qedf)
1356 {
1357 	struct fc_lport *lport = qedf->lport;
1358 	struct fc_host_attrs *fc_host = shost_to_fc_host(lport->host);
1359 	u8 buf[8];
1360 	int i, pos;
1361 
1362 	/*
1363 	 * fdmi_enabled needs to be set for libfc to execute FDMI registration.
1364 	 */
1365 	lport->fdmi_enabled = 1;
1366 
1367 	/*
1368 	 * Setup the necessary fc_host attributes to that will be used to fill
1369 	 * in the FDMI information.
1370 	 */
1371 
1372 	/* Get the PCI-e Device Serial Number Capability */
1373 	pos = pci_find_ext_capability(qedf->pdev, PCI_EXT_CAP_ID_DSN);
1374 	if (pos) {
1375 		pos += 4;
1376 		for (i = 0; i < 8; i++)
1377 			pci_read_config_byte(qedf->pdev, pos + i, &buf[i]);
1378 
1379 		snprintf(fc_host->serial_number,
1380 		    sizeof(fc_host->serial_number),
1381 		    "%02X%02X%02X%02X%02X%02X%02X%02X",
1382 		    buf[7], buf[6], buf[5], buf[4],
1383 		    buf[3], buf[2], buf[1], buf[0]);
1384 	} else
1385 		snprintf(fc_host->serial_number,
1386 		    sizeof(fc_host->serial_number), "Unknown");
1387 
1388 	snprintf(fc_host->manufacturer,
1389 	    sizeof(fc_host->manufacturer), "%s", "Cavium Inc.");
1390 
1391 	snprintf(fc_host->model, sizeof(fc_host->model), "%s", "QL41000");
1392 
1393 	snprintf(fc_host->model_description, sizeof(fc_host->model_description),
1394 	    "%s", "QLogic FastLinQ QL41000 Series 10/25/40/50GGbE Controller"
1395 	    "(FCoE)");
1396 
1397 	snprintf(fc_host->hardware_version, sizeof(fc_host->hardware_version),
1398 	    "Rev %d", qedf->pdev->revision);
1399 
1400 	snprintf(fc_host->driver_version, sizeof(fc_host->driver_version),
1401 	    "%s", QEDF_VERSION);
1402 
1403 	snprintf(fc_host->firmware_version, sizeof(fc_host->firmware_version),
1404 	    "%d.%d.%d.%d", FW_MAJOR_VERSION, FW_MINOR_VERSION,
1405 	    FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
1406 }
1407 
1408 static int qedf_lport_setup(struct qedf_ctx *qedf)
1409 {
1410 	struct fc_lport *lport = qedf->lport;
1411 
1412 	lport->link_up = 0;
1413 	lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
1414 	lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
1415 	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
1416 	    FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
1417 	lport->boot_time = jiffies;
1418 	lport->e_d_tov = 2 * 1000;
1419 	lport->r_a_tov = 10 * 1000;
1420 
1421 	/* Set NPIV support */
1422 	lport->does_npiv = 1;
1423 	fc_host_max_npiv_vports(lport->host) = QEDF_MAX_NPIV;
1424 
1425 	fc_set_wwnn(lport, qedf->wwnn);
1426 	fc_set_wwpn(lport, qedf->wwpn);
1427 
1428 	fcoe_libfc_config(lport, &qedf->ctlr, &qedf_lport_template, 0);
1429 
1430 	/* Allocate the exchange manager */
1431 	fc_exch_mgr_alloc(lport, FC_CLASS_3, qedf->max_scsi_xid + 1,
1432 	    qedf->max_els_xid, NULL);
1433 
1434 	if (fc_lport_init_stats(lport))
1435 		return -ENOMEM;
1436 
1437 	/* Finish lport config */
1438 	fc_lport_config(lport);
1439 
1440 	/* Set max frame size */
1441 	fc_set_mfs(lport, QEDF_MFS);
1442 	fc_host_maxframe_size(lport->host) = lport->mfs;
1443 
1444 	/* Set default dev_loss_tmo based on module parameter */
1445 	fc_host_dev_loss_tmo(lport->host) = qedf_dev_loss_tmo;
1446 
1447 	/* Set symbolic node name */
1448 	snprintf(fc_host_symbolic_name(lport->host), 256,
1449 	    "QLogic %s v%s", QEDF_MODULE_NAME, QEDF_VERSION);
1450 
1451 	qedf_setup_fdmi(qedf);
1452 
1453 	return 0;
1454 }
1455 
1456 /*
1457  * NPIV functions
1458  */
1459 
1460 static int qedf_vport_libfc_config(struct fc_vport *vport,
1461 	struct fc_lport *lport)
1462 {
1463 	lport->link_up = 0;
1464 	lport->qfull = 0;
1465 	lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
1466 	lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
1467 	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
1468 	    FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
1469 	lport->boot_time = jiffies;
1470 	lport->e_d_tov = 2 * 1000;
1471 	lport->r_a_tov = 10 * 1000;
1472 	lport->does_npiv = 1; /* Temporary until we add NPIV support */
1473 
1474 	/* Allocate stats for vport */
1475 	if (fc_lport_init_stats(lport))
1476 		return -ENOMEM;
1477 
1478 	/* Finish lport config */
1479 	fc_lport_config(lport);
1480 
1481 	/* offload related configuration */
1482 	lport->crc_offload = 0;
1483 	lport->seq_offload = 0;
1484 	lport->lro_enabled = 0;
1485 	lport->lro_xid = 0;
1486 	lport->lso_max = 0;
1487 
1488 	return 0;
1489 }
1490 
1491 static int qedf_vport_create(struct fc_vport *vport, bool disabled)
1492 {
1493 	struct Scsi_Host *shost = vport_to_shost(vport);
1494 	struct fc_lport *n_port = shost_priv(shost);
1495 	struct fc_lport *vn_port;
1496 	struct qedf_ctx *base_qedf = lport_priv(n_port);
1497 	struct qedf_ctx *vport_qedf;
1498 
1499 	char buf[32];
1500 	int rc = 0;
1501 
1502 	rc = fcoe_validate_vport_create(vport);
1503 	if (rc) {
1504 		fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1505 		QEDF_WARN(&(base_qedf->dbg_ctx), "Failed to create vport, "
1506 			   "WWPN (0x%s) already exists.\n", buf);
1507 		goto err1;
1508 	}
1509 
1510 	if (atomic_read(&base_qedf->link_state) != QEDF_LINK_UP) {
1511 		QEDF_WARN(&(base_qedf->dbg_ctx), "Cannot create vport "
1512 			   "because link is not up.\n");
1513 		rc = -EIO;
1514 		goto err1;
1515 	}
1516 
1517 	vn_port = libfc_vport_create(vport, sizeof(struct qedf_ctx));
1518 	if (!vn_port) {
1519 		QEDF_WARN(&(base_qedf->dbg_ctx), "Could not create lport "
1520 			   "for vport.\n");
1521 		rc = -ENOMEM;
1522 		goto err1;
1523 	}
1524 
1525 	fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1526 	QEDF_ERR(&(base_qedf->dbg_ctx), "Creating NPIV port, WWPN=%s.\n",
1527 	    buf);
1528 
1529 	/* Copy some fields from base_qedf */
1530 	vport_qedf = lport_priv(vn_port);
1531 	memcpy(vport_qedf, base_qedf, sizeof(struct qedf_ctx));
1532 
1533 	/* Set qedf data specific to this vport */
1534 	vport_qedf->lport = vn_port;
1535 	/* Use same hba_lock as base_qedf */
1536 	vport_qedf->hba_lock = base_qedf->hba_lock;
1537 	vport_qedf->pdev = base_qedf->pdev;
1538 	vport_qedf->cmd_mgr = base_qedf->cmd_mgr;
1539 	init_completion(&vport_qedf->flogi_compl);
1540 	INIT_LIST_HEAD(&vport_qedf->fcports);
1541 
1542 	rc = qedf_vport_libfc_config(vport, vn_port);
1543 	if (rc) {
1544 		QEDF_ERR(&(base_qedf->dbg_ctx), "Could not allocate memory "
1545 		    "for lport stats.\n");
1546 		goto err2;
1547 	}
1548 
1549 	fc_set_wwnn(vn_port, vport->node_name);
1550 	fc_set_wwpn(vn_port, vport->port_name);
1551 	vport_qedf->wwnn = vn_port->wwnn;
1552 	vport_qedf->wwpn = vn_port->wwpn;
1553 
1554 	vn_port->host->transportt = qedf_fc_vport_transport_template;
1555 	vn_port->host->can_queue = QEDF_MAX_ELS_XID;
1556 	vn_port->host->max_lun = qedf_max_lun;
1557 	vn_port->host->sg_tablesize = QEDF_MAX_BDS_PER_CMD;
1558 	vn_port->host->max_cmd_len = QEDF_MAX_CDB_LEN;
1559 
1560 	rc = scsi_add_host(vn_port->host, &vport->dev);
1561 	if (rc) {
1562 		QEDF_WARN(&(base_qedf->dbg_ctx), "Error adding Scsi_Host.\n");
1563 		goto err2;
1564 	}
1565 
1566 	/* Set default dev_loss_tmo based on module parameter */
1567 	fc_host_dev_loss_tmo(vn_port->host) = qedf_dev_loss_tmo;
1568 
1569 	/* Init libfc stuffs */
1570 	memcpy(&vn_port->tt, &qedf_lport_template,
1571 		sizeof(qedf_lport_template));
1572 	fc_exch_init(vn_port);
1573 	fc_elsct_init(vn_port);
1574 	fc_lport_init(vn_port);
1575 	fc_disc_init(vn_port);
1576 	fc_disc_config(vn_port, vn_port);
1577 
1578 
1579 	/* Allocate the exchange manager */
1580 	shost = vport_to_shost(vport);
1581 	n_port = shost_priv(shost);
1582 	fc_exch_mgr_list_clone(n_port, vn_port);
1583 
1584 	/* Set max frame size */
1585 	fc_set_mfs(vn_port, QEDF_MFS);
1586 
1587 	fc_host_port_type(vn_port->host) = FC_PORTTYPE_UNKNOWN;
1588 
1589 	if (disabled) {
1590 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
1591 	} else {
1592 		vn_port->boot_time = jiffies;
1593 		fc_fabric_login(vn_port);
1594 		fc_vport_setlink(vn_port);
1595 	}
1596 
1597 	QEDF_INFO(&(base_qedf->dbg_ctx), QEDF_LOG_NPIV, "vn_port=%p.\n",
1598 		   vn_port);
1599 
1600 	/* Set up debug context for vport */
1601 	vport_qedf->dbg_ctx.host_no = vn_port->host->host_no;
1602 	vport_qedf->dbg_ctx.pdev = base_qedf->pdev;
1603 
1604 err2:
1605 	scsi_host_put(vn_port->host);
1606 err1:
1607 	return rc;
1608 }
1609 
1610 static int qedf_vport_destroy(struct fc_vport *vport)
1611 {
1612 	struct Scsi_Host *shost = vport_to_shost(vport);
1613 	struct fc_lport *n_port = shost_priv(shost);
1614 	struct fc_lport *vn_port = vport->dd_data;
1615 
1616 	mutex_lock(&n_port->lp_mutex);
1617 	list_del(&vn_port->list);
1618 	mutex_unlock(&n_port->lp_mutex);
1619 
1620 	fc_fabric_logoff(vn_port);
1621 	fc_lport_destroy(vn_port);
1622 
1623 	/* Detach from scsi-ml */
1624 	fc_remove_host(vn_port->host);
1625 	scsi_remove_host(vn_port->host);
1626 
1627 	/*
1628 	 * Only try to release the exchange manager if the vn_port
1629 	 * configuration is complete.
1630 	 */
1631 	if (vn_port->state == LPORT_ST_READY)
1632 		fc_exch_mgr_free(vn_port);
1633 
1634 	/* Free memory used by statistical counters */
1635 	fc_lport_free_stats(vn_port);
1636 
1637 	/* Release Scsi_Host */
1638 	if (vn_port->host)
1639 		scsi_host_put(vn_port->host);
1640 
1641 	return 0;
1642 }
1643 
1644 static int qedf_vport_disable(struct fc_vport *vport, bool disable)
1645 {
1646 	struct fc_lport *lport = vport->dd_data;
1647 
1648 	if (disable) {
1649 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
1650 		fc_fabric_logoff(lport);
1651 	} else {
1652 		lport->boot_time = jiffies;
1653 		fc_fabric_login(lport);
1654 		fc_vport_setlink(lport);
1655 	}
1656 	return 0;
1657 }
1658 
1659 /*
1660  * During removal we need to wait for all the vports associated with a port
1661  * to be destroyed so we avoid a race condition where libfc is still trying
1662  * to reap vports while the driver remove function has already reaped the
1663  * driver contexts associated with the physical port.
1664  */
1665 static void qedf_wait_for_vport_destroy(struct qedf_ctx *qedf)
1666 {
1667 	struct fc_host_attrs *fc_host = shost_to_fc_host(qedf->lport->host);
1668 
1669 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
1670 	    "Entered.\n");
1671 	while (fc_host->npiv_vports_inuse > 0) {
1672 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
1673 		    "Waiting for all vports to be reaped.\n");
1674 		msleep(1000);
1675 	}
1676 }
1677 
1678 /**
1679  * qedf_fcoe_reset - Resets the fcoe
1680  *
1681  * @shost: shost the reset is from
1682  *
1683  * Returns: always 0
1684  */
1685 static int qedf_fcoe_reset(struct Scsi_Host *shost)
1686 {
1687 	struct fc_lport *lport = shost_priv(shost);
1688 
1689 	qedf_ctx_soft_reset(lport);
1690 	return 0;
1691 }
1692 
1693 static struct fc_host_statistics *qedf_fc_get_host_stats(struct Scsi_Host
1694 	*shost)
1695 {
1696 	struct fc_host_statistics *qedf_stats;
1697 	struct fc_lport *lport = shost_priv(shost);
1698 	struct qedf_ctx *qedf = lport_priv(lport);
1699 	struct qed_fcoe_stats *fw_fcoe_stats;
1700 
1701 	qedf_stats = fc_get_host_stats(shost);
1702 
1703 	/* We don't collect offload stats for specific NPIV ports */
1704 	if (lport->vport)
1705 		goto out;
1706 
1707 	fw_fcoe_stats = kmalloc(sizeof(struct qed_fcoe_stats), GFP_KERNEL);
1708 	if (!fw_fcoe_stats) {
1709 		QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate memory for "
1710 		    "fw_fcoe_stats.\n");
1711 		goto out;
1712 	}
1713 
1714 	/* Query firmware for offload stats */
1715 	qed_ops->get_stats(qedf->cdev, fw_fcoe_stats);
1716 
1717 	/*
1718 	 * The expectation is that we add our offload stats to the stats
1719 	 * being maintained by libfc each time the fc_get_host_status callback
1720 	 * is invoked. The additions are not carried over for each call to
1721 	 * the fc_get_host_stats callback.
1722 	 */
1723 	qedf_stats->tx_frames += fw_fcoe_stats->fcoe_tx_data_pkt_cnt +
1724 	    fw_fcoe_stats->fcoe_tx_xfer_pkt_cnt +
1725 	    fw_fcoe_stats->fcoe_tx_other_pkt_cnt;
1726 	qedf_stats->rx_frames += fw_fcoe_stats->fcoe_rx_data_pkt_cnt +
1727 	    fw_fcoe_stats->fcoe_rx_xfer_pkt_cnt +
1728 	    fw_fcoe_stats->fcoe_rx_other_pkt_cnt;
1729 	qedf_stats->fcp_input_megabytes +=
1730 	    do_div(fw_fcoe_stats->fcoe_rx_byte_cnt, 1000000);
1731 	qedf_stats->fcp_output_megabytes +=
1732 	    do_div(fw_fcoe_stats->fcoe_tx_byte_cnt, 1000000);
1733 	qedf_stats->rx_words += fw_fcoe_stats->fcoe_rx_byte_cnt / 4;
1734 	qedf_stats->tx_words += fw_fcoe_stats->fcoe_tx_byte_cnt / 4;
1735 	qedf_stats->invalid_crc_count +=
1736 	    fw_fcoe_stats->fcoe_silent_drop_pkt_crc_error_cnt;
1737 	qedf_stats->dumped_frames =
1738 	    fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
1739 	qedf_stats->error_frames +=
1740 	    fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
1741 	qedf_stats->fcp_input_requests += qedf->input_requests;
1742 	qedf_stats->fcp_output_requests += qedf->output_requests;
1743 	qedf_stats->fcp_control_requests += qedf->control_requests;
1744 	qedf_stats->fcp_packet_aborts += qedf->packet_aborts;
1745 	qedf_stats->fcp_frame_alloc_failures += qedf->alloc_failures;
1746 
1747 	kfree(fw_fcoe_stats);
1748 out:
1749 	return qedf_stats;
1750 }
1751 
1752 static struct fc_function_template qedf_fc_transport_fn = {
1753 	.show_host_node_name = 1,
1754 	.show_host_port_name = 1,
1755 	.show_host_supported_classes = 1,
1756 	.show_host_supported_fc4s = 1,
1757 	.show_host_active_fc4s = 1,
1758 	.show_host_maxframe_size = 1,
1759 
1760 	.show_host_port_id = 1,
1761 	.show_host_supported_speeds = 1,
1762 	.get_host_speed = fc_get_host_speed,
1763 	.show_host_speed = 1,
1764 	.show_host_port_type = 1,
1765 	.get_host_port_state = fc_get_host_port_state,
1766 	.show_host_port_state = 1,
1767 	.show_host_symbolic_name = 1,
1768 
1769 	/*
1770 	 * Tell FC transport to allocate enough space to store the backpointer
1771 	 * for the associate qedf_rport struct.
1772 	 */
1773 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
1774 				sizeof(struct qedf_rport)),
1775 	.show_rport_maxframe_size = 1,
1776 	.show_rport_supported_classes = 1,
1777 	.show_host_fabric_name = 1,
1778 	.show_starget_node_name = 1,
1779 	.show_starget_port_name = 1,
1780 	.show_starget_port_id = 1,
1781 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
1782 	.show_rport_dev_loss_tmo = 1,
1783 	.get_fc_host_stats = qedf_fc_get_host_stats,
1784 	.issue_fc_host_lip = qedf_fcoe_reset,
1785 	.vport_create = qedf_vport_create,
1786 	.vport_delete = qedf_vport_destroy,
1787 	.vport_disable = qedf_vport_disable,
1788 	.bsg_request = fc_lport_bsg_request,
1789 };
1790 
1791 static struct fc_function_template qedf_fc_vport_transport_fn = {
1792 	.show_host_node_name = 1,
1793 	.show_host_port_name = 1,
1794 	.show_host_supported_classes = 1,
1795 	.show_host_supported_fc4s = 1,
1796 	.show_host_active_fc4s = 1,
1797 	.show_host_maxframe_size = 1,
1798 	.show_host_port_id = 1,
1799 	.show_host_supported_speeds = 1,
1800 	.get_host_speed = fc_get_host_speed,
1801 	.show_host_speed = 1,
1802 	.show_host_port_type = 1,
1803 	.get_host_port_state = fc_get_host_port_state,
1804 	.show_host_port_state = 1,
1805 	.show_host_symbolic_name = 1,
1806 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
1807 				sizeof(struct qedf_rport)),
1808 	.show_rport_maxframe_size = 1,
1809 	.show_rport_supported_classes = 1,
1810 	.show_host_fabric_name = 1,
1811 	.show_starget_node_name = 1,
1812 	.show_starget_port_name = 1,
1813 	.show_starget_port_id = 1,
1814 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
1815 	.show_rport_dev_loss_tmo = 1,
1816 	.get_fc_host_stats = fc_get_host_stats,
1817 	.issue_fc_host_lip = qedf_fcoe_reset,
1818 	.bsg_request = fc_lport_bsg_request,
1819 };
1820 
1821 static bool qedf_fp_has_work(struct qedf_fastpath *fp)
1822 {
1823 	struct qedf_ctx *qedf = fp->qedf;
1824 	struct global_queue *que;
1825 	struct qed_sb_info *sb_info = fp->sb_info;
1826 	struct status_block *sb = sb_info->sb_virt;
1827 	u16 prod_idx;
1828 
1829 	/* Get the pointer to the global CQ this completion is on */
1830 	que = qedf->global_queues[fp->sb_id];
1831 
1832 	/* Be sure all responses have been written to PI */
1833 	rmb();
1834 
1835 	/* Get the current firmware producer index */
1836 	prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
1837 
1838 	return (que->cq_prod_idx != prod_idx);
1839 }
1840 
1841 /*
1842  * Interrupt handler code.
1843  */
1844 
1845 /* Process completion queue and copy CQE contents for deferred processesing
1846  *
1847  * Return true if we should wake the I/O thread, false if not.
1848  */
1849 static bool qedf_process_completions(struct qedf_fastpath *fp)
1850 {
1851 	struct qedf_ctx *qedf = fp->qedf;
1852 	struct qed_sb_info *sb_info = fp->sb_info;
1853 	struct status_block *sb = sb_info->sb_virt;
1854 	struct global_queue *que;
1855 	u16 prod_idx;
1856 	struct fcoe_cqe *cqe;
1857 	struct qedf_io_work *io_work;
1858 	int num_handled = 0;
1859 	unsigned int cpu;
1860 	struct qedf_ioreq *io_req = NULL;
1861 	u16 xid;
1862 	u16 new_cqes;
1863 	u32 comp_type;
1864 
1865 	/* Get the current firmware producer index */
1866 	prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
1867 
1868 	/* Get the pointer to the global CQ this completion is on */
1869 	que = qedf->global_queues[fp->sb_id];
1870 
1871 	/* Calculate the amount of new elements since last processing */
1872 	new_cqes = (prod_idx >= que->cq_prod_idx) ?
1873 	    (prod_idx - que->cq_prod_idx) :
1874 	    0x10000 - que->cq_prod_idx + prod_idx;
1875 
1876 	/* Save producer index */
1877 	que->cq_prod_idx = prod_idx;
1878 
1879 	while (new_cqes) {
1880 		fp->completions++;
1881 		num_handled++;
1882 		cqe = &que->cq[que->cq_cons_idx];
1883 
1884 		comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
1885 		    FCOE_CQE_CQE_TYPE_MASK;
1886 
1887 		/*
1888 		 * Process unsolicited CQEs directly in the interrupt handler
1889 		 * sine we need the fastpath ID
1890 		 */
1891 		if (comp_type == FCOE_UNSOLIC_CQE_TYPE) {
1892 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
1893 			   "Unsolicated CQE.\n");
1894 			qedf_process_unsol_compl(qedf, fp->sb_id, cqe);
1895 			/*
1896 			 * Don't add a work list item.  Increment consumer
1897 			 * consumer index and move on.
1898 			 */
1899 			goto inc_idx;
1900 		}
1901 
1902 		xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
1903 		io_req = &qedf->cmd_mgr->cmds[xid];
1904 
1905 		/*
1906 		 * Figure out which percpu thread we should queue this I/O
1907 		 * on.
1908 		 */
1909 		if (!io_req)
1910 			/* If there is not io_req assocated with this CQE
1911 			 * just queue it on CPU 0
1912 			 */
1913 			cpu = 0;
1914 		else {
1915 			cpu = io_req->cpu;
1916 			io_req->int_cpu = smp_processor_id();
1917 		}
1918 
1919 		io_work = mempool_alloc(qedf->io_mempool, GFP_ATOMIC);
1920 		if (!io_work) {
1921 			QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate "
1922 				   "work for I/O completion.\n");
1923 			continue;
1924 		}
1925 		memset(io_work, 0, sizeof(struct qedf_io_work));
1926 
1927 		INIT_WORK(&io_work->work, qedf_fp_io_handler);
1928 
1929 		/* Copy contents of CQE for deferred processing */
1930 		memcpy(&io_work->cqe, cqe, sizeof(struct fcoe_cqe));
1931 
1932 		io_work->qedf = fp->qedf;
1933 		io_work->fp = NULL; /* Only used for unsolicited frames */
1934 
1935 		queue_work_on(cpu, qedf_io_wq, &io_work->work);
1936 
1937 inc_idx:
1938 		que->cq_cons_idx++;
1939 		if (que->cq_cons_idx == fp->cq_num_entries)
1940 			que->cq_cons_idx = 0;
1941 		new_cqes--;
1942 	}
1943 
1944 	return true;
1945 }
1946 
1947 
1948 /* MSI-X fastpath handler code */
1949 static irqreturn_t qedf_msix_handler(int irq, void *dev_id)
1950 {
1951 	struct qedf_fastpath *fp = dev_id;
1952 
1953 	if (!fp) {
1954 		QEDF_ERR(NULL, "fp is null.\n");
1955 		return IRQ_HANDLED;
1956 	}
1957 	if (!fp->sb_info) {
1958 		QEDF_ERR(NULL, "fp->sb_info in null.");
1959 		return IRQ_HANDLED;
1960 	}
1961 
1962 	/*
1963 	 * Disable interrupts for this status block while we process new
1964 	 * completions
1965 	 */
1966 	qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);
1967 
1968 	while (1) {
1969 		qedf_process_completions(fp);
1970 
1971 		if (qedf_fp_has_work(fp) == 0) {
1972 			/* Update the sb information */
1973 			qed_sb_update_sb_idx(fp->sb_info);
1974 
1975 			/* Check for more work */
1976 			rmb();
1977 
1978 			if (qedf_fp_has_work(fp) == 0) {
1979 				/* Re-enable interrupts */
1980 				qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
1981 				return IRQ_HANDLED;
1982 			}
1983 		}
1984 	}
1985 
1986 	/* Do we ever want to break out of above loop? */
1987 	return IRQ_HANDLED;
1988 }
1989 
1990 /* simd handler for MSI/INTa */
1991 static void qedf_simd_int_handler(void *cookie)
1992 {
1993 	/* Cookie is qedf_ctx struct */
1994 	struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
1995 
1996 	QEDF_WARN(&(qedf->dbg_ctx), "qedf=%p.\n", qedf);
1997 }
1998 
1999 #define QEDF_SIMD_HANDLER_NUM		0
2000 static void qedf_sync_free_irqs(struct qedf_ctx *qedf)
2001 {
2002 	int i;
2003 
2004 	if (qedf->int_info.msix_cnt) {
2005 		for (i = 0; i < qedf->int_info.used_cnt; i++) {
2006 			synchronize_irq(qedf->int_info.msix[i].vector);
2007 			irq_set_affinity_hint(qedf->int_info.msix[i].vector,
2008 			    NULL);
2009 			irq_set_affinity_notifier(qedf->int_info.msix[i].vector,
2010 			    NULL);
2011 			free_irq(qedf->int_info.msix[i].vector,
2012 			    &qedf->fp_array[i]);
2013 		}
2014 	} else
2015 		qed_ops->common->simd_handler_clean(qedf->cdev,
2016 		    QEDF_SIMD_HANDLER_NUM);
2017 
2018 	qedf->int_info.used_cnt = 0;
2019 	qed_ops->common->set_fp_int(qedf->cdev, 0);
2020 }
2021 
2022 static int qedf_request_msix_irq(struct qedf_ctx *qedf)
2023 {
2024 	int i, rc, cpu;
2025 
2026 	cpu = cpumask_first(cpu_online_mask);
2027 	for (i = 0; i < qedf->num_queues; i++) {
2028 		rc = request_irq(qedf->int_info.msix[i].vector,
2029 		    qedf_msix_handler, 0, "qedf", &qedf->fp_array[i]);
2030 
2031 		if (rc) {
2032 			QEDF_WARN(&(qedf->dbg_ctx), "request_irq failed.\n");
2033 			qedf_sync_free_irqs(qedf);
2034 			return rc;
2035 		}
2036 
2037 		qedf->int_info.used_cnt++;
2038 		rc = irq_set_affinity_hint(qedf->int_info.msix[i].vector,
2039 		    get_cpu_mask(cpu));
2040 		cpu = cpumask_next(cpu, cpu_online_mask);
2041 	}
2042 
2043 	return 0;
2044 }
2045 
2046 static int qedf_setup_int(struct qedf_ctx *qedf)
2047 {
2048 	int rc = 0;
2049 
2050 	/*
2051 	 * Learn interrupt configuration
2052 	 */
2053 	rc = qed_ops->common->set_fp_int(qedf->cdev, num_online_cpus());
2054 	if (rc <= 0)
2055 		return 0;
2056 
2057 	rc  = qed_ops->common->get_fp_int(qedf->cdev, &qedf->int_info);
2058 	if (rc)
2059 		return 0;
2060 
2061 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of msix_cnt = "
2062 		   "0x%x num of cpus = 0x%x\n", qedf->int_info.msix_cnt,
2063 		   num_online_cpus());
2064 
2065 	if (qedf->int_info.msix_cnt)
2066 		return qedf_request_msix_irq(qedf);
2067 
2068 	qed_ops->common->simd_handler_config(qedf->cdev, &qedf,
2069 	    QEDF_SIMD_HANDLER_NUM, qedf_simd_int_handler);
2070 	qedf->int_info.used_cnt = 1;
2071 
2072 	return 0;
2073 }
2074 
2075 /* Main function for libfc frame reception */
2076 static void qedf_recv_frame(struct qedf_ctx *qedf,
2077 	struct sk_buff *skb)
2078 {
2079 	u32 fr_len;
2080 	struct fc_lport *lport;
2081 	struct fc_frame_header *fh;
2082 	struct fcoe_crc_eof crc_eof;
2083 	struct fc_frame *fp;
2084 	u8 *mac = NULL;
2085 	u8 *dest_mac = NULL;
2086 	struct fcoe_hdr *hp;
2087 	struct qedf_rport *fcport;
2088 	struct fc_lport *vn_port;
2089 	u32 f_ctl;
2090 
2091 	lport = qedf->lport;
2092 	if (lport == NULL || lport->state == LPORT_ST_DISABLED) {
2093 		QEDF_WARN(NULL, "Invalid lport struct or lport disabled.\n");
2094 		kfree_skb(skb);
2095 		return;
2096 	}
2097 
2098 	if (skb_is_nonlinear(skb))
2099 		skb_linearize(skb);
2100 	mac = eth_hdr(skb)->h_source;
2101 	dest_mac = eth_hdr(skb)->h_dest;
2102 
2103 	/* Pull the header */
2104 	hp = (struct fcoe_hdr *)skb->data;
2105 	fh = (struct fc_frame_header *) skb_transport_header(skb);
2106 	skb_pull(skb, sizeof(struct fcoe_hdr));
2107 	fr_len = skb->len - sizeof(struct fcoe_crc_eof);
2108 
2109 	fp = (struct fc_frame *)skb;
2110 	fc_frame_init(fp);
2111 	fr_dev(fp) = lport;
2112 	fr_sof(fp) = hp->fcoe_sof;
2113 	if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
2114 		kfree_skb(skb);
2115 		return;
2116 	}
2117 	fr_eof(fp) = crc_eof.fcoe_eof;
2118 	fr_crc(fp) = crc_eof.fcoe_crc32;
2119 	if (pskb_trim(skb, fr_len)) {
2120 		kfree_skb(skb);
2121 		return;
2122 	}
2123 
2124 	fh = fc_frame_header_get(fp);
2125 
2126 	/*
2127 	 * Invalid frame filters.
2128 	 */
2129 
2130 	if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
2131 	    fh->fh_type == FC_TYPE_FCP) {
2132 		/* Drop FCP data. We dont this in L2 path */
2133 		kfree_skb(skb);
2134 		return;
2135 	}
2136 	if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
2137 	    fh->fh_type == FC_TYPE_ELS) {
2138 		switch (fc_frame_payload_op(fp)) {
2139 		case ELS_LOGO:
2140 			if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
2141 				/* drop non-FIP LOGO */
2142 				kfree_skb(skb);
2143 				return;
2144 			}
2145 			break;
2146 		}
2147 	}
2148 
2149 	if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
2150 		/* Drop incoming ABTS */
2151 		kfree_skb(skb);
2152 		return;
2153 	}
2154 
2155 	if (ntoh24(&dest_mac[3]) != ntoh24(fh->fh_d_id)) {
2156 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2157 		    "FC frame d_id mismatch with MAC %pM.\n", dest_mac);
2158 		return;
2159 	}
2160 
2161 	if (qedf->ctlr.state) {
2162 		if (!ether_addr_equal(mac, qedf->ctlr.dest_addr)) {
2163 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2164 			    "Wrong source address: mac:%pM dest_addr:%pM.\n",
2165 			    mac, qedf->ctlr.dest_addr);
2166 			kfree_skb(skb);
2167 			return;
2168 		}
2169 	}
2170 
2171 	vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
2172 
2173 	/*
2174 	 * If the destination ID from the frame header does not match what we
2175 	 * have on record for lport and the search for a NPIV port came up
2176 	 * empty then this is not addressed to our port so simply drop it.
2177 	 */
2178 	if (lport->port_id != ntoh24(fh->fh_d_id) && !vn_port) {
2179 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2180 		    "Dropping frame due to destination mismatch: lport->port_id=%x fh->d_id=%x.\n",
2181 		    lport->port_id, ntoh24(fh->fh_d_id));
2182 		kfree_skb(skb);
2183 		return;
2184 	}
2185 
2186 	f_ctl = ntoh24(fh->fh_f_ctl);
2187 	if ((fh->fh_type == FC_TYPE_BLS) && (f_ctl & FC_FC_SEQ_CTX) &&
2188 	    (f_ctl & FC_FC_EX_CTX)) {
2189 		/* Drop incoming ABTS response that has both SEQ/EX CTX set */
2190 		kfree_skb(skb);
2191 		return;
2192 	}
2193 
2194 	/*
2195 	 * If a connection is uploading, drop incoming FCoE frames as there
2196 	 * is a small window where we could try to return a frame while libfc
2197 	 * is trying to clean things up.
2198 	 */
2199 
2200 	/* Get fcport associated with d_id if it exists */
2201 	fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
2202 
2203 	if (fcport && test_bit(QEDF_RPORT_UPLOADING_CONNECTION,
2204 	    &fcport->flags)) {
2205 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2206 		    "Connection uploading, dropping fp=%p.\n", fp);
2207 		kfree_skb(skb);
2208 		return;
2209 	}
2210 
2211 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame receive: "
2212 	    "skb=%p fp=%p src=%06x dest=%06x r_ctl=%x fh_type=%x.\n", skb, fp,
2213 	    ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl,
2214 	    fh->fh_type);
2215 	if (qedf_dump_frames)
2216 		print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
2217 		    1, skb->data, skb->len, false);
2218 	fc_exch_recv(lport, fp);
2219 }
2220 
2221 static void qedf_ll2_process_skb(struct work_struct *work)
2222 {
2223 	struct qedf_skb_work *skb_work =
2224 	    container_of(work, struct qedf_skb_work, work);
2225 	struct qedf_ctx *qedf = skb_work->qedf;
2226 	struct sk_buff *skb = skb_work->skb;
2227 	struct ethhdr *eh;
2228 
2229 	if (!qedf) {
2230 		QEDF_ERR(NULL, "qedf is NULL\n");
2231 		goto err_out;
2232 	}
2233 
2234 	eh = (struct ethhdr *)skb->data;
2235 
2236 	/* Undo VLAN encapsulation */
2237 	if (eh->h_proto == htons(ETH_P_8021Q)) {
2238 		memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
2239 		eh = skb_pull(skb, VLAN_HLEN);
2240 		skb_reset_mac_header(skb);
2241 	}
2242 
2243 	/*
2244 	 * Process either a FIP frame or FCoE frame based on the
2245 	 * protocol value.  If it's not either just drop the
2246 	 * frame.
2247 	 */
2248 	if (eh->h_proto == htons(ETH_P_FIP)) {
2249 		qedf_fip_recv(qedf, skb);
2250 		goto out;
2251 	} else if (eh->h_proto == htons(ETH_P_FCOE)) {
2252 		__skb_pull(skb, ETH_HLEN);
2253 		qedf_recv_frame(qedf, skb);
2254 		goto out;
2255 	} else
2256 		goto err_out;
2257 
2258 err_out:
2259 	kfree_skb(skb);
2260 out:
2261 	kfree(skb_work);
2262 	return;
2263 }
2264 
2265 static int qedf_ll2_rx(void *cookie, struct sk_buff *skb,
2266 	u32 arg1, u32 arg2)
2267 {
2268 	struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
2269 	struct qedf_skb_work *skb_work;
2270 
2271 	skb_work = kzalloc(sizeof(struct qedf_skb_work), GFP_ATOMIC);
2272 	if (!skb_work) {
2273 		QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate skb_work so "
2274 			   "dropping frame.\n");
2275 		kfree_skb(skb);
2276 		return 0;
2277 	}
2278 
2279 	INIT_WORK(&skb_work->work, qedf_ll2_process_skb);
2280 	skb_work->skb = skb;
2281 	skb_work->qedf = qedf;
2282 	queue_work(qedf->ll2_recv_wq, &skb_work->work);
2283 
2284 	return 0;
2285 }
2286 
2287 static struct qed_ll2_cb_ops qedf_ll2_cb_ops = {
2288 	.rx_cb = qedf_ll2_rx,
2289 	.tx_cb = NULL,
2290 };
2291 
2292 /* Main thread to process I/O completions */
2293 void qedf_fp_io_handler(struct work_struct *work)
2294 {
2295 	struct qedf_io_work *io_work =
2296 	    container_of(work, struct qedf_io_work, work);
2297 	u32 comp_type;
2298 
2299 	/*
2300 	 * Deferred part of unsolicited CQE sends
2301 	 * frame to libfc.
2302 	 */
2303 	comp_type = (io_work->cqe.cqe_data >>
2304 	    FCOE_CQE_CQE_TYPE_SHIFT) &
2305 	    FCOE_CQE_CQE_TYPE_MASK;
2306 	if (comp_type == FCOE_UNSOLIC_CQE_TYPE &&
2307 	    io_work->fp)
2308 		fc_exch_recv(io_work->qedf->lport, io_work->fp);
2309 	else
2310 		qedf_process_cqe(io_work->qedf, &io_work->cqe);
2311 
2312 	kfree(io_work);
2313 }
2314 
2315 static int qedf_alloc_and_init_sb(struct qedf_ctx *qedf,
2316 	struct qed_sb_info *sb_info, u16 sb_id)
2317 {
2318 	struct status_block *sb_virt;
2319 	dma_addr_t sb_phys;
2320 	int ret;
2321 
2322 	sb_virt = dma_alloc_coherent(&qedf->pdev->dev,
2323 	    sizeof(struct status_block), &sb_phys, GFP_KERNEL);
2324 
2325 	if (!sb_virt) {
2326 		QEDF_ERR(&(qedf->dbg_ctx), "Status block allocation failed "
2327 			  "for id = %d.\n", sb_id);
2328 		return -ENOMEM;
2329 	}
2330 
2331 	ret = qed_ops->common->sb_init(qedf->cdev, sb_info, sb_virt, sb_phys,
2332 	    sb_id, QED_SB_TYPE_STORAGE);
2333 
2334 	if (ret) {
2335 		QEDF_ERR(&(qedf->dbg_ctx), "Status block initialization "
2336 			  "failed for id = %d.\n", sb_id);
2337 		return ret;
2338 	}
2339 
2340 	return 0;
2341 }
2342 
2343 static void qedf_free_sb(struct qedf_ctx *qedf, struct qed_sb_info *sb_info)
2344 {
2345 	if (sb_info->sb_virt)
2346 		dma_free_coherent(&qedf->pdev->dev, sizeof(*sb_info->sb_virt),
2347 		    (void *)sb_info->sb_virt, sb_info->sb_phys);
2348 }
2349 
2350 static void qedf_destroy_sb(struct qedf_ctx *qedf)
2351 {
2352 	int id;
2353 	struct qedf_fastpath *fp = NULL;
2354 
2355 	for (id = 0; id < qedf->num_queues; id++) {
2356 		fp = &(qedf->fp_array[id]);
2357 		if (fp->sb_id == QEDF_SB_ID_NULL)
2358 			break;
2359 		qedf_free_sb(qedf, fp->sb_info);
2360 		kfree(fp->sb_info);
2361 	}
2362 	kfree(qedf->fp_array);
2363 }
2364 
2365 static int qedf_prepare_sb(struct qedf_ctx *qedf)
2366 {
2367 	int id;
2368 	struct qedf_fastpath *fp;
2369 	int ret;
2370 
2371 	qedf->fp_array =
2372 	    kcalloc(qedf->num_queues, sizeof(struct qedf_fastpath),
2373 		GFP_KERNEL);
2374 
2375 	if (!qedf->fp_array) {
2376 		QEDF_ERR(&(qedf->dbg_ctx), "fastpath array allocation "
2377 			  "failed.\n");
2378 		return -ENOMEM;
2379 	}
2380 
2381 	for (id = 0; id < qedf->num_queues; id++) {
2382 		fp = &(qedf->fp_array[id]);
2383 		fp->sb_id = QEDF_SB_ID_NULL;
2384 		fp->sb_info = kcalloc(1, sizeof(*fp->sb_info), GFP_KERNEL);
2385 		if (!fp->sb_info) {
2386 			QEDF_ERR(&(qedf->dbg_ctx), "SB info struct "
2387 				  "allocation failed.\n");
2388 			goto err;
2389 		}
2390 		ret = qedf_alloc_and_init_sb(qedf, fp->sb_info, id);
2391 		if (ret) {
2392 			QEDF_ERR(&(qedf->dbg_ctx), "SB allocation and "
2393 				  "initialization failed.\n");
2394 			goto err;
2395 		}
2396 		fp->sb_id = id;
2397 		fp->qedf = qedf;
2398 		fp->cq_num_entries =
2399 		    qedf->global_queues[id]->cq_mem_size /
2400 		    sizeof(struct fcoe_cqe);
2401 	}
2402 err:
2403 	return 0;
2404 }
2405 
2406 void qedf_process_cqe(struct qedf_ctx *qedf, struct fcoe_cqe *cqe)
2407 {
2408 	u16 xid;
2409 	struct qedf_ioreq *io_req;
2410 	struct qedf_rport *fcport;
2411 	u32 comp_type;
2412 
2413 	comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
2414 	    FCOE_CQE_CQE_TYPE_MASK;
2415 
2416 	xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
2417 	io_req = &qedf->cmd_mgr->cmds[xid];
2418 
2419 	/* Completion not for a valid I/O anymore so just return */
2420 	if (!io_req)
2421 		return;
2422 
2423 	fcport = io_req->fcport;
2424 
2425 	if (fcport == NULL) {
2426 		QEDF_ERR(&(qedf->dbg_ctx), "fcport is NULL.\n");
2427 		return;
2428 	}
2429 
2430 	/*
2431 	 * Check that fcport is offloaded.  If it isn't then the spinlock
2432 	 * isn't valid and shouldn't be taken. We should just return.
2433 	 */
2434 	if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
2435 		QEDF_ERR(&(qedf->dbg_ctx), "Session not offloaded yet.\n");
2436 		return;
2437 	}
2438 
2439 
2440 	switch (comp_type) {
2441 	case FCOE_GOOD_COMPLETION_CQE_TYPE:
2442 		atomic_inc(&fcport->free_sqes);
2443 		switch (io_req->cmd_type) {
2444 		case QEDF_SCSI_CMD:
2445 			qedf_scsi_completion(qedf, cqe, io_req);
2446 			break;
2447 		case QEDF_ELS:
2448 			qedf_process_els_compl(qedf, cqe, io_req);
2449 			break;
2450 		case QEDF_TASK_MGMT_CMD:
2451 			qedf_process_tmf_compl(qedf, cqe, io_req);
2452 			break;
2453 		case QEDF_SEQ_CLEANUP:
2454 			qedf_process_seq_cleanup_compl(qedf, cqe, io_req);
2455 			break;
2456 		}
2457 		break;
2458 	case FCOE_ERROR_DETECTION_CQE_TYPE:
2459 		atomic_inc(&fcport->free_sqes);
2460 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2461 		    "Error detect CQE.\n");
2462 		qedf_process_error_detect(qedf, cqe, io_req);
2463 		break;
2464 	case FCOE_EXCH_CLEANUP_CQE_TYPE:
2465 		atomic_inc(&fcport->free_sqes);
2466 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2467 		    "Cleanup CQE.\n");
2468 		qedf_process_cleanup_compl(qedf, cqe, io_req);
2469 		break;
2470 	case FCOE_ABTS_CQE_TYPE:
2471 		atomic_inc(&fcport->free_sqes);
2472 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2473 		    "Abort CQE.\n");
2474 		qedf_process_abts_compl(qedf, cqe, io_req);
2475 		break;
2476 	case FCOE_DUMMY_CQE_TYPE:
2477 		atomic_inc(&fcport->free_sqes);
2478 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2479 		    "Dummy CQE.\n");
2480 		break;
2481 	case FCOE_LOCAL_COMP_CQE_TYPE:
2482 		atomic_inc(&fcport->free_sqes);
2483 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2484 		    "Local completion CQE.\n");
2485 		break;
2486 	case FCOE_WARNING_CQE_TYPE:
2487 		atomic_inc(&fcport->free_sqes);
2488 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2489 		    "Warning CQE.\n");
2490 		qedf_process_warning_compl(qedf, cqe, io_req);
2491 		break;
2492 	case MAX_FCOE_CQE_TYPE:
2493 		atomic_inc(&fcport->free_sqes);
2494 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2495 		    "Max FCoE CQE.\n");
2496 		break;
2497 	default:
2498 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2499 		    "Default CQE.\n");
2500 		break;
2501 	}
2502 }
2503 
2504 static void qedf_free_bdq(struct qedf_ctx *qedf)
2505 {
2506 	int i;
2507 
2508 	if (qedf->bdq_pbl_list)
2509 		dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
2510 		    qedf->bdq_pbl_list, qedf->bdq_pbl_list_dma);
2511 
2512 	if (qedf->bdq_pbl)
2513 		dma_free_coherent(&qedf->pdev->dev, qedf->bdq_pbl_mem_size,
2514 		    qedf->bdq_pbl, qedf->bdq_pbl_dma);
2515 
2516 	for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2517 		if (qedf->bdq[i].buf_addr) {
2518 			dma_free_coherent(&qedf->pdev->dev, QEDF_BDQ_BUF_SIZE,
2519 			    qedf->bdq[i].buf_addr, qedf->bdq[i].buf_dma);
2520 		}
2521 	}
2522 }
2523 
2524 static void qedf_free_global_queues(struct qedf_ctx *qedf)
2525 {
2526 	int i;
2527 	struct global_queue **gl = qedf->global_queues;
2528 
2529 	for (i = 0; i < qedf->num_queues; i++) {
2530 		if (!gl[i])
2531 			continue;
2532 
2533 		if (gl[i]->cq)
2534 			dma_free_coherent(&qedf->pdev->dev,
2535 			    gl[i]->cq_mem_size, gl[i]->cq, gl[i]->cq_dma);
2536 		if (gl[i]->cq_pbl)
2537 			dma_free_coherent(&qedf->pdev->dev, gl[i]->cq_pbl_size,
2538 			    gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
2539 
2540 		kfree(gl[i]);
2541 	}
2542 
2543 	qedf_free_bdq(qedf);
2544 }
2545 
2546 static int qedf_alloc_bdq(struct qedf_ctx *qedf)
2547 {
2548 	int i;
2549 	struct scsi_bd *pbl;
2550 	u64 *list;
2551 	dma_addr_t page;
2552 
2553 	/* Alloc dma memory for BDQ buffers */
2554 	for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2555 		qedf->bdq[i].buf_addr = dma_alloc_coherent(&qedf->pdev->dev,
2556 		    QEDF_BDQ_BUF_SIZE, &qedf->bdq[i].buf_dma, GFP_KERNEL);
2557 		if (!qedf->bdq[i].buf_addr) {
2558 			QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ "
2559 			    "buffer %d.\n", i);
2560 			return -ENOMEM;
2561 		}
2562 	}
2563 
2564 	/* Alloc dma memory for BDQ page buffer list */
2565 	qedf->bdq_pbl_mem_size =
2566 	    QEDF_BDQ_SIZE * sizeof(struct scsi_bd);
2567 	qedf->bdq_pbl_mem_size =
2568 	    ALIGN(qedf->bdq_pbl_mem_size, QEDF_PAGE_SIZE);
2569 
2570 	qedf->bdq_pbl = dma_alloc_coherent(&qedf->pdev->dev,
2571 	    qedf->bdq_pbl_mem_size, &qedf->bdq_pbl_dma, GFP_KERNEL);
2572 	if (!qedf->bdq_pbl) {
2573 		QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ PBL.\n");
2574 		return -ENOMEM;
2575 	}
2576 
2577 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2578 		  "BDQ PBL addr=0x%p dma=%pad\n",
2579 		  qedf->bdq_pbl, &qedf->bdq_pbl_dma);
2580 
2581 	/*
2582 	 * Populate BDQ PBL with physical and virtual address of individual
2583 	 * BDQ buffers
2584 	 */
2585 	pbl = (struct scsi_bd *)qedf->bdq_pbl;
2586 	for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2587 		pbl->address.hi = cpu_to_le32(U64_HI(qedf->bdq[i].buf_dma));
2588 		pbl->address.lo = cpu_to_le32(U64_LO(qedf->bdq[i].buf_dma));
2589 		pbl->opaque.hi = 0;
2590 		/* Opaque lo data is an index into the BDQ array */
2591 		pbl->opaque.lo = cpu_to_le32(i);
2592 		pbl++;
2593 	}
2594 
2595 	/* Allocate list of PBL pages */
2596 	qedf->bdq_pbl_list = dma_zalloc_coherent(&qedf->pdev->dev,
2597 	    QEDF_PAGE_SIZE, &qedf->bdq_pbl_list_dma, GFP_KERNEL);
2598 	if (!qedf->bdq_pbl_list) {
2599 		QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate list of PBL pages.\n");
2600 		return -ENOMEM;
2601 	}
2602 
2603 	/*
2604 	 * Now populate PBL list with pages that contain pointers to the
2605 	 * individual buffers.
2606 	 */
2607 	qedf->bdq_pbl_list_num_entries = qedf->bdq_pbl_mem_size /
2608 	    QEDF_PAGE_SIZE;
2609 	list = (u64 *)qedf->bdq_pbl_list;
2610 	page = qedf->bdq_pbl_list_dma;
2611 	for (i = 0; i < qedf->bdq_pbl_list_num_entries; i++) {
2612 		*list = qedf->bdq_pbl_dma;
2613 		list++;
2614 		page += QEDF_PAGE_SIZE;
2615 	}
2616 
2617 	return 0;
2618 }
2619 
2620 static int qedf_alloc_global_queues(struct qedf_ctx *qedf)
2621 {
2622 	u32 *list;
2623 	int i;
2624 	int status = 0, rc;
2625 	u32 *pbl;
2626 	dma_addr_t page;
2627 	int num_pages;
2628 
2629 	/* Allocate and map CQs, RQs */
2630 	/*
2631 	 * Number of global queues (CQ / RQ). This should
2632 	 * be <= number of available MSIX vectors for the PF
2633 	 */
2634 	if (!qedf->num_queues) {
2635 		QEDF_ERR(&(qedf->dbg_ctx), "No MSI-X vectors available!\n");
2636 		return 1;
2637 	}
2638 
2639 	/*
2640 	 * Make sure we allocated the PBL that will contain the physical
2641 	 * addresses of our queues
2642 	 */
2643 	if (!qedf->p_cpuq) {
2644 		status = 1;
2645 		goto mem_alloc_failure;
2646 	}
2647 
2648 	qedf->global_queues = kzalloc((sizeof(struct global_queue *)
2649 	    * qedf->num_queues), GFP_KERNEL);
2650 	if (!qedf->global_queues) {
2651 		QEDF_ERR(&(qedf->dbg_ctx), "Unable to allocate global "
2652 			  "queues array ptr memory\n");
2653 		return -ENOMEM;
2654 	}
2655 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2656 		   "qedf->global_queues=%p.\n", qedf->global_queues);
2657 
2658 	/* Allocate DMA coherent buffers for BDQ */
2659 	rc = qedf_alloc_bdq(qedf);
2660 	if (rc)
2661 		goto mem_alloc_failure;
2662 
2663 	/* Allocate a CQ and an associated PBL for each MSI-X vector */
2664 	for (i = 0; i < qedf->num_queues; i++) {
2665 		qedf->global_queues[i] = kzalloc(sizeof(struct global_queue),
2666 		    GFP_KERNEL);
2667 		if (!qedf->global_queues[i]) {
2668 			QEDF_WARN(&(qedf->dbg_ctx), "Unable to allocate "
2669 				   "global queue %d.\n", i);
2670 			status = -ENOMEM;
2671 			goto mem_alloc_failure;
2672 		}
2673 
2674 		qedf->global_queues[i]->cq_mem_size =
2675 		    FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
2676 		qedf->global_queues[i]->cq_mem_size =
2677 		    ALIGN(qedf->global_queues[i]->cq_mem_size, QEDF_PAGE_SIZE);
2678 
2679 		qedf->global_queues[i]->cq_pbl_size =
2680 		    (qedf->global_queues[i]->cq_mem_size /
2681 		    PAGE_SIZE) * sizeof(void *);
2682 		qedf->global_queues[i]->cq_pbl_size =
2683 		    ALIGN(qedf->global_queues[i]->cq_pbl_size, QEDF_PAGE_SIZE);
2684 
2685 		qedf->global_queues[i]->cq =
2686 		    dma_zalloc_coherent(&qedf->pdev->dev,
2687 			qedf->global_queues[i]->cq_mem_size,
2688 			&qedf->global_queues[i]->cq_dma, GFP_KERNEL);
2689 
2690 		if (!qedf->global_queues[i]->cq) {
2691 			QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq.\n");
2692 			status = -ENOMEM;
2693 			goto mem_alloc_failure;
2694 		}
2695 
2696 		qedf->global_queues[i]->cq_pbl =
2697 		    dma_zalloc_coherent(&qedf->pdev->dev,
2698 			qedf->global_queues[i]->cq_pbl_size,
2699 			&qedf->global_queues[i]->cq_pbl_dma, GFP_KERNEL);
2700 
2701 		if (!qedf->global_queues[i]->cq_pbl) {
2702 			QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq PBL.\n");
2703 			status = -ENOMEM;
2704 			goto mem_alloc_failure;
2705 		}
2706 
2707 		/* Create PBL */
2708 		num_pages = qedf->global_queues[i]->cq_mem_size /
2709 		    QEDF_PAGE_SIZE;
2710 		page = qedf->global_queues[i]->cq_dma;
2711 		pbl = (u32 *)qedf->global_queues[i]->cq_pbl;
2712 
2713 		while (num_pages--) {
2714 			*pbl = U64_LO(page);
2715 			pbl++;
2716 			*pbl = U64_HI(page);
2717 			pbl++;
2718 			page += QEDF_PAGE_SIZE;
2719 		}
2720 		/* Set the initial consumer index for cq */
2721 		qedf->global_queues[i]->cq_cons_idx = 0;
2722 	}
2723 
2724 	list = (u32 *)qedf->p_cpuq;
2725 
2726 	/*
2727 	 * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
2728 	 * CQ#1 PBL pointer, RQ#1 PBL pointer, etc.  Each PBL pointer points
2729 	 * to the physical address which contains an array of pointers to
2730 	 * the physical addresses of the specific queue pages.
2731 	 */
2732 	for (i = 0; i < qedf->num_queues; i++) {
2733 		*list = U64_LO(qedf->global_queues[i]->cq_pbl_dma);
2734 		list++;
2735 		*list = U64_HI(qedf->global_queues[i]->cq_pbl_dma);
2736 		list++;
2737 		*list = U64_LO(0);
2738 		list++;
2739 		*list = U64_HI(0);
2740 		list++;
2741 	}
2742 
2743 	return 0;
2744 
2745 mem_alloc_failure:
2746 	qedf_free_global_queues(qedf);
2747 	return status;
2748 }
2749 
2750 static int qedf_set_fcoe_pf_param(struct qedf_ctx *qedf)
2751 {
2752 	u8 sq_num_pbl_pages;
2753 	u32 sq_mem_size;
2754 	u32 cq_mem_size;
2755 	u32 cq_num_entries;
2756 	int rval;
2757 
2758 	/*
2759 	 * The number of completion queues/fastpath interrupts/status blocks
2760 	 * we allocation is the minimum off:
2761 	 *
2762 	 * Number of CPUs
2763 	 * Number of MSI-X vectors
2764 	 * Max number allocated in hardware (QEDF_MAX_NUM_CQS)
2765 	 */
2766 	qedf->num_queues = min((unsigned int)QEDF_MAX_NUM_CQS,
2767 	    num_online_cpus());
2768 
2769 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of CQs is %d.\n",
2770 		   qedf->num_queues);
2771 
2772 	qedf->p_cpuq = pci_alloc_consistent(qedf->pdev,
2773 	    qedf->num_queues * sizeof(struct qedf_glbl_q_params),
2774 	    &qedf->hw_p_cpuq);
2775 
2776 	if (!qedf->p_cpuq) {
2777 		QEDF_ERR(&(qedf->dbg_ctx), "pci_alloc_consistent failed.\n");
2778 		return 1;
2779 	}
2780 
2781 	rval = qedf_alloc_global_queues(qedf);
2782 	if (rval) {
2783 		QEDF_ERR(&(qedf->dbg_ctx), "Global queue allocation "
2784 			  "failed.\n");
2785 		return 1;
2786 	}
2787 
2788 	/* Calculate SQ PBL size in the same manner as in qedf_sq_alloc() */
2789 	sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
2790 	sq_mem_size = ALIGN(sq_mem_size, QEDF_PAGE_SIZE);
2791 	sq_num_pbl_pages = (sq_mem_size / QEDF_PAGE_SIZE);
2792 
2793 	/* Calculate CQ num entries */
2794 	cq_mem_size = FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
2795 	cq_mem_size = ALIGN(cq_mem_size, QEDF_PAGE_SIZE);
2796 	cq_num_entries = cq_mem_size / sizeof(struct fcoe_cqe);
2797 
2798 	memset(&(qedf->pf_params), 0, sizeof(qedf->pf_params));
2799 
2800 	/* Setup the value for fcoe PF */
2801 	qedf->pf_params.fcoe_pf_params.num_cons = QEDF_MAX_SESSIONS;
2802 	qedf->pf_params.fcoe_pf_params.num_tasks = FCOE_PARAMS_NUM_TASKS;
2803 	qedf->pf_params.fcoe_pf_params.glbl_q_params_addr =
2804 	    (u64)qedf->hw_p_cpuq;
2805 	qedf->pf_params.fcoe_pf_params.sq_num_pbl_pages = sq_num_pbl_pages;
2806 
2807 	qedf->pf_params.fcoe_pf_params.rq_buffer_log_size = 0;
2808 
2809 	qedf->pf_params.fcoe_pf_params.cq_num_entries = cq_num_entries;
2810 	qedf->pf_params.fcoe_pf_params.num_cqs = qedf->num_queues;
2811 
2812 	/* log_page_size: 12 for 4KB pages */
2813 	qedf->pf_params.fcoe_pf_params.log_page_size = ilog2(QEDF_PAGE_SIZE);
2814 
2815 	qedf->pf_params.fcoe_pf_params.mtu = 9000;
2816 	qedf->pf_params.fcoe_pf_params.gl_rq_pi = QEDF_FCOE_PARAMS_GL_RQ_PI;
2817 	qedf->pf_params.fcoe_pf_params.gl_cmd_pi = QEDF_FCOE_PARAMS_GL_CMD_PI;
2818 
2819 	/* BDQ address and size */
2820 	qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0] =
2821 	    qedf->bdq_pbl_list_dma;
2822 	qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0] =
2823 	    qedf->bdq_pbl_list_num_entries;
2824 	qedf->pf_params.fcoe_pf_params.rq_buffer_size = QEDF_BDQ_BUF_SIZE;
2825 
2826 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2827 	    "bdq_list=%p bdq_pbl_list_dma=%llx bdq_pbl_list_entries=%d.\n",
2828 	    qedf->bdq_pbl_list,
2829 	    qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0],
2830 	    qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0]);
2831 
2832 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2833 	    "cq_num_entries=%d.\n",
2834 	    qedf->pf_params.fcoe_pf_params.cq_num_entries);
2835 
2836 	return 0;
2837 }
2838 
2839 /* Free DMA coherent memory for array of queue pointers we pass to qed */
2840 static void qedf_free_fcoe_pf_param(struct qedf_ctx *qedf)
2841 {
2842 	size_t size = 0;
2843 
2844 	if (qedf->p_cpuq) {
2845 		size = qedf->num_queues * sizeof(struct qedf_glbl_q_params);
2846 		pci_free_consistent(qedf->pdev, size, qedf->p_cpuq,
2847 		    qedf->hw_p_cpuq);
2848 	}
2849 
2850 	qedf_free_global_queues(qedf);
2851 
2852 	if (qedf->global_queues)
2853 		kfree(qedf->global_queues);
2854 }
2855 
2856 /*
2857  * PCI driver functions
2858  */
2859 
2860 static const struct pci_device_id qedf_pci_tbl[] = {
2861 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165c) },
2862 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8080) },
2863 	{0}
2864 };
2865 MODULE_DEVICE_TABLE(pci, qedf_pci_tbl);
2866 
2867 static struct pci_driver qedf_pci_driver = {
2868 	.name = QEDF_MODULE_NAME,
2869 	.id_table = qedf_pci_tbl,
2870 	.probe = qedf_probe,
2871 	.remove = qedf_remove,
2872 };
2873 
2874 static int __qedf_probe(struct pci_dev *pdev, int mode)
2875 {
2876 	int rc = -EINVAL;
2877 	struct fc_lport *lport;
2878 	struct qedf_ctx *qedf;
2879 	struct Scsi_Host *host;
2880 	bool is_vf = false;
2881 	struct qed_ll2_params params;
2882 	char host_buf[20];
2883 	struct qed_link_params link_params;
2884 	int status;
2885 	void *task_start, *task_end;
2886 	struct qed_slowpath_params slowpath_params;
2887 	struct qed_probe_params qed_params;
2888 	u16 tmp;
2889 
2890 	/*
2891 	 * When doing error recovery we didn't reap the lport so don't try
2892 	 * to reallocate it.
2893 	 */
2894 	if (mode != QEDF_MODE_RECOVERY) {
2895 		lport = libfc_host_alloc(&qedf_host_template,
2896 		    sizeof(struct qedf_ctx));
2897 
2898 		if (!lport) {
2899 			QEDF_ERR(NULL, "Could not allocate lport.\n");
2900 			rc = -ENOMEM;
2901 			goto err0;
2902 		}
2903 
2904 		/* Initialize qedf_ctx */
2905 		qedf = lport_priv(lport);
2906 		qedf->lport = lport;
2907 		qedf->ctlr.lp = lport;
2908 		qedf->pdev = pdev;
2909 		qedf->dbg_ctx.pdev = pdev;
2910 		qedf->dbg_ctx.host_no = lport->host->host_no;
2911 		spin_lock_init(&qedf->hba_lock);
2912 		INIT_LIST_HEAD(&qedf->fcports);
2913 		qedf->curr_conn_id = QEDF_MAX_SESSIONS - 1;
2914 		atomic_set(&qedf->num_offloads, 0);
2915 		qedf->stop_io_on_error = false;
2916 		pci_set_drvdata(pdev, qedf);
2917 		init_completion(&qedf->fipvlan_compl);
2918 
2919 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO,
2920 		   "QLogic FastLinQ FCoE Module qedf %s, "
2921 		   "FW %d.%d.%d.%d\n", QEDF_VERSION,
2922 		   FW_MAJOR_VERSION, FW_MINOR_VERSION, FW_REVISION_VERSION,
2923 		   FW_ENGINEERING_VERSION);
2924 	} else {
2925 		/* Init pointers during recovery */
2926 		qedf = pci_get_drvdata(pdev);
2927 		lport = qedf->lport;
2928 	}
2929 
2930 	host = lport->host;
2931 
2932 	/* Allocate mempool for qedf_io_work structs */
2933 	qedf->io_mempool = mempool_create_slab_pool(QEDF_IO_WORK_MIN,
2934 	    qedf_io_work_cache);
2935 	if (qedf->io_mempool == NULL) {
2936 		QEDF_ERR(&(qedf->dbg_ctx), "qedf->io_mempool is NULL.\n");
2937 		goto err1;
2938 	}
2939 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO, "qedf->io_mempool=%p.\n",
2940 	    qedf->io_mempool);
2941 
2942 	sprintf(host_buf, "qedf_%u_link",
2943 	    qedf->lport->host->host_no);
2944 	qedf->link_update_wq = create_singlethread_workqueue(host_buf);
2945 	INIT_DELAYED_WORK(&qedf->link_update, qedf_handle_link_update);
2946 	INIT_DELAYED_WORK(&qedf->link_recovery, qedf_link_recovery);
2947 
2948 	qedf->fipvlan_retries = qedf_fipvlan_retries;
2949 
2950 	/*
2951 	 * Common probe. Takes care of basic hardware init and pci_*
2952 	 * functions.
2953 	 */
2954 	memset(&qed_params, 0, sizeof(qed_params));
2955 	qed_params.protocol = QED_PROTOCOL_FCOE;
2956 	qed_params.dp_module = qedf_dp_module;
2957 	qed_params.dp_level = qedf_dp_level;
2958 	qed_params.is_vf = is_vf;
2959 	qedf->cdev = qed_ops->common->probe(pdev, &qed_params);
2960 	if (!qedf->cdev) {
2961 		rc = -ENODEV;
2962 		goto err1;
2963 	}
2964 
2965 	/* queue allocation code should come here
2966 	 * order should be
2967 	 * 	slowpath_start
2968 	 * 	status block allocation
2969 	 *	interrupt registration (to get min number of queues)
2970 	 *	set_fcoe_pf_param
2971 	 *	qed_sp_fcoe_func_start
2972 	 */
2973 	rc = qedf_set_fcoe_pf_param(qedf);
2974 	if (rc) {
2975 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot set fcoe pf param.\n");
2976 		goto err2;
2977 	}
2978 	qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
2979 
2980 	/* Learn information crucial for qedf to progress */
2981 	rc = qed_ops->fill_dev_info(qedf->cdev, &qedf->dev_info);
2982 	if (rc) {
2983 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to dev info.\n");
2984 		goto err1;
2985 	}
2986 
2987 	/* Record BDQ producer doorbell addresses */
2988 	qedf->bdq_primary_prod = qedf->dev_info.primary_dbq_rq_addr;
2989 	qedf->bdq_secondary_prod = qedf->dev_info.secondary_bdq_rq_addr;
2990 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2991 	    "BDQ primary_prod=%p secondary_prod=%p.\n", qedf->bdq_primary_prod,
2992 	    qedf->bdq_secondary_prod);
2993 
2994 	qed_ops->register_ops(qedf->cdev, &qedf_cb_ops, qedf);
2995 
2996 	rc = qedf_prepare_sb(qedf);
2997 	if (rc) {
2998 
2999 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
3000 		goto err2;
3001 	}
3002 
3003 	/* Start the Slowpath-process */
3004 	slowpath_params.int_mode = QED_INT_MODE_MSIX;
3005 	slowpath_params.drv_major = QEDF_DRIVER_MAJOR_VER;
3006 	slowpath_params.drv_minor = QEDF_DRIVER_MINOR_VER;
3007 	slowpath_params.drv_rev = QEDF_DRIVER_REV_VER;
3008 	slowpath_params.drv_eng = QEDF_DRIVER_ENG_VER;
3009 	strncpy(slowpath_params.name, "qedf", QED_DRV_VER_STR_SIZE);
3010 	rc = qed_ops->common->slowpath_start(qedf->cdev, &slowpath_params);
3011 	if (rc) {
3012 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
3013 		goto err2;
3014 	}
3015 
3016 	/*
3017 	 * update_pf_params needs to be called before and after slowpath
3018 	 * start
3019 	 */
3020 	qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
3021 
3022 	/* Setup interrupts */
3023 	rc = qedf_setup_int(qedf);
3024 	if (rc)
3025 		goto err3;
3026 
3027 	rc = qed_ops->start(qedf->cdev, &qedf->tasks);
3028 	if (rc) {
3029 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot start FCoE function.\n");
3030 		goto err4;
3031 	}
3032 	task_start = qedf_get_task_mem(&qedf->tasks, 0);
3033 	task_end = qedf_get_task_mem(&qedf->tasks, MAX_TID_BLOCKS_FCOE - 1);
3034 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Task context start=%p, "
3035 		   "end=%p block_size=%u.\n", task_start, task_end,
3036 		   qedf->tasks.size);
3037 
3038 	/*
3039 	 * We need to write the number of BDs in the BDQ we've preallocated so
3040 	 * the f/w will do a prefetch and we'll get an unsolicited CQE when a
3041 	 * packet arrives.
3042 	 */
3043 	qedf->bdq_prod_idx = QEDF_BDQ_SIZE;
3044 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3045 	    "Writing %d to primary and secondary BDQ doorbell registers.\n",
3046 	    qedf->bdq_prod_idx);
3047 	writew(qedf->bdq_prod_idx, qedf->bdq_primary_prod);
3048 	tmp = readw(qedf->bdq_primary_prod);
3049 	writew(qedf->bdq_prod_idx, qedf->bdq_secondary_prod);
3050 	tmp = readw(qedf->bdq_secondary_prod);
3051 
3052 	qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
3053 
3054 	/* Now that the dev_info struct has been filled in set the MAC
3055 	 * address
3056 	 */
3057 	ether_addr_copy(qedf->mac, qedf->dev_info.common.hw_mac);
3058 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "MAC address is %pM.\n",
3059 		   qedf->mac);
3060 
3061 	/* Set the WWNN and WWPN based on the MAC address */
3062 	qedf->wwnn = fcoe_wwn_from_mac(qedf->mac, 1, 0);
3063 	qedf->wwpn = fcoe_wwn_from_mac(qedf->mac, 2, 0);
3064 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,  "WWNN=%016llx "
3065 		   "WWPN=%016llx.\n", qedf->wwnn, qedf->wwpn);
3066 
3067 	sprintf(host_buf, "host_%d", host->host_no);
3068 	qed_ops->common->set_name(qedf->cdev, host_buf);
3069 
3070 
3071 	/* Set xid max values */
3072 	qedf->max_scsi_xid = QEDF_MAX_SCSI_XID;
3073 	qedf->max_els_xid = QEDF_MAX_ELS_XID;
3074 
3075 	/* Allocate cmd mgr */
3076 	qedf->cmd_mgr = qedf_cmd_mgr_alloc(qedf);
3077 	if (!qedf->cmd_mgr) {
3078 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to allocate cmd mgr.\n");
3079 		goto err5;
3080 	}
3081 
3082 	if (mode != QEDF_MODE_RECOVERY) {
3083 		host->transportt = qedf_fc_transport_template;
3084 		host->can_queue = QEDF_MAX_ELS_XID;
3085 		host->max_lun = qedf_max_lun;
3086 		host->max_cmd_len = QEDF_MAX_CDB_LEN;
3087 		rc = scsi_add_host(host, &pdev->dev);
3088 		if (rc)
3089 			goto err6;
3090 	}
3091 
3092 	memset(&params, 0, sizeof(params));
3093 	params.mtu = 9000;
3094 	ether_addr_copy(params.ll2_mac_address, qedf->mac);
3095 
3096 	/* Start LL2 processing thread */
3097 	snprintf(host_buf, 20, "qedf_%d_ll2", host->host_no);
3098 	qedf->ll2_recv_wq =
3099 		create_singlethread_workqueue(host_buf);
3100 	if (!qedf->ll2_recv_wq) {
3101 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to LL2 workqueue.\n");
3102 		goto err7;
3103 	}
3104 
3105 #ifdef CONFIG_DEBUG_FS
3106 	qedf_dbg_host_init(&(qedf->dbg_ctx), &qedf_debugfs_ops,
3107 			    &qedf_dbg_fops);
3108 #endif
3109 
3110 	/* Start LL2 */
3111 	qed_ops->ll2->register_cb_ops(qedf->cdev, &qedf_ll2_cb_ops, qedf);
3112 	rc = qed_ops->ll2->start(qedf->cdev, &params);
3113 	if (rc) {
3114 		QEDF_ERR(&(qedf->dbg_ctx), "Could not start Light L2.\n");
3115 		goto err7;
3116 	}
3117 	set_bit(QEDF_LL2_STARTED, &qedf->flags);
3118 
3119 	/* hw will be insterting vlan tag*/
3120 	qedf->vlan_hw_insert = 1;
3121 	qedf->vlan_id = 0;
3122 
3123 	/*
3124 	 * No need to setup fcoe_ctlr or fc_lport objects during recovery since
3125 	 * they were not reaped during the unload process.
3126 	 */
3127 	if (mode != QEDF_MODE_RECOVERY) {
3128 		/* Setup imbedded fcoe controller */
3129 		qedf_fcoe_ctlr_setup(qedf);
3130 
3131 		/* Setup lport */
3132 		rc = qedf_lport_setup(qedf);
3133 		if (rc) {
3134 			QEDF_ERR(&(qedf->dbg_ctx),
3135 			    "qedf_lport_setup failed.\n");
3136 			goto err7;
3137 		}
3138 	}
3139 
3140 	sprintf(host_buf, "qedf_%u_timer", qedf->lport->host->host_no);
3141 	qedf->timer_work_queue =
3142 		create_singlethread_workqueue(host_buf);
3143 	if (!qedf->timer_work_queue) {
3144 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to start timer "
3145 			  "workqueue.\n");
3146 		goto err7;
3147 	}
3148 
3149 	/* DPC workqueue is not reaped during recovery unload */
3150 	if (mode != QEDF_MODE_RECOVERY) {
3151 		sprintf(host_buf, "qedf_%u_dpc",
3152 		    qedf->lport->host->host_no);
3153 		qedf->dpc_wq = create_singlethread_workqueue(host_buf);
3154 	}
3155 
3156 	/*
3157 	 * GRC dump and sysfs parameters are not reaped during the recovery
3158 	 * unload process.
3159 	 */
3160 	if (mode != QEDF_MODE_RECOVERY) {
3161 		qedf->grcdump_size = qed_ops->common->dbg_grc_size(qedf->cdev);
3162 		if (qedf->grcdump_size) {
3163 			rc = qedf_alloc_grc_dump_buf(&qedf->grcdump,
3164 			    qedf->grcdump_size);
3165 			if (rc) {
3166 				QEDF_ERR(&(qedf->dbg_ctx),
3167 				    "GRC Dump buffer alloc failed.\n");
3168 				qedf->grcdump = NULL;
3169 			}
3170 
3171 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3172 			    "grcdump: addr=%p, size=%u.\n",
3173 			    qedf->grcdump, qedf->grcdump_size);
3174 		}
3175 		qedf_create_sysfs_ctx_attr(qedf);
3176 
3177 		/* Initialize I/O tracing for this adapter */
3178 		spin_lock_init(&qedf->io_trace_lock);
3179 		qedf->io_trace_idx = 0;
3180 	}
3181 
3182 	init_completion(&qedf->flogi_compl);
3183 
3184 	memset(&link_params, 0, sizeof(struct qed_link_params));
3185 	link_params.link_up = true;
3186 	status = qed_ops->common->set_link(qedf->cdev, &link_params);
3187 	if (status)
3188 		QEDF_WARN(&(qedf->dbg_ctx), "set_link failed.\n");
3189 
3190 	/* Start/restart discovery */
3191 	if (mode == QEDF_MODE_RECOVERY)
3192 		fcoe_ctlr_link_up(&qedf->ctlr);
3193 	else
3194 		fc_fabric_login(lport);
3195 
3196 	/* All good */
3197 	return 0;
3198 
3199 err7:
3200 	if (qedf->ll2_recv_wq)
3201 		destroy_workqueue(qedf->ll2_recv_wq);
3202 	fc_remove_host(qedf->lport->host);
3203 	scsi_remove_host(qedf->lport->host);
3204 #ifdef CONFIG_DEBUG_FS
3205 	qedf_dbg_host_exit(&(qedf->dbg_ctx));
3206 #endif
3207 err6:
3208 	qedf_cmd_mgr_free(qedf->cmd_mgr);
3209 err5:
3210 	qed_ops->stop(qedf->cdev);
3211 err4:
3212 	qedf_free_fcoe_pf_param(qedf);
3213 	qedf_sync_free_irqs(qedf);
3214 err3:
3215 	qed_ops->common->slowpath_stop(qedf->cdev);
3216 err2:
3217 	qed_ops->common->remove(qedf->cdev);
3218 err1:
3219 	scsi_host_put(lport->host);
3220 err0:
3221 	return rc;
3222 }
3223 
3224 static int qedf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3225 {
3226 	return __qedf_probe(pdev, QEDF_MODE_NORMAL);
3227 }
3228 
3229 static void __qedf_remove(struct pci_dev *pdev, int mode)
3230 {
3231 	struct qedf_ctx *qedf;
3232 
3233 	if (!pdev) {
3234 		QEDF_ERR(NULL, "pdev is NULL.\n");
3235 		return;
3236 	}
3237 
3238 	qedf = pci_get_drvdata(pdev);
3239 
3240 	/*
3241 	 * Prevent race where we're in board disable work and then try to
3242 	 * rmmod the module.
3243 	 */
3244 	if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
3245 		QEDF_ERR(&qedf->dbg_ctx, "Already removing PCI function.\n");
3246 		return;
3247 	}
3248 
3249 	if (mode != QEDF_MODE_RECOVERY)
3250 		set_bit(QEDF_UNLOADING, &qedf->flags);
3251 
3252 	/* Logoff the fabric to upload all connections */
3253 	if (mode == QEDF_MODE_RECOVERY)
3254 		fcoe_ctlr_link_down(&qedf->ctlr);
3255 	else
3256 		fc_fabric_logoff(qedf->lport);
3257 	qedf_wait_for_upload(qedf);
3258 
3259 #ifdef CONFIG_DEBUG_FS
3260 	qedf_dbg_host_exit(&(qedf->dbg_ctx));
3261 #endif
3262 
3263 	/* Stop any link update handling */
3264 	cancel_delayed_work_sync(&qedf->link_update);
3265 	destroy_workqueue(qedf->link_update_wq);
3266 	qedf->link_update_wq = NULL;
3267 
3268 	if (qedf->timer_work_queue)
3269 		destroy_workqueue(qedf->timer_work_queue);
3270 
3271 	/* Stop Light L2 */
3272 	clear_bit(QEDF_LL2_STARTED, &qedf->flags);
3273 	qed_ops->ll2->stop(qedf->cdev);
3274 	if (qedf->ll2_recv_wq)
3275 		destroy_workqueue(qedf->ll2_recv_wq);
3276 
3277 	/* Stop fastpath */
3278 	qedf_sync_free_irqs(qedf);
3279 	qedf_destroy_sb(qedf);
3280 
3281 	/*
3282 	 * During recovery don't destroy OS constructs that represent the
3283 	 * physical port.
3284 	 */
3285 	if (mode != QEDF_MODE_RECOVERY) {
3286 		qedf_free_grc_dump_buf(&qedf->grcdump);
3287 		qedf_remove_sysfs_ctx_attr(qedf);
3288 
3289 		/* Remove all SCSI/libfc/libfcoe structures */
3290 		fcoe_ctlr_destroy(&qedf->ctlr);
3291 		fc_lport_destroy(qedf->lport);
3292 		fc_remove_host(qedf->lport->host);
3293 		scsi_remove_host(qedf->lport->host);
3294 	}
3295 
3296 	qedf_cmd_mgr_free(qedf->cmd_mgr);
3297 
3298 	if (mode != QEDF_MODE_RECOVERY) {
3299 		fc_exch_mgr_free(qedf->lport);
3300 		fc_lport_free_stats(qedf->lport);
3301 
3302 		/* Wait for all vports to be reaped */
3303 		qedf_wait_for_vport_destroy(qedf);
3304 	}
3305 
3306 	/*
3307 	 * Now that all connections have been uploaded we can stop the
3308 	 * rest of the qed operations
3309 	 */
3310 	qed_ops->stop(qedf->cdev);
3311 
3312 	if (mode != QEDF_MODE_RECOVERY) {
3313 		if (qedf->dpc_wq) {
3314 			/* Stop general DPC handling */
3315 			destroy_workqueue(qedf->dpc_wq);
3316 			qedf->dpc_wq = NULL;
3317 		}
3318 	}
3319 
3320 	/* Final shutdown for the board */
3321 	qedf_free_fcoe_pf_param(qedf);
3322 	if (mode != QEDF_MODE_RECOVERY) {
3323 		qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
3324 		pci_set_drvdata(pdev, NULL);
3325 	}
3326 	qed_ops->common->slowpath_stop(qedf->cdev);
3327 	qed_ops->common->remove(qedf->cdev);
3328 
3329 	mempool_destroy(qedf->io_mempool);
3330 
3331 	/* Only reap the Scsi_host on a real removal */
3332 	if (mode != QEDF_MODE_RECOVERY)
3333 		scsi_host_put(qedf->lport->host);
3334 }
3335 
3336 static void qedf_remove(struct pci_dev *pdev)
3337 {
3338 	/* Check to make sure this function wasn't already disabled */
3339 	if (!atomic_read(&pdev->enable_cnt))
3340 		return;
3341 
3342 	__qedf_remove(pdev, QEDF_MODE_NORMAL);
3343 }
3344 
3345 /*
3346  * Module Init/Remove
3347  */
3348 
3349 static int __init qedf_init(void)
3350 {
3351 	int ret;
3352 
3353 	/* If debug=1 passed, set the default log mask */
3354 	if (qedf_debug == QEDF_LOG_DEFAULT)
3355 		qedf_debug = QEDF_DEFAULT_LOG_MASK;
3356 
3357 	/* Print driver banner */
3358 	QEDF_INFO(NULL, QEDF_LOG_INFO, "%s v%s.\n", QEDF_DESCR,
3359 		   QEDF_VERSION);
3360 
3361 	/* Create kmem_cache for qedf_io_work structs */
3362 	qedf_io_work_cache = kmem_cache_create("qedf_io_work_cache",
3363 	    sizeof(struct qedf_io_work), 0, SLAB_HWCACHE_ALIGN, NULL);
3364 	if (qedf_io_work_cache == NULL) {
3365 		QEDF_ERR(NULL, "qedf_io_work_cache is NULL.\n");
3366 		goto err1;
3367 	}
3368 	QEDF_INFO(NULL, QEDF_LOG_DISC, "qedf_io_work_cache=%p.\n",
3369 	    qedf_io_work_cache);
3370 
3371 	qed_ops = qed_get_fcoe_ops();
3372 	if (!qed_ops) {
3373 		QEDF_ERR(NULL, "Failed to get qed fcoe operations\n");
3374 		goto err1;
3375 	}
3376 
3377 #ifdef CONFIG_DEBUG_FS
3378 	qedf_dbg_init("qedf");
3379 #endif
3380 
3381 	qedf_fc_transport_template =
3382 	    fc_attach_transport(&qedf_fc_transport_fn);
3383 	if (!qedf_fc_transport_template) {
3384 		QEDF_ERR(NULL, "Could not register with FC transport\n");
3385 		goto err2;
3386 	}
3387 
3388 	qedf_fc_vport_transport_template =
3389 		fc_attach_transport(&qedf_fc_vport_transport_fn);
3390 	if (!qedf_fc_vport_transport_template) {
3391 		QEDF_ERR(NULL, "Could not register vport template with FC "
3392 			  "transport\n");
3393 		goto err3;
3394 	}
3395 
3396 	qedf_io_wq = create_workqueue("qedf_io_wq");
3397 	if (!qedf_io_wq) {
3398 		QEDF_ERR(NULL, "Could not create qedf_io_wq.\n");
3399 		goto err4;
3400 	}
3401 
3402 	qedf_cb_ops.get_login_failures = qedf_get_login_failures;
3403 
3404 	ret = pci_register_driver(&qedf_pci_driver);
3405 	if (ret) {
3406 		QEDF_ERR(NULL, "Failed to register driver\n");
3407 		goto err5;
3408 	}
3409 
3410 	return 0;
3411 
3412 err5:
3413 	destroy_workqueue(qedf_io_wq);
3414 err4:
3415 	fc_release_transport(qedf_fc_vport_transport_template);
3416 err3:
3417 	fc_release_transport(qedf_fc_transport_template);
3418 err2:
3419 #ifdef CONFIG_DEBUG_FS
3420 	qedf_dbg_exit();
3421 #endif
3422 	qed_put_fcoe_ops();
3423 err1:
3424 	return -EINVAL;
3425 }
3426 
3427 static void __exit qedf_cleanup(void)
3428 {
3429 	pci_unregister_driver(&qedf_pci_driver);
3430 
3431 	destroy_workqueue(qedf_io_wq);
3432 
3433 	fc_release_transport(qedf_fc_vport_transport_template);
3434 	fc_release_transport(qedf_fc_transport_template);
3435 #ifdef CONFIG_DEBUG_FS
3436 	qedf_dbg_exit();
3437 #endif
3438 	qed_put_fcoe_ops();
3439 
3440 	kmem_cache_destroy(qedf_io_work_cache);
3441 }
3442 
3443 MODULE_LICENSE("GPL");
3444 MODULE_DESCRIPTION("QLogic QEDF 25/40/50/100Gb FCoE Driver");
3445 MODULE_AUTHOR("QLogic Corporation");
3446 MODULE_VERSION(QEDF_VERSION);
3447 module_init(qedf_init);
3448 module_exit(qedf_cleanup);
3449