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