xref: /linux/drivers/scsi/lpfc/lpfc_nportdisc.c (revision 68a052239fc4b351e961f698b824f7654a346091)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2025 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/pci.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 
35 #include "lpfc_hw4.h"
36 #include "lpfc_hw.h"
37 #include "lpfc_sli.h"
38 #include "lpfc_sli4.h"
39 #include "lpfc_nl.h"
40 #include "lpfc_disc.h"
41 #include "lpfc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc_nvme.h"
44 #include "lpfc_logmsg.h"
45 #include "lpfc_crtn.h"
46 #include "lpfc_vport.h"
47 #include "lpfc_debugfs.h"
48 
49 
50 /* Called to clear RSCN discovery flags when driver is unloading. */
51 static bool
52 lpfc_check_unload_and_clr_rscn(unsigned long *fc_flag)
53 {
54 	/* If unloading, then clear the FC_RSCN_DEFERRED flag */
55 	if (test_bit(FC_UNLOADING, fc_flag)) {
56 		clear_bit(FC_RSCN_DEFERRED, fc_flag);
57 		return false;
58 	}
59 	return test_bit(FC_RSCN_DEFERRED, fc_flag);
60 }
61 
62 /* Called to verify a rcv'ed ADISC was intended for us. */
63 static int
64 lpfc_check_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
65 		 struct lpfc_name *nn, struct lpfc_name *pn)
66 {
67 
68 	/* Compare the ADISC rsp WWNN / WWPN matches our internal node
69 	 * table entry for that node.
70 	 */
71 	if (memcmp(nn, &ndlp->nlp_nodename, sizeof (struct lpfc_name)))
72 		return 0;
73 
74 	if (memcmp(pn, &ndlp->nlp_portname, sizeof (struct lpfc_name)))
75 		return 0;
76 
77 	/* we match, return success */
78 	return 1;
79 }
80 
81 int
82 lpfc_check_sparm(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
83 		 struct serv_parm *sp, uint32_t class, int flogi)
84 {
85 	volatile struct serv_parm *hsp = &vport->fc_sparam;
86 	uint16_t hsp_value, ssp_value = 0;
87 
88 	/*
89 	 * The receive data field size and buffer-to-buffer receive data field
90 	 * size entries are 16 bits but are represented as two 8-bit fields in
91 	 * the driver data structure to account for rsvd bits and other control
92 	 * bits.  Reconstruct and compare the fields as a 16-bit values before
93 	 * correcting the byte values.
94 	 */
95 	if (sp->cls1.classValid) {
96 		if (!flogi) {
97 			hsp_value = ((hsp->cls1.rcvDataSizeMsb << 8) |
98 				     hsp->cls1.rcvDataSizeLsb);
99 			ssp_value = ((sp->cls1.rcvDataSizeMsb << 8) |
100 				     sp->cls1.rcvDataSizeLsb);
101 			if (!ssp_value)
102 				goto bad_service_param;
103 			if (ssp_value > hsp_value) {
104 				sp->cls1.rcvDataSizeLsb =
105 					hsp->cls1.rcvDataSizeLsb;
106 				sp->cls1.rcvDataSizeMsb =
107 					hsp->cls1.rcvDataSizeMsb;
108 			}
109 		}
110 	} else if (class == CLASS1)
111 		goto bad_service_param;
112 	if (sp->cls2.classValid) {
113 		if (!flogi) {
114 			hsp_value = ((hsp->cls2.rcvDataSizeMsb << 8) |
115 				     hsp->cls2.rcvDataSizeLsb);
116 			ssp_value = ((sp->cls2.rcvDataSizeMsb << 8) |
117 				     sp->cls2.rcvDataSizeLsb);
118 			if (!ssp_value)
119 				goto bad_service_param;
120 			if (ssp_value > hsp_value) {
121 				sp->cls2.rcvDataSizeLsb =
122 					hsp->cls2.rcvDataSizeLsb;
123 				sp->cls2.rcvDataSizeMsb =
124 					hsp->cls2.rcvDataSizeMsb;
125 			}
126 		}
127 	} else if (class == CLASS2)
128 		goto bad_service_param;
129 	if (sp->cls3.classValid) {
130 		if (!flogi) {
131 			hsp_value = ((hsp->cls3.rcvDataSizeMsb << 8) |
132 				     hsp->cls3.rcvDataSizeLsb);
133 			ssp_value = ((sp->cls3.rcvDataSizeMsb << 8) |
134 				     sp->cls3.rcvDataSizeLsb);
135 			if (!ssp_value)
136 				goto bad_service_param;
137 			if (ssp_value > hsp_value) {
138 				sp->cls3.rcvDataSizeLsb =
139 					hsp->cls3.rcvDataSizeLsb;
140 				sp->cls3.rcvDataSizeMsb =
141 					hsp->cls3.rcvDataSizeMsb;
142 			}
143 		}
144 	} else if (class == CLASS3)
145 		goto bad_service_param;
146 
147 	/*
148 	 * Preserve the upper four bits of the MSB from the PLOGI response.
149 	 * These bits contain the Buffer-to-Buffer State Change Number
150 	 * from the target and need to be passed to the FW.
151 	 */
152 	hsp_value = (hsp->cmn.bbRcvSizeMsb << 8) | hsp->cmn.bbRcvSizeLsb;
153 	ssp_value = (sp->cmn.bbRcvSizeMsb << 8) | sp->cmn.bbRcvSizeLsb;
154 	if (ssp_value > hsp_value) {
155 		sp->cmn.bbRcvSizeLsb = hsp->cmn.bbRcvSizeLsb;
156 		sp->cmn.bbRcvSizeMsb = (sp->cmn.bbRcvSizeMsb & 0xF0) |
157 				       (hsp->cmn.bbRcvSizeMsb & 0x0F);
158 	}
159 
160 	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
161 	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof (struct lpfc_name));
162 	return 1;
163 bad_service_param:
164 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
165 			 "0207 Device %x "
166 			 "(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x) sent "
167 			 "invalid service parameters.  Ignoring device.\n",
168 			 ndlp->nlp_DID,
169 			 sp->nodeName.u.wwn[0], sp->nodeName.u.wwn[1],
170 			 sp->nodeName.u.wwn[2], sp->nodeName.u.wwn[3],
171 			 sp->nodeName.u.wwn[4], sp->nodeName.u.wwn[5],
172 			 sp->nodeName.u.wwn[6], sp->nodeName.u.wwn[7]);
173 	return 0;
174 }
175 
176 static void *
177 lpfc_check_elscmpl_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
178 			struct lpfc_iocbq *rspiocb)
179 {
180 	struct lpfc_dmabuf *pcmd, *prsp;
181 	uint32_t *lp;
182 	void     *ptr = NULL;
183 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
184 
185 	pcmd = cmdiocb->cmd_dmabuf;
186 
187 	/* For lpfc_els_abort, cmd_dmabuf could be zero'ed to delay
188 	 * freeing associated memory till after ABTS completes.
189 	 */
190 	if (pcmd) {
191 		prsp =  list_get_first(&pcmd->list, struct lpfc_dmabuf,
192 				       list);
193 		if (prsp) {
194 			lp = (uint32_t *) prsp->virt;
195 			ptr = (void *)((uint8_t *)lp + sizeof(uint32_t));
196 		}
197 	} else {
198 		/* Force ulp_status error since we are returning NULL ptr */
199 		if (!(ulp_status)) {
200 			if (phba->sli_rev == LPFC_SLI_REV4) {
201 				bf_set(lpfc_wcqe_c_status, &rspiocb->wcqe_cmpl,
202 				       IOSTAT_LOCAL_REJECT);
203 				rspiocb->wcqe_cmpl.parameter = IOERR_SLI_ABORTED;
204 			} else {
205 				rspiocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
206 				rspiocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
207 			}
208 		}
209 		ptr = NULL;
210 	}
211 	return ptr;
212 }
213 
214 
215 
216 /*
217  * Free resources / clean up outstanding I/Os
218  * associated with a LPFC_NODELIST entry. This
219  * routine effectively results in a "software abort".
220  */
221 void
222 lpfc_els_abort(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
223 {
224 	LIST_HEAD(abort_list);
225 	LIST_HEAD(drv_cmpl_list);
226 	struct lpfc_sli_ring *pring;
227 	struct lpfc_iocbq *iocb, *next_iocb;
228 	int retval = 0;
229 
230 	pring = lpfc_phba_elsring(phba);
231 
232 	/* In case of error recovery path, we might have a NULL pring here */
233 	if (unlikely(!pring))
234 		return;
235 
236 	/* Abort outstanding I/O on NPort <nlp_DID> */
237 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_DISCOVERY,
238 			 "2819 Abort outstanding I/O on NPort x%x "
239 			 "Data: x%lx x%x x%x\n",
240 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
241 			 ndlp->nlp_rpi);
242 	/* Clean up all fabric IOs first.*/
243 	lpfc_fabric_abort_nport(ndlp);
244 
245 	/*
246 	 * Lock the ELS ring txcmplq for SLI3/SLI4 and build a local list
247 	 * of all ELS IOs that need an ABTS.  The IOs need to stay on the
248 	 * txcmplq so that the abort operation completes them successfully.
249 	 */
250 	spin_lock_irq(&phba->hbalock);
251 	if (phba->sli_rev == LPFC_SLI_REV4)
252 		spin_lock(&pring->ring_lock);
253 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
254 	/* Add to abort_list on on NDLP match. */
255 		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
256 			list_add_tail(&iocb->dlist, &abort_list);
257 	}
258 	if (phba->sli_rev == LPFC_SLI_REV4)
259 		spin_unlock(&pring->ring_lock);
260 	spin_unlock_irq(&phba->hbalock);
261 
262 	/* Abort the targeted IOs and remove them from the abort list. */
263 	list_for_each_entry_safe(iocb, next_iocb, &abort_list, dlist) {
264 		spin_lock_irq(&phba->hbalock);
265 		list_del_init(&iocb->dlist);
266 		retval = lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
267 		spin_unlock_irq(&phba->hbalock);
268 
269 		if (retval && test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
270 			list_del_init(&iocb->list);
271 			list_add_tail(&iocb->list, &drv_cmpl_list);
272 		}
273 	}
274 
275 	lpfc_sli_cancel_iocbs(phba, &drv_cmpl_list, IOSTAT_LOCAL_REJECT,
276 			      IOERR_SLI_ABORTED);
277 
278 	/* Make sure HBA is alive */
279 	lpfc_issue_hb_tmo(phba);
280 
281 	INIT_LIST_HEAD(&abort_list);
282 
283 	/* Now process the txq */
284 	spin_lock_irq(&phba->hbalock);
285 	if (phba->sli_rev == LPFC_SLI_REV4)
286 		spin_lock(&pring->ring_lock);
287 
288 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
289 		/* Check to see if iocb matches the nport we are looking for */
290 		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp)) {
291 			list_del_init(&iocb->list);
292 			list_add_tail(&iocb->list, &abort_list);
293 		}
294 	}
295 
296 	if (phba->sli_rev == LPFC_SLI_REV4)
297 		spin_unlock(&pring->ring_lock);
298 	spin_unlock_irq(&phba->hbalock);
299 
300 	/* Cancel all the IOCBs from the completions list */
301 	lpfc_sli_cancel_iocbs(phba, &abort_list,
302 			      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
303 
304 	lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
305 }
306 
307 /* lpfc_defer_plogi_acc - Issue PLOGI ACC after reg_login completes
308  * @phba: pointer to lpfc hba data structure.
309  * @login_mbox: pointer to REG_RPI mailbox object
310  *
311  * The ACC for a rcv'ed PLOGI is deferred until AFTER the REG_RPI completes
312  */
313 static void
314 lpfc_defer_plogi_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *login_mbox)
315 {
316 	struct lpfc_iocbq *save_iocb;
317 	struct lpfc_nodelist *ndlp;
318 	MAILBOX_t *mb = &login_mbox->u.mb;
319 
320 	int rc;
321 
322 	ndlp = login_mbox->ctx_ndlp;
323 	save_iocb = login_mbox->ctx_u.save_iocb;
324 
325 	if (mb->mbxStatus == MBX_SUCCESS) {
326 		/* Now that REG_RPI completed successfully,
327 		 * we can now proceed with sending the PLOGI ACC.
328 		 */
329 		if (test_bit(FC_PT2PT, &ndlp->vport->fc_flag)) {
330 			rc = lpfc_els_rsp_acc(login_mbox->vport, ELS_CMD_PLOGI,
331 					      save_iocb, ndlp, login_mbox);
332 		} else {
333 			rc = lpfc_els_rsp_acc(login_mbox->vport, ELS_CMD_PLOGI,
334 					      save_iocb, ndlp, NULL);
335 		}
336 
337 		if (rc) {
338 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
339 					"4576 PLOGI ACC fails pt2pt discovery: "
340 					"DID %x Data: %x\n", ndlp->nlp_DID, rc);
341 		}
342 	}
343 
344 	/* If this is a fabric topology, complete the reg_rpi and prli now.
345 	 * For Pt2Pt, the reg_rpi and PRLI are deferred until after the LS_ACC
346 	 * completes.  This ensures, in Pt2Pt, that the PLOGI LS_ACC is sent
347 	 * before the PRLI.
348 	 */
349 	if (!test_bit(FC_PT2PT, &ndlp->vport->fc_flag)) {
350 		/* Now process the REG_RPI cmpl */
351 		lpfc_mbx_cmpl_reg_login(phba, login_mbox);
352 		clear_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag);
353 	}
354 	kfree(save_iocb);
355 }
356 
357 static int
358 lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
359 	       struct lpfc_iocbq *cmdiocb)
360 {
361 	struct lpfc_hba    *phba = vport->phba;
362 	struct lpfc_dmabuf *pcmd;
363 	uint64_t nlp_portwwn = 0;
364 	uint32_t *lp;
365 	union lpfc_wqe128 *wqe;
366 	IOCB_t *icmd;
367 	struct serv_parm *sp;
368 	uint32_t ed_tov;
369 	LPFC_MBOXQ_t *link_mbox;
370 	LPFC_MBOXQ_t *login_mbox;
371 	struct lpfc_iocbq *save_iocb;
372 	struct ls_rjt stat;
373 	uint32_t vid, flag;
374 	int rc;
375 	u32 remote_did;
376 
377 	memset(&stat, 0, sizeof (struct ls_rjt));
378 	pcmd = cmdiocb->cmd_dmabuf;
379 	lp = (uint32_t *) pcmd->virt;
380 	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
381 	if (wwn_to_u64(sp->portName.u.wwn) == 0) {
382 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
383 				 "0140 PLOGI Reject: invalid pname\n");
384 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
385 		stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_PNAME;
386 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
387 			NULL);
388 		return 0;
389 	}
390 	if (wwn_to_u64(sp->nodeName.u.wwn) == 0) {
391 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
392 				 "0141 PLOGI Reject: invalid nname\n");
393 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
394 		stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_NNAME;
395 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
396 			NULL);
397 		return 0;
398 	}
399 
400 	nlp_portwwn = wwn_to_u64(ndlp->nlp_portname.u.wwn);
401 	if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0) == 0)) {
402 		/* Reject this request because invalid parameters */
403 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
404 		stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
405 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
406 			NULL);
407 		return 0;
408 	}
409 
410 	if (phba->sli_rev == LPFC_SLI_REV4)
411 		wqe = &cmdiocb->wqe;
412 	else
413 		icmd = &cmdiocb->iocb;
414 
415 	/* PLOGI chkparm OK */
416 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
417 			 "0114 PLOGI chkparm OK Data: x%x x%x x%lx "
418 			 "x%x x%x x%lx\n",
419 			 ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
420 			 ndlp->nlp_rpi, vport->port_state,
421 			 vport->fc_flag);
422 
423 	if (vport->cfg_fcp_class == 2 && sp->cls2.classValid)
424 		ndlp->nlp_fcp_info |= CLASS2;
425 	else
426 		ndlp->nlp_fcp_info |= CLASS3;
427 
428 	ndlp->nlp_class_sup = 0;
429 	if (sp->cls1.classValid)
430 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
431 	if (sp->cls2.classValid)
432 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
433 	if (sp->cls3.classValid)
434 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
435 	if (sp->cls4.classValid)
436 		ndlp->nlp_class_sup |= FC_COS_CLASS4;
437 	ndlp->nlp_maxframe =
438 		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
439 	/* if already logged in, do implicit logout */
440 	switch (ndlp->nlp_state) {
441 	case  NLP_STE_NPR_NODE:
442 		if (!test_bit(NLP_NPR_ADISC, &ndlp->nlp_flag))
443 			break;
444 		fallthrough;
445 	case  NLP_STE_REG_LOGIN_ISSUE:
446 	case  NLP_STE_PRLI_ISSUE:
447 	case  NLP_STE_UNMAPPED_NODE:
448 	case  NLP_STE_MAPPED_NODE:
449 		/* For initiators, lpfc_plogi_confirm_nport skips fabric did.
450 		 * For target mode, execute implicit logo.
451 		 * Fabric nodes go into NPR.
452 		 */
453 		if (!(ndlp->nlp_type & NLP_FABRIC) &&
454 		    !(phba->nvmet_support)) {
455 			/* Clear ndlp info, since follow up PRLI may have
456 			 * updated ndlp information
457 			 */
458 			ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
459 			ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
460 			ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
461 			ndlp->nlp_nvme_info &= ~NLP_NVME_NSLER;
462 			clear_bit(NLP_FIRSTBURST, &ndlp->nlp_flag);
463 
464 			lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
465 					 ndlp, NULL);
466 			return 1;
467 		}
468 		if (nlp_portwwn != 0 &&
469 		    nlp_portwwn != wwn_to_u64(sp->portName.u.wwn))
470 			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
471 					 "0143 PLOGI recv'd from DID: x%x "
472 					 "WWPN changed: old %llx new %llx\n",
473 					 ndlp->nlp_DID,
474 					 (unsigned long long)nlp_portwwn,
475 					 (unsigned long long)
476 					 wwn_to_u64(sp->portName.u.wwn));
477 
478 		/* Notify transport of connectivity loss to trigger cleanup. */
479 		if (phba->nvmet_support &&
480 		    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
481 			lpfc_nvmet_invalidate_host(phba, ndlp);
482 
483 		ndlp->nlp_prev_state = ndlp->nlp_state;
484 		/* rport needs to be unregistered first */
485 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
486 		break;
487 	}
488 
489 	ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
490 	ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
491 	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
492 	ndlp->nlp_nvme_info &= ~NLP_NVME_NSLER;
493 	clear_bit(NLP_FIRSTBURST, &ndlp->nlp_flag);
494 
495 	login_mbox = NULL;
496 	link_mbox = NULL;
497 	save_iocb = NULL;
498 
499 	/* Check for Nport to NPort pt2pt protocol */
500 	if (test_bit(FC_PT2PT, &vport->fc_flag) &&
501 	    !test_bit(FC_PT2PT_PLOGI, &vport->fc_flag)) {
502 		/* rcv'ed PLOGI decides what our NPortId will be */
503 		if (phba->sli_rev == LPFC_SLI_REV4) {
504 			vport->fc_myDID = bf_get(els_rsp64_sid,
505 						 &cmdiocb->wqe.xmit_els_rsp);
506 		} else {
507 			vport->fc_myDID = icmd->un.rcvels.parmRo;
508 		}
509 
510 		/* If there is an outstanding FLOGI, abort it now.
511 		 * The remote NPort is not going to ACC our FLOGI
512 		 * if its already issuing a PLOGI for pt2pt mode.
513 		 * This indicates our FLOGI was dropped; however, we
514 		 * must have ACCed the remote NPorts FLOGI to us
515 		 * to make it here.
516 		 */
517 		if (test_bit(HBA_FLOGI_OUTSTANDING, &phba->hba_flag))
518 			lpfc_els_abort_flogi(phba);
519 
520 		ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
521 		if (sp->cmn.edtovResolution) {
522 			/* E_D_TOV ticks are in nanoseconds */
523 			ed_tov = (phba->fc_edtov + 999999) / 1000000;
524 		}
525 
526 		/*
527 		 * For pt-to-pt, use the larger EDTOV
528 		 * RATOV = 2 * EDTOV
529 		 */
530 		if (ed_tov > phba->fc_edtov)
531 			phba->fc_edtov = ed_tov;
532 		phba->fc_ratov = (2 * phba->fc_edtov) / 1000;
533 
534 		memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
535 
536 		/* Issue CONFIG_LINK for SLI3 or REG_VFI for SLI4,
537 		 * to account for updated TOV's / parameters
538 		 */
539 		if (phba->sli_rev == LPFC_SLI_REV4)
540 			lpfc_issue_reg_vfi(vport);
541 		else {
542 			link_mbox = mempool_alloc(phba->mbox_mem_pool,
543 						  GFP_KERNEL);
544 			if (!link_mbox)
545 				goto out;
546 			lpfc_config_link(phba, link_mbox);
547 			link_mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
548 			link_mbox->vport = vport;
549 
550 			/* The default completion handling for CONFIG_LINK
551 			 * does not require the ndlp so no reference is needed.
552 			 */
553 			link_mbox->ctx_ndlp = ndlp;
554 
555 			rc = lpfc_sli_issue_mbox(phba, link_mbox, MBX_NOWAIT);
556 			if (rc == MBX_NOT_FINISHED) {
557 				mempool_free(link_mbox, phba->mbox_mem_pool);
558 				goto out;
559 			}
560 		}
561 
562 		lpfc_can_disctmo(vport);
563 	}
564 
565 	clear_bit(NLP_SUPPRESS_RSP, &ndlp->nlp_flag);
566 	if ((phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) &&
567 	    sp->cmn.valid_vendor_ver_level) {
568 		vid = be32_to_cpu(sp->un.vv.vid);
569 		flag = be32_to_cpu(sp->un.vv.flags);
570 		if ((vid == LPFC_VV_EMLX_ID) && (flag & LPFC_VV_SUPPRESS_RSP))
571 			set_bit(NLP_SUPPRESS_RSP, &ndlp->nlp_flag);
572 	}
573 
574 	login_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
575 	if (!login_mbox)
576 		goto out;
577 
578 	save_iocb = kzalloc(sizeof(*save_iocb), GFP_KERNEL);
579 	if (!save_iocb)
580 		goto out;
581 
582 	/* Save info from cmd IOCB to be used in rsp after all mbox completes */
583 	memcpy((uint8_t *)save_iocb, (uint8_t *)cmdiocb,
584 	       sizeof(struct lpfc_iocbq));
585 
586 	/* Registering an existing RPI behaves differently for SLI3 vs SLI4 */
587 	if (phba->sli_rev == LPFC_SLI_REV4)
588 		lpfc_unreg_rpi(vport, ndlp);
589 
590 	/* Issue REG_LOGIN first, before ACCing the PLOGI, thus we will
591 	 * always be deferring the ACC.
592 	 */
593 	if (phba->sli_rev == LPFC_SLI_REV4)
594 		remote_did = bf_get(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest);
595 	else
596 		remote_did = icmd->un.rcvels.remoteID;
597 	rc = lpfc_reg_rpi(phba, vport->vpi, remote_did,
598 			    (uint8_t *)sp, login_mbox, ndlp->nlp_rpi);
599 	if (rc)
600 		goto out;
601 
602 	login_mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
603 	login_mbox->vport = vport;
604 
605 	/*
606 	 * If there is an outstanding PLOGI issued, abort it before
607 	 * sending ACC rsp for received PLOGI. If pending plogi
608 	 * is not canceled here, the plogi will be rejected by
609 	 * remote port and will be retried. On a configuration with
610 	 * single discovery thread, this will cause a huge delay in
611 	 * discovery. Also this will cause multiple state machines
612 	 * running in parallel for this node.
613 	 * This only applies to a fabric environment.
614 	 */
615 	if ((ndlp->nlp_state == NLP_STE_PLOGI_ISSUE) &&
616 	    test_bit(FC_FABRIC, &vport->fc_flag)) {
617 		/* software abort outstanding PLOGI */
618 		lpfc_els_abort(phba, ndlp);
619 	}
620 
621 	if ((vport->port_type == LPFC_NPIV_PORT &&
622 	     vport->cfg_restrict_login)) {
623 
624 		/* no deferred ACC */
625 		kfree(save_iocb);
626 
627 		/* This is an NPIV SLI4 instance that does not need to register
628 		 * a default RPI.
629 		 */
630 		if (phba->sli_rev == LPFC_SLI_REV4) {
631 			lpfc_mbox_rsrc_cleanup(phba, login_mbox,
632 					       MBOX_THD_UNLOCKED);
633 			login_mbox = NULL;
634 		} else {
635 			/* In order to preserve RPIs, we want to cleanup
636 			 * the default RPI the firmware created to rcv
637 			 * this ELS request. The only way to do this is
638 			 * to register, then unregister the RPI.
639 			 */
640 			set_bit(NLP_RM_DFLT_RPI, &ndlp->nlp_flag);
641 			set_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag);
642 			set_bit(NLP_RCV_PLOGI, &ndlp->nlp_flag);
643 		}
644 
645 		stat.un.b.lsRjtRsnCode = LSRJT_INVALID_CMD;
646 		stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
647 		rc = lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
648 					 ndlp, login_mbox);
649 		if (rc && login_mbox)
650 			lpfc_mbox_rsrc_cleanup(phba, login_mbox,
651 					       MBOX_THD_UNLOCKED);
652 		return 1;
653 	}
654 
655 	/* So the order here should be:
656 	 * SLI3 pt2pt
657 	 *   Issue CONFIG_LINK mbox
658 	 *   CONFIG_LINK cmpl
659 	 * SLI4 pt2pt
660 	 *   Issue REG_VFI mbox
661 	 *   REG_VFI cmpl
662 	 * SLI4
663 	 *   Issue UNREG RPI mbx
664 	 *   UNREG RPI cmpl
665 	 * Issue REG_RPI mbox
666 	 * REG RPI cmpl
667 	 * Issue PLOGI ACC
668 	 * PLOGI ACC cmpl
669 	 */
670 	login_mbox->mbox_cmpl = lpfc_defer_plogi_acc;
671 	login_mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
672 	if (!login_mbox->ctx_ndlp)
673 		goto out;
674 
675 	login_mbox->ctx_u.save_iocb = save_iocb; /* For PLOGI ACC */
676 
677 	set_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag);
678 	set_bit(NLP_RCV_PLOGI, &ndlp->nlp_flag);
679 
680 	/* Start the ball rolling by issuing REG_LOGIN here */
681 	rc = lpfc_sli_issue_mbox(phba, login_mbox, MBX_NOWAIT);
682 	if (rc == MBX_NOT_FINISHED) {
683 		lpfc_nlp_put(ndlp);
684 		goto out;
685 	}
686 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
687 
688 	return 1;
689 out:
690 	kfree(save_iocb);
691 	if (login_mbox)
692 		mempool_free(login_mbox, phba->mbox_mem_pool);
693 
694 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
695 	stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
696 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
697 	return 0;
698 }
699 
700 /**
701  * lpfc_mbx_cmpl_resume_rpi - Resume RPI completion routine
702  * @phba: pointer to lpfc hba data structure.
703  * @mboxq: pointer to mailbox object
704  *
705  * This routine is invoked to issue a completion to a rcv'ed
706  * ADISC or PDISC after the paused RPI has been resumed.
707  **/
708 static void
709 lpfc_mbx_cmpl_resume_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
710 {
711 	struct lpfc_vport *vport;
712 	struct lpfc_iocbq *elsiocb;
713 	struct lpfc_nodelist *ndlp;
714 	uint32_t cmd;
715 
716 	elsiocb = mboxq->ctx_u.save_iocb;
717 	ndlp = mboxq->ctx_ndlp;
718 	vport = mboxq->vport;
719 	cmd = elsiocb->drvrTimeout;
720 
721 	if (cmd == ELS_CMD_ADISC) {
722 		lpfc_els_rsp_adisc_acc(vport, elsiocb, ndlp);
723 	} else {
724 		lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, elsiocb,
725 			ndlp, NULL);
726 	}
727 
728 	/* This nlp_put pairs with lpfc_sli4_resume_rpi */
729 	lpfc_nlp_put(ndlp);
730 
731 	kfree(elsiocb);
732 	mempool_free(mboxq, phba->mbox_mem_pool);
733 }
734 
735 static int
736 lpfc_rcv_padisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
737 		struct lpfc_iocbq *cmdiocb)
738 {
739 	struct lpfc_hba *phba = vport->phba;
740 	struct lpfc_iocbq  *elsiocb;
741 	struct lpfc_dmabuf *pcmd;
742 	struct serv_parm   *sp;
743 	struct lpfc_name   *pnn, *ppn;
744 	struct ls_rjt stat;
745 	ADISC *ap;
746 	uint32_t *lp;
747 	uint32_t cmd;
748 	int rc;
749 
750 	pcmd = cmdiocb->cmd_dmabuf;
751 	lp = (uint32_t *) pcmd->virt;
752 
753 	cmd = *lp++;
754 	if (cmd == ELS_CMD_ADISC) {
755 		ap = (ADISC *) lp;
756 		pnn = (struct lpfc_name *) & ap->nodeName;
757 		ppn = (struct lpfc_name *) & ap->portName;
758 	} else {
759 		sp = (struct serv_parm *) lp;
760 		pnn = (struct lpfc_name *) & sp->nodeName;
761 		ppn = (struct lpfc_name *) & sp->portName;
762 	}
763 
764 	if (get_job_ulpstatus(phba, cmdiocb) == 0 &&
765 	    lpfc_check_adisc(vport, ndlp, pnn, ppn)) {
766 
767 		/*
768 		 * As soon as  we send ACC, the remote NPort can
769 		 * start sending us data. Thus, for SLI4 we must
770 		 * resume the RPI before the ACC goes out.
771 		 */
772 		if (vport->phba->sli_rev == LPFC_SLI_REV4) {
773 			/* Don't resume an unregistered RPI - unnecessary
774 			 * mailbox. Just send the ACC when the RPI is not
775 			 * registered.
776 			 */
777 			if (test_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag)) {
778 				elsiocb = kmalloc(sizeof(*elsiocb), GFP_KERNEL);
779 				if (elsiocb) {
780 					/* Save info from cmd IOCB used in
781 					 * rsp
782 					 */
783 					memcpy(elsiocb, cmdiocb,
784 					       sizeof(*elsiocb));
785 
786 					elsiocb->drvrTimeout = cmd;
787 
788 					rc = lpfc_sli4_resume_rpi(ndlp,
789 								  lpfc_mbx_cmpl_resume_rpi,
790 								  elsiocb);
791 					if (rc)
792 						kfree(elsiocb);
793 
794 					goto out;
795 				}
796 			}
797 		}
798 
799 		if (cmd == ELS_CMD_ADISC) {
800 			lpfc_els_rsp_adisc_acc(vport, cmdiocb, ndlp);
801 		} else {
802 			lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
803 				ndlp, NULL);
804 		}
805 out:
806 		/* If we are authenticated, move to the proper state.
807 		 * It is possible an ADISC arrived and the remote nport
808 		 * is already in MAPPED or UNMAPPED state.  Catch this
809 		 * condition and don't set the nlp_state again because
810 		 * it causes an unnecessary transport unregister/register.
811 		 *
812 		 * Nodes marked for ADISC will move MAPPED or UNMAPPED state
813 		 * after issuing ADISC
814 		 */
815 		if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET)) {
816 			if ((ndlp->nlp_state != NLP_STE_MAPPED_NODE) &&
817 			    !test_bit(NLP_NPR_ADISC, &ndlp->nlp_flag))
818 				lpfc_nlp_set_state(vport, ndlp,
819 						   NLP_STE_MAPPED_NODE);
820 		}
821 
822 		return 1;
823 	}
824 	/* Reject this request because invalid parameters */
825 	stat.un.b.lsRjtRsvd0 = 0;
826 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
827 	stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
828 	stat.un.b.vendorUnique = 0;
829 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
830 
831 	/* 1 sec timeout */
832 	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
833 
834 	set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
835 	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
836 	ndlp->nlp_prev_state = ndlp->nlp_state;
837 	return 0;
838 }
839 
840 static int
841 lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
842 	      struct lpfc_iocbq *cmdiocb, uint32_t els_cmd)
843 {
844 	struct lpfc_hba    *phba = vport->phba;
845 	struct lpfc_vport **vports;
846 	int i, active_vlink_present = 0 ;
847 
848 	/* Put ndlp in NPR state with 1 sec timeout for plogi, ACC logo */
849 	/* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
850 	 * PLOGIs during LOGO storms from a device.
851 	 */
852 	set_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
853 	if (els_cmd == ELS_CMD_PRLO)
854 		lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
855 	else
856 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
857 
858 	/* This clause allows the initiator to ACC the LOGO back to the
859 	 * Fabric Domain Controller.  It does deliberately skip all other
860 	 * steps because some fabrics send RDP requests after logging out
861 	 * from the initiator.
862 	 */
863 	if (ndlp->nlp_type & NLP_FABRIC &&
864 	    ((ndlp->nlp_DID & WELL_KNOWN_DID_MASK) != WELL_KNOWN_DID_MASK))
865 		return 0;
866 
867 	/* Notify transport of connectivity loss to trigger cleanup. */
868 	if (phba->nvmet_support &&
869 	    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
870 		lpfc_nvmet_invalidate_host(phba, ndlp);
871 
872 	if (ndlp->nlp_DID == Fabric_DID) {
873 		if (vport->port_state <= LPFC_FDISC ||
874 		    test_bit(FC_PT2PT, &vport->fc_flag))
875 			goto out;
876 		lpfc_linkdown_port(vport);
877 		set_bit(FC_VPORT_LOGO_RCVD, &vport->fc_flag);
878 		vports = lpfc_create_vport_work_array(phba);
879 		if (vports) {
880 			for (i = 0; i <= phba->max_vports && vports[i] != NULL;
881 					i++) {
882 				if (!test_bit(FC_VPORT_LOGO_RCVD,
883 					      &vports[i]->fc_flag) &&
884 				    vports[i]->port_state > LPFC_FDISC) {
885 					active_vlink_present = 1;
886 					break;
887 				}
888 			}
889 			lpfc_destroy_vport_work_array(phba, vports);
890 		}
891 
892 		/*
893 		 * Don't re-instantiate if vport is marked for deletion.
894 		 * If we are here first then vport_delete is going to wait
895 		 * for discovery to complete.
896 		 */
897 		if (!test_bit(FC_UNLOADING, &vport->load_flag) &&
898 		    active_vlink_present) {
899 			/*
900 			 * If there are other active VLinks present,
901 			 * re-instantiate the Vlink using FDISC.
902 			 */
903 			mod_timer(&ndlp->nlp_delayfunc,
904 				  jiffies + msecs_to_jiffies(1000));
905 			set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
906 			ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
907 			vport->port_state = LPFC_FDISC;
908 		} else {
909 			clear_bit(FC_LOGO_RCVD_DID_CHNG, &phba->pport->fc_flag);
910 			lpfc_retry_pport_discovery(phba);
911 		}
912 	} else {
913 		lpfc_printf_vlog(vport, KERN_INFO,
914 				 LOG_NODE | LOG_ELS | LOG_DISCOVERY,
915 				 "3203 LOGO recover nport x%06x state x%x "
916 				 "ntype x%x fc_flag x%lx\n",
917 				 ndlp->nlp_DID, ndlp->nlp_state,
918 				 ndlp->nlp_type, vport->fc_flag);
919 
920 		/* Special cases for rports that recover post LOGO. */
921 		if ((!(ndlp->nlp_type == NLP_FABRIC) &&
922 		     (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) ||
923 		      test_bit(FC_PT2PT, &vport->fc_flag))) ||
924 		    (ndlp->nlp_state >= NLP_STE_ADISC_ISSUE ||
925 		     ndlp->nlp_state <= NLP_STE_PRLI_ISSUE)) {
926 			mod_timer(&ndlp->nlp_delayfunc,
927 				  jiffies + secs_to_jiffies(1));
928 			set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
929 			ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
930 			lpfc_printf_vlog(vport, KERN_INFO,
931 					 LOG_NODE | LOG_ELS | LOG_DISCOVERY,
932 					 "3204 Start nlpdelay on DID x%06x "
933 					 "nflag x%lx lastels x%x ref cnt %u",
934 					 ndlp->nlp_DID, ndlp->nlp_flag,
935 					 ndlp->nlp_last_elscmd,
936 					 kref_read(&ndlp->kref));
937 		}
938 	}
939 out:
940 	/* Unregister from backend, could have been skipped due to ADISC */
941 	lpfc_nlp_unreg_node(vport, ndlp);
942 
943 	ndlp->nlp_prev_state = ndlp->nlp_state;
944 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
945 
946 	clear_bit(NLP_NPR_ADISC, &ndlp->nlp_flag);
947 	/* The driver has to wait until the ACC completes before it continues
948 	 * processing the LOGO.  The action will resume in
949 	 * lpfc_cmpl_els_logo_acc routine. Since part of processing includes an
950 	 * unreg_login, the driver waits so the ACC does not get aborted.
951 	 */
952 	return 0;
953 }
954 
955 static uint32_t
956 lpfc_rcv_prli_support_check(struct lpfc_vport *vport,
957 			    struct lpfc_nodelist *ndlp,
958 			    struct lpfc_iocbq *cmdiocb)
959 {
960 	struct ls_rjt stat;
961 	uint32_t *payload;
962 	uint32_t cmd;
963 	PRLI *npr;
964 
965 	payload = cmdiocb->cmd_dmabuf->virt;
966 	cmd = *payload;
967 	npr = (PRLI *)((uint8_t *)payload + sizeof(uint32_t));
968 
969 	if (vport->phba->nvmet_support) {
970 		/* Must be a NVME PRLI */
971 		if (cmd == ELS_CMD_PRLI)
972 			goto out;
973 	} else {
974 		/* Initiator mode. */
975 		if (!vport->nvmei_support && (cmd == ELS_CMD_NVMEPRLI))
976 			goto out;
977 
978 		/* NPIV ports will RJT initiator only functions */
979 		if (vport->port_type == LPFC_NPIV_PORT &&
980 		    npr->initiatorFunc && !npr->targetFunc)
981 			goto out;
982 	}
983 	return 1;
984 out:
985 	lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
986 			 "6115 Rcv PRLI (%x) check failed: ndlp rpi %d "
987 			 "state x%x flags x%lx port_type: x%x "
988 			 "npr->initfcn: x%x npr->tgtfcn: x%x\n",
989 			 cmd, ndlp->nlp_rpi, ndlp->nlp_state,
990 			 ndlp->nlp_flag, vport->port_type,
991 			 npr->initiatorFunc, npr->targetFunc);
992 	memset(&stat, 0, sizeof(struct ls_rjt));
993 	stat.un.b.lsRjtRsnCode = LSRJT_CMD_UNSUPPORTED;
994 	stat.un.b.lsRjtRsnCodeExp = LSEXP_REQ_UNSUPPORTED;
995 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
996 			    ndlp, NULL);
997 	return 0;
998 }
999 
1000 static void
1001 lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1002 	      struct lpfc_iocbq *cmdiocb)
1003 {
1004 	struct lpfc_hba  *phba = vport->phba;
1005 	struct lpfc_dmabuf *pcmd;
1006 	uint32_t *lp;
1007 	PRLI *npr;
1008 	struct fc_rport *rport = ndlp->rport;
1009 	u32 roles;
1010 
1011 	pcmd = cmdiocb->cmd_dmabuf;
1012 	lp = (uint32_t *)pcmd->virt;
1013 	npr = (PRLI *)((uint8_t *)lp + sizeof(uint32_t));
1014 
1015 	if ((npr->prliType == PRLI_FCP_TYPE) ||
1016 	    (npr->prliType == PRLI_NVME_TYPE)) {
1017 		if (npr->initiatorFunc) {
1018 			if (npr->prliType == PRLI_FCP_TYPE)
1019 				ndlp->nlp_type |= NLP_FCP_INITIATOR;
1020 			if (npr->prliType == PRLI_NVME_TYPE)
1021 				ndlp->nlp_type |= NLP_NVME_INITIATOR;
1022 		}
1023 		if (npr->targetFunc) {
1024 			if (npr->prliType == PRLI_FCP_TYPE)
1025 				ndlp->nlp_type |= NLP_FCP_TARGET;
1026 			if (npr->prliType == PRLI_NVME_TYPE)
1027 				ndlp->nlp_type |= NLP_NVME_TARGET;
1028 			if (npr->writeXferRdyDis)
1029 				set_bit(NLP_FIRSTBURST, &ndlp->nlp_flag);
1030 		}
1031 		if (npr->Retry && ndlp->nlp_type &
1032 					(NLP_FCP_INITIATOR | NLP_FCP_TARGET))
1033 			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
1034 
1035 		if (npr->Retry && phba->nsler &&
1036 		    ndlp->nlp_type & (NLP_NVME_INITIATOR | NLP_NVME_TARGET))
1037 			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;
1038 
1039 
1040 		/* If this driver is in nvme target mode, set the ndlp's fc4
1041 		 * type to NVME provided the PRLI response claims NVME FC4
1042 		 * type.  Target mode does not issue gft_id so doesn't get
1043 		 * the fc4 type set until now.
1044 		 */
1045 		if (phba->nvmet_support && (npr->prliType == PRLI_NVME_TYPE)) {
1046 			ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1047 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1048 		}
1049 
1050 		/* Fabric Controllers send FCP PRLI as an initiator but should
1051 		 * not get recognized as FCP type and registered with transport.
1052 		 */
1053 		if (npr->prliType == PRLI_FCP_TYPE &&
1054 		    !(ndlp->nlp_type & NLP_FABRIC))
1055 			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1056 	}
1057 	if (rport) {
1058 		/* We need to update the rport role values */
1059 		roles = FC_RPORT_ROLE_UNKNOWN;
1060 		if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1061 			roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1062 		if (ndlp->nlp_type & NLP_FCP_TARGET)
1063 			roles |= FC_RPORT_ROLE_FCP_TARGET;
1064 
1065 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1066 			"rport rolechg:   role:x%x did:x%x flg:x%lx",
1067 			roles, ndlp->nlp_DID, ndlp->nlp_flag);
1068 
1069 		if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1070 			fc_remote_port_rolechg(rport, roles);
1071 	}
1072 }
1073 
1074 static uint32_t
1075 lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1076 {
1077 	if (!test_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag)) {
1078 		clear_bit(NLP_NPR_ADISC, &ndlp->nlp_flag);
1079 		return 0;
1080 	}
1081 
1082 	if (!test_bit(FC_PT2PT, &vport->fc_flag)) {
1083 		/* Check config parameter use-adisc or FCP-2 */
1084 		if (vport->cfg_use_adisc &&
1085 		    (test_bit(FC_RSCN_MODE, &vport->fc_flag) ||
1086 		    ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) &&
1087 		     (ndlp->nlp_type & NLP_FCP_TARGET)))) {
1088 			set_bit(NLP_NPR_ADISC, &ndlp->nlp_flag);
1089 			return 1;
1090 		}
1091 	}
1092 
1093 	clear_bit(NLP_NPR_ADISC, &ndlp->nlp_flag);
1094 	lpfc_unreg_rpi(vport, ndlp);
1095 	return 0;
1096 }
1097 
1098 /**
1099  * lpfc_release_rpi - Release a RPI by issuing unreg_login mailbox cmd.
1100  * @phba : Pointer to lpfc_hba structure.
1101  * @vport: Pointer to lpfc_vport structure.
1102  * @ndlp: Pointer to lpfc_nodelist structure.
1103  * @rpi  : rpi to be release.
1104  *
1105  * This function will send a unreg_login mailbox command to the firmware
1106  * to release a rpi.
1107  **/
1108 static void
1109 lpfc_release_rpi(struct lpfc_hba *phba, struct lpfc_vport *vport,
1110 		 struct lpfc_nodelist *ndlp, uint16_t rpi)
1111 {
1112 	LPFC_MBOXQ_t *pmb;
1113 	int rc;
1114 
1115 	/* If there is already an UNREG in progress for this ndlp,
1116 	 * no need to queue up another one.
1117 	 */
1118 	if (test_bit(NLP_UNREG_INP, &ndlp->nlp_flag)) {
1119 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1120 				 "1435 release_rpi SKIP UNREG x%x on "
1121 				 "NPort x%x deferred x%x  flg x%lx "
1122 				 "Data: x%px\n",
1123 				 ndlp->nlp_rpi, ndlp->nlp_DID,
1124 				 ndlp->nlp_defer_did,
1125 				 ndlp->nlp_flag, ndlp);
1126 		return;
1127 	}
1128 
1129 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
1130 			GFP_KERNEL);
1131 	if (!pmb)
1132 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1133 				 "2796 mailbox memory allocation failed \n");
1134 	else {
1135 		lpfc_unreg_login(phba, vport->vpi, rpi, pmb);
1136 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1137 		pmb->vport = vport;
1138 		pmb->ctx_ndlp = lpfc_nlp_get(ndlp);
1139 		if (!pmb->ctx_ndlp) {
1140 			mempool_free(pmb, phba->mbox_mem_pool);
1141 			return;
1142 		}
1143 
1144 		if (((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
1145 		    (!test_bit(FC_OFFLINE_MODE, &vport->fc_flag)))
1146 			set_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
1147 
1148 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1149 				 "1437 release_rpi UNREG x%x "
1150 				 "on NPort x%x flg x%lx\n",
1151 				 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag);
1152 
1153 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1154 		if (rc == MBX_NOT_FINISHED) {
1155 			lpfc_nlp_put(ndlp);
1156 			mempool_free(pmb, phba->mbox_mem_pool);
1157 		}
1158 	}
1159 }
1160 
1161 static uint32_t
1162 lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1163 		  void *arg, uint32_t evt)
1164 {
1165 	struct lpfc_hba *phba;
1166 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1167 	uint16_t rpi;
1168 
1169 	phba = vport->phba;
1170 	/* Release the RPI if reglogin completing */
1171 	if (!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&
1172 	    evt == NLP_EVT_CMPL_REG_LOGIN && !pmb->u.mb.mbxStatus) {
1173 		rpi = pmb->u.mb.un.varWords[0];
1174 		lpfc_release_rpi(phba, vport, ndlp, rpi);
1175 	}
1176 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1177 			 "0271 Illegal State Transition: node x%x "
1178 			 "event x%x, state x%x Data: x%x x%lx\n",
1179 			 ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
1180 			 ndlp->nlp_flag);
1181 	return ndlp->nlp_state;
1182 }
1183 
1184 static uint32_t
1185 lpfc_cmpl_plogi_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1186 		  void *arg, uint32_t evt)
1187 {
1188 	/* This transition is only legal if we previously
1189 	 * rcv'ed a PLOGI. Since we don't want 2 discovery threads
1190 	 * working on the same NPortID, do nothing for this thread
1191 	 * to stop it.
1192 	 */
1193 	if (!test_bit(NLP_RCV_PLOGI, &ndlp->nlp_flag))
1194 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1195 				 "0272 Illegal State Transition: node x%x "
1196 				 "event x%x, state x%x Data: x%x x%lx\n",
1197 				  ndlp->nlp_DID, evt, ndlp->nlp_state,
1198 				  ndlp->nlp_rpi, ndlp->nlp_flag);
1199 	return ndlp->nlp_state;
1200 }
1201 
1202 /* Start of Discovery State Machine routines */
1203 
1204 static uint32_t
1205 lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1206 			   void *arg, uint32_t evt)
1207 {
1208 	struct lpfc_iocbq *cmdiocb;
1209 
1210 	cmdiocb = (struct lpfc_iocbq *) arg;
1211 
1212 	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1213 		return ndlp->nlp_state;
1214 	}
1215 	return NLP_STE_FREED_NODE;
1216 }
1217 
1218 static uint32_t
1219 lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1220 			 void *arg, uint32_t evt)
1221 {
1222 	lpfc_issue_els_logo(vport, ndlp, 0);
1223 	return ndlp->nlp_state;
1224 }
1225 
1226 static uint32_t
1227 lpfc_rcv_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1228 			  void *arg, uint32_t evt)
1229 {
1230 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1231 
1232 	set_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
1233 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1234 
1235 	return ndlp->nlp_state;
1236 }
1237 
1238 static uint32_t
1239 lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1240 			   void *arg, uint32_t evt)
1241 {
1242 	return NLP_STE_FREED_NODE;
1243 }
1244 
1245 static uint32_t
1246 lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1247 			   void *arg, uint32_t evt)
1248 {
1249 	return NLP_STE_FREED_NODE;
1250 }
1251 
1252 static uint32_t
1253 lpfc_device_recov_unused_node(struct lpfc_vport *vport,
1254 			struct lpfc_nodelist *ndlp,
1255 			   void *arg, uint32_t evt)
1256 {
1257 	return ndlp->nlp_state;
1258 }
1259 
1260 static uint32_t
1261 lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1262 			   void *arg, uint32_t evt)
1263 {
1264 	struct lpfc_hba   *phba = vport->phba;
1265 	struct lpfc_iocbq *cmdiocb = arg;
1266 	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
1267 	uint32_t *lp = (uint32_t *) pcmd->virt;
1268 	struct serv_parm *sp = (struct serv_parm *) (lp + 1);
1269 	struct ls_rjt stat;
1270 	int port_cmp;
1271 
1272 	memset(&stat, 0, sizeof (struct ls_rjt));
1273 
1274 	/* For a PLOGI, we only accept if our portname is less
1275 	 * than the remote portname.
1276 	 */
1277 	phba->fc_stat.elsLogiCol++;
1278 	port_cmp = memcmp(&vport->fc_portname, &sp->portName,
1279 			  sizeof(struct lpfc_name));
1280 
1281 	if (port_cmp >= 0) {
1282 		/* Reject this request because the remote node will accept
1283 		   ours */
1284 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1285 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
1286 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
1287 			NULL);
1288 	} else {
1289 		if (lpfc_rcv_plogi(vport, ndlp, cmdiocb) &&
1290 		    test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag) &&
1291 		    vport->num_disc_nodes) {
1292 			clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
1293 			/* Check if there are more PLOGIs to be sent */
1294 			lpfc_more_plogi(vport);
1295 			if (vport->num_disc_nodes == 0) {
1296 				clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
1297 				lpfc_can_disctmo(vport);
1298 				lpfc_end_rscn(vport);
1299 			}
1300 		}
1301 	} /* If our portname was less */
1302 
1303 	return ndlp->nlp_state;
1304 }
1305 
1306 static uint32_t
1307 lpfc_rcv_prli_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1308 			  void *arg, uint32_t evt)
1309 {
1310 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1311 	struct ls_rjt     stat;
1312 
1313 	memset(&stat, 0, sizeof (struct ls_rjt));
1314 	stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
1315 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1316 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1317 	return ndlp->nlp_state;
1318 }
1319 
1320 static uint32_t
1321 lpfc_rcv_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1322 			  void *arg, uint32_t evt)
1323 {
1324 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1325 
1326 	/* Retrieve RPI from LOGO IOCB. RPI is used for CMD_ABORT_XRI_CN */
1327 	if (vport->phba->sli_rev == LPFC_SLI_REV3)
1328 		ndlp->nlp_rpi = cmdiocb->iocb.ulpIoTag;
1329 				/* software abort outstanding PLOGI */
1330 	lpfc_els_abort(vport->phba, ndlp);
1331 
1332 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1333 	return ndlp->nlp_state;
1334 }
1335 
1336 static uint32_t
1337 lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1338 			 void *arg, uint32_t evt)
1339 {
1340 	struct lpfc_hba   *phba = vport->phba;
1341 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1342 
1343 	/* software abort outstanding PLOGI */
1344 	lpfc_els_abort(phba, ndlp);
1345 
1346 	if (evt == NLP_EVT_RCV_LOGO) {
1347 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1348 	} else {
1349 		lpfc_issue_els_logo(vport, ndlp, 0);
1350 	}
1351 
1352 	/* Put ndlp in npr state set plogi timer for 1 sec */
1353 	mod_timer(&ndlp->nlp_delayfunc, jiffies + secs_to_jiffies(1));
1354 	set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
1355 	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1356 	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
1357 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1358 
1359 	return ndlp->nlp_state;
1360 }
1361 
1362 static uint32_t
1363 lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
1364 			    struct lpfc_nodelist *ndlp,
1365 			    void *arg,
1366 			    uint32_t evt)
1367 {
1368 	struct lpfc_hba    *phba = vport->phba;
1369 	struct lpfc_iocbq  *cmdiocb, *rspiocb;
1370 	struct lpfc_dmabuf *pcmd, *prsp;
1371 	uint32_t *lp;
1372 	uint32_t vid, flag;
1373 	struct serv_parm *sp;
1374 	uint32_t ed_tov;
1375 	LPFC_MBOXQ_t *mbox;
1376 	int rc;
1377 	u32 ulp_status;
1378 	u32 did;
1379 
1380 	cmdiocb = (struct lpfc_iocbq *) arg;
1381 	rspiocb = cmdiocb->rsp_iocb;
1382 
1383 	ulp_status = get_job_ulpstatus(phba, rspiocb);
1384 
1385 	if (test_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag)) {
1386 		/* Recovery from PLOGI collision logic */
1387 		return ndlp->nlp_state;
1388 	}
1389 
1390 	if (ulp_status)
1391 		goto out;
1392 
1393 	pcmd = cmdiocb->cmd_dmabuf;
1394 
1395 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1396 	if (!prsp)
1397 		goto out;
1398 
1399 	lp = (uint32_t *) prsp->virt;
1400 	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
1401 
1402 	/* Some switches have FDMI servers returning 0 for WWN */
1403 	if ((ndlp->nlp_DID != FDMI_DID) &&
1404 		(wwn_to_u64(sp->portName.u.wwn) == 0 ||
1405 		wwn_to_u64(sp->nodeName.u.wwn) == 0)) {
1406 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1407 				 "0142 PLOGI RSP: Invalid WWN.\n");
1408 		goto out;
1409 	}
1410 	if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0))
1411 		goto out;
1412 	/* PLOGI chkparm OK */
1413 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1414 			 "0121 PLOGI chkparm OK Data: x%x x%x x%lx x%x\n",
1415 			 ndlp->nlp_DID, ndlp->nlp_state,
1416 			 ndlp->nlp_flag, ndlp->nlp_rpi);
1417 	if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
1418 		ndlp->nlp_fcp_info |= CLASS2;
1419 	else
1420 		ndlp->nlp_fcp_info |= CLASS3;
1421 
1422 	ndlp->nlp_class_sup = 0;
1423 	if (sp->cls1.classValid)
1424 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
1425 	if (sp->cls2.classValid)
1426 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
1427 	if (sp->cls3.classValid)
1428 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
1429 	if (sp->cls4.classValid)
1430 		ndlp->nlp_class_sup |= FC_COS_CLASS4;
1431 	ndlp->nlp_maxframe =
1432 		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
1433 
1434 	if (test_bit(FC_PT2PT, &vport->fc_flag) &&
1435 	    test_bit(FC_PT2PT_PLOGI, &vport->fc_flag)) {
1436 		ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
1437 		if (sp->cmn.edtovResolution) {
1438 			/* E_D_TOV ticks are in nanoseconds */
1439 			ed_tov = (phba->fc_edtov + 999999) / 1000000;
1440 		}
1441 
1442 		clear_bit(NLP_SUPPRESS_RSP, &ndlp->nlp_flag);
1443 		if ((phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) &&
1444 		    sp->cmn.valid_vendor_ver_level) {
1445 			vid = be32_to_cpu(sp->un.vv.vid);
1446 			flag = be32_to_cpu(sp->un.vv.flags);
1447 			if ((vid == LPFC_VV_EMLX_ID) &&
1448 			    (flag & LPFC_VV_SUPPRESS_RSP))
1449 				set_bit(NLP_SUPPRESS_RSP, &ndlp->nlp_flag);
1450 		}
1451 
1452 		/*
1453 		 * Use the larger EDTOV
1454 		 * RATOV = 2 * EDTOV for pt-to-pt
1455 		 */
1456 		if (ed_tov > phba->fc_edtov)
1457 			phba->fc_edtov = ed_tov;
1458 		phba->fc_ratov = (2 * phba->fc_edtov) / 1000;
1459 
1460 		memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
1461 
1462 		/* Issue config_link / reg_vfi to account for updated TOV's */
1463 		if (phba->sli_rev == LPFC_SLI_REV4) {
1464 			lpfc_issue_reg_vfi(vport);
1465 		} else {
1466 			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1467 			if (!mbox) {
1468 				lpfc_printf_vlog(vport, KERN_ERR,
1469 						 LOG_TRACE_EVENT,
1470 						 "0133 PLOGI: no memory "
1471 						 "for config_link "
1472 						 "Data: x%x x%x x%lx x%x\n",
1473 						 ndlp->nlp_DID, ndlp->nlp_state,
1474 						 ndlp->nlp_flag, ndlp->nlp_rpi);
1475 				goto out;
1476 			}
1477 
1478 			lpfc_config_link(phba, mbox);
1479 
1480 			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1481 			mbox->vport = vport;
1482 			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1483 			if (rc == MBX_NOT_FINISHED) {
1484 				mempool_free(mbox, phba->mbox_mem_pool);
1485 				goto out;
1486 			}
1487 		}
1488 	}
1489 
1490 	lpfc_unreg_rpi(vport, ndlp);
1491 
1492 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1493 	if (!mbox) {
1494 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1495 				 "0018 PLOGI: no memory for reg_login "
1496 				 "Data: x%x x%x x%lx x%x\n",
1497 				 ndlp->nlp_DID, ndlp->nlp_state,
1498 				 ndlp->nlp_flag, ndlp->nlp_rpi);
1499 		goto out;
1500 	}
1501 
1502 	did = get_job_els_rsp64_did(phba, cmdiocb);
1503 
1504 	if (lpfc_reg_rpi(phba, vport->vpi, did,
1505 			 (uint8_t *) sp, mbox, ndlp->nlp_rpi) == 0) {
1506 		switch (ndlp->nlp_DID) {
1507 		case NameServer_DID:
1508 			mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
1509 			/* Fabric Controller Node needs these parameters. */
1510 			memcpy(&ndlp->fc_sparam, sp, sizeof(struct serv_parm));
1511 			break;
1512 		case FDMI_DID:
1513 			mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
1514 			break;
1515 		default:
1516 			set_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
1517 			mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
1518 		}
1519 
1520 		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
1521 		if (!mbox->ctx_ndlp)
1522 			goto out;
1523 
1524 		mbox->vport = vport;
1525 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
1526 		    != MBX_NOT_FINISHED) {
1527 			lpfc_nlp_set_state(vport, ndlp,
1528 					   NLP_STE_REG_LOGIN_ISSUE);
1529 			return ndlp->nlp_state;
1530 		}
1531 		clear_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
1532 		/* decrement node reference count to the failed mbox
1533 		 * command
1534 		 */
1535 		lpfc_nlp_put(ndlp);
1536 		lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
1537 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1538 				 "0134 PLOGI: cannot issue reg_login "
1539 				 "Data: x%x x%x x%lx x%x\n",
1540 				 ndlp->nlp_DID, ndlp->nlp_state,
1541 				 ndlp->nlp_flag, ndlp->nlp_rpi);
1542 	} else {
1543 		mempool_free(mbox, phba->mbox_mem_pool);
1544 
1545 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1546 				 "0135 PLOGI: cannot format reg_login "
1547 				 "Data: x%x x%x x%lx x%x\n",
1548 				 ndlp->nlp_DID, ndlp->nlp_state,
1549 				 ndlp->nlp_flag, ndlp->nlp_rpi);
1550 	}
1551 
1552 
1553 out:
1554 	if (ndlp->nlp_DID == NameServer_DID) {
1555 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1556 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1557 				 "0261 Cannot Register NameServer login\n");
1558 	}
1559 
1560 	/*
1561 	** In case the node reference counter does not go to zero, ensure that
1562 	** the stale state for the node is not processed.
1563 	*/
1564 
1565 	ndlp->nlp_prev_state = ndlp->nlp_state;
1566 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1567 	return NLP_STE_FREED_NODE;
1568 }
1569 
1570 static uint32_t
1571 lpfc_cmpl_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1572 			   void *arg, uint32_t evt)
1573 {
1574 	return ndlp->nlp_state;
1575 }
1576 
1577 static uint32_t
1578 lpfc_cmpl_reglogin_plogi_issue(struct lpfc_vport *vport,
1579 	struct lpfc_nodelist *ndlp, void *arg, uint32_t evt)
1580 {
1581 	struct lpfc_hba *phba;
1582 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1583 	MAILBOX_t *mb = &pmb->u.mb;
1584 	uint16_t rpi;
1585 
1586 	phba = vport->phba;
1587 	/* Release the RPI */
1588 	if (!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&
1589 	    !mb->mbxStatus) {
1590 		rpi = pmb->u.mb.un.varWords[0];
1591 		lpfc_release_rpi(phba, vport, ndlp, rpi);
1592 	}
1593 	return ndlp->nlp_state;
1594 }
1595 
1596 static uint32_t
1597 lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1598 			   void *arg, uint32_t evt)
1599 {
1600 	if (test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag)) {
1601 		set_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
1602 		return ndlp->nlp_state;
1603 	}
1604 	/* software abort outstanding PLOGI */
1605 	lpfc_els_abort(vport->phba, ndlp);
1606 
1607 	lpfc_drop_node(vport, ndlp);
1608 	return NLP_STE_FREED_NODE;
1609 }
1610 
1611 static uint32_t
1612 lpfc_device_recov_plogi_issue(struct lpfc_vport *vport,
1613 			      struct lpfc_nodelist *ndlp,
1614 			      void *arg,
1615 			      uint32_t evt)
1616 {
1617 	struct lpfc_hba  *phba = vport->phba;
1618 
1619 	/* Don't do anything that disrupts the RSCN unless lpfc is unloading. */
1620 	if (lpfc_check_unload_and_clr_rscn(&vport->fc_flag))
1621 		return ndlp->nlp_state;
1622 
1623 	/* software abort outstanding PLOGI */
1624 	lpfc_els_abort(phba, ndlp);
1625 
1626 	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
1627 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1628 	clear_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
1629 	clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
1630 
1631 	return ndlp->nlp_state;
1632 }
1633 
1634 static uint32_t
1635 lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1636 			   void *arg, uint32_t evt)
1637 {
1638 	struct lpfc_hba   *phba = vport->phba;
1639 	struct lpfc_iocbq *cmdiocb;
1640 
1641 	/* software abort outstanding ADISC */
1642 	lpfc_els_abort(phba, ndlp);
1643 
1644 	cmdiocb = (struct lpfc_iocbq *) arg;
1645 
1646 	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1647 		if (test_and_clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag)) {
1648 			if (vport->num_disc_nodes)
1649 				lpfc_more_adisc(vport);
1650 		}
1651 		return ndlp->nlp_state;
1652 	}
1653 	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1654 	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
1655 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1656 
1657 	return ndlp->nlp_state;
1658 }
1659 
1660 static uint32_t
1661 lpfc_rcv_prli_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1662 			  void *arg, uint32_t evt)
1663 {
1664 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1665 
1666 	if (lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
1667 		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1668 	return ndlp->nlp_state;
1669 }
1670 
1671 static uint32_t
1672 lpfc_rcv_logo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1673 			  void *arg, uint32_t evt)
1674 {
1675 	struct lpfc_hba *phba = vport->phba;
1676 	struct lpfc_iocbq *cmdiocb;
1677 
1678 	cmdiocb = (struct lpfc_iocbq *) arg;
1679 
1680 	/* software abort outstanding ADISC */
1681 	lpfc_els_abort(phba, ndlp);
1682 
1683 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1684 	return ndlp->nlp_state;
1685 }
1686 
1687 static uint32_t
1688 lpfc_rcv_padisc_adisc_issue(struct lpfc_vport *vport,
1689 			    struct lpfc_nodelist *ndlp,
1690 			    void *arg, uint32_t evt)
1691 {
1692 	struct lpfc_iocbq *cmdiocb;
1693 
1694 	cmdiocb = (struct lpfc_iocbq *) arg;
1695 
1696 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1697 	return ndlp->nlp_state;
1698 }
1699 
1700 static uint32_t
1701 lpfc_rcv_prlo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1702 			  void *arg, uint32_t evt)
1703 {
1704 	struct lpfc_iocbq *cmdiocb;
1705 
1706 	cmdiocb = (struct lpfc_iocbq *) arg;
1707 
1708 	/* Treat like rcv logo */
1709 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1710 	return ndlp->nlp_state;
1711 }
1712 
1713 static uint32_t
1714 lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
1715 			    struct lpfc_nodelist *ndlp,
1716 			    void *arg, uint32_t evt)
1717 {
1718 	struct lpfc_hba   *phba = vport->phba;
1719 	struct lpfc_iocbq *cmdiocb, *rspiocb;
1720 	ADISC *ap;
1721 	int rc;
1722 	u32 ulp_status;
1723 
1724 	cmdiocb = (struct lpfc_iocbq *) arg;
1725 	rspiocb = cmdiocb->rsp_iocb;
1726 
1727 	ulp_status = get_job_ulpstatus(phba, rspiocb);
1728 
1729 	ap = (ADISC *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1730 
1731 	if ((ulp_status) ||
1732 	    (!lpfc_check_adisc(vport, ndlp, &ap->nodeName, &ap->portName))) {
1733 		/* 1 sec timeout */
1734 		mod_timer(&ndlp->nlp_delayfunc,
1735 			  jiffies + msecs_to_jiffies(1000));
1736 		set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
1737 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1738 
1739 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1740 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1741 		lpfc_unreg_rpi(vport, ndlp);
1742 		return ndlp->nlp_state;
1743 	}
1744 
1745 	if (phba->sli_rev == LPFC_SLI_REV4) {
1746 		rc = lpfc_sli4_resume_rpi(ndlp, NULL, NULL);
1747 		if (rc) {
1748 			/* Stay in state and retry. */
1749 			ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1750 			return ndlp->nlp_state;
1751 		}
1752 	}
1753 
1754 	if (ndlp->nlp_type & NLP_FCP_TARGET)
1755 		ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1756 
1757 	if (ndlp->nlp_type & NLP_NVME_TARGET)
1758 		ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1759 
1760 	if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET)) {
1761 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1762 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1763 	} else {
1764 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1765 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1766 	}
1767 
1768 	return ndlp->nlp_state;
1769 }
1770 
1771 static uint32_t
1772 lpfc_device_rm_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1773 			   void *arg, uint32_t evt)
1774 {
1775 	if (test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag)) {
1776 		set_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
1777 		return ndlp->nlp_state;
1778 	}
1779 	/* software abort outstanding ADISC */
1780 	lpfc_els_abort(vport->phba, ndlp);
1781 
1782 	lpfc_drop_node(vport, ndlp);
1783 	return NLP_STE_FREED_NODE;
1784 }
1785 
1786 static uint32_t
1787 lpfc_device_recov_adisc_issue(struct lpfc_vport *vport,
1788 			      struct lpfc_nodelist *ndlp,
1789 			      void *arg,
1790 			      uint32_t evt)
1791 {
1792 	struct lpfc_hba  *phba = vport->phba;
1793 
1794 	/* Don't do anything that disrupts the RSCN unless lpfc is unloading. */
1795 	if (lpfc_check_unload_and_clr_rscn(&vport->fc_flag))
1796 		return ndlp->nlp_state;
1797 
1798 	/* software abort outstanding ADISC */
1799 	lpfc_els_abort(phba, ndlp);
1800 
1801 	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1802 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1803 	clear_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
1804 	clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
1805 	lpfc_disc_set_adisc(vport, ndlp);
1806 	return ndlp->nlp_state;
1807 }
1808 
1809 static uint32_t
1810 lpfc_rcv_plogi_reglogin_issue(struct lpfc_vport *vport,
1811 			      struct lpfc_nodelist *ndlp,
1812 			      void *arg,
1813 			      uint32_t evt)
1814 {
1815 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1816 
1817 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1818 	return ndlp->nlp_state;
1819 }
1820 
1821 static uint32_t
1822 lpfc_rcv_prli_reglogin_issue(struct lpfc_vport *vport,
1823 			     struct lpfc_nodelist *ndlp,
1824 			     void *arg,
1825 			     uint32_t evt)
1826 {
1827 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1828 	struct ls_rjt     stat;
1829 
1830 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb)) {
1831 		return ndlp->nlp_state;
1832 	}
1833 	if (vport->phba->nvmet_support) {
1834 		/* NVME Target mode.  Handle and respond to the PRLI and
1835 		 * transition to UNMAPPED provided the RPI has completed
1836 		 * registration.
1837 		 */
1838 		if (test_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag)) {
1839 			lpfc_rcv_prli(vport, ndlp, cmdiocb);
1840 			lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1841 		} else {
1842 			/* RPI registration has not completed. Reject the PRLI
1843 			 * to prevent an illegal state transition when the
1844 			 * rpi registration does complete.
1845 			 */
1846 			memset(&stat, 0, sizeof(struct ls_rjt));
1847 			stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
1848 			stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1849 			lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
1850 					    ndlp, NULL);
1851 			return ndlp->nlp_state;
1852 		}
1853 	} else {
1854 		/* Initiator mode. */
1855 		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1856 	}
1857 	return ndlp->nlp_state;
1858 }
1859 
1860 static uint32_t
1861 lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
1862 			     struct lpfc_nodelist *ndlp,
1863 			     void *arg,
1864 			     uint32_t evt)
1865 {
1866 	struct lpfc_hba   *phba = vport->phba;
1867 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1868 	LPFC_MBOXQ_t	  *mb;
1869 	LPFC_MBOXQ_t	  *nextmb;
1870 
1871 	cmdiocb = (struct lpfc_iocbq *) arg;
1872 
1873 	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1874 	if ((mb = phba->sli.mbox_active)) {
1875 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1876 		   (ndlp == mb->ctx_ndlp)) {
1877 			clear_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
1878 			lpfc_nlp_put(ndlp);
1879 			mb->ctx_ndlp = NULL;
1880 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1881 		}
1882 	}
1883 
1884 	spin_lock_irq(&phba->hbalock);
1885 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1886 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1887 		   (ndlp == mb->ctx_ndlp)) {
1888 			clear_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
1889 			lpfc_nlp_put(ndlp);
1890 			list_del(&mb->list);
1891 			phba->sli.mboxq_cnt--;
1892 			lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_LOCKED);
1893 		}
1894 	}
1895 	spin_unlock_irq(&phba->hbalock);
1896 
1897 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1898 	return ndlp->nlp_state;
1899 }
1900 
1901 static uint32_t
1902 lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport *vport,
1903 			       struct lpfc_nodelist *ndlp,
1904 			       void *arg,
1905 			       uint32_t evt)
1906 {
1907 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1908 
1909 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1910 	return ndlp->nlp_state;
1911 }
1912 
1913 static uint32_t
1914 lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
1915 			     struct lpfc_nodelist *ndlp,
1916 			     void *arg,
1917 			     uint32_t evt)
1918 {
1919 	struct lpfc_iocbq *cmdiocb;
1920 
1921 	cmdiocb = (struct lpfc_iocbq *) arg;
1922 	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1923 	return ndlp->nlp_state;
1924 }
1925 
1926 static uint32_t
1927 lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
1928 				  struct lpfc_nodelist *ndlp,
1929 				  void *arg,
1930 				  uint32_t evt)
1931 {
1932 	struct lpfc_hba *phba = vport->phba;
1933 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1934 	MAILBOX_t *mb = &pmb->u.mb;
1935 	uint32_t did  = mb->un.varWords[1];
1936 
1937 	if (mb->mbxStatus) {
1938 		/* RegLogin failed */
1939 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1940 				 "0246 RegLogin failed Data: x%x x%x x%x x%x "
1941 				 "x%x\n",
1942 				 did, mb->mbxStatus, vport->port_state,
1943 				 mb->un.varRegLogin.vpi,
1944 				 mb->un.varRegLogin.rpi);
1945 		/*
1946 		 * If RegLogin failed due to lack of HBA resources do not
1947 		 * retry discovery.
1948 		 */
1949 		if (mb->mbxStatus == MBXERR_RPI_FULL) {
1950 			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1951 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1952 			return ndlp->nlp_state;
1953 		}
1954 
1955 		/* Put ndlp in npr state set plogi timer for 1 sec */
1956 		mod_timer(&ndlp->nlp_delayfunc,
1957 			  jiffies + secs_to_jiffies(1));
1958 		set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
1959 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1960 
1961 		lpfc_issue_els_logo(vport, ndlp, 0);
1962 		return ndlp->nlp_state;
1963 	}
1964 
1965 	/* SLI4 ports have preallocated logical rpis. */
1966 	if (phba->sli_rev < LPFC_SLI_REV4)
1967 		ndlp->nlp_rpi = mb->un.varWords[0];
1968 
1969 	set_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag);
1970 
1971 	/* Only if we are not a fabric nport do we issue PRLI */
1972 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1973 			 "3066 RegLogin Complete on x%x x%x x%x\n",
1974 			 did, ndlp->nlp_type, ndlp->nlp_fc4_type);
1975 	if (!(ndlp->nlp_type & NLP_FABRIC) &&
1976 	    (phba->nvmet_support == 0)) {
1977 		/* The driver supports FCP and NVME concurrently.  If the
1978 		 * ndlp's nlp_fc4_type is still zero, the driver doesn't
1979 		 * know what PRLI to send yet.  Figure that out now and
1980 		 * call PRLI depending on the outcome.
1981 		 */
1982 		if (test_bit(FC_PT2PT, &vport->fc_flag)) {
1983 			/* If we are pt2pt, there is no Fabric to determine
1984 			 * the FC4 type of the remote nport. So if NVME
1985 			 * is configured try it.
1986 			 */
1987 			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1988 			if ((!test_bit(FC_PT2PT_NO_NVME, &vport->fc_flag)) &&
1989 			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
1990 			    vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
1991 				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1992 				/* We need to update the localport also */
1993 				lpfc_nvme_update_localport(vport);
1994 			}
1995 
1996 		} else if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
1997 			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1998 
1999 		} else if (ndlp->nlp_fc4_type == 0) {
2000 			/* If we are only configured for FCP, the driver
2001 			 * should just issue PRLI for FCP. Otherwise issue
2002 			 * GFT_ID to determine if remote port supports NVME.
2003 			 */
2004 			if (vport->cfg_enable_fc4_type != LPFC_ENABLE_FCP) {
2005 				lpfc_ns_cmd(vport, SLI_CTNS_GFT_ID, 0,
2006 					    ndlp->nlp_DID);
2007 				return ndlp->nlp_state;
2008 			}
2009 			ndlp->nlp_fc4_type = NLP_FC4_FCP;
2010 		}
2011 
2012 		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
2013 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2014 		if (lpfc_issue_els_prli(vport, ndlp, 0)) {
2015 			lpfc_issue_els_logo(vport, ndlp, 0);
2016 			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
2017 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2018 		}
2019 	} else {
2020 		if (test_bit(FC_PT2PT, &vport->fc_flag) && phba->nvmet_support)
2021 			phba->targetport->port_id = vport->fc_myDID;
2022 
2023 		/* Only Fabric ports should transition. NVME target
2024 		 * must complete PRLI.
2025 		 */
2026 		if (ndlp->nlp_type & NLP_FABRIC) {
2027 			ndlp->nlp_fc4_type &= ~NLP_FC4_FCP;
2028 			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
2029 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2030 		}
2031 	}
2032 	return ndlp->nlp_state;
2033 }
2034 
2035 static uint32_t
2036 lpfc_device_rm_reglogin_issue(struct lpfc_vport *vport,
2037 			      struct lpfc_nodelist *ndlp,
2038 			      void *arg,
2039 			      uint32_t evt)
2040 {
2041 	if (test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag)) {
2042 		set_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
2043 		return ndlp->nlp_state;
2044 	}
2045 	lpfc_drop_node(vport, ndlp);
2046 	return NLP_STE_FREED_NODE;
2047 }
2048 
2049 static uint32_t
2050 lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
2051 				 struct lpfc_nodelist *ndlp,
2052 				 void *arg,
2053 				 uint32_t evt)
2054 {
2055 	/* Don't do anything that disrupts the RSCN unless lpfc is unloading. */
2056 	if (lpfc_check_unload_and_clr_rscn(&vport->fc_flag))
2057 		return ndlp->nlp_state;
2058 
2059 	ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
2060 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2061 
2062 	/* If we are a target we won't immediately transition into PRLI,
2063 	 * so if REG_LOGIN already completed we don't need to ignore it.
2064 	 */
2065 	if (!test_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag) ||
2066 	    !vport->phba->nvmet_support)
2067 		set_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag);
2068 
2069 	clear_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
2070 	clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2071 	lpfc_disc_set_adisc(vport, ndlp);
2072 	return ndlp->nlp_state;
2073 }
2074 
2075 static uint32_t
2076 lpfc_rcv_plogi_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2077 			  void *arg, uint32_t evt)
2078 {
2079 	struct lpfc_iocbq *cmdiocb;
2080 
2081 	cmdiocb = (struct lpfc_iocbq *) arg;
2082 
2083 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2084 	return ndlp->nlp_state;
2085 }
2086 
2087 static uint32_t
2088 lpfc_rcv_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2089 			 void *arg, uint32_t evt)
2090 {
2091 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2092 
2093 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
2094 		return ndlp->nlp_state;
2095 	lpfc_rcv_prli(vport, ndlp, cmdiocb);
2096 	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2097 	return ndlp->nlp_state;
2098 }
2099 
2100 static uint32_t
2101 lpfc_rcv_logo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2102 			 void *arg, uint32_t evt)
2103 {
2104 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2105 
2106 	/* Software abort outstanding PRLI before sending acc */
2107 	lpfc_els_abort(vport->phba, ndlp);
2108 
2109 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2110 	return ndlp->nlp_state;
2111 }
2112 
2113 static uint32_t
2114 lpfc_rcv_padisc_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2115 			   void *arg, uint32_t evt)
2116 {
2117 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2118 
2119 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2120 	return ndlp->nlp_state;
2121 }
2122 
2123 /* This routine is envoked when we rcv a PRLO request from a nport
2124  * we are logged into.  We should send back a PRLO rsp setting the
2125  * appropriate bits.
2126  * NEXT STATE = PRLI_ISSUE
2127  */
2128 static uint32_t
2129 lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2130 			 void *arg, uint32_t evt)
2131 {
2132 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2133 
2134 	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2135 	return ndlp->nlp_state;
2136 }
2137 
2138 static uint32_t
2139 lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2140 			  void *arg, uint32_t evt)
2141 {
2142 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2143 	struct lpfc_hba   *phba = vport->phba;
2144 	PRLI *npr;
2145 	struct lpfc_nvme_prli *nvpr;
2146 	void *temp_ptr;
2147 	u32 ulp_status;
2148 	bool acc_imode_sps = false;
2149 
2150 	cmdiocb = (struct lpfc_iocbq *) arg;
2151 	rspiocb = cmdiocb->rsp_iocb;
2152 
2153 	ulp_status = get_job_ulpstatus(phba, rspiocb);
2154 
2155 	/* A solicited PRLI is either FCP or NVME.  The PRLI cmd/rsp
2156 	 * format is different so NULL the two PRLI types so that the
2157 	 * driver correctly gets the correct context.
2158 	 */
2159 	npr = NULL;
2160 	nvpr = NULL;
2161 	temp_ptr = lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
2162 	if (cmdiocb->cmd_flag & LPFC_PRLI_FCP_REQ)
2163 		npr = (PRLI *) temp_ptr;
2164 	else if (cmdiocb->cmd_flag & LPFC_PRLI_NVME_REQ)
2165 		nvpr = (struct lpfc_nvme_prli *) temp_ptr;
2166 
2167 	if (ulp_status) {
2168 		if ((vport->port_type == LPFC_NPIV_PORT) &&
2169 		    vport->cfg_restrict_login) {
2170 			goto out;
2171 		}
2172 
2173 		/* Adjust the nlp_type accordingly if the PRLI failed */
2174 		if (npr)
2175 			ndlp->nlp_fc4_type &= ~NLP_FC4_FCP;
2176 		if (nvpr)
2177 			ndlp->nlp_fc4_type &= ~NLP_FC4_NVME;
2178 
2179 		/* We can't set the DSM state till BOTH PRLIs complete */
2180 		goto out_err;
2181 	}
2182 
2183 	if (npr && npr->prliType == PRLI_FCP_TYPE) {
2184 		lpfc_printf_vlog(vport, KERN_INFO,
2185 				 LOG_ELS | LOG_NODE | LOG_DISCOVERY,
2186 				 "6028 FCP NPR PRLI Cmpl Init %d Target %d "
2187 				 "EIP %d AccCode x%x\n",
2188 				 npr->initiatorFunc, npr->targetFunc,
2189 				 npr->estabImagePair, npr->acceptRspCode);
2190 
2191 		if (npr->acceptRspCode == PRLI_INV_SRV_PARM) {
2192 			/* Strict initiators don't establish an image pair. */
2193 			if (npr->initiatorFunc && !npr->targetFunc &&
2194 			    !npr->estabImagePair)
2195 				acc_imode_sps = true;
2196 		}
2197 
2198 		if (npr->acceptRspCode == PRLI_REQ_EXECUTED || acc_imode_sps) {
2199 			if (npr->initiatorFunc)
2200 				ndlp->nlp_type |= NLP_FCP_INITIATOR;
2201 			if (npr->targetFunc) {
2202 				ndlp->nlp_type |= NLP_FCP_TARGET;
2203 				if (npr->writeXferRdyDis)
2204 					set_bit(NLP_FIRSTBURST,
2205 						&ndlp->nlp_flag);
2206 			}
2207 			if (npr->Retry)
2208 				ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
2209 		}
2210 	} else if (nvpr &&
2211 		   (bf_get_be32(prli_acc_rsp_code, nvpr) ==
2212 		    PRLI_REQ_EXECUTED) &&
2213 		   (bf_get_be32(prli_type_code, nvpr) ==
2214 		    PRLI_NVME_TYPE)) {
2215 
2216 		/* Complete setting up the remote ndlp personality. */
2217 		if (bf_get_be32(prli_init, nvpr))
2218 			ndlp->nlp_type |= NLP_NVME_INITIATOR;
2219 
2220 		if (phba->nsler && bf_get_be32(prli_nsler, nvpr) &&
2221 		    bf_get_be32(prli_conf, nvpr))
2222 
2223 			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;
2224 		else
2225 			ndlp->nlp_nvme_info &= ~NLP_NVME_NSLER;
2226 
2227 		/* Target driver cannot solicit NVME FB. */
2228 		if (bf_get_be32(prli_tgt, nvpr)) {
2229 			/* Complete the nvme target roles.  The transport
2230 			 * needs to know if the rport is capable of
2231 			 * discovery in addition to its role.
2232 			 */
2233 			ndlp->nlp_type |= NLP_NVME_TARGET;
2234 			if (bf_get_be32(prli_disc, nvpr))
2235 				ndlp->nlp_type |= NLP_NVME_DISCOVERY;
2236 
2237 			/*
2238 			 * If prli_fba is set, the Target supports FirstBurst.
2239 			 * If prli_fb_sz is 0, the FirstBurst size is unlimited,
2240 			 * otherwise it defines the actual size supported by
2241 			 * the NVME Target.
2242 			 */
2243 			if ((bf_get_be32(prli_fba, nvpr) == 1) &&
2244 			    (phba->cfg_nvme_enable_fb) &&
2245 			    (!phba->nvmet_support)) {
2246 				/* Both sides support FB. The target's first
2247 				 * burst size is a 512 byte encoded value.
2248 				 */
2249 				set_bit(NLP_FIRSTBURST, &ndlp->nlp_flag);
2250 				ndlp->nvme_fb_size = bf_get_be32(prli_fb_sz,
2251 								 nvpr);
2252 
2253 				/* Expressed in units of 512 bytes */
2254 				if (ndlp->nvme_fb_size)
2255 					ndlp->nvme_fb_size <<=
2256 						LPFC_NVME_FB_SHIFT;
2257 				else
2258 					ndlp->nvme_fb_size = LPFC_NVME_MAX_FB;
2259 			}
2260 		}
2261 
2262 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2263 				 "6029 NVME PRLI Cmpl w1 x%08x "
2264 				 "w4 x%08x w5 x%08x flag x%lx, "
2265 				 "fcp_info x%x nlp_type x%x\n",
2266 				 be32_to_cpu(nvpr->word1),
2267 				 be32_to_cpu(nvpr->word4),
2268 				 be32_to_cpu(nvpr->word5),
2269 				 ndlp->nlp_flag, ndlp->nlp_fcp_info,
2270 				 ndlp->nlp_type);
2271 	}
2272 	if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
2273 	    (vport->port_type == LPFC_NPIV_PORT) &&
2274 	     vport->cfg_restrict_login) {
2275 out:
2276 		lpfc_printf_vlog(vport, KERN_INFO,
2277 				 LOG_ELS | LOG_DISCOVERY | LOG_NODE,
2278 				 "6228 Sending LOGO, determined nlp_type "
2279 				 "0x%x nlp_flag x%lx refcnt %u\n",
2280 				 ndlp->nlp_type, ndlp->nlp_flag,
2281 				 kref_read(&ndlp->kref));
2282 		lpfc_issue_els_logo(vport, ndlp, 0);
2283 		return ndlp->nlp_state;
2284 	}
2285 
2286 out_err:
2287 	/* The ndlp state cannot move to MAPPED or UNMAPPED before all PRLIs
2288 	 * are complete.
2289 	 */
2290 	if (ndlp->fc4_prli_sent == 0) {
2291 		ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
2292 		if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET))
2293 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
2294 		else if (ndlp->nlp_type &
2295 			 (NLP_FCP_INITIATOR | NLP_NVME_INITIATOR))
2296 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2297 	} else
2298 		lpfc_printf_vlog(vport,
2299 				 KERN_INFO, LOG_ELS,
2300 				 "3067 PRLI's still outstanding "
2301 				 "on x%06x - count %d, Pend Node Mode "
2302 				 "transition...\n",
2303 				 ndlp->nlp_DID, ndlp->fc4_prli_sent);
2304 
2305 	return ndlp->nlp_state;
2306 }
2307 
2308 /*! lpfc_device_rm_prli_issue
2309  *
2310  * \pre
2311  * \post
2312  * \param   phba
2313  * \param   ndlp
2314  * \param   arg
2315  * \param   evt
2316  * \return  uint32_t
2317  *
2318  * \b Description:
2319  *    This routine is envoked when we a request to remove a nport we are in the
2320  *    process of PRLIing. We should software abort outstanding prli, unreg
2321  *    login, send a logout. We will change node state to UNUSED_NODE, put it
2322  *    on plogi list so it can be freed when LOGO completes.
2323  *
2324  */
2325 
2326 static uint32_t
2327 lpfc_device_rm_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2328 			  void *arg, uint32_t evt)
2329 {
2330 	if (test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag)) {
2331 		set_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
2332 		return ndlp->nlp_state;
2333 	}
2334 	/* software abort outstanding PLOGI */
2335 	lpfc_els_abort(vport->phba, ndlp);
2336 
2337 	lpfc_drop_node(vport, ndlp);
2338 	return NLP_STE_FREED_NODE;
2339 }
2340 
2341 
2342 /*! lpfc_device_recov_prli_issue
2343  *
2344  * \pre
2345  * \post
2346  * \param   phba
2347  * \param   ndlp
2348  * \param   arg
2349  * \param   evt
2350  * \return  uint32_t
2351  *
2352  * \b Description:
2353  *    The routine is envoked when the state of a device is unknown, like
2354  *    during a link down. We should remove the nodelist entry from the
2355  *    unmapped list, issue a UNREG_LOGIN, do a software abort of the
2356  *    outstanding PRLI command, then free the node entry.
2357  */
2358 static uint32_t
2359 lpfc_device_recov_prli_issue(struct lpfc_vport *vport,
2360 			     struct lpfc_nodelist *ndlp,
2361 			     void *arg,
2362 			     uint32_t evt)
2363 {
2364 	struct lpfc_hba  *phba = vport->phba;
2365 
2366 	/* Don't do anything that disrupts the RSCN unless lpfc is unloading. */
2367 	if (lpfc_check_unload_and_clr_rscn(&vport->fc_flag))
2368 		return ndlp->nlp_state;
2369 
2370 	/* software abort outstanding PRLI */
2371 	lpfc_els_abort(phba, ndlp);
2372 
2373 	ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
2374 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2375 	clear_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
2376 	clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2377 	lpfc_disc_set_adisc(vport, ndlp);
2378 	return ndlp->nlp_state;
2379 }
2380 
2381 static uint32_t
2382 lpfc_rcv_plogi_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2383 			  void *arg, uint32_t evt)
2384 {
2385 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2386 	struct ls_rjt     stat;
2387 
2388 	memset(&stat, 0, sizeof(struct ls_rjt));
2389 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2390 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2391 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2392 	return ndlp->nlp_state;
2393 }
2394 
2395 static uint32_t
2396 lpfc_rcv_prli_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2397 			 void *arg, uint32_t evt)
2398 {
2399 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2400 	struct ls_rjt     stat;
2401 
2402 	memset(&stat, 0, sizeof(struct ls_rjt));
2403 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2404 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2405 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2406 	return ndlp->nlp_state;
2407 }
2408 
2409 static uint32_t
2410 lpfc_rcv_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2411 			 void *arg, uint32_t evt)
2412 {
2413 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2414 
2415 	set_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
2416 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2417 	return ndlp->nlp_state;
2418 }
2419 
2420 static uint32_t
2421 lpfc_rcv_padisc_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2422 			   void *arg, uint32_t evt)
2423 {
2424 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2425 	struct ls_rjt     stat;
2426 
2427 	memset(&stat, 0, sizeof(struct ls_rjt));
2428 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2429 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2430 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2431 	return ndlp->nlp_state;
2432 }
2433 
2434 static uint32_t
2435 lpfc_rcv_prlo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2436 			 void *arg, uint32_t evt)
2437 {
2438 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2439 	struct ls_rjt     stat;
2440 
2441 	memset(&stat, 0, sizeof(struct ls_rjt));
2442 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2443 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2444 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2445 	return ndlp->nlp_state;
2446 }
2447 
2448 static uint32_t
2449 lpfc_cmpl_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2450 			  void *arg, uint32_t evt)
2451 {
2452 	ndlp->nlp_prev_state = NLP_STE_LOGO_ISSUE;
2453 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2454 	clear_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
2455 	clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2456 	lpfc_disc_set_adisc(vport, ndlp);
2457 	return ndlp->nlp_state;
2458 }
2459 
2460 static uint32_t
2461 lpfc_device_rm_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2462 			  void *arg, uint32_t evt)
2463 {
2464 	/*
2465 	 * DevLoss has timed out and is calling for Device Remove.
2466 	 * In this case, abort the LOGO and cleanup the ndlp
2467 	 */
2468 
2469 	lpfc_unreg_rpi(vport, ndlp);
2470 	/* software abort outstanding PLOGI */
2471 	lpfc_els_abort(vport->phba, ndlp);
2472 	lpfc_drop_node(vport, ndlp);
2473 	return NLP_STE_FREED_NODE;
2474 }
2475 
2476 static uint32_t
2477 lpfc_device_recov_logo_issue(struct lpfc_vport *vport,
2478 			     struct lpfc_nodelist *ndlp,
2479 			     void *arg, uint32_t evt)
2480 {
2481 	/*
2482 	 * Device Recovery events have no meaning for a node with a LOGO
2483 	 * outstanding.  The LOGO has to complete first and handle the
2484 	 * node from that point.
2485 	 */
2486 	return ndlp->nlp_state;
2487 }
2488 
2489 static uint32_t
2490 lpfc_rcv_plogi_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2491 			  void *arg, uint32_t evt)
2492 {
2493 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2494 
2495 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2496 	return ndlp->nlp_state;
2497 }
2498 
2499 static uint32_t
2500 lpfc_rcv_prli_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2501 			 void *arg, uint32_t evt)
2502 {
2503 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2504 
2505 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
2506 		return ndlp->nlp_state;
2507 
2508 	lpfc_rcv_prli(vport, ndlp, cmdiocb);
2509 	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2510 	return ndlp->nlp_state;
2511 }
2512 
2513 static uint32_t
2514 lpfc_rcv_logo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2515 			 void *arg, uint32_t evt)
2516 {
2517 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2518 
2519 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2520 	return ndlp->nlp_state;
2521 }
2522 
2523 static uint32_t
2524 lpfc_rcv_padisc_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2525 			   void *arg, uint32_t evt)
2526 {
2527 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2528 
2529 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2530 	return ndlp->nlp_state;
2531 }
2532 
2533 static uint32_t
2534 lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2535 			 void *arg, uint32_t evt)
2536 {
2537 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2538 
2539 	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2540 	return ndlp->nlp_state;
2541 }
2542 
2543 static uint32_t
2544 lpfc_device_rm_unmap_node(struct lpfc_vport *vport,
2545 			  struct lpfc_nodelist *ndlp,
2546 			  void *arg,
2547 			  uint32_t evt)
2548 {
2549 	lpfc_drop_node(vport, ndlp);
2550 	return NLP_STE_FREED_NODE;
2551 }
2552 
2553 static uint32_t
2554 lpfc_device_recov_unmap_node(struct lpfc_vport *vport,
2555 			     struct lpfc_nodelist *ndlp,
2556 			     void *arg,
2557 			     uint32_t evt)
2558 {
2559 	ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
2560 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2561 	clear_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
2562 	clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2563 	spin_lock_irq(&ndlp->lock);
2564 	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
2565 	spin_unlock_irq(&ndlp->lock);
2566 	lpfc_disc_set_adisc(vport, ndlp);
2567 
2568 	return ndlp->nlp_state;
2569 }
2570 
2571 static uint32_t
2572 lpfc_rcv_plogi_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2573 			   void *arg, uint32_t evt)
2574 {
2575 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2576 
2577 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2578 	return ndlp->nlp_state;
2579 }
2580 
2581 static uint32_t
2582 lpfc_rcv_prli_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2583 			  void *arg, uint32_t evt)
2584 {
2585 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2586 
2587 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
2588 		return ndlp->nlp_state;
2589 	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2590 	return ndlp->nlp_state;
2591 }
2592 
2593 static uint32_t
2594 lpfc_rcv_logo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2595 			  void *arg, uint32_t evt)
2596 {
2597 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2598 
2599 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2600 	return ndlp->nlp_state;
2601 }
2602 
2603 static uint32_t
2604 lpfc_rcv_padisc_mapped_node(struct lpfc_vport *vport,
2605 			    struct lpfc_nodelist *ndlp,
2606 			    void *arg, uint32_t evt)
2607 {
2608 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2609 
2610 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2611 	return ndlp->nlp_state;
2612 }
2613 
2614 static uint32_t
2615 lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2616 			  void *arg, uint32_t evt)
2617 {
2618 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2619 
2620 	/* flush the target */
2621 	lpfc_sli_abort_iocb(vport, ndlp->nlp_sid, 0, LPFC_CTX_TGT);
2622 
2623 	/* Send PRLO_ACC */
2624 	set_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
2625 	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2626 
2627 	/* Save ELS_CMD_PRLO as the last elscmd and then set to NPR.
2628 	 * lpfc_cmpl_els_logo_acc is expected to restart discovery.
2629 	 */
2630 	ndlp->nlp_last_elscmd = ELS_CMD_PRLO;
2631 	ndlp->nlp_prev_state = ndlp->nlp_state;
2632 
2633 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_ELS | LOG_DISCOVERY,
2634 			 "3422 DID x%06x nflag x%lx lastels x%x ref cnt %u\n",
2635 			 ndlp->nlp_DID, ndlp->nlp_flag,
2636 			 ndlp->nlp_last_elscmd,
2637 			 kref_read(&ndlp->kref));
2638 
2639 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2640 
2641 	return ndlp->nlp_state;
2642 }
2643 
2644 static uint32_t
2645 lpfc_device_recov_mapped_node(struct lpfc_vport *vport,
2646 			      struct lpfc_nodelist *ndlp,
2647 			      void *arg,
2648 			      uint32_t evt)
2649 {
2650 	lpfc_disc_set_adisc(vport, ndlp);
2651 
2652 	ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
2653 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2654 	clear_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
2655 	clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2656 	spin_lock_irq(&ndlp->lock);
2657 	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
2658 	spin_unlock_irq(&ndlp->lock);
2659 	return ndlp->nlp_state;
2660 }
2661 
2662 static uint32_t
2663 lpfc_rcv_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2664 			void *arg, uint32_t evt)
2665 {
2666 	struct lpfc_iocbq *cmdiocb  = (struct lpfc_iocbq *) arg;
2667 
2668 	/* Ignore PLOGI if we have an outstanding LOGO */
2669 	if (test_bit(NLP_LOGO_SND, &ndlp->nlp_flag) ||
2670 	    test_bit(NLP_LOGO_ACC, &ndlp->nlp_flag))
2671 		return ndlp->nlp_state;
2672 	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
2673 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
2674 		clear_bit(NLP_NPR_ADISC, &ndlp->nlp_flag);
2675 		clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2676 	} else if (!test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag)) {
2677 		/* send PLOGI immediately, move to PLOGI issue state */
2678 		if (!test_bit(NLP_DELAY_TMO, &ndlp->nlp_flag)) {
2679 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2680 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2681 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2682 		}
2683 	}
2684 	return ndlp->nlp_state;
2685 }
2686 
2687 static uint32_t
2688 lpfc_rcv_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2689 		       void *arg, uint32_t evt)
2690 {
2691 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2692 	struct ls_rjt     stat;
2693 
2694 	memset(&stat, 0, sizeof (struct ls_rjt));
2695 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2696 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2697 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2698 
2699 	if (!test_bit(NLP_DELAY_TMO, &ndlp->nlp_flag)) {
2700 		/*
2701 		 * ADISC nodes will be handled in regular discovery path after
2702 		 * receiving response from NS.
2703 		 *
2704 		 * For other nodes, Send PLOGI to trigger an implicit LOGO.
2705 		 */
2706 		if (!test_bit(NLP_NPR_ADISC, &ndlp->nlp_flag)) {
2707 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2708 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2709 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2710 		}
2711 	}
2712 	return ndlp->nlp_state;
2713 }
2714 
2715 static uint32_t
2716 lpfc_rcv_logo_npr_node(struct lpfc_vport *vport,  struct lpfc_nodelist *ndlp,
2717 		       void *arg, uint32_t evt)
2718 {
2719 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2720 
2721 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2722 	return ndlp->nlp_state;
2723 }
2724 
2725 static uint32_t
2726 lpfc_rcv_padisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2727 			 void *arg, uint32_t evt)
2728 {
2729 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2730 
2731 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2732 	/*
2733 	 * Do not start discovery if discovery is about to start
2734 	 * or discovery in progress for this node. Starting discovery
2735 	 * here will affect the counting of discovery threads.
2736 	 */
2737 	if (!test_bit(NLP_DELAY_TMO, &ndlp->nlp_flag) &&
2738 	    !test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag)) {
2739 		/*
2740 		 * ADISC nodes will be handled in regular discovery path after
2741 		 * receiving response from NS.
2742 		 *
2743 		 * For other nodes, Send PLOGI to trigger an implicit LOGO.
2744 		 */
2745 		if (!test_bit(NLP_NPR_ADISC, &ndlp->nlp_flag)) {
2746 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2747 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2748 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2749 		}
2750 	}
2751 	return ndlp->nlp_state;
2752 }
2753 
2754 static uint32_t
2755 lpfc_rcv_prlo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2756 		       void *arg, uint32_t evt)
2757 {
2758 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2759 
2760 	set_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
2761 
2762 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2763 
2764 	if (!test_bit(NLP_DELAY_TMO, &ndlp->nlp_flag)) {
2765 		mod_timer(&ndlp->nlp_delayfunc,
2766 			  jiffies + secs_to_jiffies(1));
2767 		set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
2768 		clear_bit(NLP_NPR_ADISC, &ndlp->nlp_flag);
2769 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
2770 	} else {
2771 		clear_bit(NLP_NPR_ADISC, &ndlp->nlp_flag);
2772 	}
2773 	return ndlp->nlp_state;
2774 }
2775 
2776 static uint32_t
2777 lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2778 			 void *arg, uint32_t evt)
2779 {
2780 	struct lpfc_hba *phba = vport->phba;
2781 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2782 	u32 ulp_status;
2783 
2784 	cmdiocb = (struct lpfc_iocbq *) arg;
2785 	rspiocb = cmdiocb->rsp_iocb;
2786 
2787 	ulp_status = get_job_ulpstatus(phba, rspiocb);
2788 
2789 	if (ulp_status)
2790 		return NLP_STE_FREED_NODE;
2791 
2792 	return ndlp->nlp_state;
2793 }
2794 
2795 static uint32_t
2796 lpfc_cmpl_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2797 			void *arg, uint32_t evt)
2798 {
2799 	struct lpfc_hba *phba = vport->phba;
2800 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2801 	u32 ulp_status;
2802 
2803 	cmdiocb = (struct lpfc_iocbq *) arg;
2804 	rspiocb = cmdiocb->rsp_iocb;
2805 
2806 	ulp_status = get_job_ulpstatus(phba, rspiocb);
2807 
2808 	if (ulp_status && test_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag)) {
2809 		lpfc_drop_node(vport, ndlp);
2810 		return NLP_STE_FREED_NODE;
2811 	}
2812 	return ndlp->nlp_state;
2813 }
2814 
2815 static uint32_t
2816 lpfc_cmpl_logo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2817 			void *arg, uint32_t evt)
2818 {
2819 	/* For the fabric port just clear the fc flags. */
2820 	if (ndlp->nlp_DID == Fabric_DID) {
2821 		clear_bit(FC_FABRIC, &vport->fc_flag);
2822 		clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
2823 	}
2824 	lpfc_unreg_rpi(vport, ndlp);
2825 	return ndlp->nlp_state;
2826 }
2827 
2828 static uint32_t
2829 lpfc_cmpl_adisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2830 			 void *arg, uint32_t evt)
2831 {
2832 	struct lpfc_hba *phba = vport->phba;
2833 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2834 	u32 ulp_status;
2835 
2836 	cmdiocb = (struct lpfc_iocbq *) arg;
2837 	rspiocb = cmdiocb->rsp_iocb;
2838 
2839 	ulp_status = get_job_ulpstatus(phba, rspiocb);
2840 
2841 	if (ulp_status && test_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag)) {
2842 		lpfc_drop_node(vport, ndlp);
2843 		return NLP_STE_FREED_NODE;
2844 	}
2845 	return ndlp->nlp_state;
2846 }
2847 
2848 static uint32_t
2849 lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
2850 			    struct lpfc_nodelist *ndlp,
2851 			    void *arg, uint32_t evt)
2852 {
2853 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
2854 	MAILBOX_t    *mb = &pmb->u.mb;
2855 
2856 	if (!mb->mbxStatus) {
2857 		/* SLI4 ports have preallocated logical rpis. */
2858 		if (vport->phba->sli_rev < LPFC_SLI_REV4)
2859 			ndlp->nlp_rpi = mb->un.varWords[0];
2860 		set_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag);
2861 		if (test_bit(NLP_LOGO_ACC, &ndlp->nlp_flag))
2862 			lpfc_unreg_rpi(vport, ndlp);
2863 	} else {
2864 		if (test_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag)) {
2865 			lpfc_drop_node(vport, ndlp);
2866 			return NLP_STE_FREED_NODE;
2867 		}
2868 	}
2869 	return ndlp->nlp_state;
2870 }
2871 
2872 static uint32_t
2873 lpfc_device_rm_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2874 			void *arg, uint32_t evt)
2875 {
2876 	if (test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag)) {
2877 		set_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
2878 		return ndlp->nlp_state;
2879 	}
2880 	lpfc_drop_node(vport, ndlp);
2881 	return NLP_STE_FREED_NODE;
2882 }
2883 
2884 static uint32_t
2885 lpfc_device_recov_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2886 			   void *arg, uint32_t evt)
2887 {
2888 	/* Don't do anything that disrupts the RSCN unless lpfc is unloading. */
2889 	if (lpfc_check_unload_and_clr_rscn(&vport->fc_flag))
2890 		return ndlp->nlp_state;
2891 
2892 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
2893 	clear_bit(NLP_NODEV_REMOVE, &ndlp->nlp_flag);
2894 	clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2895 	spin_lock_irq(&ndlp->lock);
2896 	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
2897 	spin_unlock_irq(&ndlp->lock);
2898 	return ndlp->nlp_state;
2899 }
2900 
2901 
2902 /* This next section defines the NPort Discovery State Machine */
2903 
2904 /* There are 4 different double linked lists nodelist entries can reside on.
2905  * The plogi list and adisc list are used when Link Up discovery or RSCN
2906  * processing is needed. Each list holds the nodes that we will send PLOGI
2907  * or ADISC on. These lists will keep track of what nodes will be effected
2908  * by an RSCN, or a Link Up (Typically, all nodes are effected on Link Up).
2909  * The unmapped_list will contain all nodes that we have successfully logged
2910  * into at the Fibre Channel level. The mapped_list will contain all nodes
2911  * that are mapped FCP targets.
2912  */
2913 /*
2914  * The bind list is a list of undiscovered (potentially non-existent) nodes
2915  * that we have saved binding information on. This information is used when
2916  * nodes transition from the unmapped to the mapped list.
2917  */
2918 /* For UNUSED_NODE state, the node has just been allocated .
2919  * For PLOGI_ISSUE and REG_LOGIN_ISSUE, the node is on
2920  * the PLOGI list. For REG_LOGIN_COMPL, the node is taken off the PLOGI list
2921  * and put on the unmapped list. For ADISC processing, the node is taken off
2922  * the ADISC list and placed on either the mapped or unmapped list (depending
2923  * on its previous state). Once on the unmapped list, a PRLI is issued and the
2924  * state changed to PRLI_ISSUE. When the PRLI completion occurs, the state is
2925  * changed to UNMAPPED_NODE. If the completion indicates a mapped
2926  * node, the node is taken off the unmapped list. The binding list is checked
2927  * for a valid binding, or a binding is automatically assigned. If binding
2928  * assignment is unsuccessful, the node is left on the unmapped list. If
2929  * binding assignment is successful, the associated binding list entry (if
2930  * any) is removed, and the node is placed on the mapped list.
2931  */
2932 /*
2933  * For a Link Down, all nodes on the ADISC, PLOGI, unmapped or mapped
2934  * lists will receive a DEVICE_RECOVERY event. If the linkdown or devloss timers
2935  * expire, all effected nodes will receive a DEVICE_RM event.
2936  */
2937 /*
2938  * For a Link Up or RSCN, all nodes will move from the mapped / unmapped lists
2939  * to either the ADISC or PLOGI list.  After a Nameserver query or ALPA loopmap
2940  * check, additional nodes may be added or removed (via DEVICE_RM) to / from
2941  * the PLOGI or ADISC lists. Once the PLOGI and ADISC lists are populated,
2942  * we will first process the ADISC list.  32 entries are processed initially and
2943  * ADISC is initited for each one.  Completions / Events for each node are
2944  * funnelled thru the state machine.  As each node finishes ADISC processing, it
2945  * starts ADISC for any nodes waiting for ADISC processing. If no nodes are
2946  * waiting, and the ADISC list count is identically 0, then we are done. For
2947  * Link Up discovery, since all nodes on the PLOGI list are UNREG_LOGIN'ed, we
2948  * can issue a CLEAR_LA and reenable Link Events. Next we will process the PLOGI
2949  * list.  32 entries are processed initially and PLOGI is initited for each one.
2950  * Completions / Events for each node are funnelled thru the state machine.  As
2951  * each node finishes PLOGI processing, it starts PLOGI for any nodes waiting
2952  * for PLOGI processing. If no nodes are waiting, and the PLOGI list count is
2953  * indentically 0, then we are done. We have now completed discovery / RSCN
2954  * handling. Upon completion, ALL nodes should be on either the mapped or
2955  * unmapped lists.
2956  */
2957 
2958 static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
2959      (struct lpfc_vport *, struct lpfc_nodelist *, void *, uint32_t) = {
2960 	/* Action routine                  Event       Current State  */
2961 	lpfc_rcv_plogi_unused_node,	/* RCV_PLOGI   UNUSED_NODE    */
2962 	lpfc_rcv_els_unused_node,	/* RCV_PRLI        */
2963 	lpfc_rcv_logo_unused_node,	/* RCV_LOGO        */
2964 	lpfc_rcv_els_unused_node,	/* RCV_ADISC       */
2965 	lpfc_rcv_els_unused_node,	/* RCV_PDISC       */
2966 	lpfc_rcv_els_unused_node,	/* RCV_PRLO        */
2967 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
2968 	lpfc_disc_illegal,		/* CMPL_PRLI       */
2969 	lpfc_cmpl_logo_unused_node,	/* CMPL_LOGO       */
2970 	lpfc_disc_illegal,		/* CMPL_ADISC      */
2971 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
2972 	lpfc_device_rm_unused_node,	/* DEVICE_RM       */
2973 	lpfc_device_recov_unused_node,	/* DEVICE_RECOVERY */
2974 
2975 	lpfc_rcv_plogi_plogi_issue,	/* RCV_PLOGI   PLOGI_ISSUE    */
2976 	lpfc_rcv_prli_plogi_issue,	/* RCV_PRLI        */
2977 	lpfc_rcv_logo_plogi_issue,	/* RCV_LOGO        */
2978 	lpfc_rcv_els_plogi_issue,	/* RCV_ADISC       */
2979 	lpfc_rcv_els_plogi_issue,	/* RCV_PDISC       */
2980 	lpfc_rcv_els_plogi_issue,	/* RCV_PRLO        */
2981 	lpfc_cmpl_plogi_plogi_issue,	/* CMPL_PLOGI      */
2982 	lpfc_disc_illegal,		/* CMPL_PRLI       */
2983 	lpfc_cmpl_logo_plogi_issue,	/* CMPL_LOGO       */
2984 	lpfc_disc_illegal,		/* CMPL_ADISC      */
2985 	lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
2986 	lpfc_device_rm_plogi_issue,	/* DEVICE_RM       */
2987 	lpfc_device_recov_plogi_issue,	/* DEVICE_RECOVERY */
2988 
2989 	lpfc_rcv_plogi_adisc_issue,	/* RCV_PLOGI   ADISC_ISSUE    */
2990 	lpfc_rcv_prli_adisc_issue,	/* RCV_PRLI        */
2991 	lpfc_rcv_logo_adisc_issue,	/* RCV_LOGO        */
2992 	lpfc_rcv_padisc_adisc_issue,	/* RCV_ADISC       */
2993 	lpfc_rcv_padisc_adisc_issue,	/* RCV_PDISC       */
2994 	lpfc_rcv_prlo_adisc_issue,	/* RCV_PRLO        */
2995 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
2996 	lpfc_disc_illegal,		/* CMPL_PRLI       */
2997 	lpfc_disc_illegal,		/* CMPL_LOGO       */
2998 	lpfc_cmpl_adisc_adisc_issue,	/* CMPL_ADISC      */
2999 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3000 	lpfc_device_rm_adisc_issue,	/* DEVICE_RM       */
3001 	lpfc_device_recov_adisc_issue,	/* DEVICE_RECOVERY */
3002 
3003 	lpfc_rcv_plogi_reglogin_issue,	/* RCV_PLOGI  REG_LOGIN_ISSUE */
3004 	lpfc_rcv_prli_reglogin_issue,	/* RCV_PLOGI       */
3005 	lpfc_rcv_logo_reglogin_issue,	/* RCV_LOGO        */
3006 	lpfc_rcv_padisc_reglogin_issue,	/* RCV_ADISC       */
3007 	lpfc_rcv_padisc_reglogin_issue,	/* RCV_PDISC       */
3008 	lpfc_rcv_prlo_reglogin_issue,	/* RCV_PRLO        */
3009 	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
3010 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3011 	lpfc_disc_illegal,		/* CMPL_LOGO       */
3012 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3013 	lpfc_cmpl_reglogin_reglogin_issue,/* CMPL_REG_LOGIN  */
3014 	lpfc_device_rm_reglogin_issue,	/* DEVICE_RM       */
3015 	lpfc_device_recov_reglogin_issue,/* DEVICE_RECOVERY */
3016 
3017 	lpfc_rcv_plogi_prli_issue,	/* RCV_PLOGI   PRLI_ISSUE     */
3018 	lpfc_rcv_prli_prli_issue,	/* RCV_PRLI        */
3019 	lpfc_rcv_logo_prli_issue,	/* RCV_LOGO        */
3020 	lpfc_rcv_padisc_prli_issue,	/* RCV_ADISC       */
3021 	lpfc_rcv_padisc_prli_issue,	/* RCV_PDISC       */
3022 	lpfc_rcv_prlo_prli_issue,	/* RCV_PRLO        */
3023 	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
3024 	lpfc_cmpl_prli_prli_issue,	/* CMPL_PRLI       */
3025 	lpfc_disc_illegal,		/* CMPL_LOGO       */
3026 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3027 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3028 	lpfc_device_rm_prli_issue,	/* DEVICE_RM       */
3029 	lpfc_device_recov_prli_issue,	/* DEVICE_RECOVERY */
3030 
3031 	lpfc_rcv_plogi_logo_issue,	/* RCV_PLOGI   LOGO_ISSUE     */
3032 	lpfc_rcv_prli_logo_issue,	/* RCV_PRLI        */
3033 	lpfc_rcv_logo_logo_issue,	/* RCV_LOGO        */
3034 	lpfc_rcv_padisc_logo_issue,	/* RCV_ADISC       */
3035 	lpfc_rcv_padisc_logo_issue,	/* RCV_PDISC       */
3036 	lpfc_rcv_prlo_logo_issue,	/* RCV_PRLO        */
3037 	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
3038 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3039 	lpfc_cmpl_logo_logo_issue,	/* CMPL_LOGO       */
3040 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3041 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3042 	lpfc_device_rm_logo_issue,	/* DEVICE_RM       */
3043 	lpfc_device_recov_logo_issue,	/* DEVICE_RECOVERY */
3044 
3045 	lpfc_rcv_plogi_unmap_node,	/* RCV_PLOGI   UNMAPPED_NODE  */
3046 	lpfc_rcv_prli_unmap_node,	/* RCV_PRLI        */
3047 	lpfc_rcv_logo_unmap_node,	/* RCV_LOGO        */
3048 	lpfc_rcv_padisc_unmap_node,	/* RCV_ADISC       */
3049 	lpfc_rcv_padisc_unmap_node,	/* RCV_PDISC       */
3050 	lpfc_rcv_prlo_unmap_node,	/* RCV_PRLO        */
3051 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
3052 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3053 	lpfc_disc_illegal,		/* CMPL_LOGO       */
3054 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3055 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3056 	lpfc_device_rm_unmap_node,	/* DEVICE_RM       */
3057 	lpfc_device_recov_unmap_node,	/* DEVICE_RECOVERY */
3058 
3059 	lpfc_rcv_plogi_mapped_node,	/* RCV_PLOGI   MAPPED_NODE    */
3060 	lpfc_rcv_prli_mapped_node,	/* RCV_PRLI        */
3061 	lpfc_rcv_logo_mapped_node,	/* RCV_LOGO        */
3062 	lpfc_rcv_padisc_mapped_node,	/* RCV_ADISC       */
3063 	lpfc_rcv_padisc_mapped_node,	/* RCV_PDISC       */
3064 	lpfc_rcv_prlo_mapped_node,	/* RCV_PRLO        */
3065 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
3066 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3067 	lpfc_disc_illegal,		/* CMPL_LOGO       */
3068 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3069 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3070 	lpfc_disc_illegal,		/* DEVICE_RM       */
3071 	lpfc_device_recov_mapped_node,	/* DEVICE_RECOVERY */
3072 
3073 	lpfc_rcv_plogi_npr_node,        /* RCV_PLOGI   NPR_NODE    */
3074 	lpfc_rcv_prli_npr_node,         /* RCV_PRLI        */
3075 	lpfc_rcv_logo_npr_node,         /* RCV_LOGO        */
3076 	lpfc_rcv_padisc_npr_node,       /* RCV_ADISC       */
3077 	lpfc_rcv_padisc_npr_node,       /* RCV_PDISC       */
3078 	lpfc_rcv_prlo_npr_node,         /* RCV_PRLO        */
3079 	lpfc_cmpl_plogi_npr_node,	/* CMPL_PLOGI      */
3080 	lpfc_cmpl_prli_npr_node,	/* CMPL_PRLI       */
3081 	lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
3082 	lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
3083 	lpfc_cmpl_reglogin_npr_node,    /* CMPL_REG_LOGIN  */
3084 	lpfc_device_rm_npr_node,        /* DEVICE_RM       */
3085 	lpfc_device_recov_npr_node,     /* DEVICE_RECOVERY */
3086 };
3087 
3088 int
3089 lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3090 			void *arg, uint32_t evt)
3091 {
3092 	uint32_t cur_state, rc;
3093 	uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
3094 			 uint32_t);
3095 	uint32_t got_ndlp = 0;
3096 	uint32_t data1;
3097 
3098 	if (lpfc_nlp_get(ndlp))
3099 		got_ndlp = 1;
3100 
3101 	cur_state = ndlp->nlp_state;
3102 
3103 	data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
3104 		((uint32_t)ndlp->nlp_type));
3105 	/* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
3106 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3107 			 "0211 DSM in event x%x on NPort x%x in "
3108 			 "state %d rpi x%x Data: x%lx x%x\n",
3109 			 evt, ndlp->nlp_DID, cur_state, ndlp->nlp_rpi,
3110 			 ndlp->nlp_flag, data1);
3111 
3112 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
3113 		 "DSM in:          evt:%d ste:%d did:x%x",
3114 		evt, cur_state, ndlp->nlp_DID);
3115 
3116 	func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
3117 	rc = (func) (vport, ndlp, arg, evt);
3118 
3119 	/* DSM out state <rc> on NPort <nlp_DID> */
3120 	if (got_ndlp) {
3121 		data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
3122 			((uint32_t)ndlp->nlp_type));
3123 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3124 			 "0212 DSM out state %d on NPort x%x "
3125 			 "rpi x%x Data: x%lx x%x\n",
3126 			 rc, ndlp->nlp_DID, ndlp->nlp_rpi, ndlp->nlp_flag,
3127 			 data1);
3128 
3129 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
3130 			"DSM out:         ste:%d did:x%x flg:x%lx",
3131 			rc, ndlp->nlp_DID, ndlp->nlp_flag);
3132 		/* Decrement the ndlp reference count held for this function */
3133 		lpfc_nlp_put(ndlp);
3134 	} else {
3135 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3136 			"0213 DSM out state %d on NPort free\n", rc);
3137 
3138 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
3139 			"DSM out:         ste:%d did:x%x flg:x%x",
3140 			rc, 0, 0);
3141 	}
3142 
3143 	return rc;
3144 }
3145