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