xref: /linux/drivers/scsi/lpfc/lpfc_nportdisc.c (revision de2fe5e07d58424bc286fff3fd3c1b0bf933cd58)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2006 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/interrupt.h>
25 
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_transport_fc.h>
30 
31 #include "lpfc_hw.h"
32 #include "lpfc_sli.h"
33 #include "lpfc_disc.h"
34 #include "lpfc_scsi.h"
35 #include "lpfc.h"
36 #include "lpfc_logmsg.h"
37 #include "lpfc_crtn.h"
38 
39 
40 /* Called to verify a rcv'ed ADISC was intended for us. */
41 static int
42 lpfc_check_adisc(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
43 		 struct lpfc_name * nn, struct lpfc_name * pn)
44 {
45 	/* Compare the ADISC rsp WWNN / WWPN matches our internal node
46 	 * table entry for that node.
47 	 */
48 	if (memcmp(nn, &ndlp->nlp_nodename, sizeof (struct lpfc_name)) != 0)
49 		return 0;
50 
51 	if (memcmp(pn, &ndlp->nlp_portname, sizeof (struct lpfc_name)) != 0)
52 		return 0;
53 
54 	/* we match, return success */
55 	return 1;
56 }
57 
58 int
59 lpfc_check_sparm(struct lpfc_hba * phba,
60 		 struct lpfc_nodelist * ndlp, struct serv_parm * sp,
61 		 uint32_t class)
62 {
63 	volatile struct serv_parm *hsp = &phba->fc_sparam;
64 	uint16_t hsp_value, ssp_value = 0;
65 
66 	/*
67 	 * The receive data field size and buffer-to-buffer receive data field
68 	 * size entries are 16 bits but are represented as two 8-bit fields in
69 	 * the driver data structure to account for rsvd bits and other control
70 	 * bits.  Reconstruct and compare the fields as a 16-bit values before
71 	 * correcting the byte values.
72 	 */
73 	if (sp->cls1.classValid) {
74 		hsp_value = (hsp->cls1.rcvDataSizeMsb << 8) |
75 				hsp->cls1.rcvDataSizeLsb;
76 		ssp_value = (sp->cls1.rcvDataSizeMsb << 8) |
77 				sp->cls1.rcvDataSizeLsb;
78 		if (ssp_value > hsp_value) {
79 			sp->cls1.rcvDataSizeLsb = hsp->cls1.rcvDataSizeLsb;
80 			sp->cls1.rcvDataSizeMsb = hsp->cls1.rcvDataSizeMsb;
81 		}
82 	} else if (class == CLASS1) {
83 		return 0;
84 	}
85 
86 	if (sp->cls2.classValid) {
87 		hsp_value = (hsp->cls2.rcvDataSizeMsb << 8) |
88 				hsp->cls2.rcvDataSizeLsb;
89 		ssp_value = (sp->cls2.rcvDataSizeMsb << 8) |
90 				sp->cls2.rcvDataSizeLsb;
91 		if (ssp_value > hsp_value) {
92 			sp->cls2.rcvDataSizeLsb = hsp->cls2.rcvDataSizeLsb;
93 			sp->cls2.rcvDataSizeMsb = hsp->cls2.rcvDataSizeMsb;
94 		}
95 	} else if (class == CLASS2) {
96 		return 0;
97 	}
98 
99 	if (sp->cls3.classValid) {
100 		hsp_value = (hsp->cls3.rcvDataSizeMsb << 8) |
101 				hsp->cls3.rcvDataSizeLsb;
102 		ssp_value = (sp->cls3.rcvDataSizeMsb << 8) |
103 				sp->cls3.rcvDataSizeLsb;
104 		if (ssp_value > hsp_value) {
105 			sp->cls3.rcvDataSizeLsb = hsp->cls3.rcvDataSizeLsb;
106 			sp->cls3.rcvDataSizeMsb = hsp->cls3.rcvDataSizeMsb;
107 		}
108 	} else if (class == CLASS3) {
109 		return 0;
110 	}
111 
112 	/*
113 	 * Preserve the upper four bits of the MSB from the PLOGI response.
114 	 * These bits contain the Buffer-to-Buffer State Change Number
115 	 * from the target and need to be passed to the FW.
116 	 */
117 	hsp_value = (hsp->cmn.bbRcvSizeMsb << 8) | hsp->cmn.bbRcvSizeLsb;
118 	ssp_value = (sp->cmn.bbRcvSizeMsb << 8) | sp->cmn.bbRcvSizeLsb;
119 	if (ssp_value > hsp_value) {
120 		sp->cmn.bbRcvSizeLsb = hsp->cmn.bbRcvSizeLsb;
121 		sp->cmn.bbRcvSizeMsb = (sp->cmn.bbRcvSizeMsb & 0xF0) |
122 				       (hsp->cmn.bbRcvSizeMsb & 0x0F);
123 	}
124 
125 	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
126 	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof (struct lpfc_name));
127 	return 1;
128 }
129 
130 static void *
131 lpfc_check_elscmpl_iocb(struct lpfc_hba * phba,
132 		      struct lpfc_iocbq *cmdiocb,
133 		      struct lpfc_iocbq *rspiocb)
134 {
135 	struct lpfc_dmabuf *pcmd, *prsp;
136 	uint32_t *lp;
137 	void     *ptr = NULL;
138 	IOCB_t   *irsp;
139 
140 	irsp = &rspiocb->iocb;
141 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
142 
143 	/* For lpfc_els_abort, context2 could be zero'ed to delay
144 	 * freeing associated memory till after ABTS completes.
145 	 */
146 	if (pcmd) {
147 		prsp =  list_get_first(&pcmd->list, struct lpfc_dmabuf,
148 				       list);
149 		if (prsp) {
150 			lp = (uint32_t *) prsp->virt;
151 			ptr = (void *)((uint8_t *)lp + sizeof(uint32_t));
152 		}
153 	} else {
154 		/* Force ulpStatus error since we are returning NULL ptr */
155 		if (!(irsp->ulpStatus)) {
156 			irsp->ulpStatus = IOSTAT_LOCAL_REJECT;
157 			irsp->un.ulpWord[4] = IOERR_SLI_ABORTED;
158 		}
159 		ptr = NULL;
160 	}
161 	return ptr;
162 }
163 
164 
165 /*
166  * Free resources / clean up outstanding I/Os
167  * associated with a LPFC_NODELIST entry. This
168  * routine effectively results in a "software abort".
169  */
170 int
171 lpfc_els_abort(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
172 	int send_abts)
173 {
174 	struct lpfc_sli *psli;
175 	struct lpfc_sli_ring *pring;
176 	struct lpfc_iocbq *iocb, *next_iocb;
177 	IOCB_t *icmd;
178 	int    found = 0;
179 
180 	/* Abort outstanding I/O on NPort <nlp_DID> */
181 	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
182 			"%d:0201 Abort outstanding I/O on NPort x%x "
183 			"Data: x%x x%x x%x\n",
184 			phba->brd_no, ndlp->nlp_DID, ndlp->nlp_flag,
185 			ndlp->nlp_state, ndlp->nlp_rpi);
186 
187 	psli = &phba->sli;
188 	pring = &psli->ring[LPFC_ELS_RING];
189 
190 	/* First check the txq */
191 	do {
192 		found = 0;
193 		spin_lock_irq(phba->host->host_lock);
194 		list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
195 			/* Check to see if iocb matches the nport we are looking
196 			   for */
197 			if ((lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))) {
198 				found = 1;
199 				/* It matches, so deque and call compl with an
200 				   error */
201 				list_del(&iocb->list);
202 				pring->txq_cnt--;
203 				if (iocb->iocb_cmpl) {
204 					icmd = &iocb->iocb;
205 					icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
206 					icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
207 					spin_unlock_irq(phba->host->host_lock);
208 					(iocb->iocb_cmpl) (phba, iocb, iocb);
209 					spin_lock_irq(phba->host->host_lock);
210 				} else
211 					lpfc_sli_release_iocbq(phba, iocb);
212 				break;
213 			}
214 		}
215 		spin_unlock_irq(phba->host->host_lock);
216 	} while (found);
217 
218 	/* Everything on txcmplq will be returned by firmware
219 	 * with a no rpi / linkdown / abort error.  For ring 0,
220 	 * ELS discovery, we want to get rid of it right here.
221 	 */
222 	/* Next check the txcmplq */
223 	do {
224 		found = 0;
225 		spin_lock_irq(phba->host->host_lock);
226 		list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
227 					 list) {
228 			/* Check to see if iocb matches the nport we are looking
229 			   for */
230 			if ((lpfc_check_sli_ndlp (phba, pring, iocb, ndlp))) {
231 				found = 1;
232 				/* It matches, so deque and call compl with an
233 				   error */
234 				list_del(&iocb->list);
235 				pring->txcmplq_cnt--;
236 
237 				icmd = &iocb->iocb;
238 				/* If the driver is completing an ELS
239 				 * command early, flush it out of the firmware.
240 				 */
241 				if (send_abts &&
242 				   (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) &&
243 				   (icmd->un.elsreq64.bdl.ulpIoTag32)) {
244 					lpfc_sli_issue_abort_iotag32(phba,
245 							     pring, iocb);
246 				}
247 				if (iocb->iocb_cmpl) {
248 					icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
249 					icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
250 					spin_unlock_irq(phba->host->host_lock);
251 					(iocb->iocb_cmpl) (phba, iocb, iocb);
252 					spin_lock_irq(phba->host->host_lock);
253 				} else
254 					lpfc_sli_release_iocbq(phba, iocb);
255 				break;
256 			}
257 		}
258 		spin_unlock_irq(phba->host->host_lock);
259 	} while(found);
260 
261 	/* If we are delaying issuing an ELS command, cancel it */
262 	if (ndlp->nlp_flag & NLP_DELAY_TMO)
263 		lpfc_cancel_retry_delay_tmo(phba, ndlp);
264 	return 0;
265 }
266 
267 static int
268 lpfc_rcv_plogi(struct lpfc_hba * phba,
269 		      struct lpfc_nodelist * ndlp,
270 		      struct lpfc_iocbq *cmdiocb)
271 {
272 	struct lpfc_dmabuf *pcmd;
273 	uint32_t *lp;
274 	IOCB_t *icmd;
275 	struct serv_parm *sp;
276 	LPFC_MBOXQ_t *mbox;
277 	struct ls_rjt stat;
278 	int rc;
279 
280 	memset(&stat, 0, sizeof (struct ls_rjt));
281 	if (phba->hba_state <= LPFC_FLOGI) {
282 		/* Before responding to PLOGI, check for pt2pt mode.
283 		 * If we are pt2pt, with an outstanding FLOGI, abort
284 		 * the FLOGI and resend it first.
285 		 */
286 		if (phba->fc_flag & FC_PT2PT) {
287 			lpfc_els_abort_flogi(phba);
288 		        if (!(phba->fc_flag & FC_PT2PT_PLOGI)) {
289 				/* If the other side is supposed to initiate
290 				 * the PLOGI anyway, just ACC it now and
291 				 * move on with discovery.
292 				 */
293 				phba->fc_edtov = FF_DEF_EDTOV;
294 				phba->fc_ratov = FF_DEF_RATOV;
295 				/* Start discovery - this should just do
296 				   CLEAR_LA */
297 				lpfc_disc_start(phba);
298 			} else {
299 				lpfc_initial_flogi(phba);
300 			}
301 		} else {
302 			stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
303 			stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
304 			lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb,
305 					    ndlp);
306 			return 0;
307 		}
308 	}
309 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
310 	lp = (uint32_t *) pcmd->virt;
311 	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
312 	if ((lpfc_check_sparm(phba, ndlp, sp, CLASS3) == 0)) {
313 		/* Reject this request because invalid parameters */
314 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
315 		stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
316 		lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
317 		return 0;
318 	}
319 	icmd = &cmdiocb->iocb;
320 
321 	/* PLOGI chkparm OK */
322 	lpfc_printf_log(phba,
323 			KERN_INFO,
324 			LOG_ELS,
325 			"%d:0114 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
326 			phba->brd_no,
327 			ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
328 			ndlp->nlp_rpi);
329 
330 	if ((phba->cfg_fcp_class == 2) &&
331 	    (sp->cls2.classValid)) {
332 		ndlp->nlp_fcp_info |= CLASS2;
333 	} else {
334 		ndlp->nlp_fcp_info |= CLASS3;
335 	}
336 	ndlp->nlp_class_sup = 0;
337 	if (sp->cls1.classValid)
338 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
339 	if (sp->cls2.classValid)
340 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
341 	if (sp->cls3.classValid)
342 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
343 	if (sp->cls4.classValid)
344 		ndlp->nlp_class_sup |= FC_COS_CLASS4;
345 	ndlp->nlp_maxframe =
346 		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
347 
348 	/* no need to reg_login if we are already in one of these states */
349 	switch (ndlp->nlp_state) {
350 	case  NLP_STE_NPR_NODE:
351 		if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
352 			break;
353 	case  NLP_STE_REG_LOGIN_ISSUE:
354 	case  NLP_STE_PRLI_ISSUE:
355 	case  NLP_STE_UNMAPPED_NODE:
356 	case  NLP_STE_MAPPED_NODE:
357 		lpfc_els_rsp_acc(phba, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL, 0);
358 		return 1;
359 	}
360 
361 	if ((phba->fc_flag & FC_PT2PT)
362 	    && !(phba->fc_flag & FC_PT2PT_PLOGI)) {
363 		/* rcv'ed PLOGI decides what our NPortId will be */
364 		phba->fc_myDID = icmd->un.rcvels.parmRo;
365 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
366 		if (mbox == NULL)
367 			goto out;
368 		lpfc_config_link(phba, mbox);
369 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
370 		rc = lpfc_sli_issue_mbox
371 			(phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
372 		if (rc == MBX_NOT_FINISHED) {
373 			mempool_free( mbox, phba->mbox_mem_pool);
374 			goto out;
375 		}
376 
377 		lpfc_can_disctmo(phba);
378 	}
379 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
380 	if (mbox == NULL)
381 		goto out;
382 
383 	if (lpfc_reg_login(phba, icmd->un.rcvels.remoteID,
384 			   (uint8_t *) sp, mbox, 0)) {
385 		mempool_free( mbox, phba->mbox_mem_pool);
386 		goto out;
387 	}
388 
389 	/* ACC PLOGI rsp command needs to execute first,
390 	 * queue this mbox command to be processed later.
391 	 */
392 	mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
393 	mbox->context2  = ndlp;
394 	ndlp->nlp_flag |= (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI);
395 
396 	lpfc_els_rsp_acc(phba, ELS_CMD_PLOGI, cmdiocb, ndlp, mbox, 0);
397 	return 1;
398 
399 out:
400 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
401 	stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
402 	lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
403 	return 0;
404 }
405 
406 static int
407 lpfc_rcv_padisc(struct lpfc_hba * phba,
408 		struct lpfc_nodelist * ndlp,
409 		struct lpfc_iocbq *cmdiocb)
410 {
411 	struct lpfc_dmabuf *pcmd;
412 	struct serv_parm *sp;
413 	struct lpfc_name *pnn, *ppn;
414 	struct ls_rjt stat;
415 	ADISC *ap;
416 	IOCB_t *icmd;
417 	uint32_t *lp;
418 	uint32_t cmd;
419 
420 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
421 	lp = (uint32_t *) pcmd->virt;
422 
423 	cmd = *lp++;
424 	if (cmd == ELS_CMD_ADISC) {
425 		ap = (ADISC *) lp;
426 		pnn = (struct lpfc_name *) & ap->nodeName;
427 		ppn = (struct lpfc_name *) & ap->portName;
428 	} else {
429 		sp = (struct serv_parm *) lp;
430 		pnn = (struct lpfc_name *) & sp->nodeName;
431 		ppn = (struct lpfc_name *) & sp->portName;
432 	}
433 
434 	icmd = &cmdiocb->iocb;
435 	if ((icmd->ulpStatus == 0) &&
436 	    (lpfc_check_adisc(phba, ndlp, pnn, ppn))) {
437 		if (cmd == ELS_CMD_ADISC) {
438 			lpfc_els_rsp_adisc_acc(phba, cmdiocb, ndlp);
439 		} else {
440 			lpfc_els_rsp_acc(phba, ELS_CMD_PLOGI, cmdiocb, ndlp,
441 				NULL, 0);
442 		}
443 		return 1;
444 	}
445 	/* Reject this request because invalid parameters */
446 	stat.un.b.lsRjtRsvd0 = 0;
447 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
448 	stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
449 	stat.un.b.vendorUnique = 0;
450 	lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
451 
452 	/* 1 sec timeout */
453 	mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
454 
455 	spin_lock_irq(phba->host->host_lock);
456 	ndlp->nlp_flag |= NLP_DELAY_TMO;
457 	spin_unlock_irq(phba->host->host_lock);
458 	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
459 	ndlp->nlp_prev_state = ndlp->nlp_state;
460 	ndlp->nlp_state = NLP_STE_NPR_NODE;
461 	lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
462 	return 0;
463 }
464 
465 static int
466 lpfc_rcv_logo(struct lpfc_hba * phba,
467 		      struct lpfc_nodelist * ndlp,
468 		      struct lpfc_iocbq *cmdiocb)
469 {
470 	/* Put ndlp on NPR list with 1 sec timeout for plogi, ACC logo */
471 	/* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
472 	 * PLOGIs during LOGO storms from a device.
473 	 */
474 	ndlp->nlp_flag |= NLP_LOGO_ACC;
475 	lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
476 
477 	if (!(ndlp->nlp_type & NLP_FABRIC) ||
478 		(ndlp->nlp_state == NLP_STE_ADISC_ISSUE)) {
479 		/* Only try to re-login if this is NOT a Fabric Node */
480 		mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
481 		spin_lock_irq(phba->host->host_lock);
482 		ndlp->nlp_flag |= NLP_DELAY_TMO;
483 		spin_unlock_irq(phba->host->host_lock);
484 
485 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
486 		ndlp->nlp_prev_state = ndlp->nlp_state;
487 		ndlp->nlp_state = NLP_STE_NPR_NODE;
488 		lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
489 	} else {
490 		ndlp->nlp_prev_state = ndlp->nlp_state;
491 		ndlp->nlp_state = NLP_STE_UNUSED_NODE;
492 		lpfc_nlp_list(phba, ndlp, NLP_UNUSED_LIST);
493 	}
494 
495 	spin_lock_irq(phba->host->host_lock);
496 	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
497 	spin_unlock_irq(phba->host->host_lock);
498 	/* The driver has to wait until the ACC completes before it continues
499 	 * processing the LOGO.  The action will resume in
500 	 * lpfc_cmpl_els_logo_acc routine. Since part of processing includes an
501 	 * unreg_login, the driver waits so the ACC does not get aborted.
502 	 */
503 	return 0;
504 }
505 
506 static void
507 lpfc_rcv_prli(struct lpfc_hba * phba,
508 		      struct lpfc_nodelist * ndlp,
509 		      struct lpfc_iocbq *cmdiocb)
510 {
511 	struct lpfc_dmabuf *pcmd;
512 	uint32_t *lp;
513 	PRLI *npr;
514 	struct fc_rport *rport = ndlp->rport;
515 	u32 roles;
516 
517 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
518 	lp = (uint32_t *) pcmd->virt;
519 	npr = (PRLI *) ((uint8_t *) lp + sizeof (uint32_t));
520 
521 	ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
522 	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
523 	if ((npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
524 	    (npr->prliType == PRLI_FCP_TYPE)) {
525 		if (npr->initiatorFunc)
526 			ndlp->nlp_type |= NLP_FCP_INITIATOR;
527 		if (npr->targetFunc)
528 			ndlp->nlp_type |= NLP_FCP_TARGET;
529 		if (npr->Retry)
530 			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
531 	}
532 	if (rport) {
533 		/* We need to update the rport role values */
534 		roles = FC_RPORT_ROLE_UNKNOWN;
535 		if (ndlp->nlp_type & NLP_FCP_INITIATOR)
536 			roles |= FC_RPORT_ROLE_FCP_INITIATOR;
537 		if (ndlp->nlp_type & NLP_FCP_TARGET)
538 			roles |= FC_RPORT_ROLE_FCP_TARGET;
539 		fc_remote_port_rolechg(rport, roles);
540 	}
541 }
542 
543 static uint32_t
544 lpfc_disc_set_adisc(struct lpfc_hba * phba,
545 		      struct lpfc_nodelist * ndlp)
546 {
547 	/* Check config parameter use-adisc or FCP-2 */
548 	if ((phba->cfg_use_adisc == 0) &&
549 		!(phba->fc_flag & FC_RSCN_MODE)) {
550 		if (!(ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
551 			return 0;
552 	}
553 	spin_lock_irq(phba->host->host_lock);
554 	ndlp->nlp_flag |= NLP_NPR_ADISC;
555 	spin_unlock_irq(phba->host->host_lock);
556 	return 1;
557 }
558 
559 static uint32_t
560 lpfc_disc_illegal(struct lpfc_hba * phba,
561 		   struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
562 {
563 	lpfc_printf_log(phba,
564 			KERN_ERR,
565 			LOG_DISCOVERY,
566 			"%d:0253 Illegal State Transition: node x%x event x%x, "
567 			"state x%x Data: x%x x%x\n",
568 			phba->brd_no,
569 			ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
570 			ndlp->nlp_flag);
571 	return ndlp->nlp_state;
572 }
573 
574 /* Start of Discovery State Machine routines */
575 
576 static uint32_t
577 lpfc_rcv_plogi_unused_node(struct lpfc_hba * phba,
578 			   struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
579 {
580 	struct lpfc_iocbq *cmdiocb;
581 
582 	cmdiocb = (struct lpfc_iocbq *) arg;
583 
584 	if (lpfc_rcv_plogi(phba, ndlp, cmdiocb)) {
585 		ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
586 		ndlp->nlp_state = NLP_STE_UNUSED_NODE;
587 		lpfc_nlp_list(phba, ndlp, NLP_UNUSED_LIST);
588 		return ndlp->nlp_state;
589 	}
590 	lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
591 	return NLP_STE_FREED_NODE;
592 }
593 
594 static uint32_t
595 lpfc_rcv_els_unused_node(struct lpfc_hba * phba,
596 			 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
597 {
598 	lpfc_issue_els_logo(phba, ndlp, 0);
599 	lpfc_nlp_list(phba, ndlp, NLP_UNUSED_LIST);
600 	return ndlp->nlp_state;
601 }
602 
603 static uint32_t
604 lpfc_rcv_logo_unused_node(struct lpfc_hba * phba,
605 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
606 {
607 	struct lpfc_iocbq     *cmdiocb;
608 
609 	cmdiocb = (struct lpfc_iocbq *) arg;
610 
611 	spin_lock_irq(phba->host->host_lock);
612 	ndlp->nlp_flag |= NLP_LOGO_ACC;
613 	spin_unlock_irq(phba->host->host_lock);
614 	lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
615 	lpfc_nlp_list(phba, ndlp, NLP_UNUSED_LIST);
616 
617 	return ndlp->nlp_state;
618 }
619 
620 static uint32_t
621 lpfc_cmpl_logo_unused_node(struct lpfc_hba * phba,
622 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
623 {
624 	lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
625 	return NLP_STE_FREED_NODE;
626 }
627 
628 static uint32_t
629 lpfc_device_rm_unused_node(struct lpfc_hba * phba,
630 			   struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
631 {
632 	lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
633 	return NLP_STE_FREED_NODE;
634 }
635 
636 static uint32_t
637 lpfc_rcv_plogi_plogi_issue(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
638 			   void *arg, uint32_t evt)
639 {
640 	struct lpfc_iocbq *cmdiocb = arg;
641 	struct lpfc_dmabuf *pcmd;
642 	struct serv_parm *sp;
643 	uint32_t *lp;
644 	struct ls_rjt stat;
645 	int port_cmp;
646 
647 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
648 	lp = (uint32_t *) pcmd->virt;
649 	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
650 
651 	memset(&stat, 0, sizeof (struct ls_rjt));
652 
653 	/* For a PLOGI, we only accept if our portname is less
654 	 * than the remote portname.
655 	 */
656 	phba->fc_stat.elsLogiCol++;
657 	port_cmp = memcmp(&phba->fc_portname, &sp->portName,
658 			  sizeof (struct lpfc_name));
659 
660 	if (port_cmp >= 0) {
661 		/* Reject this request because the remote node will accept
662 		   ours */
663 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
664 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
665 		lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
666 	} else {
667 		lpfc_rcv_plogi(phba, ndlp, cmdiocb);
668 	} /* if our portname was less */
669 
670 	return ndlp->nlp_state;
671 }
672 
673 static uint32_t
674 lpfc_rcv_logo_plogi_issue(struct lpfc_hba * phba,
675 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
676 {
677 	struct lpfc_iocbq     *cmdiocb;
678 
679 	cmdiocb = (struct lpfc_iocbq *) arg;
680 
681 	/* software abort outstanding PLOGI */
682 	lpfc_els_abort(phba, ndlp, 1);
683 
684 	lpfc_rcv_logo(phba, ndlp, cmdiocb);
685 	return ndlp->nlp_state;
686 }
687 
688 static uint32_t
689 lpfc_rcv_els_plogi_issue(struct lpfc_hba * phba,
690 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
691 {
692 	struct lpfc_iocbq     *cmdiocb;
693 
694 	cmdiocb = (struct lpfc_iocbq *) arg;
695 
696 	/* software abort outstanding PLOGI */
697 	lpfc_els_abort(phba, ndlp, 1);
698 
699 	if (evt == NLP_EVT_RCV_LOGO) {
700 		lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
701 	} else {
702 		lpfc_issue_els_logo(phba, ndlp, 0);
703 	}
704 
705 	/* Put ndlp in npr list set plogi timer for 1 sec */
706 	mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
707 	spin_lock_irq(phba->host->host_lock);
708 	ndlp->nlp_flag |= NLP_DELAY_TMO;
709 	spin_unlock_irq(phba->host->host_lock);
710 	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
711 	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
712 	ndlp->nlp_state = NLP_STE_NPR_NODE;
713 	lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
714 
715 	return ndlp->nlp_state;
716 }
717 
718 static uint32_t
719 lpfc_cmpl_plogi_plogi_issue(struct lpfc_hba * phba,
720 			    struct lpfc_nodelist * ndlp, void *arg,
721 			    uint32_t evt)
722 {
723 	struct lpfc_iocbq *cmdiocb, *rspiocb;
724 	struct lpfc_dmabuf *pcmd, *prsp;
725 	uint32_t *lp;
726 	IOCB_t *irsp;
727 	struct serv_parm *sp;
728 	LPFC_MBOXQ_t *mbox;
729 
730 	cmdiocb = (struct lpfc_iocbq *) arg;
731 	rspiocb = cmdiocb->context_un.rsp_iocb;
732 
733 	if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
734 		/* Recovery from PLOGI collision logic */
735 		return ndlp->nlp_state;
736 	}
737 
738 	irsp = &rspiocb->iocb;
739 
740 	if (irsp->ulpStatus)
741 		goto out;
742 
743 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
744 
745 	prsp = list_get_first(&pcmd->list,
746 			      struct lpfc_dmabuf,
747 			      list);
748 	lp = (uint32_t *) prsp->virt;
749 
750 	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
751 	if (!lpfc_check_sparm(phba, ndlp, sp, CLASS3))
752 		goto out;
753 
754 	/* PLOGI chkparm OK */
755 	lpfc_printf_log(phba,
756 			KERN_INFO,
757 			LOG_ELS,
758 			"%d:0121 PLOGI chkparm OK "
759 			"Data: x%x x%x x%x x%x\n",
760 			phba->brd_no,
761 			ndlp->nlp_DID, ndlp->nlp_state,
762 			ndlp->nlp_flag, ndlp->nlp_rpi);
763 
764 	if ((phba->cfg_fcp_class == 2) &&
765 	    (sp->cls2.classValid)) {
766 		ndlp->nlp_fcp_info |= CLASS2;
767 	} else {
768 		ndlp->nlp_fcp_info |= CLASS3;
769 	}
770 	ndlp->nlp_class_sup = 0;
771 	if (sp->cls1.classValid)
772 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
773 	if (sp->cls2.classValid)
774 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
775 	if (sp->cls3.classValid)
776 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
777 	if (sp->cls4.classValid)
778 		ndlp->nlp_class_sup |= FC_COS_CLASS4;
779 	ndlp->nlp_maxframe =
780 		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
781 		sp->cmn.bbRcvSizeLsb;
782 
783 	if (!(mbox = mempool_alloc(phba->mbox_mem_pool,
784 				   GFP_KERNEL)))
785 		goto out;
786 
787 	lpfc_unreg_rpi(phba, ndlp);
788 	if (lpfc_reg_login
789 	    (phba, irsp->un.elsreq64.remoteID,
790 	     (uint8_t *) sp, mbox, 0) == 0) {
791 		/* set_slim mailbox command needs to
792 		 * execute first, queue this command to
793 		 * be processed later.
794 		 */
795 		switch (ndlp->nlp_DID) {
796 		case NameServer_DID:
797 			mbox->mbox_cmpl =
798 				lpfc_mbx_cmpl_ns_reg_login;
799 			break;
800 		case FDMI_DID:
801 			mbox->mbox_cmpl =
802 				lpfc_mbx_cmpl_fdmi_reg_login;
803 			break;
804 		default:
805 			mbox->mbox_cmpl =
806 				lpfc_mbx_cmpl_reg_login;
807 		}
808 		mbox->context2 = ndlp;
809 		if (lpfc_sli_issue_mbox(phba, mbox,
810 					(MBX_NOWAIT | MBX_STOP_IOCB))
811 		    != MBX_NOT_FINISHED) {
812 			ndlp->nlp_state =
813 				NLP_STE_REG_LOGIN_ISSUE;
814 			lpfc_nlp_list(phba, ndlp,
815 				      NLP_REGLOGIN_LIST);
816 			return ndlp->nlp_state;
817 		}
818 		mempool_free(mbox, phba->mbox_mem_pool);
819 	} else {
820 		mempool_free(mbox, phba->mbox_mem_pool);
821 	}
822 
823 
824  out:
825 	/* Free this node since the driver cannot login or has the wrong
826 	   sparm */
827 	lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
828 	return NLP_STE_FREED_NODE;
829 }
830 
831 static uint32_t
832 lpfc_device_rm_plogi_issue(struct lpfc_hba * phba,
833 			   struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
834 {
835 	/* software abort outstanding PLOGI */
836 	lpfc_els_abort(phba, ndlp, 1);
837 
838 	lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
839 	return NLP_STE_FREED_NODE;
840 }
841 
842 static uint32_t
843 lpfc_device_recov_plogi_issue(struct lpfc_hba * phba,
844 			    struct lpfc_nodelist * ndlp, void *arg,
845 			    uint32_t evt)
846 {
847 	/* software abort outstanding PLOGI */
848 	lpfc_els_abort(phba, ndlp, 1);
849 
850 	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
851 	ndlp->nlp_state = NLP_STE_NPR_NODE;
852 	lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
853 	spin_lock_irq(phba->host->host_lock);
854 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
855 	spin_unlock_irq(phba->host->host_lock);
856 
857 	return ndlp->nlp_state;
858 }
859 
860 static uint32_t
861 lpfc_rcv_plogi_adisc_issue(struct lpfc_hba * phba,
862 			    struct lpfc_nodelist * ndlp, void *arg,
863 			    uint32_t evt)
864 {
865 	struct lpfc_iocbq *cmdiocb;
866 
867 	/* software abort outstanding ADISC */
868 	lpfc_els_abort(phba, ndlp, 1);
869 
870 	cmdiocb = (struct lpfc_iocbq *) arg;
871 
872 	if (lpfc_rcv_plogi(phba, ndlp, cmdiocb)) {
873 		return ndlp->nlp_state;
874 	}
875 	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
876 	ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
877 	lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
878 	lpfc_issue_els_plogi(phba, ndlp->nlp_DID, 0);
879 
880 	return ndlp->nlp_state;
881 }
882 
883 static uint32_t
884 lpfc_rcv_prli_adisc_issue(struct lpfc_hba * phba,
885 			    struct lpfc_nodelist * ndlp, void *arg,
886 			    uint32_t evt)
887 {
888 	struct lpfc_iocbq *cmdiocb;
889 
890 	cmdiocb = (struct lpfc_iocbq *) arg;
891 
892 	lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
893 	return ndlp->nlp_state;
894 }
895 
896 static uint32_t
897 lpfc_rcv_logo_adisc_issue(struct lpfc_hba * phba,
898 			    struct lpfc_nodelist * ndlp, void *arg,
899 			    uint32_t evt)
900 {
901 	struct lpfc_iocbq *cmdiocb;
902 
903 	cmdiocb = (struct lpfc_iocbq *) arg;
904 
905 	/* software abort outstanding ADISC */
906 	lpfc_els_abort(phba, ndlp, 0);
907 
908 	lpfc_rcv_logo(phba, ndlp, cmdiocb);
909 	return ndlp->nlp_state;
910 }
911 
912 static uint32_t
913 lpfc_rcv_padisc_adisc_issue(struct lpfc_hba * phba,
914 			    struct lpfc_nodelist * ndlp, void *arg,
915 			    uint32_t evt)
916 {
917 	struct lpfc_iocbq *cmdiocb;
918 
919 	cmdiocb = (struct lpfc_iocbq *) arg;
920 
921 	lpfc_rcv_padisc(phba, ndlp, cmdiocb);
922 	return ndlp->nlp_state;
923 }
924 
925 static uint32_t
926 lpfc_rcv_prlo_adisc_issue(struct lpfc_hba * phba,
927 			    struct lpfc_nodelist * ndlp, void *arg,
928 			    uint32_t evt)
929 {
930 	struct lpfc_iocbq *cmdiocb;
931 
932 	cmdiocb = (struct lpfc_iocbq *) arg;
933 
934 	/* Treat like rcv logo */
935 	lpfc_rcv_logo(phba, ndlp, cmdiocb);
936 	return ndlp->nlp_state;
937 }
938 
939 static uint32_t
940 lpfc_cmpl_adisc_adisc_issue(struct lpfc_hba * phba,
941 			    struct lpfc_nodelist * ndlp, void *arg,
942 			    uint32_t evt)
943 {
944 	struct lpfc_iocbq *cmdiocb, *rspiocb;
945 	IOCB_t *irsp;
946 	ADISC *ap;
947 
948 	cmdiocb = (struct lpfc_iocbq *) arg;
949 	rspiocb = cmdiocb->context_un.rsp_iocb;
950 
951 	ap = (ADISC *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
952 	irsp = &rspiocb->iocb;
953 
954 	if ((irsp->ulpStatus) ||
955 		(!lpfc_check_adisc(phba, ndlp, &ap->nodeName, &ap->portName))) {
956 		/* 1 sec timeout */
957 		mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
958 		spin_lock_irq(phba->host->host_lock);
959 		ndlp->nlp_flag |= NLP_DELAY_TMO;
960 		spin_unlock_irq(phba->host->host_lock);
961 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
962 
963 		memset(&ndlp->nlp_nodename, 0, sizeof (struct lpfc_name));
964 		memset(&ndlp->nlp_portname, 0, sizeof (struct lpfc_name));
965 
966 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
967 		ndlp->nlp_state = NLP_STE_NPR_NODE;
968 		lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
969 		lpfc_unreg_rpi(phba, ndlp);
970 		return ndlp->nlp_state;
971 	}
972 
973 	if (ndlp->nlp_type & NLP_FCP_TARGET) {
974 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
975 		ndlp->nlp_state = NLP_STE_MAPPED_NODE;
976 		lpfc_nlp_list(phba, ndlp, NLP_MAPPED_LIST);
977 	} else {
978 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
979 		ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
980 		lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
981 	}
982 	return ndlp->nlp_state;
983 }
984 
985 static uint32_t
986 lpfc_device_rm_adisc_issue(struct lpfc_hba * phba,
987 			    struct lpfc_nodelist * ndlp, void *arg,
988 			    uint32_t evt)
989 {
990 	/* software abort outstanding ADISC */
991 	lpfc_els_abort(phba, ndlp, 1);
992 
993 	lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
994 	return NLP_STE_FREED_NODE;
995 }
996 
997 static uint32_t
998 lpfc_device_recov_adisc_issue(struct lpfc_hba * phba,
999 			    struct lpfc_nodelist * ndlp, void *arg,
1000 			    uint32_t evt)
1001 {
1002 	/* software abort outstanding ADISC */
1003 	lpfc_els_abort(phba, ndlp, 1);
1004 
1005 	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1006 	ndlp->nlp_state = NLP_STE_NPR_NODE;
1007 	lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1008 	spin_lock_irq(phba->host->host_lock);
1009 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1010 	ndlp->nlp_flag |= NLP_NPR_ADISC;
1011 	spin_unlock_irq(phba->host->host_lock);
1012 
1013 	return ndlp->nlp_state;
1014 }
1015 
1016 static uint32_t
1017 lpfc_rcv_plogi_reglogin_issue(struct lpfc_hba * phba,
1018 			      struct lpfc_nodelist * ndlp, void *arg,
1019 			      uint32_t evt)
1020 {
1021 	struct lpfc_iocbq *cmdiocb;
1022 
1023 	cmdiocb = (struct lpfc_iocbq *) arg;
1024 
1025 	lpfc_rcv_plogi(phba, ndlp, cmdiocb);
1026 	return ndlp->nlp_state;
1027 }
1028 
1029 static uint32_t
1030 lpfc_rcv_prli_reglogin_issue(struct lpfc_hba * phba,
1031 			     struct lpfc_nodelist * ndlp, void *arg,
1032 			     uint32_t evt)
1033 {
1034 	struct lpfc_iocbq *cmdiocb;
1035 
1036 	cmdiocb = (struct lpfc_iocbq *) arg;
1037 
1038 	lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
1039 	return ndlp->nlp_state;
1040 }
1041 
1042 static uint32_t
1043 lpfc_rcv_logo_reglogin_issue(struct lpfc_hba * phba,
1044 			     struct lpfc_nodelist * ndlp, void *arg,
1045 			     uint32_t evt)
1046 {
1047 	struct lpfc_iocbq *cmdiocb;
1048 
1049 	cmdiocb = (struct lpfc_iocbq *) arg;
1050 
1051 	lpfc_rcv_logo(phba, ndlp, cmdiocb);
1052 	return ndlp->nlp_state;
1053 }
1054 
1055 static uint32_t
1056 lpfc_rcv_padisc_reglogin_issue(struct lpfc_hba * phba,
1057 			       struct lpfc_nodelist * ndlp, void *arg,
1058 			       uint32_t evt)
1059 {
1060 	struct lpfc_iocbq *cmdiocb;
1061 
1062 	cmdiocb = (struct lpfc_iocbq *) arg;
1063 
1064 	lpfc_rcv_padisc(phba, ndlp, cmdiocb);
1065 	return ndlp->nlp_state;
1066 }
1067 
1068 static uint32_t
1069 lpfc_rcv_prlo_reglogin_issue(struct lpfc_hba * phba,
1070 			     struct lpfc_nodelist * ndlp, void *arg,
1071 			     uint32_t evt)
1072 {
1073 	struct lpfc_iocbq *cmdiocb;
1074 
1075 	cmdiocb = (struct lpfc_iocbq *) arg;
1076 	lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
1077 	return ndlp->nlp_state;
1078 }
1079 
1080 static uint32_t
1081 lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_hba * phba,
1082 				  struct lpfc_nodelist * ndlp,
1083 				  void *arg, uint32_t evt)
1084 {
1085 	LPFC_MBOXQ_t *pmb;
1086 	MAILBOX_t *mb;
1087 	uint32_t did;
1088 
1089 	pmb = (LPFC_MBOXQ_t *) arg;
1090 	mb = &pmb->mb;
1091 	did = mb->un.varWords[1];
1092 	if (mb->mbxStatus) {
1093 		/* RegLogin failed */
1094 		lpfc_printf_log(phba,
1095 				KERN_ERR,
1096 				LOG_DISCOVERY,
1097 				"%d:0246 RegLogin failed Data: x%x x%x x%x\n",
1098 				phba->brd_no,
1099 				did, mb->mbxStatus, phba->hba_state);
1100 
1101 		/* Put ndlp in npr list set plogi timer for 1 sec */
1102 		mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
1103 		spin_lock_irq(phba->host->host_lock);
1104 		ndlp->nlp_flag |= NLP_DELAY_TMO;
1105 		spin_unlock_irq(phba->host->host_lock);
1106 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1107 
1108 		lpfc_issue_els_logo(phba, ndlp, 0);
1109 		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1110 		ndlp->nlp_state = NLP_STE_NPR_NODE;
1111 		lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1112 		return ndlp->nlp_state;
1113 	}
1114 
1115 	ndlp->nlp_rpi = mb->un.varWords[0];
1116 
1117 	/* Only if we are not a fabric nport do we issue PRLI */
1118 	if (!(ndlp->nlp_type & NLP_FABRIC)) {
1119 		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1120 		ndlp->nlp_state = NLP_STE_PRLI_ISSUE;
1121 		lpfc_nlp_list(phba, ndlp, NLP_PRLI_LIST);
1122 		lpfc_issue_els_prli(phba, ndlp, 0);
1123 	} else {
1124 		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1125 		ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
1126 		lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
1127 	}
1128 	return ndlp->nlp_state;
1129 }
1130 
1131 static uint32_t
1132 lpfc_device_rm_reglogin_issue(struct lpfc_hba * phba,
1133 			      struct lpfc_nodelist * ndlp, void *arg,
1134 			      uint32_t evt)
1135 {
1136 	lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1137 	return NLP_STE_FREED_NODE;
1138 }
1139 
1140 static uint32_t
1141 lpfc_device_recov_reglogin_issue(struct lpfc_hba * phba,
1142 			       struct lpfc_nodelist * ndlp, void *arg,
1143 			       uint32_t evt)
1144 {
1145 	ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1146 	ndlp->nlp_state = NLP_STE_NPR_NODE;
1147 	lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1148 	spin_lock_irq(phba->host->host_lock);
1149 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1150 	spin_unlock_irq(phba->host->host_lock);
1151 	return ndlp->nlp_state;
1152 }
1153 
1154 static uint32_t
1155 lpfc_rcv_plogi_prli_issue(struct lpfc_hba * phba,
1156 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1157 {
1158 	struct lpfc_iocbq *cmdiocb;
1159 
1160 	cmdiocb = (struct lpfc_iocbq *) arg;
1161 
1162 	lpfc_rcv_plogi(phba, ndlp, cmdiocb);
1163 	return ndlp->nlp_state;
1164 }
1165 
1166 static uint32_t
1167 lpfc_rcv_prli_prli_issue(struct lpfc_hba * phba,
1168 			 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1169 {
1170 	struct lpfc_iocbq *cmdiocb;
1171 
1172 	cmdiocb = (struct lpfc_iocbq *) arg;
1173 
1174 	lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
1175 	return ndlp->nlp_state;
1176 }
1177 
1178 static uint32_t
1179 lpfc_rcv_logo_prli_issue(struct lpfc_hba * phba,
1180 			 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1181 {
1182 	struct lpfc_iocbq *cmdiocb;
1183 
1184 	cmdiocb = (struct lpfc_iocbq *) arg;
1185 
1186 	/* Software abort outstanding PRLI before sending acc */
1187 	lpfc_els_abort(phba, ndlp, 1);
1188 
1189 	lpfc_rcv_logo(phba, ndlp, cmdiocb);
1190 	return ndlp->nlp_state;
1191 }
1192 
1193 static uint32_t
1194 lpfc_rcv_padisc_prli_issue(struct lpfc_hba * phba,
1195 			   struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1196 {
1197 	struct lpfc_iocbq *cmdiocb;
1198 
1199 	cmdiocb = (struct lpfc_iocbq *) arg;
1200 
1201 	lpfc_rcv_padisc(phba, ndlp, cmdiocb);
1202 	return ndlp->nlp_state;
1203 }
1204 
1205 /* This routine is envoked when we rcv a PRLO request from a nport
1206  * we are logged into.  We should send back a PRLO rsp setting the
1207  * appropriate bits.
1208  * NEXT STATE = PRLI_ISSUE
1209  */
1210 static uint32_t
1211 lpfc_rcv_prlo_prli_issue(struct lpfc_hba * phba,
1212 			 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1213 {
1214 	struct lpfc_iocbq *cmdiocb;
1215 
1216 	cmdiocb = (struct lpfc_iocbq *) arg;
1217 	lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
1218 	return ndlp->nlp_state;
1219 }
1220 
1221 static uint32_t
1222 lpfc_cmpl_prli_prli_issue(struct lpfc_hba * phba,
1223 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1224 {
1225 	struct lpfc_iocbq *cmdiocb, *rspiocb;
1226 	IOCB_t *irsp;
1227 	PRLI *npr;
1228 
1229 	cmdiocb = (struct lpfc_iocbq *) arg;
1230 	rspiocb = cmdiocb->context_un.rsp_iocb;
1231 	npr = (PRLI *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1232 
1233 	irsp = &rspiocb->iocb;
1234 	if (irsp->ulpStatus) {
1235 		ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1236 		ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
1237 		lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
1238 		return ndlp->nlp_state;
1239 	}
1240 
1241 	/* Check out PRLI rsp */
1242 	ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1243 	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
1244 	if ((npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
1245 	    (npr->prliType == PRLI_FCP_TYPE)) {
1246 		if (npr->initiatorFunc)
1247 			ndlp->nlp_type |= NLP_FCP_INITIATOR;
1248 		if (npr->targetFunc)
1249 			ndlp->nlp_type |= NLP_FCP_TARGET;
1250 		if (npr->Retry)
1251 			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
1252 	}
1253 
1254 	ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1255 	ndlp->nlp_state = NLP_STE_MAPPED_NODE;
1256 	lpfc_nlp_list(phba, ndlp, NLP_MAPPED_LIST);
1257 	return ndlp->nlp_state;
1258 }
1259 
1260 /*! lpfc_device_rm_prli_issue
1261   *
1262   * \pre
1263   * \post
1264   * \param   phba
1265   * \param   ndlp
1266   * \param   arg
1267   * \param   evt
1268   * \return  uint32_t
1269   *
1270   * \b Description:
1271   *    This routine is envoked when we a request to remove a nport we are in the
1272   *    process of PRLIing. We should software abort outstanding prli, unreg
1273   *    login, send a logout. We will change node state to UNUSED_NODE, put it
1274   *    on plogi list so it can be freed when LOGO completes.
1275   *
1276   */
1277 static uint32_t
1278 lpfc_device_rm_prli_issue(struct lpfc_hba * phba,
1279 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1280 {
1281 	/* software abort outstanding PRLI */
1282 	lpfc_els_abort(phba, ndlp, 1);
1283 
1284 	lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1285 	return NLP_STE_FREED_NODE;
1286 }
1287 
1288 
1289 /*! lpfc_device_recov_prli_issue
1290   *
1291   * \pre
1292   * \post
1293   * \param   phba
1294   * \param   ndlp
1295   * \param   arg
1296   * \param   evt
1297   * \return  uint32_t
1298   *
1299   * \b Description:
1300   *    The routine is envoked when the state of a device is unknown, like
1301   *    during a link down. We should remove the nodelist entry from the
1302   *    unmapped list, issue a UNREG_LOGIN, do a software abort of the
1303   *    outstanding PRLI command, then free the node entry.
1304   */
1305 static uint32_t
1306 lpfc_device_recov_prli_issue(struct lpfc_hba * phba,
1307 			   struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1308 {
1309 	/* software abort outstanding PRLI */
1310 	lpfc_els_abort(phba, ndlp, 1);
1311 
1312 	ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1313 	ndlp->nlp_state = NLP_STE_NPR_NODE;
1314 	lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1315 	spin_lock_irq(phba->host->host_lock);
1316 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1317 	spin_unlock_irq(phba->host->host_lock);
1318 	return ndlp->nlp_state;
1319 }
1320 
1321 static uint32_t
1322 lpfc_rcv_plogi_unmap_node(struct lpfc_hba * phba,
1323 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1324 {
1325 	struct lpfc_iocbq *cmdiocb;
1326 
1327 	cmdiocb = (struct lpfc_iocbq *) arg;
1328 
1329 	lpfc_rcv_plogi(phba, ndlp, cmdiocb);
1330 	return ndlp->nlp_state;
1331 }
1332 
1333 static uint32_t
1334 lpfc_rcv_prli_unmap_node(struct lpfc_hba * phba,
1335 			 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1336 {
1337 	struct lpfc_iocbq *cmdiocb;
1338 
1339 	cmdiocb = (struct lpfc_iocbq *) arg;
1340 
1341 	lpfc_rcv_prli(phba, ndlp, cmdiocb);
1342 	lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
1343 	return ndlp->nlp_state;
1344 }
1345 
1346 static uint32_t
1347 lpfc_rcv_logo_unmap_node(struct lpfc_hba * phba,
1348 			 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1349 {
1350 	struct lpfc_iocbq *cmdiocb;
1351 
1352 	cmdiocb = (struct lpfc_iocbq *) arg;
1353 
1354 	lpfc_rcv_logo(phba, ndlp, cmdiocb);
1355 	return ndlp->nlp_state;
1356 }
1357 
1358 static uint32_t
1359 lpfc_rcv_padisc_unmap_node(struct lpfc_hba * phba,
1360 			   struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1361 {
1362 	struct lpfc_iocbq *cmdiocb;
1363 
1364 	cmdiocb = (struct lpfc_iocbq *) arg;
1365 
1366 	lpfc_rcv_padisc(phba, ndlp, cmdiocb);
1367 	return ndlp->nlp_state;
1368 }
1369 
1370 static uint32_t
1371 lpfc_rcv_prlo_unmap_node(struct lpfc_hba * phba,
1372 			 struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1373 {
1374 	struct lpfc_iocbq *cmdiocb;
1375 
1376 	cmdiocb = (struct lpfc_iocbq *) arg;
1377 
1378 	lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
1379 	return ndlp->nlp_state;
1380 }
1381 
1382 static uint32_t
1383 lpfc_device_recov_unmap_node(struct lpfc_hba * phba,
1384 			   struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1385 {
1386 	ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
1387 	ndlp->nlp_state = NLP_STE_NPR_NODE;
1388 	lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1389 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1390 	lpfc_disc_set_adisc(phba, ndlp);
1391 
1392 	return ndlp->nlp_state;
1393 }
1394 
1395 static uint32_t
1396 lpfc_rcv_plogi_mapped_node(struct lpfc_hba * phba,
1397 			   struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1398 {
1399 	struct lpfc_iocbq *cmdiocb;
1400 
1401 	cmdiocb = (struct lpfc_iocbq *) arg;
1402 
1403 	lpfc_rcv_plogi(phba, ndlp, cmdiocb);
1404 	return ndlp->nlp_state;
1405 }
1406 
1407 static uint32_t
1408 lpfc_rcv_prli_mapped_node(struct lpfc_hba * phba,
1409 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1410 {
1411 	struct lpfc_iocbq *cmdiocb;
1412 
1413 	cmdiocb = (struct lpfc_iocbq *) arg;
1414 
1415 	lpfc_els_rsp_prli_acc(phba, cmdiocb, ndlp);
1416 	return ndlp->nlp_state;
1417 }
1418 
1419 static uint32_t
1420 lpfc_rcv_logo_mapped_node(struct lpfc_hba * phba,
1421 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1422 {
1423 	struct lpfc_iocbq *cmdiocb;
1424 
1425 	cmdiocb = (struct lpfc_iocbq *) arg;
1426 
1427 	lpfc_rcv_logo(phba, ndlp, cmdiocb);
1428 	return ndlp->nlp_state;
1429 }
1430 
1431 static uint32_t
1432 lpfc_rcv_padisc_mapped_node(struct lpfc_hba * phba,
1433 			    struct lpfc_nodelist * ndlp, void *arg,
1434 			    uint32_t evt)
1435 {
1436 	struct lpfc_iocbq *cmdiocb;
1437 
1438 	cmdiocb = (struct lpfc_iocbq *) arg;
1439 
1440 	lpfc_rcv_padisc(phba, ndlp, cmdiocb);
1441 	return ndlp->nlp_state;
1442 }
1443 
1444 static uint32_t
1445 lpfc_rcv_prlo_mapped_node(struct lpfc_hba * phba,
1446 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1447 {
1448 	struct lpfc_iocbq *cmdiocb;
1449 
1450 	cmdiocb = (struct lpfc_iocbq *) arg;
1451 
1452 	/* flush the target */
1453 	spin_lock_irq(phba->host->host_lock);
1454 	lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1455 			       ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
1456 	spin_unlock_irq(phba->host->host_lock);
1457 
1458 	/* Treat like rcv logo */
1459 	lpfc_rcv_logo(phba, ndlp, cmdiocb);
1460 	return ndlp->nlp_state;
1461 }
1462 
1463 static uint32_t
1464 lpfc_device_recov_mapped_node(struct lpfc_hba * phba,
1465 			    struct lpfc_nodelist * ndlp, void *arg,
1466 			    uint32_t evt)
1467 {
1468 	ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
1469 	ndlp->nlp_state = NLP_STE_NPR_NODE;
1470 	lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1471 	spin_lock_irq(phba->host->host_lock);
1472 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1473 	spin_unlock_irq(phba->host->host_lock);
1474 	lpfc_disc_set_adisc(phba, ndlp);
1475 	return ndlp->nlp_state;
1476 }
1477 
1478 static uint32_t
1479 lpfc_rcv_plogi_npr_node(struct lpfc_hba * phba,
1480 			    struct lpfc_nodelist * ndlp, void *arg,
1481 			    uint32_t evt)
1482 {
1483 	struct lpfc_iocbq *cmdiocb;
1484 
1485 	cmdiocb = (struct lpfc_iocbq *) arg;
1486 
1487 	/* Ignore PLOGI if we have an outstanding LOGO */
1488 	if (ndlp->nlp_flag & NLP_LOGO_SND) {
1489 		return ndlp->nlp_state;
1490 	}
1491 
1492 	if (lpfc_rcv_plogi(phba, ndlp, cmdiocb)) {
1493 		spin_lock_irq(phba->host->host_lock);
1494 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1495 		spin_unlock_irq(phba->host->host_lock);
1496 		return ndlp->nlp_state;
1497 	}
1498 
1499 	/* send PLOGI immediately, move to PLOGI issue state */
1500 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1501 		ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
1502 		ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1503 		lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
1504 		lpfc_issue_els_plogi(phba, ndlp->nlp_DID, 0);
1505 	}
1506 
1507 	return ndlp->nlp_state;
1508 }
1509 
1510 static uint32_t
1511 lpfc_rcv_prli_npr_node(struct lpfc_hba * phba,
1512 			    struct lpfc_nodelist * ndlp, void *arg,
1513 			    uint32_t evt)
1514 {
1515 	struct lpfc_iocbq     *cmdiocb;
1516 	struct ls_rjt          stat;
1517 
1518 	cmdiocb = (struct lpfc_iocbq *) arg;
1519 
1520 	memset(&stat, 0, sizeof (struct ls_rjt));
1521 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1522 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1523 	lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
1524 
1525 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1526 		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
1527 			spin_lock_irq(phba->host->host_lock);
1528 			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1529 			spin_unlock_irq(phba->host->host_lock);
1530 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
1531 			ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
1532 			lpfc_nlp_list(phba, ndlp, NLP_ADISC_LIST);
1533 			lpfc_issue_els_adisc(phba, ndlp, 0);
1534 		} else {
1535 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
1536 			ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1537 			lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
1538 			lpfc_issue_els_plogi(phba, ndlp->nlp_DID, 0);
1539 		}
1540 
1541 	}
1542 	return ndlp->nlp_state;
1543 }
1544 
1545 static uint32_t
1546 lpfc_rcv_logo_npr_node(struct lpfc_hba * phba,
1547 			    struct lpfc_nodelist * ndlp, void *arg,
1548 			    uint32_t evt)
1549 {
1550 	struct lpfc_iocbq     *cmdiocb;
1551 
1552 	cmdiocb = (struct lpfc_iocbq *) arg;
1553 
1554 	lpfc_rcv_logo(phba, ndlp, cmdiocb);
1555 	return ndlp->nlp_state;
1556 }
1557 
1558 static uint32_t
1559 lpfc_rcv_padisc_npr_node(struct lpfc_hba * phba,
1560 			    struct lpfc_nodelist * ndlp, void *arg,
1561 			    uint32_t evt)
1562 {
1563 	struct lpfc_iocbq     *cmdiocb;
1564 
1565 	cmdiocb = (struct lpfc_iocbq *) arg;
1566 
1567 	lpfc_rcv_padisc(phba, ndlp, cmdiocb);
1568 
1569 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1570 		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
1571 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
1572 			ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
1573 			lpfc_nlp_list(phba, ndlp, NLP_ADISC_LIST);
1574 			lpfc_issue_els_adisc(phba, ndlp, 0);
1575 		} else {
1576 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
1577 			ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1578 			lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
1579 			lpfc_issue_els_plogi(phba, ndlp->nlp_DID, 0);
1580 		}
1581 	}
1582 	return ndlp->nlp_state;
1583 }
1584 
1585 static uint32_t
1586 lpfc_rcv_prlo_npr_node(struct lpfc_hba * phba,
1587 			    struct lpfc_nodelist * ndlp, void *arg,
1588 			    uint32_t evt)
1589 {
1590 	struct lpfc_iocbq     *cmdiocb;
1591 
1592 	cmdiocb = (struct lpfc_iocbq *) arg;
1593 
1594 	spin_lock_irq(phba->host->host_lock);
1595 	ndlp->nlp_flag |= NLP_LOGO_ACC;
1596 	spin_unlock_irq(phba->host->host_lock);
1597 
1598 	lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
1599 
1600 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1601 		mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
1602 		spin_lock_irq(phba->host->host_lock);
1603 		ndlp->nlp_flag |= NLP_DELAY_TMO;
1604 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1605 		spin_unlock_irq(phba->host->host_lock);
1606 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1607 	} else {
1608 		spin_lock_irq(phba->host->host_lock);
1609 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1610 		spin_unlock_irq(phba->host->host_lock);
1611 	}
1612 	return ndlp->nlp_state;
1613 }
1614 
1615 static uint32_t
1616 lpfc_cmpl_plogi_npr_node(struct lpfc_hba * phba,
1617 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1618 {
1619 	struct lpfc_iocbq *cmdiocb, *rspiocb;
1620 
1621 	cmdiocb = (struct lpfc_iocbq *) arg;
1622 	rspiocb = cmdiocb->context_un.rsp_iocb;
1623 	return ndlp->nlp_state;
1624 }
1625 
1626 static uint32_t
1627 lpfc_cmpl_prli_npr_node(struct lpfc_hba * phba,
1628 			  struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1629 {
1630 	struct lpfc_iocbq *cmdiocb, *rspiocb;
1631 
1632 	cmdiocb = (struct lpfc_iocbq *) arg;
1633 	rspiocb = cmdiocb->context_un.rsp_iocb;
1634 	return ndlp->nlp_state;
1635 }
1636 
1637 static uint32_t
1638 lpfc_cmpl_logo_npr_node(struct lpfc_hba * phba,
1639 		struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1640 {
1641 	lpfc_unreg_rpi(phba, ndlp);
1642 	/* This routine does nothing, just return the current state */
1643 	return ndlp->nlp_state;
1644 }
1645 
1646 static uint32_t
1647 lpfc_cmpl_adisc_npr_node(struct lpfc_hba * phba,
1648 			    struct lpfc_nodelist * ndlp, void *arg,
1649 			    uint32_t evt)
1650 {
1651 	struct lpfc_iocbq *cmdiocb, *rspiocb;
1652 
1653 	cmdiocb = (struct lpfc_iocbq *) arg;
1654 	rspiocb = cmdiocb->context_un.rsp_iocb;
1655 	return ndlp->nlp_state;
1656 }
1657 
1658 static uint32_t
1659 lpfc_cmpl_reglogin_npr_node(struct lpfc_hba * phba,
1660 			    struct lpfc_nodelist * ndlp, void *arg,
1661 			    uint32_t evt)
1662 {
1663 	LPFC_MBOXQ_t *pmb;
1664 	MAILBOX_t *mb;
1665 
1666 	pmb = (LPFC_MBOXQ_t *) arg;
1667 	mb = &pmb->mb;
1668 
1669 	if (!mb->mbxStatus)
1670 		ndlp->nlp_rpi = mb->un.varWords[0];
1671 
1672 	return ndlp->nlp_state;
1673 }
1674 
1675 static uint32_t
1676 lpfc_device_rm_npr_node(struct lpfc_hba * phba,
1677 			    struct lpfc_nodelist * ndlp, void *arg,
1678 			    uint32_t evt)
1679 {
1680 	lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1681 	return NLP_STE_FREED_NODE;
1682 }
1683 
1684 static uint32_t
1685 lpfc_device_recov_npr_node(struct lpfc_hba * phba,
1686 			    struct lpfc_nodelist * ndlp, void *arg,
1687 			    uint32_t evt)
1688 {
1689 	spin_lock_irq(phba->host->host_lock);
1690 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1691 	spin_unlock_irq(phba->host->host_lock);
1692 	if (ndlp->nlp_flag & NLP_DELAY_TMO) {
1693 		lpfc_cancel_retry_delay_tmo(phba, ndlp);
1694 	}
1695 	return ndlp->nlp_state;
1696 }
1697 
1698 
1699 /* This next section defines the NPort Discovery State Machine */
1700 
1701 /* There are 4 different double linked lists nodelist entries can reside on.
1702  * The plogi list and adisc list are used when Link Up discovery or RSCN
1703  * processing is needed. Each list holds the nodes that we will send PLOGI
1704  * or ADISC on. These lists will keep track of what nodes will be effected
1705  * by an RSCN, or a Link Up (Typically, all nodes are effected on Link Up).
1706  * The unmapped_list will contain all nodes that we have successfully logged
1707  * into at the Fibre Channel level. The mapped_list will contain all nodes
1708  * that are mapped FCP targets.
1709  */
1710 /*
1711  * The bind list is a list of undiscovered (potentially non-existent) nodes
1712  * that we have saved binding information on. This information is used when
1713  * nodes transition from the unmapped to the mapped list.
1714  */
1715 /* For UNUSED_NODE state, the node has just been allocated .
1716  * For PLOGI_ISSUE and REG_LOGIN_ISSUE, the node is on
1717  * the PLOGI list. For REG_LOGIN_COMPL, the node is taken off the PLOGI list
1718  * and put on the unmapped list. For ADISC processing, the node is taken off
1719  * the ADISC list and placed on either the mapped or unmapped list (depending
1720  * on its previous state). Once on the unmapped list, a PRLI is issued and the
1721  * state changed to PRLI_ISSUE. When the PRLI completion occurs, the state is
1722  * changed to UNMAPPED_NODE. If the completion indicates a mapped
1723  * node, the node is taken off the unmapped list. The binding list is checked
1724  * for a valid binding, or a binding is automatically assigned. If binding
1725  * assignment is unsuccessful, the node is left on the unmapped list. If
1726  * binding assignment is successful, the associated binding list entry (if
1727  * any) is removed, and the node is placed on the mapped list.
1728  */
1729 /*
1730  * For a Link Down, all nodes on the ADISC, PLOGI, unmapped or mapped
1731  * lists will receive a DEVICE_RECOVERY event. If the linkdown or nodev timers
1732  * expire, all effected nodes will receive a DEVICE_RM event.
1733  */
1734 /*
1735  * For a Link Up or RSCN, all nodes will move from the mapped / unmapped lists
1736  * to either the ADISC or PLOGI list.  After a Nameserver query or ALPA loopmap
1737  * check, additional nodes may be added or removed (via DEVICE_RM) to / from
1738  * the PLOGI or ADISC lists. Once the PLOGI and ADISC lists are populated,
1739  * we will first process the ADISC list.  32 entries are processed initially and
1740  * ADISC is initited for each one.  Completions / Events for each node are
1741  * funnelled thru the state machine.  As each node finishes ADISC processing, it
1742  * starts ADISC for any nodes waiting for ADISC processing. If no nodes are
1743  * waiting, and the ADISC list count is identically 0, then we are done. For
1744  * Link Up discovery, since all nodes on the PLOGI list are UNREG_LOGIN'ed, we
1745  * can issue a CLEAR_LA and reenable Link Events. Next we will process the PLOGI
1746  * list.  32 entries are processed initially and PLOGI is initited for each one.
1747  * Completions / Events for each node are funnelled thru the state machine.  As
1748  * each node finishes PLOGI processing, it starts PLOGI for any nodes waiting
1749  * for PLOGI processing. If no nodes are waiting, and the PLOGI list count is
1750  * indentically 0, then we are done. We have now completed discovery / RSCN
1751  * handling. Upon completion, ALL nodes should be on either the mapped or
1752  * unmapped lists.
1753  */
1754 
1755 static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
1756      (struct lpfc_hba *, struct lpfc_nodelist *, void *, uint32_t) = {
1757 	/* Action routine                  Event       Current State  */
1758 	lpfc_rcv_plogi_unused_node,	/* RCV_PLOGI   UNUSED_NODE    */
1759 	lpfc_rcv_els_unused_node,	/* RCV_PRLI        */
1760 	lpfc_rcv_logo_unused_node,	/* RCV_LOGO        */
1761 	lpfc_rcv_els_unused_node,	/* RCV_ADISC       */
1762 	lpfc_rcv_els_unused_node,	/* RCV_PDISC       */
1763 	lpfc_rcv_els_unused_node,	/* RCV_PRLO        */
1764 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
1765 	lpfc_disc_illegal,		/* CMPL_PRLI       */
1766 	lpfc_cmpl_logo_unused_node,	/* CMPL_LOGO       */
1767 	lpfc_disc_illegal,		/* CMPL_ADISC      */
1768 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
1769 	lpfc_device_rm_unused_node,	/* DEVICE_RM       */
1770 	lpfc_disc_illegal,		/* DEVICE_RECOVERY */
1771 
1772 	lpfc_rcv_plogi_plogi_issue,	/* RCV_PLOGI   PLOGI_ISSUE    */
1773 	lpfc_rcv_els_plogi_issue,	/* RCV_PRLI        */
1774 	lpfc_rcv_logo_plogi_issue,	/* RCV_LOGO        */
1775 	lpfc_rcv_els_plogi_issue,	/* RCV_ADISC       */
1776 	lpfc_rcv_els_plogi_issue,	/* RCV_PDISC       */
1777 	lpfc_rcv_els_plogi_issue,	/* RCV_PRLO        */
1778 	lpfc_cmpl_plogi_plogi_issue,	/* CMPL_PLOGI      */
1779 	lpfc_disc_illegal,		/* CMPL_PRLI       */
1780 	lpfc_disc_illegal,		/* CMPL_LOGO       */
1781 	lpfc_disc_illegal,		/* CMPL_ADISC      */
1782 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
1783 	lpfc_device_rm_plogi_issue,	/* DEVICE_RM       */
1784 	lpfc_device_recov_plogi_issue,	/* DEVICE_RECOVERY */
1785 
1786 	lpfc_rcv_plogi_adisc_issue,	/* RCV_PLOGI   ADISC_ISSUE    */
1787 	lpfc_rcv_prli_adisc_issue,	/* RCV_PRLI        */
1788 	lpfc_rcv_logo_adisc_issue,	/* RCV_LOGO        */
1789 	lpfc_rcv_padisc_adisc_issue,	/* RCV_ADISC       */
1790 	lpfc_rcv_padisc_adisc_issue,	/* RCV_PDISC       */
1791 	lpfc_rcv_prlo_adisc_issue,	/* RCV_PRLO        */
1792 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
1793 	lpfc_disc_illegal,		/* CMPL_PRLI       */
1794 	lpfc_disc_illegal,		/* CMPL_LOGO       */
1795 	lpfc_cmpl_adisc_adisc_issue,	/* CMPL_ADISC      */
1796 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
1797 	lpfc_device_rm_adisc_issue,	/* DEVICE_RM       */
1798 	lpfc_device_recov_adisc_issue,	/* DEVICE_RECOVERY */
1799 
1800 	lpfc_rcv_plogi_reglogin_issue,	/* RCV_PLOGI  REG_LOGIN_ISSUE */
1801 	lpfc_rcv_prli_reglogin_issue,	/* RCV_PLOGI       */
1802 	lpfc_rcv_logo_reglogin_issue,	/* RCV_LOGO        */
1803 	lpfc_rcv_padisc_reglogin_issue,	/* RCV_ADISC       */
1804 	lpfc_rcv_padisc_reglogin_issue,	/* RCV_PDISC       */
1805 	lpfc_rcv_prlo_reglogin_issue,	/* RCV_PRLO        */
1806 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
1807 	lpfc_disc_illegal,		/* CMPL_PRLI       */
1808 	lpfc_disc_illegal,		/* CMPL_LOGO       */
1809 	lpfc_disc_illegal,		/* CMPL_ADISC      */
1810 	lpfc_cmpl_reglogin_reglogin_issue,/* CMPL_REG_LOGIN  */
1811 	lpfc_device_rm_reglogin_issue,	/* DEVICE_RM       */
1812 	lpfc_device_recov_reglogin_issue,/* DEVICE_RECOVERY */
1813 
1814 	lpfc_rcv_plogi_prli_issue,	/* RCV_PLOGI   PRLI_ISSUE     */
1815 	lpfc_rcv_prli_prli_issue,	/* RCV_PRLI        */
1816 	lpfc_rcv_logo_prli_issue,	/* RCV_LOGO        */
1817 	lpfc_rcv_padisc_prli_issue,	/* RCV_ADISC       */
1818 	lpfc_rcv_padisc_prli_issue,	/* RCV_PDISC       */
1819 	lpfc_rcv_prlo_prli_issue,	/* RCV_PRLO        */
1820 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
1821 	lpfc_cmpl_prli_prli_issue,	/* CMPL_PRLI       */
1822 	lpfc_disc_illegal,		/* CMPL_LOGO       */
1823 	lpfc_disc_illegal,		/* CMPL_ADISC      */
1824 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
1825 	lpfc_device_rm_prli_issue,	/* DEVICE_RM       */
1826 	lpfc_device_recov_prli_issue,	/* DEVICE_RECOVERY */
1827 
1828 	lpfc_rcv_plogi_unmap_node,	/* RCV_PLOGI   UNMAPPED_NODE  */
1829 	lpfc_rcv_prli_unmap_node,	/* RCV_PRLI        */
1830 	lpfc_rcv_logo_unmap_node,	/* RCV_LOGO        */
1831 	lpfc_rcv_padisc_unmap_node,	/* RCV_ADISC       */
1832 	lpfc_rcv_padisc_unmap_node,	/* RCV_PDISC       */
1833 	lpfc_rcv_prlo_unmap_node,	/* RCV_PRLO        */
1834 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
1835 	lpfc_disc_illegal,		/* CMPL_PRLI       */
1836 	lpfc_disc_illegal,		/* CMPL_LOGO       */
1837 	lpfc_disc_illegal,		/* CMPL_ADISC      */
1838 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
1839 	lpfc_disc_illegal,		/* DEVICE_RM       */
1840 	lpfc_device_recov_unmap_node,	/* DEVICE_RECOVERY */
1841 
1842 	lpfc_rcv_plogi_mapped_node,	/* RCV_PLOGI   MAPPED_NODE    */
1843 	lpfc_rcv_prli_mapped_node,	/* RCV_PRLI        */
1844 	lpfc_rcv_logo_mapped_node,	/* RCV_LOGO        */
1845 	lpfc_rcv_padisc_mapped_node,	/* RCV_ADISC       */
1846 	lpfc_rcv_padisc_mapped_node,	/* RCV_PDISC       */
1847 	lpfc_rcv_prlo_mapped_node,	/* RCV_PRLO        */
1848 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
1849 	lpfc_disc_illegal,		/* CMPL_PRLI       */
1850 	lpfc_disc_illegal,		/* CMPL_LOGO       */
1851 	lpfc_disc_illegal,		/* CMPL_ADISC      */
1852 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
1853 	lpfc_disc_illegal,		/* DEVICE_RM       */
1854 	lpfc_device_recov_mapped_node,	/* DEVICE_RECOVERY */
1855 
1856 	lpfc_rcv_plogi_npr_node,        /* RCV_PLOGI   NPR_NODE    */
1857 	lpfc_rcv_prli_npr_node,         /* RCV_PRLI        */
1858 	lpfc_rcv_logo_npr_node,         /* RCV_LOGO        */
1859 	lpfc_rcv_padisc_npr_node,       /* RCV_ADISC       */
1860 	lpfc_rcv_padisc_npr_node,       /* RCV_PDISC       */
1861 	lpfc_rcv_prlo_npr_node,         /* RCV_PRLO        */
1862 	lpfc_cmpl_plogi_npr_node,	/* CMPL_PLOGI      */
1863 	lpfc_cmpl_prli_npr_node,	/* CMPL_PRLI       */
1864 	lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
1865 	lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
1866 	lpfc_cmpl_reglogin_npr_node,    /* CMPL_REG_LOGIN  */
1867 	lpfc_device_rm_npr_node,        /* DEVICE_RM       */
1868 	lpfc_device_recov_npr_node,     /* DEVICE_RECOVERY */
1869 };
1870 
1871 int
1872 lpfc_disc_state_machine(struct lpfc_hba * phba,
1873 			struct lpfc_nodelist * ndlp, void *arg, uint32_t evt)
1874 {
1875 	uint32_t cur_state, rc;
1876 	uint32_t(*func) (struct lpfc_hba *, struct lpfc_nodelist *, void *,
1877 			 uint32_t);
1878 
1879 	ndlp->nlp_disc_refcnt++;
1880 	cur_state = ndlp->nlp_state;
1881 
1882 	/* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
1883 	lpfc_printf_log(phba,
1884 			KERN_INFO,
1885 			LOG_DISCOVERY,
1886 			"%d:0211 DSM in event x%x on NPort x%x in state %d "
1887 			"Data: x%x\n",
1888 			phba->brd_no,
1889 			evt, ndlp->nlp_DID, cur_state, ndlp->nlp_flag);
1890 
1891 	func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
1892 	rc = (func) (phba, ndlp, arg, evt);
1893 
1894 	/* DSM out state <rc> on NPort <nlp_DID> */
1895 	lpfc_printf_log(phba,
1896 		       KERN_INFO,
1897 		       LOG_DISCOVERY,
1898 		       "%d:0212 DSM out state %d on NPort x%x Data: x%x\n",
1899 		       phba->brd_no,
1900 		       rc, ndlp->nlp_DID, ndlp->nlp_flag);
1901 
1902 	ndlp->nlp_disc_refcnt--;
1903 
1904 	/* Check to see if ndlp removal is deferred */
1905 	if ((ndlp->nlp_disc_refcnt == 0)
1906 	    && (ndlp->nlp_flag & NLP_DELAY_REMOVE)) {
1907 		spin_lock_irq(phba->host->host_lock);
1908 		ndlp->nlp_flag &= ~NLP_DELAY_REMOVE;
1909 		spin_unlock_irq(phba->host->host_lock);
1910 		lpfc_nlp_remove(phba, ndlp);
1911 		return NLP_STE_FREED_NODE;
1912 	}
1913 	if (rc == NLP_STE_FREED_NODE)
1914 		return NLP_STE_FREED_NODE;
1915 	return rc;
1916 }
1917