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