xref: /linux/drivers/scsi/lpfc/lpfc_hbadisc.c (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2024 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 
24 #include <linux/blkdev.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/pci.h>
28 #include <linux/kthread.h>
29 #include <linux/interrupt.h>
30 #include <linux/lockdep.h>
31 #include <linux/utsname.h>
32 
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <scsi/fc/fc_fs.h>
38 
39 #include "lpfc_hw4.h"
40 #include "lpfc_hw.h"
41 #include "lpfc_nl.h"
42 #include "lpfc_disc.h"
43 #include "lpfc_sli.h"
44 #include "lpfc_sli4.h"
45 #include "lpfc.h"
46 #include "lpfc_scsi.h"
47 #include "lpfc_nvme.h"
48 #include "lpfc_logmsg.h"
49 #include "lpfc_crtn.h"
50 #include "lpfc_vport.h"
51 #include "lpfc_debugfs.h"
52 
53 /* AlpaArray for assignment of scsid for scan-down and bind_method */
54 static uint8_t lpfcAlpaArray[] = {
55 	0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
56 	0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
57 	0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
58 	0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
59 	0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
60 	0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
61 	0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
62 	0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
63 	0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
64 	0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
65 	0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
66 	0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
67 	0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
68 };
69 
70 static void lpfc_disc_timeout_handler(struct lpfc_vport *);
71 static void lpfc_disc_flush_list(struct lpfc_vport *vport);
72 static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
73 static int lpfc_fcf_inuse(struct lpfc_hba *);
74 static void lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *, LPFC_MBOXQ_t *);
75 static void lpfc_check_inactive_vmid(struct lpfc_hba *phba);
76 static void lpfc_check_vmid_qfpa_issue(struct lpfc_hba *phba);
77 
78 static int
79 lpfc_valid_xpt_node(struct lpfc_nodelist *ndlp)
80 {
81 	if (ndlp->nlp_fc4_type ||
82 	    ndlp->nlp_type & NLP_FABRIC)
83 		return 1;
84 	return 0;
85 }
86 /* The source of a terminate rport I/O is either a dev_loss_tmo
87  * event or a call to fc_remove_host.  While the rport should be
88  * valid during these downcalls, the transport can call twice
89  * in a single event.  This routine provides somoe protection
90  * as the NDLP isn't really free, just released to the pool.
91  */
92 static int
93 lpfc_rport_invalid(struct fc_rport *rport)
94 {
95 	struct lpfc_rport_data *rdata;
96 	struct lpfc_nodelist *ndlp;
97 
98 	if (!rport) {
99 		pr_err("**** %s: NULL rport, exit.\n", __func__);
100 		return -EINVAL;
101 	}
102 
103 	rdata = rport->dd_data;
104 	if (!rdata) {
105 		pr_err("**** %s: NULL dd_data on rport x%px SID x%x\n",
106 		       __func__, rport, rport->scsi_target_id);
107 		return -EINVAL;
108 	}
109 
110 	ndlp = rdata->pnode;
111 	if (!rdata->pnode) {
112 		pr_info("**** %s: NULL ndlp on rport x%px SID x%x\n",
113 			__func__, rport, rport->scsi_target_id);
114 		return -EINVAL;
115 	}
116 
117 	if (!ndlp->vport) {
118 		pr_err("**** %s: Null vport on ndlp x%px, DID x%x rport x%px "
119 		       "SID x%x\n", __func__, ndlp, ndlp->nlp_DID, rport,
120 		       rport->scsi_target_id);
121 		return -EINVAL;
122 	}
123 	return 0;
124 }
125 
126 void
127 lpfc_terminate_rport_io(struct fc_rport *rport)
128 {
129 	struct lpfc_rport_data *rdata;
130 	struct lpfc_nodelist *ndlp;
131 	struct lpfc_vport *vport;
132 
133 	if (lpfc_rport_invalid(rport))
134 		return;
135 
136 	rdata = rport->dd_data;
137 	ndlp = rdata->pnode;
138 	vport = ndlp->vport;
139 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
140 			      "rport terminate: sid:x%x did:x%x flg:x%x",
141 			      ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
142 
143 	if (ndlp->nlp_sid != NLP_NO_SID)
144 		lpfc_sli_abort_iocb(vport, ndlp->nlp_sid, 0, LPFC_CTX_TGT);
145 }
146 
147 /*
148  * This function will be called when dev_loss_tmo fire.
149  */
150 void
151 lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
152 {
153 	struct lpfc_nodelist *ndlp;
154 	struct lpfc_vport *vport;
155 	struct lpfc_hba   *phba;
156 	struct lpfc_work_evt *evtp;
157 	unsigned long iflags;
158 
159 	ndlp = ((struct lpfc_rport_data *)rport->dd_data)->pnode;
160 	if (!ndlp)
161 		return;
162 
163 	vport = ndlp->vport;
164 	phba  = vport->phba;
165 
166 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
167 		"rport devlosscb: sid:x%x did:x%x flg:x%x",
168 		ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
169 
170 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
171 			 "3181 dev_loss_callbk x%06x, rport x%px flg x%x "
172 			 "load_flag x%lx refcnt %u state %d xpt x%x\n",
173 			 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag,
174 			 vport->load_flag, kref_read(&ndlp->kref),
175 			 ndlp->nlp_state, ndlp->fc4_xpt_flags);
176 
177 	/* Don't schedule a worker thread event if the vport is going down. */
178 	if (test_bit(FC_UNLOADING, &vport->load_flag)) {
179 		spin_lock_irqsave(&ndlp->lock, iflags);
180 		ndlp->rport = NULL;
181 
182 		/* The scsi_transport is done with the rport so lpfc cannot
183 		 * call to unregister. Remove the scsi transport reference
184 		 * and clean up the SCSI transport node details.
185 		 */
186 		if (ndlp->fc4_xpt_flags & (NLP_XPT_REGD | SCSI_XPT_REGD)) {
187 			ndlp->fc4_xpt_flags &= ~SCSI_XPT_REGD;
188 
189 			/* NVME transport-registered rports need the
190 			 * NLP_XPT_REGD flag to complete an unregister.
191 			 */
192 			if (!(ndlp->fc4_xpt_flags & NVME_XPT_REGD))
193 				ndlp->fc4_xpt_flags &= ~NLP_XPT_REGD;
194 			spin_unlock_irqrestore(&ndlp->lock, iflags);
195 			lpfc_nlp_put(ndlp);
196 			spin_lock_irqsave(&ndlp->lock, iflags);
197 		}
198 
199 		/* Only 1 thread can drop the initial node reference.  If
200 		 * another thread has set NLP_DROPPED, this thread is done.
201 		 */
202 		if (!(ndlp->fc4_xpt_flags & NVME_XPT_REGD) &&
203 		    !(ndlp->nlp_flag & NLP_DROPPED)) {
204 			ndlp->nlp_flag |= NLP_DROPPED;
205 			spin_unlock_irqrestore(&ndlp->lock, iflags);
206 			lpfc_nlp_put(ndlp);
207 			return;
208 		}
209 
210 		spin_unlock_irqrestore(&ndlp->lock, iflags);
211 		return;
212 	}
213 
214 	if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
215 		return;
216 
217 	if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn))
218 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
219 				 "6789 rport name %llx != node port name %llx",
220 				 rport->port_name,
221 				 wwn_to_u64(ndlp->nlp_portname.u.wwn));
222 
223 	evtp = &ndlp->dev_loss_evt;
224 
225 	if (!list_empty(&evtp->evt_listp)) {
226 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
227 				 "6790 rport name %llx dev_loss_evt pending\n",
228 				 rport->port_name);
229 		return;
230 	}
231 
232 	spin_lock_irqsave(&ndlp->lock, iflags);
233 	ndlp->nlp_flag |= NLP_IN_DEV_LOSS;
234 
235 	/* If there is a PLOGI in progress, and we are in a
236 	 * NLP_NPR_2B_DISC state, don't turn off the flag.
237 	 */
238 	if (ndlp->nlp_state != NLP_STE_PLOGI_ISSUE)
239 		ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
240 
241 	/*
242 	 * The backend does not expect any more calls associated with this
243 	 * rport. Remove the association between rport and ndlp.
244 	 */
245 	ndlp->fc4_xpt_flags &= ~SCSI_XPT_REGD;
246 	((struct lpfc_rport_data *)rport->dd_data)->pnode = NULL;
247 	ndlp->rport = NULL;
248 	spin_unlock_irqrestore(&ndlp->lock, iflags);
249 
250 	if (phba->worker_thread) {
251 		/* We need to hold the node by incrementing the reference
252 		 * count until this queued work is done
253 		 */
254 		evtp->evt_arg1 = lpfc_nlp_get(ndlp);
255 
256 		spin_lock_irqsave(&phba->hbalock, iflags);
257 		if (evtp->evt_arg1) {
258 			evtp->evt = LPFC_EVT_DEV_LOSS;
259 			list_add_tail(&evtp->evt_listp, &phba->work_list);
260 			lpfc_worker_wake_up(phba);
261 		}
262 		spin_unlock_irqrestore(&phba->hbalock, iflags);
263 	} else {
264 		lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
265 				 "3188 worker thread is stopped %s x%06x, "
266 				 " rport x%px flg x%x load_flag x%lx refcnt "
267 				 "%d\n", __func__, ndlp->nlp_DID,
268 				 ndlp->rport, ndlp->nlp_flag,
269 				 vport->load_flag, kref_read(&ndlp->kref));
270 		if (!(ndlp->fc4_xpt_flags & NVME_XPT_REGD)) {
271 			spin_lock_irqsave(&ndlp->lock, iflags);
272 			/* Node is in dev loss.  No further transaction. */
273 			ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
274 			spin_unlock_irqrestore(&ndlp->lock, iflags);
275 			lpfc_disc_state_machine(vport, ndlp, NULL,
276 						NLP_EVT_DEVICE_RM);
277 		}
278 
279 	}
280 
281 	return;
282 }
283 
284 /**
285  * lpfc_check_inactive_vmid_one - VMID inactivity checker for a vport
286  * @vport: Pointer to vport context object.
287  *
288  * This function checks for idle VMID entries related to a particular vport. If
289  * found unused/idle, free them accordingly.
290  **/
291 static void lpfc_check_inactive_vmid_one(struct lpfc_vport *vport)
292 {
293 	u16 keep;
294 	u32 difftime = 0, r, bucket;
295 	u64 *lta;
296 	int cpu;
297 	struct lpfc_vmid *vmp;
298 
299 	write_lock(&vport->vmid_lock);
300 
301 	if (!vport->cur_vmid_cnt)
302 		goto out;
303 
304 	/* iterate through the table */
305 	hash_for_each(vport->hash_table, bucket, vmp, hnode) {
306 		keep = 0;
307 		if (vmp->flag & LPFC_VMID_REGISTERED) {
308 			/* check if the particular VMID is in use */
309 			/* for all available per cpu variable */
310 			for_each_possible_cpu(cpu) {
311 				/* if last access time is less than timeout */
312 				lta = per_cpu_ptr(vmp->last_io_time, cpu);
313 				if (!lta)
314 					continue;
315 				difftime = (jiffies) - (*lta);
316 				if ((vport->vmid_inactivity_timeout *
317 				     JIFFIES_PER_HR) > difftime) {
318 					keep = 1;
319 					break;
320 				}
321 			}
322 
323 			/* if none of the cpus have been used by the vm, */
324 			/*  remove the entry if already registered */
325 			if (!keep) {
326 				/* mark the entry for deregistration */
327 				vmp->flag = LPFC_VMID_DE_REGISTER;
328 				write_unlock(&vport->vmid_lock);
329 				if (vport->vmid_priority_tagging)
330 					r = lpfc_vmid_uvem(vport, vmp, false);
331 				else
332 					r = lpfc_vmid_cmd(vport,
333 							  SLI_CTAS_DAPP_IDENT,
334 							  vmp);
335 
336 				/* decrement number of active vms and mark */
337 				/* entry in slot as free */
338 				write_lock(&vport->vmid_lock);
339 				if (!r) {
340 					struct lpfc_vmid *ht = vmp;
341 
342 					vport->cur_vmid_cnt--;
343 					ht->flag = LPFC_VMID_SLOT_FREE;
344 					free_percpu(ht->last_io_time);
345 					ht->last_io_time = NULL;
346 					hash_del(&ht->hnode);
347 				}
348 			}
349 		}
350 	}
351  out:
352 	write_unlock(&vport->vmid_lock);
353 }
354 
355 /**
356  * lpfc_check_inactive_vmid - VMID inactivity checker
357  * @phba: Pointer to hba context object.
358  *
359  * This function is called from the worker thread to determine if an entry in
360  * the VMID table can be released since there was no I/O activity seen from that
361  * particular VM for the specified time. When this happens, the entry in the
362  * table is released and also the resources on the switch cleared.
363  **/
364 
365 static void lpfc_check_inactive_vmid(struct lpfc_hba *phba)
366 {
367 	struct lpfc_vport *vport;
368 	struct lpfc_vport **vports;
369 	int i;
370 
371 	vports = lpfc_create_vport_work_array(phba);
372 	if (!vports)
373 		return;
374 
375 	for (i = 0; i <= phba->max_vports; i++) {
376 		if ((!vports[i]) && (i == 0))
377 			vport = phba->pport;
378 		else
379 			vport = vports[i];
380 		if (!vport)
381 			break;
382 
383 		lpfc_check_inactive_vmid_one(vport);
384 	}
385 	lpfc_destroy_vport_work_array(phba, vports);
386 }
387 
388 /**
389  * lpfc_check_nlp_post_devloss - Check to restore ndlp refcnt after devloss
390  * @vport: Pointer to vport object.
391  * @ndlp: Pointer to remote node object.
392  *
393  * If NLP_IN_RECOV_POST_DEV_LOSS flag was set due to outstanding recovery of
394  * node during dev_loss_tmo processing, then this function restores the nlp_put
395  * kref decrement from lpfc_dev_loss_tmo_handler.
396  **/
397 void
398 lpfc_check_nlp_post_devloss(struct lpfc_vport *vport,
399 			    struct lpfc_nodelist *ndlp)
400 {
401 	unsigned long iflags;
402 
403 	spin_lock_irqsave(&ndlp->lock, iflags);
404 	if (ndlp->save_flags & NLP_IN_RECOV_POST_DEV_LOSS) {
405 		ndlp->save_flags &= ~NLP_IN_RECOV_POST_DEV_LOSS;
406 		spin_unlock_irqrestore(&ndlp->lock, iflags);
407 		lpfc_nlp_get(ndlp);
408 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY | LOG_NODE,
409 				 "8438 Devloss timeout reversed on DID x%x "
410 				 "refcnt %d ndlp %p flag x%x "
411 				 "port_state = x%x\n",
412 				 ndlp->nlp_DID, kref_read(&ndlp->kref), ndlp,
413 				 ndlp->nlp_flag, vport->port_state);
414 		return;
415 	}
416 	spin_unlock_irqrestore(&ndlp->lock, iflags);
417 }
418 
419 /**
420  * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
421  * @ndlp: Pointer to remote node object.
422  *
423  * This function is called from the worker thread when devloss timeout timer
424  * expires. For SLI4 host, this routine shall return 1 when at lease one
425  * remote node, including this @ndlp, is still in use of FCF; otherwise, this
426  * routine shall return 0 when there is no remote node is still in use of FCF
427  * when devloss timeout happened to this @ndlp.
428  **/
429 static int
430 lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
431 {
432 	struct lpfc_vport *vport;
433 	struct lpfc_hba   *phba;
434 	uint8_t *name;
435 	int warn_on = 0;
436 	int fcf_inuse = 0;
437 	bool recovering = false;
438 	struct fc_vport *fc_vport = NULL;
439 	unsigned long iflags;
440 
441 	vport = ndlp->vport;
442 	name = (uint8_t *)&ndlp->nlp_portname;
443 	phba = vport->phba;
444 
445 	if (phba->sli_rev == LPFC_SLI_REV4)
446 		fcf_inuse = lpfc_fcf_inuse(phba);
447 
448 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
449 			      "rport devlosstmo:did:x%x type:x%x id:x%x",
450 			      ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_sid);
451 
452 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
453 			 "3182 %s x%06x, nflag x%x xflags x%x refcnt %d\n",
454 			 __func__, ndlp->nlp_DID, ndlp->nlp_flag,
455 			 ndlp->fc4_xpt_flags, kref_read(&ndlp->kref));
456 
457 	/* If the driver is recovering the rport, ignore devloss. */
458 	if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
459 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
460 				 "0284 Devloss timeout Ignored on "
461 				 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
462 				 "NPort x%x\n",
463 				 *name, *(name+1), *(name+2), *(name+3),
464 				 *(name+4), *(name+5), *(name+6), *(name+7),
465 				 ndlp->nlp_DID);
466 
467 		spin_lock_irqsave(&ndlp->lock, iflags);
468 		ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
469 		spin_unlock_irqrestore(&ndlp->lock, iflags);
470 		return fcf_inuse;
471 	}
472 
473 	/* Fabric nodes are done. */
474 	if (ndlp->nlp_type & NLP_FABRIC) {
475 		spin_lock_irqsave(&ndlp->lock, iflags);
476 
477 		/* The driver has to account for a race between any fabric
478 		 * node that's in recovery when dev_loss_tmo expires. When this
479 		 * happens, the driver has to allow node recovery.
480 		 */
481 		switch (ndlp->nlp_DID) {
482 		case Fabric_DID:
483 			fc_vport = vport->fc_vport;
484 			if (fc_vport) {
485 				/* NPIV path. */
486 				if (fc_vport->vport_state ==
487 				    FC_VPORT_INITIALIZING)
488 					recovering = true;
489 			} else {
490 				/* Physical port path. */
491 				if (phba->hba_flag & HBA_FLOGI_OUTSTANDING)
492 					recovering = true;
493 			}
494 			break;
495 		case Fabric_Cntl_DID:
496 			if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
497 				recovering = true;
498 			break;
499 		case FDMI_DID:
500 			fallthrough;
501 		case NameServer_DID:
502 			if (ndlp->nlp_state >= NLP_STE_PLOGI_ISSUE &&
503 			    ndlp->nlp_state <= NLP_STE_REG_LOGIN_ISSUE)
504 				recovering = true;
505 			break;
506 		default:
507 			/* Ensure the nlp_DID at least has the correct prefix.
508 			 * The fabric domain controller's last three nibbles
509 			 * vary so we handle it in the default case.
510 			 */
511 			if (ndlp->nlp_DID & Fabric_DID_MASK) {
512 				if (ndlp->nlp_state >= NLP_STE_PLOGI_ISSUE &&
513 				    ndlp->nlp_state <= NLP_STE_REG_LOGIN_ISSUE)
514 					recovering = true;
515 			}
516 			break;
517 		}
518 		spin_unlock_irqrestore(&ndlp->lock, iflags);
519 
520 		/* Mark an NLP_IN_RECOV_POST_DEV_LOSS flag to know if reversing
521 		 * the following lpfc_nlp_put is necessary after fabric node is
522 		 * recovered.
523 		 */
524 		if (recovering) {
525 			lpfc_printf_vlog(vport, KERN_INFO,
526 					 LOG_DISCOVERY | LOG_NODE,
527 					 "8436 Devloss timeout marked on "
528 					 "DID x%x refcnt %d ndlp %p "
529 					 "flag x%x port_state = x%x\n",
530 					 ndlp->nlp_DID, kref_read(&ndlp->kref),
531 					 ndlp, ndlp->nlp_flag,
532 					 vport->port_state);
533 			spin_lock_irqsave(&ndlp->lock, iflags);
534 			ndlp->save_flags |= NLP_IN_RECOV_POST_DEV_LOSS;
535 			spin_unlock_irqrestore(&ndlp->lock, iflags);
536 		} else if (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
537 			/* Fabric node fully recovered before this dev_loss_tmo
538 			 * queue work is processed.  Thus, ignore the
539 			 * dev_loss_tmo event.
540 			 */
541 			lpfc_printf_vlog(vport, KERN_INFO,
542 					 LOG_DISCOVERY | LOG_NODE,
543 					 "8437 Devloss timeout ignored on "
544 					 "DID x%x refcnt %d ndlp %p "
545 					 "flag x%x port_state = x%x\n",
546 					 ndlp->nlp_DID, kref_read(&ndlp->kref),
547 					 ndlp, ndlp->nlp_flag,
548 					 vport->port_state);
549 			return fcf_inuse;
550 		}
551 
552 		spin_lock_irqsave(&ndlp->lock, iflags);
553 		ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
554 		spin_unlock_irqrestore(&ndlp->lock, iflags);
555 		lpfc_nlp_put(ndlp);
556 		return fcf_inuse;
557 	}
558 
559 	if (ndlp->nlp_sid != NLP_NO_SID) {
560 		warn_on = 1;
561 		lpfc_sli_abort_iocb(vport, ndlp->nlp_sid, 0, LPFC_CTX_TGT);
562 	}
563 
564 	if (warn_on) {
565 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
566 				 "0203 Devloss timeout on "
567 				 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
568 				 "NPort x%06x Data: x%x x%x x%x refcnt %d\n",
569 				 *name, *(name+1), *(name+2), *(name+3),
570 				 *(name+4), *(name+5), *(name+6), *(name+7),
571 				 ndlp->nlp_DID, ndlp->nlp_flag,
572 				 ndlp->nlp_state, ndlp->nlp_rpi,
573 				 kref_read(&ndlp->kref));
574 	} else {
575 		lpfc_printf_vlog(vport, KERN_INFO, LOG_TRACE_EVENT,
576 				 "0204 Devloss timeout on "
577 				 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
578 				 "NPort x%06x Data: x%x x%x x%x\n",
579 				 *name, *(name+1), *(name+2), *(name+3),
580 				 *(name+4), *(name+5), *(name+6), *(name+7),
581 				 ndlp->nlp_DID, ndlp->nlp_flag,
582 				 ndlp->nlp_state, ndlp->nlp_rpi);
583 	}
584 	spin_lock_irqsave(&ndlp->lock, iflags);
585 	ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
586 	spin_unlock_irqrestore(&ndlp->lock, iflags);
587 
588 	/* If we are devloss, but we are in the process of rediscovering the
589 	 * ndlp, don't issue a NLP_EVT_DEVICE_RM event.
590 	 */
591 	if (ndlp->nlp_state >= NLP_STE_PLOGI_ISSUE &&
592 	    ndlp->nlp_state <= NLP_STE_PRLI_ISSUE) {
593 		return fcf_inuse;
594 	}
595 
596 	if (!(ndlp->fc4_xpt_flags & NVME_XPT_REGD))
597 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
598 
599 	return fcf_inuse;
600 }
601 
602 static void lpfc_check_vmid_qfpa_issue(struct lpfc_hba *phba)
603 {
604 	struct lpfc_vport *vport;
605 	struct lpfc_vport **vports;
606 	int i;
607 
608 	vports = lpfc_create_vport_work_array(phba);
609 	if (!vports)
610 		return;
611 
612 	for (i = 0; i <= phba->max_vports; i++) {
613 		if ((!vports[i]) && (i == 0))
614 			vport = phba->pport;
615 		else
616 			vport = vports[i];
617 		if (!vport)
618 			break;
619 
620 		if (vport->vmid_flag & LPFC_VMID_ISSUE_QFPA) {
621 			if (!lpfc_issue_els_qfpa(vport))
622 				vport->vmid_flag &= ~LPFC_VMID_ISSUE_QFPA;
623 		}
624 	}
625 	lpfc_destroy_vport_work_array(phba, vports);
626 }
627 
628 /**
629  * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
630  * @phba: Pointer to hba context object.
631  * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
632  * @nlp_did: remote node identifer with devloss timeout.
633  *
634  * This function is called from the worker thread after invoking devloss
635  * timeout handler and releasing the reference count for the ndlp with
636  * which the devloss timeout was handled for SLI4 host. For the devloss
637  * timeout of the last remote node which had been in use of FCF, when this
638  * routine is invoked, it shall be guaranteed that none of the remote are
639  * in-use of FCF. When devloss timeout to the last remote using the FCF,
640  * if the FIP engine is neither in FCF table scan process nor roundrobin
641  * failover process, the in-use FCF shall be unregistered. If the FIP
642  * engine is in FCF discovery process, the devloss timeout state shall
643  * be set for either the FCF table scan process or roundrobin failover
644  * process to unregister the in-use FCF.
645  **/
646 static void
647 lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
648 				    uint32_t nlp_did)
649 {
650 	/* If devloss timeout happened to a remote node when FCF had no
651 	 * longer been in-use, do nothing.
652 	 */
653 	if (!fcf_inuse)
654 		return;
655 
656 	if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
657 		spin_lock_irq(&phba->hbalock);
658 		if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
659 			if (phba->hba_flag & HBA_DEVLOSS_TMO) {
660 				spin_unlock_irq(&phba->hbalock);
661 				return;
662 			}
663 			phba->hba_flag |= HBA_DEVLOSS_TMO;
664 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
665 					"2847 Last remote node (x%x) using "
666 					"FCF devloss tmo\n", nlp_did);
667 		}
668 		if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
669 			spin_unlock_irq(&phba->hbalock);
670 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
671 					"2868 Devloss tmo to FCF rediscovery "
672 					"in progress\n");
673 			return;
674 		}
675 		if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
676 			spin_unlock_irq(&phba->hbalock);
677 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
678 					"2869 Devloss tmo to idle FIP engine, "
679 					"unreg in-use FCF and rescan.\n");
680 			/* Unregister in-use FCF and rescan */
681 			lpfc_unregister_fcf_rescan(phba);
682 			return;
683 		}
684 		spin_unlock_irq(&phba->hbalock);
685 		if (phba->hba_flag & FCF_TS_INPROG)
686 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
687 					"2870 FCF table scan in progress\n");
688 		if (phba->hba_flag & FCF_RR_INPROG)
689 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
690 					"2871 FLOGI roundrobin FCF failover "
691 					"in progress\n");
692 	}
693 	lpfc_unregister_unused_fcf(phba);
694 }
695 
696 /**
697  * lpfc_alloc_fast_evt - Allocates data structure for posting event
698  * @phba: Pointer to hba context object.
699  *
700  * This function is called from the functions which need to post
701  * events from interrupt context. This function allocates data
702  * structure required for posting event. It also keeps track of
703  * number of events pending and prevent event storm when there are
704  * too many events.
705  **/
706 struct lpfc_fast_path_event *
707 lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
708 	struct lpfc_fast_path_event *ret;
709 
710 	/* If there are lot of fast event do not exhaust memory due to this */
711 	if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
712 		return NULL;
713 
714 	ret = kzalloc(sizeof(struct lpfc_fast_path_event),
715 			GFP_ATOMIC);
716 	if (ret) {
717 		atomic_inc(&phba->fast_event_count);
718 		INIT_LIST_HEAD(&ret->work_evt.evt_listp);
719 		ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
720 	}
721 	return ret;
722 }
723 
724 /**
725  * lpfc_free_fast_evt - Frees event data structure
726  * @phba: Pointer to hba context object.
727  * @evt:  Event object which need to be freed.
728  *
729  * This function frees the data structure required for posting
730  * events.
731  **/
732 void
733 lpfc_free_fast_evt(struct lpfc_hba *phba,
734 		struct lpfc_fast_path_event *evt) {
735 
736 	atomic_dec(&phba->fast_event_count);
737 	kfree(evt);
738 }
739 
740 /**
741  * lpfc_send_fastpath_evt - Posts events generated from fast path
742  * @phba: Pointer to hba context object.
743  * @evtp: Event data structure.
744  *
745  * This function is called from worker thread, when the interrupt
746  * context need to post an event. This function posts the event
747  * to fc transport netlink interface.
748  **/
749 static void
750 lpfc_send_fastpath_evt(struct lpfc_hba *phba,
751 		struct lpfc_work_evt *evtp)
752 {
753 	unsigned long evt_category, evt_sub_category;
754 	struct lpfc_fast_path_event *fast_evt_data;
755 	char *evt_data;
756 	uint32_t evt_data_size;
757 	struct Scsi_Host *shost;
758 
759 	fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
760 		work_evt);
761 
762 	evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
763 	evt_sub_category = (unsigned long) fast_evt_data->un.
764 			fabric_evt.subcategory;
765 	shost = lpfc_shost_from_vport(fast_evt_data->vport);
766 	if (evt_category == FC_REG_FABRIC_EVENT) {
767 		if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
768 			evt_data = (char *) &fast_evt_data->un.read_check_error;
769 			evt_data_size = sizeof(fast_evt_data->un.
770 				read_check_error);
771 		} else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
772 			(evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
773 			evt_data = (char *) &fast_evt_data->un.fabric_evt;
774 			evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
775 		} else {
776 			lpfc_free_fast_evt(phba, fast_evt_data);
777 			return;
778 		}
779 	} else if (evt_category == FC_REG_SCSI_EVENT) {
780 		switch (evt_sub_category) {
781 		case LPFC_EVENT_QFULL:
782 		case LPFC_EVENT_DEVBSY:
783 			evt_data = (char *) &fast_evt_data->un.scsi_evt;
784 			evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
785 			break;
786 		case LPFC_EVENT_CHECK_COND:
787 			evt_data = (char *) &fast_evt_data->un.check_cond_evt;
788 			evt_data_size =  sizeof(fast_evt_data->un.
789 				check_cond_evt);
790 			break;
791 		case LPFC_EVENT_VARQUEDEPTH:
792 			evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
793 			evt_data_size = sizeof(fast_evt_data->un.
794 				queue_depth_evt);
795 			break;
796 		default:
797 			lpfc_free_fast_evt(phba, fast_evt_data);
798 			return;
799 		}
800 	} else {
801 		lpfc_free_fast_evt(phba, fast_evt_data);
802 		return;
803 	}
804 
805 	if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
806 		fc_host_post_vendor_event(shost,
807 			fc_get_event_number(),
808 			evt_data_size,
809 			evt_data,
810 			LPFC_NL_VENDOR_ID);
811 
812 	lpfc_free_fast_evt(phba, fast_evt_data);
813 	return;
814 }
815 
816 static void
817 lpfc_work_list_done(struct lpfc_hba *phba)
818 {
819 	struct lpfc_work_evt  *evtp = NULL;
820 	struct lpfc_nodelist  *ndlp;
821 	int free_evt;
822 	int fcf_inuse;
823 	uint32_t nlp_did;
824 	bool hba_pci_err;
825 
826 	spin_lock_irq(&phba->hbalock);
827 	while (!list_empty(&phba->work_list)) {
828 		list_remove_head((&phba->work_list), evtp, typeof(*evtp),
829 				 evt_listp);
830 		spin_unlock_irq(&phba->hbalock);
831 		hba_pci_err = test_bit(HBA_PCI_ERR, &phba->bit_flags);
832 		free_evt = 1;
833 		switch (evtp->evt) {
834 		case LPFC_EVT_ELS_RETRY:
835 			ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
836 			if (!hba_pci_err) {
837 				lpfc_els_retry_delay_handler(ndlp);
838 				free_evt = 0; /* evt is part of ndlp */
839 			}
840 			/* decrement the node reference count held
841 			 * for this queued work
842 			 */
843 			lpfc_nlp_put(ndlp);
844 			break;
845 		case LPFC_EVT_DEV_LOSS:
846 			ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
847 			fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
848 			free_evt = 0;
849 			/* decrement the node reference count held for
850 			 * this queued work
851 			 */
852 			nlp_did = ndlp->nlp_DID;
853 			lpfc_nlp_put(ndlp);
854 			if (phba->sli_rev == LPFC_SLI_REV4)
855 				lpfc_sli4_post_dev_loss_tmo_handler(phba,
856 								    fcf_inuse,
857 								    nlp_did);
858 			break;
859 		case LPFC_EVT_RECOVER_PORT:
860 			ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
861 			if (!hba_pci_err) {
862 				lpfc_sli_abts_recover_port(ndlp->vport, ndlp);
863 				free_evt = 0;
864 			}
865 			/* decrement the node reference count held for
866 			 * this queued work
867 			 */
868 			lpfc_nlp_put(ndlp);
869 			break;
870 		case LPFC_EVT_ONLINE:
871 			if (phba->link_state < LPFC_LINK_DOWN)
872 				*(int *) (evtp->evt_arg1) = lpfc_online(phba);
873 			else
874 				*(int *) (evtp->evt_arg1) = 0;
875 			complete((struct completion *)(evtp->evt_arg2));
876 			break;
877 		case LPFC_EVT_OFFLINE_PREP:
878 			if (phba->link_state >= LPFC_LINK_DOWN)
879 				lpfc_offline_prep(phba, LPFC_MBX_WAIT);
880 			*(int *)(evtp->evt_arg1) = 0;
881 			complete((struct completion *)(evtp->evt_arg2));
882 			break;
883 		case LPFC_EVT_OFFLINE:
884 			lpfc_offline(phba);
885 			lpfc_sli_brdrestart(phba);
886 			*(int *)(evtp->evt_arg1) =
887 				lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
888 			lpfc_unblock_mgmt_io(phba);
889 			complete((struct completion *)(evtp->evt_arg2));
890 			break;
891 		case LPFC_EVT_WARM_START:
892 			lpfc_offline(phba);
893 			lpfc_reset_barrier(phba);
894 			lpfc_sli_brdreset(phba);
895 			lpfc_hba_down_post(phba);
896 			*(int *)(evtp->evt_arg1) =
897 				lpfc_sli_brdready(phba, HS_MBRDY);
898 			lpfc_unblock_mgmt_io(phba);
899 			complete((struct completion *)(evtp->evt_arg2));
900 			break;
901 		case LPFC_EVT_KILL:
902 			lpfc_offline(phba);
903 			*(int *)(evtp->evt_arg1)
904 				= (phba->pport->stopped)
905 				        ? 0 : lpfc_sli_brdkill(phba);
906 			lpfc_unblock_mgmt_io(phba);
907 			complete((struct completion *)(evtp->evt_arg2));
908 			break;
909 		case LPFC_EVT_FASTPATH_MGMT_EVT:
910 			lpfc_send_fastpath_evt(phba, evtp);
911 			free_evt = 0;
912 			break;
913 		case LPFC_EVT_RESET_HBA:
914 			if (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
915 				lpfc_reset_hba(phba);
916 			break;
917 		}
918 		if (free_evt)
919 			kfree(evtp);
920 		spin_lock_irq(&phba->hbalock);
921 	}
922 	spin_unlock_irq(&phba->hbalock);
923 
924 }
925 
926 static void
927 lpfc_work_done(struct lpfc_hba *phba)
928 {
929 	struct lpfc_sli_ring *pring;
930 	uint32_t ha_copy, status, control, work_port_events;
931 	struct lpfc_vport **vports;
932 	struct lpfc_vport *vport;
933 	int i;
934 	bool hba_pci_err;
935 
936 	hba_pci_err = test_bit(HBA_PCI_ERR, &phba->bit_flags);
937 	spin_lock_irq(&phba->hbalock);
938 	ha_copy = phba->work_ha;
939 	phba->work_ha = 0;
940 	spin_unlock_irq(&phba->hbalock);
941 	if (hba_pci_err)
942 		ha_copy = 0;
943 
944 	/* First, try to post the next mailbox command to SLI4 device */
945 	if (phba->pci_dev_grp == LPFC_PCI_DEV_OC && !hba_pci_err)
946 		lpfc_sli4_post_async_mbox(phba);
947 
948 	if (ha_copy & HA_ERATT) {
949 		/* Handle the error attention event */
950 		lpfc_handle_eratt(phba);
951 
952 		if (phba->fw_dump_cmpl) {
953 			complete(phba->fw_dump_cmpl);
954 			phba->fw_dump_cmpl = NULL;
955 		}
956 	}
957 
958 	if (ha_copy & HA_MBATT)
959 		lpfc_sli_handle_mb_event(phba);
960 
961 	if (ha_copy & HA_LATT)
962 		lpfc_handle_latt(phba);
963 
964 	/* Handle VMID Events */
965 	if (lpfc_is_vmid_enabled(phba) && !hba_pci_err) {
966 		if (phba->pport->work_port_events &
967 		    WORKER_CHECK_VMID_ISSUE_QFPA) {
968 			lpfc_check_vmid_qfpa_issue(phba);
969 			phba->pport->work_port_events &=
970 				~WORKER_CHECK_VMID_ISSUE_QFPA;
971 		}
972 		if (phba->pport->work_port_events &
973 		    WORKER_CHECK_INACTIVE_VMID) {
974 			lpfc_check_inactive_vmid(phba);
975 			phba->pport->work_port_events &=
976 			    ~WORKER_CHECK_INACTIVE_VMID;
977 		}
978 	}
979 
980 	/* Process SLI4 events */
981 	if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
982 		if (phba->hba_flag & HBA_RRQ_ACTIVE)
983 			lpfc_handle_rrq_active(phba);
984 		if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
985 			lpfc_sli4_els_xri_abort_event_proc(phba);
986 		if (phba->hba_flag & ASYNC_EVENT)
987 			lpfc_sli4_async_event_proc(phba);
988 		if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
989 			spin_lock_irq(&phba->hbalock);
990 			phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
991 			spin_unlock_irq(&phba->hbalock);
992 			lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
993 		}
994 		if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
995 			lpfc_sli4_fcf_redisc_event_proc(phba);
996 	}
997 
998 	vports = lpfc_create_vport_work_array(phba);
999 	if (vports != NULL)
1000 		for (i = 0; i <= phba->max_vports; i++) {
1001 			/*
1002 			 * We could have no vports in array if unloading, so if
1003 			 * this happens then just use the pport
1004 			 */
1005 			if (vports[i] == NULL && i == 0)
1006 				vport = phba->pport;
1007 			else
1008 				vport = vports[i];
1009 			if (vport == NULL)
1010 				break;
1011 			spin_lock_irq(&vport->work_port_lock);
1012 			work_port_events = vport->work_port_events;
1013 			vport->work_port_events &= ~work_port_events;
1014 			spin_unlock_irq(&vport->work_port_lock);
1015 			if (hba_pci_err)
1016 				continue;
1017 			if (work_port_events & WORKER_DISC_TMO)
1018 				lpfc_disc_timeout_handler(vport);
1019 			if (work_port_events & WORKER_ELS_TMO)
1020 				lpfc_els_timeout_handler(vport);
1021 			if (work_port_events & WORKER_HB_TMO)
1022 				lpfc_hb_timeout_handler(phba);
1023 			if (work_port_events & WORKER_MBOX_TMO)
1024 				lpfc_mbox_timeout_handler(phba);
1025 			if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
1026 				lpfc_unblock_fabric_iocbs(phba);
1027 			if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
1028 				lpfc_ramp_down_queue_handler(phba);
1029 			if (work_port_events & WORKER_DELAYED_DISC_TMO)
1030 				lpfc_delayed_disc_timeout_handler(vport);
1031 		}
1032 	lpfc_destroy_vport_work_array(phba, vports);
1033 
1034 	pring = lpfc_phba_elsring(phba);
1035 	status = (ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
1036 	status >>= (4*LPFC_ELS_RING);
1037 	if (pring && (status & HA_RXMASK ||
1038 		      pring->flag & LPFC_DEFERRED_RING_EVENT ||
1039 		      phba->hba_flag & HBA_SP_QUEUE_EVT)) {
1040 		if (pring->flag & LPFC_STOP_IOCB_EVENT) {
1041 			pring->flag |= LPFC_DEFERRED_RING_EVENT;
1042 			/* Preserve legacy behavior. */
1043 			if (!(phba->hba_flag & HBA_SP_QUEUE_EVT))
1044 				set_bit(LPFC_DATA_READY, &phba->data_flags);
1045 		} else {
1046 			/* Driver could have abort request completed in queue
1047 			 * when link goes down.  Allow for this transition.
1048 			 */
1049 			if (phba->link_state >= LPFC_LINK_DOWN ||
1050 			    phba->link_flag & LS_MDS_LOOPBACK) {
1051 				pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
1052 				lpfc_sli_handle_slow_ring_event(phba, pring,
1053 								(status &
1054 								HA_RXMASK));
1055 			}
1056 		}
1057 		if (phba->sli_rev == LPFC_SLI_REV4)
1058 			lpfc_drain_txq(phba);
1059 		/*
1060 		 * Turn on Ring interrupts
1061 		 */
1062 		if (phba->sli_rev <= LPFC_SLI_REV3) {
1063 			spin_lock_irq(&phba->hbalock);
1064 			control = readl(phba->HCregaddr);
1065 			if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
1066 				lpfc_debugfs_slow_ring_trc(phba,
1067 					"WRK Enable ring: cntl:x%x hacopy:x%x",
1068 					control, ha_copy, 0);
1069 
1070 				control |= (HC_R0INT_ENA << LPFC_ELS_RING);
1071 				writel(control, phba->HCregaddr);
1072 				readl(phba->HCregaddr); /* flush */
1073 			} else {
1074 				lpfc_debugfs_slow_ring_trc(phba,
1075 					"WRK Ring ok:     cntl:x%x hacopy:x%x",
1076 					control, ha_copy, 0);
1077 			}
1078 			spin_unlock_irq(&phba->hbalock);
1079 		}
1080 	}
1081 	lpfc_work_list_done(phba);
1082 }
1083 
1084 int
1085 lpfc_do_work(void *p)
1086 {
1087 	struct lpfc_hba *phba = p;
1088 	int rc;
1089 
1090 	set_user_nice(current, MIN_NICE);
1091 	current->flags |= PF_NOFREEZE;
1092 	phba->data_flags = 0;
1093 
1094 	while (!kthread_should_stop()) {
1095 		/* wait and check worker queue activities */
1096 		rc = wait_event_interruptible(phba->work_waitq,
1097 					(test_and_clear_bit(LPFC_DATA_READY,
1098 							    &phba->data_flags)
1099 					 || kthread_should_stop()));
1100 		/* Signal wakeup shall terminate the worker thread */
1101 		if (rc) {
1102 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1103 					"0433 Wakeup on signal: rc=x%x\n", rc);
1104 			break;
1105 		}
1106 
1107 		/* Attend pending lpfc data processing */
1108 		lpfc_work_done(phba);
1109 	}
1110 	phba->worker_thread = NULL;
1111 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1112 			"0432 Worker thread stopped.\n");
1113 	return 0;
1114 }
1115 
1116 /*
1117  * This is only called to handle FC worker events. Since this a rare
1118  * occurrence, we allocate a struct lpfc_work_evt structure here instead of
1119  * embedding it in the IOCB.
1120  */
1121 int
1122 lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
1123 		      uint32_t evt)
1124 {
1125 	struct lpfc_work_evt  *evtp;
1126 	unsigned long flags;
1127 
1128 	/*
1129 	 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
1130 	 * be queued to worker thread for processing
1131 	 */
1132 	evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
1133 	if (!evtp)
1134 		return 0;
1135 
1136 	evtp->evt_arg1  = arg1;
1137 	evtp->evt_arg2  = arg2;
1138 	evtp->evt       = evt;
1139 
1140 	spin_lock_irqsave(&phba->hbalock, flags);
1141 	list_add_tail(&evtp->evt_listp, &phba->work_list);
1142 	spin_unlock_irqrestore(&phba->hbalock, flags);
1143 
1144 	lpfc_worker_wake_up(phba);
1145 
1146 	return 1;
1147 }
1148 
1149 void
1150 lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
1151 {
1152 	struct lpfc_hba  *phba = vport->phba;
1153 	struct lpfc_nodelist *ndlp, *next_ndlp;
1154 
1155 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1156 		if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
1157 		    ((vport->port_type == LPFC_NPIV_PORT) &&
1158 		     ((ndlp->nlp_DID == NameServer_DID) ||
1159 		      (ndlp->nlp_DID == FDMI_DID) ||
1160 		      (ndlp->nlp_DID == Fabric_Cntl_DID))))
1161 			lpfc_unreg_rpi(vport, ndlp);
1162 
1163 		/* Leave Fabric nodes alone on link down */
1164 		if ((phba->sli_rev < LPFC_SLI_REV4) &&
1165 		    (!remove && ndlp->nlp_type & NLP_FABRIC))
1166 			continue;
1167 
1168 		/* Notify transport of connectivity loss to trigger cleanup. */
1169 		if (phba->nvmet_support &&
1170 		    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
1171 			lpfc_nvmet_invalidate_host(phba, ndlp);
1172 
1173 		lpfc_disc_state_machine(vport, ndlp, NULL,
1174 					remove
1175 					? NLP_EVT_DEVICE_RM
1176 					: NLP_EVT_DEVICE_RECOVERY);
1177 	}
1178 	if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
1179 		if (phba->sli_rev == LPFC_SLI_REV4)
1180 			lpfc_sli4_unreg_all_rpis(vport);
1181 		lpfc_mbx_unreg_vpi(vport);
1182 		set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
1183 	}
1184 }
1185 
1186 void
1187 lpfc_port_link_failure(struct lpfc_vport *vport)
1188 {
1189 	lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
1190 
1191 	/* Cleanup any outstanding received buffers */
1192 	lpfc_cleanup_rcv_buffers(vport);
1193 
1194 	/* Cleanup any outstanding RSCN activity */
1195 	lpfc_els_flush_rscn(vport);
1196 
1197 	/* Cleanup any outstanding ELS commands */
1198 	lpfc_els_flush_cmd(vport);
1199 
1200 	lpfc_cleanup_rpis(vport, 0);
1201 
1202 	/* Turn off discovery timer if its running */
1203 	lpfc_can_disctmo(vport);
1204 }
1205 
1206 void
1207 lpfc_linkdown_port(struct lpfc_vport *vport)
1208 {
1209 	struct lpfc_hba *phba = vport->phba;
1210 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1211 
1212 	if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1213 		fc_host_post_event(shost, fc_get_event_number(),
1214 				   FCH_EVT_LINKDOWN, 0);
1215 
1216 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1217 		"Link Down:       state:x%x rtry:x%x flg:x%x",
1218 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
1219 
1220 	lpfc_port_link_failure(vport);
1221 
1222 	/* Stop delayed Nport discovery */
1223 	clear_bit(FC_DISC_DELAYED, &vport->fc_flag);
1224 	del_timer_sync(&vport->delayed_disc_tmo);
1225 
1226 	if (phba->sli_rev == LPFC_SLI_REV4 &&
1227 	    vport->port_type == LPFC_PHYSICAL_PORT &&
1228 	    phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_CONFIG) {
1229 		/* Assume success on link up */
1230 		phba->sli4_hba.fawwpn_flag |= LPFC_FAWWPN_FABRIC;
1231 	}
1232 }
1233 
1234 int
1235 lpfc_linkdown(struct lpfc_hba *phba)
1236 {
1237 	struct lpfc_vport *vport = phba->pport;
1238 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1239 	struct lpfc_vport **vports;
1240 	LPFC_MBOXQ_t          *mb;
1241 	int i;
1242 	int offline;
1243 
1244 	if (phba->link_state == LPFC_LINK_DOWN)
1245 		return 0;
1246 
1247 	/* Block all SCSI stack I/Os */
1248 	lpfc_scsi_dev_block(phba);
1249 	offline = pci_channel_offline(phba->pcidev);
1250 
1251 	phba->defer_flogi_acc_flag = false;
1252 
1253 	/* Clear external loopback plug detected flag */
1254 	phba->link_flag &= ~LS_EXTERNAL_LOOPBACK;
1255 
1256 	spin_lock_irq(&phba->hbalock);
1257 	phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1258 	spin_unlock_irq(&phba->hbalock);
1259 	if (phba->link_state > LPFC_LINK_DOWN) {
1260 		phba->link_state = LPFC_LINK_DOWN;
1261 		if (phba->sli4_hba.conf_trunk) {
1262 			phba->trunk_link.link0.state = 0;
1263 			phba->trunk_link.link1.state = 0;
1264 			phba->trunk_link.link2.state = 0;
1265 			phba->trunk_link.link3.state = 0;
1266 			phba->trunk_link.phy_lnk_speed =
1267 						LPFC_LINK_SPEED_UNKNOWN;
1268 			phba->sli4_hba.link_state.logical_speed =
1269 						LPFC_LINK_SPEED_UNKNOWN;
1270 		}
1271 		clear_bit(FC_LBIT, &phba->pport->fc_flag);
1272 	}
1273 	vports = lpfc_create_vport_work_array(phba);
1274 	if (vports != NULL) {
1275 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1276 			/* Issue a LINK DOWN event to all nodes */
1277 			lpfc_linkdown_port(vports[i]);
1278 
1279 			vports[i]->fc_myDID = 0;
1280 
1281 			if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1282 			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
1283 				if (phba->nvmet_support)
1284 					lpfc_nvmet_update_targetport(phba);
1285 				else
1286 					lpfc_nvme_update_localport(vports[i]);
1287 			}
1288 		}
1289 	}
1290 	lpfc_destroy_vport_work_array(phba, vports);
1291 
1292 	/* Clean up any SLI3 firmware default rpi's */
1293 	if (phba->sli_rev > LPFC_SLI_REV3 || offline)
1294 		goto skip_unreg_did;
1295 
1296 	mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1297 	if (mb) {
1298 		lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
1299 		mb->vport = vport;
1300 		mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1301 		if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
1302 		    == MBX_NOT_FINISHED) {
1303 			mempool_free(mb, phba->mbox_mem_pool);
1304 		}
1305 	}
1306 
1307  skip_unreg_did:
1308 	/* Setup myDID for link up if we are in pt2pt mode */
1309 	if (test_bit(FC_PT2PT, &phba->pport->fc_flag)) {
1310 		mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1311 		if (mb) {
1312 			lpfc_config_link(phba, mb);
1313 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1314 			mb->vport = vport;
1315 			if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
1316 			    == MBX_NOT_FINISHED) {
1317 				mempool_free(mb, phba->mbox_mem_pool);
1318 			}
1319 		}
1320 		clear_bit(FC_PT2PT, &phba->pport->fc_flag);
1321 		clear_bit(FC_PT2PT_PLOGI, &phba->pport->fc_flag);
1322 		spin_lock_irq(shost->host_lock);
1323 		phba->pport->rcv_flogi_cnt = 0;
1324 		spin_unlock_irq(shost->host_lock);
1325 	}
1326 	return 0;
1327 }
1328 
1329 static void
1330 lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
1331 {
1332 	struct lpfc_nodelist *ndlp;
1333 
1334 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
1335 		ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
1336 
1337 		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1338 			continue;
1339 		if (ndlp->nlp_type & NLP_FABRIC) {
1340 			/* On Linkup its safe to clean up the ndlp
1341 			 * from Fabric connections.
1342 			 */
1343 			if (ndlp->nlp_DID != Fabric_DID)
1344 				lpfc_unreg_rpi(vport, ndlp);
1345 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1346 		} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
1347 			/* Fail outstanding IO now since device is
1348 			 * marked for PLOGI.
1349 			 */
1350 			lpfc_unreg_rpi(vport, ndlp);
1351 		}
1352 	}
1353 }
1354 
1355 static void
1356 lpfc_linkup_port(struct lpfc_vport *vport)
1357 {
1358 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1359 	struct lpfc_hba  *phba = vport->phba;
1360 
1361 	if (test_bit(FC_UNLOADING, &vport->load_flag))
1362 		return;
1363 
1364 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1365 		"Link Up:         top:x%x speed:x%x flg:x%x",
1366 		phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
1367 
1368 	/* If NPIV is not enabled, only bring the physical port up */
1369 	if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1370 		(vport != phba->pport))
1371 		return;
1372 
1373 	if (phba->defer_flogi_acc_flag) {
1374 		clear_bit(FC_ABORT_DISCOVERY, &vport->fc_flag);
1375 		clear_bit(FC_RSCN_MODE, &vport->fc_flag);
1376 		clear_bit(FC_NLP_MORE, &vport->fc_flag);
1377 		clear_bit(FC_RSCN_DISCOVERY, &vport->fc_flag);
1378 	} else {
1379 		clear_bit(FC_PT2PT, &vport->fc_flag);
1380 		clear_bit(FC_PT2PT_PLOGI, &vport->fc_flag);
1381 		clear_bit(FC_ABORT_DISCOVERY, &vport->fc_flag);
1382 		clear_bit(FC_RSCN_MODE, &vport->fc_flag);
1383 		clear_bit(FC_NLP_MORE, &vport->fc_flag);
1384 		clear_bit(FC_RSCN_DISCOVERY, &vport->fc_flag);
1385 	}
1386 	set_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
1387 
1388 	spin_lock_irq(shost->host_lock);
1389 	vport->fc_ns_retry = 0;
1390 	spin_unlock_irq(shost->host_lock);
1391 	lpfc_setup_fdmi_mask(vport);
1392 
1393 	lpfc_linkup_cleanup_nodes(vport);
1394 }
1395 
1396 static int
1397 lpfc_linkup(struct lpfc_hba *phba)
1398 {
1399 	struct lpfc_vport **vports;
1400 	int i;
1401 	struct Scsi_Host  *shost = lpfc_shost_from_vport(phba->pport);
1402 
1403 	phba->link_state = LPFC_LINK_UP;
1404 
1405 	/* Unblock fabric iocbs if they are blocked */
1406 	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1407 	del_timer_sync(&phba->fabric_block_timer);
1408 
1409 	vports = lpfc_create_vport_work_array(phba);
1410 	if (vports != NULL)
1411 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1412 			lpfc_linkup_port(vports[i]);
1413 	lpfc_destroy_vport_work_array(phba, vports);
1414 
1415 	/* Clear the pport flogi counter in case the link down was
1416 	 * absorbed without an ACQE. No lock here - in worker thread
1417 	 * and discovery is synchronized.
1418 	 */
1419 	spin_lock_irq(shost->host_lock);
1420 	phba->pport->rcv_flogi_cnt = 0;
1421 	spin_unlock_irq(shost->host_lock);
1422 
1423 	/* reinitialize initial HBA flag */
1424 	phba->hba_flag &= ~(HBA_FLOGI_ISSUED | HBA_RHBA_CMPL);
1425 
1426 	return 0;
1427 }
1428 
1429 /*
1430  * This routine handles processing a CLEAR_LA mailbox
1431  * command upon completion. It is setup in the LPFC_MBOXQ
1432  * as the completion routine when the command is
1433  * handed off to the SLI layer. SLI3 only.
1434  */
1435 static void
1436 lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1437 {
1438 	struct lpfc_vport *vport = pmb->vport;
1439 	struct lpfc_sli   *psli = &phba->sli;
1440 	MAILBOX_t *mb = &pmb->u.mb;
1441 	uint32_t control;
1442 
1443 	/* Since we don't do discovery right now, turn these off here */
1444 	psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1445 	psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1446 
1447 	/* Check for error */
1448 	if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
1449 		/* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
1450 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1451 				 "0320 CLEAR_LA mbxStatus error x%x hba "
1452 				 "state x%x\n",
1453 				 mb->mbxStatus, vport->port_state);
1454 		phba->link_state = LPFC_HBA_ERROR;
1455 		goto out;
1456 	}
1457 
1458 	if (vport->port_type == LPFC_PHYSICAL_PORT)
1459 		phba->link_state = LPFC_HBA_READY;
1460 
1461 	spin_lock_irq(&phba->hbalock);
1462 	psli->sli_flag |= LPFC_PROCESS_LA;
1463 	control = readl(phba->HCregaddr);
1464 	control |= HC_LAINT_ENA;
1465 	writel(control, phba->HCregaddr);
1466 	readl(phba->HCregaddr); /* flush */
1467 	spin_unlock_irq(&phba->hbalock);
1468 	mempool_free(pmb, phba->mbox_mem_pool);
1469 	return;
1470 
1471 out:
1472 	/* Device Discovery completes */
1473 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1474 			 "0225 Device Discovery completes\n");
1475 	mempool_free(pmb, phba->mbox_mem_pool);
1476 
1477 	clear_bit(FC_ABORT_DISCOVERY, &vport->fc_flag);
1478 
1479 	lpfc_can_disctmo(vport);
1480 
1481 	/* turn on Link Attention interrupts */
1482 
1483 	spin_lock_irq(&phba->hbalock);
1484 	psli->sli_flag |= LPFC_PROCESS_LA;
1485 	control = readl(phba->HCregaddr);
1486 	control |= HC_LAINT_ENA;
1487 	writel(control, phba->HCregaddr);
1488 	readl(phba->HCregaddr); /* flush */
1489 	spin_unlock_irq(&phba->hbalock);
1490 
1491 	return;
1492 }
1493 
1494 void
1495 lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1496 {
1497 	struct lpfc_vport *vport = pmb->vport;
1498 	LPFC_MBOXQ_t *sparam_mb;
1499 	u16 status = pmb->u.mb.mbxStatus;
1500 	int rc;
1501 
1502 	mempool_free(pmb, phba->mbox_mem_pool);
1503 
1504 	if (status)
1505 		goto out;
1506 
1507 	/* don't perform discovery for SLI4 loopback diagnostic test */
1508 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
1509 	    !(phba->hba_flag & HBA_FCOE_MODE) &&
1510 	    (phba->link_flag & LS_LOOPBACK_MODE))
1511 		return;
1512 
1513 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
1514 	    test_bit(FC_PUBLIC_LOOP, &vport->fc_flag) &&
1515 	    !test_bit(FC_LBIT, &vport->fc_flag)) {
1516 			/* Need to wait for FAN - use discovery timer
1517 			 * for timeout.  port_state is identically
1518 			 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1519 			 */
1520 			lpfc_set_disctmo(vport);
1521 			return;
1522 	}
1523 
1524 	/* Start discovery by sending a FLOGI. port_state is identically
1525 	 * LPFC_FLOGI while waiting for FLOGI cmpl.
1526 	 */
1527 	if (vport->port_state != LPFC_FLOGI) {
1528 		/* Issue MBX_READ_SPARAM to update CSPs before FLOGI if
1529 		 * bb-credit recovery is in place.
1530 		 */
1531 		if (phba->bbcredit_support && phba->cfg_enable_bbcr &&
1532 		    !(phba->link_flag & LS_LOOPBACK_MODE)) {
1533 			sparam_mb = mempool_alloc(phba->mbox_mem_pool,
1534 						  GFP_KERNEL);
1535 			if (!sparam_mb)
1536 				goto sparam_out;
1537 
1538 			rc = lpfc_read_sparam(phba, sparam_mb, 0);
1539 			if (rc) {
1540 				mempool_free(sparam_mb, phba->mbox_mem_pool);
1541 				goto sparam_out;
1542 			}
1543 			sparam_mb->vport = vport;
1544 			sparam_mb->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
1545 			rc = lpfc_sli_issue_mbox(phba, sparam_mb, MBX_NOWAIT);
1546 			if (rc == MBX_NOT_FINISHED) {
1547 				lpfc_mbox_rsrc_cleanup(phba, sparam_mb,
1548 						       MBOX_THD_UNLOCKED);
1549 				goto sparam_out;
1550 			}
1551 
1552 			phba->hba_flag |= HBA_DEFER_FLOGI;
1553 		}  else {
1554 			lpfc_initial_flogi(vport);
1555 		}
1556 	} else {
1557 		if (test_bit(FC_PT2PT, &vport->fc_flag))
1558 			lpfc_disc_start(vport);
1559 	}
1560 	return;
1561 
1562 out:
1563 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1564 			 "0306 CONFIG_LINK mbxStatus error x%x HBA state x%x\n",
1565 			 status, vport->port_state);
1566 
1567 sparam_out:
1568 	lpfc_linkdown(phba);
1569 
1570 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1571 			 "0200 CONFIG_LINK bad hba state x%x\n",
1572 			 vport->port_state);
1573 
1574 	lpfc_issue_clear_la(phba, vport);
1575 	return;
1576 }
1577 
1578 /**
1579  * lpfc_sli4_clear_fcf_rr_bmask
1580  * @phba: pointer to the struct lpfc_hba for this port.
1581  * This fucnction resets the round robin bit mask and clears the
1582  * fcf priority list. The list deletions are done while holding the
1583  * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1584  * from the lpfc_fcf_pri record.
1585  **/
1586 void
1587 lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1588 {
1589 	struct lpfc_fcf_pri *fcf_pri;
1590 	struct lpfc_fcf_pri *next_fcf_pri;
1591 	memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1592 	spin_lock_irq(&phba->hbalock);
1593 	list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1594 				&phba->fcf.fcf_pri_list, list) {
1595 		list_del_init(&fcf_pri->list);
1596 		fcf_pri->fcf_rec.flag = 0;
1597 	}
1598 	spin_unlock_irq(&phba->hbalock);
1599 }
1600 static void
1601 lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1602 {
1603 	struct lpfc_vport *vport = mboxq->vport;
1604 
1605 	if (mboxq->u.mb.mbxStatus) {
1606 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1607 				 "2017 REG_FCFI mbxStatus error x%x "
1608 				 "HBA state x%x\n", mboxq->u.mb.mbxStatus,
1609 				 vport->port_state);
1610 		goto fail_out;
1611 	}
1612 
1613 	/* Start FCoE discovery by sending a FLOGI. */
1614 	phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1615 	/* Set the FCFI registered flag */
1616 	spin_lock_irq(&phba->hbalock);
1617 	phba->fcf.fcf_flag |= FCF_REGISTERED;
1618 	spin_unlock_irq(&phba->hbalock);
1619 
1620 	/* If there is a pending FCoE event, restart FCF table scan. */
1621 	if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1622 		lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
1623 		goto fail_out;
1624 
1625 	/* Mark successful completion of FCF table scan */
1626 	spin_lock_irq(&phba->hbalock);
1627 	phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1628 	phba->hba_flag &= ~FCF_TS_INPROG;
1629 	if (vport->port_state != LPFC_FLOGI) {
1630 		phba->hba_flag |= FCF_RR_INPROG;
1631 		spin_unlock_irq(&phba->hbalock);
1632 		lpfc_issue_init_vfi(vport);
1633 		goto out;
1634 	}
1635 	spin_unlock_irq(&phba->hbalock);
1636 	goto out;
1637 
1638 fail_out:
1639 	spin_lock_irq(&phba->hbalock);
1640 	phba->hba_flag &= ~FCF_RR_INPROG;
1641 	spin_unlock_irq(&phba->hbalock);
1642 out:
1643 	mempool_free(mboxq, phba->mbox_mem_pool);
1644 }
1645 
1646 /**
1647  * lpfc_fab_name_match - Check if the fcf fabric name match.
1648  * @fab_name: pointer to fabric name.
1649  * @new_fcf_record: pointer to fcf record.
1650  *
1651  * This routine compare the fcf record's fabric name with provided
1652  * fabric name. If the fabric name are identical this function
1653  * returns 1 else return 0.
1654  **/
1655 static uint32_t
1656 lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1657 {
1658 	if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
1659 		return 0;
1660 	if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1661 		return 0;
1662 	if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1663 		return 0;
1664 	if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1665 		return 0;
1666 	if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1667 		return 0;
1668 	if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1669 		return 0;
1670 	if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1671 		return 0;
1672 	if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1673 		return 0;
1674 	return 1;
1675 }
1676 
1677 /**
1678  * lpfc_sw_name_match - Check if the fcf switch name match.
1679  * @sw_name: pointer to switch name.
1680  * @new_fcf_record: pointer to fcf record.
1681  *
1682  * This routine compare the fcf record's switch name with provided
1683  * switch name. If the switch name are identical this function
1684  * returns 1 else return 0.
1685  **/
1686 static uint32_t
1687 lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1688 {
1689 	if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
1690 		return 0;
1691 	if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1692 		return 0;
1693 	if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1694 		return 0;
1695 	if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1696 		return 0;
1697 	if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1698 		return 0;
1699 	if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1700 		return 0;
1701 	if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1702 		return 0;
1703 	if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1704 		return 0;
1705 	return 1;
1706 }
1707 
1708 /**
1709  * lpfc_mac_addr_match - Check if the fcf mac address match.
1710  * @mac_addr: pointer to mac address.
1711  * @new_fcf_record: pointer to fcf record.
1712  *
1713  * This routine compare the fcf record's mac address with HBA's
1714  * FCF mac address. If the mac addresses are identical this function
1715  * returns 1 else return 0.
1716  **/
1717 static uint32_t
1718 lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
1719 {
1720 	if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
1721 		return 0;
1722 	if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1723 		return 0;
1724 	if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1725 		return 0;
1726 	if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1727 		return 0;
1728 	if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1729 		return 0;
1730 	if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1731 		return 0;
1732 	return 1;
1733 }
1734 
1735 static bool
1736 lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1737 {
1738 	return (curr_vlan_id == new_vlan_id);
1739 }
1740 
1741 /**
1742  * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1743  * @phba: pointer to lpfc hba data structure.
1744  * @fcf_index: Index for the lpfc_fcf_record.
1745  * @new_fcf_record: pointer to hba fcf record.
1746  *
1747  * This routine updates the driver FCF priority record from the new HBA FCF
1748  * record. The hbalock is asserted held in the code path calling this
1749  * routine.
1750  **/
1751 static void
1752 __lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1753 				 struct fcf_record *new_fcf_record
1754 				 )
1755 {
1756 	struct lpfc_fcf_pri *fcf_pri;
1757 
1758 	fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1759 	fcf_pri->fcf_rec.fcf_index = fcf_index;
1760 	/* FCF record priority */
1761 	fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1762 
1763 }
1764 
1765 /**
1766  * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1767  * @fcf_rec: pointer to driver fcf record.
1768  * @new_fcf_record: pointer to fcf record.
1769  *
1770  * This routine copies the FCF information from the FCF
1771  * record to lpfc_hba data structure.
1772  **/
1773 static void
1774 lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1775 		     struct fcf_record *new_fcf_record)
1776 {
1777 	/* Fabric name */
1778 	fcf_rec->fabric_name[0] =
1779 		bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1780 	fcf_rec->fabric_name[1] =
1781 		bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1782 	fcf_rec->fabric_name[2] =
1783 		bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1784 	fcf_rec->fabric_name[3] =
1785 		bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1786 	fcf_rec->fabric_name[4] =
1787 		bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1788 	fcf_rec->fabric_name[5] =
1789 		bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1790 	fcf_rec->fabric_name[6] =
1791 		bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1792 	fcf_rec->fabric_name[7] =
1793 		bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1794 	/* Mac address */
1795 	fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1796 	fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1797 	fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1798 	fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1799 	fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1800 	fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1801 	/* FCF record index */
1802 	fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1803 	/* FCF record priority */
1804 	fcf_rec->priority = new_fcf_record->fip_priority;
1805 	/* Switch name */
1806 	fcf_rec->switch_name[0] =
1807 		bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1808 	fcf_rec->switch_name[1] =
1809 		bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1810 	fcf_rec->switch_name[2] =
1811 		bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1812 	fcf_rec->switch_name[3] =
1813 		bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1814 	fcf_rec->switch_name[4] =
1815 		bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1816 	fcf_rec->switch_name[5] =
1817 		bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1818 	fcf_rec->switch_name[6] =
1819 		bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1820 	fcf_rec->switch_name[7] =
1821 		bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
1822 }
1823 
1824 /**
1825  * __lpfc_update_fcf_record - Update driver fcf record
1826  * @phba: pointer to lpfc hba data structure.
1827  * @fcf_rec: pointer to driver fcf record.
1828  * @new_fcf_record: pointer to hba fcf record.
1829  * @addr_mode: address mode to be set to the driver fcf record.
1830  * @vlan_id: vlan tag to be set to the driver fcf record.
1831  * @flag: flag bits to be set to the driver fcf record.
1832  *
1833  * This routine updates the driver FCF record from the new HBA FCF record
1834  * together with the address mode, vlan_id, and other informations. This
1835  * routine is called with the hbalock held.
1836  **/
1837 static void
1838 __lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1839 		       struct fcf_record *new_fcf_record, uint32_t addr_mode,
1840 		       uint16_t vlan_id, uint32_t flag)
1841 {
1842 	lockdep_assert_held(&phba->hbalock);
1843 
1844 	/* Copy the fields from the HBA's FCF record */
1845 	lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1846 	/* Update other fields of driver FCF record */
1847 	fcf_rec->addr_mode = addr_mode;
1848 	fcf_rec->vlan_id = vlan_id;
1849 	fcf_rec->flag |= (flag | RECORD_VALID);
1850 	__lpfc_update_fcf_record_pri(phba,
1851 		bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1852 				 new_fcf_record);
1853 }
1854 
1855 /**
1856  * lpfc_register_fcf - Register the FCF with hba.
1857  * @phba: pointer to lpfc hba data structure.
1858  *
1859  * This routine issues a register fcfi mailbox command to register
1860  * the fcf with HBA.
1861  **/
1862 static void
1863 lpfc_register_fcf(struct lpfc_hba *phba)
1864 {
1865 	LPFC_MBOXQ_t *fcf_mbxq;
1866 	int rc;
1867 
1868 	spin_lock_irq(&phba->hbalock);
1869 	/* If the FCF is not available do nothing. */
1870 	if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
1871 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1872 		spin_unlock_irq(&phba->hbalock);
1873 		return;
1874 	}
1875 
1876 	/* The FCF is already registered, start discovery */
1877 	if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1878 		phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1879 		phba->hba_flag &= ~FCF_TS_INPROG;
1880 		if (phba->pport->port_state != LPFC_FLOGI &&
1881 		    test_bit(FC_FABRIC, &phba->pport->fc_flag)) {
1882 			phba->hba_flag |= FCF_RR_INPROG;
1883 			spin_unlock_irq(&phba->hbalock);
1884 			lpfc_initial_flogi(phba->pport);
1885 			return;
1886 		}
1887 		spin_unlock_irq(&phba->hbalock);
1888 		return;
1889 	}
1890 	spin_unlock_irq(&phba->hbalock);
1891 
1892 	fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1893 	if (!fcf_mbxq) {
1894 		spin_lock_irq(&phba->hbalock);
1895 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1896 		spin_unlock_irq(&phba->hbalock);
1897 		return;
1898 	}
1899 
1900 	lpfc_reg_fcfi(phba, fcf_mbxq);
1901 	fcf_mbxq->vport = phba->pport;
1902 	fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1903 	rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
1904 	if (rc == MBX_NOT_FINISHED) {
1905 		spin_lock_irq(&phba->hbalock);
1906 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1907 		spin_unlock_irq(&phba->hbalock);
1908 		mempool_free(fcf_mbxq, phba->mbox_mem_pool);
1909 	}
1910 
1911 	return;
1912 }
1913 
1914 /**
1915  * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1916  * @phba: pointer to lpfc hba data structure.
1917  * @new_fcf_record: pointer to fcf record.
1918  * @boot_flag: Indicates if this record used by boot bios.
1919  * @addr_mode: The address mode to be used by this FCF
1920  * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
1921  *
1922  * This routine compare the fcf record with connect list obtained from the
1923  * config region to decide if this FCF can be used for SAN discovery. It returns
1924  * 1 if this record can be used for SAN discovery else return zero. If this FCF
1925  * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1926  * is used by boot bios and addr_mode will indicate the addressing mode to be
1927  * used for this FCF when the function returns.
1928  * If the FCF record need to be used with a particular vlan id, the vlan is
1929  * set in the vlan_id on return of the function. If not VLAN tagging need to
1930  * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
1931  **/
1932 static int
1933 lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1934 			struct fcf_record *new_fcf_record,
1935 			uint32_t *boot_flag, uint32_t *addr_mode,
1936 			uint16_t *vlan_id)
1937 {
1938 	struct lpfc_fcf_conn_entry *conn_entry;
1939 	int i, j, fcf_vlan_id = 0;
1940 
1941 	/* Find the lowest VLAN id in the FCF record */
1942 	for (i = 0; i < 512; i++) {
1943 		if (new_fcf_record->vlan_bitmap[i]) {
1944 			fcf_vlan_id = i * 8;
1945 			j = 0;
1946 			while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1947 				j++;
1948 				fcf_vlan_id++;
1949 			}
1950 			break;
1951 		}
1952 	}
1953 
1954 	/* FCF not valid/available or solicitation in progress */
1955 	if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1956 	    !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1957 	    bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
1958 		return 0;
1959 
1960 	if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
1961 		*boot_flag = 0;
1962 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1963 				new_fcf_record);
1964 		if (phba->valid_vlan)
1965 			*vlan_id = phba->vlan_id;
1966 		else
1967 			*vlan_id = LPFC_FCOE_NULL_VID;
1968 		return 1;
1969 	}
1970 
1971 	/*
1972 	 * If there are no FCF connection table entry, driver connect to all
1973 	 * FCFs.
1974 	 */
1975 	if (list_empty(&phba->fcf_conn_rec_list)) {
1976 		*boot_flag = 0;
1977 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1978 			new_fcf_record);
1979 
1980 		/*
1981 		 * When there are no FCF connect entries, use driver's default
1982 		 * addressing mode - FPMA.
1983 		 */
1984 		if (*addr_mode & LPFC_FCF_FPMA)
1985 			*addr_mode = LPFC_FCF_FPMA;
1986 
1987 		/* If FCF record report a vlan id use that vlan id */
1988 		if (fcf_vlan_id)
1989 			*vlan_id = fcf_vlan_id;
1990 		else
1991 			*vlan_id = LPFC_FCOE_NULL_VID;
1992 		return 1;
1993 	}
1994 
1995 	list_for_each_entry(conn_entry,
1996 			    &phba->fcf_conn_rec_list, list) {
1997 		if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1998 			continue;
1999 
2000 		if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
2001 			!lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
2002 					     new_fcf_record))
2003 			continue;
2004 		if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
2005 			!lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
2006 					    new_fcf_record))
2007 			continue;
2008 		if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
2009 			/*
2010 			 * If the vlan bit map does not have the bit set for the
2011 			 * vlan id to be used, then it is not a match.
2012 			 */
2013 			if (!(new_fcf_record->vlan_bitmap
2014 				[conn_entry->conn_rec.vlan_tag / 8] &
2015 				(1 << (conn_entry->conn_rec.vlan_tag % 8))))
2016 				continue;
2017 		}
2018 
2019 		/*
2020 		 * If connection record does not support any addressing mode,
2021 		 * skip the FCF record.
2022 		 */
2023 		if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
2024 			& (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
2025 			continue;
2026 
2027 		/*
2028 		 * Check if the connection record specifies a required
2029 		 * addressing mode.
2030 		 */
2031 		if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
2032 			!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
2033 
2034 			/*
2035 			 * If SPMA required but FCF not support this continue.
2036 			 */
2037 			if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
2038 				!(bf_get(lpfc_fcf_record_mac_addr_prov,
2039 					new_fcf_record) & LPFC_FCF_SPMA))
2040 				continue;
2041 
2042 			/*
2043 			 * If FPMA required but FCF not support this continue.
2044 			 */
2045 			if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
2046 				!(bf_get(lpfc_fcf_record_mac_addr_prov,
2047 				new_fcf_record) & LPFC_FCF_FPMA))
2048 				continue;
2049 		}
2050 
2051 		/*
2052 		 * This fcf record matches filtering criteria.
2053 		 */
2054 		if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
2055 			*boot_flag = 1;
2056 		else
2057 			*boot_flag = 0;
2058 
2059 		/*
2060 		 * If user did not specify any addressing mode, or if the
2061 		 * preferred addressing mode specified by user is not supported
2062 		 * by FCF, allow fabric to pick the addressing mode.
2063 		 */
2064 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
2065 				new_fcf_record);
2066 		/*
2067 		 * If the user specified a required address mode, assign that
2068 		 * address mode
2069 		 */
2070 		if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
2071 			(!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
2072 			*addr_mode = (conn_entry->conn_rec.flags &
2073 				FCFCNCT_AM_SPMA) ?
2074 				LPFC_FCF_SPMA : LPFC_FCF_FPMA;
2075 		/*
2076 		 * If the user specified a preferred address mode, use the
2077 		 * addr mode only if FCF support the addr_mode.
2078 		 */
2079 		else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
2080 			(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
2081 			(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
2082 			(*addr_mode & LPFC_FCF_SPMA))
2083 				*addr_mode = LPFC_FCF_SPMA;
2084 		else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
2085 			(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
2086 			!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
2087 			(*addr_mode & LPFC_FCF_FPMA))
2088 				*addr_mode = LPFC_FCF_FPMA;
2089 
2090 		/* If matching connect list has a vlan id, use it */
2091 		if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
2092 			*vlan_id = conn_entry->conn_rec.vlan_tag;
2093 		/*
2094 		 * If no vlan id is specified in connect list, use the vlan id
2095 		 * in the FCF record
2096 		 */
2097 		else if (fcf_vlan_id)
2098 			*vlan_id = fcf_vlan_id;
2099 		else
2100 			*vlan_id = LPFC_FCOE_NULL_VID;
2101 
2102 		return 1;
2103 	}
2104 
2105 	return 0;
2106 }
2107 
2108 /**
2109  * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
2110  * @phba: pointer to lpfc hba data structure.
2111  * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
2112  *
2113  * This function check if there is any fcoe event pending while driver
2114  * scan FCF entries. If there is any pending event, it will restart the
2115  * FCF saning and return 1 else return 0.
2116  */
2117 int
2118 lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
2119 {
2120 	/*
2121 	 * If the Link is up and no FCoE events while in the
2122 	 * FCF discovery, no need to restart FCF discovery.
2123 	 */
2124 	if ((phba->link_state  >= LPFC_LINK_UP) &&
2125 	    (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
2126 		return 0;
2127 
2128 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2129 			"2768 Pending link or FCF event during current "
2130 			"handling of the previous event: link_state:x%x, "
2131 			"evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
2132 			phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
2133 			phba->fcoe_eventtag);
2134 
2135 	spin_lock_irq(&phba->hbalock);
2136 	phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
2137 	spin_unlock_irq(&phba->hbalock);
2138 
2139 	if (phba->link_state >= LPFC_LINK_UP) {
2140 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2141 				"2780 Restart FCF table scan due to "
2142 				"pending FCF event:evt_tag_at_scan:x%x, "
2143 				"evt_tag_current:x%x\n",
2144 				phba->fcoe_eventtag_at_fcf_scan,
2145 				phba->fcoe_eventtag);
2146 		lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
2147 	} else {
2148 		/*
2149 		 * Do not continue FCF discovery and clear FCF_TS_INPROG
2150 		 * flag
2151 		 */
2152 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2153 				"2833 Stop FCF discovery process due to link "
2154 				"state change (x%x)\n", phba->link_state);
2155 		spin_lock_irq(&phba->hbalock);
2156 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
2157 		phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
2158 		spin_unlock_irq(&phba->hbalock);
2159 	}
2160 
2161 	/* Unregister the currently registered FCF if required */
2162 	if (unreg_fcf) {
2163 		spin_lock_irq(&phba->hbalock);
2164 		phba->fcf.fcf_flag &= ~FCF_REGISTERED;
2165 		spin_unlock_irq(&phba->hbalock);
2166 		lpfc_sli4_unregister_fcf(phba);
2167 	}
2168 	return 1;
2169 }
2170 
2171 /**
2172  * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
2173  * @phba: pointer to lpfc hba data structure.
2174  * @fcf_cnt: number of eligible fcf record seen so far.
2175  *
2176  * This function makes an running random selection decision on FCF record to
2177  * use through a sequence of @fcf_cnt eligible FCF records with equal
2178  * probability. To perform integer manunipulation of random numbers with
2179  * size unit32_t, a 16-bit random number returned from get_random_u16() is
2180  * taken as the random random number generated.
2181  *
2182  * Returns true when outcome is for the newly read FCF record should be
2183  * chosen; otherwise, return false when outcome is for keeping the previously
2184  * chosen FCF record.
2185  **/
2186 static bool
2187 lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
2188 {
2189 	uint32_t rand_num;
2190 
2191 	/* Get 16-bit uniform random number */
2192 	rand_num = get_random_u16();
2193 
2194 	/* Decision with probability 1/fcf_cnt */
2195 	if ((fcf_cnt * rand_num) < 0xFFFF)
2196 		return true;
2197 	else
2198 		return false;
2199 }
2200 
2201 /**
2202  * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
2203  * @phba: pointer to lpfc hba data structure.
2204  * @mboxq: pointer to mailbox object.
2205  * @next_fcf_index: pointer to holder of next fcf index.
2206  *
2207  * This routine parses the non-embedded fcf mailbox command by performing the
2208  * necessarily error checking, non-embedded read FCF record mailbox command
2209  * SGE parsing, and endianness swapping.
2210  *
2211  * Returns the pointer to the new FCF record in the non-embedded mailbox
2212  * command DMA memory if successfully, other NULL.
2213  */
2214 static struct fcf_record *
2215 lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
2216 			     uint16_t *next_fcf_index)
2217 {
2218 	void *virt_addr;
2219 	struct lpfc_mbx_sge sge;
2220 	struct lpfc_mbx_read_fcf_tbl *read_fcf;
2221 	uint32_t shdr_status, shdr_add_status, if_type;
2222 	union lpfc_sli4_cfg_shdr *shdr;
2223 	struct fcf_record *new_fcf_record;
2224 
2225 	/* Get the first SGE entry from the non-embedded DMA memory. This
2226 	 * routine only uses a single SGE.
2227 	 */
2228 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
2229 	if (unlikely(!mboxq->sge_array)) {
2230 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2231 				"2524 Failed to get the non-embedded SGE "
2232 				"virtual address\n");
2233 		return NULL;
2234 	}
2235 	virt_addr = mboxq->sge_array->addr[0];
2236 
2237 	shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
2238 	lpfc_sli_pcimem_bcopy(shdr, shdr,
2239 			      sizeof(union lpfc_sli4_cfg_shdr));
2240 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2241 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
2242 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2243 	if (shdr_status || shdr_add_status) {
2244 		if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
2245 					if_type == LPFC_SLI_INTF_IF_TYPE_2)
2246 			lpfc_printf_log(phba, KERN_ERR,
2247 					LOG_TRACE_EVENT,
2248 					"2726 READ_FCF_RECORD Indicates empty "
2249 					"FCF table.\n");
2250 		else
2251 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2252 					"2521 READ_FCF_RECORD mailbox failed "
2253 					"with status x%x add_status x%x, "
2254 					"mbx\n", shdr_status, shdr_add_status);
2255 		return NULL;
2256 	}
2257 
2258 	/* Interpreting the returned information of the FCF record */
2259 	read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
2260 	lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
2261 			      sizeof(struct lpfc_mbx_read_fcf_tbl));
2262 	*next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
2263 	new_fcf_record = (struct fcf_record *)(virt_addr +
2264 			  sizeof(struct lpfc_mbx_read_fcf_tbl));
2265 	lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
2266 				offsetof(struct fcf_record, vlan_bitmap));
2267 	new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
2268 	new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
2269 
2270 	return new_fcf_record;
2271 }
2272 
2273 /**
2274  * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
2275  * @phba: pointer to lpfc hba data structure.
2276  * @fcf_record: pointer to the fcf record.
2277  * @vlan_id: the lowest vlan identifier associated to this fcf record.
2278  * @next_fcf_index: the index to the next fcf record in hba's fcf table.
2279  *
2280  * This routine logs the detailed FCF record if the LOG_FIP loggin is
2281  * enabled.
2282  **/
2283 static void
2284 lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
2285 			      struct fcf_record *fcf_record,
2286 			      uint16_t vlan_id,
2287 			      uint16_t next_fcf_index)
2288 {
2289 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2290 			"2764 READ_FCF_RECORD:\n"
2291 			"\tFCF_Index     : x%x\n"
2292 			"\tFCF_Avail     : x%x\n"
2293 			"\tFCF_Valid     : x%x\n"
2294 			"\tFCF_SOL       : x%x\n"
2295 			"\tFIP_Priority  : x%x\n"
2296 			"\tMAC_Provider  : x%x\n"
2297 			"\tLowest VLANID : x%x\n"
2298 			"\tFCF_MAC Addr  : x%x:%x:%x:%x:%x:%x\n"
2299 			"\tFabric_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
2300 			"\tSwitch_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
2301 			"\tNext_FCF_Index: x%x\n",
2302 			bf_get(lpfc_fcf_record_fcf_index, fcf_record),
2303 			bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
2304 			bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
2305 			bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
2306 			fcf_record->fip_priority,
2307 			bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
2308 			vlan_id,
2309 			bf_get(lpfc_fcf_record_mac_0, fcf_record),
2310 			bf_get(lpfc_fcf_record_mac_1, fcf_record),
2311 			bf_get(lpfc_fcf_record_mac_2, fcf_record),
2312 			bf_get(lpfc_fcf_record_mac_3, fcf_record),
2313 			bf_get(lpfc_fcf_record_mac_4, fcf_record),
2314 			bf_get(lpfc_fcf_record_mac_5, fcf_record),
2315 			bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
2316 			bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
2317 			bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
2318 			bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
2319 			bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
2320 			bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
2321 			bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
2322 			bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
2323 			bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
2324 			bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
2325 			bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
2326 			bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
2327 			bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
2328 			bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
2329 			bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
2330 			bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
2331 			next_fcf_index);
2332 }
2333 
2334 /**
2335  * lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
2336  * @phba: pointer to lpfc hba data structure.
2337  * @fcf_rec: pointer to an existing FCF record.
2338  * @new_fcf_record: pointer to a new FCF record.
2339  * @new_vlan_id: vlan id from the new FCF record.
2340  *
2341  * This function performs matching test of a new FCF record against an existing
2342  * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
2343  * will not be used as part of the FCF record matching criteria.
2344  *
2345  * Returns true if all the fields matching, otherwise returns false.
2346  */
2347 static bool
2348 lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
2349 			   struct lpfc_fcf_rec *fcf_rec,
2350 			   struct fcf_record *new_fcf_record,
2351 			   uint16_t new_vlan_id)
2352 {
2353 	if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
2354 		if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
2355 			return false;
2356 	if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
2357 		return false;
2358 	if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
2359 		return false;
2360 	if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
2361 		return false;
2362 	if (fcf_rec->priority != new_fcf_record->fip_priority)
2363 		return false;
2364 	return true;
2365 }
2366 
2367 /**
2368  * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
2369  * @vport: Pointer to vport object.
2370  * @fcf_index: index to next fcf.
2371  *
2372  * This function processing the roundrobin fcf failover to next fcf index.
2373  * When this function is invoked, there will be a current fcf registered
2374  * for flogi.
2375  * Return: 0 for continue retrying flogi on currently registered fcf;
2376  *         1 for stop flogi on currently registered fcf;
2377  */
2378 int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
2379 {
2380 	struct lpfc_hba *phba = vport->phba;
2381 	int rc;
2382 
2383 	if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
2384 		spin_lock_irq(&phba->hbalock);
2385 		if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2386 			spin_unlock_irq(&phba->hbalock);
2387 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2388 					"2872 Devloss tmo with no eligible "
2389 					"FCF, unregister in-use FCF (x%x) "
2390 					"and rescan FCF table\n",
2391 					phba->fcf.current_rec.fcf_indx);
2392 			lpfc_unregister_fcf_rescan(phba);
2393 			goto stop_flogi_current_fcf;
2394 		}
2395 		/* Mark the end to FLOGI roundrobin failover */
2396 		phba->hba_flag &= ~FCF_RR_INPROG;
2397 		/* Allow action to new fcf asynchronous event */
2398 		phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2399 		spin_unlock_irq(&phba->hbalock);
2400 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2401 				"2865 No FCF available, stop roundrobin FCF "
2402 				"failover and change port state:x%x/x%x\n",
2403 				phba->pport->port_state, LPFC_VPORT_UNKNOWN);
2404 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2405 
2406 		if (!phba->fcf.fcf_redisc_attempted) {
2407 			lpfc_unregister_fcf(phba);
2408 
2409 			rc = lpfc_sli4_redisc_fcf_table(phba);
2410 			if (!rc) {
2411 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2412 						"3195 Rediscover FCF table\n");
2413 				phba->fcf.fcf_redisc_attempted = 1;
2414 				lpfc_sli4_clear_fcf_rr_bmask(phba);
2415 			} else {
2416 				lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2417 						"3196 Rediscover FCF table "
2418 						"failed. Status:x%x\n", rc);
2419 			}
2420 		} else {
2421 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2422 					"3197 Already rediscover FCF table "
2423 					"attempted. No more retry\n");
2424 		}
2425 		goto stop_flogi_current_fcf;
2426 	} else {
2427 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
2428 				"2794 Try FLOGI roundrobin FCF failover to "
2429 				"(x%x)\n", fcf_index);
2430 		rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
2431 		if (rc)
2432 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
2433 					"2761 FLOGI roundrobin FCF failover "
2434 					"failed (rc:x%x) to read FCF (x%x)\n",
2435 					rc, phba->fcf.current_rec.fcf_indx);
2436 		else
2437 			goto stop_flogi_current_fcf;
2438 	}
2439 	return 0;
2440 
2441 stop_flogi_current_fcf:
2442 	lpfc_can_disctmo(vport);
2443 	return 1;
2444 }
2445 
2446 /**
2447  * lpfc_sli4_fcf_pri_list_del
2448  * @phba: pointer to lpfc hba data structure.
2449  * @fcf_index: the index of the fcf record to delete
2450  * This routine checks the on list flag of the fcf_index to be deleted.
2451  * If it is one the list then it is removed from the list, and the flag
2452  * is cleared. This routine grab the hbalock before removing the fcf
2453  * record from the list.
2454  **/
2455 static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2456 			uint16_t fcf_index)
2457 {
2458 	struct lpfc_fcf_pri *new_fcf_pri;
2459 
2460 	new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2461 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2462 		"3058 deleting idx x%x pri x%x flg x%x\n",
2463 		fcf_index, new_fcf_pri->fcf_rec.priority,
2464 		 new_fcf_pri->fcf_rec.flag);
2465 	spin_lock_irq(&phba->hbalock);
2466 	if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2467 		if (phba->fcf.current_rec.priority ==
2468 				new_fcf_pri->fcf_rec.priority)
2469 			phba->fcf.eligible_fcf_cnt--;
2470 		list_del_init(&new_fcf_pri->list);
2471 		new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2472 	}
2473 	spin_unlock_irq(&phba->hbalock);
2474 }
2475 
2476 /**
2477  * lpfc_sli4_set_fcf_flogi_fail
2478  * @phba: pointer to lpfc hba data structure.
2479  * @fcf_index: the index of the fcf record to update
2480  * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2481  * flag so the round robin selection for the particular priority level
2482  * will try a different fcf record that does not have this bit set.
2483  * If the fcf record is re-read for any reason this flag is cleared brfore
2484  * adding it to the priority list.
2485  **/
2486 void
2487 lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2488 {
2489 	struct lpfc_fcf_pri *new_fcf_pri;
2490 	new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2491 	spin_lock_irq(&phba->hbalock);
2492 	new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2493 	spin_unlock_irq(&phba->hbalock);
2494 }
2495 
2496 /**
2497  * lpfc_sli4_fcf_pri_list_add
2498  * @phba: pointer to lpfc hba data structure.
2499  * @fcf_index: the index of the fcf record to add
2500  * @new_fcf_record: pointer to a new FCF record.
2501  * This routine checks the priority of the fcf_index to be added.
2502  * If it is a lower priority than the current head of the fcf_pri list
2503  * then it is added to the list in the right order.
2504  * If it is the same priority as the current head of the list then it
2505  * is added to the head of the list and its bit in the rr_bmask is set.
2506  * If the fcf_index to be added is of a higher priority than the current
2507  * head of the list then the rr_bmask is cleared, its bit is set in the
2508  * rr_bmask and it is added to the head of the list.
2509  * returns:
2510  * 0=success 1=failure
2511  **/
2512 static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2513 	uint16_t fcf_index,
2514 	struct fcf_record *new_fcf_record)
2515 {
2516 	uint16_t current_fcf_pri;
2517 	uint16_t last_index;
2518 	struct lpfc_fcf_pri *fcf_pri;
2519 	struct lpfc_fcf_pri *next_fcf_pri;
2520 	struct lpfc_fcf_pri *new_fcf_pri;
2521 	int ret;
2522 
2523 	new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2524 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2525 		"3059 adding idx x%x pri x%x flg x%x\n",
2526 		fcf_index, new_fcf_record->fip_priority,
2527 		 new_fcf_pri->fcf_rec.flag);
2528 	spin_lock_irq(&phba->hbalock);
2529 	if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2530 		list_del_init(&new_fcf_pri->list);
2531 	new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2532 	new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2533 	if (list_empty(&phba->fcf.fcf_pri_list)) {
2534 		list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2535 		ret = lpfc_sli4_fcf_rr_index_set(phba,
2536 				new_fcf_pri->fcf_rec.fcf_index);
2537 		goto out;
2538 	}
2539 
2540 	last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2541 				LPFC_SLI4_FCF_TBL_INDX_MAX);
2542 	if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2543 		ret = 0; /* Empty rr list */
2544 		goto out;
2545 	}
2546 	current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2547 	if (new_fcf_pri->fcf_rec.priority <=  current_fcf_pri) {
2548 		list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2549 		if (new_fcf_pri->fcf_rec.priority <  current_fcf_pri) {
2550 			memset(phba->fcf.fcf_rr_bmask, 0,
2551 				sizeof(*phba->fcf.fcf_rr_bmask));
2552 			/* fcfs_at_this_priority_level = 1; */
2553 			phba->fcf.eligible_fcf_cnt = 1;
2554 		} else
2555 			/* fcfs_at_this_priority_level++; */
2556 			phba->fcf.eligible_fcf_cnt++;
2557 		ret = lpfc_sli4_fcf_rr_index_set(phba,
2558 				new_fcf_pri->fcf_rec.fcf_index);
2559 		goto out;
2560 	}
2561 
2562 	list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2563 				&phba->fcf.fcf_pri_list, list) {
2564 		if (new_fcf_pri->fcf_rec.priority <=
2565 				fcf_pri->fcf_rec.priority) {
2566 			if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2567 				list_add(&new_fcf_pri->list,
2568 						&phba->fcf.fcf_pri_list);
2569 			else
2570 				list_add(&new_fcf_pri->list,
2571 					 &((struct lpfc_fcf_pri *)
2572 					fcf_pri->list.prev)->list);
2573 			ret = 0;
2574 			goto out;
2575 		} else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2576 			|| new_fcf_pri->fcf_rec.priority <
2577 				next_fcf_pri->fcf_rec.priority) {
2578 			list_add(&new_fcf_pri->list, &fcf_pri->list);
2579 			ret = 0;
2580 			goto out;
2581 		}
2582 		if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2583 			continue;
2584 
2585 	}
2586 	ret = 1;
2587 out:
2588 	/* we use = instead of |= to clear the FLOGI_FAILED flag. */
2589 	new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2590 	spin_unlock_irq(&phba->hbalock);
2591 	return ret;
2592 }
2593 
2594 /**
2595  * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2596  * @phba: pointer to lpfc hba data structure.
2597  * @mboxq: pointer to mailbox object.
2598  *
2599  * This function iterates through all the fcf records available in
2600  * HBA and chooses the optimal FCF record for discovery. After finding
2601  * the FCF for discovery it registers the FCF record and kicks start
2602  * discovery.
2603  * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2604  * use an FCF record which matches fabric name and mac address of the
2605  * currently used FCF record.
2606  * If the driver supports only one FCF, it will try to use the FCF record
2607  * used by BOOT_BIOS.
2608  */
2609 void
2610 lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2611 {
2612 	struct fcf_record *new_fcf_record;
2613 	uint32_t boot_flag, addr_mode;
2614 	uint16_t fcf_index, next_fcf_index;
2615 	struct lpfc_fcf_rec *fcf_rec = NULL;
2616 	uint16_t vlan_id = LPFC_FCOE_NULL_VID;
2617 	bool select_new_fcf;
2618 	int rc;
2619 
2620 	/* If there is pending FCoE event restart FCF table scan */
2621 	if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
2622 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
2623 		return;
2624 	}
2625 
2626 	/* Parse the FCF record from the non-embedded mailbox command */
2627 	new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2628 						      &next_fcf_index);
2629 	if (!new_fcf_record) {
2630 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2631 				"2765 Mailbox command READ_FCF_RECORD "
2632 				"failed to retrieve a FCF record.\n");
2633 		/* Let next new FCF event trigger fast failover */
2634 		spin_lock_irq(&phba->hbalock);
2635 		phba->hba_flag &= ~FCF_TS_INPROG;
2636 		spin_unlock_irq(&phba->hbalock);
2637 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
2638 		return;
2639 	}
2640 
2641 	/* Check the FCF record against the connection list */
2642 	rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2643 				      &addr_mode, &vlan_id);
2644 
2645 	/* Log the FCF record information if turned on */
2646 	lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2647 				      next_fcf_index);
2648 
2649 	/*
2650 	 * If the fcf record does not match with connect list entries
2651 	 * read the next entry; otherwise, this is an eligible FCF
2652 	 * record for roundrobin FCF failover.
2653 	 */
2654 	if (!rc) {
2655 		lpfc_sli4_fcf_pri_list_del(phba,
2656 					bf_get(lpfc_fcf_record_fcf_index,
2657 					       new_fcf_record));
2658 		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2659 				"2781 FCF (x%x) failed connection "
2660 				"list check: (x%x/x%x/%x)\n",
2661 				bf_get(lpfc_fcf_record_fcf_index,
2662 				       new_fcf_record),
2663 				bf_get(lpfc_fcf_record_fcf_avail,
2664 				       new_fcf_record),
2665 				bf_get(lpfc_fcf_record_fcf_valid,
2666 				       new_fcf_record),
2667 				bf_get(lpfc_fcf_record_fcf_sol,
2668 				       new_fcf_record));
2669 		if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2670 		    lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2671 		    new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
2672 			if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2673 			    phba->fcf.current_rec.fcf_indx) {
2674 				lpfc_printf_log(phba, KERN_ERR,
2675 						LOG_TRACE_EVENT,
2676 					"2862 FCF (x%x) matches property "
2677 					"of in-use FCF (x%x)\n",
2678 					bf_get(lpfc_fcf_record_fcf_index,
2679 					       new_fcf_record),
2680 					phba->fcf.current_rec.fcf_indx);
2681 				goto read_next_fcf;
2682 			}
2683 			/*
2684 			 * In case the current in-use FCF record becomes
2685 			 * invalid/unavailable during FCF discovery that
2686 			 * was not triggered by fast FCF failover process,
2687 			 * treat it as fast FCF failover.
2688 			 */
2689 			if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2690 			    !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2691 				lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2692 						"2835 Invalid in-use FCF "
2693 						"(x%x), enter FCF failover "
2694 						"table scan.\n",
2695 						phba->fcf.current_rec.fcf_indx);
2696 				spin_lock_irq(&phba->hbalock);
2697 				phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2698 				spin_unlock_irq(&phba->hbalock);
2699 				lpfc_sli4_mbox_cmd_free(phba, mboxq);
2700 				lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2701 						LPFC_FCOE_FCF_GET_FIRST);
2702 				return;
2703 			}
2704 		}
2705 		goto read_next_fcf;
2706 	} else {
2707 		fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2708 		rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2709 							new_fcf_record);
2710 		if (rc)
2711 			goto read_next_fcf;
2712 	}
2713 
2714 	/*
2715 	 * If this is not the first FCF discovery of the HBA, use last
2716 	 * FCF record for the discovery. The condition that a rescan
2717 	 * matches the in-use FCF record: fabric name, switch name, mac
2718 	 * address, and vlan_id.
2719 	 */
2720 	spin_lock_irq(&phba->hbalock);
2721 	if (phba->fcf.fcf_flag & FCF_IN_USE) {
2722 		if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2723 			lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2724 		    new_fcf_record, vlan_id)) {
2725 			if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2726 			    phba->fcf.current_rec.fcf_indx) {
2727 				phba->fcf.fcf_flag |= FCF_AVAILABLE;
2728 				if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2729 					/* Stop FCF redisc wait timer */
2730 					__lpfc_sli4_stop_fcf_redisc_wait_timer(
2731 									phba);
2732 				else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2733 					/* Fast failover, mark completed */
2734 					phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2735 				spin_unlock_irq(&phba->hbalock);
2736 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2737 						"2836 New FCF matches in-use "
2738 						"FCF (x%x), port_state:x%x, "
2739 						"fc_flag:x%lx\n",
2740 						phba->fcf.current_rec.fcf_indx,
2741 						phba->pport->port_state,
2742 						phba->pport->fc_flag);
2743 				goto out;
2744 			} else
2745 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2746 					"2863 New FCF (x%x) matches "
2747 					"property of in-use FCF (x%x)\n",
2748 					bf_get(lpfc_fcf_record_fcf_index,
2749 					       new_fcf_record),
2750 					phba->fcf.current_rec.fcf_indx);
2751 		}
2752 		/*
2753 		 * Read next FCF record from HBA searching for the matching
2754 		 * with in-use record only if not during the fast failover
2755 		 * period. In case of fast failover period, it shall try to
2756 		 * determine whether the FCF record just read should be the
2757 		 * next candidate.
2758 		 */
2759 		if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2760 			spin_unlock_irq(&phba->hbalock);
2761 			goto read_next_fcf;
2762 		}
2763 	}
2764 	/*
2765 	 * Update on failover FCF record only if it's in FCF fast-failover
2766 	 * period; otherwise, update on current FCF record.
2767 	 */
2768 	if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2769 		fcf_rec = &phba->fcf.failover_rec;
2770 	else
2771 		fcf_rec = &phba->fcf.current_rec;
2772 
2773 	if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2774 		/*
2775 		 * If the driver FCF record does not have boot flag
2776 		 * set and new hba fcf record has boot flag set, use
2777 		 * the new hba fcf record.
2778 		 */
2779 		if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2780 			/* Choose this FCF record */
2781 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2782 					"2837 Update current FCF record "
2783 					"(x%x) with new FCF record (x%x)\n",
2784 					fcf_rec->fcf_indx,
2785 					bf_get(lpfc_fcf_record_fcf_index,
2786 					new_fcf_record));
2787 			__lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2788 					addr_mode, vlan_id, BOOT_ENABLE);
2789 			spin_unlock_irq(&phba->hbalock);
2790 			goto read_next_fcf;
2791 		}
2792 		/*
2793 		 * If the driver FCF record has boot flag set and the
2794 		 * new hba FCF record does not have boot flag, read
2795 		 * the next FCF record.
2796 		 */
2797 		if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
2798 			spin_unlock_irq(&phba->hbalock);
2799 			goto read_next_fcf;
2800 		}
2801 		/*
2802 		 * If the new hba FCF record has lower priority value
2803 		 * than the driver FCF record, use the new record.
2804 		 */
2805 		if (new_fcf_record->fip_priority < fcf_rec->priority) {
2806 			/* Choose the new FCF record with lower priority */
2807 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2808 					"2838 Update current FCF record "
2809 					"(x%x) with new FCF record (x%x)\n",
2810 					fcf_rec->fcf_indx,
2811 					bf_get(lpfc_fcf_record_fcf_index,
2812 					       new_fcf_record));
2813 			__lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2814 					addr_mode, vlan_id, 0);
2815 			/* Reset running random FCF selection count */
2816 			phba->fcf.eligible_fcf_cnt = 1;
2817 		} else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2818 			/* Update running random FCF selection count */
2819 			phba->fcf.eligible_fcf_cnt++;
2820 			select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2821 						phba->fcf.eligible_fcf_cnt);
2822 			if (select_new_fcf) {
2823 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2824 					"2839 Update current FCF record "
2825 					"(x%x) with new FCF record (x%x)\n",
2826 					fcf_rec->fcf_indx,
2827 					bf_get(lpfc_fcf_record_fcf_index,
2828 					       new_fcf_record));
2829 				/* Choose the new FCF by random selection */
2830 				__lpfc_update_fcf_record(phba, fcf_rec,
2831 							 new_fcf_record,
2832 							 addr_mode, vlan_id, 0);
2833 			}
2834 		}
2835 		spin_unlock_irq(&phba->hbalock);
2836 		goto read_next_fcf;
2837 	}
2838 	/*
2839 	 * This is the first suitable FCF record, choose this record for
2840 	 * initial best-fit FCF.
2841 	 */
2842 	if (fcf_rec) {
2843 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2844 				"2840 Update initial FCF candidate "
2845 				"with FCF (x%x)\n",
2846 				bf_get(lpfc_fcf_record_fcf_index,
2847 				       new_fcf_record));
2848 		__lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2849 					 addr_mode, vlan_id, (boot_flag ?
2850 					 BOOT_ENABLE : 0));
2851 		phba->fcf.fcf_flag |= FCF_AVAILABLE;
2852 		/* Setup initial running random FCF selection count */
2853 		phba->fcf.eligible_fcf_cnt = 1;
2854 	}
2855 	spin_unlock_irq(&phba->hbalock);
2856 	goto read_next_fcf;
2857 
2858 read_next_fcf:
2859 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
2860 	if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2861 		if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2862 			/*
2863 			 * Case of FCF fast failover scan
2864 			 */
2865 
2866 			/*
2867 			 * It has not found any suitable FCF record, cancel
2868 			 * FCF scan inprogress, and do nothing
2869 			 */
2870 			if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
2871 				lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2872 					       "2782 No suitable FCF found: "
2873 					       "(x%x/x%x)\n",
2874 					       phba->fcoe_eventtag_at_fcf_scan,
2875 					       bf_get(lpfc_fcf_record_fcf_index,
2876 						      new_fcf_record));
2877 				spin_lock_irq(&phba->hbalock);
2878 				if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2879 					phba->hba_flag &= ~FCF_TS_INPROG;
2880 					spin_unlock_irq(&phba->hbalock);
2881 					/* Unregister in-use FCF and rescan */
2882 					lpfc_printf_log(phba, KERN_INFO,
2883 							LOG_FIP,
2884 							"2864 On devloss tmo "
2885 							"unreg in-use FCF and "
2886 							"rescan FCF table\n");
2887 					lpfc_unregister_fcf_rescan(phba);
2888 					return;
2889 				}
2890 				/*
2891 				 * Let next new FCF event trigger fast failover
2892 				 */
2893 				phba->hba_flag &= ~FCF_TS_INPROG;
2894 				spin_unlock_irq(&phba->hbalock);
2895 				return;
2896 			}
2897 			/*
2898 			 * It has found a suitable FCF record that is not
2899 			 * the same as in-use FCF record, unregister the
2900 			 * in-use FCF record, replace the in-use FCF record
2901 			 * with the new FCF record, mark FCF fast failover
2902 			 * completed, and then start register the new FCF
2903 			 * record.
2904 			 */
2905 
2906 			/* Unregister the current in-use FCF record */
2907 			lpfc_unregister_fcf(phba);
2908 
2909 			/* Replace in-use record with the new record */
2910 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2911 					"2842 Replace in-use FCF (x%x) "
2912 					"with failover FCF (x%x)\n",
2913 					phba->fcf.current_rec.fcf_indx,
2914 					phba->fcf.failover_rec.fcf_indx);
2915 			memcpy(&phba->fcf.current_rec,
2916 			       &phba->fcf.failover_rec,
2917 			       sizeof(struct lpfc_fcf_rec));
2918 			/*
2919 			 * Mark the fast FCF failover rediscovery completed
2920 			 * and the start of the first round of the roundrobin
2921 			 * FCF failover.
2922 			 */
2923 			spin_lock_irq(&phba->hbalock);
2924 			phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2925 			spin_unlock_irq(&phba->hbalock);
2926 			/* Register to the new FCF record */
2927 			lpfc_register_fcf(phba);
2928 		} else {
2929 			/*
2930 			 * In case of transaction period to fast FCF failover,
2931 			 * do nothing when search to the end of the FCF table.
2932 			 */
2933 			if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2934 			    (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2935 				return;
2936 
2937 			if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2938 				phba->fcf.fcf_flag & FCF_IN_USE) {
2939 				/*
2940 				 * In case the current in-use FCF record no
2941 				 * longer existed during FCF discovery that
2942 				 * was not triggered by fast FCF failover
2943 				 * process, treat it as fast FCF failover.
2944 				 */
2945 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2946 						"2841 In-use FCF record (x%x) "
2947 						"not reported, entering fast "
2948 						"FCF failover mode scanning.\n",
2949 						phba->fcf.current_rec.fcf_indx);
2950 				spin_lock_irq(&phba->hbalock);
2951 				phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2952 				spin_unlock_irq(&phba->hbalock);
2953 				lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2954 						LPFC_FCOE_FCF_GET_FIRST);
2955 				return;
2956 			}
2957 			/* Register to the new FCF record */
2958 			lpfc_register_fcf(phba);
2959 		}
2960 	} else
2961 		lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
2962 	return;
2963 
2964 out:
2965 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
2966 	lpfc_register_fcf(phba);
2967 
2968 	return;
2969 }
2970 
2971 /**
2972  * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
2973  * @phba: pointer to lpfc hba data structure.
2974  * @mboxq: pointer to mailbox object.
2975  *
2976  * This is the callback function for FLOGI failure roundrobin FCF failover
2977  * read FCF record mailbox command from the eligible FCF record bmask for
2978  * performing the failover. If the FCF read back is not valid/available, it
2979  * fails through to retrying FLOGI to the currently registered FCF again.
2980  * Otherwise, if the FCF read back is valid and available, it will set the
2981  * newly read FCF record to the failover FCF record, unregister currently
2982  * registered FCF record, copy the failover FCF record to the current
2983  * FCF record, and then register the current FCF record before proceeding
2984  * to trying FLOGI on the new failover FCF.
2985  */
2986 void
2987 lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2988 {
2989 	struct fcf_record *new_fcf_record;
2990 	uint32_t boot_flag, addr_mode;
2991 	uint16_t next_fcf_index, fcf_index;
2992 	uint16_t current_fcf_index;
2993 	uint16_t vlan_id = LPFC_FCOE_NULL_VID;
2994 	int rc;
2995 
2996 	/* If link state is not up, stop the roundrobin failover process */
2997 	if (phba->link_state < LPFC_LINK_UP) {
2998 		spin_lock_irq(&phba->hbalock);
2999 		phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
3000 		phba->hba_flag &= ~FCF_RR_INPROG;
3001 		spin_unlock_irq(&phba->hbalock);
3002 		goto out;
3003 	}
3004 
3005 	/* Parse the FCF record from the non-embedded mailbox command */
3006 	new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
3007 						      &next_fcf_index);
3008 	if (!new_fcf_record) {
3009 		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
3010 				"2766 Mailbox command READ_FCF_RECORD "
3011 				"failed to retrieve a FCF record. "
3012 				"hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
3013 				phba->fcf.fcf_flag);
3014 		lpfc_unregister_fcf_rescan(phba);
3015 		goto out;
3016 	}
3017 
3018 	/* Get the needed parameters from FCF record */
3019 	rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
3020 				      &addr_mode, &vlan_id);
3021 
3022 	/* Log the FCF record information if turned on */
3023 	lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
3024 				      next_fcf_index);
3025 
3026 	fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
3027 	if (!rc) {
3028 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3029 				"2848 Remove ineligible FCF (x%x) from "
3030 				"from roundrobin bmask\n", fcf_index);
3031 		/* Clear roundrobin bmask bit for ineligible FCF */
3032 		lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
3033 		/* Perform next round of roundrobin FCF failover */
3034 		fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
3035 		rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
3036 		if (rc)
3037 			goto out;
3038 		goto error_out;
3039 	}
3040 
3041 	if (fcf_index == phba->fcf.current_rec.fcf_indx) {
3042 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3043 				"2760 Perform FLOGI roundrobin FCF failover: "
3044 				"FCF (x%x) back to FCF (x%x)\n",
3045 				phba->fcf.current_rec.fcf_indx, fcf_index);
3046 		/* Wait 500 ms before retrying FLOGI to current FCF */
3047 		msleep(500);
3048 		lpfc_issue_init_vfi(phba->pport);
3049 		goto out;
3050 	}
3051 
3052 	/* Upload new FCF record to the failover FCF record */
3053 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3054 			"2834 Update current FCF (x%x) with new FCF (x%x)\n",
3055 			phba->fcf.failover_rec.fcf_indx, fcf_index);
3056 	spin_lock_irq(&phba->hbalock);
3057 	__lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
3058 				 new_fcf_record, addr_mode, vlan_id,
3059 				 (boot_flag ? BOOT_ENABLE : 0));
3060 	spin_unlock_irq(&phba->hbalock);
3061 
3062 	current_fcf_index = phba->fcf.current_rec.fcf_indx;
3063 
3064 	/* Unregister the current in-use FCF record */
3065 	lpfc_unregister_fcf(phba);
3066 
3067 	/* Replace in-use record with the new record */
3068 	memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
3069 	       sizeof(struct lpfc_fcf_rec));
3070 
3071 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3072 			"2783 Perform FLOGI roundrobin FCF failover: FCF "
3073 			"(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
3074 
3075 error_out:
3076 	lpfc_register_fcf(phba);
3077 out:
3078 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
3079 }
3080 
3081 /**
3082  * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
3083  * @phba: pointer to lpfc hba data structure.
3084  * @mboxq: pointer to mailbox object.
3085  *
3086  * This is the callback function of read FCF record mailbox command for
3087  * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
3088  * failover when a new FCF event happened. If the FCF read back is
3089  * valid/available and it passes the connection list check, it updates
3090  * the bmask for the eligible FCF record for roundrobin failover.
3091  */
3092 void
3093 lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3094 {
3095 	struct fcf_record *new_fcf_record;
3096 	uint32_t boot_flag, addr_mode;
3097 	uint16_t fcf_index, next_fcf_index;
3098 	uint16_t vlan_id =  LPFC_FCOE_NULL_VID;
3099 	int rc;
3100 
3101 	/* If link state is not up, no need to proceed */
3102 	if (phba->link_state < LPFC_LINK_UP)
3103 		goto out;
3104 
3105 	/* If FCF discovery period is over, no need to proceed */
3106 	if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
3107 		goto out;
3108 
3109 	/* Parse the FCF record from the non-embedded mailbox command */
3110 	new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
3111 						      &next_fcf_index);
3112 	if (!new_fcf_record) {
3113 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3114 				"2767 Mailbox command READ_FCF_RECORD "
3115 				"failed to retrieve a FCF record.\n");
3116 		goto out;
3117 	}
3118 
3119 	/* Check the connection list for eligibility */
3120 	rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
3121 				      &addr_mode, &vlan_id);
3122 
3123 	/* Log the FCF record information if turned on */
3124 	lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
3125 				      next_fcf_index);
3126 
3127 	if (!rc)
3128 		goto out;
3129 
3130 	/* Update the eligible FCF record index bmask */
3131 	fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
3132 
3133 	rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
3134 
3135 out:
3136 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
3137 }
3138 
3139 /**
3140  * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
3141  * @phba: pointer to lpfc hba data structure.
3142  * @mboxq: pointer to mailbox data structure.
3143  *
3144  * This function handles completion of init vfi mailbox command.
3145  */
3146 static void
3147 lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3148 {
3149 	struct lpfc_vport *vport = mboxq->vport;
3150 
3151 	/*
3152 	 * VFI not supported on interface type 0, just do the flogi
3153 	 * Also continue if the VFI is in use - just use the same one.
3154 	 */
3155 	if (mboxq->u.mb.mbxStatus &&
3156 	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3157 			LPFC_SLI_INTF_IF_TYPE_0) &&
3158 	    mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
3159 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3160 				 "2891 Init VFI mailbox failed 0x%x\n",
3161 				 mboxq->u.mb.mbxStatus);
3162 		mempool_free(mboxq, phba->mbox_mem_pool);
3163 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3164 		return;
3165 	}
3166 
3167 	lpfc_initial_flogi(vport);
3168 	mempool_free(mboxq, phba->mbox_mem_pool);
3169 	return;
3170 }
3171 
3172 /**
3173  * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
3174  * @vport: pointer to lpfc_vport data structure.
3175  *
3176  * This function issue a init_vfi mailbox command to initialize the VFI and
3177  * VPI for the physical port.
3178  */
3179 void
3180 lpfc_issue_init_vfi(struct lpfc_vport *vport)
3181 {
3182 	LPFC_MBOXQ_t *mboxq;
3183 	int rc;
3184 	struct lpfc_hba *phba = vport->phba;
3185 
3186 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3187 	if (!mboxq) {
3188 		lpfc_printf_vlog(vport, KERN_ERR,
3189 			LOG_TRACE_EVENT, "2892 Failed to allocate "
3190 			"init_vfi mailbox\n");
3191 		return;
3192 	}
3193 	lpfc_init_vfi(mboxq, vport);
3194 	mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
3195 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
3196 	if (rc == MBX_NOT_FINISHED) {
3197 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3198 				 "2893 Failed to issue init_vfi mailbox\n");
3199 		mempool_free(mboxq, vport->phba->mbox_mem_pool);
3200 	}
3201 }
3202 
3203 /**
3204  * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
3205  * @phba: pointer to lpfc hba data structure.
3206  * @mboxq: pointer to mailbox data structure.
3207  *
3208  * This function handles completion of init vpi mailbox command.
3209  */
3210 void
3211 lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3212 {
3213 	struct lpfc_vport *vport = mboxq->vport;
3214 	struct lpfc_nodelist *ndlp;
3215 
3216 	if (mboxq->u.mb.mbxStatus) {
3217 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3218 				 "2609 Init VPI mailbox failed 0x%x\n",
3219 				 mboxq->u.mb.mbxStatus);
3220 		mempool_free(mboxq, phba->mbox_mem_pool);
3221 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3222 		return;
3223 	}
3224 	clear_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag);
3225 
3226 	/* If this port is physical port or FDISC is done, do reg_vpi */
3227 	if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
3228 			ndlp = lpfc_findnode_did(vport, Fabric_DID);
3229 			if (!ndlp)
3230 				lpfc_printf_vlog(vport, KERN_ERR,
3231 					LOG_TRACE_EVENT,
3232 					"2731 Cannot find fabric "
3233 					"controller node\n");
3234 			else
3235 				lpfc_register_new_vport(phba, vport, ndlp);
3236 			mempool_free(mboxq, phba->mbox_mem_pool);
3237 			return;
3238 	}
3239 
3240 	if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
3241 		lpfc_initial_fdisc(vport);
3242 	else {
3243 		lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
3244 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3245 				 "2606 No NPIV Fabric support\n");
3246 	}
3247 	mempool_free(mboxq, phba->mbox_mem_pool);
3248 	return;
3249 }
3250 
3251 /**
3252  * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
3253  * @vport: pointer to lpfc_vport data structure.
3254  *
3255  * This function issue a init_vpi mailbox command to initialize
3256  * VPI for the vport.
3257  */
3258 void
3259 lpfc_issue_init_vpi(struct lpfc_vport *vport)
3260 {
3261 	LPFC_MBOXQ_t *mboxq;
3262 	int rc, vpi;
3263 
3264 	if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
3265 		vpi = lpfc_alloc_vpi(vport->phba);
3266 		if (!vpi) {
3267 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3268 					 "3303 Failed to obtain vport vpi\n");
3269 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3270 			return;
3271 		}
3272 		vport->vpi = vpi;
3273 	}
3274 
3275 	mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
3276 	if (!mboxq) {
3277 		lpfc_printf_vlog(vport, KERN_ERR,
3278 			LOG_TRACE_EVENT, "2607 Failed to allocate "
3279 			"init_vpi mailbox\n");
3280 		return;
3281 	}
3282 	lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
3283 	mboxq->vport = vport;
3284 	mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
3285 	rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
3286 	if (rc == MBX_NOT_FINISHED) {
3287 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3288 				 "2608 Failed to issue init_vpi mailbox\n");
3289 		mempool_free(mboxq, vport->phba->mbox_mem_pool);
3290 	}
3291 }
3292 
3293 /**
3294  * lpfc_start_fdiscs - send fdiscs for each vports on this port.
3295  * @phba: pointer to lpfc hba data structure.
3296  *
3297  * This function loops through the list of vports on the @phba and issues an
3298  * FDISC if possible.
3299  */
3300 void
3301 lpfc_start_fdiscs(struct lpfc_hba *phba)
3302 {
3303 	struct lpfc_vport **vports;
3304 	int i;
3305 
3306 	vports = lpfc_create_vport_work_array(phba);
3307 	if (vports != NULL) {
3308 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3309 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
3310 				continue;
3311 			/* There are no vpi for this vport */
3312 			if (vports[i]->vpi > phba->max_vpi) {
3313 				lpfc_vport_set_state(vports[i],
3314 						     FC_VPORT_FAILED);
3315 				continue;
3316 			}
3317 			if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3318 				lpfc_vport_set_state(vports[i],
3319 						     FC_VPORT_LINKDOWN);
3320 				continue;
3321 			}
3322 			if (test_bit(FC_VPORT_NEEDS_INIT_VPI,
3323 				     &vports[i]->fc_flag)) {
3324 				lpfc_issue_init_vpi(vports[i]);
3325 				continue;
3326 			}
3327 			if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
3328 				lpfc_initial_fdisc(vports[i]);
3329 			else {
3330 				lpfc_vport_set_state(vports[i],
3331 						     FC_VPORT_NO_FABRIC_SUPP);
3332 				lpfc_printf_vlog(vports[i], KERN_ERR,
3333 						 LOG_TRACE_EVENT,
3334 						 "0259 No NPIV "
3335 						 "Fabric support\n");
3336 			}
3337 		}
3338 	}
3339 	lpfc_destroy_vport_work_array(phba, vports);
3340 }
3341 
3342 void
3343 lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3344 {
3345 	struct lpfc_vport *vport = mboxq->vport;
3346 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3347 
3348 	/*
3349 	 * VFI not supported for interface type 0, so ignore any mailbox
3350 	 * error (except VFI in use) and continue with the discovery.
3351 	 */
3352 	if (mboxq->u.mb.mbxStatus &&
3353 	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3354 			LPFC_SLI_INTF_IF_TYPE_0) &&
3355 	    mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
3356 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3357 				 "2018 REG_VFI mbxStatus error x%x "
3358 				 "HBA state x%x\n",
3359 				 mboxq->u.mb.mbxStatus, vport->port_state);
3360 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3361 			/* FLOGI failed, use loop map to make discovery list */
3362 			lpfc_disc_list_loopmap(vport);
3363 			/* Start discovery */
3364 			lpfc_disc_start(vport);
3365 			goto out_free_mem;
3366 		}
3367 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3368 		goto out_free_mem;
3369 	}
3370 
3371 	/* If the VFI is already registered, there is nothing else to do
3372 	 * Unless this was a VFI update and we are in PT2PT mode, then
3373 	 * we should drop through to set the port state to ready.
3374 	 */
3375 	if (test_bit(FC_VFI_REGISTERED, &vport->fc_flag))
3376 		if (!(phba->sli_rev == LPFC_SLI_REV4 &&
3377 		      test_bit(FC_PT2PT, &vport->fc_flag)))
3378 			goto out_free_mem;
3379 
3380 	/* The VPI is implicitly registered when the VFI is registered */
3381 	set_bit(FC_VFI_REGISTERED, &vport->fc_flag);
3382 	clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
3383 	clear_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag);
3384 	spin_lock_irq(shost->host_lock);
3385 	vport->vpi_state |= LPFC_VPI_REGISTERED;
3386 	spin_unlock_irq(shost->host_lock);
3387 
3388 	/* In case SLI4 FC loopback test, we are ready */
3389 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
3390 	    (phba->link_flag & LS_LOOPBACK_MODE)) {
3391 		phba->link_state = LPFC_HBA_READY;
3392 		goto out_free_mem;
3393 	}
3394 
3395 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3396 			 "3313 cmpl reg vfi  port_state:%x fc_flag:%lx "
3397 			 "myDid:%x alpacnt:%d LinkState:%x topology:%x\n",
3398 			 vport->port_state, vport->fc_flag, vport->fc_myDID,
3399 			 vport->phba->alpa_map[0],
3400 			 phba->link_state, phba->fc_topology);
3401 
3402 	if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
3403 		/*
3404 		 * For private loop or for NPort pt2pt,
3405 		 * just start discovery and we are done.
3406 		 */
3407 		if (test_bit(FC_PT2PT, &vport->fc_flag) ||
3408 		    (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
3409 		    !test_bit(FC_PUBLIC_LOOP, &vport->fc_flag))) {
3410 
3411 			/* Use loop map to make discovery list */
3412 			lpfc_disc_list_loopmap(vport);
3413 			/* Start discovery */
3414 			if (test_bit(FC_PT2PT, &vport->fc_flag))
3415 				vport->port_state = LPFC_VPORT_READY;
3416 			else
3417 				lpfc_disc_start(vport);
3418 		} else {
3419 			lpfc_start_fdiscs(phba);
3420 			lpfc_do_scr_ns_plogi(phba, vport);
3421 		}
3422 	}
3423 
3424 out_free_mem:
3425 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
3426 }
3427 
3428 static void
3429 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3430 {
3431 	MAILBOX_t *mb = &pmb->u.mb;
3432 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3433 	struct lpfc_vport  *vport = pmb->vport;
3434 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3435 	struct serv_parm *sp = &vport->fc_sparam;
3436 	uint32_t ed_tov;
3437 
3438 	/* Check for error */
3439 	if (mb->mbxStatus) {
3440 		/* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
3441 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3442 				 "0319 READ_SPARAM mbxStatus error x%x "
3443 				 "hba state x%x>\n",
3444 				 mb->mbxStatus, vport->port_state);
3445 		lpfc_linkdown(phba);
3446 		goto out;
3447 	}
3448 
3449 	memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
3450 	       sizeof (struct serv_parm));
3451 
3452 	ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3453 	if (sp->cmn.edtovResolution)	/* E_D_TOV ticks are in nanoseconds */
3454 		ed_tov = (ed_tov + 999999) / 1000000;
3455 
3456 	phba->fc_edtov = ed_tov;
3457 	phba->fc_ratov = (2 * ed_tov) / 1000;
3458 	if (phba->fc_ratov < FF_DEF_RATOV) {
3459 		/* RA_TOV should be atleast 10sec for initial flogi */
3460 		phba->fc_ratov = FF_DEF_RATOV;
3461 	}
3462 
3463 	lpfc_update_vport_wwn(vport);
3464 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
3465 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
3466 		memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3467 		memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3468 	}
3469 
3470 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
3471 
3472 	/* Check if sending the FLOGI is being deferred to after we get
3473 	 * up to date CSPs from MBX_READ_SPARAM.
3474 	 */
3475 	if (phba->hba_flag & HBA_DEFER_FLOGI) {
3476 		lpfc_initial_flogi(vport);
3477 		phba->hba_flag &= ~HBA_DEFER_FLOGI;
3478 	}
3479 	return;
3480 
3481 out:
3482 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
3483 	lpfc_issue_clear_la(phba, vport);
3484 }
3485 
3486 static void
3487 lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
3488 {
3489 	struct lpfc_vport *vport = phba->pport;
3490 	LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
3491 	int i;
3492 	int rc;
3493 	struct fcf_record *fcf_record;
3494 	unsigned long iflags;
3495 
3496 	spin_lock_irqsave(&phba->hbalock, iflags);
3497 	phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3498 
3499 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3500 		switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3501 		case LPFC_LINK_SPEED_1GHZ:
3502 		case LPFC_LINK_SPEED_2GHZ:
3503 		case LPFC_LINK_SPEED_4GHZ:
3504 		case LPFC_LINK_SPEED_8GHZ:
3505 		case LPFC_LINK_SPEED_10GHZ:
3506 		case LPFC_LINK_SPEED_16GHZ:
3507 		case LPFC_LINK_SPEED_32GHZ:
3508 		case LPFC_LINK_SPEED_64GHZ:
3509 		case LPFC_LINK_SPEED_128GHZ:
3510 		case LPFC_LINK_SPEED_256GHZ:
3511 			break;
3512 		default:
3513 			phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3514 			break;
3515 		}
3516 	}
3517 
3518 	if (phba->fc_topology &&
3519 	    phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3520 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3521 				"3314 Toplogy changed was 0x%x is 0x%x\n",
3522 				phba->fc_topology,
3523 				bf_get(lpfc_mbx_read_top_topology, la));
3524 		phba->fc_topology_changed = 1;
3525 	}
3526 
3527 	phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
3528 	phba->link_flag &= ~(LS_NPIV_FAB_SUPPORTED | LS_CT_VEN_RPA);
3529 
3530 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3531 		phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
3532 
3533 		/* if npiv is enabled and this adapter supports npiv log
3534 		 * a message that npiv is not supported in this topology
3535 		 */
3536 		if (phba->cfg_enable_npiv && phba->max_vpi)
3537 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3538 				"1309 Link Up Event npiv not supported in loop "
3539 				"topology\n");
3540 				/* Get Loop Map information */
3541 		if (bf_get(lpfc_mbx_read_top_il, la))
3542 			set_bit(FC_LBIT, &vport->fc_flag);
3543 
3544 		vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3545 		i = la->lilpBde64.tus.f.bdeSize;
3546 
3547 		if (i == 0) {
3548 			phba->alpa_map[0] = 0;
3549 		} else {
3550 			if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
3551 				int numalpa, j, k;
3552 				union {
3553 					uint8_t pamap[16];
3554 					struct {
3555 						uint32_t wd1;
3556 						uint32_t wd2;
3557 						uint32_t wd3;
3558 						uint32_t wd4;
3559 					} pa;
3560 				} un;
3561 				numalpa = phba->alpa_map[0];
3562 				j = 0;
3563 				while (j < numalpa) {
3564 					memset(un.pamap, 0, 16);
3565 					for (k = 1; j < numalpa; k++) {
3566 						un.pamap[k - 1] =
3567 							phba->alpa_map[j + 1];
3568 						j++;
3569 						if (k == 16)
3570 							break;
3571 					}
3572 					/* Link Up Event ALPA map */
3573 					lpfc_printf_log(phba,
3574 							KERN_WARNING,
3575 							LOG_LINK_EVENT,
3576 							"1304 Link Up Event "
3577 							"ALPA map Data: x%x "
3578 							"x%x x%x x%x\n",
3579 							un.pa.wd1, un.pa.wd2,
3580 							un.pa.wd3, un.pa.wd4);
3581 				}
3582 			}
3583 		}
3584 	} else {
3585 		if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
3586 			if (phba->max_vpi && phba->cfg_enable_npiv &&
3587 			   (phba->sli_rev >= LPFC_SLI_REV3))
3588 				phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3589 		}
3590 		vport->fc_myDID = phba->fc_pref_DID;
3591 		set_bit(FC_LBIT, &vport->fc_flag);
3592 	}
3593 	spin_unlock_irqrestore(&phba->hbalock, iflags);
3594 
3595 	lpfc_linkup(phba);
3596 	sparam_mbox = NULL;
3597 
3598 	sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3599 	if (!sparam_mbox)
3600 		goto out;
3601 
3602 	rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3603 	if (rc) {
3604 		mempool_free(sparam_mbox, phba->mbox_mem_pool);
3605 		goto out;
3606 	}
3607 	sparam_mbox->vport = vport;
3608 	sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3609 	rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3610 	if (rc == MBX_NOT_FINISHED) {
3611 		lpfc_mbox_rsrc_cleanup(phba, sparam_mbox, MBOX_THD_UNLOCKED);
3612 		goto out;
3613 	}
3614 
3615 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3616 		cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3617 		if (!cfglink_mbox)
3618 			goto out;
3619 		vport->port_state = LPFC_LOCAL_CFG_LINK;
3620 		lpfc_config_link(phba, cfglink_mbox);
3621 		cfglink_mbox->vport = vport;
3622 		cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
3623 		rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
3624 		if (rc == MBX_NOT_FINISHED) {
3625 			mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3626 			goto out;
3627 		}
3628 	} else {
3629 		vport->port_state = LPFC_VPORT_UNKNOWN;
3630 		/*
3631 		 * Add the driver's default FCF record at FCF index 0 now. This
3632 		 * is phase 1 implementation that support FCF index 0 and driver
3633 		 * defaults.
3634 		 */
3635 		if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
3636 			fcf_record = kzalloc(sizeof(struct fcf_record),
3637 					GFP_KERNEL);
3638 			if (unlikely(!fcf_record)) {
3639 				lpfc_printf_log(phba, KERN_ERR,
3640 					LOG_TRACE_EVENT,
3641 					"2554 Could not allocate memory for "
3642 					"fcf record\n");
3643 				rc = -ENODEV;
3644 				goto out;
3645 			}
3646 
3647 			lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3648 						LPFC_FCOE_FCF_DEF_INDEX);
3649 			rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3650 			if (unlikely(rc)) {
3651 				lpfc_printf_log(phba, KERN_ERR,
3652 					LOG_TRACE_EVENT,
3653 					"2013 Could not manually add FCF "
3654 					"record 0, status %d\n", rc);
3655 				rc = -ENODEV;
3656 				kfree(fcf_record);
3657 				goto out;
3658 			}
3659 			kfree(fcf_record);
3660 		}
3661 		/*
3662 		 * The driver is expected to do FIP/FCF. Call the port
3663 		 * and get the FCF Table.
3664 		 */
3665 		spin_lock_irqsave(&phba->hbalock, iflags);
3666 		if (phba->hba_flag & FCF_TS_INPROG) {
3667 			spin_unlock_irqrestore(&phba->hbalock, iflags);
3668 			return;
3669 		}
3670 		/* This is the initial FCF discovery scan */
3671 		phba->fcf.fcf_flag |= FCF_INIT_DISC;
3672 		spin_unlock_irqrestore(&phba->hbalock, iflags);
3673 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3674 				"2778 Start FCF table scan at linkup\n");
3675 		rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3676 						     LPFC_FCOE_FCF_GET_FIRST);
3677 		if (rc) {
3678 			spin_lock_irqsave(&phba->hbalock, iflags);
3679 			phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3680 			spin_unlock_irqrestore(&phba->hbalock, iflags);
3681 			goto out;
3682 		}
3683 		/* Reset FCF roundrobin bmask for new discovery */
3684 		lpfc_sli4_clear_fcf_rr_bmask(phba);
3685 	}
3686 
3687 	/* Prepare for LINK up registrations */
3688 	memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
3689 	scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
3690 		  init_utsname()->nodename);
3691 	return;
3692 out:
3693 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3694 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3695 			 "0263 Discovery Mailbox error: state: 0x%x : x%px x%px\n",
3696 			 vport->port_state, sparam_mbox, cfglink_mbox);
3697 	lpfc_issue_clear_la(phba, vport);
3698 	return;
3699 }
3700 
3701 static void
3702 lpfc_enable_la(struct lpfc_hba *phba)
3703 {
3704 	uint32_t control;
3705 	struct lpfc_sli *psli = &phba->sli;
3706 	spin_lock_irq(&phba->hbalock);
3707 	psli->sli_flag |= LPFC_PROCESS_LA;
3708 	if (phba->sli_rev <= LPFC_SLI_REV3) {
3709 		control = readl(phba->HCregaddr);
3710 		control |= HC_LAINT_ENA;
3711 		writel(control, phba->HCregaddr);
3712 		readl(phba->HCregaddr); /* flush */
3713 	}
3714 	spin_unlock_irq(&phba->hbalock);
3715 }
3716 
3717 static void
3718 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3719 {
3720 	lpfc_linkdown(phba);
3721 	lpfc_enable_la(phba);
3722 	lpfc_unregister_unused_fcf(phba);
3723 	/* turn on Link Attention interrupts - no CLEAR_LA needed */
3724 }
3725 
3726 
3727 /*
3728  * This routine handles processing a READ_TOPOLOGY mailbox
3729  * command upon completion. It is setup in the LPFC_MBOXQ
3730  * as the completion routine when the command is
3731  * handed off to the SLI layer. SLI4 only.
3732  */
3733 void
3734 lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3735 {
3736 	struct lpfc_vport *vport = pmb->vport;
3737 	struct lpfc_mbx_read_top *la;
3738 	struct lpfc_sli_ring *pring;
3739 	MAILBOX_t *mb = &pmb->u.mb;
3740 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3741 	uint8_t attn_type;
3742 
3743 	/* Unblock ELS traffic */
3744 	pring = lpfc_phba_elsring(phba);
3745 	if (pring)
3746 		pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3747 
3748 	/* Check for error */
3749 	if (mb->mbxStatus) {
3750 		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3751 				"1307 READ_LA mbox error x%x state x%x\n",
3752 				mb->mbxStatus, vport->port_state);
3753 		lpfc_mbx_issue_link_down(phba);
3754 		phba->link_state = LPFC_HBA_ERROR;
3755 		goto lpfc_mbx_cmpl_read_topology_free_mbuf;
3756 	}
3757 
3758 	la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3759 	attn_type = bf_get(lpfc_mbx_read_top_att_type, la);
3760 
3761 	memcpy(&phba->alpa_map[0], mp->virt, 128);
3762 
3763 	if (bf_get(lpfc_mbx_read_top_pb, la))
3764 		set_bit(FC_BYPASSED_MODE, &vport->fc_flag);
3765 	else
3766 		clear_bit(FC_BYPASSED_MODE, &vport->fc_flag);
3767 
3768 	if (phba->fc_eventTag <= la->eventTag) {
3769 		phba->fc_stat.LinkMultiEvent++;
3770 		if (attn_type == LPFC_ATT_LINK_UP)
3771 			if (phba->fc_eventTag != 0)
3772 				lpfc_linkdown(phba);
3773 	}
3774 
3775 	phba->fc_eventTag = la->eventTag;
3776 	phba->link_events++;
3777 	if (attn_type == LPFC_ATT_LINK_UP) {
3778 		phba->fc_stat.LinkUp++;
3779 		if (phba->link_flag & LS_LOOPBACK_MODE) {
3780 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3781 					"1306 Link Up Event in loop back mode "
3782 					"x%x received Data: x%x x%x x%x x%x\n",
3783 					la->eventTag, phba->fc_eventTag,
3784 					bf_get(lpfc_mbx_read_top_alpa_granted,
3785 					       la),
3786 					bf_get(lpfc_mbx_read_top_link_spd, la),
3787 					phba->alpa_map[0]);
3788 		} else {
3789 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3790 					"1303 Link Up Event x%x received "
3791 					"Data: x%x x%x x%x x%x x%x\n",
3792 					la->eventTag, phba->fc_eventTag,
3793 					bf_get(lpfc_mbx_read_top_alpa_granted,
3794 					       la),
3795 					bf_get(lpfc_mbx_read_top_link_spd, la),
3796 					phba->alpa_map[0],
3797 					bf_get(lpfc_mbx_read_top_fa, la));
3798 		}
3799 		lpfc_mbx_process_link_up(phba, la);
3800 
3801 		if (phba->cmf_active_mode != LPFC_CFG_OFF)
3802 			lpfc_cmf_signal_init(phba);
3803 
3804 		if (phba->lmt & LMT_64Gb)
3805 			lpfc_read_lds_params(phba);
3806 
3807 	} else if (attn_type == LPFC_ATT_LINK_DOWN ||
3808 		   attn_type == LPFC_ATT_UNEXP_WWPN) {
3809 		phba->fc_stat.LinkDown++;
3810 		if (phba->link_flag & LS_LOOPBACK_MODE)
3811 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3812 				"1308 Link Down Event in loop back mode "
3813 				"x%x received "
3814 				"Data: x%x x%x x%lx\n",
3815 				la->eventTag, phba->fc_eventTag,
3816 				phba->pport->port_state, vport->fc_flag);
3817 		else if (attn_type == LPFC_ATT_UNEXP_WWPN)
3818 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3819 				"1313 Link Down Unexpected FA WWPN Event x%x "
3820 				"received Data: x%x x%x x%lx x%x\n",
3821 				la->eventTag, phba->fc_eventTag,
3822 				phba->pport->port_state, vport->fc_flag,
3823 				bf_get(lpfc_mbx_read_top_fa, la));
3824 		else
3825 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3826 				"1305 Link Down Event x%x received "
3827 				"Data: x%x x%x x%lx x%x\n",
3828 				la->eventTag, phba->fc_eventTag,
3829 				phba->pport->port_state, vport->fc_flag,
3830 				bf_get(lpfc_mbx_read_top_fa, la));
3831 		lpfc_mbx_issue_link_down(phba);
3832 	}
3833 
3834 	if ((phba->sli_rev < LPFC_SLI_REV4) &&
3835 	    bf_get(lpfc_mbx_read_top_fa, la))
3836 		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3837 				"1311 fa %d\n",
3838 				bf_get(lpfc_mbx_read_top_fa, la));
3839 
3840 lpfc_mbx_cmpl_read_topology_free_mbuf:
3841 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
3842 }
3843 
3844 /*
3845  * This routine handles processing a REG_LOGIN mailbox
3846  * command upon completion. It is setup in the LPFC_MBOXQ
3847  * as the completion routine when the command is
3848  * handed off to the SLI layer.
3849  */
3850 void
3851 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3852 {
3853 	struct lpfc_vport  *vport = pmb->vport;
3854 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3855 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3856 
3857 	/* The driver calls the state machine with the pmb pointer
3858 	 * but wants to make sure a stale ctx_buf isn't acted on.
3859 	 * The ctx_buf is restored later and cleaned up.
3860 	 */
3861 	pmb->ctx_buf = NULL;
3862 	pmb->ctx_ndlp = NULL;
3863 
3864 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NODE | LOG_DISCOVERY,
3865 			 "0002 rpi:%x DID:%x flg:%x %d x%px\n",
3866 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3867 			 kref_read(&ndlp->kref),
3868 			 ndlp);
3869 	if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3870 		ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3871 
3872 	if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3873 	    ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
3874 		/* We rcvd a rscn after issuing this
3875 		 * mbox reg login, we may have cycled
3876 		 * back through the state and be
3877 		 * back at reg login state so this
3878 		 * mbox needs to be ignored becase
3879 		 * there is another reg login in
3880 		 * process.
3881 		 */
3882 		spin_lock_irq(&ndlp->lock);
3883 		ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3884 		spin_unlock_irq(&ndlp->lock);
3885 
3886 		/*
3887 		 * We cannot leave the RPI registered because
3888 		 * if we go thru discovery again for this ndlp
3889 		 * a subsequent REG_RPI will fail.
3890 		 */
3891 		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3892 		lpfc_unreg_rpi(vport, ndlp);
3893 	}
3894 
3895 	/* Call state machine */
3896 	lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
3897 	pmb->ctx_buf = mp;
3898 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
3899 
3900 	/* decrement the node reference count held for this callback
3901 	 * function.
3902 	 */
3903 	lpfc_nlp_put(ndlp);
3904 
3905 	return;
3906 }
3907 
3908 static void
3909 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3910 {
3911 	MAILBOX_t *mb = &pmb->u.mb;
3912 	struct lpfc_vport *vport = pmb->vport;
3913 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3914 
3915 	switch (mb->mbxStatus) {
3916 	case 0x0011:
3917 	case 0x0020:
3918 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3919 				 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3920 				 mb->mbxStatus);
3921 		break;
3922 	/* If VPI is busy, reset the HBA */
3923 	case 0x9700:
3924 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3925 			"2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3926 			vport->vpi, mb->mbxStatus);
3927 		if (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
3928 			lpfc_workq_post_event(phba, NULL, NULL,
3929 				LPFC_EVT_RESET_HBA);
3930 	}
3931 
3932 	set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
3933 	spin_lock_irq(shost->host_lock);
3934 	vport->vpi_state &= ~LPFC_VPI_REGISTERED;
3935 	spin_unlock_irq(shost->host_lock);
3936 	mempool_free(pmb, phba->mbox_mem_pool);
3937 	lpfc_cleanup_vports_rrqs(vport, NULL);
3938 	/*
3939 	 * This shost reference might have been taken at the beginning of
3940 	 * lpfc_vport_delete()
3941 	 */
3942 	if (test_bit(FC_UNLOADING, &vport->load_flag) && vport != phba->pport)
3943 		scsi_host_put(shost);
3944 }
3945 
3946 int
3947 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3948 {
3949 	struct lpfc_hba  *phba = vport->phba;
3950 	LPFC_MBOXQ_t *mbox;
3951 	int rc;
3952 
3953 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3954 	if (!mbox)
3955 		return 1;
3956 
3957 	lpfc_unreg_vpi(phba, vport->vpi, mbox);
3958 	mbox->vport = vport;
3959 	mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
3960 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3961 	if (rc == MBX_NOT_FINISHED) {
3962 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3963 				 "1800 Could not issue unreg_vpi\n");
3964 		mempool_free(mbox, phba->mbox_mem_pool);
3965 		return rc;
3966 	}
3967 	return 0;
3968 }
3969 
3970 static void
3971 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3972 {
3973 	struct lpfc_vport *vport = pmb->vport;
3974 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3975 	MAILBOX_t *mb = &pmb->u.mb;
3976 
3977 	switch (mb->mbxStatus) {
3978 	case 0x0011:
3979 	case 0x9601:
3980 	case 0x9602:
3981 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3982 				 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3983 				 mb->mbxStatus);
3984 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3985 		clear_bit(FC_FABRIC, &vport->fc_flag);
3986 		clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
3987 		vport->fc_myDID = 0;
3988 
3989 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3990 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3991 			if (phba->nvmet_support)
3992 				lpfc_nvmet_update_targetport(phba);
3993 			else
3994 				lpfc_nvme_update_localport(vport);
3995 		}
3996 		goto out;
3997 	}
3998 
3999 	clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
4000 	spin_lock_irq(shost->host_lock);
4001 	vport->vpi_state |= LPFC_VPI_REGISTERED;
4002 	spin_unlock_irq(shost->host_lock);
4003 	vport->num_disc_nodes = 0;
4004 	/* go thru NPR list and issue ELS PLOGIs */
4005 	if (atomic_read(&vport->fc_npr_cnt))
4006 		lpfc_els_disc_plogi(vport);
4007 
4008 	if (!vport->num_disc_nodes) {
4009 		clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
4010 		lpfc_can_disctmo(vport);
4011 	}
4012 	vport->port_state = LPFC_VPORT_READY;
4013 
4014 out:
4015 	mempool_free(pmb, phba->mbox_mem_pool);
4016 	return;
4017 }
4018 
4019 /**
4020  * lpfc_create_static_vport - Read HBA config region to create static vports.
4021  * @phba: pointer to lpfc hba data structure.
4022  *
4023  * This routine issue a DUMP mailbox command for config region 22 to get
4024  * the list of static vports to be created. The function create vports
4025  * based on the information returned from the HBA.
4026  **/
4027 void
4028 lpfc_create_static_vport(struct lpfc_hba *phba)
4029 {
4030 	LPFC_MBOXQ_t *pmb = NULL;
4031 	MAILBOX_t *mb;
4032 	struct static_vport_info *vport_info;
4033 	int mbx_wait_rc = 0, i;
4034 	struct fc_vport_identifiers vport_id;
4035 	struct fc_vport *new_fc_vport;
4036 	struct Scsi_Host *shost;
4037 	struct lpfc_vport *vport;
4038 	uint16_t offset = 0;
4039 	uint8_t *vport_buff;
4040 	struct lpfc_dmabuf *mp;
4041 	uint32_t byte_count = 0;
4042 
4043 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4044 	if (!pmb) {
4045 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4046 				"0542 lpfc_create_static_vport failed to"
4047 				" allocate mailbox memory\n");
4048 		return;
4049 	}
4050 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
4051 	mb = &pmb->u.mb;
4052 
4053 	vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
4054 	if (!vport_info) {
4055 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4056 				"0543 lpfc_create_static_vport failed to"
4057 				" allocate vport_info\n");
4058 		mempool_free(pmb, phba->mbox_mem_pool);
4059 		return;
4060 	}
4061 
4062 	vport_buff = (uint8_t *) vport_info;
4063 	do {
4064 		/* While loop iteration forces a free dma buffer from
4065 		 * the previous loop because the mbox is reused and
4066 		 * the dump routine is a single-use construct.
4067 		 */
4068 		if (pmb->ctx_buf) {
4069 			mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
4070 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
4071 			kfree(mp);
4072 			pmb->ctx_buf = NULL;
4073 		}
4074 		if (lpfc_dump_static_vport(phba, pmb, offset))
4075 			goto out;
4076 
4077 		pmb->vport = phba->pport;
4078 		mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
4079 							LPFC_MBOX_TMO);
4080 
4081 		if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
4082 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4083 				"0544 lpfc_create_static_vport failed to"
4084 				" issue dump mailbox command ret 0x%x "
4085 				"status 0x%x\n",
4086 				mbx_wait_rc, mb->mbxStatus);
4087 			goto out;
4088 		}
4089 
4090 		if (phba->sli_rev == LPFC_SLI_REV4) {
4091 			byte_count = pmb->u.mqe.un.mb_words[5];
4092 			mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
4093 			if (byte_count > sizeof(struct static_vport_info) -
4094 					offset)
4095 				byte_count = sizeof(struct static_vport_info)
4096 					- offset;
4097 			memcpy(vport_buff + offset, mp->virt, byte_count);
4098 			offset += byte_count;
4099 		} else {
4100 			if (mb->un.varDmp.word_cnt >
4101 				sizeof(struct static_vport_info) - offset)
4102 				mb->un.varDmp.word_cnt =
4103 					sizeof(struct static_vport_info)
4104 						- offset;
4105 			byte_count = mb->un.varDmp.word_cnt;
4106 			lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
4107 				vport_buff + offset,
4108 				byte_count);
4109 
4110 			offset += byte_count;
4111 		}
4112 
4113 	} while (byte_count &&
4114 		offset < sizeof(struct static_vport_info));
4115 
4116 
4117 	if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
4118 		((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
4119 			!= VPORT_INFO_REV)) {
4120 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4121 				"0545 lpfc_create_static_vport bad"
4122 				" information header 0x%x 0x%x\n",
4123 				le32_to_cpu(vport_info->signature),
4124 				le32_to_cpu(vport_info->rev) &
4125 				VPORT_INFO_REV_MASK);
4126 
4127 		goto out;
4128 	}
4129 
4130 	shost = lpfc_shost_from_vport(phba->pport);
4131 
4132 	for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
4133 		memset(&vport_id, 0, sizeof(vport_id));
4134 		vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
4135 		vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
4136 		if (!vport_id.port_name || !vport_id.node_name)
4137 			continue;
4138 
4139 		vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
4140 		vport_id.vport_type = FC_PORTTYPE_NPIV;
4141 		vport_id.disable = false;
4142 		new_fc_vport = fc_vport_create(shost, 0, &vport_id);
4143 
4144 		if (!new_fc_vport) {
4145 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4146 				"0546 lpfc_create_static_vport failed to"
4147 				" create vport\n");
4148 			continue;
4149 		}
4150 
4151 		vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
4152 		vport->vport_flag |= STATIC_VPORT;
4153 	}
4154 
4155 out:
4156 	kfree(vport_info);
4157 	if (mbx_wait_rc != MBX_TIMEOUT)
4158 		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4159 }
4160 
4161 /*
4162  * This routine handles processing a Fabric REG_LOGIN mailbox
4163  * command upon completion. It is setup in the LPFC_MBOXQ
4164  * as the completion routine when the command is
4165  * handed off to the SLI layer.
4166  */
4167 void
4168 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4169 {
4170 	struct lpfc_vport *vport = pmb->vport;
4171 	MAILBOX_t *mb = &pmb->u.mb;
4172 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4173 
4174 	pmb->ctx_ndlp = NULL;
4175 
4176 	if (mb->mbxStatus) {
4177 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4178 				 "0258 Register Fabric login error: 0x%x\n",
4179 				 mb->mbxStatus);
4180 		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4181 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4182 			/* FLOGI failed, use loop map to make discovery list */
4183 			lpfc_disc_list_loopmap(vport);
4184 
4185 			/* Start discovery */
4186 			lpfc_disc_start(vport);
4187 			/* Decrement the reference count to ndlp after the
4188 			 * reference to the ndlp are done.
4189 			 */
4190 			lpfc_nlp_put(ndlp);
4191 			return;
4192 		}
4193 
4194 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4195 		/* Decrement the reference count to ndlp after the reference
4196 		 * to the ndlp are done.
4197 		 */
4198 		lpfc_nlp_put(ndlp);
4199 		return;
4200 	}
4201 
4202 	if (phba->sli_rev < LPFC_SLI_REV4)
4203 		ndlp->nlp_rpi = mb->un.varWords[0];
4204 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4205 	ndlp->nlp_type |= NLP_FABRIC;
4206 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4207 
4208 	if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
4209 		/* when physical port receive logo donot start
4210 		 * vport discovery */
4211 		if (!test_and_clear_bit(FC_LOGO_RCVD_DID_CHNG, &vport->fc_flag))
4212 			lpfc_start_fdiscs(phba);
4213 		lpfc_do_scr_ns_plogi(phba, vport);
4214 	}
4215 
4216 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4217 
4218 	/* Drop the reference count from the mbox at the end after
4219 	 * all the current reference to the ndlp have been done.
4220 	 */
4221 	lpfc_nlp_put(ndlp);
4222 	return;
4223 }
4224 
4225  /*
4226   * This routine will issue a GID_FT for each FC4 Type supported
4227   * by the driver. ALL GID_FTs must complete before discovery is started.
4228   */
4229 int
4230 lpfc_issue_gidft(struct lpfc_vport *vport)
4231 {
4232 	/* Good status, issue CT Request to NameServer */
4233 	if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4234 	    (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
4235 		if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
4236 			/* Cannot issue NameServer FCP Query, so finish up
4237 			 * discovery
4238 			 */
4239 			lpfc_printf_vlog(vport, KERN_ERR,
4240 					 LOG_TRACE_EVENT,
4241 					 "0604 %s FC TYPE %x %s\n",
4242 					 "Failed to issue GID_FT to ",
4243 					 FC_TYPE_FCP,
4244 					 "Finishing discovery.");
4245 			return 0;
4246 		}
4247 		vport->gidft_inp++;
4248 	}
4249 
4250 	if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4251 	    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
4252 		if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
4253 			/* Cannot issue NameServer NVME Query, so finish up
4254 			 * discovery
4255 			 */
4256 			lpfc_printf_vlog(vport, KERN_ERR,
4257 					 LOG_TRACE_EVENT,
4258 					 "0605 %s FC_TYPE %x %s %d\n",
4259 					 "Failed to issue GID_FT to ",
4260 					 FC_TYPE_NVME,
4261 					 "Finishing discovery: gidftinp ",
4262 					 vport->gidft_inp);
4263 			if (vport->gidft_inp == 0)
4264 				return 0;
4265 		} else
4266 			vport->gidft_inp++;
4267 	}
4268 	return vport->gidft_inp;
4269 }
4270 
4271 /**
4272  * lpfc_issue_gidpt - issue a GID_PT for all N_Ports
4273  * @vport: The virtual port for which this call is being executed.
4274  *
4275  * This routine will issue a GID_PT to get a list of all N_Ports
4276  *
4277  * Return value :
4278  *   0 - Failure to issue a GID_PT
4279  *   1 - GID_PT issued
4280  **/
4281 int
4282 lpfc_issue_gidpt(struct lpfc_vport *vport)
4283 {
4284 	/* Good status, issue CT Request to NameServer */
4285 	if (lpfc_ns_cmd(vport, SLI_CTNS_GID_PT, 0, GID_PT_N_PORT)) {
4286 		/* Cannot issue NameServer FCP Query, so finish up
4287 		 * discovery
4288 		 */
4289 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4290 				 "0606 %s Port TYPE %x %s\n",
4291 				 "Failed to issue GID_PT to ",
4292 				 GID_PT_N_PORT,
4293 				 "Finishing discovery.");
4294 		return 0;
4295 	}
4296 	vport->gidft_inp++;
4297 	return 1;
4298 }
4299 
4300 /*
4301  * This routine handles processing a NameServer REG_LOGIN mailbox
4302  * command upon completion. It is setup in the LPFC_MBOXQ
4303  * as the completion routine when the command is
4304  * handed off to the SLI layer.
4305  */
4306 void
4307 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4308 {
4309 	MAILBOX_t *mb = &pmb->u.mb;
4310 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4311 	struct lpfc_vport *vport = pmb->vport;
4312 	int rc;
4313 
4314 	pmb->ctx_ndlp = NULL;
4315 	vport->gidft_inp = 0;
4316 
4317 	if (mb->mbxStatus) {
4318 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4319 				 "0260 Register NameServer error: 0x%x\n",
4320 				 mb->mbxStatus);
4321 
4322 out:
4323 		/* decrement the node reference count held for this
4324 		 * callback function.
4325 		 */
4326 		lpfc_nlp_put(ndlp);
4327 		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4328 
4329 		/* If the node is not registered with the scsi or nvme
4330 		 * transport, remove the fabric node.  The failed reg_login
4331 		 * is terminal and forces the removal of the last node
4332 		 * reference.
4333 		 */
4334 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
4335 			spin_lock_irq(&ndlp->lock);
4336 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
4337 			spin_unlock_irq(&ndlp->lock);
4338 			lpfc_nlp_put(ndlp);
4339 		}
4340 
4341 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4342 			/*
4343 			 * RegLogin failed, use loop map to make discovery
4344 			 * list
4345 			 */
4346 			lpfc_disc_list_loopmap(vport);
4347 
4348 			/* Start discovery */
4349 			lpfc_disc_start(vport);
4350 			return;
4351 		}
4352 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4353 		return;
4354 	}
4355 
4356 	if (phba->sli_rev < LPFC_SLI_REV4)
4357 		ndlp->nlp_rpi = mb->un.varWords[0];
4358 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4359 	ndlp->nlp_type |= NLP_FABRIC;
4360 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4361 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
4362 			 "0003 rpi:%x DID:%x flg:%x %d x%px\n",
4363 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4364 			 kref_read(&ndlp->kref),
4365 			 ndlp);
4366 
4367 	if (vport->port_state < LPFC_VPORT_READY) {
4368 		/* Link up discovery requires Fabric registration. */
4369 		lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
4370 		lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
4371 		lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
4372 		lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
4373 
4374 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4375 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
4376 			lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
4377 
4378 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4379 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
4380 			lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
4381 				    FC_TYPE_NVME);
4382 
4383 		/* Issue SCR just before NameServer GID_FT Query */
4384 		lpfc_issue_els_scr(vport, 0);
4385 
4386 		/* Link was bounced or a Fabric LOGO occurred.  Start EDC
4387 		 * with initial FW values provided the congestion mode is
4388 		 * not off.  Note that signals may or may not be supported
4389 		 * by the adapter but FPIN is provided by default for 1
4390 		 * or both missing signals support.
4391 		 */
4392 		if (phba->cmf_active_mode != LPFC_CFG_OFF) {
4393 			phba->cgn_reg_fpin = phba->cgn_init_reg_fpin;
4394 			phba->cgn_reg_signal = phba->cgn_init_reg_signal;
4395 			rc = lpfc_issue_els_edc(vport, 0);
4396 			lpfc_printf_log(phba, KERN_INFO,
4397 					LOG_INIT | LOG_ELS | LOG_DISCOVERY,
4398 					"4220 Issue EDC status x%x Data x%x\n",
4399 					rc, phba->cgn_init_reg_signal);
4400 		} else if (phba->lmt & LMT_64Gb) {
4401 			/* may send link fault capability descriptor */
4402 			lpfc_issue_els_edc(vport, 0);
4403 		} else {
4404 			lpfc_issue_els_rdf(vport, 0);
4405 		}
4406 	}
4407 
4408 	vport->fc_ns_retry = 0;
4409 	if (lpfc_issue_gidft(vport) == 0)
4410 		goto out;
4411 
4412 	/*
4413 	 * At this point in time we may need to wait for multiple
4414 	 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
4415 	 *
4416 	 * decrement the node reference count held for this
4417 	 * callback function.
4418 	 */
4419 	lpfc_nlp_put(ndlp);
4420 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4421 	return;
4422 }
4423 
4424 /*
4425  * This routine handles processing a Fabric Controller REG_LOGIN mailbox
4426  * command upon completion. It is setup in the LPFC_MBOXQ
4427  * as the completion routine when the command is handed off to the SLI layer.
4428  */
4429 void
4430 lpfc_mbx_cmpl_fc_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4431 {
4432 	struct lpfc_vport *vport = pmb->vport;
4433 	MAILBOX_t *mb = &pmb->u.mb;
4434 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4435 
4436 	pmb->ctx_ndlp = NULL;
4437 	if (mb->mbxStatus) {
4438 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4439 				 "0933 %s: Register FC login error: 0x%x\n",
4440 				 __func__, mb->mbxStatus);
4441 		goto out;
4442 	}
4443 
4444 	lpfc_check_nlp_post_devloss(vport, ndlp);
4445 
4446 	if (phba->sli_rev < LPFC_SLI_REV4)
4447 		ndlp->nlp_rpi = mb->un.varWords[0];
4448 
4449 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4450 			 "0934 %s: Complete FC x%x RegLogin rpi x%x ste x%x\n",
4451 			 __func__, ndlp->nlp_DID, ndlp->nlp_rpi,
4452 			 ndlp->nlp_state);
4453 
4454 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4455 	ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
4456 	ndlp->nlp_type |= NLP_FABRIC;
4457 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4458 
4459  out:
4460 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4461 
4462 	/* Drop the reference count from the mbox at the end after
4463 	 * all the current reference to the ndlp have been done.
4464 	 */
4465 	lpfc_nlp_put(ndlp);
4466 }
4467 
4468 static void
4469 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4470 {
4471 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4472 	struct fc_rport  *rport;
4473 	struct lpfc_rport_data *rdata;
4474 	struct fc_rport_identifiers rport_ids;
4475 	struct lpfc_hba  *phba = vport->phba;
4476 	unsigned long flags;
4477 
4478 	if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4479 		return;
4480 
4481 	/* Remote port has reappeared. Re-register w/ FC transport */
4482 	rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4483 	rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
4484 	rport_ids.port_id = ndlp->nlp_DID;
4485 	rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
4486 
4487 
4488 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4489 			      "rport add:       did:x%x flg:x%x type x%x",
4490 			      ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4491 
4492 	/* Don't add the remote port if unloading. */
4493 	if (test_bit(FC_UNLOADING, &vport->load_flag))
4494 		return;
4495 
4496 	ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
4497 	if (!rport) {
4498 		dev_printk(KERN_WARNING, &phba->pcidev->dev,
4499 			   "Warning: fc_remote_port_add failed\n");
4500 		return;
4501 	}
4502 
4503 	/* Successful port add.  Complete initializing node data */
4504 	rport->maxframe_size = ndlp->nlp_maxframe;
4505 	rport->supported_classes = ndlp->nlp_class_sup;
4506 	rdata = rport->dd_data;
4507 	rdata->pnode = lpfc_nlp_get(ndlp);
4508 	if (!rdata->pnode) {
4509 		dev_warn(&phba->pcidev->dev,
4510 			 "Warning - node ref failed. Unreg rport\n");
4511 		fc_remote_port_delete(rport);
4512 		ndlp->rport = NULL;
4513 		return;
4514 	}
4515 
4516 	spin_lock_irqsave(&ndlp->lock, flags);
4517 	ndlp->fc4_xpt_flags |= SCSI_XPT_REGD;
4518 	spin_unlock_irqrestore(&ndlp->lock, flags);
4519 
4520 	if (ndlp->nlp_type & NLP_FCP_TARGET)
4521 		rport_ids.roles |= FC_PORT_ROLE_FCP_TARGET;
4522 	if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4523 		rport_ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
4524 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
4525 		rport_ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
4526 	if (ndlp->nlp_type & NLP_NVME_TARGET)
4527 		rport_ids.roles |= FC_PORT_ROLE_NVME_TARGET;
4528 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
4529 		rport_ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
4530 
4531 	if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
4532 		fc_remote_port_rolechg(rport, rport_ids.roles);
4533 
4534 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4535 			 "3183 %s rport x%px DID x%x, role x%x refcnt %d\n",
4536 			 __func__, rport, rport->port_id, rport->roles,
4537 			 kref_read(&ndlp->kref));
4538 
4539 	if ((rport->scsi_target_id != -1) &&
4540 	    (rport->scsi_target_id < LPFC_MAX_TARGET)) {
4541 		ndlp->nlp_sid = rport->scsi_target_id;
4542 	}
4543 
4544 	return;
4545 }
4546 
4547 static void
4548 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
4549 {
4550 	struct fc_rport *rport = ndlp->rport;
4551 	struct lpfc_vport *vport = ndlp->vport;
4552 
4553 	if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4554 		return;
4555 
4556 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4557 		"rport delete:    did:x%x flg:x%x type x%x",
4558 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4559 
4560 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4561 			 "3184 rport unregister x%06x, rport x%px "
4562 			 "xptflg x%x refcnt %d\n",
4563 			 ndlp->nlp_DID, rport, ndlp->fc4_xpt_flags,
4564 			 kref_read(&ndlp->kref));
4565 
4566 	fc_remote_port_delete(rport);
4567 	lpfc_nlp_put(ndlp);
4568 }
4569 
4570 static void
4571 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
4572 {
4573 	switch (state) {
4574 	case NLP_STE_UNUSED_NODE:
4575 		atomic_add(count, &vport->fc_unused_cnt);
4576 		break;
4577 	case NLP_STE_PLOGI_ISSUE:
4578 		atomic_add(count, &vport->fc_plogi_cnt);
4579 		break;
4580 	case NLP_STE_ADISC_ISSUE:
4581 		atomic_add(count, &vport->fc_adisc_cnt);
4582 		break;
4583 	case NLP_STE_REG_LOGIN_ISSUE:
4584 		atomic_add(count, &vport->fc_reglogin_cnt);
4585 		break;
4586 	case NLP_STE_PRLI_ISSUE:
4587 		atomic_add(count, &vport->fc_prli_cnt);
4588 		break;
4589 	case NLP_STE_UNMAPPED_NODE:
4590 		atomic_add(count, &vport->fc_unmap_cnt);
4591 		break;
4592 	case NLP_STE_MAPPED_NODE:
4593 		atomic_add(count, &vport->fc_map_cnt);
4594 		break;
4595 	case NLP_STE_NPR_NODE:
4596 		if (!atomic_read(&vport->fc_npr_cnt) && count == -1)
4597 			atomic_set(&vport->fc_npr_cnt, 0);
4598 		else
4599 			atomic_add(count, &vport->fc_npr_cnt);
4600 		break;
4601 	}
4602 }
4603 
4604 /* Register a node with backend if not already done */
4605 void
4606 lpfc_nlp_reg_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4607 {
4608 	unsigned long iflags;
4609 
4610 	lpfc_check_nlp_post_devloss(vport, ndlp);
4611 
4612 	spin_lock_irqsave(&ndlp->lock, iflags);
4613 	if (ndlp->fc4_xpt_flags & NLP_XPT_REGD) {
4614 		/* Already registered with backend, trigger rescan */
4615 		spin_unlock_irqrestore(&ndlp->lock, iflags);
4616 
4617 		if (ndlp->fc4_xpt_flags & NVME_XPT_REGD &&
4618 		    ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY)) {
4619 			lpfc_nvme_rescan_port(vport, ndlp);
4620 		}
4621 		return;
4622 	}
4623 
4624 	ndlp->fc4_xpt_flags |= NLP_XPT_REGD;
4625 	spin_unlock_irqrestore(&ndlp->lock, iflags);
4626 
4627 	if (lpfc_valid_xpt_node(ndlp)) {
4628 		vport->phba->nport_event_cnt++;
4629 		/*
4630 		 * Tell the fc transport about the port, if we haven't
4631 		 * already. If we have, and it's a scsi entity, be
4632 		 */
4633 		lpfc_register_remote_port(vport, ndlp);
4634 	}
4635 
4636 	/* We are done if we do not have any NVME remote node */
4637 	if (!(ndlp->nlp_fc4_type & NLP_FC4_NVME))
4638 		return;
4639 
4640 	/* Notify the NVME transport of this new rport. */
4641 	if (vport->phba->sli_rev >= LPFC_SLI_REV4 &&
4642 			ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4643 		if (vport->phba->nvmet_support == 0) {
4644 			/* Register this rport with the transport.
4645 			 * Only NVME Target Rports are registered with
4646 			 * the transport.
4647 			 */
4648 			if (ndlp->nlp_type & NLP_NVME_TARGET) {
4649 				vport->phba->nport_event_cnt++;
4650 				lpfc_nvme_register_port(vport, ndlp);
4651 			}
4652 		} else {
4653 			/* Just take an NDLP ref count since the
4654 			 * target does not register rports.
4655 			 */
4656 			lpfc_nlp_get(ndlp);
4657 		}
4658 	}
4659 }
4660 
4661 /* Unregister a node with backend if not already done */
4662 void
4663 lpfc_nlp_unreg_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4664 {
4665 	unsigned long iflags;
4666 
4667 	spin_lock_irqsave(&ndlp->lock, iflags);
4668 	if (!(ndlp->fc4_xpt_flags & NLP_XPT_REGD)) {
4669 		spin_unlock_irqrestore(&ndlp->lock, iflags);
4670 		lpfc_printf_vlog(vport, KERN_INFO,
4671 				 LOG_ELS | LOG_NODE | LOG_DISCOVERY,
4672 				 "0999 %s Not regd: ndlp x%px rport x%px DID "
4673 				 "x%x FLG x%x XPT x%x\n",
4674 				  __func__, ndlp, ndlp->rport, ndlp->nlp_DID,
4675 				  ndlp->nlp_flag, ndlp->fc4_xpt_flags);
4676 		return;
4677 	}
4678 
4679 	ndlp->fc4_xpt_flags &= ~NLP_XPT_REGD;
4680 	spin_unlock_irqrestore(&ndlp->lock, iflags);
4681 
4682 	if (ndlp->rport &&
4683 	    ndlp->fc4_xpt_flags & SCSI_XPT_REGD) {
4684 		vport->phba->nport_event_cnt++;
4685 		lpfc_unregister_remote_port(ndlp);
4686 	} else if (!ndlp->rport) {
4687 		lpfc_printf_vlog(vport, KERN_INFO,
4688 				 LOG_ELS | LOG_NODE | LOG_DISCOVERY,
4689 				 "1999 %s NDLP in devloss x%px DID x%x FLG x%x"
4690 				 " XPT x%x refcnt %u\n",
4691 				 __func__, ndlp, ndlp->nlp_DID, ndlp->nlp_flag,
4692 				 ndlp->fc4_xpt_flags,
4693 				 kref_read(&ndlp->kref));
4694 	}
4695 
4696 	if (ndlp->fc4_xpt_flags & NVME_XPT_REGD) {
4697 		vport->phba->nport_event_cnt++;
4698 		if (vport->phba->nvmet_support == 0) {
4699 			/* Start devloss if target. */
4700 			if (ndlp->nlp_type & NLP_NVME_TARGET)
4701 				lpfc_nvme_unregister_port(vport, ndlp);
4702 		} else {
4703 			/* NVMET has no upcall. */
4704 			lpfc_nlp_put(ndlp);
4705 		}
4706 	}
4707 
4708 }
4709 
4710 /*
4711  * Adisc state change handling
4712  */
4713 static void
4714 lpfc_handle_adisc_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4715 		int new_state)
4716 {
4717 	switch (new_state) {
4718 	/*
4719 	 * Any state to ADISC_ISSUE
4720 	 * Do nothing, adisc cmpl handling will trigger state changes
4721 	 */
4722 	case NLP_STE_ADISC_ISSUE:
4723 		break;
4724 
4725 	/*
4726 	 * ADISC_ISSUE to mapped states
4727 	 * Trigger a registration with backend, it will be nop if
4728 	 * already registered
4729 	 */
4730 	case NLP_STE_UNMAPPED_NODE:
4731 		ndlp->nlp_type |= NLP_FC_NODE;
4732 		fallthrough;
4733 	case NLP_STE_MAPPED_NODE:
4734 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4735 		lpfc_nlp_reg_node(vport, ndlp);
4736 		break;
4737 
4738 	/*
4739 	 * ADISC_ISSUE to non-mapped states
4740 	 * We are moving from ADISC_ISSUE to a non-mapped state because
4741 	 * ADISC failed, we would have skipped unregistering with
4742 	 * backend, attempt it now
4743 	 */
4744 	case NLP_STE_NPR_NODE:
4745 		ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
4746 		fallthrough;
4747 	default:
4748 		lpfc_nlp_unreg_node(vport, ndlp);
4749 		break;
4750 	}
4751 
4752 }
4753 
4754 static void
4755 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4756 		       int old_state, int new_state)
4757 {
4758 	/* Trap ADISC changes here */
4759 	if (new_state == NLP_STE_ADISC_ISSUE ||
4760 	    old_state == NLP_STE_ADISC_ISSUE) {
4761 		lpfc_handle_adisc_state(vport, ndlp, new_state);
4762 		return;
4763 	}
4764 
4765 	if (new_state == NLP_STE_UNMAPPED_NODE) {
4766 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4767 		ndlp->nlp_type |= NLP_FC_NODE;
4768 	}
4769 	if (new_state == NLP_STE_MAPPED_NODE)
4770 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4771 	if (new_state == NLP_STE_NPR_NODE)
4772 		ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
4773 
4774 	/* Reg/Unreg for FCP and NVME Transport interface */
4775 	if ((old_state == NLP_STE_MAPPED_NODE ||
4776 	     old_state == NLP_STE_UNMAPPED_NODE)) {
4777 		/* For nodes marked for ADISC, Handle unreg in ADISC cmpl
4778 		 * if linkup. In linkdown do unreg_node
4779 		 */
4780 		if (!(ndlp->nlp_flag & NLP_NPR_ADISC) ||
4781 		    !lpfc_is_link_up(vport->phba))
4782 			lpfc_nlp_unreg_node(vport, ndlp);
4783 	}
4784 
4785 	if (new_state ==  NLP_STE_MAPPED_NODE ||
4786 	    new_state == NLP_STE_UNMAPPED_NODE)
4787 		lpfc_nlp_reg_node(vport, ndlp);
4788 
4789 	/*
4790 	 * If the node just added to Mapped list was an FCP target,
4791 	 * but the remote port registration failed or assigned a target
4792 	 * id outside the presentable range - move the node to the
4793 	 * Unmapped List.
4794 	 */
4795 	if ((new_state == NLP_STE_MAPPED_NODE) &&
4796 	    (ndlp->nlp_type & NLP_FCP_TARGET) &&
4797 	    (!ndlp->rport ||
4798 	     ndlp->rport->scsi_target_id == -1 ||
4799 	     ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
4800 		spin_lock_irq(&ndlp->lock);
4801 		ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
4802 		spin_unlock_irq(&ndlp->lock);
4803 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4804 	}
4805 }
4806 
4807 static char *
4808 lpfc_nlp_state_name(char *buffer, size_t size, int state)
4809 {
4810 	static char *states[] = {
4811 		[NLP_STE_UNUSED_NODE] = "UNUSED",
4812 		[NLP_STE_PLOGI_ISSUE] = "PLOGI",
4813 		[NLP_STE_ADISC_ISSUE] = "ADISC",
4814 		[NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4815 		[NLP_STE_PRLI_ISSUE] = "PRLI",
4816 		[NLP_STE_LOGO_ISSUE] = "LOGO",
4817 		[NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4818 		[NLP_STE_MAPPED_NODE] = "MAPPED",
4819 		[NLP_STE_NPR_NODE] = "NPR",
4820 	};
4821 
4822 	if (state < NLP_STE_MAX_STATE && states[state])
4823 		strscpy(buffer, states[state], size);
4824 	else
4825 		snprintf(buffer, size, "unknown (%d)", state);
4826 	return buffer;
4827 }
4828 
4829 void
4830 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4831 		   int state)
4832 {
4833 	int  old_state = ndlp->nlp_state;
4834 	int node_dropped = ndlp->nlp_flag & NLP_DROPPED;
4835 	char name1[16], name2[16];
4836 	unsigned long iflags;
4837 
4838 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4839 			 "0904 NPort state transition x%06x, %s -> %s\n",
4840 			 ndlp->nlp_DID,
4841 			 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4842 			 lpfc_nlp_state_name(name2, sizeof(name2), state));
4843 
4844 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4845 		"node statechg    did:x%x old:%d ste:%d",
4846 		ndlp->nlp_DID, old_state, state);
4847 
4848 	if (node_dropped && old_state == NLP_STE_UNUSED_NODE &&
4849 	    state != NLP_STE_UNUSED_NODE) {
4850 		ndlp->nlp_flag &= ~NLP_DROPPED;
4851 		lpfc_nlp_get(ndlp);
4852 	}
4853 
4854 	if (old_state == NLP_STE_NPR_NODE &&
4855 	    state != NLP_STE_NPR_NODE)
4856 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
4857 	if (old_state == NLP_STE_UNMAPPED_NODE) {
4858 		ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4859 		ndlp->nlp_type &= ~NLP_FC_NODE;
4860 	}
4861 
4862 	if (list_empty(&ndlp->nlp_listp)) {
4863 		spin_lock_irqsave(&vport->fc_nodes_list_lock, iflags);
4864 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4865 		spin_unlock_irqrestore(&vport->fc_nodes_list_lock, iflags);
4866 	} else if (old_state)
4867 		lpfc_nlp_counters(vport, old_state, -1);
4868 
4869 	ndlp->nlp_state = state;
4870 	lpfc_nlp_counters(vport, state, 1);
4871 	lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
4872 }
4873 
4874 void
4875 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4876 {
4877 	unsigned long iflags;
4878 
4879 	if (list_empty(&ndlp->nlp_listp)) {
4880 		spin_lock_irqsave(&vport->fc_nodes_list_lock, iflags);
4881 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4882 		spin_unlock_irqrestore(&vport->fc_nodes_list_lock, iflags);
4883 	}
4884 }
4885 
4886 void
4887 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4888 {
4889 	unsigned long iflags;
4890 
4891 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
4892 	if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4893 		lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4894 	spin_lock_irqsave(&vport->fc_nodes_list_lock, iflags);
4895 	list_del_init(&ndlp->nlp_listp);
4896 	spin_unlock_irqrestore(&vport->fc_nodes_list_lock, iflags);
4897 	lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4898 				NLP_STE_UNUSED_NODE);
4899 }
4900 
4901 /**
4902  * lpfc_initialize_node - Initialize all fields of node object
4903  * @vport: Pointer to Virtual Port object.
4904  * @ndlp: Pointer to FC node object.
4905  * @did: FC_ID of the node.
4906  *
4907  * This function is always called when node object need to be initialized.
4908  * It initializes all the fields of the node object. Although the reference
4909  * to phba from @ndlp can be obtained indirectly through it's reference to
4910  * @vport, a direct reference to phba is taken here by @ndlp. This is due
4911  * to the life-span of the @ndlp might go beyond the existence of @vport as
4912  * the final release of ndlp is determined by its reference count. And, the
4913  * operation on @ndlp needs the reference to phba.
4914  **/
4915 static inline void
4916 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4917 	uint32_t did)
4918 {
4919 	INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4920 	INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4921 	timer_setup(&ndlp->nlp_delayfunc, lpfc_els_retry_delay, 0);
4922 	INIT_LIST_HEAD(&ndlp->recovery_evt.evt_listp);
4923 
4924 	ndlp->nlp_DID = did;
4925 	ndlp->vport = vport;
4926 	ndlp->phba = vport->phba;
4927 	ndlp->nlp_sid = NLP_NO_SID;
4928 	ndlp->nlp_fc4_type = NLP_FC4_NONE;
4929 	kref_init(&ndlp->kref);
4930 	atomic_set(&ndlp->cmd_pending, 0);
4931 	ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
4932 	ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4933 }
4934 
4935 void
4936 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4937 {
4938 	/*
4939 	 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
4940 	 * be used when lpfc wants to remove the "last" lpfc_nlp_put() to
4941 	 * release the ndlp from the vport when conditions are correct.
4942 	 */
4943 	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4944 		return;
4945 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
4946 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4947 		lpfc_cleanup_vports_rrqs(vport, ndlp);
4948 		lpfc_unreg_rpi(vport, ndlp);
4949 	}
4950 
4951 	/* NLP_DROPPED means another thread already removed the initial
4952 	 * reference from lpfc_nlp_init.  If set, don't drop it again and
4953 	 * introduce an imbalance.
4954 	 */
4955 	spin_lock_irq(&ndlp->lock);
4956 	if (!(ndlp->nlp_flag & NLP_DROPPED)) {
4957 		ndlp->nlp_flag |= NLP_DROPPED;
4958 		spin_unlock_irq(&ndlp->lock);
4959 		lpfc_nlp_put(ndlp);
4960 		return;
4961 	}
4962 	spin_unlock_irq(&ndlp->lock);
4963 }
4964 
4965 /*
4966  * Start / ReStart rescue timer for Discovery / RSCN handling
4967  */
4968 void
4969 lpfc_set_disctmo(struct lpfc_vport *vport)
4970 {
4971 	struct lpfc_hba  *phba = vport->phba;
4972 	uint32_t tmo;
4973 
4974 	if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
4975 		/* For FAN, timeout should be greater than edtov */
4976 		tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4977 	} else {
4978 		/* Normal discovery timeout should be > than ELS/CT timeout
4979 		 * FC spec states we need 3 * ratov for CT requests
4980 		 */
4981 		tmo = ((phba->fc_ratov * 3) + 3);
4982 	}
4983 
4984 
4985 	if (!timer_pending(&vport->fc_disctmo)) {
4986 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4987 			"set disc timer:  tmo:x%x state:x%x flg:x%x",
4988 			tmo, vport->port_state, vport->fc_flag);
4989 	}
4990 
4991 	mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
4992 	set_bit(FC_DISC_TMO, &vport->fc_flag);
4993 
4994 	/* Start Discovery Timer state <hba_state> */
4995 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4996 			 "0247 Start Discovery Timer state x%x "
4997 			 "Data: x%x x%lx x%x x%x\n",
4998 			 vport->port_state, tmo,
4999 			 (unsigned long)&vport->fc_disctmo,
5000 			 atomic_read(&vport->fc_plogi_cnt),
5001 			 atomic_read(&vport->fc_adisc_cnt));
5002 
5003 	return;
5004 }
5005 
5006 /*
5007  * Cancel rescue timer for Discovery / RSCN handling
5008  */
5009 int
5010 lpfc_can_disctmo(struct lpfc_vport *vport)
5011 {
5012 	unsigned long iflags;
5013 
5014 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5015 		"can disc timer:  state:x%x rtry:x%x flg:x%x",
5016 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5017 
5018 	/* Turn off discovery timer if its running */
5019 	if (test_bit(FC_DISC_TMO, &vport->fc_flag) ||
5020 	    timer_pending(&vport->fc_disctmo)) {
5021 		clear_bit(FC_DISC_TMO, &vport->fc_flag);
5022 		del_timer_sync(&vport->fc_disctmo);
5023 		spin_lock_irqsave(&vport->work_port_lock, iflags);
5024 		vport->work_port_events &= ~WORKER_DISC_TMO;
5025 		spin_unlock_irqrestore(&vport->work_port_lock, iflags);
5026 	}
5027 
5028 	/* Cancel Discovery Timer state <hba_state> */
5029 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5030 			 "0248 Cancel Discovery Timer state x%x "
5031 			 "Data: x%lx x%x x%x\n",
5032 			 vport->port_state, vport->fc_flag,
5033 			 atomic_read(&vport->fc_plogi_cnt),
5034 			 atomic_read(&vport->fc_adisc_cnt));
5035 	return 0;
5036 }
5037 
5038 /*
5039  * Check specified ring for outstanding IOCB on the SLI queue
5040  * Return true if iocb matches the specified nport
5041  */
5042 int
5043 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
5044 		    struct lpfc_sli_ring *pring,
5045 		    struct lpfc_iocbq *iocb,
5046 		    struct lpfc_nodelist *ndlp)
5047 {
5048 	struct lpfc_vport *vport = ndlp->vport;
5049 	u8 ulp_command;
5050 	u16 ulp_context;
5051 	u32 remote_id;
5052 
5053 	if (iocb->vport != vport)
5054 		return 0;
5055 
5056 	ulp_command = get_job_cmnd(phba, iocb);
5057 	ulp_context = get_job_ulpcontext(phba, iocb);
5058 	remote_id = get_job_els_rsp64_did(phba, iocb);
5059 
5060 	if (pring->ringno == LPFC_ELS_RING) {
5061 		switch (ulp_command) {
5062 		case CMD_GEN_REQUEST64_CR:
5063 			if (iocb->ndlp == ndlp)
5064 				return 1;
5065 			fallthrough;
5066 		case CMD_ELS_REQUEST64_CR:
5067 			if (remote_id == ndlp->nlp_DID)
5068 				return 1;
5069 			fallthrough;
5070 		case CMD_XMIT_ELS_RSP64_CX:
5071 			if (iocb->ndlp == ndlp)
5072 				return 1;
5073 		}
5074 	} else if (pring->ringno == LPFC_FCP_RING) {
5075 		/* Skip match check if waiting to relogin to FCP target */
5076 		if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
5077 		    (ndlp->nlp_flag & NLP_DELAY_TMO)) {
5078 			return 0;
5079 		}
5080 		if (ulp_context == ndlp->nlp_rpi)
5081 			return 1;
5082 	}
5083 	return 0;
5084 }
5085 
5086 static void
5087 __lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
5088 		struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
5089 		struct list_head *dequeue_list)
5090 {
5091 	struct lpfc_iocbq *iocb, *next_iocb;
5092 
5093 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5094 		/* Check to see if iocb matches the nport */
5095 		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
5096 			/* match, dequeue */
5097 			list_move_tail(&iocb->list, dequeue_list);
5098 	}
5099 }
5100 
5101 static void
5102 lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
5103 		struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
5104 {
5105 	struct lpfc_sli *psli = &phba->sli;
5106 	uint32_t i;
5107 
5108 	spin_lock_irq(&phba->hbalock);
5109 	for (i = 0; i < psli->num_rings; i++)
5110 		__lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
5111 						dequeue_list);
5112 	spin_unlock_irq(&phba->hbalock);
5113 }
5114 
5115 static void
5116 lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
5117 		struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
5118 {
5119 	struct lpfc_sli_ring *pring;
5120 	struct lpfc_queue *qp = NULL;
5121 
5122 	spin_lock_irq(&phba->hbalock);
5123 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
5124 		pring = qp->pring;
5125 		if (!pring)
5126 			continue;
5127 		spin_lock(&pring->ring_lock);
5128 		__lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
5129 		spin_unlock(&pring->ring_lock);
5130 	}
5131 	spin_unlock_irq(&phba->hbalock);
5132 }
5133 
5134 /*
5135  * Free resources / clean up outstanding I/Os
5136  * associated with nlp_rpi in the LPFC_NODELIST entry.
5137  */
5138 static int
5139 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
5140 {
5141 	LIST_HEAD(completions);
5142 
5143 	lpfc_fabric_abort_nport(ndlp);
5144 
5145 	/*
5146 	 * Everything that matches on txcmplq will be returned
5147 	 * by firmware with a no rpi error.
5148 	 */
5149 	if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5150 		if (phba->sli_rev != LPFC_SLI_REV4)
5151 			lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
5152 		else
5153 			lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
5154 	}
5155 
5156 	/* Cancel all the IOCBs from the completions list */
5157 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5158 			      IOERR_SLI_ABORTED);
5159 
5160 	return 0;
5161 }
5162 
5163 /**
5164  * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
5165  * @phba: Pointer to HBA context object.
5166  * @pmb: Pointer to mailbox object.
5167  *
5168  * This function will issue an ELS LOGO command after completing
5169  * the UNREG_RPI.
5170  **/
5171 static void
5172 lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5173 {
5174 	struct lpfc_vport  *vport = pmb->vport;
5175 	struct lpfc_nodelist *ndlp;
5176 
5177 	ndlp = (struct lpfc_nodelist *)(pmb->ctx_ndlp);
5178 	if (!ndlp)
5179 		return;
5180 	lpfc_issue_els_logo(vport, ndlp, 0);
5181 
5182 	/* Check to see if there are any deferred events to process */
5183 	if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
5184 	    (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
5185 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5186 				 "1434 UNREG cmpl deferred logo x%x "
5187 				 "on NPort x%x Data: x%x x%px\n",
5188 				 ndlp->nlp_rpi, ndlp->nlp_DID,
5189 				 ndlp->nlp_defer_did, ndlp);
5190 
5191 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
5192 		ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
5193 		lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5194 	} else {
5195 		/* NLP_RELEASE_RPI is only set for SLI4 ports. */
5196 		if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
5197 			lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
5198 			spin_lock_irq(&ndlp->lock);
5199 			ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
5200 			ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
5201 			spin_unlock_irq(&ndlp->lock);
5202 		}
5203 		spin_lock_irq(&ndlp->lock);
5204 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
5205 		spin_unlock_irq(&ndlp->lock);
5206 	}
5207 
5208 	/* The node has an outstanding reference for the unreg. Now
5209 	 * that the LOGO action and cleanup are finished, release
5210 	 * resources.
5211 	 */
5212 	lpfc_nlp_put(ndlp);
5213 	mempool_free(pmb, phba->mbox_mem_pool);
5214 }
5215 
5216 /*
5217  * Sets the mailbox completion handler to be used for the
5218  * unreg_rpi command. The handler varies based on the state of
5219  * the port and what will be happening to the rpi next.
5220  */
5221 static void
5222 lpfc_set_unreg_login_mbx_cmpl(struct lpfc_hba *phba, struct lpfc_vport *vport,
5223 	struct lpfc_nodelist *ndlp, LPFC_MBOXQ_t *mbox)
5224 {
5225 	unsigned long iflags;
5226 
5227 	/* Driver always gets a reference on the mailbox job
5228 	 * in support of async jobs.
5229 	 */
5230 	mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
5231 	if (!mbox->ctx_ndlp)
5232 		return;
5233 
5234 	if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
5235 		mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
5236 
5237 	} else if (phba->sli_rev == LPFC_SLI_REV4 &&
5238 		   !test_bit(FC_UNLOADING, &vport->load_flag) &&
5239 		    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
5240 				      LPFC_SLI_INTF_IF_TYPE_2) &&
5241 		    (kref_read(&ndlp->kref) > 0)) {
5242 		mbox->mbox_cmpl = lpfc_sli4_unreg_rpi_cmpl_clr;
5243 	} else {
5244 		if (test_bit(FC_UNLOADING, &vport->load_flag)) {
5245 			if (phba->sli_rev == LPFC_SLI_REV4) {
5246 				spin_lock_irqsave(&ndlp->lock, iflags);
5247 				ndlp->nlp_flag |= NLP_RELEASE_RPI;
5248 				spin_unlock_irqrestore(&ndlp->lock, iflags);
5249 			}
5250 		}
5251 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5252 	}
5253 }
5254 
5255 /*
5256  * Free rpi associated with LPFC_NODELIST entry.
5257  * This routine is called from lpfc_freenode(), when we are removing
5258  * a LPFC_NODELIST entry. It is also called if the driver initiates a
5259  * LOGO that completes successfully, and we are waiting to PLOGI back
5260  * to the remote NPort. In addition, it is called after we receive
5261  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
5262  * we are waiting to PLOGI back to the remote NPort.
5263  */
5264 int
5265 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5266 {
5267 	struct lpfc_hba *phba = vport->phba;
5268 	LPFC_MBOXQ_t    *mbox;
5269 	int rc, acc_plogi = 1;
5270 	uint16_t rpi;
5271 
5272 	if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
5273 	    ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
5274 		if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
5275 			lpfc_printf_vlog(vport, KERN_INFO,
5276 					 LOG_NODE | LOG_DISCOVERY,
5277 					 "3366 RPI x%x needs to be "
5278 					 "unregistered nlp_flag x%x "
5279 					 "did x%x\n",
5280 					 ndlp->nlp_rpi, ndlp->nlp_flag,
5281 					 ndlp->nlp_DID);
5282 
5283 		/* If there is already an UNREG in progress for this ndlp,
5284 		 * no need to queue up another one.
5285 		 */
5286 		if (ndlp->nlp_flag & NLP_UNREG_INP) {
5287 			lpfc_printf_vlog(vport, KERN_INFO,
5288 					 LOG_NODE | LOG_DISCOVERY,
5289 					 "1436 unreg_rpi SKIP UNREG x%x on "
5290 					 "NPort x%x deferred x%x  flg x%x "
5291 					 "Data: x%px\n",
5292 					 ndlp->nlp_rpi, ndlp->nlp_DID,
5293 					 ndlp->nlp_defer_did,
5294 					 ndlp->nlp_flag, ndlp);
5295 			goto out;
5296 		}
5297 
5298 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5299 		if (mbox) {
5300 			/* SLI4 ports require the physical rpi value. */
5301 			rpi = ndlp->nlp_rpi;
5302 			if (phba->sli_rev == LPFC_SLI_REV4)
5303 				rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
5304 
5305 			lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
5306 			mbox->vport = vport;
5307 			lpfc_set_unreg_login_mbx_cmpl(phba, vport, ndlp, mbox);
5308 			if (!mbox->ctx_ndlp) {
5309 				mempool_free(mbox, phba->mbox_mem_pool);
5310 				return 1;
5311 			}
5312 
5313 			if (mbox->mbox_cmpl == lpfc_sli4_unreg_rpi_cmpl_clr)
5314 				/*
5315 				 * accept PLOGIs after unreg_rpi_cmpl
5316 				 */
5317 				acc_plogi = 0;
5318 			if (((ndlp->nlp_DID & Fabric_DID_MASK) !=
5319 			    Fabric_DID_MASK) &&
5320 			    (!test_bit(FC_OFFLINE_MODE, &vport->fc_flag)))
5321 				ndlp->nlp_flag |= NLP_UNREG_INP;
5322 
5323 			lpfc_printf_vlog(vport, KERN_INFO,
5324 					 LOG_NODE | LOG_DISCOVERY,
5325 					 "1433 unreg_rpi UNREG x%x on "
5326 					 "NPort x%x deferred flg x%x "
5327 					 "Data:x%px\n",
5328 					 ndlp->nlp_rpi, ndlp->nlp_DID,
5329 					 ndlp->nlp_flag, ndlp);
5330 
5331 			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5332 			if (rc == MBX_NOT_FINISHED) {
5333 				ndlp->nlp_flag &= ~NLP_UNREG_INP;
5334 				mempool_free(mbox, phba->mbox_mem_pool);
5335 				acc_plogi = 1;
5336 				lpfc_nlp_put(ndlp);
5337 			}
5338 		} else {
5339 			lpfc_printf_vlog(vport, KERN_INFO,
5340 					 LOG_NODE | LOG_DISCOVERY,
5341 					 "1444 Failed to allocate mempool "
5342 					 "unreg_rpi UNREG x%x, "
5343 					 "DID x%x, flag x%x, "
5344 					 "ndlp x%px\n",
5345 					 ndlp->nlp_rpi, ndlp->nlp_DID,
5346 					 ndlp->nlp_flag, ndlp);
5347 
5348 			/* Because mempool_alloc failed, we
5349 			 * will issue a LOGO here and keep the rpi alive if
5350 			 * not unloading.
5351 			 */
5352 			if (!test_bit(FC_UNLOADING, &vport->load_flag)) {
5353 				ndlp->nlp_flag &= ~NLP_UNREG_INP;
5354 				lpfc_issue_els_logo(vport, ndlp, 0);
5355 				ndlp->nlp_prev_state = ndlp->nlp_state;
5356 				lpfc_nlp_set_state(vport, ndlp,
5357 						   NLP_STE_NPR_NODE);
5358 			}
5359 
5360 			return 1;
5361 		}
5362 		lpfc_no_rpi(phba, ndlp);
5363 out:
5364 		if (phba->sli_rev != LPFC_SLI_REV4)
5365 			ndlp->nlp_rpi = 0;
5366 		ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
5367 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
5368 		if (acc_plogi)
5369 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5370 		return 1;
5371 	}
5372 	ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5373 	return 0;
5374 }
5375 
5376 /**
5377  * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
5378  * @phba: pointer to lpfc hba data structure.
5379  *
5380  * This routine is invoked to unregister all the currently registered RPIs
5381  * to the HBA.
5382  **/
5383 void
5384 lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
5385 {
5386 	struct lpfc_vport **vports;
5387 	struct lpfc_nodelist *ndlp;
5388 	int i;
5389 	unsigned long iflags;
5390 
5391 	vports = lpfc_create_vport_work_array(phba);
5392 	if (!vports) {
5393 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5394 				"2884 Vport array allocation failed \n");
5395 		return;
5396 	}
5397 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5398 		spin_lock_irqsave(&vports[i]->fc_nodes_list_lock, iflags);
5399 		list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5400 			if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5401 				/* The mempool_alloc might sleep */
5402 				spin_unlock_irqrestore(&vports[i]->fc_nodes_list_lock,
5403 						       iflags);
5404 				lpfc_unreg_rpi(vports[i], ndlp);
5405 				spin_lock_irqsave(&vports[i]->fc_nodes_list_lock,
5406 						  iflags);
5407 			}
5408 		}
5409 		spin_unlock_irqrestore(&vports[i]->fc_nodes_list_lock, iflags);
5410 	}
5411 	lpfc_destroy_vport_work_array(phba, vports);
5412 }
5413 
5414 void
5415 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
5416 {
5417 	struct lpfc_hba  *phba  = vport->phba;
5418 	LPFC_MBOXQ_t     *mbox;
5419 	int rc;
5420 
5421 	if (phba->sli_rev == LPFC_SLI_REV4) {
5422 		lpfc_sli4_unreg_all_rpis(vport);
5423 		return;
5424 	}
5425 
5426 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5427 	if (mbox) {
5428 		lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
5429 				 mbox);
5430 		mbox->vport = vport;
5431 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5432 		mbox->ctx_ndlp = NULL;
5433 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5434 		if (rc != MBX_TIMEOUT)
5435 			mempool_free(mbox, phba->mbox_mem_pool);
5436 
5437 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5438 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5439 					 "1836 Could not issue "
5440 					 "unreg_login(all_rpis) status %d\n",
5441 					 rc);
5442 	}
5443 }
5444 
5445 void
5446 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
5447 {
5448 	struct lpfc_hba  *phba  = vport->phba;
5449 	LPFC_MBOXQ_t     *mbox;
5450 	int rc;
5451 
5452 	/* Unreg DID is an SLI3 operation. */
5453 	if (phba->sli_rev > LPFC_SLI_REV3)
5454 		return;
5455 
5456 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5457 	if (mbox) {
5458 		lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
5459 			       mbox);
5460 		mbox->vport = vport;
5461 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5462 		mbox->ctx_ndlp = NULL;
5463 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5464 		if (rc != MBX_TIMEOUT)
5465 			mempool_free(mbox, phba->mbox_mem_pool);
5466 
5467 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5468 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5469 					 "1815 Could not issue "
5470 					 "unreg_did (default rpis) status %d\n",
5471 					 rc);
5472 	}
5473 }
5474 
5475 /*
5476  * Free resources associated with LPFC_NODELIST entry
5477  * so it can be freed.
5478  */
5479 static int
5480 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5481 {
5482 	struct lpfc_hba  *phba = vport->phba;
5483 	LPFC_MBOXQ_t *mb, *nextmb;
5484 
5485 	/* Cleanup node for NPort <nlp_DID> */
5486 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5487 			 "0900 Cleanup node for NPort x%x "
5488 			 "Data: x%x x%x x%x\n",
5489 			 ndlp->nlp_DID, ndlp->nlp_flag,
5490 			 ndlp->nlp_state, ndlp->nlp_rpi);
5491 	lpfc_dequeue_node(vport, ndlp);
5492 
5493 	/* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
5494 
5495 	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
5496 	if ((mb = phba->sli.mbox_active)) {
5497 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5498 		   !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5499 		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5500 			mb->ctx_ndlp = NULL;
5501 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5502 		}
5503 	}
5504 
5505 	spin_lock_irq(&phba->hbalock);
5506 	/* Cleanup REG_LOGIN completions which are not yet processed */
5507 	list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
5508 		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
5509 			(mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
5510 			(ndlp != (struct lpfc_nodelist *)mb->ctx_ndlp))
5511 			continue;
5512 
5513 		mb->ctx_ndlp = NULL;
5514 		mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5515 	}
5516 
5517 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
5518 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5519 		   !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5520 		    (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5521 			list_del(&mb->list);
5522 			lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_LOCKED);
5523 
5524 			/* Don't invoke lpfc_nlp_put. The driver is in
5525 			 * lpfc_nlp_release context.
5526 			 */
5527 		}
5528 	}
5529 	spin_unlock_irq(&phba->hbalock);
5530 
5531 	lpfc_els_abort(phba, ndlp);
5532 
5533 	spin_lock_irq(&ndlp->lock);
5534 	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
5535 	spin_unlock_irq(&ndlp->lock);
5536 
5537 	ndlp->nlp_last_elscmd = 0;
5538 	del_timer_sync(&ndlp->nlp_delayfunc);
5539 
5540 	list_del_init(&ndlp->els_retry_evt.evt_listp);
5541 	list_del_init(&ndlp->dev_loss_evt.evt_listp);
5542 	list_del_init(&ndlp->recovery_evt.evt_listp);
5543 	lpfc_cleanup_vports_rrqs(vport, ndlp);
5544 
5545 	if (phba->sli_rev == LPFC_SLI_REV4)
5546 		ndlp->nlp_flag |= NLP_RELEASE_RPI;
5547 
5548 	return 0;
5549 }
5550 
5551 static int
5552 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5553 	      uint32_t did)
5554 {
5555 	D_ID mydid, ndlpdid, matchdid;
5556 
5557 	if (did == Bcast_DID)
5558 		return 0;
5559 
5560 	/* First check for Direct match */
5561 	if (ndlp->nlp_DID == did)
5562 		return 1;
5563 
5564 	/* Next check for area/domain identically equals 0 match */
5565 	mydid.un.word = vport->fc_myDID;
5566 	if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
5567 		return 0;
5568 	}
5569 
5570 	matchdid.un.word = did;
5571 	ndlpdid.un.word = ndlp->nlp_DID;
5572 	if (matchdid.un.b.id == ndlpdid.un.b.id) {
5573 		if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5574 		    (mydid.un.b.area == matchdid.un.b.area)) {
5575 			/* This code is supposed to match the ID
5576 			 * for a private loop device that is
5577 			 * connect to fl_port. But we need to
5578 			 * check that the port did not just go
5579 			 * from pt2pt to fabric or we could end
5580 			 * up matching ndlp->nlp_DID 000001 to
5581 			 * fabric DID 0x20101
5582 			 */
5583 			if ((ndlpdid.un.b.domain == 0) &&
5584 			    (ndlpdid.un.b.area == 0)) {
5585 				if (ndlpdid.un.b.id &&
5586 				    vport->phba->fc_topology ==
5587 				    LPFC_TOPOLOGY_LOOP)
5588 					return 1;
5589 			}
5590 			return 0;
5591 		}
5592 
5593 		matchdid.un.word = ndlp->nlp_DID;
5594 		if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5595 		    (mydid.un.b.area == ndlpdid.un.b.area)) {
5596 			if ((matchdid.un.b.domain == 0) &&
5597 			    (matchdid.un.b.area == 0)) {
5598 				if (matchdid.un.b.id)
5599 					return 1;
5600 			}
5601 		}
5602 	}
5603 	return 0;
5604 }
5605 
5606 /* Search for a nodelist entry */
5607 static struct lpfc_nodelist *
5608 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5609 {
5610 	struct lpfc_nodelist *ndlp;
5611 	uint32_t data1;
5612 
5613 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5614 		if (lpfc_matchdid(vport, ndlp, did)) {
5615 			data1 = (((uint32_t)ndlp->nlp_state << 24) |
5616 				 ((uint32_t)ndlp->nlp_xri << 16) |
5617 				 ((uint32_t)ndlp->nlp_type << 8)
5618 				 );
5619 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE_VERBOSE,
5620 					 "0929 FIND node DID "
5621 					 "Data: x%px x%x x%x x%x x%x x%px\n",
5622 					 ndlp, ndlp->nlp_DID,
5623 					 ndlp->nlp_flag, data1, ndlp->nlp_rpi,
5624 					 ndlp->active_rrqs_xri_bitmap);
5625 			return ndlp;
5626 		}
5627 	}
5628 
5629 	/* FIND node did <did> NOT FOUND */
5630 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5631 			 "0932 FIND node did x%x NOT FOUND.\n", did);
5632 	return NULL;
5633 }
5634 
5635 struct lpfc_nodelist *
5636 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5637 {
5638 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5639 	struct lpfc_nodelist *ndlp;
5640 	unsigned long iflags;
5641 
5642 	spin_lock_irqsave(shost->host_lock, iflags);
5643 	ndlp = __lpfc_findnode_did(vport, did);
5644 	spin_unlock_irqrestore(shost->host_lock, iflags);
5645 	return ndlp;
5646 }
5647 
5648 struct lpfc_nodelist *
5649 lpfc_findnode_mapped(struct lpfc_vport *vport)
5650 {
5651 	struct lpfc_nodelist *ndlp;
5652 	uint32_t data1;
5653 	unsigned long iflags;
5654 
5655 	spin_lock_irqsave(&vport->fc_nodes_list_lock, iflags);
5656 
5657 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5658 		if (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
5659 		    ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
5660 			data1 = (((uint32_t)ndlp->nlp_state << 24) |
5661 				 ((uint32_t)ndlp->nlp_xri << 16) |
5662 				 ((uint32_t)ndlp->nlp_type << 8) |
5663 				 ((uint32_t)ndlp->nlp_rpi & 0xff));
5664 			spin_unlock_irqrestore(&vport->fc_nodes_list_lock,
5665 					       iflags);
5666 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE_VERBOSE,
5667 					 "2025 FIND node DID MAPPED "
5668 					 "Data: x%px x%x x%x x%x x%px\n",
5669 					 ndlp, ndlp->nlp_DID,
5670 					 ndlp->nlp_flag, data1,
5671 					 ndlp->active_rrqs_xri_bitmap);
5672 			return ndlp;
5673 		}
5674 	}
5675 	spin_unlock_irqrestore(&vport->fc_nodes_list_lock, iflags);
5676 
5677 	/* FIND node did <did> NOT FOUND */
5678 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5679 			 "2026 FIND mapped did NOT FOUND.\n");
5680 	return NULL;
5681 }
5682 
5683 struct lpfc_nodelist *
5684 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5685 {
5686 	struct lpfc_nodelist *ndlp;
5687 
5688 	ndlp = lpfc_findnode_did(vport, did);
5689 	if (!ndlp) {
5690 		if (vport->phba->nvmet_support)
5691 			return NULL;
5692 		if (test_bit(FC_RSCN_MODE, &vport->fc_flag) &&
5693 		    lpfc_rscn_payload_check(vport, did) == 0)
5694 			return NULL;
5695 		ndlp = lpfc_nlp_init(vport, did);
5696 		if (!ndlp)
5697 			return NULL;
5698 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5699 
5700 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5701 				 "6453 Setup New Node 2B_DISC x%x "
5702 				 "Data:x%x x%x x%lx\n",
5703 				 ndlp->nlp_DID, ndlp->nlp_flag,
5704 				 ndlp->nlp_state, vport->fc_flag);
5705 
5706 		spin_lock_irq(&ndlp->lock);
5707 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5708 		spin_unlock_irq(&ndlp->lock);
5709 		return ndlp;
5710 	}
5711 
5712 	/* The NVME Target does not want to actively manage an rport.
5713 	 * The goal is to allow the target to reset its state and clear
5714 	 * pending IO in preparation for the initiator to recover.
5715 	 */
5716 	if (test_bit(FC_RSCN_MODE, &vport->fc_flag) &&
5717 	    !test_bit(FC_NDISC_ACTIVE, &vport->fc_flag)) {
5718 		if (lpfc_rscn_payload_check(vport, did)) {
5719 
5720 			/* Since this node is marked for discovery,
5721 			 * delay timeout is not needed.
5722 			 */
5723 			lpfc_cancel_retry_delay_tmo(vport, ndlp);
5724 
5725 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5726 					 "6455 Setup RSCN Node 2B_DISC x%x "
5727 					 "Data:x%x x%x x%lx\n",
5728 					 ndlp->nlp_DID, ndlp->nlp_flag,
5729 					 ndlp->nlp_state, vport->fc_flag);
5730 
5731 			/* NVME Target mode waits until rport is known to be
5732 			 * impacted by the RSCN before it transitions.  No
5733 			 * active management - just go to NPR provided the
5734 			 * node had a valid login.
5735 			 */
5736 			if (vport->phba->nvmet_support)
5737 				return ndlp;
5738 
5739 			if (ndlp->nlp_state > NLP_STE_UNUSED_NODE &&
5740 			    ndlp->nlp_state < NLP_STE_PRLI_ISSUE) {
5741 				lpfc_disc_state_machine(vport, ndlp, NULL,
5742 							NLP_EVT_DEVICE_RECOVERY);
5743 			}
5744 
5745 			spin_lock_irq(&ndlp->lock);
5746 			ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5747 			spin_unlock_irq(&ndlp->lock);
5748 		} else {
5749 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5750 					 "6456 Skip Setup RSCN Node x%x "
5751 					 "Data:x%x x%x x%lx\n",
5752 					 ndlp->nlp_DID, ndlp->nlp_flag,
5753 					 ndlp->nlp_state, vport->fc_flag);
5754 			ndlp = NULL;
5755 		}
5756 	} else {
5757 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5758 				 "6457 Setup Active Node 2B_DISC x%x "
5759 				 "Data:x%x x%x x%lx\n",
5760 				 ndlp->nlp_DID, ndlp->nlp_flag,
5761 				 ndlp->nlp_state, vport->fc_flag);
5762 
5763 		/* If the initiator received a PLOGI from this NPort or if the
5764 		 * initiator is already in the process of discovery on it,
5765 		 * there's no need to try to discover it again.
5766 		 */
5767 		if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
5768 		    ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5769 		    (!vport->phba->nvmet_support &&
5770 		     ndlp->nlp_flag & NLP_RCV_PLOGI))
5771 			return NULL;
5772 
5773 		if (vport->phba->nvmet_support)
5774 			return ndlp;
5775 
5776 		/* Moving to NPR state clears unsolicited flags and
5777 		 * allows for rediscovery
5778 		 */
5779 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5780 
5781 		spin_lock_irq(&ndlp->lock);
5782 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5783 		spin_unlock_irq(&ndlp->lock);
5784 	}
5785 	return ndlp;
5786 }
5787 
5788 /* Build a list of nodes to discover based on the loopmap */
5789 void
5790 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
5791 {
5792 	struct lpfc_hba  *phba = vport->phba;
5793 	int j;
5794 	uint32_t alpa, index;
5795 
5796 	if (!lpfc_is_link_up(phba))
5797 		return;
5798 
5799 	if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
5800 		return;
5801 
5802 	/* Check for loop map present or not */
5803 	if (phba->alpa_map[0]) {
5804 		for (j = 1; j <= phba->alpa_map[0]; j++) {
5805 			alpa = phba->alpa_map[j];
5806 			if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
5807 				continue;
5808 			lpfc_setup_disc_node(vport, alpa);
5809 		}
5810 	} else {
5811 		/* No alpamap, so try all alpa's */
5812 		for (j = 0; j < FC_MAXLOOP; j++) {
5813 			/* If cfg_scan_down is set, start from highest
5814 			 * ALPA (0xef) to lowest (0x1).
5815 			 */
5816 			if (vport->cfg_scan_down)
5817 				index = j;
5818 			else
5819 				index = FC_MAXLOOP - j - 1;
5820 			alpa = lpfcAlpaArray[index];
5821 			if ((vport->fc_myDID & 0xff) == alpa)
5822 				continue;
5823 			lpfc_setup_disc_node(vport, alpa);
5824 		}
5825 	}
5826 	return;
5827 }
5828 
5829 /* SLI3 only */
5830 void
5831 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
5832 {
5833 	LPFC_MBOXQ_t *mbox;
5834 	struct lpfc_sli *psli = &phba->sli;
5835 	struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5836 	struct lpfc_sli_ring *fcp_ring   = &psli->sli3_ring[LPFC_FCP_RING];
5837 	int  rc;
5838 
5839 	/*
5840 	 * if it's not a physical port or if we already send
5841 	 * clear_la then don't send it.
5842 	 */
5843 	if ((phba->link_state >= LPFC_CLEAR_LA) ||
5844 	    (vport->port_type != LPFC_PHYSICAL_PORT) ||
5845 		(phba->sli_rev == LPFC_SLI_REV4))
5846 		return;
5847 
5848 			/* Link up discovery */
5849 	if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5850 		phba->link_state = LPFC_CLEAR_LA;
5851 		lpfc_clear_la(phba, mbox);
5852 		mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5853 		mbox->vport = vport;
5854 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5855 		if (rc == MBX_NOT_FINISHED) {
5856 			mempool_free(mbox, phba->mbox_mem_pool);
5857 			lpfc_disc_flush_list(vport);
5858 			extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5859 			fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5860 			phba->link_state = LPFC_HBA_ERROR;
5861 		}
5862 	}
5863 }
5864 
5865 /* Reg_vpi to tell firmware to resume normal operations */
5866 void
5867 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5868 {
5869 	LPFC_MBOXQ_t *regvpimbox;
5870 
5871 	regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5872 	if (regvpimbox) {
5873 		lpfc_reg_vpi(vport, regvpimbox);
5874 		regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5875 		regvpimbox->vport = vport;
5876 		if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
5877 					== MBX_NOT_FINISHED) {
5878 			mempool_free(regvpimbox, phba->mbox_mem_pool);
5879 		}
5880 	}
5881 }
5882 
5883 /* Start Link up / RSCN discovery on NPR nodes */
5884 void
5885 lpfc_disc_start(struct lpfc_vport *vport)
5886 {
5887 	struct lpfc_hba  *phba = vport->phba;
5888 	uint32_t num_sent;
5889 	uint32_t clear_la_pending;
5890 
5891 	if (!lpfc_is_link_up(phba)) {
5892 		lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5893 				 "3315 Link is not up %x\n",
5894 				 phba->link_state);
5895 		return;
5896 	}
5897 
5898 	if (phba->link_state == LPFC_CLEAR_LA)
5899 		clear_la_pending = 1;
5900 	else
5901 		clear_la_pending = 0;
5902 
5903 	if (vport->port_state < LPFC_VPORT_READY)
5904 		vport->port_state = LPFC_DISC_AUTH;
5905 
5906 	lpfc_set_disctmo(vport);
5907 
5908 	vport->fc_prevDID = vport->fc_myDID;
5909 	vport->num_disc_nodes = 0;
5910 
5911 	/* Start Discovery state <hba_state> */
5912 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5913 			 "0202 Start Discovery port state x%x "
5914 			 "flg x%lx Data: x%x x%x x%x\n",
5915 			 vport->port_state, vport->fc_flag,
5916 			 atomic_read(&vport->fc_plogi_cnt),
5917 			 atomic_read(&vport->fc_adisc_cnt),
5918 			 atomic_read(&vport->fc_npr_cnt));
5919 
5920 	/* First do ADISCs - if any */
5921 	num_sent = lpfc_els_disc_adisc(vport);
5922 
5923 	if (num_sent)
5924 		return;
5925 
5926 	/* Register the VPI for SLI3, NPIV only. */
5927 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5928 	    !test_bit(FC_PT2PT, &vport->fc_flag) &&
5929 	    !test_bit(FC_RSCN_MODE, &vport->fc_flag) &&
5930 	    (phba->sli_rev < LPFC_SLI_REV4)) {
5931 		lpfc_issue_clear_la(phba, vport);
5932 		lpfc_issue_reg_vpi(phba, vport);
5933 		return;
5934 	}
5935 
5936 	/*
5937 	 * For SLI2, we need to set port_state to READY and continue
5938 	 * discovery.
5939 	 */
5940 	if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5941 		/* If we get here, there is nothing to ADISC */
5942 		lpfc_issue_clear_la(phba, vport);
5943 
5944 		if (!test_bit(FC_ABORT_DISCOVERY, &vport->fc_flag)) {
5945 			vport->num_disc_nodes = 0;
5946 			/* go thru NPR nodes and issue ELS PLOGIs */
5947 			if (atomic_read(&vport->fc_npr_cnt))
5948 				lpfc_els_disc_plogi(vport);
5949 
5950 			if (!vport->num_disc_nodes) {
5951 				clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
5952 				lpfc_can_disctmo(vport);
5953 			}
5954 		}
5955 		vport->port_state = LPFC_VPORT_READY;
5956 	} else {
5957 		/* Next do PLOGIs - if any */
5958 		num_sent = lpfc_els_disc_plogi(vport);
5959 
5960 		if (num_sent)
5961 			return;
5962 
5963 		if (test_bit(FC_RSCN_MODE, &vport->fc_flag)) {
5964 			/* Check to see if more RSCNs came in while we
5965 			 * were processing this one.
5966 			 */
5967 			if (vport->fc_rscn_id_cnt == 0 &&
5968 			    !test_bit(FC_RSCN_DISCOVERY, &vport->fc_flag)) {
5969 				clear_bit(FC_RSCN_MODE, &vport->fc_flag);
5970 				lpfc_can_disctmo(vport);
5971 			} else {
5972 				lpfc_els_handle_rscn(vport);
5973 			}
5974 		}
5975 	}
5976 	return;
5977 }
5978 
5979 /*
5980  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5981  *  ring the match the sppecified nodelist.
5982  */
5983 static void
5984 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
5985 {
5986 	LIST_HEAD(completions);
5987 	struct lpfc_iocbq    *iocb, *next_iocb;
5988 	struct lpfc_sli_ring *pring;
5989 	u32 ulp_command;
5990 
5991 	pring = lpfc_phba_elsring(phba);
5992 	if (unlikely(!pring))
5993 		return;
5994 
5995 	/* Error matching iocb on txq or txcmplq
5996 	 * First check the txq.
5997 	 */
5998 	spin_lock_irq(&phba->hbalock);
5999 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6000 		if (iocb->ndlp != ndlp)
6001 			continue;
6002 
6003 		ulp_command = get_job_cmnd(phba, iocb);
6004 
6005 		if (ulp_command == CMD_ELS_REQUEST64_CR ||
6006 		    ulp_command == CMD_XMIT_ELS_RSP64_CX) {
6007 
6008 			list_move_tail(&iocb->list, &completions);
6009 		}
6010 	}
6011 
6012 	/* Next check the txcmplq */
6013 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
6014 		if (iocb->ndlp != ndlp)
6015 			continue;
6016 
6017 		ulp_command = get_job_cmnd(phba, iocb);
6018 
6019 		if (ulp_command == CMD_ELS_REQUEST64_CR ||
6020 		    ulp_command == CMD_XMIT_ELS_RSP64_CX) {
6021 			lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
6022 		}
6023 	}
6024 	spin_unlock_irq(&phba->hbalock);
6025 
6026 	/* Make sure HBA is alive */
6027 	lpfc_issue_hb_tmo(phba);
6028 
6029 	/* Cancel all the IOCBs from the completions list */
6030 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6031 			      IOERR_SLI_ABORTED);
6032 }
6033 
6034 static void
6035 lpfc_disc_flush_list(struct lpfc_vport *vport)
6036 {
6037 	struct lpfc_nodelist *ndlp, *next_ndlp;
6038 	struct lpfc_hba *phba = vport->phba;
6039 
6040 	if (atomic_read(&vport->fc_plogi_cnt) ||
6041 	    atomic_read(&vport->fc_adisc_cnt)) {
6042 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
6043 					 nlp_listp) {
6044 			if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
6045 			    ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
6046 				lpfc_free_tx(phba, ndlp);
6047 			}
6048 		}
6049 	}
6050 }
6051 
6052 /*
6053  * lpfc_notify_xport_npr - notifies xport of node disappearance
6054  * @vport: Pointer to Virtual Port object.
6055  *
6056  * Transitions all ndlps to NPR state.  When lpfc_nlp_set_state
6057  * calls lpfc_nlp_state_cleanup, the ndlp->rport is unregistered
6058  * and transport notified that the node is gone.
6059  * Return Code:
6060  *	none
6061  */
6062 static void
6063 lpfc_notify_xport_npr(struct lpfc_vport *vport)
6064 {
6065 	struct lpfc_nodelist *ndlp, *next_ndlp;
6066 
6067 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
6068 				 nlp_listp) {
6069 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
6070 	}
6071 }
6072 void
6073 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
6074 {
6075 	lpfc_els_flush_rscn(vport);
6076 	lpfc_els_flush_cmd(vport);
6077 	lpfc_disc_flush_list(vport);
6078 	if (pci_channel_offline(vport->phba->pcidev))
6079 		lpfc_notify_xport_npr(vport);
6080 }
6081 
6082 /*****************************************************************************/
6083 /*
6084  * NAME:     lpfc_disc_timeout
6085  *
6086  * FUNCTION: Fibre Channel driver discovery timeout routine.
6087  *
6088  * EXECUTION ENVIRONMENT: interrupt only
6089  *
6090  * CALLED FROM:
6091  *      Timer function
6092  *
6093  * RETURNS:
6094  *      none
6095  */
6096 /*****************************************************************************/
6097 void
6098 lpfc_disc_timeout(struct timer_list *t)
6099 {
6100 	struct lpfc_vport *vport = from_timer(vport, t, fc_disctmo);
6101 	struct lpfc_hba   *phba = vport->phba;
6102 	uint32_t tmo_posted;
6103 	unsigned long flags = 0;
6104 
6105 	if (unlikely(!phba))
6106 		return;
6107 
6108 	spin_lock_irqsave(&vport->work_port_lock, flags);
6109 	tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
6110 	if (!tmo_posted)
6111 		vport->work_port_events |= WORKER_DISC_TMO;
6112 	spin_unlock_irqrestore(&vport->work_port_lock, flags);
6113 
6114 	if (!tmo_posted)
6115 		lpfc_worker_wake_up(phba);
6116 	return;
6117 }
6118 
6119 static void
6120 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
6121 {
6122 	struct lpfc_hba  *phba = vport->phba;
6123 	struct lpfc_sli  *psli = &phba->sli;
6124 	struct lpfc_nodelist *ndlp, *next_ndlp;
6125 	LPFC_MBOXQ_t *initlinkmbox;
6126 	int rc, clrlaerr = 0;
6127 
6128 	if (!test_and_clear_bit(FC_DISC_TMO, &vport->fc_flag))
6129 		return;
6130 
6131 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6132 		"disc timeout:    state:x%x rtry:x%x flg:x%x",
6133 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
6134 
6135 	switch (vport->port_state) {
6136 
6137 	case LPFC_LOCAL_CFG_LINK:
6138 		/*
6139 		 * port_state is identically  LPFC_LOCAL_CFG_LINK while
6140 		 * waiting for FAN timeout
6141 		 */
6142 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
6143 				 "0221 FAN timeout\n");
6144 
6145 		/* Start discovery by sending FLOGI, clean up old rpis */
6146 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
6147 					 nlp_listp) {
6148 			if (ndlp->nlp_state != NLP_STE_NPR_NODE)
6149 				continue;
6150 			if (ndlp->nlp_type & NLP_FABRIC) {
6151 				/* Clean up the ndlp on Fabric connections */
6152 				lpfc_drop_node(vport, ndlp);
6153 
6154 			} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
6155 				/* Fail outstanding IO now since device
6156 				 * is marked for PLOGI.
6157 				 */
6158 				lpfc_unreg_rpi(vport, ndlp);
6159 			}
6160 		}
6161 		if (vport->port_state != LPFC_FLOGI) {
6162 			if (phba->sli_rev <= LPFC_SLI_REV3)
6163 				lpfc_initial_flogi(vport);
6164 			else
6165 				lpfc_issue_init_vfi(vport);
6166 			return;
6167 		}
6168 		break;
6169 
6170 	case LPFC_FDISC:
6171 	case LPFC_FLOGI:
6172 	/* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
6173 		/* Initial FLOGI timeout */
6174 		lpfc_printf_vlog(vport, KERN_ERR,
6175 				 LOG_TRACE_EVENT,
6176 				 "0222 Initial %s timeout\n",
6177 				 vport->vpi ? "FDISC" : "FLOGI");
6178 
6179 		/* Assume no Fabric and go on with discovery.
6180 		 * Check for outstanding ELS FLOGI to abort.
6181 		 */
6182 
6183 		/* FLOGI failed, so just use loop map to make discovery list */
6184 		lpfc_disc_list_loopmap(vport);
6185 
6186 		/* Start discovery */
6187 		lpfc_disc_start(vport);
6188 		break;
6189 
6190 	case LPFC_FABRIC_CFG_LINK:
6191 	/* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
6192 	   NameServer login */
6193 		lpfc_printf_vlog(vport, KERN_ERR,
6194 				 LOG_TRACE_EVENT,
6195 				 "0223 Timeout while waiting for "
6196 				 "NameServer login\n");
6197 		/* Next look for NameServer ndlp */
6198 		ndlp = lpfc_findnode_did(vport, NameServer_DID);
6199 		if (ndlp)
6200 			lpfc_els_abort(phba, ndlp);
6201 
6202 		/* ReStart discovery */
6203 		goto restart_disc;
6204 
6205 	case LPFC_NS_QRY:
6206 	/* Check for wait for NameServer Rsp timeout */
6207 		lpfc_printf_vlog(vport, KERN_ERR,
6208 				 LOG_TRACE_EVENT,
6209 				 "0224 NameServer Query timeout "
6210 				 "Data: x%x x%x\n",
6211 				 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
6212 
6213 		if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
6214 			/* Try it one more time */
6215 			vport->fc_ns_retry++;
6216 			vport->gidft_inp = 0;
6217 			rc = lpfc_issue_gidft(vport);
6218 			if (rc == 0)
6219 				break;
6220 		}
6221 		vport->fc_ns_retry = 0;
6222 
6223 restart_disc:
6224 		/*
6225 		 * Discovery is over.
6226 		 * set port_state to PORT_READY if SLI2.
6227 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
6228 		 */
6229 		if (phba->sli_rev < LPFC_SLI_REV4) {
6230 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
6231 				lpfc_issue_reg_vpi(phba, vport);
6232 			else  {
6233 				lpfc_issue_clear_la(phba, vport);
6234 				vport->port_state = LPFC_VPORT_READY;
6235 			}
6236 		}
6237 
6238 		/* Setup and issue mailbox INITIALIZE LINK command */
6239 		initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6240 		if (!initlinkmbox) {
6241 			lpfc_printf_vlog(vport, KERN_ERR,
6242 					 LOG_TRACE_EVENT,
6243 					 "0206 Device Discovery "
6244 					 "completion error\n");
6245 			phba->link_state = LPFC_HBA_ERROR;
6246 			break;
6247 		}
6248 
6249 		lpfc_linkdown(phba);
6250 		lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
6251 			       phba->cfg_link_speed);
6252 		initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
6253 		initlinkmbox->vport = vport;
6254 		initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6255 		rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
6256 		lpfc_set_loopback_flag(phba);
6257 		if (rc == MBX_NOT_FINISHED)
6258 			mempool_free(initlinkmbox, phba->mbox_mem_pool);
6259 
6260 		break;
6261 
6262 	case LPFC_DISC_AUTH:
6263 	/* Node Authentication timeout */
6264 		lpfc_printf_vlog(vport, KERN_ERR,
6265 				 LOG_TRACE_EVENT,
6266 				 "0227 Node Authentication timeout\n");
6267 		lpfc_disc_flush_list(vport);
6268 
6269 		/*
6270 		 * set port_state to PORT_READY if SLI2.
6271 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
6272 		 */
6273 		if (phba->sli_rev < LPFC_SLI_REV4) {
6274 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
6275 				lpfc_issue_reg_vpi(phba, vport);
6276 			else  {	/* NPIV Not enabled */
6277 				lpfc_issue_clear_la(phba, vport);
6278 				vport->port_state = LPFC_VPORT_READY;
6279 			}
6280 		}
6281 		break;
6282 
6283 	case LPFC_VPORT_READY:
6284 		if (test_bit(FC_RSCN_MODE, &vport->fc_flag)) {
6285 			lpfc_printf_vlog(vport, KERN_ERR,
6286 					 LOG_TRACE_EVENT,
6287 					 "0231 RSCN timeout Data: x%x "
6288 					 "x%x x%x x%x\n",
6289 					 vport->fc_ns_retry, LPFC_MAX_NS_RETRY,
6290 					 vport->port_state, vport->gidft_inp);
6291 
6292 			/* Cleanup any outstanding ELS commands */
6293 			lpfc_els_flush_cmd(vport);
6294 
6295 			lpfc_els_flush_rscn(vport);
6296 			lpfc_disc_flush_list(vport);
6297 		}
6298 		break;
6299 
6300 	default:
6301 		lpfc_printf_vlog(vport, KERN_ERR,
6302 				 LOG_TRACE_EVENT,
6303 				 "0273 Unexpected discovery timeout, "
6304 				 "vport State x%x\n", vport->port_state);
6305 		break;
6306 	}
6307 
6308 	switch (phba->link_state) {
6309 	case LPFC_CLEAR_LA:
6310 				/* CLEAR LA timeout */
6311 		lpfc_printf_vlog(vport, KERN_ERR,
6312 				 LOG_TRACE_EVENT,
6313 				 "0228 CLEAR LA timeout\n");
6314 		clrlaerr = 1;
6315 		break;
6316 
6317 	case LPFC_LINK_UP:
6318 		lpfc_issue_clear_la(phba, vport);
6319 		fallthrough;
6320 	case LPFC_LINK_UNKNOWN:
6321 	case LPFC_WARM_START:
6322 	case LPFC_INIT_START:
6323 	case LPFC_INIT_MBX_CMDS:
6324 	case LPFC_LINK_DOWN:
6325 	case LPFC_HBA_ERROR:
6326 		lpfc_printf_vlog(vport, KERN_ERR,
6327 				 LOG_TRACE_EVENT,
6328 				 "0230 Unexpected timeout, hba link "
6329 				 "state x%x\n", phba->link_state);
6330 		clrlaerr = 1;
6331 		break;
6332 
6333 	case LPFC_HBA_READY:
6334 		break;
6335 	}
6336 
6337 	if (clrlaerr) {
6338 		lpfc_disc_flush_list(vport);
6339 		if (phba->sli_rev != LPFC_SLI_REV4) {
6340 			psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
6341 				~LPFC_STOP_IOCB_EVENT;
6342 			psli->sli3_ring[LPFC_FCP_RING].flag &=
6343 				~LPFC_STOP_IOCB_EVENT;
6344 		}
6345 		vport->port_state = LPFC_VPORT_READY;
6346 	}
6347 	return;
6348 }
6349 
6350 /*
6351  * This routine handles processing a NameServer REG_LOGIN mailbox
6352  * command upon completion. It is setup in the LPFC_MBOXQ
6353  * as the completion routine when the command is
6354  * handed off to the SLI layer.
6355  */
6356 void
6357 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6358 {
6359 	MAILBOX_t *mb = &pmb->u.mb;
6360 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
6361 	struct lpfc_vport    *vport = pmb->vport;
6362 
6363 	pmb->ctx_ndlp = NULL;
6364 
6365 	if (phba->sli_rev < LPFC_SLI_REV4)
6366 		ndlp->nlp_rpi = mb->un.varWords[0];
6367 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
6368 	ndlp->nlp_type |= NLP_FABRIC;
6369 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
6370 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
6371 			 "0004 rpi:%x DID:%x flg:%x %d x%px\n",
6372 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
6373 			 kref_read(&ndlp->kref),
6374 			 ndlp);
6375 	/*
6376 	 * Start issuing Fabric-Device Management Interface (FDMI) command to
6377 	 * 0xfffffa (FDMI well known port).
6378 	 * DHBA -> DPRT -> RHBA -> RPA  (physical port)
6379 	 * DPRT -> RPRT (vports)
6380 	 */
6381 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
6382 		phba->link_flag &= ~LS_CT_VEN_RPA; /* For extra Vendor RPA */
6383 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
6384 	} else {
6385 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
6386 	}
6387 
6388 
6389 	/* decrement the node reference count held for this callback
6390 	 * function.
6391 	 */
6392 	lpfc_nlp_put(ndlp);
6393 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
6394 	return;
6395 }
6396 
6397 static int
6398 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
6399 {
6400 	uint16_t *rpi = param;
6401 
6402 	return ndlp->nlp_rpi == *rpi;
6403 }
6404 
6405 static int
6406 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
6407 {
6408 	return memcmp(&ndlp->nlp_portname, param,
6409 		      sizeof(ndlp->nlp_portname)) == 0;
6410 }
6411 
6412 static struct lpfc_nodelist *
6413 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
6414 {
6415 	struct lpfc_nodelist *ndlp;
6416 
6417 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
6418 		if (filter(ndlp, param)) {
6419 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE_VERBOSE,
6420 					 "3185 FIND node filter %ps DID "
6421 					 "ndlp x%px did x%x flg x%x st x%x "
6422 					 "xri x%x type x%x rpi x%x\n",
6423 					 filter, ndlp, ndlp->nlp_DID,
6424 					 ndlp->nlp_flag, ndlp->nlp_state,
6425 					 ndlp->nlp_xri, ndlp->nlp_type,
6426 					 ndlp->nlp_rpi);
6427 			return ndlp;
6428 		}
6429 	}
6430 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6431 			 "3186 FIND node filter %ps NOT FOUND.\n", filter);
6432 	return NULL;
6433 }
6434 
6435 /*
6436  * This routine looks up the ndlp lists for the given RPI. If rpi found it
6437  * returns the node list element pointer else return NULL.
6438  */
6439 struct lpfc_nodelist *
6440 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6441 {
6442 	return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
6443 }
6444 
6445 /*
6446  * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
6447  * returns the node element list pointer else return NULL.
6448  */
6449 struct lpfc_nodelist *
6450 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
6451 {
6452 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6453 	struct lpfc_nodelist *ndlp;
6454 
6455 	spin_lock_irq(shost->host_lock);
6456 	ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
6457 	spin_unlock_irq(shost->host_lock);
6458 	return ndlp;
6459 }
6460 
6461 /*
6462  * This routine looks up the ndlp lists for the given RPI. If the rpi
6463  * is found, the routine returns the node element list pointer else
6464  * return NULL.
6465  */
6466 struct lpfc_nodelist *
6467 lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6468 {
6469 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6470 	struct lpfc_nodelist *ndlp;
6471 	unsigned long flags;
6472 
6473 	spin_lock_irqsave(shost->host_lock, flags);
6474 	ndlp = __lpfc_findnode_rpi(vport, rpi);
6475 	spin_unlock_irqrestore(shost->host_lock, flags);
6476 	return ndlp;
6477 }
6478 
6479 /**
6480  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6481  * @phba: pointer to lpfc hba data structure.
6482  * @vpi: the physical host virtual N_Port identifier.
6483  *
6484  * This routine finds a vport on a HBA (referred by @phba) through a
6485  * @vpi. The function walks the HBA's vport list and returns the address
6486  * of the vport with the matching @vpi.
6487  *
6488  * Return code
6489  *    NULL - No vport with the matching @vpi found
6490  *    Otherwise - Address to the vport with the matching @vpi.
6491  **/
6492 struct lpfc_vport *
6493 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6494 {
6495 	struct lpfc_vport *vport;
6496 	unsigned long flags;
6497 	int i = 0;
6498 
6499 	/* The physical ports are always vpi 0 - translate is unnecessary. */
6500 	if (vpi > 0) {
6501 		/*
6502 		 * Translate the physical vpi to the logical vpi.  The
6503 		 * vport stores the logical vpi.
6504 		 */
6505 		for (i = 0; i <= phba->max_vpi; i++) {
6506 			if (vpi == phba->vpi_ids[i])
6507 				break;
6508 		}
6509 
6510 		if (i > phba->max_vpi) {
6511 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6512 					"2936 Could not find Vport mapped "
6513 					"to vpi %d\n", vpi);
6514 			return NULL;
6515 		}
6516 	}
6517 
6518 	spin_lock_irqsave(&phba->port_list_lock, flags);
6519 	list_for_each_entry(vport, &phba->port_list, listentry) {
6520 		if (vport->vpi == i) {
6521 			spin_unlock_irqrestore(&phba->port_list_lock, flags);
6522 			return vport;
6523 		}
6524 	}
6525 	spin_unlock_irqrestore(&phba->port_list_lock, flags);
6526 	return NULL;
6527 }
6528 
6529 struct lpfc_nodelist *
6530 lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
6531 {
6532 	struct lpfc_nodelist *ndlp;
6533 	int rpi = LPFC_RPI_ALLOC_ERROR;
6534 
6535 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6536 		rpi = lpfc_sli4_alloc_rpi(vport->phba);
6537 		if (rpi == LPFC_RPI_ALLOC_ERROR)
6538 			return NULL;
6539 	}
6540 
6541 	ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
6542 	if (!ndlp) {
6543 		if (vport->phba->sli_rev == LPFC_SLI_REV4)
6544 			lpfc_sli4_free_rpi(vport->phba, rpi);
6545 		return NULL;
6546 	}
6547 
6548 	memset(ndlp, 0, sizeof (struct lpfc_nodelist));
6549 
6550 	spin_lock_init(&ndlp->lock);
6551 
6552 	lpfc_initialize_node(vport, ndlp, did);
6553 	INIT_LIST_HEAD(&ndlp->nlp_listp);
6554 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6555 		ndlp->nlp_rpi = rpi;
6556 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
6557 				 "0007 Init New ndlp x%px, rpi:x%x DID:%x "
6558 				 "flg:x%x refcnt:%d\n",
6559 				 ndlp, ndlp->nlp_rpi, ndlp->nlp_DID,
6560 				 ndlp->nlp_flag, kref_read(&ndlp->kref));
6561 
6562 		ndlp->active_rrqs_xri_bitmap =
6563 				mempool_alloc(vport->phba->active_rrq_pool,
6564 					      GFP_KERNEL);
6565 		if (ndlp->active_rrqs_xri_bitmap)
6566 			memset(ndlp->active_rrqs_xri_bitmap, 0,
6567 			       ndlp->phba->cfg_rrq_xri_bitmap_sz);
6568 	}
6569 
6570 
6571 
6572 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
6573 		"node init:       did:x%x",
6574 		ndlp->nlp_DID, 0, 0);
6575 
6576 	return ndlp;
6577 }
6578 
6579 /* This routine releases all resources associated with a specifc NPort's ndlp
6580  * and mempool_free's the nodelist.
6581  */
6582 static void
6583 lpfc_nlp_release(struct kref *kref)
6584 {
6585 	struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
6586 						  kref);
6587 	struct lpfc_vport *vport = ndlp->vport;
6588 
6589 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6590 		"node release:    did:x%x flg:x%x type:x%x",
6591 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
6592 
6593 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6594 			 "0279 %s: ndlp: x%px did %x refcnt:%d rpi:%x\n",
6595 			 __func__, ndlp, ndlp->nlp_DID,
6596 			 kref_read(&ndlp->kref), ndlp->nlp_rpi);
6597 
6598 	/* remove ndlp from action. */
6599 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
6600 	lpfc_cleanup_node(vport, ndlp);
6601 
6602 	/* Not all ELS transactions have registered the RPI with the port.
6603 	 * In these cases the rpi usage is temporary and the node is
6604 	 * released when the WQE is completed.  Catch this case to free the
6605 	 * RPI to the pool.  Because this node is in the release path, a lock
6606 	 * is unnecessary.  All references are gone and the node has been
6607 	 * dequeued.
6608 	 */
6609 	if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
6610 		if (ndlp->nlp_rpi != LPFC_RPI_ALLOC_ERROR &&
6611 		    !(ndlp->nlp_flag & (NLP_RPI_REGISTERED | NLP_UNREG_INP))) {
6612 			lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
6613 			ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
6614 		}
6615 	}
6616 
6617 	/* The node is not freed back to memory, it is released to a pool so
6618 	 * the node fields need to be cleaned up.
6619 	 */
6620 	ndlp->vport = NULL;
6621 	ndlp->nlp_state = NLP_STE_FREED_NODE;
6622 	ndlp->nlp_flag = 0;
6623 	ndlp->fc4_xpt_flags = 0;
6624 
6625 	/* free ndlp memory for final ndlp release */
6626 	if (ndlp->phba->sli_rev == LPFC_SLI_REV4)
6627 		mempool_free(ndlp->active_rrqs_xri_bitmap,
6628 				ndlp->phba->active_rrq_pool);
6629 	mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
6630 }
6631 
6632 /* This routine bumps the reference count for a ndlp structure to ensure
6633  * that one discovery thread won't free a ndlp while another discovery thread
6634  * is using it.
6635  */
6636 struct lpfc_nodelist *
6637 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6638 {
6639 	unsigned long flags;
6640 
6641 	if (ndlp) {
6642 		lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6643 			"node get:        did:x%x flg:x%x refcnt:x%x",
6644 			ndlp->nlp_DID, ndlp->nlp_flag,
6645 			kref_read(&ndlp->kref));
6646 
6647 		/* The check of ndlp usage to prevent incrementing the
6648 		 * ndlp reference count that is in the process of being
6649 		 * released.
6650 		 */
6651 		spin_lock_irqsave(&ndlp->lock, flags);
6652 		if (!kref_get_unless_zero(&ndlp->kref)) {
6653 			spin_unlock_irqrestore(&ndlp->lock, flags);
6654 			lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6655 				"0276 %s: ndlp:x%px refcnt:%d\n",
6656 				__func__, (void *)ndlp, kref_read(&ndlp->kref));
6657 			return NULL;
6658 		}
6659 		spin_unlock_irqrestore(&ndlp->lock, flags);
6660 	} else {
6661 		WARN_ONCE(!ndlp, "**** %s, get ref on NULL ndlp!", __func__);
6662 	}
6663 
6664 	return ndlp;
6665 }
6666 
6667 /* This routine decrements the reference count for a ndlp structure. If the
6668  * count goes to 0, this indicates the associated nodelist should be freed.
6669  */
6670 int
6671 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6672 {
6673 	if (ndlp) {
6674 		lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6675 				"node put:        did:x%x flg:x%x refcnt:x%x",
6676 				ndlp->nlp_DID, ndlp->nlp_flag,
6677 				kref_read(&ndlp->kref));
6678 	} else {
6679 		WARN_ONCE(!ndlp, "**** %s, put ref on NULL ndlp!", __func__);
6680 	}
6681 
6682 	return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
6683 }
6684 
6685 /**
6686  * lpfc_fcf_inuse - Check if FCF can be unregistered.
6687  * @phba: Pointer to hba context object.
6688  *
6689  * This function iterate through all FC nodes associated
6690  * will all vports to check if there is any node with
6691  * fc_rports associated with it. If there is an fc_rport
6692  * associated with the node, then the node is either in
6693  * discovered state or its devloss_timer is pending.
6694  */
6695 static int
6696 lpfc_fcf_inuse(struct lpfc_hba *phba)
6697 {
6698 	struct lpfc_vport **vports;
6699 	int i, ret = 0;
6700 	struct lpfc_nodelist *ndlp;
6701 	unsigned long iflags;
6702 
6703 	vports = lpfc_create_vport_work_array(phba);
6704 
6705 	/* If driver cannot allocate memory, indicate fcf is in use */
6706 	if (!vports)
6707 		return 1;
6708 
6709 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6710 		/*
6711 		 * IF the CVL_RCVD bit is not set then we have sent the
6712 		 * flogi.
6713 		 * If dev_loss fires while we are waiting we do not want to
6714 		 * unreg the fcf.
6715 		 */
6716 		if (!test_bit(FC_VPORT_CVL_RCVD, &vports[i]->fc_flag)) {
6717 			ret =  1;
6718 			goto out;
6719 		}
6720 		spin_lock_irqsave(&vports[i]->fc_nodes_list_lock, iflags);
6721 		list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6722 			if (ndlp->rport &&
6723 			  (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6724 				ret = 1;
6725 				spin_unlock_irqrestore(&vports[i]->fc_nodes_list_lock,
6726 						       iflags);
6727 				goto out;
6728 			} else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6729 				ret = 1;
6730 				lpfc_printf_log(phba, KERN_INFO,
6731 						LOG_NODE | LOG_DISCOVERY,
6732 						"2624 RPI %x DID %x flag %x "
6733 						"still logged in\n",
6734 						ndlp->nlp_rpi, ndlp->nlp_DID,
6735 						ndlp->nlp_flag);
6736 			}
6737 		}
6738 		spin_unlock_irqrestore(&vports[i]->fc_nodes_list_lock, iflags);
6739 	}
6740 out:
6741 	lpfc_destroy_vport_work_array(phba, vports);
6742 	return ret;
6743 }
6744 
6745 /**
6746  * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6747  * @phba: Pointer to hba context object.
6748  * @mboxq: Pointer to mailbox object.
6749  *
6750  * This function frees memory associated with the mailbox command.
6751  */
6752 void
6753 lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6754 {
6755 	struct lpfc_vport *vport = mboxq->vport;
6756 
6757 	if (mboxq->u.mb.mbxStatus) {
6758 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6759 				"2555 UNREG_VFI mbxStatus error x%x "
6760 				"HBA state x%x\n",
6761 				mboxq->u.mb.mbxStatus, vport->port_state);
6762 	}
6763 	clear_bit(FC_VFI_REGISTERED, &phba->pport->fc_flag);
6764 	mempool_free(mboxq, phba->mbox_mem_pool);
6765 	return;
6766 }
6767 
6768 /**
6769  * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6770  * @phba: Pointer to hba context object.
6771  * @mboxq: Pointer to mailbox object.
6772  *
6773  * This function frees memory associated with the mailbox command.
6774  */
6775 static void
6776 lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6777 {
6778 	struct lpfc_vport *vport = mboxq->vport;
6779 
6780 	if (mboxq->u.mb.mbxStatus) {
6781 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6782 				"2550 UNREG_FCFI mbxStatus error x%x "
6783 				"HBA state x%x\n",
6784 				mboxq->u.mb.mbxStatus, vport->port_state);
6785 	}
6786 	mempool_free(mboxq, phba->mbox_mem_pool);
6787 	return;
6788 }
6789 
6790 /**
6791  * lpfc_unregister_fcf_prep - Unregister fcf record preparation
6792  * @phba: Pointer to hba context object.
6793  *
6794  * This function prepare the HBA for unregistering the currently registered
6795  * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6796  * VFIs.
6797  */
6798 int
6799 lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
6800 {
6801 	struct lpfc_vport **vports;
6802 	struct lpfc_nodelist *ndlp;
6803 	struct Scsi_Host *shost;
6804 	int i = 0, rc;
6805 
6806 	/* Unregister RPIs */
6807 	if (lpfc_fcf_inuse(phba))
6808 		lpfc_unreg_hba_rpis(phba);
6809 
6810 	/* At this point, all discovery is aborted */
6811 	phba->pport->port_state = LPFC_VPORT_UNKNOWN;
6812 
6813 	/* Unregister VPIs */
6814 	vports = lpfc_create_vport_work_array(phba);
6815 	if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
6816 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6817 			/* Stop FLOGI/FDISC retries */
6818 			ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6819 			if (ndlp)
6820 				lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
6821 			lpfc_cleanup_pending_mbox(vports[i]);
6822 			if (phba->sli_rev == LPFC_SLI_REV4)
6823 				lpfc_sli4_unreg_all_rpis(vports[i]);
6824 			lpfc_mbx_unreg_vpi(vports[i]);
6825 			shost = lpfc_shost_from_vport(vports[i]);
6826 			spin_lock_irq(shost->host_lock);
6827 			vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
6828 			spin_unlock_irq(shost->host_lock);
6829 			set_bit(FC_VPORT_NEEDS_INIT_VPI, &vports[i]->fc_flag);
6830 		}
6831 	lpfc_destroy_vport_work_array(phba, vports);
6832 	if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6833 		ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6834 		if (ndlp)
6835 			lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6836 		lpfc_cleanup_pending_mbox(phba->pport);
6837 		if (phba->sli_rev == LPFC_SLI_REV4)
6838 			lpfc_sli4_unreg_all_rpis(phba->pport);
6839 		lpfc_mbx_unreg_vpi(phba->pport);
6840 		shost = lpfc_shost_from_vport(phba->pport);
6841 		spin_lock_irq(shost->host_lock);
6842 		phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6843 		spin_unlock_irq(shost->host_lock);
6844 		set_bit(FC_VPORT_NEEDS_INIT_VPI, &phba->pport->fc_flag);
6845 	}
6846 
6847 	/* Cleanup any outstanding ELS commands */
6848 	lpfc_els_flush_all_cmd(phba);
6849 
6850 	/* Unregister the physical port VFI */
6851 	rc = lpfc_issue_unreg_vfi(phba->pport);
6852 	return rc;
6853 }
6854 
6855 /**
6856  * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6857  * @phba: Pointer to hba context object.
6858  *
6859  * This function issues synchronous unregister FCF mailbox command to HBA to
6860  * unregister the currently registered FCF record. The driver does not reset
6861  * the driver FCF usage state flags.
6862  *
6863  * Return 0 if successfully issued, none-zero otherwise.
6864  */
6865 int
6866 lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6867 {
6868 	LPFC_MBOXQ_t *mbox;
6869 	int rc;
6870 
6871 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6872 	if (!mbox) {
6873 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6874 				"2551 UNREG_FCFI mbox allocation failed"
6875 				"HBA state x%x\n", phba->pport->port_state);
6876 		return -ENOMEM;
6877 	}
6878 	lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6879 	mbox->vport = phba->pport;
6880 	mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6881 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6882 
6883 	if (rc == MBX_NOT_FINISHED) {
6884 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6885 				"2552 Unregister FCFI command failed rc x%x "
6886 				"HBA state x%x\n",
6887 				rc, phba->pport->port_state);
6888 		return -EINVAL;
6889 	}
6890 	return 0;
6891 }
6892 
6893 /**
6894  * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6895  * @phba: Pointer to hba context object.
6896  *
6897  * This function unregisters the currently reigstered FCF. This function
6898  * also tries to find another FCF for discovery by rescan the HBA FCF table.
6899  */
6900 void
6901 lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6902 {
6903 	int rc;
6904 
6905 	/* Preparation for unregistering fcf */
6906 	rc = lpfc_unregister_fcf_prep(phba);
6907 	if (rc) {
6908 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6909 				"2748 Failed to prepare for unregistering "
6910 				"HBA's FCF record: rc=%d\n", rc);
6911 		return;
6912 	}
6913 
6914 	/* Now, unregister FCF record and reset HBA FCF state */
6915 	rc = lpfc_sli4_unregister_fcf(phba);
6916 	if (rc)
6917 		return;
6918 	/* Reset HBA FCF states after successful unregister FCF */
6919 	spin_lock_irq(&phba->hbalock);
6920 	phba->fcf.fcf_flag = 0;
6921 	spin_unlock_irq(&phba->hbalock);
6922 	phba->fcf.current_rec.flag = 0;
6923 
6924 	/*
6925 	 * If driver is not unloading, check if there is any other
6926 	 * FCF record that can be used for discovery.
6927 	 */
6928 	if (test_bit(FC_UNLOADING, &phba->pport->load_flag) ||
6929 	    phba->link_state < LPFC_LINK_UP)
6930 		return;
6931 
6932 	/* This is considered as the initial FCF discovery scan */
6933 	spin_lock_irq(&phba->hbalock);
6934 	phba->fcf.fcf_flag |= FCF_INIT_DISC;
6935 	spin_unlock_irq(&phba->hbalock);
6936 
6937 	/* Reset FCF roundrobin bmask for new discovery */
6938 	lpfc_sli4_clear_fcf_rr_bmask(phba);
6939 
6940 	rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
6941 
6942 	if (rc) {
6943 		spin_lock_irq(&phba->hbalock);
6944 		phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6945 		spin_unlock_irq(&phba->hbalock);
6946 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6947 				"2553 lpfc_unregister_unused_fcf failed "
6948 				"to read FCF record HBA state x%x\n",
6949 				phba->pport->port_state);
6950 	}
6951 }
6952 
6953 /**
6954  * lpfc_unregister_fcf - Unregister the currently registered fcf record
6955  * @phba: Pointer to hba context object.
6956  *
6957  * This function just unregisters the currently reigstered FCF. It does not
6958  * try to find another FCF for discovery.
6959  */
6960 void
6961 lpfc_unregister_fcf(struct lpfc_hba *phba)
6962 {
6963 	int rc;
6964 
6965 	/* Preparation for unregistering fcf */
6966 	rc = lpfc_unregister_fcf_prep(phba);
6967 	if (rc) {
6968 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6969 				"2749 Failed to prepare for unregistering "
6970 				"HBA's FCF record: rc=%d\n", rc);
6971 		return;
6972 	}
6973 
6974 	/* Now, unregister FCF record and reset HBA FCF state */
6975 	rc = lpfc_sli4_unregister_fcf(phba);
6976 	if (rc)
6977 		return;
6978 	/* Set proper HBA FCF states after successful unregister FCF */
6979 	spin_lock_irq(&phba->hbalock);
6980 	phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6981 	spin_unlock_irq(&phba->hbalock);
6982 }
6983 
6984 /**
6985  * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6986  * @phba: Pointer to hba context object.
6987  *
6988  * This function check if there are any connected remote port for the FCF and
6989  * if all the devices are disconnected, this function unregister FCFI.
6990  * This function also tries to use another FCF for discovery.
6991  */
6992 void
6993 lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6994 {
6995 	/*
6996 	 * If HBA is not running in FIP mode, if HBA does not support
6997 	 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6998 	 * registered, do nothing.
6999 	 */
7000 	spin_lock_irq(&phba->hbalock);
7001 	if (!(phba->hba_flag & HBA_FCOE_MODE) ||
7002 	    !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
7003 	    !(phba->hba_flag & HBA_FIP_SUPPORT) ||
7004 	    (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
7005 	    (phba->pport->port_state == LPFC_FLOGI)) {
7006 		spin_unlock_irq(&phba->hbalock);
7007 		return;
7008 	}
7009 	spin_unlock_irq(&phba->hbalock);
7010 
7011 	if (lpfc_fcf_inuse(phba))
7012 		return;
7013 
7014 	lpfc_unregister_fcf_rescan(phba);
7015 }
7016 
7017 /**
7018  * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
7019  * @phba: Pointer to hba context object.
7020  * @buff: Buffer containing the FCF connection table as in the config
7021  *         region.
7022  * This function create driver data structure for the FCF connection
7023  * record table read from config region 23.
7024  */
7025 static void
7026 lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
7027 	uint8_t *buff)
7028 {
7029 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
7030 	struct lpfc_fcf_conn_hdr *conn_hdr;
7031 	struct lpfc_fcf_conn_rec *conn_rec;
7032 	uint32_t record_count;
7033 	int i;
7034 
7035 	/* Free the current connect table */
7036 	list_for_each_entry_safe(conn_entry, next_conn_entry,
7037 		&phba->fcf_conn_rec_list, list) {
7038 		list_del_init(&conn_entry->list);
7039 		kfree(conn_entry);
7040 	}
7041 
7042 	conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
7043 	record_count = conn_hdr->length * sizeof(uint32_t)/
7044 		sizeof(struct lpfc_fcf_conn_rec);
7045 
7046 	conn_rec = (struct lpfc_fcf_conn_rec *)
7047 		(buff + sizeof(struct lpfc_fcf_conn_hdr));
7048 
7049 	for (i = 0; i < record_count; i++) {
7050 		if (!(conn_rec[i].flags & FCFCNCT_VALID))
7051 			continue;
7052 		conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
7053 			GFP_KERNEL);
7054 		if (!conn_entry) {
7055 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7056 					"2566 Failed to allocate connection"
7057 					" table entry\n");
7058 			return;
7059 		}
7060 
7061 		memcpy(&conn_entry->conn_rec, &conn_rec[i],
7062 			sizeof(struct lpfc_fcf_conn_rec));
7063 		list_add_tail(&conn_entry->list,
7064 			&phba->fcf_conn_rec_list);
7065 	}
7066 
7067 	if (!list_empty(&phba->fcf_conn_rec_list)) {
7068 		i = 0;
7069 		list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
7070 				    list) {
7071 			conn_rec = &conn_entry->conn_rec;
7072 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7073 					"3345 FCF connection list rec[%02d]: "
7074 					"flags:x%04x, vtag:x%04x, "
7075 					"fabric_name:x%02x:%02x:%02x:%02x:"
7076 					"%02x:%02x:%02x:%02x, "
7077 					"switch_name:x%02x:%02x:%02x:%02x:"
7078 					"%02x:%02x:%02x:%02x\n", i++,
7079 					conn_rec->flags, conn_rec->vlan_tag,
7080 					conn_rec->fabric_name[0],
7081 					conn_rec->fabric_name[1],
7082 					conn_rec->fabric_name[2],
7083 					conn_rec->fabric_name[3],
7084 					conn_rec->fabric_name[4],
7085 					conn_rec->fabric_name[5],
7086 					conn_rec->fabric_name[6],
7087 					conn_rec->fabric_name[7],
7088 					conn_rec->switch_name[0],
7089 					conn_rec->switch_name[1],
7090 					conn_rec->switch_name[2],
7091 					conn_rec->switch_name[3],
7092 					conn_rec->switch_name[4],
7093 					conn_rec->switch_name[5],
7094 					conn_rec->switch_name[6],
7095 					conn_rec->switch_name[7]);
7096 		}
7097 	}
7098 }
7099 
7100 /**
7101  * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
7102  * @phba: Pointer to hba context object.
7103  * @buff: Buffer containing the FCoE parameter data structure.
7104  *
7105  *  This function update driver data structure with config
7106  *  parameters read from config region 23.
7107  */
7108 static void
7109 lpfc_read_fcoe_param(struct lpfc_hba *phba,
7110 			uint8_t *buff)
7111 {
7112 	struct lpfc_fip_param_hdr *fcoe_param_hdr;
7113 	struct lpfc_fcoe_params *fcoe_param;
7114 
7115 	fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
7116 		buff;
7117 	fcoe_param = (struct lpfc_fcoe_params *)
7118 		(buff + sizeof(struct lpfc_fip_param_hdr));
7119 
7120 	if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
7121 		(fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
7122 		return;
7123 
7124 	if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
7125 		phba->valid_vlan = 1;
7126 		phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
7127 			0xFFF;
7128 	}
7129 
7130 	phba->fc_map[0] = fcoe_param->fc_map[0];
7131 	phba->fc_map[1] = fcoe_param->fc_map[1];
7132 	phba->fc_map[2] = fcoe_param->fc_map[2];
7133 	return;
7134 }
7135 
7136 /**
7137  * lpfc_get_rec_conf23 - Get a record type in config region data.
7138  * @buff: Buffer containing config region 23 data.
7139  * @size: Size of the data buffer.
7140  * @rec_type: Record type to be searched.
7141  *
7142  * This function searches config region data to find the beginning
7143  * of the record specified by record_type. If record found, this
7144  * function return pointer to the record else return NULL.
7145  */
7146 static uint8_t *
7147 lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
7148 {
7149 	uint32_t offset = 0, rec_length;
7150 
7151 	if ((buff[0] == LPFC_REGION23_LAST_REC) ||
7152 		(size < sizeof(uint32_t)))
7153 		return NULL;
7154 
7155 	rec_length = buff[offset + 1];
7156 
7157 	/*
7158 	 * One TLV record has one word header and number of data words
7159 	 * specified in the rec_length field of the record header.
7160 	 */
7161 	while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
7162 		<= size) {
7163 		if (buff[offset] == rec_type)
7164 			return &buff[offset];
7165 
7166 		if (buff[offset] == LPFC_REGION23_LAST_REC)
7167 			return NULL;
7168 
7169 		offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
7170 		rec_length = buff[offset + 1];
7171 	}
7172 	return NULL;
7173 }
7174 
7175 /**
7176  * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
7177  * @phba: Pointer to lpfc_hba data structure.
7178  * @buff: Buffer containing config region 23 data.
7179  * @size: Size of the data buffer.
7180  *
7181  * This function parses the FCoE config parameters in config region 23 and
7182  * populate driver data structure with the parameters.
7183  */
7184 void
7185 lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
7186 		uint8_t *buff,
7187 		uint32_t size)
7188 {
7189 	uint32_t offset = 0;
7190 	uint8_t *rec_ptr;
7191 
7192 	/*
7193 	 * If data size is less than 2 words signature and version cannot be
7194 	 * verified.
7195 	 */
7196 	if (size < 2*sizeof(uint32_t))
7197 		return;
7198 
7199 	/* Check the region signature first */
7200 	if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
7201 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7202 			"2567 Config region 23 has bad signature\n");
7203 		return;
7204 	}
7205 
7206 	offset += 4;
7207 
7208 	/* Check the data structure version */
7209 	if (buff[offset] != LPFC_REGION23_VERSION) {
7210 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7211 				"2568 Config region 23 has bad version\n");
7212 		return;
7213 	}
7214 	offset += 4;
7215 
7216 	/* Read FCoE param record */
7217 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
7218 			size - offset, FCOE_PARAM_TYPE);
7219 	if (rec_ptr)
7220 		lpfc_read_fcoe_param(phba, rec_ptr);
7221 
7222 	/* Read FCF connection table */
7223 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
7224 		size - offset, FCOE_CONN_TBL_TYPE);
7225 	if (rec_ptr)
7226 		lpfc_read_fcf_conn_tbl(phba, rec_ptr);
7227 
7228 }
7229 
7230 /*
7231  * lpfc_error_lost_link - IO failure from link event or FW reset check.
7232  *
7233  * @vport: Pointer to lpfc_vport data structure.
7234  * @ulp_status: IO completion status.
7235  * @ulp_word4: Reason code for the ulp_status.
7236  *
7237  * This function evaluates the ulp_status and ulp_word4 values
7238  * for specific error values that indicate an internal link fault
7239  * or fw reset event for the completing IO.  Callers require this
7240  * common data to decide next steps on the IO.
7241  *
7242  * Return:
7243  * false - No link or reset error occurred.
7244  * true - A link or reset error occurred.
7245  */
7246 bool
7247 lpfc_error_lost_link(struct lpfc_vport *vport, u32 ulp_status, u32 ulp_word4)
7248 {
7249 	/* Mask off the extra port data to get just the reason code. */
7250 	u32 rsn_code = IOERR_PARAM_MASK & ulp_word4;
7251 
7252 	if (ulp_status == IOSTAT_LOCAL_REJECT &&
7253 	    (rsn_code == IOERR_SLI_ABORTED ||
7254 	     rsn_code == IOERR_LINK_DOWN ||
7255 	     rsn_code == IOERR_SLI_DOWN)) {
7256 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI | LOG_ELS,
7257 				 "0408 Report link error true: <x%x:x%x>\n",
7258 				 ulp_status, ulp_word4);
7259 		return true;
7260 	}
7261 
7262 	return false;
7263 }
7264