1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2023 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 		    vport->fc_flag & FC_PT2PT)
839 			goto out;
840 		lpfc_linkdown_port(vport);
841 		spin_lock_irq(shost->host_lock);
842 		vport->fc_flag |= FC_VPORT_LOGO_RCVD;
843 		spin_unlock_irq(shost->host_lock);
844 		vports = lpfc_create_vport_work_array(phba);
845 		if (vports) {
846 			for (i = 0; i <= phba->max_vports && vports[i] != NULL;
847 					i++) {
848 				if ((!(vports[i]->fc_flag &
849 					FC_VPORT_LOGO_RCVD)) &&
850 					(vports[i]->port_state > LPFC_FDISC)) {
851 					active_vlink_present = 1;
852 					break;
853 				}
854 			}
855 			lpfc_destroy_vport_work_array(phba, vports);
856 		}
857 
858 		/*
859 		 * Don't re-instantiate if vport is marked for deletion.
860 		 * If we are here first then vport_delete is going to wait
861 		 * for discovery to complete.
862 		 */
863 		if (!(vport->load_flag & FC_UNLOADING) &&
864 					active_vlink_present) {
865 			/*
866 			 * If there are other active VLinks present,
867 			 * re-instantiate the Vlink using FDISC.
868 			 */
869 			mod_timer(&ndlp->nlp_delayfunc,
870 				  jiffies + msecs_to_jiffies(1000));
871 			spin_lock_irq(&ndlp->lock);
872 			ndlp->nlp_flag |= NLP_DELAY_TMO;
873 			spin_unlock_irq(&ndlp->lock);
874 			ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
875 			vport->port_state = LPFC_FDISC;
876 		} else {
877 			spin_lock_irq(shost->host_lock);
878 			phba->pport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG;
879 			spin_unlock_irq(shost->host_lock);
880 			lpfc_retry_pport_discovery(phba);
881 		}
882 	} else {
883 		lpfc_printf_vlog(vport, KERN_INFO,
884 				 LOG_NODE | LOG_ELS | LOG_DISCOVERY,
885 				 "3203 LOGO recover nport x%06x state x%x "
886 				 "ntype x%x fc_flag x%x\n",
887 				 ndlp->nlp_DID, ndlp->nlp_state,
888 				 ndlp->nlp_type, vport->fc_flag);
889 
890 		/* Special cases for rports that recover post LOGO. */
891 		if ((!(ndlp->nlp_type == NLP_FABRIC) &&
892 		     (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) ||
893 		      vport->fc_flag & FC_PT2PT)) ||
894 		    (ndlp->nlp_state >= NLP_STE_ADISC_ISSUE ||
895 		     ndlp->nlp_state <= NLP_STE_PRLI_ISSUE)) {
896 			mod_timer(&ndlp->nlp_delayfunc,
897 				  jiffies + msecs_to_jiffies(1000 * 1));
898 			spin_lock_irq(&ndlp->lock);
899 			ndlp->nlp_flag |= NLP_DELAY_TMO;
900 			spin_unlock_irq(&ndlp->lock);
901 			ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
902 			lpfc_printf_vlog(vport, KERN_INFO,
903 					 LOG_NODE | LOG_ELS | LOG_DISCOVERY,
904 					 "3204 Start nlpdelay on DID x%06x "
905 					 "nflag x%x lastels x%x ref cnt %u",
906 					 ndlp->nlp_DID, ndlp->nlp_flag,
907 					 ndlp->nlp_last_elscmd,
908 					 kref_read(&ndlp->kref));
909 		}
910 	}
911 out:
912 	/* Unregister from backend, could have been skipped due to ADISC */
913 	lpfc_nlp_unreg_node(vport, ndlp);
914 
915 	ndlp->nlp_prev_state = ndlp->nlp_state;
916 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
917 
918 	spin_lock_irq(&ndlp->lock);
919 	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
920 	spin_unlock_irq(&ndlp->lock);
921 	/* The driver has to wait until the ACC completes before it continues
922 	 * processing the LOGO.  The action will resume in
923 	 * lpfc_cmpl_els_logo_acc routine. Since part of processing includes an
924 	 * unreg_login, the driver waits so the ACC does not get aborted.
925 	 */
926 	return 0;
927 }
928 
929 static uint32_t
930 lpfc_rcv_prli_support_check(struct lpfc_vport *vport,
931 			    struct lpfc_nodelist *ndlp,
932 			    struct lpfc_iocbq *cmdiocb)
933 {
934 	struct ls_rjt stat;
935 	uint32_t *payload;
936 	uint32_t cmd;
937 
938 	payload = cmdiocb->cmd_dmabuf->virt;
939 	cmd = *payload;
940 	if (vport->phba->nvmet_support) {
941 		/* Must be a NVME PRLI */
942 		if (cmd ==  ELS_CMD_PRLI)
943 			goto out;
944 	} else {
945 		/* Initiator mode. */
946 		if (!vport->nvmei_support && (cmd == ELS_CMD_NVMEPRLI))
947 			goto out;
948 	}
949 	return 1;
950 out:
951 	lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME_DISC,
952 			 "6115 Rcv PRLI (%x) check failed: ndlp rpi %d "
953 			 "state x%x flags x%x\n",
954 			 cmd, ndlp->nlp_rpi, ndlp->nlp_state,
955 			 ndlp->nlp_flag);
956 	memset(&stat, 0, sizeof(struct ls_rjt));
957 	stat.un.b.lsRjtRsnCode = LSRJT_CMD_UNSUPPORTED;
958 	stat.un.b.lsRjtRsnCodeExp = LSEXP_REQ_UNSUPPORTED;
959 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
960 			    ndlp, NULL);
961 	return 0;
962 }
963 
964 static void
965 lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
966 	      struct lpfc_iocbq *cmdiocb)
967 {
968 	struct lpfc_hba  *phba = vport->phba;
969 	struct lpfc_dmabuf *pcmd;
970 	uint32_t *lp;
971 	PRLI *npr;
972 	struct fc_rport *rport = ndlp->rport;
973 	u32 roles;
974 
975 	pcmd = cmdiocb->cmd_dmabuf;
976 	lp = (uint32_t *)pcmd->virt;
977 	npr = (PRLI *)((uint8_t *)lp + sizeof(uint32_t));
978 
979 	if ((npr->prliType == PRLI_FCP_TYPE) ||
980 	    (npr->prliType == PRLI_NVME_TYPE)) {
981 		if (npr->initiatorFunc) {
982 			if (npr->prliType == PRLI_FCP_TYPE)
983 				ndlp->nlp_type |= NLP_FCP_INITIATOR;
984 			if (npr->prliType == PRLI_NVME_TYPE)
985 				ndlp->nlp_type |= NLP_NVME_INITIATOR;
986 		}
987 		if (npr->targetFunc) {
988 			if (npr->prliType == PRLI_FCP_TYPE)
989 				ndlp->nlp_type |= NLP_FCP_TARGET;
990 			if (npr->prliType == PRLI_NVME_TYPE)
991 				ndlp->nlp_type |= NLP_NVME_TARGET;
992 			if (npr->writeXferRdyDis)
993 				ndlp->nlp_flag |= NLP_FIRSTBURST;
994 		}
995 		if (npr->Retry && ndlp->nlp_type &
996 					(NLP_FCP_INITIATOR | NLP_FCP_TARGET))
997 			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
998 
999 		if (npr->Retry && phba->nsler &&
1000 		    ndlp->nlp_type & (NLP_NVME_INITIATOR | NLP_NVME_TARGET))
1001 			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;
1002 
1003 
1004 		/* If this driver is in nvme target mode, set the ndlp's fc4
1005 		 * type to NVME provided the PRLI response claims NVME FC4
1006 		 * type.  Target mode does not issue gft_id so doesn't get
1007 		 * the fc4 type set until now.
1008 		 */
1009 		if (phba->nvmet_support && (npr->prliType == PRLI_NVME_TYPE)) {
1010 			ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1011 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1012 		}
1013 
1014 		/* Fabric Controllers send FCP PRLI as an initiator but should
1015 		 * not get recognized as FCP type and registered with transport.
1016 		 */
1017 		if (npr->prliType == PRLI_FCP_TYPE &&
1018 		    !(ndlp->nlp_type & NLP_FABRIC))
1019 			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1020 	}
1021 	if (rport) {
1022 		/* We need to update the rport role values */
1023 		roles = FC_RPORT_ROLE_UNKNOWN;
1024 		if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1025 			roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1026 		if (ndlp->nlp_type & NLP_FCP_TARGET)
1027 			roles |= FC_RPORT_ROLE_FCP_TARGET;
1028 
1029 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1030 			"rport rolechg:   role:x%x did:x%x flg:x%x",
1031 			roles, ndlp->nlp_DID, ndlp->nlp_flag);
1032 
1033 		if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1034 			fc_remote_port_rolechg(rport, roles);
1035 	}
1036 }
1037 
1038 static uint32_t
1039 lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1040 {
1041 	if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
1042 		spin_lock_irq(&ndlp->lock);
1043 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1044 		spin_unlock_irq(&ndlp->lock);
1045 		return 0;
1046 	}
1047 
1048 	if (!(vport->fc_flag & FC_PT2PT)) {
1049 		/* Check config parameter use-adisc or FCP-2 */
1050 		if (vport->cfg_use_adisc && ((vport->fc_flag & FC_RSCN_MODE) ||
1051 		    ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) &&
1052 		     (ndlp->nlp_type & NLP_FCP_TARGET)))) {
1053 			spin_lock_irq(&ndlp->lock);
1054 			ndlp->nlp_flag |= NLP_NPR_ADISC;
1055 			spin_unlock_irq(&ndlp->lock);
1056 			return 1;
1057 		}
1058 	}
1059 
1060 	spin_lock_irq(&ndlp->lock);
1061 	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1062 	spin_unlock_irq(&ndlp->lock);
1063 	lpfc_unreg_rpi(vport, ndlp);
1064 	return 0;
1065 }
1066 
1067 /**
1068  * lpfc_release_rpi - Release a RPI by issuing unreg_login mailbox cmd.
1069  * @phba : Pointer to lpfc_hba structure.
1070  * @vport: Pointer to lpfc_vport structure.
1071  * @ndlp: Pointer to lpfc_nodelist structure.
1072  * @rpi  : rpi to be release.
1073  *
1074  * This function will send a unreg_login mailbox command to the firmware
1075  * to release a rpi.
1076  **/
1077 static void
1078 lpfc_release_rpi(struct lpfc_hba *phba, struct lpfc_vport *vport,
1079 		 struct lpfc_nodelist *ndlp, uint16_t rpi)
1080 {
1081 	LPFC_MBOXQ_t *pmb;
1082 	int rc;
1083 
1084 	/* If there is already an UNREG in progress for this ndlp,
1085 	 * no need to queue up another one.
1086 	 */
1087 	if (ndlp->nlp_flag & NLP_UNREG_INP) {
1088 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1089 				 "1435 release_rpi SKIP UNREG x%x on "
1090 				 "NPort x%x deferred x%x  flg x%x "
1091 				 "Data: x%px\n",
1092 				 ndlp->nlp_rpi, ndlp->nlp_DID,
1093 				 ndlp->nlp_defer_did,
1094 				 ndlp->nlp_flag, ndlp);
1095 		return;
1096 	}
1097 
1098 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
1099 			GFP_KERNEL);
1100 	if (!pmb)
1101 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1102 				 "2796 mailbox memory allocation failed \n");
1103 	else {
1104 		lpfc_unreg_login(phba, vport->vpi, rpi, pmb);
1105 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1106 		pmb->vport = vport;
1107 		pmb->ctx_ndlp = lpfc_nlp_get(ndlp);
1108 		if (!pmb->ctx_ndlp) {
1109 			mempool_free(pmb, phba->mbox_mem_pool);
1110 			return;
1111 		}
1112 
1113 		if (((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
1114 		    (!(vport->fc_flag & FC_OFFLINE_MODE)))
1115 			ndlp->nlp_flag |= NLP_UNREG_INP;
1116 
1117 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1118 				 "1437 release_rpi UNREG x%x "
1119 				 "on NPort x%x flg x%x\n",
1120 				 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag);
1121 
1122 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1123 		if (rc == MBX_NOT_FINISHED) {
1124 			lpfc_nlp_put(ndlp);
1125 			mempool_free(pmb, phba->mbox_mem_pool);
1126 		}
1127 	}
1128 }
1129 
1130 static uint32_t
1131 lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1132 		  void *arg, uint32_t evt)
1133 {
1134 	struct lpfc_hba *phba;
1135 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1136 	uint16_t rpi;
1137 
1138 	phba = vport->phba;
1139 	/* Release the RPI if reglogin completing */
1140 	if (!(phba->pport->load_flag & FC_UNLOADING) &&
1141 		(evt == NLP_EVT_CMPL_REG_LOGIN) &&
1142 		(!pmb->u.mb.mbxStatus)) {
1143 		rpi = pmb->u.mb.un.varWords[0];
1144 		lpfc_release_rpi(phba, vport, ndlp, rpi);
1145 	}
1146 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1147 			 "0271 Illegal State Transition: node x%x "
1148 			 "event x%x, state x%x Data: x%x x%x\n",
1149 			 ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
1150 			 ndlp->nlp_flag);
1151 	return ndlp->nlp_state;
1152 }
1153 
1154 static uint32_t
1155 lpfc_cmpl_plogi_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1156 		  void *arg, uint32_t evt)
1157 {
1158 	/* This transition is only legal if we previously
1159 	 * rcv'ed a PLOGI. Since we don't want 2 discovery threads
1160 	 * working on the same NPortID, do nothing for this thread
1161 	 * to stop it.
1162 	 */
1163 	if (!(ndlp->nlp_flag & NLP_RCV_PLOGI)) {
1164 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1165 				 "0272 Illegal State Transition: node x%x "
1166 				 "event x%x, state x%x Data: x%x x%x\n",
1167 				  ndlp->nlp_DID, evt, ndlp->nlp_state,
1168 				  ndlp->nlp_rpi, ndlp->nlp_flag);
1169 	}
1170 	return ndlp->nlp_state;
1171 }
1172 
1173 /* Start of Discovery State Machine routines */
1174 
1175 static uint32_t
1176 lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1177 			   void *arg, uint32_t evt)
1178 {
1179 	struct lpfc_iocbq *cmdiocb;
1180 
1181 	cmdiocb = (struct lpfc_iocbq *) arg;
1182 
1183 	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1184 		return ndlp->nlp_state;
1185 	}
1186 	return NLP_STE_FREED_NODE;
1187 }
1188 
1189 static uint32_t
1190 lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1191 			 void *arg, uint32_t evt)
1192 {
1193 	lpfc_issue_els_logo(vport, ndlp, 0);
1194 	return ndlp->nlp_state;
1195 }
1196 
1197 static uint32_t
1198 lpfc_rcv_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1199 			  void *arg, uint32_t evt)
1200 {
1201 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1202 
1203 	spin_lock_irq(&ndlp->lock);
1204 	ndlp->nlp_flag |= NLP_LOGO_ACC;
1205 	spin_unlock_irq(&ndlp->lock);
1206 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1207 
1208 	return ndlp->nlp_state;
1209 }
1210 
1211 static uint32_t
1212 lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1213 			   void *arg, uint32_t evt)
1214 {
1215 	return NLP_STE_FREED_NODE;
1216 }
1217 
1218 static uint32_t
1219 lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1220 			   void *arg, uint32_t evt)
1221 {
1222 	return NLP_STE_FREED_NODE;
1223 }
1224 
1225 static uint32_t
1226 lpfc_device_recov_unused_node(struct lpfc_vport *vport,
1227 			struct lpfc_nodelist *ndlp,
1228 			   void *arg, uint32_t evt)
1229 {
1230 	return ndlp->nlp_state;
1231 }
1232 
1233 static uint32_t
1234 lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1235 			   void *arg, uint32_t evt)
1236 {
1237 	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1238 	struct lpfc_hba   *phba = vport->phba;
1239 	struct lpfc_iocbq *cmdiocb = arg;
1240 	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
1241 	uint32_t *lp = (uint32_t *) pcmd->virt;
1242 	struct serv_parm *sp = (struct serv_parm *) (lp + 1);
1243 	struct ls_rjt stat;
1244 	int port_cmp;
1245 
1246 	memset(&stat, 0, sizeof (struct ls_rjt));
1247 
1248 	/* For a PLOGI, we only accept if our portname is less
1249 	 * than the remote portname.
1250 	 */
1251 	phba->fc_stat.elsLogiCol++;
1252 	port_cmp = memcmp(&vport->fc_portname, &sp->portName,
1253 			  sizeof(struct lpfc_name));
1254 
1255 	if (port_cmp >= 0) {
1256 		/* Reject this request because the remote node will accept
1257 		   ours */
1258 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1259 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
1260 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
1261 			NULL);
1262 	} else {
1263 		if (lpfc_rcv_plogi(vport, ndlp, cmdiocb) &&
1264 		    (ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
1265 		    (vport->num_disc_nodes)) {
1266 			spin_lock_irq(&ndlp->lock);
1267 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1268 			spin_unlock_irq(&ndlp->lock);
1269 			/* Check if there are more PLOGIs to be sent */
1270 			lpfc_more_plogi(vport);
1271 			if (vport->num_disc_nodes == 0) {
1272 				spin_lock_irq(shost->host_lock);
1273 				vport->fc_flag &= ~FC_NDISC_ACTIVE;
1274 				spin_unlock_irq(shost->host_lock);
1275 				lpfc_can_disctmo(vport);
1276 				lpfc_end_rscn(vport);
1277 			}
1278 		}
1279 	} /* If our portname was less */
1280 
1281 	return ndlp->nlp_state;
1282 }
1283 
1284 static uint32_t
1285 lpfc_rcv_prli_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1286 			  void *arg, uint32_t evt)
1287 {
1288 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1289 	struct ls_rjt     stat;
1290 
1291 	memset(&stat, 0, sizeof (struct ls_rjt));
1292 	stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
1293 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1294 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1295 	return ndlp->nlp_state;
1296 }
1297 
1298 static uint32_t
1299 lpfc_rcv_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1300 			  void *arg, uint32_t evt)
1301 {
1302 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1303 
1304 	/* Retrieve RPI from LOGO IOCB. RPI is used for CMD_ABORT_XRI_CN */
1305 	if (vport->phba->sli_rev == LPFC_SLI_REV3)
1306 		ndlp->nlp_rpi = cmdiocb->iocb.ulpIoTag;
1307 				/* software abort outstanding PLOGI */
1308 	lpfc_els_abort(vport->phba, ndlp);
1309 
1310 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1311 	return ndlp->nlp_state;
1312 }
1313 
1314 static uint32_t
1315 lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1316 			 void *arg, uint32_t evt)
1317 {
1318 	struct lpfc_hba   *phba = vport->phba;
1319 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1320 
1321 	/* software abort outstanding PLOGI */
1322 	lpfc_els_abort(phba, ndlp);
1323 
1324 	if (evt == NLP_EVT_RCV_LOGO) {
1325 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1326 	} else {
1327 		lpfc_issue_els_logo(vport, ndlp, 0);
1328 	}
1329 
1330 	/* Put ndlp in npr state set plogi timer for 1 sec */
1331 	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000 * 1));
1332 	spin_lock_irq(&ndlp->lock);
1333 	ndlp->nlp_flag |= NLP_DELAY_TMO;
1334 	spin_unlock_irq(&ndlp->lock);
1335 	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1336 	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
1337 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1338 
1339 	return ndlp->nlp_state;
1340 }
1341 
1342 static uint32_t
1343 lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
1344 			    struct lpfc_nodelist *ndlp,
1345 			    void *arg,
1346 			    uint32_t evt)
1347 {
1348 	struct lpfc_hba    *phba = vport->phba;
1349 	struct lpfc_iocbq  *cmdiocb, *rspiocb;
1350 	struct lpfc_dmabuf *pcmd, *prsp;
1351 	uint32_t *lp;
1352 	uint32_t vid, flag;
1353 	struct serv_parm *sp;
1354 	uint32_t ed_tov;
1355 	LPFC_MBOXQ_t *mbox;
1356 	int rc;
1357 	u32 ulp_status;
1358 	u32 did;
1359 
1360 	cmdiocb = (struct lpfc_iocbq *) arg;
1361 	rspiocb = cmdiocb->rsp_iocb;
1362 
1363 	ulp_status = get_job_ulpstatus(phba, rspiocb);
1364 
1365 	if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
1366 		/* Recovery from PLOGI collision logic */
1367 		return ndlp->nlp_state;
1368 	}
1369 
1370 	if (ulp_status)
1371 		goto out;
1372 
1373 	pcmd = cmdiocb->cmd_dmabuf;
1374 
1375 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1376 	if (!prsp)
1377 		goto out;
1378 
1379 	lp = (uint32_t *) prsp->virt;
1380 	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
1381 
1382 	/* Some switches have FDMI servers returning 0 for WWN */
1383 	if ((ndlp->nlp_DID != FDMI_DID) &&
1384 		(wwn_to_u64(sp->portName.u.wwn) == 0 ||
1385 		wwn_to_u64(sp->nodeName.u.wwn) == 0)) {
1386 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1387 				 "0142 PLOGI RSP: Invalid WWN.\n");
1388 		goto out;
1389 	}
1390 	if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0))
1391 		goto out;
1392 	/* PLOGI chkparm OK */
1393 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1394 			 "0121 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
1395 			 ndlp->nlp_DID, ndlp->nlp_state,
1396 			 ndlp->nlp_flag, ndlp->nlp_rpi);
1397 	if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
1398 		ndlp->nlp_fcp_info |= CLASS2;
1399 	else
1400 		ndlp->nlp_fcp_info |= CLASS3;
1401 
1402 	ndlp->nlp_class_sup = 0;
1403 	if (sp->cls1.classValid)
1404 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
1405 	if (sp->cls2.classValid)
1406 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
1407 	if (sp->cls3.classValid)
1408 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
1409 	if (sp->cls4.classValid)
1410 		ndlp->nlp_class_sup |= FC_COS_CLASS4;
1411 	ndlp->nlp_maxframe =
1412 		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
1413 
1414 	if ((vport->fc_flag & FC_PT2PT) &&
1415 	    (vport->fc_flag & FC_PT2PT_PLOGI)) {
1416 		ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
1417 		if (sp->cmn.edtovResolution) {
1418 			/* E_D_TOV ticks are in nanoseconds */
1419 			ed_tov = (phba->fc_edtov + 999999) / 1000000;
1420 		}
1421 
1422 		ndlp->nlp_flag &= ~NLP_SUPPRESS_RSP;
1423 		if ((phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) &&
1424 		    sp->cmn.valid_vendor_ver_level) {
1425 			vid = be32_to_cpu(sp->un.vv.vid);
1426 			flag = be32_to_cpu(sp->un.vv.flags);
1427 			if ((vid == LPFC_VV_EMLX_ID) &&
1428 			    (flag & LPFC_VV_SUPPRESS_RSP))
1429 				ndlp->nlp_flag |= NLP_SUPPRESS_RSP;
1430 		}
1431 
1432 		/*
1433 		 * Use the larger EDTOV
1434 		 * RATOV = 2 * EDTOV for pt-to-pt
1435 		 */
1436 		if (ed_tov > phba->fc_edtov)
1437 			phba->fc_edtov = ed_tov;
1438 		phba->fc_ratov = (2 * phba->fc_edtov) / 1000;
1439 
1440 		memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
1441 
1442 		/* Issue config_link / reg_vfi to account for updated TOV's */
1443 		if (phba->sli_rev == LPFC_SLI_REV4) {
1444 			lpfc_issue_reg_vfi(vport);
1445 		} else {
1446 			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1447 			if (!mbox) {
1448 				lpfc_printf_vlog(vport, KERN_ERR,
1449 						 LOG_TRACE_EVENT,
1450 						 "0133 PLOGI: no memory "
1451 						 "for config_link "
1452 						 "Data: x%x x%x x%x x%x\n",
1453 						 ndlp->nlp_DID, ndlp->nlp_state,
1454 						 ndlp->nlp_flag, ndlp->nlp_rpi);
1455 				goto out;
1456 			}
1457 
1458 			lpfc_config_link(phba, mbox);
1459 
1460 			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1461 			mbox->vport = vport;
1462 			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1463 			if (rc == MBX_NOT_FINISHED) {
1464 				mempool_free(mbox, phba->mbox_mem_pool);
1465 				goto out;
1466 			}
1467 		}
1468 	}
1469 
1470 	lpfc_unreg_rpi(vport, ndlp);
1471 
1472 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1473 	if (!mbox) {
1474 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1475 				 "0018 PLOGI: no memory for reg_login "
1476 				 "Data: x%x x%x x%x x%x\n",
1477 				 ndlp->nlp_DID, ndlp->nlp_state,
1478 				 ndlp->nlp_flag, ndlp->nlp_rpi);
1479 		goto out;
1480 	}
1481 
1482 	did = get_job_els_rsp64_did(phba, cmdiocb);
1483 
1484 	if (lpfc_reg_rpi(phba, vport->vpi, did,
1485 			 (uint8_t *) sp, mbox, ndlp->nlp_rpi) == 0) {
1486 		switch (ndlp->nlp_DID) {
1487 		case NameServer_DID:
1488 			mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
1489 			/* Fabric Controller Node needs these parameters. */
1490 			memcpy(&ndlp->fc_sparam, sp, sizeof(struct serv_parm));
1491 			break;
1492 		case FDMI_DID:
1493 			mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
1494 			break;
1495 		default:
1496 			ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
1497 			mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
1498 		}
1499 
1500 		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
1501 		if (!mbox->ctx_ndlp)
1502 			goto out;
1503 
1504 		mbox->vport = vport;
1505 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
1506 		    != MBX_NOT_FINISHED) {
1507 			lpfc_nlp_set_state(vport, ndlp,
1508 					   NLP_STE_REG_LOGIN_ISSUE);
1509 			return ndlp->nlp_state;
1510 		}
1511 		if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
1512 			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1513 		/* decrement node reference count to the failed mbox
1514 		 * command
1515 		 */
1516 		lpfc_nlp_put(ndlp);
1517 		lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
1518 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1519 				 "0134 PLOGI: cannot issue reg_login "
1520 				 "Data: x%x x%x x%x x%x\n",
1521 				 ndlp->nlp_DID, ndlp->nlp_state,
1522 				 ndlp->nlp_flag, ndlp->nlp_rpi);
1523 	} else {
1524 		mempool_free(mbox, phba->mbox_mem_pool);
1525 
1526 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1527 				 "0135 PLOGI: cannot format reg_login "
1528 				 "Data: x%x x%x x%x x%x\n",
1529 				 ndlp->nlp_DID, ndlp->nlp_state,
1530 				 ndlp->nlp_flag, ndlp->nlp_rpi);
1531 	}
1532 
1533 
1534 out:
1535 	if (ndlp->nlp_DID == NameServer_DID) {
1536 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1537 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1538 				 "0261 Cannot Register NameServer login\n");
1539 	}
1540 
1541 	/*
1542 	** In case the node reference counter does not go to zero, ensure that
1543 	** the stale state for the node is not processed.
1544 	*/
1545 
1546 	ndlp->nlp_prev_state = ndlp->nlp_state;
1547 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1548 	return NLP_STE_FREED_NODE;
1549 }
1550 
1551 static uint32_t
1552 lpfc_cmpl_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1553 			   void *arg, uint32_t evt)
1554 {
1555 	return ndlp->nlp_state;
1556 }
1557 
1558 static uint32_t
1559 lpfc_cmpl_reglogin_plogi_issue(struct lpfc_vport *vport,
1560 	struct lpfc_nodelist *ndlp, void *arg, uint32_t evt)
1561 {
1562 	struct lpfc_hba *phba;
1563 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1564 	MAILBOX_t *mb = &pmb->u.mb;
1565 	uint16_t rpi;
1566 
1567 	phba = vport->phba;
1568 	/* Release the RPI */
1569 	if (!(phba->pport->load_flag & FC_UNLOADING) &&
1570 		!mb->mbxStatus) {
1571 		rpi = pmb->u.mb.un.varWords[0];
1572 		lpfc_release_rpi(phba, vport, ndlp, rpi);
1573 	}
1574 	return ndlp->nlp_state;
1575 }
1576 
1577 static uint32_t
1578 lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1579 			   void *arg, uint32_t evt)
1580 {
1581 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1582 		spin_lock_irq(&ndlp->lock);
1583 		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1584 		spin_unlock_irq(&ndlp->lock);
1585 		return ndlp->nlp_state;
1586 	} else {
1587 		/* software abort outstanding PLOGI */
1588 		lpfc_els_abort(vport->phba, ndlp);
1589 
1590 		lpfc_drop_node(vport, ndlp);
1591 		return NLP_STE_FREED_NODE;
1592 	}
1593 }
1594 
1595 static uint32_t
1596 lpfc_device_recov_plogi_issue(struct lpfc_vport *vport,
1597 			      struct lpfc_nodelist *ndlp,
1598 			      void *arg,
1599 			      uint32_t evt)
1600 {
1601 	struct lpfc_hba  *phba = vport->phba;
1602 
1603 	/* Don't do anything that will mess up processing of the
1604 	 * previous RSCN.
1605 	 */
1606 	if (vport->fc_flag & FC_RSCN_DEFERRED)
1607 		return ndlp->nlp_state;
1608 
1609 	/* software abort outstanding PLOGI */
1610 	lpfc_els_abort(phba, ndlp);
1611 
1612 	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
1613 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1614 	spin_lock_irq(&ndlp->lock);
1615 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1616 	spin_unlock_irq(&ndlp->lock);
1617 
1618 	return ndlp->nlp_state;
1619 }
1620 
1621 static uint32_t
1622 lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1623 			   void *arg, uint32_t evt)
1624 {
1625 	struct lpfc_hba   *phba = vport->phba;
1626 	struct lpfc_iocbq *cmdiocb;
1627 
1628 	/* software abort outstanding ADISC */
1629 	lpfc_els_abort(phba, ndlp);
1630 
1631 	cmdiocb = (struct lpfc_iocbq *) arg;
1632 
1633 	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1634 		if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1635 			spin_lock_irq(&ndlp->lock);
1636 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1637 			spin_unlock_irq(&ndlp->lock);
1638 			if (vport->num_disc_nodes)
1639 				lpfc_more_adisc(vport);
1640 		}
1641 		return ndlp->nlp_state;
1642 	}
1643 	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1644 	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
1645 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1646 
1647 	return ndlp->nlp_state;
1648 }
1649 
1650 static uint32_t
1651 lpfc_rcv_prli_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1652 			  void *arg, uint32_t evt)
1653 {
1654 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1655 
1656 	if (lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
1657 		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1658 	return ndlp->nlp_state;
1659 }
1660 
1661 static uint32_t
1662 lpfc_rcv_logo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1663 			  void *arg, uint32_t evt)
1664 {
1665 	struct lpfc_hba *phba = vport->phba;
1666 	struct lpfc_iocbq *cmdiocb;
1667 
1668 	cmdiocb = (struct lpfc_iocbq *) arg;
1669 
1670 	/* software abort outstanding ADISC */
1671 	lpfc_els_abort(phba, ndlp);
1672 
1673 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1674 	return ndlp->nlp_state;
1675 }
1676 
1677 static uint32_t
1678 lpfc_rcv_padisc_adisc_issue(struct lpfc_vport *vport,
1679 			    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 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1687 	return ndlp->nlp_state;
1688 }
1689 
1690 static uint32_t
1691 lpfc_rcv_prlo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1692 			  void *arg, uint32_t evt)
1693 {
1694 	struct lpfc_iocbq *cmdiocb;
1695 
1696 	cmdiocb = (struct lpfc_iocbq *) arg;
1697 
1698 	/* Treat like rcv logo */
1699 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1700 	return ndlp->nlp_state;
1701 }
1702 
1703 static uint32_t
1704 lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
1705 			    struct lpfc_nodelist *ndlp,
1706 			    void *arg, uint32_t evt)
1707 {
1708 	struct lpfc_hba   *phba = vport->phba;
1709 	struct lpfc_iocbq *cmdiocb, *rspiocb;
1710 	ADISC *ap;
1711 	int rc;
1712 	u32 ulp_status;
1713 
1714 	cmdiocb = (struct lpfc_iocbq *) arg;
1715 	rspiocb = cmdiocb->rsp_iocb;
1716 
1717 	ulp_status = get_job_ulpstatus(phba, rspiocb);
1718 
1719 	ap = (ADISC *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1720 
1721 	if ((ulp_status) ||
1722 	    (!lpfc_check_adisc(vport, ndlp, &ap->nodeName, &ap->portName))) {
1723 		/* 1 sec timeout */
1724 		mod_timer(&ndlp->nlp_delayfunc,
1725 			  jiffies + msecs_to_jiffies(1000));
1726 		spin_lock_irq(&ndlp->lock);
1727 		ndlp->nlp_flag |= NLP_DELAY_TMO;
1728 		spin_unlock_irq(&ndlp->lock);
1729 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1730 
1731 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1732 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1733 		lpfc_unreg_rpi(vport, ndlp);
1734 		return ndlp->nlp_state;
1735 	}
1736 
1737 	if (phba->sli_rev == LPFC_SLI_REV4) {
1738 		rc = lpfc_sli4_resume_rpi(ndlp, NULL, NULL);
1739 		if (rc) {
1740 			/* Stay in state and retry. */
1741 			ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1742 			return ndlp->nlp_state;
1743 		}
1744 	}
1745 
1746 	if (ndlp->nlp_type & NLP_FCP_TARGET)
1747 		ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1748 
1749 	if (ndlp->nlp_type & NLP_NVME_TARGET)
1750 		ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1751 
1752 	if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET)) {
1753 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1754 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1755 	} else {
1756 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1757 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1758 	}
1759 
1760 	return ndlp->nlp_state;
1761 }
1762 
1763 static uint32_t
1764 lpfc_device_rm_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1765 			   void *arg, uint32_t evt)
1766 {
1767 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1768 		spin_lock_irq(&ndlp->lock);
1769 		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1770 		spin_unlock_irq(&ndlp->lock);
1771 		return ndlp->nlp_state;
1772 	} else {
1773 		/* software abort outstanding ADISC */
1774 		lpfc_els_abort(vport->phba, ndlp);
1775 
1776 		lpfc_drop_node(vport, ndlp);
1777 		return NLP_STE_FREED_NODE;
1778 	}
1779 }
1780 
1781 static uint32_t
1782 lpfc_device_recov_adisc_issue(struct lpfc_vport *vport,
1783 			      struct lpfc_nodelist *ndlp,
1784 			      void *arg,
1785 			      uint32_t evt)
1786 {
1787 	struct lpfc_hba  *phba = vport->phba;
1788 
1789 	/* Don't do anything that will mess up processing of the
1790 	 * previous RSCN.
1791 	 */
1792 	if (vport->fc_flag & FC_RSCN_DEFERRED)
1793 		return ndlp->nlp_state;
1794 
1795 	/* software abort outstanding ADISC */
1796 	lpfc_els_abort(phba, ndlp);
1797 
1798 	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1799 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1800 	spin_lock_irq(&ndlp->lock);
1801 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1802 	spin_unlock_irq(&ndlp->lock);
1803 	lpfc_disc_set_adisc(vport, ndlp);
1804 	return ndlp->nlp_state;
1805 }
1806 
1807 static uint32_t
1808 lpfc_rcv_plogi_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 
1815 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1816 	return ndlp->nlp_state;
1817 }
1818 
1819 static uint32_t
1820 lpfc_rcv_prli_reglogin_issue(struct lpfc_vport *vport,
1821 			     struct lpfc_nodelist *ndlp,
1822 			     void *arg,
1823 			     uint32_t evt)
1824 {
1825 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1826 	struct ls_rjt     stat;
1827 
1828 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb)) {
1829 		return ndlp->nlp_state;
1830 	}
1831 	if (vport->phba->nvmet_support) {
1832 		/* NVME Target mode.  Handle and respond to the PRLI and
1833 		 * transition to UNMAPPED provided the RPI has completed
1834 		 * registration.
1835 		 */
1836 		if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
1837 			lpfc_rcv_prli(vport, ndlp, cmdiocb);
1838 			lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1839 		} else {
1840 			/* RPI registration has not completed. Reject the PRLI
1841 			 * to prevent an illegal state transition when the
1842 			 * rpi registration does complete.
1843 			 */
1844 			memset(&stat, 0, sizeof(struct ls_rjt));
1845 			stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
1846 			stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1847 			lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
1848 					    ndlp, NULL);
1849 			return ndlp->nlp_state;
1850 		}
1851 	} else {
1852 		/* Initiator mode. */
1853 		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1854 	}
1855 	return ndlp->nlp_state;
1856 }
1857 
1858 static uint32_t
1859 lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
1860 			     struct lpfc_nodelist *ndlp,
1861 			     void *arg,
1862 			     uint32_t evt)
1863 {
1864 	struct lpfc_hba   *phba = vport->phba;
1865 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1866 	LPFC_MBOXQ_t	  *mb;
1867 	LPFC_MBOXQ_t	  *nextmb;
1868 
1869 	cmdiocb = (struct lpfc_iocbq *) arg;
1870 
1871 	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1872 	if ((mb = phba->sli.mbox_active)) {
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 			mb->ctx_ndlp = NULL;
1878 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1879 		}
1880 	}
1881 
1882 	spin_lock_irq(&phba->hbalock);
1883 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1884 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1885 		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
1886 			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1887 			lpfc_nlp_put(ndlp);
1888 			list_del(&mb->list);
1889 			phba->sli.mboxq_cnt--;
1890 			lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_LOCKED);
1891 		}
1892 	}
1893 	spin_unlock_irq(&phba->hbalock);
1894 
1895 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1896 	return ndlp->nlp_state;
1897 }
1898 
1899 static uint32_t
1900 lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport *vport,
1901 			       struct lpfc_nodelist *ndlp,
1902 			       void *arg,
1903 			       uint32_t evt)
1904 {
1905 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1906 
1907 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1908 	return ndlp->nlp_state;
1909 }
1910 
1911 static uint32_t
1912 lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
1913 			     struct lpfc_nodelist *ndlp,
1914 			     void *arg,
1915 			     uint32_t evt)
1916 {
1917 	struct lpfc_iocbq *cmdiocb;
1918 
1919 	cmdiocb = (struct lpfc_iocbq *) arg;
1920 	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1921 	return ndlp->nlp_state;
1922 }
1923 
1924 static uint32_t
1925 lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
1926 				  struct lpfc_nodelist *ndlp,
1927 				  void *arg,
1928 				  uint32_t evt)
1929 {
1930 	struct lpfc_hba *phba = vport->phba;
1931 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1932 	MAILBOX_t *mb = &pmb->u.mb;
1933 	uint32_t did  = mb->un.varWords[1];
1934 
1935 	if (mb->mbxStatus) {
1936 		/* RegLogin failed */
1937 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1938 				 "0246 RegLogin failed Data: x%x x%x x%x x%x "
1939 				 "x%x\n",
1940 				 did, mb->mbxStatus, vport->port_state,
1941 				 mb->un.varRegLogin.vpi,
1942 				 mb->un.varRegLogin.rpi);
1943 		/*
1944 		 * If RegLogin failed due to lack of HBA resources do not
1945 		 * retry discovery.
1946 		 */
1947 		if (mb->mbxStatus == MBXERR_RPI_FULL) {
1948 			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1949 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1950 			return ndlp->nlp_state;
1951 		}
1952 
1953 		/* Put ndlp in npr state set plogi timer for 1 sec */
1954 		mod_timer(&ndlp->nlp_delayfunc,
1955 			  jiffies + msecs_to_jiffies(1000 * 1));
1956 		spin_lock_irq(&ndlp->lock);
1957 		ndlp->nlp_flag |= NLP_DELAY_TMO;
1958 		spin_unlock_irq(&ndlp->lock);
1959 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1960 
1961 		lpfc_issue_els_logo(vport, ndlp, 0);
1962 		return ndlp->nlp_state;
1963 	}
1964 
1965 	/* SLI4 ports have preallocated logical rpis. */
1966 	if (phba->sli_rev < LPFC_SLI_REV4)
1967 		ndlp->nlp_rpi = mb->un.varWords[0];
1968 
1969 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1970 
1971 	/* Only if we are not a fabric nport do we issue PRLI */
1972 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1973 			 "3066 RegLogin Complete on x%x x%x x%x\n",
1974 			 did, ndlp->nlp_type, ndlp->nlp_fc4_type);
1975 	if (!(ndlp->nlp_type & NLP_FABRIC) &&
1976 	    (phba->nvmet_support == 0)) {
1977 		/* The driver supports FCP and NVME concurrently.  If the
1978 		 * ndlp's nlp_fc4_type is still zero, the driver doesn't
1979 		 * know what PRLI to send yet.  Figure that out now and
1980 		 * call PRLI depending on the outcome.
1981 		 */
1982 		if (vport->fc_flag & FC_PT2PT) {
1983 			/* If we are pt2pt, there is no Fabric to determine
1984 			 * the FC4 type of the remote nport. So if NVME
1985 			 * is configured try it.
1986 			 */
1987 			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1988 			if ((!(vport->fc_flag & FC_PT2PT_NO_NVME)) &&
1989 			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
1990 			    vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
1991 				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1992 				/* We need to update the localport also */
1993 				lpfc_nvme_update_localport(vport);
1994 			}
1995 
1996 		} else if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
1997 			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1998 
1999 		} else if (ndlp->nlp_fc4_type == 0) {
2000 			/* If we are only configured for FCP, the driver
2001 			 * should just issue PRLI for FCP. Otherwise issue
2002 			 * GFT_ID to determine if remote port supports NVME.
2003 			 */
2004 			if (vport->cfg_enable_fc4_type != LPFC_ENABLE_FCP) {
2005 				lpfc_ns_cmd(vport, SLI_CTNS_GFT_ID, 0,
2006 					    ndlp->nlp_DID);
2007 				return ndlp->nlp_state;
2008 			}
2009 			ndlp->nlp_fc4_type = NLP_FC4_FCP;
2010 		}
2011 
2012 		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
2013 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2014 		if (lpfc_issue_els_prli(vport, ndlp, 0)) {
2015 			lpfc_issue_els_logo(vport, ndlp, 0);
2016 			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
2017 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2018 		}
2019 	} else {
2020 		if ((vport->fc_flag & FC_PT2PT) && phba->nvmet_support)
2021 			phba->targetport->port_id = vport->fc_myDID;
2022 
2023 		/* Only Fabric ports should transition. NVME target
2024 		 * must complete PRLI.
2025 		 */
2026 		if (ndlp->nlp_type & NLP_FABRIC) {
2027 			ndlp->nlp_fc4_type &= ~NLP_FC4_FCP;
2028 			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
2029 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2030 		}
2031 	}
2032 	return ndlp->nlp_state;
2033 }
2034 
2035 static uint32_t
2036 lpfc_device_rm_reglogin_issue(struct lpfc_vport *vport,
2037 			      struct lpfc_nodelist *ndlp,
2038 			      void *arg,
2039 			      uint32_t evt)
2040 {
2041 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
2042 		spin_lock_irq(&ndlp->lock);
2043 		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
2044 		spin_unlock_irq(&ndlp->lock);
2045 		return ndlp->nlp_state;
2046 	} else {
2047 		lpfc_drop_node(vport, ndlp);
2048 		return NLP_STE_FREED_NODE;
2049 	}
2050 }
2051 
2052 static uint32_t
2053 lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
2054 				 struct lpfc_nodelist *ndlp,
2055 				 void *arg,
2056 				 uint32_t evt)
2057 {
2058 	/* Don't do anything that will mess up processing of the
2059 	 * previous RSCN.
2060 	 */
2061 	if (vport->fc_flag & FC_RSCN_DEFERRED)
2062 		return ndlp->nlp_state;
2063 
2064 	ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
2065 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2066 	spin_lock_irq(&ndlp->lock);
2067 
2068 	/* If we are a target we won't immediately transition into PRLI,
2069 	 * so if REG_LOGIN already completed we don't need to ignore it.
2070 	 */
2071 	if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED) ||
2072 	    !vport->phba->nvmet_support)
2073 		ndlp->nlp_flag |= NLP_IGNR_REG_CMPL;
2074 
2075 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2076 	spin_unlock_irq(&ndlp->lock);
2077 	lpfc_disc_set_adisc(vport, ndlp);
2078 	return ndlp->nlp_state;
2079 }
2080 
2081 static uint32_t
2082 lpfc_rcv_plogi_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2083 			  void *arg, uint32_t evt)
2084 {
2085 	struct lpfc_iocbq *cmdiocb;
2086 
2087 	cmdiocb = (struct lpfc_iocbq *) arg;
2088 
2089 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2090 	return ndlp->nlp_state;
2091 }
2092 
2093 static uint32_t
2094 lpfc_rcv_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2095 			 void *arg, uint32_t evt)
2096 {
2097 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2098 
2099 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
2100 		return ndlp->nlp_state;
2101 	lpfc_rcv_prli(vport, ndlp, cmdiocb);
2102 	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2103 	return ndlp->nlp_state;
2104 }
2105 
2106 static uint32_t
2107 lpfc_rcv_logo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2108 			 void *arg, uint32_t evt)
2109 {
2110 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2111 
2112 	/* Software abort outstanding PRLI before sending acc */
2113 	lpfc_els_abort(vport->phba, ndlp);
2114 
2115 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2116 	return ndlp->nlp_state;
2117 }
2118 
2119 static uint32_t
2120 lpfc_rcv_padisc_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2121 			   void *arg, uint32_t evt)
2122 {
2123 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2124 
2125 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2126 	return ndlp->nlp_state;
2127 }
2128 
2129 /* This routine is envoked when we rcv a PRLO request from a nport
2130  * we are logged into.  We should send back a PRLO rsp setting the
2131  * appropriate bits.
2132  * NEXT STATE = PRLI_ISSUE
2133  */
2134 static uint32_t
2135 lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2136 			 void *arg, uint32_t evt)
2137 {
2138 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2139 
2140 	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2141 	return ndlp->nlp_state;
2142 }
2143 
2144 static uint32_t
2145 lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2146 			  void *arg, uint32_t evt)
2147 {
2148 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2149 	struct lpfc_hba   *phba = vport->phba;
2150 	PRLI *npr;
2151 	struct lpfc_nvme_prli *nvpr;
2152 	void *temp_ptr;
2153 	u32 ulp_status;
2154 	bool acc_imode_sps = false;
2155 
2156 	cmdiocb = (struct lpfc_iocbq *) arg;
2157 	rspiocb = cmdiocb->rsp_iocb;
2158 
2159 	ulp_status = get_job_ulpstatus(phba, rspiocb);
2160 
2161 	/* A solicited PRLI is either FCP or NVME.  The PRLI cmd/rsp
2162 	 * format is different so NULL the two PRLI types so that the
2163 	 * driver correctly gets the correct context.
2164 	 */
2165 	npr = NULL;
2166 	nvpr = NULL;
2167 	temp_ptr = lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
2168 	if (cmdiocb->cmd_flag & LPFC_PRLI_FCP_REQ)
2169 		npr = (PRLI *) temp_ptr;
2170 	else if (cmdiocb->cmd_flag & LPFC_PRLI_NVME_REQ)
2171 		nvpr = (struct lpfc_nvme_prli *) temp_ptr;
2172 
2173 	if (ulp_status) {
2174 		if ((vport->port_type == LPFC_NPIV_PORT) &&
2175 		    vport->cfg_restrict_login) {
2176 			goto out;
2177 		}
2178 
2179 		/* Adjust the nlp_type accordingly if the PRLI failed */
2180 		if (npr)
2181 			ndlp->nlp_fc4_type &= ~NLP_FC4_FCP;
2182 		if (nvpr)
2183 			ndlp->nlp_fc4_type &= ~NLP_FC4_NVME;
2184 
2185 		/* We can't set the DSM state till BOTH PRLIs complete */
2186 		goto out_err;
2187 	}
2188 
2189 	if (npr && npr->prliType == PRLI_FCP_TYPE) {
2190 		lpfc_printf_vlog(vport, KERN_INFO,
2191 				 LOG_ELS | LOG_NODE | LOG_DISCOVERY,
2192 				 "6028 FCP NPR PRLI Cmpl Init %d Target %d "
2193 				 "EIP %d AccCode x%x\n",
2194 				 npr->initiatorFunc, npr->targetFunc,
2195 				 npr->estabImagePair, npr->acceptRspCode);
2196 
2197 		if (npr->acceptRspCode == PRLI_INV_SRV_PARM) {
2198 			/* Strict initiators don't establish an image pair. */
2199 			if (npr->initiatorFunc && !npr->targetFunc &&
2200 			    !npr->estabImagePair)
2201 				acc_imode_sps = true;
2202 		}
2203 
2204 		if (npr->acceptRspCode == PRLI_REQ_EXECUTED || acc_imode_sps) {
2205 			if (npr->initiatorFunc)
2206 				ndlp->nlp_type |= NLP_FCP_INITIATOR;
2207 			if (npr->targetFunc) {
2208 				ndlp->nlp_type |= NLP_FCP_TARGET;
2209 				if (npr->writeXferRdyDis)
2210 					ndlp->nlp_flag |= NLP_FIRSTBURST;
2211 			}
2212 			if (npr->Retry)
2213 				ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
2214 		}
2215 	} else if (nvpr &&
2216 		   (bf_get_be32(prli_acc_rsp_code, nvpr) ==
2217 		    PRLI_REQ_EXECUTED) &&
2218 		   (bf_get_be32(prli_type_code, nvpr) ==
2219 		    PRLI_NVME_TYPE)) {
2220 
2221 		/* Complete setting up the remote ndlp personality. */
2222 		if (bf_get_be32(prli_init, nvpr))
2223 			ndlp->nlp_type |= NLP_NVME_INITIATOR;
2224 
2225 		if (phba->nsler && bf_get_be32(prli_nsler, nvpr) &&
2226 		    bf_get_be32(prli_conf, nvpr))
2227 
2228 			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;
2229 		else
2230 			ndlp->nlp_nvme_info &= ~NLP_NVME_NSLER;
2231 
2232 		/* Target driver cannot solicit NVME FB. */
2233 		if (bf_get_be32(prli_tgt, nvpr)) {
2234 			/* Complete the nvme target roles.  The transport
2235 			 * needs to know if the rport is capable of
2236 			 * discovery in addition to its role.
2237 			 */
2238 			ndlp->nlp_type |= NLP_NVME_TARGET;
2239 			if (bf_get_be32(prli_disc, nvpr))
2240 				ndlp->nlp_type |= NLP_NVME_DISCOVERY;
2241 
2242 			/*
2243 			 * If prli_fba is set, the Target supports FirstBurst.
2244 			 * If prli_fb_sz is 0, the FirstBurst size is unlimited,
2245 			 * otherwise it defines the actual size supported by
2246 			 * the NVME Target.
2247 			 */
2248 			if ((bf_get_be32(prli_fba, nvpr) == 1) &&
2249 			    (phba->cfg_nvme_enable_fb) &&
2250 			    (!phba->nvmet_support)) {
2251 				/* Both sides support FB. The target's first
2252 				 * burst size is a 512 byte encoded value.
2253 				 */
2254 				ndlp->nlp_flag |= NLP_FIRSTBURST;
2255 				ndlp->nvme_fb_size = bf_get_be32(prli_fb_sz,
2256 								 nvpr);
2257 
2258 				/* Expressed in units of 512 bytes */
2259 				if (ndlp->nvme_fb_size)
2260 					ndlp->nvme_fb_size <<=
2261 						LPFC_NVME_FB_SHIFT;
2262 				else
2263 					ndlp->nvme_fb_size = LPFC_NVME_MAX_FB;
2264 			}
2265 		}
2266 
2267 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2268 				 "6029 NVME PRLI Cmpl w1 x%08x "
2269 				 "w4 x%08x w5 x%08x flag x%x, "
2270 				 "fcp_info x%x nlp_type x%x\n",
2271 				 be32_to_cpu(nvpr->word1),
2272 				 be32_to_cpu(nvpr->word4),
2273 				 be32_to_cpu(nvpr->word5),
2274 				 ndlp->nlp_flag, ndlp->nlp_fcp_info,
2275 				 ndlp->nlp_type);
2276 	}
2277 	if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
2278 	    (vport->port_type == LPFC_NPIV_PORT) &&
2279 	     vport->cfg_restrict_login) {
2280 out:
2281 		spin_lock_irq(&ndlp->lock);
2282 		ndlp->nlp_flag |= NLP_TARGET_REMOVE;
2283 		spin_unlock_irq(&ndlp->lock);
2284 		lpfc_issue_els_logo(vport, ndlp, 0);
2285 
2286 		ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
2287 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2288 		return ndlp->nlp_state;
2289 	}
2290 
2291 out_err:
2292 	/* The ndlp state cannot move to MAPPED or UNMAPPED before all PRLIs
2293 	 * are complete.
2294 	 */
2295 	if (ndlp->fc4_prli_sent == 0) {
2296 		ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
2297 		if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET))
2298 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
2299 		else if (ndlp->nlp_type &
2300 			 (NLP_FCP_INITIATOR | NLP_NVME_INITIATOR))
2301 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2302 	} else
2303 		lpfc_printf_vlog(vport,
2304 				 KERN_INFO, LOG_ELS,
2305 				 "3067 PRLI's still outstanding "
2306 				 "on x%06x - count %d, Pend Node Mode "
2307 				 "transition...\n",
2308 				 ndlp->nlp_DID, ndlp->fc4_prli_sent);
2309 
2310 	return ndlp->nlp_state;
2311 }
2312 
2313 /*! lpfc_device_rm_prli_issue
2314  *
2315  * \pre
2316  * \post
2317  * \param   phba
2318  * \param   ndlp
2319  * \param   arg
2320  * \param   evt
2321  * \return  uint32_t
2322  *
2323  * \b Description:
2324  *    This routine is envoked when we a request to remove a nport we are in the
2325  *    process of PRLIing. We should software abort outstanding prli, unreg
2326  *    login, send a logout. We will change node state to UNUSED_NODE, put it
2327  *    on plogi list so it can be freed when LOGO completes.
2328  *
2329  */
2330 
2331 static uint32_t
2332 lpfc_device_rm_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2333 			  void *arg, uint32_t evt)
2334 {
2335 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
2336 		spin_lock_irq(&ndlp->lock);
2337 		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
2338 		spin_unlock_irq(&ndlp->lock);
2339 		return ndlp->nlp_state;
2340 	} else {
2341 		/* software abort outstanding PLOGI */
2342 		lpfc_els_abort(vport->phba, ndlp);
2343 
2344 		lpfc_drop_node(vport, ndlp);
2345 		return NLP_STE_FREED_NODE;
2346 	}
2347 }
2348 
2349 
2350 /*! lpfc_device_recov_prli_issue
2351  *
2352  * \pre
2353  * \post
2354  * \param   phba
2355  * \param   ndlp
2356  * \param   arg
2357  * \param   evt
2358  * \return  uint32_t
2359  *
2360  * \b Description:
2361  *    The routine is envoked when the state of a device is unknown, like
2362  *    during a link down. We should remove the nodelist entry from the
2363  *    unmapped list, issue a UNREG_LOGIN, do a software abort of the
2364  *    outstanding PRLI command, then free the node entry.
2365  */
2366 static uint32_t
2367 lpfc_device_recov_prli_issue(struct lpfc_vport *vport,
2368 			     struct lpfc_nodelist *ndlp,
2369 			     void *arg,
2370 			     uint32_t evt)
2371 {
2372 	struct lpfc_hba  *phba = vport->phba;
2373 
2374 	/* Don't do anything that will mess up processing of the
2375 	 * previous RSCN.
2376 	 */
2377 	if (vport->fc_flag & FC_RSCN_DEFERRED)
2378 		return ndlp->nlp_state;
2379 
2380 	/* software abort outstanding PRLI */
2381 	lpfc_els_abort(phba, ndlp);
2382 
2383 	ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
2384 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2385 	spin_lock_irq(&ndlp->lock);
2386 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2387 	spin_unlock_irq(&ndlp->lock);
2388 	lpfc_disc_set_adisc(vport, ndlp);
2389 	return ndlp->nlp_state;
2390 }
2391 
2392 static uint32_t
2393 lpfc_rcv_plogi_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2394 			  void *arg, uint32_t evt)
2395 {
2396 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2397 	struct ls_rjt     stat;
2398 
2399 	memset(&stat, 0, sizeof(struct ls_rjt));
2400 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2401 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2402 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2403 	return ndlp->nlp_state;
2404 }
2405 
2406 static uint32_t
2407 lpfc_rcv_prli_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2408 			 void *arg, uint32_t evt)
2409 {
2410 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2411 	struct ls_rjt     stat;
2412 
2413 	memset(&stat, 0, sizeof(struct ls_rjt));
2414 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2415 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2416 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2417 	return ndlp->nlp_state;
2418 }
2419 
2420 static uint32_t
2421 lpfc_rcv_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2422 			 void *arg, uint32_t evt)
2423 {
2424 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2425 
2426 	spin_lock_irq(&ndlp->lock);
2427 	ndlp->nlp_flag |= NLP_LOGO_ACC;
2428 	spin_unlock_irq(&ndlp->lock);
2429 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2430 	return ndlp->nlp_state;
2431 }
2432 
2433 static uint32_t
2434 lpfc_rcv_padisc_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2435 			   void *arg, uint32_t evt)
2436 {
2437 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2438 	struct ls_rjt     stat;
2439 
2440 	memset(&stat, 0, sizeof(struct ls_rjt));
2441 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2442 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2443 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2444 	return ndlp->nlp_state;
2445 }
2446 
2447 static uint32_t
2448 lpfc_rcv_prlo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2449 			 void *arg, uint32_t evt)
2450 {
2451 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2452 	struct ls_rjt     stat;
2453 
2454 	memset(&stat, 0, sizeof(struct ls_rjt));
2455 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2456 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2457 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2458 	return ndlp->nlp_state;
2459 }
2460 
2461 static uint32_t
2462 lpfc_cmpl_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2463 			  void *arg, uint32_t evt)
2464 {
2465 	ndlp->nlp_prev_state = NLP_STE_LOGO_ISSUE;
2466 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2467 	spin_lock_irq(&ndlp->lock);
2468 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2469 	spin_unlock_irq(&ndlp->lock);
2470 	lpfc_disc_set_adisc(vport, ndlp);
2471 	return ndlp->nlp_state;
2472 }
2473 
2474 static uint32_t
2475 lpfc_device_rm_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2476 			  void *arg, uint32_t evt)
2477 {
2478 	/*
2479 	 * DevLoss has timed out and is calling for Device Remove.
2480 	 * In this case, abort the LOGO and cleanup the ndlp
2481 	 */
2482 
2483 	lpfc_unreg_rpi(vport, ndlp);
2484 	/* software abort outstanding PLOGI */
2485 	lpfc_els_abort(vport->phba, ndlp);
2486 	lpfc_drop_node(vport, ndlp);
2487 	return NLP_STE_FREED_NODE;
2488 }
2489 
2490 static uint32_t
2491 lpfc_device_recov_logo_issue(struct lpfc_vport *vport,
2492 			     struct lpfc_nodelist *ndlp,
2493 			     void *arg, uint32_t evt)
2494 {
2495 	/*
2496 	 * Device Recovery events have no meaning for a node with a LOGO
2497 	 * outstanding.  The LOGO has to complete first and handle the
2498 	 * node from that point.
2499 	 */
2500 	return ndlp->nlp_state;
2501 }
2502 
2503 static uint32_t
2504 lpfc_rcv_plogi_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2505 			  void *arg, uint32_t evt)
2506 {
2507 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2508 
2509 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2510 	return ndlp->nlp_state;
2511 }
2512 
2513 static uint32_t
2514 lpfc_rcv_prli_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2515 			 void *arg, uint32_t evt)
2516 {
2517 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2518 
2519 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
2520 		return ndlp->nlp_state;
2521 
2522 	lpfc_rcv_prli(vport, ndlp, cmdiocb);
2523 	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2524 	return ndlp->nlp_state;
2525 }
2526 
2527 static uint32_t
2528 lpfc_rcv_logo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2529 			 void *arg, uint32_t evt)
2530 {
2531 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2532 
2533 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2534 	return ndlp->nlp_state;
2535 }
2536 
2537 static uint32_t
2538 lpfc_rcv_padisc_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2539 			   void *arg, uint32_t evt)
2540 {
2541 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2542 
2543 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2544 	return ndlp->nlp_state;
2545 }
2546 
2547 static uint32_t
2548 lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2549 			 void *arg, uint32_t evt)
2550 {
2551 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2552 
2553 	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2554 	return ndlp->nlp_state;
2555 }
2556 
2557 static uint32_t
2558 lpfc_device_rm_unmap_node(struct lpfc_vport *vport,
2559 			  struct lpfc_nodelist *ndlp,
2560 			  void *arg,
2561 			  uint32_t evt)
2562 {
2563 	lpfc_drop_node(vport, ndlp);
2564 	return NLP_STE_FREED_NODE;
2565 }
2566 
2567 static uint32_t
2568 lpfc_device_recov_unmap_node(struct lpfc_vport *vport,
2569 			     struct lpfc_nodelist *ndlp,
2570 			     void *arg,
2571 			     uint32_t evt)
2572 {
2573 	ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
2574 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2575 	spin_lock_irq(&ndlp->lock);
2576 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2577 	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
2578 	spin_unlock_irq(&ndlp->lock);
2579 	lpfc_disc_set_adisc(vport, ndlp);
2580 
2581 	return ndlp->nlp_state;
2582 }
2583 
2584 static uint32_t
2585 lpfc_rcv_plogi_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2586 			   void *arg, uint32_t evt)
2587 {
2588 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2589 
2590 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2591 	return ndlp->nlp_state;
2592 }
2593 
2594 static uint32_t
2595 lpfc_rcv_prli_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2596 			  void *arg, uint32_t evt)
2597 {
2598 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2599 
2600 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
2601 		return ndlp->nlp_state;
2602 	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2603 	return ndlp->nlp_state;
2604 }
2605 
2606 static uint32_t
2607 lpfc_rcv_logo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2608 			  void *arg, uint32_t evt)
2609 {
2610 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2611 
2612 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2613 	return ndlp->nlp_state;
2614 }
2615 
2616 static uint32_t
2617 lpfc_rcv_padisc_mapped_node(struct lpfc_vport *vport,
2618 			    struct lpfc_nodelist *ndlp,
2619 			    void *arg, uint32_t evt)
2620 {
2621 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2622 
2623 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2624 	return ndlp->nlp_state;
2625 }
2626 
2627 static uint32_t
2628 lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2629 			  void *arg, uint32_t evt)
2630 {
2631 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2632 
2633 	/* flush the target */
2634 	lpfc_sli_abort_iocb(vport, ndlp->nlp_sid, 0, LPFC_CTX_TGT);
2635 
2636 	/* Treat like rcv logo */
2637 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
2638 	return ndlp->nlp_state;
2639 }
2640 
2641 static uint32_t
2642 lpfc_device_recov_mapped_node(struct lpfc_vport *vport,
2643 			      struct lpfc_nodelist *ndlp,
2644 			      void *arg,
2645 			      uint32_t evt)
2646 {
2647 	lpfc_disc_set_adisc(vport, ndlp);
2648 
2649 	ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
2650 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2651 	spin_lock_irq(&ndlp->lock);
2652 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2653 	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
2654 	spin_unlock_irq(&ndlp->lock);
2655 	return ndlp->nlp_state;
2656 }
2657 
2658 static uint32_t
2659 lpfc_rcv_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2660 			void *arg, uint32_t evt)
2661 {
2662 	struct lpfc_iocbq *cmdiocb  = (struct lpfc_iocbq *) arg;
2663 
2664 	/* Ignore PLOGI if we have an outstanding LOGO */
2665 	if (ndlp->nlp_flag & (NLP_LOGO_SND | NLP_LOGO_ACC))
2666 		return ndlp->nlp_state;
2667 	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
2668 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
2669 		spin_lock_irq(&ndlp->lock);
2670 		ndlp->nlp_flag &= ~(NLP_NPR_ADISC | NLP_NPR_2B_DISC);
2671 		spin_unlock_irq(&ndlp->lock);
2672 	} else if (!(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
2673 		/* send PLOGI immediately, move to PLOGI issue state */
2674 		if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2675 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2676 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2677 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2678 		}
2679 	}
2680 	return ndlp->nlp_state;
2681 }
2682 
2683 static uint32_t
2684 lpfc_rcv_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2685 		       void *arg, uint32_t evt)
2686 {
2687 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2688 	struct ls_rjt     stat;
2689 
2690 	memset(&stat, 0, sizeof (struct ls_rjt));
2691 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2692 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2693 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2694 
2695 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2696 		/*
2697 		 * ADISC nodes will be handled in regular discovery path after
2698 		 * receiving response from NS.
2699 		 *
2700 		 * For other nodes, Send PLOGI to trigger an implicit LOGO.
2701 		 */
2702 		if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
2703 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2704 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2705 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2706 		}
2707 	}
2708 	return ndlp->nlp_state;
2709 }
2710 
2711 static uint32_t
2712 lpfc_rcv_logo_npr_node(struct lpfc_vport *vport,  struct lpfc_nodelist *ndlp,
2713 		       void *arg, uint32_t evt)
2714 {
2715 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2716 
2717 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2718 	return ndlp->nlp_state;
2719 }
2720 
2721 static uint32_t
2722 lpfc_rcv_padisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2723 			 void *arg, uint32_t evt)
2724 {
2725 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2726 
2727 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2728 	/*
2729 	 * Do not start discovery if discovery is about to start
2730 	 * or discovery in progress for this node. Starting discovery
2731 	 * here will affect the counting of discovery threads.
2732 	 */
2733 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
2734 	    !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
2735 		/*
2736 		 * ADISC nodes will be handled in regular discovery path after
2737 		 * receiving response from NS.
2738 		 *
2739 		 * For other nodes, Send PLOGI to trigger an implicit LOGO.
2740 		 */
2741 		if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
2742 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2743 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2744 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2745 		}
2746 	}
2747 	return ndlp->nlp_state;
2748 }
2749 
2750 static uint32_t
2751 lpfc_rcv_prlo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2752 		       void *arg, uint32_t evt)
2753 {
2754 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2755 
2756 	spin_lock_irq(&ndlp->lock);
2757 	ndlp->nlp_flag |= NLP_LOGO_ACC;
2758 	spin_unlock_irq(&ndlp->lock);
2759 
2760 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2761 
2762 	if ((ndlp->nlp_flag & NLP_DELAY_TMO) == 0) {
2763 		mod_timer(&ndlp->nlp_delayfunc,
2764 			  jiffies + msecs_to_jiffies(1000 * 1));
2765 		spin_lock_irq(&ndlp->lock);
2766 		ndlp->nlp_flag |= NLP_DELAY_TMO;
2767 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2768 		spin_unlock_irq(&ndlp->lock);
2769 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
2770 	} else {
2771 		spin_lock_irq(&ndlp->lock);
2772 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2773 		spin_unlock_irq(&ndlp->lock);
2774 	}
2775 	return ndlp->nlp_state;
2776 }
2777 
2778 static uint32_t
2779 lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2780 			 void *arg, uint32_t evt)
2781 {
2782 	struct lpfc_hba *phba = vport->phba;
2783 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2784 	u32 ulp_status;
2785 
2786 	cmdiocb = (struct lpfc_iocbq *) arg;
2787 	rspiocb = cmdiocb->rsp_iocb;
2788 
2789 	ulp_status = get_job_ulpstatus(phba, rspiocb);
2790 
2791 	if (ulp_status)
2792 		return NLP_STE_FREED_NODE;
2793 
2794 	return ndlp->nlp_state;
2795 }
2796 
2797 static uint32_t
2798 lpfc_cmpl_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2799 			void *arg, uint32_t evt)
2800 {
2801 	struct lpfc_hba *phba = vport->phba;
2802 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2803 	u32 ulp_status;
2804 
2805 	cmdiocb = (struct lpfc_iocbq *) arg;
2806 	rspiocb = cmdiocb->rsp_iocb;
2807 
2808 	ulp_status = get_job_ulpstatus(phba, rspiocb);
2809 
2810 	if (ulp_status && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
2811 		lpfc_drop_node(vport, ndlp);
2812 		return NLP_STE_FREED_NODE;
2813 	}
2814 	return ndlp->nlp_state;
2815 }
2816 
2817 static uint32_t
2818 lpfc_cmpl_logo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2819 			void *arg, uint32_t evt)
2820 {
2821 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2822 
2823 	/* For the fabric port just clear the fc flags. */
2824 	if (ndlp->nlp_DID == Fabric_DID) {
2825 		spin_lock_irq(shost->host_lock);
2826 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2827 		spin_unlock_irq(shost->host_lock);
2828 	}
2829 	lpfc_unreg_rpi(vport, ndlp);
2830 	return ndlp->nlp_state;
2831 }
2832 
2833 static uint32_t
2834 lpfc_cmpl_adisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2835 			 void *arg, uint32_t evt)
2836 {
2837 	struct lpfc_hba *phba = vport->phba;
2838 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2839 	u32 ulp_status;
2840 
2841 	cmdiocb = (struct lpfc_iocbq *) arg;
2842 	rspiocb = cmdiocb->rsp_iocb;
2843 
2844 	ulp_status = get_job_ulpstatus(phba, rspiocb);
2845 
2846 	if (ulp_status && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
2847 		lpfc_drop_node(vport, ndlp);
2848 		return NLP_STE_FREED_NODE;
2849 	}
2850 	return ndlp->nlp_state;
2851 }
2852 
2853 static uint32_t
2854 lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
2855 			    struct lpfc_nodelist *ndlp,
2856 			    void *arg, uint32_t evt)
2857 {
2858 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
2859 	MAILBOX_t    *mb = &pmb->u.mb;
2860 
2861 	if (!mb->mbxStatus) {
2862 		/* SLI4 ports have preallocated logical rpis. */
2863 		if (vport->phba->sli_rev < LPFC_SLI_REV4)
2864 			ndlp->nlp_rpi = mb->un.varWords[0];
2865 		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
2866 		if (ndlp->nlp_flag & NLP_LOGO_ACC) {
2867 			lpfc_unreg_rpi(vport, ndlp);
2868 		}
2869 	} else {
2870 		if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
2871 			lpfc_drop_node(vport, ndlp);
2872 			return NLP_STE_FREED_NODE;
2873 		}
2874 	}
2875 	return ndlp->nlp_state;
2876 }
2877 
2878 static uint32_t
2879 lpfc_device_rm_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2880 			void *arg, uint32_t evt)
2881 {
2882 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
2883 		spin_lock_irq(&ndlp->lock);
2884 		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
2885 		spin_unlock_irq(&ndlp->lock);
2886 		return ndlp->nlp_state;
2887 	}
2888 	lpfc_drop_node(vport, ndlp);
2889 	return NLP_STE_FREED_NODE;
2890 }
2891 
2892 static uint32_t
2893 lpfc_device_recov_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2894 			   void *arg, uint32_t evt)
2895 {
2896 	/* Don't do anything that will mess up processing of the
2897 	 * previous RSCN.
2898 	 */
2899 	if (vport->fc_flag & FC_RSCN_DEFERRED)
2900 		return ndlp->nlp_state;
2901 
2902 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
2903 	spin_lock_irq(&ndlp->lock);
2904 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2905 	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
2906 	spin_unlock_irq(&ndlp->lock);
2907 	return ndlp->nlp_state;
2908 }
2909 
2910 
2911 /* This next section defines the NPort Discovery State Machine */
2912 
2913 /* There are 4 different double linked lists nodelist entries can reside on.
2914  * The plogi list and adisc list are used when Link Up discovery or RSCN
2915  * processing is needed. Each list holds the nodes that we will send PLOGI
2916  * or ADISC on. These lists will keep track of what nodes will be effected
2917  * by an RSCN, or a Link Up (Typically, all nodes are effected on Link Up).
2918  * The unmapped_list will contain all nodes that we have successfully logged
2919  * into at the Fibre Channel level. The mapped_list will contain all nodes
2920  * that are mapped FCP targets.
2921  */
2922 /*
2923  * The bind list is a list of undiscovered (potentially non-existent) nodes
2924  * that we have saved binding information on. This information is used when
2925  * nodes transition from the unmapped to the mapped list.
2926  */
2927 /* For UNUSED_NODE state, the node has just been allocated .
2928  * For PLOGI_ISSUE and REG_LOGIN_ISSUE, the node is on
2929  * the PLOGI list. For REG_LOGIN_COMPL, the node is taken off the PLOGI list
2930  * and put on the unmapped list. For ADISC processing, the node is taken off
2931  * the ADISC list and placed on either the mapped or unmapped list (depending
2932  * on its previous state). Once on the unmapped list, a PRLI is issued and the
2933  * state changed to PRLI_ISSUE. When the PRLI completion occurs, the state is
2934  * changed to UNMAPPED_NODE. If the completion indicates a mapped
2935  * node, the node is taken off the unmapped list. The binding list is checked
2936  * for a valid binding, or a binding is automatically assigned. If binding
2937  * assignment is unsuccessful, the node is left on the unmapped list. If
2938  * binding assignment is successful, the associated binding list entry (if
2939  * any) is removed, and the node is placed on the mapped list.
2940  */
2941 /*
2942  * For a Link Down, all nodes on the ADISC, PLOGI, unmapped or mapped
2943  * lists will receive a DEVICE_RECOVERY event. If the linkdown or devloss timers
2944  * expire, all effected nodes will receive a DEVICE_RM event.
2945  */
2946 /*
2947  * For a Link Up or RSCN, all nodes will move from the mapped / unmapped lists
2948  * to either the ADISC or PLOGI list.  After a Nameserver query or ALPA loopmap
2949  * check, additional nodes may be added or removed (via DEVICE_RM) to / from
2950  * the PLOGI or ADISC lists. Once the PLOGI and ADISC lists are populated,
2951  * we will first process the ADISC list.  32 entries are processed initially and
2952  * ADISC is initited for each one.  Completions / Events for each node are
2953  * funnelled thru the state machine.  As each node finishes ADISC processing, it
2954  * starts ADISC for any nodes waiting for ADISC processing. If no nodes are
2955  * waiting, and the ADISC list count is identically 0, then we are done. For
2956  * Link Up discovery, since all nodes on the PLOGI list are UNREG_LOGIN'ed, we
2957  * can issue a CLEAR_LA and reenable Link Events. Next we will process the PLOGI
2958  * list.  32 entries are processed initially and PLOGI is initited for each one.
2959  * Completions / Events for each node are funnelled thru the state machine.  As
2960  * each node finishes PLOGI processing, it starts PLOGI for any nodes waiting
2961  * for PLOGI processing. If no nodes are waiting, and the PLOGI list count is
2962  * indentically 0, then we are done. We have now completed discovery / RSCN
2963  * handling. Upon completion, ALL nodes should be on either the mapped or
2964  * unmapped lists.
2965  */
2966 
2967 static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
2968      (struct lpfc_vport *, struct lpfc_nodelist *, void *, uint32_t) = {
2969 	/* Action routine                  Event       Current State  */
2970 	lpfc_rcv_plogi_unused_node,	/* RCV_PLOGI   UNUSED_NODE    */
2971 	lpfc_rcv_els_unused_node,	/* RCV_PRLI        */
2972 	lpfc_rcv_logo_unused_node,	/* RCV_LOGO        */
2973 	lpfc_rcv_els_unused_node,	/* RCV_ADISC       */
2974 	lpfc_rcv_els_unused_node,	/* RCV_PDISC       */
2975 	lpfc_rcv_els_unused_node,	/* RCV_PRLO        */
2976 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
2977 	lpfc_disc_illegal,		/* CMPL_PRLI       */
2978 	lpfc_cmpl_logo_unused_node,	/* CMPL_LOGO       */
2979 	lpfc_disc_illegal,		/* CMPL_ADISC      */
2980 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
2981 	lpfc_device_rm_unused_node,	/* DEVICE_RM       */
2982 	lpfc_device_recov_unused_node,	/* DEVICE_RECOVERY */
2983 
2984 	lpfc_rcv_plogi_plogi_issue,	/* RCV_PLOGI   PLOGI_ISSUE    */
2985 	lpfc_rcv_prli_plogi_issue,	/* RCV_PRLI        */
2986 	lpfc_rcv_logo_plogi_issue,	/* RCV_LOGO        */
2987 	lpfc_rcv_els_plogi_issue,	/* RCV_ADISC       */
2988 	lpfc_rcv_els_plogi_issue,	/* RCV_PDISC       */
2989 	lpfc_rcv_els_plogi_issue,	/* RCV_PRLO        */
2990 	lpfc_cmpl_plogi_plogi_issue,	/* CMPL_PLOGI      */
2991 	lpfc_disc_illegal,		/* CMPL_PRLI       */
2992 	lpfc_cmpl_logo_plogi_issue,	/* CMPL_LOGO       */
2993 	lpfc_disc_illegal,		/* CMPL_ADISC      */
2994 	lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
2995 	lpfc_device_rm_plogi_issue,	/* DEVICE_RM       */
2996 	lpfc_device_recov_plogi_issue,	/* DEVICE_RECOVERY */
2997 
2998 	lpfc_rcv_plogi_adisc_issue,	/* RCV_PLOGI   ADISC_ISSUE    */
2999 	lpfc_rcv_prli_adisc_issue,	/* RCV_PRLI        */
3000 	lpfc_rcv_logo_adisc_issue,	/* RCV_LOGO        */
3001 	lpfc_rcv_padisc_adisc_issue,	/* RCV_ADISC       */
3002 	lpfc_rcv_padisc_adisc_issue,	/* RCV_PDISC       */
3003 	lpfc_rcv_prlo_adisc_issue,	/* RCV_PRLO        */
3004 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
3005 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3006 	lpfc_disc_illegal,		/* CMPL_LOGO       */
3007 	lpfc_cmpl_adisc_adisc_issue,	/* CMPL_ADISC      */
3008 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3009 	lpfc_device_rm_adisc_issue,	/* DEVICE_RM       */
3010 	lpfc_device_recov_adisc_issue,	/* DEVICE_RECOVERY */
3011 
3012 	lpfc_rcv_plogi_reglogin_issue,	/* RCV_PLOGI  REG_LOGIN_ISSUE */
3013 	lpfc_rcv_prli_reglogin_issue,	/* RCV_PLOGI       */
3014 	lpfc_rcv_logo_reglogin_issue,	/* RCV_LOGO        */
3015 	lpfc_rcv_padisc_reglogin_issue,	/* RCV_ADISC       */
3016 	lpfc_rcv_padisc_reglogin_issue,	/* RCV_PDISC       */
3017 	lpfc_rcv_prlo_reglogin_issue,	/* RCV_PRLO        */
3018 	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
3019 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3020 	lpfc_disc_illegal,		/* CMPL_LOGO       */
3021 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3022 	lpfc_cmpl_reglogin_reglogin_issue,/* CMPL_REG_LOGIN  */
3023 	lpfc_device_rm_reglogin_issue,	/* DEVICE_RM       */
3024 	lpfc_device_recov_reglogin_issue,/* DEVICE_RECOVERY */
3025 
3026 	lpfc_rcv_plogi_prli_issue,	/* RCV_PLOGI   PRLI_ISSUE     */
3027 	lpfc_rcv_prli_prli_issue,	/* RCV_PRLI        */
3028 	lpfc_rcv_logo_prli_issue,	/* RCV_LOGO        */
3029 	lpfc_rcv_padisc_prli_issue,	/* RCV_ADISC       */
3030 	lpfc_rcv_padisc_prli_issue,	/* RCV_PDISC       */
3031 	lpfc_rcv_prlo_prli_issue,	/* RCV_PRLO        */
3032 	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
3033 	lpfc_cmpl_prli_prli_issue,	/* CMPL_PRLI       */
3034 	lpfc_disc_illegal,		/* CMPL_LOGO       */
3035 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3036 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3037 	lpfc_device_rm_prli_issue,	/* DEVICE_RM       */
3038 	lpfc_device_recov_prli_issue,	/* DEVICE_RECOVERY */
3039 
3040 	lpfc_rcv_plogi_logo_issue,	/* RCV_PLOGI   LOGO_ISSUE     */
3041 	lpfc_rcv_prli_logo_issue,	/* RCV_PRLI        */
3042 	lpfc_rcv_logo_logo_issue,	/* RCV_LOGO        */
3043 	lpfc_rcv_padisc_logo_issue,	/* RCV_ADISC       */
3044 	lpfc_rcv_padisc_logo_issue,	/* RCV_PDISC       */
3045 	lpfc_rcv_prlo_logo_issue,	/* RCV_PRLO        */
3046 	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
3047 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3048 	lpfc_cmpl_logo_logo_issue,	/* CMPL_LOGO       */
3049 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3050 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3051 	lpfc_device_rm_logo_issue,	/* DEVICE_RM       */
3052 	lpfc_device_recov_logo_issue,	/* DEVICE_RECOVERY */
3053 
3054 	lpfc_rcv_plogi_unmap_node,	/* RCV_PLOGI   UNMAPPED_NODE  */
3055 	lpfc_rcv_prli_unmap_node,	/* RCV_PRLI        */
3056 	lpfc_rcv_logo_unmap_node,	/* RCV_LOGO        */
3057 	lpfc_rcv_padisc_unmap_node,	/* RCV_ADISC       */
3058 	lpfc_rcv_padisc_unmap_node,	/* RCV_PDISC       */
3059 	lpfc_rcv_prlo_unmap_node,	/* RCV_PRLO        */
3060 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
3061 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3062 	lpfc_disc_illegal,		/* CMPL_LOGO       */
3063 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3064 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3065 	lpfc_device_rm_unmap_node,	/* DEVICE_RM       */
3066 	lpfc_device_recov_unmap_node,	/* DEVICE_RECOVERY */
3067 
3068 	lpfc_rcv_plogi_mapped_node,	/* RCV_PLOGI   MAPPED_NODE    */
3069 	lpfc_rcv_prli_mapped_node,	/* RCV_PRLI        */
3070 	lpfc_rcv_logo_mapped_node,	/* RCV_LOGO        */
3071 	lpfc_rcv_padisc_mapped_node,	/* RCV_ADISC       */
3072 	lpfc_rcv_padisc_mapped_node,	/* RCV_PDISC       */
3073 	lpfc_rcv_prlo_mapped_node,	/* RCV_PRLO        */
3074 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
3075 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3076 	lpfc_disc_illegal,		/* CMPL_LOGO       */
3077 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3078 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3079 	lpfc_disc_illegal,		/* DEVICE_RM       */
3080 	lpfc_device_recov_mapped_node,	/* DEVICE_RECOVERY */
3081 
3082 	lpfc_rcv_plogi_npr_node,        /* RCV_PLOGI   NPR_NODE    */
3083 	lpfc_rcv_prli_npr_node,         /* RCV_PRLI        */
3084 	lpfc_rcv_logo_npr_node,         /* RCV_LOGO        */
3085 	lpfc_rcv_padisc_npr_node,       /* RCV_ADISC       */
3086 	lpfc_rcv_padisc_npr_node,       /* RCV_PDISC       */
3087 	lpfc_rcv_prlo_npr_node,         /* RCV_PRLO        */
3088 	lpfc_cmpl_plogi_npr_node,	/* CMPL_PLOGI      */
3089 	lpfc_cmpl_prli_npr_node,	/* CMPL_PRLI       */
3090 	lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
3091 	lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
3092 	lpfc_cmpl_reglogin_npr_node,    /* CMPL_REG_LOGIN  */
3093 	lpfc_device_rm_npr_node,        /* DEVICE_RM       */
3094 	lpfc_device_recov_npr_node,     /* DEVICE_RECOVERY */
3095 };
3096 
3097 int
3098 lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3099 			void *arg, uint32_t evt)
3100 {
3101 	uint32_t cur_state, rc;
3102 	uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
3103 			 uint32_t);
3104 	uint32_t got_ndlp = 0;
3105 	uint32_t data1;
3106 
3107 	if (lpfc_nlp_get(ndlp))
3108 		got_ndlp = 1;
3109 
3110 	cur_state = ndlp->nlp_state;
3111 
3112 	data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
3113 		((uint32_t)ndlp->nlp_type));
3114 	/* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
3115 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3116 			 "0211 DSM in event x%x on NPort x%x in "
3117 			 "state %d rpi x%x Data: x%x x%x\n",
3118 			 evt, ndlp->nlp_DID, cur_state, ndlp->nlp_rpi,
3119 			 ndlp->nlp_flag, data1);
3120 
3121 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
3122 		 "DSM in:          evt:%d ste:%d did:x%x",
3123 		evt, cur_state, ndlp->nlp_DID);
3124 
3125 	func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
3126 	rc = (func) (vport, ndlp, arg, evt);
3127 
3128 	/* DSM out state <rc> on NPort <nlp_DID> */
3129 	if (got_ndlp) {
3130 		data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
3131 			((uint32_t)ndlp->nlp_type));
3132 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3133 			 "0212 DSM out state %d on NPort x%x "
3134 			 "rpi x%x Data: x%x x%x\n",
3135 			 rc, ndlp->nlp_DID, ndlp->nlp_rpi, ndlp->nlp_flag,
3136 			 data1);
3137 
3138 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
3139 			"DSM out:         ste:%d did:x%x flg:x%x",
3140 			rc, ndlp->nlp_DID, ndlp->nlp_flag);
3141 		/* Decrement the ndlp reference count held for this function */
3142 		lpfc_nlp_put(ndlp);
3143 	} else {
3144 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3145 			"0213 DSM out state %d on NPort free\n", rc);
3146 
3147 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
3148 			"DSM out:         ste:%d did:x%x flg:x%x",
3149 			rc, 0, 0);
3150 	}
3151 
3152 	return rc;
3153 }
3154