xref: /linux/drivers/scsi/qedf/qedf_main.c (revision 17cfcb68af3bc7d5e8ae08779b1853310a2949f3)
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 struct fc_host_statistics *qedf_fc_get_host_stats(struct Scsi_Host
1930 	*shost)
1931 {
1932 	struct fc_host_statistics *qedf_stats;
1933 	struct fc_lport *lport = shost_priv(shost);
1934 	struct qedf_ctx *qedf = lport_priv(lport);
1935 	struct qed_fcoe_stats *fw_fcoe_stats;
1936 
1937 	qedf_stats = fc_get_host_stats(shost);
1938 
1939 	/* We don't collect offload stats for specific NPIV ports */
1940 	if (lport->vport)
1941 		goto out;
1942 
1943 	fw_fcoe_stats = kmalloc(sizeof(struct qed_fcoe_stats), GFP_KERNEL);
1944 	if (!fw_fcoe_stats) {
1945 		QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate memory for "
1946 		    "fw_fcoe_stats.\n");
1947 		goto out;
1948 	}
1949 
1950 	mutex_lock(&qedf->stats_mutex);
1951 
1952 	/* Query firmware for offload stats */
1953 	qed_ops->get_stats(qedf->cdev, fw_fcoe_stats);
1954 
1955 	/*
1956 	 * The expectation is that we add our offload stats to the stats
1957 	 * being maintained by libfc each time the fc_get_host_status callback
1958 	 * is invoked. The additions are not carried over for each call to
1959 	 * the fc_get_host_stats callback.
1960 	 */
1961 	qedf_stats->tx_frames += fw_fcoe_stats->fcoe_tx_data_pkt_cnt +
1962 	    fw_fcoe_stats->fcoe_tx_xfer_pkt_cnt +
1963 	    fw_fcoe_stats->fcoe_tx_other_pkt_cnt;
1964 	qedf_stats->rx_frames += fw_fcoe_stats->fcoe_rx_data_pkt_cnt +
1965 	    fw_fcoe_stats->fcoe_rx_xfer_pkt_cnt +
1966 	    fw_fcoe_stats->fcoe_rx_other_pkt_cnt;
1967 	qedf_stats->fcp_input_megabytes +=
1968 	    do_div(fw_fcoe_stats->fcoe_rx_byte_cnt, 1000000);
1969 	qedf_stats->fcp_output_megabytes +=
1970 	    do_div(fw_fcoe_stats->fcoe_tx_byte_cnt, 1000000);
1971 	qedf_stats->rx_words += fw_fcoe_stats->fcoe_rx_byte_cnt / 4;
1972 	qedf_stats->tx_words += fw_fcoe_stats->fcoe_tx_byte_cnt / 4;
1973 	qedf_stats->invalid_crc_count +=
1974 	    fw_fcoe_stats->fcoe_silent_drop_pkt_crc_error_cnt;
1975 	qedf_stats->dumped_frames =
1976 	    fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
1977 	qedf_stats->error_frames +=
1978 	    fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
1979 	qedf_stats->fcp_input_requests += qedf->input_requests;
1980 	qedf_stats->fcp_output_requests += qedf->output_requests;
1981 	qedf_stats->fcp_control_requests += qedf->control_requests;
1982 	qedf_stats->fcp_packet_aborts += qedf->packet_aborts;
1983 	qedf_stats->fcp_frame_alloc_failures += qedf->alloc_failures;
1984 
1985 	mutex_unlock(&qedf->stats_mutex);
1986 	kfree(fw_fcoe_stats);
1987 out:
1988 	return qedf_stats;
1989 }
1990 
1991 static struct fc_function_template qedf_fc_transport_fn = {
1992 	.show_host_node_name = 1,
1993 	.show_host_port_name = 1,
1994 	.show_host_supported_classes = 1,
1995 	.show_host_supported_fc4s = 1,
1996 	.show_host_active_fc4s = 1,
1997 	.show_host_maxframe_size = 1,
1998 
1999 	.show_host_port_id = 1,
2000 	.show_host_supported_speeds = 1,
2001 	.get_host_speed = fc_get_host_speed,
2002 	.show_host_speed = 1,
2003 	.show_host_port_type = 1,
2004 	.get_host_port_state = fc_get_host_port_state,
2005 	.show_host_port_state = 1,
2006 	.show_host_symbolic_name = 1,
2007 
2008 	/*
2009 	 * Tell FC transport to allocate enough space to store the backpointer
2010 	 * for the associate qedf_rport struct.
2011 	 */
2012 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2013 				sizeof(struct qedf_rport)),
2014 	.show_rport_maxframe_size = 1,
2015 	.show_rport_supported_classes = 1,
2016 	.show_host_fabric_name = 1,
2017 	.show_starget_node_name = 1,
2018 	.show_starget_port_name = 1,
2019 	.show_starget_port_id = 1,
2020 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2021 	.show_rport_dev_loss_tmo = 1,
2022 	.get_fc_host_stats = qedf_fc_get_host_stats,
2023 	.issue_fc_host_lip = qedf_fcoe_reset,
2024 	.vport_create = qedf_vport_create,
2025 	.vport_delete = qedf_vport_destroy,
2026 	.vport_disable = qedf_vport_disable,
2027 	.bsg_request = fc_lport_bsg_request,
2028 };
2029 
2030 static struct fc_function_template qedf_fc_vport_transport_fn = {
2031 	.show_host_node_name = 1,
2032 	.show_host_port_name = 1,
2033 	.show_host_supported_classes = 1,
2034 	.show_host_supported_fc4s = 1,
2035 	.show_host_active_fc4s = 1,
2036 	.show_host_maxframe_size = 1,
2037 	.show_host_port_id = 1,
2038 	.show_host_supported_speeds = 1,
2039 	.get_host_speed = fc_get_host_speed,
2040 	.show_host_speed = 1,
2041 	.show_host_port_type = 1,
2042 	.get_host_port_state = fc_get_host_port_state,
2043 	.show_host_port_state = 1,
2044 	.show_host_symbolic_name = 1,
2045 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2046 				sizeof(struct qedf_rport)),
2047 	.show_rport_maxframe_size = 1,
2048 	.show_rport_supported_classes = 1,
2049 	.show_host_fabric_name = 1,
2050 	.show_starget_node_name = 1,
2051 	.show_starget_port_name = 1,
2052 	.show_starget_port_id = 1,
2053 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2054 	.show_rport_dev_loss_tmo = 1,
2055 	.get_fc_host_stats = fc_get_host_stats,
2056 	.issue_fc_host_lip = qedf_fcoe_reset,
2057 	.bsg_request = fc_lport_bsg_request,
2058 };
2059 
2060 static bool qedf_fp_has_work(struct qedf_fastpath *fp)
2061 {
2062 	struct qedf_ctx *qedf = fp->qedf;
2063 	struct global_queue *que;
2064 	struct qed_sb_info *sb_info = fp->sb_info;
2065 	struct status_block_e4 *sb = sb_info->sb_virt;
2066 	u16 prod_idx;
2067 
2068 	/* Get the pointer to the global CQ this completion is on */
2069 	que = qedf->global_queues[fp->sb_id];
2070 
2071 	/* Be sure all responses have been written to PI */
2072 	rmb();
2073 
2074 	/* Get the current firmware producer index */
2075 	prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
2076 
2077 	return (que->cq_prod_idx != prod_idx);
2078 }
2079 
2080 /*
2081  * Interrupt handler code.
2082  */
2083 
2084 /* Process completion queue and copy CQE contents for deferred processesing
2085  *
2086  * Return true if we should wake the I/O thread, false if not.
2087  */
2088 static bool qedf_process_completions(struct qedf_fastpath *fp)
2089 {
2090 	struct qedf_ctx *qedf = fp->qedf;
2091 	struct qed_sb_info *sb_info = fp->sb_info;
2092 	struct status_block_e4 *sb = sb_info->sb_virt;
2093 	struct global_queue *que;
2094 	u16 prod_idx;
2095 	struct fcoe_cqe *cqe;
2096 	struct qedf_io_work *io_work;
2097 	int num_handled = 0;
2098 	unsigned int cpu;
2099 	struct qedf_ioreq *io_req = NULL;
2100 	u16 xid;
2101 	u16 new_cqes;
2102 	u32 comp_type;
2103 
2104 	/* Get the current firmware producer index */
2105 	prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
2106 
2107 	/* Get the pointer to the global CQ this completion is on */
2108 	que = qedf->global_queues[fp->sb_id];
2109 
2110 	/* Calculate the amount of new elements since last processing */
2111 	new_cqes = (prod_idx >= que->cq_prod_idx) ?
2112 	    (prod_idx - que->cq_prod_idx) :
2113 	    0x10000 - que->cq_prod_idx + prod_idx;
2114 
2115 	/* Save producer index */
2116 	que->cq_prod_idx = prod_idx;
2117 
2118 	while (new_cqes) {
2119 		fp->completions++;
2120 		num_handled++;
2121 		cqe = &que->cq[que->cq_cons_idx];
2122 
2123 		comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
2124 		    FCOE_CQE_CQE_TYPE_MASK;
2125 
2126 		/*
2127 		 * Process unsolicited CQEs directly in the interrupt handler
2128 		 * sine we need the fastpath ID
2129 		 */
2130 		if (comp_type == FCOE_UNSOLIC_CQE_TYPE) {
2131 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
2132 			   "Unsolicated CQE.\n");
2133 			qedf_process_unsol_compl(qedf, fp->sb_id, cqe);
2134 			/*
2135 			 * Don't add a work list item.  Increment consumer
2136 			 * consumer index and move on.
2137 			 */
2138 			goto inc_idx;
2139 		}
2140 
2141 		xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
2142 		io_req = &qedf->cmd_mgr->cmds[xid];
2143 
2144 		/*
2145 		 * Figure out which percpu thread we should queue this I/O
2146 		 * on.
2147 		 */
2148 		if (!io_req)
2149 			/* If there is not io_req assocated with this CQE
2150 			 * just queue it on CPU 0
2151 			 */
2152 			cpu = 0;
2153 		else {
2154 			cpu = io_req->cpu;
2155 			io_req->int_cpu = smp_processor_id();
2156 		}
2157 
2158 		io_work = mempool_alloc(qedf->io_mempool, GFP_ATOMIC);
2159 		if (!io_work) {
2160 			QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate "
2161 				   "work for I/O completion.\n");
2162 			continue;
2163 		}
2164 		memset(io_work, 0, sizeof(struct qedf_io_work));
2165 
2166 		INIT_WORK(&io_work->work, qedf_fp_io_handler);
2167 
2168 		/* Copy contents of CQE for deferred processing */
2169 		memcpy(&io_work->cqe, cqe, sizeof(struct fcoe_cqe));
2170 
2171 		io_work->qedf = fp->qedf;
2172 		io_work->fp = NULL; /* Only used for unsolicited frames */
2173 
2174 		queue_work_on(cpu, qedf_io_wq, &io_work->work);
2175 
2176 inc_idx:
2177 		que->cq_cons_idx++;
2178 		if (que->cq_cons_idx == fp->cq_num_entries)
2179 			que->cq_cons_idx = 0;
2180 		new_cqes--;
2181 	}
2182 
2183 	return true;
2184 }
2185 
2186 
2187 /* MSI-X fastpath handler code */
2188 static irqreturn_t qedf_msix_handler(int irq, void *dev_id)
2189 {
2190 	struct qedf_fastpath *fp = dev_id;
2191 
2192 	if (!fp) {
2193 		QEDF_ERR(NULL, "fp is null.\n");
2194 		return IRQ_HANDLED;
2195 	}
2196 	if (!fp->sb_info) {
2197 		QEDF_ERR(NULL, "fp->sb_info in null.");
2198 		return IRQ_HANDLED;
2199 	}
2200 
2201 	/*
2202 	 * Disable interrupts for this status block while we process new
2203 	 * completions
2204 	 */
2205 	qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);
2206 
2207 	while (1) {
2208 		qedf_process_completions(fp);
2209 
2210 		if (qedf_fp_has_work(fp) == 0) {
2211 			/* Update the sb information */
2212 			qed_sb_update_sb_idx(fp->sb_info);
2213 
2214 			/* Check for more work */
2215 			rmb();
2216 
2217 			if (qedf_fp_has_work(fp) == 0) {
2218 				/* Re-enable interrupts */
2219 				qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
2220 				return IRQ_HANDLED;
2221 			}
2222 		}
2223 	}
2224 
2225 	/* Do we ever want to break out of above loop? */
2226 	return IRQ_HANDLED;
2227 }
2228 
2229 /* simd handler for MSI/INTa */
2230 static void qedf_simd_int_handler(void *cookie)
2231 {
2232 	/* Cookie is qedf_ctx struct */
2233 	struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
2234 
2235 	QEDF_WARN(&(qedf->dbg_ctx), "qedf=%p.\n", qedf);
2236 }
2237 
2238 #define QEDF_SIMD_HANDLER_NUM		0
2239 static void qedf_sync_free_irqs(struct qedf_ctx *qedf)
2240 {
2241 	int i;
2242 	u16 vector_idx = 0;
2243 	u32 vector;
2244 
2245 	if (qedf->int_info.msix_cnt) {
2246 		for (i = 0; i < qedf->int_info.used_cnt; i++) {
2247 			vector_idx = i * qedf->dev_info.common.num_hwfns +
2248 				qed_ops->common->get_affin_hwfn_idx(qedf->cdev);
2249 			QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
2250 				  "Freeing IRQ #%d vector_idx=%d.\n",
2251 				  i, vector_idx);
2252 			vector = qedf->int_info.msix[vector_idx].vector;
2253 			synchronize_irq(vector);
2254 			irq_set_affinity_hint(vector, NULL);
2255 			irq_set_affinity_notifier(vector, NULL);
2256 			free_irq(vector, &qedf->fp_array[i]);
2257 		}
2258 	} else
2259 		qed_ops->common->simd_handler_clean(qedf->cdev,
2260 		    QEDF_SIMD_HANDLER_NUM);
2261 
2262 	qedf->int_info.used_cnt = 0;
2263 	qed_ops->common->set_fp_int(qedf->cdev, 0);
2264 }
2265 
2266 static int qedf_request_msix_irq(struct qedf_ctx *qedf)
2267 {
2268 	int i, rc, cpu;
2269 	u16 vector_idx = 0;
2270 	u32 vector;
2271 
2272 	cpu = cpumask_first(cpu_online_mask);
2273 	for (i = 0; i < qedf->num_queues; i++) {
2274 		vector_idx = i * qedf->dev_info.common.num_hwfns +
2275 			qed_ops->common->get_affin_hwfn_idx(qedf->cdev);
2276 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
2277 			  "Requesting IRQ #%d vector_idx=%d.\n",
2278 			  i, vector_idx);
2279 		vector = qedf->int_info.msix[vector_idx].vector;
2280 		rc = request_irq(vector, qedf_msix_handler, 0, "qedf",
2281 				 &qedf->fp_array[i]);
2282 
2283 		if (rc) {
2284 			QEDF_WARN(&(qedf->dbg_ctx), "request_irq failed.\n");
2285 			qedf_sync_free_irqs(qedf);
2286 			return rc;
2287 		}
2288 
2289 		qedf->int_info.used_cnt++;
2290 		rc = irq_set_affinity_hint(vector, get_cpu_mask(cpu));
2291 		cpu = cpumask_next(cpu, cpu_online_mask);
2292 	}
2293 
2294 	return 0;
2295 }
2296 
2297 static int qedf_setup_int(struct qedf_ctx *qedf)
2298 {
2299 	int rc = 0;
2300 
2301 	/*
2302 	 * Learn interrupt configuration
2303 	 */
2304 	rc = qed_ops->common->set_fp_int(qedf->cdev, num_online_cpus());
2305 	if (rc <= 0)
2306 		return 0;
2307 
2308 	rc  = qed_ops->common->get_fp_int(qedf->cdev, &qedf->int_info);
2309 	if (rc)
2310 		return 0;
2311 
2312 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of msix_cnt = "
2313 		   "0x%x num of cpus = 0x%x\n", qedf->int_info.msix_cnt,
2314 		   num_online_cpus());
2315 
2316 	if (qedf->int_info.msix_cnt)
2317 		return qedf_request_msix_irq(qedf);
2318 
2319 	qed_ops->common->simd_handler_config(qedf->cdev, &qedf,
2320 	    QEDF_SIMD_HANDLER_NUM, qedf_simd_int_handler);
2321 	qedf->int_info.used_cnt = 1;
2322 
2323 	QEDF_ERR(&qedf->dbg_ctx,
2324 		 "Cannot load driver due to a lack of MSI-X vectors.\n");
2325 	return -EINVAL;
2326 }
2327 
2328 /* Main function for libfc frame reception */
2329 static void qedf_recv_frame(struct qedf_ctx *qedf,
2330 	struct sk_buff *skb)
2331 {
2332 	u32 fr_len;
2333 	struct fc_lport *lport;
2334 	struct fc_frame_header *fh;
2335 	struct fcoe_crc_eof crc_eof;
2336 	struct fc_frame *fp;
2337 	u8 *mac = NULL;
2338 	u8 *dest_mac = NULL;
2339 	struct fcoe_hdr *hp;
2340 	struct qedf_rport *fcport;
2341 	struct fc_lport *vn_port;
2342 	u32 f_ctl;
2343 
2344 	lport = qedf->lport;
2345 	if (lport == NULL || lport->state == LPORT_ST_DISABLED) {
2346 		QEDF_WARN(NULL, "Invalid lport struct or lport disabled.\n");
2347 		kfree_skb(skb);
2348 		return;
2349 	}
2350 
2351 	if (skb_is_nonlinear(skb))
2352 		skb_linearize(skb);
2353 	mac = eth_hdr(skb)->h_source;
2354 	dest_mac = eth_hdr(skb)->h_dest;
2355 
2356 	/* Pull the header */
2357 	hp = (struct fcoe_hdr *)skb->data;
2358 	fh = (struct fc_frame_header *) skb_transport_header(skb);
2359 	skb_pull(skb, sizeof(struct fcoe_hdr));
2360 	fr_len = skb->len - sizeof(struct fcoe_crc_eof);
2361 
2362 	fp = (struct fc_frame *)skb;
2363 	fc_frame_init(fp);
2364 	fr_dev(fp) = lport;
2365 	fr_sof(fp) = hp->fcoe_sof;
2366 	if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
2367 		QEDF_INFO(NULL, QEDF_LOG_LL2, "skb_copy_bits failed.\n");
2368 		kfree_skb(skb);
2369 		return;
2370 	}
2371 	fr_eof(fp) = crc_eof.fcoe_eof;
2372 	fr_crc(fp) = crc_eof.fcoe_crc32;
2373 	if (pskb_trim(skb, fr_len)) {
2374 		QEDF_INFO(NULL, QEDF_LOG_LL2, "pskb_trim failed.\n");
2375 		kfree_skb(skb);
2376 		return;
2377 	}
2378 
2379 	fh = fc_frame_header_get(fp);
2380 
2381 	/*
2382 	 * Invalid frame filters.
2383 	 */
2384 
2385 	if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
2386 	    fh->fh_type == FC_TYPE_FCP) {
2387 		/* Drop FCP data. We dont this in L2 path */
2388 		kfree_skb(skb);
2389 		return;
2390 	}
2391 	if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
2392 	    fh->fh_type == FC_TYPE_ELS) {
2393 		switch (fc_frame_payload_op(fp)) {
2394 		case ELS_LOGO:
2395 			if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
2396 				/* drop non-FIP LOGO */
2397 				kfree_skb(skb);
2398 				return;
2399 			}
2400 			break;
2401 		}
2402 	}
2403 
2404 	if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
2405 		/* Drop incoming ABTS */
2406 		kfree_skb(skb);
2407 		return;
2408 	}
2409 
2410 	if (ntoh24(&dest_mac[3]) != ntoh24(fh->fh_d_id)) {
2411 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2412 		    "FC frame d_id mismatch with MAC %pM.\n", dest_mac);
2413 		kfree_skb(skb);
2414 		return;
2415 	}
2416 
2417 	if (qedf->ctlr.state) {
2418 		if (!ether_addr_equal(mac, qedf->ctlr.dest_addr)) {
2419 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2420 			    "Wrong source address: mac:%pM dest_addr:%pM.\n",
2421 			    mac, qedf->ctlr.dest_addr);
2422 			kfree_skb(skb);
2423 			return;
2424 		}
2425 	}
2426 
2427 	vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
2428 
2429 	/*
2430 	 * If the destination ID from the frame header does not match what we
2431 	 * have on record for lport and the search for a NPIV port came up
2432 	 * empty then this is not addressed to our port so simply drop it.
2433 	 */
2434 	if (lport->port_id != ntoh24(fh->fh_d_id) && !vn_port) {
2435 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
2436 			  "Dropping frame due to destination mismatch: lport->port_id=0x%x fh->d_id=0x%x.\n",
2437 			  lport->port_id, ntoh24(fh->fh_d_id));
2438 		kfree_skb(skb);
2439 		return;
2440 	}
2441 
2442 	f_ctl = ntoh24(fh->fh_f_ctl);
2443 	if ((fh->fh_type == FC_TYPE_BLS) && (f_ctl & FC_FC_SEQ_CTX) &&
2444 	    (f_ctl & FC_FC_EX_CTX)) {
2445 		/* Drop incoming ABTS response that has both SEQ/EX CTX set */
2446 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
2447 			  "Dropping ABTS response as both SEQ/EX CTX set.\n");
2448 		kfree_skb(skb);
2449 		return;
2450 	}
2451 
2452 	/*
2453 	 * If a connection is uploading, drop incoming FCoE frames as there
2454 	 * is a small window where we could try to return a frame while libfc
2455 	 * is trying to clean things up.
2456 	 */
2457 
2458 	/* Get fcport associated with d_id if it exists */
2459 	fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
2460 
2461 	if (fcport && test_bit(QEDF_RPORT_UPLOADING_CONNECTION,
2462 	    &fcport->flags)) {
2463 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2464 		    "Connection uploading, dropping fp=%p.\n", fp);
2465 		kfree_skb(skb);
2466 		return;
2467 	}
2468 
2469 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame receive: "
2470 	    "skb=%p fp=%p src=%06x dest=%06x r_ctl=%x fh_type=%x.\n", skb, fp,
2471 	    ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl,
2472 	    fh->fh_type);
2473 	if (qedf_dump_frames)
2474 		print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
2475 		    1, skb->data, skb->len, false);
2476 	fc_exch_recv(lport, fp);
2477 }
2478 
2479 static void qedf_ll2_process_skb(struct work_struct *work)
2480 {
2481 	struct qedf_skb_work *skb_work =
2482 	    container_of(work, struct qedf_skb_work, work);
2483 	struct qedf_ctx *qedf = skb_work->qedf;
2484 	struct sk_buff *skb = skb_work->skb;
2485 	struct ethhdr *eh;
2486 
2487 	if (!qedf) {
2488 		QEDF_ERR(NULL, "qedf is NULL\n");
2489 		goto err_out;
2490 	}
2491 
2492 	eh = (struct ethhdr *)skb->data;
2493 
2494 	/* Undo VLAN encapsulation */
2495 	if (eh->h_proto == htons(ETH_P_8021Q)) {
2496 		memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
2497 		eh = skb_pull(skb, VLAN_HLEN);
2498 		skb_reset_mac_header(skb);
2499 	}
2500 
2501 	/*
2502 	 * Process either a FIP frame or FCoE frame based on the
2503 	 * protocol value.  If it's not either just drop the
2504 	 * frame.
2505 	 */
2506 	if (eh->h_proto == htons(ETH_P_FIP)) {
2507 		qedf_fip_recv(qedf, skb);
2508 		goto out;
2509 	} else if (eh->h_proto == htons(ETH_P_FCOE)) {
2510 		__skb_pull(skb, ETH_HLEN);
2511 		qedf_recv_frame(qedf, skb);
2512 		goto out;
2513 	} else
2514 		goto err_out;
2515 
2516 err_out:
2517 	kfree_skb(skb);
2518 out:
2519 	kfree(skb_work);
2520 	return;
2521 }
2522 
2523 static int qedf_ll2_rx(void *cookie, struct sk_buff *skb,
2524 	u32 arg1, u32 arg2)
2525 {
2526 	struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
2527 	struct qedf_skb_work *skb_work;
2528 
2529 	if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
2530 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
2531 			  "Dropping frame as link state is down.\n");
2532 		kfree_skb(skb);
2533 		return 0;
2534 	}
2535 
2536 	skb_work = kzalloc(sizeof(struct qedf_skb_work), GFP_ATOMIC);
2537 	if (!skb_work) {
2538 		QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate skb_work so "
2539 			   "dropping frame.\n");
2540 		kfree_skb(skb);
2541 		return 0;
2542 	}
2543 
2544 	INIT_WORK(&skb_work->work, qedf_ll2_process_skb);
2545 	skb_work->skb = skb;
2546 	skb_work->qedf = qedf;
2547 	queue_work(qedf->ll2_recv_wq, &skb_work->work);
2548 
2549 	return 0;
2550 }
2551 
2552 static struct qed_ll2_cb_ops qedf_ll2_cb_ops = {
2553 	.rx_cb = qedf_ll2_rx,
2554 	.tx_cb = NULL,
2555 };
2556 
2557 /* Main thread to process I/O completions */
2558 void qedf_fp_io_handler(struct work_struct *work)
2559 {
2560 	struct qedf_io_work *io_work =
2561 	    container_of(work, struct qedf_io_work, work);
2562 	u32 comp_type;
2563 
2564 	/*
2565 	 * Deferred part of unsolicited CQE sends
2566 	 * frame to libfc.
2567 	 */
2568 	comp_type = (io_work->cqe.cqe_data >>
2569 	    FCOE_CQE_CQE_TYPE_SHIFT) &
2570 	    FCOE_CQE_CQE_TYPE_MASK;
2571 	if (comp_type == FCOE_UNSOLIC_CQE_TYPE &&
2572 	    io_work->fp)
2573 		fc_exch_recv(io_work->qedf->lport, io_work->fp);
2574 	else
2575 		qedf_process_cqe(io_work->qedf, &io_work->cqe);
2576 
2577 	kfree(io_work);
2578 }
2579 
2580 static int qedf_alloc_and_init_sb(struct qedf_ctx *qedf,
2581 	struct qed_sb_info *sb_info, u16 sb_id)
2582 {
2583 	struct status_block_e4 *sb_virt;
2584 	dma_addr_t sb_phys;
2585 	int ret;
2586 
2587 	sb_virt = dma_alloc_coherent(&qedf->pdev->dev,
2588 	    sizeof(struct status_block_e4), &sb_phys, GFP_KERNEL);
2589 
2590 	if (!sb_virt) {
2591 		QEDF_ERR(&qedf->dbg_ctx,
2592 			 "Status block allocation failed for id = %d.\n",
2593 			 sb_id);
2594 		return -ENOMEM;
2595 	}
2596 
2597 	ret = qed_ops->common->sb_init(qedf->cdev, sb_info, sb_virt, sb_phys,
2598 	    sb_id, QED_SB_TYPE_STORAGE);
2599 
2600 	if (ret) {
2601 		QEDF_ERR(&qedf->dbg_ctx,
2602 			 "Status block initialization failed (0x%x) for id = %d.\n",
2603 			 ret, sb_id);
2604 		return ret;
2605 	}
2606 
2607 	return 0;
2608 }
2609 
2610 static void qedf_free_sb(struct qedf_ctx *qedf, struct qed_sb_info *sb_info)
2611 {
2612 	if (sb_info->sb_virt)
2613 		dma_free_coherent(&qedf->pdev->dev, sizeof(*sb_info->sb_virt),
2614 		    (void *)sb_info->sb_virt, sb_info->sb_phys);
2615 }
2616 
2617 static void qedf_destroy_sb(struct qedf_ctx *qedf)
2618 {
2619 	int id;
2620 	struct qedf_fastpath *fp = NULL;
2621 
2622 	for (id = 0; id < qedf->num_queues; id++) {
2623 		fp = &(qedf->fp_array[id]);
2624 		if (fp->sb_id == QEDF_SB_ID_NULL)
2625 			break;
2626 		qedf_free_sb(qedf, fp->sb_info);
2627 		kfree(fp->sb_info);
2628 	}
2629 	kfree(qedf->fp_array);
2630 }
2631 
2632 static int qedf_prepare_sb(struct qedf_ctx *qedf)
2633 {
2634 	int id;
2635 	struct qedf_fastpath *fp;
2636 	int ret;
2637 
2638 	qedf->fp_array =
2639 	    kcalloc(qedf->num_queues, sizeof(struct qedf_fastpath),
2640 		GFP_KERNEL);
2641 
2642 	if (!qedf->fp_array) {
2643 		QEDF_ERR(&(qedf->dbg_ctx), "fastpath array allocation "
2644 			  "failed.\n");
2645 		return -ENOMEM;
2646 	}
2647 
2648 	for (id = 0; id < qedf->num_queues; id++) {
2649 		fp = &(qedf->fp_array[id]);
2650 		fp->sb_id = QEDF_SB_ID_NULL;
2651 		fp->sb_info = kcalloc(1, sizeof(*fp->sb_info), GFP_KERNEL);
2652 		if (!fp->sb_info) {
2653 			QEDF_ERR(&(qedf->dbg_ctx), "SB info struct "
2654 				  "allocation failed.\n");
2655 			goto err;
2656 		}
2657 		ret = qedf_alloc_and_init_sb(qedf, fp->sb_info, id);
2658 		if (ret) {
2659 			QEDF_ERR(&(qedf->dbg_ctx), "SB allocation and "
2660 				  "initialization failed.\n");
2661 			goto err;
2662 		}
2663 		fp->sb_id = id;
2664 		fp->qedf = qedf;
2665 		fp->cq_num_entries =
2666 		    qedf->global_queues[id]->cq_mem_size /
2667 		    sizeof(struct fcoe_cqe);
2668 	}
2669 err:
2670 	return 0;
2671 }
2672 
2673 void qedf_process_cqe(struct qedf_ctx *qedf, struct fcoe_cqe *cqe)
2674 {
2675 	u16 xid;
2676 	struct qedf_ioreq *io_req;
2677 	struct qedf_rport *fcport;
2678 	u32 comp_type;
2679 
2680 	comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
2681 	    FCOE_CQE_CQE_TYPE_MASK;
2682 
2683 	xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
2684 	io_req = &qedf->cmd_mgr->cmds[xid];
2685 
2686 	/* Completion not for a valid I/O anymore so just return */
2687 	if (!io_req) {
2688 		QEDF_ERR(&qedf->dbg_ctx,
2689 			 "io_req is NULL for xid=0x%x.\n", xid);
2690 		return;
2691 	}
2692 
2693 	fcport = io_req->fcport;
2694 
2695 	if (fcport == NULL) {
2696 		QEDF_ERR(&qedf->dbg_ctx,
2697 			 "fcport is NULL for xid=0x%x io_req=%p.\n",
2698 			 xid, io_req);
2699 		return;
2700 	}
2701 
2702 	/*
2703 	 * Check that fcport is offloaded.  If it isn't then the spinlock
2704 	 * isn't valid and shouldn't be taken. We should just return.
2705 	 */
2706 	if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
2707 		QEDF_ERR(&qedf->dbg_ctx,
2708 			 "Session not offloaded yet, fcport = %p.\n", fcport);
2709 		return;
2710 	}
2711 
2712 
2713 	switch (comp_type) {
2714 	case FCOE_GOOD_COMPLETION_CQE_TYPE:
2715 		atomic_inc(&fcport->free_sqes);
2716 		switch (io_req->cmd_type) {
2717 		case QEDF_SCSI_CMD:
2718 			qedf_scsi_completion(qedf, cqe, io_req);
2719 			break;
2720 		case QEDF_ELS:
2721 			qedf_process_els_compl(qedf, cqe, io_req);
2722 			break;
2723 		case QEDF_TASK_MGMT_CMD:
2724 			qedf_process_tmf_compl(qedf, cqe, io_req);
2725 			break;
2726 		case QEDF_SEQ_CLEANUP:
2727 			qedf_process_seq_cleanup_compl(qedf, cqe, io_req);
2728 			break;
2729 		}
2730 		break;
2731 	case FCOE_ERROR_DETECTION_CQE_TYPE:
2732 		atomic_inc(&fcport->free_sqes);
2733 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2734 		    "Error detect CQE.\n");
2735 		qedf_process_error_detect(qedf, cqe, io_req);
2736 		break;
2737 	case FCOE_EXCH_CLEANUP_CQE_TYPE:
2738 		atomic_inc(&fcport->free_sqes);
2739 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2740 		    "Cleanup CQE.\n");
2741 		qedf_process_cleanup_compl(qedf, cqe, io_req);
2742 		break;
2743 	case FCOE_ABTS_CQE_TYPE:
2744 		atomic_inc(&fcport->free_sqes);
2745 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2746 		    "Abort CQE.\n");
2747 		qedf_process_abts_compl(qedf, cqe, io_req);
2748 		break;
2749 	case FCOE_DUMMY_CQE_TYPE:
2750 		atomic_inc(&fcport->free_sqes);
2751 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2752 		    "Dummy CQE.\n");
2753 		break;
2754 	case FCOE_LOCAL_COMP_CQE_TYPE:
2755 		atomic_inc(&fcport->free_sqes);
2756 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2757 		    "Local completion CQE.\n");
2758 		break;
2759 	case FCOE_WARNING_CQE_TYPE:
2760 		atomic_inc(&fcport->free_sqes);
2761 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2762 		    "Warning CQE.\n");
2763 		qedf_process_warning_compl(qedf, cqe, io_req);
2764 		break;
2765 	case MAX_FCOE_CQE_TYPE:
2766 		atomic_inc(&fcport->free_sqes);
2767 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2768 		    "Max FCoE CQE.\n");
2769 		break;
2770 	default:
2771 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2772 		    "Default CQE.\n");
2773 		break;
2774 	}
2775 }
2776 
2777 static void qedf_free_bdq(struct qedf_ctx *qedf)
2778 {
2779 	int i;
2780 
2781 	if (qedf->bdq_pbl_list)
2782 		dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
2783 		    qedf->bdq_pbl_list, qedf->bdq_pbl_list_dma);
2784 
2785 	if (qedf->bdq_pbl)
2786 		dma_free_coherent(&qedf->pdev->dev, qedf->bdq_pbl_mem_size,
2787 		    qedf->bdq_pbl, qedf->bdq_pbl_dma);
2788 
2789 	for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2790 		if (qedf->bdq[i].buf_addr) {
2791 			dma_free_coherent(&qedf->pdev->dev, QEDF_BDQ_BUF_SIZE,
2792 			    qedf->bdq[i].buf_addr, qedf->bdq[i].buf_dma);
2793 		}
2794 	}
2795 }
2796 
2797 static void qedf_free_global_queues(struct qedf_ctx *qedf)
2798 {
2799 	int i;
2800 	struct global_queue **gl = qedf->global_queues;
2801 
2802 	for (i = 0; i < qedf->num_queues; i++) {
2803 		if (!gl[i])
2804 			continue;
2805 
2806 		if (gl[i]->cq)
2807 			dma_free_coherent(&qedf->pdev->dev,
2808 			    gl[i]->cq_mem_size, gl[i]->cq, gl[i]->cq_dma);
2809 		if (gl[i]->cq_pbl)
2810 			dma_free_coherent(&qedf->pdev->dev, gl[i]->cq_pbl_size,
2811 			    gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
2812 
2813 		kfree(gl[i]);
2814 	}
2815 
2816 	qedf_free_bdq(qedf);
2817 }
2818 
2819 static int qedf_alloc_bdq(struct qedf_ctx *qedf)
2820 {
2821 	int i;
2822 	struct scsi_bd *pbl;
2823 	u64 *list;
2824 	dma_addr_t page;
2825 
2826 	/* Alloc dma memory for BDQ buffers */
2827 	for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2828 		qedf->bdq[i].buf_addr = dma_alloc_coherent(&qedf->pdev->dev,
2829 		    QEDF_BDQ_BUF_SIZE, &qedf->bdq[i].buf_dma, GFP_KERNEL);
2830 		if (!qedf->bdq[i].buf_addr) {
2831 			QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ "
2832 			    "buffer %d.\n", i);
2833 			return -ENOMEM;
2834 		}
2835 	}
2836 
2837 	/* Alloc dma memory for BDQ page buffer list */
2838 	qedf->bdq_pbl_mem_size =
2839 	    QEDF_BDQ_SIZE * sizeof(struct scsi_bd);
2840 	qedf->bdq_pbl_mem_size =
2841 	    ALIGN(qedf->bdq_pbl_mem_size, QEDF_PAGE_SIZE);
2842 
2843 	qedf->bdq_pbl = dma_alloc_coherent(&qedf->pdev->dev,
2844 	    qedf->bdq_pbl_mem_size, &qedf->bdq_pbl_dma, GFP_KERNEL);
2845 	if (!qedf->bdq_pbl) {
2846 		QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ PBL.\n");
2847 		return -ENOMEM;
2848 	}
2849 
2850 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2851 		  "BDQ PBL addr=0x%p dma=%pad\n",
2852 		  qedf->bdq_pbl, &qedf->bdq_pbl_dma);
2853 
2854 	/*
2855 	 * Populate BDQ PBL with physical and virtual address of individual
2856 	 * BDQ buffers
2857 	 */
2858 	pbl = (struct scsi_bd *)qedf->bdq_pbl;
2859 	for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2860 		pbl->address.hi = cpu_to_le32(U64_HI(qedf->bdq[i].buf_dma));
2861 		pbl->address.lo = cpu_to_le32(U64_LO(qedf->bdq[i].buf_dma));
2862 		pbl->opaque.fcoe_opaque.hi = 0;
2863 		/* Opaque lo data is an index into the BDQ array */
2864 		pbl->opaque.fcoe_opaque.lo = cpu_to_le32(i);
2865 		pbl++;
2866 	}
2867 
2868 	/* Allocate list of PBL pages */
2869 	qedf->bdq_pbl_list = dma_alloc_coherent(&qedf->pdev->dev,
2870 						QEDF_PAGE_SIZE,
2871 						&qedf->bdq_pbl_list_dma,
2872 						GFP_KERNEL);
2873 	if (!qedf->bdq_pbl_list) {
2874 		QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate list of PBL pages.\n");
2875 		return -ENOMEM;
2876 	}
2877 
2878 	/*
2879 	 * Now populate PBL list with pages that contain pointers to the
2880 	 * individual buffers.
2881 	 */
2882 	qedf->bdq_pbl_list_num_entries = qedf->bdq_pbl_mem_size /
2883 	    QEDF_PAGE_SIZE;
2884 	list = (u64 *)qedf->bdq_pbl_list;
2885 	page = qedf->bdq_pbl_list_dma;
2886 	for (i = 0; i < qedf->bdq_pbl_list_num_entries; i++) {
2887 		*list = qedf->bdq_pbl_dma;
2888 		list++;
2889 		page += QEDF_PAGE_SIZE;
2890 	}
2891 
2892 	return 0;
2893 }
2894 
2895 static int qedf_alloc_global_queues(struct qedf_ctx *qedf)
2896 {
2897 	u32 *list;
2898 	int i;
2899 	int status = 0, rc;
2900 	u32 *pbl;
2901 	dma_addr_t page;
2902 	int num_pages;
2903 
2904 	/* Allocate and map CQs, RQs */
2905 	/*
2906 	 * Number of global queues (CQ / RQ). This should
2907 	 * be <= number of available MSIX vectors for the PF
2908 	 */
2909 	if (!qedf->num_queues) {
2910 		QEDF_ERR(&(qedf->dbg_ctx), "No MSI-X vectors available!\n");
2911 		return 1;
2912 	}
2913 
2914 	/*
2915 	 * Make sure we allocated the PBL that will contain the physical
2916 	 * addresses of our queues
2917 	 */
2918 	if (!qedf->p_cpuq) {
2919 		status = 1;
2920 		QEDF_ERR(&qedf->dbg_ctx, "p_cpuq is NULL.\n");
2921 		goto mem_alloc_failure;
2922 	}
2923 
2924 	qedf->global_queues = kzalloc((sizeof(struct global_queue *)
2925 	    * qedf->num_queues), GFP_KERNEL);
2926 	if (!qedf->global_queues) {
2927 		QEDF_ERR(&(qedf->dbg_ctx), "Unable to allocate global "
2928 			  "queues array ptr memory\n");
2929 		return -ENOMEM;
2930 	}
2931 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2932 		   "qedf->global_queues=%p.\n", qedf->global_queues);
2933 
2934 	/* Allocate DMA coherent buffers for BDQ */
2935 	rc = qedf_alloc_bdq(qedf);
2936 	if (rc) {
2937 		QEDF_ERR(&qedf->dbg_ctx, "Unable to allocate bdq.\n");
2938 		goto mem_alloc_failure;
2939 	}
2940 
2941 	/* Allocate a CQ and an associated PBL for each MSI-X vector */
2942 	for (i = 0; i < qedf->num_queues; i++) {
2943 		qedf->global_queues[i] = kzalloc(sizeof(struct global_queue),
2944 		    GFP_KERNEL);
2945 		if (!qedf->global_queues[i]) {
2946 			QEDF_WARN(&(qedf->dbg_ctx), "Unable to allocate "
2947 				   "global queue %d.\n", i);
2948 			status = -ENOMEM;
2949 			goto mem_alloc_failure;
2950 		}
2951 
2952 		qedf->global_queues[i]->cq_mem_size =
2953 		    FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
2954 		qedf->global_queues[i]->cq_mem_size =
2955 		    ALIGN(qedf->global_queues[i]->cq_mem_size, QEDF_PAGE_SIZE);
2956 
2957 		qedf->global_queues[i]->cq_pbl_size =
2958 		    (qedf->global_queues[i]->cq_mem_size /
2959 		    PAGE_SIZE) * sizeof(void *);
2960 		qedf->global_queues[i]->cq_pbl_size =
2961 		    ALIGN(qedf->global_queues[i]->cq_pbl_size, QEDF_PAGE_SIZE);
2962 
2963 		qedf->global_queues[i]->cq =
2964 		    dma_alloc_coherent(&qedf->pdev->dev,
2965 				       qedf->global_queues[i]->cq_mem_size,
2966 				       &qedf->global_queues[i]->cq_dma,
2967 				       GFP_KERNEL);
2968 
2969 		if (!qedf->global_queues[i]->cq) {
2970 			QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq.\n");
2971 			status = -ENOMEM;
2972 			goto mem_alloc_failure;
2973 		}
2974 
2975 		qedf->global_queues[i]->cq_pbl =
2976 		    dma_alloc_coherent(&qedf->pdev->dev,
2977 				       qedf->global_queues[i]->cq_pbl_size,
2978 				       &qedf->global_queues[i]->cq_pbl_dma,
2979 				       GFP_KERNEL);
2980 
2981 		if (!qedf->global_queues[i]->cq_pbl) {
2982 			QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq PBL.\n");
2983 			status = -ENOMEM;
2984 			goto mem_alloc_failure;
2985 		}
2986 
2987 		/* Create PBL */
2988 		num_pages = qedf->global_queues[i]->cq_mem_size /
2989 		    QEDF_PAGE_SIZE;
2990 		page = qedf->global_queues[i]->cq_dma;
2991 		pbl = (u32 *)qedf->global_queues[i]->cq_pbl;
2992 
2993 		while (num_pages--) {
2994 			*pbl = U64_LO(page);
2995 			pbl++;
2996 			*pbl = U64_HI(page);
2997 			pbl++;
2998 			page += QEDF_PAGE_SIZE;
2999 		}
3000 		/* Set the initial consumer index for cq */
3001 		qedf->global_queues[i]->cq_cons_idx = 0;
3002 	}
3003 
3004 	list = (u32 *)qedf->p_cpuq;
3005 
3006 	/*
3007 	 * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
3008 	 * CQ#1 PBL pointer, RQ#1 PBL pointer, etc.  Each PBL pointer points
3009 	 * to the physical address which contains an array of pointers to
3010 	 * the physical addresses of the specific queue pages.
3011 	 */
3012 	for (i = 0; i < qedf->num_queues; i++) {
3013 		*list = U64_LO(qedf->global_queues[i]->cq_pbl_dma);
3014 		list++;
3015 		*list = U64_HI(qedf->global_queues[i]->cq_pbl_dma);
3016 		list++;
3017 		*list = U64_LO(0);
3018 		list++;
3019 		*list = U64_HI(0);
3020 		list++;
3021 	}
3022 
3023 	return 0;
3024 
3025 mem_alloc_failure:
3026 	qedf_free_global_queues(qedf);
3027 	return status;
3028 }
3029 
3030 static int qedf_set_fcoe_pf_param(struct qedf_ctx *qedf)
3031 {
3032 	u8 sq_num_pbl_pages;
3033 	u32 sq_mem_size;
3034 	u32 cq_mem_size;
3035 	u32 cq_num_entries;
3036 	int rval;
3037 
3038 	/*
3039 	 * The number of completion queues/fastpath interrupts/status blocks
3040 	 * we allocation is the minimum off:
3041 	 *
3042 	 * Number of CPUs
3043 	 * Number allocated by qed for our PCI function
3044 	 */
3045 	qedf->num_queues = MIN_NUM_CPUS_MSIX(qedf);
3046 
3047 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of CQs is %d.\n",
3048 		   qedf->num_queues);
3049 
3050 	qedf->p_cpuq = dma_alloc_coherent(&qedf->pdev->dev,
3051 	    qedf->num_queues * sizeof(struct qedf_glbl_q_params),
3052 	    &qedf->hw_p_cpuq, GFP_KERNEL);
3053 
3054 	if (!qedf->p_cpuq) {
3055 		QEDF_ERR(&(qedf->dbg_ctx), "dma_alloc_coherent failed.\n");
3056 		return 1;
3057 	}
3058 
3059 	rval = qedf_alloc_global_queues(qedf);
3060 	if (rval) {
3061 		QEDF_ERR(&(qedf->dbg_ctx), "Global queue allocation "
3062 			  "failed.\n");
3063 		return 1;
3064 	}
3065 
3066 	/* Calculate SQ PBL size in the same manner as in qedf_sq_alloc() */
3067 	sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
3068 	sq_mem_size = ALIGN(sq_mem_size, QEDF_PAGE_SIZE);
3069 	sq_num_pbl_pages = (sq_mem_size / QEDF_PAGE_SIZE);
3070 
3071 	/* Calculate CQ num entries */
3072 	cq_mem_size = FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
3073 	cq_mem_size = ALIGN(cq_mem_size, QEDF_PAGE_SIZE);
3074 	cq_num_entries = cq_mem_size / sizeof(struct fcoe_cqe);
3075 
3076 	memset(&(qedf->pf_params), 0, sizeof(qedf->pf_params));
3077 
3078 	/* Setup the value for fcoe PF */
3079 	qedf->pf_params.fcoe_pf_params.num_cons = QEDF_MAX_SESSIONS;
3080 	qedf->pf_params.fcoe_pf_params.num_tasks = FCOE_PARAMS_NUM_TASKS;
3081 	qedf->pf_params.fcoe_pf_params.glbl_q_params_addr =
3082 	    (u64)qedf->hw_p_cpuq;
3083 	qedf->pf_params.fcoe_pf_params.sq_num_pbl_pages = sq_num_pbl_pages;
3084 
3085 	qedf->pf_params.fcoe_pf_params.rq_buffer_log_size = 0;
3086 
3087 	qedf->pf_params.fcoe_pf_params.cq_num_entries = cq_num_entries;
3088 	qedf->pf_params.fcoe_pf_params.num_cqs = qedf->num_queues;
3089 
3090 	/* log_page_size: 12 for 4KB pages */
3091 	qedf->pf_params.fcoe_pf_params.log_page_size = ilog2(QEDF_PAGE_SIZE);
3092 
3093 	qedf->pf_params.fcoe_pf_params.mtu = 9000;
3094 	qedf->pf_params.fcoe_pf_params.gl_rq_pi = QEDF_FCOE_PARAMS_GL_RQ_PI;
3095 	qedf->pf_params.fcoe_pf_params.gl_cmd_pi = QEDF_FCOE_PARAMS_GL_CMD_PI;
3096 
3097 	/* BDQ address and size */
3098 	qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0] =
3099 	    qedf->bdq_pbl_list_dma;
3100 	qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0] =
3101 	    qedf->bdq_pbl_list_num_entries;
3102 	qedf->pf_params.fcoe_pf_params.rq_buffer_size = QEDF_BDQ_BUF_SIZE;
3103 
3104 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3105 	    "bdq_list=%p bdq_pbl_list_dma=%llx bdq_pbl_list_entries=%d.\n",
3106 	    qedf->bdq_pbl_list,
3107 	    qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0],
3108 	    qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0]);
3109 
3110 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3111 	    "cq_num_entries=%d.\n",
3112 	    qedf->pf_params.fcoe_pf_params.cq_num_entries);
3113 
3114 	return 0;
3115 }
3116 
3117 /* Free DMA coherent memory for array of queue pointers we pass to qed */
3118 static void qedf_free_fcoe_pf_param(struct qedf_ctx *qedf)
3119 {
3120 	size_t size = 0;
3121 
3122 	if (qedf->p_cpuq) {
3123 		size = qedf->num_queues * sizeof(struct qedf_glbl_q_params);
3124 		dma_free_coherent(&qedf->pdev->dev, size, qedf->p_cpuq,
3125 		    qedf->hw_p_cpuq);
3126 	}
3127 
3128 	qedf_free_global_queues(qedf);
3129 
3130 	kfree(qedf->global_queues);
3131 }
3132 
3133 /*
3134  * PCI driver functions
3135  */
3136 
3137 static const struct pci_device_id qedf_pci_tbl[] = {
3138 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165c) },
3139 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8080) },
3140 	{0}
3141 };
3142 MODULE_DEVICE_TABLE(pci, qedf_pci_tbl);
3143 
3144 static struct pci_driver qedf_pci_driver = {
3145 	.name = QEDF_MODULE_NAME,
3146 	.id_table = qedf_pci_tbl,
3147 	.probe = qedf_probe,
3148 	.remove = qedf_remove,
3149 	.shutdown = qedf_shutdown,
3150 };
3151 
3152 static int __qedf_probe(struct pci_dev *pdev, int mode)
3153 {
3154 	int rc = -EINVAL;
3155 	struct fc_lport *lport;
3156 	struct qedf_ctx *qedf;
3157 	struct Scsi_Host *host;
3158 	bool is_vf = false;
3159 	struct qed_ll2_params params;
3160 	char host_buf[20];
3161 	struct qed_link_params link_params;
3162 	int status;
3163 	void *task_start, *task_end;
3164 	struct qed_slowpath_params slowpath_params;
3165 	struct qed_probe_params qed_params;
3166 	u16 tmp;
3167 
3168 	/*
3169 	 * When doing error recovery we didn't reap the lport so don't try
3170 	 * to reallocate it.
3171 	 */
3172 	if (mode != QEDF_MODE_RECOVERY) {
3173 		lport = libfc_host_alloc(&qedf_host_template,
3174 		    sizeof(struct qedf_ctx));
3175 
3176 		if (!lport) {
3177 			QEDF_ERR(NULL, "Could not allocate lport.\n");
3178 			rc = -ENOMEM;
3179 			goto err0;
3180 		}
3181 
3182 		fc_disc_init(lport);
3183 
3184 		/* Initialize qedf_ctx */
3185 		qedf = lport_priv(lport);
3186 		qedf->lport = lport;
3187 		qedf->ctlr.lp = lport;
3188 		qedf->pdev = pdev;
3189 		qedf->dbg_ctx.pdev = pdev;
3190 		qedf->dbg_ctx.host_no = lport->host->host_no;
3191 		spin_lock_init(&qedf->hba_lock);
3192 		INIT_LIST_HEAD(&qedf->fcports);
3193 		qedf->curr_conn_id = QEDF_MAX_SESSIONS - 1;
3194 		atomic_set(&qedf->num_offloads, 0);
3195 		qedf->stop_io_on_error = false;
3196 		pci_set_drvdata(pdev, qedf);
3197 		init_completion(&qedf->fipvlan_compl);
3198 		mutex_init(&qedf->stats_mutex);
3199 		mutex_init(&qedf->flush_mutex);
3200 
3201 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO,
3202 		   "QLogic FastLinQ FCoE Module qedf %s, "
3203 		   "FW %d.%d.%d.%d\n", QEDF_VERSION,
3204 		   FW_MAJOR_VERSION, FW_MINOR_VERSION, FW_REVISION_VERSION,
3205 		   FW_ENGINEERING_VERSION);
3206 	} else {
3207 		/* Init pointers during recovery */
3208 		qedf = pci_get_drvdata(pdev);
3209 		lport = qedf->lport;
3210 	}
3211 
3212 	host = lport->host;
3213 
3214 	/* Allocate mempool for qedf_io_work structs */
3215 	qedf->io_mempool = mempool_create_slab_pool(QEDF_IO_WORK_MIN,
3216 	    qedf_io_work_cache);
3217 	if (qedf->io_mempool == NULL) {
3218 		QEDF_ERR(&(qedf->dbg_ctx), "qedf->io_mempool is NULL.\n");
3219 		goto err1;
3220 	}
3221 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO, "qedf->io_mempool=%p.\n",
3222 	    qedf->io_mempool);
3223 
3224 	sprintf(host_buf, "qedf_%u_link",
3225 	    qedf->lport->host->host_no);
3226 	qedf->link_update_wq = create_workqueue(host_buf);
3227 	INIT_DELAYED_WORK(&qedf->link_update, qedf_handle_link_update);
3228 	INIT_DELAYED_WORK(&qedf->link_recovery, qedf_link_recovery);
3229 	INIT_DELAYED_WORK(&qedf->grcdump_work, qedf_wq_grcdump);
3230 	qedf->fipvlan_retries = qedf_fipvlan_retries;
3231 	/* Set a default prio in case DCBX doesn't converge */
3232 	if (qedf_default_prio > -1) {
3233 		/*
3234 		 * This is the case where we pass a modparam in so we want to
3235 		 * honor it even if dcbx doesn't converge.
3236 		 */
3237 		qedf->prio = qedf_default_prio;
3238 	} else
3239 		qedf->prio = QEDF_DEFAULT_PRIO;
3240 
3241 	/*
3242 	 * Common probe. Takes care of basic hardware init and pci_*
3243 	 * functions.
3244 	 */
3245 	memset(&qed_params, 0, sizeof(qed_params));
3246 	qed_params.protocol = QED_PROTOCOL_FCOE;
3247 	qed_params.dp_module = qedf_dp_module;
3248 	qed_params.dp_level = qedf_dp_level;
3249 	qed_params.is_vf = is_vf;
3250 	qedf->cdev = qed_ops->common->probe(pdev, &qed_params);
3251 	if (!qedf->cdev) {
3252 		QEDF_ERR(&qedf->dbg_ctx, "common probe failed.\n");
3253 		rc = -ENODEV;
3254 		goto err1;
3255 	}
3256 
3257 	/* Learn information crucial for qedf to progress */
3258 	rc = qed_ops->fill_dev_info(qedf->cdev, &qedf->dev_info);
3259 	if (rc) {
3260 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to dev info.\n");
3261 		goto err1;
3262 	}
3263 
3264 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
3265 		  "dev_info: num_hwfns=%d affin_hwfn_idx=%d.\n",
3266 		  qedf->dev_info.common.num_hwfns,
3267 		  qed_ops->common->get_affin_hwfn_idx(qedf->cdev));
3268 
3269 	/* queue allocation code should come here
3270 	 * order should be
3271 	 * 	slowpath_start
3272 	 * 	status block allocation
3273 	 *	interrupt registration (to get min number of queues)
3274 	 *	set_fcoe_pf_param
3275 	 *	qed_sp_fcoe_func_start
3276 	 */
3277 	rc = qedf_set_fcoe_pf_param(qedf);
3278 	if (rc) {
3279 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot set fcoe pf param.\n");
3280 		goto err2;
3281 	}
3282 	qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
3283 
3284 	/* Record BDQ producer doorbell addresses */
3285 	qedf->bdq_primary_prod = qedf->dev_info.primary_dbq_rq_addr;
3286 	qedf->bdq_secondary_prod = qedf->dev_info.secondary_bdq_rq_addr;
3287 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3288 	    "BDQ primary_prod=%p secondary_prod=%p.\n", qedf->bdq_primary_prod,
3289 	    qedf->bdq_secondary_prod);
3290 
3291 	qed_ops->register_ops(qedf->cdev, &qedf_cb_ops, qedf);
3292 
3293 	rc = qedf_prepare_sb(qedf);
3294 	if (rc) {
3295 
3296 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
3297 		goto err2;
3298 	}
3299 
3300 	/* Start the Slowpath-process */
3301 	slowpath_params.int_mode = QED_INT_MODE_MSIX;
3302 	slowpath_params.drv_major = QEDF_DRIVER_MAJOR_VER;
3303 	slowpath_params.drv_minor = QEDF_DRIVER_MINOR_VER;
3304 	slowpath_params.drv_rev = QEDF_DRIVER_REV_VER;
3305 	slowpath_params.drv_eng = QEDF_DRIVER_ENG_VER;
3306 	strncpy(slowpath_params.name, "qedf", QED_DRV_VER_STR_SIZE);
3307 	rc = qed_ops->common->slowpath_start(qedf->cdev, &slowpath_params);
3308 	if (rc) {
3309 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
3310 		goto err2;
3311 	}
3312 
3313 	/*
3314 	 * update_pf_params needs to be called before and after slowpath
3315 	 * start
3316 	 */
3317 	qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
3318 
3319 	/* Setup interrupts */
3320 	rc = qedf_setup_int(qedf);
3321 	if (rc) {
3322 		QEDF_ERR(&qedf->dbg_ctx, "Setup interrupts failed.\n");
3323 		goto err3;
3324 	}
3325 
3326 	rc = qed_ops->start(qedf->cdev, &qedf->tasks);
3327 	if (rc) {
3328 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot start FCoE function.\n");
3329 		goto err4;
3330 	}
3331 	task_start = qedf_get_task_mem(&qedf->tasks, 0);
3332 	task_end = qedf_get_task_mem(&qedf->tasks, MAX_TID_BLOCKS_FCOE - 1);
3333 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Task context start=%p, "
3334 		   "end=%p block_size=%u.\n", task_start, task_end,
3335 		   qedf->tasks.size);
3336 
3337 	/*
3338 	 * We need to write the number of BDs in the BDQ we've preallocated so
3339 	 * the f/w will do a prefetch and we'll get an unsolicited CQE when a
3340 	 * packet arrives.
3341 	 */
3342 	qedf->bdq_prod_idx = QEDF_BDQ_SIZE;
3343 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3344 	    "Writing %d to primary and secondary BDQ doorbell registers.\n",
3345 	    qedf->bdq_prod_idx);
3346 	writew(qedf->bdq_prod_idx, qedf->bdq_primary_prod);
3347 	tmp = readw(qedf->bdq_primary_prod);
3348 	writew(qedf->bdq_prod_idx, qedf->bdq_secondary_prod);
3349 	tmp = readw(qedf->bdq_secondary_prod);
3350 
3351 	qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
3352 
3353 	/* Now that the dev_info struct has been filled in set the MAC
3354 	 * address
3355 	 */
3356 	ether_addr_copy(qedf->mac, qedf->dev_info.common.hw_mac);
3357 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "MAC address is %pM.\n",
3358 		   qedf->mac);
3359 
3360 	/*
3361 	 * Set the WWNN and WWPN in the following way:
3362 	 *
3363 	 * If the info we get from qed is non-zero then use that to set the
3364 	 * WWPN and WWNN. Otherwise fall back to use fcoe_wwn_from_mac() based
3365 	 * on the MAC address.
3366 	 */
3367 	if (qedf->dev_info.wwnn != 0 && qedf->dev_info.wwpn != 0) {
3368 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3369 		    "Setting WWPN and WWNN from qed dev_info.\n");
3370 		qedf->wwnn = qedf->dev_info.wwnn;
3371 		qedf->wwpn = qedf->dev_info.wwpn;
3372 	} else {
3373 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3374 		    "Setting WWPN and WWNN using fcoe_wwn_from_mac().\n");
3375 		qedf->wwnn = fcoe_wwn_from_mac(qedf->mac, 1, 0);
3376 		qedf->wwpn = fcoe_wwn_from_mac(qedf->mac, 2, 0);
3377 	}
3378 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,  "WWNN=%016llx "
3379 		   "WWPN=%016llx.\n", qedf->wwnn, qedf->wwpn);
3380 
3381 	sprintf(host_buf, "host_%d", host->host_no);
3382 	qed_ops->common->set_name(qedf->cdev, host_buf);
3383 
3384 	/* Allocate cmd mgr */
3385 	qedf->cmd_mgr = qedf_cmd_mgr_alloc(qedf);
3386 	if (!qedf->cmd_mgr) {
3387 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to allocate cmd mgr.\n");
3388 		rc = -ENOMEM;
3389 		goto err5;
3390 	}
3391 
3392 	if (mode != QEDF_MODE_RECOVERY) {
3393 		host->transportt = qedf_fc_transport_template;
3394 		host->max_lun = qedf_max_lun;
3395 		host->max_cmd_len = QEDF_MAX_CDB_LEN;
3396 		host->can_queue = FCOE_PARAMS_NUM_TASKS;
3397 		rc = scsi_add_host(host, &pdev->dev);
3398 		if (rc) {
3399 			QEDF_WARN(&qedf->dbg_ctx,
3400 				  "Error adding Scsi_Host rc=0x%x.\n", rc);
3401 			goto err6;
3402 		}
3403 	}
3404 
3405 	memset(&params, 0, sizeof(params));
3406 	params.mtu = QEDF_LL2_BUF_SIZE;
3407 	ether_addr_copy(params.ll2_mac_address, qedf->mac);
3408 
3409 	/* Start LL2 processing thread */
3410 	snprintf(host_buf, 20, "qedf_%d_ll2", host->host_no);
3411 	qedf->ll2_recv_wq =
3412 		create_workqueue(host_buf);
3413 	if (!qedf->ll2_recv_wq) {
3414 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to LL2 workqueue.\n");
3415 		rc = -ENOMEM;
3416 		goto err7;
3417 	}
3418 
3419 #ifdef CONFIG_DEBUG_FS
3420 	qedf_dbg_host_init(&(qedf->dbg_ctx), qedf_debugfs_ops,
3421 			    qedf_dbg_fops);
3422 #endif
3423 
3424 	/* Start LL2 */
3425 	qed_ops->ll2->register_cb_ops(qedf->cdev, &qedf_ll2_cb_ops, qedf);
3426 	rc = qed_ops->ll2->start(qedf->cdev, &params);
3427 	if (rc) {
3428 		QEDF_ERR(&(qedf->dbg_ctx), "Could not start Light L2.\n");
3429 		goto err7;
3430 	}
3431 	set_bit(QEDF_LL2_STARTED, &qedf->flags);
3432 
3433 	/* Set initial FIP/FCoE VLAN to NULL */
3434 	qedf->vlan_id = 0;
3435 
3436 	/*
3437 	 * No need to setup fcoe_ctlr or fc_lport objects during recovery since
3438 	 * they were not reaped during the unload process.
3439 	 */
3440 	if (mode != QEDF_MODE_RECOVERY) {
3441 		/* Setup imbedded fcoe controller */
3442 		qedf_fcoe_ctlr_setup(qedf);
3443 
3444 		/* Setup lport */
3445 		rc = qedf_lport_setup(qedf);
3446 		if (rc) {
3447 			QEDF_ERR(&(qedf->dbg_ctx),
3448 			    "qedf_lport_setup failed.\n");
3449 			goto err7;
3450 		}
3451 	}
3452 
3453 	sprintf(host_buf, "qedf_%u_timer", qedf->lport->host->host_no);
3454 	qedf->timer_work_queue =
3455 		create_workqueue(host_buf);
3456 	if (!qedf->timer_work_queue) {
3457 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to start timer "
3458 			  "workqueue.\n");
3459 		rc = -ENOMEM;
3460 		goto err7;
3461 	}
3462 
3463 	/* DPC workqueue is not reaped during recovery unload */
3464 	if (mode != QEDF_MODE_RECOVERY) {
3465 		sprintf(host_buf, "qedf_%u_dpc",
3466 		    qedf->lport->host->host_no);
3467 		qedf->dpc_wq = create_workqueue(host_buf);
3468 	}
3469 
3470 	/*
3471 	 * GRC dump and sysfs parameters are not reaped during the recovery
3472 	 * unload process.
3473 	 */
3474 	if (mode != QEDF_MODE_RECOVERY) {
3475 		qedf->grcdump_size =
3476 		    qed_ops->common->dbg_all_data_size(qedf->cdev);
3477 		if (qedf->grcdump_size) {
3478 			rc = qedf_alloc_grc_dump_buf(&qedf->grcdump,
3479 			    qedf->grcdump_size);
3480 			if (rc) {
3481 				QEDF_ERR(&(qedf->dbg_ctx),
3482 				    "GRC Dump buffer alloc failed.\n");
3483 				qedf->grcdump = NULL;
3484 			}
3485 
3486 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3487 			    "grcdump: addr=%p, size=%u.\n",
3488 			    qedf->grcdump, qedf->grcdump_size);
3489 		}
3490 		qedf_create_sysfs_ctx_attr(qedf);
3491 
3492 		/* Initialize I/O tracing for this adapter */
3493 		spin_lock_init(&qedf->io_trace_lock);
3494 		qedf->io_trace_idx = 0;
3495 	}
3496 
3497 	init_completion(&qedf->flogi_compl);
3498 
3499 	status = qed_ops->common->update_drv_state(qedf->cdev, true);
3500 	if (status)
3501 		QEDF_ERR(&(qedf->dbg_ctx),
3502 			"Failed to send drv state to MFW.\n");
3503 
3504 	memset(&link_params, 0, sizeof(struct qed_link_params));
3505 	link_params.link_up = true;
3506 	status = qed_ops->common->set_link(qedf->cdev, &link_params);
3507 	if (status)
3508 		QEDF_WARN(&(qedf->dbg_ctx), "set_link failed.\n");
3509 
3510 	/* Start/restart discovery */
3511 	if (mode == QEDF_MODE_RECOVERY)
3512 		fcoe_ctlr_link_up(&qedf->ctlr);
3513 	else
3514 		fc_fabric_login(lport);
3515 
3516 	/* All good */
3517 	return 0;
3518 
3519 err7:
3520 	if (qedf->ll2_recv_wq)
3521 		destroy_workqueue(qedf->ll2_recv_wq);
3522 	fc_remove_host(qedf->lport->host);
3523 	scsi_remove_host(qedf->lport->host);
3524 #ifdef CONFIG_DEBUG_FS
3525 	qedf_dbg_host_exit(&(qedf->dbg_ctx));
3526 #endif
3527 err6:
3528 	qedf_cmd_mgr_free(qedf->cmd_mgr);
3529 err5:
3530 	qed_ops->stop(qedf->cdev);
3531 err4:
3532 	qedf_free_fcoe_pf_param(qedf);
3533 	qedf_sync_free_irqs(qedf);
3534 err3:
3535 	qed_ops->common->slowpath_stop(qedf->cdev);
3536 err2:
3537 	qed_ops->common->remove(qedf->cdev);
3538 err1:
3539 	scsi_host_put(lport->host);
3540 err0:
3541 	return rc;
3542 }
3543 
3544 static int qedf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3545 {
3546 	return __qedf_probe(pdev, QEDF_MODE_NORMAL);
3547 }
3548 
3549 static void __qedf_remove(struct pci_dev *pdev, int mode)
3550 {
3551 	struct qedf_ctx *qedf;
3552 	int rc;
3553 
3554 	if (!pdev) {
3555 		QEDF_ERR(NULL, "pdev is NULL.\n");
3556 		return;
3557 	}
3558 
3559 	qedf = pci_get_drvdata(pdev);
3560 
3561 	/*
3562 	 * Prevent race where we're in board disable work and then try to
3563 	 * rmmod the module.
3564 	 */
3565 	if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
3566 		QEDF_ERR(&qedf->dbg_ctx, "Already removing PCI function.\n");
3567 		return;
3568 	}
3569 
3570 	if (mode != QEDF_MODE_RECOVERY)
3571 		set_bit(QEDF_UNLOADING, &qedf->flags);
3572 
3573 	/* Logoff the fabric to upload all connections */
3574 	if (mode == QEDF_MODE_RECOVERY)
3575 		fcoe_ctlr_link_down(&qedf->ctlr);
3576 	else
3577 		fc_fabric_logoff(qedf->lport);
3578 
3579 	if (qedf_wait_for_upload(qedf) == false)
3580 		QEDF_ERR(&qedf->dbg_ctx, "Could not upload all sessions.\n");
3581 
3582 #ifdef CONFIG_DEBUG_FS
3583 	qedf_dbg_host_exit(&(qedf->dbg_ctx));
3584 #endif
3585 
3586 	/* Stop any link update handling */
3587 	cancel_delayed_work_sync(&qedf->link_update);
3588 	destroy_workqueue(qedf->link_update_wq);
3589 	qedf->link_update_wq = NULL;
3590 
3591 	if (qedf->timer_work_queue)
3592 		destroy_workqueue(qedf->timer_work_queue);
3593 
3594 	/* Stop Light L2 */
3595 	clear_bit(QEDF_LL2_STARTED, &qedf->flags);
3596 	qed_ops->ll2->stop(qedf->cdev);
3597 	if (qedf->ll2_recv_wq)
3598 		destroy_workqueue(qedf->ll2_recv_wq);
3599 
3600 	/* Stop fastpath */
3601 	qedf_sync_free_irqs(qedf);
3602 	qedf_destroy_sb(qedf);
3603 
3604 	/*
3605 	 * During recovery don't destroy OS constructs that represent the
3606 	 * physical port.
3607 	 */
3608 	if (mode != QEDF_MODE_RECOVERY) {
3609 		qedf_free_grc_dump_buf(&qedf->grcdump);
3610 		qedf_remove_sysfs_ctx_attr(qedf);
3611 
3612 		/* Remove all SCSI/libfc/libfcoe structures */
3613 		fcoe_ctlr_destroy(&qedf->ctlr);
3614 		fc_lport_destroy(qedf->lport);
3615 		fc_remove_host(qedf->lport->host);
3616 		scsi_remove_host(qedf->lport->host);
3617 	}
3618 
3619 	qedf_cmd_mgr_free(qedf->cmd_mgr);
3620 
3621 	if (mode != QEDF_MODE_RECOVERY) {
3622 		fc_exch_mgr_free(qedf->lport);
3623 		fc_lport_free_stats(qedf->lport);
3624 
3625 		/* Wait for all vports to be reaped */
3626 		qedf_wait_for_vport_destroy(qedf);
3627 	}
3628 
3629 	/*
3630 	 * Now that all connections have been uploaded we can stop the
3631 	 * rest of the qed operations
3632 	 */
3633 	qed_ops->stop(qedf->cdev);
3634 
3635 	if (mode != QEDF_MODE_RECOVERY) {
3636 		if (qedf->dpc_wq) {
3637 			/* Stop general DPC handling */
3638 			destroy_workqueue(qedf->dpc_wq);
3639 			qedf->dpc_wq = NULL;
3640 		}
3641 	}
3642 
3643 	/* Final shutdown for the board */
3644 	qedf_free_fcoe_pf_param(qedf);
3645 	if (mode != QEDF_MODE_RECOVERY) {
3646 		qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
3647 		pci_set_drvdata(pdev, NULL);
3648 	}
3649 
3650 	rc = qed_ops->common->update_drv_state(qedf->cdev, false);
3651 	if (rc)
3652 		QEDF_ERR(&(qedf->dbg_ctx),
3653 			"Failed to send drv state to MFW.\n");
3654 
3655 	qed_ops->common->slowpath_stop(qedf->cdev);
3656 	qed_ops->common->remove(qedf->cdev);
3657 
3658 	mempool_destroy(qedf->io_mempool);
3659 
3660 	/* Only reap the Scsi_host on a real removal */
3661 	if (mode != QEDF_MODE_RECOVERY)
3662 		scsi_host_put(qedf->lport->host);
3663 }
3664 
3665 static void qedf_remove(struct pci_dev *pdev)
3666 {
3667 	/* Check to make sure this function wasn't already disabled */
3668 	if (!atomic_read(&pdev->enable_cnt))
3669 		return;
3670 
3671 	__qedf_remove(pdev, QEDF_MODE_NORMAL);
3672 }
3673 
3674 void qedf_wq_grcdump(struct work_struct *work)
3675 {
3676 	struct qedf_ctx *qedf =
3677 	    container_of(work, struct qedf_ctx, grcdump_work.work);
3678 
3679 	QEDF_ERR(&(qedf->dbg_ctx), "Collecting GRC dump.\n");
3680 	qedf_capture_grc_dump(qedf);
3681 }
3682 
3683 /*
3684  * Protocol TLV handler
3685  */
3686 void qedf_get_protocol_tlv_data(void *dev, void *data)
3687 {
3688 	struct qedf_ctx *qedf = dev;
3689 	struct qed_mfw_tlv_fcoe *fcoe = data;
3690 	struct fc_lport *lport = qedf->lport;
3691 	struct Scsi_Host *host = lport->host;
3692 	struct fc_host_attrs *fc_host = shost_to_fc_host(host);
3693 	struct fc_host_statistics *hst;
3694 
3695 	/* Force a refresh of the fc_host stats including offload stats */
3696 	hst = qedf_fc_get_host_stats(host);
3697 
3698 	fcoe->qos_pri_set = true;
3699 	fcoe->qos_pri = 3; /* Hard coded to 3 in driver */
3700 
3701 	fcoe->ra_tov_set = true;
3702 	fcoe->ra_tov = lport->r_a_tov;
3703 
3704 	fcoe->ed_tov_set = true;
3705 	fcoe->ed_tov = lport->e_d_tov;
3706 
3707 	fcoe->npiv_state_set = true;
3708 	fcoe->npiv_state = 1; /* NPIV always enabled */
3709 
3710 	fcoe->num_npiv_ids_set = true;
3711 	fcoe->num_npiv_ids = fc_host->npiv_vports_inuse;
3712 
3713 	/* Certain attributes we only want to set if we've selected an FCF */
3714 	if (qedf->ctlr.sel_fcf) {
3715 		fcoe->switch_name_set = true;
3716 		u64_to_wwn(qedf->ctlr.sel_fcf->switch_name, fcoe->switch_name);
3717 	}
3718 
3719 	fcoe->port_state_set = true;
3720 	/* For qedf we're either link down or fabric attach */
3721 	if (lport->link_up)
3722 		fcoe->port_state = QED_MFW_TLV_PORT_STATE_FABRIC;
3723 	else
3724 		fcoe->port_state = QED_MFW_TLV_PORT_STATE_OFFLINE;
3725 
3726 	fcoe->link_failures_set = true;
3727 	fcoe->link_failures = (u16)hst->link_failure_count;
3728 
3729 	fcoe->fcoe_txq_depth_set = true;
3730 	fcoe->fcoe_rxq_depth_set = true;
3731 	fcoe->fcoe_rxq_depth = FCOE_PARAMS_NUM_TASKS;
3732 	fcoe->fcoe_txq_depth = FCOE_PARAMS_NUM_TASKS;
3733 
3734 	fcoe->fcoe_rx_frames_set = true;
3735 	fcoe->fcoe_rx_frames = hst->rx_frames;
3736 
3737 	fcoe->fcoe_tx_frames_set = true;
3738 	fcoe->fcoe_tx_frames = hst->tx_frames;
3739 
3740 	fcoe->fcoe_rx_bytes_set = true;
3741 	fcoe->fcoe_rx_bytes = hst->fcp_input_megabytes * 1000000;
3742 
3743 	fcoe->fcoe_tx_bytes_set = true;
3744 	fcoe->fcoe_tx_bytes = hst->fcp_output_megabytes * 1000000;
3745 
3746 	fcoe->crc_count_set = true;
3747 	fcoe->crc_count = hst->invalid_crc_count;
3748 
3749 	fcoe->tx_abts_set = true;
3750 	fcoe->tx_abts = hst->fcp_packet_aborts;
3751 
3752 	fcoe->tx_lun_rst_set = true;
3753 	fcoe->tx_lun_rst = qedf->lun_resets;
3754 
3755 	fcoe->abort_task_sets_set = true;
3756 	fcoe->abort_task_sets = qedf->packet_aborts;
3757 
3758 	fcoe->scsi_busy_set = true;
3759 	fcoe->scsi_busy = qedf->busy;
3760 
3761 	fcoe->scsi_tsk_full_set = true;
3762 	fcoe->scsi_tsk_full = qedf->task_set_fulls;
3763 }
3764 
3765 static void qedf_shutdown(struct pci_dev *pdev)
3766 {
3767 	__qedf_remove(pdev, QEDF_MODE_NORMAL);
3768 }
3769 
3770 /* Generic TLV data callback */
3771 void qedf_get_generic_tlv_data(void *dev, struct qed_generic_tlvs *data)
3772 {
3773 	struct qedf_ctx *qedf;
3774 
3775 	if (!dev) {
3776 		QEDF_INFO(NULL, QEDF_LOG_EVT,
3777 			  "dev is NULL so ignoring get_generic_tlv_data request.\n");
3778 		return;
3779 	}
3780 	qedf = (struct qedf_ctx *)dev;
3781 
3782 	memset(data, 0, sizeof(struct qed_generic_tlvs));
3783 	ether_addr_copy(data->mac[0], qedf->mac);
3784 }
3785 
3786 /*
3787  * Module Init/Remove
3788  */
3789 
3790 static int __init qedf_init(void)
3791 {
3792 	int ret;
3793 
3794 	/* If debug=1 passed, set the default log mask */
3795 	if (qedf_debug == QEDF_LOG_DEFAULT)
3796 		qedf_debug = QEDF_DEFAULT_LOG_MASK;
3797 
3798 	/*
3799 	 * Check that default prio for FIP/FCoE traffic is between 0..7 if a
3800 	 * value has been set
3801 	 */
3802 	if (qedf_default_prio > -1)
3803 		if (qedf_default_prio > 7) {
3804 			qedf_default_prio = QEDF_DEFAULT_PRIO;
3805 			QEDF_ERR(NULL, "FCoE/FIP priority out of range, resetting to %d.\n",
3806 			    QEDF_DEFAULT_PRIO);
3807 		}
3808 
3809 	/* Print driver banner */
3810 	QEDF_INFO(NULL, QEDF_LOG_INFO, "%s v%s.\n", QEDF_DESCR,
3811 		   QEDF_VERSION);
3812 
3813 	/* Create kmem_cache for qedf_io_work structs */
3814 	qedf_io_work_cache = kmem_cache_create("qedf_io_work_cache",
3815 	    sizeof(struct qedf_io_work), 0, SLAB_HWCACHE_ALIGN, NULL);
3816 	if (qedf_io_work_cache == NULL) {
3817 		QEDF_ERR(NULL, "qedf_io_work_cache is NULL.\n");
3818 		goto err1;
3819 	}
3820 	QEDF_INFO(NULL, QEDF_LOG_DISC, "qedf_io_work_cache=%p.\n",
3821 	    qedf_io_work_cache);
3822 
3823 	qed_ops = qed_get_fcoe_ops();
3824 	if (!qed_ops) {
3825 		QEDF_ERR(NULL, "Failed to get qed fcoe operations\n");
3826 		goto err1;
3827 	}
3828 
3829 #ifdef CONFIG_DEBUG_FS
3830 	qedf_dbg_init("qedf");
3831 #endif
3832 
3833 	qedf_fc_transport_template =
3834 	    fc_attach_transport(&qedf_fc_transport_fn);
3835 	if (!qedf_fc_transport_template) {
3836 		QEDF_ERR(NULL, "Could not register with FC transport\n");
3837 		goto err2;
3838 	}
3839 
3840 	qedf_fc_vport_transport_template =
3841 		fc_attach_transport(&qedf_fc_vport_transport_fn);
3842 	if (!qedf_fc_vport_transport_template) {
3843 		QEDF_ERR(NULL, "Could not register vport template with FC "
3844 			  "transport\n");
3845 		goto err3;
3846 	}
3847 
3848 	qedf_io_wq = create_workqueue("qedf_io_wq");
3849 	if (!qedf_io_wq) {
3850 		QEDF_ERR(NULL, "Could not create qedf_io_wq.\n");
3851 		goto err4;
3852 	}
3853 
3854 	qedf_cb_ops.get_login_failures = qedf_get_login_failures;
3855 
3856 	ret = pci_register_driver(&qedf_pci_driver);
3857 	if (ret) {
3858 		QEDF_ERR(NULL, "Failed to register driver\n");
3859 		goto err5;
3860 	}
3861 
3862 	return 0;
3863 
3864 err5:
3865 	destroy_workqueue(qedf_io_wq);
3866 err4:
3867 	fc_release_transport(qedf_fc_vport_transport_template);
3868 err3:
3869 	fc_release_transport(qedf_fc_transport_template);
3870 err2:
3871 #ifdef CONFIG_DEBUG_FS
3872 	qedf_dbg_exit();
3873 #endif
3874 	qed_put_fcoe_ops();
3875 err1:
3876 	return -EINVAL;
3877 }
3878 
3879 static void __exit qedf_cleanup(void)
3880 {
3881 	pci_unregister_driver(&qedf_pci_driver);
3882 
3883 	destroy_workqueue(qedf_io_wq);
3884 
3885 	fc_release_transport(qedf_fc_vport_transport_template);
3886 	fc_release_transport(qedf_fc_transport_template);
3887 #ifdef CONFIG_DEBUG_FS
3888 	qedf_dbg_exit();
3889 #endif
3890 	qed_put_fcoe_ops();
3891 
3892 	kmem_cache_destroy(qedf_io_work_cache);
3893 }
3894 
3895 MODULE_LICENSE("GPL");
3896 MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx FCoE Module");
3897 MODULE_AUTHOR("QLogic Corporation");
3898 MODULE_VERSION(QEDF_VERSION);
3899 module_init(qedf_init);
3900 module_exit(qedf_cleanup);
3901