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