xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_hbadisc.c (revision ffcdf473)
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.
4337 		 */
4338 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
4339 			spin_lock_irq(&ndlp->lock);
4340 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
4341 			spin_unlock_irq(&ndlp->lock);
4342 			lpfc_nlp_not_used(ndlp);
4343 		}
4344 
4345 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4346 			/*
4347 			 * RegLogin failed, use loop map to make discovery
4348 			 * list
4349 			 */
4350 			lpfc_disc_list_loopmap(vport);
4351 
4352 			/* Start discovery */
4353 			lpfc_disc_start(vport);
4354 			return;
4355 		}
4356 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4357 		return;
4358 	}
4359 
4360 	if (phba->sli_rev < LPFC_SLI_REV4)
4361 		ndlp->nlp_rpi = mb->un.varWords[0];
4362 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4363 	ndlp->nlp_type |= NLP_FABRIC;
4364 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4365 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
4366 			 "0003 rpi:%x DID:%x flg:%x %d x%px\n",
4367 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4368 			 kref_read(&ndlp->kref),
4369 			 ndlp);
4370 
4371 	if (vport->port_state < LPFC_VPORT_READY) {
4372 		/* Link up discovery requires Fabric registration. */
4373 		lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
4374 		lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
4375 		lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
4376 		lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
4377 
4378 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4379 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
4380 			lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
4381 
4382 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4383 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
4384 			lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
4385 				    FC_TYPE_NVME);
4386 
4387 		/* Issue SCR just before NameServer GID_FT Query */
4388 		lpfc_issue_els_scr(vport, 0);
4389 
4390 		/* Link was bounced or a Fabric LOGO occurred.  Start EDC
4391 		 * with initial FW values provided the congestion mode is
4392 		 * not off.  Note that signals may or may not be supported
4393 		 * by the adapter but FPIN is provided by default for 1
4394 		 * or both missing signals support.
4395 		 */
4396 		if (phba->cmf_active_mode != LPFC_CFG_OFF) {
4397 			phba->cgn_reg_fpin = phba->cgn_init_reg_fpin;
4398 			phba->cgn_reg_signal = phba->cgn_init_reg_signal;
4399 			rc = lpfc_issue_els_edc(vport, 0);
4400 			lpfc_printf_log(phba, KERN_INFO,
4401 					LOG_INIT | LOG_ELS | LOG_DISCOVERY,
4402 					"4220 Issue EDC status x%x Data x%x\n",
4403 					rc, phba->cgn_init_reg_signal);
4404 		} else if (phba->lmt & LMT_64Gb) {
4405 			/* may send link fault capability descriptor */
4406 			lpfc_issue_els_edc(vport, 0);
4407 		} else {
4408 			lpfc_issue_els_rdf(vport, 0);
4409 		}
4410 	}
4411 
4412 	vport->fc_ns_retry = 0;
4413 	if (lpfc_issue_gidft(vport) == 0)
4414 		goto out;
4415 
4416 	/*
4417 	 * At this point in time we may need to wait for multiple
4418 	 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
4419 	 *
4420 	 * decrement the node reference count held for this
4421 	 * callback function.
4422 	 */
4423 	lpfc_nlp_put(ndlp);
4424 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4425 	return;
4426 }
4427 
4428 /*
4429  * This routine handles processing a Fabric Controller REG_LOGIN mailbox
4430  * command upon completion. It is setup in the LPFC_MBOXQ
4431  * as the completion routine when the command is handed off to the SLI layer.
4432  */
4433 void
4434 lpfc_mbx_cmpl_fc_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4435 {
4436 	struct lpfc_vport *vport = pmb->vport;
4437 	MAILBOX_t *mb = &pmb->u.mb;
4438 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4439 
4440 	pmb->ctx_ndlp = NULL;
4441 	if (mb->mbxStatus) {
4442 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4443 				 "0933 %s: Register FC login error: 0x%x\n",
4444 				 __func__, mb->mbxStatus);
4445 		goto out;
4446 	}
4447 
4448 	lpfc_check_nlp_post_devloss(vport, ndlp);
4449 
4450 	if (phba->sli_rev < LPFC_SLI_REV4)
4451 		ndlp->nlp_rpi = mb->un.varWords[0];
4452 
4453 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4454 			 "0934 %s: Complete FC x%x RegLogin rpi x%x ste x%x\n",
4455 			 __func__, ndlp->nlp_DID, ndlp->nlp_rpi,
4456 			 ndlp->nlp_state);
4457 
4458 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4459 	ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
4460 	ndlp->nlp_type |= NLP_FABRIC;
4461 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4462 
4463  out:
4464 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4465 
4466 	/* Drop the reference count from the mbox at the end after
4467 	 * all the current reference to the ndlp have been done.
4468 	 */
4469 	lpfc_nlp_put(ndlp);
4470 }
4471 
4472 static void
4473 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4474 {
4475 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4476 	struct fc_rport  *rport;
4477 	struct lpfc_rport_data *rdata;
4478 	struct fc_rport_identifiers rport_ids;
4479 	struct lpfc_hba  *phba = vport->phba;
4480 	unsigned long flags;
4481 
4482 	if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4483 		return;
4484 
4485 	/* Remote port has reappeared. Re-register w/ FC transport */
4486 	rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4487 	rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
4488 	rport_ids.port_id = ndlp->nlp_DID;
4489 	rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
4490 
4491 
4492 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4493 			      "rport add:       did:x%x flg:x%x type x%x",
4494 			      ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4495 
4496 	/* Don't add the remote port if unloading. */
4497 	if (vport->load_flag & FC_UNLOADING)
4498 		return;
4499 
4500 	/*
4501 	 * Disassociate any older association between this ndlp and rport
4502 	 */
4503 	if (ndlp->rport) {
4504 		rdata = ndlp->rport->dd_data;
4505 		rdata->pnode = NULL;
4506 	}
4507 
4508 	ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
4509 	if (!rport) {
4510 		dev_printk(KERN_WARNING, &phba->pcidev->dev,
4511 			   "Warning: fc_remote_port_add failed\n");
4512 		return;
4513 	}
4514 
4515 	/* Successful port add.  Complete initializing node data */
4516 	rport->maxframe_size = ndlp->nlp_maxframe;
4517 	rport->supported_classes = ndlp->nlp_class_sup;
4518 	rdata = rport->dd_data;
4519 	rdata->pnode = lpfc_nlp_get(ndlp);
4520 	if (!rdata->pnode) {
4521 		dev_warn(&phba->pcidev->dev,
4522 			 "Warning - node ref failed. Unreg rport\n");
4523 		fc_remote_port_delete(rport);
4524 		ndlp->rport = NULL;
4525 		return;
4526 	}
4527 
4528 	spin_lock_irqsave(&ndlp->lock, flags);
4529 	ndlp->fc4_xpt_flags |= SCSI_XPT_REGD;
4530 	spin_unlock_irqrestore(&ndlp->lock, flags);
4531 
4532 	if (ndlp->nlp_type & NLP_FCP_TARGET)
4533 		rport_ids.roles |= FC_PORT_ROLE_FCP_TARGET;
4534 	if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4535 		rport_ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
4536 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
4537 		rport_ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
4538 	if (ndlp->nlp_type & NLP_NVME_TARGET)
4539 		rport_ids.roles |= FC_PORT_ROLE_NVME_TARGET;
4540 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
4541 		rport_ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
4542 
4543 	if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
4544 		fc_remote_port_rolechg(rport, rport_ids.roles);
4545 
4546 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4547 			 "3183 %s rport x%px DID x%x, role x%x refcnt %d\n",
4548 			 __func__, rport, rport->port_id, rport->roles,
4549 			 kref_read(&ndlp->kref));
4550 
4551 	if ((rport->scsi_target_id != -1) &&
4552 	    (rport->scsi_target_id < LPFC_MAX_TARGET)) {
4553 		ndlp->nlp_sid = rport->scsi_target_id;
4554 	}
4555 
4556 	return;
4557 }
4558 
4559 static void
4560 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
4561 {
4562 	struct fc_rport *rport = ndlp->rport;
4563 	struct lpfc_vport *vport = ndlp->vport;
4564 
4565 	if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4566 		return;
4567 
4568 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4569 		"rport delete:    did:x%x flg:x%x type x%x",
4570 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4571 
4572 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4573 			 "3184 rport unregister x%06x, rport x%px "
4574 			 "xptflg x%x refcnt %d\n",
4575 			 ndlp->nlp_DID, rport, ndlp->fc4_xpt_flags,
4576 			 kref_read(&ndlp->kref));
4577 
4578 	fc_remote_port_delete(rport);
4579 	lpfc_nlp_put(ndlp);
4580 }
4581 
4582 static void
4583 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
4584 {
4585 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4586 	unsigned long iflags;
4587 
4588 	spin_lock_irqsave(shost->host_lock, iflags);
4589 	switch (state) {
4590 	case NLP_STE_UNUSED_NODE:
4591 		vport->fc_unused_cnt += count;
4592 		break;
4593 	case NLP_STE_PLOGI_ISSUE:
4594 		vport->fc_plogi_cnt += count;
4595 		break;
4596 	case NLP_STE_ADISC_ISSUE:
4597 		vport->fc_adisc_cnt += count;
4598 		break;
4599 	case NLP_STE_REG_LOGIN_ISSUE:
4600 		vport->fc_reglogin_cnt += count;
4601 		break;
4602 	case NLP_STE_PRLI_ISSUE:
4603 		vport->fc_prli_cnt += count;
4604 		break;
4605 	case NLP_STE_UNMAPPED_NODE:
4606 		vport->fc_unmap_cnt += count;
4607 		break;
4608 	case NLP_STE_MAPPED_NODE:
4609 		vport->fc_map_cnt += count;
4610 		break;
4611 	case NLP_STE_NPR_NODE:
4612 		if (vport->fc_npr_cnt == 0 && count == -1)
4613 			vport->fc_npr_cnt = 0;
4614 		else
4615 			vport->fc_npr_cnt += count;
4616 		break;
4617 	}
4618 	spin_unlock_irqrestore(shost->host_lock, iflags);
4619 }
4620 
4621 /* Register a node with backend if not already done */
4622 void
4623 lpfc_nlp_reg_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4624 {
4625 	unsigned long iflags;
4626 
4627 	lpfc_check_nlp_post_devloss(vport, ndlp);
4628 
4629 	spin_lock_irqsave(&ndlp->lock, iflags);
4630 	if (ndlp->fc4_xpt_flags & NLP_XPT_REGD) {
4631 		/* Already registered with backend, trigger rescan */
4632 		spin_unlock_irqrestore(&ndlp->lock, iflags);
4633 
4634 		if (ndlp->fc4_xpt_flags & NVME_XPT_REGD &&
4635 		    ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY)) {
4636 			lpfc_nvme_rescan_port(vport, ndlp);
4637 		}
4638 		return;
4639 	}
4640 
4641 	ndlp->fc4_xpt_flags |= NLP_XPT_REGD;
4642 	spin_unlock_irqrestore(&ndlp->lock, iflags);
4643 
4644 	if (lpfc_valid_xpt_node(ndlp)) {
4645 		vport->phba->nport_event_cnt++;
4646 		/*
4647 		 * Tell the fc transport about the port, if we haven't
4648 		 * already. If we have, and it's a scsi entity, be
4649 		 */
4650 		lpfc_register_remote_port(vport, ndlp);
4651 	}
4652 
4653 	/* We are done if we do not have any NVME remote node */
4654 	if (!(ndlp->nlp_fc4_type & NLP_FC4_NVME))
4655 		return;
4656 
4657 	/* Notify the NVME transport of this new rport. */
4658 	if (vport->phba->sli_rev >= LPFC_SLI_REV4 &&
4659 			ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4660 		if (vport->phba->nvmet_support == 0) {
4661 			/* Register this rport with the transport.
4662 			 * Only NVME Target Rports are registered with
4663 			 * the transport.
4664 			 */
4665 			if (ndlp->nlp_type & NLP_NVME_TARGET) {
4666 				vport->phba->nport_event_cnt++;
4667 				lpfc_nvme_register_port(vport, ndlp);
4668 			}
4669 		} else {
4670 			/* Just take an NDLP ref count since the
4671 			 * target does not register rports.
4672 			 */
4673 			lpfc_nlp_get(ndlp);
4674 		}
4675 	}
4676 }
4677 
4678 /* Unregister a node with backend if not already done */
4679 void
4680 lpfc_nlp_unreg_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4681 {
4682 	unsigned long iflags;
4683 
4684 	spin_lock_irqsave(&ndlp->lock, iflags);
4685 	if (!(ndlp->fc4_xpt_flags & NLP_XPT_REGD)) {
4686 		spin_unlock_irqrestore(&ndlp->lock, iflags);
4687 		lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4688 				 "0999 %s Not regd: ndlp x%px rport x%px DID "
4689 				 "x%x FLG x%x XPT x%x\n",
4690 				  __func__, ndlp, ndlp->rport, ndlp->nlp_DID,
4691 				  ndlp->nlp_flag, ndlp->fc4_xpt_flags);
4692 		return;
4693 	}
4694 
4695 	ndlp->fc4_xpt_flags &= ~NLP_XPT_REGD;
4696 	spin_unlock_irqrestore(&ndlp->lock, iflags);
4697 
4698 	if (ndlp->rport &&
4699 	    ndlp->fc4_xpt_flags & SCSI_XPT_REGD) {
4700 		vport->phba->nport_event_cnt++;
4701 		lpfc_unregister_remote_port(ndlp);
4702 	} else if (!ndlp->rport) {
4703 		lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4704 				 "1999 %s NDLP in devloss x%px DID x%x FLG x%x"
4705 				 " XPT x%x refcnt %d\n",
4706 				 __func__, ndlp, ndlp->nlp_DID, ndlp->nlp_flag,
4707 				 ndlp->fc4_xpt_flags,
4708 				 kref_read(&ndlp->kref));
4709 	}
4710 
4711 	if (ndlp->fc4_xpt_flags & NVME_XPT_REGD) {
4712 		vport->phba->nport_event_cnt++;
4713 		if (vport->phba->nvmet_support == 0) {
4714 			/* Start devloss if target. */
4715 			if (ndlp->nlp_type & NLP_NVME_TARGET)
4716 				lpfc_nvme_unregister_port(vport, ndlp);
4717 		} else {
4718 			/* NVMET has no upcall. */
4719 			lpfc_nlp_put(ndlp);
4720 		}
4721 	}
4722 
4723 }
4724 
4725 /*
4726  * Adisc state change handling
4727  */
4728 static void
4729 lpfc_handle_adisc_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4730 		int new_state)
4731 {
4732 	switch (new_state) {
4733 	/*
4734 	 * Any state to ADISC_ISSUE
4735 	 * Do nothing, adisc cmpl handling will trigger state changes
4736 	 */
4737 	case NLP_STE_ADISC_ISSUE:
4738 		break;
4739 
4740 	/*
4741 	 * ADISC_ISSUE to mapped states
4742 	 * Trigger a registration with backend, it will be nop if
4743 	 * already registered
4744 	 */
4745 	case NLP_STE_UNMAPPED_NODE:
4746 		ndlp->nlp_type |= NLP_FC_NODE;
4747 		fallthrough;
4748 	case NLP_STE_MAPPED_NODE:
4749 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4750 		lpfc_nlp_reg_node(vport, ndlp);
4751 		break;
4752 
4753 	/*
4754 	 * ADISC_ISSUE to non-mapped states
4755 	 * We are moving from ADISC_ISSUE to a non-mapped state because
4756 	 * ADISC failed, we would have skipped unregistering with
4757 	 * backend, attempt it now
4758 	 */
4759 	case NLP_STE_NPR_NODE:
4760 		ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
4761 		fallthrough;
4762 	default:
4763 		lpfc_nlp_unreg_node(vport, ndlp);
4764 		break;
4765 	}
4766 
4767 }
4768 
4769 static void
4770 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4771 		       int old_state, int new_state)
4772 {
4773 	/* Trap ADISC changes here */
4774 	if (new_state == NLP_STE_ADISC_ISSUE ||
4775 	    old_state == NLP_STE_ADISC_ISSUE) {
4776 		lpfc_handle_adisc_state(vport, ndlp, new_state);
4777 		return;
4778 	}
4779 
4780 	if (new_state == NLP_STE_UNMAPPED_NODE) {
4781 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4782 		ndlp->nlp_type |= NLP_FC_NODE;
4783 	}
4784 	if (new_state == NLP_STE_MAPPED_NODE)
4785 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4786 	if (new_state == NLP_STE_NPR_NODE)
4787 		ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
4788 
4789 	/* Reg/Unreg for FCP and NVME Transport interface */
4790 	if ((old_state == NLP_STE_MAPPED_NODE ||
4791 	     old_state == NLP_STE_UNMAPPED_NODE)) {
4792 		/* For nodes marked for ADISC, Handle unreg in ADISC cmpl
4793 		 * if linkup. In linkdown do unreg_node
4794 		 */
4795 		if (!(ndlp->nlp_flag & NLP_NPR_ADISC) ||
4796 		    !lpfc_is_link_up(vport->phba))
4797 			lpfc_nlp_unreg_node(vport, ndlp);
4798 	}
4799 
4800 	if (new_state ==  NLP_STE_MAPPED_NODE ||
4801 	    new_state == NLP_STE_UNMAPPED_NODE)
4802 		lpfc_nlp_reg_node(vport, ndlp);
4803 
4804 	/*
4805 	 * If the node just added to Mapped list was an FCP target,
4806 	 * but the remote port registration failed or assigned a target
4807 	 * id outside the presentable range - move the node to the
4808 	 * Unmapped List.
4809 	 */
4810 	if ((new_state == NLP_STE_MAPPED_NODE) &&
4811 	    (ndlp->nlp_type & NLP_FCP_TARGET) &&
4812 	    (!ndlp->rport ||
4813 	     ndlp->rport->scsi_target_id == -1 ||
4814 	     ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
4815 		spin_lock_irq(&ndlp->lock);
4816 		ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
4817 		spin_unlock_irq(&ndlp->lock);
4818 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4819 	}
4820 }
4821 
4822 static char *
4823 lpfc_nlp_state_name(char *buffer, size_t size, int state)
4824 {
4825 	static char *states[] = {
4826 		[NLP_STE_UNUSED_NODE] = "UNUSED",
4827 		[NLP_STE_PLOGI_ISSUE] = "PLOGI",
4828 		[NLP_STE_ADISC_ISSUE] = "ADISC",
4829 		[NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4830 		[NLP_STE_PRLI_ISSUE] = "PRLI",
4831 		[NLP_STE_LOGO_ISSUE] = "LOGO",
4832 		[NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4833 		[NLP_STE_MAPPED_NODE] = "MAPPED",
4834 		[NLP_STE_NPR_NODE] = "NPR",
4835 	};
4836 
4837 	if (state < NLP_STE_MAX_STATE && states[state])
4838 		strlcpy(buffer, states[state], size);
4839 	else
4840 		snprintf(buffer, size, "unknown (%d)", state);
4841 	return buffer;
4842 }
4843 
4844 void
4845 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4846 		   int state)
4847 {
4848 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4849 	int  old_state = ndlp->nlp_state;
4850 	int node_dropped = ndlp->nlp_flag & NLP_DROPPED;
4851 	char name1[16], name2[16];
4852 
4853 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4854 			 "0904 NPort state transition x%06x, %s -> %s\n",
4855 			 ndlp->nlp_DID,
4856 			 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4857 			 lpfc_nlp_state_name(name2, sizeof(name2), state));
4858 
4859 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4860 		"node statechg    did:x%x old:%d ste:%d",
4861 		ndlp->nlp_DID, old_state, state);
4862 
4863 	if (node_dropped && old_state == NLP_STE_UNUSED_NODE &&
4864 	    state != NLP_STE_UNUSED_NODE) {
4865 		ndlp->nlp_flag &= ~NLP_DROPPED;
4866 		lpfc_nlp_get(ndlp);
4867 	}
4868 
4869 	if (old_state == NLP_STE_NPR_NODE &&
4870 	    state != NLP_STE_NPR_NODE)
4871 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
4872 	if (old_state == NLP_STE_UNMAPPED_NODE) {
4873 		ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4874 		ndlp->nlp_type &= ~NLP_FC_NODE;
4875 	}
4876 
4877 	if (list_empty(&ndlp->nlp_listp)) {
4878 		spin_lock_irq(shost->host_lock);
4879 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4880 		spin_unlock_irq(shost->host_lock);
4881 	} else if (old_state)
4882 		lpfc_nlp_counters(vport, old_state, -1);
4883 
4884 	ndlp->nlp_state = state;
4885 	lpfc_nlp_counters(vport, state, 1);
4886 	lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
4887 }
4888 
4889 void
4890 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4891 {
4892 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4893 
4894 	if (list_empty(&ndlp->nlp_listp)) {
4895 		spin_lock_irq(shost->host_lock);
4896 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4897 		spin_unlock_irq(shost->host_lock);
4898 	}
4899 }
4900 
4901 void
4902 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4903 {
4904 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4905 
4906 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
4907 	if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4908 		lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4909 	spin_lock_irq(shost->host_lock);
4910 	list_del_init(&ndlp->nlp_listp);
4911 	spin_unlock_irq(shost->host_lock);
4912 	lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4913 				NLP_STE_UNUSED_NODE);
4914 }
4915 
4916 /**
4917  * lpfc_initialize_node - Initialize all fields of node object
4918  * @vport: Pointer to Virtual Port object.
4919  * @ndlp: Pointer to FC node object.
4920  * @did: FC_ID of the node.
4921  *
4922  * This function is always called when node object need to be initialized.
4923  * It initializes all the fields of the node object. Although the reference
4924  * to phba from @ndlp can be obtained indirectly through it's reference to
4925  * @vport, a direct reference to phba is taken here by @ndlp. This is due
4926  * to the life-span of the @ndlp might go beyond the existence of @vport as
4927  * the final release of ndlp is determined by its reference count. And, the
4928  * operation on @ndlp needs the reference to phba.
4929  **/
4930 static inline void
4931 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4932 	uint32_t did)
4933 {
4934 	INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4935 	INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4936 	timer_setup(&ndlp->nlp_delayfunc, lpfc_els_retry_delay, 0);
4937 	INIT_LIST_HEAD(&ndlp->recovery_evt.evt_listp);
4938 
4939 	ndlp->nlp_DID = did;
4940 	ndlp->vport = vport;
4941 	ndlp->phba = vport->phba;
4942 	ndlp->nlp_sid = NLP_NO_SID;
4943 	ndlp->nlp_fc4_type = NLP_FC4_NONE;
4944 	kref_init(&ndlp->kref);
4945 	atomic_set(&ndlp->cmd_pending, 0);
4946 	ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
4947 	ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4948 }
4949 
4950 void
4951 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4952 {
4953 	/*
4954 	 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
4955 	 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
4956 	 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4957 	 * until ALL other outstanding threads have completed. We check
4958 	 * that the ndlp not already in the UNUSED state before we proceed.
4959 	 */
4960 	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4961 		return;
4962 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
4963 	ndlp->nlp_flag |= NLP_DROPPED;
4964 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4965 		lpfc_cleanup_vports_rrqs(vport, ndlp);
4966 		lpfc_unreg_rpi(vport, ndlp);
4967 	}
4968 
4969 	lpfc_nlp_put(ndlp);
4970 	return;
4971 }
4972 
4973 /*
4974  * Start / ReStart rescue timer for Discovery / RSCN handling
4975  */
4976 void
4977 lpfc_set_disctmo(struct lpfc_vport *vport)
4978 {
4979 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4980 	struct lpfc_hba  *phba = vport->phba;
4981 	uint32_t tmo;
4982 
4983 	if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
4984 		/* For FAN, timeout should be greater than edtov */
4985 		tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4986 	} else {
4987 		/* Normal discovery timeout should be > than ELS/CT timeout
4988 		 * FC spec states we need 3 * ratov for CT requests
4989 		 */
4990 		tmo = ((phba->fc_ratov * 3) + 3);
4991 	}
4992 
4993 
4994 	if (!timer_pending(&vport->fc_disctmo)) {
4995 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4996 			"set disc timer:  tmo:x%x state:x%x flg:x%x",
4997 			tmo, vport->port_state, vport->fc_flag);
4998 	}
4999 
5000 	mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
5001 	spin_lock_irq(shost->host_lock);
5002 	vport->fc_flag |= FC_DISC_TMO;
5003 	spin_unlock_irq(shost->host_lock);
5004 
5005 	/* Start Discovery Timer state <hba_state> */
5006 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5007 			 "0247 Start Discovery Timer state x%x "
5008 			 "Data: x%x x%lx x%x x%x\n",
5009 			 vport->port_state, tmo,
5010 			 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
5011 			 vport->fc_adisc_cnt);
5012 
5013 	return;
5014 }
5015 
5016 /*
5017  * Cancel rescue timer for Discovery / RSCN handling
5018  */
5019 int
5020 lpfc_can_disctmo(struct lpfc_vport *vport)
5021 {
5022 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5023 	unsigned long iflags;
5024 
5025 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5026 		"can disc timer:  state:x%x rtry:x%x flg:x%x",
5027 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5028 
5029 	/* Turn off discovery timer if its running */
5030 	if (vport->fc_flag & FC_DISC_TMO ||
5031 	    timer_pending(&vport->fc_disctmo)) {
5032 		spin_lock_irqsave(shost->host_lock, iflags);
5033 		vport->fc_flag &= ~FC_DISC_TMO;
5034 		spin_unlock_irqrestore(shost->host_lock, iflags);
5035 		del_timer_sync(&vport->fc_disctmo);
5036 		spin_lock_irqsave(&vport->work_port_lock, iflags);
5037 		vport->work_port_events &= ~WORKER_DISC_TMO;
5038 		spin_unlock_irqrestore(&vport->work_port_lock, iflags);
5039 	}
5040 
5041 	/* Cancel Discovery Timer state <hba_state> */
5042 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5043 			 "0248 Cancel Discovery Timer state x%x "
5044 			 "Data: x%x x%x x%x\n",
5045 			 vport->port_state, vport->fc_flag,
5046 			 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
5047 	return 0;
5048 }
5049 
5050 /*
5051  * Check specified ring for outstanding IOCB on the SLI queue
5052  * Return true if iocb matches the specified nport
5053  */
5054 int
5055 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
5056 		    struct lpfc_sli_ring *pring,
5057 		    struct lpfc_iocbq *iocb,
5058 		    struct lpfc_nodelist *ndlp)
5059 {
5060 	struct lpfc_vport *vport = ndlp->vport;
5061 	u8 ulp_command;
5062 	u16 ulp_context;
5063 	u32 remote_id;
5064 
5065 	if (iocb->vport != vport)
5066 		return 0;
5067 
5068 	ulp_command = get_job_cmnd(phba, iocb);
5069 	ulp_context = get_job_ulpcontext(phba, iocb);
5070 	remote_id = get_job_els_rsp64_did(phba, iocb);
5071 
5072 	if (pring->ringno == LPFC_ELS_RING) {
5073 		switch (ulp_command) {
5074 		case CMD_GEN_REQUEST64_CR:
5075 			if (iocb->ndlp == ndlp)
5076 				return 1;
5077 			fallthrough;
5078 		case CMD_ELS_REQUEST64_CR:
5079 			if (remote_id == ndlp->nlp_DID)
5080 				return 1;
5081 			fallthrough;
5082 		case CMD_XMIT_ELS_RSP64_CX:
5083 			if (iocb->ndlp == ndlp)
5084 				return 1;
5085 		}
5086 	} else if (pring->ringno == LPFC_FCP_RING) {
5087 		/* Skip match check if waiting to relogin to FCP target */
5088 		if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
5089 		    (ndlp->nlp_flag & NLP_DELAY_TMO)) {
5090 			return 0;
5091 		}
5092 		if (ulp_context == ndlp->nlp_rpi)
5093 			return 1;
5094 	}
5095 	return 0;
5096 }
5097 
5098 static void
5099 __lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
5100 		struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
5101 		struct list_head *dequeue_list)
5102 {
5103 	struct lpfc_iocbq *iocb, *next_iocb;
5104 
5105 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5106 		/* Check to see if iocb matches the nport */
5107 		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
5108 			/* match, dequeue */
5109 			list_move_tail(&iocb->list, dequeue_list);
5110 	}
5111 }
5112 
5113 static void
5114 lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
5115 		struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
5116 {
5117 	struct lpfc_sli *psli = &phba->sli;
5118 	uint32_t i;
5119 
5120 	spin_lock_irq(&phba->hbalock);
5121 	for (i = 0; i < psli->num_rings; i++)
5122 		__lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
5123 						dequeue_list);
5124 	spin_unlock_irq(&phba->hbalock);
5125 }
5126 
5127 static void
5128 lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
5129 		struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
5130 {
5131 	struct lpfc_sli_ring *pring;
5132 	struct lpfc_queue *qp = NULL;
5133 
5134 	spin_lock_irq(&phba->hbalock);
5135 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
5136 		pring = qp->pring;
5137 		if (!pring)
5138 			continue;
5139 		spin_lock(&pring->ring_lock);
5140 		__lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
5141 		spin_unlock(&pring->ring_lock);
5142 	}
5143 	spin_unlock_irq(&phba->hbalock);
5144 }
5145 
5146 /*
5147  * Free resources / clean up outstanding I/Os
5148  * associated with nlp_rpi in the LPFC_NODELIST entry.
5149  */
5150 static int
5151 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
5152 {
5153 	LIST_HEAD(completions);
5154 
5155 	lpfc_fabric_abort_nport(ndlp);
5156 
5157 	/*
5158 	 * Everything that matches on txcmplq will be returned
5159 	 * by firmware with a no rpi error.
5160 	 */
5161 	if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5162 		if (phba->sli_rev != LPFC_SLI_REV4)
5163 			lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
5164 		else
5165 			lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
5166 	}
5167 
5168 	/* Cancel all the IOCBs from the completions list */
5169 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5170 			      IOERR_SLI_ABORTED);
5171 
5172 	return 0;
5173 }
5174 
5175 /**
5176  * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
5177  * @phba: Pointer to HBA context object.
5178  * @pmb: Pointer to mailbox object.
5179  *
5180  * This function will issue an ELS LOGO command after completing
5181  * the UNREG_RPI.
5182  **/
5183 static void
5184 lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5185 {
5186 	struct lpfc_vport  *vport = pmb->vport;
5187 	struct lpfc_nodelist *ndlp;
5188 
5189 	ndlp = (struct lpfc_nodelist *)(pmb->ctx_ndlp);
5190 	if (!ndlp)
5191 		return;
5192 	lpfc_issue_els_logo(vport, ndlp, 0);
5193 
5194 	/* Check to see if there are any deferred events to process */
5195 	if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
5196 	    (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
5197 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5198 				 "1434 UNREG cmpl deferred logo x%x "
5199 				 "on NPort x%x Data: x%x x%px\n",
5200 				 ndlp->nlp_rpi, ndlp->nlp_DID,
5201 				 ndlp->nlp_defer_did, ndlp);
5202 
5203 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
5204 		ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
5205 		lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5206 	} else {
5207 		/* NLP_RELEASE_RPI is only set for SLI4 ports. */
5208 		if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
5209 			lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
5210 			spin_lock_irq(&ndlp->lock);
5211 			ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
5212 			ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
5213 			spin_unlock_irq(&ndlp->lock);
5214 		}
5215 		spin_lock_irq(&ndlp->lock);
5216 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
5217 		spin_unlock_irq(&ndlp->lock);
5218 	}
5219 
5220 	/* The node has an outstanding reference for the unreg. Now
5221 	 * that the LOGO action and cleanup are finished, release
5222 	 * resources.
5223 	 */
5224 	lpfc_nlp_put(ndlp);
5225 	mempool_free(pmb, phba->mbox_mem_pool);
5226 }
5227 
5228 /*
5229  * Sets the mailbox completion handler to be used for the
5230  * unreg_rpi command. The handler varies based on the state of
5231  * the port and what will be happening to the rpi next.
5232  */
5233 static void
5234 lpfc_set_unreg_login_mbx_cmpl(struct lpfc_hba *phba, struct lpfc_vport *vport,
5235 	struct lpfc_nodelist *ndlp, LPFC_MBOXQ_t *mbox)
5236 {
5237 	unsigned long iflags;
5238 
5239 	/* Driver always gets a reference on the mailbox job
5240 	 * in support of async jobs.
5241 	 */
5242 	mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
5243 	if (!mbox->ctx_ndlp)
5244 		return;
5245 
5246 	if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
5247 		mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
5248 
5249 	} else if (phba->sli_rev == LPFC_SLI_REV4 &&
5250 		   (!(vport->load_flag & FC_UNLOADING)) &&
5251 		    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
5252 				      LPFC_SLI_INTF_IF_TYPE_2) &&
5253 		    (kref_read(&ndlp->kref) > 0)) {
5254 		mbox->mbox_cmpl = lpfc_sli4_unreg_rpi_cmpl_clr;
5255 	} else {
5256 		if (vport->load_flag & FC_UNLOADING) {
5257 			if (phba->sli_rev == LPFC_SLI_REV4) {
5258 				spin_lock_irqsave(&ndlp->lock, iflags);
5259 				ndlp->nlp_flag |= NLP_RELEASE_RPI;
5260 				spin_unlock_irqrestore(&ndlp->lock, iflags);
5261 			}
5262 		}
5263 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5264 	}
5265 }
5266 
5267 /*
5268  * Free rpi associated with LPFC_NODELIST entry.
5269  * This routine is called from lpfc_freenode(), when we are removing
5270  * a LPFC_NODELIST entry. It is also called if the driver initiates a
5271  * LOGO that completes successfully, and we are waiting to PLOGI back
5272  * to the remote NPort. In addition, it is called after we receive
5273  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
5274  * we are waiting to PLOGI back to the remote NPort.
5275  */
5276 int
5277 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5278 {
5279 	struct lpfc_hba *phba = vport->phba;
5280 	LPFC_MBOXQ_t    *mbox;
5281 	int rc, acc_plogi = 1;
5282 	uint16_t rpi;
5283 
5284 	if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
5285 	    ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
5286 		if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
5287 			lpfc_printf_vlog(vport, KERN_INFO,
5288 					 LOG_NODE | LOG_DISCOVERY,
5289 					 "3366 RPI x%x needs to be "
5290 					 "unregistered nlp_flag x%x "
5291 					 "did x%x\n",
5292 					 ndlp->nlp_rpi, ndlp->nlp_flag,
5293 					 ndlp->nlp_DID);
5294 
5295 		/* If there is already an UNREG in progress for this ndlp,
5296 		 * no need to queue up another one.
5297 		 */
5298 		if (ndlp->nlp_flag & NLP_UNREG_INP) {
5299 			lpfc_printf_vlog(vport, KERN_INFO,
5300 					 LOG_NODE | LOG_DISCOVERY,
5301 					 "1436 unreg_rpi SKIP UNREG x%x on "
5302 					 "NPort x%x deferred x%x  flg x%x "
5303 					 "Data: x%px\n",
5304 					 ndlp->nlp_rpi, ndlp->nlp_DID,
5305 					 ndlp->nlp_defer_did,
5306 					 ndlp->nlp_flag, ndlp);
5307 			goto out;
5308 		}
5309 
5310 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5311 		if (mbox) {
5312 			/* SLI4 ports require the physical rpi value. */
5313 			rpi = ndlp->nlp_rpi;
5314 			if (phba->sli_rev == LPFC_SLI_REV4)
5315 				rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
5316 
5317 			lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
5318 			mbox->vport = vport;
5319 			lpfc_set_unreg_login_mbx_cmpl(phba, vport, ndlp, mbox);
5320 			if (!mbox->ctx_ndlp) {
5321 				mempool_free(mbox, phba->mbox_mem_pool);
5322 				return 1;
5323 			}
5324 
5325 			if (mbox->mbox_cmpl == lpfc_sli4_unreg_rpi_cmpl_clr)
5326 				/*
5327 				 * accept PLOGIs after unreg_rpi_cmpl
5328 				 */
5329 				acc_plogi = 0;
5330 			if (((ndlp->nlp_DID & Fabric_DID_MASK) !=
5331 			    Fabric_DID_MASK) &&
5332 			    (!(vport->fc_flag & FC_OFFLINE_MODE)))
5333 				ndlp->nlp_flag |= NLP_UNREG_INP;
5334 
5335 			lpfc_printf_vlog(vport, KERN_INFO,
5336 					 LOG_NODE | LOG_DISCOVERY,
5337 					 "1433 unreg_rpi UNREG x%x on "
5338 					 "NPort x%x deferred flg x%x "
5339 					 "Data:x%px\n",
5340 					 ndlp->nlp_rpi, ndlp->nlp_DID,
5341 					 ndlp->nlp_flag, ndlp);
5342 
5343 			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5344 			if (rc == MBX_NOT_FINISHED) {
5345 				ndlp->nlp_flag &= ~NLP_UNREG_INP;
5346 				mempool_free(mbox, phba->mbox_mem_pool);
5347 				acc_plogi = 1;
5348 				lpfc_nlp_put(ndlp);
5349 			}
5350 		} else {
5351 			lpfc_printf_vlog(vport, KERN_INFO,
5352 					 LOG_NODE | LOG_DISCOVERY,
5353 					 "1444 Failed to allocate mempool "
5354 					 "unreg_rpi UNREG x%x, "
5355 					 "DID x%x, flag x%x, "
5356 					 "ndlp x%px\n",
5357 					 ndlp->nlp_rpi, ndlp->nlp_DID,
5358 					 ndlp->nlp_flag, ndlp);
5359 
5360 			/* Because mempool_alloc failed, we
5361 			 * will issue a LOGO here and keep the rpi alive if
5362 			 * not unloading.
5363 			 */
5364 			if (!(vport->load_flag & FC_UNLOADING)) {
5365 				ndlp->nlp_flag &= ~NLP_UNREG_INP;
5366 				lpfc_issue_els_logo(vport, ndlp, 0);
5367 				ndlp->nlp_prev_state = ndlp->nlp_state;
5368 				lpfc_nlp_set_state(vport, ndlp,
5369 						   NLP_STE_NPR_NODE);
5370 			}
5371 
5372 			return 1;
5373 		}
5374 		lpfc_no_rpi(phba, ndlp);
5375 out:
5376 		if (phba->sli_rev != LPFC_SLI_REV4)
5377 			ndlp->nlp_rpi = 0;
5378 		ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
5379 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
5380 		if (acc_plogi)
5381 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5382 		return 1;
5383 	}
5384 	ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5385 	return 0;
5386 }
5387 
5388 /**
5389  * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
5390  * @phba: pointer to lpfc hba data structure.
5391  *
5392  * This routine is invoked to unregister all the currently registered RPIs
5393  * to the HBA.
5394  **/
5395 void
5396 lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
5397 {
5398 	struct lpfc_vport **vports;
5399 	struct lpfc_nodelist *ndlp;
5400 	struct Scsi_Host *shost;
5401 	int i;
5402 
5403 	vports = lpfc_create_vport_work_array(phba);
5404 	if (!vports) {
5405 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5406 				"2884 Vport array allocation failed \n");
5407 		return;
5408 	}
5409 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5410 		shost = lpfc_shost_from_vport(vports[i]);
5411 		spin_lock_irq(shost->host_lock);
5412 		list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5413 			if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5414 				/* The mempool_alloc might sleep */
5415 				spin_unlock_irq(shost->host_lock);
5416 				lpfc_unreg_rpi(vports[i], ndlp);
5417 				spin_lock_irq(shost->host_lock);
5418 			}
5419 		}
5420 		spin_unlock_irq(shost->host_lock);
5421 	}
5422 	lpfc_destroy_vport_work_array(phba, vports);
5423 }
5424 
5425 void
5426 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
5427 {
5428 	struct lpfc_hba  *phba  = vport->phba;
5429 	LPFC_MBOXQ_t     *mbox;
5430 	int rc;
5431 
5432 	if (phba->sli_rev == LPFC_SLI_REV4) {
5433 		lpfc_sli4_unreg_all_rpis(vport);
5434 		return;
5435 	}
5436 
5437 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5438 	if (mbox) {
5439 		lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
5440 				 mbox);
5441 		mbox->vport = vport;
5442 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5443 		mbox->ctx_ndlp = NULL;
5444 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5445 		if (rc != MBX_TIMEOUT)
5446 			mempool_free(mbox, phba->mbox_mem_pool);
5447 
5448 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5449 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5450 					 "1836 Could not issue "
5451 					 "unreg_login(all_rpis) status %d\n",
5452 					 rc);
5453 	}
5454 }
5455 
5456 void
5457 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
5458 {
5459 	struct lpfc_hba  *phba  = vport->phba;
5460 	LPFC_MBOXQ_t     *mbox;
5461 	int rc;
5462 
5463 	/* Unreg DID is an SLI3 operation. */
5464 	if (phba->sli_rev > LPFC_SLI_REV3)
5465 		return;
5466 
5467 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5468 	if (mbox) {
5469 		lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
5470 			       mbox);
5471 		mbox->vport = vport;
5472 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5473 		mbox->ctx_ndlp = NULL;
5474 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5475 		if (rc != MBX_TIMEOUT)
5476 			mempool_free(mbox, phba->mbox_mem_pool);
5477 
5478 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5479 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5480 					 "1815 Could not issue "
5481 					 "unreg_did (default rpis) status %d\n",
5482 					 rc);
5483 	}
5484 }
5485 
5486 /*
5487  * Free resources associated with LPFC_NODELIST entry
5488  * so it can be freed.
5489  */
5490 static int
5491 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5492 {
5493 	struct lpfc_hba  *phba = vport->phba;
5494 	LPFC_MBOXQ_t *mb, *nextmb;
5495 
5496 	/* Cleanup node for NPort <nlp_DID> */
5497 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5498 			 "0900 Cleanup node for NPort x%x "
5499 			 "Data: x%x x%x x%x\n",
5500 			 ndlp->nlp_DID, ndlp->nlp_flag,
5501 			 ndlp->nlp_state, ndlp->nlp_rpi);
5502 	lpfc_dequeue_node(vport, ndlp);
5503 
5504 	/* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
5505 
5506 	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
5507 	if ((mb = phba->sli.mbox_active)) {
5508 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5509 		   !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5510 		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5511 			mb->ctx_ndlp = NULL;
5512 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5513 		}
5514 	}
5515 
5516 	spin_lock_irq(&phba->hbalock);
5517 	/* Cleanup REG_LOGIN completions which are not yet processed */
5518 	list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
5519 		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
5520 			(mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
5521 			(ndlp != (struct lpfc_nodelist *)mb->ctx_ndlp))
5522 			continue;
5523 
5524 		mb->ctx_ndlp = NULL;
5525 		mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5526 	}
5527 
5528 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
5529 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5530 		   !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5531 		    (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5532 			list_del(&mb->list);
5533 			lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_LOCKED);
5534 
5535 			/* Don't invoke lpfc_nlp_put. The driver is in
5536 			 * lpfc_nlp_release context.
5537 			 */
5538 		}
5539 	}
5540 	spin_unlock_irq(&phba->hbalock);
5541 
5542 	lpfc_els_abort(phba, ndlp);
5543 
5544 	spin_lock_irq(&ndlp->lock);
5545 	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
5546 	spin_unlock_irq(&ndlp->lock);
5547 
5548 	ndlp->nlp_last_elscmd = 0;
5549 	del_timer_sync(&ndlp->nlp_delayfunc);
5550 
5551 	list_del_init(&ndlp->els_retry_evt.evt_listp);
5552 	list_del_init(&ndlp->dev_loss_evt.evt_listp);
5553 	list_del_init(&ndlp->recovery_evt.evt_listp);
5554 	lpfc_cleanup_vports_rrqs(vport, ndlp);
5555 
5556 	if (phba->sli_rev == LPFC_SLI_REV4)
5557 		ndlp->nlp_flag |= NLP_RELEASE_RPI;
5558 
5559 	return 0;
5560 }
5561 
5562 static int
5563 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5564 	      uint32_t did)
5565 {
5566 	D_ID mydid, ndlpdid, matchdid;
5567 
5568 	if (did == Bcast_DID)
5569 		return 0;
5570 
5571 	/* First check for Direct match */
5572 	if (ndlp->nlp_DID == did)
5573 		return 1;
5574 
5575 	/* Next check for area/domain identically equals 0 match */
5576 	mydid.un.word = vport->fc_myDID;
5577 	if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
5578 		return 0;
5579 	}
5580 
5581 	matchdid.un.word = did;
5582 	ndlpdid.un.word = ndlp->nlp_DID;
5583 	if (matchdid.un.b.id == ndlpdid.un.b.id) {
5584 		if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5585 		    (mydid.un.b.area == matchdid.un.b.area)) {
5586 			/* This code is supposed to match the ID
5587 			 * for a private loop device that is
5588 			 * connect to fl_port. But we need to
5589 			 * check that the port did not just go
5590 			 * from pt2pt to fabric or we could end
5591 			 * up matching ndlp->nlp_DID 000001 to
5592 			 * fabric DID 0x20101
5593 			 */
5594 			if ((ndlpdid.un.b.domain == 0) &&
5595 			    (ndlpdid.un.b.area == 0)) {
5596 				if (ndlpdid.un.b.id &&
5597 				    vport->phba->fc_topology ==
5598 				    LPFC_TOPOLOGY_LOOP)
5599 					return 1;
5600 			}
5601 			return 0;
5602 		}
5603 
5604 		matchdid.un.word = ndlp->nlp_DID;
5605 		if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5606 		    (mydid.un.b.area == ndlpdid.un.b.area)) {
5607 			if ((matchdid.un.b.domain == 0) &&
5608 			    (matchdid.un.b.area == 0)) {
5609 				if (matchdid.un.b.id)
5610 					return 1;
5611 			}
5612 		}
5613 	}
5614 	return 0;
5615 }
5616 
5617 /* Search for a nodelist entry */
5618 static struct lpfc_nodelist *
5619 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5620 {
5621 	struct lpfc_nodelist *ndlp;
5622 	uint32_t data1;
5623 
5624 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5625 		if (lpfc_matchdid(vport, ndlp, did)) {
5626 			data1 = (((uint32_t)ndlp->nlp_state << 24) |
5627 				 ((uint32_t)ndlp->nlp_xri << 16) |
5628 				 ((uint32_t)ndlp->nlp_type << 8)
5629 				 );
5630 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5631 					 "0929 FIND node DID "
5632 					 "Data: x%px x%x x%x x%x x%x x%px\n",
5633 					 ndlp, ndlp->nlp_DID,
5634 					 ndlp->nlp_flag, data1, ndlp->nlp_rpi,
5635 					 ndlp->active_rrqs_xri_bitmap);
5636 			return ndlp;
5637 		}
5638 	}
5639 
5640 	/* FIND node did <did> NOT FOUND */
5641 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5642 			 "0932 FIND node did x%x NOT FOUND.\n", did);
5643 	return NULL;
5644 }
5645 
5646 struct lpfc_nodelist *
5647 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5648 {
5649 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5650 	struct lpfc_nodelist *ndlp;
5651 	unsigned long iflags;
5652 
5653 	spin_lock_irqsave(shost->host_lock, iflags);
5654 	ndlp = __lpfc_findnode_did(vport, did);
5655 	spin_unlock_irqrestore(shost->host_lock, iflags);
5656 	return ndlp;
5657 }
5658 
5659 struct lpfc_nodelist *
5660 lpfc_findnode_mapped(struct lpfc_vport *vport)
5661 {
5662 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5663 	struct lpfc_nodelist *ndlp;
5664 	uint32_t data1;
5665 	unsigned long iflags;
5666 
5667 	spin_lock_irqsave(shost->host_lock, iflags);
5668 
5669 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5670 		if (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
5671 		    ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
5672 			data1 = (((uint32_t)ndlp->nlp_state << 24) |
5673 				 ((uint32_t)ndlp->nlp_xri << 16) |
5674 				 ((uint32_t)ndlp->nlp_type << 8) |
5675 				 ((uint32_t)ndlp->nlp_rpi & 0xff));
5676 			spin_unlock_irqrestore(shost->host_lock, iflags);
5677 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5678 					 "2025 FIND node DID "
5679 					 "Data: x%px x%x x%x x%x x%px\n",
5680 					 ndlp, ndlp->nlp_DID,
5681 					 ndlp->nlp_flag, data1,
5682 					 ndlp->active_rrqs_xri_bitmap);
5683 			return ndlp;
5684 		}
5685 	}
5686 	spin_unlock_irqrestore(shost->host_lock, iflags);
5687 
5688 	/* FIND node did <did> NOT FOUND */
5689 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5690 			 "2026 FIND mapped did NOT FOUND.\n");
5691 	return NULL;
5692 }
5693 
5694 struct lpfc_nodelist *
5695 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5696 {
5697 	struct lpfc_nodelist *ndlp;
5698 
5699 	ndlp = lpfc_findnode_did(vport, did);
5700 	if (!ndlp) {
5701 		if (vport->phba->nvmet_support)
5702 			return NULL;
5703 		if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5704 		    lpfc_rscn_payload_check(vport, did) == 0)
5705 			return NULL;
5706 		ndlp = lpfc_nlp_init(vport, did);
5707 		if (!ndlp)
5708 			return NULL;
5709 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5710 
5711 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5712 				 "6453 Setup New Node 2B_DISC x%x "
5713 				 "Data:x%x x%x x%x\n",
5714 				 ndlp->nlp_DID, ndlp->nlp_flag,
5715 				 ndlp->nlp_state, vport->fc_flag);
5716 
5717 		spin_lock_irq(&ndlp->lock);
5718 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5719 		spin_unlock_irq(&ndlp->lock);
5720 		return ndlp;
5721 	}
5722 
5723 	/* The NVME Target does not want to actively manage an rport.
5724 	 * The goal is to allow the target to reset its state and clear
5725 	 * pending IO in preparation for the initiator to recover.
5726 	 */
5727 	if ((vport->fc_flag & FC_RSCN_MODE) &&
5728 	    !(vport->fc_flag & FC_NDISC_ACTIVE)) {
5729 		if (lpfc_rscn_payload_check(vport, did)) {
5730 
5731 			/* Since this node is marked for discovery,
5732 			 * delay timeout is not needed.
5733 			 */
5734 			lpfc_cancel_retry_delay_tmo(vport, ndlp);
5735 
5736 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5737 					 "6455 Setup RSCN Node 2B_DISC x%x "
5738 					 "Data:x%x x%x x%x\n",
5739 					 ndlp->nlp_DID, ndlp->nlp_flag,
5740 					 ndlp->nlp_state, vport->fc_flag);
5741 
5742 			/* NVME Target mode waits until rport is known to be
5743 			 * impacted by the RSCN before it transitions.  No
5744 			 * active management - just go to NPR provided the
5745 			 * node had a valid login.
5746 			 */
5747 			if (vport->phba->nvmet_support)
5748 				return ndlp;
5749 
5750 			/* If we've already received a PLOGI from this NPort
5751 			 * we don't need to try to discover it again.
5752 			 */
5753 			if (ndlp->nlp_flag & NLP_RCV_PLOGI &&
5754 			    !(ndlp->nlp_type &
5755 			     (NLP_FCP_TARGET | NLP_NVME_TARGET)))
5756 				return NULL;
5757 
5758 			lpfc_disc_state_machine(vport, ndlp, NULL,
5759 						NLP_EVT_DEVICE_RECOVERY);
5760 
5761 			spin_lock_irq(&ndlp->lock);
5762 			ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5763 			spin_unlock_irq(&ndlp->lock);
5764 		} else {
5765 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5766 					 "6456 Skip Setup RSCN Node x%x "
5767 					 "Data:x%x x%x x%x\n",
5768 					 ndlp->nlp_DID, ndlp->nlp_flag,
5769 					 ndlp->nlp_state, vport->fc_flag);
5770 			ndlp = NULL;
5771 		}
5772 	} else {
5773 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5774 				 "6457 Setup Active Node 2B_DISC x%x "
5775 				 "Data:x%x x%x x%x\n",
5776 				 ndlp->nlp_DID, ndlp->nlp_flag,
5777 				 ndlp->nlp_state, vport->fc_flag);
5778 
5779 		/* If the initiator received a PLOGI from this NPort or if the
5780 		 * initiator is already in the process of discovery on it,
5781 		 * there's no need to try to discover it again.
5782 		 */
5783 		if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
5784 		    ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5785 		    (!vport->phba->nvmet_support &&
5786 		     ndlp->nlp_flag & NLP_RCV_PLOGI))
5787 			return NULL;
5788 
5789 		if (vport->phba->nvmet_support)
5790 			return ndlp;
5791 
5792 		/* Moving to NPR state clears unsolicited flags and
5793 		 * allows for rediscovery
5794 		 */
5795 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5796 
5797 		spin_lock_irq(&ndlp->lock);
5798 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5799 		spin_unlock_irq(&ndlp->lock);
5800 	}
5801 	return ndlp;
5802 }
5803 
5804 /* Build a list of nodes to discover based on the loopmap */
5805 void
5806 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
5807 {
5808 	struct lpfc_hba  *phba = vport->phba;
5809 	int j;
5810 	uint32_t alpa, index;
5811 
5812 	if (!lpfc_is_link_up(phba))
5813 		return;
5814 
5815 	if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
5816 		return;
5817 
5818 	/* Check for loop map present or not */
5819 	if (phba->alpa_map[0]) {
5820 		for (j = 1; j <= phba->alpa_map[0]; j++) {
5821 			alpa = phba->alpa_map[j];
5822 			if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
5823 				continue;
5824 			lpfc_setup_disc_node(vport, alpa);
5825 		}
5826 	} else {
5827 		/* No alpamap, so try all alpa's */
5828 		for (j = 0; j < FC_MAXLOOP; j++) {
5829 			/* If cfg_scan_down is set, start from highest
5830 			 * ALPA (0xef) to lowest (0x1).
5831 			 */
5832 			if (vport->cfg_scan_down)
5833 				index = j;
5834 			else
5835 				index = FC_MAXLOOP - j - 1;
5836 			alpa = lpfcAlpaArray[index];
5837 			if ((vport->fc_myDID & 0xff) == alpa)
5838 				continue;
5839 			lpfc_setup_disc_node(vport, alpa);
5840 		}
5841 	}
5842 	return;
5843 }
5844 
5845 /* SLI3 only */
5846 void
5847 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
5848 {
5849 	LPFC_MBOXQ_t *mbox;
5850 	struct lpfc_sli *psli = &phba->sli;
5851 	struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5852 	struct lpfc_sli_ring *fcp_ring   = &psli->sli3_ring[LPFC_FCP_RING];
5853 	int  rc;
5854 
5855 	/*
5856 	 * if it's not a physical port or if we already send
5857 	 * clear_la then don't send it.
5858 	 */
5859 	if ((phba->link_state >= LPFC_CLEAR_LA) ||
5860 	    (vport->port_type != LPFC_PHYSICAL_PORT) ||
5861 		(phba->sli_rev == LPFC_SLI_REV4))
5862 		return;
5863 
5864 			/* Link up discovery */
5865 	if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5866 		phba->link_state = LPFC_CLEAR_LA;
5867 		lpfc_clear_la(phba, mbox);
5868 		mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5869 		mbox->vport = vport;
5870 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5871 		if (rc == MBX_NOT_FINISHED) {
5872 			mempool_free(mbox, phba->mbox_mem_pool);
5873 			lpfc_disc_flush_list(vport);
5874 			extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5875 			fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5876 			phba->link_state = LPFC_HBA_ERROR;
5877 		}
5878 	}
5879 }
5880 
5881 /* Reg_vpi to tell firmware to resume normal operations */
5882 void
5883 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5884 {
5885 	LPFC_MBOXQ_t *regvpimbox;
5886 
5887 	regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5888 	if (regvpimbox) {
5889 		lpfc_reg_vpi(vport, regvpimbox);
5890 		regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5891 		regvpimbox->vport = vport;
5892 		if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
5893 					== MBX_NOT_FINISHED) {
5894 			mempool_free(regvpimbox, phba->mbox_mem_pool);
5895 		}
5896 	}
5897 }
5898 
5899 /* Start Link up / RSCN discovery on NPR nodes */
5900 void
5901 lpfc_disc_start(struct lpfc_vport *vport)
5902 {
5903 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5904 	struct lpfc_hba  *phba = vport->phba;
5905 	uint32_t num_sent;
5906 	uint32_t clear_la_pending;
5907 
5908 	if (!lpfc_is_link_up(phba)) {
5909 		lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5910 				 "3315 Link is not up %x\n",
5911 				 phba->link_state);
5912 		return;
5913 	}
5914 
5915 	if (phba->link_state == LPFC_CLEAR_LA)
5916 		clear_la_pending = 1;
5917 	else
5918 		clear_la_pending = 0;
5919 
5920 	if (vport->port_state < LPFC_VPORT_READY)
5921 		vport->port_state = LPFC_DISC_AUTH;
5922 
5923 	lpfc_set_disctmo(vport);
5924 
5925 	vport->fc_prevDID = vport->fc_myDID;
5926 	vport->num_disc_nodes = 0;
5927 
5928 	/* Start Discovery state <hba_state> */
5929 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5930 			 "0202 Start Discovery port state x%x "
5931 			 "flg x%x Data: x%x x%x x%x\n",
5932 			 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5933 			 vport->fc_adisc_cnt, vport->fc_npr_cnt);
5934 
5935 	/* First do ADISCs - if any */
5936 	num_sent = lpfc_els_disc_adisc(vport);
5937 
5938 	if (num_sent)
5939 		return;
5940 
5941 	/* Register the VPI for SLI3, NPIV only. */
5942 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5943 	    !(vport->fc_flag & FC_PT2PT) &&
5944 	    !(vport->fc_flag & FC_RSCN_MODE) &&
5945 	    (phba->sli_rev < LPFC_SLI_REV4)) {
5946 		lpfc_issue_clear_la(phba, vport);
5947 		lpfc_issue_reg_vpi(phba, vport);
5948 		return;
5949 	}
5950 
5951 	/*
5952 	 * For SLI2, we need to set port_state to READY and continue
5953 	 * discovery.
5954 	 */
5955 	if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5956 		/* If we get here, there is nothing to ADISC */
5957 		lpfc_issue_clear_la(phba, vport);
5958 
5959 		if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
5960 			vport->num_disc_nodes = 0;
5961 			/* go thru NPR nodes and issue ELS PLOGIs */
5962 			if (vport->fc_npr_cnt)
5963 				lpfc_els_disc_plogi(vport);
5964 
5965 			if (!vport->num_disc_nodes) {
5966 				spin_lock_irq(shost->host_lock);
5967 				vport->fc_flag &= ~FC_NDISC_ACTIVE;
5968 				spin_unlock_irq(shost->host_lock);
5969 				lpfc_can_disctmo(vport);
5970 			}
5971 		}
5972 		vport->port_state = LPFC_VPORT_READY;
5973 	} else {
5974 		/* Next do PLOGIs - if any */
5975 		num_sent = lpfc_els_disc_plogi(vport);
5976 
5977 		if (num_sent)
5978 			return;
5979 
5980 		if (vport->fc_flag & FC_RSCN_MODE) {
5981 			/* Check to see if more RSCNs came in while we
5982 			 * were processing this one.
5983 			 */
5984 			if ((vport->fc_rscn_id_cnt == 0) &&
5985 			    (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5986 				spin_lock_irq(shost->host_lock);
5987 				vport->fc_flag &= ~FC_RSCN_MODE;
5988 				spin_unlock_irq(shost->host_lock);
5989 				lpfc_can_disctmo(vport);
5990 			} else
5991 				lpfc_els_handle_rscn(vport);
5992 		}
5993 	}
5994 	return;
5995 }
5996 
5997 /*
5998  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5999  *  ring the match the sppecified nodelist.
6000  */
6001 static void
6002 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
6003 {
6004 	LIST_HEAD(completions);
6005 	struct lpfc_iocbq    *iocb, *next_iocb;
6006 	struct lpfc_sli_ring *pring;
6007 	u32 ulp_command;
6008 
6009 	pring = lpfc_phba_elsring(phba);
6010 	if (unlikely(!pring))
6011 		return;
6012 
6013 	/* Error matching iocb on txq or txcmplq
6014 	 * First check the txq.
6015 	 */
6016 	spin_lock_irq(&phba->hbalock);
6017 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6018 		if (iocb->ndlp != ndlp)
6019 			continue;
6020 
6021 		ulp_command = get_job_cmnd(phba, iocb);
6022 
6023 		if (ulp_command == CMD_ELS_REQUEST64_CR ||
6024 		    ulp_command == CMD_XMIT_ELS_RSP64_CX) {
6025 
6026 			list_move_tail(&iocb->list, &completions);
6027 		}
6028 	}
6029 
6030 	/* Next check the txcmplq */
6031 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
6032 		if (iocb->ndlp != ndlp)
6033 			continue;
6034 
6035 		ulp_command = get_job_cmnd(phba, iocb);
6036 
6037 		if (ulp_command == CMD_ELS_REQUEST64_CR ||
6038 		    ulp_command == CMD_XMIT_ELS_RSP64_CX) {
6039 			lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
6040 		}
6041 	}
6042 	spin_unlock_irq(&phba->hbalock);
6043 
6044 	/* Make sure HBA is alive */
6045 	lpfc_issue_hb_tmo(phba);
6046 
6047 	/* Cancel all the IOCBs from the completions list */
6048 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6049 			      IOERR_SLI_ABORTED);
6050 }
6051 
6052 static void
6053 lpfc_disc_flush_list(struct lpfc_vport *vport)
6054 {
6055 	struct lpfc_nodelist *ndlp, *next_ndlp;
6056 	struct lpfc_hba *phba = vport->phba;
6057 
6058 	if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
6059 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
6060 					 nlp_listp) {
6061 			if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
6062 			    ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
6063 				lpfc_free_tx(phba, ndlp);
6064 			}
6065 		}
6066 	}
6067 }
6068 
6069 /*
6070  * lpfc_notify_xport_npr - notifies xport of node disappearance
6071  * @vport: Pointer to Virtual Port object.
6072  *
6073  * Transitions all ndlps to NPR state.  When lpfc_nlp_set_state
6074  * calls lpfc_nlp_state_cleanup, the ndlp->rport is unregistered
6075  * and transport notified that the node is gone.
6076  * Return Code:
6077  *	none
6078  */
6079 static void
6080 lpfc_notify_xport_npr(struct lpfc_vport *vport)
6081 {
6082 	struct lpfc_nodelist *ndlp, *next_ndlp;
6083 
6084 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
6085 				 nlp_listp) {
6086 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
6087 	}
6088 }
6089 void
6090 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
6091 {
6092 	lpfc_els_flush_rscn(vport);
6093 	lpfc_els_flush_cmd(vport);
6094 	lpfc_disc_flush_list(vport);
6095 	if (pci_channel_offline(vport->phba->pcidev))
6096 		lpfc_notify_xport_npr(vport);
6097 }
6098 
6099 /*****************************************************************************/
6100 /*
6101  * NAME:     lpfc_disc_timeout
6102  *
6103  * FUNCTION: Fibre Channel driver discovery timeout routine.
6104  *
6105  * EXECUTION ENVIRONMENT: interrupt only
6106  *
6107  * CALLED FROM:
6108  *      Timer function
6109  *
6110  * RETURNS:
6111  *      none
6112  */
6113 /*****************************************************************************/
6114 void
6115 lpfc_disc_timeout(struct timer_list *t)
6116 {
6117 	struct lpfc_vport *vport = from_timer(vport, t, fc_disctmo);
6118 	struct lpfc_hba   *phba = vport->phba;
6119 	uint32_t tmo_posted;
6120 	unsigned long flags = 0;
6121 
6122 	if (unlikely(!phba))
6123 		return;
6124 
6125 	spin_lock_irqsave(&vport->work_port_lock, flags);
6126 	tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
6127 	if (!tmo_posted)
6128 		vport->work_port_events |= WORKER_DISC_TMO;
6129 	spin_unlock_irqrestore(&vport->work_port_lock, flags);
6130 
6131 	if (!tmo_posted)
6132 		lpfc_worker_wake_up(phba);
6133 	return;
6134 }
6135 
6136 static void
6137 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
6138 {
6139 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6140 	struct lpfc_hba  *phba = vport->phba;
6141 	struct lpfc_sli  *psli = &phba->sli;
6142 	struct lpfc_nodelist *ndlp, *next_ndlp;
6143 	LPFC_MBOXQ_t *initlinkmbox;
6144 	int rc, clrlaerr = 0;
6145 
6146 	if (!(vport->fc_flag & FC_DISC_TMO))
6147 		return;
6148 
6149 	spin_lock_irq(shost->host_lock);
6150 	vport->fc_flag &= ~FC_DISC_TMO;
6151 	spin_unlock_irq(shost->host_lock);
6152 
6153 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6154 		"disc timeout:    state:x%x rtry:x%x flg:x%x",
6155 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
6156 
6157 	switch (vport->port_state) {
6158 
6159 	case LPFC_LOCAL_CFG_LINK:
6160 		/*
6161 		 * port_state is identically  LPFC_LOCAL_CFG_LINK while
6162 		 * waiting for FAN timeout
6163 		 */
6164 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
6165 				 "0221 FAN timeout\n");
6166 
6167 		/* Start discovery by sending FLOGI, clean up old rpis */
6168 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
6169 					 nlp_listp) {
6170 			if (ndlp->nlp_state != NLP_STE_NPR_NODE)
6171 				continue;
6172 			if (ndlp->nlp_type & NLP_FABRIC) {
6173 				/* Clean up the ndlp on Fabric connections */
6174 				lpfc_drop_node(vport, ndlp);
6175 
6176 			} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
6177 				/* Fail outstanding IO now since device
6178 				 * is marked for PLOGI.
6179 				 */
6180 				lpfc_unreg_rpi(vport, ndlp);
6181 			}
6182 		}
6183 		if (vport->port_state != LPFC_FLOGI) {
6184 			if (phba->sli_rev <= LPFC_SLI_REV3)
6185 				lpfc_initial_flogi(vport);
6186 			else
6187 				lpfc_issue_init_vfi(vport);
6188 			return;
6189 		}
6190 		break;
6191 
6192 	case LPFC_FDISC:
6193 	case LPFC_FLOGI:
6194 	/* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
6195 		/* Initial FLOGI timeout */
6196 		lpfc_printf_vlog(vport, KERN_ERR,
6197 				 LOG_TRACE_EVENT,
6198 				 "0222 Initial %s timeout\n",
6199 				 vport->vpi ? "FDISC" : "FLOGI");
6200 
6201 		/* Assume no Fabric and go on with discovery.
6202 		 * Check for outstanding ELS FLOGI to abort.
6203 		 */
6204 
6205 		/* FLOGI failed, so just use loop map to make discovery list */
6206 		lpfc_disc_list_loopmap(vport);
6207 
6208 		/* Start discovery */
6209 		lpfc_disc_start(vport);
6210 		break;
6211 
6212 	case LPFC_FABRIC_CFG_LINK:
6213 	/* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
6214 	   NameServer login */
6215 		lpfc_printf_vlog(vport, KERN_ERR,
6216 				 LOG_TRACE_EVENT,
6217 				 "0223 Timeout while waiting for "
6218 				 "NameServer login\n");
6219 		/* Next look for NameServer ndlp */
6220 		ndlp = lpfc_findnode_did(vport, NameServer_DID);
6221 		if (ndlp)
6222 			lpfc_els_abort(phba, ndlp);
6223 
6224 		/* ReStart discovery */
6225 		goto restart_disc;
6226 
6227 	case LPFC_NS_QRY:
6228 	/* Check for wait for NameServer Rsp timeout */
6229 		lpfc_printf_vlog(vport, KERN_ERR,
6230 				 LOG_TRACE_EVENT,
6231 				 "0224 NameServer Query timeout "
6232 				 "Data: x%x x%x\n",
6233 				 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
6234 
6235 		if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
6236 			/* Try it one more time */
6237 			vport->fc_ns_retry++;
6238 			vport->gidft_inp = 0;
6239 			rc = lpfc_issue_gidft(vport);
6240 			if (rc == 0)
6241 				break;
6242 		}
6243 		vport->fc_ns_retry = 0;
6244 
6245 restart_disc:
6246 		/*
6247 		 * Discovery is over.
6248 		 * set port_state to PORT_READY if SLI2.
6249 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
6250 		 */
6251 		if (phba->sli_rev < LPFC_SLI_REV4) {
6252 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
6253 				lpfc_issue_reg_vpi(phba, vport);
6254 			else  {
6255 				lpfc_issue_clear_la(phba, vport);
6256 				vport->port_state = LPFC_VPORT_READY;
6257 			}
6258 		}
6259 
6260 		/* Setup and issue mailbox INITIALIZE LINK command */
6261 		initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6262 		if (!initlinkmbox) {
6263 			lpfc_printf_vlog(vport, KERN_ERR,
6264 					 LOG_TRACE_EVENT,
6265 					 "0206 Device Discovery "
6266 					 "completion error\n");
6267 			phba->link_state = LPFC_HBA_ERROR;
6268 			break;
6269 		}
6270 
6271 		lpfc_linkdown(phba);
6272 		lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
6273 			       phba->cfg_link_speed);
6274 		initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
6275 		initlinkmbox->vport = vport;
6276 		initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6277 		rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
6278 		lpfc_set_loopback_flag(phba);
6279 		if (rc == MBX_NOT_FINISHED)
6280 			mempool_free(initlinkmbox, phba->mbox_mem_pool);
6281 
6282 		break;
6283 
6284 	case LPFC_DISC_AUTH:
6285 	/* Node Authentication timeout */
6286 		lpfc_printf_vlog(vport, KERN_ERR,
6287 				 LOG_TRACE_EVENT,
6288 				 "0227 Node Authentication timeout\n");
6289 		lpfc_disc_flush_list(vport);
6290 
6291 		/*
6292 		 * set port_state to PORT_READY if SLI2.
6293 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
6294 		 */
6295 		if (phba->sli_rev < LPFC_SLI_REV4) {
6296 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
6297 				lpfc_issue_reg_vpi(phba, vport);
6298 			else  {	/* NPIV Not enabled */
6299 				lpfc_issue_clear_la(phba, vport);
6300 				vport->port_state = LPFC_VPORT_READY;
6301 			}
6302 		}
6303 		break;
6304 
6305 	case LPFC_VPORT_READY:
6306 		if (vport->fc_flag & FC_RSCN_MODE) {
6307 			lpfc_printf_vlog(vport, KERN_ERR,
6308 					 LOG_TRACE_EVENT,
6309 					 "0231 RSCN timeout Data: x%x "
6310 					 "x%x x%x x%x\n",
6311 					 vport->fc_ns_retry, LPFC_MAX_NS_RETRY,
6312 					 vport->port_state, vport->gidft_inp);
6313 
6314 			/* Cleanup any outstanding ELS commands */
6315 			lpfc_els_flush_cmd(vport);
6316 
6317 			lpfc_els_flush_rscn(vport);
6318 			lpfc_disc_flush_list(vport);
6319 		}
6320 		break;
6321 
6322 	default:
6323 		lpfc_printf_vlog(vport, KERN_ERR,
6324 				 LOG_TRACE_EVENT,
6325 				 "0273 Unexpected discovery timeout, "
6326 				 "vport State x%x\n", vport->port_state);
6327 		break;
6328 	}
6329 
6330 	switch (phba->link_state) {
6331 	case LPFC_CLEAR_LA:
6332 				/* CLEAR LA timeout */
6333 		lpfc_printf_vlog(vport, KERN_ERR,
6334 				 LOG_TRACE_EVENT,
6335 				 "0228 CLEAR LA timeout\n");
6336 		clrlaerr = 1;
6337 		break;
6338 
6339 	case LPFC_LINK_UP:
6340 		lpfc_issue_clear_la(phba, vport);
6341 		fallthrough;
6342 	case LPFC_LINK_UNKNOWN:
6343 	case LPFC_WARM_START:
6344 	case LPFC_INIT_START:
6345 	case LPFC_INIT_MBX_CMDS:
6346 	case LPFC_LINK_DOWN:
6347 	case LPFC_HBA_ERROR:
6348 		lpfc_printf_vlog(vport, KERN_ERR,
6349 				 LOG_TRACE_EVENT,
6350 				 "0230 Unexpected timeout, hba link "
6351 				 "state x%x\n", phba->link_state);
6352 		clrlaerr = 1;
6353 		break;
6354 
6355 	case LPFC_HBA_READY:
6356 		break;
6357 	}
6358 
6359 	if (clrlaerr) {
6360 		lpfc_disc_flush_list(vport);
6361 		if (phba->sli_rev != LPFC_SLI_REV4) {
6362 			psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
6363 				~LPFC_STOP_IOCB_EVENT;
6364 			psli->sli3_ring[LPFC_FCP_RING].flag &=
6365 				~LPFC_STOP_IOCB_EVENT;
6366 		}
6367 		vport->port_state = LPFC_VPORT_READY;
6368 	}
6369 	return;
6370 }
6371 
6372 /*
6373  * This routine handles processing a NameServer REG_LOGIN mailbox
6374  * command upon completion. It is setup in the LPFC_MBOXQ
6375  * as the completion routine when the command is
6376  * handed off to the SLI layer.
6377  */
6378 void
6379 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6380 {
6381 	MAILBOX_t *mb = &pmb->u.mb;
6382 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
6383 	struct lpfc_vport    *vport = pmb->vport;
6384 
6385 	pmb->ctx_ndlp = NULL;
6386 
6387 	if (phba->sli_rev < LPFC_SLI_REV4)
6388 		ndlp->nlp_rpi = mb->un.varWords[0];
6389 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
6390 	ndlp->nlp_type |= NLP_FABRIC;
6391 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
6392 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
6393 			 "0004 rpi:%x DID:%x flg:%x %d x%px\n",
6394 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
6395 			 kref_read(&ndlp->kref),
6396 			 ndlp);
6397 	/*
6398 	 * Start issuing Fabric-Device Management Interface (FDMI) command to
6399 	 * 0xfffffa (FDMI well known port).
6400 	 * DHBA -> DPRT -> RHBA -> RPA  (physical port)
6401 	 * DPRT -> RPRT (vports)
6402 	 */
6403 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
6404 		phba->link_flag &= ~LS_CT_VEN_RPA; /* For extra Vendor RPA */
6405 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
6406 	} else {
6407 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
6408 	}
6409 
6410 
6411 	/* decrement the node reference count held for this callback
6412 	 * function.
6413 	 */
6414 	lpfc_nlp_put(ndlp);
6415 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
6416 	return;
6417 }
6418 
6419 static int
6420 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
6421 {
6422 	uint16_t *rpi = param;
6423 
6424 	return ndlp->nlp_rpi == *rpi;
6425 }
6426 
6427 static int
6428 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
6429 {
6430 	return memcmp(&ndlp->nlp_portname, param,
6431 		      sizeof(ndlp->nlp_portname)) == 0;
6432 }
6433 
6434 static struct lpfc_nodelist *
6435 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
6436 {
6437 	struct lpfc_nodelist *ndlp;
6438 
6439 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
6440 		if (filter(ndlp, param)) {
6441 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6442 					 "3185 FIND node filter %ps DID "
6443 					 "ndlp x%px did x%x flg x%x st x%x "
6444 					 "xri x%x type x%x rpi x%x\n",
6445 					 filter, ndlp, ndlp->nlp_DID,
6446 					 ndlp->nlp_flag, ndlp->nlp_state,
6447 					 ndlp->nlp_xri, ndlp->nlp_type,
6448 					 ndlp->nlp_rpi);
6449 			return ndlp;
6450 		}
6451 	}
6452 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6453 			 "3186 FIND node filter %ps NOT FOUND.\n", filter);
6454 	return NULL;
6455 }
6456 
6457 /*
6458  * This routine looks up the ndlp lists for the given RPI. If rpi found it
6459  * returns the node list element pointer else return NULL.
6460  */
6461 struct lpfc_nodelist *
6462 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6463 {
6464 	return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
6465 }
6466 
6467 /*
6468  * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
6469  * returns the node element list pointer else return NULL.
6470  */
6471 struct lpfc_nodelist *
6472 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
6473 {
6474 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6475 	struct lpfc_nodelist *ndlp;
6476 
6477 	spin_lock_irq(shost->host_lock);
6478 	ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
6479 	spin_unlock_irq(shost->host_lock);
6480 	return ndlp;
6481 }
6482 
6483 /*
6484  * This routine looks up the ndlp lists for the given RPI. If the rpi
6485  * is found, the routine returns the node element list pointer else
6486  * return NULL.
6487  */
6488 struct lpfc_nodelist *
6489 lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6490 {
6491 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6492 	struct lpfc_nodelist *ndlp;
6493 	unsigned long flags;
6494 
6495 	spin_lock_irqsave(shost->host_lock, flags);
6496 	ndlp = __lpfc_findnode_rpi(vport, rpi);
6497 	spin_unlock_irqrestore(shost->host_lock, flags);
6498 	return ndlp;
6499 }
6500 
6501 /**
6502  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6503  * @phba: pointer to lpfc hba data structure.
6504  * @vpi: the physical host virtual N_Port identifier.
6505  *
6506  * This routine finds a vport on a HBA (referred by @phba) through a
6507  * @vpi. The function walks the HBA's vport list and returns the address
6508  * of the vport with the matching @vpi.
6509  *
6510  * Return code
6511  *    NULL - No vport with the matching @vpi found
6512  *    Otherwise - Address to the vport with the matching @vpi.
6513  **/
6514 struct lpfc_vport *
6515 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6516 {
6517 	struct lpfc_vport *vport;
6518 	unsigned long flags;
6519 	int i = 0;
6520 
6521 	/* The physical ports are always vpi 0 - translate is unnecessary. */
6522 	if (vpi > 0) {
6523 		/*
6524 		 * Translate the physical vpi to the logical vpi.  The
6525 		 * vport stores the logical vpi.
6526 		 */
6527 		for (i = 0; i <= phba->max_vpi; i++) {
6528 			if (vpi == phba->vpi_ids[i])
6529 				break;
6530 		}
6531 
6532 		if (i > phba->max_vpi) {
6533 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6534 					"2936 Could not find Vport mapped "
6535 					"to vpi %d\n", vpi);
6536 			return NULL;
6537 		}
6538 	}
6539 
6540 	spin_lock_irqsave(&phba->port_list_lock, flags);
6541 	list_for_each_entry(vport, &phba->port_list, listentry) {
6542 		if (vport->vpi == i) {
6543 			spin_unlock_irqrestore(&phba->port_list_lock, flags);
6544 			return vport;
6545 		}
6546 	}
6547 	spin_unlock_irqrestore(&phba->port_list_lock, flags);
6548 	return NULL;
6549 }
6550 
6551 struct lpfc_nodelist *
6552 lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
6553 {
6554 	struct lpfc_nodelist *ndlp;
6555 	int rpi = LPFC_RPI_ALLOC_ERROR;
6556 
6557 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6558 		rpi = lpfc_sli4_alloc_rpi(vport->phba);
6559 		if (rpi == LPFC_RPI_ALLOC_ERROR)
6560 			return NULL;
6561 	}
6562 
6563 	ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
6564 	if (!ndlp) {
6565 		if (vport->phba->sli_rev == LPFC_SLI_REV4)
6566 			lpfc_sli4_free_rpi(vport->phba, rpi);
6567 		return NULL;
6568 	}
6569 
6570 	memset(ndlp, 0, sizeof (struct lpfc_nodelist));
6571 
6572 	spin_lock_init(&ndlp->lock);
6573 
6574 	lpfc_initialize_node(vport, ndlp, did);
6575 	INIT_LIST_HEAD(&ndlp->nlp_listp);
6576 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6577 		ndlp->nlp_rpi = rpi;
6578 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
6579 				 "0007 Init New ndlp x%px, rpi:x%x DID:%x "
6580 				 "flg:x%x refcnt:%d\n",
6581 				 ndlp, ndlp->nlp_rpi, ndlp->nlp_DID,
6582 				 ndlp->nlp_flag, kref_read(&ndlp->kref));
6583 
6584 		ndlp->active_rrqs_xri_bitmap =
6585 				mempool_alloc(vport->phba->active_rrq_pool,
6586 					      GFP_KERNEL);
6587 		if (ndlp->active_rrqs_xri_bitmap)
6588 			memset(ndlp->active_rrqs_xri_bitmap, 0,
6589 			       ndlp->phba->cfg_rrq_xri_bitmap_sz);
6590 	}
6591 
6592 
6593 
6594 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
6595 		"node init:       did:x%x",
6596 		ndlp->nlp_DID, 0, 0);
6597 
6598 	return ndlp;
6599 }
6600 
6601 /* This routine releases all resources associated with a specifc NPort's ndlp
6602  * and mempool_free's the nodelist.
6603  */
6604 static void
6605 lpfc_nlp_release(struct kref *kref)
6606 {
6607 	struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
6608 						  kref);
6609 	struct lpfc_vport *vport = ndlp->vport;
6610 
6611 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6612 		"node release:    did:x%x flg:x%x type:x%x",
6613 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
6614 
6615 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6616 			 "0279 %s: ndlp: x%px did %x refcnt:%d rpi:%x\n",
6617 			 __func__, ndlp, ndlp->nlp_DID,
6618 			 kref_read(&ndlp->kref), ndlp->nlp_rpi);
6619 
6620 	/* remove ndlp from action. */
6621 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
6622 	lpfc_cleanup_node(vport, ndlp);
6623 
6624 	/* Not all ELS transactions have registered the RPI with the port.
6625 	 * In these cases the rpi usage is temporary and the node is
6626 	 * released when the WQE is completed.  Catch this case to free the
6627 	 * RPI to the pool.  Because this node is in the release path, a lock
6628 	 * is unnecessary.  All references are gone and the node has been
6629 	 * dequeued.
6630 	 */
6631 	if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
6632 		if (ndlp->nlp_rpi != LPFC_RPI_ALLOC_ERROR &&
6633 		    !(ndlp->nlp_flag & (NLP_RPI_REGISTERED | NLP_UNREG_INP))) {
6634 			lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
6635 			ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
6636 		}
6637 	}
6638 
6639 	/* The node is not freed back to memory, it is released to a pool so
6640 	 * the node fields need to be cleaned up.
6641 	 */
6642 	ndlp->vport = NULL;
6643 	ndlp->nlp_state = NLP_STE_FREED_NODE;
6644 	ndlp->nlp_flag = 0;
6645 	ndlp->fc4_xpt_flags = 0;
6646 
6647 	/* free ndlp memory for final ndlp release */
6648 	if (ndlp->phba->sli_rev == LPFC_SLI_REV4)
6649 		mempool_free(ndlp->active_rrqs_xri_bitmap,
6650 				ndlp->phba->active_rrq_pool);
6651 	mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
6652 }
6653 
6654 /* This routine bumps the reference count for a ndlp structure to ensure
6655  * that one discovery thread won't free a ndlp while another discovery thread
6656  * is using it.
6657  */
6658 struct lpfc_nodelist *
6659 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6660 {
6661 	unsigned long flags;
6662 
6663 	if (ndlp) {
6664 		lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6665 			"node get:        did:x%x flg:x%x refcnt:x%x",
6666 			ndlp->nlp_DID, ndlp->nlp_flag,
6667 			kref_read(&ndlp->kref));
6668 
6669 		/* The check of ndlp usage to prevent incrementing the
6670 		 * ndlp reference count that is in the process of being
6671 		 * released.
6672 		 */
6673 		spin_lock_irqsave(&ndlp->lock, flags);
6674 		if (!kref_get_unless_zero(&ndlp->kref)) {
6675 			spin_unlock_irqrestore(&ndlp->lock, flags);
6676 			lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6677 				"0276 %s: ndlp:x%px refcnt:%d\n",
6678 				__func__, (void *)ndlp, kref_read(&ndlp->kref));
6679 			return NULL;
6680 		}
6681 		spin_unlock_irqrestore(&ndlp->lock, flags);
6682 	} else {
6683 		WARN_ONCE(!ndlp, "**** %s, get ref on NULL ndlp!", __func__);
6684 	}
6685 
6686 	return ndlp;
6687 }
6688 
6689 /* This routine decrements the reference count for a ndlp structure. If the
6690  * count goes to 0, this indicates the associated nodelist should be freed.
6691  */
6692 int
6693 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6694 {
6695 	if (ndlp) {
6696 		lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6697 				"node put:        did:x%x flg:x%x refcnt:x%x",
6698 				ndlp->nlp_DID, ndlp->nlp_flag,
6699 				kref_read(&ndlp->kref));
6700 	} else {
6701 		WARN_ONCE(!ndlp, "**** %s, put ref on NULL ndlp!", __func__);
6702 	}
6703 
6704 	return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
6705 }
6706 
6707 /* This routine free's the specified nodelist if it is not in use
6708  * by any other discovery thread. This routine returns 1 if the
6709  * ndlp has been freed. A return value of 0 indicates the ndlp is
6710  * not yet been released.
6711  */
6712 int
6713 lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6714 {
6715 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6716 		"node not used:   did:x%x flg:x%x refcnt:x%x",
6717 		ndlp->nlp_DID, ndlp->nlp_flag,
6718 		kref_read(&ndlp->kref));
6719 
6720 	if (kref_read(&ndlp->kref) == 1)
6721 		if (lpfc_nlp_put(ndlp))
6722 			return 1;
6723 	return 0;
6724 }
6725 
6726 /**
6727  * lpfc_fcf_inuse - Check if FCF can be unregistered.
6728  * @phba: Pointer to hba context object.
6729  *
6730  * This function iterate through all FC nodes associated
6731  * will all vports to check if there is any node with
6732  * fc_rports associated with it. If there is an fc_rport
6733  * associated with the node, then the node is either in
6734  * discovered state or its devloss_timer is pending.
6735  */
6736 static int
6737 lpfc_fcf_inuse(struct lpfc_hba *phba)
6738 {
6739 	struct lpfc_vport **vports;
6740 	int i, ret = 0;
6741 	struct lpfc_nodelist *ndlp;
6742 	struct Scsi_Host  *shost;
6743 
6744 	vports = lpfc_create_vport_work_array(phba);
6745 
6746 	/* If driver cannot allocate memory, indicate fcf is in use */
6747 	if (!vports)
6748 		return 1;
6749 
6750 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6751 		shost = lpfc_shost_from_vport(vports[i]);
6752 		spin_lock_irq(shost->host_lock);
6753 		/*
6754 		 * IF the CVL_RCVD bit is not set then we have sent the
6755 		 * flogi.
6756 		 * If dev_loss fires while we are waiting we do not want to
6757 		 * unreg the fcf.
6758 		 */
6759 		if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6760 			spin_unlock_irq(shost->host_lock);
6761 			ret =  1;
6762 			goto out;
6763 		}
6764 		list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6765 			if (ndlp->rport &&
6766 			  (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6767 				ret = 1;
6768 				spin_unlock_irq(shost->host_lock);
6769 				goto out;
6770 			} else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6771 				ret = 1;
6772 				lpfc_printf_log(phba, KERN_INFO,
6773 						LOG_NODE | LOG_DISCOVERY,
6774 						"2624 RPI %x DID %x flag %x "
6775 						"still logged in\n",
6776 						ndlp->nlp_rpi, ndlp->nlp_DID,
6777 						ndlp->nlp_flag);
6778 			}
6779 		}
6780 		spin_unlock_irq(shost->host_lock);
6781 	}
6782 out:
6783 	lpfc_destroy_vport_work_array(phba, vports);
6784 	return ret;
6785 }
6786 
6787 /**
6788  * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6789  * @phba: Pointer to hba context object.
6790  * @mboxq: Pointer to mailbox object.
6791  *
6792  * This function frees memory associated with the mailbox command.
6793  */
6794 void
6795 lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6796 {
6797 	struct lpfc_vport *vport = mboxq->vport;
6798 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6799 
6800 	if (mboxq->u.mb.mbxStatus) {
6801 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6802 				"2555 UNREG_VFI mbxStatus error x%x "
6803 				"HBA state x%x\n",
6804 				mboxq->u.mb.mbxStatus, vport->port_state);
6805 	}
6806 	spin_lock_irq(shost->host_lock);
6807 	phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6808 	spin_unlock_irq(shost->host_lock);
6809 	mempool_free(mboxq, phba->mbox_mem_pool);
6810 	return;
6811 }
6812 
6813 /**
6814  * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6815  * @phba: Pointer to hba context object.
6816  * @mboxq: Pointer to mailbox object.
6817  *
6818  * This function frees memory associated with the mailbox command.
6819  */
6820 static void
6821 lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6822 {
6823 	struct lpfc_vport *vport = mboxq->vport;
6824 
6825 	if (mboxq->u.mb.mbxStatus) {
6826 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6827 				"2550 UNREG_FCFI mbxStatus error x%x "
6828 				"HBA state x%x\n",
6829 				mboxq->u.mb.mbxStatus, vport->port_state);
6830 	}
6831 	mempool_free(mboxq, phba->mbox_mem_pool);
6832 	return;
6833 }
6834 
6835 /**
6836  * lpfc_unregister_fcf_prep - Unregister fcf record preparation
6837  * @phba: Pointer to hba context object.
6838  *
6839  * This function prepare the HBA for unregistering the currently registered
6840  * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6841  * VFIs.
6842  */
6843 int
6844 lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
6845 {
6846 	struct lpfc_vport **vports;
6847 	struct lpfc_nodelist *ndlp;
6848 	struct Scsi_Host *shost;
6849 	int i = 0, rc;
6850 
6851 	/* Unregister RPIs */
6852 	if (lpfc_fcf_inuse(phba))
6853 		lpfc_unreg_hba_rpis(phba);
6854 
6855 	/* At this point, all discovery is aborted */
6856 	phba->pport->port_state = LPFC_VPORT_UNKNOWN;
6857 
6858 	/* Unregister VPIs */
6859 	vports = lpfc_create_vport_work_array(phba);
6860 	if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
6861 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6862 			/* Stop FLOGI/FDISC retries */
6863 			ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6864 			if (ndlp)
6865 				lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
6866 			lpfc_cleanup_pending_mbox(vports[i]);
6867 			if (phba->sli_rev == LPFC_SLI_REV4)
6868 				lpfc_sli4_unreg_all_rpis(vports[i]);
6869 			lpfc_mbx_unreg_vpi(vports[i]);
6870 			shost = lpfc_shost_from_vport(vports[i]);
6871 			spin_lock_irq(shost->host_lock);
6872 			vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6873 			vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
6874 			spin_unlock_irq(shost->host_lock);
6875 		}
6876 	lpfc_destroy_vport_work_array(phba, vports);
6877 	if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6878 		ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6879 		if (ndlp)
6880 			lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6881 		lpfc_cleanup_pending_mbox(phba->pport);
6882 		if (phba->sli_rev == LPFC_SLI_REV4)
6883 			lpfc_sli4_unreg_all_rpis(phba->pport);
6884 		lpfc_mbx_unreg_vpi(phba->pport);
6885 		shost = lpfc_shost_from_vport(phba->pport);
6886 		spin_lock_irq(shost->host_lock);
6887 		phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6888 		phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6889 		spin_unlock_irq(shost->host_lock);
6890 	}
6891 
6892 	/* Cleanup any outstanding ELS commands */
6893 	lpfc_els_flush_all_cmd(phba);
6894 
6895 	/* Unregister the physical port VFI */
6896 	rc = lpfc_issue_unreg_vfi(phba->pport);
6897 	return rc;
6898 }
6899 
6900 /**
6901  * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6902  * @phba: Pointer to hba context object.
6903  *
6904  * This function issues synchronous unregister FCF mailbox command to HBA to
6905  * unregister the currently registered FCF record. The driver does not reset
6906  * the driver FCF usage state flags.
6907  *
6908  * Return 0 if successfully issued, none-zero otherwise.
6909  */
6910 int
6911 lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6912 {
6913 	LPFC_MBOXQ_t *mbox;
6914 	int rc;
6915 
6916 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6917 	if (!mbox) {
6918 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6919 				"2551 UNREG_FCFI mbox allocation failed"
6920 				"HBA state x%x\n", phba->pport->port_state);
6921 		return -ENOMEM;
6922 	}
6923 	lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6924 	mbox->vport = phba->pport;
6925 	mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6926 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6927 
6928 	if (rc == MBX_NOT_FINISHED) {
6929 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6930 				"2552 Unregister FCFI command failed rc x%x "
6931 				"HBA state x%x\n",
6932 				rc, phba->pport->port_state);
6933 		return -EINVAL;
6934 	}
6935 	return 0;
6936 }
6937 
6938 /**
6939  * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6940  * @phba: Pointer to hba context object.
6941  *
6942  * This function unregisters the currently reigstered FCF. This function
6943  * also tries to find another FCF for discovery by rescan the HBA FCF table.
6944  */
6945 void
6946 lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6947 {
6948 	int rc;
6949 
6950 	/* Preparation for unregistering fcf */
6951 	rc = lpfc_unregister_fcf_prep(phba);
6952 	if (rc) {
6953 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6954 				"2748 Failed to prepare for unregistering "
6955 				"HBA's FCF record: rc=%d\n", rc);
6956 		return;
6957 	}
6958 
6959 	/* Now, unregister FCF record and reset HBA FCF state */
6960 	rc = lpfc_sli4_unregister_fcf(phba);
6961 	if (rc)
6962 		return;
6963 	/* Reset HBA FCF states after successful unregister FCF */
6964 	phba->fcf.fcf_flag = 0;
6965 	phba->fcf.current_rec.flag = 0;
6966 
6967 	/*
6968 	 * If driver is not unloading, check if there is any other
6969 	 * FCF record that can be used for discovery.
6970 	 */
6971 	if ((phba->pport->load_flag & FC_UNLOADING) ||
6972 	    (phba->link_state < LPFC_LINK_UP))
6973 		return;
6974 
6975 	/* This is considered as the initial FCF discovery scan */
6976 	spin_lock_irq(&phba->hbalock);
6977 	phba->fcf.fcf_flag |= FCF_INIT_DISC;
6978 	spin_unlock_irq(&phba->hbalock);
6979 
6980 	/* Reset FCF roundrobin bmask for new discovery */
6981 	lpfc_sli4_clear_fcf_rr_bmask(phba);
6982 
6983 	rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
6984 
6985 	if (rc) {
6986 		spin_lock_irq(&phba->hbalock);
6987 		phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6988 		spin_unlock_irq(&phba->hbalock);
6989 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6990 				"2553 lpfc_unregister_unused_fcf failed "
6991 				"to read FCF record HBA state x%x\n",
6992 				phba->pport->port_state);
6993 	}
6994 }
6995 
6996 /**
6997  * lpfc_unregister_fcf - Unregister the currently registered fcf record
6998  * @phba: Pointer to hba context object.
6999  *
7000  * This function just unregisters the currently reigstered FCF. It does not
7001  * try to find another FCF for discovery.
7002  */
7003 void
7004 lpfc_unregister_fcf(struct lpfc_hba *phba)
7005 {
7006 	int rc;
7007 
7008 	/* Preparation for unregistering fcf */
7009 	rc = lpfc_unregister_fcf_prep(phba);
7010 	if (rc) {
7011 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7012 				"2749 Failed to prepare for unregistering "
7013 				"HBA's FCF record: rc=%d\n", rc);
7014 		return;
7015 	}
7016 
7017 	/* Now, unregister FCF record and reset HBA FCF state */
7018 	rc = lpfc_sli4_unregister_fcf(phba);
7019 	if (rc)
7020 		return;
7021 	/* Set proper HBA FCF states after successful unregister FCF */
7022 	spin_lock_irq(&phba->hbalock);
7023 	phba->fcf.fcf_flag &= ~FCF_REGISTERED;
7024 	spin_unlock_irq(&phba->hbalock);
7025 }
7026 
7027 /**
7028  * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
7029  * @phba: Pointer to hba context object.
7030  *
7031  * This function check if there are any connected remote port for the FCF and
7032  * if all the devices are disconnected, this function unregister FCFI.
7033  * This function also tries to use another FCF for discovery.
7034  */
7035 void
7036 lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
7037 {
7038 	/*
7039 	 * If HBA is not running in FIP mode, if HBA does not support
7040 	 * FCoE, if FCF discovery is ongoing, or if FCF has not been
7041 	 * registered, do nothing.
7042 	 */
7043 	spin_lock_irq(&phba->hbalock);
7044 	if (!(phba->hba_flag & HBA_FCOE_MODE) ||
7045 	    !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
7046 	    !(phba->hba_flag & HBA_FIP_SUPPORT) ||
7047 	    (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
7048 	    (phba->pport->port_state == LPFC_FLOGI)) {
7049 		spin_unlock_irq(&phba->hbalock);
7050 		return;
7051 	}
7052 	spin_unlock_irq(&phba->hbalock);
7053 
7054 	if (lpfc_fcf_inuse(phba))
7055 		return;
7056 
7057 	lpfc_unregister_fcf_rescan(phba);
7058 }
7059 
7060 /**
7061  * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
7062  * @phba: Pointer to hba context object.
7063  * @buff: Buffer containing the FCF connection table as in the config
7064  *         region.
7065  * This function create driver data structure for the FCF connection
7066  * record table read from config region 23.
7067  */
7068 static void
7069 lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
7070 	uint8_t *buff)
7071 {
7072 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
7073 	struct lpfc_fcf_conn_hdr *conn_hdr;
7074 	struct lpfc_fcf_conn_rec *conn_rec;
7075 	uint32_t record_count;
7076 	int i;
7077 
7078 	/* Free the current connect table */
7079 	list_for_each_entry_safe(conn_entry, next_conn_entry,
7080 		&phba->fcf_conn_rec_list, list) {
7081 		list_del_init(&conn_entry->list);
7082 		kfree(conn_entry);
7083 	}
7084 
7085 	conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
7086 	record_count = conn_hdr->length * sizeof(uint32_t)/
7087 		sizeof(struct lpfc_fcf_conn_rec);
7088 
7089 	conn_rec = (struct lpfc_fcf_conn_rec *)
7090 		(buff + sizeof(struct lpfc_fcf_conn_hdr));
7091 
7092 	for (i = 0; i < record_count; i++) {
7093 		if (!(conn_rec[i].flags & FCFCNCT_VALID))
7094 			continue;
7095 		conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
7096 			GFP_KERNEL);
7097 		if (!conn_entry) {
7098 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7099 					"2566 Failed to allocate connection"
7100 					" table entry\n");
7101 			return;
7102 		}
7103 
7104 		memcpy(&conn_entry->conn_rec, &conn_rec[i],
7105 			sizeof(struct lpfc_fcf_conn_rec));
7106 		list_add_tail(&conn_entry->list,
7107 			&phba->fcf_conn_rec_list);
7108 	}
7109 
7110 	if (!list_empty(&phba->fcf_conn_rec_list)) {
7111 		i = 0;
7112 		list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
7113 				    list) {
7114 			conn_rec = &conn_entry->conn_rec;
7115 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7116 					"3345 FCF connection list rec[%02d]: "
7117 					"flags:x%04x, vtag:x%04x, "
7118 					"fabric_name:x%02x:%02x:%02x:%02x:"
7119 					"%02x:%02x:%02x:%02x, "
7120 					"switch_name:x%02x:%02x:%02x:%02x:"
7121 					"%02x:%02x:%02x:%02x\n", i++,
7122 					conn_rec->flags, conn_rec->vlan_tag,
7123 					conn_rec->fabric_name[0],
7124 					conn_rec->fabric_name[1],
7125 					conn_rec->fabric_name[2],
7126 					conn_rec->fabric_name[3],
7127 					conn_rec->fabric_name[4],
7128 					conn_rec->fabric_name[5],
7129 					conn_rec->fabric_name[6],
7130 					conn_rec->fabric_name[7],
7131 					conn_rec->switch_name[0],
7132 					conn_rec->switch_name[1],
7133 					conn_rec->switch_name[2],
7134 					conn_rec->switch_name[3],
7135 					conn_rec->switch_name[4],
7136 					conn_rec->switch_name[5],
7137 					conn_rec->switch_name[6],
7138 					conn_rec->switch_name[7]);
7139 		}
7140 	}
7141 }
7142 
7143 /**
7144  * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
7145  * @phba: Pointer to hba context object.
7146  * @buff: Buffer containing the FCoE parameter data structure.
7147  *
7148  *  This function update driver data structure with config
7149  *  parameters read from config region 23.
7150  */
7151 static void
7152 lpfc_read_fcoe_param(struct lpfc_hba *phba,
7153 			uint8_t *buff)
7154 {
7155 	struct lpfc_fip_param_hdr *fcoe_param_hdr;
7156 	struct lpfc_fcoe_params *fcoe_param;
7157 
7158 	fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
7159 		buff;
7160 	fcoe_param = (struct lpfc_fcoe_params *)
7161 		(buff + sizeof(struct lpfc_fip_param_hdr));
7162 
7163 	if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
7164 		(fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
7165 		return;
7166 
7167 	if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
7168 		phba->valid_vlan = 1;
7169 		phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
7170 			0xFFF;
7171 	}
7172 
7173 	phba->fc_map[0] = fcoe_param->fc_map[0];
7174 	phba->fc_map[1] = fcoe_param->fc_map[1];
7175 	phba->fc_map[2] = fcoe_param->fc_map[2];
7176 	return;
7177 }
7178 
7179 /**
7180  * lpfc_get_rec_conf23 - Get a record type in config region data.
7181  * @buff: Buffer containing config region 23 data.
7182  * @size: Size of the data buffer.
7183  * @rec_type: Record type to be searched.
7184  *
7185  * This function searches config region data to find the beginning
7186  * of the record specified by record_type. If record found, this
7187  * function return pointer to the record else return NULL.
7188  */
7189 static uint8_t *
7190 lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
7191 {
7192 	uint32_t offset = 0, rec_length;
7193 
7194 	if ((buff[0] == LPFC_REGION23_LAST_REC) ||
7195 		(size < sizeof(uint32_t)))
7196 		return NULL;
7197 
7198 	rec_length = buff[offset + 1];
7199 
7200 	/*
7201 	 * One TLV record has one word header and number of data words
7202 	 * specified in the rec_length field of the record header.
7203 	 */
7204 	while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
7205 		<= size) {
7206 		if (buff[offset] == rec_type)
7207 			return &buff[offset];
7208 
7209 		if (buff[offset] == LPFC_REGION23_LAST_REC)
7210 			return NULL;
7211 
7212 		offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
7213 		rec_length = buff[offset + 1];
7214 	}
7215 	return NULL;
7216 }
7217 
7218 /**
7219  * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
7220  * @phba: Pointer to lpfc_hba data structure.
7221  * @buff: Buffer containing config region 23 data.
7222  * @size: Size of the data buffer.
7223  *
7224  * This function parses the FCoE config parameters in config region 23 and
7225  * populate driver data structure with the parameters.
7226  */
7227 void
7228 lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
7229 		uint8_t *buff,
7230 		uint32_t size)
7231 {
7232 	uint32_t offset = 0;
7233 	uint8_t *rec_ptr;
7234 
7235 	/*
7236 	 * If data size is less than 2 words signature and version cannot be
7237 	 * verified.
7238 	 */
7239 	if (size < 2*sizeof(uint32_t))
7240 		return;
7241 
7242 	/* Check the region signature first */
7243 	if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
7244 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7245 			"2567 Config region 23 has bad signature\n");
7246 		return;
7247 	}
7248 
7249 	offset += 4;
7250 
7251 	/* Check the data structure version */
7252 	if (buff[offset] != LPFC_REGION23_VERSION) {
7253 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7254 				"2568 Config region 23 has bad version\n");
7255 		return;
7256 	}
7257 	offset += 4;
7258 
7259 	/* Read FCoE param record */
7260 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
7261 			size - offset, FCOE_PARAM_TYPE);
7262 	if (rec_ptr)
7263 		lpfc_read_fcoe_param(phba, rec_ptr);
7264 
7265 	/* Read FCF connection table */
7266 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
7267 		size - offset, FCOE_CONN_TBL_TYPE);
7268 	if (rec_ptr)
7269 		lpfc_read_fcf_conn_tbl(phba, rec_ptr);
7270 
7271 }
7272 
7273 /*
7274  * lpfc_error_lost_link - IO failure from link event or FW reset check.
7275  *
7276  * @vport: Pointer to lpfc_vport data structure.
7277  * @ulp_status: IO completion status.
7278  * @ulp_word4: Reason code for the ulp_status.
7279  *
7280  * This function evaluates the ulp_status and ulp_word4 values
7281  * for specific error values that indicate an internal link fault
7282  * or fw reset event for the completing IO.  Callers require this
7283  * common data to decide next steps on the IO.
7284  *
7285  * Return:
7286  * false - No link or reset error occurred.
7287  * true - A link or reset error occurred.
7288  */
7289 bool
7290 lpfc_error_lost_link(struct lpfc_vport *vport, u32 ulp_status, u32 ulp_word4)
7291 {
7292 	/* Mask off the extra port data to get just the reason code. */
7293 	u32 rsn_code = IOERR_PARAM_MASK & ulp_word4;
7294 
7295 	if (ulp_status == IOSTAT_LOCAL_REJECT &&
7296 	    (rsn_code == IOERR_SLI_ABORTED ||
7297 	     rsn_code == IOERR_LINK_DOWN ||
7298 	     rsn_code == IOERR_SLI_DOWN)) {
7299 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI | LOG_ELS,
7300 				 "0408 Report link error true: <x%x:x%x>\n",
7301 				 ulp_status, ulp_word4);
7302 		return true;
7303 	}
7304 
7305 	return false;
7306 }
7307