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