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