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