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