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