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