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