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