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