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