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