lpfc_nportdisc.c 94.2 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
#include <scsi/fc/fc_fs.h>

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


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

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

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

	/* we match, return success */
69
	return 1;
已提交
70 71 72
}

int
J
James Smart 已提交
73
lpfc_check_sparm(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
74
		 struct serv_parm *sp, uint32_t class, int flogi)
已提交
75
{
J
James Smart 已提交
76
	volatile struct serv_parm *hsp = &vport->fc_sparam;
77 78 79 80 81 82 83 84 85
	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.
	 */
已提交
86
	if (sp->cls1.classValid) {
87 88 89 90 91 92 93 94 95 96 97 98 99
		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;
			}
100
		}
101
	} else if (class == CLASS1)
102
		goto bad_service_param;
已提交
103
	if (sp->cls2.classValid) {
104 105 106 107 108 109 110 111 112 113 114 115 116
		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;
			}
117
		}
118
	} else if (class == CLASS2)
119
		goto bad_service_param;
已提交
120
	if (sp->cls3.classValid) {
121 122 123 124 125 126 127 128 129 130 131 132 133
		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;
			}
134
		}
135
	} else if (class == CLASS3)
136
		goto bad_service_param;
已提交
137

138 139 140 141 142 143 144 145
	/*
	 * 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) {
已提交
146
		sp->cmn.bbRcvSizeLsb = hsp->cmn.bbRcvSizeLsb;
147 148 149
		sp->cmn.bbRcvSizeMsb = (sp->cmn.bbRcvSizeMsb & 0xF0) |
				       (hsp->cmn.bbRcvSizeMsb & 0x0F);
	}
已提交
150 151 152

	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof (struct lpfc_name));
153
	return 1;
154
bad_service_param:
155
	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
156 157 158 159 160 161 162 163
			 "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]);
164
	return 0;
已提交
165 166 167
}

static void *
J
James Smart 已提交
168
lpfc_check_elscmpl_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
169
			struct lpfc_iocbq *rspiocb)
已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
{
	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));
		}
189
	} else {
已提交
190 191 192 193 194 195 196
		/* 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;
	}
197
	return ptr;
已提交
198 199 200
}


201

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

214 215
	pring = lpfc_phba_elsring(phba);

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

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

229 230 231 232 233
	/*
	 * 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 已提交
234
	spin_lock_irq(&phba->hbalock);
235 236 237 238
	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. */
239 240
		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
			list_add_tail(&iocb->dlist, &abort_list);
241
	}
242 243
	if (phba->sli_rev == LPFC_SLI_REV4)
		spin_unlock(&pring->ring_lock);
J
James Smart 已提交
244
	spin_unlock_irq(&phba->hbalock);
已提交
245

246
	/* Abort the targeted IOs and remove them from the abort list. */
247 248 249 250 251 252 253
	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);
	}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
	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);

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

277
	lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
已提交
278 279
}

280 281 282 283 284 285 286
/* 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.
 */
287
static void
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
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) {
304
		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
305 306 307 308
				"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);
309
		kfree(save_iocb);
310 311 312 313 314 315 316 317 318
		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) {
319
		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
320 321 322 323 324 325
				"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);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	kfree(save_iocb);
}

/**
 * lpfc_defer_tgt_acc - Progress SLI4 target rcv PLOGI handler
 * @phba: Pointer to HBA context object.
 * @pmb: Pointer to mailbox object.
 *
 * This function provides the unreg rpi mailbox completion handler for a tgt.
 * The routine frees the memory resources associated with the completed
 * mailbox command and transmits the ELS ACC.
 *
 * This routine is only called if we are SLI4, acting in target
 * mode and the remote NPort issues the PLOGI after link up.
 **/
341
static void
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
lpfc_defer_acc_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
{
	struct lpfc_vport *vport = pmb->vport;
	struct lpfc_nodelist *ndlp = pmb->ctx_ndlp;
	LPFC_MBOXQ_t *mbox = pmb->context3;
	struct lpfc_iocbq *piocb = NULL;
	int rc;

	if (mbox) {
		pmb->context3 = NULL;
		piocb = mbox->context3;
		mbox->context3 = NULL;
	}

	/*
	 * Complete the unreg rpi mbx request, and update flags.
	 * This will also restart any deferred events.
	 */
	lpfc_sli4_unreg_rpi_cmpl_clr(phba, pmb);

	if (!piocb) {
363
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
364 365 366
				 "4578 PLOGI ACC fail\n");
		if (mbox)
			mempool_free(mbox, phba->mbox_mem_pool);
367
		return;
368 369 370 371
	}

	rc = lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, piocb, ndlp, mbox);
	if (rc) {
372
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
373 374 375 376 377
				 "4579 PLOGI ACC fail %x\n", rc);
		if (mbox)
			mempool_free(mbox, phba->mbox_mem_pool);
	}
	kfree(piocb);
378 379
}

已提交
380
static int
J
James Smart 已提交
381
lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
382
	       struct lpfc_iocbq *cmdiocb)
已提交
383
{
J
James Smart 已提交
384 385
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba    *phba = vport->phba;
已提交
386
	struct lpfc_dmabuf *pcmd;
387
	uint64_t nlp_portwwn = 0;
已提交
388 389 390
	uint32_t *lp;
	IOCB_t *icmd;
	struct serv_parm *sp;
391
	uint32_t ed_tov;
392 393 394
	LPFC_MBOXQ_t *link_mbox;
	LPFC_MBOXQ_t *login_mbox;
	struct lpfc_iocbq *save_iocb;
已提交
395
	struct ls_rjt stat;
396
	uint32_t vid, flag;
397
	u16 rpi;
398
	int rc, defer_acc;
已提交
399 400 401 402 403

	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));
404
	if (wwn_to_u64(sp->portName.u.wwn) == 0) {
405
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
406 407 408 409 410 411 412 413
				 "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) {
414
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
415 416 417 418 419 420 421
				 "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;
	}
422 423

	nlp_portwwn = wwn_to_u64(ndlp->nlp_portname.u.wwn);
424
	if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0) == 0)) {
已提交
425 426 427
		/* Reject this request because invalid parameters */
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
J
James Smart 已提交
428 429
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
430
		return 0;
已提交
431 432 433 434
	}
	icmd = &cmdiocb->iocb;

	/* PLOGI chkparm OK */
435
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
436 437
			 "0114 PLOGI chkparm OK Data: x%x x%x x%x "
			 "x%x x%x x%x\n",
438
			 ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
439 440
			 ndlp->nlp_rpi, vport->port_state,
			 vport->fc_flag);
已提交
441

442
	if (vport->cfg_fcp_class == 2 && sp->cls2.classValid)
已提交
443
		ndlp->nlp_fcp_info |= CLASS2;
444
	else
已提交
445
		ndlp->nlp_fcp_info |= CLASS3;
J
James Smart 已提交
446

447
	defer_acc = 0;
已提交
448 449 450 451 452 453 454 455 456 457 458
	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;
459
	/* if already logged in, do implicit logout */
460
	switch (ndlp->nlp_state) {
已提交
461 462 463
	case  NLP_STE_NPR_NODE:
		if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
			break;
464
		fallthrough;
已提交
465 466 467 468
	case  NLP_STE_REG_LOGIN_ISSUE:
	case  NLP_STE_PRLI_ISSUE:
	case  NLP_STE_UNMAPPED_NODE:
	case  NLP_STE_MAPPED_NODE:
469 470 471 472 473 474
		/* 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)) {
475 476 477 478 479 480
			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))
481
			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
482 483 484 485 486 487 488
					 "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));

489 490 491 492 493
		/* Notify transport of connectivity loss to trigger cleanup. */
		if (phba->nvmet_support &&
		    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
			lpfc_nvmet_invalidate_host(phba, ndlp);

494 495 496 497
		ndlp->nlp_prev_state = ndlp->nlp_state;
		/* rport needs to be unregistered first */
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		break;
已提交
498 499
	}

500 501 502 503 504
	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;

505 506 507 508
	login_mbox = NULL;
	link_mbox = NULL;
	save_iocb = NULL;

509
	/* Check for Nport to NPort pt2pt protocol */
510 511
	if ((vport->fc_flag & FC_PT2PT) &&
	    !(vport->fc_flag & FC_PT2PT_PLOGI)) {
已提交
512
		/* rcv'ed PLOGI decides what our NPortId will be */
J
James Smart 已提交
513
		vport->fc_myDID = icmd->un.rcvels.parmRo;
514 515 516 517 518

		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;
已提交
519
		}
520

521
		/*
522 523
		 * For pt-to-pt, use the larger EDTOV
		 * RATOV = 2 * EDTOV
524
		 */
525 526 527 528 529 530 531 532
		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 */

533 534
		if (phba->sli_rev == LPFC_SLI_REV4)
			lpfc_issue_reg_vfi(vport);
535
		else {
536 537 538 539
			defer_acc = 1;
			link_mbox = mempool_alloc(phba->mbox_mem_pool,
						  GFP_KERNEL);
			if (!link_mbox)
540
				goto out;
541 542 543 544 545
			lpfc_config_link(phba, link_mbox);
			link_mbox->mbox_cmpl = lpfc_defer_pt2pt_acc;
			link_mbox->vport = vport;
			link_mbox->ctx_ndlp = ndlp;

546
			save_iocb = kzalloc(sizeof(*save_iocb), GFP_KERNEL);
547
			if (!save_iocb)
548
				goto out;
549 550 551
			/* Save info from cmd IOCB used in rsp */
			memcpy((uint8_t *)save_iocb, (uint8_t *)cmdiocb,
			       sizeof(struct lpfc_iocbq));
552
		}
已提交
553

J
James Smart 已提交
554
		lpfc_can_disctmo(vport);
已提交
555
	}
556

557 558 559 560 561 562 563 564 565
	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;
	}

566 567
	login_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!login_mbox)
已提交
568 569
		goto out;

570
	/* Registering an existing RPI behaves differently for SLI3 vs SLI4 */
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
	if (phba->nvmet_support && !defer_acc) {
		link_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
		if (!link_mbox)
			goto out;

		/* As unique identifiers such as iotag would be overwritten
		 * with those from the cmdiocb, allocate separate temporary
		 * storage for the copy.
		 */
		save_iocb = kzalloc(sizeof(*save_iocb), GFP_KERNEL);
		if (!save_iocb)
			goto out;

		/* Unreg RPI is required for SLI4. */
		rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
		lpfc_unreg_login(phba, vport->vpi, rpi, link_mbox);
		link_mbox->vport = vport;
588 589 590 591
		link_mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
		if (!link_mbox->ctx_ndlp)
			goto out;

592 593 594 595 596 597 598 599 600 601 602 603
		link_mbox->mbox_cmpl = lpfc_defer_acc_rsp;

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

		/* Save info from cmd IOCB used in rsp */
		memcpy(save_iocb, cmdiocb, sizeof(*save_iocb));

		/* Delay sending ACC till unreg RPI completes. */
		defer_acc = 1;
	} else if (phba->sli_rev == LPFC_SLI_REV4)
604 605
		lpfc_unreg_rpi(vport, ndlp);

606
	rc = lpfc_reg_rpi(phba, vport->vpi, icmd->un.rcvels.remoteID,
607 608
			    (uint8_t *)sp, login_mbox, ndlp->nlp_rpi);
	if (rc)
已提交
609 610 611
		goto out;

	/* ACC PLOGI rsp command needs to execute first,
612
	 * queue this login_mbox command to be processed later.
已提交
613
	 */
614
	login_mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
615
	/*
616
	 * login_mbox->ctx_ndlp = lpfc_nlp_get(ndlp) deferred until mailbox
617 618
	 * command issued in lpfc_cmpl_els_acc().
	 */
619
	login_mbox->vport = vport;
J
James Smart 已提交
620
	spin_lock_irq(shost->host_lock);
621
	ndlp->nlp_flag |= (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI);
J
James Smart 已提交
622
	spin_unlock_irq(shost->host_lock);
已提交
623

J
James Smart 已提交
624 625 626 627 628 629 630 631
	/*
	 * 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.
632
	 * This only applies to a fabric environment.
J
James Smart 已提交
633
	 */
634 635
	if ((ndlp->nlp_state == NLP_STE_PLOGI_ISSUE) &&
	    (vport->fc_flag & FC_FABRIC)) {
J
James Smart 已提交
636
		/* software abort outstanding PLOGI */
637
		lpfc_els_abort(phba, ndlp);
J
James Smart 已提交
638 639
	}

J
James Smart 已提交
640
	if ((vport->port_type == LPFC_NPIV_PORT &&
641
	     vport->cfg_restrict_login)) {
J
James Smart 已提交
642

643 644 645
		/* no deferred ACC */
		kfree(save_iocb);

J
James Smart 已提交
646 647 648 649 650 651 652 653 654 655
		/* 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;
656
		rc = lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
657
			ndlp, login_mbox);
658
		if (rc)
659 660 661 662 663
			mempool_free(login_mbox, phba->mbox_mem_pool);
		return 1;
	}
	if (defer_acc) {
		/* So the order here should be:
664 665 666 667 668 669
		 * SLI3 pt2pt
		 *   Issue CONFIG_LINK mbox
		 *   CONFIG_LINK cmpl
		 * SLI4 tgt
		 *   Issue UNREG RPI mbx
		 *   UNREG RPI cmpl
670 671 672 673 674 675 676 677 678 679 680 681 682
		 * 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 已提交
683 684
		return 1;
	}
685 686

	rc = lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, login_mbox);
687
	if (rc)
688
		mempool_free(login_mbox, phba->mbox_mem_pool);
689
	return 1;
已提交
690
out:
691
	if (defer_acc)
692
		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
693
				"4577 discovery failure: %p %p %p\n",
694
				save_iocb, link_mbox, login_mbox);
695
	kfree(save_iocb);
696 697 698 699 700
	if (link_mbox)
		mempool_free(link_mbox, phba->mbox_mem_pool);
	if (login_mbox)
		mempool_free(login_mbox, phba->mbox_mem_pool);

已提交
701 702
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
J
James Smart 已提交
703
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
704
	return 0;
已提交
705 706
}

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
/**
 * 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;

723 724
	elsiocb = (struct lpfc_iocbq *)mboxq->ctx_buf;
	ndlp = (struct lpfc_nodelist *)mboxq->ctx_ndlp;
725 726 727 728 729 730 731 732 733 734
	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);
735
	mempool_free(mboxq, phba->mbox_mem_pool);
736 737
}

已提交
738
static int
J
James Smart 已提交
739
lpfc_rcv_padisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
740 741
		struct lpfc_iocbq *cmdiocb)
{
J
James Smart 已提交
742
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
743
	struct lpfc_iocbq  *elsiocb;
已提交
744
	struct lpfc_dmabuf *pcmd;
J
James Smart 已提交
745 746
	struct serv_parm   *sp;
	struct lpfc_name   *pnn, *ppn;
已提交
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
	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 已提交
768
	if (icmd->ulpStatus == 0 && lpfc_check_adisc(vport, ndlp, pnn, ppn)) {
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

		/*
		 * 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;
			}
		}

已提交
793
		if (cmd == ELS_CMD_ADISC) {
J
James Smart 已提交
794
			lpfc_els_rsp_adisc_acc(vport, cmdiocb, ndlp);
795
		} else {
796 797
			lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
				ndlp, NULL);
已提交
798
		}
799
out:
800 801 802 803 804 805 806 807 808 809 810
		/* If we are authenticated, move to the proper state.
		 * It is possible an ADISC arrived and the remote nport
		 * is already in MAPPED or UNMAPPED state.  Catch this
		 * condition and don't set the nlp_state again because
		 * it causes an unnecessary transport unregister/register.
		 */
		if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET)) {
			if (ndlp->nlp_state != NLP_STE_MAPPED_NODE)
				lpfc_nlp_set_state(vport, ndlp,
						   NLP_STE_MAPPED_NODE);
		}
811

812
		return 1;
已提交
813 814 815 816 817 818
	}
	/* 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 已提交
819
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
已提交
820 821

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

J
James Smart 已提交
824
	spin_lock_irq(shost->host_lock);
已提交
825
	ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
826
	spin_unlock_irq(shost->host_lock);
827 828
	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
	ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
829
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
830
	return 0;
已提交
831 832 833
}

static int
J
James Smart 已提交
834 835
lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
	      struct lpfc_iocbq *cmdiocb, uint32_t els_cmd)
已提交
836
{
J
James Smart 已提交
837
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
838 839 840
	struct lpfc_hba    *phba = vport->phba;
	struct lpfc_vport **vports;
	int i, active_vlink_present = 0 ;
J
James Smart 已提交
841 842

	/* Put ndlp in NPR state with 1 sec timeout for plogi, ACC logo */
已提交
843 844 845
	/* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
	 * PLOGIs during LOGO storms from a device.
	 */
J
James Smart 已提交
846
	spin_lock_irq(shost->host_lock);
已提交
847
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
848
	spin_unlock_irq(shost->host_lock);
849
	if (els_cmd == ELS_CMD_PRLO)
850
		lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
851
	else
852
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
853 854 855 856 857 858

	/* Notify transport of connectivity loss to trigger cleanup. */
	if (phba->nvmet_support &&
	    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
		lpfc_nvmet_invalidate_host(phba, ndlp);

J
James Smart 已提交
859 860 861
	if (ndlp->nlp_DID == Fabric_DID) {
		if (vport->port_state <= LPFC_FDISC)
			goto out;
862 863
		lpfc_linkdown_port(vport);
		spin_lock_irq(shost->host_lock);
J
James Smart 已提交
864
		vport->fc_flag |= FC_VPORT_LOGO_RCVD;
865
		spin_unlock_irq(shost->host_lock);
J
James Smart 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878
		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);
		}
已提交
879

J
James Smart 已提交
880 881 882 883 884 885 886
		/*
		 * 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 已提交
887 888 889 890
			/*
			 * If there are other active VLinks present,
			 * re-instantiate the Vlink using FDISC.
			 */
891 892
			mod_timer(&ndlp->nlp_delayfunc,
				  jiffies + msecs_to_jiffies(1000));
J
James Smart 已提交
893 894 895 896 897 898 899 900 901 902 903
			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);
		}
904 905 906 907
	} 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)) {
已提交
908
		/* Only try to re-login if this is NOT a Fabric Node */
909 910
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
911
		spin_lock_irq(shost->host_lock);
已提交
912
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
913
		spin_unlock_irq(shost->host_lock);
已提交
914

915
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
916
	}
J
James Smart 已提交
917
out:
918 919
	ndlp->nlp_prev_state = ndlp->nlp_state;
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
920

J
James Smart 已提交
921
	spin_lock_irq(shost->host_lock);
已提交
922
	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
923
	spin_unlock_irq(shost->host_lock);
已提交
924 925 926 927 928
	/* 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.
	 */
929
	return 0;
已提交
930 931
}

932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
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;
}

已提交
967
static void
J
James Smart 已提交
968 969
lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
	      struct lpfc_iocbq *cmdiocb)
已提交
970
{
971
	struct lpfc_hba  *phba = vport->phba;
已提交
972 973 974 975 976 977 978 979 980 981
	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));

982 983 984 985 986 987 988 989
	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;
		}
990
		if (npr->targetFunc) {
991 992 993 994
			if (npr->prliType == PRLI_FCP_TYPE)
				ndlp->nlp_type |= NLP_FCP_TARGET;
			if (npr->prliType == PRLI_NVME_TYPE)
				ndlp->nlp_type |= NLP_NVME_TARGET;
995 996 997
			if (npr->writeXferRdyDis)
				ndlp->nlp_flag |= NLP_FIRSTBURST;
		}
998 999
		if (npr->Retry && ndlp->nlp_type &
					(NLP_FCP_INITIATOR | NLP_FCP_TARGET))
已提交
1000
			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
1001

1002 1003 1004 1005 1006
		if (npr->Retry && phba->nsler &&
		    ndlp->nlp_type & (NLP_NVME_INITIATOR | NLP_NVME_TARGET))
			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;


1007 1008 1009 1010 1011
		/* 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.
		 */
1012
		if (phba->nvmet_support && (npr->prliType == PRLI_NVME_TYPE)) {
1013
			ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1014 1015 1016 1017
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
		}
		if (npr->prliType == PRLI_FCP_TYPE)
			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
已提交
1018 1019 1020 1021 1022 1023 1024 1025
	}
	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 已提交
1026 1027 1028 1029 1030

		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);

1031
		if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1032
			fc_remote_port_rolechg(rport, roles);
已提交
1033 1034 1035 1036
	}
}

static uint32_t
J
James Smart 已提交
1037
lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
已提交
1038
{
J
James Smart 已提交
1039 1040
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

1041
	if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
1042
		spin_lock_irq(shost->host_lock);
1043
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1044
		spin_unlock_irq(shost->host_lock);
1045 1046 1047
		return 0;
	}

1048 1049
	if (!(vport->fc_flag & FC_PT2PT)) {
		/* Check config parameter use-adisc or FCP-2 */
1050
		if (vport->cfg_use_adisc && ((vport->fc_flag & FC_RSCN_MODE) ||
1051
		    ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) &&
1052
		     (ndlp->nlp_type & NLP_FCP_TARGET)))) {
1053 1054 1055 1056 1057
			spin_lock_irq(shost->host_lock);
			ndlp->nlp_flag |= NLP_NPR_ADISC;
			spin_unlock_irq(shost->host_lock);
			return 1;
		}
1058
	}
1059 1060

	spin_lock_irq(shost->host_lock);
1061
	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1062
	spin_unlock_irq(shost->host_lock);
1063 1064
	lpfc_unreg_rpi(vport, ndlp);
	return 0;
已提交
1065
}
1066

1067
/**
L
Lucas De Marchi 已提交
1068
 * lpfc_release_rpi - Release a RPI by issuing unreg_login mailbox cmd.
1069 1070
 * @phba : Pointer to lpfc_hba structure.
 * @vport: Pointer to lpfc_vport structure.
1071
 * @ndlp: Pointer to lpfc_nodelist structure.
1072 1073 1074 1075 1076
 * @rpi  : rpi to be release.
 *
 * This function will send a unreg_login mailbox command to the firmware
 * to release a rpi.
 **/
1077
static void
1078 1079
lpfc_release_rpi(struct lpfc_hba *phba, struct lpfc_vport *vport,
		 struct lpfc_nodelist *ndlp, uint16_t rpi)
1080 1081 1082 1083
{
	LPFC_MBOXQ_t *pmb;
	int rc;

1084 1085 1086 1087 1088 1089 1090
	/* 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 "
1091
				 "Data: x%px\n",
1092 1093 1094 1095 1096 1097
				 ndlp->nlp_rpi, ndlp->nlp_DID,
				 ndlp->nlp_defer_did,
				 ndlp->nlp_flag, ndlp);
		return;
	}

1098 1099 1100
	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
			GFP_KERNEL);
	if (!pmb)
1101 1102
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
				 "2796 mailbox memory allocation failed \n");
1103 1104 1105
	else {
		lpfc_unreg_login(phba, vport->vpi, rpi, pmb);
		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1106
		pmb->vport = vport;
1107 1108 1109 1110 1111
		pmb->ctx_ndlp = lpfc_nlp_get(ndlp);
		if (!pmb->ctx_ndlp) {
			mempool_free(pmb, phba->mbox_mem_pool);
			return;
		}
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121

		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);

1122 1123 1124 1125 1126
		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
		if (rc == MBX_NOT_FINISHED)
			mempool_free(pmb, phba->mbox_mem_pool);
	}
}
已提交
1127 1128

static uint32_t
J
James Smart 已提交
1129 1130
lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		  void *arg, uint32_t evt)
已提交
1131
{
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
	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];
1142
		lpfc_release_rpi(phba, vport, ndlp, rpi);
1143
	}
1144
	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1145
			 "0271 Illegal State Transition: node x%x "
1146 1147 1148
			 "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);
1149
	return ndlp->nlp_state;
已提交
1150 1151
}

1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
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)) {
1162 1163 1164 1165 1166
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
				 "0272 Illegal State Transition: node x%x "
				 "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);
1167 1168 1169 1170
	}
	return ndlp->nlp_state;
}

已提交
1171 1172 1173
/* Start of Discovery State Machine routines */

static uint32_t
J
James Smart 已提交
1174 1175
lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1176 1177 1178 1179 1180
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1181
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1182
		return ndlp->nlp_state;
已提交
1183
	}
1184
	return NLP_STE_FREED_NODE;
已提交
1185 1186 1187
}

static uint32_t
J
James Smart 已提交
1188 1189
lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1190
{
J
James Smart 已提交
1191
	lpfc_issue_els_logo(vport, ndlp, 0);
1192
	return ndlp->nlp_state;
已提交
1193 1194 1195
}

static uint32_t
J
James Smart 已提交
1196 1197
lpfc_rcv_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1198
{
J
James Smart 已提交
1199 1200
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1201

J
James Smart 已提交
1202
	spin_lock_irq(shost->host_lock);
已提交
1203
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
1204
	spin_unlock_irq(shost->host_lock);
1205
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
1206

1207
	return ndlp->nlp_state;
已提交
1208 1209 1210
}

static uint32_t
J
James Smart 已提交
1211 1212
lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1213
{
1214
	return NLP_STE_FREED_NODE;
已提交
1215 1216 1217
}

static uint32_t
J
James Smart 已提交
1218 1219
lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1220
{
1221
	return NLP_STE_FREED_NODE;
已提交
1222 1223
}

1224 1225 1226 1227 1228 1229 1230 1231
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;
}

已提交
1232
static uint32_t
J
James Smart 已提交
1233
lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1234 1235
			   void *arg, uint32_t evt)
{
1236
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
1237
	struct lpfc_hba   *phba = vport->phba;
已提交
1238
	struct lpfc_iocbq *cmdiocb = arg;
J
James Smart 已提交
1239 1240 1241
	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);
已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250
	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 已提交
1251
	port_cmp = memcmp(&vport->fc_portname, &sp->portName,
1252
			  sizeof(struct lpfc_name));
已提交
1253 1254 1255 1256 1257 1258

	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 已提交
1259 1260
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
1261
	} else {
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
		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 已提交
1278
	} /* If our portname was less */
已提交
1279

1280 1281 1282
	return ndlp->nlp_state;
}

1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
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 已提交
1293
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1294 1295 1296
	return ndlp->nlp_state;
}

1297
static uint32_t
J
James Smart 已提交
1298 1299
lpfc_rcv_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
1300
{
J
James Smart 已提交
1301
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1302

1303 1304 1305
	/* 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;
1306
				/* software abort outstanding PLOGI */
J
James Smart 已提交
1307
	lpfc_els_abort(vport->phba, ndlp);
1308

J
James Smart 已提交
1309
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1310
	return ndlp->nlp_state;
已提交
1311 1312 1313
}

static uint32_t
J
James Smart 已提交
1314 1315
lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1316
{
J
James Smart 已提交
1317 1318 1319
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba   *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1320 1321

	/* software abort outstanding PLOGI */
1322
	lpfc_els_abort(phba, ndlp);
已提交
1323 1324

	if (evt == NLP_EVT_RCV_LOGO) {
1325
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1326
	} else {
J
James Smart 已提交
1327
		lpfc_issue_els_logo(vport, ndlp, 0);
已提交
1328 1329
	}

J
James Smart 已提交
1330
	/* Put ndlp in npr state set plogi timer for 1 sec */
1331
	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
1332
	spin_lock_irq(shost->host_lock);
1333
	ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1334
	spin_unlock_irq(shost->host_lock);
1335 1336
	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
J
James Smart 已提交
1337
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
1338

1339
	return ndlp->nlp_state;
已提交
1340 1341 1342
}

static uint32_t
J
James Smart 已提交
1343 1344 1345
lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg,
已提交
1346 1347
			    uint32_t evt)
{
J
James Smart 已提交
1348 1349
	struct lpfc_hba    *phba = vport->phba;
	struct lpfc_iocbq  *cmdiocb, *rspiocb;
1350
	struct lpfc_dmabuf *pcmd, *prsp, *mp;
已提交
1351
	uint32_t *lp;
1352
	uint32_t vid, flag;
已提交
1353 1354
	IOCB_t *irsp;
	struct serv_parm *sp;
1355
	uint32_t ed_tov;
已提交
1356
	LPFC_MBOXQ_t *mbox;
1357
	int rc;
已提交
1358 1359 1360 1361 1362

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

	if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
1363
		/* Recovery from PLOGI collision logic */
1364
		return ndlp->nlp_state;
已提交
1365 1366 1367 1368 1369 1370 1371 1372 1373
	}

	irsp = &rspiocb->iocb;

	if (irsp->ulpStatus)
		goto out;

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

J
James Smart 已提交
1374
	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1375 1376
	if (!prsp)
		goto out;
已提交
1377

J
James Smart 已提交
1378
	lp = (uint32_t *) prsp->virt;
已提交
1379
	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
1380 1381 1382 1383 1384

	/* 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)) {
1385
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1386 1387 1388
				 "0142 PLOGI RSP: Invalid WWN.\n");
		goto out;
	}
1389
	if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0))
已提交
1390 1391
		goto out;
	/* PLOGI chkparm OK */
1392 1393 1394 1395
	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);
1396
	if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
已提交
1397
		ndlp->nlp_fcp_info |= CLASS2;
J
James Smart 已提交
1398
	else
已提交
1399
		ndlp->nlp_fcp_info |= CLASS3;
J
James Smart 已提交
1400

已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
	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 已提交
1411
		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
已提交
1412

1413 1414 1415 1416 1417 1418 1419 1420
	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;
		}

1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
		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;
		}

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
		/*
		 * 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 {
1445 1446
			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
			if (!mbox) {
1447 1448
				lpfc_printf_vlog(vport, KERN_ERR,
						 LOG_TRACE_EVENT,
1449 1450 1451 1452 1453 1454 1455 1456
						 "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;
			}

1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
			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;
			}
		}
1467
	}
已提交
1468

J
James Smart 已提交
1469 1470
	lpfc_unreg_rpi(vport, ndlp);

1471 1472
	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!mbox) {
1473
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1474 1475 1476 1477 1478 1479 1480
				 "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;
	}

1481
	if (lpfc_reg_rpi(phba, vport->vpi, irsp->un.elsreq64.remoteID,
1482
			 (uint8_t *) sp, mbox, ndlp->nlp_rpi) == 0) {
1483
		switch (ndlp->nlp_DID) {
已提交
1484
		case NameServer_DID:
1485
			mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
已提交
1486 1487
			break;
		case FDMI_DID:
1488
			mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
已提交
1489 1490
			break;
		default:
1491
			ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
1492
			mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
已提交
1493
		}
1494

1495
		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
1496 1497 1498
		if (!mbox->ctx_ndlp)
			goto out;

J
James Smart 已提交
1499
		mbox->vport = vport;
1500
		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
已提交
1501
		    != MBX_NOT_FINISHED) {
J
James Smart 已提交
1502 1503
			lpfc_nlp_set_state(vport, ndlp,
					   NLP_STE_REG_LOGIN_ISSUE);
1504
			return ndlp->nlp_state;
已提交
1505
		}
1506 1507
		if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1508 1509 1510
		/* decrement node reference count to the failed mbox
		 * command
		 */
1511
		lpfc_nlp_put(ndlp);
1512
		mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
1513 1514
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
已提交
1515
		mempool_free(mbox, phba->mbox_mem_pool);
1516

1517
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1518 1519 1520 1521
				 "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);
已提交
1522 1523
	} else {
		mempool_free(mbox, phba->mbox_mem_pool);
1524

1525
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1526 1527 1528 1529
				 "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);
已提交
1530 1531 1532
	}


1533 1534 1535
out:
	if (ndlp->nlp_DID == NameServer_DID) {
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1536
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1537
				 "0261 Cannot Register NameServer login\n");
1538 1539
	}

1540 1541 1542 1543 1544 1545 1546
	/*
	** 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);
1547
	return NLP_STE_FREED_NODE;
已提交
1548 1549
}

1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
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)
{
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
	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];
1571
		lpfc_release_rpi(phba, vport, ndlp, rpi);
1572
	}
1573 1574 1575
	return ndlp->nlp_state;
}

已提交
1576
static uint32_t
J
James Smart 已提交
1577 1578
lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1579
{
J
James Smart 已提交
1580 1581 1582 1583
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1584
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1585
		spin_unlock_irq(shost->host_lock);
1586
		return ndlp->nlp_state;
J
James Smart 已提交
1587
	} else {
1588
		/* software abort outstanding PLOGI */
J
James Smart 已提交
1589
		lpfc_els_abort(vport->phba, ndlp);
已提交
1590

J
James Smart 已提交
1591
		lpfc_drop_node(vport, ndlp);
1592 1593
		return NLP_STE_FREED_NODE;
	}
已提交
1594 1595 1596
}

static uint32_t
J
James Smart 已提交
1597 1598 1599 1600
lpfc_device_recov_plogi_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
1601
{
J
James Smart 已提交
1602 1603 1604
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

1605 1606 1607 1608 1609 1610
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
1611
	/* software abort outstanding PLOGI */
1612
	lpfc_els_abort(phba, ndlp);
已提交
1613

1614
	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
J
James Smart 已提交
1615
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1616
	spin_lock_irq(shost->host_lock);
1617
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1618
	spin_unlock_irq(shost->host_lock);
已提交
1619

1620
	return ndlp->nlp_state;
已提交
1621 1622 1623
}

static uint32_t
J
James Smart 已提交
1624 1625
lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1626
{
1627
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
1628
	struct lpfc_hba   *phba = vport->phba;
已提交
1629 1630 1631
	struct lpfc_iocbq *cmdiocb;

	/* software abort outstanding ADISC */
1632
	lpfc_els_abort(phba, ndlp);
已提交
1633 1634 1635

	cmdiocb = (struct lpfc_iocbq *) arg;

1636 1637 1638 1639 1640
	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);
1641
			if (vport->num_disc_nodes)
1642 1643 1644 1645
				lpfc_more_adisc(vport);
		}
		return ndlp->nlp_state;
	}
1646
	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1647 1648
	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
已提交
1649

1650
	return ndlp->nlp_state;
已提交
1651 1652 1653
}

static uint32_t
J
James Smart 已提交
1654 1655
lpfc_rcv_prli_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1656
{
J
James Smart 已提交
1657
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1658

1659 1660
	if (lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1661
	return ndlp->nlp_state;
已提交
1662 1663 1664
}

static uint32_t
J
James Smart 已提交
1665 1666
lpfc_rcv_logo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1667
{
J
James Smart 已提交
1668
	struct lpfc_hba *phba = vport->phba;
已提交
1669 1670 1671 1672 1673
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

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

J
James Smart 已提交
1676
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1677
	return ndlp->nlp_state;
已提交
1678 1679 1680
}

static uint32_t
J
James Smart 已提交
1681 1682 1683
lpfc_rcv_padisc_adisc_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1684 1685 1686 1687 1688
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1689
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1690
	return ndlp->nlp_state;
已提交
1691 1692 1693
}

static uint32_t
J
James Smart 已提交
1694 1695
lpfc_rcv_prlo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1696 1697 1698 1699 1700 1701
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

	/* Treat like rcv logo */
J
James Smart 已提交
1702
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1703
	return ndlp->nlp_state;
已提交
1704 1705 1706
}

static uint32_t
J
James Smart 已提交
1707 1708 1709
lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1710
{
J
James Smart 已提交
1711 1712
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba   *phba = vport->phba;
已提交
1713 1714 1715
	struct lpfc_iocbq *cmdiocb, *rspiocb;
	IOCB_t *irsp;
	ADISC *ap;
1716
	int rc;
已提交
1717 1718 1719 1720 1721 1722 1723 1724

	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) ||
1725
	    (!lpfc_check_adisc(vport, ndlp, &ap->nodeName, &ap->portName))) {
已提交
1726
		/* 1 sec timeout */
1727 1728
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000));
J
James Smart 已提交
1729
		spin_lock_irq(shost->host_lock);
已提交
1730
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1731
		spin_unlock_irq(shost->host_lock);
1732
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
已提交
1733

J
James Smart 已提交
1734 1735
		memset(&ndlp->nlp_nodename, 0, sizeof(struct lpfc_name));
		memset(&ndlp->nlp_portname, 0, sizeof(struct lpfc_name));
已提交
1736

1737
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1738 1739
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		lpfc_unreg_rpi(vport, ndlp);
1740
		return ndlp->nlp_state;
已提交
1741
	}
1742

1743
	if (phba->sli_rev == LPFC_SLI_REV4) {
1744
		rc = lpfc_sli4_resume_rpi(ndlp, NULL, NULL);
1745 1746 1747 1748 1749 1750 1751
		if (rc) {
			/* Stay in state and retry. */
			ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
			return ndlp->nlp_state;
		}
	}

1752 1753 1754 1755 1756 1757 1758
	if (ndlp->nlp_type & NLP_FCP_TARGET)
		ndlp->nlp_fc4_type |= NLP_FC4_FCP;

	if (ndlp->nlp_type & NLP_NVME_TARGET)
		ndlp->nlp_fc4_type |= NLP_FC4_NVME;

	if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET)) {
1759
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1760
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1761
	} else {
1762
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1763
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1764
	}
1765

1766
	return ndlp->nlp_state;
已提交
1767 1768 1769
}

static uint32_t
J
James Smart 已提交
1770 1771
lpfc_device_rm_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1772
{
J
James Smart 已提交
1773 1774 1775 1776
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1777
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1778
		spin_unlock_irq(shost->host_lock);
1779
		return ndlp->nlp_state;
J
James Smart 已提交
1780
	} else {
1781
		/* software abort outstanding ADISC */
J
James Smart 已提交
1782
		lpfc_els_abort(vport->phba, ndlp);
已提交
1783

J
James Smart 已提交
1784
		lpfc_drop_node(vport, ndlp);
1785 1786
		return NLP_STE_FREED_NODE;
	}
已提交
1787 1788 1789
}

static uint32_t
J
James Smart 已提交
1790 1791 1792 1793
lpfc_device_recov_adisc_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
1794
{
J
James Smart 已提交
1795 1796 1797
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

1798 1799 1800 1801 1802 1803
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
1804
	/* software abort outstanding ADISC */
1805
	lpfc_els_abort(phba, ndlp);
已提交
1806

1807
	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1808 1809
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
1810
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1811
	spin_unlock_irq(shost->host_lock);
1812
	lpfc_disc_set_adisc(vport, ndlp);
1813
	return ndlp->nlp_state;
已提交
1814 1815 1816
}

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

J
James Smart 已提交
1824
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1825
	return ndlp->nlp_state;
已提交
1826 1827 1828
}

static uint32_t
J
James Smart 已提交
1829 1830 1831
lpfc_rcv_prli_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1832 1833
			     uint32_t evt)
{
J
James Smart 已提交
1834
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1835
	struct ls_rjt     stat;
已提交
1836

1837 1838 1839
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb)) {
		return ndlp->nlp_state;
	}
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
	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));
1854 1855
			stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
			stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1856 1857
			lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
					    ndlp, NULL);
1858
			return ndlp->nlp_state;
1859 1860 1861 1862 1863
		}
	} else {
		/* Initiator mode. */
		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
	}
1864
	return ndlp->nlp_state;
已提交
1865 1866 1867
}

static uint32_t
J
James Smart 已提交
1868 1869 1870
lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1871 1872
			     uint32_t evt)
{
J
James Smart 已提交
1873 1874
	struct lpfc_hba   *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1875 1876 1877
	LPFC_MBOXQ_t	  *mb;
	LPFC_MBOXQ_t	  *nextmb;
	struct lpfc_dmabuf *mp;
1878
	struct lpfc_nodelist *ns_ndlp;
已提交
1879 1880 1881

	cmdiocb = (struct lpfc_iocbq *) arg;

1882 1883
	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
	if ((mb = phba->sli.mbox_active)) {
1884
		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1885
		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
1886
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1887
			lpfc_nlp_put(ndlp);
1888
			mb->ctx_ndlp = NULL;
1889 1890 1891 1892
			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
		}
	}

J
James Smart 已提交
1893
	spin_lock_irq(&phba->hbalock);
1894
	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1895
		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1896 1897
		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
			mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
1898
			if (mp) {
1899
				__lpfc_mbuf_free(phba, mp->virt, mp->phys);
1900 1901
				kfree(mp);
			}
1902
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1903
			lpfc_nlp_put(ndlp);
1904
			list_del(&mb->list);
1905
			phba->sli.mboxq_cnt--;
1906 1907 1908
			mempool_free(mb, phba->mbox_mem_pool);
		}
	}
J
James Smart 已提交
1909
	spin_unlock_irq(&phba->hbalock);
1910

1911 1912 1913
	/* 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);
1914
		if (ns_ndlp)
1915 1916 1917
			lpfc_els_abort(phba, ns_ndlp);
	}

J
James Smart 已提交
1918
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1919
	return ndlp->nlp_state;
已提交
1920 1921 1922
}

static uint32_t
J
James Smart 已提交
1923 1924 1925
lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport *vport,
			       struct lpfc_nodelist *ndlp,
			       void *arg,
已提交
1926 1927
			       uint32_t evt)
{
J
James Smart 已提交
1928
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1929

J
James Smart 已提交
1930
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1931
	return ndlp->nlp_state;
已提交
1932 1933 1934
}

static uint32_t
J
James Smart 已提交
1935 1936 1937
lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1938 1939 1940 1941 1942
			     uint32_t evt)
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;
1943
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1944
	return ndlp->nlp_state;
已提交
1945 1946 1947
}

static uint32_t
J
James Smart 已提交
1948 1949 1950 1951
lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
				  struct lpfc_nodelist *ndlp,
				  void *arg,
				  uint32_t evt)
已提交
1952
{
J
James Smart 已提交
1953
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1954
	struct lpfc_hba *phba = vport->phba;
J
James Smart 已提交
1955
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1956
	MAILBOX_t *mb = &pmb->u.mb;
J
James Smart 已提交
1957
	uint32_t did  = mb->un.varWords[1];
已提交
1958 1959 1960

	if (mb->mbxStatus) {
		/* RegLogin failed */
1961 1962
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
				 "0246 RegLogin failed Data: x%x x%x x%x x%x "
1963 1964 1965 1966
				 "x%x\n",
				 did, mb->mbxStatus, vport->port_state,
				 mb->un.varRegLogin.vpi,
				 mb->un.varRegLogin.rpi);
1967 1968 1969 1970 1971
		/*
		 * If RegLogin failed due to lack of HBA resources do not
		 * retry discovery.
		 */
		if (mb->mbxStatus == MBXERR_RPI_FULL) {
1972 1973
			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1974 1975 1976
			return ndlp->nlp_state;
		}

J
James Smart 已提交
1977
		/* Put ndlp in npr state set plogi timer for 1 sec */
1978 1979
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
1980
		spin_lock_irq(shost->host_lock);
已提交
1981
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1982
		spin_unlock_irq(shost->host_lock);
1983
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
已提交
1984

J
James Smart 已提交
1985
		lpfc_issue_els_logo(vport, ndlp, 0);
1986
		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1987
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1988
		return ndlp->nlp_state;
已提交
1989 1990
	}

1991
	/* SLI4 ports have preallocated logical rpis. */
1992
	if (phba->sli_rev < LPFC_SLI_REV4)
1993 1994
		ndlp->nlp_rpi = mb->un.varWords[0];

1995
	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
已提交
1996 1997

	/* Only if we are not a fabric nport do we issue PRLI */
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
	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;
2014 2015
			if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
2016 2017
				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
				/* We need to update the localport also */
2018
				lpfc_nvme_update_localport(vport);
2019 2020
			}

2021 2022 2023
		} else if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
			ndlp->nlp_fc4_type |= NLP_FC4_FCP;

2024
		} else if (ndlp->nlp_fc4_type == 0) {
2025 2026 2027 2028
			/* 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.
			 */
2029
			if (vport->cfg_enable_fc4_type != LPFC_ENABLE_FCP) {
2030 2031
				lpfc_ns_cmd(vport, SLI_CTNS_GFT_ID, 0,
					    ndlp->nlp_DID);
2032 2033 2034
				return ndlp->nlp_state;
			}
			ndlp->nlp_fc4_type = NLP_FC4_FCP;
2035 2036
		}

2037
		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
2038
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2039 2040 2041 2042 2043
		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);
		}
已提交
2044
	} else {
2045 2046 2047 2048 2049 2050
		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.
		 */
2051 2052 2053 2054
		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);
		}
已提交
2055
	}
2056
	return ndlp->nlp_state;
已提交
2057 2058 2059
}

static uint32_t
J
James Smart 已提交
2060 2061 2062
lpfc_device_rm_reglogin_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
已提交
2063 2064
			      uint32_t evt)
{
J
James Smart 已提交
2065 2066 2067 2068
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
2069
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
2070
		spin_unlock_irq(shost->host_lock);
2071
		return ndlp->nlp_state;
J
James Smart 已提交
2072 2073
	} else {
		lpfc_drop_node(vport, ndlp);
2074 2075
		return NLP_STE_FREED_NODE;
	}
已提交
2076 2077 2078
}

static uint32_t
J
James Smart 已提交
2079 2080 2081 2082
lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
				 struct lpfc_nodelist *ndlp,
				 void *arg,
				 uint32_t evt)
已提交
2083
{
J
James Smart 已提交
2084 2085
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2086 2087 2088 2089 2090 2091
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

2092
	ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
2093 2094
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2095

2096 2097 2098 2099 2100 2101 2102
	/* 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;

2103
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
2104
	spin_unlock_irq(shost->host_lock);
2105
	lpfc_disc_set_adisc(vport, ndlp);
2106
	return ndlp->nlp_state;
已提交
2107 2108 2109
}

static uint32_t
J
James Smart 已提交
2110 2111
lpfc_rcv_plogi_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2112 2113 2114 2115 2116
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
2117
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2118
	return ndlp->nlp_state;
已提交
2119 2120 2121
}

static uint32_t
J
James Smart 已提交
2122 2123
lpfc_rcv_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2124
{
J
James Smart 已提交
2125
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2126

2127 2128
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;
J
James Smart 已提交
2129
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2130
	return ndlp->nlp_state;
已提交
2131 2132 2133
}

static uint32_t
J
James Smart 已提交
2134 2135
lpfc_rcv_logo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2136
{
J
James Smart 已提交
2137
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2138 2139

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

J
James Smart 已提交
2142
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2143
	return ndlp->nlp_state;
已提交
2144 2145 2146
}

static uint32_t
J
James Smart 已提交
2147 2148
lpfc_rcv_padisc_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2149
{
J
James Smart 已提交
2150
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2151

J
James Smart 已提交
2152
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2153
	return ndlp->nlp_state;
已提交
2154 2155 2156 2157 2158 2159 2160 2161
}

/* 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 已提交
2162 2163
lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2164
{
J
James Smart 已提交
2165
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2166

2167
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2168
	return ndlp->nlp_state;
已提交
2169 2170 2171
}

static uint32_t
J
James Smart 已提交
2172 2173
lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2174
{
2175
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
2176
	struct lpfc_iocbq *cmdiocb, *rspiocb;
J
James Smart 已提交
2177
	struct lpfc_hba   *phba = vport->phba;
已提交
2178 2179
	IOCB_t *irsp;
	PRLI *npr;
2180 2181
	struct lpfc_nvme_prli *nvpr;
	void *temp_ptr;
已提交
2182 2183 2184

	cmdiocb = (struct lpfc_iocbq *) arg;
	rspiocb = cmdiocb->context_un.rsp_iocb;
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196

	/* 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;
已提交
2197 2198 2199

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus) {
J
James Smart 已提交
2200
		if ((vport->port_type == LPFC_NPIV_PORT) &&
2201
		    vport->cfg_restrict_login) {
J
James Smart 已提交
2202 2203
			goto out;
		}
2204

2205 2206
		/* Adjust the nlp_type accordingly if the PRLI failed */
		if (npr)
2207
			ndlp->nlp_fc4_type &= ~NLP_FC4_FCP;
2208 2209
		if (nvpr)
			ndlp->nlp_fc4_type &= ~NLP_FC4_NVME;
2210

2211 2212
		/* We can't set the DSM state till BOTH PRLIs complete */
		goto out_err;
已提交
2213 2214
	}

2215
	if (npr && (npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
已提交
2216
	    (npr->prliType == PRLI_FCP_TYPE)) {
2217 2218 2219 2220
		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
				 "6028 FCP NPR PRLI Cmpl Init %d Target %d\n",
				 npr->initiatorFunc,
				 npr->targetFunc);
已提交
2221 2222
		if (npr->initiatorFunc)
			ndlp->nlp_type |= NLP_FCP_INITIATOR;
2223
		if (npr->targetFunc) {
已提交
2224
			ndlp->nlp_type |= NLP_FCP_TARGET;
2225 2226 2227
			if (npr->writeXferRdyDis)
				ndlp->nlp_flag |= NLP_FIRSTBURST;
		}
已提交
2228 2229
		if (npr->Retry)
			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240

	} 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;

2241 2242 2243
		if (phba->nsler && bf_get_be32(prli_nsler, nvpr) &&
		    bf_get_be32(prli_conf, nvpr))

2244 2245 2246 2247
			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;
		else
			ndlp->nlp_nvme_info &= ~NLP_NVME_NSLER;

2248 2249
		/* Target driver cannot solicit NVME FB. */
		if (bf_get_be32(prli_tgt, nvpr)) {
2250 2251 2252 2253
			/* Complete the nvme target roles.  The transport
			 * needs to know if the rport is capable of
			 * discovery in addition to its role.
			 */
2254
			ndlp->nlp_type |= NLP_NVME_TARGET;
2255 2256
			if (bf_get_be32(prli_disc, nvpr))
				ndlp->nlp_type |= NLP_NVME_DISCOVERY;
2257 2258 2259 2260 2261 2262 2263

			/*
			 * 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.
			 */
2264 2265 2266 2267 2268 2269 2270 2271 2272
			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);
2273 2274 2275 2276 2277 2278 2279

				/* 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;
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291
			}
		}

		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);
已提交
2292
	}
2293 2294
	if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
	    (vport->port_type == LPFC_NPIV_PORT) &&
2295
	     vport->cfg_restrict_login) {
J
James Smart 已提交
2296
out:
2297 2298 2299 2300 2301 2302
		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;
2303
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2304 2305
		return ndlp->nlp_state;
	}
已提交
2306

2307 2308 2309 2310 2311 2312 2313 2314
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);
2315 2316
		else if (ndlp->nlp_type &
			 (NLP_FCP_INITIATOR | NLP_NVME_INITIATOR))
2317 2318 2319 2320 2321 2322 2323 2324 2325
			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);

2326
	return ndlp->nlp_state;
已提交
2327 2328 2329
}

/*! lpfc_device_rm_prli_issue
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
 *
 * \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.
 *
 */

已提交
2347
static uint32_t
J
James Smart 已提交
2348 2349
lpfc_device_rm_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2350
{
J
James Smart 已提交
2351 2352 2353 2354
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
2355
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
2356
		spin_unlock_irq(shost->host_lock);
2357
		return ndlp->nlp_state;
J
James Smart 已提交
2358
	} else {
2359
		/* software abort outstanding PLOGI */
J
James Smart 已提交
2360
		lpfc_els_abort(vport->phba, ndlp);
已提交
2361

J
James Smart 已提交
2362
		lpfc_drop_node(vport, ndlp);
2363 2364
		return NLP_STE_FREED_NODE;
	}
已提交
2365 2366 2367 2368
}


/*! lpfc_device_recov_prli_issue
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383
 *
 * \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.
 */
已提交
2384
static uint32_t
J
James Smart 已提交
2385 2386 2387 2388
lpfc_device_recov_prli_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
			     uint32_t evt)
已提交
2389
{
J
James Smart 已提交
2390 2391 2392
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

2393 2394 2395 2396 2397 2398
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
2399
	/* software abort outstanding PRLI */
2400
	lpfc_els_abort(phba, ndlp);
已提交
2401

2402
	ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
J
James Smart 已提交
2403 2404
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2405
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
2406
	spin_unlock_irq(shost->host_lock);
2407
	lpfc_disc_set_adisc(vport, ndlp);
2408
	return ndlp->nlp_state;
已提交
2409 2410
}

2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446
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);
2447
	ndlp->nlp_flag |= NLP_LOGO_ACC;
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
	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)
{
	/*
2501 2502
	 * DevLoss has timed out and is calling for Device Remove.
	 * In this case, abort the LOGO and cleanup the ndlp
2503
	 */
2504 2505 2506 2507 2508 2509

	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;
2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
}

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;
}

已提交
2525
static uint32_t
J
James Smart 已提交
2526 2527
lpfc_rcv_plogi_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2528
{
J
James Smart 已提交
2529
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2530

J
James Smart 已提交
2531
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2532
	return ndlp->nlp_state;
已提交
2533 2534 2535
}

static uint32_t
J
James Smart 已提交
2536 2537
lpfc_rcv_prli_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2538
{
J
James Smart 已提交
2539
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2540

2541 2542 2543
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;

J
James Smart 已提交
2544 2545
	lpfc_rcv_prli(vport, ndlp, cmdiocb);
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2546
	return ndlp->nlp_state;
已提交
2547 2548 2549
}

static uint32_t
J
James Smart 已提交
2550 2551
lpfc_rcv_logo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2552
{
J
James Smart 已提交
2553
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2554

J
James Smart 已提交
2555
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2556
	return ndlp->nlp_state;
已提交
2557 2558 2559
}

static uint32_t
J
James Smart 已提交
2560 2561
lpfc_rcv_padisc_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2562
{
J
James Smart 已提交
2563
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2564

J
James Smart 已提交
2565
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2566
	return ndlp->nlp_state;
已提交
2567 2568 2569
}

static uint32_t
J
James Smart 已提交
2570 2571
lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2572
{
J
James Smart 已提交
2573
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2574

2575
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2576
	return ndlp->nlp_state;
已提交
2577 2578 2579
}

static uint32_t
J
James Smart 已提交
2580 2581 2582 2583
lpfc_device_recov_unmap_node(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
			     uint32_t evt)
已提交
2584
{
J
James Smart 已提交
2585 2586
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2587
	ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
J
James Smart 已提交
2588 2589
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2590
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2591
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2592 2593
	spin_unlock_irq(shost->host_lock);
	lpfc_disc_set_adisc(vport, ndlp);
已提交
2594

2595
	return ndlp->nlp_state;
已提交
2596 2597 2598
}

static uint32_t
J
James Smart 已提交
2599 2600
lpfc_rcv_plogi_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2601
{
J
James Smart 已提交
2602
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2603

J
James Smart 已提交
2604
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2605
	return ndlp->nlp_state;
已提交
2606 2607 2608
}

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

2614 2615
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;
J
James Smart 已提交
2616
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2617
	return ndlp->nlp_state;
已提交
2618 2619 2620
}

static uint32_t
J
James Smart 已提交
2621 2622
lpfc_rcv_logo_mapped_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_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2627
	return ndlp->nlp_state;
已提交
2628 2629 2630
}

static uint32_t
J
James Smart 已提交
2631 2632 2633
lpfc_rcv_padisc_mapped_node(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
2634
{
J
James Smart 已提交
2635
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2636

J
James Smart 已提交
2637
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2638
	return ndlp->nlp_state;
已提交
2639 2640 2641
}

static uint32_t
J
James Smart 已提交
2642 2643
lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2644
{
J
James Smart 已提交
2645 2646
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2647 2648

	/* flush the target */
2649
	lpfc_sli_abort_iocb(vport, &phba->sli.sli3_ring[LPFC_FCP_RING],
2650
			    ndlp->nlp_sid, 0, LPFC_CTX_TGT);
已提交
2651 2652

	/* Treat like rcv logo */
J
James Smart 已提交
2653
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
2654
	return ndlp->nlp_state;
已提交
2655 2656 2657
}

static uint32_t
J
James Smart 已提交
2658 2659 2660 2661
lpfc_device_recov_mapped_node(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
2662
{
J
James Smart 已提交
2663 2664
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2665
	ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
J
James Smart 已提交
2666 2667
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2668
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2669
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2670 2671
	spin_unlock_irq(shost->host_lock);
	lpfc_disc_set_adisc(vport, ndlp);
2672
	return ndlp->nlp_state;
已提交
2673 2674 2675
}

static uint32_t
J
James Smart 已提交
2676 2677
lpfc_rcv_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2678
{
J
James Smart 已提交
2679 2680
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb  = (struct lpfc_iocbq *) arg;
已提交
2681 2682

	/* Ignore PLOGI if we have an outstanding LOGO */
2683
	if (ndlp->nlp_flag & (NLP_LOGO_SND | NLP_LOGO_ACC))
2684
		return ndlp->nlp_state;
J
James Smart 已提交
2685
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
2686
		lpfc_cancel_retry_delay_tmo(vport, ndlp);
J
James Smart 已提交
2687
		spin_lock_irq(shost->host_lock);
2688
		ndlp->nlp_flag &= ~(NLP_NPR_ADISC | NLP_NPR_2B_DISC);
J
James Smart 已提交
2689
		spin_unlock_irq(shost->host_lock);
2690 2691 2692 2693 2694 2695 2696
	} 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);
		}
已提交
2697
	}
2698
	return ndlp->nlp_state;
已提交
2699 2700 2701
}

static uint32_t
J
James Smart 已提交
2702 2703
lpfc_rcv_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		       void *arg, uint32_t evt)
已提交
2704
{
J
James Smart 已提交
2705 2706 2707
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
	struct ls_rjt     stat;
已提交
2708 2709 2710 2711

	memset(&stat, 0, sizeof (struct ls_rjt));
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
J
James Smart 已提交
2712
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
已提交
2713 2714 2715

	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
J
James Smart 已提交
2716
			spin_lock_irq(shost->host_lock);
2717
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2718
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2719 2720 2721
			spin_unlock_irq(shost->host_lock);
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
已提交
2722
		} else {
2723
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2724 2725
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
2726 2727
		}
	}
2728
	return ndlp->nlp_state;
已提交
2729 2730 2731
}

static uint32_t
J
James Smart 已提交
2732 2733
lpfc_rcv_logo_npr_node(struct lpfc_vport *vport,  struct lpfc_nodelist *ndlp,
		       void *arg, uint32_t evt)
已提交
2734
{
J
James Smart 已提交
2735
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2736

J
James Smart 已提交
2737
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2738
	return ndlp->nlp_state;
已提交
2739 2740 2741
}

static uint32_t
J
James Smart 已提交
2742 2743
lpfc_rcv_padisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2744
{
J
James Smart 已提交
2745
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2746

J
James Smart 已提交
2747
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
J
James Smart 已提交
2748 2749 2750 2751 2752
	/*
	 * 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 已提交
2753
	if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
2754
	    !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
已提交
2755
		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
2756
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2757
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2758 2759
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
已提交
2760
		} else {
2761
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2762 2763
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
2764 2765
		}
	}
2766
	return ndlp->nlp_state;
已提交
2767 2768 2769
}

static uint32_t
J
James Smart 已提交
2770 2771
lpfc_rcv_prlo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		       void *arg, uint32_t evt)
已提交
2772
{
J
James Smart 已提交
2773 2774
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2775

J
James Smart 已提交
2776
	spin_lock_irq(shost->host_lock);
2777
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
2778
	spin_unlock_irq(shost->host_lock);
2779

2780
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
2781

J
James Smart 已提交
2782
	if ((ndlp->nlp_flag & NLP_DELAY_TMO) == 0) {
2783 2784
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
2785
		spin_lock_irq(shost->host_lock);
2786 2787
		ndlp->nlp_flag |= NLP_DELAY_TMO;
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
2788
		spin_unlock_irq(shost->host_lock);
2789
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
2790
	} else {
J
James Smart 已提交
2791
		spin_lock_irq(shost->host_lock);
2792
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
2793
		spin_unlock_irq(shost->host_lock);
已提交
2794
	}
2795 2796
	return ndlp->nlp_state;
}
已提交
2797

2798
static uint32_t
J
James Smart 已提交
2799 2800
lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
2801 2802
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2803
	IOCB_t *irsp;
2804 2805 2806

	cmdiocb = (struct lpfc_iocbq *) arg;
	rspiocb = cmdiocb->context_un.rsp_iocb;
2807 2808 2809 2810 2811

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus) {
		return NLP_STE_FREED_NODE;
	}
2812 2813 2814 2815
	return ndlp->nlp_state;
}

static uint32_t
J
James Smart 已提交
2816 2817
lpfc_cmpl_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
2818 2819
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2820
	IOCB_t *irsp;
2821 2822 2823

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

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
J
James Smart 已提交
2827
		lpfc_drop_node(vport, ndlp);
2828 2829
		return NLP_STE_FREED_NODE;
	}
2830
	return ndlp->nlp_state;
已提交
2831 2832 2833
}

static uint32_t
J
James Smart 已提交
2834 2835
lpfc_cmpl_logo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2836
{
2837
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2838 2839

	/* For the fabric port just clear the fc flags. */
2840 2841 2842 2843 2844
	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 已提交
2845
	lpfc_unreg_rpi(vport, ndlp);
2846 2847 2848 2849
	return ndlp->nlp_state;
}

static uint32_t
J
James Smart 已提交
2850 2851
lpfc_cmpl_adisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
2852 2853
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2854
	IOCB_t *irsp;
2855 2856 2857

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

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
J
James Smart 已提交
2861
		lpfc_drop_node(vport, ndlp);
2862 2863
		return NLP_STE_FREED_NODE;
	}
2864
	return ndlp->nlp_state;
已提交
2865 2866 2867
}

static uint32_t
J
James Smart 已提交
2868 2869 2870
lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
2871
{
J
James Smart 已提交
2872
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
2873
	MAILBOX_t    *mb = &pmb->u.mb;
已提交
2874

2875
	if (!mb->mbxStatus) {
2876 2877 2878
		/* SLI4 ports have preallocated logical rpis. */
		if (vport->phba->sli_rev < LPFC_SLI_REV4)
			ndlp->nlp_rpi = mb->un.varWords[0];
2879
		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
2880 2881 2882
		if (ndlp->nlp_flag & NLP_LOGO_ACC) {
			lpfc_unreg_rpi(vport, ndlp);
		}
2883
	} else {
2884
		if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
J
James Smart 已提交
2885
			lpfc_drop_node(vport, ndlp);
2886 2887 2888
			return NLP_STE_FREED_NODE;
		}
	}
2889
	return ndlp->nlp_state;
已提交
2890 2891 2892
}

static uint32_t
J
James Smart 已提交
2893 2894
lpfc_device_rm_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2895
{
J
James Smart 已提交
2896 2897
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2898
	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
J
James Smart 已提交
2899
		spin_lock_irq(shost->host_lock);
2900
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
2901
		spin_unlock_irq(shost->host_lock);
2902 2903
		return ndlp->nlp_state;
	}
J
James Smart 已提交
2904
	lpfc_drop_node(vport, ndlp);
2905
	return NLP_STE_FREED_NODE;
已提交
2906 2907 2908
}

static uint32_t
J
James Smart 已提交
2909 2910
lpfc_device_recov_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2911
{
J
James Smart 已提交
2912 2913
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2914 2915 2916 2917 2918 2919
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

2920
	lpfc_cancel_retry_delay_tmo(vport, ndlp);
J
James Smart 已提交
2921
	spin_lock_irq(shost->host_lock);
2922
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2923
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2924
	spin_unlock_irq(shost->host_lock);
2925
	return ndlp->nlp_state;
已提交
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960
}


/* 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
2961
 * lists will receive a DEVICE_RECOVERY event. If the linkdown or devloss timers
已提交
2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985
 * 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 已提交
2986
     (struct lpfc_vport *, struct lpfc_nodelist *, void *, uint32_t) = {
已提交
2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999
	/* 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       */
3000
	lpfc_device_recov_unused_node,	/* DEVICE_RECOVERY */
已提交
3001 3002

	lpfc_rcv_plogi_plogi_issue,	/* RCV_PLOGI   PLOGI_ISSUE    */
3003
	lpfc_rcv_prli_plogi_issue,	/* RCV_PRLI        */
3004
	lpfc_rcv_logo_plogi_issue,	/* RCV_LOGO        */
已提交
3005 3006 3007 3008 3009
	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       */
3010
	lpfc_cmpl_logo_plogi_issue,	/* CMPL_LOGO       */
已提交
3011
	lpfc_disc_illegal,		/* CMPL_ADISC      */
3012
	lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
已提交
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035
	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        */
3036
	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
已提交
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049
	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        */
3050
	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
已提交
3051 3052 3053 3054 3055 3056 3057
	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 */

3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071
	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 */

已提交
3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105
	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        */
3106 3107
	lpfc_cmpl_plogi_npr_node,	/* CMPL_PLOGI      */
	lpfc_cmpl_prli_npr_node,	/* CMPL_PRLI       */
已提交
3108
	lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
3109
	lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
已提交
3110 3111 3112 3113 3114 3115
	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 已提交
3116 3117
lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
3118 3119
{
	uint32_t cur_state, rc;
J
James Smart 已提交
3120
	uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
已提交
3121
			 uint32_t);
3122
	uint32_t got_ndlp = 0;
3123
	uint32_t data1;
3124 3125 3126

	if (lpfc_nlp_get(ndlp))
		got_ndlp = 1;
已提交
3127 3128 3129

	cur_state = ndlp->nlp_state;

3130 3131
	data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
		((uint32_t)ndlp->nlp_type));
已提交
3132
	/* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
3133 3134
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0211 DSM in event x%x on NPort x%x in "
3135 3136
			 "state %d rpi x%x Data: x%x x%x\n",
			 evt, ndlp->nlp_DID, cur_state, ndlp->nlp_rpi,
3137
			 ndlp->nlp_flag, data1);
已提交
3138

J
James Smart 已提交
3139 3140 3141 3142
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
		 "DSM in:          evt:%d ste:%d did:x%x",
		evt, cur_state, ndlp->nlp_DID);

已提交
3143
	func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
J
James Smart 已提交
3144
	rc = (func) (vport, ndlp, arg, evt);
已提交
3145 3146

	/* DSM out state <rc> on NPort <nlp_DID> */
3147
	if (got_ndlp) {
3148 3149
		data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
			((uint32_t)ndlp->nlp_type));
3150
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3151
			 "0212 DSM out state %d on NPort x%x "
3152 3153 3154
			 "rpi x%x Data: x%x x%x\n",
			 rc, ndlp->nlp_DID, ndlp->nlp_rpi, ndlp->nlp_flag,
			 data1);
已提交
3155

3156 3157 3158 3159 3160 3161 3162
		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,
3163
			"0213 DSM out state %d on NPort free\n", rc);
J
James Smart 已提交
3164

3165 3166 3167 3168
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
			"DSM out:         ste:%d did:x%x flg:x%x",
			rc, 0, 0);
	}
已提交
3169

3170
	return rc;
已提交
3171
}