lpfc_nportdisc.c 93.5 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 156 157 158 159 160 161 162 163
	lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
			 "0207 Device %x "
			 "(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x) sent "
			 "invalid service parameters.  Ignoring device.\n",
			 ndlp->nlp_DID,
			 sp->nodeName.u.wwn[0], sp->nodeName.u.wwn[1],
			 sp->nodeName.u.wwn[2], sp->nodeName.u.wwn[3],
			 sp->nodeName.u.wwn[4], sp->nodeName.u.wwn[5],
			 sp->nodeName.u.wwn[6], sp->nodeName.u.wwn[7]);
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 304 305 306 307 308
lpfc_defer_pt2pt_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *link_mbox)
{
	LPFC_MBOXQ_t *login_mbox;
	MAILBOX_t *mb = &link_mbox->u.mb;
	struct lpfc_iocbq *save_iocb;
	struct lpfc_nodelist *ndlp;
	int rc;

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

	/* Check for CONFIG_LINK error */
	if (mb->mbxStatus) {
		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
				"4575 CONFIG_LINK fails pt2pt discovery: %x\n",
				mb->mbxStatus);
		mempool_free(login_mbox, phba->mbox_mem_pool);
		mempool_free(link_mbox, phba->mbox_mem_pool);
309
		kfree(save_iocb);
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
		return;
	}

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

	mempool_free(link_mbox, phba->mbox_mem_pool);
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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
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_nlp_get(ndlp);
	lpfc_sli4_unreg_rpi_cmpl_clr(phba, pmb);

	if (!piocb) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY | LOG_ELS,
				 "4578 PLOGI ACC fail\n");
		if (mbox)
			mempool_free(mbox, phba->mbox_mem_pool);
		goto out;
	}

	rc = lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, piocb, ndlp, mbox);
	if (rc) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY | LOG_ELS,
				 "4579 PLOGI ACC fail %x\n", rc);
		if (mbox)
			mempool_free(mbox, phba->mbox_mem_pool);
	}
	kfree(piocb);
out:
	lpfc_nlp_put(ndlp);
381 382
}

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

	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));
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	if (wwn_to_u64(sp->portName.u.wwn) == 0) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0140 PLOGI Reject: invalid nname\n");
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_PNAME;
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
		return 0;
	}
	if (wwn_to_u64(sp->nodeName.u.wwn) == 0) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0141 PLOGI Reject: invalid pname\n");
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_NNAME;
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
		return 0;
	}
425 426

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

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

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

450
	defer_acc = 0;
已提交
451 452 453 454 455 456 457 458 459 460 461
	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;
462
	/* if already logged in, do implicit logout */
463
	switch (ndlp->nlp_state) {
已提交
464 465 466
	case  NLP_STE_NPR_NODE:
		if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
			break;
467
		/* fall through */
已提交
468 469 470 471
	case  NLP_STE_REG_LOGIN_ISSUE:
	case  NLP_STE_PRLI_ISSUE:
	case  NLP_STE_UNMAPPED_NODE:
	case  NLP_STE_MAPPED_NODE:
472 473 474 475 476 477
		/* 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)) {
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
			lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
					 ndlp, NULL);
			return 1;
		}
		if (nlp_portwwn != 0 &&
		    nlp_portwwn != wwn_to_u64(sp->portName.u.wwn))
			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
					 "0143 PLOGI recv'd from DID: x%x "
					 "WWPN changed: old %llx new %llx\n",
					 ndlp->nlp_DID,
					 (unsigned long long)nlp_portwwn,
					 (unsigned long long)
					 wwn_to_u64(sp->portName.u.wwn));

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

498 499 500 501 502
	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;

503 504 505 506
	login_mbox = NULL;
	link_mbox = NULL;
	save_iocb = NULL;

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

		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;
已提交
517
		}
518

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

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

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

J
James Smart 已提交
552
		lpfc_can_disctmo(vport);
已提交
553
	}
554

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

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

568
	/* Registering an existing RPI behaves differently for SLI3 vs SLI4 */
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	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;
		link_mbox->ctx_ndlp = ndlp;
		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)
599 600
		lpfc_unreg_rpi(vport, ndlp);

601
	rc = lpfc_reg_rpi(phba, vport->vpi, icmd->un.rcvels.remoteID,
602 603
			    (uint8_t *)sp, login_mbox, ndlp->nlp_rpi);
	if (rc)
已提交
604 605 606
		goto out;

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

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

J
James Smart 已提交
635
	if ((vport->port_type == LPFC_NPIV_PORT &&
636
	     vport->cfg_restrict_login)) {
J
James Smart 已提交
637

638 639 640
		/* no deferred ACC */
		kfree(save_iocb);

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

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

已提交
696 697
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
J
James Smart 已提交
698
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
699
	return 0;
已提交
700 701
}

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
/**
 * 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;

718 719
	elsiocb = (struct lpfc_iocbq *)mboxq->ctx_buf;
	ndlp = (struct lpfc_nodelist *)mboxq->ctx_ndlp;
720 721 722 723 724 725 726 727 728 729
	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);
730
	mempool_free(mboxq, phba->mbox_mem_pool);
731 732
}

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

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

已提交
788
		if (cmd == ELS_CMD_ADISC) {
J
James Smart 已提交
789
			lpfc_els_rsp_adisc_acc(vport, cmdiocb, ndlp);
790
		} else {
791 792
			lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
				ndlp, NULL);
已提交
793
		}
794 795
out:
		/* If we are authenticated, move to the proper state */
796
		if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET))
797 798 799 800
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
		else
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);

801
		return 1;
已提交
802 803 804 805 806 807
	}
	/* 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 已提交
808
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
已提交
809 810

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

J
James Smart 已提交
813
	spin_lock_irq(shost->host_lock);
已提交
814
	ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
815
	spin_unlock_irq(shost->host_lock);
816 817
	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
	ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
818
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
819
	return 0;
已提交
820 821 822
}

static int
J
James Smart 已提交
823 824
lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
	      struct lpfc_iocbq *cmdiocb, uint32_t els_cmd)
已提交
825
{
J
James Smart 已提交
826
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
827 828 829
	struct lpfc_hba    *phba = vport->phba;
	struct lpfc_vport **vports;
	int i, active_vlink_present = 0 ;
J
James Smart 已提交
830 831

	/* Put ndlp in NPR state with 1 sec timeout for plogi, ACC logo */
已提交
832 833 834
	/* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
	 * PLOGIs during LOGO storms from a device.
	 */
J
James Smart 已提交
835
	spin_lock_irq(shost->host_lock);
已提交
836
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
837
	spin_unlock_irq(shost->host_lock);
838
	if (els_cmd == ELS_CMD_PRLO)
839
		lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
840
	else
841
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
J
James Smart 已提交
842 843 844
	if (ndlp->nlp_DID == Fabric_DID) {
		if (vport->port_state <= LPFC_FDISC)
			goto out;
845 846
		lpfc_linkdown_port(vport);
		spin_lock_irq(shost->host_lock);
J
James Smart 已提交
847
		vport->fc_flag |= FC_VPORT_LOGO_RCVD;
848
		spin_unlock_irq(shost->host_lock);
J
James Smart 已提交
849 850 851 852 853 854 855 856 857 858 859 860 861
		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);
		}
已提交
862

J
James Smart 已提交
863 864 865 866 867 868 869
		/*
		 * 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 已提交
870 871 872 873
			/*
			 * If there are other active VLinks present,
			 * re-instantiate the Vlink using FDISC.
			 */
874 875
			mod_timer(&ndlp->nlp_delayfunc,
				  jiffies + msecs_to_jiffies(1000));
J
James Smart 已提交
876 877 878 879 880 881 882 883 884 885 886
			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);
		}
887 888 889 890
	} 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)) {
已提交
891
		/* Only try to re-login if this is NOT a Fabric Node */
892 893
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
894
		spin_lock_irq(shost->host_lock);
已提交
895
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
896
		spin_unlock_irq(shost->host_lock);
已提交
897

898
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
899
	}
J
James Smart 已提交
900
out:
901 902
	ndlp->nlp_prev_state = ndlp->nlp_state;
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
903

J
James Smart 已提交
904
	spin_lock_irq(shost->host_lock);
已提交
905
	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
906
	spin_unlock_irq(shost->host_lock);
已提交
907 908 909 910 911
	/* 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.
	 */
912
	return 0;
已提交
913 914
}

915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
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;
}

已提交
950
static void
J
James Smart 已提交
951 952
lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
	      struct lpfc_iocbq *cmdiocb)
已提交
953
{
954
	struct lpfc_hba  *phba = vport->phba;
已提交
955 956 957 958 959 960 961 962 963 964
	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));

965 966 967 968 969 970 971 972
	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;
		}
973
		if (npr->targetFunc) {
974 975 976 977
			if (npr->prliType == PRLI_FCP_TYPE)
				ndlp->nlp_type |= NLP_FCP_TARGET;
			if (npr->prliType == PRLI_NVME_TYPE)
				ndlp->nlp_type |= NLP_NVME_TARGET;
978 979 980
			if (npr->writeXferRdyDis)
				ndlp->nlp_flag |= NLP_FIRSTBURST;
		}
981 982
		if (npr->Retry && ndlp->nlp_type &
					(NLP_FCP_INITIATOR | NLP_FCP_TARGET))
已提交
983
			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
984

985 986 987 988 989
		if (npr->Retry && phba->nsler &&
		    ndlp->nlp_type & (NLP_NVME_INITIATOR | NLP_NVME_TARGET))
			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;


990 991 992 993 994
		/* 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.
		 */
995
		if (phba->nvmet_support && (npr->prliType == PRLI_NVME_TYPE)) {
996
			ndlp->nlp_fc4_type |= NLP_FC4_NVME;
997 998 999 1000
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
		}
		if (npr->prliType == PRLI_FCP_TYPE)
			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
已提交
1001 1002 1003 1004 1005 1006 1007 1008
	}
	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 已提交
1009 1010 1011 1012 1013

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

1014
		if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1015
			fc_remote_port_rolechg(rport, roles);
已提交
1016 1017 1018 1019
	}
}

static uint32_t
J
James Smart 已提交
1020
lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
已提交
1021
{
J
James Smart 已提交
1022 1023
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

1024
	if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
1025
		spin_lock_irq(shost->host_lock);
1026
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1027
		spin_unlock_irq(shost->host_lock);
1028 1029 1030
		return 0;
	}

1031 1032
	if (!(vport->fc_flag & FC_PT2PT)) {
		/* Check config parameter use-adisc or FCP-2 */
1033
		if (vport->cfg_use_adisc && ((vport->fc_flag & FC_RSCN_MODE) ||
1034
		    ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) &&
1035
		     (ndlp->nlp_type & NLP_FCP_TARGET)))) {
1036 1037 1038 1039 1040
			spin_lock_irq(shost->host_lock);
			ndlp->nlp_flag |= NLP_NPR_ADISC;
			spin_unlock_irq(shost->host_lock);
			return 1;
		}
1041
	}
1042 1043

	spin_lock_irq(shost->host_lock);
1044
	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1045
	spin_unlock_irq(shost->host_lock);
1046 1047
	lpfc_unreg_rpi(vport, ndlp);
	return 0;
已提交
1048
}
1049

1050
/**
L
Lucas De Marchi 已提交
1051
 * lpfc_release_rpi - Release a RPI by issuing unreg_login mailbox cmd.
1052 1053 1054 1055 1056 1057 1058
 * @phba : Pointer to lpfc_hba structure.
 * @vport: Pointer to lpfc_vport structure.
 * @rpi  : rpi to be release.
 *
 * This function will send a unreg_login mailbox command to the firmware
 * to release a rpi.
 **/
1059
static void
1060 1061
lpfc_release_rpi(struct lpfc_hba *phba, struct lpfc_vport *vport,
		 struct lpfc_nodelist *ndlp, uint16_t rpi)
1062 1063 1064 1065
{
	LPFC_MBOXQ_t *pmb;
	int rc;

1066 1067 1068 1069 1070 1071 1072
	/* 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 "
1073
				 "Data: x%px\n",
1074 1075 1076 1077 1078 1079
				 ndlp->nlp_rpi, ndlp->nlp_DID,
				 ndlp->nlp_defer_did,
				 ndlp->nlp_flag, ndlp);
		return;
	}

1080 1081 1082 1083 1084 1085 1086 1087
	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
			GFP_KERNEL);
	if (!pmb)
		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
			"2796 mailbox memory allocation failed \n");
	else {
		lpfc_unreg_login(phba, vport->vpi, rpi, pmb);
		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
		pmb->vport = vport;
		pmb->ctx_ndlp = ndlp;

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

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

1100 1101 1102 1103 1104
		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
		if (rc == MBX_NOT_FINISHED)
			mempool_free(pmb, phba->mbox_mem_pool);
	}
}
已提交
1105 1106

static uint32_t
J
James Smart 已提交
1107 1108
lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		  void *arg, uint32_t evt)
已提交
1109
{
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
	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];
1120
		lpfc_release_rpi(phba, vport, ndlp, rpi);
1121
	}
1122
	lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1123
			 "0271 Illegal State Transition: node x%x "
1124 1125 1126
			 "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);
1127
	return ndlp->nlp_state;
已提交
1128 1129
}

1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
static uint32_t
lpfc_cmpl_plogi_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		  void *arg, uint32_t evt)
{
	/* This transition is only legal if we previously
	 * rcv'ed a PLOGI. Since we don't want 2 discovery threads
	 * working on the same NPortID, do nothing for this thread
	 * to stop it.
	 */
	if (!(ndlp->nlp_flag & NLP_RCV_PLOGI)) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1141
			 "0272 Illegal State Transition: node x%x "
1142 1143 1144 1145 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);
	}
	return ndlp->nlp_state;
}

已提交
1149 1150 1151
/* Start of Discovery State Machine routines */

static uint32_t
J
James Smart 已提交
1152 1153
lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1154 1155 1156 1157 1158
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1159
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1160
		return ndlp->nlp_state;
已提交
1161
	}
1162
	return NLP_STE_FREED_NODE;
已提交
1163 1164 1165
}

static uint32_t
J
James Smart 已提交
1166 1167
lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1168
{
J
James Smart 已提交
1169
	lpfc_issue_els_logo(vport, ndlp, 0);
1170
	return ndlp->nlp_state;
已提交
1171 1172 1173
}

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

J
James Smart 已提交
1180
	spin_lock_irq(shost->host_lock);
已提交
1181
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
1182
	spin_unlock_irq(shost->host_lock);
1183
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
1184

1185
	return ndlp->nlp_state;
已提交
1186 1187 1188
}

static uint32_t
J
James Smart 已提交
1189 1190
lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1191
{
1192
	return NLP_STE_FREED_NODE;
已提交
1193 1194 1195
}

static uint32_t
J
James Smart 已提交
1196 1197
lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1198
{
1199
	return NLP_STE_FREED_NODE;
已提交
1200 1201
}

1202 1203 1204 1205 1206 1207 1208 1209
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;
}

已提交
1210
static uint32_t
J
James Smart 已提交
1211
lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1212 1213
			   void *arg, uint32_t evt)
{
1214
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
1215
	struct lpfc_hba   *phba = vport->phba;
已提交
1216
	struct lpfc_iocbq *cmdiocb = arg;
J
James Smart 已提交
1217 1218 1219
	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);
已提交
1220 1221 1222 1223 1224 1225 1226 1227 1228
	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 已提交
1229
	port_cmp = memcmp(&vport->fc_portname, &sp->portName,
1230
			  sizeof(struct lpfc_name));
已提交
1231 1232 1233 1234 1235 1236

	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 已提交
1237 1238
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
1239
	} else {
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
		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 已提交
1256
	} /* If our portname was less */
已提交
1257

1258 1259 1260
	return ndlp->nlp_state;
}

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
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 已提交
1271
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1272 1273 1274
	return ndlp->nlp_state;
}

1275
static uint32_t
J
James Smart 已提交
1276 1277
lpfc_rcv_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
1278
{
J
James Smart 已提交
1279
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1280

1281 1282 1283
	/* 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;
1284
				/* software abort outstanding PLOGI */
J
James Smart 已提交
1285
	lpfc_els_abort(vport->phba, ndlp);
1286

J
James Smart 已提交
1287
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1288
	return ndlp->nlp_state;
已提交
1289 1290 1291
}

static uint32_t
J
James Smart 已提交
1292 1293
lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1294
{
J
James Smart 已提交
1295 1296 1297
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba   *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1298 1299

	/* software abort outstanding PLOGI */
1300
	lpfc_els_abort(phba, ndlp);
已提交
1301 1302

	if (evt == NLP_EVT_RCV_LOGO) {
1303
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1304
	} else {
J
James Smart 已提交
1305
		lpfc_issue_els_logo(vport, ndlp, 0);
已提交
1306 1307
	}

J
James Smart 已提交
1308
	/* Put ndlp in npr state set plogi timer for 1 sec */
1309
	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
1310
	spin_lock_irq(shost->host_lock);
1311
	ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1312
	spin_unlock_irq(shost->host_lock);
1313 1314
	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
J
James Smart 已提交
1315
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
1316

1317
	return ndlp->nlp_state;
已提交
1318 1319 1320
}

static uint32_t
J
James Smart 已提交
1321 1322 1323
lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg,
已提交
1324 1325
			    uint32_t evt)
{
J
James Smart 已提交
1326
	struct lpfc_hba    *phba = vport->phba;
1327
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
1328
	struct lpfc_iocbq  *cmdiocb, *rspiocb;
1329
	struct lpfc_dmabuf *pcmd, *prsp, *mp;
已提交
1330
	uint32_t *lp;
1331
	uint32_t vid, flag;
已提交
1332 1333
	IOCB_t *irsp;
	struct serv_parm *sp;
1334
	uint32_t ed_tov;
已提交
1335
	LPFC_MBOXQ_t *mbox;
1336
	int rc;
已提交
1337 1338 1339 1340 1341

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

	if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
1342
		/* Recovery from PLOGI collision logic */
1343
		return ndlp->nlp_state;
已提交
1344 1345 1346 1347 1348 1349 1350 1351 1352
	}

	irsp = &rspiocb->iocb;

	if (irsp->ulpStatus)
		goto out;

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

J
James Smart 已提交
1353
	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1354 1355
	if (!prsp)
		goto out;
已提交
1356

J
James Smart 已提交
1357
	lp = (uint32_t *) prsp->virt;
已提交
1358
	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
1359 1360 1361 1362 1363

	/* 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)) {
1364 1365 1366 1367
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0142 PLOGI RSP: Invalid WWN.\n");
		goto out;
	}
1368
	if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0))
已提交
1369 1370
		goto out;
	/* PLOGI chkparm OK */
1371 1372 1373 1374
	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);
1375
	if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
已提交
1376
		ndlp->nlp_fcp_info |= CLASS2;
J
James Smart 已提交
1377
	else
已提交
1378
		ndlp->nlp_fcp_info |= CLASS3;
J
James Smart 已提交
1379

已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
	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 已提交
1390
		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
已提交
1391

1392 1393 1394 1395 1396 1397 1398 1399
	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;
		}

1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
		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;
		}

1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
		/*
		 * 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 {
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
			if (!mbox) {
				lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
						 "0133 PLOGI: no memory "
						 "for config_link "
						 "Data: x%x x%x x%x x%x\n",
						 ndlp->nlp_DID, ndlp->nlp_state,
						 ndlp->nlp_flag, ndlp->nlp_rpi);
				goto out;
			}

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
			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;
			}
		}
1445
	}
已提交
1446

J
James Smart 已提交
1447 1448
	lpfc_unreg_rpi(vport, ndlp);

1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!mbox) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0018 PLOGI: no memory for reg_login "
				 "Data: x%x x%x x%x x%x\n",
				 ndlp->nlp_DID, ndlp->nlp_state,
				 ndlp->nlp_flag, ndlp->nlp_rpi);
		goto out;
	}

1459
	if (lpfc_reg_rpi(phba, vport->vpi, irsp->un.elsreq64.remoteID,
1460
			 (uint8_t *) sp, mbox, ndlp->nlp_rpi) == 0) {
1461
		switch (ndlp->nlp_DID) {
已提交
1462
		case NameServer_DID:
1463
			mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
已提交
1464 1465
			break;
		case FDMI_DID:
1466
			mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
已提交
1467 1468
			break;
		default:
1469
			ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
1470
			mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
已提交
1471
		}
1472
		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
J
James Smart 已提交
1473
		mbox->vport = vport;
1474
		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
已提交
1475
		    != MBX_NOT_FINISHED) {
J
James Smart 已提交
1476 1477
			lpfc_nlp_set_state(vport, ndlp,
					   NLP_STE_REG_LOGIN_ISSUE);
1478
			return ndlp->nlp_state;
已提交
1479
		}
1480 1481
		if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1482 1483 1484
		/* decrement node reference count to the failed mbox
		 * command
		 */
1485
		lpfc_nlp_put(ndlp);
1486
		mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
1487 1488
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
已提交
1489
		mempool_free(mbox, phba->mbox_mem_pool);
1490

1491 1492 1493 1494 1495
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0134 PLOGI: cannot issue reg_login "
				 "Data: x%x x%x x%x x%x\n",
				 ndlp->nlp_DID, ndlp->nlp_state,
				 ndlp->nlp_flag, ndlp->nlp_rpi);
已提交
1496 1497
	} else {
		mempool_free(mbox, phba->mbox_mem_pool);
1498

1499 1500 1501 1502 1503
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0135 PLOGI: cannot format reg_login "
				 "Data: x%x x%x x%x x%x\n",
				 ndlp->nlp_DID, ndlp->nlp_state,
				 ndlp->nlp_flag, ndlp->nlp_rpi);
已提交
1504 1505 1506
	}


1507 1508 1509
out:
	if (ndlp->nlp_DID == NameServer_DID) {
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1510 1511
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0261 Cannot Register NameServer login\n");
1512 1513
	}

1514 1515 1516 1517 1518 1519 1520
	/*
	** 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);
1521
	spin_lock_irq(shost->host_lock);
1522
	ndlp->nlp_flag |= NLP_DEFER_RM;
1523
	spin_unlock_irq(shost->host_lock);
1524
	return NLP_STE_FREED_NODE;
已提交
1525 1526
}

1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
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)
{
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
	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];
1548
		lpfc_release_rpi(phba, vport, ndlp, rpi);
1549
	}
1550 1551 1552
	return ndlp->nlp_state;
}

已提交
1553
static uint32_t
J
James Smart 已提交
1554 1555
lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1556
{
J
James Smart 已提交
1557 1558 1559 1560
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1561
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1562
		spin_unlock_irq(shost->host_lock);
1563
		return ndlp->nlp_state;
J
James Smart 已提交
1564
	} else {
1565
		/* software abort outstanding PLOGI */
J
James Smart 已提交
1566
		lpfc_els_abort(vport->phba, ndlp);
已提交
1567

J
James Smart 已提交
1568
		lpfc_drop_node(vport, ndlp);
1569 1570
		return NLP_STE_FREED_NODE;
	}
已提交
1571 1572 1573
}

static uint32_t
J
James Smart 已提交
1574 1575 1576 1577
lpfc_device_recov_plogi_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
1578
{
J
James Smart 已提交
1579 1580 1581
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

1582 1583 1584 1585 1586 1587
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
1588
	/* software abort outstanding PLOGI */
1589
	lpfc_els_abort(phba, ndlp);
已提交
1590

1591
	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
J
James Smart 已提交
1592
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1593
	spin_lock_irq(shost->host_lock);
1594
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1595
	spin_unlock_irq(shost->host_lock);
已提交
1596

1597
	return ndlp->nlp_state;
已提交
1598 1599 1600
}

static uint32_t
J
James Smart 已提交
1601 1602
lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1603
{
1604
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
1605
	struct lpfc_hba   *phba = vport->phba;
已提交
1606 1607 1608
	struct lpfc_iocbq *cmdiocb;

	/* software abort outstanding ADISC */
1609
	lpfc_els_abort(phba, ndlp);
已提交
1610 1611 1612

	cmdiocb = (struct lpfc_iocbq *) arg;

1613 1614 1615 1616 1617
	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);
1618
			if (vport->num_disc_nodes)
1619 1620 1621 1622
				lpfc_more_adisc(vport);
		}
		return ndlp->nlp_state;
	}
1623
	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1624 1625
	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
已提交
1626

1627
	return ndlp->nlp_state;
已提交
1628 1629 1630
}

static uint32_t
J
James Smart 已提交
1631 1632
lpfc_rcv_prli_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1633
{
J
James Smart 已提交
1634
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1635

1636 1637
	if (lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1638
	return ndlp->nlp_state;
已提交
1639 1640 1641
}

static uint32_t
J
James Smart 已提交
1642 1643
lpfc_rcv_logo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1644
{
J
James Smart 已提交
1645
	struct lpfc_hba *phba = vport->phba;
已提交
1646 1647 1648 1649 1650
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

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

J
James Smart 已提交
1653
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1654
	return ndlp->nlp_state;
已提交
1655 1656 1657
}

static uint32_t
J
James Smart 已提交
1658 1659 1660
lpfc_rcv_padisc_adisc_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1661 1662 1663 1664 1665
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1666
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1667
	return ndlp->nlp_state;
已提交
1668 1669 1670
}

static uint32_t
J
James Smart 已提交
1671 1672
lpfc_rcv_prlo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1673 1674 1675 1676 1677 1678
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

	/* Treat like rcv logo */
J
James Smart 已提交
1679
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1680
	return ndlp->nlp_state;
已提交
1681 1682 1683
}

static uint32_t
J
James Smart 已提交
1684 1685 1686
lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1687
{
J
James Smart 已提交
1688 1689
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba   *phba = vport->phba;
已提交
1690 1691 1692
	struct lpfc_iocbq *cmdiocb, *rspiocb;
	IOCB_t *irsp;
	ADISC *ap;
1693
	int rc;
已提交
1694 1695 1696 1697 1698 1699 1700 1701

	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) ||
1702
	    (!lpfc_check_adisc(vport, ndlp, &ap->nodeName, &ap->portName))) {
已提交
1703
		/* 1 sec timeout */
1704 1705
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000));
J
James Smart 已提交
1706
		spin_lock_irq(shost->host_lock);
已提交
1707
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1708
		spin_unlock_irq(shost->host_lock);
1709
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
已提交
1710

J
James Smart 已提交
1711 1712
		memset(&ndlp->nlp_nodename, 0, sizeof(struct lpfc_name));
		memset(&ndlp->nlp_portname, 0, sizeof(struct lpfc_name));
已提交
1713

1714
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1715 1716
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		lpfc_unreg_rpi(vport, ndlp);
1717
		return ndlp->nlp_state;
已提交
1718
	}
1719

1720
	if (phba->sli_rev == LPFC_SLI_REV4) {
1721
		rc = lpfc_sli4_resume_rpi(ndlp, NULL, NULL);
1722 1723 1724 1725 1726 1727 1728
		if (rc) {
			/* Stay in state and retry. */
			ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
			return ndlp->nlp_state;
		}
	}

1729
	if (ndlp->nlp_type & NLP_FCP_TARGET) {
1730
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1731
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1732
	} else {
1733
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1734
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1735
	}
1736

1737
	return ndlp->nlp_state;
已提交
1738 1739 1740
}

static uint32_t
J
James Smart 已提交
1741 1742
lpfc_device_rm_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1743
{
J
James Smart 已提交
1744 1745 1746 1747
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1748
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1749
		spin_unlock_irq(shost->host_lock);
1750
		return ndlp->nlp_state;
J
James Smart 已提交
1751
	} else {
1752
		/* software abort outstanding ADISC */
J
James Smart 已提交
1753
		lpfc_els_abort(vport->phba, ndlp);
已提交
1754

J
James Smart 已提交
1755
		lpfc_drop_node(vport, ndlp);
1756 1757
		return NLP_STE_FREED_NODE;
	}
已提交
1758 1759 1760
}

static uint32_t
J
James Smart 已提交
1761 1762 1763 1764
lpfc_device_recov_adisc_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
1765
{
J
James Smart 已提交
1766 1767 1768
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

1769 1770 1771 1772 1773 1774
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
1775
	/* software abort outstanding ADISC */
1776
	lpfc_els_abort(phba, ndlp);
已提交
1777

1778
	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1779 1780
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
1781
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1782
	spin_unlock_irq(shost->host_lock);
1783
	lpfc_disc_set_adisc(vport, ndlp);
1784
	return ndlp->nlp_state;
已提交
1785 1786 1787
}

static uint32_t
J
James Smart 已提交
1788 1789 1790
lpfc_rcv_plogi_reglogin_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
已提交
1791 1792
			      uint32_t evt)
{
J
James Smart 已提交
1793
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1794

J
James Smart 已提交
1795
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1796
	return ndlp->nlp_state;
已提交
1797 1798 1799
}

static uint32_t
J
James Smart 已提交
1800 1801 1802
lpfc_rcv_prli_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1803 1804
			     uint32_t evt)
{
J
James Smart 已提交
1805
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1806
	struct ls_rjt     stat;
已提交
1807

1808 1809 1810
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb)) {
		return ndlp->nlp_state;
	}
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
	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));
1825 1826
			stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
			stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1827 1828
			lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
					    ndlp, NULL);
1829
			return ndlp->nlp_state;
1830 1831 1832 1833 1834
		}
	} else {
		/* Initiator mode. */
		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
	}
1835
	return ndlp->nlp_state;
已提交
1836 1837 1838
}

static uint32_t
J
James Smart 已提交
1839 1840 1841
lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1842 1843
			     uint32_t evt)
{
J
James Smart 已提交
1844 1845
	struct lpfc_hba   *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1846 1847 1848
	LPFC_MBOXQ_t	  *mb;
	LPFC_MBOXQ_t	  *nextmb;
	struct lpfc_dmabuf *mp;
1849
	struct lpfc_nodelist *ns_ndlp;
已提交
1850 1851 1852

	cmdiocb = (struct lpfc_iocbq *) arg;

1853 1854
	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
	if ((mb = phba->sli.mbox_active)) {
1855
		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1856
		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
1857
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1858
			lpfc_nlp_put(ndlp);
1859
			mb->ctx_ndlp = NULL;
1860 1861 1862 1863
			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
		}
	}

J
James Smart 已提交
1864
	spin_lock_irq(&phba->hbalock);
1865
	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1866
		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1867 1868
		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
			mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
1869
			if (mp) {
1870
				__lpfc_mbuf_free(phba, mp->virt, mp->phys);
1871 1872
				kfree(mp);
			}
1873
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1874
			lpfc_nlp_put(ndlp);
1875
			list_del(&mb->list);
1876
			phba->sli.mboxq_cnt--;
1877 1878 1879
			mempool_free(mb, phba->mbox_mem_pool);
		}
	}
J
James Smart 已提交
1880
	spin_unlock_irq(&phba->hbalock);
1881

1882 1883 1884 1885 1886 1887 1888
	/* software abort if any GID_FT is outstanding */
	if (vport->cfg_enable_fc4_type != LPFC_ENABLE_FCP) {
		ns_ndlp = lpfc_findnode_did(vport, NameServer_DID);
		if (ns_ndlp && NLP_CHK_NODE_ACT(ns_ndlp))
			lpfc_els_abort(phba, ns_ndlp);
	}

J
James Smart 已提交
1889
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1890
	return ndlp->nlp_state;
已提交
1891 1892 1893
}

static uint32_t
J
James Smart 已提交
1894 1895 1896
lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport *vport,
			       struct lpfc_nodelist *ndlp,
			       void *arg,
已提交
1897 1898
			       uint32_t evt)
{
J
James Smart 已提交
1899
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1900

J
James Smart 已提交
1901
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1902
	return ndlp->nlp_state;
已提交
1903 1904 1905
}

static uint32_t
J
James Smart 已提交
1906 1907 1908
lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1909 1910 1911 1912 1913
			     uint32_t evt)
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;
1914
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1915
	return ndlp->nlp_state;
已提交
1916 1917 1918
}

static uint32_t
J
James Smart 已提交
1919 1920 1921 1922
lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
				  struct lpfc_nodelist *ndlp,
				  void *arg,
				  uint32_t evt)
已提交
1923
{
J
James Smart 已提交
1924
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1925
	struct lpfc_hba *phba = vport->phba;
J
James Smart 已提交
1926
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1927
	MAILBOX_t *mb = &pmb->u.mb;
J
James Smart 已提交
1928
	uint32_t did  = mb->un.varWords[1];
已提交
1929 1930 1931

	if (mb->mbxStatus) {
		/* RegLogin failed */
1932
		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1933 1934 1935 1936 1937
				"0246 RegLogin failed Data: x%x x%x x%x x%x "
				 "x%x\n",
				 did, mb->mbxStatus, vport->port_state,
				 mb->un.varRegLogin.vpi,
				 mb->un.varRegLogin.rpi);
1938 1939 1940 1941 1942
		/*
		 * If RegLogin failed due to lack of HBA resources do not
		 * retry discovery.
		 */
		if (mb->mbxStatus == MBXERR_RPI_FULL) {
1943 1944
			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1945 1946 1947
			return ndlp->nlp_state;
		}

J
James Smart 已提交
1948
		/* Put ndlp in npr state set plogi timer for 1 sec */
1949 1950
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
1951
		spin_lock_irq(shost->host_lock);
已提交
1952
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1953
		spin_unlock_irq(shost->host_lock);
1954
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
已提交
1955

J
James Smart 已提交
1956
		lpfc_issue_els_logo(vport, ndlp, 0);
1957
		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1958
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1959
		return ndlp->nlp_state;
已提交
1960 1961
	}

1962
	/* SLI4 ports have preallocated logical rpis. */
1963
	if (phba->sli_rev < LPFC_SLI_REV4)
1964 1965
		ndlp->nlp_rpi = mb->un.varWords[0];

1966
	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
已提交
1967 1968

	/* Only if we are not a fabric nport do we issue PRLI */
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
	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;
1985 1986
			if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
1987 1988
				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
				/* We need to update the localport also */
1989
				lpfc_nvme_update_localport(vport);
1990 1991
			}

1992 1993 1994
		} else if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
			ndlp->nlp_fc4_type |= NLP_FC4_FCP;

1995
		} else if (ndlp->nlp_fc4_type == 0) {
1996 1997 1998 1999
			/* 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.
			 */
2000
			if (vport->cfg_enable_fc4_type != LPFC_ENABLE_FCP) {
2001 2002
				lpfc_ns_cmd(vport, SLI_CTNS_GFT_ID, 0,
					    ndlp->nlp_DID);
2003 2004 2005
				return ndlp->nlp_state;
			}
			ndlp->nlp_fc4_type = NLP_FC4_FCP;
2006 2007
		}

2008
		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
2009
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2010 2011 2012 2013 2014
		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);
		}
已提交
2015
	} else {
2016 2017 2018 2019 2020 2021
		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.
		 */
2022 2023 2024 2025
		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);
		}
已提交
2026
	}
2027
	return ndlp->nlp_state;
已提交
2028 2029 2030
}

static uint32_t
J
James Smart 已提交
2031 2032 2033
lpfc_device_rm_reglogin_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
已提交
2034 2035
			      uint32_t evt)
{
J
James Smart 已提交
2036 2037 2038 2039
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
2040
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
2041
		spin_unlock_irq(shost->host_lock);
2042
		return ndlp->nlp_state;
J
James Smart 已提交
2043 2044
	} else {
		lpfc_drop_node(vport, ndlp);
2045 2046
		return NLP_STE_FREED_NODE;
	}
已提交
2047 2048 2049
}

static uint32_t
J
James Smart 已提交
2050 2051 2052 2053
lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
				 struct lpfc_nodelist *ndlp,
				 void *arg,
				 uint32_t evt)
已提交
2054
{
J
James Smart 已提交
2055 2056
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2057 2058 2059 2060 2061 2062
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

2063
	ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
2064 2065
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2066

2067 2068 2069 2070 2071 2072 2073
	/* 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;

2074
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
2075
	spin_unlock_irq(shost->host_lock);
2076
	lpfc_disc_set_adisc(vport, ndlp);
2077
	return ndlp->nlp_state;
已提交
2078 2079 2080
}

static uint32_t
J
James Smart 已提交
2081 2082
lpfc_rcv_plogi_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2083 2084 2085 2086 2087
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
2088
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2089
	return ndlp->nlp_state;
已提交
2090 2091 2092
}

static uint32_t
J
James Smart 已提交
2093 2094
lpfc_rcv_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2095
{
J
James Smart 已提交
2096
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2097

2098 2099
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;
J
James Smart 已提交
2100
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2101
	return ndlp->nlp_state;
已提交
2102 2103 2104
}

static uint32_t
J
James Smart 已提交
2105 2106
lpfc_rcv_logo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2107
{
J
James Smart 已提交
2108
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2109 2110

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

J
James Smart 已提交
2113
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2114
	return ndlp->nlp_state;
已提交
2115 2116 2117
}

static uint32_t
J
James Smart 已提交
2118 2119
lpfc_rcv_padisc_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2120
{
J
James Smart 已提交
2121
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2122

J
James Smart 已提交
2123
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2124
	return ndlp->nlp_state;
已提交
2125 2126 2127 2128 2129 2130 2131 2132
}

/* 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 已提交
2133 2134
lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2135
{
J
James Smart 已提交
2136
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2137

2138
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2139
	return ndlp->nlp_state;
已提交
2140 2141 2142
}

static uint32_t
J
James Smart 已提交
2143 2144
lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2145
{
2146
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
2147
	struct lpfc_iocbq *cmdiocb, *rspiocb;
J
James Smart 已提交
2148
	struct lpfc_hba   *phba = vport->phba;
已提交
2149 2150
	IOCB_t *irsp;
	PRLI *npr;
2151 2152
	struct lpfc_nvme_prli *nvpr;
	void *temp_ptr;
已提交
2153 2154 2155

	cmdiocb = (struct lpfc_iocbq *) arg;
	rspiocb = cmdiocb->context_un.rsp_iocb;
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167

	/* 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;
已提交
2168 2169 2170

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus) {
J
James Smart 已提交
2171
		if ((vport->port_type == LPFC_NPIV_PORT) &&
2172
		    vport->cfg_restrict_login) {
J
James Smart 已提交
2173 2174
			goto out;
		}
2175

2176 2177
		/* Adjust the nlp_type accordingly if the PRLI failed */
		if (npr)
2178
			ndlp->nlp_fc4_type &= ~NLP_FC4_FCP;
2179 2180
		if (nvpr)
			ndlp->nlp_fc4_type &= ~NLP_FC4_NVME;
2181

2182 2183
		/* We can't set the DSM state till BOTH PRLIs complete */
		goto out_err;
已提交
2184 2185
	}

2186
	if (npr && (npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
已提交
2187
	    (npr->prliType == PRLI_FCP_TYPE)) {
2188 2189 2190 2191
		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
				 "6028 FCP NPR PRLI Cmpl Init %d Target %d\n",
				 npr->initiatorFunc,
				 npr->targetFunc);
已提交
2192 2193
		if (npr->initiatorFunc)
			ndlp->nlp_type |= NLP_FCP_INITIATOR;
2194
		if (npr->targetFunc) {
已提交
2195
			ndlp->nlp_type |= NLP_FCP_TARGET;
2196 2197 2198
			if (npr->writeXferRdyDis)
				ndlp->nlp_flag |= NLP_FIRSTBURST;
		}
已提交
2199 2200
		if (npr->Retry)
			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211

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

2212 2213 2214
		if (phba->nsler && bf_get_be32(prli_nsler, nvpr) &&
		    bf_get_be32(prli_conf, nvpr))

2215 2216 2217 2218
			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;
		else
			ndlp->nlp_nvme_info &= ~NLP_NVME_NSLER;

2219 2220
		/* Target driver cannot solicit NVME FB. */
		if (bf_get_be32(prli_tgt, nvpr)) {
2221 2222 2223 2224
			/* Complete the nvme target roles.  The transport
			 * needs to know if the rport is capable of
			 * discovery in addition to its role.
			 */
2225
			ndlp->nlp_type |= NLP_NVME_TARGET;
2226 2227
			if (bf_get_be32(prli_disc, nvpr))
				ndlp->nlp_type |= NLP_NVME_DISCOVERY;
2228 2229 2230 2231 2232 2233 2234

			/*
			 * 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.
			 */
2235 2236 2237 2238 2239 2240 2241 2242 2243
			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);
2244 2245 2246 2247 2248 2249 2250

				/* 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;
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
			}
		}

		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);
已提交
2263
	}
2264 2265
	if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
	    (vport->port_type == LPFC_NPIV_PORT) &&
2266
	     vport->cfg_restrict_login) {
J
James Smart 已提交
2267
out:
2268 2269 2270 2271 2272 2273
		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;
2274
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2275 2276
		return ndlp->nlp_state;
	}
已提交
2277

2278 2279 2280 2281 2282 2283 2284 2285
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);
2286 2287
		else if (ndlp->nlp_type &
			 (NLP_FCP_INITIATOR | NLP_NVME_INITIATOR))
2288 2289 2290 2291 2292 2293 2294 2295 2296
			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);

2297
	return ndlp->nlp_state;
已提交
2298 2299 2300
}

/*! lpfc_device_rm_prli_issue
2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
 *
 * \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.
 *
 */

已提交
2318
static uint32_t
J
James Smart 已提交
2319 2320
lpfc_device_rm_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2321
{
J
James Smart 已提交
2322 2323 2324 2325
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
2326
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
2327
		spin_unlock_irq(shost->host_lock);
2328
		return ndlp->nlp_state;
J
James Smart 已提交
2329
	} else {
2330
		/* software abort outstanding PLOGI */
J
James Smart 已提交
2331
		lpfc_els_abort(vport->phba, ndlp);
已提交
2332

J
James Smart 已提交
2333
		lpfc_drop_node(vport, ndlp);
2334 2335
		return NLP_STE_FREED_NODE;
	}
已提交
2336 2337 2338 2339
}


/*! lpfc_device_recov_prli_issue
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
 *
 * \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.
 */
已提交
2355
static uint32_t
J
James Smart 已提交
2356 2357 2358 2359
lpfc_device_recov_prli_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
			     uint32_t evt)
已提交
2360
{
J
James Smart 已提交
2361 2362 2363
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

2364 2365 2366 2367 2368 2369
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
2370
	/* software abort outstanding PRLI */
2371
	lpfc_els_abort(phba, ndlp);
已提交
2372

2373
	ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
J
James Smart 已提交
2374 2375
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2376
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
2377
	spin_unlock_irq(shost->host_lock);
2378
	lpfc_disc_set_adisc(vport, ndlp);
2379
	return ndlp->nlp_state;
已提交
2380 2381
}

2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
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);
2418
	ndlp->nlp_flag |= NLP_LOGO_ACC;
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 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471
	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)
{
	/*
2472 2473
	 * DevLoss has timed out and is calling for Device Remove.
	 * In this case, abort the LOGO and cleanup the ndlp
2474
	 */
2475 2476 2477 2478 2479 2480

	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;
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
}

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

已提交
2496
static uint32_t
J
James Smart 已提交
2497 2498
lpfc_rcv_plogi_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2499
{
J
James Smart 已提交
2500
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2501

J
James Smart 已提交
2502
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2503
	return ndlp->nlp_state;
已提交
2504 2505 2506
}

static uint32_t
J
James Smart 已提交
2507 2508
lpfc_rcv_prli_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2509
{
J
James Smart 已提交
2510
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2511

2512 2513 2514
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;

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

static uint32_t
J
James Smart 已提交
2521 2522
lpfc_rcv_logo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2523
{
J
James Smart 已提交
2524
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2525

J
James Smart 已提交
2526
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2527
	return ndlp->nlp_state;
已提交
2528 2529 2530
}

static uint32_t
J
James Smart 已提交
2531 2532
lpfc_rcv_padisc_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2533
{
J
James Smart 已提交
2534
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2535

J
James Smart 已提交
2536
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2537
	return ndlp->nlp_state;
已提交
2538 2539 2540
}

static uint32_t
J
James Smart 已提交
2541 2542
lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2543
{
J
James Smart 已提交
2544
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2545

2546
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2547
	return ndlp->nlp_state;
已提交
2548 2549 2550
}

static uint32_t
J
James Smart 已提交
2551 2552 2553 2554
lpfc_device_recov_unmap_node(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
			     uint32_t evt)
已提交
2555
{
J
James Smart 已提交
2556 2557
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2558
	ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
J
James Smart 已提交
2559 2560
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2561
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2562
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2563 2564
	spin_unlock_irq(shost->host_lock);
	lpfc_disc_set_adisc(vport, ndlp);
已提交
2565

2566
	return ndlp->nlp_state;
已提交
2567 2568 2569
}

static uint32_t
J
James Smart 已提交
2570 2571
lpfc_rcv_plogi_mapped_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

J
James Smart 已提交
2575
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2576
	return ndlp->nlp_state;
已提交
2577 2578 2579
}

static uint32_t
J
James Smart 已提交
2580 2581
lpfc_rcv_prli_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2582
{
J
James Smart 已提交
2583
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2584

2585 2586
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;
J
James Smart 已提交
2587
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2588
	return ndlp->nlp_state;
已提交
2589 2590 2591
}

static uint32_t
J
James Smart 已提交
2592 2593
lpfc_rcv_logo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2594
{
J
James Smart 已提交
2595
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2596

J
James Smart 已提交
2597
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2598
	return ndlp->nlp_state;
已提交
2599 2600 2601
}

static uint32_t
J
James Smart 已提交
2602 2603 2604
lpfc_rcv_padisc_mapped_node(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
2605
{
J
James Smart 已提交
2606
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2607

J
James Smart 已提交
2608
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2609
	return ndlp->nlp_state;
已提交
2610 2611 2612
}

static uint32_t
J
James Smart 已提交
2613 2614
lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2615
{
J
James Smart 已提交
2616 2617
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2618 2619

	/* flush the target */
2620
	lpfc_sli_abort_iocb(vport, &phba->sli.sli3_ring[LPFC_FCP_RING],
2621
			    ndlp->nlp_sid, 0, LPFC_CTX_TGT);
已提交
2622 2623

	/* Treat like rcv logo */
J
James Smart 已提交
2624
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
2625
	return ndlp->nlp_state;
已提交
2626 2627 2628
}

static uint32_t
J
James Smart 已提交
2629 2630 2631 2632
lpfc_device_recov_mapped_node(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
2633
{
J
James Smart 已提交
2634 2635
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2636
	ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
J
James Smart 已提交
2637 2638
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2639
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2640
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2641 2642
	spin_unlock_irq(shost->host_lock);
	lpfc_disc_set_adisc(vport, ndlp);
2643
	return ndlp->nlp_state;
已提交
2644 2645 2646
}

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

	/* Ignore PLOGI if we have an outstanding LOGO */
2654
	if (ndlp->nlp_flag & (NLP_LOGO_SND | NLP_LOGO_ACC))
2655
		return ndlp->nlp_state;
J
James Smart 已提交
2656
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
2657
		lpfc_cancel_retry_delay_tmo(vport, ndlp);
J
James Smart 已提交
2658
		spin_lock_irq(shost->host_lock);
2659
		ndlp->nlp_flag &= ~(NLP_NPR_ADISC | NLP_NPR_2B_DISC);
J
James Smart 已提交
2660
		spin_unlock_irq(shost->host_lock);
2661 2662 2663 2664 2665 2666 2667
	} 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);
		}
已提交
2668
	}
2669
	return ndlp->nlp_state;
已提交
2670 2671 2672
}

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

	memset(&stat, 0, sizeof (struct ls_rjt));
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
J
James Smart 已提交
2683
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
已提交
2684 2685 2686

	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
J
James Smart 已提交
2687
			spin_lock_irq(shost->host_lock);
2688
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2689
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2690 2691 2692
			spin_unlock_irq(shost->host_lock);
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
已提交
2693
		} else {
2694
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2695 2696
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
2697 2698
		}
	}
2699
	return ndlp->nlp_state;
已提交
2700 2701 2702
}

static uint32_t
J
James Smart 已提交
2703 2704
lpfc_rcv_logo_npr_node(struct lpfc_vport *vport,  struct lpfc_nodelist *ndlp,
		       void *arg, uint32_t evt)
已提交
2705
{
J
James Smart 已提交
2706
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2707

J
James Smart 已提交
2708
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2709
	return ndlp->nlp_state;
已提交
2710 2711 2712
}

static uint32_t
J
James Smart 已提交
2713 2714
lpfc_rcv_padisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2715
{
J
James Smart 已提交
2716
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2717

J
James Smart 已提交
2718
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
J
James Smart 已提交
2719 2720 2721 2722 2723
	/*
	 * 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 已提交
2724
	if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
2725
	    !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
已提交
2726
		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
2727
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2728
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2729 2730
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
已提交
2731
		} else {
2732
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2733 2734
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
2735 2736
		}
	}
2737
	return ndlp->nlp_state;
已提交
2738 2739 2740
}

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

J
James Smart 已提交
2747
	spin_lock_irq(shost->host_lock);
2748
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
2749
	spin_unlock_irq(shost->host_lock);
2750

2751
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
2752

J
James Smart 已提交
2753
	if ((ndlp->nlp_flag & NLP_DELAY_TMO) == 0) {
2754 2755
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
2756
		spin_lock_irq(shost->host_lock);
2757 2758
		ndlp->nlp_flag |= NLP_DELAY_TMO;
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
2759
		spin_unlock_irq(shost->host_lock);
2760
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
2761
	} else {
J
James Smart 已提交
2762
		spin_lock_irq(shost->host_lock);
2763
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
2764
		spin_unlock_irq(shost->host_lock);
已提交
2765
	}
2766 2767
	return ndlp->nlp_state;
}
已提交
2768

2769
static uint32_t
J
James Smart 已提交
2770 2771
lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
2772 2773
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2774
	IOCB_t *irsp;
2775
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2776 2777 2778

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

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus) {
2782
		spin_lock_irq(shost->host_lock);
2783
		ndlp->nlp_flag |= NLP_DEFER_RM;
2784
		spin_unlock_irq(shost->host_lock);
2785 2786
		return NLP_STE_FREED_NODE;
	}
2787 2788 2789 2790
	return ndlp->nlp_state;
}

static uint32_t
J
James Smart 已提交
2791 2792
lpfc_cmpl_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
2793 2794
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2795
	IOCB_t *irsp;
2796 2797 2798

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

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
J
James Smart 已提交
2802
		lpfc_drop_node(vport, ndlp);
2803 2804
		return NLP_STE_FREED_NODE;
	}
2805
	return ndlp->nlp_state;
已提交
2806 2807 2808
}

static uint32_t
J
James Smart 已提交
2809 2810
lpfc_cmpl_logo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2811
{
2812
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2813 2814

	/* For the fabric port just clear the fc flags. */
2815 2816 2817 2818 2819
	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 已提交
2820
	lpfc_unreg_rpi(vport, ndlp);
2821 2822 2823 2824
	return ndlp->nlp_state;
}

static uint32_t
J
James Smart 已提交
2825 2826
lpfc_cmpl_adisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
2827 2828
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2829
	IOCB_t *irsp;
2830 2831 2832

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

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
J
James Smart 已提交
2836
		lpfc_drop_node(vport, ndlp);
2837 2838
		return NLP_STE_FREED_NODE;
	}
2839
	return ndlp->nlp_state;
已提交
2840 2841 2842
}

static uint32_t
J
James Smart 已提交
2843 2844 2845
lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
2846
{
J
James Smart 已提交
2847
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
2848
	MAILBOX_t    *mb = &pmb->u.mb;
已提交
2849

2850
	if (!mb->mbxStatus) {
2851 2852 2853
		/* SLI4 ports have preallocated logical rpis. */
		if (vport->phba->sli_rev < LPFC_SLI_REV4)
			ndlp->nlp_rpi = mb->un.varWords[0];
2854
		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
2855 2856 2857
		if (ndlp->nlp_flag & NLP_LOGO_ACC) {
			lpfc_unreg_rpi(vport, ndlp);
		}
2858
	} else {
2859
		if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
J
James Smart 已提交
2860
			lpfc_drop_node(vport, ndlp);
2861 2862 2863
			return NLP_STE_FREED_NODE;
		}
	}
2864
	return ndlp->nlp_state;
已提交
2865 2866 2867
}

static uint32_t
J
James Smart 已提交
2868 2869
lpfc_device_rm_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2870
{
J
James Smart 已提交
2871 2872
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2873
	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
J
James Smart 已提交
2874
		spin_lock_irq(shost->host_lock);
2875
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
2876
		spin_unlock_irq(shost->host_lock);
2877 2878
		return ndlp->nlp_state;
	}
J
James Smart 已提交
2879
	lpfc_drop_node(vport, ndlp);
2880
	return NLP_STE_FREED_NODE;
已提交
2881 2882 2883
}

static uint32_t
J
James Smart 已提交
2884 2885
lpfc_device_recov_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2886
{
J
James Smart 已提交
2887 2888
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2889 2890 2891 2892 2893 2894
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

2895
	lpfc_cancel_retry_delay_tmo(vport, ndlp);
J
James Smart 已提交
2896
	spin_lock_irq(shost->host_lock);
2897
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2898
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2899
	spin_unlock_irq(shost->host_lock);
2900
	return ndlp->nlp_state;
已提交
2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935
}


/* 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
2936
 * lists will receive a DEVICE_RECOVERY event. If the linkdown or devloss timers
已提交
2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960
 * 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 已提交
2961
     (struct lpfc_vport *, struct lpfc_nodelist *, void *, uint32_t) = {
已提交
2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974
	/* 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       */
2975
	lpfc_device_recov_unused_node,	/* DEVICE_RECOVERY */
已提交
2976 2977

	lpfc_rcv_plogi_plogi_issue,	/* RCV_PLOGI   PLOGI_ISSUE    */
2978
	lpfc_rcv_prli_plogi_issue,	/* RCV_PRLI        */
2979
	lpfc_rcv_logo_plogi_issue,	/* RCV_LOGO        */
已提交
2980 2981 2982 2983 2984
	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       */
2985
	lpfc_cmpl_logo_plogi_issue,	/* CMPL_LOGO       */
已提交
2986
	lpfc_disc_illegal,		/* CMPL_ADISC      */
2987
	lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
已提交
2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010
	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        */
3011
	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
已提交
3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024
	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        */
3025
	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
已提交
3026 3027 3028 3029 3030 3031 3032
	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 */

3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046
	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 */

已提交
3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
	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        */
3081 3082
	lpfc_cmpl_plogi_npr_node,	/* CMPL_PLOGI      */
	lpfc_cmpl_prli_npr_node,	/* CMPL_PRLI       */
已提交
3083
	lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
3084
	lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
已提交
3085 3086 3087 3088 3089 3090
	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 已提交
3091 3092
lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
3093 3094
{
	uint32_t cur_state, rc;
J
James Smart 已提交
3095
	uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
已提交
3096
			 uint32_t);
3097
	uint32_t got_ndlp = 0;
3098
	uint32_t data1;
3099 3100 3101

	if (lpfc_nlp_get(ndlp))
		got_ndlp = 1;
已提交
3102 3103 3104

	cur_state = ndlp->nlp_state;

3105 3106
	data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
		((uint32_t)ndlp->nlp_type));
已提交
3107
	/* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
3108 3109
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0211 DSM in event x%x on NPort x%x in "
3110 3111
			 "state %d rpi x%x Data: x%x x%x\n",
			 evt, ndlp->nlp_DID, cur_state, ndlp->nlp_rpi,
3112
			 ndlp->nlp_flag, data1);
已提交
3113

J
James Smart 已提交
3114 3115 3116 3117
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
		 "DSM in:          evt:%d ste:%d did:x%x",
		evt, cur_state, ndlp->nlp_DID);

已提交
3118
	func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
J
James Smart 已提交
3119
	rc = (func) (vport, ndlp, arg, evt);
已提交
3120 3121

	/* DSM out state <rc> on NPort <nlp_DID> */
3122
	if (got_ndlp) {
3123 3124
		data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
			((uint32_t)ndlp->nlp_type));
3125
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3126
			 "0212 DSM out state %d on NPort x%x "
3127 3128 3129
			 "rpi x%x Data: x%x x%x\n",
			 rc, ndlp->nlp_DID, ndlp->nlp_rpi, ndlp->nlp_flag,
			 data1);
已提交
3130

3131 3132 3133 3134 3135 3136 3137
		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,
3138
			"0213 DSM out state %d on NPort free\n", rc);
J
James Smart 已提交
3139

3140 3141 3142 3143
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
			"DSM out:         ste:%d did:x%x flg:x%x",
			rc, 0, 0);
	}
已提交
3144

3145
	return rc;
已提交
3146
}