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