xref: /openbmc/linux/drivers/scsi/qedf/qedf_main.c (revision f97769fd)
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 static int qedf_xmit(struct fc_lport *lport, struct fc_frame *fp)
1048 {
1049 	struct fc_lport		*base_lport;
1050 	struct qedf_ctx		*qedf;
1051 	struct ethhdr		*eh;
1052 	struct fcoe_crc_eof	*cp;
1053 	struct sk_buff		*skb;
1054 	struct fc_frame_header	*fh;
1055 	struct fcoe_hdr		*hp;
1056 	u8			sof, eof;
1057 	u32			crc;
1058 	unsigned int		hlen, tlen, elen;
1059 	int			wlen;
1060 	struct fc_stats		*stats;
1061 	struct fc_lport *tmp_lport;
1062 	struct fc_lport *vn_port = NULL;
1063 	struct qedf_rport *fcport;
1064 	int rc;
1065 	u16 vlan_tci = 0;
1066 
1067 	qedf = (struct qedf_ctx *)lport_priv(lport);
1068 
1069 	fh = fc_frame_header_get(fp);
1070 	skb = fp_skb(fp);
1071 
1072 	/* Filter out traffic to other NPIV ports on the same host */
1073 	if (lport->vport)
1074 		base_lport = shost_priv(vport_to_shost(lport->vport));
1075 	else
1076 		base_lport = lport;
1077 
1078 	/* Flag if the destination is the base port */
1079 	if (base_lport->port_id == ntoh24(fh->fh_d_id)) {
1080 		vn_port = base_lport;
1081 	} else {
1082 		/* Got through the list of vports attached to the base_lport
1083 		 * and see if we have a match with the destination address.
1084 		 */
1085 		list_for_each_entry(tmp_lport, &base_lport->vports, list) {
1086 			if (tmp_lport->port_id == ntoh24(fh->fh_d_id)) {
1087 				vn_port = tmp_lport;
1088 				break;
1089 			}
1090 		}
1091 	}
1092 	if (vn_port && ntoh24(fh->fh_d_id) != FC_FID_FLOGI) {
1093 		struct fc_rport_priv *rdata = NULL;
1094 
1095 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
1096 		    "Dropping FCoE frame to %06x.\n", ntoh24(fh->fh_d_id));
1097 		kfree_skb(skb);
1098 		rdata = fc_rport_lookup(lport, ntoh24(fh->fh_d_id));
1099 		if (rdata) {
1100 			rdata->retries = lport->max_rport_retry_count;
1101 			kref_put(&rdata->kref, fc_rport_destroy);
1102 		}
1103 		return -EINVAL;
1104 	}
1105 	/* End NPIV filtering */
1106 
1107 	if (!qedf->ctlr.sel_fcf) {
1108 		kfree_skb(skb);
1109 		return 0;
1110 	}
1111 
1112 	if (!test_bit(QEDF_LL2_STARTED, &qedf->flags)) {
1113 		QEDF_WARN(&(qedf->dbg_ctx), "LL2 not started\n");
1114 		kfree_skb(skb);
1115 		return 0;
1116 	}
1117 
1118 	if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
1119 		QEDF_WARN(&(qedf->dbg_ctx), "qedf link down\n");
1120 		kfree_skb(skb);
1121 		return 0;
1122 	}
1123 
1124 	if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
1125 		if (fcoe_ctlr_els_send(&qedf->ctlr, lport, skb))
1126 			return 0;
1127 	}
1128 
1129 	/* Check to see if this needs to be sent on an offloaded session */
1130 	fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
1131 
1132 	if (fcport && test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1133 		rc = qedf_xmit_l2_frame(fcport, fp);
1134 		/*
1135 		 * If the frame was successfully sent over the middle path
1136 		 * then do not try to also send it over the LL2 path
1137 		 */
1138 		if (rc)
1139 			return 0;
1140 	}
1141 
1142 	sof = fr_sof(fp);
1143 	eof = fr_eof(fp);
1144 
1145 	elen = sizeof(struct ethhdr);
1146 	hlen = sizeof(struct fcoe_hdr);
1147 	tlen = sizeof(struct fcoe_crc_eof);
1148 	wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1149 
1150 	skb->ip_summed = CHECKSUM_NONE;
1151 	crc = fcoe_fc_crc(fp);
1152 
1153 	/* copy port crc and eof to the skb buff */
1154 	if (skb_is_nonlinear(skb)) {
1155 		skb_frag_t *frag;
1156 
1157 		if (qedf_get_paged_crc_eof(skb, tlen)) {
1158 			kfree_skb(skb);
1159 			return -ENOMEM;
1160 		}
1161 		frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1162 		cp = kmap_atomic(skb_frag_page(frag)) + skb_frag_off(frag);
1163 	} else {
1164 		cp = skb_put(skb, tlen);
1165 	}
1166 
1167 	memset(cp, 0, sizeof(*cp));
1168 	cp->fcoe_eof = eof;
1169 	cp->fcoe_crc32 = cpu_to_le32(~crc);
1170 	if (skb_is_nonlinear(skb)) {
1171 		kunmap_atomic(cp);
1172 		cp = NULL;
1173 	}
1174 
1175 
1176 	/* adjust skb network/transport offsets to match mac/fcoe/port */
1177 	skb_push(skb, elen + hlen);
1178 	skb_reset_mac_header(skb);
1179 	skb_reset_network_header(skb);
1180 	skb->mac_len = elen;
1181 	skb->protocol = htons(ETH_P_FCOE);
1182 
1183 	/*
1184 	 * Add VLAN tag to non-offload FCoE frame based on current stored VLAN
1185 	 * for FIP/FCoE traffic.
1186 	 */
1187 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), qedf->vlan_id);
1188 
1189 	/* fill up mac and fcoe headers */
1190 	eh = eth_hdr(skb);
1191 	eh->h_proto = htons(ETH_P_FCOE);
1192 	if (qedf->ctlr.map_dest)
1193 		fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1194 	else
1195 		/* insert GW address */
1196 		ether_addr_copy(eh->h_dest, qedf->ctlr.dest_addr);
1197 
1198 	/* Set the source MAC address */
1199 	ether_addr_copy(eh->h_source, qedf->data_src_addr);
1200 
1201 	hp = (struct fcoe_hdr *)(eh + 1);
1202 	memset(hp, 0, sizeof(*hp));
1203 	if (FC_FCOE_VER)
1204 		FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1205 	hp->fcoe_sof = sof;
1206 
1207 	/*update tx stats */
1208 	stats = per_cpu_ptr(lport->stats, get_cpu());
1209 	stats->TxFrames++;
1210 	stats->TxWords += wlen;
1211 	put_cpu();
1212 
1213 	/* Get VLAN ID from skb for printing purposes */
1214 	__vlan_hwaccel_get_tag(skb, &vlan_tci);
1215 
1216 	/* send down to lld */
1217 	fr_dev(fp) = lport;
1218 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame send: "
1219 	    "src=%06x dest=%06x r_ctl=%x type=%x vlan=%04x.\n",
1220 	    ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl, fh->fh_type,
1221 	    vlan_tci);
1222 	if (qedf_dump_frames)
1223 		print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
1224 		    1, skb->data, skb->len, false);
1225 	rc = qed_ops->ll2->start_xmit(qedf->cdev, skb, 0);
1226 	if (rc) {
1227 		QEDF_ERR(&qedf->dbg_ctx, "start_xmit failed rc = %d.\n", rc);
1228 		kfree_skb(skb);
1229 		return rc;
1230 	}
1231 
1232 	return 0;
1233 }
1234 
1235 static int qedf_alloc_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
1236 {
1237 	int rval = 0;
1238 	u32 *pbl;
1239 	dma_addr_t page;
1240 	int num_pages;
1241 
1242 	/* Calculate appropriate queue and PBL sizes */
1243 	fcport->sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
1244 	fcport->sq_mem_size = ALIGN(fcport->sq_mem_size, QEDF_PAGE_SIZE);
1245 	fcport->sq_pbl_size = (fcport->sq_mem_size / QEDF_PAGE_SIZE) *
1246 	    sizeof(void *);
1247 	fcport->sq_pbl_size = fcport->sq_pbl_size + QEDF_PAGE_SIZE;
1248 
1249 	fcport->sq = dma_alloc_coherent(&qedf->pdev->dev, fcport->sq_mem_size,
1250 					&fcport->sq_dma, GFP_KERNEL);
1251 	if (!fcport->sq) {
1252 		QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue.\n");
1253 		rval = 1;
1254 		goto out;
1255 	}
1256 
1257 	fcport->sq_pbl = dma_alloc_coherent(&qedf->pdev->dev,
1258 					    fcport->sq_pbl_size,
1259 					    &fcport->sq_pbl_dma, GFP_KERNEL);
1260 	if (!fcport->sq_pbl) {
1261 		QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue PBL.\n");
1262 		rval = 1;
1263 		goto out_free_sq;
1264 	}
1265 
1266 	/* Create PBL */
1267 	num_pages = fcport->sq_mem_size / QEDF_PAGE_SIZE;
1268 	page = fcport->sq_dma;
1269 	pbl = (u32 *)fcport->sq_pbl;
1270 
1271 	while (num_pages--) {
1272 		*pbl = U64_LO(page);
1273 		pbl++;
1274 		*pbl = U64_HI(page);
1275 		pbl++;
1276 		page += QEDF_PAGE_SIZE;
1277 	}
1278 
1279 	return rval;
1280 
1281 out_free_sq:
1282 	dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size, fcport->sq,
1283 	    fcport->sq_dma);
1284 out:
1285 	return rval;
1286 }
1287 
1288 static void qedf_free_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
1289 {
1290 	if (fcport->sq_pbl)
1291 		dma_free_coherent(&qedf->pdev->dev, fcport->sq_pbl_size,
1292 		    fcport->sq_pbl, fcport->sq_pbl_dma);
1293 	if (fcport->sq)
1294 		dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size,
1295 		    fcport->sq, fcport->sq_dma);
1296 }
1297 
1298 static int qedf_offload_connection(struct qedf_ctx *qedf,
1299 	struct qedf_rport *fcport)
1300 {
1301 	struct qed_fcoe_params_offload conn_info;
1302 	u32 port_id;
1303 	int rval;
1304 	uint16_t total_sqe = (fcport->sq_mem_size / sizeof(struct fcoe_wqe));
1305 
1306 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offloading connection "
1307 		   "portid=%06x.\n", fcport->rdata->ids.port_id);
1308 	rval = qed_ops->acquire_conn(qedf->cdev, &fcport->handle,
1309 	    &fcport->fw_cid, &fcport->p_doorbell);
1310 	if (rval) {
1311 		QEDF_WARN(&(qedf->dbg_ctx), "Could not acquire connection "
1312 			   "for portid=%06x.\n", fcport->rdata->ids.port_id);
1313 		rval = 1; /* For some reason qed returns 0 on failure here */
1314 		goto out;
1315 	}
1316 
1317 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "portid=%06x "
1318 		   "fw_cid=%08x handle=%d.\n", fcport->rdata->ids.port_id,
1319 		   fcport->fw_cid, fcport->handle);
1320 
1321 	memset(&conn_info, 0, sizeof(struct qed_fcoe_params_offload));
1322 
1323 	/* Fill in the offload connection info */
1324 	conn_info.sq_pbl_addr = fcport->sq_pbl_dma;
1325 
1326 	conn_info.sq_curr_page_addr = (dma_addr_t)(*(u64 *)fcport->sq_pbl);
1327 	conn_info.sq_next_page_addr =
1328 	    (dma_addr_t)(*(u64 *)(fcport->sq_pbl + 8));
1329 
1330 	/* Need to use our FCoE MAC for the offload session */
1331 	ether_addr_copy(conn_info.src_mac, qedf->data_src_addr);
1332 
1333 	ether_addr_copy(conn_info.dst_mac, qedf->ctlr.dest_addr);
1334 
1335 	conn_info.tx_max_fc_pay_len = fcport->rdata->maxframe_size;
1336 	conn_info.e_d_tov_timer_val = qedf->lport->e_d_tov / 20;
1337 	conn_info.rec_tov_timer_val = 3; /* I think this is what E3 was */
1338 	conn_info.rx_max_fc_pay_len = fcport->rdata->maxframe_size;
1339 
1340 	/* Set VLAN data */
1341 	conn_info.vlan_tag = qedf->vlan_id <<
1342 	    FCOE_CONN_OFFLOAD_RAMROD_DATA_VLAN_ID_SHIFT;
1343 	conn_info.vlan_tag |=
1344 	    qedf->prio << FCOE_CONN_OFFLOAD_RAMROD_DATA_PRIORITY_SHIFT;
1345 	conn_info.flags |= (FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_MASK <<
1346 	    FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_SHIFT);
1347 
1348 	/* Set host port source id */
1349 	port_id = fc_host_port_id(qedf->lport->host);
1350 	fcport->sid = port_id;
1351 	conn_info.s_id.addr_hi = (port_id & 0x000000FF);
1352 	conn_info.s_id.addr_mid = (port_id & 0x0000FF00) >> 8;
1353 	conn_info.s_id.addr_lo = (port_id & 0x00FF0000) >> 16;
1354 
1355 	conn_info.max_conc_seqs_c3 = fcport->rdata->max_seq;
1356 
1357 	/* Set remote port destination id */
1358 	port_id = fcport->rdata->rport->port_id;
1359 	conn_info.d_id.addr_hi = (port_id & 0x000000FF);
1360 	conn_info.d_id.addr_mid = (port_id & 0x0000FF00) >> 8;
1361 	conn_info.d_id.addr_lo = (port_id & 0x00FF0000) >> 16;
1362 
1363 	conn_info.def_q_idx = 0; /* Default index for send queue? */
1364 
1365 	/* Set FC-TAPE specific flags if needed */
1366 	if (fcport->dev_type == QEDF_RPORT_TYPE_TAPE) {
1367 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN,
1368 		    "Enable CONF, REC for portid=%06x.\n",
1369 		    fcport->rdata->ids.port_id);
1370 		conn_info.flags |= 1 <<
1371 		    FCOE_CONN_OFFLOAD_RAMROD_DATA_B_CONF_REQ_SHIFT;
1372 		conn_info.flags |=
1373 		    ((fcport->rdata->sp_features & FC_SP_FT_SEQC) ? 1 : 0) <<
1374 		    FCOE_CONN_OFFLOAD_RAMROD_DATA_B_REC_VALID_SHIFT;
1375 	}
1376 
1377 	rval = qed_ops->offload_conn(qedf->cdev, fcport->handle, &conn_info);
1378 	if (rval) {
1379 		QEDF_WARN(&(qedf->dbg_ctx), "Could not offload connection "
1380 			   "for portid=%06x.\n", fcport->rdata->ids.port_id);
1381 		goto out_free_conn;
1382 	} else
1383 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offload "
1384 			   "succeeded portid=%06x total_sqe=%d.\n",
1385 			   fcport->rdata->ids.port_id, total_sqe);
1386 
1387 	spin_lock_init(&fcport->rport_lock);
1388 	atomic_set(&fcport->free_sqes, total_sqe);
1389 	return 0;
1390 out_free_conn:
1391 	qed_ops->release_conn(qedf->cdev, fcport->handle);
1392 out:
1393 	return rval;
1394 }
1395 
1396 #define QEDF_TERM_BUFF_SIZE		10
1397 static void qedf_upload_connection(struct qedf_ctx *qedf,
1398 	struct qedf_rport *fcport)
1399 {
1400 	void *term_params;
1401 	dma_addr_t term_params_dma;
1402 
1403 	/* Term params needs to be a DMA coherent buffer as qed shared the
1404 	 * physical DMA address with the firmware. The buffer may be used in
1405 	 * the receive path so we may eventually have to move this.
1406 	 */
1407 	term_params = dma_alloc_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE,
1408 		&term_params_dma, GFP_KERNEL);
1409 
1410 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Uploading connection "
1411 		   "port_id=%06x.\n", fcport->rdata->ids.port_id);
1412 
1413 	qed_ops->destroy_conn(qedf->cdev, fcport->handle, term_params_dma);
1414 	qed_ops->release_conn(qedf->cdev, fcport->handle);
1415 
1416 	dma_free_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE, term_params,
1417 	    term_params_dma);
1418 }
1419 
1420 static void qedf_cleanup_fcport(struct qedf_ctx *qedf,
1421 	struct qedf_rport *fcport)
1422 {
1423 	struct fc_rport_priv *rdata = fcport->rdata;
1424 
1425 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Cleaning up portid=%06x.\n",
1426 	    fcport->rdata->ids.port_id);
1427 
1428 	/* Flush any remaining i/o's before we upload the connection */
1429 	qedf_flush_active_ios(fcport, -1);
1430 
1431 	if (test_and_clear_bit(QEDF_RPORT_SESSION_READY, &fcport->flags))
1432 		qedf_upload_connection(qedf, fcport);
1433 	qedf_free_sq(qedf, fcport);
1434 	fcport->rdata = NULL;
1435 	fcport->qedf = NULL;
1436 	kref_put(&rdata->kref, fc_rport_destroy);
1437 }
1438 
1439 /*
1440  * This event_callback is called after successful completion of libfc
1441  * initiated target login. qedf can proceed with initiating the session
1442  * establishment.
1443  */
1444 static void qedf_rport_event_handler(struct fc_lport *lport,
1445 				struct fc_rport_priv *rdata,
1446 				enum fc_rport_event event)
1447 {
1448 	struct qedf_ctx *qedf = lport_priv(lport);
1449 	struct fc_rport *rport = rdata->rport;
1450 	struct fc_rport_libfc_priv *rp;
1451 	struct qedf_rport *fcport;
1452 	u32 port_id;
1453 	int rval;
1454 	unsigned long flags;
1455 
1456 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "event = %d, "
1457 		   "port_id = 0x%x\n", event, rdata->ids.port_id);
1458 
1459 	switch (event) {
1460 	case RPORT_EV_READY:
1461 		if (!rport) {
1462 			QEDF_WARN(&(qedf->dbg_ctx), "rport is NULL.\n");
1463 			break;
1464 		}
1465 
1466 		rp = rport->dd_data;
1467 		fcport = (struct qedf_rport *)&rp[1];
1468 		fcport->qedf = qedf;
1469 
1470 		if (atomic_read(&qedf->num_offloads) >= QEDF_MAX_SESSIONS) {
1471 			QEDF_ERR(&(qedf->dbg_ctx), "Not offloading "
1472 			    "portid=0x%x as max number of offloaded sessions "
1473 			    "reached.\n", rdata->ids.port_id);
1474 			return;
1475 		}
1476 
1477 		/*
1478 		 * Don't try to offload the session again. Can happen when we
1479 		 * get an ADISC
1480 		 */
1481 		if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
1482 			QEDF_WARN(&(qedf->dbg_ctx), "Session already "
1483 				   "offloaded, portid=0x%x.\n",
1484 				   rdata->ids.port_id);
1485 			return;
1486 		}
1487 
1488 		if (rport->port_id == FC_FID_DIR_SERV) {
1489 			/*
1490 			 * qedf_rport structure doesn't exist for
1491 			 * directory server.
1492 			 * We should not come here, as lport will
1493 			 * take care of fabric login
1494 			 */
1495 			QEDF_WARN(&(qedf->dbg_ctx), "rport struct does not "
1496 			    "exist for dir server port_id=%x\n",
1497 			    rdata->ids.port_id);
1498 			break;
1499 		}
1500 
1501 		if (rdata->spp_type != FC_TYPE_FCP) {
1502 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1503 			    "Not offloading since spp type isn't FCP\n");
1504 			break;
1505 		}
1506 		if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
1507 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1508 			    "Not FCP target so not offloading\n");
1509 			break;
1510 		}
1511 
1512 		/* Initial reference held on entry, so this can't fail */
1513 		kref_get(&rdata->kref);
1514 		fcport->rdata = rdata;
1515 		fcport->rport = rport;
1516 
1517 		rval = qedf_alloc_sq(qedf, fcport);
1518 		if (rval) {
1519 			qedf_cleanup_fcport(qedf, fcport);
1520 			break;
1521 		}
1522 
1523 		/* Set device type */
1524 		if (rdata->flags & FC_RP_FLAGS_RETRY &&
1525 		    rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET &&
1526 		    !(rdata->ids.roles & FC_RPORT_ROLE_FCP_INITIATOR)) {
1527 			fcport->dev_type = QEDF_RPORT_TYPE_TAPE;
1528 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1529 			    "portid=%06x is a TAPE device.\n",
1530 			    rdata->ids.port_id);
1531 		} else {
1532 			fcport->dev_type = QEDF_RPORT_TYPE_DISK;
1533 		}
1534 
1535 		rval = qedf_offload_connection(qedf, fcport);
1536 		if (rval) {
1537 			qedf_cleanup_fcport(qedf, fcport);
1538 			break;
1539 		}
1540 
1541 		/* Add fcport to list of qedf_ctx list of offloaded ports */
1542 		spin_lock_irqsave(&qedf->hba_lock, flags);
1543 		list_add_rcu(&fcport->peers, &qedf->fcports);
1544 		spin_unlock_irqrestore(&qedf->hba_lock, flags);
1545 
1546 		/*
1547 		 * Set the session ready bit to let everyone know that this
1548 		 * connection is ready for I/O
1549 		 */
1550 		set_bit(QEDF_RPORT_SESSION_READY, &fcport->flags);
1551 		atomic_inc(&qedf->num_offloads);
1552 
1553 		break;
1554 	case RPORT_EV_LOGO:
1555 	case RPORT_EV_FAILED:
1556 	case RPORT_EV_STOP:
1557 		port_id = rdata->ids.port_id;
1558 		if (port_id == FC_FID_DIR_SERV)
1559 			break;
1560 
1561 		if (!rport) {
1562 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
1563 			    "port_id=%x - rport notcreated Yet!!\n", port_id);
1564 			break;
1565 		}
1566 		rp = rport->dd_data;
1567 		/*
1568 		 * Perform session upload. Note that rdata->peers is already
1569 		 * removed from disc->rports list before we get this event.
1570 		 */
1571 		fcport = (struct qedf_rport *)&rp[1];
1572 
1573 		spin_lock_irqsave(&fcport->rport_lock, flags);
1574 		/* Only free this fcport if it is offloaded already */
1575 		if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) &&
1576 		    !test_bit(QEDF_RPORT_UPLOADING_CONNECTION,
1577 		    &fcport->flags)) {
1578 			set_bit(QEDF_RPORT_UPLOADING_CONNECTION,
1579 				&fcport->flags);
1580 			spin_unlock_irqrestore(&fcport->rport_lock, flags);
1581 			qedf_cleanup_fcport(qedf, fcport);
1582 			/*
1583 			 * Remove fcport to list of qedf_ctx list of offloaded
1584 			 * ports
1585 			 */
1586 			spin_lock_irqsave(&qedf->hba_lock, flags);
1587 			list_del_rcu(&fcport->peers);
1588 			spin_unlock_irqrestore(&qedf->hba_lock, flags);
1589 
1590 			clear_bit(QEDF_RPORT_UPLOADING_CONNECTION,
1591 			    &fcport->flags);
1592 			atomic_dec(&qedf->num_offloads);
1593 		} else {
1594 			spin_unlock_irqrestore(&fcport->rport_lock, flags);
1595 		}
1596 		break;
1597 
1598 	case RPORT_EV_NONE:
1599 		break;
1600 	}
1601 }
1602 
1603 static void qedf_abort_io(struct fc_lport *lport)
1604 {
1605 	/* NO-OP but need to fill in the template */
1606 }
1607 
1608 static void qedf_fcp_cleanup(struct fc_lport *lport)
1609 {
1610 	/*
1611 	 * NO-OP but need to fill in template to prevent a NULL
1612 	 * function pointer dereference during link down. I/Os
1613 	 * will be flushed when port is uploaded.
1614 	 */
1615 }
1616 
1617 static struct libfc_function_template qedf_lport_template = {
1618 	.frame_send		= qedf_xmit,
1619 	.fcp_abort_io		= qedf_abort_io,
1620 	.fcp_cleanup		= qedf_fcp_cleanup,
1621 	.rport_event_callback	= qedf_rport_event_handler,
1622 	.elsct_send		= qedf_elsct_send,
1623 };
1624 
1625 static void qedf_fcoe_ctlr_setup(struct qedf_ctx *qedf)
1626 {
1627 	fcoe_ctlr_init(&qedf->ctlr, FIP_MODE_AUTO);
1628 
1629 	qedf->ctlr.send = qedf_fip_send;
1630 	qedf->ctlr.get_src_addr = qedf_get_src_mac;
1631 	ether_addr_copy(qedf->ctlr.ctl_src_addr, qedf->mac);
1632 }
1633 
1634 static void qedf_setup_fdmi(struct qedf_ctx *qedf)
1635 {
1636 	struct fc_lport *lport = qedf->lport;
1637 	struct fc_host_attrs *fc_host = shost_to_fc_host(lport->host);
1638 	u64 dsn;
1639 
1640 	/*
1641 	 * fdmi_enabled needs to be set for libfc to execute FDMI registration.
1642 	 */
1643 	lport->fdmi_enabled = 1;
1644 
1645 	/*
1646 	 * Setup the necessary fc_host attributes to that will be used to fill
1647 	 * in the FDMI information.
1648 	 */
1649 
1650 	/* Get the PCI-e Device Serial Number Capability */
1651 	dsn = pci_get_dsn(qedf->pdev);
1652 	if (dsn)
1653 		snprintf(fc_host->serial_number,
1654 		    sizeof(fc_host->serial_number), "%016llX", dsn);
1655 	else
1656 		snprintf(fc_host->serial_number,
1657 		    sizeof(fc_host->serial_number), "Unknown");
1658 
1659 	snprintf(fc_host->manufacturer,
1660 	    sizeof(fc_host->manufacturer), "%s", "Cavium Inc.");
1661 
1662 	snprintf(fc_host->model, sizeof(fc_host->model), "%s", "QL41000");
1663 
1664 	snprintf(fc_host->model_description, sizeof(fc_host->model_description),
1665 	    "%s", "QLogic FastLinQ QL41000 Series 10/25/40/50GGbE Controller"
1666 	    "(FCoE)");
1667 
1668 	snprintf(fc_host->hardware_version, sizeof(fc_host->hardware_version),
1669 	    "Rev %d", qedf->pdev->revision);
1670 
1671 	snprintf(fc_host->driver_version, sizeof(fc_host->driver_version),
1672 	    "%s", QEDF_VERSION);
1673 
1674 	snprintf(fc_host->firmware_version, sizeof(fc_host->firmware_version),
1675 	    "%d.%d.%d.%d", FW_MAJOR_VERSION, FW_MINOR_VERSION,
1676 	    FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
1677 }
1678 
1679 static int qedf_lport_setup(struct qedf_ctx *qedf)
1680 {
1681 	struct fc_lport *lport = qedf->lport;
1682 
1683 	lport->link_up = 0;
1684 	lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
1685 	lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
1686 	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
1687 	    FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
1688 	lport->boot_time = jiffies;
1689 	lport->e_d_tov = 2 * 1000;
1690 	lport->r_a_tov = 10 * 1000;
1691 
1692 	/* Set NPIV support */
1693 	lport->does_npiv = 1;
1694 	fc_host_max_npiv_vports(lport->host) = QEDF_MAX_NPIV;
1695 
1696 	fc_set_wwnn(lport, qedf->wwnn);
1697 	fc_set_wwpn(lport, qedf->wwpn);
1698 
1699 	if (fcoe_libfc_config(lport, &qedf->ctlr, &qedf_lport_template, 0)) {
1700 		QEDF_ERR(&qedf->dbg_ctx,
1701 			 "fcoe_libfc_config failed.\n");
1702 		return -ENOMEM;
1703 	}
1704 
1705 	/* Allocate the exchange manager */
1706 	fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_PARAMS_NUM_TASKS,
1707 			  0xfffe, NULL);
1708 
1709 	if (fc_lport_init_stats(lport))
1710 		return -ENOMEM;
1711 
1712 	/* Finish lport config */
1713 	fc_lport_config(lport);
1714 
1715 	/* Set max frame size */
1716 	fc_set_mfs(lport, QEDF_MFS);
1717 	fc_host_maxframe_size(lport->host) = lport->mfs;
1718 
1719 	/* Set default dev_loss_tmo based on module parameter */
1720 	fc_host_dev_loss_tmo(lport->host) = qedf_dev_loss_tmo;
1721 
1722 	/* Set symbolic node name */
1723 	snprintf(fc_host_symbolic_name(lport->host), 256,
1724 	    "QLogic %s v%s", QEDF_MODULE_NAME, QEDF_VERSION);
1725 
1726 	qedf_setup_fdmi(qedf);
1727 
1728 	return 0;
1729 }
1730 
1731 /*
1732  * NPIV functions
1733  */
1734 
1735 static int qedf_vport_libfc_config(struct fc_vport *vport,
1736 	struct fc_lport *lport)
1737 {
1738 	lport->link_up = 0;
1739 	lport->qfull = 0;
1740 	lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
1741 	lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
1742 	lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
1743 	    FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
1744 	lport->boot_time = jiffies;
1745 	lport->e_d_tov = 2 * 1000;
1746 	lport->r_a_tov = 10 * 1000;
1747 	lport->does_npiv = 1; /* Temporary until we add NPIV support */
1748 
1749 	/* Allocate stats for vport */
1750 	if (fc_lport_init_stats(lport))
1751 		return -ENOMEM;
1752 
1753 	/* Finish lport config */
1754 	fc_lport_config(lport);
1755 
1756 	/* offload related configuration */
1757 	lport->crc_offload = 0;
1758 	lport->seq_offload = 0;
1759 	lport->lro_enabled = 0;
1760 	lport->lro_xid = 0;
1761 	lport->lso_max = 0;
1762 
1763 	return 0;
1764 }
1765 
1766 static int qedf_vport_create(struct fc_vport *vport, bool disabled)
1767 {
1768 	struct Scsi_Host *shost = vport_to_shost(vport);
1769 	struct fc_lport *n_port = shost_priv(shost);
1770 	struct fc_lport *vn_port;
1771 	struct qedf_ctx *base_qedf = lport_priv(n_port);
1772 	struct qedf_ctx *vport_qedf;
1773 
1774 	char buf[32];
1775 	int rc = 0;
1776 
1777 	rc = fcoe_validate_vport_create(vport);
1778 	if (rc) {
1779 		fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1780 		QEDF_WARN(&(base_qedf->dbg_ctx), "Failed to create vport, "
1781 			   "WWPN (0x%s) already exists.\n", buf);
1782 		goto err1;
1783 	}
1784 
1785 	if (atomic_read(&base_qedf->link_state) != QEDF_LINK_UP) {
1786 		QEDF_WARN(&(base_qedf->dbg_ctx), "Cannot create vport "
1787 			   "because link is not up.\n");
1788 		rc = -EIO;
1789 		goto err1;
1790 	}
1791 
1792 	vn_port = libfc_vport_create(vport, sizeof(struct qedf_ctx));
1793 	if (!vn_port) {
1794 		QEDF_WARN(&(base_qedf->dbg_ctx), "Could not create lport "
1795 			   "for vport.\n");
1796 		rc = -ENOMEM;
1797 		goto err1;
1798 	}
1799 
1800 	fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1801 	QEDF_ERR(&(base_qedf->dbg_ctx), "Creating NPIV port, WWPN=%s.\n",
1802 	    buf);
1803 
1804 	/* Copy some fields from base_qedf */
1805 	vport_qedf = lport_priv(vn_port);
1806 	memcpy(vport_qedf, base_qedf, sizeof(struct qedf_ctx));
1807 
1808 	/* Set qedf data specific to this vport */
1809 	vport_qedf->lport = vn_port;
1810 	/* Use same hba_lock as base_qedf */
1811 	vport_qedf->hba_lock = base_qedf->hba_lock;
1812 	vport_qedf->pdev = base_qedf->pdev;
1813 	vport_qedf->cmd_mgr = base_qedf->cmd_mgr;
1814 	init_completion(&vport_qedf->flogi_compl);
1815 	INIT_LIST_HEAD(&vport_qedf->fcports);
1816 
1817 	rc = qedf_vport_libfc_config(vport, vn_port);
1818 	if (rc) {
1819 		QEDF_ERR(&(base_qedf->dbg_ctx), "Could not allocate memory "
1820 		    "for lport stats.\n");
1821 		goto err2;
1822 	}
1823 
1824 	fc_set_wwnn(vn_port, vport->node_name);
1825 	fc_set_wwpn(vn_port, vport->port_name);
1826 	vport_qedf->wwnn = vn_port->wwnn;
1827 	vport_qedf->wwpn = vn_port->wwpn;
1828 
1829 	vn_port->host->transportt = qedf_fc_vport_transport_template;
1830 	vn_port->host->can_queue = FCOE_PARAMS_NUM_TASKS;
1831 	vn_port->host->max_lun = qedf_max_lun;
1832 	vn_port->host->sg_tablesize = QEDF_MAX_BDS_PER_CMD;
1833 	vn_port->host->max_cmd_len = QEDF_MAX_CDB_LEN;
1834 
1835 	rc = scsi_add_host(vn_port->host, &vport->dev);
1836 	if (rc) {
1837 		QEDF_WARN(&base_qedf->dbg_ctx,
1838 			  "Error adding Scsi_Host rc=0x%x.\n", rc);
1839 		goto err2;
1840 	}
1841 
1842 	/* Set default dev_loss_tmo based on module parameter */
1843 	fc_host_dev_loss_tmo(vn_port->host) = qedf_dev_loss_tmo;
1844 
1845 	/* Init libfc stuffs */
1846 	memcpy(&vn_port->tt, &qedf_lport_template,
1847 		sizeof(qedf_lport_template));
1848 	fc_exch_init(vn_port);
1849 	fc_elsct_init(vn_port);
1850 	fc_lport_init(vn_port);
1851 	fc_disc_init(vn_port);
1852 	fc_disc_config(vn_port, vn_port);
1853 
1854 
1855 	/* Allocate the exchange manager */
1856 	shost = vport_to_shost(vport);
1857 	n_port = shost_priv(shost);
1858 	fc_exch_mgr_list_clone(n_port, vn_port);
1859 
1860 	/* Set max frame size */
1861 	fc_set_mfs(vn_port, QEDF_MFS);
1862 
1863 	fc_host_port_type(vn_port->host) = FC_PORTTYPE_UNKNOWN;
1864 
1865 	if (disabled) {
1866 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
1867 	} else {
1868 		vn_port->boot_time = jiffies;
1869 		fc_fabric_login(vn_port);
1870 		fc_vport_setlink(vn_port);
1871 	}
1872 
1873 	QEDF_INFO(&(base_qedf->dbg_ctx), QEDF_LOG_NPIV, "vn_port=%p.\n",
1874 		   vn_port);
1875 
1876 	/* Set up debug context for vport */
1877 	vport_qedf->dbg_ctx.host_no = vn_port->host->host_no;
1878 	vport_qedf->dbg_ctx.pdev = base_qedf->pdev;
1879 
1880 err2:
1881 	scsi_host_put(vn_port->host);
1882 err1:
1883 	return rc;
1884 }
1885 
1886 static int qedf_vport_destroy(struct fc_vport *vport)
1887 {
1888 	struct Scsi_Host *shost = vport_to_shost(vport);
1889 	struct fc_lport *n_port = shost_priv(shost);
1890 	struct fc_lport *vn_port = vport->dd_data;
1891 	struct qedf_ctx *qedf = lport_priv(vn_port);
1892 
1893 	if (!qedf) {
1894 		QEDF_ERR(NULL, "qedf is NULL.\n");
1895 		goto out;
1896 	}
1897 
1898 	/* Set unloading bit on vport qedf_ctx to prevent more I/O */
1899 	set_bit(QEDF_UNLOADING, &qedf->flags);
1900 
1901 	mutex_lock(&n_port->lp_mutex);
1902 	list_del(&vn_port->list);
1903 	mutex_unlock(&n_port->lp_mutex);
1904 
1905 	fc_fabric_logoff(vn_port);
1906 	fc_lport_destroy(vn_port);
1907 
1908 	/* Detach from scsi-ml */
1909 	fc_remove_host(vn_port->host);
1910 	scsi_remove_host(vn_port->host);
1911 
1912 	/*
1913 	 * Only try to release the exchange manager if the vn_port
1914 	 * configuration is complete.
1915 	 */
1916 	if (vn_port->state == LPORT_ST_READY)
1917 		fc_exch_mgr_free(vn_port);
1918 
1919 	/* Free memory used by statistical counters */
1920 	fc_lport_free_stats(vn_port);
1921 
1922 	/* Release Scsi_Host */
1923 	if (vn_port->host)
1924 		scsi_host_put(vn_port->host);
1925 
1926 out:
1927 	return 0;
1928 }
1929 
1930 static int qedf_vport_disable(struct fc_vport *vport, bool disable)
1931 {
1932 	struct fc_lport *lport = vport->dd_data;
1933 
1934 	if (disable) {
1935 		fc_vport_set_state(vport, FC_VPORT_DISABLED);
1936 		fc_fabric_logoff(lport);
1937 	} else {
1938 		lport->boot_time = jiffies;
1939 		fc_fabric_login(lport);
1940 		fc_vport_setlink(lport);
1941 	}
1942 	return 0;
1943 }
1944 
1945 /*
1946  * During removal we need to wait for all the vports associated with a port
1947  * to be destroyed so we avoid a race condition where libfc is still trying
1948  * to reap vports while the driver remove function has already reaped the
1949  * driver contexts associated with the physical port.
1950  */
1951 static void qedf_wait_for_vport_destroy(struct qedf_ctx *qedf)
1952 {
1953 	struct fc_host_attrs *fc_host = shost_to_fc_host(qedf->lport->host);
1954 
1955 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
1956 	    "Entered.\n");
1957 	while (fc_host->npiv_vports_inuse > 0) {
1958 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
1959 		    "Waiting for all vports to be reaped.\n");
1960 		msleep(1000);
1961 	}
1962 }
1963 
1964 /**
1965  * qedf_fcoe_reset - Resets the fcoe
1966  *
1967  * @shost: shost the reset is from
1968  *
1969  * Returns: always 0
1970  */
1971 static int qedf_fcoe_reset(struct Scsi_Host *shost)
1972 {
1973 	struct fc_lport *lport = shost_priv(shost);
1974 
1975 	qedf_ctx_soft_reset(lport);
1976 	return 0;
1977 }
1978 
1979 static void qedf_get_host_port_id(struct Scsi_Host *shost)
1980 {
1981 	struct fc_lport *lport = shost_priv(shost);
1982 
1983 	fc_host_port_id(shost) = lport->port_id;
1984 }
1985 
1986 static struct fc_host_statistics *qedf_fc_get_host_stats(struct Scsi_Host
1987 	*shost)
1988 {
1989 	struct fc_host_statistics *qedf_stats;
1990 	struct fc_lport *lport = shost_priv(shost);
1991 	struct qedf_ctx *qedf = lport_priv(lport);
1992 	struct qed_fcoe_stats *fw_fcoe_stats;
1993 
1994 	qedf_stats = fc_get_host_stats(shost);
1995 
1996 	/* We don't collect offload stats for specific NPIV ports */
1997 	if (lport->vport)
1998 		goto out;
1999 
2000 	fw_fcoe_stats = kmalloc(sizeof(struct qed_fcoe_stats), GFP_KERNEL);
2001 	if (!fw_fcoe_stats) {
2002 		QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate memory for "
2003 		    "fw_fcoe_stats.\n");
2004 		goto out;
2005 	}
2006 
2007 	mutex_lock(&qedf->stats_mutex);
2008 
2009 	/* Query firmware for offload stats */
2010 	qed_ops->get_stats(qedf->cdev, fw_fcoe_stats);
2011 
2012 	/*
2013 	 * The expectation is that we add our offload stats to the stats
2014 	 * being maintained by libfc each time the fc_get_host_status callback
2015 	 * is invoked. The additions are not carried over for each call to
2016 	 * the fc_get_host_stats callback.
2017 	 */
2018 	qedf_stats->tx_frames += fw_fcoe_stats->fcoe_tx_data_pkt_cnt +
2019 	    fw_fcoe_stats->fcoe_tx_xfer_pkt_cnt +
2020 	    fw_fcoe_stats->fcoe_tx_other_pkt_cnt;
2021 	qedf_stats->rx_frames += fw_fcoe_stats->fcoe_rx_data_pkt_cnt +
2022 	    fw_fcoe_stats->fcoe_rx_xfer_pkt_cnt +
2023 	    fw_fcoe_stats->fcoe_rx_other_pkt_cnt;
2024 	qedf_stats->fcp_input_megabytes +=
2025 	    do_div(fw_fcoe_stats->fcoe_rx_byte_cnt, 1000000);
2026 	qedf_stats->fcp_output_megabytes +=
2027 	    do_div(fw_fcoe_stats->fcoe_tx_byte_cnt, 1000000);
2028 	qedf_stats->rx_words += fw_fcoe_stats->fcoe_rx_byte_cnt / 4;
2029 	qedf_stats->tx_words += fw_fcoe_stats->fcoe_tx_byte_cnt / 4;
2030 	qedf_stats->invalid_crc_count +=
2031 	    fw_fcoe_stats->fcoe_silent_drop_pkt_crc_error_cnt;
2032 	qedf_stats->dumped_frames =
2033 	    fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
2034 	qedf_stats->error_frames +=
2035 	    fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
2036 	qedf_stats->fcp_input_requests += qedf->input_requests;
2037 	qedf_stats->fcp_output_requests += qedf->output_requests;
2038 	qedf_stats->fcp_control_requests += qedf->control_requests;
2039 	qedf_stats->fcp_packet_aborts += qedf->packet_aborts;
2040 	qedf_stats->fcp_frame_alloc_failures += qedf->alloc_failures;
2041 
2042 	mutex_unlock(&qedf->stats_mutex);
2043 	kfree(fw_fcoe_stats);
2044 out:
2045 	return qedf_stats;
2046 }
2047 
2048 static struct fc_function_template qedf_fc_transport_fn = {
2049 	.show_host_node_name = 1,
2050 	.show_host_port_name = 1,
2051 	.show_host_supported_classes = 1,
2052 	.show_host_supported_fc4s = 1,
2053 	.show_host_active_fc4s = 1,
2054 	.show_host_maxframe_size = 1,
2055 
2056 	.get_host_port_id = qedf_get_host_port_id,
2057 	.show_host_port_id = 1,
2058 	.show_host_supported_speeds = 1,
2059 	.get_host_speed = fc_get_host_speed,
2060 	.show_host_speed = 1,
2061 	.show_host_port_type = 1,
2062 	.get_host_port_state = fc_get_host_port_state,
2063 	.show_host_port_state = 1,
2064 	.show_host_symbolic_name = 1,
2065 
2066 	/*
2067 	 * Tell FC transport to allocate enough space to store the backpointer
2068 	 * for the associate qedf_rport struct.
2069 	 */
2070 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2071 				sizeof(struct qedf_rport)),
2072 	.show_rport_maxframe_size = 1,
2073 	.show_rport_supported_classes = 1,
2074 	.show_host_fabric_name = 1,
2075 	.show_starget_node_name = 1,
2076 	.show_starget_port_name = 1,
2077 	.show_starget_port_id = 1,
2078 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2079 	.show_rport_dev_loss_tmo = 1,
2080 	.get_fc_host_stats = qedf_fc_get_host_stats,
2081 	.issue_fc_host_lip = qedf_fcoe_reset,
2082 	.vport_create = qedf_vport_create,
2083 	.vport_delete = qedf_vport_destroy,
2084 	.vport_disable = qedf_vport_disable,
2085 	.bsg_request = fc_lport_bsg_request,
2086 };
2087 
2088 static struct fc_function_template qedf_fc_vport_transport_fn = {
2089 	.show_host_node_name = 1,
2090 	.show_host_port_name = 1,
2091 	.show_host_supported_classes = 1,
2092 	.show_host_supported_fc4s = 1,
2093 	.show_host_active_fc4s = 1,
2094 	.show_host_maxframe_size = 1,
2095 	.show_host_port_id = 1,
2096 	.show_host_supported_speeds = 1,
2097 	.get_host_speed = fc_get_host_speed,
2098 	.show_host_speed = 1,
2099 	.show_host_port_type = 1,
2100 	.get_host_port_state = fc_get_host_port_state,
2101 	.show_host_port_state = 1,
2102 	.show_host_symbolic_name = 1,
2103 	.dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2104 				sizeof(struct qedf_rport)),
2105 	.show_rport_maxframe_size = 1,
2106 	.show_rport_supported_classes = 1,
2107 	.show_host_fabric_name = 1,
2108 	.show_starget_node_name = 1,
2109 	.show_starget_port_name = 1,
2110 	.show_starget_port_id = 1,
2111 	.set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2112 	.show_rport_dev_loss_tmo = 1,
2113 	.get_fc_host_stats = fc_get_host_stats,
2114 	.issue_fc_host_lip = qedf_fcoe_reset,
2115 	.bsg_request = fc_lport_bsg_request,
2116 };
2117 
2118 static bool qedf_fp_has_work(struct qedf_fastpath *fp)
2119 {
2120 	struct qedf_ctx *qedf = fp->qedf;
2121 	struct global_queue *que;
2122 	struct qed_sb_info *sb_info = fp->sb_info;
2123 	struct status_block_e4 *sb = sb_info->sb_virt;
2124 	u16 prod_idx;
2125 
2126 	/* Get the pointer to the global CQ this completion is on */
2127 	que = qedf->global_queues[fp->sb_id];
2128 
2129 	/* Be sure all responses have been written to PI */
2130 	rmb();
2131 
2132 	/* Get the current firmware producer index */
2133 	prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
2134 
2135 	return (que->cq_prod_idx != prod_idx);
2136 }
2137 
2138 /*
2139  * Interrupt handler code.
2140  */
2141 
2142 /* Process completion queue and copy CQE contents for deferred processesing
2143  *
2144  * Return true if we should wake the I/O thread, false if not.
2145  */
2146 static bool qedf_process_completions(struct qedf_fastpath *fp)
2147 {
2148 	struct qedf_ctx *qedf = fp->qedf;
2149 	struct qed_sb_info *sb_info = fp->sb_info;
2150 	struct status_block_e4 *sb = sb_info->sb_virt;
2151 	struct global_queue *que;
2152 	u16 prod_idx;
2153 	struct fcoe_cqe *cqe;
2154 	struct qedf_io_work *io_work;
2155 	int num_handled = 0;
2156 	unsigned int cpu;
2157 	struct qedf_ioreq *io_req = NULL;
2158 	u16 xid;
2159 	u16 new_cqes;
2160 	u32 comp_type;
2161 
2162 	/* Get the current firmware producer index */
2163 	prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
2164 
2165 	/* Get the pointer to the global CQ this completion is on */
2166 	que = qedf->global_queues[fp->sb_id];
2167 
2168 	/* Calculate the amount of new elements since last processing */
2169 	new_cqes = (prod_idx >= que->cq_prod_idx) ?
2170 	    (prod_idx - que->cq_prod_idx) :
2171 	    0x10000 - que->cq_prod_idx + prod_idx;
2172 
2173 	/* Save producer index */
2174 	que->cq_prod_idx = prod_idx;
2175 
2176 	while (new_cqes) {
2177 		fp->completions++;
2178 		num_handled++;
2179 		cqe = &que->cq[que->cq_cons_idx];
2180 
2181 		comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
2182 		    FCOE_CQE_CQE_TYPE_MASK;
2183 
2184 		/*
2185 		 * Process unsolicited CQEs directly in the interrupt handler
2186 		 * sine we need the fastpath ID
2187 		 */
2188 		if (comp_type == FCOE_UNSOLIC_CQE_TYPE) {
2189 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
2190 			   "Unsolicated CQE.\n");
2191 			qedf_process_unsol_compl(qedf, fp->sb_id, cqe);
2192 			/*
2193 			 * Don't add a work list item.  Increment consumer
2194 			 * consumer index and move on.
2195 			 */
2196 			goto inc_idx;
2197 		}
2198 
2199 		xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
2200 		io_req = &qedf->cmd_mgr->cmds[xid];
2201 
2202 		/*
2203 		 * Figure out which percpu thread we should queue this I/O
2204 		 * on.
2205 		 */
2206 		if (!io_req)
2207 			/* If there is not io_req assocated with this CQE
2208 			 * just queue it on CPU 0
2209 			 */
2210 			cpu = 0;
2211 		else {
2212 			cpu = io_req->cpu;
2213 			io_req->int_cpu = smp_processor_id();
2214 		}
2215 
2216 		io_work = mempool_alloc(qedf->io_mempool, GFP_ATOMIC);
2217 		if (!io_work) {
2218 			QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate "
2219 				   "work for I/O completion.\n");
2220 			continue;
2221 		}
2222 		memset(io_work, 0, sizeof(struct qedf_io_work));
2223 
2224 		INIT_WORK(&io_work->work, qedf_fp_io_handler);
2225 
2226 		/* Copy contents of CQE for deferred processing */
2227 		memcpy(&io_work->cqe, cqe, sizeof(struct fcoe_cqe));
2228 
2229 		io_work->qedf = fp->qedf;
2230 		io_work->fp = NULL; /* Only used for unsolicited frames */
2231 
2232 		queue_work_on(cpu, qedf_io_wq, &io_work->work);
2233 
2234 inc_idx:
2235 		que->cq_cons_idx++;
2236 		if (que->cq_cons_idx == fp->cq_num_entries)
2237 			que->cq_cons_idx = 0;
2238 		new_cqes--;
2239 	}
2240 
2241 	return true;
2242 }
2243 
2244 
2245 /* MSI-X fastpath handler code */
2246 static irqreturn_t qedf_msix_handler(int irq, void *dev_id)
2247 {
2248 	struct qedf_fastpath *fp = dev_id;
2249 
2250 	if (!fp) {
2251 		QEDF_ERR(NULL, "fp is null.\n");
2252 		return IRQ_HANDLED;
2253 	}
2254 	if (!fp->sb_info) {
2255 		QEDF_ERR(NULL, "fp->sb_info in null.");
2256 		return IRQ_HANDLED;
2257 	}
2258 
2259 	/*
2260 	 * Disable interrupts for this status block while we process new
2261 	 * completions
2262 	 */
2263 	qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);
2264 
2265 	while (1) {
2266 		qedf_process_completions(fp);
2267 
2268 		if (qedf_fp_has_work(fp) == 0) {
2269 			/* Update the sb information */
2270 			qed_sb_update_sb_idx(fp->sb_info);
2271 
2272 			/* Check for more work */
2273 			rmb();
2274 
2275 			if (qedf_fp_has_work(fp) == 0) {
2276 				/* Re-enable interrupts */
2277 				qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
2278 				return IRQ_HANDLED;
2279 			}
2280 		}
2281 	}
2282 
2283 	/* Do we ever want to break out of above loop? */
2284 	return IRQ_HANDLED;
2285 }
2286 
2287 /* simd handler for MSI/INTa */
2288 static void qedf_simd_int_handler(void *cookie)
2289 {
2290 	/* Cookie is qedf_ctx struct */
2291 	struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
2292 
2293 	QEDF_WARN(&(qedf->dbg_ctx), "qedf=%p.\n", qedf);
2294 }
2295 
2296 #define QEDF_SIMD_HANDLER_NUM		0
2297 static void qedf_sync_free_irqs(struct qedf_ctx *qedf)
2298 {
2299 	int i;
2300 	u16 vector_idx = 0;
2301 	u32 vector;
2302 
2303 	if (qedf->int_info.msix_cnt) {
2304 		for (i = 0; i < qedf->int_info.used_cnt; i++) {
2305 			vector_idx = i * qedf->dev_info.common.num_hwfns +
2306 				qed_ops->common->get_affin_hwfn_idx(qedf->cdev);
2307 			QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
2308 				  "Freeing IRQ #%d vector_idx=%d.\n",
2309 				  i, vector_idx);
2310 			vector = qedf->int_info.msix[vector_idx].vector;
2311 			synchronize_irq(vector);
2312 			irq_set_affinity_hint(vector, NULL);
2313 			irq_set_affinity_notifier(vector, NULL);
2314 			free_irq(vector, &qedf->fp_array[i]);
2315 		}
2316 	} else
2317 		qed_ops->common->simd_handler_clean(qedf->cdev,
2318 		    QEDF_SIMD_HANDLER_NUM);
2319 
2320 	qedf->int_info.used_cnt = 0;
2321 	qed_ops->common->set_fp_int(qedf->cdev, 0);
2322 }
2323 
2324 static int qedf_request_msix_irq(struct qedf_ctx *qedf)
2325 {
2326 	int i, rc, cpu;
2327 	u16 vector_idx = 0;
2328 	u32 vector;
2329 
2330 	cpu = cpumask_first(cpu_online_mask);
2331 	for (i = 0; i < qedf->num_queues; i++) {
2332 		vector_idx = i * qedf->dev_info.common.num_hwfns +
2333 			qed_ops->common->get_affin_hwfn_idx(qedf->cdev);
2334 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
2335 			  "Requesting IRQ #%d vector_idx=%d.\n",
2336 			  i, vector_idx);
2337 		vector = qedf->int_info.msix[vector_idx].vector;
2338 		rc = request_irq(vector, qedf_msix_handler, 0, "qedf",
2339 				 &qedf->fp_array[i]);
2340 
2341 		if (rc) {
2342 			QEDF_WARN(&(qedf->dbg_ctx), "request_irq failed.\n");
2343 			qedf_sync_free_irqs(qedf);
2344 			return rc;
2345 		}
2346 
2347 		qedf->int_info.used_cnt++;
2348 		rc = irq_set_affinity_hint(vector, get_cpu_mask(cpu));
2349 		cpu = cpumask_next(cpu, cpu_online_mask);
2350 	}
2351 
2352 	return 0;
2353 }
2354 
2355 static int qedf_setup_int(struct qedf_ctx *qedf)
2356 {
2357 	int rc = 0;
2358 
2359 	/*
2360 	 * Learn interrupt configuration
2361 	 */
2362 	rc = qed_ops->common->set_fp_int(qedf->cdev, num_online_cpus());
2363 	if (rc <= 0)
2364 		return 0;
2365 
2366 	rc  = qed_ops->common->get_fp_int(qedf->cdev, &qedf->int_info);
2367 	if (rc)
2368 		return 0;
2369 
2370 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of msix_cnt = "
2371 		   "0x%x num of cpus = 0x%x\n", qedf->int_info.msix_cnt,
2372 		   num_online_cpus());
2373 
2374 	if (qedf->int_info.msix_cnt)
2375 		return qedf_request_msix_irq(qedf);
2376 
2377 	qed_ops->common->simd_handler_config(qedf->cdev, &qedf,
2378 	    QEDF_SIMD_HANDLER_NUM, qedf_simd_int_handler);
2379 	qedf->int_info.used_cnt = 1;
2380 
2381 	QEDF_ERR(&qedf->dbg_ctx,
2382 		 "Cannot load driver due to a lack of MSI-X vectors.\n");
2383 	return -EINVAL;
2384 }
2385 
2386 /* Main function for libfc frame reception */
2387 static void qedf_recv_frame(struct qedf_ctx *qedf,
2388 	struct sk_buff *skb)
2389 {
2390 	u32 fr_len;
2391 	struct fc_lport *lport;
2392 	struct fc_frame_header *fh;
2393 	struct fcoe_crc_eof crc_eof;
2394 	struct fc_frame *fp;
2395 	u8 *mac = NULL;
2396 	u8 *dest_mac = NULL;
2397 	struct fcoe_hdr *hp;
2398 	struct qedf_rport *fcport;
2399 	struct fc_lport *vn_port;
2400 	u32 f_ctl;
2401 
2402 	lport = qedf->lport;
2403 	if (lport == NULL || lport->state == LPORT_ST_DISABLED) {
2404 		QEDF_WARN(NULL, "Invalid lport struct or lport disabled.\n");
2405 		kfree_skb(skb);
2406 		return;
2407 	}
2408 
2409 	if (skb_is_nonlinear(skb))
2410 		skb_linearize(skb);
2411 	mac = eth_hdr(skb)->h_source;
2412 	dest_mac = eth_hdr(skb)->h_dest;
2413 
2414 	/* Pull the header */
2415 	hp = (struct fcoe_hdr *)skb->data;
2416 	fh = (struct fc_frame_header *) skb_transport_header(skb);
2417 	skb_pull(skb, sizeof(struct fcoe_hdr));
2418 	fr_len = skb->len - sizeof(struct fcoe_crc_eof);
2419 
2420 	fp = (struct fc_frame *)skb;
2421 	fc_frame_init(fp);
2422 	fr_dev(fp) = lport;
2423 	fr_sof(fp) = hp->fcoe_sof;
2424 	if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
2425 		QEDF_INFO(NULL, QEDF_LOG_LL2, "skb_copy_bits failed.\n");
2426 		kfree_skb(skb);
2427 		return;
2428 	}
2429 	fr_eof(fp) = crc_eof.fcoe_eof;
2430 	fr_crc(fp) = crc_eof.fcoe_crc32;
2431 	if (pskb_trim(skb, fr_len)) {
2432 		QEDF_INFO(NULL, QEDF_LOG_LL2, "pskb_trim failed.\n");
2433 		kfree_skb(skb);
2434 		return;
2435 	}
2436 
2437 	fh = fc_frame_header_get(fp);
2438 
2439 	/*
2440 	 * Invalid frame filters.
2441 	 */
2442 
2443 	if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
2444 	    fh->fh_type == FC_TYPE_FCP) {
2445 		/* Drop FCP data. We dont this in L2 path */
2446 		kfree_skb(skb);
2447 		return;
2448 	}
2449 	if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
2450 	    fh->fh_type == FC_TYPE_ELS) {
2451 		switch (fc_frame_payload_op(fp)) {
2452 		case ELS_LOGO:
2453 			if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
2454 				/* drop non-FIP LOGO */
2455 				kfree_skb(skb);
2456 				return;
2457 			}
2458 			break;
2459 		}
2460 	}
2461 
2462 	if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
2463 		/* Drop incoming ABTS */
2464 		kfree_skb(skb);
2465 		return;
2466 	}
2467 
2468 	if (ntoh24(&dest_mac[3]) != ntoh24(fh->fh_d_id)) {
2469 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2470 		    "FC frame d_id mismatch with MAC %pM.\n", dest_mac);
2471 		kfree_skb(skb);
2472 		return;
2473 	}
2474 
2475 	if (qedf->ctlr.state) {
2476 		if (!ether_addr_equal(mac, qedf->ctlr.dest_addr)) {
2477 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2478 			    "Wrong source address: mac:%pM dest_addr:%pM.\n",
2479 			    mac, qedf->ctlr.dest_addr);
2480 			kfree_skb(skb);
2481 			return;
2482 		}
2483 	}
2484 
2485 	vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
2486 
2487 	/*
2488 	 * If the destination ID from the frame header does not match what we
2489 	 * have on record for lport and the search for a NPIV port came up
2490 	 * empty then this is not addressed to our port so simply drop it.
2491 	 */
2492 	if (lport->port_id != ntoh24(fh->fh_d_id) && !vn_port) {
2493 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
2494 			  "Dropping frame due to destination mismatch: lport->port_id=0x%x fh->d_id=0x%x.\n",
2495 			  lport->port_id, ntoh24(fh->fh_d_id));
2496 		kfree_skb(skb);
2497 		return;
2498 	}
2499 
2500 	f_ctl = ntoh24(fh->fh_f_ctl);
2501 	if ((fh->fh_type == FC_TYPE_BLS) && (f_ctl & FC_FC_SEQ_CTX) &&
2502 	    (f_ctl & FC_FC_EX_CTX)) {
2503 		/* Drop incoming ABTS response that has both SEQ/EX CTX set */
2504 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
2505 			  "Dropping ABTS response as both SEQ/EX CTX set.\n");
2506 		kfree_skb(skb);
2507 		return;
2508 	}
2509 
2510 	/*
2511 	 * If a connection is uploading, drop incoming FCoE frames as there
2512 	 * is a small window where we could try to return a frame while libfc
2513 	 * is trying to clean things up.
2514 	 */
2515 
2516 	/* Get fcport associated with d_id if it exists */
2517 	fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
2518 
2519 	if (fcport && test_bit(QEDF_RPORT_UPLOADING_CONNECTION,
2520 	    &fcport->flags)) {
2521 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
2522 		    "Connection uploading, dropping fp=%p.\n", fp);
2523 		kfree_skb(skb);
2524 		return;
2525 	}
2526 
2527 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame receive: "
2528 	    "skb=%p fp=%p src=%06x dest=%06x r_ctl=%x fh_type=%x.\n", skb, fp,
2529 	    ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl,
2530 	    fh->fh_type);
2531 	if (qedf_dump_frames)
2532 		print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
2533 		    1, skb->data, skb->len, false);
2534 	fc_exch_recv(lport, fp);
2535 }
2536 
2537 static void qedf_ll2_process_skb(struct work_struct *work)
2538 {
2539 	struct qedf_skb_work *skb_work =
2540 	    container_of(work, struct qedf_skb_work, work);
2541 	struct qedf_ctx *qedf = skb_work->qedf;
2542 	struct sk_buff *skb = skb_work->skb;
2543 	struct ethhdr *eh;
2544 
2545 	if (!qedf) {
2546 		QEDF_ERR(NULL, "qedf is NULL\n");
2547 		goto err_out;
2548 	}
2549 
2550 	eh = (struct ethhdr *)skb->data;
2551 
2552 	/* Undo VLAN encapsulation */
2553 	if (eh->h_proto == htons(ETH_P_8021Q)) {
2554 		memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
2555 		eh = skb_pull(skb, VLAN_HLEN);
2556 		skb_reset_mac_header(skb);
2557 	}
2558 
2559 	/*
2560 	 * Process either a FIP frame or FCoE frame based on the
2561 	 * protocol value.  If it's not either just drop the
2562 	 * frame.
2563 	 */
2564 	if (eh->h_proto == htons(ETH_P_FIP)) {
2565 		qedf_fip_recv(qedf, skb);
2566 		goto out;
2567 	} else if (eh->h_proto == htons(ETH_P_FCOE)) {
2568 		__skb_pull(skb, ETH_HLEN);
2569 		qedf_recv_frame(qedf, skb);
2570 		goto out;
2571 	} else
2572 		goto err_out;
2573 
2574 err_out:
2575 	kfree_skb(skb);
2576 out:
2577 	kfree(skb_work);
2578 	return;
2579 }
2580 
2581 static int qedf_ll2_rx(void *cookie, struct sk_buff *skb,
2582 	u32 arg1, u32 arg2)
2583 {
2584 	struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
2585 	struct qedf_skb_work *skb_work;
2586 
2587 	if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
2588 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
2589 			  "Dropping frame as link state is down.\n");
2590 		kfree_skb(skb);
2591 		return 0;
2592 	}
2593 
2594 	skb_work = kzalloc(sizeof(struct qedf_skb_work), GFP_ATOMIC);
2595 	if (!skb_work) {
2596 		QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate skb_work so "
2597 			   "dropping frame.\n");
2598 		kfree_skb(skb);
2599 		return 0;
2600 	}
2601 
2602 	INIT_WORK(&skb_work->work, qedf_ll2_process_skb);
2603 	skb_work->skb = skb;
2604 	skb_work->qedf = qedf;
2605 	queue_work(qedf->ll2_recv_wq, &skb_work->work);
2606 
2607 	return 0;
2608 }
2609 
2610 static struct qed_ll2_cb_ops qedf_ll2_cb_ops = {
2611 	.rx_cb = qedf_ll2_rx,
2612 	.tx_cb = NULL,
2613 };
2614 
2615 /* Main thread to process I/O completions */
2616 void qedf_fp_io_handler(struct work_struct *work)
2617 {
2618 	struct qedf_io_work *io_work =
2619 	    container_of(work, struct qedf_io_work, work);
2620 	u32 comp_type;
2621 
2622 	/*
2623 	 * Deferred part of unsolicited CQE sends
2624 	 * frame to libfc.
2625 	 */
2626 	comp_type = (io_work->cqe.cqe_data >>
2627 	    FCOE_CQE_CQE_TYPE_SHIFT) &
2628 	    FCOE_CQE_CQE_TYPE_MASK;
2629 	if (comp_type == FCOE_UNSOLIC_CQE_TYPE &&
2630 	    io_work->fp)
2631 		fc_exch_recv(io_work->qedf->lport, io_work->fp);
2632 	else
2633 		qedf_process_cqe(io_work->qedf, &io_work->cqe);
2634 
2635 	kfree(io_work);
2636 }
2637 
2638 static int qedf_alloc_and_init_sb(struct qedf_ctx *qedf,
2639 	struct qed_sb_info *sb_info, u16 sb_id)
2640 {
2641 	struct status_block_e4 *sb_virt;
2642 	dma_addr_t sb_phys;
2643 	int ret;
2644 
2645 	sb_virt = dma_alloc_coherent(&qedf->pdev->dev,
2646 	    sizeof(struct status_block_e4), &sb_phys, GFP_KERNEL);
2647 
2648 	if (!sb_virt) {
2649 		QEDF_ERR(&qedf->dbg_ctx,
2650 			 "Status block allocation failed for id = %d.\n",
2651 			 sb_id);
2652 		return -ENOMEM;
2653 	}
2654 
2655 	ret = qed_ops->common->sb_init(qedf->cdev, sb_info, sb_virt, sb_phys,
2656 	    sb_id, QED_SB_TYPE_STORAGE);
2657 
2658 	if (ret) {
2659 		QEDF_ERR(&qedf->dbg_ctx,
2660 			 "Status block initialization failed (0x%x) for id = %d.\n",
2661 			 ret, sb_id);
2662 		return ret;
2663 	}
2664 
2665 	return 0;
2666 }
2667 
2668 static void qedf_free_sb(struct qedf_ctx *qedf, struct qed_sb_info *sb_info)
2669 {
2670 	if (sb_info->sb_virt)
2671 		dma_free_coherent(&qedf->pdev->dev, sizeof(*sb_info->sb_virt),
2672 		    (void *)sb_info->sb_virt, sb_info->sb_phys);
2673 }
2674 
2675 static void qedf_destroy_sb(struct qedf_ctx *qedf)
2676 {
2677 	int id;
2678 	struct qedf_fastpath *fp = NULL;
2679 
2680 	for (id = 0; id < qedf->num_queues; id++) {
2681 		fp = &(qedf->fp_array[id]);
2682 		if (fp->sb_id == QEDF_SB_ID_NULL)
2683 			break;
2684 		qedf_free_sb(qedf, fp->sb_info);
2685 		kfree(fp->sb_info);
2686 	}
2687 	kfree(qedf->fp_array);
2688 }
2689 
2690 static int qedf_prepare_sb(struct qedf_ctx *qedf)
2691 {
2692 	int id;
2693 	struct qedf_fastpath *fp;
2694 	int ret;
2695 
2696 	qedf->fp_array =
2697 	    kcalloc(qedf->num_queues, sizeof(struct qedf_fastpath),
2698 		GFP_KERNEL);
2699 
2700 	if (!qedf->fp_array) {
2701 		QEDF_ERR(&(qedf->dbg_ctx), "fastpath array allocation "
2702 			  "failed.\n");
2703 		return -ENOMEM;
2704 	}
2705 
2706 	for (id = 0; id < qedf->num_queues; id++) {
2707 		fp = &(qedf->fp_array[id]);
2708 		fp->sb_id = QEDF_SB_ID_NULL;
2709 		fp->sb_info = kcalloc(1, sizeof(*fp->sb_info), GFP_KERNEL);
2710 		if (!fp->sb_info) {
2711 			QEDF_ERR(&(qedf->dbg_ctx), "SB info struct "
2712 				  "allocation failed.\n");
2713 			goto err;
2714 		}
2715 		ret = qedf_alloc_and_init_sb(qedf, fp->sb_info, id);
2716 		if (ret) {
2717 			QEDF_ERR(&(qedf->dbg_ctx), "SB allocation and "
2718 				  "initialization failed.\n");
2719 			goto err;
2720 		}
2721 		fp->sb_id = id;
2722 		fp->qedf = qedf;
2723 		fp->cq_num_entries =
2724 		    qedf->global_queues[id]->cq_mem_size /
2725 		    sizeof(struct fcoe_cqe);
2726 	}
2727 err:
2728 	return 0;
2729 }
2730 
2731 void qedf_process_cqe(struct qedf_ctx *qedf, struct fcoe_cqe *cqe)
2732 {
2733 	u16 xid;
2734 	struct qedf_ioreq *io_req;
2735 	struct qedf_rport *fcport;
2736 	u32 comp_type;
2737 
2738 	comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
2739 	    FCOE_CQE_CQE_TYPE_MASK;
2740 
2741 	xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
2742 	io_req = &qedf->cmd_mgr->cmds[xid];
2743 
2744 	/* Completion not for a valid I/O anymore so just return */
2745 	if (!io_req) {
2746 		QEDF_ERR(&qedf->dbg_ctx,
2747 			 "io_req is NULL for xid=0x%x.\n", xid);
2748 		return;
2749 	}
2750 
2751 	fcport = io_req->fcport;
2752 
2753 	if (fcport == NULL) {
2754 		QEDF_ERR(&qedf->dbg_ctx,
2755 			 "fcport is NULL for xid=0x%x io_req=%p.\n",
2756 			 xid, io_req);
2757 		return;
2758 	}
2759 
2760 	/*
2761 	 * Check that fcport is offloaded.  If it isn't then the spinlock
2762 	 * isn't valid and shouldn't be taken. We should just return.
2763 	 */
2764 	if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
2765 		QEDF_ERR(&qedf->dbg_ctx,
2766 			 "Session not offloaded yet, fcport = %p.\n", fcport);
2767 		return;
2768 	}
2769 
2770 
2771 	switch (comp_type) {
2772 	case FCOE_GOOD_COMPLETION_CQE_TYPE:
2773 		atomic_inc(&fcport->free_sqes);
2774 		switch (io_req->cmd_type) {
2775 		case QEDF_SCSI_CMD:
2776 			qedf_scsi_completion(qedf, cqe, io_req);
2777 			break;
2778 		case QEDF_ELS:
2779 			qedf_process_els_compl(qedf, cqe, io_req);
2780 			break;
2781 		case QEDF_TASK_MGMT_CMD:
2782 			qedf_process_tmf_compl(qedf, cqe, io_req);
2783 			break;
2784 		case QEDF_SEQ_CLEANUP:
2785 			qedf_process_seq_cleanup_compl(qedf, cqe, io_req);
2786 			break;
2787 		}
2788 		break;
2789 	case FCOE_ERROR_DETECTION_CQE_TYPE:
2790 		atomic_inc(&fcport->free_sqes);
2791 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2792 		    "Error detect CQE.\n");
2793 		qedf_process_error_detect(qedf, cqe, io_req);
2794 		break;
2795 	case FCOE_EXCH_CLEANUP_CQE_TYPE:
2796 		atomic_inc(&fcport->free_sqes);
2797 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2798 		    "Cleanup CQE.\n");
2799 		qedf_process_cleanup_compl(qedf, cqe, io_req);
2800 		break;
2801 	case FCOE_ABTS_CQE_TYPE:
2802 		atomic_inc(&fcport->free_sqes);
2803 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2804 		    "Abort CQE.\n");
2805 		qedf_process_abts_compl(qedf, cqe, io_req);
2806 		break;
2807 	case FCOE_DUMMY_CQE_TYPE:
2808 		atomic_inc(&fcport->free_sqes);
2809 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2810 		    "Dummy CQE.\n");
2811 		break;
2812 	case FCOE_LOCAL_COMP_CQE_TYPE:
2813 		atomic_inc(&fcport->free_sqes);
2814 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2815 		    "Local completion CQE.\n");
2816 		break;
2817 	case FCOE_WARNING_CQE_TYPE:
2818 		atomic_inc(&fcport->free_sqes);
2819 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2820 		    "Warning CQE.\n");
2821 		qedf_process_warning_compl(qedf, cqe, io_req);
2822 		break;
2823 	case MAX_FCOE_CQE_TYPE:
2824 		atomic_inc(&fcport->free_sqes);
2825 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2826 		    "Max FCoE CQE.\n");
2827 		break;
2828 	default:
2829 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
2830 		    "Default CQE.\n");
2831 		break;
2832 	}
2833 }
2834 
2835 static void qedf_free_bdq(struct qedf_ctx *qedf)
2836 {
2837 	int i;
2838 
2839 	if (qedf->bdq_pbl_list)
2840 		dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
2841 		    qedf->bdq_pbl_list, qedf->bdq_pbl_list_dma);
2842 
2843 	if (qedf->bdq_pbl)
2844 		dma_free_coherent(&qedf->pdev->dev, qedf->bdq_pbl_mem_size,
2845 		    qedf->bdq_pbl, qedf->bdq_pbl_dma);
2846 
2847 	for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2848 		if (qedf->bdq[i].buf_addr) {
2849 			dma_free_coherent(&qedf->pdev->dev, QEDF_BDQ_BUF_SIZE,
2850 			    qedf->bdq[i].buf_addr, qedf->bdq[i].buf_dma);
2851 		}
2852 	}
2853 }
2854 
2855 static void qedf_free_global_queues(struct qedf_ctx *qedf)
2856 {
2857 	int i;
2858 	struct global_queue **gl = qedf->global_queues;
2859 
2860 	for (i = 0; i < qedf->num_queues; i++) {
2861 		if (!gl[i])
2862 			continue;
2863 
2864 		if (gl[i]->cq)
2865 			dma_free_coherent(&qedf->pdev->dev,
2866 			    gl[i]->cq_mem_size, gl[i]->cq, gl[i]->cq_dma);
2867 		if (gl[i]->cq_pbl)
2868 			dma_free_coherent(&qedf->pdev->dev, gl[i]->cq_pbl_size,
2869 			    gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
2870 
2871 		kfree(gl[i]);
2872 	}
2873 
2874 	qedf_free_bdq(qedf);
2875 }
2876 
2877 static int qedf_alloc_bdq(struct qedf_ctx *qedf)
2878 {
2879 	int i;
2880 	struct scsi_bd *pbl;
2881 	u64 *list;
2882 	dma_addr_t page;
2883 
2884 	/* Alloc dma memory for BDQ buffers */
2885 	for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2886 		qedf->bdq[i].buf_addr = dma_alloc_coherent(&qedf->pdev->dev,
2887 		    QEDF_BDQ_BUF_SIZE, &qedf->bdq[i].buf_dma, GFP_KERNEL);
2888 		if (!qedf->bdq[i].buf_addr) {
2889 			QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ "
2890 			    "buffer %d.\n", i);
2891 			return -ENOMEM;
2892 		}
2893 	}
2894 
2895 	/* Alloc dma memory for BDQ page buffer list */
2896 	qedf->bdq_pbl_mem_size =
2897 	    QEDF_BDQ_SIZE * sizeof(struct scsi_bd);
2898 	qedf->bdq_pbl_mem_size =
2899 	    ALIGN(qedf->bdq_pbl_mem_size, QEDF_PAGE_SIZE);
2900 
2901 	qedf->bdq_pbl = dma_alloc_coherent(&qedf->pdev->dev,
2902 	    qedf->bdq_pbl_mem_size, &qedf->bdq_pbl_dma, GFP_KERNEL);
2903 	if (!qedf->bdq_pbl) {
2904 		QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ PBL.\n");
2905 		return -ENOMEM;
2906 	}
2907 
2908 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2909 		  "BDQ PBL addr=0x%p dma=%pad\n",
2910 		  qedf->bdq_pbl, &qedf->bdq_pbl_dma);
2911 
2912 	/*
2913 	 * Populate BDQ PBL with physical and virtual address of individual
2914 	 * BDQ buffers
2915 	 */
2916 	pbl = (struct scsi_bd *)qedf->bdq_pbl;
2917 	for (i = 0; i < QEDF_BDQ_SIZE; i++) {
2918 		pbl->address.hi = cpu_to_le32(U64_HI(qedf->bdq[i].buf_dma));
2919 		pbl->address.lo = cpu_to_le32(U64_LO(qedf->bdq[i].buf_dma));
2920 		pbl->opaque.fcoe_opaque.hi = 0;
2921 		/* Opaque lo data is an index into the BDQ array */
2922 		pbl->opaque.fcoe_opaque.lo = cpu_to_le32(i);
2923 		pbl++;
2924 	}
2925 
2926 	/* Allocate list of PBL pages */
2927 	qedf->bdq_pbl_list = dma_alloc_coherent(&qedf->pdev->dev,
2928 						QEDF_PAGE_SIZE,
2929 						&qedf->bdq_pbl_list_dma,
2930 						GFP_KERNEL);
2931 	if (!qedf->bdq_pbl_list) {
2932 		QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate list of PBL pages.\n");
2933 		return -ENOMEM;
2934 	}
2935 
2936 	/*
2937 	 * Now populate PBL list with pages that contain pointers to the
2938 	 * individual buffers.
2939 	 */
2940 	qedf->bdq_pbl_list_num_entries = qedf->bdq_pbl_mem_size /
2941 	    QEDF_PAGE_SIZE;
2942 	list = (u64 *)qedf->bdq_pbl_list;
2943 	page = qedf->bdq_pbl_list_dma;
2944 	for (i = 0; i < qedf->bdq_pbl_list_num_entries; i++) {
2945 		*list = qedf->bdq_pbl_dma;
2946 		list++;
2947 		page += QEDF_PAGE_SIZE;
2948 	}
2949 
2950 	return 0;
2951 }
2952 
2953 static int qedf_alloc_global_queues(struct qedf_ctx *qedf)
2954 {
2955 	u32 *list;
2956 	int i;
2957 	int status = 0, rc;
2958 	u32 *pbl;
2959 	dma_addr_t page;
2960 	int num_pages;
2961 
2962 	/* Allocate and map CQs, RQs */
2963 	/*
2964 	 * Number of global queues (CQ / RQ). This should
2965 	 * be <= number of available MSIX vectors for the PF
2966 	 */
2967 	if (!qedf->num_queues) {
2968 		QEDF_ERR(&(qedf->dbg_ctx), "No MSI-X vectors available!\n");
2969 		return 1;
2970 	}
2971 
2972 	/*
2973 	 * Make sure we allocated the PBL that will contain the physical
2974 	 * addresses of our queues
2975 	 */
2976 	if (!qedf->p_cpuq) {
2977 		status = 1;
2978 		QEDF_ERR(&qedf->dbg_ctx, "p_cpuq is NULL.\n");
2979 		goto mem_alloc_failure;
2980 	}
2981 
2982 	qedf->global_queues = kzalloc((sizeof(struct global_queue *)
2983 	    * qedf->num_queues), GFP_KERNEL);
2984 	if (!qedf->global_queues) {
2985 		QEDF_ERR(&(qedf->dbg_ctx), "Unable to allocate global "
2986 			  "queues array ptr memory\n");
2987 		return -ENOMEM;
2988 	}
2989 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
2990 		   "qedf->global_queues=%p.\n", qedf->global_queues);
2991 
2992 	/* Allocate DMA coherent buffers for BDQ */
2993 	rc = qedf_alloc_bdq(qedf);
2994 	if (rc) {
2995 		QEDF_ERR(&qedf->dbg_ctx, "Unable to allocate bdq.\n");
2996 		goto mem_alloc_failure;
2997 	}
2998 
2999 	/* Allocate a CQ and an associated PBL for each MSI-X vector */
3000 	for (i = 0; i < qedf->num_queues; i++) {
3001 		qedf->global_queues[i] = kzalloc(sizeof(struct global_queue),
3002 		    GFP_KERNEL);
3003 		if (!qedf->global_queues[i]) {
3004 			QEDF_WARN(&(qedf->dbg_ctx), "Unable to allocate "
3005 				   "global queue %d.\n", i);
3006 			status = -ENOMEM;
3007 			goto mem_alloc_failure;
3008 		}
3009 
3010 		qedf->global_queues[i]->cq_mem_size =
3011 		    FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
3012 		qedf->global_queues[i]->cq_mem_size =
3013 		    ALIGN(qedf->global_queues[i]->cq_mem_size, QEDF_PAGE_SIZE);
3014 
3015 		qedf->global_queues[i]->cq_pbl_size =
3016 		    (qedf->global_queues[i]->cq_mem_size /
3017 		    PAGE_SIZE) * sizeof(void *);
3018 		qedf->global_queues[i]->cq_pbl_size =
3019 		    ALIGN(qedf->global_queues[i]->cq_pbl_size, QEDF_PAGE_SIZE);
3020 
3021 		qedf->global_queues[i]->cq =
3022 		    dma_alloc_coherent(&qedf->pdev->dev,
3023 				       qedf->global_queues[i]->cq_mem_size,
3024 				       &qedf->global_queues[i]->cq_dma,
3025 				       GFP_KERNEL);
3026 
3027 		if (!qedf->global_queues[i]->cq) {
3028 			QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq.\n");
3029 			status = -ENOMEM;
3030 			goto mem_alloc_failure;
3031 		}
3032 
3033 		qedf->global_queues[i]->cq_pbl =
3034 		    dma_alloc_coherent(&qedf->pdev->dev,
3035 				       qedf->global_queues[i]->cq_pbl_size,
3036 				       &qedf->global_queues[i]->cq_pbl_dma,
3037 				       GFP_KERNEL);
3038 
3039 		if (!qedf->global_queues[i]->cq_pbl) {
3040 			QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq PBL.\n");
3041 			status = -ENOMEM;
3042 			goto mem_alloc_failure;
3043 		}
3044 
3045 		/* Create PBL */
3046 		num_pages = qedf->global_queues[i]->cq_mem_size /
3047 		    QEDF_PAGE_SIZE;
3048 		page = qedf->global_queues[i]->cq_dma;
3049 		pbl = (u32 *)qedf->global_queues[i]->cq_pbl;
3050 
3051 		while (num_pages--) {
3052 			*pbl = U64_LO(page);
3053 			pbl++;
3054 			*pbl = U64_HI(page);
3055 			pbl++;
3056 			page += QEDF_PAGE_SIZE;
3057 		}
3058 		/* Set the initial consumer index for cq */
3059 		qedf->global_queues[i]->cq_cons_idx = 0;
3060 	}
3061 
3062 	list = (u32 *)qedf->p_cpuq;
3063 
3064 	/*
3065 	 * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
3066 	 * CQ#1 PBL pointer, RQ#1 PBL pointer, etc.  Each PBL pointer points
3067 	 * to the physical address which contains an array of pointers to
3068 	 * the physical addresses of the specific queue pages.
3069 	 */
3070 	for (i = 0; i < qedf->num_queues; i++) {
3071 		*list = U64_LO(qedf->global_queues[i]->cq_pbl_dma);
3072 		list++;
3073 		*list = U64_HI(qedf->global_queues[i]->cq_pbl_dma);
3074 		list++;
3075 		*list = U64_LO(0);
3076 		list++;
3077 		*list = U64_HI(0);
3078 		list++;
3079 	}
3080 
3081 	return 0;
3082 
3083 mem_alloc_failure:
3084 	qedf_free_global_queues(qedf);
3085 	return status;
3086 }
3087 
3088 static int qedf_set_fcoe_pf_param(struct qedf_ctx *qedf)
3089 {
3090 	u8 sq_num_pbl_pages;
3091 	u32 sq_mem_size;
3092 	u32 cq_mem_size;
3093 	u32 cq_num_entries;
3094 	int rval;
3095 
3096 	/*
3097 	 * The number of completion queues/fastpath interrupts/status blocks
3098 	 * we allocation is the minimum off:
3099 	 *
3100 	 * Number of CPUs
3101 	 * Number allocated by qed for our PCI function
3102 	 */
3103 	qedf->num_queues = MIN_NUM_CPUS_MSIX(qedf);
3104 
3105 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of CQs is %d.\n",
3106 		   qedf->num_queues);
3107 
3108 	qedf->p_cpuq = dma_alloc_coherent(&qedf->pdev->dev,
3109 	    qedf->num_queues * sizeof(struct qedf_glbl_q_params),
3110 	    &qedf->hw_p_cpuq, GFP_KERNEL);
3111 
3112 	if (!qedf->p_cpuq) {
3113 		QEDF_ERR(&(qedf->dbg_ctx), "dma_alloc_coherent failed.\n");
3114 		return 1;
3115 	}
3116 
3117 	rval = qedf_alloc_global_queues(qedf);
3118 	if (rval) {
3119 		QEDF_ERR(&(qedf->dbg_ctx), "Global queue allocation "
3120 			  "failed.\n");
3121 		return 1;
3122 	}
3123 
3124 	/* Calculate SQ PBL size in the same manner as in qedf_sq_alloc() */
3125 	sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
3126 	sq_mem_size = ALIGN(sq_mem_size, QEDF_PAGE_SIZE);
3127 	sq_num_pbl_pages = (sq_mem_size / QEDF_PAGE_SIZE);
3128 
3129 	/* Calculate CQ num entries */
3130 	cq_mem_size = FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
3131 	cq_mem_size = ALIGN(cq_mem_size, QEDF_PAGE_SIZE);
3132 	cq_num_entries = cq_mem_size / sizeof(struct fcoe_cqe);
3133 
3134 	memset(&(qedf->pf_params), 0, sizeof(qedf->pf_params));
3135 
3136 	/* Setup the value for fcoe PF */
3137 	qedf->pf_params.fcoe_pf_params.num_cons = QEDF_MAX_SESSIONS;
3138 	qedf->pf_params.fcoe_pf_params.num_tasks = FCOE_PARAMS_NUM_TASKS;
3139 	qedf->pf_params.fcoe_pf_params.glbl_q_params_addr =
3140 	    (u64)qedf->hw_p_cpuq;
3141 	qedf->pf_params.fcoe_pf_params.sq_num_pbl_pages = sq_num_pbl_pages;
3142 
3143 	qedf->pf_params.fcoe_pf_params.rq_buffer_log_size = 0;
3144 
3145 	qedf->pf_params.fcoe_pf_params.cq_num_entries = cq_num_entries;
3146 	qedf->pf_params.fcoe_pf_params.num_cqs = qedf->num_queues;
3147 
3148 	/* log_page_size: 12 for 4KB pages */
3149 	qedf->pf_params.fcoe_pf_params.log_page_size = ilog2(QEDF_PAGE_SIZE);
3150 
3151 	qedf->pf_params.fcoe_pf_params.mtu = 9000;
3152 	qedf->pf_params.fcoe_pf_params.gl_rq_pi = QEDF_FCOE_PARAMS_GL_RQ_PI;
3153 	qedf->pf_params.fcoe_pf_params.gl_cmd_pi = QEDF_FCOE_PARAMS_GL_CMD_PI;
3154 
3155 	/* BDQ address and size */
3156 	qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0] =
3157 	    qedf->bdq_pbl_list_dma;
3158 	qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0] =
3159 	    qedf->bdq_pbl_list_num_entries;
3160 	qedf->pf_params.fcoe_pf_params.rq_buffer_size = QEDF_BDQ_BUF_SIZE;
3161 
3162 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3163 	    "bdq_list=%p bdq_pbl_list_dma=%llx bdq_pbl_list_entries=%d.\n",
3164 	    qedf->bdq_pbl_list,
3165 	    qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0],
3166 	    qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0]);
3167 
3168 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3169 	    "cq_num_entries=%d.\n",
3170 	    qedf->pf_params.fcoe_pf_params.cq_num_entries);
3171 
3172 	return 0;
3173 }
3174 
3175 /* Free DMA coherent memory for array of queue pointers we pass to qed */
3176 static void qedf_free_fcoe_pf_param(struct qedf_ctx *qedf)
3177 {
3178 	size_t size = 0;
3179 
3180 	if (qedf->p_cpuq) {
3181 		size = qedf->num_queues * sizeof(struct qedf_glbl_q_params);
3182 		dma_free_coherent(&qedf->pdev->dev, size, qedf->p_cpuq,
3183 		    qedf->hw_p_cpuq);
3184 	}
3185 
3186 	qedf_free_global_queues(qedf);
3187 
3188 	kfree(qedf->global_queues);
3189 }
3190 
3191 /*
3192  * PCI driver functions
3193  */
3194 
3195 static const struct pci_device_id qedf_pci_tbl[] = {
3196 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165c) },
3197 	{ PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8080) },
3198 	{0}
3199 };
3200 MODULE_DEVICE_TABLE(pci, qedf_pci_tbl);
3201 
3202 static struct pci_driver qedf_pci_driver = {
3203 	.name = QEDF_MODULE_NAME,
3204 	.id_table = qedf_pci_tbl,
3205 	.probe = qedf_probe,
3206 	.remove = qedf_remove,
3207 	.shutdown = qedf_shutdown,
3208 };
3209 
3210 static int __qedf_probe(struct pci_dev *pdev, int mode)
3211 {
3212 	int rc = -EINVAL;
3213 	struct fc_lport *lport;
3214 	struct qedf_ctx *qedf = NULL;
3215 	struct Scsi_Host *host;
3216 	bool is_vf = false;
3217 	struct qed_ll2_params params;
3218 	char host_buf[20];
3219 	struct qed_link_params link_params;
3220 	int status;
3221 	void *task_start, *task_end;
3222 	struct qed_slowpath_params slowpath_params;
3223 	struct qed_probe_params qed_params;
3224 
3225 	/*
3226 	 * When doing error recovery we didn't reap the lport so don't try
3227 	 * to reallocate it.
3228 	 */
3229 	if (mode != QEDF_MODE_RECOVERY) {
3230 		lport = libfc_host_alloc(&qedf_host_template,
3231 		    sizeof(struct qedf_ctx));
3232 
3233 		if (!lport) {
3234 			QEDF_ERR(NULL, "Could not allocate lport.\n");
3235 			rc = -ENOMEM;
3236 			goto err0;
3237 		}
3238 
3239 		fc_disc_init(lport);
3240 
3241 		/* Initialize qedf_ctx */
3242 		qedf = lport_priv(lport);
3243 		set_bit(QEDF_PROBING, &qedf->flags);
3244 		qedf->lport = lport;
3245 		qedf->ctlr.lp = lport;
3246 		qedf->pdev = pdev;
3247 		qedf->dbg_ctx.pdev = pdev;
3248 		qedf->dbg_ctx.host_no = lport->host->host_no;
3249 		spin_lock_init(&qedf->hba_lock);
3250 		INIT_LIST_HEAD(&qedf->fcports);
3251 		qedf->curr_conn_id = QEDF_MAX_SESSIONS - 1;
3252 		atomic_set(&qedf->num_offloads, 0);
3253 		qedf->stop_io_on_error = false;
3254 		pci_set_drvdata(pdev, qedf);
3255 		init_completion(&qedf->fipvlan_compl);
3256 		mutex_init(&qedf->stats_mutex);
3257 		mutex_init(&qedf->flush_mutex);
3258 		qedf->flogi_pending = 0;
3259 
3260 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO,
3261 		   "QLogic FastLinQ FCoE Module qedf %s, "
3262 		   "FW %d.%d.%d.%d\n", QEDF_VERSION,
3263 		   FW_MAJOR_VERSION, FW_MINOR_VERSION, FW_REVISION_VERSION,
3264 		   FW_ENGINEERING_VERSION);
3265 	} else {
3266 		/* Init pointers during recovery */
3267 		qedf = pci_get_drvdata(pdev);
3268 		set_bit(QEDF_PROBING, &qedf->flags);
3269 		lport = qedf->lport;
3270 	}
3271 
3272 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC, "Probe started.\n");
3273 
3274 	host = lport->host;
3275 
3276 	/* Allocate mempool for qedf_io_work structs */
3277 	qedf->io_mempool = mempool_create_slab_pool(QEDF_IO_WORK_MIN,
3278 	    qedf_io_work_cache);
3279 	if (qedf->io_mempool == NULL) {
3280 		QEDF_ERR(&(qedf->dbg_ctx), "qedf->io_mempool is NULL.\n");
3281 		goto err1;
3282 	}
3283 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO, "qedf->io_mempool=%p.\n",
3284 	    qedf->io_mempool);
3285 
3286 	sprintf(host_buf, "qedf_%u_link",
3287 	    qedf->lport->host->host_no);
3288 	qedf->link_update_wq = create_workqueue(host_buf);
3289 	INIT_DELAYED_WORK(&qedf->link_update, qedf_handle_link_update);
3290 	INIT_DELAYED_WORK(&qedf->link_recovery, qedf_link_recovery);
3291 	INIT_DELAYED_WORK(&qedf->grcdump_work, qedf_wq_grcdump);
3292 	INIT_DELAYED_WORK(&qedf->stag_work, qedf_stag_change_work);
3293 	qedf->fipvlan_retries = qedf_fipvlan_retries;
3294 	/* Set a default prio in case DCBX doesn't converge */
3295 	if (qedf_default_prio > -1) {
3296 		/*
3297 		 * This is the case where we pass a modparam in so we want to
3298 		 * honor it even if dcbx doesn't converge.
3299 		 */
3300 		qedf->prio = qedf_default_prio;
3301 	} else
3302 		qedf->prio = QEDF_DEFAULT_PRIO;
3303 
3304 	/*
3305 	 * Common probe. Takes care of basic hardware init and pci_*
3306 	 * functions.
3307 	 */
3308 	memset(&qed_params, 0, sizeof(qed_params));
3309 	qed_params.protocol = QED_PROTOCOL_FCOE;
3310 	qed_params.dp_module = qedf_dp_module;
3311 	qed_params.dp_level = qedf_dp_level;
3312 	qed_params.is_vf = is_vf;
3313 	qedf->cdev = qed_ops->common->probe(pdev, &qed_params);
3314 	if (!qedf->cdev) {
3315 		QEDF_ERR(&qedf->dbg_ctx, "common probe failed.\n");
3316 		rc = -ENODEV;
3317 		goto err1;
3318 	}
3319 
3320 	/* Learn information crucial for qedf to progress */
3321 	rc = qed_ops->fill_dev_info(qedf->cdev, &qedf->dev_info);
3322 	if (rc) {
3323 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to dev info.\n");
3324 		goto err1;
3325 	}
3326 
3327 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
3328 		  "dev_info: num_hwfns=%d affin_hwfn_idx=%d.\n",
3329 		  qedf->dev_info.common.num_hwfns,
3330 		  qed_ops->common->get_affin_hwfn_idx(qedf->cdev));
3331 
3332 	/* queue allocation code should come here
3333 	 * order should be
3334 	 * 	slowpath_start
3335 	 * 	status block allocation
3336 	 *	interrupt registration (to get min number of queues)
3337 	 *	set_fcoe_pf_param
3338 	 *	qed_sp_fcoe_func_start
3339 	 */
3340 	rc = qedf_set_fcoe_pf_param(qedf);
3341 	if (rc) {
3342 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot set fcoe pf param.\n");
3343 		goto err2;
3344 	}
3345 	qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
3346 
3347 	/* Learn information crucial for qedf to progress */
3348 	rc = qed_ops->fill_dev_info(qedf->cdev, &qedf->dev_info);
3349 	if (rc) {
3350 		QEDF_ERR(&qedf->dbg_ctx, "Failed to fill dev info.\n");
3351 		goto err2;
3352 	}
3353 
3354 	/* Record BDQ producer doorbell addresses */
3355 	qedf->bdq_primary_prod = qedf->dev_info.primary_dbq_rq_addr;
3356 	qedf->bdq_secondary_prod = qedf->dev_info.secondary_bdq_rq_addr;
3357 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3358 	    "BDQ primary_prod=%p secondary_prod=%p.\n", qedf->bdq_primary_prod,
3359 	    qedf->bdq_secondary_prod);
3360 
3361 	qed_ops->register_ops(qedf->cdev, &qedf_cb_ops, qedf);
3362 
3363 	rc = qedf_prepare_sb(qedf);
3364 	if (rc) {
3365 
3366 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
3367 		goto err2;
3368 	}
3369 
3370 	/* Start the Slowpath-process */
3371 	slowpath_params.int_mode = QED_INT_MODE_MSIX;
3372 	slowpath_params.drv_major = QEDF_DRIVER_MAJOR_VER;
3373 	slowpath_params.drv_minor = QEDF_DRIVER_MINOR_VER;
3374 	slowpath_params.drv_rev = QEDF_DRIVER_REV_VER;
3375 	slowpath_params.drv_eng = QEDF_DRIVER_ENG_VER;
3376 	strncpy(slowpath_params.name, "qedf", QED_DRV_VER_STR_SIZE);
3377 	rc = qed_ops->common->slowpath_start(qedf->cdev, &slowpath_params);
3378 	if (rc) {
3379 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
3380 		goto err2;
3381 	}
3382 
3383 	/*
3384 	 * update_pf_params needs to be called before and after slowpath
3385 	 * start
3386 	 */
3387 	qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
3388 
3389 	/* Setup interrupts */
3390 	rc = qedf_setup_int(qedf);
3391 	if (rc) {
3392 		QEDF_ERR(&qedf->dbg_ctx, "Setup interrupts failed.\n");
3393 		goto err3;
3394 	}
3395 
3396 	rc = qed_ops->start(qedf->cdev, &qedf->tasks);
3397 	if (rc) {
3398 		QEDF_ERR(&(qedf->dbg_ctx), "Cannot start FCoE function.\n");
3399 		goto err4;
3400 	}
3401 	task_start = qedf_get_task_mem(&qedf->tasks, 0);
3402 	task_end = qedf_get_task_mem(&qedf->tasks, MAX_TID_BLOCKS_FCOE - 1);
3403 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Task context start=%p, "
3404 		   "end=%p block_size=%u.\n", task_start, task_end,
3405 		   qedf->tasks.size);
3406 
3407 	/*
3408 	 * We need to write the number of BDs in the BDQ we've preallocated so
3409 	 * the f/w will do a prefetch and we'll get an unsolicited CQE when a
3410 	 * packet arrives.
3411 	 */
3412 	qedf->bdq_prod_idx = QEDF_BDQ_SIZE;
3413 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3414 	    "Writing %d to primary and secondary BDQ doorbell registers.\n",
3415 	    qedf->bdq_prod_idx);
3416 	writew(qedf->bdq_prod_idx, qedf->bdq_primary_prod);
3417 	readw(qedf->bdq_primary_prod);
3418 	writew(qedf->bdq_prod_idx, qedf->bdq_secondary_prod);
3419 	readw(qedf->bdq_secondary_prod);
3420 
3421 	qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
3422 
3423 	/* Now that the dev_info struct has been filled in set the MAC
3424 	 * address
3425 	 */
3426 	ether_addr_copy(qedf->mac, qedf->dev_info.common.hw_mac);
3427 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "MAC address is %pM.\n",
3428 		   qedf->mac);
3429 
3430 	/*
3431 	 * Set the WWNN and WWPN in the following way:
3432 	 *
3433 	 * If the info we get from qed is non-zero then use that to set the
3434 	 * WWPN and WWNN. Otherwise fall back to use fcoe_wwn_from_mac() based
3435 	 * on the MAC address.
3436 	 */
3437 	if (qedf->dev_info.wwnn != 0 && qedf->dev_info.wwpn != 0) {
3438 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3439 		    "Setting WWPN and WWNN from qed dev_info.\n");
3440 		qedf->wwnn = qedf->dev_info.wwnn;
3441 		qedf->wwpn = qedf->dev_info.wwpn;
3442 	} else {
3443 		QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3444 		    "Setting WWPN and WWNN using fcoe_wwn_from_mac().\n");
3445 		qedf->wwnn = fcoe_wwn_from_mac(qedf->mac, 1, 0);
3446 		qedf->wwpn = fcoe_wwn_from_mac(qedf->mac, 2, 0);
3447 	}
3448 	QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,  "WWNN=%016llx "
3449 		   "WWPN=%016llx.\n", qedf->wwnn, qedf->wwpn);
3450 
3451 	sprintf(host_buf, "host_%d", host->host_no);
3452 	qed_ops->common->set_name(qedf->cdev, host_buf);
3453 
3454 	/* Allocate cmd mgr */
3455 	qedf->cmd_mgr = qedf_cmd_mgr_alloc(qedf);
3456 	if (!qedf->cmd_mgr) {
3457 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to allocate cmd mgr.\n");
3458 		rc = -ENOMEM;
3459 		goto err5;
3460 	}
3461 
3462 	if (mode != QEDF_MODE_RECOVERY) {
3463 		host->transportt = qedf_fc_transport_template;
3464 		host->max_lun = qedf_max_lun;
3465 		host->max_cmd_len = QEDF_MAX_CDB_LEN;
3466 		host->can_queue = FCOE_PARAMS_NUM_TASKS;
3467 		rc = scsi_add_host(host, &pdev->dev);
3468 		if (rc) {
3469 			QEDF_WARN(&qedf->dbg_ctx,
3470 				  "Error adding Scsi_Host rc=0x%x.\n", rc);
3471 			goto err6;
3472 		}
3473 	}
3474 
3475 	memset(&params, 0, sizeof(params));
3476 	params.mtu = QEDF_LL2_BUF_SIZE;
3477 	ether_addr_copy(params.ll2_mac_address, qedf->mac);
3478 
3479 	/* Start LL2 processing thread */
3480 	snprintf(host_buf, 20, "qedf_%d_ll2", host->host_no);
3481 	qedf->ll2_recv_wq =
3482 		create_workqueue(host_buf);
3483 	if (!qedf->ll2_recv_wq) {
3484 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to LL2 workqueue.\n");
3485 		rc = -ENOMEM;
3486 		goto err7;
3487 	}
3488 
3489 #ifdef CONFIG_DEBUG_FS
3490 	qedf_dbg_host_init(&(qedf->dbg_ctx), qedf_debugfs_ops,
3491 			    qedf_dbg_fops);
3492 #endif
3493 
3494 	/* Start LL2 */
3495 	qed_ops->ll2->register_cb_ops(qedf->cdev, &qedf_ll2_cb_ops, qedf);
3496 	rc = qed_ops->ll2->start(qedf->cdev, &params);
3497 	if (rc) {
3498 		QEDF_ERR(&(qedf->dbg_ctx), "Could not start Light L2.\n");
3499 		goto err7;
3500 	}
3501 	set_bit(QEDF_LL2_STARTED, &qedf->flags);
3502 
3503 	/* Set initial FIP/FCoE VLAN to NULL */
3504 	qedf->vlan_id = 0;
3505 
3506 	/*
3507 	 * No need to setup fcoe_ctlr or fc_lport objects during recovery since
3508 	 * they were not reaped during the unload process.
3509 	 */
3510 	if (mode != QEDF_MODE_RECOVERY) {
3511 		/* Setup imbedded fcoe controller */
3512 		qedf_fcoe_ctlr_setup(qedf);
3513 
3514 		/* Setup lport */
3515 		rc = qedf_lport_setup(qedf);
3516 		if (rc) {
3517 			QEDF_ERR(&(qedf->dbg_ctx),
3518 			    "qedf_lport_setup failed.\n");
3519 			goto err7;
3520 		}
3521 	}
3522 
3523 	sprintf(host_buf, "qedf_%u_timer", qedf->lport->host->host_no);
3524 	qedf->timer_work_queue =
3525 		create_workqueue(host_buf);
3526 	if (!qedf->timer_work_queue) {
3527 		QEDF_ERR(&(qedf->dbg_ctx), "Failed to start timer "
3528 			  "workqueue.\n");
3529 		rc = -ENOMEM;
3530 		goto err7;
3531 	}
3532 
3533 	/* DPC workqueue is not reaped during recovery unload */
3534 	if (mode != QEDF_MODE_RECOVERY) {
3535 		sprintf(host_buf, "qedf_%u_dpc",
3536 		    qedf->lport->host->host_no);
3537 		qedf->dpc_wq = create_workqueue(host_buf);
3538 	}
3539 	INIT_DELAYED_WORK(&qedf->recovery_work, qedf_recovery_handler);
3540 
3541 	/*
3542 	 * GRC dump and sysfs parameters are not reaped during the recovery
3543 	 * unload process.
3544 	 */
3545 	if (mode != QEDF_MODE_RECOVERY) {
3546 		qedf->grcdump_size =
3547 		    qed_ops->common->dbg_all_data_size(qedf->cdev);
3548 		if (qedf->grcdump_size) {
3549 			rc = qedf_alloc_grc_dump_buf(&qedf->grcdump,
3550 			    qedf->grcdump_size);
3551 			if (rc) {
3552 				QEDF_ERR(&(qedf->dbg_ctx),
3553 				    "GRC Dump buffer alloc failed.\n");
3554 				qedf->grcdump = NULL;
3555 			}
3556 
3557 			QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
3558 			    "grcdump: addr=%p, size=%u.\n",
3559 			    qedf->grcdump, qedf->grcdump_size);
3560 		}
3561 		qedf_create_sysfs_ctx_attr(qedf);
3562 
3563 		/* Initialize I/O tracing for this adapter */
3564 		spin_lock_init(&qedf->io_trace_lock);
3565 		qedf->io_trace_idx = 0;
3566 	}
3567 
3568 	init_completion(&qedf->flogi_compl);
3569 
3570 	status = qed_ops->common->update_drv_state(qedf->cdev, true);
3571 	if (status)
3572 		QEDF_ERR(&(qedf->dbg_ctx),
3573 			"Failed to send drv state to MFW.\n");
3574 
3575 	memset(&link_params, 0, sizeof(struct qed_link_params));
3576 	link_params.link_up = true;
3577 	status = qed_ops->common->set_link(qedf->cdev, &link_params);
3578 	if (status)
3579 		QEDF_WARN(&(qedf->dbg_ctx), "set_link failed.\n");
3580 
3581 	/* Start/restart discovery */
3582 	if (mode == QEDF_MODE_RECOVERY)
3583 		fcoe_ctlr_link_up(&qedf->ctlr);
3584 	else
3585 		fc_fabric_login(lport);
3586 
3587 	QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC, "Probe done.\n");
3588 
3589 	clear_bit(QEDF_PROBING, &qedf->flags);
3590 
3591 	/* All good */
3592 	return 0;
3593 
3594 err7:
3595 	if (qedf->ll2_recv_wq)
3596 		destroy_workqueue(qedf->ll2_recv_wq);
3597 	fc_remove_host(qedf->lport->host);
3598 	scsi_remove_host(qedf->lport->host);
3599 #ifdef CONFIG_DEBUG_FS
3600 	qedf_dbg_host_exit(&(qedf->dbg_ctx));
3601 #endif
3602 err6:
3603 	qedf_cmd_mgr_free(qedf->cmd_mgr);
3604 err5:
3605 	qed_ops->stop(qedf->cdev);
3606 err4:
3607 	qedf_free_fcoe_pf_param(qedf);
3608 	qedf_sync_free_irqs(qedf);
3609 err3:
3610 	qed_ops->common->slowpath_stop(qedf->cdev);
3611 err2:
3612 	qed_ops->common->remove(qedf->cdev);
3613 err1:
3614 	scsi_host_put(lport->host);
3615 err0:
3616 	if (qedf) {
3617 		QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC, "Probe done.\n");
3618 
3619 		clear_bit(QEDF_PROBING, &qedf->flags);
3620 	}
3621 	return rc;
3622 }
3623 
3624 static int qedf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3625 {
3626 	return __qedf_probe(pdev, QEDF_MODE_NORMAL);
3627 }
3628 
3629 static void __qedf_remove(struct pci_dev *pdev, int mode)
3630 {
3631 	struct qedf_ctx *qedf;
3632 	int rc;
3633 
3634 	if (!pdev) {
3635 		QEDF_ERR(NULL, "pdev is NULL.\n");
3636 		return;
3637 	}
3638 
3639 	qedf = pci_get_drvdata(pdev);
3640 
3641 	/*
3642 	 * Prevent race where we're in board disable work and then try to
3643 	 * rmmod the module.
3644 	 */
3645 	if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
3646 		QEDF_ERR(&qedf->dbg_ctx, "Already removing PCI function.\n");
3647 		return;
3648 	}
3649 
3650 	if (mode != QEDF_MODE_RECOVERY)
3651 		set_bit(QEDF_UNLOADING, &qedf->flags);
3652 
3653 	/* Logoff the fabric to upload all connections */
3654 	if (mode == QEDF_MODE_RECOVERY)
3655 		fcoe_ctlr_link_down(&qedf->ctlr);
3656 	else
3657 		fc_fabric_logoff(qedf->lport);
3658 
3659 	if (qedf_wait_for_upload(qedf) == false)
3660 		QEDF_ERR(&qedf->dbg_ctx, "Could not upload all sessions.\n");
3661 
3662 #ifdef CONFIG_DEBUG_FS
3663 	qedf_dbg_host_exit(&(qedf->dbg_ctx));
3664 #endif
3665 
3666 	/* Stop any link update handling */
3667 	cancel_delayed_work_sync(&qedf->link_update);
3668 	destroy_workqueue(qedf->link_update_wq);
3669 	qedf->link_update_wq = NULL;
3670 
3671 	if (qedf->timer_work_queue)
3672 		destroy_workqueue(qedf->timer_work_queue);
3673 
3674 	/* Stop Light L2 */
3675 	clear_bit(QEDF_LL2_STARTED, &qedf->flags);
3676 	qed_ops->ll2->stop(qedf->cdev);
3677 	if (qedf->ll2_recv_wq)
3678 		destroy_workqueue(qedf->ll2_recv_wq);
3679 
3680 	/* Stop fastpath */
3681 	qedf_sync_free_irqs(qedf);
3682 	qedf_destroy_sb(qedf);
3683 
3684 	/*
3685 	 * During recovery don't destroy OS constructs that represent the
3686 	 * physical port.
3687 	 */
3688 	if (mode != QEDF_MODE_RECOVERY) {
3689 		qedf_free_grc_dump_buf(&qedf->grcdump);
3690 		qedf_remove_sysfs_ctx_attr(qedf);
3691 
3692 		/* Remove all SCSI/libfc/libfcoe structures */
3693 		fcoe_ctlr_destroy(&qedf->ctlr);
3694 		fc_lport_destroy(qedf->lport);
3695 		fc_remove_host(qedf->lport->host);
3696 		scsi_remove_host(qedf->lport->host);
3697 	}
3698 
3699 	qedf_cmd_mgr_free(qedf->cmd_mgr);
3700 
3701 	if (mode != QEDF_MODE_RECOVERY) {
3702 		fc_exch_mgr_free(qedf->lport);
3703 		fc_lport_free_stats(qedf->lport);
3704 
3705 		/* Wait for all vports to be reaped */
3706 		qedf_wait_for_vport_destroy(qedf);
3707 	}
3708 
3709 	/*
3710 	 * Now that all connections have been uploaded we can stop the
3711 	 * rest of the qed operations
3712 	 */
3713 	qed_ops->stop(qedf->cdev);
3714 
3715 	if (mode != QEDF_MODE_RECOVERY) {
3716 		if (qedf->dpc_wq) {
3717 			/* Stop general DPC handling */
3718 			destroy_workqueue(qedf->dpc_wq);
3719 			qedf->dpc_wq = NULL;
3720 		}
3721 	}
3722 
3723 	/* Final shutdown for the board */
3724 	qedf_free_fcoe_pf_param(qedf);
3725 	if (mode != QEDF_MODE_RECOVERY) {
3726 		qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
3727 		pci_set_drvdata(pdev, NULL);
3728 	}
3729 
3730 	rc = qed_ops->common->update_drv_state(qedf->cdev, false);
3731 	if (rc)
3732 		QEDF_ERR(&(qedf->dbg_ctx),
3733 			"Failed to send drv state to MFW.\n");
3734 
3735 	qed_ops->common->slowpath_stop(qedf->cdev);
3736 	qed_ops->common->remove(qedf->cdev);
3737 
3738 	mempool_destroy(qedf->io_mempool);
3739 
3740 	/* Only reap the Scsi_host on a real removal */
3741 	if (mode != QEDF_MODE_RECOVERY)
3742 		scsi_host_put(qedf->lport->host);
3743 }
3744 
3745 static void qedf_remove(struct pci_dev *pdev)
3746 {
3747 	/* Check to make sure this function wasn't already disabled */
3748 	if (!atomic_read(&pdev->enable_cnt))
3749 		return;
3750 
3751 	__qedf_remove(pdev, QEDF_MODE_NORMAL);
3752 }
3753 
3754 void qedf_wq_grcdump(struct work_struct *work)
3755 {
3756 	struct qedf_ctx *qedf =
3757 	    container_of(work, struct qedf_ctx, grcdump_work.work);
3758 
3759 	QEDF_ERR(&(qedf->dbg_ctx), "Collecting GRC dump.\n");
3760 	qedf_capture_grc_dump(qedf);
3761 }
3762 
3763 /*
3764  * Protocol TLV handler
3765  */
3766 void qedf_get_protocol_tlv_data(void *dev, void *data)
3767 {
3768 	struct qedf_ctx *qedf = dev;
3769 	struct qed_mfw_tlv_fcoe *fcoe = data;
3770 	struct fc_lport *lport;
3771 	struct Scsi_Host *host;
3772 	struct fc_host_attrs *fc_host;
3773 	struct fc_host_statistics *hst;
3774 
3775 	if (!qedf) {
3776 		QEDF_ERR(NULL, "qedf is null.\n");
3777 		return;
3778 	}
3779 
3780 	if (test_bit(QEDF_PROBING, &qedf->flags)) {
3781 		QEDF_ERR(&qedf->dbg_ctx, "Function is still probing.\n");
3782 		return;
3783 	}
3784 
3785 	lport = qedf->lport;
3786 	host = lport->host;
3787 	fc_host = shost_to_fc_host(host);
3788 
3789 	/* Force a refresh of the fc_host stats including offload stats */
3790 	hst = qedf_fc_get_host_stats(host);
3791 
3792 	fcoe->qos_pri_set = true;
3793 	fcoe->qos_pri = 3; /* Hard coded to 3 in driver */
3794 
3795 	fcoe->ra_tov_set = true;
3796 	fcoe->ra_tov = lport->r_a_tov;
3797 
3798 	fcoe->ed_tov_set = true;
3799 	fcoe->ed_tov = lport->e_d_tov;
3800 
3801 	fcoe->npiv_state_set = true;
3802 	fcoe->npiv_state = 1; /* NPIV always enabled */
3803 
3804 	fcoe->num_npiv_ids_set = true;
3805 	fcoe->num_npiv_ids = fc_host->npiv_vports_inuse;
3806 
3807 	/* Certain attributes we only want to set if we've selected an FCF */
3808 	if (qedf->ctlr.sel_fcf) {
3809 		fcoe->switch_name_set = true;
3810 		u64_to_wwn(qedf->ctlr.sel_fcf->switch_name, fcoe->switch_name);
3811 	}
3812 
3813 	fcoe->port_state_set = true;
3814 	/* For qedf we're either link down or fabric attach */
3815 	if (lport->link_up)
3816 		fcoe->port_state = QED_MFW_TLV_PORT_STATE_FABRIC;
3817 	else
3818 		fcoe->port_state = QED_MFW_TLV_PORT_STATE_OFFLINE;
3819 
3820 	fcoe->link_failures_set = true;
3821 	fcoe->link_failures = (u16)hst->link_failure_count;
3822 
3823 	fcoe->fcoe_txq_depth_set = true;
3824 	fcoe->fcoe_rxq_depth_set = true;
3825 	fcoe->fcoe_rxq_depth = FCOE_PARAMS_NUM_TASKS;
3826 	fcoe->fcoe_txq_depth = FCOE_PARAMS_NUM_TASKS;
3827 
3828 	fcoe->fcoe_rx_frames_set = true;
3829 	fcoe->fcoe_rx_frames = hst->rx_frames;
3830 
3831 	fcoe->fcoe_tx_frames_set = true;
3832 	fcoe->fcoe_tx_frames = hst->tx_frames;
3833 
3834 	fcoe->fcoe_rx_bytes_set = true;
3835 	fcoe->fcoe_rx_bytes = hst->fcp_input_megabytes * 1000000;
3836 
3837 	fcoe->fcoe_tx_bytes_set = true;
3838 	fcoe->fcoe_tx_bytes = hst->fcp_output_megabytes * 1000000;
3839 
3840 	fcoe->crc_count_set = true;
3841 	fcoe->crc_count = hst->invalid_crc_count;
3842 
3843 	fcoe->tx_abts_set = true;
3844 	fcoe->tx_abts = hst->fcp_packet_aborts;
3845 
3846 	fcoe->tx_lun_rst_set = true;
3847 	fcoe->tx_lun_rst = qedf->lun_resets;
3848 
3849 	fcoe->abort_task_sets_set = true;
3850 	fcoe->abort_task_sets = qedf->packet_aborts;
3851 
3852 	fcoe->scsi_busy_set = true;
3853 	fcoe->scsi_busy = qedf->busy;
3854 
3855 	fcoe->scsi_tsk_full_set = true;
3856 	fcoe->scsi_tsk_full = qedf->task_set_fulls;
3857 }
3858 
3859 /* Deferred work function to perform soft context reset on STAG change */
3860 void qedf_stag_change_work(struct work_struct *work)
3861 {
3862 	struct qedf_ctx *qedf =
3863 	    container_of(work, struct qedf_ctx, stag_work.work);
3864 
3865 	if (!qedf) {
3866 		QEDF_ERR(NULL, "qedf is NULL");
3867 		return;
3868 	}
3869 	QEDF_ERR(&qedf->dbg_ctx, "Performing software context reset.\n");
3870 	qedf_ctx_soft_reset(qedf->lport);
3871 }
3872 
3873 static void qedf_shutdown(struct pci_dev *pdev)
3874 {
3875 	__qedf_remove(pdev, QEDF_MODE_NORMAL);
3876 }
3877 
3878 /*
3879  * Recovery handler code
3880  */
3881 static void qedf_schedule_recovery_handler(void *dev)
3882 {
3883 	struct qedf_ctx *qedf = dev;
3884 
3885 	QEDF_ERR(&qedf->dbg_ctx, "Recovery handler scheduled.\n");
3886 	schedule_delayed_work(&qedf->recovery_work, 0);
3887 }
3888 
3889 static void qedf_recovery_handler(struct work_struct *work)
3890 {
3891 	struct qedf_ctx *qedf =
3892 	    container_of(work, struct qedf_ctx, recovery_work.work);
3893 
3894 	if (test_and_set_bit(QEDF_IN_RECOVERY, &qedf->flags))
3895 		return;
3896 
3897 	/*
3898 	 * Call common_ops->recovery_prolog to allow the MFW to quiesce
3899 	 * any PCI transactions.
3900 	 */
3901 	qed_ops->common->recovery_prolog(qedf->cdev);
3902 
3903 	QEDF_ERR(&qedf->dbg_ctx, "Recovery work start.\n");
3904 	__qedf_remove(qedf->pdev, QEDF_MODE_RECOVERY);
3905 	/*
3906 	 * Reset link and dcbx to down state since we will not get a link down
3907 	 * event from the MFW but calling __qedf_remove will essentially be a
3908 	 * link down event.
3909 	 */
3910 	atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
3911 	atomic_set(&qedf->dcbx, QEDF_DCBX_PENDING);
3912 	__qedf_probe(qedf->pdev, QEDF_MODE_RECOVERY);
3913 	clear_bit(QEDF_IN_RECOVERY, &qedf->flags);
3914 	QEDF_ERR(&qedf->dbg_ctx, "Recovery work complete.\n");
3915 }
3916 
3917 /* Generic TLV data callback */
3918 void qedf_get_generic_tlv_data(void *dev, struct qed_generic_tlvs *data)
3919 {
3920 	struct qedf_ctx *qedf;
3921 
3922 	if (!dev) {
3923 		QEDF_INFO(NULL, QEDF_LOG_EVT,
3924 			  "dev is NULL so ignoring get_generic_tlv_data request.\n");
3925 		return;
3926 	}
3927 	qedf = (struct qedf_ctx *)dev;
3928 
3929 	memset(data, 0, sizeof(struct qed_generic_tlvs));
3930 	ether_addr_copy(data->mac[0], qedf->mac);
3931 }
3932 
3933 /*
3934  * Module Init/Remove
3935  */
3936 
3937 static int __init qedf_init(void)
3938 {
3939 	int ret;
3940 
3941 	/* If debug=1 passed, set the default log mask */
3942 	if (qedf_debug == QEDF_LOG_DEFAULT)
3943 		qedf_debug = QEDF_DEFAULT_LOG_MASK;
3944 
3945 	/*
3946 	 * Check that default prio for FIP/FCoE traffic is between 0..7 if a
3947 	 * value has been set
3948 	 */
3949 	if (qedf_default_prio > -1)
3950 		if (qedf_default_prio > 7) {
3951 			qedf_default_prio = QEDF_DEFAULT_PRIO;
3952 			QEDF_ERR(NULL, "FCoE/FIP priority out of range, resetting to %d.\n",
3953 			    QEDF_DEFAULT_PRIO);
3954 		}
3955 
3956 	/* Print driver banner */
3957 	QEDF_INFO(NULL, QEDF_LOG_INFO, "%s v%s.\n", QEDF_DESCR,
3958 		   QEDF_VERSION);
3959 
3960 	/* Create kmem_cache for qedf_io_work structs */
3961 	qedf_io_work_cache = kmem_cache_create("qedf_io_work_cache",
3962 	    sizeof(struct qedf_io_work), 0, SLAB_HWCACHE_ALIGN, NULL);
3963 	if (qedf_io_work_cache == NULL) {
3964 		QEDF_ERR(NULL, "qedf_io_work_cache is NULL.\n");
3965 		goto err1;
3966 	}
3967 	QEDF_INFO(NULL, QEDF_LOG_DISC, "qedf_io_work_cache=%p.\n",
3968 	    qedf_io_work_cache);
3969 
3970 	qed_ops = qed_get_fcoe_ops();
3971 	if (!qed_ops) {
3972 		QEDF_ERR(NULL, "Failed to get qed fcoe operations\n");
3973 		goto err1;
3974 	}
3975 
3976 #ifdef CONFIG_DEBUG_FS
3977 	qedf_dbg_init("qedf");
3978 #endif
3979 
3980 	qedf_fc_transport_template =
3981 	    fc_attach_transport(&qedf_fc_transport_fn);
3982 	if (!qedf_fc_transport_template) {
3983 		QEDF_ERR(NULL, "Could not register with FC transport\n");
3984 		goto err2;
3985 	}
3986 
3987 	qedf_fc_vport_transport_template =
3988 		fc_attach_transport(&qedf_fc_vport_transport_fn);
3989 	if (!qedf_fc_vport_transport_template) {
3990 		QEDF_ERR(NULL, "Could not register vport template with FC "
3991 			  "transport\n");
3992 		goto err3;
3993 	}
3994 
3995 	qedf_io_wq = create_workqueue("qedf_io_wq");
3996 	if (!qedf_io_wq) {
3997 		QEDF_ERR(NULL, "Could not create qedf_io_wq.\n");
3998 		goto err4;
3999 	}
4000 
4001 	qedf_cb_ops.get_login_failures = qedf_get_login_failures;
4002 
4003 	ret = pci_register_driver(&qedf_pci_driver);
4004 	if (ret) {
4005 		QEDF_ERR(NULL, "Failed to register driver\n");
4006 		goto err5;
4007 	}
4008 
4009 	return 0;
4010 
4011 err5:
4012 	destroy_workqueue(qedf_io_wq);
4013 err4:
4014 	fc_release_transport(qedf_fc_vport_transport_template);
4015 err3:
4016 	fc_release_transport(qedf_fc_transport_template);
4017 err2:
4018 #ifdef CONFIG_DEBUG_FS
4019 	qedf_dbg_exit();
4020 #endif
4021 	qed_put_fcoe_ops();
4022 err1:
4023 	return -EINVAL;
4024 }
4025 
4026 static void __exit qedf_cleanup(void)
4027 {
4028 	pci_unregister_driver(&qedf_pci_driver);
4029 
4030 	destroy_workqueue(qedf_io_wq);
4031 
4032 	fc_release_transport(qedf_fc_vport_transport_template);
4033 	fc_release_transport(qedf_fc_transport_template);
4034 #ifdef CONFIG_DEBUG_FS
4035 	qedf_dbg_exit();
4036 #endif
4037 	qed_put_fcoe_ops();
4038 
4039 	kmem_cache_destroy(qedf_io_work_cache);
4040 }
4041 
4042 MODULE_LICENSE("GPL");
4043 MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx FCoE Module");
4044 MODULE_AUTHOR("QLogic Corporation");
4045 MODULE_VERSION(QEDF_VERSION);
4046 module_init(qedf_init);
4047 module_exit(qedf_cleanup);
4048