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