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