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