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