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