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

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

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


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

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

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

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

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

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

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

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

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

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

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


203

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

216 217
	pring = lpfc_phba_elsring(phba);

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

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

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

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

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

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

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

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

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

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

static int
J
James Smart 已提交
283
lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
284
	       struct lpfc_iocbq *cmdiocb)
已提交
285
{
J
James Smart 已提交
286 287
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba    *phba = vport->phba;
已提交
288
	struct lpfc_dmabuf *pcmd;
289
	uint64_t nlp_portwwn = 0;
已提交
290 291 292
	uint32_t *lp;
	IOCB_t *icmd;
	struct serv_parm *sp;
293
	uint32_t ed_tov;
已提交
294 295
	LPFC_MBOXQ_t *mbox;
	struct ls_rjt stat;
296
	uint32_t vid, flag;
已提交
297 298 299 300 301 302
	int rc;

	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));
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	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;
	}
321 322

	nlp_portwwn = wwn_to_u64(ndlp->nlp_portname.u.wwn);
323
	if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0) == 0)) {
已提交
324 325 326
		/* Reject this request because invalid parameters */
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
J
James Smart 已提交
327 328
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
329
		return 0;
已提交
330 331 332 333
	}
	icmd = &cmdiocb->iocb;

	/* PLOGI chkparm OK */
334
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
335 336
			 "0114 PLOGI chkparm OK Data: x%x x%x x%x "
			 "x%x x%x x%x\n",
337
			 ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
338 339
			 ndlp->nlp_rpi, vport->port_state,
			 vport->fc_flag);
已提交
340

341
	if (vport->cfg_fcp_class == 2 && sp->cls2.classValid)
已提交
342
		ndlp->nlp_fcp_info |= CLASS2;
343
	else
已提交
344
		ndlp->nlp_fcp_info |= CLASS3;
J
James Smart 已提交
345

已提交
346 347 348 349 350 351 352 353 354 355 356 357
	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;

358
	/* if already logged in, do implicit logout */
359
	switch (ndlp->nlp_state) {
已提交
360 361 362
	case  NLP_STE_NPR_NODE:
		if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
			break;
363
		/* fall through */
已提交
364 365 366 367
	case  NLP_STE_REG_LOGIN_ISSUE:
	case  NLP_STE_PRLI_ISSUE:
	case  NLP_STE_UNMAPPED_NODE:
	case  NLP_STE_MAPPED_NODE:
368 369 370 371 372 373
		/* 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)) {
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
			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;
已提交
392 393
	}

394 395 396 397 398
	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;

399
	/* Check for Nport to NPort pt2pt protocol */
400 401
	if ((vport->fc_flag & FC_PT2PT) &&
	    !(vport->fc_flag & FC_PT2PT_PLOGI)) {
已提交
402
		/* rcv'ed PLOGI decides what our NPortId will be */
J
James Smart 已提交
403
		vport->fc_myDID = icmd->un.rcvels.parmRo;
404 405 406 407 408

		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;
已提交
409
		}
410

411
		/*
412 413
		 * For pt-to-pt, use the larger EDTOV
		 * RATOV = 2 * EDTOV
414
		 */
415 416 417 418 419 420 421 422
		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 */

423 424
		if (phba->sli_rev == LPFC_SLI_REV4)
			lpfc_issue_reg_vfi(vport);
425 426 427 428 429 430 431 432 433 434 435 436 437
		else {
			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
			if (mbox == NULL)
				goto out;
			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;
			}
		}
已提交
438

J
James Smart 已提交
439
		lpfc_can_disctmo(vport);
已提交
440
	}
441

442 443 444 445 446 447 448 449 450
	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;
	}

已提交
451
	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
J
James Smart 已提交
452
	if (!mbox)
已提交
453 454
		goto out;

455 456 457 458
	/* Registering an existing RPI behaves differently for SLI3 vs SLI4 */
	if (phba->sli_rev == LPFC_SLI_REV4)
		lpfc_unreg_rpi(vport, ndlp);

459
	rc = lpfc_reg_rpi(phba, vport->vpi, icmd->un.rcvels.remoteID,
460
			    (uint8_t *) sp, mbox, ndlp->nlp_rpi);
J
James Smart 已提交
461 462
	if (rc) {
		mempool_free(mbox, phba->mbox_mem_pool);
已提交
463 464 465 466 467 468 469
		goto out;
	}

	/* ACC PLOGI rsp command needs to execute first,
	 * queue this mbox command to be processed later.
	 */
	mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
470
	/*
471
	 * mbox->ctx_ndlp = lpfc_nlp_get(ndlp) deferred until mailbox
472 473
	 * command issued in lpfc_cmpl_els_acc().
	 */
J
James Smart 已提交
474 475
	mbox->vport = vport;
	spin_lock_irq(shost->host_lock);
476
	ndlp->nlp_flag |= (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI);
J
James Smart 已提交
477
	spin_unlock_irq(shost->host_lock);
已提交
478

J
James Smart 已提交
479 480 481 482 483 484 485 486 487 488 489
	/*
	 * If there is an outstanding PLOGI issued, abort it before
	 * sending ACC rsp for received PLOGI. If pending plogi
	 * is not canceled here, the plogi will be rejected by
	 * remote port and will be retried. On a configuration with
	 * single discovery thread, this will cause a huge delay in
	 * discovery. Also this will cause multiple state machines
	 * running in parallel for this node.
	 */
	if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE) {
		/* software abort outstanding PLOGI */
490
		lpfc_els_abort(phba, ndlp);
J
James Smart 已提交
491 492
	}

J
James Smart 已提交
493
	if ((vport->port_type == LPFC_NPIV_PORT &&
494
	     vport->cfg_restrict_login)) {
J
James Smart 已提交
495 496 497 498 499 500 501 502 503 504 505

		/* 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;
506
		rc = lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
J
James Smart 已提交
507
			ndlp, mbox);
508 509
		if (rc)
			mempool_free(mbox, phba->mbox_mem_pool);
J
James Smart 已提交
510 511
		return 1;
	}
512 513 514
	rc = lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, mbox);
	if (rc)
		mempool_free(mbox, phba->mbox_mem_pool);
515
	return 1;
已提交
516 517 518
out:
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
J
James Smart 已提交
519
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
520
	return 0;
已提交
521 522
}

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
/**
 * 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;

539 540
	elsiocb = (struct lpfc_iocbq *)mboxq->ctx_buf;
	ndlp = (struct lpfc_nodelist *)mboxq->ctx_ndlp;
541 542 543 544 545 546 547 548 549 550
	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);
551
	mempool_free(mboxq, phba->mbox_mem_pool);
552 553
}

已提交
554
static int
J
James Smart 已提交
555
lpfc_rcv_padisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
556 557
		struct lpfc_iocbq *cmdiocb)
{
J
James Smart 已提交
558
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
559
	struct lpfc_iocbq  *elsiocb;
已提交
560
	struct lpfc_dmabuf *pcmd;
J
James Smart 已提交
561 562
	struct serv_parm   *sp;
	struct lpfc_name   *pnn, *ppn;
已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	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 已提交
584
	if (icmd->ulpStatus == 0 && lpfc_check_adisc(vport, ndlp, pnn, ppn)) {
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

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

已提交
609
		if (cmd == ELS_CMD_ADISC) {
J
James Smart 已提交
610
			lpfc_els_rsp_adisc_acc(vport, cmdiocb, ndlp);
611
		} else {
612 613
			lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
				ndlp, NULL);
已提交
614
		}
615 616
out:
		/* If we are authenticated, move to the proper state */
617
		if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET))
618 619 620 621
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
		else
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);

622
		return 1;
已提交
623 624 625 626 627 628
	}
	/* 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 已提交
629
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
已提交
630 631

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

J
James Smart 已提交
634
	spin_lock_irq(shost->host_lock);
已提交
635
	ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
636
	spin_unlock_irq(shost->host_lock);
637 638
	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
	ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
639
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
640
	return 0;
已提交
641 642 643
}

static int
J
James Smart 已提交
644 645
lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
	      struct lpfc_iocbq *cmdiocb, uint32_t els_cmd)
已提交
646
{
J
James Smart 已提交
647
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
648 649 650
	struct lpfc_hba    *phba = vport->phba;
	struct lpfc_vport **vports;
	int i, active_vlink_present = 0 ;
J
James Smart 已提交
651 652

	/* Put ndlp in NPR state with 1 sec timeout for plogi, ACC logo */
已提交
653 654 655
	/* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
	 * PLOGIs during LOGO storms from a device.
	 */
J
James Smart 已提交
656
	spin_lock_irq(shost->host_lock);
已提交
657
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
658
	spin_unlock_irq(shost->host_lock);
659
	if (els_cmd == ELS_CMD_PRLO)
660
		lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
661
	else
662
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
J
James Smart 已提交
663 664 665
	if (ndlp->nlp_DID == Fabric_DID) {
		if (vport->port_state <= LPFC_FDISC)
			goto out;
666 667
		lpfc_linkdown_port(vport);
		spin_lock_irq(shost->host_lock);
J
James Smart 已提交
668
		vport->fc_flag |= FC_VPORT_LOGO_RCVD;
669
		spin_unlock_irq(shost->host_lock);
J
James Smart 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682
		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);
		}
已提交
683

J
James Smart 已提交
684 685 686 687 688 689 690
		/*
		 * 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 已提交
691 692 693 694
			/*
			 * If there are other active VLinks present,
			 * re-instantiate the Vlink using FDISC.
			 */
695 696
			mod_timer(&ndlp->nlp_delayfunc,
				  jiffies + msecs_to_jiffies(1000));
J
James Smart 已提交
697 698 699 700 701 702 703 704 705 706 707
			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);
		}
708 709 710 711
	} 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)) {
已提交
712
		/* Only try to re-login if this is NOT a Fabric Node */
713 714
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
715
		spin_lock_irq(shost->host_lock);
已提交
716
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
717
		spin_unlock_irq(shost->host_lock);
已提交
718

719
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
720
	}
J
James Smart 已提交
721
out:
722 723
	ndlp->nlp_prev_state = ndlp->nlp_state;
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
724

J
James Smart 已提交
725
	spin_lock_irq(shost->host_lock);
已提交
726
	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
727
	spin_unlock_irq(shost->host_lock);
已提交
728 729 730 731 732
	/* 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.
	 */
733
	return 0;
已提交
734 735
}

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
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;
}

已提交
771
static void
J
James Smart 已提交
772 773
lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
	      struct lpfc_iocbq *cmdiocb)
已提交
774
{
775
	struct lpfc_hba  *phba = vport->phba;
已提交
776 777 778 779 780 781 782 783 784 785
	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));

786 787 788 789 790 791 792 793
	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;
		}
794
		if (npr->targetFunc) {
795 796 797 798
			if (npr->prliType == PRLI_FCP_TYPE)
				ndlp->nlp_type |= NLP_FCP_TARGET;
			if (npr->prliType == PRLI_NVME_TYPE)
				ndlp->nlp_type |= NLP_NVME_TARGET;
799 800 801
			if (npr->writeXferRdyDis)
				ndlp->nlp_flag |= NLP_FIRSTBURST;
		}
802 803
		if (npr->Retry && ndlp->nlp_type &
					(NLP_FCP_INITIATOR | NLP_FCP_TARGET))
已提交
804
			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
805

806 807 808 809 810
		if (npr->Retry && phba->nsler &&
		    ndlp->nlp_type & (NLP_NVME_INITIATOR | NLP_NVME_TARGET))
			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;


811 812 813 814 815
		/* 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.
		 */
816
		if (phba->nvmet_support && (npr->prliType == PRLI_NVME_TYPE)) {
817
			ndlp->nlp_fc4_type |= NLP_FC4_NVME;
818 819 820 821
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
		}
		if (npr->prliType == PRLI_FCP_TYPE)
			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
已提交
822 823 824 825 826 827 828 829
	}
	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 已提交
830 831 832 833 834

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

835
		if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
836
			fc_remote_port_rolechg(rport, roles);
已提交
837 838 839 840
	}
}

static uint32_t
J
James Smart 已提交
841
lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
已提交
842
{
J
James Smart 已提交
843 844
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

845
	if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
846
		spin_lock_irq(shost->host_lock);
847
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
848
		spin_unlock_irq(shost->host_lock);
849 850 851
		return 0;
	}

852 853 854
	if (!(vport->fc_flag & FC_PT2PT)) {
		/* Check config parameter use-adisc or FCP-2 */
		if ((vport->cfg_use_adisc && (vport->fc_flag & FC_RSCN_MODE)) ||
855 856
		    ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) &&
		     (ndlp->nlp_type & NLP_FCP_TARGET))) {
857 858 859 860 861
			spin_lock_irq(shost->host_lock);
			ndlp->nlp_flag |= NLP_NPR_ADISC;
			spin_unlock_irq(shost->host_lock);
			return 1;
		}
862
	}
863 864

	spin_lock_irq(shost->host_lock);
865
	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
866
	spin_unlock_irq(shost->host_lock);
867 868
	lpfc_unreg_rpi(vport, ndlp);
	return 0;
已提交
869
}
870

871
/**
L
Lucas De Marchi 已提交
872
 * lpfc_release_rpi - Release a RPI by issuing unreg_login mailbox cmd.
873 874 875 876 877 878 879
 * @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.
 **/
880
static void
881 882
lpfc_release_rpi(struct lpfc_hba *phba, struct lpfc_vport *vport,
		 struct lpfc_nodelist *ndlp, uint16_t rpi)
883 884 885 886
{
	LPFC_MBOXQ_t *pmb;
	int rc;

887 888 889 890 891 892 893
	/* 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 "
894
				 "Data: x%px\n",
895 896 897 898 899 900
				 ndlp->nlp_rpi, ndlp->nlp_DID,
				 ndlp->nlp_defer_did,
				 ndlp->nlp_flag, ndlp);
		return;
	}

901 902 903 904 905 906 907 908
	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;
909 910 911 912 913 914 915 916 917 918 919 920
		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);

921 922 923 924 925
		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
		if (rc == MBX_NOT_FINISHED)
			mempool_free(pmb, phba->mbox_mem_pool);
	}
}
已提交
926 927

static uint32_t
J
James Smart 已提交
928 929
lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		  void *arg, uint32_t evt)
已提交
930
{
931 932 933 934 935 936 937 938 939 940
	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];
941
		lpfc_release_rpi(phba, vport, ndlp, rpi);
942
	}
943
	lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
944
			 "0271 Illegal State Transition: node x%x "
945 946 947
			 "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);
948
	return ndlp->nlp_state;
已提交
949 950
}

951 952 953 954 955 956 957 958 959 960 961
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,
962
			 "0272 Illegal State Transition: node x%x "
963 964 965 966 967 968 969
			 "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;
}

已提交
970 971 972
/* Start of Discovery State Machine routines */

static uint32_t
J
James Smart 已提交
973 974
lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
975 976 977 978 979
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
980
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
981
		return ndlp->nlp_state;
已提交
982
	}
983
	return NLP_STE_FREED_NODE;
已提交
984 985 986
}

static uint32_t
J
James Smart 已提交
987 988
lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
989
{
J
James Smart 已提交
990
	lpfc_issue_els_logo(vport, ndlp, 0);
991
	return ndlp->nlp_state;
已提交
992 993 994
}

static uint32_t
J
James Smart 已提交
995 996
lpfc_rcv_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
997
{
J
James Smart 已提交
998 999
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1000

J
James Smart 已提交
1001
	spin_lock_irq(shost->host_lock);
已提交
1002
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
1003
	spin_unlock_irq(shost->host_lock);
1004
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
1005

1006
	return ndlp->nlp_state;
已提交
1007 1008 1009
}

static uint32_t
J
James Smart 已提交
1010 1011
lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1012
{
1013
	return NLP_STE_FREED_NODE;
已提交
1014 1015 1016
}

static uint32_t
J
James Smart 已提交
1017 1018
lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1019
{
1020
	return NLP_STE_FREED_NODE;
已提交
1021 1022
}

1023 1024 1025 1026 1027 1028 1029 1030
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;
}

已提交
1031
static uint32_t
J
James Smart 已提交
1032
lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1033 1034
			   void *arg, uint32_t evt)
{
1035
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
1036
	struct lpfc_hba   *phba = vport->phba;
已提交
1037
	struct lpfc_iocbq *cmdiocb = arg;
J
James Smart 已提交
1038 1039 1040
	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);
已提交
1041 1042 1043 1044 1045 1046 1047 1048 1049
	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 已提交
1050
	port_cmp = memcmp(&vport->fc_portname, &sp->portName,
1051
			  sizeof(struct lpfc_name));
已提交
1052 1053 1054 1055 1056 1057

	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 已提交
1058 1059
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
1060
	} else {
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
		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 已提交
1077
	} /* If our portname was less */
已提交
1078

1079 1080 1081
	return ndlp->nlp_state;
}

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
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 已提交
1092
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1093 1094 1095
	return ndlp->nlp_state;
}

1096
static uint32_t
J
James Smart 已提交
1097 1098
lpfc_rcv_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
1099
{
J
James Smart 已提交
1100
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1101

1102 1103 1104
	/* 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;
1105
				/* software abort outstanding PLOGI */
J
James Smart 已提交
1106
	lpfc_els_abort(vport->phba, ndlp);
1107

J
James Smart 已提交
1108
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1109
	return ndlp->nlp_state;
已提交
1110 1111 1112
}

static uint32_t
J
James Smart 已提交
1113 1114
lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1115
{
J
James Smart 已提交
1116 1117 1118
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba   *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1119 1120

	/* software abort outstanding PLOGI */
1121
	lpfc_els_abort(phba, ndlp);
已提交
1122 1123

	if (evt == NLP_EVT_RCV_LOGO) {
1124
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1125
	} else {
J
James Smart 已提交
1126
		lpfc_issue_els_logo(vport, ndlp, 0);
已提交
1127 1128
	}

J
James Smart 已提交
1129
	/* Put ndlp in npr state set plogi timer for 1 sec */
1130
	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
1131
	spin_lock_irq(shost->host_lock);
1132
	ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1133
	spin_unlock_irq(shost->host_lock);
1134 1135
	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
J
James Smart 已提交
1136
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
1137

1138
	return ndlp->nlp_state;
已提交
1139 1140 1141
}

static uint32_t
J
James Smart 已提交
1142 1143 1144
lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg,
已提交
1145 1146
			    uint32_t evt)
{
J
James Smart 已提交
1147
	struct lpfc_hba    *phba = vport->phba;
1148
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
1149
	struct lpfc_iocbq  *cmdiocb, *rspiocb;
1150
	struct lpfc_dmabuf *pcmd, *prsp, *mp;
已提交
1151
	uint32_t *lp;
1152
	uint32_t vid, flag;
已提交
1153 1154
	IOCB_t *irsp;
	struct serv_parm *sp;
1155
	uint32_t ed_tov;
已提交
1156
	LPFC_MBOXQ_t *mbox;
1157
	int rc;
已提交
1158 1159 1160 1161 1162

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

	if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
1163
		/* Recovery from PLOGI collision logic */
1164
		return ndlp->nlp_state;
已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173
	}

	irsp = &rspiocb->iocb;

	if (irsp->ulpStatus)
		goto out;

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

J
James Smart 已提交
1174
	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1175 1176
	if (!prsp)
		goto out;
已提交
1177

J
James Smart 已提交
1178
	lp = (uint32_t *) prsp->virt;
已提交
1179
	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
1180 1181 1182 1183 1184

	/* 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)) {
1185 1186 1187 1188
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0142 PLOGI RSP: Invalid WWN.\n");
		goto out;
	}
1189
	if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0))
已提交
1190 1191
		goto out;
	/* PLOGI chkparm OK */
1192 1193 1194 1195
	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);
1196
	if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
已提交
1197
		ndlp->nlp_fcp_info |= CLASS2;
J
James Smart 已提交
1198
	else
已提交
1199
		ndlp->nlp_fcp_info |= CLASS3;
J
James Smart 已提交
1200

已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	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 已提交
1211
		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
已提交
1212

1213 1214 1215 1216 1217 1218 1219 1220
	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;
		}

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

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
		/*
		 * 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 {
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
			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;
			}

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
			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;
			}
		}
1266
	}
已提交
1267

J
James Smart 已提交
1268 1269
	lpfc_unreg_rpi(vport, ndlp);

1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	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;
	}

1280
	if (lpfc_reg_rpi(phba, vport->vpi, irsp->un.elsreq64.remoteID,
1281
			 (uint8_t *) sp, mbox, ndlp->nlp_rpi) == 0) {
1282
		switch (ndlp->nlp_DID) {
已提交
1283
		case NameServer_DID:
1284
			mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
已提交
1285 1286
			break;
		case FDMI_DID:
1287
			mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
已提交
1288 1289
			break;
		default:
1290
			ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
1291
			mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
已提交
1292
		}
1293
		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
J
James Smart 已提交
1294
		mbox->vport = vport;
1295
		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
已提交
1296
		    != MBX_NOT_FINISHED) {
J
James Smart 已提交
1297 1298
			lpfc_nlp_set_state(vport, ndlp,
					   NLP_STE_REG_LOGIN_ISSUE);
1299
			return ndlp->nlp_state;
已提交
1300
		}
1301 1302
		if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1303 1304 1305
		/* decrement node reference count to the failed mbox
		 * command
		 */
1306
		lpfc_nlp_put(ndlp);
1307
		mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
1308 1309
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
已提交
1310
		mempool_free(mbox, phba->mbox_mem_pool);
1311

1312 1313 1314 1315 1316
		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);
已提交
1317 1318
	} else {
		mempool_free(mbox, phba->mbox_mem_pool);
1319

1320 1321 1322 1323 1324
		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);
已提交
1325 1326 1327
	}


1328 1329 1330
out:
	if (ndlp->nlp_DID == NameServer_DID) {
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1331 1332
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0261 Cannot Register NameServer login\n");
1333 1334
	}

1335 1336 1337 1338 1339 1340 1341
	/*
	** 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);
1342
	spin_lock_irq(shost->host_lock);
1343
	ndlp->nlp_flag |= NLP_DEFER_RM;
1344
	spin_unlock_irq(shost->host_lock);
1345
	return NLP_STE_FREED_NODE;
已提交
1346 1347
}

1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
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)
{
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	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];
1369
		lpfc_release_rpi(phba, vport, ndlp, rpi);
1370
	}
1371 1372 1373
	return ndlp->nlp_state;
}

已提交
1374
static uint32_t
J
James Smart 已提交
1375 1376
lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1377
{
J
James Smart 已提交
1378 1379 1380 1381
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1382
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1383
		spin_unlock_irq(shost->host_lock);
1384
		return ndlp->nlp_state;
J
James Smart 已提交
1385
	} else {
1386
		/* software abort outstanding PLOGI */
J
James Smart 已提交
1387
		lpfc_els_abort(vport->phba, ndlp);
已提交
1388

J
James Smart 已提交
1389
		lpfc_drop_node(vport, ndlp);
1390 1391
		return NLP_STE_FREED_NODE;
	}
已提交
1392 1393 1394
}

static uint32_t
J
James Smart 已提交
1395 1396 1397 1398
lpfc_device_recov_plogi_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
1399
{
J
James Smart 已提交
1400 1401 1402
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

1403 1404 1405 1406 1407 1408
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
1409
	/* software abort outstanding PLOGI */
1410
	lpfc_els_abort(phba, ndlp);
已提交
1411

1412
	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
J
James Smart 已提交
1413
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1414
	spin_lock_irq(shost->host_lock);
1415
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1416
	spin_unlock_irq(shost->host_lock);
已提交
1417

1418
	return ndlp->nlp_state;
已提交
1419 1420 1421
}

static uint32_t
J
James Smart 已提交
1422 1423
lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1424
{
1425
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
1426
	struct lpfc_hba   *phba = vport->phba;
已提交
1427 1428 1429
	struct lpfc_iocbq *cmdiocb;

	/* software abort outstanding ADISC */
1430
	lpfc_els_abort(phba, ndlp);
已提交
1431 1432 1433

	cmdiocb = (struct lpfc_iocbq *) arg;

1434 1435 1436 1437 1438
	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);
1439
			if (vport->num_disc_nodes)
1440 1441 1442 1443
				lpfc_more_adisc(vport);
		}
		return ndlp->nlp_state;
	}
1444
	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1445 1446
	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
已提交
1447

1448
	return ndlp->nlp_state;
已提交
1449 1450 1451
}

static uint32_t
J
James Smart 已提交
1452 1453
lpfc_rcv_prli_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1454
{
J
James Smart 已提交
1455
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1456

1457 1458
	if (lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1459
	return ndlp->nlp_state;
已提交
1460 1461 1462
}

static uint32_t
J
James Smart 已提交
1463 1464
lpfc_rcv_logo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1465
{
J
James Smart 已提交
1466
	struct lpfc_hba *phba = vport->phba;
已提交
1467 1468 1469 1470 1471
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

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

J
James Smart 已提交
1474
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1475
	return ndlp->nlp_state;
已提交
1476 1477 1478
}

static uint32_t
J
James Smart 已提交
1479 1480 1481
lpfc_rcv_padisc_adisc_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1482 1483 1484 1485 1486
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1487
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1488
	return ndlp->nlp_state;
已提交
1489 1490 1491
}

static uint32_t
J
James Smart 已提交
1492 1493
lpfc_rcv_prlo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1494 1495 1496 1497 1498 1499
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

	/* Treat like rcv logo */
J
James Smart 已提交
1500
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1501
	return ndlp->nlp_state;
已提交
1502 1503 1504
}

static uint32_t
J
James Smart 已提交
1505 1506 1507
lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1508
{
J
James Smart 已提交
1509 1510
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba   *phba = vport->phba;
已提交
1511 1512 1513
	struct lpfc_iocbq *cmdiocb, *rspiocb;
	IOCB_t *irsp;
	ADISC *ap;
1514
	int rc;
已提交
1515 1516 1517 1518 1519 1520 1521 1522

	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) ||
1523
	    (!lpfc_check_adisc(vport, ndlp, &ap->nodeName, &ap->portName))) {
已提交
1524
		/* 1 sec timeout */
1525 1526
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000));
J
James Smart 已提交
1527
		spin_lock_irq(shost->host_lock);
已提交
1528
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1529
		spin_unlock_irq(shost->host_lock);
1530
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
已提交
1531

J
James Smart 已提交
1532 1533
		memset(&ndlp->nlp_nodename, 0, sizeof(struct lpfc_name));
		memset(&ndlp->nlp_portname, 0, sizeof(struct lpfc_name));
已提交
1534

1535
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1536 1537
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		lpfc_unreg_rpi(vport, ndlp);
1538
		return ndlp->nlp_state;
已提交
1539
	}
1540

1541
	if (phba->sli_rev == LPFC_SLI_REV4) {
1542
		rc = lpfc_sli4_resume_rpi(ndlp, NULL, NULL);
1543 1544 1545 1546 1547 1548 1549
		if (rc) {
			/* Stay in state and retry. */
			ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
			return ndlp->nlp_state;
		}
	}

1550
	if (ndlp->nlp_type & NLP_FCP_TARGET) {
1551
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1552
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1553
	} else {
1554
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1555
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1556
	}
1557

1558
	return ndlp->nlp_state;
已提交
1559 1560 1561
}

static uint32_t
J
James Smart 已提交
1562 1563
lpfc_device_rm_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1564
{
J
James Smart 已提交
1565 1566 1567 1568
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1569
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1570
		spin_unlock_irq(shost->host_lock);
1571
		return ndlp->nlp_state;
J
James Smart 已提交
1572
	} else {
1573
		/* software abort outstanding ADISC */
J
James Smart 已提交
1574
		lpfc_els_abort(vport->phba, ndlp);
已提交
1575

J
James Smart 已提交
1576
		lpfc_drop_node(vport, ndlp);
1577 1578
		return NLP_STE_FREED_NODE;
	}
已提交
1579 1580 1581
}

static uint32_t
J
James Smart 已提交
1582 1583 1584 1585
lpfc_device_recov_adisc_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
1586
{
J
James Smart 已提交
1587 1588 1589
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

1590 1591 1592 1593 1594 1595
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
1596
	/* software abort outstanding ADISC */
1597
	lpfc_els_abort(phba, ndlp);
已提交
1598

1599
	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1600 1601
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
1602
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1603
	spin_unlock_irq(shost->host_lock);
1604
	lpfc_disc_set_adisc(vport, ndlp);
1605
	return ndlp->nlp_state;
已提交
1606 1607 1608
}

static uint32_t
J
James Smart 已提交
1609 1610 1611
lpfc_rcv_plogi_reglogin_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
已提交
1612 1613
			      uint32_t evt)
{
J
James Smart 已提交
1614
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1615

J
James Smart 已提交
1616
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1617
	return ndlp->nlp_state;
已提交
1618 1619 1620
}

static uint32_t
J
James Smart 已提交
1621 1622 1623
lpfc_rcv_prli_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1624 1625
			     uint32_t evt)
{
J
James Smart 已提交
1626
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1627
	struct ls_rjt     stat;
已提交
1628

1629 1630 1631
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb)) {
		return ndlp->nlp_state;
	}
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
	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));
1646 1647
			stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
			stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1648 1649
			lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
					    ndlp, NULL);
1650
			return ndlp->nlp_state;
1651 1652 1653 1654 1655
		}
	} else {
		/* Initiator mode. */
		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
	}
1656
	return ndlp->nlp_state;
已提交
1657 1658 1659
}

static uint32_t
J
James Smart 已提交
1660 1661 1662
lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1663 1664
			     uint32_t evt)
{
J
James Smart 已提交
1665 1666
	struct lpfc_hba   *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1667 1668 1669
	LPFC_MBOXQ_t	  *mb;
	LPFC_MBOXQ_t	  *nextmb;
	struct lpfc_dmabuf *mp;
1670
	struct lpfc_nodelist *ns_ndlp;
已提交
1671 1672 1673

	cmdiocb = (struct lpfc_iocbq *) arg;

1674 1675
	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
	if ((mb = phba->sli.mbox_active)) {
1676
		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1677
		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
1678
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1679
			lpfc_nlp_put(ndlp);
1680
			mb->ctx_ndlp = NULL;
1681 1682 1683 1684
			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
		}
	}

J
James Smart 已提交
1685
	spin_lock_irq(&phba->hbalock);
1686
	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1687
		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1688 1689
		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
			mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
1690
			if (mp) {
1691
				__lpfc_mbuf_free(phba, mp->virt, mp->phys);
1692 1693
				kfree(mp);
			}
1694
			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1695
			lpfc_nlp_put(ndlp);
1696
			list_del(&mb->list);
1697
			phba->sli.mboxq_cnt--;
1698 1699 1700
			mempool_free(mb, phba->mbox_mem_pool);
		}
	}
J
James Smart 已提交
1701
	spin_unlock_irq(&phba->hbalock);
1702

1703 1704 1705 1706 1707 1708 1709
	/* 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 已提交
1710
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1711
	return ndlp->nlp_state;
已提交
1712 1713 1714
}

static uint32_t
J
James Smart 已提交
1715 1716 1717
lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport *vport,
			       struct lpfc_nodelist *ndlp,
			       void *arg,
已提交
1718 1719
			       uint32_t evt)
{
J
James Smart 已提交
1720
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1721

J
James Smart 已提交
1722
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1723
	return ndlp->nlp_state;
已提交
1724 1725 1726
}

static uint32_t
J
James Smart 已提交
1727 1728 1729
lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1730 1731 1732 1733 1734
			     uint32_t evt)
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;
1735
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1736
	return ndlp->nlp_state;
已提交
1737 1738 1739
}

static uint32_t
J
James Smart 已提交
1740 1741 1742 1743
lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
				  struct lpfc_nodelist *ndlp,
				  void *arg,
				  uint32_t evt)
已提交
1744
{
J
James Smart 已提交
1745
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1746
	struct lpfc_hba *phba = vport->phba;
J
James Smart 已提交
1747
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1748
	MAILBOX_t *mb = &pmb->u.mb;
J
James Smart 已提交
1749
	uint32_t did  = mb->un.varWords[1];
已提交
1750 1751 1752

	if (mb->mbxStatus) {
		/* RegLogin failed */
1753
		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1754 1755 1756 1757 1758
				"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);
1759 1760 1761 1762 1763
		/*
		 * If RegLogin failed due to lack of HBA resources do not
		 * retry discovery.
		 */
		if (mb->mbxStatus == MBXERR_RPI_FULL) {
1764 1765
			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1766 1767 1768
			return ndlp->nlp_state;
		}

J
James Smart 已提交
1769
		/* Put ndlp in npr state set plogi timer for 1 sec */
1770 1771
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
1772
		spin_lock_irq(shost->host_lock);
已提交
1773
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1774
		spin_unlock_irq(shost->host_lock);
1775
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
已提交
1776

J
James Smart 已提交
1777
		lpfc_issue_els_logo(vport, ndlp, 0);
1778
		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1779
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1780
		return ndlp->nlp_state;
已提交
1781 1782
	}

1783
	/* SLI4 ports have preallocated logical rpis. */
1784
	if (phba->sli_rev < LPFC_SLI_REV4)
1785 1786
		ndlp->nlp_rpi = mb->un.varWords[0];

1787
	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
已提交
1788 1789

	/* Only if we are not a fabric nport do we issue PRLI */
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
	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;
1806 1807
			if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
1808 1809
				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
				/* We need to update the localport also */
1810
				lpfc_nvme_update_localport(vport);
1811 1812
			}

1813 1814 1815
		} else if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
			ndlp->nlp_fc4_type |= NLP_FC4_FCP;

1816
		} else if (ndlp->nlp_fc4_type == 0) {
1817 1818 1819 1820
			/* 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.
			 */
1821
			if (vport->cfg_enable_fc4_type != LPFC_ENABLE_FCP) {
1822 1823
				lpfc_ns_cmd(vport, SLI_CTNS_GFT_ID, 0,
					    ndlp->nlp_DID);
1824 1825 1826
				return ndlp->nlp_state;
			}
			ndlp->nlp_fc4_type = NLP_FC4_FCP;
1827 1828
		}

1829
		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1830
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1831 1832 1833 1834 1835
		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);
		}
已提交
1836
	} else {
1837 1838 1839 1840 1841 1842
		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.
		 */
1843 1844 1845 1846
		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);
		}
已提交
1847
	}
1848
	return ndlp->nlp_state;
已提交
1849 1850 1851
}

static uint32_t
J
James Smart 已提交
1852 1853 1854
lpfc_device_rm_reglogin_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
已提交
1855 1856
			      uint32_t evt)
{
J
James Smart 已提交
1857 1858 1859 1860
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1861
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1862
		spin_unlock_irq(shost->host_lock);
1863
		return ndlp->nlp_state;
J
James Smart 已提交
1864 1865
	} else {
		lpfc_drop_node(vport, ndlp);
1866 1867
		return NLP_STE_FREED_NODE;
	}
已提交
1868 1869 1870
}

static uint32_t
J
James Smart 已提交
1871 1872 1873 1874
lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
				 struct lpfc_nodelist *ndlp,
				 void *arg,
				 uint32_t evt)
已提交
1875
{
J
James Smart 已提交
1876 1877
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

1878 1879 1880 1881 1882 1883
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

1884
	ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1885 1886
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
1887

1888 1889 1890 1891 1892 1893 1894
	/* 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;

1895
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1896
	spin_unlock_irq(shost->host_lock);
1897
	lpfc_disc_set_adisc(vport, ndlp);
1898
	return ndlp->nlp_state;
已提交
1899 1900 1901
}

static uint32_t
J
James Smart 已提交
1902 1903
lpfc_rcv_plogi_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1904 1905 1906 1907 1908
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1909
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1910
	return ndlp->nlp_state;
已提交
1911 1912 1913
}

static uint32_t
J
James Smart 已提交
1914 1915
lpfc_rcv_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1916
{
J
James Smart 已提交
1917
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1918

1919 1920
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;
J
James Smart 已提交
1921
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1922
	return ndlp->nlp_state;
已提交
1923 1924 1925
}

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

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

J
James Smart 已提交
1934
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1935
	return ndlp->nlp_state;
已提交
1936 1937 1938
}

static uint32_t
J
James Smart 已提交
1939 1940
lpfc_rcv_padisc_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1941
{
J
James Smart 已提交
1942
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1943

J
James Smart 已提交
1944
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1945
	return ndlp->nlp_state;
已提交
1946 1947 1948 1949 1950 1951 1952 1953
}

/* 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 已提交
1954 1955
lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1956
{
J
James Smart 已提交
1957
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1958

1959
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1960
	return ndlp->nlp_state;
已提交
1961 1962 1963
}

static uint32_t
J
James Smart 已提交
1964 1965
lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1966
{
1967
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
1968
	struct lpfc_iocbq *cmdiocb, *rspiocb;
J
James Smart 已提交
1969
	struct lpfc_hba   *phba = vport->phba;
已提交
1970 1971
	IOCB_t *irsp;
	PRLI *npr;
1972 1973
	struct lpfc_nvme_prli *nvpr;
	void *temp_ptr;
已提交
1974 1975 1976

	cmdiocb = (struct lpfc_iocbq *) arg;
	rspiocb = cmdiocb->context_un.rsp_iocb;
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988

	/* 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;
已提交
1989 1990 1991

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus) {
J
James Smart 已提交
1992
		if ((vport->port_type == LPFC_NPIV_PORT) &&
1993
		    vport->cfg_restrict_login) {
J
James Smart 已提交
1994 1995
			goto out;
		}
1996

1997 1998
		/* Adjust the nlp_type accordingly if the PRLI failed */
		if (npr)
1999
			ndlp->nlp_fc4_type &= ~NLP_FC4_FCP;
2000 2001
		if (nvpr)
			ndlp->nlp_fc4_type &= ~NLP_FC4_NVME;
2002

2003 2004
		/* We can't set the DSM state till BOTH PRLIs complete */
		goto out_err;
已提交
2005 2006
	}

2007
	if (npr && (npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
已提交
2008
	    (npr->prliType == PRLI_FCP_TYPE)) {
2009 2010 2011 2012
		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
				 "6028 FCP NPR PRLI Cmpl Init %d Target %d\n",
				 npr->initiatorFunc,
				 npr->targetFunc);
已提交
2013 2014
		if (npr->initiatorFunc)
			ndlp->nlp_type |= NLP_FCP_INITIATOR;
2015
		if (npr->targetFunc) {
已提交
2016
			ndlp->nlp_type |= NLP_FCP_TARGET;
2017 2018 2019
			if (npr->writeXferRdyDis)
				ndlp->nlp_flag |= NLP_FIRSTBURST;
		}
已提交
2020 2021
		if (npr->Retry)
			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032

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

2033 2034 2035 2036 2037
		if (phba->nsler && bf_get_be32(prli_nsler, nvpr))
			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;
		else
			ndlp->nlp_nvme_info &= ~NLP_NVME_NSLER;

2038 2039
		/* Target driver cannot solicit NVME FB. */
		if (bf_get_be32(prli_tgt, nvpr)) {
2040 2041 2042 2043
			/* Complete the nvme target roles.  The transport
			 * needs to know if the rport is capable of
			 * discovery in addition to its role.
			 */
2044
			ndlp->nlp_type |= NLP_NVME_TARGET;
2045 2046
			if (bf_get_be32(prli_disc, nvpr))
				ndlp->nlp_type |= NLP_NVME_DISCOVERY;
2047 2048 2049 2050 2051 2052 2053

			/*
			 * 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.
			 */
2054 2055 2056 2057 2058 2059 2060 2061 2062
			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);
2063 2064 2065 2066 2067 2068 2069

				/* 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;
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
			}
		}

		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);
已提交
2082
	}
2083 2084
	if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
	    (vport->port_type == LPFC_NPIV_PORT) &&
2085
	     vport->cfg_restrict_login) {
J
James Smart 已提交
2086
out:
2087 2088 2089 2090 2091 2092
		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;
2093
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2094 2095
		return ndlp->nlp_state;
	}
已提交
2096

2097 2098 2099 2100 2101 2102 2103 2104
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);
2105 2106
		else if (ndlp->nlp_type &
			 (NLP_FCP_INITIATOR | NLP_NVME_INITIATOR))
2107 2108 2109 2110 2111 2112 2113 2114 2115
			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);

2116
	return ndlp->nlp_state;
已提交
2117 2118 2119
}

/*! lpfc_device_rm_prli_issue
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
 *
 * \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.
 *
 */

已提交
2137
static uint32_t
J
James Smart 已提交
2138 2139
lpfc_device_rm_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2140
{
J
James Smart 已提交
2141 2142 2143 2144
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
2145
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
2146
		spin_unlock_irq(shost->host_lock);
2147
		return ndlp->nlp_state;
J
James Smart 已提交
2148
	} else {
2149
		/* software abort outstanding PLOGI */
J
James Smart 已提交
2150
		lpfc_els_abort(vport->phba, ndlp);
已提交
2151

J
James Smart 已提交
2152
		lpfc_drop_node(vport, ndlp);
2153 2154
		return NLP_STE_FREED_NODE;
	}
已提交
2155 2156 2157 2158
}


/*! lpfc_device_recov_prli_issue
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
 *
 * \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.
 */
已提交
2174
static uint32_t
J
James Smart 已提交
2175 2176 2177 2178
lpfc_device_recov_prli_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
			     uint32_t evt)
已提交
2179
{
J
James Smart 已提交
2180 2181 2182
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

2183 2184 2185 2186 2187 2188
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
2189
	/* software abort outstanding PRLI */
2190
	lpfc_els_abort(phba, ndlp);
已提交
2191

2192
	ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
J
James Smart 已提交
2193 2194
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2195
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
2196
	spin_unlock_irq(shost->host_lock);
2197
	lpfc_disc_set_adisc(vport, ndlp);
2198
	return ndlp->nlp_state;
已提交
2199 2200
}

2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
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);
2237
	ndlp->nlp_flag |= NLP_LOGO_ACC;
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
	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)
{
	/*
2291 2292
	 * DevLoss has timed out and is calling for Device Remove.
	 * In this case, abort the LOGO and cleanup the ndlp
2293
	 */
2294 2295 2296 2297 2298 2299

	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;
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314
}

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

已提交
2315
static uint32_t
J
James Smart 已提交
2316 2317
lpfc_rcv_plogi_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2318
{
J
James Smart 已提交
2319
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2320

J
James Smart 已提交
2321
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2322
	return ndlp->nlp_state;
已提交
2323 2324 2325
}

static uint32_t
J
James Smart 已提交
2326 2327
lpfc_rcv_prli_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2328
{
J
James Smart 已提交
2329
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2330

2331 2332 2333
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;

J
James Smart 已提交
2334 2335
	lpfc_rcv_prli(vport, ndlp, cmdiocb);
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2336
	return ndlp->nlp_state;
已提交
2337 2338 2339
}

static uint32_t
J
James Smart 已提交
2340 2341
lpfc_rcv_logo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2342
{
J
James Smart 已提交
2343
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2344

J
James Smart 已提交
2345
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2346
	return ndlp->nlp_state;
已提交
2347 2348 2349
}

static uint32_t
J
James Smart 已提交
2350 2351
lpfc_rcv_padisc_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2352
{
J
James Smart 已提交
2353
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2354

J
James Smart 已提交
2355
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2356
	return ndlp->nlp_state;
已提交
2357 2358 2359
}

static uint32_t
J
James Smart 已提交
2360 2361
lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
2362
{
J
James Smart 已提交
2363
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2364

2365
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2366
	return ndlp->nlp_state;
已提交
2367 2368 2369
}

static uint32_t
J
James Smart 已提交
2370 2371 2372 2373
lpfc_device_recov_unmap_node(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
			     uint32_t evt)
已提交
2374
{
J
James Smart 已提交
2375 2376
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2377
	ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
J
James Smart 已提交
2378 2379
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2380
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2381
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2382 2383
	spin_unlock_irq(shost->host_lock);
	lpfc_disc_set_adisc(vport, ndlp);
已提交
2384

2385
	return ndlp->nlp_state;
已提交
2386 2387 2388
}

static uint32_t
J
James Smart 已提交
2389 2390
lpfc_rcv_plogi_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
2391
{
J
James Smart 已提交
2392
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2393

J
James Smart 已提交
2394
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2395
	return ndlp->nlp_state;
已提交
2396 2397 2398
}

static uint32_t
J
James Smart 已提交
2399 2400
lpfc_rcv_prli_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2401
{
J
James Smart 已提交
2402
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2403

2404 2405
	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
		return ndlp->nlp_state;
J
James Smart 已提交
2406
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2407
	return ndlp->nlp_state;
已提交
2408 2409 2410
}

static uint32_t
J
James Smart 已提交
2411 2412
lpfc_rcv_logo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2413
{
J
James Smart 已提交
2414
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2415

J
James Smart 已提交
2416
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2417
	return ndlp->nlp_state;
已提交
2418 2419 2420
}

static uint32_t
J
James Smart 已提交
2421 2422 2423
lpfc_rcv_padisc_mapped_node(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
2424
{
J
James Smart 已提交
2425
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2426

J
James Smart 已提交
2427
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2428
	return ndlp->nlp_state;
已提交
2429 2430 2431
}

static uint32_t
J
James Smart 已提交
2432 2433
lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
2434
{
J
James Smart 已提交
2435 2436
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
2437 2438

	/* flush the target */
2439
	lpfc_sli_abort_iocb(vport, &phba->sli.sli3_ring[LPFC_FCP_RING],
2440
			    ndlp->nlp_sid, 0, LPFC_CTX_TGT);
已提交
2441 2442

	/* Treat like rcv logo */
J
James Smart 已提交
2443
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
2444
	return ndlp->nlp_state;
已提交
2445 2446 2447
}

static uint32_t
J
James Smart 已提交
2448 2449 2450 2451
lpfc_device_recov_mapped_node(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
2452
{
J
James Smart 已提交
2453 2454
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2455
	ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
J
James Smart 已提交
2456 2457
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
2458
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2459
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2460 2461
	spin_unlock_irq(shost->host_lock);
	lpfc_disc_set_adisc(vport, ndlp);
2462
	return ndlp->nlp_state;
已提交
2463 2464 2465
}

static uint32_t
J
James Smart 已提交
2466 2467
lpfc_rcv_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2468
{
J
James Smart 已提交
2469 2470
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb  = (struct lpfc_iocbq *) arg;
已提交
2471 2472

	/* Ignore PLOGI if we have an outstanding LOGO */
2473
	if (ndlp->nlp_flag & (NLP_LOGO_SND | NLP_LOGO_ACC))
2474
		return ndlp->nlp_state;
J
James Smart 已提交
2475
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
2476
		lpfc_cancel_retry_delay_tmo(vport, ndlp);
J
James Smart 已提交
2477
		spin_lock_irq(shost->host_lock);
2478
		ndlp->nlp_flag &= ~(NLP_NPR_ADISC | NLP_NPR_2B_DISC);
J
James Smart 已提交
2479
		spin_unlock_irq(shost->host_lock);
2480 2481 2482 2483 2484 2485 2486
	} 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);
		}
已提交
2487
	}
2488
	return ndlp->nlp_state;
已提交
2489 2490 2491
}

static uint32_t
J
James Smart 已提交
2492 2493
lpfc_rcv_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		       void *arg, uint32_t evt)
已提交
2494
{
J
James Smart 已提交
2495 2496 2497
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
	struct ls_rjt     stat;
已提交
2498 2499 2500 2501

	memset(&stat, 0, sizeof (struct ls_rjt));
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
J
James Smart 已提交
2502
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
已提交
2503 2504 2505

	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
J
James Smart 已提交
2506
			spin_lock_irq(shost->host_lock);
2507
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2508
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2509 2510 2511
			spin_unlock_irq(shost->host_lock);
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
已提交
2512
		} else {
2513
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2514 2515
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
2516 2517
		}
	}
2518
	return ndlp->nlp_state;
已提交
2519 2520 2521
}

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

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

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

J
James Smart 已提交
2537
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
J
James Smart 已提交
2538 2539 2540 2541 2542
	/*
	 * 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 已提交
2543
	if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
2544
	    !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
已提交
2545
		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
2546
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2547
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2548 2549
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
已提交
2550
		} else {
2551
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
2552 2553
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
2554 2555
		}
	}
2556
	return ndlp->nlp_state;
已提交
2557 2558 2559
}

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

J
James Smart 已提交
2566
	spin_lock_irq(shost->host_lock);
2567
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
2568
	spin_unlock_irq(shost->host_lock);
2569

2570
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
2571

J
James Smart 已提交
2572
	if ((ndlp->nlp_flag & NLP_DELAY_TMO) == 0) {
2573 2574
		mod_timer(&ndlp->nlp_delayfunc,
			  jiffies + msecs_to_jiffies(1000 * 1));
J
James Smart 已提交
2575
		spin_lock_irq(shost->host_lock);
2576 2577
		ndlp->nlp_flag |= NLP_DELAY_TMO;
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
2578
		spin_unlock_irq(shost->host_lock);
2579
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
2580
	} else {
J
James Smart 已提交
2581
		spin_lock_irq(shost->host_lock);
2582
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
2583
		spin_unlock_irq(shost->host_lock);
已提交
2584
	}
2585 2586
	return ndlp->nlp_state;
}
已提交
2587

2588
static uint32_t
J
James Smart 已提交
2589 2590
lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
2591 2592
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2593
	IOCB_t *irsp;
2594
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2595 2596 2597

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

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus) {
2601
		spin_lock_irq(shost->host_lock);
2602
		ndlp->nlp_flag |= NLP_DEFER_RM;
2603
		spin_unlock_irq(shost->host_lock);
2604 2605
		return NLP_STE_FREED_NODE;
	}
2606 2607 2608 2609
	return ndlp->nlp_state;
}

static uint32_t
J
James Smart 已提交
2610 2611
lpfc_cmpl_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
2612 2613
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2614
	IOCB_t *irsp;
2615 2616 2617

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

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
J
James Smart 已提交
2621
		lpfc_drop_node(vport, ndlp);
2622 2623
		return NLP_STE_FREED_NODE;
	}
2624
	return ndlp->nlp_state;
已提交
2625 2626 2627
}

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

	/* For the fabric port just clear the fc flags. */
2634 2635 2636 2637 2638
	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 已提交
2639
	lpfc_unreg_rpi(vport, ndlp);
2640 2641 2642 2643
	return ndlp->nlp_state;
}

static uint32_t
J
James Smart 已提交
2644 2645
lpfc_cmpl_adisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
2646 2647
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
2648
	IOCB_t *irsp;
2649 2650 2651

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

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
J
James Smart 已提交
2655
		lpfc_drop_node(vport, ndlp);
2656 2657
		return NLP_STE_FREED_NODE;
	}
2658
	return ndlp->nlp_state;
已提交
2659 2660 2661
}

static uint32_t
J
James Smart 已提交
2662 2663 2664
lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
2665
{
J
James Smart 已提交
2666
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
2667
	MAILBOX_t    *mb = &pmb->u.mb;
已提交
2668

2669
	if (!mb->mbxStatus) {
2670 2671 2672
		/* SLI4 ports have preallocated logical rpis. */
		if (vport->phba->sli_rev < LPFC_SLI_REV4)
			ndlp->nlp_rpi = mb->un.varWords[0];
2673
		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
2674 2675 2676
		if (ndlp->nlp_flag & NLP_LOGO_ACC) {
			lpfc_unreg_rpi(vport, ndlp);
		}
2677
	} else {
2678
		if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
J
James Smart 已提交
2679
			lpfc_drop_node(vport, ndlp);
2680 2681 2682
			return NLP_STE_FREED_NODE;
		}
	}
2683
	return ndlp->nlp_state;
已提交
2684 2685 2686
}

static uint32_t
J
James Smart 已提交
2687 2688
lpfc_device_rm_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2689
{
J
James Smart 已提交
2690 2691
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

2692
	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
J
James Smart 已提交
2693
		spin_lock_irq(shost->host_lock);
2694
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
2695
		spin_unlock_irq(shost->host_lock);
2696 2697
		return ndlp->nlp_state;
	}
J
James Smart 已提交
2698
	lpfc_drop_node(vport, ndlp);
2699
	return NLP_STE_FREED_NODE;
已提交
2700 2701 2702
}

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

2708 2709 2710 2711 2712 2713
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

2714
	lpfc_cancel_retry_delay_tmo(vport, ndlp);
J
James Smart 已提交
2715
	spin_lock_irq(shost->host_lock);
2716
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2717
	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
J
James Smart 已提交
2718
	spin_unlock_irq(shost->host_lock);
2719
	return ndlp->nlp_state;
已提交
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754
}


/* 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
2755
 * lists will receive a DEVICE_RECOVERY event. If the linkdown or devloss timers
已提交
2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779
 * 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 已提交
2780
     (struct lpfc_vport *, struct lpfc_nodelist *, void *, uint32_t) = {
已提交
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
	/* 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       */
2794
	lpfc_device_recov_unused_node,	/* DEVICE_RECOVERY */
已提交
2795 2796

	lpfc_rcv_plogi_plogi_issue,	/* RCV_PLOGI   PLOGI_ISSUE    */
2797
	lpfc_rcv_prli_plogi_issue,	/* RCV_PRLI        */
2798
	lpfc_rcv_logo_plogi_issue,	/* RCV_LOGO        */
已提交
2799 2800 2801 2802 2803
	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       */
2804
	lpfc_cmpl_logo_plogi_issue,	/* CMPL_LOGO       */
已提交
2805
	lpfc_disc_illegal,		/* CMPL_ADISC      */
2806
	lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
已提交
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
	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        */
2830
	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
已提交
2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
	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        */
2844
	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
已提交
2845 2846 2847 2848 2849 2850 2851
	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 */

2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865
	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 */

已提交
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
	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        */
2900 2901
	lpfc_cmpl_plogi_npr_node,	/* CMPL_PLOGI      */
	lpfc_cmpl_prli_npr_node,	/* CMPL_PRLI       */
已提交
2902
	lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
2903
	lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
已提交
2904 2905 2906 2907 2908 2909
	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 已提交
2910 2911
lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2912 2913
{
	uint32_t cur_state, rc;
J
James Smart 已提交
2914
	uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
已提交
2915
			 uint32_t);
2916
	uint32_t got_ndlp = 0;
2917
	uint32_t data1;
2918 2919 2920

	if (lpfc_nlp_get(ndlp))
		got_ndlp = 1;
已提交
2921 2922 2923

	cur_state = ndlp->nlp_state;

2924 2925
	data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
		((uint32_t)ndlp->nlp_type));
已提交
2926
	/* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
2927 2928
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0211 DSM in event x%x on NPort x%x in "
2929 2930
			 "state %d rpi x%x Data: x%x x%x\n",
			 evt, ndlp->nlp_DID, cur_state, ndlp->nlp_rpi,
2931
			 ndlp->nlp_flag, data1);
已提交
2932

J
James Smart 已提交
2933 2934 2935 2936
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
		 "DSM in:          evt:%d ste:%d did:x%x",
		evt, cur_state, ndlp->nlp_DID);

已提交
2937
	func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
J
James Smart 已提交
2938
	rc = (func) (vport, ndlp, arg, evt);
已提交
2939 2940

	/* DSM out state <rc> on NPort <nlp_DID> */
2941
	if (got_ndlp) {
2942 2943
		data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
			((uint32_t)ndlp->nlp_type));
2944
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2945
			 "0212 DSM out state %d on NPort x%x "
2946 2947 2948
			 "rpi x%x Data: x%x x%x\n",
			 rc, ndlp->nlp_DID, ndlp->nlp_rpi, ndlp->nlp_flag,
			 data1);
已提交
2949

2950 2951 2952 2953 2954 2955 2956
		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,
2957
			"0213 DSM out state %d on NPort free\n", rc);
J
James Smart 已提交
2958

2959 2960 2961 2962
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
			"DSM out:         ste:%d did:x%x flg:x%x",
			rc, 0, 0);
	}
已提交
2963

2964
	return rc;
已提交
2965
}