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