lpfc_els.c 127.3 KB
Newer Older
已提交
1 2
/*******************************************************************
 * This file is part of the Emulex Linux Device Driver for         *
3
 * Fibre Channel Host Bus Adapters.                                *
4
 * Copyright (C) 2004-2007 Emulex.  All rights reserved.           *
5
 * EMULEX and SLI are trademarks of Emulex.                        *
已提交
6
 * www.emulex.com                                                  *
7
 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
已提交
8 9
 *                                                                 *
 * This program is free software; you can redistribute it and/or   *
10 11 12 13 14 15 16 17 18 19
 * modify it under the terms of version 2 of the GNU General       *
 * Public License as published by the Free Software Foundation.    *
 * This program is distributed in the hope that it will be useful. *
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
 * more details, a copy of which can be found in the file COPYING  *
 * included with this package.                                     *
已提交
20 21 22 23 24 25
 *******************************************************************/

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

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

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

static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
			  struct lpfc_iocbq *);
43 44 45
static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
			struct lpfc_iocbq *);

已提交
46 47
static int lpfc_max_els_tries = 3;

J
James Smart 已提交
48
int
J
James Smart 已提交
49
lpfc_els_chk_latt(struct lpfc_vport *vport)
已提交
50
{
J
James Smart 已提交
51 52
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
53 54
	uint32_t ha_copy;

J
James Smart 已提交
55 56
	if (vport->port_state >= LPFC_VPORT_READY ||
	    phba->link_state == LPFC_LINK_DOWN)
已提交
57 58 59 60 61 62 63 64 65
		return 0;

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

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

	/* Pending Link Event during Discovery */
66 67
	lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
			"%d (%d):0237 Pending Link Event during "
已提交
68
			"Discovery: State x%x\n",
69
			phba->brd_no, vport->vpi,  phba->pport->port_state);
已提交
70 71 72 73 74 75 76

	/* 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.
	 */
J
James Smart 已提交
77 78 79
	spin_lock_irq(shost->host_lock);
	vport->fc_flag |= FC_ABORT_DISCOVERY;
	spin_unlock_irq(shost->host_lock);
已提交
80

81
	if (phba->link_state != LPFC_CLEAR_LA)
82
		lpfc_issue_clear_la(phba, vport);
已提交
83

84
	return 1;
已提交
85 86 87
}

static struct lpfc_iocbq *
J
James Smart 已提交
88 89 90 91
lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
		   uint16_t cmdSize, uint8_t retry,
		   struct lpfc_nodelist *ndlp, uint32_t did,
		   uint32_t elscmd)
已提交
92
{
J
James Smart 已提交
93
	struct lpfc_hba  *phba = vport->phba;
94
	struct lpfc_iocbq *elsiocb;
已提交
95 96 97 98 99
	struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
	struct ulp_bde64 *bpl;
	IOCB_t *icmd;


J
James Smart 已提交
100 101
	if (!lpfc_is_link_up(phba))
		return NULL;
已提交
102 103

	/* Allocate buffer for  command iocb */
104
	elsiocb = lpfc_sli_get_iocbq(phba);
已提交
105 106 107 108 109 110 111

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

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

117
		lpfc_sli_release_iocbq(phba, elsiocb);
已提交
118 119 120 121 122 123 124
		return NULL;
	}

	INIT_LIST_HEAD(&pcmd->list);

	/* Allocate buffer for response payload */
	if (expectRsp) {
125
		prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
已提交
126 127 128 129
		if (prsp)
			prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
						     &prsp->phys);
		if (prsp == 0 || prsp->virt == 0) {
J
Jesper Juhl 已提交
130
			kfree(prsp);
已提交
131 132
			lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
			kfree(pcmd);
133
			lpfc_sli_release_iocbq(phba, elsiocb);
已提交
134 135 136 137 138 139 140 141
			return NULL;
		}
		INIT_LIST_HEAD(&prsp->list);
	} else {
		prsp = NULL;
	}

	/* Allocate buffer for Buffer ptr list */
142
	pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
已提交
143
	if (pbuflist)
144 145
		pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
						 &pbuflist->phys);
已提交
146
	if (pbuflist == 0 || pbuflist->virt == 0) {
147
		lpfc_sli_release_iocbq(phba, elsiocb);
已提交
148 149 150 151
		lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
		lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
		kfree(pcmd);
		kfree(prsp);
J
Jesper Juhl 已提交
152
		kfree(pbuflist);
已提交
153 154 155 156 157 158 159 160
		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;
J
James Smart 已提交
161
	icmd->un.elsreq64.remoteID = did;	/* DID */
已提交
162
	if (expectRsp) {
163
		icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
已提交
164
		icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
165
		icmd->ulpTimeout = phba->fc_ratov * 2;
已提交
166
	} else {
167
		icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
已提交
168 169 170 171 172 173
		icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
	}
	icmd->ulpBdeCount = 1;
	icmd->ulpLe = 1;
	icmd->ulpClass = CLASS3;

174 175 176 177 178 179 180 181 182
	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
		icmd->un.elsreq64.myID = vport->fc_myDID;

		/* For ELS_REQUEST64_CR, use the VPI by default */
		icmd->ulpContext = vport->vpi;
		icmd->ulpCt_h = 0;
		icmd->ulpCt_l = 1;
	}

已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	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 */
200 201
	if (elscmd != ELS_CMD_LS_RJT)
		elsiocb->context1 = lpfc_nlp_get(ndlp);
202 203
	elsiocb->context2 = pcmd;
	elsiocb->context3 = pbuflist;
已提交
204
	elsiocb->retry = retry;
J
James Smart 已提交
205
	elsiocb->vport = vport;
已提交
206 207 208 209 210 211 212 213 214
	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,
215
				"%d (%d):0116 Xmit ELS command x%x to remote "
J
James Smart 已提交
216
				"NPORT x%x I/O tag: x%x, port state: x%x\n",
217
				phba->brd_no, vport->vpi,  elscmd, did,
J
James Smart 已提交
218
				elsiocb->iotag, vport->port_state);
已提交
219 220 221
	} else {
		/* Xmit ELS response <elsCmd> to remote NPORT <did> */
		lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
222
				"%d (%d):0117 Xmit ELS response x%x to remote "
223
				"NPORT x%x I/O tag: x%x, size: x%x\n",
224
				phba->brd_no, vport->vpi, elscmd,
225
				ndlp->nlp_DID, elsiocb->iotag, cmdSize);
已提交
226 227
	}

228
	return elsiocb;
已提交
229 230 231 232
}


static int
233
lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
已提交
234
{
J
James Smart 已提交
235
	struct lpfc_hba  *phba = vport->phba;
已提交
236
	LPFC_MBOXQ_t *mbox;
237
	struct lpfc_dmabuf *mp;
238 239
	struct lpfc_nodelist *ndlp;
	struct serv_parm *sp;
已提交
240 241
	int rc;

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
	sp = &phba->fc_fabparam;
	ndlp = lpfc_findnode_did(vport, Fabric_DID);
	if (!ndlp)
		goto fail;

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

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

	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT | 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;
	rc = lpfc_reg_login(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
			    0);
	if (rc)
		goto fail_free_mbox;

	mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
	mbox->vport = vport;
	mbox->context2 = lpfc_nlp_get(ndlp);

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

	return 0;

fail_issue_reg_login:
	lpfc_nlp_put(ndlp);
	mp = (struct lpfc_dmabuf *) mbox->context1;
	lpfc_mbuf_free(phba, mp->virt, mp->phys);
	kfree(mp);
fail_free_mbox:
	mempool_free(mbox, phba->mbox_mem_pool);

fail:
	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
	lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
		"%d (%d):0249 Cannot issue Register Fabric login\n",
		phba->brd_no, vport->vpi);
	return -ENXIO;
}

static int
lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			   struct serv_parm *sp, IOCB_t *irsp)
{
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_nodelist *np;
	struct lpfc_nodelist *next_np;

J
James Smart 已提交
303 304 305
	spin_lock_irq(shost->host_lock);
	vport->fc_flag |= FC_FABRIC;
	spin_unlock_irq(shost->host_lock);
已提交
306 307 308 309 310 311 312 313

	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) {
J
James Smart 已提交
314 315 316
		spin_lock_irq(shost->host_lock);
		vport->fc_flag |= FC_PUBLIC_LOOP;
		spin_unlock_irq(shost->host_lock);
已提交
317 318 319 320 321
	} else {
		/*
		 * If we are a N-port connected to a Fabric, fixup sparam's so
		 * logins to devices on remote loops work.
		 */
J
James Smart 已提交
322
		vport->fc_sparam.cmn.altBbCredit = 1;
已提交
323 324
	}

J
James Smart 已提交
325
	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
已提交
326
	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
327
	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
已提交
328 329 330 331 332 333 334 335 336 337 338 339 340
	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));

341 342 343 344 345 346 347 348
	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
		if (sp->cmn.response_multiple_NPort) {
			lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_VPORT,
					"%d:1816 FLOGI NPIV supported, "
					"response data 0x%x\n",
					phba->brd_no,
					sp->cmn.response_multiple_NPort);
			phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
已提交
349

350 351 352 353 354 355 356 357 358 359
		} else {
			/* Because we asked f/w for NPIV it still expects us
			   to call reg_vnpid atleast for the physcial host */
			lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_VPORT,
					"%d:1817 Fabric does not support NPIV "
					"- configuring single port mode.\n",
					phba->brd_no);
			phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
		}
	}
已提交
360

361 362
	if ((vport->fc_prevDID != vport->fc_myDID) &&
		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
已提交
363

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
		/* If our NportID changed, we need to ensure all
		 * remaining NPORTs get unreg_login'ed.
		 */
		list_for_each_entry_safe(np, next_np,
					&vport->fc_nodes, nlp_listp) {
			if ((np->nlp_state != NLP_STE_NPR_NODE) ||
				   !(np->nlp_flag & NLP_NPR_ADISC))
				continue;
			spin_lock_irq(shost->host_lock);
			np->nlp_flag &= ~NLP_NPR_ADISC;
			spin_unlock_irq(shost->host_lock);
			lpfc_unreg_rpi(vport, np);
		}
		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
			lpfc_mbx_unreg_vpi(vport);
			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
		}
	}
已提交
382

383 384
	ndlp->nlp_sid = irsp->un.ulpWord[4] & Mask_DID;
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
已提交
385

386 387 388 389 390 391
	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
	    vport->fc_flag & FC_VPORT_NEEDS_REG_VPI) {
		lpfc_register_new_vport(phba, vport, ndlp);
		return 0;
	}
	lpfc_issue_fabric_reglogin(vport);
已提交
392 393 394 395 396 397 398
	return 0;
}

/*
 * We FLOGIed into an NPort, initiate pt2pt protocol
 */
static int
J
James Smart 已提交
399 400
lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  struct serv_parm *sp)
已提交
401
{
J
James Smart 已提交
402 403
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
404 405 406
	LPFC_MBOXQ_t *mbox;
	int rc;

J
James Smart 已提交
407 408 409
	spin_lock_irq(shost->host_lock);
	vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
	spin_unlock_irq(shost->host_lock);
已提交
410 411 412

	phba->fc_edtov = FF_DEF_EDTOV;
	phba->fc_ratov = FF_DEF_RATOV;
J
James Smart 已提交
413
	rc = memcmp(&vport->fc_portname, &sp->portName,
414
		    sizeof(vport->fc_portname));
已提交
415 416
	if (rc >= 0) {
		/* This side will initiate the PLOGI */
J
James Smart 已提交
417 418 419
		spin_lock_irq(shost->host_lock);
		vport->fc_flag |= FC_PT2PT_PLOGI;
		spin_unlock_irq(shost->host_lock);
已提交
420 421 422 423 424 425 426 427

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

		/* not equal */
		if (rc)
J
James Smart 已提交
428
			vport->fc_myDID = PT2PT_LocalID;
已提交
429 430 431 432 433 434 435 436

		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;
437
		mbox->vport = vport;
已提交
438
		rc = lpfc_sli_issue_mbox(phba, mbox,
439
					 MBX_NOWAIT | MBX_STOP_IOCB);
已提交
440 441 442 443
		if (rc == MBX_NOT_FINISHED) {
			mempool_free(mbox, phba->mbox_mem_pool);
			goto fail;
		}
444
		lpfc_nlp_put(ndlp);
已提交
445

J
James Smart 已提交
446
		ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
已提交
447 448 449 450 451 452 453 454 455
		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;

J
James Smart 已提交
456
			lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
已提交
457 458 459
		}

		memcpy(&ndlp->nlp_portname, &sp->portName,
J
James Smart 已提交
460
		       sizeof(struct lpfc_name));
已提交
461
		memcpy(&ndlp->nlp_nodename, &sp->nodeName,
J
James Smart 已提交
462 463 464
		       sizeof(struct lpfc_name));
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		spin_lock_irq(shost->host_lock);
已提交
465
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
466
		spin_unlock_irq(shost->host_lock);
已提交
467 468
	} else {
		/* This side will wait for the PLOGI */
469
		lpfc_nlp_put(ndlp);
已提交
470 471
	}

J
James Smart 已提交
472 473 474
	spin_lock_irq(shost->host_lock);
	vport->fc_flag |= FC_PT2PT;
	spin_unlock_irq(shost->host_lock);
已提交
475 476

	/* Start discovery - this should just do CLEAR_LA */
J
James Smart 已提交
477
	lpfc_disc_start(vport);
已提交
478
	return 0;
479
fail:
已提交
480 481 482 483
	return -ENXIO;
}

static void
484 485
lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		    struct lpfc_iocbq *rspiocb)
已提交
486
{
J
James Smart 已提交
487 488
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
489 490 491 492 493 494 495
	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 */
J
James Smart 已提交
496
	if (lpfc_els_chk_latt(vport)) {
497
		lpfc_nlp_put(ndlp);
已提交
498 499 500
		goto out;
	}

J
James Smart 已提交
501 502 503 504 505
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"FLOGI cmpl:      status:x%x/x%x state:x%x",
		irsp->ulpStatus, irsp->un.ulpWord[4],
		vport->port_state);

已提交
506 507
	if (irsp->ulpStatus) {
		/* Check for retry */
J
James Smart 已提交
508
		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
已提交
509
			goto out;
J
James Smart 已提交
510

已提交
511
		/* FLOGI failed, so there is no fabric */
J
James Smart 已提交
512 513 514
		spin_lock_irq(shost->host_lock);
		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
		spin_unlock_irq(shost->host_lock);
已提交
515

516
		/* If private loop, then allow max outstanding els to be
已提交
517 518 519 520
		 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
		 * alpa map would take too long otherwise.
		 */
		if (phba->alpa_map[0] == 0) {
521
			phba->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
已提交
522 523 524
		}

		/* FLOGI failure */
525 526 527 528
		lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
				"%d (%d):0100 FLOGI failure Data: x%x x%x "
				"x%x\n",
				phba->brd_no, vport->vpi,
529 530
				irsp->ulpStatus, irsp->un.ulpWord[4],
				irsp->ulpTimeout);
已提交
531 532 533 534 535 536 537 538 539 540 541 542 543
		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,
544
			"%d (%d):0101 FLOGI completes sucessfully "
已提交
545
			"Data: x%x x%x x%x x%x\n",
546
			phba->brd_no, vport->vpi,
已提交
547 548 549
			irsp->un.ulpWord[4], sp->cmn.e_d_tov,
			sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);

J
James Smart 已提交
550
	if (vport->port_state == LPFC_FLOGI) {
已提交
551 552 553 554 555
		/*
		 * If Common Service Parameters indicate Nport
		 * we are point to point, if Fport we are Fabric.
		 */
		if (sp->cmn.fPort)
J
James Smart 已提交
556
			rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
已提交
557
		else
J
James Smart 已提交
558
			rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
已提交
559 560 561 562 563 564

		if (!rc)
			goto out;
	}

flogifail:
565
	lpfc_nlp_put(ndlp);
已提交
566

J
James Smart 已提交
567
	if (!lpfc_error_lost_link(irsp)) {
已提交
568
		/* FLOGI failed, so just use loop map to make discovery list */
J
James Smart 已提交
569
		lpfc_disc_list_loopmap(vport);
已提交
570 571

		/* Start discovery */
J
James Smart 已提交
572
		lpfc_disc_start(vport);
已提交
573 574 575 576 577 578 579
	}

out:
	lpfc_els_free_iocb(phba, cmdiocb);
}

static int
J
James Smart 已提交
580
lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
581 582
		     uint8_t retry)
{
J
James Smart 已提交
583
	struct lpfc_hba  *phba = vport->phba;
已提交
584 585 586 587 588 589 590 591 592 593 594
	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];

595
	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
J
James Smart 已提交
596 597
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_FLOGI);
598

599
	if (!elsiocb)
600
		return 1;
已提交
601 602 603 604 605 606

	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;
607 608
	pcmd += sizeof(uint32_t);
	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
已提交
609 610 611 612 613 614 615 616 617 618 619 620 621
	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;

622 623 624 625 626 627 628 629
	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
		sp->cmn.request_multiple_Nport = 1;

		/* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
		icmd->ulpCt_h = 1;
		icmd->ulpCt_l = 0;
	}

J
James Smart 已提交
630 631 632 633 634
	if (phba->fc_topology != TOPOLOGY_LOOP) {
		icmd->un.elsreq64.myID = 0;
		icmd->un.elsreq64.fl = 1;
	}

已提交
635 636
	tmo = phba->fc_ratov;
	phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
J
James Smart 已提交
637
	lpfc_set_disctmo(vport);
已提交
638 639 640 641
	phba->fc_ratov = tmo;

	phba->fc_stat.elsXmitFLOGI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
J
James Smart 已提交
642 643 644 645 646

	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue FLOGI:     opt:x%x",
		phba->sli3_options, 0, 0);

647
	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
已提交
648 649
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
650
		return 1;
已提交
651
	}
652
	return 0;
已提交
653 654 655
}

int
J
James Smart 已提交
656
lpfc_els_abort_flogi(struct lpfc_hba *phba)
已提交
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
{
	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.
	 */
J
James Smart 已提交
674
	spin_lock_irq(&phba->hbalock);
已提交
675 676
	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
		icmd = &iocb->iocb;
J
James Smart 已提交
677 678
		if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
		    icmd->un.elsreq64.bdl.ulpIoTag32) {
已提交
679
			ndlp = (struct lpfc_nodelist *)(iocb->context1);
680
			if (ndlp && (ndlp->nlp_DID == Fabric_DID)) {
681
				lpfc_sli_issue_abort_iotag(phba, pring, iocb);
682
			}
已提交
683 684
		}
	}
J
James Smart 已提交
685
	spin_unlock_irq(&phba->hbalock);
已提交
686 687 688 689 690

	return 0;
}

int
J
James Smart 已提交
691
lpfc_initial_flogi(struct lpfc_vport *vport)
已提交
692
{
J
James Smart 已提交
693
	struct lpfc_hba *phba = vport->phba;
已提交
694 695
	struct lpfc_nodelist *ndlp;

696
	/* First look for the Fabric ndlp */
J
James Smart 已提交
697
	ndlp = lpfc_findnode_did(vport, Fabric_DID);
698
	if (!ndlp) {
已提交
699
		/* Cannot find existing Fabric ndlp, so allocate a new one */
700 701 702
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp)
			return 0;
J
James Smart 已提交
703
		lpfc_nlp_init(vport, ndlp, Fabric_DID);
704
	} else {
J
James Smart 已提交
705
		lpfc_dequeue_node(vport, ndlp);
已提交
706
	}
J
James Smart 已提交
707
	if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
708
		lpfc_nlp_put(ndlp);
已提交
709
	}
710
	return 1;
已提交
711 712
}

713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
int
lpfc_initial_fdisc(struct lpfc_vport *vport)
{
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_nodelist *ndlp;

	/* First look for the Fabric ndlp */
	ndlp = lpfc_findnode_did(vport, Fabric_DID);
	if (!ndlp) {
		/* Cannot find existing Fabric ndlp, so allocate a new one */
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp)
			return 0;
		lpfc_nlp_init(vport, ndlp, Fabric_DID);
	} else {
		lpfc_dequeue_node(vport, ndlp);
	}
	if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
		lpfc_nlp_put(ndlp);
	}
	return 1;
}
已提交
735
static void
J
James Smart 已提交
736
lpfc_more_plogi(struct lpfc_vport *vport)
已提交
737 738
{
	int sentplogi;
J
James Smart 已提交
739
	struct lpfc_hba *phba = vport->phba;
已提交
740

J
James Smart 已提交
741 742
	if (vport->num_disc_nodes)
		vport->num_disc_nodes--;
已提交
743 744 745

	/* Continue discovery with <num_disc_nodes> PLOGIs to go */
	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
746
			"%d (%d):0232 Continue discovery with %d PLOGIs to go "
已提交
747
			"Data: x%x x%x x%x\n",
748
			phba->brd_no, vport->vpi, vport->num_disc_nodes,
J
James Smart 已提交
749
			vport->fc_plogi_cnt, vport->fc_flag, vport->port_state);
已提交
750 751

	/* Check to see if there are more PLOGIs to be sent */
J
James Smart 已提交
752 753 754 755
	if (vport->fc_flag & FC_NLP_MORE)
		/* go thru NPR nodes and issue any remaining ELS PLOGIs */
		sentplogi = lpfc_els_disc_plogi(vport);

已提交
756 757 758
	return;
}

759
static struct lpfc_nodelist *
760
lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
761 762
			 struct lpfc_nodelist *ndlp)
{
J
James Smart 已提交
763
	struct lpfc_vport    *vport = ndlp->vport;
764 765
	struct lpfc_nodelist *new_ndlp;
	struct serv_parm *sp;
766
	uint8_t  name[sizeof(struct lpfc_name)];
767 768
	uint32_t rc;

J
James Smart 已提交
769 770 771 772 773 774
	/* Fabric nodes can have the same WWPN so we don't bother searching
	 * by WWPN.  Just return the ndlp that was given to us.
	 */
	if (ndlp->nlp_type & NLP_FABRIC)
		return ndlp;

775
	sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
776
	memset(name, 0, sizeof(struct lpfc_name));
777

778
	/* Now we find out if the NPort we are logging into, matches the WWPN
779 780
	 * we have for that ndlp. If not, we have some work to do.
	 */
J
James Smart 已提交
781
	new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
782

783
	if (new_ndlp == ndlp)
784 785 786
		return ndlp;

	if (!new_ndlp) {
J
James Smart 已提交
787 788
		rc = memcmp(&ndlp->nlp_portname, name,
			    sizeof(struct lpfc_name));
789 790
		if (!rc)
			return ndlp;
791 792 793 794
		new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
		if (!new_ndlp)
			return ndlp;

J
James Smart 已提交
795
		lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
796 797
	}

J
James Smart 已提交
798
	lpfc_unreg_rpi(vport, new_ndlp);
799
	new_ndlp->nlp_DID = ndlp->nlp_DID;
800
	new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
J
James Smart 已提交
801
	lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
802

J
James Smart 已提交
803
	/* Move this back to NPR state */
804
	if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0)
J
James Smart 已提交
805
		lpfc_drop_node(vport, ndlp);
806
	else {
J
James Smart 已提交
807
		lpfc_unreg_rpi(vport, ndlp);
808
		ndlp->nlp_DID = 0; /* Two ndlps cannot have the same did */
J
James Smart 已提交
809
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
810
	}
811 812 813
	return new_ndlp;
}

已提交
814
static void
J
James Smart 已提交
815 816
lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		    struct lpfc_iocbq *rspiocb)
已提交
817
{
J
James Smart 已提交
818 819
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
820 821
	IOCB_t *irsp;
	struct lpfc_nodelist *ndlp;
822
	struct lpfc_dmabuf *prsp;
已提交
823 824 825 826 827 828
	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;
J
James Smart 已提交
829 830 831 832 833
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"PLOGI cmpl:      status:x%x/x%x did:x%x",
		irsp->ulpStatus, irsp->un.ulpWord[4],
		irsp->un.elsreq64.remoteID);

J
James Smart 已提交
834
	ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
835
	if (!ndlp) {
836 837 838 839 840
		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
			"%d (%d):0136 PLOGI completes to NPort x%x "
			"with no ndlp. Data: x%x x%x x%x\n",
			phba->brd_no, vport->vpi, irsp->un.elsreq64.remoteID,
			irsp->ulpStatus, irsp->un.ulpWord[4], irsp->ulpIoTag);
841
		goto out;
842
	}
已提交
843 844 845 846

	/* Since ndlp can be freed in the disc state machine, note if this node
	 * is being used during discovery.
	 */
J
James Smart 已提交
847
	spin_lock_irq(shost->host_lock);
已提交
848
	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
849
	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
J
James Smart 已提交
850
	spin_unlock_irq(shost->host_lock);
已提交
851 852 853 854
	rc   = 0;

	/* PLOGI completes to NPort <nlp_DID> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
855
			"%d (%d):0102 PLOGI completes to NPort x%x "
856
			"Data: x%x x%x x%x x%x x%x\n",
857 858 859
			phba->brd_no, vport->vpi, ndlp->nlp_DID,
			irsp->ulpStatus, irsp->un.ulpWord[4],
			irsp->ulpTimeout, disc, vport->num_disc_nodes);
已提交
860 861

	/* Check to see if link went down during discovery */
J
James Smart 已提交
862 863
	if (lpfc_els_chk_latt(vport)) {
		spin_lock_irq(shost->host_lock);
已提交
864
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
865
		spin_unlock_irq(shost->host_lock);
已提交
866 867 868 869 870 871 872 873 874 875 876 877
		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) {
J
James Smart 已提交
878
				spin_lock_irq(shost->host_lock);
已提交
879
				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
880
				spin_unlock_irq(shost->host_lock);
已提交
881 882 883 884 885
			}
			goto out;
		}

		/* PLOGI failed */
886 887 888 889 890 891 892 893 894
		if (ndlp->nlp_DID == NameServer_DID) {
			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
			lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
				"%d (%d):0250 Nameserver login error: "
				"0x%x / 0x%x\n",
				phba->brd_no, vport->vpi,
				irsp->ulpStatus, irsp->un.ulpWord[4]);
		}

已提交
895
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
J
James Smart 已提交
896
		if (lpfc_error_lost_link(irsp)) {
897
			rc = NLP_STE_FREED_NODE;
898
		} else {
J
James Smart 已提交
899
			rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
900
						     NLP_EVT_CMPL_PLOGI);
已提交
901 902 903
		}
	} else {
		/* Good status, call state machine */
904
		prsp = list_entry(((struct lpfc_dmabuf *)
905 906 907
				   cmdiocb->context2)->list.next,
				  struct lpfc_dmabuf, list);
		ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
J
James Smart 已提交
908
		rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
909
					     NLP_EVT_CMPL_PLOGI);
已提交
910 911
	}

J
James Smart 已提交
912
	if (disc && vport->num_disc_nodes) {
已提交
913
		/* Check to see if there are more PLOGIs to be sent */
J
James Smart 已提交
914
		lpfc_more_plogi(vport);
已提交
915

J
James Smart 已提交
916 917 918 919
		if (vport->num_disc_nodes == 0) {
			spin_lock_irq(shost->host_lock);
			vport->fc_flag &= ~FC_NDISC_ACTIVE;
			spin_unlock_irq(shost->host_lock);
已提交
920

J
James Smart 已提交
921 922
			lpfc_can_disctmo(vport);
			if (vport->fc_flag & FC_RSCN_MODE) {
923 924 925 926
				/*
				 * Check to see if more RSCNs came in while
				 * we were processing this one.
				 */
J
James Smart 已提交
927 928 929 930 931
				if ((vport->fc_rscn_id_cnt == 0) &&
				    (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
					spin_lock_irq(shost->host_lock);
					vport->fc_flag &= ~FC_RSCN_MODE;
					spin_unlock_irq(shost->host_lock);
932
				} else {
J
James Smart 已提交
933
					lpfc_els_handle_rscn(vport);
934
				}
已提交
935 936 937 938 939 940 941 942 943 944
			}
		}
	}

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

int
J
James Smart 已提交
945
lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
已提交
946
{
J
James Smart 已提交
947
	struct lpfc_hba  *phba = vport->phba;
已提交
948 949 950 951 952 953 954
	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;
955
	int ret;
已提交
956 957 958 959

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

960
	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
J
James Smart 已提交
961 962
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, NULL, did,
				     ELS_CMD_PLOGI);
963 964
	if (!elsiocb)
		return 1;
已提交
965 966 967 968 969 970

	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;
971 972
	pcmd += sizeof(uint32_t);
	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
已提交
973 974 975 976 977 978 979 980
	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;

J
James Smart 已提交
981 982 983 984
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue PLOGI:     did:x%x",
		did, 0, 0);

已提交
985 986
	phba->fc_stat.elsXmitPLOGI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
987 988 989
	ret = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);

	if (ret == IOCB_ERROR) {
已提交
990
		lpfc_els_free_iocb(phba, elsiocb);
991
		return 1;
已提交
992
	}
993
	return 0;
已提交
994 995 996
}

static void
J
James Smart 已提交
997 998
lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		   struct lpfc_iocbq *rspiocb)
已提交
999
{
J
James Smart 已提交
1000 1001
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	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;
J
James Smart 已提交
1012
	spin_lock_irq(shost->host_lock);
已提交
1013
	ndlp->nlp_flag &= ~NLP_PRLI_SND;
J
James Smart 已提交
1014
	spin_unlock_irq(shost->host_lock);
已提交
1015

J
James Smart 已提交
1016 1017 1018 1019 1020
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"PRLI cmpl:       status:x%x/x%x did:x%x",
		irsp->ulpStatus, irsp->un.ulpWord[4],
		ndlp->nlp_DID);

已提交
1021 1022
	/* PRLI completes to NPort <nlp_DID> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1023
			"%d (%d):0103 PRLI completes to NPort x%x "
1024
			"Data: x%x x%x x%x x%x\n",
1025 1026
			phba->brd_no, vport->vpi, ndlp->nlp_DID,
			irsp->ulpStatus, irsp->un.ulpWord[4], irsp->ulpTimeout,
J
James Smart 已提交
1027
			vport->num_disc_nodes);
已提交
1028

J
James Smart 已提交
1029
	vport->fc_prli_sent--;
已提交
1030
	/* Check to see if link went down during discovery */
J
James Smart 已提交
1031
	if (lpfc_els_chk_latt(vport))
已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
		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 */
J
James Smart 已提交
1042
		if (lpfc_error_lost_link(irsp)) {
已提交
1043
			goto out;
1044
		} else {
J
James Smart 已提交
1045
			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1046
						NLP_EVT_CMPL_PRLI);
已提交
1047 1048 1049
		}
	} else {
		/* Good status, call state machine */
J
James Smart 已提交
1050
		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1051
					NLP_EVT_CMPL_PRLI);
已提交
1052 1053 1054 1055 1056 1057 1058 1059
	}

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

int
J
James Smart 已提交
1060
lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1061 1062
		    uint8_t retry)
{
J
James Smart 已提交
1063 1064
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba *phba = vport->phba;
已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	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 */

1076
	cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
J
James Smart 已提交
1077 1078
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_PRLI);
1079
	if (!elsiocb)
1080
		return 1;
已提交
1081 1082 1083 1084 1085

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

	/* For PRLI request, remainder of payload is service parameters */
1086
	memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
已提交
1087
	*((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1088
	pcmd += sizeof(uint32_t);
已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

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

J
James Smart 已提交
1108 1109 1110 1111
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue PRLI:      did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1112 1113
	phba->fc_stat.elsXmitPRLI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
J
James Smart 已提交
1114
	spin_lock_irq(shost->host_lock);
已提交
1115
	ndlp->nlp_flag |= NLP_PRLI_SND;
J
James Smart 已提交
1116
	spin_unlock_irq(shost->host_lock);
已提交
1117
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
J
James Smart 已提交
1118
		spin_lock_irq(shost->host_lock);
已提交
1119
		ndlp->nlp_flag &= ~NLP_PRLI_SND;
J
James Smart 已提交
1120
		spin_unlock_irq(shost->host_lock);
已提交
1121
		lpfc_els_free_iocb(phba, elsiocb);
1122
		return 1;
已提交
1123
	}
J
James Smart 已提交
1124
	vport->fc_prli_sent++;
1125
	return 0;
已提交
1126 1127 1128
}

static void
J
James Smart 已提交
1129
lpfc_more_adisc(struct lpfc_vport *vport)
已提交
1130 1131
{
	int sentadisc;
J
James Smart 已提交
1132
	struct lpfc_hba *phba = vport->phba;
已提交
1133

J
James Smart 已提交
1134 1135
	if (vport->num_disc_nodes)
		vport->num_disc_nodes--;
已提交
1136 1137 1138

	/* Continue discovery with <num_disc_nodes> ADISCs to go */
	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1139
			"%d (%d):0210 Continue discovery with %d ADISCs to go "
已提交
1140
			"Data: x%x x%x x%x\n",
1141
			phba->brd_no, vport->vpi, vport->num_disc_nodes,
J
James Smart 已提交
1142
			vport->fc_adisc_cnt, vport->fc_flag, vport->port_state);
已提交
1143 1144

	/* Check to see if there are more ADISCs to be sent */
J
James Smart 已提交
1145 1146 1147 1148
	if (vport->fc_flag & FC_NLP_MORE) {
		lpfc_set_disctmo(vport);
		/* go thru NPR nodes and issue any remaining ELS ADISCs */
		sentadisc = lpfc_els_disc_adisc(vport);
已提交
1149 1150 1151 1152 1153
	}
	return;
}

static void
J
James Smart 已提交
1154
lpfc_rscn_disc(struct lpfc_vport *vport)
已提交
1155
{
J
James Smart 已提交
1156 1157
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

J
James Smart 已提交
1158 1159
	lpfc_can_disctmo(vport);

已提交
1160
	/* RSCN discovery */
J
James Smart 已提交
1161 1162 1163
	/* go thru NPR nodes and issue ELS PLOGIs */
	if (vport->fc_npr_cnt)
		if (lpfc_els_disc_plogi(vport))
已提交
1164
			return;
J
James Smart 已提交
1165 1166

	if (vport->fc_flag & FC_RSCN_MODE) {
已提交
1167 1168 1169
		/* Check to see if more RSCNs came in while we were
		 * processing this one.
		 */
J
James Smart 已提交
1170 1171 1172 1173 1174
		if ((vport->fc_rscn_id_cnt == 0) &&
		    (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
			spin_lock_irq(shost->host_lock);
			vport->fc_flag &= ~FC_RSCN_MODE;
			spin_unlock_irq(shost->host_lock);
已提交
1175
		} else {
J
James Smart 已提交
1176
			lpfc_els_handle_rscn(vport);
已提交
1177 1178 1179 1180 1181
		}
	}
}

static void
J
James Smart 已提交
1182 1183
lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		    struct lpfc_iocbq *rspiocb)
已提交
1184
{
J
James Smart 已提交
1185 1186
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
1187 1188
	IOCB_t *irsp;
	struct lpfc_nodelist *ndlp;
J
James Smart 已提交
1189
	int  disc;
已提交
1190 1191 1192 1193 1194 1195 1196

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

J
James Smart 已提交
1197 1198 1199 1200 1201
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"ADISC cmpl:      status:x%x/x%x did:x%x",
		irsp->ulpStatus, irsp->un.ulpWord[4],
		ndlp->nlp_DID);

已提交
1202 1203 1204
	/* Since ndlp can be freed in the disc state machine, note if this node
	 * is being used during discovery.
	 */
J
James Smart 已提交
1205
	spin_lock_irq(shost->host_lock);
已提交
1206
	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1207
	ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
J
James Smart 已提交
1208
	spin_unlock_irq(shost->host_lock);
已提交
1209 1210 1211

	/* ADISC completes to NPort <nlp_DID> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1212
			"%d (%d):0104 ADISC completes to NPort x%x "
1213
			"Data: x%x x%x x%x x%x x%x\n",
1214 1215 1216
			phba->brd_no, vport->vpi, ndlp->nlp_DID,
			irsp->ulpStatus, irsp->un.ulpWord[4], irsp->ulpTimeout,
			disc, vport->num_disc_nodes);
已提交
1217 1218

	/* Check to see if link went down during discovery */
J
James Smart 已提交
1219 1220
	if (lpfc_els_chk_latt(vport)) {
		spin_lock_irq(shost->host_lock);
已提交
1221
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
1222
		spin_unlock_irq(shost->host_lock);
已提交
1223 1224 1225 1226 1227 1228 1229 1230
		goto out;
	}

	if (irsp->ulpStatus) {
		/* Check for retry */
		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
			/* ELS command is being retried */
			if (disc) {
J
James Smart 已提交
1231
				spin_lock_irq(shost->host_lock);
已提交
1232
				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
1233 1234
				spin_unlock_irq(shost->host_lock);
				lpfc_set_disctmo(vport);
已提交
1235 1236 1237 1238 1239
			}
			goto out;
		}
		/* ADISC failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
J
James Smart 已提交
1240
		if (!lpfc_error_lost_link(irsp)) {
J
James Smart 已提交
1241
			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
J
James Smart 已提交
1242
						NLP_EVT_CMPL_ADISC);
已提交
1243 1244 1245
		}
	} else {
		/* Good status, call state machine */
J
James Smart 已提交
1246
		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
已提交
1247 1248 1249
					NLP_EVT_CMPL_ADISC);
	}

J
James Smart 已提交
1250
	if (disc && vport->num_disc_nodes) {
已提交
1251
		/* Check to see if there are more ADISCs to be sent */
J
James Smart 已提交
1252
		lpfc_more_adisc(vport);
已提交
1253 1254

		/* Check to see if we are done with ADISC authentication */
J
James Smart 已提交
1255
		if (vport->num_disc_nodes == 0) {
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
			/* If we get here, there is nothing left to ADISC */
			/*
			 * For NPIV, cmpl_reg_vpi will set port_state to READY,
			 * and continue discovery.
			 */
			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
			   !(vport->fc_flag & FC_RSCN_MODE)) {
				lpfc_issue_reg_vpi(phba, vport);
				goto out;
			}
			/*
			 * For SLI2, we need to set port_state to READY
			 * and continue discovery.
			 */
			if (vport->port_state < LPFC_VPORT_READY) {
				/* If we get here, there is nothing to ADISC */
1272
				if (vport->port_type == LPFC_PHYSICAL_PORT)
J
James Smart 已提交
1273
					lpfc_issue_clear_la(phba, vport);
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290

				if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
					vport->num_disc_nodes = 0;
					/* go thru NPR list, issue ELS PLOGIs */
					if (vport->fc_npr_cnt)
						lpfc_els_disc_plogi(vport);

					if (!vport->num_disc_nodes) {
						spin_lock_irq(shost->host_lock);
						vport->fc_flag &=
							~FC_NDISC_ACTIVE;
						spin_unlock_irq(
							shost->host_lock);
						lpfc_can_disctmo(vport);
					}
				}
				vport->port_state = LPFC_VPORT_READY;
已提交
1291
			} else {
J
James Smart 已提交
1292
				lpfc_rscn_disc(vport);
已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301
			}
		}
	}
out:
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
J
James Smart 已提交
1302
lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1303 1304
		     uint8_t retry)
{
J
James Smart 已提交
1305 1306
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
1307 1308 1309
	ADISC *ap;
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
J
James Smart 已提交
1310 1311
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
已提交
1312 1313 1314
	uint8_t *pcmd;
	uint16_t cmdsize;

1315
	cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
J
James Smart 已提交
1316 1317
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ADISC);
1318
	if (!elsiocb)
1319
		return 1;
已提交
1320 1321 1322 1323 1324 1325

	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;
1326
	pcmd += sizeof(uint32_t);
已提交
1327 1328 1329 1330

	/* Fill in ADISC payload */
	ap = (ADISC *) pcmd;
	ap->hardAL_PA = phba->fc_pref_ALPA;
1331 1332
	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
J
James Smart 已提交
1333
	ap->DID = be32_to_cpu(vport->fc_myDID);
已提交
1334

J
James Smart 已提交
1335 1336 1337 1338
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue ADISC:     did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1339 1340
	phba->fc_stat.elsXmitADISC++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
J
James Smart 已提交
1341
	spin_lock_irq(shost->host_lock);
已提交
1342
	ndlp->nlp_flag |= NLP_ADISC_SND;
J
James Smart 已提交
1343
	spin_unlock_irq(shost->host_lock);
已提交
1344
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
J
James Smart 已提交
1345
		spin_lock_irq(shost->host_lock);
已提交
1346
		ndlp->nlp_flag &= ~NLP_ADISC_SND;
J
James Smart 已提交
1347
		spin_unlock_irq(shost->host_lock);
已提交
1348
		lpfc_els_free_iocb(phba, elsiocb);
1349
		return 1;
已提交
1350
	}
1351
	return 0;
已提交
1352 1353 1354
}

static void
J
James Smart 已提交
1355 1356
lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		   struct lpfc_iocbq *rspiocb)
已提交
1357
{
J
James Smart 已提交
1358 1359 1360
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	struct lpfc_vport *vport = ndlp->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
1361 1362 1363 1364 1365 1366 1367 1368
	IOCB_t *irsp;
	struct lpfc_sli *psli;

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

	irsp = &(rspiocb->iocb);
J
James Smart 已提交
1369
	spin_lock_irq(shost->host_lock);
已提交
1370
	ndlp->nlp_flag &= ~NLP_LOGO_SND;
J
James Smart 已提交
1371
	spin_unlock_irq(shost->host_lock);
已提交
1372

J
James Smart 已提交
1373 1374 1375 1376 1377
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"LOGO cmpl:       status:x%x/x%x did:x%x",
		irsp->ulpStatus, irsp->un.ulpWord[4],
		ndlp->nlp_DID);

已提交
1378 1379
	/* LOGO completes to NPort <nlp_DID> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1380
			"%d (%d):0105 LOGO completes to NPort x%x "
1381
			"Data: x%x x%x x%x x%x\n",
1382 1383
			phba->brd_no, vport->vpi, ndlp->nlp_DID,
			irsp->ulpStatus, irsp->un.ulpWord[4], irsp->ulpTimeout,
J
James Smart 已提交
1384
			vport->num_disc_nodes);
已提交
1385 1386

	/* Check to see if link went down during discovery */
J
James Smart 已提交
1387
	if (lpfc_els_chk_latt(vport))
已提交
1388 1389
		goto out;

1390 1391 1392 1393 1394 1395 1396 1397 1398
	if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
	        /* NLP_EVT_DEVICE_RM should unregister the RPI
		 * which should abort all outstanding IOs.
		 */
		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
					NLP_EVT_DEVICE_RM);
		goto out;
	}

已提交
1399 1400
	if (irsp->ulpStatus) {
		/* Check for retry */
J
James Smart 已提交
1401
		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
已提交
1402 1403 1404 1405
			/* ELS command is being retried */
			goto out;
		/* LOGO failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
J
James Smart 已提交
1406
		if (lpfc_error_lost_link(irsp))
已提交
1407
			goto out;
J
James Smart 已提交
1408
		else
J
James Smart 已提交
1409
			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1410
						NLP_EVT_CMPL_LOGO);
已提交
1411
	} else {
1412 1413 1414
		/* Good status, call state machine.
		 * This will unregister the rpi if needed.
		 */
J
James Smart 已提交
1415
		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1416
					NLP_EVT_CMPL_LOGO);
已提交
1417 1418 1419 1420 1421 1422 1423 1424
	}

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

int
J
James Smart 已提交
1425
lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1426 1427
		    uint8_t retry)
{
J
James Smart 已提交
1428 1429
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
1430 1431 1432 1433 1434 1435
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;
1436
	int rc;
已提交
1437 1438 1439 1440

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

1441
	cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
J
James Smart 已提交
1442 1443
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_LOGO);
1444
	if (!elsiocb)
1445
		return 1;
已提交
1446 1447 1448 1449

	icmd = &elsiocb->iocb;
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
1450
	pcmd += sizeof(uint32_t);
已提交
1451 1452

	/* Fill in LOGO payload */
J
James Smart 已提交
1453
	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
1454 1455
	pcmd += sizeof(uint32_t);
	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
已提交
1456

J
James Smart 已提交
1457 1458 1459 1460
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue LOGO:      did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1461 1462
	phba->fc_stat.elsXmitLOGO++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
J
James Smart 已提交
1463
	spin_lock_irq(shost->host_lock);
已提交
1464
	ndlp->nlp_flag |= NLP_LOGO_SND;
J
James Smart 已提交
1465
	spin_unlock_irq(shost->host_lock);
1466 1467 1468
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);

	if (rc == IOCB_ERROR) {
J
James Smart 已提交
1469
		spin_lock_irq(shost->host_lock);
已提交
1470
		ndlp->nlp_flag &= ~NLP_LOGO_SND;
J
James Smart 已提交
1471
		spin_unlock_irq(shost->host_lock);
已提交
1472
		lpfc_els_free_iocb(phba, elsiocb);
1473
		return 1;
已提交
1474
	}
1475
	return 0;
已提交
1476 1477 1478
}

static void
J
James Smart 已提交
1479 1480
lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_iocbq *rspiocb)
已提交
1481
{
J
James Smart 已提交
1482
	struct lpfc_vport *vport = cmdiocb->vport;
已提交
1483 1484 1485 1486
	IOCB_t *irsp;

	irsp = &rspiocb->iocb;

J
James Smart 已提交
1487 1488 1489 1490 1491
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"ELS cmd cmpl:    status:x%x/x%x did:x%x",
		irsp->ulpStatus, irsp->un.ulpWord[4],
		irsp->un.elsreq64.remoteID);

已提交
1492
	/* ELS cmd tag <ulpIoTag> completes */
1493 1494 1495 1496
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"%d (%d):0106 ELS cmd tag x%x completes Data: x%x x%x "
			"x%x\n",
			phba->brd_no, vport->vpi,
1497 1498
			irsp->ulpIoTag, irsp->ulpStatus,
			irsp->un.ulpWord[4], irsp->ulpTimeout);
已提交
1499 1500

	/* Check to see if link went down during discovery */
J
James Smart 已提交
1501
	lpfc_els_chk_latt(vport);
已提交
1502 1503 1504 1505 1506
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
J
James Smart 已提交
1507
lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
已提交
1508
{
J
James Smart 已提交
1509
	struct lpfc_hba  *phba = vport->phba;
已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
	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 */
1520
	cmdsize = (sizeof(uint32_t) + sizeof(SCR));
1521 1522 1523
	ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
	if (!ndlp)
		return 1;
已提交
1524

J
James Smart 已提交
1525 1526 1527 1528
	lpfc_nlp_init(vport, ndlp, nportid);

	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_SCR);
已提交
1529

1530
	if (!elsiocb) {
1531
		lpfc_nlp_put(ndlp);
1532
		return 1;
已提交
1533 1534 1535 1536 1537 1538
	}

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

	*((uint32_t *) (pcmd)) = ELS_CMD_SCR;
1539
	pcmd += sizeof(uint32_t);
已提交
1540 1541

	/* For SCR, remainder of payload is SCR parameter page */
1542
	memset(pcmd, 0, sizeof(SCR));
已提交
1543 1544
	((SCR *) pcmd)->Function = SCR_FUNC_FULL;

J
James Smart 已提交
1545 1546 1547 1548
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue SCR:       did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1549 1550 1551
	phba->fc_stat.elsXmitSCR++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1552
		lpfc_nlp_put(ndlp);
已提交
1553
		lpfc_els_free_iocb(phba, elsiocb);
1554
		return 1;
已提交
1555
	}
1556
	lpfc_nlp_put(ndlp);
1557
	return 0;
已提交
1558 1559 1560
}

static int
J
James Smart 已提交
1561
lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
已提交
1562
{
J
James Smart 已提交
1563
	struct lpfc_hba  *phba = vport->phba;
已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
	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 */
1577
	cmdsize = (sizeof(uint32_t) + sizeof(FARP));
1578 1579 1580
	ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
	if (!ndlp)
		return 1;
已提交
1581

J
James Smart 已提交
1582 1583 1584 1585
	lpfc_nlp_init(vport, ndlp, nportid);

	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_RNID);
1586
	if (!elsiocb) {
1587
		lpfc_nlp_put(ndlp);
1588
		return 1;
已提交
1589 1590 1591 1592 1593 1594
	}

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

	*((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
1595
	pcmd += sizeof(uint32_t);
已提交
1596 1597 1598

	/* Fill in FARPR payload */
	fp = (FARP *) (pcmd);
1599
	memset(fp, 0, sizeof(FARP));
已提交
1600 1601
	lp = (uint32_t *) pcmd;
	*lp++ = be32_to_cpu(nportid);
J
James Smart 已提交
1602
	*lp++ = be32_to_cpu(vport->fc_myDID);
已提交
1603 1604 1605
	fp->Rflags = 0;
	fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);

1606 1607
	memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
	memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
J
James Smart 已提交
1608 1609
	ondlp = lpfc_findnode_did(vport, nportid);
	if (ondlp) {
已提交
1610
		memcpy(&fp->OportName, &ondlp->nlp_portname,
1611
		       sizeof(struct lpfc_name));
已提交
1612
		memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
1613
		       sizeof(struct lpfc_name));
已提交
1614 1615
	}

J
James Smart 已提交
1616 1617 1618 1619
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue FARPR:     did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1620 1621 1622
	phba->fc_stat.elsXmitFARPR++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1623
		lpfc_nlp_put(ndlp);
已提交
1624
		lpfc_els_free_iocb(phba, elsiocb);
1625
		return 1;
已提交
1626
	}
1627
	lpfc_nlp_put(ndlp);
1628
	return 0;
已提交
1629 1630
}

1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
static void
lpfc_end_rscn(struct lpfc_vport *vport)
{
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

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

1652
void
J
James Smart 已提交
1653
lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
1654
{
J
James Smart 已提交
1655 1656 1657
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	spin_lock_irq(shost->host_lock);
1658
	nlp->nlp_flag &= ~NLP_DELAY_TMO;
J
James Smart 已提交
1659
	spin_unlock_irq(shost->host_lock);
1660 1661 1662 1663 1664 1665 1666
	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) {
J
James Smart 已提交
1667
		spin_lock_irq(shost->host_lock);
1668
		nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
J
James Smart 已提交
1669 1670
		spin_unlock_irq(shost->host_lock);
		if (vport->num_disc_nodes) {
1671 1672 1673
			/* Check to see if there are more
			 * PLOGIs to be sent
			 */
J
James Smart 已提交
1674 1675 1676 1677 1678 1679 1680
			lpfc_more_plogi(vport);

			if (vport->num_disc_nodes == 0) {
				spin_lock_irq(shost->host_lock);
				vport->fc_flag &= ~FC_NDISC_ACTIVE;
				spin_unlock_irq(shost->host_lock);
				lpfc_can_disctmo(vport);
1681
				lpfc_end_rscn(vport);
1682 1683 1684 1685 1686 1687
			}
		}
	}
	return;
}

已提交
1688 1689 1690
void
lpfc_els_retry_delay(unsigned long ptr)
{
J
James Smart 已提交
1691 1692 1693
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
	struct lpfc_vport *vport = ndlp->vport;
	struct lpfc_hba   *phba = vport->phba;
1694
	unsigned long flags;
J
James Smart 已提交
1695
	struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
已提交
1696

J
James Smart 已提交
1697 1698
	ndlp = (struct lpfc_nodelist *) ptr;
	phba = ndlp->vport->phba;
已提交
1699 1700
	evtp = &ndlp->els_retry_evt;

1701
	spin_lock_irqsave(&phba->hbalock, flags);
已提交
1702
	if (!list_empty(&evtp->evt_listp)) {
1703
		spin_unlock_irqrestore(&phba->hbalock, flags);
已提交
1704 1705 1706 1707 1708 1709 1710
		return;
	}

	evtp->evt_arg1  = ndlp;
	evtp->evt       = LPFC_EVT_ELS_RETRY;
	list_add_tail(&evtp->evt_listp, &phba->work_list);
	if (phba->work_wait)
1711
		lpfc_worker_wake_up(phba);
已提交
1712

1713
	spin_unlock_irqrestore(&phba->hbalock, flags);
已提交
1714 1715 1716 1717 1718 1719
	return;
}

void
lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
{
J
James Smart 已提交
1720 1721 1722
	struct lpfc_vport *vport = ndlp->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	uint32_t cmd, did, retry;
已提交
1723

J
James Smart 已提交
1724
	spin_lock_irq(shost->host_lock);
1725 1726 1727
	did = ndlp->nlp_DID;
	cmd = ndlp->nlp_last_elscmd;
	ndlp->nlp_last_elscmd = 0;
已提交
1728 1729

	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
J
James Smart 已提交
1730
		spin_unlock_irq(shost->host_lock);
已提交
1731 1732 1733 1734
		return;
	}

	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
J
James Smart 已提交
1735
	spin_unlock_irq(shost->host_lock);
1736 1737 1738 1739 1740 1741
	/*
	 * 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);
已提交
1742 1743 1744 1745
	retry = ndlp->nlp_retry;

	switch (cmd) {
	case ELS_CMD_FLOGI:
J
James Smart 已提交
1746
		lpfc_issue_els_flogi(vport, ndlp, retry);
已提交
1747 1748
		break;
	case ELS_CMD_PLOGI:
J
James Smart 已提交
1749
		if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
1750
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
1751
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1752
		}
已提交
1753 1754
		break;
	case ELS_CMD_ADISC:
J
James Smart 已提交
1755
		if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
1756
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
1757
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
1758
		}
已提交
1759 1760
		break;
	case ELS_CMD_PRLI:
J
James Smart 已提交
1761
		if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
1762
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
1763
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1764
		}
已提交
1765 1766
		break;
	case ELS_CMD_LOGO:
J
James Smart 已提交
1767
		if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
1768
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
1769
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1770
		}
已提交
1771
		break;
1772 1773 1774
	case ELS_CMD_FDISC:
		lpfc_issue_els_fdisc(vport, ndlp, retry);
		break;
已提交
1775 1776 1777 1778 1779
	}
	return;
}

static int
J
James Smart 已提交
1780 1781
lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
	       struct lpfc_iocbq *rspiocb)
已提交
1782
{
J
James Smart 已提交
1783 1784 1785 1786 1787
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	IOCB_t *irsp = &rspiocb->iocb;
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
已提交
1788 1789
	uint32_t *elscmd;
	struct ls_rjt stat;
J
James Smart 已提交
1790 1791
	int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
	uint32_t cmd = 0;
1792
	uint32_t did;
已提交
1793

1794

已提交
1795 1796 1797 1798 1799 1800 1801 1802 1803
	/* Note: context2 may be 0 for internal driver abort
	 * of delays ELS command.
	 */

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

1804
	if (ndlp)
1805 1806 1807 1808
		did = ndlp->nlp_DID;
	else {
		/* We should only hit this case for retrying PLOGI */
		did = irsp->un.elsreq64.remoteID;
J
James Smart 已提交
1809
		ndlp = lpfc_findnode_did(vport, did);
1810 1811 1812 1813
		if (!ndlp && (cmd != ELS_CMD_PLOGI))
			return 1;
	}

J
James Smart 已提交
1814 1815 1816 1817
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Retry ELS:       wd7:x%x wd4:x%x did:x%x",
		*(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);

已提交
1818 1819 1820 1821 1822 1823 1824 1825
	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:
J
James Smart 已提交
1826
			if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
1827
				delay = 1000;
已提交
1828 1829 1830
			retry = 1;
			break;

1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
		case IOERR_ILLEGAL_COMMAND:
			if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) &&
			    (cmd == ELS_CMD_FDISC)) {
				lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
				"%d (%d):0124 FDISC failed (3/6) retrying...\n",
					phba->brd_no, vport->vpi);
				lpfc_mbx_unreg_vpi(vport);
				retry = 1;
				/* Always retry for this case */
				cmdiocb->retry = 0;
			}
			break;

已提交
1844
		case IOERR_NO_RESOURCES:
J
James Smart 已提交
1845 1846 1847 1848 1849 1850 1851
			retry = 1;
			if (cmdiocb->retry > 100)
				delay = 100;
			maxretry = 250;
			break;

		case IOERR_ILLEGAL_FRAME:
1852
			delay = 100;
已提交
1853 1854 1855
			retry = 1;
			break;

J
James Smart 已提交
1856
		case IOERR_SEQUENCE_TIMEOUT:
已提交
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
		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) {
1886
					delay = 1000;
已提交
1887 1888 1889 1890 1891 1892
					maxretry = 48;
				}
				retry = 1;
				break;
			}
			if (cmd == ELS_CMD_PLOGI) {
1893
				delay = 1000;
已提交
1894 1895 1896 1897
				maxretry = lpfc_max_els_tries + 1;
				retry = 1;
				break;
			}
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
			  (cmd == ELS_CMD_FDISC) &&
			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
				lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
				"%d (%d):0125 FDISC Failed (x%x)."
				" Fabric out of resources\n",
				phba->brd_no, vport->vpi, stat.un.lsRjtError);
				lpfc_vport_set_state(vport,
						     FC_VPORT_NO_FABRIC_RSCS);
			}
已提交
1908 1909 1910
			break;

		case LSRJT_LOGICAL_BSY:
J
James Smart 已提交
1911 1912
			if ((cmd == ELS_CMD_PLOGI) ||
			    (cmd == ELS_CMD_PRLI)) {
1913
				delay = 1000;
已提交
1914
				maxretry = 48;
1915 1916 1917
			} else if (cmd == ELS_CMD_FDISC) {
				/* Always retry for this case */
				cmdiocb->retry = 0;
已提交
1918 1919 1920
			}
			retry = 1;
			break;
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936

		case LSRJT_LOGICAL_ERR:
		case LSRJT_PROTOCOL_ERR:
			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
			  (cmd == ELS_CMD_FDISC) &&
			  ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
			  ) {
				lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
				"%d (%d):0123 FDISC Failed (x%x)."
				" Fabric Detected Bad WWN\n",
				phba->brd_no, vport->vpi, stat.un.lsRjtError);
				lpfc_vport_set_state(vport,
						     FC_VPORT_FABRIC_REJ_WWN);
			}
			break;
已提交
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
		}
		break;

	case IOSTAT_INTERMED_RSP:
	case IOSTAT_BA_RJT:
		break;

	default:
		break;
	}

1948
	if (did == FDMI_DID)
已提交
1949 1950 1951 1952 1953 1954 1955
		retry = 1;

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

1956 1957 1958
	if ((vport->load_flag & FC_UNLOADING) != 0)
		retry = 0;

已提交
1959 1960 1961 1962
	if (retry) {

		/* Retry ELS command <elsCmd> to remote NPORT <did> */
		lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1963
				"%d (%d):0107 Retry ELS command x%x to remote "
已提交
1964
				"NPORT x%x Data: x%x x%x\n",
1965
				phba->brd_no, vport->vpi,
1966
				cmd, did, cmdiocb->retry, delay);
已提交
1967

J
James Smart 已提交
1968 1969 1970 1971 1972
		if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
			((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
			((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
			/* Don't reset timer for no resources */

已提交
1973
			/* If discovery / RSCN timer is running, reset it */
J
James Smart 已提交
1974
			if (timer_pending(&vport->fc_disctmo) ||
1975
			    (vport->fc_flag & FC_RSCN_MODE))
J
James Smart 已提交
1976
				lpfc_set_disctmo(vport);
已提交
1977 1978 1979
		}

		phba->fc_stat.elsXmitRetry++;
1980
		if (ndlp && delay) {
已提交
1981 1982 1983
			phba->fc_stat.elsDelayRetry++;
			ndlp->nlp_retry = cmdiocb->retry;

1984 1985 1986
			/* delay is specified in milliseconds */
			mod_timer(&ndlp->nlp_delayfunc,
				jiffies + msecs_to_jiffies(delay));
J
James Smart 已提交
1987
			spin_lock_irq(shost->host_lock);
已提交
1988
			ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
1989
			spin_unlock_irq(shost->host_lock);
已提交
1990

1991
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
1992 1993 1994 1995 1996 1997
			if (cmd == ELS_CMD_PRLI)
				lpfc_nlp_set_state(vport, ndlp,
					NLP_STE_REG_LOGIN_ISSUE);
			else
				lpfc_nlp_set_state(vport, ndlp,
					NLP_STE_NPR_NODE);
已提交
1998 1999
			ndlp->nlp_last_elscmd = cmd;

2000
			return 1;
已提交
2001 2002 2003
		}
		switch (cmd) {
		case ELS_CMD_FLOGI:
J
James Smart 已提交
2004
			lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
2005
			return 1;
2006 2007 2008
		case ELS_CMD_FDISC:
			lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
			return 1;
已提交
2009
		case ELS_CMD_PLOGI:
2010 2011
			if (ndlp) {
				ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2012
				lpfc_nlp_set_state(vport, ndlp,
2013
						   NLP_STE_PLOGI_ISSUE);
2014
			}
J
James Smart 已提交
2015
			lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2016
			return 1;
已提交
2017
		case ELS_CMD_ADISC:
2018
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2019 2020
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2021
			return 1;
已提交
2022
		case ELS_CMD_PRLI:
2023
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2024 2025
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
			lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
2026
			return 1;
已提交
2027
		case ELS_CMD_LOGO:
2028
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2029 2030
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
			lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2031
			return 1;
已提交
2032 2033 2034 2035 2036
		}
	}

	/* No retry ELS command <elsCmd> to remote NPORT <did> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
2037 2038 2039
			"%d (%d):0108 No retry ELS command x%x to remote "
			"NPORT x%x Data: x%x\n",
			phba->brd_no, vport->vpi,
2040
			cmd, did, cmdiocb->retry);
已提交
2041

2042
	return 0;
已提交
2043 2044 2045
}

int
2046
lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
已提交
2047 2048 2049
{
	struct lpfc_dmabuf *buf_ptr, *buf_ptr1;

2050 2051 2052 2053
	if (elsiocb->context1) {
		lpfc_nlp_put(elsiocb->context1);
		elsiocb->context1 = NULL;
	}
已提交
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073
	/* 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);
	}
2074
	lpfc_sli_release_iocbq(phba, elsiocb);
已提交
2075 2076 2077 2078
	return 0;
}

static void
J
James Smart 已提交
2079 2080
lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		       struct lpfc_iocbq *rspiocb)
已提交
2081
{
J
James Smart 已提交
2082 2083
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	struct lpfc_vport *vport = cmdiocb->vport;
J
James Smart 已提交
2084 2085 2086 2087 2088 2089
	IOCB_t *irsp;

	irsp = &rspiocb->iocb;
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
		"ACC LOGO cmpl:   status:x%x/x%x did:x%x",
		irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
已提交
2090 2091 2092

	/* ACC to LOGO completes to NPort <nlp_DID> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
2093
			"%d (%d):0109 ACC to LOGO completes to NPort x%x "
已提交
2094
			"Data: x%x x%x x%x\n",
2095 2096
			phba->brd_no, vport->vpi, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
已提交
2097 2098 2099

	switch (ndlp->nlp_state) {
	case NLP_STE_UNUSED_NODE:	/* node is just allocated */
J
James Smart 已提交
2100
		lpfc_drop_node(vport, ndlp);
已提交
2101 2102
		break;
	case NLP_STE_NPR_NODE:		/* NPort Recovery mode */
J
James Smart 已提交
2103
		lpfc_unreg_rpi(vport, ndlp);
已提交
2104 2105 2106 2107 2108 2109 2110 2111
		break;
	default:
		break;
	}
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

J
James Smart 已提交
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
void
lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
{
	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;

	pmb->context1 = NULL;
	lpfc_mbuf_free(phba, mp->virt, mp->phys);
	kfree(mp);
	mempool_free(pmb, phba->mbox_mem_pool);
	lpfc_nlp_put(ndlp);
	return;
}

已提交
2126
static void
J
James Smart 已提交
2127
lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2128
		  struct lpfc_iocbq *rspiocb)
已提交
2129
{
J
James Smart 已提交
2130 2131 2132
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
	struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
J
James Smart 已提交
2133
	IOCB_t *irsp;
已提交
2134
	LPFC_MBOXQ_t *mbox = NULL;
J
James Smart 已提交
2135
	struct lpfc_dmabuf *mp = NULL;
已提交
2136

J
James Smart 已提交
2137 2138
	irsp = &rspiocb->iocb;

已提交
2139 2140 2141 2142
	if (cmdiocb->context_un.mbox)
		mbox = cmdiocb->context_un.mbox;

	/* Check to see if link went down during discovery */
J
James Smart 已提交
2143
	if (!ndlp || lpfc_els_chk_latt(vport)) {
已提交
2144
		if (mbox) {
2145 2146 2147 2148 2149
			mp = (struct lpfc_dmabuf *) mbox->context1;
			if (mp) {
				lpfc_mbuf_free(phba, mp->virt, mp->phys);
				kfree(mp);
			}
2150
			mempool_free(mbox, phba->mbox_mem_pool);
已提交
2151 2152 2153 2154
		}
		goto out;
	}

J
James Smart 已提交
2155 2156 2157 2158 2159
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
		"ACC cmpl:        status:x%x/x%x did:x%x",
		irsp->ulpStatus, irsp->un.ulpWord[4],
		irsp->un.rcvels.remoteID);

已提交
2160 2161
	/* ELS response tag <ulpIoTag> completes */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
2162
			"%d (%d):0110 ELS response tag x%x completes "
2163
			"Data: x%x x%x x%x x%x x%x x%x x%x\n",
2164
			phba->brd_no, vport->vpi,
已提交
2165
			cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
2166
			rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
J
James Smart 已提交
2167
			ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2168
			ndlp->nlp_rpi);
已提交
2169 2170 2171 2172

	if (mbox) {
		if ((rspiocb->iocb.ulpStatus == 0)
		    && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
J
James Smart 已提交
2173
			lpfc_unreg_rpi(vport, ndlp);
2174
			mbox->context2 = lpfc_nlp_get(ndlp);
J
James Smart 已提交
2175
			mbox->vport = vport;
J
James Smart 已提交
2176 2177 2178 2179 2180 2181 2182 2183
			if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
				mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
				mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
			}
			else {
				mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
				ndlp->nlp_prev_state = ndlp->nlp_state;
				lpfc_nlp_set_state(vport, ndlp,
J
James Smart 已提交
2184
					   NLP_STE_REG_LOGIN_ISSUE);
J
James Smart 已提交
2185
			}
已提交
2186 2187 2188 2189 2190
			if (lpfc_sli_issue_mbox(phba, mbox,
						(MBX_NOWAIT | MBX_STOP_IOCB))
			    != MBX_NOT_FINISHED) {
				goto out;
			}
2191
			lpfc_nlp_put(ndlp);
已提交
2192 2193 2194
			/* NOTE: we should have messages for unsuccessful
			   reglogin */
		} else {
J
James Smart 已提交
2195 2196 2197 2198 2199
			/* Do not drop node for lpfc_els_abort'ed ELS cmds */
			if (!lpfc_error_lost_link(irsp) &&
			    ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
				lpfc_drop_node(vport, ndlp);
				ndlp = NULL;
已提交
2200 2201
			}
		}
2202 2203 2204 2205 2206 2207
		mp = (struct lpfc_dmabuf *) mbox->context1;
		if (mp) {
			lpfc_mbuf_free(phba, mp->virt, mp->phys);
			kfree(mp);
		}
		mempool_free(mbox, phba->mbox_mem_pool);
已提交
2208 2209 2210
	}
out:
	if (ndlp) {
J
James Smart 已提交
2211
		spin_lock_irq(shost->host_lock);
J
James Smart 已提交
2212
		ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
J
James Smart 已提交
2213
		spin_unlock_irq(shost->host_lock);
已提交
2214 2215 2216 2217 2218 2219
	}
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
J
James Smart 已提交
2220 2221 2222
lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
		 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
		 LPFC_MBOXQ_t *mbox, uint8_t newnode)
已提交
2223
{
J
James Smart 已提交
2224 2225
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
2226 2227 2228 2229 2230 2231 2232 2233
	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;
2234
	ELS_PKT *els_pkt_ptr;
已提交
2235 2236 2237 2238 2239 2240 2241

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

	switch (flag) {
	case ELS_CMD_ACC:
2242
		cmdsize = sizeof(uint32_t);
J
James Smart 已提交
2243 2244
		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2245
		if (!elsiocb) {
J
James Smart 已提交
2246
			spin_lock_irq(shost->host_lock);
2247
			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
J
James Smart 已提交
2248
			spin_unlock_irq(shost->host_lock);
2249
			return 1;
已提交
2250
		}
J
James Smart 已提交
2251

已提交
2252 2253 2254 2255
		icmd = &elsiocb->iocb;
		icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2256
		pcmd += sizeof(uint32_t);
J
James Smart 已提交
2257 2258 2259 2260

		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
			"Issue ACC:       did:x%x flg:x%x",
			ndlp->nlp_DID, ndlp->nlp_flag, 0);
已提交
2261 2262
		break;
	case ELS_CMD_PLOGI:
2263
		cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
J
James Smart 已提交
2264 2265
		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2266
		if (!elsiocb)
2267
			return 1;
2268

已提交
2269 2270 2271 2272 2273 2274 2275 2276
		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;
2277 2278
		pcmd += sizeof(uint32_t);
		memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
J
James Smart 已提交
2279 2280 2281 2282

		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
			"Issue ACC PLOGI: did:x%x flg:x%x",
			ndlp->nlp_DID, ndlp->nlp_flag, 0);
已提交
2283
		break;
2284
	case ELS_CMD_PRLO:
2285
		cmdsize = sizeof(uint32_t) + sizeof(PRLO);
J
James Smart 已提交
2286
		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2287 2288 2289 2290 2291 2292 2293 2294 2295
					     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,
2296
		       sizeof(uint32_t) + sizeof(PRLO));
2297 2298 2299
		*((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
		els_pkt_ptr = (ELS_PKT *) pcmd;
		els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
J
James Smart 已提交
2300 2301 2302 2303

		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
			"Issue ACC PRLO:  did:x%x flg:x%x",
			ndlp->nlp_DID, ndlp->nlp_flag, 0);
2304
		break;
已提交
2305
	default:
2306
		return 1;
已提交
2307 2308
	}

2309 2310
	if (newnode) {
		lpfc_nlp_put(ndlp);
已提交
2311
		elsiocb->context1 = NULL;
2312
	}
已提交
2313 2314 2315

	/* Xmit ELS ACC response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
2316
			"%d (%d):0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
2317
			"DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
2318
			phba->brd_no, vport->vpi, elsiocb->iotag,
已提交
2319 2320 2321 2322
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

	if (ndlp->nlp_flag & NLP_LOGO_ACC) {
J
James Smart 已提交
2323
		spin_lock_irq(shost->host_lock);
2324
		ndlp->nlp_flag &= ~NLP_LOGO_ACC;
J
James Smart 已提交
2325
		spin_unlock_irq(shost->host_lock);
已提交
2326 2327
		elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
	} else {
J
James Smart 已提交
2328
		elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2329 2330 2331 2332 2333 2334
	}

	phba->fc_stat.elsXmitACC++;
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2335
		return 1;
已提交
2336
	}
2337
	return 0;
已提交
2338 2339 2340
}

int
J
James Smart 已提交
2341
lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
J
James Smart 已提交
2342 2343
		    struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
		    LPFC_MBOXQ_t *mbox)
已提交
2344
{
J
James Smart 已提交
2345
	struct lpfc_hba  *phba = vport->phba;
已提交
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
	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 */

2358
	cmdsize = 2 * sizeof(uint32_t);
J
James Smart 已提交
2359 2360
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
2361
	if (!elsiocb)
2362
		return 1;
已提交
2363 2364 2365 2366 2367 2368 2369

	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;
2370
	pcmd += sizeof(uint32_t);
已提交
2371 2372
	*((uint32_t *) (pcmd)) = rejectError;

J
James Smart 已提交
2373 2374 2375 2376 2377
	if (mbox) {
		elsiocb->context_un.mbox = mbox;
		elsiocb->context1 = lpfc_nlp_get(ndlp);
	}

已提交
2378 2379
	/* Xmit ELS RJT <err> response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
2380 2381 2382 2383
			"%d (%d):0129 Xmit ELS RJT x%x response tag x%x "
			"xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
			"rpi x%x\n",
			phba->brd_no, vport->vpi, rejectError, elsiocb->iotag,
已提交
2384 2385 2386
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

J
James Smart 已提交
2387 2388 2389 2390
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
		"Issue LS_RJT:    did:x%x flg:x%x err:x%x",
		ndlp->nlp_DID, ndlp->nlp_flag, rejectError);

已提交
2391
	phba->fc_stat.elsXmitLSRJT++;
J
James Smart 已提交
2392
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2393 2394 2395
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2396
		return 1;
已提交
2397
	}
2398
	return 0;
已提交
2399 2400 2401
}

int
J
James Smart 已提交
2402 2403
lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
		       struct lpfc_nodelist *ndlp)
已提交
2404
{
J
James Smart 已提交
2405 2406 2407
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_sli  *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
已提交
2408
	ADISC *ap;
J
James Smart 已提交
2409
	IOCB_t *icmd, *oldcmd;
已提交
2410 2411 2412 2413 2414
	struct lpfc_iocbq *elsiocb;
	uint8_t *pcmd;
	uint16_t cmdsize;
	int rc;

2415
	cmdsize = sizeof(uint32_t) + sizeof(ADISC);
J
James Smart 已提交
2416 2417
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
2418
	if (!elsiocb)
2419
		return 1;
已提交
2420

2421 2422 2423 2424
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */

已提交
2425 2426
	/* Xmit ADISC ACC response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
2427
			"%d (%d):0130 Xmit ADISC ACC response iotag x%x xri: "
2428
			"x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
2429
			phba->brd_no, vport->vpi, elsiocb->iotag,
已提交
2430 2431 2432 2433 2434 2435
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

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

	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2436
	pcmd += sizeof(uint32_t);
已提交
2437 2438 2439

	ap = (ADISC *) (pcmd);
	ap->hardAL_PA = phba->fc_pref_ALPA;
2440 2441
	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
J
James Smart 已提交
2442
	ap->DID = be32_to_cpu(vport->fc_myDID);
已提交
2443

J
James Smart 已提交
2444 2445 2446 2447
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
		"Issue ACC ADISC: did:x%x flg:x%x",
		ndlp->nlp_DID, ndlp->nlp_flag, 0);

已提交
2448
	phba->fc_stat.elsXmitACC++;
J
James Smart 已提交
2449
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2450 2451 2452
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2453
		return 1;
已提交
2454
	}
2455
	return 0;
已提交
2456 2457 2458
}

int
J
James Smart 已提交
2459
lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
2460
		      struct lpfc_nodelist *ndlp)
已提交
2461
{
J
James Smart 已提交
2462
	struct lpfc_hba  *phba = vport->phba;
已提交
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
	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 */

2477
	cmdsize = sizeof(uint32_t) + sizeof(PRLI);
J
James Smart 已提交
2478
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2479
		ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
2480 2481
	if (!elsiocb)
		return 1;
已提交
2482

2483 2484 2485 2486
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */

已提交
2487 2488
	/* Xmit PRLI ACC response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
2489
			"%d (%d):0131 Xmit PRLI ACC response tag x%x xri x%x, "
2490
			"did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
2491
			phba->brd_no, vport->vpi, elsiocb->iotag,
已提交
2492 2493 2494 2495 2496 2497
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

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

	*((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
2498
	pcmd += sizeof(uint32_t);
已提交
2499 2500

	/* For PRLI, remainder of payload is PRLI parameter page */
2501
	memset(pcmd, 0, sizeof(PRLI));
已提交
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522

	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;

J
James Smart 已提交
2523 2524 2525 2526
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
		"Issue ACC PRLI:  did:x%x flg:x%x",
		ndlp->nlp_DID, ndlp->nlp_flag, 0);

已提交
2527
	phba->fc_stat.elsXmitACC++;
J
James Smart 已提交
2528
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2529 2530 2531 2532

	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2533
		return 1;
已提交
2534
	}
2535
	return 0;
已提交
2536 2537 2538
}

static int
J
James Smart 已提交
2539
lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
2540
		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
已提交
2541
{
J
James Smart 已提交
2542
	struct lpfc_hba  *phba = vport->phba;
已提交
2543
	RNID *rn;
J
James Smart 已提交
2544
	IOCB_t *icmd, *oldcmd;
已提交
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554
	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];

2555 2556
	cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
					+ (2 * sizeof(struct lpfc_name));
已提交
2557
	if (format)
2558
		cmdsize += sizeof(RNID_TOP_DISC);
已提交
2559

J
James Smart 已提交
2560 2561
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
2562
	if (!elsiocb)
2563
		return 1;
已提交
2564

2565 2566 2567 2568
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */

已提交
2569 2570
	/* Xmit RNID ACC response tag <ulpIoTag> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
2571
			"%d (%d):0132 Xmit RNID ACC response tag x%x "
2572
			"xri x%x\n",
2573
			phba->brd_no, vport->vpi, elsiocb->iotag,
已提交
2574 2575 2576 2577 2578
			elsiocb->iocb.ulpContext);

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

	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2579
	pcmd += sizeof(uint32_t);
已提交
2580

2581
	memset(pcmd, 0, sizeof(RNID));
已提交
2582 2583
	rn = (RNID *) (pcmd);
	rn->Format = format;
2584 2585 2586
	rn->CommonLen = (2 * sizeof(struct lpfc_name));
	memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
	memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
已提交
2587 2588 2589 2590 2591
	switch (format) {
	case 0:
		rn->SpecificLen = 0;
		break;
	case RNID_TOPOLOGY_DISC:
2592
		rn->SpecificLen = sizeof(RNID_TOP_DISC);
已提交
2593
		memcpy(&rn->un.topologyDisc.portName,
2594
		       &vport->fc_portname, sizeof(struct lpfc_name));
已提交
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
		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;
	}

J
James Smart 已提交
2605 2606 2607 2608
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
		"Issue ACC RNID:  did:x%x flg:x%x",
		ndlp->nlp_DID, ndlp->nlp_flag, 0);

已提交
2609
	phba->fc_stat.elsXmitACC++;
J
James Smart 已提交
2610
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2611
	lpfc_nlp_put(ndlp);
已提交
2612 2613 2614 2615 2616 2617
	elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
				    * it could be freed */

	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2618
		return 1;
已提交
2619
	}
2620
	return 0;
已提交
2621 2622 2623
}

int
J
James Smart 已提交
2624
lpfc_els_disc_adisc(struct lpfc_vport *vport)
已提交
2625
{
J
James Smart 已提交
2626
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
2627
	struct lpfc_nodelist *ndlp, *next_ndlp;
J
James Smart 已提交
2628
	int sentadisc = 0;
已提交
2629

2630
	/* go thru NPR nodes and issue any remaining ELS ADISCs */
J
James Smart 已提交
2631
	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2632 2633 2634
		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
		    (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
		    (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
J
James Smart 已提交
2635
			spin_lock_irq(shost->host_lock);
2636
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
2637
			spin_unlock_irq(shost->host_lock);
2638
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2639 2640
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
2641
			sentadisc++;
J
James Smart 已提交
2642 2643 2644 2645 2646 2647
			vport->num_disc_nodes++;
			if (vport->num_disc_nodes >=
			    vport->phba->cfg_discovery_threads) {
				spin_lock_irq(shost->host_lock);
				vport->fc_flag |= FC_NLP_MORE;
				spin_unlock_irq(shost->host_lock);
2648
				break;
已提交
2649 2650 2651 2652
			}
		}
	}
	if (sentadisc == 0) {
J
James Smart 已提交
2653 2654 2655
		spin_lock_irq(shost->host_lock);
		vport->fc_flag &= ~FC_NLP_MORE;
		spin_unlock_irq(shost->host_lock);
已提交
2656
	}
2657
	return sentadisc;
已提交
2658 2659 2660
}

int
J
James Smart 已提交
2661
lpfc_els_disc_plogi(struct lpfc_vport *vport)
已提交
2662
{
J
James Smart 已提交
2663
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
2664
	struct lpfc_nodelist *ndlp, *next_ndlp;
J
James Smart 已提交
2665
	int sentplogi = 0;
已提交
2666

J
James Smart 已提交
2667 2668
	/* go thru NPR nodes and issue any remaining ELS PLOGIs */
	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2669 2670 2671 2672 2673
		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
		    (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
		    (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
		    (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2674 2675
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2676
			sentplogi++;
J
James Smart 已提交
2677 2678 2679 2680 2681 2682
			vport->num_disc_nodes++;
			if (vport->num_disc_nodes >=
			    vport->phba->cfg_discovery_threads) {
				spin_lock_irq(shost->host_lock);
				vport->fc_flag |= FC_NLP_MORE;
				spin_unlock_irq(shost->host_lock);
2683
				break;
已提交
2684 2685 2686 2687
			}
		}
	}
	if (sentplogi == 0) {
J
James Smart 已提交
2688 2689 2690
		spin_lock_irq(shost->host_lock);
		vport->fc_flag &= ~FC_NLP_MORE;
		spin_unlock_irq(shost->host_lock);
已提交
2691
	}
2692
	return sentplogi;
已提交
2693 2694
}

2695
void
J
James Smart 已提交
2696
lpfc_els_flush_rscn(struct lpfc_vport *vport)
已提交
2697
{
J
James Smart 已提交
2698 2699
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
2700 2701
	int i;

J
James Smart 已提交
2702
	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
2703
		lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
J
James Smart 已提交
2704
		vport->fc_rscn_id_list[i] = NULL;
已提交
2705
	}
J
James Smart 已提交
2706 2707 2708 2709 2710
	spin_lock_irq(shost->host_lock);
	vport->fc_rscn_id_cnt = 0;
	vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
	spin_unlock_irq(shost->host_lock);
	lpfc_can_disctmo(vport);
已提交
2711 2712 2713
}

int
J
James Smart 已提交
2714
lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
已提交
2715 2716 2717 2718
{
	D_ID ns_did;
	D_ID rscn_did;
	uint32_t *lp;
2719
	uint32_t payload_len, i;
J
James Smart 已提交
2720
	struct lpfc_hba *phba = vport->phba;
已提交
2721 2722 2723 2724 2725

	ns_did.un.word = did;

	/* Never match fabric nodes for RSCNs */
	if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
J
James Smart 已提交
2726
		return 0;
已提交
2727 2728

	/* If we are doing a FULL RSCN rediscovery, match everything */
J
James Smart 已提交
2729
	if (vport->fc_flag & FC_RSCN_DISCOVERY)
2730
		return did;
已提交
2731

J
James Smart 已提交
2732
	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
2733 2734 2735
		lp = vport->fc_rscn_id_list[i]->virt;
		payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
		payload_len -= sizeof(uint32_t);	/* take off word 0 */
已提交
2736
		while (payload_len) {
2737 2738
			rscn_did.un.word = be32_to_cpu(*lp++);
			payload_len -= sizeof(uint32_t);
已提交
2739 2740
			switch (rscn_did.un.b.resv) {
			case 0:	/* Single N_Port ID effected */
J
James Smart 已提交
2741
				if (ns_did.un.word == rscn_did.un.word)
2742
					return did;
已提交
2743 2744 2745 2746
				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))
2747
					return did;
已提交
2748 2749 2750
				break;
			case 2:	/* Whole N_Port Domain effected */
				if (ns_did.un.b.domain == rscn_did.un.b.domain)
2751
					return did;
已提交
2752 2753
				break;
			default:
J
James Smart 已提交
2754
				/* Unknown Identifier in RSCN node */
已提交
2755
				lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2756 2757 2758 2759 2760 2761 2762
						"%d (%d):0217 Unknown "
						"Identifier in RSCN payload "
						"Data: x%x\n",
						phba->brd_no, vport->vpi,
						rscn_did.un.word);
			case 3:	/* Whole Fabric effected */
				return did;
已提交
2763 2764
			}
		}
2765 2766
	}
	return 0;
已提交
2767 2768 2769
}

static int
J
James Smart 已提交
2770
lpfc_rscn_recovery_check(struct lpfc_vport *vport)
已提交
2771
{
2772
	struct lpfc_nodelist *ndlp = NULL;
已提交
2773 2774

	/* Look at all nodes effected by pending RSCNs and move
2775
	 * them to NPR state.
已提交
2776 2777
	 */

J
James Smart 已提交
2778
	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2779
		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE ||
J
James Smart 已提交
2780
		    lpfc_rscn_payload_check(vport, ndlp->nlp_DID) == 0)
2781
			continue;
已提交
2782

J
James Smart 已提交
2783
		lpfc_disc_state_machine(vport, ndlp, NULL,
2784
						NLP_EVT_DEVICE_RECOVERY);
2785

2786 2787 2788 2789 2790
		/*
		 * Make sure NLP_DELAY_TMO is NOT running after a device
		 * recovery event.
		 */
		if (ndlp->nlp_flag & NLP_DELAY_TMO)
J
James Smart 已提交
2791
			lpfc_cancel_retry_delay_tmo(vport, ndlp);
已提交
2792
	}
2793

2794
	return 0;
已提交
2795 2796 2797
}

static int
J
James Smart 已提交
2798 2799
lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_nodelist *ndlp, uint8_t newnode)
已提交
2800
{
J
James Smart 已提交
2801 2802
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
2803
	struct lpfc_dmabuf *pcmd;
2804 2805
	struct lpfc_vport *next_vport;
	uint32_t *lp, *datap;
已提交
2806
	IOCB_t *icmd;
2807 2808 2809
	uint32_t payload_len, length, nportid, *cmd;
	int rscn_cnt = vport->fc_rscn_id_cnt;
	int rscn_id = 0, hba_id = 0;
2810
	int i;
已提交
2811 2812 2813 2814 2815

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

2816 2817
	payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
	payload_len -= sizeof(uint32_t);	/* take off word 0 */
已提交
2818 2819

	/* RSCN received */
2820
	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
2821 2822 2823
			"%d (%d):0214 RSCN received Data: x%x x%x x%x x%x\n",
			phba->brd_no, vport->vpi, vport->fc_flag, payload_len,
			*lp, rscn_cnt);
已提交
2824

2825
	for (i = 0; i < payload_len/sizeof(uint32_t); i++)
J
James Smart 已提交
2826
		fc_host_post_event(shost, fc_get_event_number(),
2827 2828
			FCH_EVT_RSCN, lp[i]);

已提交
2829 2830 2831
	/* If we are about to begin discovery, just ACC the RSCN.
	 * Discovery processing will satisfy it.
	 */
J
James Smart 已提交
2832
	if (vport->port_state <= LPFC_NS_QRY) {
J
James Smart 已提交
2833 2834 2835 2836
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);

J
James Smart 已提交
2837
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL,
2838
				 newnode);
2839
		return 0;
已提交
2840 2841
	}

2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867
	/* If this RSCN just contains NPortIDs for other vports on this HBA,
	 * just ACC and ignore it.
	 */
	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
		!(phba->cfg_peer_port_login)) {
		i = payload_len;
		datap = lp;
		while (i > 0) {
			nportid = *datap++;
			nportid = ((be32_to_cpu(nportid)) & Mask_DID);
			i -= sizeof(uint32_t);
			rscn_id++;
			list_for_each_entry(next_vport, &phba->port_list,
				listentry) {
				if (nportid == next_vport->fc_myDID) {
					hba_id++;
					break;
				}
			}
		}
		if (rscn_id == hba_id) {
			/* ALL NPortIDs in RSCN are on HBA */
			lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
			  "%d (%d):0214 Ignore RSCN Data: x%x x%x x%x x%x\n",
			  phba->brd_no, vport->vpi, vport->fc_flag, payload_len,
			  *lp, rscn_cnt);
J
James Smart 已提交
2868 2869 2870 2871 2872 2873

			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
				"RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
				ndlp->nlp_DID, vport->port_state,
				ndlp->nlp_flag);

2874 2875 2876 2877 2878 2879
			lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
				ndlp, NULL, newnode);
			return 0;
		}
	}

已提交
2880 2881 2882
	/* If we are already processing an RSCN, save the received
	 * RSCN payload buffer, cmdiocb->context2 to process later.
	 */
J
James Smart 已提交
2883
	if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
J
James Smart 已提交
2884 2885 2886 2887
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);

2888 2889
		vport->fc_flag |= FC_RSCN_DEFERRED;
		if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
J
James Smart 已提交
2890 2891 2892 2893
		    !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
			spin_lock_irq(shost->host_lock);
			vport->fc_flag |= FC_RSCN_MODE;
			spin_unlock_irq(shost->host_lock);
2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
			if (rscn_cnt) {
				cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
				length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
			}
			if ((rscn_cnt) &&
			    (payload_len + length <= LPFC_BPL_SIZE)) {
				*cmd &= ELS_CMD_MASK;
				*cmd |= be32_to_cpu(payload_len + length);
				memcpy(((uint8_t *)cmd) + length, lp,
				       payload_len);
			} else {
				vport->fc_rscn_id_list[rscn_cnt] = pcmd;
				vport->fc_rscn_id_cnt++;
				/* If we zero, cmdiocb->context2, the calling
				 * routine will not try to free it.
				 */
				cmdiocb->context2 = NULL;
			}
已提交
2912 2913 2914

			/* Deferred RSCN */
			lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
2915
					"%d (%d):0235 Deferred RSCN "
已提交
2916
					"Data: x%x x%x x%x\n",
2917 2918
					phba->brd_no, vport->vpi,
					vport->fc_rscn_id_cnt, vport->fc_flag,
J
James Smart 已提交
2919
					vport->port_state);
已提交
2920
		} else {
J
James Smart 已提交
2921 2922 2923
			spin_lock_irq(shost->host_lock);
			vport->fc_flag |= FC_RSCN_DISCOVERY;
			spin_unlock_irq(shost->host_lock);
已提交
2924 2925
			/* ReDiscovery RSCN */
			lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
2926
					"%d (%d):0234 ReDiscovery RSCN "
已提交
2927
					"Data: x%x x%x x%x\n",
2928 2929
					phba->brd_no, vport->vpi,
					vport->fc_rscn_id_cnt, vport->fc_flag,
J
James Smart 已提交
2930
					vport->port_state);
已提交
2931 2932
		}
		/* Send back ACC */
J
James Smart 已提交
2933
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL,
已提交
2934 2935 2936
								newnode);

		/* send RECOVERY event for ALL nodes that match RSCN payload */
J
James Smart 已提交
2937
		lpfc_rscn_recovery_check(vport);
2938
		vport->fc_flag &= ~FC_RSCN_DEFERRED;
2939
		return 0;
已提交
2940 2941
	}

J
James Smart 已提交
2942 2943 2944 2945
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
		"RCV RSCN:        did:x%x/ste:x%x flg:x%x",
		ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);

J
James Smart 已提交
2946 2947 2948 2949
	spin_lock_irq(shost->host_lock);
	vport->fc_flag |= FC_RSCN_MODE;
	spin_unlock_irq(shost->host_lock);
	vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
已提交
2950 2951 2952 2953 2954 2955
	/*
	 * If we zero, cmdiocb->context2, the calling routine will
	 * not try to free it.
	 */
	cmdiocb->context2 = NULL;

J
James Smart 已提交
2956
	lpfc_set_disctmo(vport);
已提交
2957 2958

	/* Send back ACC */
J
James Smart 已提交
2959
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL, newnode);
已提交
2960 2961

	/* send RECOVERY event for ALL nodes that match RSCN payload */
J
James Smart 已提交
2962
	lpfc_rscn_recovery_check(vport);
已提交
2963

J
James Smart 已提交
2964
	return lpfc_els_handle_rscn(vport);
已提交
2965 2966 2967
}

int
J
James Smart 已提交
2968
lpfc_els_handle_rscn(struct lpfc_vport *vport)
已提交
2969 2970
{
	struct lpfc_nodelist *ndlp;
J
James Smart 已提交
2971
	struct lpfc_hba *phba = vport->phba;
已提交
2972

2973 2974 2975 2976 2977 2978
	/* Ignore RSCN if the port is being torn down. */
	if (vport->load_flag & FC_UNLOADING) {
		lpfc_els_flush_rscn(vport);
		return 0;
	}

已提交
2979
	/* Start timer for RSCN processing */
J
James Smart 已提交
2980
	lpfc_set_disctmo(vport);
已提交
2981 2982

	/* RSCN processed */
2983
	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
2984 2985
			"%d (%d):0215 RSCN processed Data: x%x x%x x%x x%x\n",
			phba->brd_no, vport->vpi,
J
James Smart 已提交
2986 2987
			vport->fc_flag, 0, vport->fc_rscn_id_cnt,
			vport->port_state);
已提交
2988 2989

	/* To process RSCN, first compare RSCN data with NameServer */
J
James Smart 已提交
2990 2991
	vport->fc_ns_retry = 0;
	ndlp = lpfc_findnode_did(vport, NameServer_DID);
2992
	if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
已提交
2993
		/* Good ndlp, issue CT Request to NameServer */
2994
		if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
已提交
2995 2996
			/* Wait for NameServer query cmpl before we can
			   continue */
2997
			return 1;
已提交
2998 2999 3000
	} else {
		/* If login to NameServer does not exist, issue one */
		/* Good status, issue PLOGI to NameServer */
J
James Smart 已提交
3001 3002
		ndlp = lpfc_findnode_did(vport, NameServer_DID);
		if (ndlp)
已提交
3003 3004
			/* Wait for NameServer login cmpl before we can
			   continue */
3005
			return 1;
J
James Smart 已提交
3006

3007 3008
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp) {
J
James Smart 已提交
3009
			lpfc_els_flush_rscn(vport);
3010
			return 0;
已提交
3011
		} else {
J
James Smart 已提交
3012
			lpfc_nlp_init(vport, ndlp, NameServer_DID);
已提交
3013
			ndlp->nlp_type |= NLP_FABRIC;
3014
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
3015 3016
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, NameServer_DID, 0);
已提交
3017 3018
			/* Wait for NameServer login cmpl before we can
			   continue */
3019
			return 1;
已提交
3020 3021 3022
		}
	}

J
James Smart 已提交
3023
	lpfc_els_flush_rscn(vport);
3024
	return 0;
已提交
3025 3026 3027
}

static int
J
James Smart 已提交
3028 3029
lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		   struct lpfc_nodelist *ndlp, uint8_t newnode)
已提交
3030
{
J
James Smart 已提交
3031 3032
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046
	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 */

J
James Smart 已提交
3047
	lpfc_set_disctmo(vport);
已提交
3048 3049 3050 3051 3052 3053 3054 3055

	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,
3056 3057 3058
				"%d (%d):0113 An FLOGI ELS command x%x was "
				"received from DID x%x in Loop Mode\n",
				phba->brd_no, vport->vpi, cmd, did);
3059
		return 1;
已提交
3060 3061 3062 3063
	}

	did = Fabric_DID;

J
James Smart 已提交
3064
	if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3))) {
已提交
3065 3066 3067 3068
		/* For a FLOGI we accept, then if our portname is greater
		 * then the remote portname we initiate Nport login.
		 */

J
James Smart 已提交
3069
		rc = memcmp(&vport->fc_portname, &sp->portName,
3070
			    sizeof(struct lpfc_name));
已提交
3071 3072

		if (!rc) {
J
James Smart 已提交
3073 3074
			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
			if (!mbox)
3075
				return 1;
J
James Smart 已提交
3076

已提交
3077 3078 3079 3080 3081 3082
			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;
3083
			mbox->vport = vport;
已提交
3084 3085
			rc = lpfc_sli_issue_mbox
				(phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
3086
			lpfc_set_loopback_flag(phba);
已提交
3087
			if (rc == MBX_NOT_FINISHED) {
3088
				mempool_free(mbox, phba->mbox_mem_pool);
已提交
3089
			}
3090
			return 1;
3091
		} else if (rc > 0) {	/* greater than */
J
James Smart 已提交
3092 3093 3094
			spin_lock_irq(shost->host_lock);
			vport->fc_flag |= FC_PT2PT_PLOGI;
			spin_unlock_irq(shost->host_lock);
已提交
3095
		}
J
James Smart 已提交
3096 3097 3098 3099
		spin_lock_irq(shost->host_lock);
		vport->fc_flag |= FC_PT2PT;
		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
		spin_unlock_irq(shost->host_lock);
已提交
3100 3101 3102 3103 3104 3105
	} 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;
J
James Smart 已提交
3106 3107
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
3108
		return 1;
已提交
3109 3110 3111
	}

	/* Send back ACC */
J
James Smart 已提交
3112
	lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL, newnode);
已提交
3113

3114
	return 0;
已提交
3115 3116 3117
}

static int
J
James Smart 已提交
3118 3119
lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_nodelist *ndlp)
已提交
3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141
{
	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 */
J
James Smart 已提交
3142
		lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
已提交
3143 3144 3145 3146 3147 3148 3149
		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;
J
James Smart 已提交
3150 3151
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
已提交
3152
	}
3153
	return 0;
已提交
3154 3155 3156
}

static int
J
James Smart 已提交
3157 3158
lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_nodelist *ndlp)
3159 3160 3161 3162 3163 3164 3165 3166
{
	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;
J
James Smart 已提交
3167
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3168 3169 3170
	return 0;
}

3171
static void
3172
lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3173
{
J
James Smart 已提交
3174 3175
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
	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;

	mb = &pmb->mb;

	ndlp = (struct lpfc_nodelist *) pmb->context2;
	xri = (uint16_t) ((unsigned long)(pmb->context1));
R
Randy Dunlap 已提交
3189 3190
	pmb->context1 = NULL;
	pmb->context2 = NULL;
3191 3192

	if (mb->mbxStatus) {
3193
		mempool_free(pmb, phba->mbox_mem_pool);
3194 3195 3196 3197
		return;
	}

	cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
3198
	mempool_free(pmb, phba->mbox_mem_pool);
J
James Smart 已提交
3199 3200 3201
	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
				     lpfc_max_els_tries, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
3202
	lpfc_nlp_put(ndlp);
3203
	if (!elsiocb)
3204 3205 3206 3207 3208 3209 3210
		return;

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

	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3211
	pcmd += sizeof(uint32_t); /* Skip past command */
3212 3213 3214 3215 3216 3217
	rps_rsp = (RPS_RSP *)pcmd;

	if (phba->fc_topology != TOPOLOGY_LOOP)
		status = 0x10;
	else
		status = 0x8;
J
James Smart 已提交
3218
	if (phba->pport->fc_flag & FC_FABRIC)
3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231
		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,
3232 3233 3234 3235
			"%d (%d):0118 Xmit ELS RPS ACC response tag x%x "
			"xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
			"rpi x%x\n",
			phba->brd_no, ndlp->vport->vpi, elsiocb->iotag,
3236 3237 3238
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

J
James Smart 已提交
3239
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3240
	phba->fc_stat.elsXmitACC++;
3241
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR)
3242 3243 3244 3245 3246
		lpfc_els_free_iocb(phba, elsiocb);
	return;
}

static int
J
James Smart 已提交
3247 3248
lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		 struct lpfc_nodelist *ndlp)
已提交
3249
{
J
James Smart 已提交
3250
	struct lpfc_hba *phba = vport->phba;
已提交
3251
	uint32_t *lp;
3252 3253 3254 3255 3256 3257
	uint8_t flag;
	LPFC_MBOXQ_t *mbox;
	struct lpfc_dmabuf *pcmd;
	RPS *rps;
	struct ls_rjt stat;

3258 3259
	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3260 3261 3262 3263
		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;
J
James Smart 已提交
3264 3265
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
3266 3267 3268 3269 3270 3271 3272 3273 3274
	}

	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)) ||
J
James Smart 已提交
3275
	    ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
3276
				    sizeof(struct lpfc_name)) == 0))) {
J
James Smart 已提交
3277

3278 3279
		printk("Fix me....\n");
		dump_stack();
J
James Smart 已提交
3280 3281
		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
		if (mbox) {
3282 3283
			lpfc_read_lnk_stat(phba, mbox);
			mbox->context1 =
3284
			    (void *)((unsigned long) cmdiocb->iocb.ulpContext);
3285
			mbox->context2 = lpfc_nlp_get(ndlp);
3286
			mbox->vport = vport;
3287 3288
			mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
			if (lpfc_sli_issue_mbox (phba, mbox,
J
James Smart 已提交
3289
			    (MBX_NOWAIT | MBX_STOP_IOCB)) != MBX_NOT_FINISHED)
3290 3291
				/* Mbox completion will send ELS Response */
				return 0;
J
James Smart 已提交
3292

3293
			lpfc_nlp_put(ndlp);
3294 3295 3296 3297 3298 3299 3300
			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;
J
James Smart 已提交
3301
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3302 3303 3304
	return 0;
}

3305
static int
J
James Smart 已提交
3306 3307
lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
		     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3308
{
J
James Smart 已提交
3309 3310
	struct lpfc_hba *phba = vport->phba;
	IOCB_t *icmd, *oldcmd;
3311 3312
	RPL_RSP rpl_rsp;
	struct lpfc_iocbq *elsiocb;
J
James Smart 已提交
3313 3314
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3315
	uint8_t *pcmd;
已提交
3316

J
James Smart 已提交
3317 3318
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
3319

3320
	if (!elsiocb)
3321
		return 1;
3322

3323 3324 3325 3326 3327 3328
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */

	pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3329
	pcmd += sizeof(uint16_t);
3330 3331 3332 3333 3334 3335 3336
	*((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;
J
James Smart 已提交
3337 3338
	rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
	memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
3339 3340 3341 3342 3343 3344 3345
	    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,
3346 3347 3348 3349
			"%d (%d):0120 Xmit ELS RPL ACC response tag x%x "
			"xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
			"rpi x%x\n",
			phba->brd_no, vport->vpi, elsiocb->iotag,
3350 3351 3352
			elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);

J
James Smart 已提交
3353
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3354 3355 3356 3357 3358 3359 3360 3361 3362 3363

	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
J
James Smart 已提交
3364 3365
lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		 struct lpfc_nodelist *ndlp)
3366 3367 3368 3369 3370 3371 3372 3373
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	uint32_t maxsize;
	uint16_t cmdsize;
	RPL *rpl;
	struct ls_rjt stat;

3374 3375
	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3376 3377 3378 3379
		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;
J
James Smart 已提交
3380 3381
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
3382 3383
	}

已提交
3384 3385
	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;
3386
	rpl = (RPL *) (lp + 1);
已提交
3387

3388
	maxsize = be32_to_cpu(rpl->maxsize);
已提交
3389

3390 3391 3392 3393 3394
	/* 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);
3395
	} else {
3396 3397
		cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
	}
J
James Smart 已提交
3398
	lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
已提交
3399 3400 3401 3402 3403

	return 0;
}

static int
J
James Smart 已提交
3404 3405
lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_nodelist *ndlp)
已提交
3406
{
J
James Smart 已提交
3407
	struct lpfc_hba *phba = vport->phba;
已提交
3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422
	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> */
3423
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
3424 3425
			"%d (%d):0601 FARP-REQ received from DID x%x\n",
			phba->brd_no, vport->vpi, did);
已提交
3426 3427 3428

	/* We will only support match on WWPN or WWNN */
	if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
3429
		return 0;
已提交
3430 3431 3432 3433 3434
	}

	cnt = 0;
	/* If this FARP command is searching for my portname */
	if (fp->Mflags & FARP_MATCH_PORT) {
J
James Smart 已提交
3435
		if (memcmp(&fp->RportName, &vport->fc_portname,
3436
			   sizeof(struct lpfc_name)) == 0)
已提交
3437 3438 3439 3440 3441
			cnt = 1;
	}

	/* If this FARP command is searching for my nodename */
	if (fp->Mflags & FARP_MATCH_NODE) {
J
James Smart 已提交
3442
		if (memcmp(&fp->RnodeName, &vport->fc_nodename,
3443
			   sizeof(struct lpfc_name)) == 0)
已提交
3444 3445 3446 3447 3448 3449 3450 3451
			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) {
3452
				ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
3453
				lpfc_nlp_set_state(vport, ndlp,
3454
						   NLP_STE_PLOGI_ISSUE);
J
James Smart 已提交
3455
				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
3456 3457 3458
			}

			/* Send a FARP response to that node */
J
James Smart 已提交
3459 3460
			if (fp->Rflags & FARP_REQUEST_FARPR)
				lpfc_issue_els_farpr(vport, did, 0);
已提交
3461 3462
		}
	}
3463
	return 0;
已提交
3464 3465 3466
}

static int
J
James Smart 已提交
3467 3468
lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		   struct lpfc_nodelist  *ndlp)
已提交
3469 3470 3471 3472 3473
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	IOCB_t *icmd;
	uint32_t cmd, did;
J
James Smart 已提交
3474
	struct lpfc_hba *phba = vport->phba;
已提交
3475 3476 3477 3478 3479 3480 3481 3482

	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> */
3483
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
3484 3485
			"%d (%d):0600 FARP-RSP received from DID x%x\n",
			phba->brd_no, vport->vpi, did);
已提交
3486
	/* ACCEPT the Farp resp request */
J
James Smart 已提交
3487
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
已提交
3488 3489 3490 3491 3492

	return 0;
}

static int
J
James Smart 已提交
3493 3494
lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		 struct lpfc_nodelist *fan_ndlp)
已提交
3495 3496 3497 3498 3499
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	IOCB_t *icmd;
	uint32_t cmd, did;
3500 3501
	FAN *fp;
	struct lpfc_nodelist *ndlp, *next_ndlp;
J
James Smart 已提交
3502
	struct lpfc_hba *phba = vport->phba;
3503 3504

	/* FAN received */
3505
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
3506 3507
			"%d (%d):0265 FAN received\n",
			phba->brd_no, vport->vpi);
已提交
3508 3509 3510

	icmd = &cmdiocb->iocb;
	did = icmd->un.elsreq64.remoteID;
3511 3512
	pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
	lp = (uint32_t *)pcmd->virt;
已提交
3513 3514

	cmd = *lp++;
3515
	fp = (FAN *) lp;
已提交
3516

3517
	/* FAN received; Fan does not have a reply sequence */
已提交
3518

J
James Smart 已提交
3519
	if (phba->pport->port_state == LPFC_LOCAL_CFG_LINK) {
3520 3521 3522 3523 3524 3525 3526
		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
已提交
3527
			 */
3528 3529

			list_for_each_entry_safe(ndlp, next_ndlp,
J
James Smart 已提交
3530
						 &vport->fc_nodes, nlp_listp) {
3531 3532
				if (ndlp->nlp_state != NLP_STE_NPR_NODE)
					continue;
3533 3534 3535 3536 3537
				if (ndlp->nlp_type & NLP_FABRIC) {
					/*
					 * Clean up old Fabric, Nameserver and
					 * other NLP_FABRIC logins
					 */
J
James Smart 已提交
3538
					lpfc_drop_node(vport, ndlp);
3539
				} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
3540 3541 3542
					/* Fail outstanding I/O now since this
					 * device is marked for PLOGI
					 */
J
James Smart 已提交
3543
					lpfc_unreg_rpi(vport, ndlp);
3544 3545 3546
				}
			}

J
James Smart 已提交
3547 3548 3549
			vport->port_state = LPFC_FLOGI;
			lpfc_set_disctmo(vport);
			lpfc_initial_flogi(vport);
3550
			return 0;
已提交
3551
		}
3552 3553 3554
		/* Discovery not needed,
		 * move the nodes to their original state.
		 */
J
James Smart 已提交
3555
		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
3556 3557 3558
					 nlp_listp) {
			if (ndlp->nlp_state != NLP_STE_NPR_NODE)
				continue;
已提交
3559

3560 3561 3562
			switch (ndlp->nlp_prev_state) {
			case NLP_STE_UNMAPPED_NODE:
				ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
3563
				lpfc_nlp_set_state(vport, ndlp,
3564
						   NLP_STE_UNMAPPED_NODE);
3565 3566 3567 3568
				break;

			case NLP_STE_MAPPED_NODE:
				ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
3569
				lpfc_nlp_set_state(vport, ndlp,
3570
						   NLP_STE_MAPPED_NODE);
3571 3572 3573 3574 3575 3576 3577 3578
				break;

			default:
				break;
			}
		}

		/* Start discovery - this should just do CLEAR_LA */
J
James Smart 已提交
3579
		lpfc_disc_start(vport);
已提交
3580
	}
3581
	return 0;
已提交
3582 3583 3584 3585 3586
}

void
lpfc_els_timeout(unsigned long ptr)
{
J
James Smart 已提交
3587 3588
	struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
	struct lpfc_hba   *phba = vport->phba;
已提交
3589 3590
	unsigned long iflag;

J
James Smart 已提交
3591 3592 3593
	spin_lock_irqsave(&vport->work_port_lock, iflag);
	if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
		vport->work_port_events |= WORKER_ELS_TMO;
3594 3595 3596
		spin_unlock_irqrestore(&vport->work_port_lock, iflag);

		spin_lock_irqsave(&phba->hbalock, iflag);
已提交
3597
		if (phba->work_wait)
3598 3599
			lpfc_worker_wake_up(phba);
		spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
3600
	}
3601 3602
	else
		spin_unlock_irqrestore(&vport->work_port_lock, iflag);
已提交
3603 3604 3605 3606
	return;
}

void
J
James Smart 已提交
3607
lpfc_els_timeout_handler(struct lpfc_vport *vport)
已提交
3608
{
J
James Smart 已提交
3609
	struct lpfc_hba  *phba = vport->phba;
已提交
3610 3611 3612 3613
	struct lpfc_sli_ring *pring;
	struct lpfc_iocbq *tmp_iocb, *piocb;
	IOCB_t *cmd = NULL;
	struct lpfc_dmabuf *pcmd;
J
James Smart 已提交
3614
	uint32_t els_command = 0;
已提交
3615
	uint32_t timeout;
J
James Smart 已提交
3616
	uint32_t remote_ID = 0xffffffff;
已提交
3617 3618

	/* If the timer is already canceled do nothing */
J
James Smart 已提交
3619
	if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
已提交
3620 3621
		return;
	}
J
James Smart 已提交
3622
	spin_lock_irq(&phba->hbalock);
已提交
3623 3624 3625 3626 3627 3628 3629
	timeout = (uint32_t)(phba->fc_ratov << 1);

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

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

J
James Smart 已提交
3630 3631 3632
		if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
		    piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
		    piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
已提交
3633
			continue;
J
James Smart 已提交
3634 3635 3636 3637

		if (piocb->vport != vport)
			continue;

已提交
3638
		pcmd = (struct lpfc_dmabuf *) piocb->context2;
J
James Smart 已提交
3639 3640
		if (pcmd)
			els_command = *(uint32_t *) (pcmd->virt);
已提交
3641

3642 3643 3644 3645 3646 3647
		if (els_command == ELS_CMD_FARP ||
		    els_command == ELS_CMD_FARPR ||
		    els_command == ELS_CMD_FDISC)
			continue;

		if (vport != piocb->vport)
已提交
3648 3649 3650
			continue;

		if (piocb->drvrTimeout > 0) {
3651
			if (piocb->drvrTimeout >= timeout)
已提交
3652
				piocb->drvrTimeout -= timeout;
3653
			else
已提交
3654 3655 3656 3657
				piocb->drvrTimeout = 0;
			continue;
		}

J
James Smart 已提交
3658 3659
		remote_ID = 0xffffffff;
		if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
已提交
3660
			remote_ID = cmd->un.elsreq64.remoteID;
J
James Smart 已提交
3661 3662 3663 3664 3665
		else {
			struct lpfc_nodelist *ndlp;
			ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
			if (ndlp)
				remote_ID = ndlp->nlp_DID;
已提交
3666 3667
		}

3668 3669 3670 3671
		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
				"%d (%d):0127 ELS timeout Data: x%x x%x x%x "
				"x%x\n",
				phba->brd_no, vport->vpi, els_command,
已提交
3672 3673
				remote_ID, cmd->ulpCommand, cmd->ulpIoTag);

3674
		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
已提交
3675
	}
J
James Smart 已提交
3676
	spin_unlock_irq(&phba->hbalock);
3677

J
James Smart 已提交
3678 3679
	if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
		mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
已提交
3680 3681 3682
}

void
J
James Smart 已提交
3683
lpfc_els_flush_cmd(struct lpfc_vport *vport)
已提交
3684
{
3685
	LIST_HEAD(completions);
J
James Smart 已提交
3686
	struct lpfc_hba  *phba = vport->phba;
3687
	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
已提交
3688 3689
	struct lpfc_iocbq *tmp_iocb, *piocb;
	IOCB_t *cmd = NULL;
3690 3691

	lpfc_fabric_abort_vport(vport);
已提交
3692

J
James Smart 已提交
3693
	spin_lock_irq(&phba->hbalock);
已提交
3694 3695 3696 3697 3698 3699 3700 3701
	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 */
3702 3703 3704 3705
		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)
已提交
3706 3707
			continue;

J
James Smart 已提交
3708 3709 3710
		if (piocb->vport != vport)
			continue;

3711
		list_move_tail(&piocb->list, &completions);
3712
		pring->txq_cnt--;
已提交
3713 3714 3715 3716 3717 3718 3719
	}

	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
		if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
			continue;
		}

J
James Smart 已提交
3720 3721 3722
		if (piocb->vport != vport)
			continue;

3723
		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
已提交
3724
	}
J
James Smart 已提交
3725
	spin_unlock_irq(&phba->hbalock);
3726

J
James Smart 已提交
3727
	while (!list_empty(&completions)) {
3728 3729
		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
		cmd = &piocb->iocb;
3730
		list_del_init(&piocb->list);
3731

J
James Smart 已提交
3732 3733 3734
		if (!piocb->iocb_cmpl)
			lpfc_sli_release_iocbq(phba, piocb);
		else {
3735 3736 3737
			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
			(piocb->iocb_cmpl) (phba, piocb, piocb);
J
James Smart 已提交
3738
		}
3739 3740
	}

已提交
3741 3742 3743
	return;
}

3744 3745
static void
lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3746
		      struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
已提交
3747 3748 3749
{
	struct lpfc_nodelist *ndlp;
	struct ls_rjt stat;
3750
	uint32_t *payload;
J
James Smart 已提交
3751
	uint32_t cmd, did, newnode, rjt_err = 0;
3752
	IOCB_t *icmd = &elsiocb->iocb;
已提交
3753

3754
	if (vport == NULL || elsiocb->context2 == NULL)
已提交
3755
		goto dropit;
J
James Smart 已提交
3756

已提交
3757
	newnode = 0;
3758 3759
	payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
	cmd = *payload;
3760 3761
	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
		lpfc_post_buffer(phba, pring, 1, 1);
已提交
3762

J
James Smart 已提交
3763 3764 3765 3766 3767
	did = icmd->un.rcvels.remoteID;
	if (icmd->ulpStatus) {
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV Unsol ELS:  status:x%x/x%x did:x%x",
			icmd->ulpStatus, icmd->un.ulpWord[4], did);
已提交
3768
		goto dropit;
J
James Smart 已提交
3769
	}
已提交
3770 3771

	/* Check to see if link went down during discovery */
3772
	if (lpfc_els_chk_latt(vport))
已提交
3773 3774
		goto dropit;

3775 3776 3777 3778
	/* Ignore traffic recevied during vport shutdown. */
	if (vport->load_flag & FC_UNLOADING)
		goto dropit;

J
James Smart 已提交
3779
	ndlp = lpfc_findnode_did(vport, did);
3780
	if (!ndlp) {
已提交
3781
		/* Cannot find existing Fabric ndlp, so allocate a new one */
3782
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3783
		if (!ndlp)
已提交
3784 3785
			goto dropit;

J
James Smart 已提交
3786
		lpfc_nlp_init(vport, ndlp, did);
已提交
3787 3788 3789 3790
		newnode = 1;
		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK) {
			ndlp->nlp_type |= NLP_FABRIC;
		}
J
James Smart 已提交
3791
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
已提交
3792 3793 3794
	}

	phba->fc_stat.elsRcvFrame++;
3795 3796 3797
	if (elsiocb->context1)
		lpfc_nlp_put(elsiocb->context1);
	elsiocb->context1 = lpfc_nlp_get(ndlp);
J
James Smart 已提交
3798
	elsiocb->vport = vport;
已提交
3799 3800 3801 3802 3803 3804

	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,
3805 3806
			"%d (%d):0112 ELS command x%x received from NPORT x%x "
			"Data: x%x\n", phba->brd_no, vport->vpi, cmd, did,
3807
			vport->port_state);
已提交
3808 3809 3810

	switch (cmd) {
	case ELS_CMD_PLOGI:
J
James Smart 已提交
3811 3812 3813 3814
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

已提交
3815
		phba->fc_stat.elsRcvPLOGI++;
J
James Smart 已提交
3816 3817 3818 3819
		ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);

		if (vport->port_state < LPFC_DISC_AUTH) {
			rjt_err = LSRJT_UNABLE_TPC;
已提交
3820 3821
			break;
		}
J
James Smart 已提交
3822 3823
		lpfc_disc_state_machine(vport, ndlp, elsiocb,
					NLP_EVT_RCV_PLOGI);
J
James Smart 已提交
3824

已提交
3825 3826
		break;
	case ELS_CMD_FLOGI:
J
James Smart 已提交
3827 3828 3829 3830
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

已提交
3831
		phba->fc_stat.elsRcvFLOGI++;
J
James Smart 已提交
3832
		lpfc_els_rcv_flogi(vport, elsiocb, ndlp, newnode);
3833
		if (newnode)
J
James Smart 已提交
3834
			lpfc_drop_node(vport, ndlp);
已提交
3835 3836
		break;
	case ELS_CMD_LOGO:
J
James Smart 已提交
3837 3838 3839 3840
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV LOGO:        did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

已提交
3841
		phba->fc_stat.elsRcvLOGO++;
J
James Smart 已提交
3842
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
3843
			rjt_err = LSRJT_UNABLE_TPC;
已提交
3844 3845
			break;
		}
J
James Smart 已提交
3846
		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
已提交
3847 3848
		break;
	case ELS_CMD_PRLO:
J
James Smart 已提交
3849 3850 3851 3852
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV PRLO:        did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

已提交
3853
		phba->fc_stat.elsRcvPRLO++;
J
James Smart 已提交
3854
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
3855
			rjt_err = LSRJT_UNABLE_TPC;
已提交
3856 3857
			break;
		}
J
James Smart 已提交
3858
		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
已提交
3859 3860 3861
		break;
	case ELS_CMD_RSCN:
		phba->fc_stat.elsRcvRSCN++;
J
James Smart 已提交
3862
		lpfc_els_rcv_rscn(vport, elsiocb, ndlp, newnode);
3863
		if (newnode)
J
James Smart 已提交
3864
			lpfc_drop_node(vport, ndlp);
已提交
3865 3866
		break;
	case ELS_CMD_ADISC:
J
James Smart 已提交
3867 3868 3869 3870
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV ADISC:       did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

已提交
3871
		phba->fc_stat.elsRcvADISC++;
J
James Smart 已提交
3872
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
3873
			rjt_err = LSRJT_UNABLE_TPC;
已提交
3874 3875
			break;
		}
J
James Smart 已提交
3876 3877
		lpfc_disc_state_machine(vport, ndlp, elsiocb,
					NLP_EVT_RCV_ADISC);
已提交
3878 3879
		break;
	case ELS_CMD_PDISC:
J
James Smart 已提交
3880 3881 3882 3883
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV PDISC:       did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

已提交
3884
		phba->fc_stat.elsRcvPDISC++;
J
James Smart 已提交
3885
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
3886
			rjt_err = LSRJT_UNABLE_TPC;
已提交
3887 3888
			break;
		}
J
James Smart 已提交
3889 3890
		lpfc_disc_state_machine(vport, ndlp, elsiocb,
					NLP_EVT_RCV_PDISC);
已提交
3891 3892
		break;
	case ELS_CMD_FARPR:
J
James Smart 已提交
3893 3894 3895 3896
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV FARPR:       did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

已提交
3897
		phba->fc_stat.elsRcvFARPR++;
J
James Smart 已提交
3898
		lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
已提交
3899 3900
		break;
	case ELS_CMD_FARP:
J
James Smart 已提交
3901 3902 3903 3904
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV FARP:        did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

已提交
3905
		phba->fc_stat.elsRcvFARP++;
J
James Smart 已提交
3906
		lpfc_els_rcv_farp(vport, elsiocb, ndlp);
已提交
3907 3908
		break;
	case ELS_CMD_FAN:
J
James Smart 已提交
3909 3910 3911 3912
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV FAN:         did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

已提交
3913
		phba->fc_stat.elsRcvFAN++;
J
James Smart 已提交
3914
		lpfc_els_rcv_fan(vport, elsiocb, ndlp);
已提交
3915 3916
		break;
	case ELS_CMD_PRLI:
J
James Smart 已提交
3917 3918 3919 3920
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV PRLI:        did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

已提交
3921
		phba->fc_stat.elsRcvPRLI++;
J
James Smart 已提交
3922
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
3923
			rjt_err = LSRJT_UNABLE_TPC;
已提交
3924 3925
			break;
		}
J
James Smart 已提交
3926
		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
已提交
3927
		break;
3928
	case ELS_CMD_LIRR:
J
James Smart 已提交
3929 3930 3931 3932
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV LIRR:        did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

3933
		phba->fc_stat.elsRcvLIRR++;
J
James Smart 已提交
3934
		lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
3935
		if (newnode)
J
James Smart 已提交
3936
			lpfc_drop_node(vport, ndlp);
3937 3938
		break;
	case ELS_CMD_RPS:
J
James Smart 已提交
3939 3940 3941 3942
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV RPS:         did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

3943
		phba->fc_stat.elsRcvRPS++;
J
James Smart 已提交
3944
		lpfc_els_rcv_rps(vport, elsiocb, ndlp);
3945
		if (newnode)
J
James Smart 已提交
3946
			lpfc_drop_node(vport, ndlp);
3947 3948
		break;
	case ELS_CMD_RPL:
J
James Smart 已提交
3949 3950 3951 3952
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV RPL:         did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

3953
		phba->fc_stat.elsRcvRPL++;
J
James Smart 已提交
3954
		lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
3955
		if (newnode)
J
James Smart 已提交
3956
			lpfc_drop_node(vport, ndlp);
3957
		break;
已提交
3958
	case ELS_CMD_RNID:
J
James Smart 已提交
3959 3960 3961 3962
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV RNID:        did:x%x/ste:x%x flg:x%x",
			did, vport->port_state, ndlp->nlp_flag);

已提交
3963
		phba->fc_stat.elsRcvRNID++;
J
James Smart 已提交
3964
		lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
3965
		if (newnode)
J
James Smart 已提交
3966
			lpfc_drop_node(vport, ndlp);
已提交
3967 3968
		break;
	default:
J
James Smart 已提交
3969 3970 3971 3972
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
			"RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
			cmd, did, vport->port_state);

已提交
3973
		/* Unsupported ELS command, reject */
J
James Smart 已提交
3974
		rjt_err = LSRJT_INVALID_CMD;
已提交
3975 3976 3977

		/* Unknown ELS command <elsCmd> received from NPORT <did> */
		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
3978
				"%d (%d):0115 Unknown ELS command x%x "
3979
				"received from NPORT x%x\n",
3980
				phba->brd_no, vport->vpi, cmd, did);
3981
		if (newnode)
J
James Smart 已提交
3982
			lpfc_drop_node(vport, ndlp);
已提交
3983 3984 3985 3986 3987
		break;
	}

	/* check if need to LS_RJT received ELS cmd */
	if (rjt_err) {
3988
		memset(&stat, 0, sizeof(stat));
J
James Smart 已提交
3989
		stat.un.b.lsRjtRsnCode = rjt_err;
3990
		stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
J
James Smart 已提交
3991 3992
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
			NULL);
3993 3994
		if (newnode)
			lpfc_drop_node(vport, ndlp);
已提交
3995 3996
	}

3997 3998 3999 4000
	return;

dropit:
	lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
4001
			"%d (%d):0111 Dropping received ELS cmd "
4002
			"Data: x%x x%x x%x\n",
4003
			phba->brd_no, vport ? vport->vpi : 0xffff,
4004 4005 4006 4007 4008
			icmd->ulpStatus, icmd->un.ulpWord[4],
			icmd->ulpTimeout);
	phba->fc_stat.elsRcvDrop++;
}

4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019
static struct lpfc_vport *
lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
{
	struct lpfc_vport *vport;

	list_for_each_entry(vport, &phba->port_list, listentry) {
		if (vport->vpi == vpi)
			return vport;
	}
	return NULL;
}
4020 4021 4022 4023 4024 4025 4026 4027

void
lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
		     struct lpfc_iocbq *elsiocb)
{
	struct lpfc_vport *vport = phba->pport;
	IOCB_t *icmd = &elsiocb->iocb;
	dma_addr_t paddr;
4028 4029 4030 4031 4032
	struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
	struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;

	elsiocb->context2 = NULL;
	elsiocb->context3 = NULL;
4033

4034 4035 4036 4037
	if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
		lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
	} else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
	    (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
4038 4039
		phba->fc_stat.NoRcvBuf++;
		/* Not enough posted buffers; Try posting more buffers */
4040
		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
4041 4042 4043 4044
			lpfc_post_buffer(phba, pring, 0, 1);
		return;
	}

4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057
	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
	    (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
	     icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
		if (icmd->unsli3.rcvsli3.vpi == 0xffff)
			vport = phba->pport;
		else {
			uint16_t vpi = icmd->unsli3.rcvsli3.vpi;
			vport = lpfc_find_vport_by_vpid(phba, vpi);
		}
	}
				/* If there are no BDEs associated
				 * with this IOCB, there is nothing to do.
				 */
4058 4059 4060
	if (icmd->ulpBdeCount == 0)
		return;

4061 4062 4063
				/* type of ELS cmd is first 32bit word
				 * in packet
				 */
4064
	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4065
		elsiocb->context2 = bdeBuf1;
4066 4067 4068
	} else {
		paddr = getPaddr(icmd->un.cont64[0].addrHigh,
				 icmd->un.cont64[0].addrLow);
4069 4070
		elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
							     paddr);
4071 4072
	}

4073 4074 4075 4076 4077
	lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
	/*
	 * The different unsolicited event handlers would tell us
	 * if they are done with "mp" by setting context2 to NULL.
	 */
4078 4079
	lpfc_nlp_put(elsiocb->context1);
	elsiocb->context1 = NULL;
已提交
4080
	if (elsiocb->context2) {
4081 4082
		lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
		elsiocb->context2 = NULL;
已提交
4083
	}
4084 4085

	/* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
4086
	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
4087
	    icmd->ulpBdeCount == 2) {
4088 4089
		elsiocb->context2 = bdeBuf2;
		lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
4090 4091
		/* free mp if we are done with it */
		if (elsiocb->context2) {
4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250
			lpfc_in_buf_free(phba, elsiocb->context2);
			elsiocb->context2 = NULL;
		}
	}
}

void
lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
{
	struct lpfc_nodelist *ndlp, *ndlp_fdmi;

	ndlp = lpfc_findnode_did(vport, NameServer_DID);
	if (!ndlp) {
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp) {
			if (phba->fc_topology == TOPOLOGY_LOOP) {
				lpfc_disc_start(vport);
				return;
			}
			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
			lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
				"%d (%d):0251 NameServer login: no memory\n",
				phba->brd_no, vport->vpi);
			return;
		}
		lpfc_nlp_init(vport, ndlp, NameServer_DID);
		ndlp->nlp_type |= NLP_FABRIC;
	}

	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);

	if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
			"%d (%d):0252 Cannot issue NameServer login\n",
			phba->brd_no, vport->vpi);
		return;
	}

	if (phba->cfg_fdmi_on) {
		ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
					  GFP_KERNEL);
		if (ndlp_fdmi) {
			lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
			ndlp_fdmi->nlp_type |= NLP_FABRIC;
			ndlp_fdmi->nlp_state =
				NLP_STE_PLOGI_ISSUE;
			lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID,
					     0);
		}
	}
	return;
}

static void
lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
{
	struct lpfc_vport *vport = pmb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
	MAILBOX_t *mb = &pmb->mb;

	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
	lpfc_nlp_put(ndlp);

	if (mb->mbxStatus) {
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
				"%d (%d):0915 Register VPI failed: 0x%x\n",
				phba->brd_no, vport->vpi, mb->mbxStatus);

		switch (mb->mbxStatus) {
		case 0x11:	/* unsupported feature */
		case 0x9603:	/* max_vpi exceeded */
			/* giving up on vport registration */
			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
			spin_lock_irq(shost->host_lock);
			vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
			spin_unlock_irq(shost->host_lock);
			lpfc_can_disctmo(vport);
			break;
		default:
			/* Try to recover from this error */
			lpfc_mbx_unreg_vpi(vport);
			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
			lpfc_initial_fdisc(vport);
			break;
		}

	} else {
		if (vport == phba->pport)
			lpfc_issue_fabric_reglogin(vport);
		else
			lpfc_do_scr_ns_plogi(phba, vport);
	}
	mempool_free(pmb, phba->mbox_mem_pool);
	return;
}

void
lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
			struct lpfc_nodelist *ndlp)
{
	LPFC_MBOXQ_t *mbox;

	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (mbox) {
		lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, mbox);
		mbox->vport = vport;
		mbox->context2 = lpfc_nlp_get(ndlp);
		mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
		if (lpfc_sli_issue_mbox(phba, mbox,
					MBX_NOWAIT | MBX_STOP_IOCB)
		    == MBX_NOT_FINISHED) {
			mempool_free(mbox, phba->mbox_mem_pool);
			vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;

			lpfc_vport_set_state(vport, FC_VPORT_FAILED);

			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
				"%d (%d):0253 Register VPI: Cannot send mbox\n",
				phba->brd_no, vport->vpi);
		}
	} else {
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);

		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
			"%d (%d):0254 Register VPI: no memory\n",
			phba->brd_no, vport->vpi);

		vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
		lpfc_nlp_put(ndlp);
	}
}

static void
lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		    struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	struct lpfc_nodelist *np;
	struct lpfc_nodelist *next_np;
	IOCB_t *irsp = &rspiocb->iocb;
	struct lpfc_iocbq *piocb;

	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
		"%d (%d):0123 FDISC completes. x%x/x%x prevDID: x%x\n",
		phba->brd_no, vport->vpi,
		irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);

	/* Since all FDISCs are being single threaded, we
	 * must reset the discovery timer for ALL vports
	 * waiting to send FDISC when one completes.
	 */
	list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
		lpfc_set_disctmo(piocb->vport);
	}

J
James Smart 已提交
4251 4252 4253 4254
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"FDISC cmpl:      status:x%x/x%x prevdid:x%x",
		irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);

4255 4256 4257 4258 4259 4260 4261 4262 4263 4264
	if (irsp->ulpStatus) {
		/* Check for retry */
		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
			goto out;

		/* FDISC failed */
		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
			"%d (%d):0124 FDISC failed. (%d/%d)\n",
			phba->brd_no, vport->vpi,
			irsp->ulpStatus, irsp->un.ulpWord[4]);
J
James Smart 已提交
4265

4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371
		if (vport->fc_vport->vport_state == FC_VPORT_INITIALIZING)
			lpfc_vport_set_state(vport, FC_VPORT_FAILED);

		lpfc_nlp_put(ndlp);
		/* giving up on FDISC. Cancel discovery timer */
		lpfc_can_disctmo(vport);
	} else {
		spin_lock_irq(shost->host_lock);
		vport->fc_flag |= FC_FABRIC;
		if (vport->phba->fc_topology == TOPOLOGY_LOOP)
			vport->fc_flag |=  FC_PUBLIC_LOOP;
		spin_unlock_irq(shost->host_lock);

		vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
		lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
		if ((vport->fc_prevDID != vport->fc_myDID) &&
			!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
			/* If our NportID changed, we need to ensure all
			 * remaining NPORTs get unreg_login'ed so we can
			 * issue unreg_vpi.
			 */
			list_for_each_entry_safe(np, next_np,
				&vport->fc_nodes, nlp_listp) {
				if (np->nlp_state != NLP_STE_NPR_NODE
				   || !(np->nlp_flag & NLP_NPR_ADISC))
					continue;
				spin_lock_irq(shost->host_lock);
				np->nlp_flag &= ~NLP_NPR_ADISC;
				spin_unlock_irq(shost->host_lock);
				lpfc_unreg_rpi(vport, np);
			}
			lpfc_mbx_unreg_vpi(vport);
			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
		}

		if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
			lpfc_register_new_vport(phba, vport, ndlp);
		else
			lpfc_do_scr_ns_plogi(phba, vport);

		lpfc_nlp_put(ndlp); /* Free Fabric ndlp for vports */
	}

out:
	lpfc_els_free_iocb(phba, cmdiocb);
}

int
lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
		     uint8_t retry)
{
	struct lpfc_hba *phba = vport->phba;
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	struct serv_parm *sp;
	uint8_t *pcmd;
	uint16_t cmdsize;
	int did = ndlp->nlp_DID;
	int rc;

	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
				     ELS_CMD_FDISC);
	if (!elsiocb) {
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);

		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
			"%d (%d):0255 Issue FDISC: no IOCB\n",
			phba->brd_no, vport->vpi);
		return 1;
	}

	icmd = &elsiocb->iocb;
	icmd->un.elsreq64.myID = 0;
	icmd->un.elsreq64.fl = 1;

	/* For FDISC, Let FDISC rsp set the NPortID for this VPI */
	icmd->ulpCt_h = 1;
	icmd->ulpCt_l = 0;

	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
	pcmd += sizeof(uint32_t); /* CSP Word 1 */
	memcpy(pcmd, &vport->phba->pport->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;

	pcmd += sizeof(uint32_t); /* CSP Word 2 */
	pcmd += sizeof(uint32_t); /* CSP Word 3 */
	pcmd += sizeof(uint32_t); /* CSP Word 4 */
	pcmd += sizeof(uint32_t); /* Port Name */
	memcpy(pcmd, &vport->fc_portname, 8);
	pcmd += sizeof(uint32_t); /* Node Name */
	pcmd += sizeof(uint32_t); /* Node Name */
	memcpy(pcmd, &vport->fc_nodename, 8);

	lpfc_set_disctmo(vport);

	phba->fc_stat.elsXmitFDISC++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;

J
James Smart 已提交
4372 4373 4374 4375
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue FDISC:     did:x%x",
		did, 0, 0);

4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396
	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);

		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
			"%d (%d):0256 Issue FDISC: Cannot send IOCB\n",
			phba->brd_no, vport->vpi);

		return 1;
	}
	lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
	vport->port_state = LPFC_FDISC;
	return 0;
}

static void
lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
J
James Smart 已提交
4397 4398 4399 4400 4401 4402
	IOCB_t *irsp;

	irsp = &rspiocb->iocb;
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"LOGO npiv cmpl:  status:x%x/x%x did:x%x",
		irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434

	lpfc_els_free_iocb(phba, cmdiocb);
	vport->unreg_vpi_cmpl = VPORT_ERROR;
}

int
lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
{
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	uint8_t *pcmd;
	uint16_t cmdsize;

	cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
				     ELS_CMD_LOGO);
	if (!elsiocb)
		return 1;

	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(vport->fc_myDID);
	pcmd += sizeof(uint32_t);
	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));

J
James Smart 已提交
4435 4436 4437 4438
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue LOGO npiv  did:x%x flg:x%x",
		ndlp->nlp_DID, ndlp->nlp_flag, 0);

4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497
	elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
	spin_lock_irq(shost->host_lock);
	ndlp->nlp_flag |= NLP_LOGO_SND;
	spin_unlock_irq(shost->host_lock);
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
		spin_lock_irq(shost->host_lock);
		ndlp->nlp_flag &= ~NLP_LOGO_SND;
		spin_unlock_irq(shost->host_lock);
		lpfc_els_free_iocb(phba, elsiocb);
		return 1;
	}
	return 0;
}

void
lpfc_fabric_block_timeout(unsigned long ptr)
{
	struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
	unsigned long iflags;
	uint32_t tmo_posted;
	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
	tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
	if (!tmo_posted)
		phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);

	if (!tmo_posted) {
		spin_lock_irqsave(&phba->hbalock, iflags);
		if (phba->work_wait)
			lpfc_worker_wake_up(phba);
		spin_unlock_irqrestore(&phba->hbalock, iflags);
	}
}

static void
lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
{
	struct lpfc_iocbq *iocb;
	unsigned long iflags;
	int ret;
	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
	IOCB_t *cmd;

repeat:
	iocb = NULL;
	spin_lock_irqsave(&phba->hbalock, iflags);
				/* Post any pending iocb to the SLI layer */
	if (atomic_read(&phba->fabric_iocb_count) == 0) {
		list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
				 list);
		if (iocb)
			atomic_inc(&phba->fabric_iocb_count);
	}
	spin_unlock_irqrestore(&phba->hbalock, iflags);
	if (iocb) {
		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
		iocb->iocb_flag |= LPFC_IO_FABRIC;

J
James Smart 已提交
4498 4499 4500 4501
		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
			"Fabric sched1:   ste:x%x",
			iocb->vport->port_state, 0, 0);

4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558
		ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);

		if (ret == IOCB_ERROR) {
			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
			iocb->fabric_iocb_cmpl = NULL;
			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
			cmd = &iocb->iocb;
			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
			iocb->iocb_cmpl(phba, iocb, iocb);

			atomic_dec(&phba->fabric_iocb_count);
			goto repeat;
		}
	}

	return;
}

void
lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
{
	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);

	lpfc_resume_fabric_iocbs(phba);
	return;
}

static void
lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
{
	int blocked;

	blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
				/* Start a timer to unblock fabric
				 * iocbs after 100ms
				 */
	if (!blocked)
		mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );

	return;
}

static void
lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
	struct lpfc_iocbq *rspiocb)
{
	struct ls_rjt stat;

	if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
		BUG();

	switch (rspiocb->iocb.ulpStatus) {
		case IOSTAT_NPORT_RJT:
		case IOSTAT_FABRIC_RJT:
			if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
				lpfc_block_fabric_iocbs(phba);
4559
			}
4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611
			break;

		case IOSTAT_NPORT_BSY:
		case IOSTAT_FABRIC_BSY:
			lpfc_block_fabric_iocbs(phba);
			break;

		case IOSTAT_LS_RJT:
			stat.un.lsRjtError =
				be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
			if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
				(stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
				lpfc_block_fabric_iocbs(phba);
			break;
	}

	if (atomic_read(&phba->fabric_iocb_count) == 0)
		BUG();

	cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
	cmdiocb->fabric_iocb_cmpl = NULL;
	cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
	cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);

	atomic_dec(&phba->fabric_iocb_count);
	if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
				/* Post any pending iocbs to HBA */
		    lpfc_resume_fabric_iocbs(phba);
	}
}

int
lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
{
	unsigned long iflags;
	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
	int ready;
	int ret;

	if (atomic_read(&phba->fabric_iocb_count) > 1)
		BUG();

	spin_lock_irqsave(&phba->hbalock, iflags);
	ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
		!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);

	spin_unlock_irqrestore(&phba->hbalock, iflags);
	if (ready) {
		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
		iocb->iocb_flag |= LPFC_IO_FABRIC;

J
James Smart 已提交
4612 4613 4614 4615
		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
			"Fabric sched2:   ste:x%x",
			iocb->vport->port_state, 0, 0);

4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677
		atomic_inc(&phba->fabric_iocb_count);
		ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);

		if (ret == IOCB_ERROR) {
			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
			iocb->fabric_iocb_cmpl = NULL;
			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
			atomic_dec(&phba->fabric_iocb_count);
		}
	} else {
		spin_lock_irqsave(&phba->hbalock, iflags);
		list_add_tail(&iocb->list, &phba->fabric_iocb_list);
		spin_unlock_irqrestore(&phba->hbalock, iflags);
		ret = IOCB_SUCCESS;
	}
	return ret;
}


void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
{
	LIST_HEAD(completions);
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_iocbq *tmp_iocb, *piocb;
	IOCB_t *cmd;

	spin_lock_irq(&phba->hbalock);
	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
				 list) {

		if (piocb->vport != vport)
			continue;

		list_move_tail(&piocb->list, &completions);
	}
	spin_unlock_irq(&phba->hbalock);

	while (!list_empty(&completions)) {
		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
		list_del_init(&piocb->list);

		cmd = &piocb->iocb;
		cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
		cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
		(piocb->iocb_cmpl) (phba, piocb, piocb);
	}
}

void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
{
	LIST_HEAD(completions);
	struct lpfc_hba  *phba = ndlp->vport->phba;
	struct lpfc_iocbq *tmp_iocb, *piocb;
	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
	IOCB_t *cmd;

	spin_lock_irq(&phba->hbalock);
	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
				 list) {
		if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {

			list_move_tail(&piocb->list, &completions);
4678
		}
已提交
4679
	}
4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711
	spin_unlock_irq(&phba->hbalock);

	while (!list_empty(&completions)) {
		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
		list_del_init(&piocb->list);

		cmd = &piocb->iocb;
		cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
		cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
		(piocb->iocb_cmpl) (phba, piocb, piocb);
	}
}

void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
{
	LIST_HEAD(completions);
	struct lpfc_iocbq *piocb;
	IOCB_t *cmd;

	spin_lock_irq(&phba->hbalock);
	list_splice_init(&phba->fabric_iocb_list, &completions);
	spin_unlock_irq(&phba->hbalock);

	while (!list_empty(&completions)) {
		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
		list_del_init(&piocb->list);

		cmd = &piocb->iocb;
		cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
		cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
		(piocb->iocb_cmpl) (phba, piocb, piocb);
	}
已提交
4712
}
4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746


void lpfc_fabric_abort_flogi(struct lpfc_hba *phba)
{
	LIST_HEAD(completions);
	struct lpfc_iocbq *tmp_iocb, *piocb;
	IOCB_t *cmd;
	struct lpfc_nodelist *ndlp;

	spin_lock_irq(&phba->hbalock);
	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
				 list) {

		cmd = &piocb->iocb;
		ndlp = (struct lpfc_nodelist *) piocb->context1;
		if (cmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
		    ndlp != NULL &&
		    ndlp->nlp_DID == Fabric_DID)
			list_move_tail(&piocb->list, &completions);
	}
	spin_unlock_irq(&phba->hbalock);

	while (!list_empty(&completions)) {
		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
		list_del_init(&piocb->list);

		cmd = &piocb->iocb;
		cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
		cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
		(piocb->iocb_cmpl) (phba, piocb, piocb);
	}
}