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