lpfc_nportdisc.c 64.4 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) 2004-2007 Emulex.  All rights reserved.           *
5
 * EMULEX and SLI are trademarks of Emulex.                        *
已提交
6
 * www.emulex.com                                                  *
7
 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
已提交
8 9
 *                                                                 *
 * This program is free software; you can redistribute it and/or   *
10 11 12 13 14 15 16 17 18 19
 * 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.                                     *
已提交
20 21 22 23 24 25
 *******************************************************************/

#include <linux/blkdev.h>
#include <linux/pci.h>
#include <linux/interrupt.h>

26
#include <scsi/scsi.h>
已提交
27 28 29 30 31 32 33 34 35 36 37
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_transport_fc.h>

#include "lpfc_hw.h"
#include "lpfc_sli.h"
#include "lpfc_disc.h"
#include "lpfc_scsi.h"
#include "lpfc.h"
#include "lpfc_logmsg.h"
#include "lpfc_crtn.h"
38
#include "lpfc_vport.h"
J
James Smart 已提交
39
#include "lpfc_debugfs.h"
已提交
40 41 42 43


/* Called to verify a rcv'ed ADISC was intended for us. */
static int
J
James Smart 已提交
44 45
lpfc_check_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		 struct lpfc_name *nn, struct lpfc_name *pn)
已提交
46 47 48 49
{
	/* Compare the ADISC rsp WWNN / WWPN matches our internal node
	 * table entry for that node.
	 */
J
James Smart 已提交
50
	if (memcmp(nn, &ndlp->nlp_nodename, sizeof (struct lpfc_name)))
51
		return 0;
已提交
52

J
James Smart 已提交
53
	if (memcmp(pn, &ndlp->nlp_portname, sizeof (struct lpfc_name)))
54
		return 0;
已提交
55 56

	/* we match, return success */
57
	return 1;
已提交
58 59 60
}

int
J
James Smart 已提交
61 62
lpfc_check_sparm(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		 struct serv_parm * sp, uint32_t class)
已提交
63
{
J
James Smart 已提交
64
	volatile struct serv_parm *hsp = &vport->fc_sparam;
65 66 67 68 69 70 71 72 73
	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.
	 */
已提交
74
	if (sp->cls1.classValid) {
75 76 77 78
		hsp_value = (hsp->cls1.rcvDataSizeMsb << 8) |
				hsp->cls1.rcvDataSizeLsb;
		ssp_value = (sp->cls1.rcvDataSizeMsb << 8) |
				sp->cls1.rcvDataSizeLsb;
79 80
		if (!ssp_value)
			goto bad_service_param;
81
		if (ssp_value > hsp_value) {
已提交
82
			sp->cls1.rcvDataSizeLsb = hsp->cls1.rcvDataSizeLsb;
83 84
			sp->cls1.rcvDataSizeMsb = hsp->cls1.rcvDataSizeMsb;
		}
已提交
85
	} else if (class == CLASS1) {
86
		goto bad_service_param;
已提交
87 88 89
	}

	if (sp->cls2.classValid) {
90 91 92 93
		hsp_value = (hsp->cls2.rcvDataSizeMsb << 8) |
				hsp->cls2.rcvDataSizeLsb;
		ssp_value = (sp->cls2.rcvDataSizeMsb << 8) |
				sp->cls2.rcvDataSizeLsb;
94 95
		if (!ssp_value)
			goto bad_service_param;
96
		if (ssp_value > hsp_value) {
已提交
97
			sp->cls2.rcvDataSizeLsb = hsp->cls2.rcvDataSizeLsb;
98 99
			sp->cls2.rcvDataSizeMsb = hsp->cls2.rcvDataSizeMsb;
		}
已提交
100
	} else if (class == CLASS2) {
101
		goto bad_service_param;
已提交
102 103 104
	}

	if (sp->cls3.classValid) {
105 106 107 108
		hsp_value = (hsp->cls3.rcvDataSizeMsb << 8) |
				hsp->cls3.rcvDataSizeLsb;
		ssp_value = (sp->cls3.rcvDataSizeMsb << 8) |
				sp->cls3.rcvDataSizeLsb;
109 110
		if (!ssp_value)
			goto bad_service_param;
111
		if (ssp_value > hsp_value) {
已提交
112
			sp->cls3.rcvDataSizeLsb = hsp->cls3.rcvDataSizeLsb;
113 114
			sp->cls3.rcvDataSizeMsb = hsp->cls3.rcvDataSizeMsb;
		}
已提交
115
	} else if (class == CLASS3) {
116
		goto bad_service_param;
已提交
117 118
	}

119 120 121 122 123 124 125 126
	/*
	 * 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) {
已提交
127
		sp->cmn.bbRcvSizeLsb = hsp->cmn.bbRcvSizeLsb;
128 129 130
		sp->cmn.bbRcvSizeMsb = (sp->cmn.bbRcvSizeMsb & 0xF0) |
				       (hsp->cmn.bbRcvSizeMsb & 0x0F);
	}
已提交
131 132 133

	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof (struct lpfc_name));
134
	return 1;
135
bad_service_param:
136 137 138 139 140 141 142 143 144
	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]);
145
	return 0;
已提交
146 147 148
}

static void *
J
James Smart 已提交
149
lpfc_check_elscmpl_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
150
			struct lpfc_iocbq *rspiocb)
已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
{
	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));
		}
170
	} else {
已提交
171 172 173 174 175 176 177
		/* 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;
	}
178
	return ptr;
已提交
179 180 181 182 183 184 185 186 187
}


/*
 * Free resources / clean up outstanding I/Os
 * associated with a LPFC_NODELIST entry. This
 * routine effectively results in a "software abort".
 */
int
J
James Smart 已提交
188
lpfc_els_abort(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
已提交
189
{
190
	LIST_HEAD(completions);
J
James Smart 已提交
191 192
	struct lpfc_sli  *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
已提交
193
	struct lpfc_iocbq *iocb, *next_iocb;
194
	IOCB_t *cmd;
已提交
195 196

	/* Abort outstanding I/O on NPort <nlp_DID> */
197 198 199 200 201
	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_DISCOVERY,
			 "0205 Abort outstanding I/O on NPort x%x "
			 "Data: x%x x%x x%x\n",
			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
			 ndlp->nlp_rpi);
202 203

	lpfc_fabric_abort_nport(ndlp);
已提交
204 205

	/* First check the txq */
J
James Smart 已提交
206
	spin_lock_irq(&phba->hbalock);
207
	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
J
James Smart 已提交
208
		/* Check to see if iocb matches the nport we are looking for */
209
		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp)) {
J
James Smart 已提交
210
			/* It matches, so deque and call compl with anp error */
211 212
			list_move_tail(&iocb->list, &completions);
			pring->txq_cnt--;
已提交
213
		}
214
	}
已提交
215 216

	/* Next check the txcmplq */
217
	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
J
James Smart 已提交
218
		/* Check to see if iocb matches the nport we are looking for */
219
		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp)) {
220
			lpfc_sli_issue_abort_iotag(phba, pring, iocb);
221
		}
222
	}
J
James Smart 已提交
223
	spin_unlock_irq(&phba->hbalock);
已提交
224

225 226 227
	while (!list_empty(&completions)) {
		iocb = list_get_first(&completions, struct lpfc_iocbq, list);
		cmd = &iocb->iocb;
228
		list_del_init(&iocb->list);
229

J
James Smart 已提交
230 231 232
		if (!iocb->iocb_cmpl)
			lpfc_sli_release_iocbq(phba, iocb);
		else {
233 234 235
			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
			(iocb->iocb_cmpl) (phba, iocb, iocb);
J
James Smart 已提交
236
		}
237 238
	}

已提交
239
	/* If we are delaying issuing an ELS command, cancel it */
240
	if (ndlp->nlp_flag & NLP_DELAY_TMO)
J
James Smart 已提交
241
		lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
242
	return 0;
已提交
243 244 245
}

static int
J
James Smart 已提交
246
lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
247
	       struct lpfc_iocbq *cmdiocb)
已提交
248
{
J
James Smart 已提交
249 250
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba    *phba = vport->phba;
已提交
251 252 253 254 255 256 257 258 259
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	IOCB_t *icmd;
	struct serv_parm *sp;
	LPFC_MBOXQ_t *mbox;
	struct ls_rjt stat;
	int rc;

	memset(&stat, 0, sizeof (struct ls_rjt));
J
James Smart 已提交
260
	if (vport->port_state <= LPFC_FLOGI) {
已提交
261 262 263 264
		/* Before responding to PLOGI, check for pt2pt mode.
		 * If we are pt2pt, with an outstanding FLOGI, abort
		 * the FLOGI and resend it first.
		 */
J
James Smart 已提交
265
		if (vport->fc_flag & FC_PT2PT) {
266
			 lpfc_els_abort_flogi(phba);
J
James Smart 已提交
267
		        if (!(vport->fc_flag & FC_PT2PT_PLOGI)) {
已提交
268 269 270 271 272 273 274 275
				/* If the other side is supposed to initiate
				 * the PLOGI anyway, just ACC it now and
				 * move on with discovery.
				 */
				phba->fc_edtov = FF_DEF_EDTOV;
				phba->fc_ratov = FF_DEF_RATOV;
				/* Start discovery - this should just do
				   CLEAR_LA */
J
James Smart 已提交
276
				lpfc_disc_start(vport);
277
			} else
J
James Smart 已提交
278
				lpfc_initial_flogi(vport);
279
		} else {
已提交
280 281
			stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
			stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
J
James Smart 已提交
282
			lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
J
James Smart 已提交
283
					    ndlp, NULL);
已提交
284 285 286 287 288 289
			return 0;
		}
	}
	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;
	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	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;
	}
J
James Smart 已提交
308
	if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3) == 0)) {
已提交
309 310 311
		/* Reject this request because invalid parameters */
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
J
James Smart 已提交
312 313
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
314
		return 0;
已提交
315 316 317 318
	}
	icmd = &cmdiocb->iocb;

	/* PLOGI chkparm OK */
319 320 321 322
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0114 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);
已提交
323

324
	if (vport->cfg_fcp_class == 2 && sp->cls2.classValid)
已提交
325
		ndlp->nlp_fcp_info |= CLASS2;
326
	else
已提交
327
		ndlp->nlp_fcp_info |= CLASS3;
J
James Smart 已提交
328

已提交
329 330 331 332 333 334 335 336 337 338 339 340 341
	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;

	/* no need to reg_login if we are already in one of these states */
342
	switch (ndlp->nlp_state) {
已提交
343 344 345 346 347 348 349
	case  NLP_STE_NPR_NODE:
		if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
			break;
	case  NLP_STE_REG_LOGIN_ISSUE:
	case  NLP_STE_PRLI_ISSUE:
	case  NLP_STE_UNMAPPED_NODE:
	case  NLP_STE_MAPPED_NODE:
350
		lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
351
		return 1;
已提交
352 353
	}

354 355
	if ((vport->fc_flag & FC_PT2PT) &&
	    !(vport->fc_flag & FC_PT2PT_PLOGI)) {
已提交
356
		/* rcv'ed PLOGI decides what our NPortId will be */
J
James Smart 已提交
357
		vport->fc_myDID = icmd->un.rcvels.parmRo;
已提交
358 359 360 361 362
		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;
363
		mbox->vport = vport;
364
		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
已提交
365
		if (rc == MBX_NOT_FINISHED) {
366
			mempool_free(mbox, phba->mbox_mem_pool);
已提交
367 368 369
			goto out;
		}

J
James Smart 已提交
370
		lpfc_can_disctmo(vport);
已提交
371 372
	}
	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
J
James Smart 已提交
373
	if (!mbox)
已提交
374 375
		goto out;

376 377
	rc = lpfc_reg_login(phba, vport->vpi, icmd->un.rcvels.remoteID,
			    (uint8_t *) sp, mbox, 0);
J
James Smart 已提交
378 379
	if (rc) {
		mempool_free(mbox, phba->mbox_mem_pool);
已提交
380 381 382 383 384 385 386
		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;
387 388 389 390
	/*
	 * mbox->context2 = lpfc_nlp_get(ndlp) deferred until mailbox
	 * command issued in lpfc_cmpl_els_acc().
	 */
J
James Smart 已提交
391 392
	mbox->vport = vport;
	spin_lock_irq(shost->host_lock);
393
	ndlp->nlp_flag |= (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI);
J
James Smart 已提交
394
	spin_unlock_irq(shost->host_lock);
已提交
395

J
James Smart 已提交
396 397 398 399 400 401 402 403 404 405 406
	/*
	 * 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 */
407
		lpfc_els_abort(phba, ndlp);
J
James Smart 已提交
408 409
	}

J
James Smart 已提交
410
	if ((vport->port_type == LPFC_NPIV_PORT &&
411
	     vport->cfg_restrict_login)) {
J
James Smart 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

		/* 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;
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
			ndlp, mbox);
		return 1;
	}
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

	/* If the remote NPort logs into us, before we can initiate
	 * discovery to them, cleanup the NPort from discovery accordingly.
	 */
	if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
		spin_lock_irq(shost->host_lock);
		ndlp->nlp_flag &= ~NLP_DELAY_TMO;
		spin_unlock_irq(shost->host_lock);
		del_timer_sync(&ndlp->nlp_delayfunc);
		ndlp->nlp_last_elscmd = 0;

		if (!list_empty(&ndlp->els_retry_evt.evt_listp))
			list_del_init(&ndlp->els_retry_evt.evt_listp);

		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);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

			if ((ndlp->nlp_flag & NLP_ADISC_SND) &&
				(vport->num_disc_nodes)) {
				/* Check to see if there are more
				 * ADISCs to be sent
				 */
				lpfc_more_adisc(vport);

				if ((vport->num_disc_nodes == 0) &&
					(vport->fc_npr_cnt))
					lpfc_els_disc_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);
				}
			}
			else if (vport->num_disc_nodes) {
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
				/* Check to see 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);
				}
			}
		}
	}

482
	lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, mbox);
483
	return 1;
已提交
484 485 486 487

out:
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
J
James Smart 已提交
488
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
489
	return 0;
已提交
490 491 492
}

static int
J
James Smart 已提交
493
lpfc_rcv_padisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
494 495
		struct lpfc_iocbq *cmdiocb)
{
J
James Smart 已提交
496
	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
已提交
497
	struct lpfc_dmabuf *pcmd;
J
James Smart 已提交
498 499
	struct serv_parm   *sp;
	struct lpfc_name   *pnn, *ppn;
已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	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 已提交
521
	if (icmd->ulpStatus == 0 && lpfc_check_adisc(vport, ndlp, pnn, ppn)) {
已提交
522
		if (cmd == ELS_CMD_ADISC) {
J
James Smart 已提交
523
			lpfc_els_rsp_adisc_acc(vport, cmdiocb, ndlp);
524
		} else {
J
James Smart 已提交
525
			lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp,
526
					 NULL);
已提交
527
		}
528
		return 1;
已提交
529 530 531 532 533 534
	}
	/* 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 已提交
535
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
已提交
536 537 538 539

	/* 1 sec timeout */
	mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);

J
James Smart 已提交
540
	spin_lock_irq(shost->host_lock);
已提交
541
	ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
542
	spin_unlock_irq(shost->host_lock);
543 544
	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
	ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
545
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
546
	return 0;
已提交
547 548 549
}

static int
J
James Smart 已提交
550 551
lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
	      struct lpfc_iocbq *cmdiocb, uint32_t els_cmd)
已提交
552
{
J
James Smart 已提交
553 554 555
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	/* Put ndlp in NPR state with 1 sec timeout for plogi, ACC logo */
已提交
556 557 558
	/* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
	 * PLOGIs during LOGO storms from a device.
	 */
J
James Smart 已提交
559
	spin_lock_irq(shost->host_lock);
已提交
560
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
561
	spin_unlock_irq(shost->host_lock);
562
	if (els_cmd == ELS_CMD_PRLO)
563
		lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
564
	else
565
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
566

567
	if (!(ndlp->nlp_type & NLP_FABRIC) ||
568
	    (ndlp->nlp_state == NLP_STE_ADISC_ISSUE)) {
已提交
569 570
		/* Only try to re-login if this is NOT a Fabric Node */
		mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
J
James Smart 已提交
571
		spin_lock_irq(shost->host_lock);
已提交
572
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
573
		spin_unlock_irq(shost->host_lock);
已提交
574

575
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
576
	}
577 578
	ndlp->nlp_prev_state = ndlp->nlp_state;
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
579

J
James Smart 已提交
580
	spin_lock_irq(shost->host_lock);
已提交
581
	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
582
	spin_unlock_irq(shost->host_lock);
已提交
583 584 585 586 587
	/* 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.
	 */
588
	return 0;
已提交
589 590 591
}

static void
J
James Smart 已提交
592 593
lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
	      struct lpfc_iocbq *cmdiocb)
已提交
594 595 596 597 598 599 600 601 602 603 604 605 606
{
	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));

	ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
607
	if (npr->prliType == PRLI_FCP_TYPE) {
已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621
		if (npr->initiatorFunc)
			ndlp->nlp_type |= NLP_FCP_INITIATOR;
		if (npr->targetFunc)
			ndlp->nlp_type |= NLP_FCP_TARGET;
		if (npr->Retry)
			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
	}
	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 已提交
622 623 624 625 626

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

已提交
627 628 629 630 631
		fc_remote_port_rolechg(rport, roles);
	}
}

static uint32_t
J
James Smart 已提交
632
lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
已提交
633
{
J
James Smart 已提交
634 635
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

636 637 638 639 640
	if (!ndlp->nlp_rpi) {
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
		return 0;
	}

已提交
641
	/* Check config parameter use-adisc or FCP-2 */
642
	if ((vport->cfg_use_adisc && (vport->fc_flag & FC_RSCN_MODE)) ||
643 644 645 646 647 648 649 650 651
	    ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
		spin_lock_irq(shost->host_lock);
		ndlp->nlp_flag |= NLP_NPR_ADISC;
		spin_unlock_irq(shost->host_lock);
		return 1;
	}
	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
	lpfc_unreg_rpi(vport, ndlp);
	return 0;
已提交
652 653 654
}

static uint32_t
J
James Smart 已提交
655 656
lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		  void *arg, uint32_t evt)
已提交
657
{
658 659 660 661 662
	lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
			 "0253 Illegal State Transition: node x%x "
			 "event x%x, state x%x Data: x%x x%x\n",
			 ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
			 ndlp->nlp_flag);
663
	return ndlp->nlp_state;
已提交
664 665
}

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
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,
			 "0253 Illegal State Transition: node x%x "
			 "event x%x, state x%x Data: x%x x%x\n",
			 ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
			 ndlp->nlp_flag);
	}
	return ndlp->nlp_state;
}

已提交
685 686 687
/* Start of Discovery State Machine routines */

static uint32_t
J
James Smart 已提交
688 689
lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
690 691 692 693 694
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
695
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
696
		return ndlp->nlp_state;
已提交
697
	}
698
	return NLP_STE_FREED_NODE;
已提交
699 700 701
}

static uint32_t
J
James Smart 已提交
702 703
lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
704
{
J
James Smart 已提交
705
	lpfc_issue_els_logo(vport, ndlp, 0);
706
	return ndlp->nlp_state;
已提交
707 708 709
}

static uint32_t
J
James Smart 已提交
710 711
lpfc_rcv_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
712
{
J
James Smart 已提交
713 714
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
715

J
James Smart 已提交
716
	spin_lock_irq(shost->host_lock);
已提交
717
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
718
	spin_unlock_irq(shost->host_lock);
719
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
720

721
	return ndlp->nlp_state;
已提交
722 723 724
}

static uint32_t
J
James Smart 已提交
725 726
lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
727
{
728
	return NLP_STE_FREED_NODE;
已提交
729 730 731
}

static uint32_t
J
James Smart 已提交
732 733
lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
734
{
735
	return NLP_STE_FREED_NODE;
已提交
736 737 738
}

static uint32_t
J
James Smart 已提交
739
lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
740 741
			   void *arg, uint32_t evt)
{
J
James Smart 已提交
742
	struct lpfc_hba   *phba = vport->phba;
已提交
743
	struct lpfc_iocbq *cmdiocb = arg;
J
James Smart 已提交
744 745 746
	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);
已提交
747 748 749 750 751 752 753 754 755
	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 已提交
756
	port_cmp = memcmp(&vport->fc_portname, &sp->portName,
757
			  sizeof(struct lpfc_name));
已提交
758 759 760 761 762 763

	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 已提交
764 765
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
766
	} else {
J
James Smart 已提交
767 768
		lpfc_rcv_plogi(vport, ndlp, cmdiocb);
	} /* If our portname was less */
已提交
769

770 771 772
	return ndlp->nlp_state;
}

773 774 775 776 777 778 779 780 781 782
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 已提交
783
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
784 785 786
	return ndlp->nlp_state;
}

787
static uint32_t
J
James Smart 已提交
788 789
lpfc_rcv_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
790
{
J
James Smart 已提交
791
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
792

793
				/* software abort outstanding PLOGI */
J
James Smart 已提交
794
	lpfc_els_abort(vport->phba, ndlp);
795

J
James Smart 已提交
796
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
797
	return ndlp->nlp_state;
已提交
798 799 800
}

static uint32_t
J
James Smart 已提交
801 802
lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
803
{
J
James Smart 已提交
804 805 806
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba   *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
807 808

	/* software abort outstanding PLOGI */
809
	lpfc_els_abort(phba, ndlp);
已提交
810 811

	if (evt == NLP_EVT_RCV_LOGO) {
812
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
813
	} else {
J
James Smart 已提交
814
		lpfc_issue_els_logo(vport, ndlp, 0);
已提交
815 816
	}

J
James Smart 已提交
817
	/* Put ndlp in npr state set plogi timer for 1 sec */
818
	mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
J
James Smart 已提交
819
	spin_lock_irq(shost->host_lock);
820
	ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
821
	spin_unlock_irq(shost->host_lock);
822 823
	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
J
James Smart 已提交
824
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
825

826
	return ndlp->nlp_state;
已提交
827 828 829
}

static uint32_t
J
James Smart 已提交
830 831 832
lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg,
已提交
833 834
			    uint32_t evt)
{
J
James Smart 已提交
835
	struct lpfc_hba    *phba = vport->phba;
836
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
J
James Smart 已提交
837
	struct lpfc_iocbq  *cmdiocb, *rspiocb;
838
	struct lpfc_dmabuf *pcmd, *prsp, *mp;
已提交
839 840 841 842 843 844 845 846 847
	uint32_t *lp;
	IOCB_t *irsp;
	struct serv_parm *sp;
	LPFC_MBOXQ_t *mbox;

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

	if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
848
		/* Recovery from PLOGI collision logic */
849
		return ndlp->nlp_state;
已提交
850 851 852 853 854 855 856 857 858
	}

	irsp = &rspiocb->iocb;

	if (irsp->ulpStatus)
		goto out;

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

J
James Smart 已提交
859
	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
已提交
860

J
James Smart 已提交
861
	lp = (uint32_t *) prsp->virt;
已提交
862
	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
863 864 865 866 867 868
	if (wwn_to_u64(sp->portName.u.wwn) == 0 ||
	    wwn_to_u64(sp->nodeName.u.wwn) == 0) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0142 PLOGI RSP: Invalid WWN.\n");
		goto out;
	}
J
James Smart 已提交
869
	if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3))
已提交
870 871
		goto out;
	/* PLOGI chkparm OK */
872 873 874 875
	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);
876
	if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
已提交
877
		ndlp->nlp_fcp_info |= CLASS2;
J
James Smart 已提交
878
	else
已提交
879
		ndlp->nlp_fcp_info |= CLASS3;
J
James Smart 已提交
880

已提交
881 882 883 884 885 886 887 888 889 890
	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 已提交
891
		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
已提交
892

J
James Smart 已提交
893
	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
894
	if (!mbox) {
895 896
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
			"0133 PLOGI: no memory for reg_login "
897 898 899
			"Data: x%x x%x x%x x%x\n",
			ndlp->nlp_DID, ndlp->nlp_state,
			ndlp->nlp_flag, ndlp->nlp_rpi);
已提交
900
		goto out;
901
	}
已提交
902

J
James Smart 已提交
903 904
	lpfc_unreg_rpi(vport, ndlp);

905 906
	if (lpfc_reg_login(phba, vport->vpi, irsp->un.elsreq64.remoteID,
			   (uint8_t *) sp, mbox, 0) == 0) {
907
		switch (ndlp->nlp_DID) {
已提交
908
		case NameServer_DID:
909
			mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
已提交
910 911
			break;
		case FDMI_DID:
912
			mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
已提交
913 914
			break;
		default:
915
			mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
已提交
916
		}
917
		mbox->context2 = lpfc_nlp_get(ndlp);
J
James Smart 已提交
918
		mbox->vport = vport;
919
		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
已提交
920
		    != MBX_NOT_FINISHED) {
J
James Smart 已提交
921 922
			lpfc_nlp_set_state(vport, ndlp,
					   NLP_STE_REG_LOGIN_ISSUE);
923
			return ndlp->nlp_state;
已提交
924
		}
925
		lpfc_nlp_put(ndlp);
926
		mp = (struct lpfc_dmabuf *) mbox->context1;
927 928
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
已提交
929
		mempool_free(mbox, phba->mbox_mem_pool);
930

931 932 933 934 935
		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);
已提交
936 937
	} else {
		mempool_free(mbox, phba->mbox_mem_pool);
938

939 940 941 942 943
		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);
已提交
944 945 946
	}


947 948 949
out:
	if (ndlp->nlp_DID == NameServer_DID) {
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
950 951
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0261 Cannot Register NameServer login\n");
952 953
	}

954
	spin_lock_irq(shost->host_lock);
955
	ndlp->nlp_flag |= NLP_DEFER_RM;
956
	spin_unlock_irq(shost->host_lock);
957
	return NLP_STE_FREED_NODE;
已提交
958 959
}

960 961 962 963 964 965 966 967 968 969 970 971 972 973
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)
{
	return ndlp->nlp_state;
}

已提交
974
static uint32_t
J
James Smart 已提交
975 976
lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
977
{
J
James Smart 已提交
978 979 980 981
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
982
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
983
		spin_unlock_irq(shost->host_lock);
984
		return ndlp->nlp_state;
J
James Smart 已提交
985
	} else {
986
		/* software abort outstanding PLOGI */
J
James Smart 已提交
987
		lpfc_els_abort(vport->phba, ndlp);
已提交
988

J
James Smart 已提交
989
		lpfc_drop_node(vport, ndlp);
990 991
		return NLP_STE_FREED_NODE;
	}
已提交
992 993 994
}

static uint32_t
J
James Smart 已提交
995 996 997 998
lpfc_device_recov_plogi_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
999
{
J
James Smart 已提交
1000 1001 1002
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

1003 1004 1005 1006 1007 1008
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
1009
	/* software abort outstanding PLOGI */
1010
	lpfc_els_abort(phba, ndlp);
已提交
1011

1012
	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
J
James Smart 已提交
1013
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1014
	spin_lock_irq(shost->host_lock);
1015
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1016
	spin_unlock_irq(shost->host_lock);
已提交
1017

1018
	return ndlp->nlp_state;
已提交
1019 1020 1021
}

static uint32_t
J
James Smart 已提交
1022 1023
lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1024
{
J
James Smart 已提交
1025
	struct lpfc_hba   *phba = vport->phba;
已提交
1026 1027 1028
	struct lpfc_iocbq *cmdiocb;

	/* software abort outstanding ADISC */
1029
	lpfc_els_abort(phba, ndlp);
已提交
1030 1031 1032

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1033
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb))
1034
		return ndlp->nlp_state;
J
James Smart 已提交
1035

1036
	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1037 1038
	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
已提交
1039

1040
	return ndlp->nlp_state;
已提交
1041 1042 1043
}

static uint32_t
J
James Smart 已提交
1044 1045
lpfc_rcv_prli_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1046
{
J
James Smart 已提交
1047
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1048

J
James Smart 已提交
1049
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1050
	return ndlp->nlp_state;
已提交
1051 1052 1053
}

static uint32_t
J
James Smart 已提交
1054 1055
lpfc_rcv_logo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1056
{
J
James Smart 已提交
1057
	struct lpfc_hba *phba = vport->phba;
已提交
1058 1059 1060 1061 1062
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

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

J
James Smart 已提交
1065
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1066
	return ndlp->nlp_state;
已提交
1067 1068 1069
}

static uint32_t
J
James Smart 已提交
1070 1071 1072
lpfc_rcv_padisc_adisc_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1073 1074 1075 1076 1077
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1078
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1079
	return ndlp->nlp_state;
已提交
1080 1081 1082
}

static uint32_t
J
James Smart 已提交
1083 1084
lpfc_rcv_prlo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1085 1086 1087 1088 1089 1090
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

	/* Treat like rcv logo */
J
James Smart 已提交
1091
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1092
	return ndlp->nlp_state;
已提交
1093 1094 1095
}

static uint32_t
J
James Smart 已提交
1096 1097 1098
lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1099
{
J
James Smart 已提交
1100 1101
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba   *phba = vport->phba;
已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
	struct lpfc_iocbq *cmdiocb, *rspiocb;
	IOCB_t *irsp;
	ADISC *ap;

	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) ||
1113
	    (!lpfc_check_adisc(vport, ndlp, &ap->nodeName, &ap->portName))) {
已提交
1114 1115
		/* 1 sec timeout */
		mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
J
James Smart 已提交
1116
		spin_lock_irq(shost->host_lock);
已提交
1117
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1118
		spin_unlock_irq(shost->host_lock);
1119
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
已提交
1120

J
James Smart 已提交
1121 1122
		memset(&ndlp->nlp_nodename, 0, sizeof(struct lpfc_name));
		memset(&ndlp->nlp_portname, 0, sizeof(struct lpfc_name));
已提交
1123

1124
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1125 1126
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		lpfc_unreg_rpi(vport, ndlp);
1127
		return ndlp->nlp_state;
已提交
1128
	}
1129

1130
	if (ndlp->nlp_type & NLP_FCP_TARGET) {
1131
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1132
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1133
	} else {
1134
		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1135
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1136
	}
1137
	return ndlp->nlp_state;
已提交
1138 1139 1140
}

static uint32_t
J
James Smart 已提交
1141 1142
lpfc_device_rm_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1143
{
J
James Smart 已提交
1144 1145 1146 1147
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1148
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1149
		spin_unlock_irq(shost->host_lock);
1150
		return ndlp->nlp_state;
J
James Smart 已提交
1151
	} else {
1152
		/* software abort outstanding ADISC */
J
James Smart 已提交
1153
		lpfc_els_abort(vport->phba, ndlp);
已提交
1154

J
James Smart 已提交
1155
		lpfc_drop_node(vport, ndlp);
1156 1157
		return NLP_STE_FREED_NODE;
	}
已提交
1158 1159 1160
}

static uint32_t
J
James Smart 已提交
1161 1162 1163 1164
lpfc_device_recov_adisc_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
1165
{
J
James Smart 已提交
1166 1167 1168
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

1169 1170 1171 1172 1173 1174
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
1175
	/* software abort outstanding ADISC */
1176
	lpfc_els_abort(phba, ndlp);
已提交
1177

1178
	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
J
James Smart 已提交
1179 1180
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
1181
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1182
	spin_unlock_irq(shost->host_lock);
1183
	lpfc_disc_set_adisc(vport, ndlp);
1184
	return ndlp->nlp_state;
已提交
1185 1186 1187
}

static uint32_t
J
James Smart 已提交
1188 1189 1190
lpfc_rcv_plogi_reglogin_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
已提交
1191 1192
			      uint32_t evt)
{
J
James Smart 已提交
1193
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1194

J
James Smart 已提交
1195
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1196
	return ndlp->nlp_state;
已提交
1197 1198 1199
}

static uint32_t
J
James Smart 已提交
1200 1201 1202
lpfc_rcv_prli_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1203 1204
			     uint32_t evt)
{
J
James Smart 已提交
1205
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1206

J
James Smart 已提交
1207
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1208
	return ndlp->nlp_state;
已提交
1209 1210 1211
}

static uint32_t
J
James Smart 已提交
1212 1213 1214
lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1215 1216
			     uint32_t evt)
{
J
James Smart 已提交
1217 1218
	struct lpfc_hba   *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1219 1220 1221
	LPFC_MBOXQ_t	  *mb;
	LPFC_MBOXQ_t	  *nextmb;
	struct lpfc_dmabuf *mp;
已提交
1222 1223 1224

	cmdiocb = (struct lpfc_iocbq *) arg;

1225 1226 1227 1228
	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
	if ((mb = phba->sli.mbox_active)) {
		if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
		   (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1229
			lpfc_nlp_put(ndlp);
1230 1231 1232 1233 1234
			mb->context2 = NULL;
			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
		}
	}

J
James Smart 已提交
1235
	spin_lock_irq(&phba->hbalock);
1236 1237 1238 1239 1240
	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
		if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
		   (ndlp == (struct lpfc_nodelist *) mb->context2)) {
			mp = (struct lpfc_dmabuf *) (mb->context1);
			if (mp) {
1241
				__lpfc_mbuf_free(phba, mp->virt, mp->phys);
1242 1243
				kfree(mp);
			}
1244
			lpfc_nlp_put(ndlp);
1245 1246 1247 1248
			list_del(&mb->list);
			mempool_free(mb, phba->mbox_mem_pool);
		}
	}
J
James Smart 已提交
1249
	spin_unlock_irq(&phba->hbalock);
1250

J
James Smart 已提交
1251
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1252
	return ndlp->nlp_state;
已提交
1253 1254 1255
}

static uint32_t
J
James Smart 已提交
1256 1257 1258
lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport *vport,
			       struct lpfc_nodelist *ndlp,
			       void *arg,
已提交
1259 1260
			       uint32_t evt)
{
J
James Smart 已提交
1261
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1262

J
James Smart 已提交
1263
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1264
	return ndlp->nlp_state;
已提交
1265 1266 1267
}

static uint32_t
J
James Smart 已提交
1268 1269 1270
lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
已提交
1271 1272 1273 1274 1275
			     uint32_t evt)
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;
1276
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1277
	return ndlp->nlp_state;
已提交
1278 1279 1280
}

static uint32_t
J
James Smart 已提交
1281 1282 1283 1284
lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
				  struct lpfc_nodelist *ndlp,
				  void *arg,
				  uint32_t evt)
已提交
1285
{
J
James Smart 已提交
1286 1287 1288 1289
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
	MAILBOX_t *mb = &pmb->mb;
	uint32_t did  = mb->un.varWords[1];
已提交
1290 1291 1292

	if (mb->mbxStatus) {
		/* RegLogin failed */
1293 1294
		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
				"0246 RegLogin failed Data: x%x x%x x%x\n",
J
James Smart 已提交
1295
				did, mb->mbxStatus, vport->port_state);
1296 1297 1298 1299 1300
		/*
		 * If RegLogin failed due to lack of HBA resources do not
		 * retry discovery.
		 */
		if (mb->mbxStatus == MBXERR_RPI_FULL) {
1301 1302
			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1303 1304 1305
			return ndlp->nlp_state;
		}

J
James Smart 已提交
1306
		/* Put ndlp in npr state set plogi timer for 1 sec */
已提交
1307
		mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
J
James Smart 已提交
1308
		spin_lock_irq(shost->host_lock);
已提交
1309
		ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1310
		spin_unlock_irq(shost->host_lock);
1311
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
已提交
1312

J
James Smart 已提交
1313
		lpfc_issue_els_logo(vport, ndlp, 0);
1314
		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1315
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1316
		return ndlp->nlp_state;
已提交
1317 1318 1319 1320 1321 1322
	}

	ndlp->nlp_rpi = mb->un.varWords[0];

	/* Only if we are not a fabric nport do we issue PRLI */
	if (!(ndlp->nlp_type & NLP_FABRIC)) {
1323
		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1324 1325
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
		lpfc_issue_els_prli(vport, ndlp, 0);
已提交
1326
	} else {
1327
		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1328
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
已提交
1329
	}
1330
	return ndlp->nlp_state;
已提交
1331 1332 1333
}

static uint32_t
J
James Smart 已提交
1334 1335 1336
lpfc_device_rm_reglogin_issue(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
已提交
1337 1338
			      uint32_t evt)
{
J
James Smart 已提交
1339 1340 1341 1342
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1343
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1344
		spin_unlock_irq(shost->host_lock);
1345
		return ndlp->nlp_state;
J
James Smart 已提交
1346 1347
	} else {
		lpfc_drop_node(vport, ndlp);
1348 1349
		return NLP_STE_FREED_NODE;
	}
已提交
1350 1351 1352
}

static uint32_t
J
James Smart 已提交
1353 1354 1355 1356
lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
				 struct lpfc_nodelist *ndlp,
				 void *arg,
				 uint32_t evt)
已提交
1357
{
J
James Smart 已提交
1358 1359
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

1360 1361 1362 1363 1364 1365
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

1366
	ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
J
James Smart 已提交
1367 1368
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
1369
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1370
	spin_unlock_irq(shost->host_lock);
1371
	lpfc_disc_set_adisc(vport, ndlp);
1372
	return ndlp->nlp_state;
已提交
1373 1374 1375
}

static uint32_t
J
James Smart 已提交
1376 1377
lpfc_rcv_plogi_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1378 1379 1380 1381 1382
{
	struct lpfc_iocbq *cmdiocb;

	cmdiocb = (struct lpfc_iocbq *) arg;

J
James Smart 已提交
1383
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1384
	return ndlp->nlp_state;
已提交
1385 1386 1387
}

static uint32_t
J
James Smart 已提交
1388 1389
lpfc_rcv_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1390
{
J
James Smart 已提交
1391
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1392

J
James Smart 已提交
1393
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1394
	return ndlp->nlp_state;
已提交
1395 1396 1397
}

static uint32_t
J
James Smart 已提交
1398 1399
lpfc_rcv_logo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1400
{
J
James Smart 已提交
1401
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1402 1403

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

J
James Smart 已提交
1406
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1407
	return ndlp->nlp_state;
已提交
1408 1409 1410
}

static uint32_t
J
James Smart 已提交
1411 1412
lpfc_rcv_padisc_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1413
{
J
James Smart 已提交
1414
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1415

J
James Smart 已提交
1416
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1417
	return ndlp->nlp_state;
已提交
1418 1419 1420 1421 1422 1423 1424 1425
}

/* 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 已提交
1426 1427
lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1428
{
J
James Smart 已提交
1429
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1430

1431
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1432
	return ndlp->nlp_state;
已提交
1433 1434 1435
}

static uint32_t
J
James Smart 已提交
1436 1437
lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1438
{
1439
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
1440
	struct lpfc_iocbq *cmdiocb, *rspiocb;
J
James Smart 已提交
1441
	struct lpfc_hba   *phba = vport->phba;
已提交
1442 1443 1444 1445 1446 1447 1448 1449 1450
	IOCB_t *irsp;
	PRLI *npr;

	cmdiocb = (struct lpfc_iocbq *) arg;
	rspiocb = cmdiocb->context_un.rsp_iocb;
	npr = (PRLI *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus) {
J
James Smart 已提交
1451
		if ((vport->port_type == LPFC_NPIV_PORT) &&
1452
		    vport->cfg_restrict_login) {
J
James Smart 已提交
1453 1454
			goto out;
		}
1455
		ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
J
James Smart 已提交
1456
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1457
		return ndlp->nlp_state;
已提交
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
	}

	/* Check out PRLI rsp */
	ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
	if ((npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
	    (npr->prliType == PRLI_FCP_TYPE)) {
		if (npr->initiatorFunc)
			ndlp->nlp_type |= NLP_FCP_INITIATOR;
		if (npr->targetFunc)
			ndlp->nlp_type |= NLP_FCP_TARGET;
		if (npr->Retry)
			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
	}
1472 1473
	if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
	    (vport->port_type == LPFC_NPIV_PORT) &&
1474
	     vport->cfg_restrict_login) {
J
James Smart 已提交
1475
out:
1476 1477 1478 1479 1480 1481
		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;
1482
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1483 1484
		return ndlp->nlp_state;
	}
已提交
1485

1486
	ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1487 1488 1489 1490
	if (ndlp->nlp_type & NLP_FCP_TARGET)
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
	else
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1491
	return ndlp->nlp_state;
已提交
1492 1493 1494
}

/*! lpfc_device_rm_prli_issue
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
 *
 * \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.
 *
 */

已提交
1512
static uint32_t
J
James Smart 已提交
1513 1514
lpfc_device_rm_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1515
{
J
James Smart 已提交
1516 1517 1518 1519
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
		spin_lock_irq(shost->host_lock);
1520
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1521
		spin_unlock_irq(shost->host_lock);
1522
		return ndlp->nlp_state;
J
James Smart 已提交
1523
	} else {
1524
		/* software abort outstanding PLOGI */
J
James Smart 已提交
1525
		lpfc_els_abort(vport->phba, ndlp);
已提交
1526

J
James Smart 已提交
1527
		lpfc_drop_node(vport, ndlp);
1528 1529
		return NLP_STE_FREED_NODE;
	}
已提交
1530 1531 1532 1533
}


/*! lpfc_device_recov_prli_issue
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
 *
 * \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.
 */
已提交
1549
static uint32_t
J
James Smart 已提交
1550 1551 1552 1553
lpfc_device_recov_prli_issue(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
			     uint32_t evt)
已提交
1554
{
J
James Smart 已提交
1555 1556 1557
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;

1558 1559 1560 1561 1562 1563
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

已提交
1564
	/* software abort outstanding PRLI */
1565
	lpfc_els_abort(phba, ndlp);
已提交
1566

1567
	ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
J
James Smart 已提交
1568 1569
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
1570
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1571
	spin_unlock_irq(shost->host_lock);
1572
	lpfc_disc_set_adisc(vport, ndlp);
1573
	return ndlp->nlp_state;
已提交
1574 1575 1576
}

static uint32_t
J
James Smart 已提交
1577 1578
lpfc_rcv_plogi_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1579
{
J
James Smart 已提交
1580
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1581

J
James Smart 已提交
1582
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1583
	return ndlp->nlp_state;
已提交
1584 1585 1586
}

static uint32_t
J
James Smart 已提交
1587 1588
lpfc_rcv_prli_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1589
{
J
James Smart 已提交
1590
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1591

J
James Smart 已提交
1592 1593
	lpfc_rcv_prli(vport, ndlp, cmdiocb);
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1594
	return ndlp->nlp_state;
已提交
1595 1596 1597
}

static uint32_t
J
James Smart 已提交
1598 1599
lpfc_rcv_logo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1600
{
J
James Smart 已提交
1601
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1602

J
James Smart 已提交
1603
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1604
	return ndlp->nlp_state;
已提交
1605 1606 1607
}

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

J
James Smart 已提交
1613
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1614
	return ndlp->nlp_state;
已提交
1615 1616 1617
}

static uint32_t
J
James Smart 已提交
1618 1619
lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
已提交
1620
{
J
James Smart 已提交
1621
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1622

1623
	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1624
	return ndlp->nlp_state;
已提交
1625 1626 1627
}

static uint32_t
J
James Smart 已提交
1628 1629 1630 1631
lpfc_device_recov_unmap_node(struct lpfc_vport *vport,
			     struct lpfc_nodelist *ndlp,
			     void *arg,
			     uint32_t evt)
已提交
1632
{
J
James Smart 已提交
1633 1634
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

1635
	ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
J
James Smart 已提交
1636 1637
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
1638
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1639 1640
	spin_unlock_irq(shost->host_lock);
	lpfc_disc_set_adisc(vport, ndlp);
已提交
1641

1642
	return ndlp->nlp_state;
已提交
1643 1644 1645
}

static uint32_t
J
James Smart 已提交
1646 1647
lpfc_rcv_plogi_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   void *arg, uint32_t evt)
已提交
1648
{
J
James Smart 已提交
1649
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1650

J
James Smart 已提交
1651
	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1652
	return ndlp->nlp_state;
已提交
1653 1654 1655
}

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

J
James Smart 已提交
1661
	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1662
	return ndlp->nlp_state;
已提交
1663 1664 1665
}

static uint32_t
J
James Smart 已提交
1666 1667
lpfc_rcv_logo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1668
{
J
James Smart 已提交
1669
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1670

J
James Smart 已提交
1671
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1672
	return ndlp->nlp_state;
已提交
1673 1674 1675
}

static uint32_t
J
James Smart 已提交
1676 1677 1678
lpfc_rcv_padisc_mapped_node(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1679
{
J
James Smart 已提交
1680
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1681

J
James Smart 已提交
1682
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1683
	return ndlp->nlp_state;
已提交
1684 1685 1686
}

static uint32_t
J
James Smart 已提交
1687 1688
lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  void *arg, uint32_t evt)
已提交
1689
{
J
James Smart 已提交
1690 1691
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1692 1693

	/* flush the target */
1694 1695
	lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
			    ndlp->nlp_sid, 0, LPFC_CTX_TGT);
已提交
1696 1697

	/* Treat like rcv logo */
J
James Smart 已提交
1698
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1699
	return ndlp->nlp_state;
已提交
1700 1701 1702
}

static uint32_t
J
James Smart 已提交
1703 1704 1705 1706
lpfc_device_recov_mapped_node(struct lpfc_vport *vport,
			      struct lpfc_nodelist *ndlp,
			      void *arg,
			      uint32_t evt)
已提交
1707
{
J
James Smart 已提交
1708 1709
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

1710
	ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
J
James Smart 已提交
1711 1712
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
	spin_lock_irq(shost->host_lock);
1713
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1714 1715
	spin_unlock_irq(shost->host_lock);
	lpfc_disc_set_adisc(vport, ndlp);
1716
	return ndlp->nlp_state;
已提交
1717 1718 1719
}

static uint32_t
J
James Smart 已提交
1720 1721
lpfc_rcv_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
1722
{
J
James Smart 已提交
1723 1724
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb  = (struct lpfc_iocbq *) arg;
已提交
1725 1726

	/* Ignore PLOGI if we have an outstanding LOGO */
J
James Smart 已提交
1727
	if (ndlp->nlp_flag & (NLP_LOGO_SND | NLP_LOGO_ACC)) {
1728
		return ndlp->nlp_state;
已提交
1729 1730
	}

J
James Smart 已提交
1731 1732
	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
		spin_lock_irq(shost->host_lock);
1733
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
1734
		spin_unlock_irq(shost->host_lock);
1735
		return ndlp->nlp_state;
已提交
1736 1737 1738 1739
	}

	/* send PLOGI immediately, move to PLOGI issue state */
	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1740
		ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
1741 1742
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
		lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
1743
	}
1744

1745
	return ndlp->nlp_state;
已提交
1746 1747 1748
}

static uint32_t
J
James Smart 已提交
1749 1750
lpfc_rcv_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		       void *arg, uint32_t evt)
已提交
1751
{
J
James Smart 已提交
1752 1753 1754
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
	struct ls_rjt     stat;
已提交
1755 1756 1757 1758

	memset(&stat, 0, sizeof (struct ls_rjt));
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
J
James Smart 已提交
1759
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
已提交
1760 1761 1762

	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
J
James Smart 已提交
1763
			spin_lock_irq(shost->host_lock);
1764
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1765
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
1766 1767 1768
			spin_unlock_irq(shost->host_lock);
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
已提交
1769
		} else {
1770
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
1771 1772
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
1773 1774
		}
	}
1775
	return ndlp->nlp_state;
已提交
1776 1777 1778
}

static uint32_t
J
James Smart 已提交
1779 1780
lpfc_rcv_logo_npr_node(struct lpfc_vport *vport,  struct lpfc_nodelist *ndlp,
		       void *arg, uint32_t evt)
已提交
1781
{
J
James Smart 已提交
1782
	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
已提交
1783

J
James Smart 已提交
1784
	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1785
	return ndlp->nlp_state;
已提交
1786 1787 1788
}

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

J
James Smart 已提交
1794
	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
已提交
1795

J
James Smart 已提交
1796 1797 1798 1799 1800
	/*
	 * 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 已提交
1801
	if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
1802
	    !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
已提交
1803
		if (ndlp->nlp_flag & NLP_NPR_ADISC) {
1804
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1805
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
1806 1807
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
已提交
1808
		} else {
1809
			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
1810 1811
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
1812 1813
		}
	}
1814
	return ndlp->nlp_state;
已提交
1815 1816 1817
}

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

J
James Smart 已提交
1824
	spin_lock_irq(shost->host_lock);
1825
	ndlp->nlp_flag |= NLP_LOGO_ACC;
J
James Smart 已提交
1826
	spin_unlock_irq(shost->host_lock);
1827

1828
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
1829

J
James Smart 已提交
1830
	if ((ndlp->nlp_flag & NLP_DELAY_TMO) == 0) {
1831
		mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
J
James Smart 已提交
1832
		spin_lock_irq(shost->host_lock);
1833 1834
		ndlp->nlp_flag |= NLP_DELAY_TMO;
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
1835
		spin_unlock_irq(shost->host_lock);
1836
		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1837
	} else {
J
James Smart 已提交
1838
		spin_lock_irq(shost->host_lock);
1839
		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
1840
		spin_unlock_irq(shost->host_lock);
已提交
1841
	}
1842 1843
	return ndlp->nlp_state;
}
已提交
1844

1845
static uint32_t
J
James Smart 已提交
1846 1847
lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
1848 1849
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
1850
	IOCB_t *irsp;
1851 1852 1853

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

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus) {
1857
		ndlp->nlp_flag |= NLP_DEFER_RM;
1858 1859
		return NLP_STE_FREED_NODE;
	}
1860 1861 1862 1863
	return ndlp->nlp_state;
}

static uint32_t
J
James Smart 已提交
1864 1865
lpfc_cmpl_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
1866 1867
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
1868
	IOCB_t *irsp;
1869 1870 1871

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

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
J
James Smart 已提交
1875
		lpfc_drop_node(vport, ndlp);
1876 1877
		return NLP_STE_FREED_NODE;
	}
1878
	return ndlp->nlp_state;
已提交
1879 1880 1881
}

static uint32_t
J
James Smart 已提交
1882 1883
lpfc_cmpl_logo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
1884
{
J
James Smart 已提交
1885
	lpfc_unreg_rpi(vport, ndlp);
已提交
1886
	/* This routine does nothing, just return the current state */
1887 1888 1889 1890
	return ndlp->nlp_state;
}

static uint32_t
J
James Smart 已提交
1891 1892
lpfc_cmpl_adisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			 void *arg, uint32_t evt)
1893 1894
{
	struct lpfc_iocbq *cmdiocb, *rspiocb;
1895
	IOCB_t *irsp;
1896 1897 1898

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

	irsp = &rspiocb->iocb;
	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
J
James Smart 已提交
1902
		lpfc_drop_node(vport, ndlp);
1903 1904
		return NLP_STE_FREED_NODE;
	}
1905
	return ndlp->nlp_state;
已提交
1906 1907 1908
}

static uint32_t
J
James Smart 已提交
1909 1910 1911
lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
			    struct lpfc_nodelist *ndlp,
			    void *arg, uint32_t evt)
已提交
1912
{
J
James Smart 已提交
1913 1914
	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
	MAILBOX_t    *mb = &pmb->mb;
已提交
1915

1916 1917
	if (!mb->mbxStatus)
		ndlp->nlp_rpi = mb->un.varWords[0];
1918 1919
	else {
		if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
J
James Smart 已提交
1920
			lpfc_drop_node(vport, ndlp);
1921 1922 1923
			return NLP_STE_FREED_NODE;
		}
	}
1924
	return ndlp->nlp_state;
已提交
1925 1926 1927
}

static uint32_t
J
James Smart 已提交
1928 1929
lpfc_device_rm_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
1930
{
J
James Smart 已提交
1931 1932
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

1933
	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
J
James Smart 已提交
1934
		spin_lock_irq(shost->host_lock);
1935
		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
J
James Smart 已提交
1936
		spin_unlock_irq(shost->host_lock);
1937 1938
		return ndlp->nlp_state;
	}
J
James Smart 已提交
1939
	lpfc_drop_node(vport, ndlp);
1940
	return NLP_STE_FREED_NODE;
已提交
1941 1942 1943
}

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

1949 1950 1951 1952 1953 1954
	/* Don't do anything that will mess up processing of the
	 * previous RSCN.
	 */
	if (vport->fc_flag & FC_RSCN_DEFERRED)
		return ndlp->nlp_state;

J
James Smart 已提交
1955
	spin_lock_irq(shost->host_lock);
1956
	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
J
James Smart 已提交
1957
	spin_unlock_irq(shost->host_lock);
1958
	if (ndlp->nlp_flag & NLP_DELAY_TMO) {
J
James Smart 已提交
1959
		lpfc_cancel_retry_delay_tmo(vport, ndlp);
1960
	}
1961
	return ndlp->nlp_state;
已提交
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
}


/* 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
1997
 * lists will receive a DEVICE_RECOVERY event. If the linkdown or devloss timers
已提交
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
 * 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 已提交
2022
     (struct lpfc_vport *, struct lpfc_nodelist *, void *, uint32_t) = {
已提交
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
	/* 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       */
	lpfc_disc_illegal,		/* DEVICE_RECOVERY */

	lpfc_rcv_plogi_plogi_issue,	/* RCV_PLOGI   PLOGI_ISSUE    */
2039
	lpfc_rcv_prli_plogi_issue,	/* RCV_PRLI        */
2040
	lpfc_rcv_logo_plogi_issue,	/* RCV_LOGO        */
已提交
2041 2042 2043 2044 2045
	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       */
2046
	lpfc_cmpl_logo_plogi_issue,	/* CMPL_LOGO       */
已提交
2047
	lpfc_disc_illegal,		/* CMPL_ADISC      */
2048
	lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
已提交
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
	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        */
2072
	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
已提交
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
	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        */
2086
	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
已提交
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
	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 */

	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        */
2128 2129
	lpfc_cmpl_plogi_npr_node,	/* CMPL_PLOGI      */
	lpfc_cmpl_prli_npr_node,	/* CMPL_PRLI       */
已提交
2130
	lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
2131
	lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
已提交
2132 2133 2134 2135 2136 2137
	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 已提交
2138 2139
lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			void *arg, uint32_t evt)
已提交
2140 2141
{
	uint32_t cur_state, rc;
J
James Smart 已提交
2142
	uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
已提交
2143 2144
			 uint32_t);

2145
	lpfc_nlp_get(ndlp);
已提交
2146 2147 2148
	cur_state = ndlp->nlp_state;

	/* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
2149 2150 2151 2152
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0211 DSM in event x%x on NPort x%x in "
			 "state %d Data: x%x\n",
			 evt, ndlp->nlp_DID, cur_state, ndlp->nlp_flag);
已提交
2153

J
James Smart 已提交
2154 2155 2156 2157
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
		 "DSM in:          evt:%d ste:%d did:x%x",
		evt, cur_state, ndlp->nlp_DID);

已提交
2158
	func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
J
James Smart 已提交
2159
	rc = (func) (vport, ndlp, arg, evt);
已提交
2160 2161

	/* DSM out state <rc> on NPort <nlp_DID> */
2162 2163 2164
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0212 DSM out state %d on NPort x%x Data: x%x\n",
			 rc, ndlp->nlp_DID, ndlp->nlp_flag);
已提交
2165

J
James Smart 已提交
2166 2167 2168 2169
	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);

2170
	lpfc_nlp_put(ndlp);
已提交
2171

2172
	return rc;
已提交
2173
}