lpfc_els.c 93.2 KB
Newer Older
已提交
1 2
/*******************************************************************
 * This file is part of the Emulex Linux Device Driver for         *
3
 * Fibre Channel Host Bus Adapters.                                *
4
 * Copyright (C) 2004-2006 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#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"

static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
			  struct lpfc_iocbq *);
static int lpfc_max_els_tries = 3;

static int
lpfc_els_chk_latt(struct lpfc_hba * phba)
{
	struct lpfc_sli *psli;
	LPFC_MBOXQ_t *mbox;
	uint32_t ha_copy;
	int rc;

	psli = &phba->sli;

	if ((phba->hba_state >= LPFC_HBA_READY) ||
	    (phba->hba_state == LPFC_LINK_DOWN))
		return 0;

	/* Read the HBA Host Attention Register */
	spin_lock_irq(phba->host->host_lock);
	ha_copy = readl(phba->HAregaddr);
	spin_unlock_irq(phba->host->host_lock);

	if (!(ha_copy & HA_LATT))
		return 0;

	/* Pending Link Event during Discovery */
	lpfc_printf_log(phba, KERN_WARNING, LOG_DISCOVERY,
			"%d:0237 Pending Link Event during "
			"Discovery: State x%x\n",
			phba->brd_no, phba->hba_state);

	/* CLEAR_LA should re-enable link attention events and
	 * we should then imediately take a LATT event. The
	 * LATT processing should call lpfc_linkdown() which
	 * will cleanup any left over in-progress discovery
	 * events.
	 */
	spin_lock_irq(phba->host->host_lock);
	phba->fc_flag |= FC_ABORT_DISCOVERY;
	spin_unlock_irq(phba->host->host_lock);

	if (phba->hba_state != LPFC_CLEAR_LA) {
		if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
			phba->hba_state = LPFC_CLEAR_LA;
			lpfc_clear_la(phba, mbox);
			mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
			rc = lpfc_sli_issue_mbox (phba, mbox,
						  (MBX_NOWAIT | MBX_STOP_IOCB));
			if (rc == MBX_NOT_FINISHED) {
				mempool_free(mbox, phba->mbox_mem_pool);
				phba->hba_state = LPFC_HBA_ERROR;
			}
		}
	}

95
	return 1;
已提交
96 97 98 99

}

static struct lpfc_iocbq *
100 101 102
lpfc_prep_els_iocb(struct lpfc_hba * phba, uint8_t expectRsp,
		   uint16_t cmdSize, uint8_t retry, struct lpfc_nodelist * ndlp,
		   uint32_t did, uint32_t elscmd)
已提交
103 104
{
	struct lpfc_sli_ring *pring;
105
	struct lpfc_iocbq *elsiocb;
已提交
106 107 108 109 110 111 112 113 114 115 116
	struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
	struct ulp_bde64 *bpl;
	IOCB_t *icmd;

	pring = &phba->sli.ring[LPFC_ELS_RING];

	if (phba->hba_state < LPFC_LINK_UP)
		return  NULL;

	/* Allocate buffer for  command iocb */
	spin_lock_irq(phba->host->host_lock);
117
	elsiocb = lpfc_sli_get_iocbq(phba);
已提交
118 119 120 121 122 123 124 125 126 127 128
	spin_unlock_irq(phba->host->host_lock);

	if (elsiocb == NULL)
		return NULL;
	icmd = &elsiocb->iocb;

	/* fill in BDEs for command */
	/* Allocate buffer for command payload */
	if (((pcmd = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL)) == 0) ||
	    ((pcmd->virt = lpfc_mbuf_alloc(phba,
					   MEM_PRI, &(pcmd->phys))) == 0)) {
J
Jesper Juhl 已提交
129
		kfree(pcmd);
已提交
130

131 132 133
		spin_lock_irq(phba->host->host_lock);
		lpfc_sli_release_iocbq(phba, elsiocb);
		spin_unlock_irq(phba->host->host_lock);
已提交
134 135 136 137 138 139 140 141 142 143 144 145
		return NULL;
	}

	INIT_LIST_HEAD(&pcmd->list);

	/* Allocate buffer for response payload */
	if (expectRsp) {
		prsp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
		if (prsp)
			prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
						     &prsp->phys);
		if (prsp == 0 || prsp->virt == 0) {
J
Jesper Juhl 已提交
146
			kfree(prsp);
已提交
147 148
			lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
			kfree(pcmd);
149 150 151
			spin_lock_irq(phba->host->host_lock);
			lpfc_sli_release_iocbq(phba, elsiocb);
			spin_unlock_irq(phba->host->host_lock);
已提交
152 153 154 155 156 157 158 159 160 161 162 163 164
			return NULL;
		}
		INIT_LIST_HEAD(&prsp->list);
	} else {
		prsp = NULL;
	}

	/* Allocate buffer for Buffer ptr list */
	pbuflist = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
	if (pbuflist)
	    pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
					     &pbuflist->phys);
	if (pbuflist == 0 || pbuflist->virt == 0) {
165 166 167
		spin_lock_irq(phba->host->host_lock);
		lpfc_sli_release_iocbq(phba, elsiocb);
		spin_unlock_irq(phba->host->host_lock);
已提交
168 169 170 171
		lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
		lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
		kfree(pcmd);
		kfree(prsp);
J
Jesper Juhl 已提交
172
		kfree(pbuflist);
已提交
173 174 175 176 177 178 179 180 181 182
		return NULL;
	}

	INIT_LIST_HEAD(&pbuflist->list);

	icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
	icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
	icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BDL;
	if (expectRsp) {
		icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
183
		icmd->un.elsreq64.remoteID = did;	/* DID */
已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
		icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
	} else {
		icmd->un.elsreq64.bdl.bdeSize = sizeof (struct ulp_bde64);
		icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
	}

	icmd->ulpBdeCount = 1;
	icmd->ulpLe = 1;
	icmd->ulpClass = CLASS3;

	bpl = (struct ulp_bde64 *) pbuflist->virt;
	bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
	bpl->tus.f.bdeSize = cmdSize;
	bpl->tus.f.bdeFlags = 0;
	bpl->tus.w = le32_to_cpu(bpl->tus.w);

	if (expectRsp) {
		bpl++;
		bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
		bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
		bpl->tus.f.bdeSize = FCELSSIZE;
		bpl->tus.f.bdeFlags = BUFF_USE_RCV;
		bpl->tus.w = le32_to_cpu(bpl->tus.w);
	}

	/* Save for completion so we can release these resources */
	elsiocb->context1 = (uint8_t *) ndlp;
	elsiocb->context2 = (uint8_t *) pcmd;
	elsiocb->context3 = (uint8_t *) pbuflist;
	elsiocb->retry = retry;
	elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;

	if (prsp) {
		list_add(&prsp->list, &pcmd->list);
	}

	if (expectRsp) {
		/* Xmit ELS command <elsCmd> to remote NPORT <did> */
		lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
				"%d:0116 Xmit ELS command x%x to remote "
				"NPORT x%x Data: x%x x%x\n",
				phba->brd_no, elscmd,
227
				did, icmd->ulpIoTag, phba->hba_state);
已提交
228 229 230 231 232 233 234 235 236
	} else {
		/* Xmit ELS response <elsCmd> to remote NPORT <did> */
		lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
				"%d:0117 Xmit ELS response x%x to remote "
				"NPORT x%x Data: x%x x%x\n",
				phba->brd_no, elscmd,
				ndlp->nlp_DID, icmd->ulpIoTag, cmdSize);
	}

237
	return elsiocb;
已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
}


static int
lpfc_cmpl_els_flogi_fabric(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
		struct serv_parm *sp, IOCB_t *irsp)
{
	LPFC_MBOXQ_t *mbox;
	int rc;

	spin_lock_irq(phba->host->host_lock);
	phba->fc_flag |= FC_FABRIC;
	spin_unlock_irq(phba->host->host_lock);

	phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
	if (sp->cmn.edtovResolution)	/* E_D_TOV ticks are in nanoseconds */
		phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;

	phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;

	if (phba->fc_topology == TOPOLOGY_LOOP) {
		spin_lock_irq(phba->host->host_lock);
		phba->fc_flag |= FC_PUBLIC_LOOP;
		spin_unlock_irq(phba->host->host_lock);
	} else {
		/*
		 * If we are a N-port connected to a Fabric, fixup sparam's so
		 * logins to devices on remote loops work.
		 */
		phba->fc_sparam.cmn.altBbCredit = 1;
	}

	phba->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
	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;
	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));

	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!mbox)
		goto fail;

	phba->hba_state = LPFC_FABRIC_CFG_LINK;
	lpfc_config_link(phba, mbox);
	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;

	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT | MBX_STOP_IOCB);
	if (rc == MBX_NOT_FINISHED)
		goto fail_free_mbox;

	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!mbox)
		goto fail;

	if (lpfc_reg_login(phba, Fabric_DID, (uint8_t *) sp, mbox, 0))
		goto fail_free_mbox;

	mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
	mbox->context2 = ndlp;

	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT | MBX_STOP_IOCB);
	if (rc == MBX_NOT_FINISHED)
		goto fail_free_mbox;

	return 0;

 fail_free_mbox:
	mempool_free(mbox, phba->mbox_mem_pool);
 fail:
	return -ENXIO;
}

/*
 * We FLOGIed into an NPort, initiate pt2pt protocol
 */
static int
lpfc_cmpl_els_flogi_nport(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
		struct serv_parm *sp)
{
	LPFC_MBOXQ_t *mbox;
	int rc;

	spin_lock_irq(phba->host->host_lock);
	phba->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
	spin_unlock_irq(phba->host->host_lock);

	phba->fc_edtov = FF_DEF_EDTOV;
	phba->fc_ratov = FF_DEF_RATOV;
	rc = memcmp(&phba->fc_portname, &sp->portName,
			sizeof(struct lpfc_name));
	if (rc >= 0) {
		/* This side will initiate the PLOGI */
		spin_lock_irq(phba->host->host_lock);
		phba->fc_flag |= FC_PT2PT_PLOGI;
		spin_unlock_irq(phba->host->host_lock);

		/*
		 * N_Port ID cannot be 0, set our to LocalID the other
		 * side will be RemoteID.
		 */

		/* not equal */
		if (rc)
			phba->fc_myDID = PT2PT_LocalID;

		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
		if (!mbox)
			goto fail;

		lpfc_config_link(phba, mbox);

		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
		rc = lpfc_sli_issue_mbox(phba, mbox,
				MBX_NOWAIT | MBX_STOP_IOCB);
		if (rc == MBX_NOT_FINISHED) {
			mempool_free(mbox, phba->mbox_mem_pool);
			goto fail;
		}
		mempool_free(ndlp, phba->nlp_mem_pool);

		ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, PT2PT_RemoteID);
		if (!ndlp) {
			/*
			 * Cannot find existing Fabric ndlp, so allocate a
			 * new one
			 */
			ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
			if (!ndlp)
				goto fail;

			lpfc_nlp_init(phba, ndlp, PT2PT_RemoteID);
		}

		memcpy(&ndlp->nlp_portname, &sp->portName,
				sizeof(struct lpfc_name));
		memcpy(&ndlp->nlp_nodename, &sp->nodeName,
				sizeof(struct lpfc_name));
		ndlp->nlp_state = NLP_STE_NPR_NODE;
		lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
	} else {
		/* This side will wait for the PLOGI */
		mempool_free( ndlp, phba->nlp_mem_pool);
	}

	spin_lock_irq(phba->host->host_lock);
	phba->fc_flag |= FC_PT2PT;
	spin_unlock_irq(phba->host->host_lock);

	/* Start discovery - this should just do CLEAR_LA */
	lpfc_disc_start(phba);
	return 0;
 fail:
	return -ENXIO;
}

static void
lpfc_cmpl_els_flogi(struct lpfc_hba * phba,
		    struct lpfc_iocbq * cmdiocb, struct lpfc_iocbq * rspiocb)
{
	IOCB_t *irsp = &rspiocb->iocb;
	struct lpfc_nodelist *ndlp = cmdiocb->context1;
	struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
	struct serv_parm *sp;
	int rc;

	/* Check to see if link went down during discovery */
	if (lpfc_els_chk_latt(phba)) {
		lpfc_nlp_remove(phba, ndlp);
		goto out;
	}

	if (irsp->ulpStatus) {
		/* Check for retry */
		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
			/* ELS command is being retried */
			goto out;
		}
		/* FLOGI failed, so there is no fabric */
		spin_lock_irq(phba->host->host_lock);
		phba->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
		spin_unlock_irq(phba->host->host_lock);

		/* If private loop, then allow max outstandting els to be
		 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
		 * alpa map would take too long otherwise.
		 */
		if (phba->alpa_map[0] == 0) {
			phba->cfg_discovery_threads =
			    LPFC_MAX_DISC_THREADS;
		}

		/* FLOGI failure */
		lpfc_printf_log(phba,
				KERN_INFO,
				LOG_ELS,
444
				"%d:0100 FLOGI failure Data: x%x x%x x%x\n",
已提交
445
				phba->brd_no,
446 447
				irsp->ulpStatus, irsp->un.ulpWord[4],
				irsp->ulpTimeout);
已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
		goto flogifail;
	}

	/*
	 * The FLogI succeeded.  Sync the data for the CPU before
	 * accessing it.
	 */
	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);

	sp = prsp->virt + sizeof(uint32_t);

	/* FLOGI completes successfully */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0101 FLOGI completes sucessfully "
			"Data: x%x x%x x%x x%x\n",
			phba->brd_no,
			irsp->un.ulpWord[4], sp->cmn.e_d_tov,
			sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);

	if (phba->hba_state == LPFC_FLOGI) {
		/*
		 * If Common Service Parameters indicate Nport
		 * we are point to point, if Fport we are Fabric.
		 */
		if (sp->cmn.fPort)
			rc = lpfc_cmpl_els_flogi_fabric(phba, ndlp, sp, irsp);
		else
			rc = lpfc_cmpl_els_flogi_nport(phba, ndlp, sp);

		if (!rc)
			goto out;
	}

flogifail:
	lpfc_nlp_remove(phba, ndlp);

	if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
	    (irsp->un.ulpWord[4] != IOERR_SLI_ABORTED &&
	     irsp->un.ulpWord[4] != IOERR_SLI_DOWN)) {
		/* FLOGI failed, so just use loop map to make discovery list */
		lpfc_disc_list_loopmap(phba);

		/* Start discovery */
		lpfc_disc_start(phba);
	}

out:
	lpfc_els_free_iocb(phba, cmdiocb);
}

static int
lpfc_issue_els_flogi(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
		     uint8_t retry)
{
	struct serv_parm *sp;
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	uint8_t *pcmd;
	uint16_t cmdsize;
	uint32_t tmo;
	int rc;

	pring = &phba->sli.ring[LPFC_ELS_RING];

	cmdsize = (sizeof (uint32_t) + sizeof (struct serv_parm));
514 515 516
	elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry, ndlp,
						 ndlp->nlp_DID, ELS_CMD_FLOGI);
	if (!elsiocb)
517
		return 1;
已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550

	icmd = &elsiocb->iocb;
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	/* For FLOGI request, remainder of payload is service parameters */
	*((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
	pcmd += sizeof (uint32_t);
	memcpy(pcmd, &phba->fc_sparam, sizeof (struct serv_parm));
	sp = (struct serv_parm *) pcmd;

	/* Setup CSPs accordingly for Fabric */
	sp->cmn.e_d_tov = 0;
	sp->cmn.w2.r_a_tov = 0;
	sp->cls1.classValid = 0;
	sp->cls2.seqDelivery = 1;
	sp->cls3.seqDelivery = 1;
	if (sp->cmn.fcphLow < FC_PH3)
		sp->cmn.fcphLow = FC_PH3;
	if (sp->cmn.fcphHigh < FC_PH3)
		sp->cmn.fcphHigh = FC_PH3;

	tmo = phba->fc_ratov;
	phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
	lpfc_set_disctmo(phba);
	phba->fc_ratov = tmo;

	phba->fc_stat.elsXmitFLOGI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
	spin_lock_irq(phba->host->host_lock);
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	spin_unlock_irq(phba->host->host_lock);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
551
		return 1;
已提交
552
	}
553
	return 0;
已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
}

int
lpfc_els_abort_flogi(struct lpfc_hba * phba)
{
	struct lpfc_sli_ring *pring;
	struct lpfc_iocbq *iocb, *next_iocb;
	struct lpfc_nodelist *ndlp;
	IOCB_t *icmd;

	/* Abort outstanding I/O on NPort <nlp_DID> */
	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
			"%d:0201 Abort outstanding I/O on NPort x%x\n",
			phba->brd_no, Fabric_DID);

	pring = &phba->sli.ring[LPFC_ELS_RING];

	/*
	 * Check the txcmplq for an iocb that matches the nport the driver is
	 * searching for.
	 */
	spin_lock_irq(phba->host->host_lock);
	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
		icmd = &iocb->iocb;
		if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
			ndlp = (struct lpfc_nodelist *)(iocb->context1);
			if (ndlp && (ndlp->nlp_DID == Fabric_DID)) {
				list_del(&iocb->list);
				pring->txcmplq_cnt--;

				if ((icmd->un.elsreq64.bdl.ulpIoTag32)) {
					lpfc_sli_issue_abort_iotag32
						(phba, pring, iocb);
				}
				if (iocb->iocb_cmpl) {
					icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
					icmd->un.ulpWord[4] =
					    IOERR_SLI_ABORTED;
					spin_unlock_irq(phba->host->host_lock);
					(iocb->iocb_cmpl) (phba, iocb, iocb);
					spin_lock_irq(phba->host->host_lock);
595 596
				} else
					lpfc_sli_release_iocbq(phba, iocb);
已提交
597 598 599 600 601 602 603 604 605 606 607 608 609
			}
		}
	}
	spin_unlock_irq(phba->host->host_lock);

	return 0;
}

int
lpfc_initial_flogi(struct lpfc_hba * phba)
{
	struct lpfc_nodelist *ndlp;

610 611 612
	/* First look for the Fabric ndlp */
	ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, Fabric_DID);
	if (!ndlp) {
已提交
613
		/* Cannot find existing Fabric ndlp, so allocate a new one */
614 615 616
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp)
			return 0;
已提交
617
		lpfc_nlp_init(phba, ndlp, Fabric_DID);
618 619
	} else {
		lpfc_nlp_list(phba, ndlp, NLP_JUST_DQ);
已提交
620 621 622 623
	}
	if (lpfc_issue_els_flogi(phba, ndlp, 0)) {
		mempool_free( ndlp, phba->nlp_mem_pool);
	}
624
	return 1;
已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
}

static void
lpfc_more_plogi(struct lpfc_hba * phba)
{
	int sentplogi;

	if (phba->num_disc_nodes)
		phba->num_disc_nodes--;

	/* Continue discovery with <num_disc_nodes> PLOGIs to go */
	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
			"%d:0232 Continue discovery with %d PLOGIs to go "
			"Data: x%x x%x x%x\n",
			phba->brd_no, phba->num_disc_nodes, phba->fc_plogi_cnt,
			phba->fc_flag, phba->hba_state);

	/* Check to see if there are more PLOGIs to be sent */
	if (phba->fc_flag & FC_NLP_MORE) {
		/* go thru NPR list and issue any remaining ELS PLOGIs */
		sentplogi = lpfc_els_disc_plogi(phba);
	}
	return;
}

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
static struct lpfc_nodelist *
lpfc_plogi_confirm_nport(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
			 struct lpfc_nodelist *ndlp)
{
	struct lpfc_nodelist *new_ndlp;
	struct lpfc_dmabuf *pcmd, *prsp;
	uint32_t *lp;
	struct serv_parm *sp;
	uint8_t name[sizeof (struct lpfc_name)];
	uint32_t rc;

	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	prsp = (struct lpfc_dmabuf *) pcmd->list.next;
	lp = (uint32_t *) prsp->virt;
	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));

	/* Now we to find out if the NPort we are logging into, matches the WWPN
	 * we have for that ndlp. If not, we have some work to do.
	 */
	new_ndlp = lpfc_findnode_wwpn(phba, NLP_SEARCH_ALL, &sp->portName);

	memset(name, 0, sizeof (struct lpfc_name));
	rc =  memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name));
	if (!rc || (new_ndlp == ndlp)) {
		return ndlp;
	}

	if (!new_ndlp) {
		new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
		if (!new_ndlp)
			return ndlp;

		lpfc_nlp_init(phba, new_ndlp, ndlp->nlp_DID);
	}

	lpfc_unreg_rpi(phba, new_ndlp);
	new_ndlp->nlp_prev_state = ndlp->nlp_state;
	new_ndlp->nlp_DID = ndlp->nlp_DID;
	new_ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
	lpfc_nlp_list(phba, new_ndlp, NLP_PLOGI_LIST);

	/* Move this back to NPR list */
	lpfc_unreg_rpi(phba, ndlp);
	ndlp->nlp_DID = 0; /* Two ndlps cannot have the same did */
	ndlp->nlp_state = NLP_STE_NPR_NODE;
	lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);

	return new_ndlp;
}

已提交
700 701 702 703 704 705 706 707 708 709 710 711 712
static void
lpfc_cmpl_els_plogi(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
		    struct lpfc_iocbq * rspiocb)
{
	IOCB_t *irsp;
	struct lpfc_nodelist *ndlp;
	int disc, rc, did, type;


	/* we pass cmdiocb to state machine which needs rspiocb as well */
	cmdiocb->context_un.rsp_iocb = rspiocb;

	irsp = &rspiocb->iocb;
713 714 715 716
	ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL,
						irsp->un.elsreq64.remoteID);
	if (!ndlp)
		goto out;
已提交
717 718 719 720 721

	/* Since ndlp can be freed in the disc state machine, note if this node
	 * is being used during discovery.
	 */
	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
722
	spin_lock_irq(phba->host->host_lock);
723
	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
724
	spin_unlock_irq(phba->host->host_lock);
已提交
725 726 727 728 729
	rc   = 0;

	/* PLOGI completes to NPort <nlp_DID> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0102 PLOGI completes to NPort x%x "
730
			"Data: x%x x%x x%x x%x x%x\n",
已提交
731
			phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
732 733
			irsp->un.ulpWord[4], irsp->ulpTimeout, disc,
			phba->num_disc_nodes);
已提交
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

	/* Check to see if link went down during discovery */
	if (lpfc_els_chk_latt(phba)) {
		spin_lock_irq(phba->host->host_lock);
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
		spin_unlock_irq(phba->host->host_lock);
		goto out;
	}

	/* ndlp could be freed in DSM, save these values now */
	type = ndlp->nlp_type;
	did = ndlp->nlp_DID;

	if (irsp->ulpStatus) {
		/* Check for retry */
		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
			/* ELS command is being retried */
			if (disc) {
				spin_lock_irq(phba->host->host_lock);
				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
				spin_unlock_irq(phba->host->host_lock);
			}
			goto out;
		}

		/* PLOGI failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
		if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
		   ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
763
		   (irsp->un.ulpWord[4] == IOERR_LINK_DOWN) ||
已提交
764
		   (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
765
			rc = NLP_STE_FREED_NODE;
766
		} else {
已提交
767 768 769 770 771
			rc = lpfc_disc_state_machine(phba, ndlp, cmdiocb,
					NLP_EVT_CMPL_PLOGI);
		}
	} else {
		/* Good status, call state machine */
772
		ndlp = lpfc_plogi_confirm_nport(phba, cmdiocb, ndlp);
已提交
773 774 775 776 777 778 779 780 781
		rc = lpfc_disc_state_machine(phba, ndlp, cmdiocb,
					NLP_EVT_CMPL_PLOGI);
	}

	if (disc && phba->num_disc_nodes) {
		/* Check to see if there are more PLOGIs to be sent */
		lpfc_more_plogi(phba);
	}

782
	if (phba->num_disc_nodes == 0) {
已提交
783
		spin_lock_irq(phba->host->host_lock);
784
		phba->fc_flag &= ~FC_NDISC_ACTIVE;
已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
		spin_unlock_irq(phba->host->host_lock);

		lpfc_can_disctmo(phba);
		if (phba->fc_flag & FC_RSCN_MODE) {
			/* Check to see if more RSCNs came in while we were
			 * processing this one.
			 */
			if ((phba->fc_rscn_id_cnt == 0) &&
			    (!(phba->fc_flag & FC_RSCN_DISCOVERY))) {
				spin_lock_irq(phba->host->host_lock);
				phba->fc_flag &= ~FC_RSCN_MODE;
				spin_unlock_irq(phba->host->host_lock);
			} else {
				lpfc_els_handle_rscn(phba);
			}
		}
	}

out:
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
809
lpfc_issue_els_plogi(struct lpfc_hba * phba, uint32_t did, uint8_t retry)
已提交
810 811 812 813 814 815 816 817 818 819 820 821 822
{
	struct serv_parm *sp;
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */

	cmdsize = (sizeof (uint32_t) + sizeof (struct serv_parm));
823
	elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry, 0, did,
824
								ELS_CMD_PLOGI);
825 826
	if (!elsiocb)
		return 1;
已提交
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848

	icmd = &elsiocb->iocb;
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	/* For PLOGI request, remainder of payload is service parameters */
	*((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
	pcmd += sizeof (uint32_t);
	memcpy(pcmd, &phba->fc_sparam, sizeof (struct serv_parm));
	sp = (struct serv_parm *) pcmd;

	if (sp->cmn.fcphLow < FC_PH_4_3)
		sp->cmn.fcphLow = FC_PH_4_3;

	if (sp->cmn.fcphHigh < FC_PH3)
		sp->cmn.fcphHigh = FC_PH3;

	phba->fc_stat.elsXmitPLOGI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
	spin_lock_irq(phba->host->host_lock);
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
		spin_unlock_irq(phba->host->host_lock);
		lpfc_els_free_iocb(phba, elsiocb);
849
		return 1;
已提交
850 851
	}
	spin_unlock_irq(phba->host->host_lock);
852
	return 0;
已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
}

static void
lpfc_cmpl_els_prli(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
		   struct lpfc_iocbq * rspiocb)
{
	IOCB_t *irsp;
	struct lpfc_sli *psli;
	struct lpfc_nodelist *ndlp;

	psli = &phba->sli;
	/* we pass cmdiocb to state machine which needs rspiocb as well */
	cmdiocb->context_un.rsp_iocb = rspiocb;

	irsp = &(rspiocb->iocb);
	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	spin_lock_irq(phba->host->host_lock);
	ndlp->nlp_flag &= ~NLP_PRLI_SND;
	spin_unlock_irq(phba->host->host_lock);

	/* PRLI completes to NPort <nlp_DID> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0103 PRLI completes to NPort x%x "
876
			"Data: x%x x%x x%x x%x\n",
已提交
877
			phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
878 879
			irsp->un.ulpWord[4], irsp->ulpTimeout,
			phba->num_disc_nodes);
已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895

	phba->fc_prli_sent--;
	/* Check to see if link went down during discovery */
	if (lpfc_els_chk_latt(phba))
		goto out;

	if (irsp->ulpStatus) {
		/* Check for retry */
		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
			/* ELS command is being retried */
			goto out;
		}
		/* PRLI failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
		if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
		   ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
896
		   (irsp->un.ulpWord[4] == IOERR_LINK_DOWN) ||
已提交
897 898
		   (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
			goto out;
899
		} else {
已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
			lpfc_disc_state_machine(phba, ndlp, cmdiocb,
					NLP_EVT_CMPL_PRLI);
		}
	} else {
		/* Good status, call state machine */
		lpfc_disc_state_machine(phba, ndlp, cmdiocb, NLP_EVT_CMPL_PRLI);
	}

out:
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
lpfc_issue_els_prli(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
		    uint8_t retry)
{
	PRLI *npr;
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */

	cmdsize = (sizeof (uint32_t) + sizeof (PRLI));
929 930 931
	elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry, ndlp,
					ndlp->nlp_DID, ELS_CMD_PRLI);
	if (!elsiocb)
932
		return 1;
已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967

	icmd = &elsiocb->iocb;
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	/* For PRLI request, remainder of payload is service parameters */
	memset(pcmd, 0, (sizeof (PRLI) + sizeof (uint32_t)));
	*((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
	pcmd += sizeof (uint32_t);

	/* For PRLI, remainder of payload is PRLI parameter page */
	npr = (PRLI *) pcmd;
	/*
	 * If our firmware version is 3.20 or later,
	 * set the following bits for FC-TAPE support.
	 */
	if (phba->vpd.rev.feaLevelHigh >= 0x02) {
		npr->ConfmComplAllowed = 1;
		npr->Retry = 1;
		npr->TaskRetryIdReq = 1;
	}
	npr->estabImagePair = 1;
	npr->readXferRdyDis = 1;

	/* For FCP support */
	npr->prliType = PRLI_FCP_TYPE;
	npr->initiatorFunc = 1;

	phba->fc_stat.elsXmitPRLI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
	spin_lock_irq(phba->host->host_lock);
	ndlp->nlp_flag |= NLP_PRLI_SND;
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
		ndlp->nlp_flag &= ~NLP_PRLI_SND;
		spin_unlock_irq(phba->host->host_lock);
		lpfc_els_free_iocb(phba, elsiocb);
968
		return 1;
已提交
969 970 971
	}
	spin_unlock_irq(phba->host->host_lock);
	phba->fc_prli_sent++;
972
	return 0;
已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
}

static void
lpfc_more_adisc(struct lpfc_hba * phba)
{
	int sentadisc;

	if (phba->num_disc_nodes)
		phba->num_disc_nodes--;

	/* Continue discovery with <num_disc_nodes> ADISCs to go */
	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
			"%d:0210 Continue discovery with %d ADISCs to go "
			"Data: x%x x%x x%x\n",
			phba->brd_no, phba->num_disc_nodes, phba->fc_adisc_cnt,
			phba->fc_flag, phba->hba_state);

	/* Check to see if there are more ADISCs to be sent */
	if (phba->fc_flag & FC_NLP_MORE) {
		lpfc_set_disctmo(phba);

		/* go thru NPR list and issue any remaining ELS ADISCs */
		sentadisc = lpfc_els_disc_adisc(phba);
	}
	return;
}

static void
lpfc_rscn_disc(struct lpfc_hba * phba)
{
	/* RSCN discovery */
	/* go thru NPR list and issue ELS PLOGIs */
	if (phba->fc_npr_cnt) {
		if (lpfc_els_disc_plogi(phba))
			return;
	}
	if (phba->fc_flag & FC_RSCN_MODE) {
		/* Check to see if more RSCNs came in while we were
		 * processing this one.
		 */
		if ((phba->fc_rscn_id_cnt == 0) &&
		    (!(phba->fc_flag & FC_RSCN_DISCOVERY))) {
			spin_lock_irq(phba->host->host_lock);
			phba->fc_flag &= ~FC_RSCN_MODE;
			spin_unlock_irq(phba->host->host_lock);
		} else {
			lpfc_els_handle_rscn(phba);
		}
	}
}

static void
lpfc_cmpl_els_adisc(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
		    struct lpfc_iocbq * rspiocb)
{
	IOCB_t *irsp;
	struct lpfc_sli *psli;
	struct lpfc_nodelist *ndlp;
	LPFC_MBOXQ_t *mbox;
	int disc, rc;

	psli = &phba->sli;

	/* we pass cmdiocb to state machine which needs rspiocb as well */
	cmdiocb->context_un.rsp_iocb = rspiocb;

	irsp = &(rspiocb->iocb);
	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;

	/* Since ndlp can be freed in the disc state machine, note if this node
	 * is being used during discovery.
	 */
	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1046 1047 1048
	spin_lock_irq(phba->host->host_lock);
	ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
	spin_unlock_irq(phba->host->host_lock);
已提交
1049 1050 1051 1052

	/* ADISC completes to NPort <nlp_DID> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0104 ADISC completes to NPort x%x "
1053
			"Data: x%x x%x x%x x%x x%x\n",
已提交
1054
			phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
1055 1056
			irsp->un.ulpWord[4], irsp->ulpTimeout, disc,
			phba->num_disc_nodes);
已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079

	/* Check to see if link went down during discovery */
	if (lpfc_els_chk_latt(phba)) {
		spin_lock_irq(phba->host->host_lock);
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
		spin_unlock_irq(phba->host->host_lock);
		goto out;
	}

	if (irsp->ulpStatus) {
		/* Check for retry */
		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
			/* ELS command is being retried */
			if (disc) {
				spin_lock_irq(phba->host->host_lock);
				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
				spin_unlock_irq(phba->host->host_lock);
				lpfc_set_disctmo(phba);
			}
			goto out;
		}
		/* ADISC failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1080 1081 1082 1083
		if ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
		   ((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
		   (irsp->un.ulpWord[4] != IOERR_LINK_DOWN) &&
		   (irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) {
已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
			lpfc_disc_state_machine(phba, ndlp, cmdiocb,
					NLP_EVT_CMPL_ADISC);
		}
	} else {
		/* Good status, call state machine */
		lpfc_disc_state_machine(phba, ndlp, cmdiocb,
					NLP_EVT_CMPL_ADISC);
	}

	if (disc && phba->num_disc_nodes) {
		/* Check to see if there are more ADISCs to be sent */
		lpfc_more_adisc(phba);

		/* Check to see if we are done with ADISC authentication */
		if (phba->num_disc_nodes == 0) {
			lpfc_can_disctmo(phba);
			/* If we get here, there is nothing left to wait for */
			if ((phba->hba_state < LPFC_HBA_READY) &&
			    (phba->hba_state != LPFC_CLEAR_LA)) {
				/* Link up discovery */
				if ((mbox = mempool_alloc(phba->mbox_mem_pool,
							  GFP_KERNEL))) {
					phba->hba_state = LPFC_CLEAR_LA;
					lpfc_clear_la(phba, mbox);
					mbox->mbox_cmpl =
					    lpfc_mbx_cmpl_clear_la;
					rc = lpfc_sli_issue_mbox
						(phba, mbox,
						 (MBX_NOWAIT | MBX_STOP_IOCB));
					if (rc == MBX_NOT_FINISHED) {
						mempool_free(mbox,
						     phba->mbox_mem_pool);
						lpfc_disc_flush_list(phba);
						psli->ring[(psli->ip_ring)].
						    flag &=
						    ~LPFC_STOP_IOCB_EVENT;
						psli->ring[(psli->fcp_ring)].
						    flag &=
						    ~LPFC_STOP_IOCB_EVENT;
						psli->ring[(psli->next_ring)].
						    flag &=
						    ~LPFC_STOP_IOCB_EVENT;
						phba->hba_state =
						    LPFC_HBA_READY;
					}
				}
			} else {
				lpfc_rscn_disc(phba);
			}
		}
	}
out:
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
lpfc_issue_els_adisc(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
		     uint8_t retry)
{
	ADISC *ap;
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */

	cmdsize = (sizeof (uint32_t) + sizeof (ADISC));
1156 1157 1158
	elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry, ndlp,
						ndlp->nlp_DID, ELS_CMD_ADISC);
	if (!elsiocb)
1159
		return 1;
已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182

	icmd = &elsiocb->iocb;
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	/* For ADISC request, remainder of payload is service parameters */
	*((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
	pcmd += sizeof (uint32_t);

	/* Fill in ADISC payload */
	ap = (ADISC *) pcmd;
	ap->hardAL_PA = phba->fc_pref_ALPA;
	memcpy(&ap->portName, &phba->fc_portname, sizeof (struct lpfc_name));
	memcpy(&ap->nodeName, &phba->fc_nodename, sizeof (struct lpfc_name));
	ap->DID = be32_to_cpu(phba->fc_myDID);

	phba->fc_stat.elsXmitADISC++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
	spin_lock_irq(phba->host->host_lock);
	ndlp->nlp_flag |= NLP_ADISC_SND;
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
		ndlp->nlp_flag &= ~NLP_ADISC_SND;
		spin_unlock_irq(phba->host->host_lock);
		lpfc_els_free_iocb(phba, elsiocb);
1183
		return 1;
已提交
1184 1185
	}
	spin_unlock_irq(phba->host->host_lock);
1186
	return 0;
已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
}

static void
lpfc_cmpl_els_logo(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
		   struct lpfc_iocbq * rspiocb)
{
	IOCB_t *irsp;
	struct lpfc_sli *psli;
	struct lpfc_nodelist *ndlp;

	psli = &phba->sli;
	/* we pass cmdiocb to state machine which needs rspiocb as well */
	cmdiocb->context_un.rsp_iocb = rspiocb;

	irsp = &(rspiocb->iocb);
	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	spin_lock_irq(phba->host->host_lock);
	ndlp->nlp_flag &= ~NLP_LOGO_SND;
	spin_unlock_irq(phba->host->host_lock);

	/* LOGO completes to NPort <nlp_DID> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0105 LOGO completes to NPort x%x "
1210
			"Data: x%x x%x x%x x%x\n",
已提交
1211
			phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
1212 1213
			irsp->un.ulpWord[4], irsp->ulpTimeout,
			phba->num_disc_nodes);
已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

	/* Check to see if link went down during discovery */
	if (lpfc_els_chk_latt(phba))
		goto out;

	if (irsp->ulpStatus) {
		/* Check for retry */
		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
			/* ELS command is being retried */
			goto out;
		}
		/* LOGO failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
		if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
		   ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
1229
		   (irsp->un.ulpWord[4] == IOERR_LINK_DOWN) ||
已提交
1230 1231
		   (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
			goto out;
1232
		} else {
已提交
1233 1234 1235 1236
			lpfc_disc_state_machine(phba, ndlp, cmdiocb,
					NLP_EVT_CMPL_LOGO);
		}
	} else {
1237 1238 1239
		/* Good status, call state machine.
		 * This will unregister the rpi if needed.
		 */
已提交
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
		lpfc_disc_state_machine(phba, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
	}

out:
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
lpfc_issue_els_logo(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
		    uint8_t retry)
{
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];

	cmdsize = 2 * (sizeof (uint32_t) + sizeof (struct lpfc_name));
1263 1264 1265
	elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry, ndlp,
						ndlp->nlp_DID, ELS_CMD_LOGO);
	if (!elsiocb)
1266
		return 1;
已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285

	icmd = &elsiocb->iocb;
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
	pcmd += sizeof (uint32_t);

	/* Fill in LOGO payload */
	*((uint32_t *) (pcmd)) = be32_to_cpu(phba->fc_myDID);
	pcmd += sizeof (uint32_t);
	memcpy(pcmd, &phba->fc_portname, sizeof (struct lpfc_name));

	phba->fc_stat.elsXmitLOGO++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
	spin_lock_irq(phba->host->host_lock);
	ndlp->nlp_flag |= NLP_LOGO_SND;
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
		ndlp->nlp_flag &= ~NLP_LOGO_SND;
		spin_unlock_irq(phba->host->host_lock);
		lpfc_els_free_iocb(phba, elsiocb);
1286
		return 1;
已提交
1287 1288
	}
	spin_unlock_irq(phba->host->host_lock);
1289
	return 0;
已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
}

static void
lpfc_cmpl_els_cmd(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
		  struct lpfc_iocbq * rspiocb)
{
	IOCB_t *irsp;

	irsp = &rspiocb->iocb;

	/* ELS cmd tag <ulpIoTag> completes */
	lpfc_printf_log(phba,
			KERN_INFO,
			LOG_ELS,
1304
			"%d:0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
已提交
1305
			phba->brd_no,
1306 1307
			irsp->ulpIoTag, irsp->ulpStatus,
			irsp->un.ulpWord[4], irsp->ulpTimeout);
已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328

	/* Check to see if link went down during discovery */
	lpfc_els_chk_latt(phba);
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
lpfc_issue_els_scr(struct lpfc_hba * phba, uint32_t nportid, uint8_t retry)
{
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;
	struct lpfc_nodelist *ndlp;

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */
	cmdsize = (sizeof (uint32_t) + sizeof (SCR));
1329 1330 1331
	ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
	if (!ndlp)
		return 1;
已提交
1332 1333 1334

	lpfc_nlp_init(phba, ndlp, nportid);

1335 1336 1337
	elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry, ndlp,
						ndlp->nlp_DID, ELS_CMD_SCR);
	if (!elsiocb) {
已提交
1338
		mempool_free( ndlp, phba->nlp_mem_pool);
1339
		return 1;
已提交
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
	}

	icmd = &elsiocb->iocb;
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	*((uint32_t *) (pcmd)) = ELS_CMD_SCR;
	pcmd += sizeof (uint32_t);

	/* For SCR, remainder of payload is SCR parameter page */
	memset(pcmd, 0, sizeof (SCR));
	((SCR *) pcmd)->Function = SCR_FUNC_FULL;

	phba->fc_stat.elsXmitSCR++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
	spin_lock_irq(phba->host->host_lock);
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
		spin_unlock_irq(phba->host->host_lock);
		mempool_free( ndlp, phba->nlp_mem_pool);
		lpfc_els_free_iocb(phba, elsiocb);
1359
		return 1;
已提交
1360 1361 1362
	}
	spin_unlock_irq(phba->host->host_lock);
	mempool_free( ndlp, phba->nlp_mem_pool);
1363
	return 0;
已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
}

static int
lpfc_issue_els_farpr(struct lpfc_hba * phba, uint32_t nportid, uint8_t retry)
{
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	FARP *fp;
	uint8_t *pcmd;
	uint32_t *lp;
	uint16_t cmdsize;
	struct lpfc_nodelist *ondlp;
	struct lpfc_nodelist *ndlp;

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */
	cmdsize = (sizeof (uint32_t) + sizeof (FARP));
1383 1384 1385
	ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
	if (!ndlp)
		return 1;
已提交
1386 1387
	lpfc_nlp_init(phba, ndlp, nportid);

1388 1389 1390
	elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry, ndlp,
						ndlp->nlp_DID, ELS_CMD_RNID);
	if (!elsiocb) {
已提交
1391
		mempool_free( ndlp, phba->nlp_mem_pool);
1392
		return 1;
已提交
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
	}

	icmd = &elsiocb->iocb;
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	*((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
	pcmd += sizeof (uint32_t);

	/* Fill in FARPR payload */
	fp = (FARP *) (pcmd);
	memset(fp, 0, sizeof (FARP));
	lp = (uint32_t *) pcmd;
	*lp++ = be32_to_cpu(nportid);
	*lp++ = be32_to_cpu(phba->fc_myDID);
	fp->Rflags = 0;
	fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);

	memcpy(&fp->RportName, &phba->fc_portname, sizeof (struct lpfc_name));
	memcpy(&fp->RnodeName, &phba->fc_nodename, sizeof (struct lpfc_name));
	if ((ondlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, nportid))) {
		memcpy(&fp->OportName, &ondlp->nlp_portname,
		       sizeof (struct lpfc_name));
		memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
		       sizeof (struct lpfc_name));
	}

	phba->fc_stat.elsXmitFARPR++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
	spin_lock_irq(phba->host->host_lock);
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
		spin_unlock_irq(phba->host->host_lock);
		mempool_free( ndlp, phba->nlp_mem_pool);
		lpfc_els_free_iocb(phba, elsiocb);
1426
		return 1;
已提交
1427 1428 1429
	}
	spin_unlock_irq(phba->host->host_lock);
	mempool_free( ndlp, phba->nlp_mem_pool);
1430
	return 0;
已提交
1431 1432
}

1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
void
lpfc_cancel_retry_delay_tmo(struct lpfc_hba *phba, struct lpfc_nodelist * nlp)
{
	nlp->nlp_flag &= ~NLP_DELAY_TMO;
	del_timer_sync(&nlp->nlp_delayfunc);
	nlp->nlp_last_elscmd = 0;

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

	if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
		nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
		if (phba->num_disc_nodes) {
			/* Check to see if there are more
			 * PLOGIs to be sent
			 */
			lpfc_more_plogi(phba);
		}

		if (phba->num_disc_nodes == 0) {
			phba->fc_flag &= ~FC_NDISC_ACTIVE;
			lpfc_can_disctmo(phba);
			if (phba->fc_flag & FC_RSCN_MODE) {
				/* Check to see if more RSCNs
				 * came in while we were
				 * processing this one.
				 */
				if((phba->fc_rscn_id_cnt==0) &&
				   (!(phba->fc_flag & FC_RSCN_DISCOVERY))) {
					phba->fc_flag &= ~FC_RSCN_MODE;
				}
				else {
					lpfc_els_handle_rscn(phba);
				}
			}
		}
	}
	return;
}

已提交
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
void
lpfc_els_retry_delay(unsigned long ptr)
{
	struct lpfc_nodelist *ndlp;
	struct lpfc_hba *phba;
	unsigned long iflag;
	struct lpfc_work_evt  *evtp;

	ndlp = (struct lpfc_nodelist *)ptr;
	phba = ndlp->nlp_phba;
	evtp = &ndlp->els_retry_evt;

	spin_lock_irqsave(phba->host->host_lock, iflag);
	if (!list_empty(&evtp->evt_listp)) {
		spin_unlock_irqrestore(phba->host->host_lock, iflag);
		return;
	}

	evtp->evt_arg1  = ndlp;
	evtp->evt       = LPFC_EVT_ELS_RETRY;
	list_add_tail(&evtp->evt_listp, &phba->work_list);
	if (phba->work_wait)
		wake_up(phba->work_wait);

	spin_unlock_irqrestore(phba->host->host_lock, iflag);
	return;
}

void
lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
{
	struct lpfc_hba *phba;
	uint32_t cmd;
	uint32_t did;
	uint8_t retry;

	phba = ndlp->nlp_phba;
	spin_lock_irq(phba->host->host_lock);
1511 1512 1513
	did = ndlp->nlp_DID;
	cmd = ndlp->nlp_last_elscmd;
	ndlp->nlp_last_elscmd = 0;
已提交
1514 1515 1516 1517 1518 1519 1520 1521

	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
		spin_unlock_irq(phba->host->host_lock);
		return;
	}

	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
	spin_unlock_irq(phba->host->host_lock);
1522 1523 1524 1525 1526 1527
	/*
	 * If a discovery event readded nlp_delayfunc after timer
	 * firing and before processing the timer, cancel the
	 * nlp_delayfunc.
	 */
	del_timer_sync(&ndlp->nlp_delayfunc);
已提交
1528 1529 1530 1531 1532 1533 1534
	retry = ndlp->nlp_retry;

	switch (cmd) {
	case ELS_CMD_FLOGI:
		lpfc_issue_els_flogi(phba, ndlp, retry);
		break;
	case ELS_CMD_PLOGI:
1535
		if(!lpfc_issue_els_plogi(phba, ndlp->nlp_DID, retry)) {
1536
			ndlp->nlp_prev_state = ndlp->nlp_state;
1537 1538 1539
			ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
			lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
		}
已提交
1540 1541
		break;
	case ELS_CMD_ADISC:
1542
		if (!lpfc_issue_els_adisc(phba, ndlp, retry)) {
1543
			ndlp->nlp_prev_state = ndlp->nlp_state;
1544 1545 1546
			ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
			lpfc_nlp_list(phba, ndlp, NLP_ADISC_LIST);
		}
已提交
1547 1548
		break;
	case ELS_CMD_PRLI:
1549
		if (!lpfc_issue_els_prli(phba, ndlp, retry)) {
1550
			ndlp->nlp_prev_state = ndlp->nlp_state;
1551 1552 1553
			ndlp->nlp_state = NLP_STE_PRLI_ISSUE;
			lpfc_nlp_list(phba, ndlp, NLP_PRLI_LIST);
		}
已提交
1554 1555
		break;
	case ELS_CMD_LOGO:
1556
		if (!lpfc_issue_els_logo(phba, ndlp, retry)) {
1557
			ndlp->nlp_prev_state = ndlp->nlp_state;
1558 1559 1560
			ndlp->nlp_state = NLP_STE_NPR_NODE;
			lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
		}
已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
		break;
	}
	return;
}

static int
lpfc_els_retry(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
	       struct lpfc_iocbq * rspiocb)
{
	IOCB_t *irsp;
	struct lpfc_dmabuf *pcmd;
	struct lpfc_nodelist *ndlp;
	uint32_t *elscmd;
	struct ls_rjt stat;
	int retry, maxretry;
	int delay;
	uint32_t cmd;
1578
	uint32_t did;
已提交
1579 1580 1581 1582 1583 1584 1585 1586

	retry = 0;
	delay = 0;
	maxretry = lpfc_max_els_tries;
	irsp = &rspiocb->iocb;
	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	cmd = 0;
1587

已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596
	/* Note: context2 may be 0 for internal driver abort
	 * of delays ELS command.
	 */

	if (pcmd && pcmd->virt) {
		elscmd = (uint32_t *) (pcmd->virt);
		cmd = *elscmd++;
	}

1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
	if(ndlp)
		did = ndlp->nlp_DID;
	else {
		/* We should only hit this case for retrying PLOGI */
		did = irsp->un.elsreq64.remoteID;
		ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, did);
		if (!ndlp && (cmd != ELS_CMD_PLOGI))
			return 1;
	}

已提交
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
	switch (irsp->ulpStatus) {
	case IOSTAT_FCP_RSP_ERROR:
	case IOSTAT_REMOTE_STOP:
		break;

	case IOSTAT_LOCAL_REJECT:
		switch ((irsp->un.ulpWord[4] & 0xff)) {
		case IOERR_LOOP_OPEN_FAILURE:
			if (cmd == ELS_CMD_PLOGI) {
				if (cmdiocb->retry == 0) {
					delay = 1;
				}
			}
			retry = 1;
			break;

		case IOERR_SEQUENCE_TIMEOUT:
			retry = 1;
			break;

		case IOERR_NO_RESOURCES:
			if (cmd == ELS_CMD_PLOGI) {
				delay = 1;
			}
			retry = 1;
			break;

		case IOERR_INVALID_RPI:
			retry = 1;
			break;
		}
		break;

	case IOSTAT_NPORT_RJT:
	case IOSTAT_FABRIC_RJT:
		if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
			retry = 1;
			break;
		}
		break;

	case IOSTAT_NPORT_BSY:
	case IOSTAT_FABRIC_BSY:
		retry = 1;
		break;

	case IOSTAT_LS_RJT:
		stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
		/* Added for Vendor specifc support
		 * Just keep retrying for these Rsn / Exp codes
		 */
		switch (stat.un.b.lsRjtRsnCode) {
		case LSRJT_UNABLE_TPC:
			if (stat.un.b.lsRjtRsnCodeExp ==
			    LSEXP_CMD_IN_PROGRESS) {
				if (cmd == ELS_CMD_PLOGI) {
					delay = 1;
					maxretry = 48;
				}
				retry = 1;
				break;
			}
			if (cmd == ELS_CMD_PLOGI) {
				delay = 1;
				maxretry = lpfc_max_els_tries + 1;
				retry = 1;
				break;
			}
			break;

		case LSRJT_LOGICAL_BSY:
			if (cmd == ELS_CMD_PLOGI) {
				delay = 1;
				maxretry = 48;
			}
			retry = 1;
			break;
		}
		break;

	case IOSTAT_INTERMED_RSP:
	case IOSTAT_BA_RJT:
		break;

	default:
		break;
	}

1695
	if (did == FDMI_DID)
已提交
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
		retry = 1;

	if ((++cmdiocb->retry) >= maxretry) {
		phba->fc_stat.elsRetryExceeded++;
		retry = 0;
	}

	if (retry) {

		/* Retry ELS command <elsCmd> to remote NPORT <did> */
		lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
				"%d:0107 Retry ELS command x%x to remote "
				"NPORT x%x Data: x%x x%x\n",
				phba->brd_no,
1710
				cmd, did, cmdiocb->retry, delay);
已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720

		if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) {
			/* If discovery / RSCN timer is running, reset it */
			if (timer_pending(&phba->fc_disctmo) ||
			      (phba->fc_flag & FC_RSCN_MODE)) {
				lpfc_set_disctmo(phba);
			}
		}

		phba->fc_stat.elsXmitRetry++;
1721
		if (ndlp && delay) {
已提交
1722 1723 1724 1725 1726 1727
			phba->fc_stat.elsDelayRetry++;
			ndlp->nlp_retry = cmdiocb->retry;

			mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
			ndlp->nlp_flag |= NLP_DELAY_TMO;

1728
			ndlp->nlp_prev_state = ndlp->nlp_state;
已提交
1729 1730 1731 1732
			ndlp->nlp_state = NLP_STE_NPR_NODE;
			lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
			ndlp->nlp_last_elscmd = cmd;

1733
			return 1;
已提交
1734 1735 1736 1737
		}
		switch (cmd) {
		case ELS_CMD_FLOGI:
			lpfc_issue_els_flogi(phba, ndlp, cmdiocb->retry);
1738
			return 1;
已提交
1739
		case ELS_CMD_PLOGI:
1740 1741 1742 1743 1744 1745
			if (ndlp) {
				ndlp->nlp_prev_state = ndlp->nlp_state;
				ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
				lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
			}
			lpfc_issue_els_plogi(phba, did, cmdiocb->retry);
1746
			return 1;
已提交
1747
		case ELS_CMD_ADISC:
1748
			ndlp->nlp_prev_state = ndlp->nlp_state;
已提交
1749 1750 1751
			ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
			lpfc_nlp_list(phba, ndlp, NLP_ADISC_LIST);
			lpfc_issue_els_adisc(phba, ndlp, cmdiocb->retry);
1752
			return 1;
已提交
1753
		case ELS_CMD_PRLI:
1754
			ndlp->nlp_prev_state = ndlp->nlp_state;
已提交
1755 1756 1757
			ndlp->nlp_state = NLP_STE_PRLI_ISSUE;
			lpfc_nlp_list(phba, ndlp, NLP_PRLI_LIST);
			lpfc_issue_els_prli(phba, ndlp, cmdiocb->retry);
1758
			return 1;
已提交
1759
		case ELS_CMD_LOGO:
1760
			ndlp->nlp_prev_state = ndlp->nlp_state;
已提交
1761 1762 1763
			ndlp->nlp_state = NLP_STE_NPR_NODE;
			lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
			lpfc_issue_els_logo(phba, ndlp, cmdiocb->retry);
1764
			return 1;
已提交
1765 1766 1767 1768 1769 1770
		}
	}

	/* No retry ELS command <elsCmd> to remote NPORT <did> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0108 No retry ELS command x%x to remote NPORT x%x "
1771
			"Data: x%x\n",
已提交
1772
			phba->brd_no,
1773
			cmd, did, cmdiocb->retry);
已提交
1774

1775
	return 0;
已提交
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
}

int
lpfc_els_free_iocb(struct lpfc_hba * phba, struct lpfc_iocbq * elsiocb)
{
	struct lpfc_dmabuf *buf_ptr, *buf_ptr1;

	/* context2  = cmd,  context2->next = rsp, context3 = bpl */
	if (elsiocb->context2) {
		buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
		/* Free the response before processing the command.  */
		if (!list_empty(&buf_ptr1->list)) {
			list_remove_head(&buf_ptr1->list, buf_ptr,
					 struct lpfc_dmabuf,
					 list);
			lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
			kfree(buf_ptr);
		}
		lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
		kfree(buf_ptr1);
	}

	if (elsiocb->context3) {
		buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
		kfree(buf_ptr);
	}
	spin_lock_irq(phba->host->host_lock);
1804
	lpfc_sli_release_iocbq(phba, elsiocb);
已提交
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
	spin_unlock_irq(phba->host->host_lock);
	return 0;
}

static void
lpfc_cmpl_els_logo_acc(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
		       struct lpfc_iocbq * rspiocb)
{
	struct lpfc_nodelist *ndlp;

	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;

	/* ACC to LOGO completes to NPort <nlp_DID> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0109 ACC to LOGO completes to NPort x%x "
			"Data: x%x x%x x%x\n",
			phba->brd_no, ndlp->nlp_DID, ndlp->nlp_flag,
			ndlp->nlp_state, ndlp->nlp_rpi);

	switch (ndlp->nlp_state) {
	case NLP_STE_UNUSED_NODE:	/* node is just allocated */
		lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
		break;
	case NLP_STE_NPR_NODE:		/* NPort Recovery mode */
		lpfc_unreg_rpi(phba, ndlp);
		break;
	default:
		break;
	}
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

static void
lpfc_cmpl_els_acc(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
		  struct lpfc_iocbq * rspiocb)
{
	struct lpfc_nodelist *ndlp;
	LPFC_MBOXQ_t *mbox = NULL;

	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	if (cmdiocb->context_un.mbox)
		mbox = cmdiocb->context_un.mbox;


	/* Check to see if link went down during discovery */
	if ((lpfc_els_chk_latt(phba)) || !ndlp) {
		if (mbox) {
			mempool_free( mbox, phba->mbox_mem_pool);
		}
		goto out;
	}

	/* ELS response tag <ulpIoTag> completes */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0110 ELS response tag x%x completes "
1861
			"Data: x%x x%x x%x x%x x%x x%x x%x\n",
已提交
1862 1863
			phba->brd_no,
			cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
1864 1865 1866
			rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
 			ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
			ndlp->nlp_rpi);
已提交
1867 1868 1869 1870 1871 1872 1873

	if (mbox) {
		if ((rspiocb->iocb.ulpStatus == 0)
		    && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
			lpfc_unreg_rpi(phba, ndlp);
			mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
			mbox->context2 = ndlp;
1874
			ndlp->nlp_prev_state = ndlp->nlp_state;
已提交
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
			ndlp->nlp_state = NLP_STE_REG_LOGIN_ISSUE;
			lpfc_nlp_list(phba, ndlp, NLP_REGLOGIN_LIST);
			if (lpfc_sli_issue_mbox(phba, mbox,
						(MBX_NOWAIT | MBX_STOP_IOCB))
			    != MBX_NOT_FINISHED) {
				goto out;
			}
			/* NOTE: we should have messages for unsuccessful
			   reglogin */
			mempool_free( mbox, phba->mbox_mem_pool);
		} else {
			mempool_free( mbox, phba->mbox_mem_pool);
			if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
				lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1889
				ndlp = NULL;
已提交
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
			}
		}
	}
out:
	if (ndlp) {
		spin_lock_irq(phba->host->host_lock);
		ndlp->nlp_flag &= ~NLP_ACC_REGLOGIN;
		spin_unlock_irq(phba->host->host_lock);
	}
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
lpfc_els_rsp_acc(struct lpfc_hba * phba, uint32_t flag,
		 struct lpfc_iocbq * oldiocb, struct lpfc_nodelist * ndlp,
		 LPFC_MBOXQ_t * mbox, uint8_t newnode)
{
	IOCB_t *icmd;
	IOCB_t *oldcmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;
	int rc;
1916
	ELS_PKT *els_pkt_ptr;
已提交
1917 1918 1919 1920 1921 1922 1923 1924

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */
	oldcmd = &oldiocb->iocb;

	switch (flag) {
	case ELS_CMD_ACC:
		cmdsize = sizeof (uint32_t);
1925 1926 1927
		elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
					ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
		if (!elsiocb) {
1928
			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
1929
			return 1;
已提交
1930 1931 1932 1933 1934 1935 1936 1937 1938
		}
		icmd = &elsiocb->iocb;
		icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
		pcmd += sizeof (uint32_t);
		break;
	case ELS_CMD_PLOGI:
		cmdsize = (sizeof (struct serv_parm) + sizeof (uint32_t));
1939 1940 1941
		elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
					ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
		if (!elsiocb)
1942
			return 1;
1943

已提交
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
		icmd = &elsiocb->iocb;
		icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

		if (mbox)
			elsiocb->context_un.mbox = mbox;

		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
		pcmd += sizeof (uint32_t);
		memcpy(pcmd, &phba->fc_sparam, sizeof (struct serv_parm));
		break;
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
	case ELS_CMD_PRLO:
		cmdsize = sizeof (uint32_t) + sizeof (PRLO);
		elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
					     ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
		if (!elsiocb)
			return 1;

		icmd = &elsiocb->iocb;
		icmd->ulpContext = oldcmd->ulpContext; /* Xri */
		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

		memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
		       sizeof (uint32_t) + sizeof (PRLO));
		*((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
		els_pkt_ptr = (ELS_PKT *) pcmd;
		els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
		break;
已提交
1972
	default:
1973
		return 1;
已提交
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
	}

	if (newnode)
		elsiocb->context1 = NULL;

	/* Xmit ELS ACC response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0128 Xmit ELS ACC response tag x%x "
			"Data: x%x x%x x%x x%x x%x\n",
			phba->brd_no,
			elsiocb->iocb.ulpIoTag,
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

	if (ndlp->nlp_flag & NLP_LOGO_ACC) {
1989 1990 1991
		spin_lock_irq(phba->host->host_lock);
		ndlp->nlp_flag &= ~NLP_LOGO_ACC;
		spin_unlock_irq(phba->host->host_lock);
已提交
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
		elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
	} else {
		elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;
	}

	phba->fc_stat.elsXmitACC++;
	spin_lock_irq(phba->host->host_lock);
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	spin_unlock_irq(phba->host->host_lock);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2003
		return 1;
已提交
2004
	}
2005
	return 0;
已提交
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
}

int
lpfc_els_rsp_reject(struct lpfc_hba * phba, uint32_t rejectError,
		    struct lpfc_iocbq * oldiocb, struct lpfc_nodelist * ndlp)
{
	IOCB_t *icmd;
	IOCB_t *oldcmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;
	int rc;

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */

	cmdsize = 2 * sizeof (uint32_t);
2025 2026 2027
	elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
					ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
	if (!elsiocb)
2028
		return 1;
已提交
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054

	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
	pcmd += sizeof (uint32_t);
	*((uint32_t *) (pcmd)) = rejectError;

	/* Xmit ELS RJT <err> response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0129 Xmit ELS RJT x%x response tag x%x "
			"Data: x%x x%x x%x x%x x%x\n",
			phba->brd_no,
			rejectError, elsiocb->iocb.ulpIoTag,
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

	phba->fc_stat.elsXmitLSRJT++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;
	spin_lock_irq(phba->host->host_lock);
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	spin_unlock_irq(phba->host->host_lock);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2055
		return 1;
已提交
2056
	}
2057
	return 0;
已提交
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
}

int
lpfc_els_rsp_adisc_acc(struct lpfc_hba * phba,
		       struct lpfc_iocbq * oldiocb, struct lpfc_nodelist * ndlp)
{
	ADISC *ap;
	IOCB_t *icmd;
	IOCB_t *oldcmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;
	int rc;

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */

	cmdsize = sizeof (uint32_t) + sizeof (ADISC);
2078 2079 2080
	elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
					ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
	if (!elsiocb)
2081
		return 1;
已提交
2082 2083 2084 2085 2086 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

	/* Xmit ADISC ACC response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0130 Xmit ADISC ACC response tag x%x "
			"Data: x%x x%x x%x x%x x%x\n",
			phba->brd_no,
			elsiocb->iocb.ulpIoTag,
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
	pcmd += sizeof (uint32_t);

	ap = (ADISC *) (pcmd);
	ap->hardAL_PA = phba->fc_pref_ALPA;
	memcpy(&ap->portName, &phba->fc_portname, sizeof (struct lpfc_name));
	memcpy(&ap->nodeName, &phba->fc_nodename, sizeof (struct lpfc_name));
	ap->DID = be32_to_cpu(phba->fc_myDID);

	phba->fc_stat.elsXmitACC++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;
	spin_lock_irq(phba->host->host_lock);
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	spin_unlock_irq(phba->host->host_lock);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2113
		return 1;
已提交
2114
	}
2115
	return 0;
已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
}

int
lpfc_els_rsp_prli_acc(struct lpfc_hba * phba,
		      struct lpfc_iocbq * oldiocb, struct lpfc_nodelist * ndlp)
{
	PRLI *npr;
	lpfc_vpd_t *vpd;
	IOCB_t *icmd;
	IOCB_t *oldcmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;
	int rc;

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */

	cmdsize = sizeof (uint32_t) + sizeof (PRLI);
2137
	elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry, ndlp,
2138
		ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
2139 2140
	if (!elsiocb)
		return 1;
已提交
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189

	/* Xmit PRLI ACC response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0131 Xmit PRLI ACC response tag x%x "
			"Data: x%x x%x x%x x%x x%x\n",
			phba->brd_no,
			elsiocb->iocb.ulpIoTag,
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	*((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
	pcmd += sizeof (uint32_t);

	/* For PRLI, remainder of payload is PRLI parameter page */
	memset(pcmd, 0, sizeof (PRLI));

	npr = (PRLI *) pcmd;
	vpd = &phba->vpd;
	/*
	 * If our firmware version is 3.20 or later,
	 * set the following bits for FC-TAPE support.
	 */
	if (vpd->rev.feaLevelHigh >= 0x02) {
		npr->ConfmComplAllowed = 1;
		npr->Retry = 1;
		npr->TaskRetryIdReq = 1;
	}

	npr->acceptRspCode = PRLI_REQ_EXECUTED;
	npr->estabImagePair = 1;
	npr->readXferRdyDis = 1;
	npr->ConfmComplAllowed = 1;

	npr->prliType = PRLI_FCP_TYPE;
	npr->initiatorFunc = 1;

	phba->fc_stat.elsXmitACC++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;

	spin_lock_irq(phba->host->host_lock);
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	spin_unlock_irq(phba->host->host_lock);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2190
		return 1;
已提交
2191
	}
2192
	return 0;
已提交
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
}

static int
lpfc_els_rsp_rnid_acc(struct lpfc_hba * phba,
		      uint8_t format,
		      struct lpfc_iocbq * oldiocb, struct lpfc_nodelist * ndlp)
{
	RNID *rn;
	IOCB_t *icmd;
	IOCB_t *oldcmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;
	int rc;

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];

	cmdsize = sizeof (uint32_t) + sizeof (uint32_t)
		+ (2 * sizeof (struct lpfc_name));
	if (format)
		cmdsize += sizeof (RNID_TOP_DISC);

2218 2219 2220
	elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
					ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
	if (!elsiocb)
2221
		return 1;
已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 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

	/* Xmit RNID ACC response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0132 Xmit RNID ACC response tag x%x "
			"Data: x%x\n",
			phba->brd_no,
			elsiocb->iocb.ulpIoTag,
			elsiocb->iocb.ulpContext);

	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
	pcmd += sizeof (uint32_t);

	memset(pcmd, 0, sizeof (RNID));
	rn = (RNID *) (pcmd);
	rn->Format = format;
	rn->CommonLen = (2 * sizeof (struct lpfc_name));
	memcpy(&rn->portName, &phba->fc_portname, sizeof (struct lpfc_name));
	memcpy(&rn->nodeName, &phba->fc_nodename, sizeof (struct lpfc_name));
	switch (format) {
	case 0:
		rn->SpecificLen = 0;
		break;
	case RNID_TOPOLOGY_DISC:
		rn->SpecificLen = sizeof (RNID_TOP_DISC);
		memcpy(&rn->un.topologyDisc.portName,
		       &phba->fc_portname, sizeof (struct lpfc_name));
		rn->un.topologyDisc.unitType = RNID_HBA;
		rn->un.topologyDisc.physPort = 0;
		rn->un.topologyDisc.attachedNodes = 0;
		break;
	default:
		rn->CommonLen = 0;
		rn->SpecificLen = 0;
		break;
	}

	phba->fc_stat.elsXmitACC++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;
	elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
				    * it could be freed */

	spin_lock_irq(phba->host->host_lock);
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	spin_unlock_irq(phba->host->host_lock);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2273
		return 1;
已提交
2274
	}
2275
	return 0;
已提交
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
}

int
lpfc_els_disc_adisc(struct lpfc_hba * phba)
{
	int sentadisc;
	struct lpfc_nodelist *ndlp, *next_ndlp;

	sentadisc = 0;
	/* go thru NPR list and issue any remaining ELS ADISCs */
	list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
			nlp_listp) {
		if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
			if (ndlp->nlp_flag & NLP_NPR_ADISC) {
				ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2291
				ndlp->nlp_prev_state = ndlp->nlp_state;
已提交
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
				ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
				lpfc_nlp_list(phba, ndlp,
					NLP_ADISC_LIST);
				lpfc_issue_els_adisc(phba, ndlp, 0);
				sentadisc++;
				phba->num_disc_nodes++;
				if (phba->num_disc_nodes >=
				    phba->cfg_discovery_threads) {
					spin_lock_irq(phba->host->host_lock);
					phba->fc_flag |= FC_NLP_MORE;
					spin_unlock_irq(phba->host->host_lock);
					break;
				}
			}
		}
	}
	if (sentadisc == 0) {
		spin_lock_irq(phba->host->host_lock);
		phba->fc_flag &= ~FC_NLP_MORE;
		spin_unlock_irq(phba->host->host_lock);
	}
2313
	return sentadisc;
已提交
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
}

int
lpfc_els_disc_plogi(struct lpfc_hba * phba)
{
	int sentplogi;
	struct lpfc_nodelist *ndlp, *next_ndlp;

	sentplogi = 0;
	/* go thru NPR list and issue any remaining ELS PLOGIs */
	list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
				nlp_listp) {
		if ((ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
		   (!(ndlp->nlp_flag & NLP_DELAY_TMO))) {
			if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
2329
				ndlp->nlp_prev_state = ndlp->nlp_state;
已提交
2330 2331
				ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
				lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
2332
				lpfc_issue_els_plogi(phba, ndlp->nlp_DID, 0);
已提交
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
				sentplogi++;
				phba->num_disc_nodes++;
				if (phba->num_disc_nodes >=
				    phba->cfg_discovery_threads) {
					spin_lock_irq(phba->host->host_lock);
					phba->fc_flag |= FC_NLP_MORE;
					spin_unlock_irq(phba->host->host_lock);
					break;
				}
			}
		}
	}
	if (sentplogi == 0) {
		spin_lock_irq(phba->host->host_lock);
		phba->fc_flag &= ~FC_NLP_MORE;
		spin_unlock_irq(phba->host->host_lock);
	}
2350
	return sentplogi;
已提交
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
}

int
lpfc_els_flush_rscn(struct lpfc_hba * phba)
{
	struct lpfc_dmabuf *mp;
	int i;

	for (i = 0; i < phba->fc_rscn_id_cnt; i++) {
		mp = phba->fc_rscn_id_list[i];
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
		phba->fc_rscn_id_list[i] = NULL;
	}
	phba->fc_rscn_id_cnt = 0;
	spin_lock_irq(phba->host->host_lock);
	phba->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
	spin_unlock_irq(phba->host->host_lock);
	lpfc_can_disctmo(phba);
2370
	return 0;
已提交
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
}

int
lpfc_rscn_payload_check(struct lpfc_hba * phba, uint32_t did)
{
	D_ID ns_did;
	D_ID rscn_did;
	struct lpfc_dmabuf *mp;
	uint32_t *lp;
	uint32_t payload_len, cmd, i, match;

	ns_did.un.word = did;
	match = 0;

	/* Never match fabric nodes for RSCNs */
	if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
		return(0);

	/* If we are doing a FULL RSCN rediscovery, match everything */
	if (phba->fc_flag & FC_RSCN_DISCOVERY) {
2391
		return did;
已提交
2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
	}

	for (i = 0; i < phba->fc_rscn_id_cnt; i++) {
		mp = phba->fc_rscn_id_list[i];
		lp = (uint32_t *) mp->virt;
		cmd = *lp++;
		payload_len = be32_to_cpu(cmd) & 0xffff; /* payload length */
		payload_len -= sizeof (uint32_t);	/* take off word 0 */
		while (payload_len) {
			rscn_did.un.word = *lp++;
			rscn_did.un.word = be32_to_cpu(rscn_did.un.word);
			payload_len -= sizeof (uint32_t);
			switch (rscn_did.un.b.resv) {
			case 0:	/* Single N_Port ID effected */
				if (ns_did.un.word == rscn_did.un.word) {
					match = did;
				}
				break;
			case 1:	/* Whole N_Port Area effected */
				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
				    && (ns_did.un.b.area == rscn_did.un.b.area))
					{
						match = did;
					}
				break;
			case 2:	/* Whole N_Port Domain effected */
				if (ns_did.un.b.domain == rscn_did.un.b.domain)
					{
						match = did;
					}
				break;
			case 3:	/* Whole Fabric effected */
				match = did;
				break;
			default:
				/* Unknown Identifier in RSCN list */
				lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
						"%d:0217 Unknown Identifier in "
						"RSCN payload Data: x%x\n",
						phba->brd_no, rscn_did.un.word);
				break;
			}
			if (match) {
				break;
			}
		}
	}
2439
	return match;
已提交
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
}

static int
lpfc_rscn_recovery_check(struct lpfc_hba * phba)
{
	struct lpfc_nodelist *ndlp = NULL, *next_ndlp;
	struct list_head *listp;
	struct list_head *node_list[7];
	int i;

	/* Look at all nodes effected by pending RSCNs and move
	 * them to NPR list.
	 */
	node_list[0] = &phba->fc_npr_list;  /* MUST do this list first */
	node_list[1] = &phba->fc_nlpmap_list;
	node_list[2] = &phba->fc_nlpunmap_list;
	node_list[3] = &phba->fc_prli_list;
	node_list[4] = &phba->fc_reglogin_list;
	node_list[5] = &phba->fc_adisc_list;
	node_list[6] = &phba->fc_plogi_list;
	for (i = 0; i < 7; i++) {
		listp = node_list[i];
		if (list_empty(listp))
			continue;

		list_for_each_entry_safe(ndlp, next_ndlp, listp, nlp_listp) {
			if (!(lpfc_rscn_payload_check(phba, ndlp->nlp_DID)))
				continue;

			lpfc_disc_state_machine(phba, ndlp, NULL,
					NLP_EVT_DEVICE_RECOVERY);
2471 2472 2473 2474

			/* Make sure NLP_DELAY_TMO is NOT running
			 * after a device recovery event.
			 */
2475 2476
			if (ndlp->nlp_flag & NLP_DELAY_TMO)
				lpfc_cancel_retry_delay_tmo(phba, ndlp);
已提交
2477 2478
		}
	}
2479
	return 0;
已提交
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
}

static int
lpfc_els_rcv_rscn(struct lpfc_hba * phba,
		  struct lpfc_iocbq * cmdiocb,
		  struct lpfc_nodelist * ndlp, uint8_t newnode)
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	IOCB_t *icmd;
	uint32_t payload_len, cmd;

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

	cmd = *lp++;
	payload_len = be32_to_cpu(cmd) & 0xffff;	/* payload length */
	payload_len -= sizeof (uint32_t);	/* take off word 0 */
	cmd &= ELS_CMD_MASK;

	/* RSCN received */
	lpfc_printf_log(phba,
			KERN_INFO,
			LOG_DISCOVERY,
			"%d:0214 RSCN received Data: x%x x%x x%x x%x\n",
			phba->brd_no,
			phba->fc_flag, payload_len, *lp, phba->fc_rscn_id_cnt);

	/* If we are about to begin discovery, just ACC the RSCN.
	 * Discovery processing will satisfy it.
	 */
	if (phba->hba_state < LPFC_NS_QRY) {
		lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL,
								newnode);
2515
		return 0;
已提交
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556
	}

	/* If we are already processing an RSCN, save the received
	 * RSCN payload buffer, cmdiocb->context2 to process later.
	 */
	if (phba->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
		if ((phba->fc_rscn_id_cnt < FC_MAX_HOLD_RSCN) &&
		    !(phba->fc_flag & FC_RSCN_DISCOVERY)) {
			spin_lock_irq(phba->host->host_lock);
			phba->fc_flag |= FC_RSCN_MODE;
			spin_unlock_irq(phba->host->host_lock);
			phba->fc_rscn_id_list[phba->fc_rscn_id_cnt++] = pcmd;

			/* If we zero, cmdiocb->context2, the calling
			 * routine will not try to free it.
			 */
			cmdiocb->context2 = NULL;

			/* Deferred RSCN */
			lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
					"%d:0235 Deferred RSCN "
					"Data: x%x x%x x%x\n",
					phba->brd_no, phba->fc_rscn_id_cnt,
					phba->fc_flag, phba->hba_state);
		} else {
			spin_lock_irq(phba->host->host_lock);
			phba->fc_flag |= FC_RSCN_DISCOVERY;
			spin_unlock_irq(phba->host->host_lock);
			/* ReDiscovery RSCN */
			lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
					"%d:0234 ReDiscovery RSCN "
					"Data: x%x x%x x%x\n",
					phba->brd_no, phba->fc_rscn_id_cnt,
					phba->fc_flag, phba->hba_state);
		}
		/* Send back ACC */
		lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL,
								newnode);

		/* send RECOVERY event for ALL nodes that match RSCN payload */
		lpfc_rscn_recovery_check(phba);
2557
		return 0;
已提交
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
	}

	phba->fc_flag |= FC_RSCN_MODE;
	phba->fc_rscn_id_list[phba->fc_rscn_id_cnt++] = pcmd;
	/*
	 * If we zero, cmdiocb->context2, the calling routine will
	 * not try to free it.
	 */
	cmdiocb->context2 = NULL;

	lpfc_set_disctmo(phba);

	/* Send back ACC */
	lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, newnode);

	/* send RECOVERY event for ALL nodes that match RSCN payload */
	lpfc_rscn_recovery_check(phba);

2576
	return lpfc_els_handle_rscn(phba);
已提交
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597
}

int
lpfc_els_handle_rscn(struct lpfc_hba * phba)
{
	struct lpfc_nodelist *ndlp;

	/* Start timer for RSCN processing */
	lpfc_set_disctmo(phba);

	/* RSCN processed */
	lpfc_printf_log(phba,
			KERN_INFO,
			LOG_DISCOVERY,
			"%d:0215 RSCN processed Data: x%x x%x x%x x%x\n",
			phba->brd_no,
			phba->fc_flag, 0, phba->fc_rscn_id_cnt,
			phba->hba_state);

	/* To process RSCN, first compare RSCN data with NameServer */
	phba->fc_ns_retry = 0;
2598 2599
	ndlp = lpfc_findnode_did(phba, NLP_SEARCH_UNMAPPED, NameServer_DID);
	if (ndlp) {
已提交
2600 2601 2602 2603
		/* Good ndlp, issue CT Request to NameServer */
		if (lpfc_ns_cmd(phba, ndlp, SLI_CTNS_GID_FT) == 0) {
			/* Wait for NameServer query cmpl before we can
			   continue */
2604
			return 1;
已提交
2605 2606 2607 2608
		}
	} else {
		/* If login to NameServer does not exist, issue one */
		/* Good status, issue PLOGI to NameServer */
2609 2610
		ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, NameServer_DID);
		if (ndlp) {
已提交
2611 2612
			/* Wait for NameServer login cmpl before we can
			   continue */
2613
			return 1;
已提交
2614
		}
2615 2616
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp) {
已提交
2617
			lpfc_els_flush_rscn(phba);
2618
			return 0;
已提交
2619 2620 2621
		} else {
			lpfc_nlp_init(phba, ndlp, NameServer_DID);
			ndlp->nlp_type |= NLP_FABRIC;
2622
			ndlp->nlp_prev_state = ndlp->nlp_state;
已提交
2623
			ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
2624
			lpfc_issue_els_plogi(phba, NameServer_DID, 0);
已提交
2625 2626
			/* Wait for NameServer login cmpl before we can
			   continue */
2627
			return 1;
已提交
2628 2629 2630 2631
		}
	}

	lpfc_els_flush_rscn(phba);
2632
	return 0;
已提交
2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665
}

static int
lpfc_els_rcv_flogi(struct lpfc_hba * phba,
		   struct lpfc_iocbq * cmdiocb,
		   struct lpfc_nodelist * ndlp, uint8_t newnode)
{
	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	uint32_t *lp = (uint32_t *) pcmd->virt;
	IOCB_t *icmd = &cmdiocb->iocb;
	struct serv_parm *sp;
	LPFC_MBOXQ_t *mbox;
	struct ls_rjt stat;
	uint32_t cmd, did;
	int rc;

	cmd = *lp++;
	sp = (struct serv_parm *) lp;

	/* FLOGI received */

	lpfc_set_disctmo(phba);

	if (phba->fc_topology == TOPOLOGY_LOOP) {
		/* We should never receive a FLOGI in loop mode, ignore it */
		did = icmd->un.elsreq64.remoteID;

		/* An FLOGI ELS command <elsCmd> was received from DID <did> in
		   Loop Mode */
		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
				"%d:0113 An FLOGI ELS command x%x was received "
				"from DID x%x in Loop Mode\n",
				phba->brd_no, cmd, did);
2666
		return 1;
已提交
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
	}

	did = Fabric_DID;

	if ((lpfc_check_sparm(phba, ndlp, sp, CLASS3))) {
		/* For a FLOGI we accept, then if our portname is greater
		 * then the remote portname we initiate Nport login.
		 */

		rc = memcmp(&phba->fc_portname, &sp->portName,
			    sizeof (struct lpfc_name));

		if (!rc) {
			if ((mbox = mempool_alloc(phba->mbox_mem_pool,
						  GFP_KERNEL)) == 0) {
2682
				return 1;
已提交
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
			}
			lpfc_linkdown(phba);
			lpfc_init_link(phba, mbox,
				       phba->cfg_topology,
				       phba->cfg_link_speed);
			mbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
			rc = lpfc_sli_issue_mbox
				(phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
			if (rc == MBX_NOT_FINISHED) {
				mempool_free( mbox, phba->mbox_mem_pool);
			}
2695
			return 1;
2696
		} else if (rc > 0) {	/* greater than */
已提交
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709
			spin_lock_irq(phba->host->host_lock);
			phba->fc_flag |= FC_PT2PT_PLOGI;
			spin_unlock_irq(phba->host->host_lock);
		}
		phba->fc_flag |= FC_PT2PT;
		phba->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
	} else {
		/* 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;
		lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
2710
		return 1;
已提交
2711 2712 2713 2714 2715
	}

	/* Send back ACC */
	lpfc_els_rsp_acc(phba, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL, newnode);

2716
	return 0;
已提交
2717 2718 2719 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
}

static int
lpfc_els_rcv_rnid(struct lpfc_hba * phba,
		  struct lpfc_iocbq * cmdiocb, struct lpfc_nodelist * ndlp)
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	IOCB_t *icmd;
	RNID *rn;
	struct ls_rjt stat;
	uint32_t cmd, did;

	icmd = &cmdiocb->iocb;
	did = icmd->un.elsreq64.remoteID;
	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;

	cmd = *lp++;
	rn = (RNID *) lp;

	/* RNID received */

	switch (rn->Format) {
	case 0:
	case RNID_TOPOLOGY_DISC:
		/* Send back ACC */
		lpfc_els_rsp_rnid_acc(phba, rn->Format, cmdiocb, ndlp);
		break;
	default:
		/* Reject this request because format not supported */
		stat.un.b.lsRjtRsvd0 = 0;
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
		stat.un.b.vendorUnique = 0;
		lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
	}
2754
	return 0;
已提交
2755 2756 2757
}

static int
2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
lpfc_els_rcv_lirr(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
		 struct lpfc_nodelist * ndlp)
{
	struct ls_rjt stat;

	/* For now, unconditionally reject this command */
	stat.un.b.lsRjtRsvd0 = 0;
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
	stat.un.b.vendorUnique = 0;
	lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
	return 0;
}

2772
static void
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
lpfc_els_rsp_rps_acc(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
{
	struct lpfc_sli *psli;
	struct lpfc_sli_ring *pring;
	MAILBOX_t *mb;
	IOCB_t *icmd;
	RPS_RSP *rps_rsp;
	uint8_t *pcmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_nodelist *ndlp;
	uint16_t xri, status;
	uint32_t cmdsize;

	psli = &phba->sli;
	pring = &psli->ring[LPFC_ELS_RING];
	mb = &pmb->mb;

	ndlp = (struct lpfc_nodelist *) pmb->context2;
	xri = (uint16_t) ((unsigned long)(pmb->context1));
	pmb->context1 = 0;
	pmb->context2 = 0;

	if (mb->mbxStatus) {
		mempool_free( pmb, phba->mbox_mem_pool);
		return;
	}

	cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
	mempool_free( pmb, phba->mbox_mem_pool);
2802 2803
	elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, lpfc_max_els_tries, ndlp,
						ndlp->nlp_DID, ELS_CMD_ACC);
2804
	if (!elsiocb)
2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
		return;

	icmd = &elsiocb->iocb;
	icmd->ulpContext = xri;

	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
	pcmd += sizeof (uint32_t); /* Skip past command */
	rps_rsp = (RPS_RSP *)pcmd;

	if (phba->fc_topology != TOPOLOGY_LOOP)
		status = 0x10;
	else
		status = 0x8;
	if (phba->fc_flag & FC_FABRIC)
		status |= 0x4;

	rps_rsp->rsvd1 = 0;
	rps_rsp->portStatus = be16_to_cpu(status);
	rps_rsp->linkFailureCnt = be32_to_cpu(mb->un.varRdLnk.linkFailureCnt);
	rps_rsp->lossSyncCnt = be32_to_cpu(mb->un.varRdLnk.lossSyncCnt);
	rps_rsp->lossSignalCnt = be32_to_cpu(mb->un.varRdLnk.lossSignalCnt);
	rps_rsp->primSeqErrCnt = be32_to_cpu(mb->un.varRdLnk.primSeqErrCnt);
	rps_rsp->invalidXmitWord = be32_to_cpu(mb->un.varRdLnk.invalidXmitWord);
	rps_rsp->crcCnt = be32_to_cpu(mb->un.varRdLnk.crcCnt);

	/* Xmit ELS RPS ACC response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0128 Xmit ELS RPS ACC response tag x%x "
			"Data: x%x x%x x%x x%x x%x\n",
			phba->brd_no,
			elsiocb->iocb.ulpIoTag,
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

	elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;
	phba->fc_stat.elsXmitACC++;
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
	}
	return;
}

static int
lpfc_els_rcv_rps(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
已提交
2850 2851 2852
		 struct lpfc_nodelist * ndlp)
{
	uint32_t *lp;
2853 2854 2855 2856 2857 2858
	uint8_t flag;
	LPFC_MBOXQ_t *mbox;
	struct lpfc_dmabuf *pcmd;
	RPS *rps;
	struct ls_rjt stat;

2859 2860
	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
2861 2862 2863 2864 2865 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
		stat.un.b.lsRjtRsvd0 = 0;
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
		stat.un.b.vendorUnique = 0;
		lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
	}

	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;
	flag = (be32_to_cpu(*lp++) & 0xf);
	rps = (RPS *) lp;

	if ((flag == 0) ||
	    ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
	    ((flag == 2) && (memcmp(&rps->un.portName, &phba->fc_portname,
			   sizeof (struct lpfc_name)) == 0))) {
		if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC))) {
			lpfc_read_lnk_stat(phba, mbox);
			mbox->context1 =
			    (void *)((unsigned long)cmdiocb->iocb.ulpContext);
			mbox->context2 = ndlp;
			mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
			if (lpfc_sli_issue_mbox (phba, mbox,
			    (MBX_NOWAIT | MBX_STOP_IOCB)) != MBX_NOT_FINISHED) {
				/* Mbox completion will send ELS Response */
				return 0;
			}
			mempool_free(mbox, phba->mbox_mem_pool);
		}
	}
	stat.un.b.lsRjtRsvd0 = 0;
	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
	stat.un.b.vendorUnique = 0;
	lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
	return 0;
}

2899
static int
2900 2901 2902
lpfc_els_rsp_rpl_acc(struct lpfc_hba * phba, uint16_t cmdsize,
		 struct lpfc_iocbq * oldiocb, struct lpfc_nodelist * ndlp)
{
已提交
2903
	IOCB_t *icmd;
2904 2905 2906
	IOCB_t *oldcmd;
	RPL_RSP rpl_rsp;
	struct lpfc_iocbq *elsiocb;
已提交
2907 2908
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
2909
	uint8_t *pcmd;
已提交
2910 2911

	psli = &phba->sli;
2912 2913
	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */

2914 2915 2916
	elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
					ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
	if (!elsiocb)
2917
		return 1;
2918

2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */

	pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
	pcmd += sizeof (uint16_t);
	*((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
	pcmd += sizeof(uint16_t);

	/* Setup the RPL ACC payload */
	rpl_rsp.listLen = be32_to_cpu(1);
	rpl_rsp.index = 0;
	rpl_rsp.port_num_blk.portNum = 0;
	rpl_rsp.port_num_blk.portID = be32_to_cpu(phba->fc_myDID);
	memcpy(&rpl_rsp.port_num_blk.portName, &phba->fc_portname,
	    sizeof(struct lpfc_name));

	memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));


	/* Xmit ELS RPL ACC response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0128 Xmit ELS RPL ACC response tag x%x "
			"Data: x%x x%x x%x x%x x%x\n",
			phba->brd_no,
			elsiocb->iocb.ulpIoTag,
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

	elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;

	phba->fc_stat.elsXmitACC++;
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
		return 1;
	}
	return 0;
}

static int
lpfc_els_rcv_rpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
		 struct lpfc_nodelist * ndlp)
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	uint32_t maxsize;
	uint16_t cmdsize;
	RPL *rpl;
	struct ls_rjt stat;

2970 2971
	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
2972 2973 2974 2975 2976 2977 2978
		stat.un.b.lsRjtRsvd0 = 0;
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
		stat.un.b.vendorUnique = 0;
		lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
	}

已提交
2979 2980
	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;
2981
	rpl = (RPL *) (lp + 1);
已提交
2982

2983
	maxsize = be32_to_cpu(rpl->maxsize);
已提交
2984

2985 2986 2987 2988 2989
	/* We support only one port */
	if ((rpl->index == 0) &&
	    ((maxsize == 0) ||
	     ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
		cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
2990
	} else {
2991 2992 2993
		cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
	}
	lpfc_els_rsp_rpl_acc(phba, cmdsize, cmdiocb, ndlp);
已提交
2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024

	return 0;
}

static int
lpfc_els_rcv_farp(struct lpfc_hba * phba,
		  struct lpfc_iocbq * cmdiocb, struct lpfc_nodelist * ndlp)
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	IOCB_t *icmd;
	FARP *fp;
	uint32_t cmd, cnt, did;

	icmd = &cmdiocb->iocb;
	did = icmd->un.elsreq64.remoteID;
	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;

	cmd = *lp++;
	fp = (FARP *) lp;

	/* FARP-REQ received from DID <did> */
	lpfc_printf_log(phba,
			 KERN_INFO,
			 LOG_IP,
			 "%d:0601 FARP-REQ received from DID x%x\n",
			 phba->brd_no, did);

	/* We will only support match on WWPN or WWNN */
	if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
3025
		return 0;
已提交
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047
	}

	cnt = 0;
	/* If this FARP command is searching for my portname */
	if (fp->Mflags & FARP_MATCH_PORT) {
		if (memcmp(&fp->RportName, &phba->fc_portname,
			   sizeof (struct lpfc_name)) == 0)
			cnt = 1;
	}

	/* If this FARP command is searching for my nodename */
	if (fp->Mflags & FARP_MATCH_NODE) {
		if (memcmp(&fp->RnodeName, &phba->fc_nodename,
			   sizeof (struct lpfc_name)) == 0)
			cnt = 1;
	}

	if (cnt) {
		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
		   (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
			/* Log back into the node before sending the FARP. */
			if (fp->Rflags & FARP_REQUEST_PLOGI) {
3048
				ndlp->nlp_prev_state = ndlp->nlp_state;
已提交
3049 3050
				ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
				lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
3051
				lpfc_issue_els_plogi(phba, ndlp->nlp_DID, 0);
已提交
3052 3053 3054 3055 3056 3057 3058 3059
			}

			/* Send a FARP response to that node */
			if (fp->Rflags & FARP_REQUEST_FARPR) {
				lpfc_issue_els_farpr(phba, did, 0);
			}
		}
	}
3060
	return 0;
已提交
3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092
}

static int
lpfc_els_rcv_farpr(struct lpfc_hba * phba,
		   struct lpfc_iocbq * cmdiocb, struct lpfc_nodelist * ndlp)
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	IOCB_t *icmd;
	uint32_t cmd, did;

	icmd = &cmdiocb->iocb;
	did = icmd->un.elsreq64.remoteID;
	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;

	cmd = *lp++;
	/* FARP-RSP received from DID <did> */
	lpfc_printf_log(phba,
			 KERN_INFO,
			 LOG_IP,
			 "%d:0600 FARP-RSP received from DID x%x\n",
			 phba->brd_no, did);

	/* ACCEPT the Farp resp request */
	lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);

	return 0;
}

static int
lpfc_els_rcv_fan(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
3093
		 struct lpfc_nodelist * fan_ndlp)
已提交
3094 3095 3096 3097 3098
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	IOCB_t *icmd;
	uint32_t cmd, did;
3099 3100 3101 3102 3103 3104
	FAN *fp;
	struct lpfc_nodelist *ndlp, *next_ndlp;

	/* FAN received */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS, "%d:265 FAN received\n",
								phba->brd_no);
已提交
3105 3106 3107

	icmd = &cmdiocb->iocb;
	did = icmd->un.elsreq64.remoteID;
3108 3109
	pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
	lp = (uint32_t *)pcmd->virt;
已提交
3110 3111

	cmd = *lp++;
3112
	fp = (FAN *)lp;
已提交
3113

3114
	/* FAN received; Fan does not have a reply sequence */
已提交
3115 3116

	if (phba->hba_state == LPFC_LOCAL_CFG_LINK) {
3117 3118 3119 3120 3121 3122 3123
		if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
			sizeof(struct lpfc_name)) != 0) ||
		    (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
			sizeof(struct lpfc_name)) != 0)) {
			/*
			 * This node has switched fabrics.  FLOGI is required
			 * Clean up the old rpi's
已提交
3124
			 */
3125 3126 3127 3128 3129 3130 3131 3132 3133 3134

			list_for_each_entry_safe(ndlp, next_ndlp,
				&phba->fc_npr_list, nlp_listp) {

				if (ndlp->nlp_type & NLP_FABRIC) {
					/*
					 * Clean up old Fabric, Nameserver and
					 * other NLP_FABRIC logins
					 */
					lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
3135
				} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145
					/* Fail outstanding I/O now since this
					 * device is marked for PLOGI
					 */
					lpfc_unreg_rpi(phba, ndlp);
				}
			}

			phba->hba_state = LPFC_FLOGI;
			lpfc_set_disctmo(phba);
			lpfc_initial_flogi(phba);
3146
			return 0;
已提交
3147
		}
3148 3149 3150 3151 3152
		/* Discovery not needed,
		 * move the nodes to their original state.
		 */
		list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
			nlp_listp) {
已提交
3153

3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172
			switch (ndlp->nlp_prev_state) {
			case NLP_STE_UNMAPPED_NODE:
				ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
				ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
				lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
				break;

			case NLP_STE_MAPPED_NODE:
				ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
				ndlp->nlp_state = NLP_STE_MAPPED_NODE;
				lpfc_nlp_list(phba, ndlp, NLP_MAPPED_LIST);
				break;

			default:
				break;
			}
		}

		/* Start discovery - this should just do CLEAR_LA */
已提交
3173 3174
		lpfc_disc_start(phba);
	}
3175
	return 0;
已提交
3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251
}

void
lpfc_els_timeout(unsigned long ptr)
{
	struct lpfc_hba *phba;
	unsigned long iflag;

	phba = (struct lpfc_hba *)ptr;
	if (phba == 0)
		return;
	spin_lock_irqsave(phba->host->host_lock, iflag);
	if (!(phba->work_hba_events & WORKER_ELS_TMO)) {
		phba->work_hba_events |= WORKER_ELS_TMO;
		if (phba->work_wait)
			wake_up(phba->work_wait);
	}
	spin_unlock_irqrestore(phba->host->host_lock, iflag);
	return;
}

void
lpfc_els_timeout_handler(struct lpfc_hba *phba)
{
	struct lpfc_sli_ring *pring;
	struct lpfc_iocbq *tmp_iocb, *piocb;
	IOCB_t *cmd = NULL;
	struct lpfc_dmabuf *pcmd;
	struct list_head *dlp;
	uint32_t *elscmd;
	uint32_t els_command;
	uint32_t timeout;
	uint32_t remote_ID;

	if (phba == 0)
		return;
	spin_lock_irq(phba->host->host_lock);
	/* If the timer is already canceled do nothing */
	if (!(phba->work_hba_events & WORKER_ELS_TMO)) {
		spin_unlock_irq(phba->host->host_lock);
		return;
	}
	timeout = (uint32_t)(phba->fc_ratov << 1);

	pring = &phba->sli.ring[LPFC_ELS_RING];
	dlp = &pring->txcmplq;

	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
		cmd = &piocb->iocb;

		if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
			continue;
		}
		pcmd = (struct lpfc_dmabuf *) piocb->context2;
		elscmd = (uint32_t *) (pcmd->virt);
		els_command = *elscmd;

		if ((els_command == ELS_CMD_FARP)
		    || (els_command == ELS_CMD_FARPR)) {
			continue;
		}

		if (piocb->drvrTimeout > 0) {
			if (piocb->drvrTimeout >= timeout) {
				piocb->drvrTimeout -= timeout;
			} else {
				piocb->drvrTimeout = 0;
			}
			continue;
		}

		list_del(&piocb->list);
		pring->txcmplq_cnt--;

		if (cmd->ulpCommand == CMD_GEN_REQUEST64_CR) {
			struct lpfc_nodelist *ndlp;
3252
			spin_unlock_irq(phba->host->host_lock);
已提交
3253
			ndlp = lpfc_findnode_rpi(phba, cmd->ulpContext);
3254
			spin_lock_irq(phba->host->host_lock);
已提交
3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279
			remote_ID = ndlp->nlp_DID;
			if (cmd->un.elsreq64.bdl.ulpIoTag32) {
				lpfc_sli_issue_abort_iotag32(phba,
					pring, piocb);
			}
		} else {
			remote_ID = cmd->un.elsreq64.remoteID;
		}

		lpfc_printf_log(phba,
				KERN_ERR,
				LOG_ELS,
				"%d:0127 ELS timeout Data: x%x x%x x%x x%x\n",
				phba->brd_no, els_command,
				remote_ID, cmd->ulpCommand, cmd->ulpIoTag);

		/*
		 * The iocb has timed out; abort it.
		 */
		if (piocb->iocb_cmpl) {
			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
			spin_unlock_irq(phba->host->host_lock);
			(piocb->iocb_cmpl) (phba, piocb, piocb);
			spin_lock_irq(phba->host->host_lock);
3280 3281
		} else
			lpfc_sli_release_iocbq(phba, piocb);
已提交
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330
	}
	if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt) {
		phba->els_tmofunc.expires = jiffies + HZ * timeout;
		add_timer(&phba->els_tmofunc);
	}
	spin_unlock_irq(phba->host->host_lock);
}

void
lpfc_els_flush_cmd(struct lpfc_hba * phba)
{
	struct lpfc_sli_ring *pring;
	struct lpfc_iocbq *tmp_iocb, *piocb;
	IOCB_t *cmd = NULL;
	struct lpfc_dmabuf *pcmd;
	uint32_t *elscmd;
	uint32_t els_command;

	pring = &phba->sli.ring[LPFC_ELS_RING];
	spin_lock_irq(phba->host->host_lock);
	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
		cmd = &piocb->iocb;

		if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
			continue;
		}

		/* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
		if ((cmd->ulpCommand == CMD_QUE_RING_BUF_CN) ||
		    (cmd->ulpCommand == CMD_QUE_RING_BUF64_CN) ||
		    (cmd->ulpCommand == CMD_CLOSE_XRI_CN) ||
		    (cmd->ulpCommand == CMD_ABORT_XRI_CN)) {
			continue;
		}

		pcmd = (struct lpfc_dmabuf *) piocb->context2;
		elscmd = (uint32_t *) (pcmd->virt);
		els_command = *elscmd;

		list_del(&piocb->list);
		pring->txcmplq_cnt--;

		cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
		cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;

		if (piocb->iocb_cmpl) {
			spin_unlock_irq(phba->host->host_lock);
			(piocb->iocb_cmpl) (phba, piocb, piocb);
			spin_lock_irq(phba->host->host_lock);
3331
		} else
3332
			lpfc_sli_release_iocbq(phba, piocb);
已提交
3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354
	}

	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
		cmd = &piocb->iocb;

		if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
			continue;
		}
		pcmd = (struct lpfc_dmabuf *) piocb->context2;
		elscmd = (uint32_t *) (pcmd->virt);
		els_command = *elscmd;

		list_del(&piocb->list);
		pring->txcmplq_cnt--;

		cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
		cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;

		if (piocb->iocb_cmpl) {
			spin_unlock_irq(phba->host->host_lock);
			(piocb->iocb_cmpl) (phba, piocb, piocb);
			spin_lock_irq(phba->host->host_lock);
3355
		} else
3356
			lpfc_sli_release_iocbq(phba, piocb);
已提交
3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426
	}
	spin_unlock_irq(phba->host->host_lock);
	return;
}

void
lpfc_els_unsol_event(struct lpfc_hba * phba,
		     struct lpfc_sli_ring * pring, struct lpfc_iocbq * elsiocb)
{
	struct lpfc_sli *psli;
	struct lpfc_nodelist *ndlp;
	struct lpfc_dmabuf *mp;
	uint32_t *lp;
	IOCB_t *icmd;
	struct ls_rjt stat;
	uint32_t cmd;
	uint32_t did;
	uint32_t newnode;
	uint32_t drop_cmd = 0;	/* by default do NOT drop received cmd */
	uint32_t rjt_err = 0;

	psli = &phba->sli;
	icmd = &elsiocb->iocb;

	if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
		((icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING)) {
		/* Not enough posted buffers; Try posting more buffers */
		phba->fc_stat.NoRcvBuf++;
		lpfc_post_buffer(phba, pring, 0, 1);
		return;
	}

	/* If there are no BDEs associated with this IOCB,
	 * there is nothing to do.
	 */
	if (icmd->ulpBdeCount == 0)
		return;

	/* type of ELS cmd is first 32bit word in packet */
	mp = lpfc_sli_ringpostbuf_get(phba, pring, getPaddr(icmd->un.
							    cont64[0].
							    addrHigh,
							    icmd->un.
							    cont64[0].addrLow));
	if (mp == 0) {
		drop_cmd = 1;
		goto dropit;
	}

	newnode = 0;
	lp = (uint32_t *) mp->virt;
	cmd = *lp++;
	lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], 1, 1);

	if (icmd->ulpStatus) {
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
		drop_cmd = 1;
		goto dropit;
	}

	/* Check to see if link went down during discovery */
	if (lpfc_els_chk_latt(phba)) {
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
		drop_cmd = 1;
		goto dropit;
	}

	did = icmd->un.rcvels.remoteID;
3427 3428
	ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, did);
	if (!ndlp) {
已提交
3429
		/* Cannot find existing Fabric ndlp, so allocate a new one */
3430 3431
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp) {
已提交
3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460
			lpfc_mbuf_free(phba, mp->virt, mp->phys);
			kfree(mp);
			drop_cmd = 1;
			goto dropit;
		}

		lpfc_nlp_init(phba, ndlp, did);
		newnode = 1;
		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK) {
			ndlp->nlp_type |= NLP_FABRIC;
		}
	}

	phba->fc_stat.elsRcvFrame++;
	elsiocb->context1 = ndlp;
	elsiocb->context2 = mp;

	if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
		cmd &= ELS_CMD_MASK;
	}
	/* ELS command <elsCmd> received from NPORT <did> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d:0112 ELS command x%x received from NPORT x%x "
			"Data: x%x\n", phba->brd_no, cmd, did, phba->hba_state);

	switch (cmd) {
	case ELS_CMD_PLOGI:
		phba->fc_stat.elsRcvPLOGI++;
		if (phba->hba_state < LPFC_DISC_AUTH) {
3461
			rjt_err = 1;
已提交
3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475
			break;
		}
		lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_PLOGI);
		break;
	case ELS_CMD_FLOGI:
		phba->fc_stat.elsRcvFLOGI++;
		lpfc_els_rcv_flogi(phba, elsiocb, ndlp, newnode);
		if (newnode) {
			mempool_free( ndlp, phba->nlp_mem_pool);
		}
		break;
	case ELS_CMD_LOGO:
		phba->fc_stat.elsRcvLOGO++;
		if (phba->hba_state < LPFC_DISC_AUTH) {
3476
			rjt_err = 1;
已提交
3477 3478 3479 3480 3481 3482 3483
			break;
		}
		lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
		break;
	case ELS_CMD_PRLO:
		phba->fc_stat.elsRcvPRLO++;
		if (phba->hba_state < LPFC_DISC_AUTH) {
3484
			rjt_err = 1;
已提交
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498
			break;
		}
		lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
		break;
	case ELS_CMD_RSCN:
		phba->fc_stat.elsRcvRSCN++;
		lpfc_els_rcv_rscn(phba, elsiocb, ndlp, newnode);
		if (newnode) {
			mempool_free( ndlp, phba->nlp_mem_pool);
		}
		break;
	case ELS_CMD_ADISC:
		phba->fc_stat.elsRcvADISC++;
		if (phba->hba_state < LPFC_DISC_AUTH) {
3499
			rjt_err = 1;
已提交
3500 3501 3502 3503 3504 3505 3506
			break;
		}
		lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_ADISC);
		break;
	case ELS_CMD_PDISC:
		phba->fc_stat.elsRcvPDISC++;
		if (phba->hba_state < LPFC_DISC_AUTH) {
3507
			rjt_err = 1;
已提交
3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526
			break;
		}
		lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_PDISC);
		break;
	case ELS_CMD_FARPR:
		phba->fc_stat.elsRcvFARPR++;
		lpfc_els_rcv_farpr(phba, elsiocb, ndlp);
		break;
	case ELS_CMD_FARP:
		phba->fc_stat.elsRcvFARP++;
		lpfc_els_rcv_farp(phba, elsiocb, ndlp);
		break;
	case ELS_CMD_FAN:
		phba->fc_stat.elsRcvFAN++;
		lpfc_els_rcv_fan(phba, elsiocb, ndlp);
		break;
	case ELS_CMD_PRLI:
		phba->fc_stat.elsRcvPRLI++;
		if (phba->hba_state < LPFC_DISC_AUTH) {
3527
			rjt_err = 1;
已提交
3528 3529 3530 3531
			break;
		}
		lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
		break;
3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552
	case ELS_CMD_LIRR:
		phba->fc_stat.elsRcvLIRR++;
		lpfc_els_rcv_lirr(phba, elsiocb, ndlp);
		if (newnode) {
			mempool_free( ndlp, phba->nlp_mem_pool);
		}
		break;
	case ELS_CMD_RPS:
		phba->fc_stat.elsRcvRPS++;
		lpfc_els_rcv_rps(phba, elsiocb, ndlp);
		if (newnode) {
			mempool_free( ndlp, phba->nlp_mem_pool);
		}
		break;
	case ELS_CMD_RPL:
		phba->fc_stat.elsRcvRPL++;
		lpfc_els_rcv_rpl(phba, elsiocb, ndlp);
		if (newnode) {
			mempool_free( ndlp, phba->nlp_mem_pool);
		}
		break;
已提交
3553 3554 3555
	case ELS_CMD_RNID:
		phba->fc_stat.elsRcvRNID++;
		lpfc_els_rcv_rnid(phba, elsiocb, ndlp);
3556 3557 3558
		if (newnode) {
			mempool_free( ndlp, phba->nlp_mem_pool);
		}
已提交
3559 3560 3561
		break;
	default:
		/* Unsupported ELS command, reject */
3562
		rjt_err = 1;
已提交
3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577

		/* Unknown ELS command <elsCmd> received from NPORT <did> */
		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
				"%d:0115 Unknown ELS command x%x received from "
				"NPORT x%x\n", phba->brd_no, cmd, did);
		if (newnode) {
			mempool_free( ndlp, phba->nlp_mem_pool);
		}
		break;
	}

	/* check if need to LS_RJT received ELS cmd */
	if (rjt_err) {
		stat.un.b.lsRjtRsvd0 = 0;
		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3578
		stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
已提交
3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591
		stat.un.b.vendorUnique = 0;
		lpfc_els_rsp_reject(phba, stat.un.lsRjtError, elsiocb, ndlp);
	}

	if (elsiocb->context2) {
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
	}
dropit:
	/* check if need to drop received ELS cmd */
	if (drop_cmd == 1) {
		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
				"%d:0111 Dropping received ELS cmd "
3592 3593 3594
				"Data: x%x x%x x%x\n", phba->brd_no,
				icmd->ulpStatus, icmd->un.ulpWord[4],
				icmd->ulpTimeout);
已提交
3595 3596 3597 3598
		phba->fc_stat.elsRcvDrop++;
	}
	return;
}