lpfc_nportdisc.c 91.0 KB
Newer Older
1
/*******************************************************************
已提交
2
 * This file is part of the Emulex Linux Device Driver for         *
3
 * Fibre Channel Host Bus Adapters.                                *
4
 * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
5
 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6
 * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7
 * EMULEX and SLI are trademarks of Emulex.                        *
J
James Smart 已提交
8
 * www.broadcom.com                                                *
9
 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
已提交
10 11
 *                                                                 *
 * This program is free software; you can redistribute it and/or   *
12 13 14 15 16 17 18 19 20 21
 * modify it under the terms of version 2 of the GNU General       *
 * Public License as published by the Free Software Foundation.    *
 * This program is distributed in the hope that it will be useful. *
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
 * more details, a copy of which can be found in the file COPYING  *
 * included with this package.                                     *
已提交
22 23 24 25
 *******************************************************************/

#include <linux/blkdev.h>
#include <linux/pci.h>
26
#include <linux/slab.h>
已提交
27 28
#include <linux/interrupt.h>

29
#include <scsi/scsi.h>
已提交
30 31 32
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_transport_fc.h>
33 34 35
#include <scsi/fc/fc_fs.h>

#include <linux/nvme-fc-driver.h>
已提交
36

37
#include "lpfc_hw4.h"
已提交
38 39
#include "lpfc_hw.h"
#include "lpfc_sli.h"
40
#include "lpfc_sli4.h"
41
#include "lpfc_nl.h"
已提交
42 43
#include "lpfc_disc.h"
#include "lpfc.h"
44 45
#include "lpfc_scsi.h"
#include "lpfc_nvme.h"
已提交
46 47
#include "lpfc_logmsg.h"
#include "lpfc_crtn.h"
48
#include "lpfc_vport.h"
J
James Smart 已提交
49
#include "lpfc_debugfs.h"
已提交
50 51 52 53


/* Called to verify a rcv'ed ADISC was intended for us. */
static int
J
James Smart 已提交
54 55
lpfc_check_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		 struct lpfc_name *nn, struct lpfc_name *pn)
已提交
56
{
57 58 59 60
	/* First, we MUST have a RPI registered */
	if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED))
		return 0;

已提交
61 62 63
	/* Compare the ADISC rsp WWNN / WWPN matches our internal node
	 * table entry for that node.
	 */
J
James Smart 已提交
64
	if (memcmp(nn, &ndlp->nlp_nodename, sizeof (struct lpfc_name)))
65
		return 0;
已提交
66

J
James Smart 已提交
67
	if (memcmp(pn, &ndlp->nlp_portname, sizeof (struct lpfc_name)))
68
		return 0;
已提交
69 70

	/* we match, return success */
71
	return 1;
已提交
72 73 74
}

int
J
James Smart 已提交
75
lpfc_check_sparm(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
76
		 struct serv_parm *sp, uint32_t class, int flogi)
已提交
77
{
J
James Smart 已提交
78
	volatile struct serv_parm *hsp = &vport->fc_sparam;
79 80 81 82 83 84 85 86 87
	uint16_t hsp_value, ssp_value = 0;

	/*
	 * The receive data field size and buffer-to-buffer receive data field
	 * size entries are 16 bits but are represented as two 8-bit fields in
	 * the driver data structure to account for rsvd bits and other control
	 * bits.  Reconstruct and compare the fields as a 16-bit values before
	 * correcting the byte values.
	 */
已提交
88
	if (sp->cls1.classValid) {
89 90 91 92 93 94 95 96 97 98 99 100 101
		if (!flogi) {
			hsp_value = ((hsp->cls1.rcvDataSizeMsb << 8) |
				     hsp->cls1.rcvDataSizeLsb);
			ssp_value = ((sp->cls1.rcvDataSizeMsb << 8) |
				     sp->cls1.rcvDataSizeLsb);
			if (!ssp_value)
				goto bad_service_param;
			if (ssp_value > hsp_value) {
				sp->cls1.rcvDataSizeLsb =
					hsp->cls1.rcvDataSizeLsb;
				sp->cls1.rcvDataSizeMsb =
					hsp->cls1.rcvDataSizeMsb;
			}
102
		}
103
	} else if (class == CLASS1)
104
		goto bad_service_param;
已提交
105
	if (sp->cls2.classValid) {
106 107 108 109 110 111 112 113 114 115 116 117 118
		if (!flogi) {
			hsp_value = ((hsp->cls2.rcvDataSizeMsb << 8) |
				     hsp->cls2.rcvDataSizeLsb);
			ssp_value = ((sp->cls2.rcvDataSizeMsb << 8) |
				     sp->cls2.rcvDataSizeLsb);
			if (!ssp_value)
				goto bad_service_param;
			if (ssp_value > hsp_value) {
				sp->cls2.rcvDataSizeLsb =
					hsp->cls2.rcvDataSizeLsb;
				sp->cls2.rcvDataSizeMsb =
					hsp->cls2.rcvDataSizeMsb;
			}
119
		}
120
	} else if (class == CLASS2)
121
		goto bad_service_param;
已提交
122
	if (sp->cls3.classValid) {
123 124 125 126 127 128 129 130 131 132 133 134 135
		if (!flogi) {
			hsp_value = ((hsp->cls3.rcvDataSizeMsb << 8) |
				     hsp->cls3.rcvDataSizeLsb);
			ssp_value = ((sp->cls3.rcvDataSizeMsb << 8) |
				     sp->cls3.rcvDataSizeLsb);
			if (!ssp_value)
				goto bad_service_param;
			if (ssp_value > hsp_value) {
				sp->cls3.rcvDataSizeLsb =
					hsp->cls3.rcvDataSizeLsb;
				sp->cls3.rcvDataSizeMsb =
					hsp->cls3.rcvDataSizeMsb;
			}
136
		}
137
	} else if (class == CLASS3)
138
		goto bad_service_param;
已提交
139

140 141 142 143 144 145 146 147
	/*
	 * Preserve the upper four bits of the MSB from the PLOGI response.
	 * These bits contain the Buffer-to-Buffer State Change Number
	 * from the target and need to be passed to the FW.
	 */
	hsp_value = (hsp->cmn.bbRcvSizeMsb << 8) | hsp->cmn.bbRcvSizeLsb;
	ssp_value = (sp->cmn.bbRcvSizeMsb << 8) | sp->cmn.bbRcvSizeLsb;
	if (ssp_value > hsp_value) {
已提交
148
		sp->cmn.bbRcvSizeLsb = hsp->cmn.bbRcvSizeLsb;
149 150 151
		sp->cmn.bbRcvSizeMsb = (sp->cmn.bbRcvSizeMsb & 0xF0) |
				       (hsp->cmn.bbRcvSizeMsb & 0x0F);
	}
已提交
152 153 154

	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof (struct lpfc_name));
155
	return 1;
156
bad_service_param:
157 158 159 160 161 162 163 164 165
	lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
			 "0207 Device %x "
			 "(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x) sent "
			 "invalid service parameters.  Ignoring device.\n",
			 ndlp->nlp_DID,
			 sp->nodeName.u.wwn[0], sp->nodeName.u.wwn[1],
			 sp->nodeName.u.wwn[2], sp->nodeName.u.wwn[3],
			 sp->nodeName.u.wwn[4], sp->nodeName.u.wwn[5],
			 sp->nodeName.u.wwn[6], sp->nodeName.u.wwn[7]);
166
	return 0;
已提交
167 168 169
}

static void *
J
James Smart 已提交
170
lpfc_check_elscmpl_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
171
			struct lpfc_iocbq *rspiocb)
已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
{
	struct lpfc_dmabuf *pcmd, *prsp;
	uint32_t *lp;
	void     *ptr = NULL;
	IOCB_t   *irsp;

	irsp = &rspiocb->iocb;
	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;

	/* For lpfc_els_abort, context2 could be zero'ed to delay
	 * freeing associated memory till after ABTS completes.
	 */
	if (pcmd) {
		prsp =  list_get_first(&pcmd->list, struct lpfc_dmabuf,
				       list);
		if (prsp) {
			lp = (uint32_t *) prsp->virt;
			ptr = (void *)((uint8_t *)lp + sizeof(uint32_t));
		}
191
	} else {
已提交
192 193 194 195 196 197 198
		/* Force ulpStatus error since we are returning NULL ptr */
		if (!(irsp->ulpStatus)) {
			irsp->ulpStatus = IOSTAT_LOCAL_REJECT;
			irsp->un.ulpWord[4] = IOERR_SLI_ABORTED;
		}
		ptr = NULL;
	}
199
	return ptr;
已提交
200 201 202
}


203

已提交
204 205 206 207 208
/*
 * Free resources / clean up outstanding I/Os
 * associated with a LPFC_NODELIST entry. This
 * routine effectively results in a "software abort".
 */
209
void
J
James Smart 已提交
210
lpfc_els_abort(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
已提交
211
{
212
	LIST_HEAD(abort_list);
213
	struct lpfc_sli_ring *pring;
已提交
214 215
	struct lpfc_iocbq *iocb, *next_iocb;

216 217
	pring = lpfc_phba_elsring(phba);

218
	/* In case of error recovery path, we might have a NULL pring here */
219
	if (unlikely(!pring))
220 221
		return;

已提交
222
	/* Abort outstanding I/O on NPort <nlp_DID> */
223
	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_DISCOVERY,
224
			 "2819 Abort outstanding I/O on NPort x%x "
225 226 227
			 "Data: x%x x%x x%x\n",
			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
			 ndlp->nlp_rpi);
228
	/* Clean up all fabric IOs first.*/
229
	lpfc_fabric_abort_nport(ndlp);
已提交
230

231 232 233 234 235
	/*
	 * Lock the ELS ring txcmplq for SLI3/SLI4 and build a local list
	 * of all ELS IOs that need an ABTS.  The IOs need to stay on the
	 * txcmplq so that the abort operation completes them successfully.
	 */
J
James Smart 已提交
236
	spin_lock_irq(&phba->hbalock);
237 238 239 240
	if (phba->sli_rev == LPFC_SLI_REV4)
		spin_lock(&pring->ring_lock);
	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
	/* Add to abort_list on on NDLP match. */
241 242
		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
			list_add_tail(&iocb->dlist, &abort_list);
243
	}
244 245
	if (phba->sli_rev == LPFC_SLI_REV4)
		spin_unlock(&pring->ring_lock);
J
James Smart 已提交
246
	spin_unlock_irq(&phba->hbalock);
已提交
247

248
	/* Abort the targeted IOs and remove them from the abort list. */
249 250 251 252 253 254 255
	list_for_each_entry_safe(iocb, next_iocb, &abort_list, dlist) {
			spin_lock_irq(&phba->hbalock);
			list_del_init(&iocb->dlist);
			lpfc_sli_issue_abort_iotag(phba, pring, iocb);
			spin_unlock_irq(&phba->hbalock);
	}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	INIT_LIST_HEAD(&abort_list);

	/* Now process the txq */
	spin_lock_irq(&phba->hbalock);
	if (phba->sli_rev == LPFC_SLI_REV4)
		spin_lock(&pring->ring_lock);

	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
		/* Check to see if iocb matches the nport we are looking for */
		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp)) {
			list_del_init(&iocb->list);
			list_add_tail(&iocb->list, &abort_list);
		}
	}

	if (phba->sli_rev == LPFC_SLI_REV4)
		spin_unlock(&pring->ring_lock);
	spin_unlock_irq(&phba->hbalock);

275
	/* Cancel all the IOCBs from the completions list */
276 277
	lpfc_sli_cancel_iocbs(phba, &abort_list,
			      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
278

279
	lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
已提交
280 281
}

282 283 284 285 286 287 288
/* lpfc_defer_pt2pt_acc - Complete SLI3 pt2pt processing on link up
 * @phba: pointer to lpfc hba data structure.
 * @link_mbox: pointer to CONFIG_LINK mailbox object
 *
 * This routine is only called if we are SLI3, direct connect pt2pt
 * mode and the remote NPort issues the PLOGI after link up.
 */
289
static void
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
lpfc_defer_pt2pt_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *link_mbox)
{
	LPFC_MBOXQ_t *login_mbox;
	MAILBOX_t *mb = &link_mbox->u.mb;
	struct lpfc_iocbq *save_iocb;
	struct lpfc_nodelist *ndlp;
	int rc;

	ndlp = link_mbox->ctx_ndlp;
	login_mbox = link_mbox->context3;
	save_iocb = login_mbox->context3;
	link_mbox->context3 = NULL;
	login_mbox->context3 = NULL;

	/* Check for CONFIG_LINK error */
	if (mb->mbxStatus) {
		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
				"4575 CONFIG_LINK fails pt2pt discovery: %x\n",
				mb->mbxStatus);
		mempool_free(login_mbox, phba->mbox_mem_pool);
		mempool_free(link_mbox, phba->mbox_mem_pool);
		lpfc_sli_release_iocbq(phba, save_iocb);
		return;
	}

	/* Now that CONFIG_LINK completed, and our SID is configured,
	 * we can now proceed with sending the PLOGI ACC.
	 */
	rc = lpfc_els_rsp_acc(link_mbox->vport, ELS_CMD_PLOGI,
			      save_iocb, ndlp, login_mbox);
	if (rc) {
		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
				"4576 PLOGI ACC fails pt2pt discovery: %x\n",
				rc);
		mempool_free(login_mbox, phba->mbox_mem_pool);
	}

	mempool_free(link_mbox, phba->mbox_mem_pool);
	lpfc_sli_release_iocbq(phba, save_iocb);
}

已提交
331
static int
J
James Smart 已提交
332
lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
333
	       struct lpfc_iocbq *cmdiocb)
已提交
334
{
J
James Smart 已提交
335 336
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba    *phba = vport->phba;
已提交
337
	struct lpfc_dmabuf *pcmd;
338
	uint64_t nlp_portwwn = 0;
已提交
339 340 341
	uint32_t *lp;
	IOCB_t *icmd;
	struct serv_parm *sp;
342
	uint32_t ed_tov;
343 344 345
	LPFC_MBOXQ_t *link_mbox;
	LPFC_MBOXQ_t *login_mbox;
	struct lpfc_iocbq *save_iocb;
已提交
346
	struct ls_rjt stat;
347
	uint32_t vid, flag;
348
	int rc, defer_acc;
已提交
349 350 351 352 353

	memset(&stat, 0, sizeof (struct ls_rjt));
	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;
	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	if (wwn_to_u64(sp->portName.u.wwn) == 0) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0140 PLOGI Reject: invalid nname\n");
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_PNAME;
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
		return 0;
	}
	if (wwn_to_u64(sp->nodeName.u.wwn) == 0) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0141 PLOGI Reject: invalid pname\n");
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_NNAME;
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
		return 0;
	}
372 373

	nlp_portwwn = wwn_to_u64(ndlp->nlp_portname.u.wwn);
374
	if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0) == 0)) {
已提交
375 376 377
		/* Reject this request because invalid parameters */
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
J
James Smart 已提交
378 379
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
380
		return 0;
已提交
381 382 383 384
	}
	icmd = &cmdiocb->iocb;

	/* PLOGI chkparm OK */
385
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
386 387
			 "0114 PLOGI chkparm OK Data: x%x x%x x%x "
			 "x%x x%x x%x\n",
388
			 ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
389 390
			 ndlp->nlp_rpi, vport->port_state,
			 vport->fc_flag);
已提交
391

392
	if (vport->cfg_fcp_class == 2 && sp->cls2.classValid)
已提交
393
		ndlp->nlp_fcp_info |= CLASS2;
394
	else
已提交
395
		ndlp->nlp_fcp_info |= CLASS3;
J
James Smart 已提交
396

397
	defer_acc = 0;
已提交
398 399 400 401 402 403 404 405 406 407 408
	ndlp->nlp_class_sup = 0;
	if (sp->cls1.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS1;
	if (sp->cls2.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS2;
	if (sp->cls3.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS3;
	if (sp->cls4.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS4;
	ndlp->nlp_maxframe =
		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
409
	/* if already logged in, do implicit logout */
410
	switch (ndlp->nlp_state) {
已提交
411 412 413
	case  NLP_STE_NPR_NODE:
		if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
			break;
414
		/* fall through */
已提交
415 416 417 418
	case  NLP_STE_REG_LOGIN_ISSUE:
	case  NLP_STE_PRLI_ISSUE:
	case  NLP_STE_UNMAPPED_NODE:
	case  NLP_STE_MAPPED_NODE:
419 420 421 422 423 424
		/* For initiators, lpfc_plogi_confirm_nport skips fabric did.
		 * For target mode, execute implicit logo.
		 * Fabric nodes go into NPR.
		 */
		if (!(ndlp->nlp_type & NLP_FABRIC) &&
		    !(phba->nvmet_support)) {
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
			lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
					 ndlp, NULL);
			return 1;
		}
		if (nlp_portwwn != 0 &&
		    nlp_portwwn != wwn_to_u64(sp->portName.u.wwn))
			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
					 "0143 PLOGI recv'd from DID: x%x "
					 "WWPN changed: old %llx new %llx\n",
					 ndlp->nlp_DID,
					 (unsigned long long)nlp_portwwn,
					 (unsigned long long)
					 wwn_to_u64(sp->portName.u.wwn));

		ndlp->nlp_prev_state = ndlp->nlp_state;
		/* rport needs to be unregistered first */
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		break;
已提交
443 444
	}

445 446 447 448 449
	ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
	ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
	ndlp->nlp_flag &= ~NLP_FIRSTBURST;

450 451 452 453
	login_mbox = NULL;
	link_mbox = NULL;
	save_iocb = NULL;

454
	/* Check for Nport to NPort pt2pt protocol */
455 456
	if ((vport->fc_flag & FC_PT2PT) &&
	    !(vport->fc_flag & FC_PT2PT_PLOGI)) {
已提交
457
		/* rcv'ed PLOGI decides what our NPortId will be */
J
James Smart 已提交
458
		vport->fc_myDID = icmd->un.rcvels.parmRo;
459 460 461 462 463

		ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
		if (sp->cmn.edtovResolution) {
			/* E_D_TOV ticks are in nanoseconds */
			ed_tov = (phba->fc_edtov + 999999) / 1000000;
已提交
464
		}
465

466
		/*
467 468
		 * For pt-to-pt, use the larger EDTOV
		 * RATOV = 2 * EDTOV
469
		 */
470 471 472 473 474 475 476 477
		if (ed_tov > phba->fc_edtov)
			phba->fc_edtov = ed_tov;
		phba->fc_ratov = (2 * phba->fc_edtov) / 1000;

		memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));

		/* Issue config_link / reg_vfi to account for updated TOV's */

478 479
		if (phba->sli_rev == LPFC_SLI_REV4)
			lpfc_issue_reg_vfi(vport);
480
		else {
481 482 483 484
			defer_acc = 1;
			link_mbox = mempool_alloc(phba->mbox_mem_pool,
						  GFP_KERNEL);
			if (!link_mbox)
485
				goto out;
486 487 488 489 490 491 492
			lpfc_config_link(phba, link_mbox);
			link_mbox->mbox_cmpl = lpfc_defer_pt2pt_acc;
			link_mbox->vport = vport;
			link_mbox->ctx_ndlp = ndlp;

			save_iocb = lpfc_sli_get_iocbq(phba);
			if (!save_iocb)
493
				goto out;
494 495 496
			/* Save info from cmd IOCB used in rsp */
			memcpy((uint8_t *)save_iocb, (uint8_t *)cmdiocb,
			       sizeof(struct lpfc_iocbq));
497
		}
已提交
498

J
James Smart 已提交
499
		lpfc_can_disctmo(vport);
已提交
500
	}
501

502 503 504 505 506 507 508 509 510
	ndlp->nlp_flag &= ~NLP_SUPPRESS_RSP;
	if ((phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) &&
	    sp->cmn.valid_vendor_ver_level) {
		vid = be32_to_cpu(sp->un.vv.vid);
		flag = be32_to_cpu(sp->un.vv.flags);
		if ((vid == LPFC_VV_EMLX_ID) && (flag & LPFC_VV_SUPPRESS_RSP))
			ndlp->nlp_flag |= NLP_SUPPRESS_RSP;
	}

511 512
	login_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!login_mbox)
已提交
513 514
		goto out;

515 516 517 518
	/* Registering an existing RPI behaves differently for SLI3 vs SLI4 */
	if (phba->sli_rev == LPFC_SLI_REV4)
		lpfc_unreg_rpi(vport, ndlp);

519
	rc = lpfc_reg_rpi(phba, vport->vpi, icmd->un.rcvels.remoteID,
520 521
			    (uint8_t *)sp, login_mbox, ndlp->nlp_rpi);
	if (rc)
已提交
522 523 524
		goto out;

	/* ACC PLOGI rsp command needs to execute first,
525
	 * queue this login_mbox command to be processed later.
已提交
526
	 */
527
	login_mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
528
	/*
529
	 * login_mbox->ctx_ndlp = lpfc_nlp_get(ndlp) deferred until mailbox
530 531
	 * command issued in lpfc_cmpl_els_acc().
	 */
532
	login_mbox->vport = vport;
J
James Smart 已提交
533
	spin_lock_irq(shost->host_lock);
534
	ndlp->nlp_flag |= (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI);
J
James Smart 已提交
535
	spin_unlock_irq(shost->host_lock);
已提交
536

J
James Smart 已提交
537 538 539 540 541 542 543 544 545 546 547
	/*
	 * If there is an outstanding PLOGI issued, abort it before
	 * sending ACC rsp for received PLOGI. If pending plogi
	 * is not canceled here, the plogi will be rejected by
	 * remote port and will be retried. On a configuration with
	 * single discovery thread, this will cause a huge delay in
	 * discovery. Also this will cause multiple state machines
	 * running in parallel for this node.
	 */
	if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE) {
		/* software abort outstanding PLOGI */
548
		lpfc_els_abort(phba, ndlp);
J
James Smart 已提交
549 550
	}

J
James Smart 已提交
551
	if ((vport->port_type == LPFC_NPIV_PORT &&
552
	     vport->cfg_restrict_login)) {
J
James Smart 已提交
553 554 555 556 557 558 559 560 561 562 563

		/* In order to preserve RPIs, we want to cleanup
		 * the default RPI the firmware created to rcv
		 * this ELS request. The only way to do this is
		 * to register, then unregister the RPI.
		 */
		spin_lock_irq(shost->host_lock);
		ndlp->nlp_flag |= NLP_RM_DFLT_RPI;
		spin_unlock_irq(shost->host_lock);
		stat.un.b.lsRjtRsnCode = LSRJT_INVALID_CMD;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
564
		rc = lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
565
			ndlp, login_mbox);
566
		if (rc)
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
			mempool_free(login_mbox, phba->mbox_mem_pool);
		return 1;
	}
	if (defer_acc) {
		/* So the order here should be:
		 * Issue CONFIG_LINK mbox
		 * CONFIG_LINK cmpl
		 * Issue PLOGI ACC
		 * PLOGI ACC cmpl
		 * Issue REG_LOGIN mbox
		 */

		/* Save the REG_LOGIN mbox for and rcv IOCB copy later */
		link_mbox->context3 = login_mbox;
		login_mbox->context3 = save_iocb;

		/* Start the ball rolling by issuing CONFIG_LINK here */
		rc = lpfc_sli_issue_mbox(phba, link_mbox, MBX_NOWAIT);
		if (rc == MBX_NOT_FINISHED)
			goto out;
J
James Smart 已提交
587 588
		return 1;
	}
589 590

	rc = lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, login_mbox);
591
	if (rc)
592
		mempool_free(login_mbox, phba->mbox_mem_pool);
593
	return 1;
已提交
594
out:
595 596 597 598 599 600 601 602 603 604 605
	if (defer_acc)
		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
				"4577 pt2pt discovery failure: %p %p %p\n",
				save_iocb, link_mbox, login_mbox);
	if (save_iocb)
		lpfc_sli_release_iocbq(phba, save_iocb);
	if (link_mbox)
		mempool_free(link_mbox, phba->mbox_mem_pool);
	if (login_mbox)
		mempool_free(login_mbox, phba->mbox_mem_pool);

已提交
606 607
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
J
James Smart 已提交
608
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
609
	return 0;
已提交
610 611
}

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
/**
 * lpfc_mbx_cmpl_resume_rpi - Resume RPI completion routine
 * @phba: pointer to lpfc hba data structure.
 * @mboxq: pointer to mailbox object
 *
 * This routine is invoked to issue a completion to a rcv'ed
 * ADISC or PDISC after the paused RPI has been resumed.
 **/
static void
lpfc_mbx_cmpl_resume_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
{
	struct lpfc_vport *vport;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_nodelist *ndlp;
	uint32_t cmd;

628 629
	elsiocb = (struct lpfc_iocbq *)mboxq->ctx_buf;
	ndlp = (struct lpfc_nodelist *)mboxq->ctx_ndlp;
630 631 632 633 634 635 636 637 638 639
	vport = mboxq->vport;
	cmd = elsiocb->drvrTimeout;

	if (cmd == ELS_CMD_ADISC) {
		lpfc_els_rsp_adisc_acc(vport, elsiocb, ndlp);
	} else {
		lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, elsiocb,
			ndlp, NULL);
	}
	kfree(elsiocb);
640
	mempool_free(mboxq, phba->mbox_mem_pool);
641 642
}

已提交
643
static int
J
James Smart 已提交
644
lpfc_rcv_padisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
645 646
		struct lpfc_iocbq *cmdiocb)
{
J
James Smart 已提交
647
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
648
	struct lpfc_iocbq  *elsiocb;
已提交
649
	struct lpfc_dmabuf *pcmd;
J
James Smart 已提交
650 651
	struct serv_parm   *sp;
	struct lpfc_name   *pnn, *ppn;
已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
	struct ls_rjt stat;
	ADISC *ap;
	IOCB_t *icmd;
	uint32_t *lp;
	uint32_t cmd;

	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;

	cmd = *lp++;
	if (cmd == ELS_CMD_ADISC) {
		ap = (ADISC *) lp;
		pnn = (struct lpfc_name *) & ap->nodeName;
		ppn = (struct lpfc_name *) & ap->portName;
	} else {
		sp = (struct serv_parm *) lp;
		pnn = (struct lpfc_name *) & sp->nodeName;
		ppn = (struct lpfc_name *) & sp->portName;
	}

	icmd = &cmdiocb->iocb;
J
James Smart 已提交
673
	if (icmd->ulpStatus == 0 && lpfc_check_adisc(vport, ndlp, pnn, ppn)) {
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697

		/*
		 * As soon as  we send ACC, the remote NPort can
		 * start sending us data. Thus, for SLI4 we must
		 * resume the RPI before the ACC goes out.
		 */
		if (vport->phba->sli_rev == LPFC_SLI_REV4) {
			elsiocb = kmalloc(sizeof(struct lpfc_iocbq),
				GFP_KERNEL);
			if (elsiocb) {

				/* Save info from cmd IOCB used in rsp */
				memcpy((uint8_t *)elsiocb, (uint8_t *)cmdiocb,
					sizeof(struct lpfc_iocbq));

				/* Save the ELS cmd */
				elsiocb->drvrTimeout = cmd;

				lpfc_sli4_resume_rpi(ndlp,
					lpfc_mbx_cmpl_resume_rpi, elsiocb);
				goto out;
			}
		}

已提交
698
		if (cmd == ELS_CMD_ADISC) {
J
James Smart 已提交
699
			lpfc_els_rsp_adisc_acc(vport, cmdiocb, ndlp);
700
		} else {
701 702
			lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
				ndlp, NULL);
已提交
703
		}
704 705
out:
		/* If we are authenticated, move to the proper state */
706
		if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET))
707 708 709 710
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
		else
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);

711
		return 1;
已提交
712 713 714 715 716 717
	}
	/* Reject this request because invalid parameters */
	stat.un.b.lsRjtRsvd0 = 0;
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
	stat.un.b.vendorUnique = 0;
J
James Smart 已提交
718
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
已提交
719 720

	/* 1 sec timeout */
721
	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
已提交
722

J
James Smart 已提交
723
	spin_lock_irq(shost->host_lock);
已提交
724
	ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
725
	spin_unlock_irq(shost->host_lock);
726 727
	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
	ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
728
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
729
	return 0;
已提交
730 731 732
}

static int
J
James Smart 已提交
733 734
lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
	      struct lpfc_iocbq *cmdiocb, uint32_t els_cmd)
已提交
735
{
J
James Smart 已提交
736
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
737 738 739
	struct lpfc_hba    *phba = vport->phba;
	struct lpfc_vport **vports;
	int i, active_vlink_present = 0 ;
J
James Smart 已提交
740 741

	/* Put ndlp in NPR state with 1 sec timeout for plogi, ACC logo */
已提交
742 743 744
	/* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
	 * PLOGIs during LOGO storms from a device.
	 */
J
James Smart 已提交
745
	spin_lock_irq(shost->host_lock);
已提交
746
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
747
	spin_unlock_irq(shost->host_lock);
748
	if (els_cmd == ELS_CMD_PRLO)
749
		lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
750
	else
751
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
J
James Smart 已提交
752 753 754
	if (ndlp->nlp_DID == Fabric_DID) {
		if (vport->port_state <= LPFC_FDISC)
			goto out;
755 756
		lpfc_linkdown_port(vport);
		spin_lock_irq(shost->host_lock);
J
James Smart 已提交
757
		vport->fc_flag |= FC_VPORT_LOGO_RCVD;
758
		spin_unlock_irq(shost->host_lock);
J
James Smart 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771
		vports = lpfc_create_vport_work_array(phba);
		if (vports) {
			for (i = 0; i <= phba->max_vports && vports[i] != NULL;
					i++) {
				if ((!(vports[i]->fc_flag &
					FC_VPORT_LOGO_RCVD)) &&
					(vports[i]->port_state > LPFC_FDISC)) {
					active_vlink_present = 1;
					break;
				}
			}
			lpfc_destroy_vport_work_array(phba, vports);
		}
已提交
772

J
James Smart 已提交
773 774 775 776 777 778 779
		/*
		 * Don't re-instantiate if vport is marked for deletion.
		 * If we are here first then vport_delete is going to wait
		 * for discovery to complete.
		 */
		if (!(vport->load_flag & FC_UNLOADING) &&
					active_vlink_present) {
J
James Smart 已提交
780 781 782 783
			/*
			 * If there are other active VLinks present,
			 * re-instantiate the Vlink using FDISC.
			 */
784 785
			mod_timer(&ndlp->nlp_delayfunc,
				  jiffies + msecs_to_jiffies(1000));
J
James Smart 已提交
786 787 788 789 790 791 792 793 794 795 796
			spin_lock_irq(shost->host_lock);
			ndlp->nlp_flag |= NLP_DELAY_TMO;
			spin_unlock_irq(shost->host_lock);
			ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
			vport->port_state = LPFC_FDISC;
		} else {
			spin_lock_irq(shost->host_lock);
			phba->pport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG;
			spin_unlock_irq(shost->host_lock);
			lpfc_retry_pport_discovery(phba);
		}
797 798 799 800
	} else if ((!(ndlp->nlp_type & NLP_FABRIC) &&
		((ndlp->nlp_type & NLP_FCP_TARGET) ||
		!(ndlp->nlp_type & NLP_FCP_INITIATOR))) ||
		(ndlp->nlp_state == NLP_STE_ADISC_ISSUE)) {
已提交
801
		/* Only try to re-login if this is NOT a Fabric Node */
802 803
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
804
		spin_lock_irq(shost->host_lock);
已提交
805
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
806
		spin_unlock_irq(shost->host_lock);
已提交
807

808
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
809
	}
J
James Smart 已提交
810
out:
811 812
	ndlp->nlp_prev_state = ndlp->nlp_state;
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
813

J
James Smart 已提交
814
	spin_lock_irq(shost->host_lock);
已提交
815
	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
816
	spin_unlock_irq(shost->host_lock);
已提交
817 818 819 820 821
	/* The driver has to wait until the ACC completes before it continues
	 * processing the LOGO.  The action will resume in
	 * lpfc_cmpl_els_logo_acc routine. Since part of processing includes an
	 * unreg_login, the driver waits so the ACC does not get aborted.
	 */
822
	return 0;
已提交
823 824
}

825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
static uint32_t
lpfc_rcv_prli_support_check(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    struct lpfc_iocbq *cmdiocb)
{
	struct ls_rjt stat;
	uint32_t *payload;
	uint32_t cmd;

	payload = ((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
	cmd = *payload;
	if (vport->phba->nvmet_support) {
		/* Must be a NVME PRLI */
		if (cmd ==  ELS_CMD_PRLI)
			goto out;
	} else {
		/* Initiator mode. */
		if (!vport->nvmei_support && (cmd == ELS_CMD_NVMEPRLI))
			goto out;
	}
	return 1;
out:
	lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME_DISC,
			 "6115 Rcv PRLI (%x) check failed: ndlp rpi %d "
			 "state x%x flags x%x\n",
			 cmd, ndlp->nlp_rpi, ndlp->nlp_state,
			 ndlp->nlp_flag);
	memset(&stat, 0, sizeof(struct ls_rjt));
	stat.un.b.lsRjtRsnCode = LSRJT_CMD_UNSUPPORTED;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_REQ_UNSUPPORTED;
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
			    ndlp, NULL);
	return 0;
}

已提交
860
static void
J
James Smart 已提交
861 862
lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
	      struct lpfc_iocbq *cmdiocb)
已提交
863
{
864
	struct lpfc_hba  *phba = vport->phba;
已提交
865 866 867 868 869 870 871 872 873 874
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	PRLI *npr;
	struct fc_rport *rport = ndlp->rport;
	u32 roles;

	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;
	npr = (PRLI *) ((uint8_t *) lp + sizeof (uint32_t));

875 876 877 878 879 880 881 882
	if ((npr->prliType == PRLI_FCP_TYPE) ||
	    (npr->prliType == PRLI_NVME_TYPE)) {
		if (npr->initiatorFunc) {
			if (npr->prliType == PRLI_FCP_TYPE)
				ndlp->nlp_type |= NLP_FCP_INITIATOR;
			if (npr->prliType == PRLI_NVME_TYPE)
				ndlp->nlp_type |= NLP_NVME_INITIATOR;
		}
883
		if (npr->targetFunc) {
884 885 886 887
			if (npr->prliType == PRLI_FCP_TYPE)
				ndlp->nlp_type |= NLP_FCP_TARGET;
			if (npr->prliType == PRLI_NVME_TYPE)
				ndlp->nlp_type |= NLP_NVME_TARGET;
888 889 890
			if (npr->writeXferRdyDis)
				ndlp->nlp_flag |= NLP_FIRSTBURST;
		}
891 892
		if (npr->Retry && ndlp->nlp_type &
					(NLP_FCP_INITIATOR | NLP_FCP_TARGET))
已提交
893
			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
894

895 896 897 898 899
		if (npr->Retry && phba->nsler &&
		    ndlp->nlp_type & (NLP_NVME_INITIATOR | NLP_NVME_TARGET))
			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;


900 901 902 903 904
		/* If this driver is in nvme target mode, set the ndlp's fc4
		 * type to NVME provided the PRLI response claims NVME FC4
		 * type.  Target mode does not issue gft_id so doesn't get
		 * the fc4 type set until now.
		 */
905
		if (phba->nvmet_support && (npr->prliType == PRLI_NVME_TYPE)) {
906
			ndlp->nlp_fc4_type |= NLP_FC4_NVME;
907 908 909 910
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
		}
		if (npr->prliType == PRLI_FCP_TYPE)
			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
已提交
911 912 913 914 915 916 917 918
	}
	if (rport) {
		/* We need to update the rport role values */
		roles = FC_RPORT_ROLE_UNKNOWN;
		if (ndlp->nlp_type & NLP_FCP_INITIATOR)
			roles |= FC_RPORT_ROLE_FCP_INITIATOR;
		if (ndlp->nlp_type & NLP_FCP_TARGET)
			roles |= FC_RPORT_ROLE_FCP_TARGET;
J
James Smart 已提交
919 920 921 922 923

		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
			"rport rolechg:   role:x%x did:x%x flg:x%x",
			roles, ndlp->nlp_DID, ndlp->nlp_flag);

924
		if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
925
			fc_remote_port_rolechg(rport, roles);
已提交
926 927 928 929
	}
}

static uint32_t
J
James Smart 已提交
930
lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
已提交
931
{
J
James Smart 已提交
932 933
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

934
	if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
935
		spin_lock_irq(shost->host_lock);
936
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
937
		spin_unlock_irq(shost->host_lock);
938 939 940
		return 0;
	}

941 942 943
	if (!(vport->fc_flag & FC_PT2PT)) {
		/* Check config parameter use-adisc or FCP-2 */
		if ((vport->cfg_use_adisc && (vport->fc_flag & FC_RSCN_MODE)) ||
944 945
		    ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) &&
		     (ndlp->nlp_type & NLP_FCP_TARGET))) {
946 947 948 949 950
			spin_lock_irq(shost->host_lock);
			ndlp->nlp_flag |= NLP_NPR_ADISC;
			spin_unlock_irq(shost->host_lock);
			return 1;
		}
951
	}
952 953

	spin_lock_irq(shost->host_lock);
954
	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
955
	spin_unlock_irq(shost->host_lock);
956 957
	lpfc_unreg_rpi(vport, ndlp);
	return 0;
已提交
958
}
959

960
/**
L
Lucas De Marchi 已提交
961
 * lpfc_release_rpi - Release a RPI by issuing unreg_login mailbox cmd.
962 963 964 965 966 967 968
 * @phba : Pointer to lpfc_hba structure.
 * @vport: Pointer to lpfc_vport structure.
 * @rpi  : rpi to be release.
 *
 * This function will send a unreg_login mailbox command to the firmware
 * to release a rpi.
 **/
969
static void
970 971
lpfc_release_rpi(struct lpfc_hba *phba, struct lpfc_vport *vport,
		 struct lpfc_nodelist *ndlp, uint16_t rpi)
972 973 974 975
{
	LPFC_MBOXQ_t *pmb;
	int rc;

976 977 978 979 980 981 982
	/* If there is already an UNREG in progress for this ndlp,
	 * no need to queue up another one.
	 */
	if (ndlp->nlp_flag & NLP_UNREG_INP) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "1435 release_rpi SKIP UNREG x%x on "
				 "NPort x%x deferred x%x  flg x%x "
983
				 "Data: x%px\n",
984 985 986 987 988 989
				 ndlp->nlp_rpi, ndlp->nlp_DID,
				 ndlp->nlp_defer_did,
				 ndlp->nlp_flag, ndlp);
		return;
	}

990 991 992 993 994 995 996 997
	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
			GFP_KERNEL);
	if (!pmb)
		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
			"2796 mailbox memory allocation failed \n");
	else {
		lpfc_unreg_login(phba, vport->vpi, rpi, pmb);
		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
		pmb->vport = vport;
		pmb->ctx_ndlp = ndlp;

		if (((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
		    (!(vport->fc_flag & FC_OFFLINE_MODE)))
			ndlp->nlp_flag |= NLP_UNREG_INP;

		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "1437 release_rpi UNREG x%x "
				 "on NPort x%x flg x%x\n",
				 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag);

1010 1011 1012 1013 1014
		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
		if (rc == MBX_NOT_FINISHED)
			mempool_free(pmb, phba->mbox_mem_pool);
	}
}
已提交
1015 1016

static uint32_t
J
James Smart 已提交
1017 1018
lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		  void *arg, uint32_t evt)
已提交
1019
{
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
	struct lpfc_hba *phba;
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
	uint16_t rpi;

	phba = vport->phba;
	/* Release the RPI if reglogin completing */
	if (!(phba->pport->load_flag & FC_UNLOADING) &&
		(evt == NLP_EVT_CMPL_REG_LOGIN) &&
		(!pmb->u.mb.mbxStatus)) {
		rpi = pmb->u.mb.un.varWords[0];
1030
		lpfc_release_rpi(phba, vport, ndlp, rpi);
1031
	}
1032
	lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1033
			 "0271 Illegal State Transition: node x%x "
1034 1035 1036
			 "event x%x, state x%x Data: x%x x%x\n",
			 ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
			 ndlp->nlp_flag);
1037
	return ndlp->nlp_state;
已提交
1038 1039
}

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
static uint32_t
lpfc_cmpl_plogi_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		  void *arg, uint32_t evt)
{
	/* This transition is only legal if we previously
	 * rcv'ed a PLOGI. Since we don't want 2 discovery threads
	 * working on the same NPortID, do nothing for this thread
	 * to stop it.
	 */
	if (!(ndlp->nlp_flag & NLP_RCV_PLOGI)) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1051
			 "0272 Illegal State Transition: node x%x "
1052 1053 1054 1055 1056 1057 1058
			 "event x%x, state x%x Data: x%x x%x\n",
			 ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
			 ndlp->nlp_flag);
	}
	return ndlp->nlp_state;
}

已提交
1059 1060 1061
/* Start of Discovery State Machine routines */

static uint32_t
J
James Smart 已提交
1062 1063
lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1064 1065 1066 1067 1068
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1069
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1070
		return ndlp->nlp_state;
已提交
1071
	}
1072
	return NLP_STE_FREED_NODE;
已提交
1073 1074 1075
}

static uint32_t
J
James Smart 已提交
1076 1077
lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1078
{
J
James Smart 已提交
1079
	lpfc_issue_els_logo(vport, ndlp, 0);
1080
	return ndlp->nlp_state;
已提交
1081 1082 1083
}

static uint32_t
J
James Smart 已提交
1084 1085
lpfc_rcv_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1086
{
J
James Smart 已提交
1087 1088
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1089

J
James Smart 已提交
1090
	spin_lock_irq(shost->host_lock);
已提交
1091
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
1092
	spin_unlock_irq(shost->host_lock);
1093
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
1094

1095
	return ndlp->nlp_state;
已提交
1096 1097 1098
}

static uint32_t
J
James Smart 已提交
1099 1100
lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1101
{
1102
	return NLP_STE_FREED_NODE;
已提交
1103 1104 1105
}

static uint32_t
J
James Smart 已提交
1106 1107
lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1108
{
1109
	return NLP_STE_FREED_NODE;
已提交
1110 1111
}

1112 1113 1114 1115 1116 1117 1118 1119
static uint32_t
lpfc_device_recov_unused_node(struct lpfc_vport *vport,
			struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
{
	return ndlp->nlp_state;
}

已提交
1120
static uint32_t
J
James Smart 已提交
1121
lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1122 1123
			   void *arg, uint32_t evt)
{
1124
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
1125
	struct lpfc_hba   *phba = vport->phba;
已提交
1126
	struct lpfc_iocbq *cmdiocb = arg;
J
James Smart 已提交
1127 1128 1129
	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	uint32_t *lp = (uint32_t *) pcmd->virt;
	struct serv_parm *sp = (struct serv_parm *) (lp + 1);
已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138
	struct ls_rjt stat;
	int port_cmp;

	memset(&stat, 0, sizeof (struct ls_rjt));

	/* For a PLOGI, we only accept if our portname is less
	 * than the remote portname.
	 */
	phba->fc_stat.elsLogiCol++;
J
James Smart 已提交
1139
	port_cmp = memcmp(&vport->fc_portname, &sp->portName,
1140
			  sizeof(struct lpfc_name));
已提交
1141 1142 1143 1144 1145 1146

	if (port_cmp >= 0) {
		/* Reject this request because the remote node will accept
		   ours */
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
J
James Smart 已提交
1147 1148
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
1149
	} else {
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
		if (lpfc_rcv_plogi(vport, ndlp, cmdiocb) &&
		    (ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
		    (vport->num_disc_nodes)) {
			spin_lock_irq(shost->host_lock);
			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
			spin_unlock_irq(shost->host_lock);
			/* Check if there are more PLOGIs to be sent */
			lpfc_more_plogi(vport);
			if (vport->num_disc_nodes == 0) {
				spin_lock_irq(shost->host_lock);
				vport->fc_flag &= ~FC_NDISC_ACTIVE;
				spin_unlock_irq(shost->host_lock);
				lpfc_can_disctmo(vport);
				lpfc_end_rscn(vport);
			}
		}
J
James Smart 已提交
1166
	} /* If our portname was less */
已提交
1167

1168 1169 1170
	return ndlp->nlp_state;
}

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
static uint32_t
lpfc_rcv_prli_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
{
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
	struct ls_rjt     stat;

	memset(&stat, 0, sizeof (struct ls_rjt));
	stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
J
James Smart 已提交
1181
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1182 1183 1184
	return ndlp->nlp_state;
}

1185
static uint32_t
J
James Smart 已提交
1186 1187
lpfc_rcv_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
1188
{
J
James Smart 已提交
1189
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1190

1191 1192 1193
	/* Retrieve RPI from LOGO IOCB. RPI is used for CMD_ABORT_XRI_CN */
	if (vport->phba->sli_rev == LPFC_SLI_REV3)
		ndlp->nlp_rpi = cmdiocb->iocb.ulpIoTag;
1194
				/* software abort outstanding PLOGI */
J
James Smart 已提交
1195
	lpfc_els_abort(vport->phba, ndlp);
1196

J
James Smart 已提交
1197
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1198
	return ndlp->nlp_state;
已提交
1199 1200 1201
}

static uint32_t
J
James Smart 已提交
1202 1203
lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1204
{
J
James Smart 已提交
1205 1206 1207
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba   *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1208 1209

	/* software abort outstanding PLOGI */
1210
	lpfc_els_abort(phba, ndlp);
已提交
1211 1212

	if (evt == NLP_EVT_RCV_LOGO) {
1213
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1214
	} else {
J
James Smart 已提交
1215
		lpfc_issue_els_logo(vport, ndlp, 0);
已提交
1216 1217
	}

J
James Smart 已提交
1218
	/* Put ndlp in npr state set plogi timer for 1 sec */
1219
	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
1220
	spin_lock_irq(shost->host_lock);
1221
	ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1222
	spin_unlock_irq(shost->host_lock);
1223 1224
	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
J
James Smart 已提交
1225
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
1226

1227
	return ndlp->nlp_state;
已提交
1228 1229 1230
}

static uint32_t
J
James Smart 已提交
1231 1232 1233
lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg,
已提交
1234 1235
			    uint32_t evt)
{
J
James Smart 已提交
1236
	struct lpfc_hba    *phba = vport->phba;
1237
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
1238
	struct lpfc_iocbq  *cmdiocb, *rspiocb;
1239
	struct lpfc_dmabuf *pcmd, *prsp, *mp;
已提交
1240
	uint32_t *lp;
1241
	uint32_t vid, flag;
已提交
1242 1243
	IOCB_t *irsp;
	struct serv_parm *sp;
1244
	uint32_t ed_tov;
已提交
1245
	LPFC_MBOXQ_t *mbox;
1246
	int rc;
已提交
1247 1248 1249 1250 1251

	cmdiocb = (struct lpfc_iocbq *) arg;
	rspiocb = cmdiocb->context_un.rsp_iocb;

	if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
1252
		/* Recovery from PLOGI collision logic */
1253
		return ndlp->nlp_state;
已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262
	}

	irsp = &rspiocb->iocb;

	if (irsp->ulpStatus)
		goto out;

	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;

J
James Smart 已提交
1263
	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1264 1265
	if (!prsp)
		goto out;
已提交
1266

J
James Smart 已提交
1267
	lp = (uint32_t *) prsp->virt;
已提交
1268
	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
1269 1270 1271 1272 1273

	/* Some switches have FDMI servers returning 0 for WWN */
	if ((ndlp->nlp_DID != FDMI_DID) &&
		(wwn_to_u64(sp->portName.u.wwn) == 0 ||
		wwn_to_u64(sp->nodeName.u.wwn) == 0)) {
1274 1275 1276 1277
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0142 PLOGI RSP: Invalid WWN.\n");
		goto out;
	}
1278
	if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0))
已提交
1279 1280
		goto out;
	/* PLOGI chkparm OK */
1281 1282 1283 1284
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0121 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
			 ndlp->nlp_DID, ndlp->nlp_state,
			 ndlp->nlp_flag, ndlp->nlp_rpi);
1285
	if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
已提交
1286
		ndlp->nlp_fcp_info |= CLASS2;
J
James Smart 已提交
1287
	else
已提交
1288
		ndlp->nlp_fcp_info |= CLASS3;
J
James Smart 已提交
1289

已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
	ndlp->nlp_class_sup = 0;
	if (sp->cls1.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS1;
	if (sp->cls2.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS2;
	if (sp->cls3.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS3;
	if (sp->cls4.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS4;
	ndlp->nlp_maxframe =
J
James Smart 已提交
1300
		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
已提交
1301

1302 1303 1304 1305 1306 1307 1308 1309
	if ((vport->fc_flag & FC_PT2PT) &&
	    (vport->fc_flag & FC_PT2PT_PLOGI)) {
		ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
		if (sp->cmn.edtovResolution) {
			/* E_D_TOV ticks are in nanoseconds */
			ed_tov = (phba->fc_edtov + 999999) / 1000000;
		}

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
		ndlp->nlp_flag &= ~NLP_SUPPRESS_RSP;
		if ((phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) &&
		    sp->cmn.valid_vendor_ver_level) {
			vid = be32_to_cpu(sp->un.vv.vid);
			flag = be32_to_cpu(sp->un.vv.flags);
			if ((vid == LPFC_VV_EMLX_ID) &&
			    (flag & LPFC_VV_SUPPRESS_RSP))
				ndlp->nlp_flag |= NLP_SUPPRESS_RSP;
		}

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
		/*
		 * Use the larger EDTOV
		 * RATOV = 2 * EDTOV for pt-to-pt
		 */
		if (ed_tov > phba->fc_edtov)
			phba->fc_edtov = ed_tov;
		phba->fc_ratov = (2 * phba->fc_edtov) / 1000;

		memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));

		/* Issue config_link / reg_vfi to account for updated TOV's */
		if (phba->sli_rev == LPFC_SLI_REV4) {
			lpfc_issue_reg_vfi(vport);
		} else {
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
			if (!mbox) {
				lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
						 "0133 PLOGI: no memory "
						 "for config_link "
						 "Data: x%x x%x x%x x%x\n",
						 ndlp->nlp_DID, ndlp->nlp_state,
						 ndlp->nlp_flag, ndlp->nlp_rpi);
				goto out;
			}

1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
			lpfc_config_link(phba, mbox);

			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
			mbox->vport = vport;
			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
			if (rc == MBX_NOT_FINISHED) {
				mempool_free(mbox, phba->mbox_mem_pool);
				goto out;
			}
		}
1355
	}
已提交
1356

J
James Smart 已提交
1357 1358
	lpfc_unreg_rpi(vport, ndlp);

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!mbox) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0018 PLOGI: no memory for reg_login "
				 "Data: x%x x%x x%x x%x\n",
				 ndlp->nlp_DID, ndlp->nlp_state,
				 ndlp->nlp_flag, ndlp->nlp_rpi);
		goto out;
	}

1369
	if (lpfc_reg_rpi(phba, vport->vpi, irsp->un.elsreq64.remoteID,
1370
			 (uint8_t *) sp, mbox, ndlp->nlp_rpi) == 0) {
1371
		switch (ndlp->nlp_DID) {
已提交
1372
		case NameServer_DID:
1373
			mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
已提交
1374 1375
			break;
		case FDMI_DID:
1376
			mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
已提交
1377 1378
			break;
		default:
1379
			ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
1380
			mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
已提交
1381
		}
1382
		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
J
James Smart 已提交
1383
		mbox->vport = vport;
1384
		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
已提交
1385
		    != MBX_NOT_FINISHED) {
J
James Smart 已提交
1386 1387
			lpfc_nlp_set_state(vport, ndlp,
					   NLP_STE_REG_LOGIN_ISSUE);
1388
			return ndlp->nlp_state;
已提交
1389
		}
1390 1391
		if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1392 1393 1394
		/* decrement node reference count to the failed mbox
		 * command
		 */
1395
		lpfc_nlp_put(ndlp);
1396
		mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
1397 1398
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
已提交
1399
		mempool_free(mbox, phba->mbox_mem_pool);
1400

1401 1402 1403 1404 1405
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0134 PLOGI: cannot issue reg_login "
				 "Data: x%x x%x x%x x%x\n",
				 ndlp->nlp_DID, ndlp->nlp_state,
				 ndlp->nlp_flag, ndlp->nlp_rpi);
已提交
1406 1407
	} else {
		mempool_free(mbox, phba->mbox_mem_pool);
1408

1409 1410 1411 1412 1413
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0135 PLOGI: cannot format reg_login "
				 "Data: x%x x%x x%x x%x\n",
				 ndlp->nlp_DID, ndlp->nlp_state,
				 ndlp->nlp_flag, ndlp->nlp_rpi);
已提交
1414 1415 1416
	}


1417 1418 1419
out:
	if (ndlp->nlp_DID == NameServer_DID) {
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1420 1421
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0261 Cannot Register NameServer login\n");
1422 1423
	}

1424 1425 1426 1427 1428 1429 1430
	/*
	** In case the node reference counter does not go to zero, ensure that
	** the stale state for the node is not processed.
	*/

	ndlp->nlp_prev_state = ndlp->nlp_state;
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1431
	spin_lock_irq(shost->host_lock);
1432
	ndlp->nlp_flag |= NLP_DEFER_RM;
1433
	spin_unlock_irq(shost->host_lock);
1434
	return NLP_STE_FREED_NODE;
已提交
1435 1436
}

1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
static uint32_t
lpfc_cmpl_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
{
	return ndlp->nlp_state;
}

static uint32_t
lpfc_cmpl_reglogin_plogi_issue(struct lpfc_vport *vport,
	struct lpfc_nodelist *ndlp, void *arg, uint32_t evt)
{
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
	struct lpfc_hba *phba;
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
	MAILBOX_t *mb = &pmb->u.mb;
	uint16_t rpi;

	phba = vport->phba;
	/* Release the RPI */
	if (!(phba->pport->load_flag & FC_UNLOADING) &&
		!mb->mbxStatus) {
		rpi = pmb->u.mb.un.varWords[0];
1458
		lpfc_release_rpi(phba, vport, ndlp, rpi);
1459
	}
1460 1461 1462
	return ndlp->nlp_state;
}

已提交
1463
static uint32_t
J
James Smart 已提交
1464 1465
lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1466
{
J
James Smart 已提交
1467 1468 1469 1470
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1471
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1472
		spin_unlock_irq(shost->host_lock);
1473
		return ndlp->nlp_state;
J
James Smart 已提交
1474
	} else {
1475
		/* software abort outstanding PLOGI */
J
James Smart 已提交
1476
		lpfc_els_abort(vport->phba, ndlp);
已提交
1477

J
James Smart 已提交
1478
		lpfc_drop_node(vport, ndlp);
1479 1480
		return NLP_STE_FREED_NODE;
	}
已提交
1481 1482 1483
}

static uint32_t
J
James Smart 已提交
1484 1485 1486 1487
lpfc_device_recov_plogi_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
1488
{
J
James Smart 已提交
1489 1490 1491
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

1492 1493 1494 1495 1496 1497
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
1498
	/* software abort outstanding PLOGI */
1499
	lpfc_els_abort(phba, ndlp);
已提交
1500

1501
	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
J
James Smart 已提交
1502
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1503
	spin_lock_irq(shost->host_lock);
1504
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1505
	spin_unlock_irq(shost->host_lock);
已提交
1506

1507
	return ndlp->nlp_state;
已提交
1508 1509 1510
}

static uint32_t
J
James Smart 已提交
1511 1512
lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1513
{
1514
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
1515
	struct lpfc_hba   *phba = vport->phba;
已提交
1516 1517 1518
	struct lpfc_iocbq *cmdiocb;

	/* software abort outstanding ADISC */
1519
	lpfc_els_abort(phba, ndlp);
已提交
1520 1521 1522

	cmdiocb = (struct lpfc_iocbq *) arg;

1523 1524 1525 1526 1527
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
		if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
			spin_lock_irq(shost->host_lock);
			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
			spin_unlock_irq(shost->host_lock);
1528
			if (vport->num_disc_nodes)
1529 1530 1531 1532
				lpfc_more_adisc(vport);
		}
		return ndlp->nlp_state;
	}
1533
	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1534 1535
	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
已提交
1536

1537
	return ndlp->nlp_state;
已提交
1538 1539 1540
}

static uint32_t
J
James Smart 已提交
1541 1542
lpfc_rcv_prli_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1543
{
J
James Smart 已提交
1544
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1545

1546 1547
	if (lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1548
	return ndlp->nlp_state;
已提交
1549 1550 1551
}

static uint32_t
J
James Smart 已提交
1552 1553
lpfc_rcv_logo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1554
{
J
James Smart 已提交
1555
	struct lpfc_hba *phba = vport->phba;
已提交
1556 1557 1558 1559 1560
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

	/* software abort outstanding ADISC */
1561
	lpfc_els_abort(phba, ndlp);
已提交
1562

J
James Smart 已提交
1563
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1564
	return ndlp->nlp_state;
已提交
1565 1566 1567
}

static uint32_t
J
James Smart 已提交
1568 1569 1570
lpfc_rcv_padisc_adisc_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1571 1572 1573 1574 1575
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1576
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1577
	return ndlp->nlp_state;
已提交
1578 1579 1580
}

static uint32_t
J
James Smart 已提交
1581 1582
lpfc_rcv_prlo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1583 1584 1585 1586 1587 1588
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

	/* Treat like rcv logo */
J
James Smart 已提交
1589
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1590
	return ndlp->nlp_state;
已提交
1591 1592 1593
}

static uint32_t
J
James Smart 已提交
1594 1595 1596
lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1597
{
J
James Smart 已提交
1598 1599
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba   *phba = vport->phba;
已提交
1600 1601 1602
	struct lpfc_iocbq *cmdiocb, *rspiocb;
	IOCB_t *irsp;
	ADISC *ap;
1603
	int rc;
已提交
1604 1605 1606 1607 1608 1609 1610 1611

	cmdiocb = (struct lpfc_iocbq *) arg;
	rspiocb = cmdiocb->context_un.rsp_iocb;

	ap = (ADISC *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
	irsp = &rspiocb->iocb;

	if ((irsp->ulpStatus) ||
1612
	    (!lpfc_check_adisc(vport, ndlp, &ap->nodeName, &ap->portName))) {
已提交
1613
		/* 1 sec timeout */
1614 1615
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000));
J
James Smart 已提交
1616
		spin_lock_irq(shost->host_lock);
已提交
1617
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1618
		spin_unlock_irq(shost->host_lock);
1619
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
已提交
1620

J
James Smart 已提交
1621 1622
		memset(&ndlp->nlp_nodename, 0, sizeof(struct lpfc_name));
		memset(&ndlp->nlp_portname, 0, sizeof(struct lpfc_name));
已提交
1623

1624
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1625 1626
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		lpfc_unreg_rpi(vport, ndlp);
1627
		return ndlp->nlp_state;
已提交
1628
	}
1629

1630
	if (phba->sli_rev == LPFC_SLI_REV4) {
1631
		rc = lpfc_sli4_resume_rpi(ndlp, NULL, NULL);
1632 1633 1634 1635 1636 1637 1638
		if (rc) {
			/* Stay in state and retry. */
			ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
			return ndlp->nlp_state;
		}
	}

1639
	if (ndlp->nlp_type & NLP_FCP_TARGET) {
1640
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1641
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1642
	} else {
1643
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1644
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1645
	}
1646

1647
	return ndlp->nlp_state;
已提交
1648 1649 1650
}

static uint32_t
J
James Smart 已提交
1651 1652
lpfc_device_rm_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1653
{
J
James Smart 已提交
1654 1655 1656 1657
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1658
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1659
		spin_unlock_irq(shost->host_lock);
1660
		return ndlp->nlp_state;
J
James Smart 已提交
1661
	} else {
1662
		/* software abort outstanding ADISC */
J
James Smart 已提交
1663
		lpfc_els_abort(vport->phba, ndlp);
已提交
1664

J
James Smart 已提交
1665
		lpfc_drop_node(vport, ndlp);
1666 1667
		return NLP_STE_FREED_NODE;
	}
已提交
1668 1669 1670
}

static uint32_t
J
James Smart 已提交
1671 1672 1673 1674
lpfc_device_recov_adisc_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
1675
{
J
James Smart 已提交
1676 1677 1678
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

1679 1680 1681 1682 1683 1684
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
1685
	/* software abort outstanding ADISC */
1686
	lpfc_els_abort(phba, ndlp);
已提交
1687

1688
	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1689 1690
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
1691
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1692
	spin_unlock_irq(shost->host_lock);
1693
	lpfc_disc_set_adisc(vport, ndlp);
1694
	return ndlp->nlp_state;
已提交
1695 1696 1697
}

static uint32_t
J
James Smart 已提交
1698 1699 1700
lpfc_rcv_plogi_reglogin_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
已提交
1701 1702
			      uint32_t evt)
{
J
James Smart 已提交
1703
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1704

J
James Smart 已提交
1705
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1706
	return ndlp->nlp_state;
已提交
1707 1708 1709
}

static uint32_t
J
James Smart 已提交
1710 1711 1712
lpfc_rcv_prli_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1713 1714
			     uint32_t evt)
{
J
James Smart 已提交
1715
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1716
	struct ls_rjt     stat;
已提交
1717

1718 1719 1720
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb)) {
		return ndlp->nlp_state;
	}
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
	if (vport->phba->nvmet_support) {
		/* NVME Target mode.  Handle and respond to the PRLI and
		 * transition to UNMAPPED provided the RPI has completed
		 * registration.
		 */
		if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
			lpfc_rcv_prli(vport, ndlp, cmdiocb);
			lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
		} else {
			/* RPI registration has not completed. Reject the PRLI
			 * to prevent an illegal state transition when the
			 * rpi registration does complete.
			 */
			memset(&stat, 0, sizeof(struct ls_rjt));
1735 1736
			stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
			stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1737 1738
			lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
					    ndlp, NULL);
1739
			return ndlp->nlp_state;
1740 1741 1742 1743 1744
		}
	} else {
		/* Initiator mode. */
		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
	}
1745
	return ndlp->nlp_state;
已提交
1746 1747 1748
}

static uint32_t
J
James Smart 已提交
1749 1750 1751
lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1752 1753
			     uint32_t evt)
{
J
James Smart 已提交
1754 1755
	struct lpfc_hba   *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1756 1757 1758
	LPFC_MBOXQ_t	  *mb;
	LPFC_MBOXQ_t	  *nextmb;
	struct lpfc_dmabuf *mp;
1759
	struct lpfc_nodelist *ns_ndlp;
已提交
1760 1761 1762

	cmdiocb = (struct lpfc_iocbq *) arg;

1763 1764
	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
	if ((mb = phba->sli.mbox_active)) {
1765
		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1766
		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
1767
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1768
			lpfc_nlp_put(ndlp);
1769
			mb->ctx_ndlp = NULL;
1770 1771 1772 1773
			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
		}
	}

J
James Smart 已提交
1774
	spin_lock_irq(&phba->hbalock);
1775
	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1776
		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1777 1778
		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
			mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
1779
			if (mp) {
1780
				__lpfc_mbuf_free(phba, mp->virt, mp->phys);
1781 1782
				kfree(mp);
			}
1783
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1784
			lpfc_nlp_put(ndlp);
1785
			list_del(&mb->list);
1786
			phba->sli.mboxq_cnt--;
1787 1788 1789
			mempool_free(mb, phba->mbox_mem_pool);
		}
	}
J
James Smart 已提交
1790
	spin_unlock_irq(&phba->hbalock);
1791

1792 1793 1794 1795 1796 1797 1798
	/* software abort if any GID_FT is outstanding */
	if (vport->cfg_enable_fc4_type != LPFC_ENABLE_FCP) {
		ns_ndlp = lpfc_findnode_did(vport, NameServer_DID);
		if (ns_ndlp && NLP_CHK_NODE_ACT(ns_ndlp))
			lpfc_els_abort(phba, ns_ndlp);
	}

J
James Smart 已提交
1799
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1800
	return ndlp->nlp_state;
已提交
1801 1802 1803
}

static uint32_t
J
James Smart 已提交
1804 1805 1806
lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport *vport,
			       struct lpfc_nodelist *ndlp,
			       void *arg,
已提交
1807 1808
			       uint32_t evt)
{
J
James Smart 已提交
1809
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1810

J
James Smart 已提交
1811
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1812
	return ndlp->nlp_state;
已提交
1813 1814 1815
}

static uint32_t
J
James Smart 已提交
1816 1817 1818
lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1819 1820 1821 1822 1823
			     uint32_t evt)
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;
1824
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1825
	return ndlp->nlp_state;
已提交
1826 1827 1828
}

static uint32_t
J
James Smart 已提交
1829 1830 1831 1832
lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
				  struct lpfc_nodelist *ndlp,
				  void *arg,
				  uint32_t evt)
已提交
1833
{
J
James Smart 已提交
1834
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1835
	struct lpfc_hba *phba = vport->phba;
J
James Smart 已提交
1836
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1837
	MAILBOX_t *mb = &pmb->u.mb;
J
James Smart 已提交
1838
	uint32_t did  = mb->un.varWords[1];
已提交
1839 1840 1841

	if (mb->mbxStatus) {
		/* RegLogin failed */
1842
		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1843 1844 1845 1846 1847
				"0246 RegLogin failed Data: x%x x%x x%x x%x "
				 "x%x\n",
				 did, mb->mbxStatus, vport->port_state,
				 mb->un.varRegLogin.vpi,
				 mb->un.varRegLogin.rpi);
1848 1849 1850 1851 1852
		/*
		 * If RegLogin failed due to lack of HBA resources do not
		 * retry discovery.
		 */
		if (mb->mbxStatus == MBXERR_RPI_FULL) {
1853 1854
			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1855 1856 1857
			return ndlp->nlp_state;
		}

J
James Smart 已提交
1858
		/* Put ndlp in npr state set plogi timer for 1 sec */
1859 1860
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
1861
		spin_lock_irq(shost->host_lock);
已提交
1862
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1863
		spin_unlock_irq(shost->host_lock);
1864
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
已提交
1865

J
James Smart 已提交
1866
		lpfc_issue_els_logo(vport, ndlp, 0);
1867
		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1868
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1869
		return ndlp->nlp_state;
已提交
1870 1871
	}

1872
	/* SLI4 ports have preallocated logical rpis. */
1873
	if (phba->sli_rev < LPFC_SLI_REV4)
1874 1875
		ndlp->nlp_rpi = mb->un.varWords[0];

1876
	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
已提交
1877 1878

	/* Only if we are not a fabric nport do we issue PRLI */
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "3066 RegLogin Complete on x%x x%x x%x\n",
			 did, ndlp->nlp_type, ndlp->nlp_fc4_type);
	if (!(ndlp->nlp_type & NLP_FABRIC) &&
	    (phba->nvmet_support == 0)) {
		/* The driver supports FCP and NVME concurrently.  If the
		 * ndlp's nlp_fc4_type is still zero, the driver doesn't
		 * know what PRLI to send yet.  Figure that out now and
		 * call PRLI depending on the outcome.
		 */
		if (vport->fc_flag & FC_PT2PT) {
			/* If we are pt2pt, there is no Fabric to determine
			 * the FC4 type of the remote nport. So if NVME
			 * is configured try it.
			 */
			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1895 1896
			if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
1897 1898
				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
				/* We need to update the localport also */
1899
				lpfc_nvme_update_localport(vport);
1900 1901
			}

1902 1903 1904
		} else if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
			ndlp->nlp_fc4_type |= NLP_FC4_FCP;

1905
		} else if (ndlp->nlp_fc4_type == 0) {
1906 1907 1908 1909
			/* If we are only configured for FCP, the driver
			 * should just issue PRLI for FCP. Otherwise issue
			 * GFT_ID to determine if remote port supports NVME.
			 */
1910
			if (vport->cfg_enable_fc4_type != LPFC_ENABLE_FCP) {
1911 1912
				lpfc_ns_cmd(vport, SLI_CTNS_GFT_ID, 0,
					    ndlp->nlp_DID);
1913 1914 1915
				return ndlp->nlp_state;
			}
			ndlp->nlp_fc4_type = NLP_FC4_FCP;
1916 1917
		}

1918
		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1919
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1920 1921 1922 1923 1924
		if (lpfc_issue_els_prli(vport, ndlp, 0)) {
			lpfc_issue_els_logo(vport, ndlp, 0);
			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		}
已提交
1925
	} else {
1926 1927 1928 1929 1930 1931
		if ((vport->fc_flag & FC_PT2PT) && phba->nvmet_support)
			phba->targetport->port_id = vport->fc_myDID;

		/* Only Fabric ports should transition. NVME target
		 * must complete PRLI.
		 */
1932 1933 1934 1935
		if (ndlp->nlp_type & NLP_FABRIC) {
			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
		}
已提交
1936
	}
1937
	return ndlp->nlp_state;
已提交
1938 1939 1940
}

static uint32_t
J
James Smart 已提交
1941 1942 1943
lpfc_device_rm_reglogin_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
已提交
1944 1945
			      uint32_t evt)
{
J
James Smart 已提交
1946 1947 1948 1949
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1950
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1951
		spin_unlock_irq(shost->host_lock);
1952
		return ndlp->nlp_state;
J
James Smart 已提交
1953 1954
	} else {
		lpfc_drop_node(vport, ndlp);
1955 1956
		return NLP_STE_FREED_NODE;
	}
已提交
1957 1958 1959
}

static uint32_t
J
James Smart 已提交
1960 1961 1962 1963
lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
				 struct lpfc_nodelist *ndlp,
				 void *arg,
				 uint32_t evt)
已提交
1964
{
J
James Smart 已提交
1965 1966
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

1967 1968 1969 1970 1971 1972
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

1973
	ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1974 1975
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
1976

1977 1978 1979 1980 1981 1982 1983
	/* If we are a target we won't immediately transition into PRLI,
	 * so if REG_LOGIN already completed we don't need to ignore it.
	 */
	if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED) ||
	    !vport->phba->nvmet_support)
		ndlp->nlp_flag |= NLP_IGNR_REG_CMPL;

1984
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1985
	spin_unlock_irq(shost->host_lock);
1986
	lpfc_disc_set_adisc(vport, ndlp);
1987
	return ndlp->nlp_state;
已提交
1988 1989 1990
}

static uint32_t
J
James Smart 已提交
1991 1992
lpfc_rcv_plogi_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1993 1994 1995 1996 1997
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1998
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1999
	return ndlp->nlp_state;
已提交
2000 2001 2002
}

static uint32_t
J
James Smart 已提交
2003 2004
lpfc_rcv_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2005
{
J
James Smart 已提交
2006
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2007

2008 2009
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;
J
James Smart 已提交
2010
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2011
	return ndlp->nlp_state;
已提交
2012 2013 2014
}

static uint32_t
J
James Smart 已提交
2015 2016
lpfc_rcv_logo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2017
{
J
James Smart 已提交
2018
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2019 2020

	/* Software abort outstanding PRLI before sending acc */
J
James Smart 已提交
2021
	lpfc_els_abort(vport->phba, ndlp);
已提交
2022

J
James Smart 已提交
2023
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2024
	return ndlp->nlp_state;
已提交
2025 2026 2027
}

static uint32_t
J
James Smart 已提交
2028 2029
lpfc_rcv_padisc_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2030
{
J
James Smart 已提交
2031
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2032

J
James Smart 已提交
2033
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2034
	return ndlp->nlp_state;
已提交
2035 2036 2037 2038 2039 2040 2041 2042
}

/* This routine is envoked when we rcv a PRLO request from a nport
 * we are logged into.  We should send back a PRLO rsp setting the
 * appropriate bits.
 * NEXT STATE = PRLI_ISSUE
 */
static uint32_t
J
James Smart 已提交
2043 2044
lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2045
{
J
James Smart 已提交
2046
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2047

2048
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2049
	return ndlp->nlp_state;
已提交
2050 2051 2052
}

static uint32_t
J
James Smart 已提交
2053 2054
lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2055
{
2056
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
2057
	struct lpfc_iocbq *cmdiocb, *rspiocb;
J
James Smart 已提交
2058
	struct lpfc_hba   *phba = vport->phba;
已提交
2059 2060
	IOCB_t *irsp;
	PRLI *npr;
2061 2062
	struct lpfc_nvme_prli *nvpr;
	void *temp_ptr;
已提交
2063 2064 2065

	cmdiocb = (struct lpfc_iocbq *) arg;
	rspiocb = cmdiocb->context_un.rsp_iocb;
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077

	/* A solicited PRLI is either FCP or NVME.  The PRLI cmd/rsp
	 * format is different so NULL the two PRLI types so that the
	 * driver correctly gets the correct context.
	 */
	npr = NULL;
	nvpr = NULL;
	temp_ptr = lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
	if (cmdiocb->iocb_flag & LPFC_PRLI_FCP_REQ)
		npr = (PRLI *) temp_ptr;
	else if (cmdiocb->iocb_flag & LPFC_PRLI_NVME_REQ)
		nvpr = (struct lpfc_nvme_prli *) temp_ptr;
已提交
2078 2079 2080

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus) {
J
James Smart 已提交
2081
		if ((vport->port_type == LPFC_NPIV_PORT) &&
2082
		    vport->cfg_restrict_login) {
J
James Smart 已提交
2083 2084
			goto out;
		}
2085

2086 2087
		/* Adjust the nlp_type accordingly if the PRLI failed */
		if (npr)
2088
			ndlp->nlp_fc4_type &= ~NLP_FC4_FCP;
2089 2090
		if (nvpr)
			ndlp->nlp_fc4_type &= ~NLP_FC4_NVME;
2091

2092 2093
		/* We can't set the DSM state till BOTH PRLIs complete */
		goto out_err;
已提交
2094 2095
	}

2096
	if (npr && (npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
已提交
2097
	    (npr->prliType == PRLI_FCP_TYPE)) {
2098 2099 2100 2101
		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
				 "6028 FCP NPR PRLI Cmpl Init %d Target %d\n",
				 npr->initiatorFunc,
				 npr->targetFunc);
已提交
2102 2103
		if (npr->initiatorFunc)
			ndlp->nlp_type |= NLP_FCP_INITIATOR;
2104
		if (npr->targetFunc) {
已提交
2105
			ndlp->nlp_type |= NLP_FCP_TARGET;
2106 2107 2108
			if (npr->writeXferRdyDis)
				ndlp->nlp_flag |= NLP_FIRSTBURST;
		}
已提交
2109 2110
		if (npr->Retry)
			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121

	} else if (nvpr &&
		   (bf_get_be32(prli_acc_rsp_code, nvpr) ==
		    PRLI_REQ_EXECUTED) &&
		   (bf_get_be32(prli_type_code, nvpr) ==
		    PRLI_NVME_TYPE)) {

		/* Complete setting up the remote ndlp personality. */
		if (bf_get_be32(prli_init, nvpr))
			ndlp->nlp_type |= NLP_NVME_INITIATOR;

2122 2123 2124 2125 2126
		if (phba->nsler && bf_get_be32(prli_nsler, nvpr))
			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;
		else
			ndlp->nlp_nvme_info &= ~NLP_NVME_NSLER;

2127 2128
		/* Target driver cannot solicit NVME FB. */
		if (bf_get_be32(prli_tgt, nvpr)) {
2129 2130 2131 2132
			/* Complete the nvme target roles.  The transport
			 * needs to know if the rport is capable of
			 * discovery in addition to its role.
			 */
2133
			ndlp->nlp_type |= NLP_NVME_TARGET;
2134 2135
			if (bf_get_be32(prli_disc, nvpr))
				ndlp->nlp_type |= NLP_NVME_DISCOVERY;
2136 2137 2138 2139 2140 2141 2142

			/*
			 * If prli_fba is set, the Target supports FirstBurst.
			 * If prli_fb_sz is 0, the FirstBurst size is unlimited,
			 * otherwise it defines the actual size supported by
			 * the NVME Target.
			 */
2143 2144 2145 2146 2147 2148 2149 2150 2151
			if ((bf_get_be32(prli_fba, nvpr) == 1) &&
			    (phba->cfg_nvme_enable_fb) &&
			    (!phba->nvmet_support)) {
				/* Both sides support FB. The target's first
				 * burst size is a 512 byte encoded value.
				 */
				ndlp->nlp_flag |= NLP_FIRSTBURST;
				ndlp->nvme_fb_size = bf_get_be32(prli_fb_sz,
								 nvpr);
2152 2153 2154 2155 2156 2157 2158

				/* Expressed in units of 512 bytes */
				if (ndlp->nvme_fb_size)
					ndlp->nvme_fb_size <<=
						LPFC_NVME_FB_SHIFT;
				else
					ndlp->nvme_fb_size = LPFC_NVME_MAX_FB;
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
			}
		}

		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
				 "6029 NVME PRLI Cmpl w1 x%08x "
				 "w4 x%08x w5 x%08x flag x%x, "
				 "fcp_info x%x nlp_type x%x\n",
				 be32_to_cpu(nvpr->word1),
				 be32_to_cpu(nvpr->word4),
				 be32_to_cpu(nvpr->word5),
				 ndlp->nlp_flag, ndlp->nlp_fcp_info,
				 ndlp->nlp_type);
已提交
2171
	}
2172 2173
	if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
	    (vport->port_type == LPFC_NPIV_PORT) &&
2174
	     vport->cfg_restrict_login) {
J
James Smart 已提交
2175
out:
2176 2177 2178 2179 2180 2181
		spin_lock_irq(shost->host_lock);
		ndlp->nlp_flag |= NLP_TARGET_REMOVE;
		spin_unlock_irq(shost->host_lock);
		lpfc_issue_els_logo(vport, ndlp, 0);

		ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
2182
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2183 2184
		return ndlp->nlp_state;
	}
已提交
2185

2186 2187 2188 2189 2190 2191 2192 2193
out_err:
	/* The ndlp state cannot move to MAPPED or UNMAPPED before all PRLIs
	 * are complete.
	 */
	if (ndlp->fc4_prli_sent == 0) {
		ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
		if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET))
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
2194 2195
		else if (ndlp->nlp_type &
			 (NLP_FCP_INITIATOR | NLP_NVME_INITIATOR))
2196 2197 2198 2199 2200 2201 2202 2203 2204
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
	} else
		lpfc_printf_vlog(vport,
				 KERN_INFO, LOG_ELS,
				 "3067 PRLI's still outstanding "
				 "on x%06x - count %d, Pend Node Mode "
				 "transition...\n",
				 ndlp->nlp_DID, ndlp->fc4_prli_sent);

2205
	return ndlp->nlp_state;
已提交
2206 2207 2208
}

/*! lpfc_device_rm_prli_issue
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
 *
 * \pre
 * \post
 * \param   phba
 * \param   ndlp
 * \param   arg
 * \param   evt
 * \return  uint32_t
 *
 * \b Description:
 *    This routine is envoked when we a request to remove a nport we are in the
 *    process of PRLIing. We should software abort outstanding prli, unreg
 *    login, send a logout. We will change node state to UNUSED_NODE, put it
 *    on plogi list so it can be freed when LOGO completes.
 *
 */

已提交
2226
static uint32_t
J
James Smart 已提交
2227 2228
lpfc_device_rm_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2229
{
J
James Smart 已提交
2230 2231 2232 2233
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
2234
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
2235
		spin_unlock_irq(shost->host_lock);
2236
		return ndlp->nlp_state;
J
James Smart 已提交
2237
	} else {
2238
		/* software abort outstanding PLOGI */
J
James Smart 已提交
2239
		lpfc_els_abort(vport->phba, ndlp);
已提交
2240

J
James Smart 已提交
2241
		lpfc_drop_node(vport, ndlp);
2242 2243
		return NLP_STE_FREED_NODE;
	}
已提交
2244 2245 2246 2247
}


/*! lpfc_device_recov_prli_issue
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
 *
 * \pre
 * \post
 * \param   phba
 * \param   ndlp
 * \param   arg
 * \param   evt
 * \return  uint32_t
 *
 * \b Description:
 *    The routine is envoked when the state of a device is unknown, like
 *    during a link down. We should remove the nodelist entry from the
 *    unmapped list, issue a UNREG_LOGIN, do a software abort of the
 *    outstanding PRLI command, then free the node entry.
 */
已提交
2263
static uint32_t
J
James Smart 已提交
2264 2265 2266 2267
lpfc_device_recov_prli_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
			     uint32_t evt)
已提交
2268
{
J
James Smart 已提交
2269 2270 2271
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

2272 2273 2274 2275 2276 2277
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
2278
	/* software abort outstanding PRLI */
2279
	lpfc_els_abort(phba, ndlp);
已提交
2280

2281
	ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
J
James Smart 已提交
2282 2283
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2284
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
2285
	spin_unlock_irq(shost->host_lock);
2286
	lpfc_disc_set_adisc(vport, ndlp);
2287
	return ndlp->nlp_state;
已提交
2288 2289
}

2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
static uint32_t
lpfc_rcv_plogi_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
{
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
	struct ls_rjt     stat;

	memset(&stat, 0, sizeof(struct ls_rjt));
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
	return ndlp->nlp_state;
}

static uint32_t
lpfc_rcv_prli_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
{
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
	struct ls_rjt     stat;

	memset(&stat, 0, sizeof(struct ls_rjt));
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
	return ndlp->nlp_state;
}

static uint32_t
lpfc_rcv_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
{
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;

	spin_lock_irq(shost->host_lock);
2326
	ndlp->nlp_flag |= NLP_LOGO_ACC;
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
	spin_unlock_irq(shost->host_lock);
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
	return ndlp->nlp_state;
}

static uint32_t
lpfc_rcv_padisc_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
{
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
	struct ls_rjt     stat;

	memset(&stat, 0, sizeof(struct ls_rjt));
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
	return ndlp->nlp_state;
}

static uint32_t
lpfc_rcv_prlo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
{
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
	struct ls_rjt     stat;

	memset(&stat, 0, sizeof(struct ls_rjt));
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
	return ndlp->nlp_state;
}

static uint32_t
lpfc_cmpl_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
{
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	ndlp->nlp_prev_state = NLP_STE_LOGO_ISSUE;
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
	spin_unlock_irq(shost->host_lock);
	lpfc_disc_set_adisc(vport, ndlp);
	return ndlp->nlp_state;
}

static uint32_t
lpfc_device_rm_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
{
	/*
2380 2381
	 * DevLoss has timed out and is calling for Device Remove.
	 * In this case, abort the LOGO and cleanup the ndlp
2382
	 */
2383 2384 2385 2386 2387 2388

	lpfc_unreg_rpi(vport, ndlp);
	/* software abort outstanding PLOGI */
	lpfc_els_abort(vport->phba, ndlp);
	lpfc_drop_node(vport, ndlp);
	return NLP_STE_FREED_NODE;
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
}

static uint32_t
lpfc_device_recov_logo_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg, uint32_t evt)
{
	/*
	 * Device Recovery events have no meaning for a node with a LOGO
	 * outstanding.  The LOGO has to complete first and handle the
	 * node from that point.
	 */
	return ndlp->nlp_state;
}

已提交
2404
static uint32_t
J
James Smart 已提交
2405 2406
lpfc_rcv_plogi_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2407
{
J
James Smart 已提交
2408
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2409

J
James Smart 已提交
2410
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2411
	return ndlp->nlp_state;
已提交
2412 2413 2414
}

static uint32_t
J
James Smart 已提交
2415 2416
lpfc_rcv_prli_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2417
{
J
James Smart 已提交
2418
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2419

2420 2421 2422
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;

J
James Smart 已提交
2423 2424
	lpfc_rcv_prli(vport, ndlp, cmdiocb);
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2425
	return ndlp->nlp_state;
已提交
2426 2427 2428
}

static uint32_t
J
James Smart 已提交
2429 2430
lpfc_rcv_logo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2431
{
J
James Smart 已提交
2432
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2433

J
James Smart 已提交
2434
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2435
	return ndlp->nlp_state;
已提交
2436 2437 2438
}

static uint32_t
J
James Smart 已提交
2439 2440
lpfc_rcv_padisc_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2441
{
J
James Smart 已提交
2442
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2443

J
James Smart 已提交
2444
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2445
	return ndlp->nlp_state;
已提交
2446 2447 2448
}

static uint32_t
J
James Smart 已提交
2449 2450
lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2451
{
J
James Smart 已提交
2452
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2453

2454
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2455
	return ndlp->nlp_state;
已提交
2456 2457 2458
}

static uint32_t
J
James Smart 已提交
2459 2460 2461 2462
lpfc_device_recov_unmap_node(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
			     uint32_t evt)
已提交
2463
{
J
James Smart 已提交
2464 2465
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2466
	ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
J
James Smart 已提交
2467 2468
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2469
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2470
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2471 2472
	spin_unlock_irq(shost->host_lock);
	lpfc_disc_set_adisc(vport, ndlp);
已提交
2473

2474
	return ndlp->nlp_state;
已提交
2475 2476 2477
}

static uint32_t
J
James Smart 已提交
2478 2479
lpfc_rcv_plogi_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2480
{
J
James Smart 已提交
2481
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2482

J
James Smart 已提交
2483
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2484
	return ndlp->nlp_state;
已提交
2485 2486 2487
}

static uint32_t
J
James Smart 已提交
2488 2489
lpfc_rcv_prli_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2490
{
J
James Smart 已提交
2491
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2492

2493 2494
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;
J
James Smart 已提交
2495
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2496
	return ndlp->nlp_state;
已提交
2497 2498 2499
}

static uint32_t
J
James Smart 已提交
2500 2501
lpfc_rcv_logo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2502
{
J
James Smart 已提交
2503
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2504

J
James Smart 已提交
2505
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2506
	return ndlp->nlp_state;
已提交
2507 2508 2509
}

static uint32_t
J
James Smart 已提交
2510 2511 2512
lpfc_rcv_padisc_mapped_node(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
2513
{
J
James Smart 已提交
2514
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2515

J
James Smart 已提交
2516
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2517
	return ndlp->nlp_state;
已提交
2518 2519 2520
}

static uint32_t
J
James Smart 已提交
2521 2522
lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2523
{
J
James Smart 已提交
2524 2525
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2526 2527

	/* flush the target */
2528
	lpfc_sli_abort_iocb(vport, &phba->sli.sli3_ring[LPFC_FCP_RING],
2529
			    ndlp->nlp_sid, 0, LPFC_CTX_TGT);
已提交
2530 2531

	/* Treat like rcv logo */
J
James Smart 已提交
2532
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
2533
	return ndlp->nlp_state;
已提交
2534 2535 2536
}

static uint32_t
J
James Smart 已提交
2537 2538 2539 2540
lpfc_device_recov_mapped_node(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
2541
{
J
James Smart 已提交
2542 2543
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2544
	ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
J
James Smart 已提交
2545 2546
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2547
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2548
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2549 2550
	spin_unlock_irq(shost->host_lock);
	lpfc_disc_set_adisc(vport, ndlp);
2551
	return ndlp->nlp_state;
已提交
2552 2553 2554
}

static uint32_t
J
James Smart 已提交
2555 2556
lpfc_rcv_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2557
{
J
James Smart 已提交
2558 2559
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb  = (struct lpfc_iocbq *) arg;
已提交
2560 2561

	/* Ignore PLOGI if we have an outstanding LOGO */
2562
	if (ndlp->nlp_flag & (NLP_LOGO_SND | NLP_LOGO_ACC))
2563
		return ndlp->nlp_state;
J
James Smart 已提交
2564
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
2565
		lpfc_cancel_retry_delay_tmo(vport, ndlp);
J
James Smart 已提交
2566
		spin_lock_irq(shost->host_lock);
2567
		ndlp->nlp_flag &= ~(NLP_NPR_ADISC | NLP_NPR_2B_DISC);
J
James Smart 已提交
2568
		spin_unlock_irq(shost->host_lock);
2569 2570 2571 2572 2573 2574 2575
	} else if (!(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
		/* send PLOGI immediately, move to PLOGI issue state */
		if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
		}
已提交
2576
	}
2577
	return ndlp->nlp_state;
已提交
2578 2579 2580
}

static uint32_t
J
James Smart 已提交
2581 2582
lpfc_rcv_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		       void *arg, uint32_t evt)
已提交
2583
{
J
James Smart 已提交
2584 2585 2586
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
	struct ls_rjt     stat;
已提交
2587 2588 2589 2590

	memset(&stat, 0, sizeof (struct ls_rjt));
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
J
James Smart 已提交
2591
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
已提交
2592 2593 2594

	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
J
James Smart 已提交
2595
			spin_lock_irq(shost->host_lock);
2596
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2597
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2598 2599 2600
			spin_unlock_irq(shost->host_lock);
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
已提交
2601
		} else {
2602
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2603 2604
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
2605 2606
		}
	}
2607
	return ndlp->nlp_state;
已提交
2608 2609 2610
}

static uint32_t
J
James Smart 已提交
2611 2612
lpfc_rcv_logo_npr_node(struct lpfc_vport *vport,  struct lpfc_nodelist *ndlp,
		       void *arg, uint32_t evt)
已提交
2613
{
J
James Smart 已提交
2614
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2615

J
James Smart 已提交
2616
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2617
	return ndlp->nlp_state;
已提交
2618 2619 2620
}

static uint32_t
J
James Smart 已提交
2621 2622
lpfc_rcv_padisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2623
{
J
James Smart 已提交
2624
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2625

J
James Smart 已提交
2626
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
J
James Smart 已提交
2627 2628 2629 2630 2631
	/*
	 * Do not start discovery if discovery is about to start
	 * or discovery in progress for this node. Starting discovery
	 * here will affect the counting of discovery threads.
	 */
J
James Smart 已提交
2632
	if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
2633
	    !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
已提交
2634
		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
2635
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2636
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2637 2638
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
已提交
2639
		} else {
2640
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2641 2642
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
2643 2644
		}
	}
2645
	return ndlp->nlp_state;
已提交
2646 2647 2648
}

static uint32_t
J
James Smart 已提交
2649 2650
lpfc_rcv_prlo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		       void *arg, uint32_t evt)
已提交
2651
{
J
James Smart 已提交
2652 2653
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2654

J
James Smart 已提交
2655
	spin_lock_irq(shost->host_lock);
2656
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
2657
	spin_unlock_irq(shost->host_lock);
2658

2659
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
2660

J
James Smart 已提交
2661
	if ((ndlp->nlp_flag & NLP_DELAY_TMO) == 0) {
2662 2663
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
2664
		spin_lock_irq(shost->host_lock);
2665 2666
		ndlp->nlp_flag |= NLP_DELAY_TMO;
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
2667
		spin_unlock_irq(shost->host_lock);
2668
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
2669
	} else {
J
James Smart 已提交
2670
		spin_lock_irq(shost->host_lock);
2671
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
2672
		spin_unlock_irq(shost->host_lock);
已提交
2673
	}
2674 2675
	return ndlp->nlp_state;
}
已提交
2676

2677
static uint32_t
J
James Smart 已提交
2678 2679
lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
2680 2681
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2682
	IOCB_t *irsp;
2683
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2684 2685 2686

	cmdiocb = (struct lpfc_iocbq *) arg;
	rspiocb = cmdiocb->context_un.rsp_iocb;
2687 2688 2689

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus) {
2690
		spin_lock_irq(shost->host_lock);
2691
		ndlp->nlp_flag |= NLP_DEFER_RM;
2692
		spin_unlock_irq(shost->host_lock);
2693 2694
		return NLP_STE_FREED_NODE;
	}
2695 2696 2697 2698
	return ndlp->nlp_state;
}

static uint32_t
J
James Smart 已提交
2699 2700
lpfc_cmpl_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
2701 2702
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2703
	IOCB_t *irsp;
2704 2705 2706

	cmdiocb = (struct lpfc_iocbq *) arg;
	rspiocb = cmdiocb->context_un.rsp_iocb;
2707 2708 2709

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
J
James Smart 已提交
2710
		lpfc_drop_node(vport, ndlp);
2711 2712
		return NLP_STE_FREED_NODE;
	}
2713
	return ndlp->nlp_state;
已提交
2714 2715 2716
}

static uint32_t
J
James Smart 已提交
2717 2718
lpfc_cmpl_logo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2719
{
2720
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2721 2722

	/* For the fabric port just clear the fc flags. */
2723 2724 2725 2726 2727
	if (ndlp->nlp_DID == Fabric_DID) {
		spin_lock_irq(shost->host_lock);
		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
		spin_unlock_irq(shost->host_lock);
	}
J
James Smart 已提交
2728
	lpfc_unreg_rpi(vport, ndlp);
2729 2730 2731 2732
	return ndlp->nlp_state;
}

static uint32_t
J
James Smart 已提交
2733 2734
lpfc_cmpl_adisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
2735 2736
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2737
	IOCB_t *irsp;
2738 2739 2740

	cmdiocb = (struct lpfc_iocbq *) arg;
	rspiocb = cmdiocb->context_un.rsp_iocb;
2741 2742 2743

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
J
James Smart 已提交
2744
		lpfc_drop_node(vport, ndlp);
2745 2746
		return NLP_STE_FREED_NODE;
	}
2747
	return ndlp->nlp_state;
已提交
2748 2749 2750
}

static uint32_t
J
James Smart 已提交
2751 2752 2753
lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
2754
{
J
James Smart 已提交
2755
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
2756
	MAILBOX_t    *mb = &pmb->u.mb;
已提交
2757

2758
	if (!mb->mbxStatus) {
2759 2760 2761
		/* SLI4 ports have preallocated logical rpis. */
		if (vport->phba->sli_rev < LPFC_SLI_REV4)
			ndlp->nlp_rpi = mb->un.varWords[0];
2762
		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
2763 2764 2765
		if (ndlp->nlp_flag & NLP_LOGO_ACC) {
			lpfc_unreg_rpi(vport, ndlp);
		}
2766
	} else {
2767
		if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
J
James Smart 已提交
2768
			lpfc_drop_node(vport, ndlp);
2769 2770 2771
			return NLP_STE_FREED_NODE;
		}
	}
2772
	return ndlp->nlp_state;
已提交
2773 2774 2775
}

static uint32_t
J
James Smart 已提交
2776 2777
lpfc_device_rm_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2778
{
J
James Smart 已提交
2779 2780
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2781
	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
J
James Smart 已提交
2782
		spin_lock_irq(shost->host_lock);
2783
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
2784
		spin_unlock_irq(shost->host_lock);
2785 2786
		return ndlp->nlp_state;
	}
J
James Smart 已提交
2787
	lpfc_drop_node(vport, ndlp);
2788
	return NLP_STE_FREED_NODE;
已提交
2789 2790 2791
}

static uint32_t
J
James Smart 已提交
2792 2793
lpfc_device_recov_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2794
{
J
James Smart 已提交
2795 2796
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2797 2798 2799 2800 2801 2802
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

2803
	lpfc_cancel_retry_delay_tmo(vport, ndlp);
J
James Smart 已提交
2804
	spin_lock_irq(shost->host_lock);
2805
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2806
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2807
	spin_unlock_irq(shost->host_lock);
2808
	return ndlp->nlp_state;
已提交
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
}


/* This next section defines the NPort Discovery State Machine */

/* There are 4 different double linked lists nodelist entries can reside on.
 * The plogi list and adisc list are used when Link Up discovery or RSCN
 * processing is needed. Each list holds the nodes that we will send PLOGI
 * or ADISC on. These lists will keep track of what nodes will be effected
 * by an RSCN, or a Link Up (Typically, all nodes are effected on Link Up).
 * The unmapped_list will contain all nodes that we have successfully logged
 * into at the Fibre Channel level. The mapped_list will contain all nodes
 * that are mapped FCP targets.
 */
/*
 * The bind list is a list of undiscovered (potentially non-existent) nodes
 * that we have saved binding information on. This information is used when
 * nodes transition from the unmapped to the mapped list.
 */
/* For UNUSED_NODE state, the node has just been allocated .
 * For PLOGI_ISSUE and REG_LOGIN_ISSUE, the node is on
 * the PLOGI list. For REG_LOGIN_COMPL, the node is taken off the PLOGI list
 * and put on the unmapped list. For ADISC processing, the node is taken off
 * the ADISC list and placed on either the mapped or unmapped list (depending
 * on its previous state). Once on the unmapped list, a PRLI is issued and the
 * state changed to PRLI_ISSUE. When the PRLI completion occurs, the state is
 * changed to UNMAPPED_NODE. If the completion indicates a mapped
 * node, the node is taken off the unmapped list. The binding list is checked
 * for a valid binding, or a binding is automatically assigned. If binding
 * assignment is unsuccessful, the node is left on the unmapped list. If
 * binding assignment is successful, the associated binding list entry (if
 * any) is removed, and the node is placed on the mapped list.
 */
/*
 * For a Link Down, all nodes on the ADISC, PLOGI, unmapped or mapped
2844
 * lists will receive a DEVICE_RECOVERY event. If the linkdown or devloss timers
已提交
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
 * expire, all effected nodes will receive a DEVICE_RM event.
 */
/*
 * For a Link Up or RSCN, all nodes will move from the mapped / unmapped lists
 * to either the ADISC or PLOGI list.  After a Nameserver query or ALPA loopmap
 * check, additional nodes may be added or removed (via DEVICE_RM) to / from
 * the PLOGI or ADISC lists. Once the PLOGI and ADISC lists are populated,
 * we will first process the ADISC list.  32 entries are processed initially and
 * ADISC is initited for each one.  Completions / Events for each node are
 * funnelled thru the state machine.  As each node finishes ADISC processing, it
 * starts ADISC for any nodes waiting for ADISC processing. If no nodes are
 * waiting, and the ADISC list count is identically 0, then we are done. For
 * Link Up discovery, since all nodes on the PLOGI list are UNREG_LOGIN'ed, we
 * can issue a CLEAR_LA and reenable Link Events. Next we will process the PLOGI
 * list.  32 entries are processed initially and PLOGI is initited for each one.
 * Completions / Events for each node are funnelled thru the state machine.  As
 * each node finishes PLOGI processing, it starts PLOGI for any nodes waiting
 * for PLOGI processing. If no nodes are waiting, and the PLOGI list count is
 * indentically 0, then we are done. We have now completed discovery / RSCN
 * handling. Upon completion, ALL nodes should be on either the mapped or
 * unmapped lists.
 */

static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
J
James Smart 已提交
2869
     (struct lpfc_vport *, struct lpfc_nodelist *, void *, uint32_t) = {
已提交
2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882
	/* Action routine                  Event       Current State  */
	lpfc_rcv_plogi_unused_node,	/* RCV_PLOGI   UNUSED_NODE    */
	lpfc_rcv_els_unused_node,	/* RCV_PRLI        */
	lpfc_rcv_logo_unused_node,	/* RCV_LOGO        */
	lpfc_rcv_els_unused_node,	/* RCV_ADISC       */
	lpfc_rcv_els_unused_node,	/* RCV_PDISC       */
	lpfc_rcv_els_unused_node,	/* RCV_PRLO        */
	lpfc_disc_illegal,		/* CMPL_PLOGI      */
	lpfc_disc_illegal,		/* CMPL_PRLI       */
	lpfc_cmpl_logo_unused_node,	/* CMPL_LOGO       */
	lpfc_disc_illegal,		/* CMPL_ADISC      */
	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
	lpfc_device_rm_unused_node,	/* DEVICE_RM       */
2883
	lpfc_device_recov_unused_node,	/* DEVICE_RECOVERY */
已提交
2884 2885

	lpfc_rcv_plogi_plogi_issue,	/* RCV_PLOGI   PLOGI_ISSUE    */
2886
	lpfc_rcv_prli_plogi_issue,	/* RCV_PRLI        */
2887
	lpfc_rcv_logo_plogi_issue,	/* RCV_LOGO        */
已提交
2888 2889 2890 2891 2892
	lpfc_rcv_els_plogi_issue,	/* RCV_ADISC       */
	lpfc_rcv_els_plogi_issue,	/* RCV_PDISC       */
	lpfc_rcv_els_plogi_issue,	/* RCV_PRLO        */
	lpfc_cmpl_plogi_plogi_issue,	/* CMPL_PLOGI      */
	lpfc_disc_illegal,		/* CMPL_PRLI       */
2893
	lpfc_cmpl_logo_plogi_issue,	/* CMPL_LOGO       */
已提交
2894
	lpfc_disc_illegal,		/* CMPL_ADISC      */
2895
	lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
已提交
2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
	lpfc_device_rm_plogi_issue,	/* DEVICE_RM       */
	lpfc_device_recov_plogi_issue,	/* DEVICE_RECOVERY */

	lpfc_rcv_plogi_adisc_issue,	/* RCV_PLOGI   ADISC_ISSUE    */
	lpfc_rcv_prli_adisc_issue,	/* RCV_PRLI        */
	lpfc_rcv_logo_adisc_issue,	/* RCV_LOGO        */
	lpfc_rcv_padisc_adisc_issue,	/* RCV_ADISC       */
	lpfc_rcv_padisc_adisc_issue,	/* RCV_PDISC       */
	lpfc_rcv_prlo_adisc_issue,	/* RCV_PRLO        */
	lpfc_disc_illegal,		/* CMPL_PLOGI      */
	lpfc_disc_illegal,		/* CMPL_PRLI       */
	lpfc_disc_illegal,		/* CMPL_LOGO       */
	lpfc_cmpl_adisc_adisc_issue,	/* CMPL_ADISC      */
	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
	lpfc_device_rm_adisc_issue,	/* DEVICE_RM       */
	lpfc_device_recov_adisc_issue,	/* DEVICE_RECOVERY */

	lpfc_rcv_plogi_reglogin_issue,	/* RCV_PLOGI  REG_LOGIN_ISSUE */
	lpfc_rcv_prli_reglogin_issue,	/* RCV_PLOGI       */
	lpfc_rcv_logo_reglogin_issue,	/* RCV_LOGO        */
	lpfc_rcv_padisc_reglogin_issue,	/* RCV_ADISC       */
	lpfc_rcv_padisc_reglogin_issue,	/* RCV_PDISC       */
	lpfc_rcv_prlo_reglogin_issue,	/* RCV_PRLO        */
2919
	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
已提交
2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
	lpfc_disc_illegal,		/* CMPL_PRLI       */
	lpfc_disc_illegal,		/* CMPL_LOGO       */
	lpfc_disc_illegal,		/* CMPL_ADISC      */
	lpfc_cmpl_reglogin_reglogin_issue,/* CMPL_REG_LOGIN  */
	lpfc_device_rm_reglogin_issue,	/* DEVICE_RM       */
	lpfc_device_recov_reglogin_issue,/* DEVICE_RECOVERY */

	lpfc_rcv_plogi_prli_issue,	/* RCV_PLOGI   PRLI_ISSUE     */
	lpfc_rcv_prli_prli_issue,	/* RCV_PRLI        */
	lpfc_rcv_logo_prli_issue,	/* RCV_LOGO        */
	lpfc_rcv_padisc_prli_issue,	/* RCV_ADISC       */
	lpfc_rcv_padisc_prli_issue,	/* RCV_PDISC       */
	lpfc_rcv_prlo_prli_issue,	/* RCV_PRLO        */
2933
	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
已提交
2934 2935 2936 2937 2938 2939 2940
	lpfc_cmpl_prli_prli_issue,	/* CMPL_PRLI       */
	lpfc_disc_illegal,		/* CMPL_LOGO       */
	lpfc_disc_illegal,		/* CMPL_ADISC      */
	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
	lpfc_device_rm_prli_issue,	/* DEVICE_RM       */
	lpfc_device_recov_prli_issue,	/* DEVICE_RECOVERY */

2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
	lpfc_rcv_plogi_logo_issue,	/* RCV_PLOGI   LOGO_ISSUE     */
	lpfc_rcv_prli_logo_issue,	/* RCV_PRLI        */
	lpfc_rcv_logo_logo_issue,	/* RCV_LOGO        */
	lpfc_rcv_padisc_logo_issue,	/* RCV_ADISC       */
	lpfc_rcv_padisc_logo_issue,	/* RCV_PDISC       */
	lpfc_rcv_prlo_logo_issue,	/* RCV_PRLO        */
	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
	lpfc_disc_illegal,		/* CMPL_PRLI       */
	lpfc_cmpl_logo_logo_issue,	/* CMPL_LOGO       */
	lpfc_disc_illegal,		/* CMPL_ADISC      */
	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
	lpfc_device_rm_logo_issue,	/* DEVICE_RM       */
	lpfc_device_recov_logo_issue,	/* DEVICE_RECOVERY */

已提交
2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
	lpfc_rcv_plogi_unmap_node,	/* RCV_PLOGI   UNMAPPED_NODE  */
	lpfc_rcv_prli_unmap_node,	/* RCV_PRLI        */
	lpfc_rcv_logo_unmap_node,	/* RCV_LOGO        */
	lpfc_rcv_padisc_unmap_node,	/* RCV_ADISC       */
	lpfc_rcv_padisc_unmap_node,	/* RCV_PDISC       */
	lpfc_rcv_prlo_unmap_node,	/* RCV_PRLO        */
	lpfc_disc_illegal,		/* CMPL_PLOGI      */
	lpfc_disc_illegal,		/* CMPL_PRLI       */
	lpfc_disc_illegal,		/* CMPL_LOGO       */
	lpfc_disc_illegal,		/* CMPL_ADISC      */
	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
	lpfc_disc_illegal,		/* DEVICE_RM       */
	lpfc_device_recov_unmap_node,	/* DEVICE_RECOVERY */

	lpfc_rcv_plogi_mapped_node,	/* RCV_PLOGI   MAPPED_NODE    */
	lpfc_rcv_prli_mapped_node,	/* RCV_PRLI        */
	lpfc_rcv_logo_mapped_node,	/* RCV_LOGO        */
	lpfc_rcv_padisc_mapped_node,	/* RCV_ADISC       */
	lpfc_rcv_padisc_mapped_node,	/* RCV_PDISC       */
	lpfc_rcv_prlo_mapped_node,	/* RCV_PRLO        */
	lpfc_disc_illegal,		/* CMPL_PLOGI      */
	lpfc_disc_illegal,		/* CMPL_PRLI       */
	lpfc_disc_illegal,		/* CMPL_LOGO       */
	lpfc_disc_illegal,		/* CMPL_ADISC      */
	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
	lpfc_disc_illegal,		/* DEVICE_RM       */
	lpfc_device_recov_mapped_node,	/* DEVICE_RECOVERY */

	lpfc_rcv_plogi_npr_node,        /* RCV_PLOGI   NPR_NODE    */
	lpfc_rcv_prli_npr_node,         /* RCV_PRLI        */
	lpfc_rcv_logo_npr_node,         /* RCV_LOGO        */
	lpfc_rcv_padisc_npr_node,       /* RCV_ADISC       */
	lpfc_rcv_padisc_npr_node,       /* RCV_PDISC       */
	lpfc_rcv_prlo_npr_node,         /* RCV_PRLO        */
2989 2990
	lpfc_cmpl_plogi_npr_node,	/* CMPL_PLOGI      */
	lpfc_cmpl_prli_npr_node,	/* CMPL_PRLI       */
已提交
2991
	lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
2992
	lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
已提交
2993 2994 2995 2996 2997 2998
	lpfc_cmpl_reglogin_npr_node,    /* CMPL_REG_LOGIN  */
	lpfc_device_rm_npr_node,        /* DEVICE_RM       */
	lpfc_device_recov_npr_node,     /* DEVICE_RECOVERY */
};

int
J
James Smart 已提交
2999 3000
lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
3001 3002
{
	uint32_t cur_state, rc;
J
James Smart 已提交
3003
	uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
已提交
3004
			 uint32_t);
3005
	uint32_t got_ndlp = 0;
3006
	uint32_t data1;
3007 3008 3009

	if (lpfc_nlp_get(ndlp))
		got_ndlp = 1;
已提交
3010 3011 3012

	cur_state = ndlp->nlp_state;

3013 3014
	data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
		((uint32_t)ndlp->nlp_type));
已提交
3015
	/* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
3016 3017
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0211 DSM in event x%x on NPort x%x in "
3018 3019
			 "state %d rpi x%x Data: x%x x%x\n",
			 evt, ndlp->nlp_DID, cur_state, ndlp->nlp_rpi,
3020
			 ndlp->nlp_flag, data1);
已提交
3021

J
James Smart 已提交
3022 3023 3024 3025
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
		 "DSM in:          evt:%d ste:%d did:x%x",
		evt, cur_state, ndlp->nlp_DID);

已提交
3026
	func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
J
James Smart 已提交
3027
	rc = (func) (vport, ndlp, arg, evt);
已提交
3028 3029

	/* DSM out state <rc> on NPort <nlp_DID> */
3030
	if (got_ndlp) {
3031 3032
		data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
			((uint32_t)ndlp->nlp_type));
3033
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3034
			 "0212 DSM out state %d on NPort x%x "
3035 3036 3037
			 "rpi x%x Data: x%x x%x\n",
			 rc, ndlp->nlp_DID, ndlp->nlp_rpi, ndlp->nlp_flag,
			 data1);
已提交
3038

3039 3040 3041 3042 3043 3044 3045
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
			"DSM out:         ste:%d did:x%x flg:x%x",
			rc, ndlp->nlp_DID, ndlp->nlp_flag);
		/* Decrement the ndlp reference count held for this function */
		lpfc_nlp_put(ndlp);
	} else {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3046
			"0213 DSM out state %d on NPort free\n", rc);
J
James Smart 已提交
3047

3048 3049 3050 3051
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
			"DSM out:         ste:%d did:x%x flg:x%x",
			rc, 0, 0);
	}
已提交
3052

3053
	return rc;
已提交
3054
}