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