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