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