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