lpfc_els.c 131.2 KB
Newer Older
已提交
1 2
/*******************************************************************
 * This file is part of the Emulex Linux Device Driver for         *
3
 * Fibre Channel Host Bus Adapters.                                *
4
 * Copyright (C) 2004-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
/* See Fibre Channel protocol T11 FC-LS for details */
已提交
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
static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
			struct lpfc_iocbq *);
A
Adrian Bunk 已提交
45 46 47 48 49 50 51 52
static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
				struct lpfc_nodelist *ndlp, uint8_t retry);
static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
				  struct lpfc_iocbq *iocb);
static void lpfc_register_new_vport(struct lpfc_hba *phba,
				    struct lpfc_vport *vport,
				    struct lpfc_nodelist *ndlp);
53

已提交
54 55
static int lpfc_max_els_tries = 3;

J
James Smart 已提交
56
int
J
James Smart 已提交
57
lpfc_els_chk_latt(struct lpfc_vport *vport)
已提交
58
{
J
James Smart 已提交
59 60
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
61 62
	uint32_t ha_copy;

J
James Smart 已提交
63 64
	if (vport->port_state >= LPFC_VPORT_READY ||
	    phba->link_state == LPFC_LINK_DOWN)
已提交
65 66 67 68 69 70 71 72 73
		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 */
74 75 76 77
	lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
			 "0237 Pending Link Event during "
			 "Discovery: State x%x\n",
			 phba->pport->port_state);
已提交
78 79 80 81 82 83 84

	/* 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 已提交
85 86 87
	spin_lock_irq(shost->host_lock);
	vport->fc_flag |= FC_ABORT_DISCOVERY;
	spin_unlock_irq(shost->host_lock);
已提交
88

89
	if (phba->link_state != LPFC_CLEAR_LA)
90
		lpfc_issue_clear_la(phba, vport);
已提交
91

92
	return 1;
已提交
93 94 95
}

static struct lpfc_iocbq *
J
James Smart 已提交
96 97 98 99
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)
已提交
100
{
J
James Smart 已提交
101
	struct lpfc_hba  *phba = vport->phba;
102
	struct lpfc_iocbq *elsiocb;
已提交
103 104 105 106 107
	struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
	struct ulp_bde64 *bpl;
	IOCB_t *icmd;


J
James Smart 已提交
108 109
	if (!lpfc_is_link_up(phba))
		return NULL;
已提交
110 111

	/* Allocate buffer for  command iocb */
112
	elsiocb = lpfc_sli_get_iocbq(phba);
已提交
113 114 115 116 117 118 119

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

	/* fill in BDEs for command */
	/* Allocate buffer for command payload */
120 121 122 123
	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
	if (pcmd)
		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
	if (!pcmd || !pcmd->virt) {
J
Jesper Juhl 已提交
124
		kfree(pcmd);
已提交
125

126
		lpfc_sli_release_iocbq(phba, elsiocb);
已提交
127 128 129 130 131 132 133
		return NULL;
	}

	INIT_LIST_HEAD(&pcmd->list);

	/* Allocate buffer for response payload */
	if (expectRsp) {
134
		prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
已提交
135 136 137
		if (prsp)
			prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
						     &prsp->phys);
138
		if (!prsp || !prsp->virt) {
J
Jesper Juhl 已提交
139
			kfree(prsp);
已提交
140 141
			lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
			kfree(pcmd);
142
			lpfc_sli_release_iocbq(phba, elsiocb);
已提交
143 144 145 146 147 148 149 150
			return NULL;
		}
		INIT_LIST_HEAD(&prsp->list);
	} else {
		prsp = NULL;
	}

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

183 184 185 186 187 188 189 190 191
	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;
	}

已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	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);
	}

208
	elsiocb->context1 = lpfc_nlp_get(ndlp);
209 210
	elsiocb->context2 = pcmd;
	elsiocb->context3 = pbuflist;
已提交
211
	elsiocb->retry = retry;
J
James Smart 已提交
212
	elsiocb->vport = vport;
已提交
213 214 215 216 217 218 219
	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> */
220 221 222 223 224
		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
				 "0116 Xmit ELS command x%x to remote "
				 "NPORT x%x I/O tag: x%x, port state: x%x\n",
				 elscmd, did, elsiocb->iotag,
				 vport->port_state);
已提交
225 226
	} else {
		/* Xmit ELS response <elsCmd> to remote NPORT <did> */
227 228 229 230 231
		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
				 "0117 Xmit ELS response x%x to remote "
				 "NPORT x%x I/O tag: x%x, size: x%x\n",
				 elscmd, ndlp->nlp_DID, elsiocb->iotag,
				 cmdSize);
已提交
232
	}
233
	return elsiocb;
已提交
234 235 236 237
}


static int
238
lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
已提交
239
{
J
James Smart 已提交
240
	struct lpfc_hba  *phba = vport->phba;
已提交
241
	LPFC_MBOXQ_t *mbox;
242
	struct lpfc_dmabuf *mp;
243 244
	struct lpfc_nodelist *ndlp;
	struct serv_parm *sp;
已提交
245
	int rc;
246
	int err = 0;
已提交
247

248 249
	sp = &phba->fc_fabparam;
	ndlp = lpfc_findnode_did(vport, Fabric_DID);
250 251
	if (!ndlp) {
		err = 1;
252
		goto fail;
253
	}
254 255

	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
256 257
	if (!mbox) {
		err = 2;
258
		goto fail;
259
	}
260 261 262 263 264 265

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

266
	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
267 268
	if (rc == MBX_NOT_FINISHED) {
		err = 3;
269
		goto fail_free_mbox;
270
	}
271 272

	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
273 274
	if (!mbox) {
		err = 4;
275
		goto fail;
276
	}
277 278
	rc = lpfc_reg_login(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
			    0);
279 280
	if (rc) {
		err = 5;
281
		goto fail_free_mbox;
282
	}
283 284 285 286 287

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

288
	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
289 290
	if (rc == MBX_NOT_FINISHED) {
		err = 6;
291
		goto fail_issue_reg_login;
292
	}
293 294 295 296 297 298 299 300 301 302 303 304 305

	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);
306
	lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
307
		"0249 Cannot issue Register Fabric login: Err %d\n", err);
308 309 310 311 312 313 314 315 316 317 318 319
	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 已提交
320 321 322
	spin_lock_irq(shost->host_lock);
	vport->fc_flag |= FC_FABRIC;
	spin_unlock_irq(shost->host_lock);
已提交
323 324 325 326 327 328 329 330

	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 已提交
331 332 333
		spin_lock_irq(shost->host_lock);
		vport->fc_flag |= FC_PUBLIC_LOOP;
		spin_unlock_irq(shost->host_lock);
已提交
334 335 336 337 338
	} 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 已提交
339
		vport->fc_sparam.cmn.altBbCredit = 1;
已提交
340 341
	}

J
James Smart 已提交
342
	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
已提交
343
	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
344
	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
已提交
345 346 347 348 349 350 351 352 353 354 355 356 357
	ndlp->nlp_class_sup = 0;
	if (sp->cls1.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS1;
	if (sp->cls2.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS2;
	if (sp->cls3.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS3;
	if (sp->cls4.classValid)
		ndlp->nlp_class_sup |= FC_COS_CLASS4;
	ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
				sp->cmn.bbRcvSizeLsb;
	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));

358 359
	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
		if (sp->cmn.response_multiple_NPort) {
360 361 362 363 364
			lpfc_printf_vlog(vport, KERN_WARNING,
					 LOG_ELS | LOG_VPORT,
					 "1816 FLOGI NPIV supported, "
					 "response data 0x%x\n",
					 sp->cmn.response_multiple_NPort);
365 366 367
			phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
		} else {
			/* Because we asked f/w for NPIV it still expects us
368 369 370 371 372
			to call reg_vnpid atleast for the physcial host */
			lpfc_printf_vlog(vport, KERN_WARNING,
					 LOG_ELS | LOG_VPORT,
					 "1817 Fabric does not support NPIV "
					 "- configuring single port mode.\n");
373 374 375
			phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
		}
	}
已提交
376

377 378
	if ((vport->fc_prevDID != vport->fc_myDID) &&
		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
已提交
379

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
		/* 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);
395
			spin_lock_irq(shost->host_lock);
396
			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
397
			spin_unlock_irq(shost->host_lock);
398 399
		}
	}
已提交
400

401
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
已提交
402

403 404 405 406 407 408
	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);
已提交
409 410 411 412 413 414 415
	return 0;
}

/*
 * We FLOGIed into an NPort, initiate pt2pt protocol
 */
static int
J
James Smart 已提交
416 417
lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  struct serv_parm *sp)
已提交
418
{
J
James Smart 已提交
419 420
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
421 422 423
	LPFC_MBOXQ_t *mbox;
	int rc;

J
James Smart 已提交
424 425 426
	spin_lock_irq(shost->host_lock);
	vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
	spin_unlock_irq(shost->host_lock);
已提交
427 428 429

	phba->fc_edtov = FF_DEF_EDTOV;
	phba->fc_ratov = FF_DEF_RATOV;
J
James Smart 已提交
430
	rc = memcmp(&vport->fc_portname, &sp->portName,
431
		    sizeof(vport->fc_portname));
已提交
432 433
	if (rc >= 0) {
		/* This side will initiate the PLOGI */
J
James Smart 已提交
434 435 436
		spin_lock_irq(shost->host_lock);
		vport->fc_flag |= FC_PT2PT_PLOGI;
		spin_unlock_irq(shost->host_lock);
已提交
437 438 439 440 441 442 443 444

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

		/* not equal */
		if (rc)
J
James Smart 已提交
445
			vport->fc_myDID = PT2PT_LocalID;
已提交
446 447 448 449 450 451 452 453

		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;
454
		mbox->vport = vport;
455
		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
已提交
456 457 458 459
		if (rc == MBX_NOT_FINISHED) {
			mempool_free(mbox, phba->mbox_mem_pool);
			goto fail;
		}
460
		lpfc_nlp_put(ndlp);
已提交
461

J
James Smart 已提交
462
		ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
已提交
463 464 465 466 467 468 469 470 471
		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 已提交
472
			lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
已提交
473 474 475
		}

		memcpy(&ndlp->nlp_portname, &sp->portName,
J
James Smart 已提交
476
		       sizeof(struct lpfc_name));
已提交
477
		memcpy(&ndlp->nlp_nodename, &sp->nodeName,
J
James Smart 已提交
478 479 480
		       sizeof(struct lpfc_name));
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		spin_lock_irq(shost->host_lock);
已提交
481
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
482
		spin_unlock_irq(shost->host_lock);
已提交
483 484
	} else {
		/* This side will wait for the PLOGI */
485
		lpfc_nlp_put(ndlp);
已提交
486 487
	}

488 489 490
	/* If we are pt2pt with another NPort, force NPIV off! */
	phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;

J
James Smart 已提交
491 492 493
	spin_lock_irq(shost->host_lock);
	vport->fc_flag |= FC_PT2PT;
	spin_unlock_irq(shost->host_lock);
已提交
494 495

	/* Start discovery - this should just do CLEAR_LA */
J
James Smart 已提交
496
	lpfc_disc_start(vport);
已提交
497
	return 0;
498
fail:
已提交
499 500 501 502
	return -ENXIO;
}

static void
503 504
lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		    struct lpfc_iocbq *rspiocb)
已提交
505
{
J
James Smart 已提交
506 507
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
508 509 510 511 512 513 514
	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 已提交
515
	if (lpfc_els_chk_latt(vport)) {
516
		lpfc_nlp_put(ndlp);
已提交
517 518 519
		goto out;
	}

J
James Smart 已提交
520 521 522 523 524
	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);

已提交
525 526
	if (irsp->ulpStatus) {
		/* Check for retry */
J
James Smart 已提交
527
		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
已提交
528
			goto out;
J
James Smart 已提交
529

已提交
530
		/* FLOGI failed, so there is no fabric */
J
James Smart 已提交
531 532 533
		spin_lock_irq(shost->host_lock);
		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
		spin_unlock_irq(shost->host_lock);
已提交
534

535
		/* If private loop, then allow max outstanding els to be
已提交
536 537 538 539
		 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
		 * alpa map would take too long otherwise.
		 */
		if (phba->alpa_map[0] == 0) {
540
			vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
已提交
541 542 543
		}

		/* FLOGI failure */
544 545 546 547 548
		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
				 "0100 FLOGI failure Data: x%x x%x "
				 "x%x\n",
				 irsp->ulpStatus, irsp->un.ulpWord[4],
				 irsp->ulpTimeout);
已提交
549 550 551 552 553 554 555 556 557 558 559 560
		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 */
561 562 563 564 565
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0101 FLOGI completes sucessfully "
			 "Data: x%x x%x x%x x%x\n",
			 irsp->un.ulpWord[4], sp->cmn.e_d_tov,
			 sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
已提交
566

J
James Smart 已提交
567
	if (vport->port_state == LPFC_FLOGI) {
已提交
568 569 570 571 572
		/*
		 * If Common Service Parameters indicate Nport
		 * we are point to point, if Fport we are Fabric.
		 */
		if (sp->cmn.fPort)
J
James Smart 已提交
573
			rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
已提交
574
		else
J
James Smart 已提交
575
			rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
已提交
576 577 578 579 580 581

		if (!rc)
			goto out;
	}

flogifail:
582
	lpfc_nlp_put(ndlp);
已提交
583

J
James Smart 已提交
584
	if (!lpfc_error_lost_link(irsp)) {
已提交
585
		/* FLOGI failed, so just use loop map to make discovery list */
J
James Smart 已提交
586
		lpfc_disc_list_loopmap(vport);
已提交
587 588

		/* Start discovery */
J
James Smart 已提交
589
		lpfc_disc_start(vport);
590 591 592 593 594 595
	} else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
			((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
			(irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
			(phba->link_state != LPFC_CLEAR_LA)) {
		/* If FLOGI failed enable link interrupt. */
		lpfc_issue_clear_la(phba, vport);
已提交
596 597 598 599 600 601
	}
out:
	lpfc_els_free_iocb(phba, cmdiocb);
}

static int
J
James Smart 已提交
602
lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
603 604
		     uint8_t retry)
{
J
James Smart 已提交
605
	struct lpfc_hba  *phba = vport->phba;
已提交
606 607 608 609 610 611 612 613 614 615 616
	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];

617
	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
J
James Smart 已提交
618 619
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_FLOGI);
620

621
	if (!elsiocb)
622
		return 1;
已提交
623 624 625 626 627 628

	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;
629 630
	pcmd += sizeof(uint32_t);
	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
已提交
631 632 633 634 635 636 637 638 639 640 641 642 643
	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;

644 645 646 647 648 649 650 651
	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 已提交
652 653 654 655 656
	if (phba->fc_topology != TOPOLOGY_LOOP) {
		icmd->un.elsreq64.myID = 0;
		icmd->un.elsreq64.fl = 1;
	}

已提交
657 658
	tmo = phba->fc_ratov;
	phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
J
James Smart 已提交
659
	lpfc_set_disctmo(vport);
已提交
660 661 662 663
	phba->fc_ratov = tmo;

	phba->fc_stat.elsXmitFLOGI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
J
James Smart 已提交
664 665 666 667 668

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

669
	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
已提交
670 671
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
672
		return 1;
已提交
673
	}
674
	return 0;
已提交
675 676 677
}

int
J
James Smart 已提交
678
lpfc_els_abort_flogi(struct lpfc_hba *phba)
已提交
679 680 681 682 683 684 685 686
{
	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,
687 688
			"0201 Abort outstanding I/O on NPort x%x\n",
			Fabric_DID);
已提交
689 690 691 692 693 694 695

	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 已提交
696
	spin_lock_irq(&phba->hbalock);
已提交
697 698
	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
		icmd = &iocb->iocb;
J
James Smart 已提交
699 700
		if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
		    icmd->un.elsreq64.bdl.ulpIoTag32) {
已提交
701
			ndlp = (struct lpfc_nodelist *)(iocb->context1);
702
			if (ndlp && (ndlp->nlp_DID == Fabric_DID)) {
703
				lpfc_sli_issue_abort_iotag(phba, pring, iocb);
704
			}
已提交
705 706
		}
	}
J
James Smart 已提交
707
	spin_unlock_irq(&phba->hbalock);
已提交
708 709 710 711 712

	return 0;
}

int
J
James Smart 已提交
713
lpfc_initial_flogi(struct lpfc_vport *vport)
已提交
714
{
J
James Smart 已提交
715
	struct lpfc_hba *phba = vport->phba;
已提交
716 717
	struct lpfc_nodelist *ndlp;

718 719 720
	vport->port_state = LPFC_FLOGI;
	lpfc_set_disctmo(vport);

721
	/* First look for the Fabric ndlp */
J
James Smart 已提交
722
	ndlp = lpfc_findnode_did(vport, Fabric_DID);
723
	if (!ndlp) {
已提交
724
		/* Cannot find existing Fabric ndlp, so allocate a new one */
725 726 727
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp)
			return 0;
J
James Smart 已提交
728
		lpfc_nlp_init(vport, ndlp, Fabric_DID);
729
	} else {
J
James Smart 已提交
730
		lpfc_dequeue_node(vport, ndlp);
已提交
731
	}
732

J
James Smart 已提交
733
	if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
734
		lpfc_nlp_put(ndlp);
已提交
735
	}
736
	return 1;
已提交
737 738
}

739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
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;
}
761 762

void
J
James Smart 已提交
763
lpfc_more_plogi(struct lpfc_vport *vport)
已提交
764 765 766
{
	int sentplogi;

J
James Smart 已提交
767 768
	if (vport->num_disc_nodes)
		vport->num_disc_nodes--;
已提交
769 770

	/* Continue discovery with <num_disc_nodes> PLOGIs to go */
771 772 773 774 775
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0232 Continue discovery with %d PLOGIs to go "
			 "Data: x%x x%x x%x\n",
			 vport->num_disc_nodes, vport->fc_plogi_cnt,
			 vport->fc_flag, vport->port_state);
已提交
776
	/* Check to see if there are more PLOGIs to be sent */
J
James Smart 已提交
777 778 779 780
	if (vport->fc_flag & FC_NLP_MORE)
		/* go thru NPR nodes and issue any remaining ELS PLOGIs */
		sentplogi = lpfc_els_disc_plogi(vport);

已提交
781 782 783
	return;
}

784
static struct lpfc_nodelist *
785
lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
786 787
			 struct lpfc_nodelist *ndlp)
{
J
James Smart 已提交
788
	struct lpfc_vport    *vport = ndlp->vport;
789
	struct lpfc_nodelist *new_ndlp;
790 791
	struct lpfc_rport_data *rdata;
	struct fc_rport *rport;
792
	struct serv_parm *sp;
793
	uint8_t  name[sizeof(struct lpfc_name)];
794 795
	uint32_t rc;

J
James Smart 已提交
796 797 798 799 800 801
	/* 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;

802
	sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
803
	memset(name, 0, sizeof(struct lpfc_name));
804

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

810
	if (new_ndlp == ndlp)
811 812 813
		return ndlp;

	if (!new_ndlp) {
J
James Smart 已提交
814 815
		rc = memcmp(&ndlp->nlp_portname, name,
			    sizeof(struct lpfc_name));
816 817
		if (!rc)
			return ndlp;
818 819 820 821
		new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
		if (!new_ndlp)
			return ndlp;

J
James Smart 已提交
822
		lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
823 824
	}

J
James Smart 已提交
825
	lpfc_unreg_rpi(vport, new_ndlp);
826
	new_ndlp->nlp_DID = ndlp->nlp_DID;
827
	new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
828 829 830 831 832

	if (ndlp->nlp_flag & NLP_NPR_2B_DISC)
		new_ndlp->nlp_flag |= NLP_NPR_2B_DISC;
	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;

J
James Smart 已提交
833
	lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
834

J
James Smart 已提交
835
	/* Move this back to NPR state */
836 837 838 839
	if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
		/* The new_ndlp is replacing ndlp totally, so we need
		 * to put ndlp on UNUSED list and try to free it.
		 */
840 841 842 843 844 845 846 847 848 849 850 851 852 853

		/* Fix up the rport accordingly */
		rport =  ndlp->rport;
		if (rport) {
			rdata = rport->dd_data;
			if (rdata->pnode == ndlp) {
				lpfc_nlp_put(ndlp);
				ndlp->rport = NULL;
				rdata->pnode = lpfc_nlp_get(new_ndlp);
				new_ndlp->rport = rport;
			}
			new_ndlp->nlp_type = ndlp->nlp_type;
		}

J
James Smart 已提交
854
		lpfc_drop_node(vport, ndlp);
855
	}
856
	else {
J
James Smart 已提交
857
		lpfc_unreg_rpi(vport, ndlp);
858
		ndlp->nlp_DID = 0; /* Two ndlps cannot have the same did */
J
James Smart 已提交
859
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
860
	}
861 862 863
	return new_ndlp;
}

864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
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);
		}
	}
}

已提交
885
static void
J
James Smart 已提交
886 887
lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		    struct lpfc_iocbq *rspiocb)
已提交
888
{
J
James Smart 已提交
889 890
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
891 892
	IOCB_t *irsp;
	struct lpfc_nodelist *ndlp;
893
	struct lpfc_dmabuf *prsp;
已提交
894 895 896 897 898 899
	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 已提交
900 901 902 903 904
	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 已提交
905
	ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
906
	if (!ndlp) {
907 908 909 910 911 912
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0136 PLOGI completes to NPort x%x "
				 "with no ndlp. Data: x%x x%x x%x\n",
				 irsp->un.elsreq64.remoteID,
				 irsp->ulpStatus, irsp->un.ulpWord[4],
				 irsp->ulpIoTag);
913
		goto out;
914
	}
已提交
915 916 917 918

	/* Since ndlp can be freed in the disc state machine, note if this node
	 * is being used during discovery.
	 */
J
James Smart 已提交
919
	spin_lock_irq(shost->host_lock);
已提交
920
	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
921
	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
J
James Smart 已提交
922
	spin_unlock_irq(shost->host_lock);
已提交
923 924 925
	rc   = 0;

	/* PLOGI completes to NPort <nlp_DID> */
926 927 928 929 930
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0102 PLOGI completes to NPort x%x "
			 "Data: x%x x%x x%x x%x x%x\n",
			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
			 irsp->ulpTimeout, disc, vport->num_disc_nodes);
已提交
931
	/* Check to see if link went down during discovery */
J
James Smart 已提交
932 933
	if (lpfc_els_chk_latt(vport)) {
		spin_lock_irq(shost->host_lock);
已提交
934
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
935
		spin_unlock_irq(shost->host_lock);
已提交
936 937 938 939 940 941 942 943 944 945 946 947
		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 已提交
948
				spin_lock_irq(shost->host_lock);
已提交
949
				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
950
				spin_unlock_irq(shost->host_lock);
已提交
951 952 953 954 955
			}
			goto out;
		}
		/* PLOGI failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
J
James Smart 已提交
956
		if (lpfc_error_lost_link(irsp)) {
957
			rc = NLP_STE_FREED_NODE;
958
		} else {
J
James Smart 已提交
959
			rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
960
						     NLP_EVT_CMPL_PLOGI);
已提交
961 962 963
		}
	} else {
		/* Good status, call state machine */
964
		prsp = list_entry(((struct lpfc_dmabuf *)
965 966 967
				   cmdiocb->context2)->list.next,
				  struct lpfc_dmabuf, list);
		ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
J
James Smart 已提交
968
		rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
969
					     NLP_EVT_CMPL_PLOGI);
已提交
970 971
	}

J
James Smart 已提交
972
	if (disc && vport->num_disc_nodes) {
已提交
973
		/* Check to see if there are more PLOGIs to be sent */
J
James Smart 已提交
974
		lpfc_more_plogi(vport);
已提交
975

J
James Smart 已提交
976 977 978 979
		if (vport->num_disc_nodes == 0) {
			spin_lock_irq(shost->host_lock);
			vport->fc_flag &= ~FC_NDISC_ACTIVE;
			spin_unlock_irq(shost->host_lock);
已提交
980

J
James Smart 已提交
981
			lpfc_can_disctmo(vport);
982
			lpfc_end_rscn(vport);
已提交
983 984 985 986 987 988 989 990 991
		}
	}

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

int
J
James Smart 已提交
992
lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
已提交
993
{
J
James Smart 已提交
994
	struct lpfc_hba  *phba = vport->phba;
已提交
995 996
	struct serv_parm *sp;
	IOCB_t *icmd;
997
	struct lpfc_nodelist *ndlp;
已提交
998 999 1000 1001 1002
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;
1003
	int ret;
已提交
1004 1005 1006 1007

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

1008 1009 1010
	ndlp = lpfc_findnode_did(vport, did);
	/* If ndlp if not NULL, we will bump the reference count on it */

1011
	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1012
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
J
James Smart 已提交
1013
				     ELS_CMD_PLOGI);
1014 1015
	if (!elsiocb)
		return 1;
已提交
1016 1017 1018 1019 1020 1021

	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;
1022 1023
	pcmd += sizeof(uint32_t);
	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
已提交
1024 1025 1026 1027 1028 1029 1030 1031
	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 已提交
1032 1033 1034 1035
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue PLOGI:     did:x%x",
		did, 0, 0);

已提交
1036 1037
	phba->fc_stat.elsXmitPLOGI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1038 1039 1040
	ret = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);

	if (ret == IOCB_ERROR) {
已提交
1041
		lpfc_els_free_iocb(phba, elsiocb);
1042
		return 1;
已提交
1043
	}
1044
	return 0;
已提交
1045 1046 1047
}

static void
J
James Smart 已提交
1048 1049
lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		   struct lpfc_iocbq *rspiocb)
已提交
1050
{
J
James Smart 已提交
1051 1052
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	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 已提交
1063
	spin_lock_irq(shost->host_lock);
已提交
1064
	ndlp->nlp_flag &= ~NLP_PRLI_SND;
J
James Smart 已提交
1065
	spin_unlock_irq(shost->host_lock);
已提交
1066

J
James Smart 已提交
1067 1068 1069 1070
	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);
已提交
1071
	/* PRLI completes to NPort <nlp_DID> */
1072 1073 1074 1075 1076
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0103 PRLI completes to NPort x%x "
			 "Data: x%x x%x x%x x%x\n",
			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
			 irsp->ulpTimeout, vport->num_disc_nodes);
已提交
1077

J
James Smart 已提交
1078
	vport->fc_prli_sent--;
已提交
1079
	/* Check to see if link went down during discovery */
J
James Smart 已提交
1080
	if (lpfc_els_chk_latt(vport))
已提交
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
		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 已提交
1091
		if (lpfc_error_lost_link(irsp)) {
已提交
1092
			goto out;
1093
		} else {
J
James Smart 已提交
1094
			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1095
						NLP_EVT_CMPL_PRLI);
已提交
1096 1097 1098
		}
	} else {
		/* Good status, call state machine */
J
James Smart 已提交
1099
		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1100
					NLP_EVT_CMPL_PRLI);
已提交
1101 1102 1103 1104 1105 1106 1107 1108
	}

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

int
J
James Smart 已提交
1109
lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1110 1111
		    uint8_t retry)
{
J
James Smart 已提交
1112 1113
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba *phba = vport->phba;
已提交
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	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 */

1125
	cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
J
James Smart 已提交
1126 1127
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_PRLI);
1128
	if (!elsiocb)
1129
		return 1;
已提交
1130 1131 1132 1133 1134

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

	/* For PRLI request, remainder of payload is service parameters */
1135
	memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
已提交
1136
	*((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1137
	pcmd += sizeof(uint32_t);
已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156

	/* 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 已提交
1157 1158 1159 1160
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue PRLI:      did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1161 1162
	phba->fc_stat.elsXmitPRLI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
J
James Smart 已提交
1163
	spin_lock_irq(shost->host_lock);
已提交
1164
	ndlp->nlp_flag |= NLP_PRLI_SND;
J
James Smart 已提交
1165
	spin_unlock_irq(shost->host_lock);
已提交
1166
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
J
James Smart 已提交
1167
		spin_lock_irq(shost->host_lock);
已提交
1168
		ndlp->nlp_flag &= ~NLP_PRLI_SND;
J
James Smart 已提交
1169
		spin_unlock_irq(shost->host_lock);
已提交
1170
		lpfc_els_free_iocb(phba, elsiocb);
1171
		return 1;
已提交
1172
	}
J
James Smart 已提交
1173
	vport->fc_prli_sent++;
1174
	return 0;
已提交
1175 1176
}

1177
void
J
James Smart 已提交
1178
lpfc_more_adisc(struct lpfc_vport *vport)
已提交
1179 1180 1181
{
	int sentadisc;

J
James Smart 已提交
1182 1183
	if (vport->num_disc_nodes)
		vport->num_disc_nodes--;
已提交
1184
	/* Continue discovery with <num_disc_nodes> ADISCs to go */
1185 1186 1187 1188 1189
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0210 Continue discovery with %d ADISCs to go "
			 "Data: x%x x%x x%x\n",
			 vport->num_disc_nodes, vport->fc_adisc_cnt,
			 vport->fc_flag, vport->port_state);
已提交
1190
	/* Check to see if there are more ADISCs to be sent */
J
James Smart 已提交
1191 1192 1193 1194
	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);
已提交
1195 1196 1197 1198 1199
	}
	return;
}

static void
J
James Smart 已提交
1200
lpfc_rscn_disc(struct lpfc_vport *vport)
已提交
1201
{
J
James Smart 已提交
1202 1203
	lpfc_can_disctmo(vport);

已提交
1204
	/* RSCN discovery */
J
James Smart 已提交
1205 1206 1207
	/* go thru NPR nodes and issue ELS PLOGIs */
	if (vport->fc_npr_cnt)
		if (lpfc_els_disc_plogi(vport))
已提交
1208
			return;
J
James Smart 已提交
1209

1210
	lpfc_end_rscn(vport);
已提交
1211 1212 1213
}

static void
J
James Smart 已提交
1214 1215
lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		    struct lpfc_iocbq *rspiocb)
已提交
1216
{
J
James Smart 已提交
1217 1218
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
1219 1220
	IOCB_t *irsp;
	struct lpfc_nodelist *ndlp;
J
James Smart 已提交
1221
	int  disc;
已提交
1222 1223 1224 1225 1226 1227 1228

	/* 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 已提交
1229 1230 1231 1232 1233
	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);

已提交
1234 1235 1236
	/* Since ndlp can be freed in the disc state machine, note if this node
	 * is being used during discovery.
	 */
J
James Smart 已提交
1237
	spin_lock_irq(shost->host_lock);
已提交
1238
	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1239
	ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
J
James Smart 已提交
1240
	spin_unlock_irq(shost->host_lock);
已提交
1241
	/* ADISC completes to NPort <nlp_DID> */
1242 1243 1244 1245 1246
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0104 ADISC completes to NPort x%x "
			 "Data: x%x x%x x%x x%x x%x\n",
			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
			 irsp->ulpTimeout, disc, vport->num_disc_nodes);
已提交
1247
	/* Check to see if link went down during discovery */
J
James Smart 已提交
1248 1249
	if (lpfc_els_chk_latt(vport)) {
		spin_lock_irq(shost->host_lock);
已提交
1250
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
1251
		spin_unlock_irq(shost->host_lock);
已提交
1252 1253 1254 1255 1256 1257 1258 1259
		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 已提交
1260
				spin_lock_irq(shost->host_lock);
已提交
1261
				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
1262 1263
				spin_unlock_irq(shost->host_lock);
				lpfc_set_disctmo(vport);
已提交
1264 1265 1266 1267 1268
			}
			goto out;
		}
		/* ADISC failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
J
James Smart 已提交
1269
		if (!lpfc_error_lost_link(irsp)) {
J
James Smart 已提交
1270
			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
J
James Smart 已提交
1271
						NLP_EVT_CMPL_ADISC);
已提交
1272 1273 1274
		}
	} else {
		/* Good status, call state machine */
J
James Smart 已提交
1275
		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
已提交
1276 1277 1278
					NLP_EVT_CMPL_ADISC);
	}

J
James Smart 已提交
1279
	if (disc && vport->num_disc_nodes) {
已提交
1280
		/* Check to see if there are more ADISCs to be sent */
J
James Smart 已提交
1281
		lpfc_more_adisc(vport);
已提交
1282 1283

		/* Check to see if we are done with ADISC authentication */
J
James Smart 已提交
1284
		if (vport->num_disc_nodes == 0) {
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
			/* 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 */
1301
				if (vport->port_type == LPFC_PHYSICAL_PORT)
J
James Smart 已提交
1302
					lpfc_issue_clear_la(phba, vport);
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319

				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;
已提交
1320
			} else {
J
James Smart 已提交
1321
				lpfc_rscn_disc(vport);
已提交
1322 1323 1324 1325 1326 1327 1328 1329 1330
			}
		}
	}
out:
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
J
James Smart 已提交
1331
lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1332 1333
		     uint8_t retry)
{
J
James Smart 已提交
1334 1335
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
1336 1337 1338
	ADISC *ap;
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
J
James Smart 已提交
1339 1340
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
已提交
1341 1342 1343
	uint8_t *pcmd;
	uint16_t cmdsize;

1344
	cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
J
James Smart 已提交
1345 1346
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ADISC);
1347
	if (!elsiocb)
1348
		return 1;
已提交
1349 1350 1351 1352 1353 1354

	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;
1355
	pcmd += sizeof(uint32_t);
已提交
1356 1357 1358 1359

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

J
James Smart 已提交
1364 1365 1366 1367
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue ADISC:     did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1368 1369
	phba->fc_stat.elsXmitADISC++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
J
James Smart 已提交
1370
	spin_lock_irq(shost->host_lock);
已提交
1371
	ndlp->nlp_flag |= NLP_ADISC_SND;
J
James Smart 已提交
1372
	spin_unlock_irq(shost->host_lock);
已提交
1373
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
J
James Smart 已提交
1374
		spin_lock_irq(shost->host_lock);
已提交
1375
		ndlp->nlp_flag &= ~NLP_ADISC_SND;
J
James Smart 已提交
1376
		spin_unlock_irq(shost->host_lock);
已提交
1377
		lpfc_els_free_iocb(phba, elsiocb);
1378
		return 1;
已提交
1379
	}
1380
	return 0;
已提交
1381 1382 1383
}

static void
J
James Smart 已提交
1384 1385
lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		   struct lpfc_iocbq *rspiocb)
已提交
1386
{
J
James Smart 已提交
1387 1388 1389
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	struct lpfc_vport *vport = ndlp->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
1390 1391 1392 1393 1394 1395 1396 1397
	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 已提交
1398
	spin_lock_irq(shost->host_lock);
已提交
1399
	ndlp->nlp_flag &= ~NLP_LOGO_SND;
J
James Smart 已提交
1400
	spin_unlock_irq(shost->host_lock);
已提交
1401

J
James Smart 已提交
1402 1403 1404 1405
	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);
已提交
1406
	/* LOGO completes to NPort <nlp_DID> */
1407 1408 1409 1410 1411
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0105 LOGO completes to NPort x%x "
			 "Data: x%x x%x x%x x%x\n",
			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
			 irsp->ulpTimeout, vport->num_disc_nodes);
已提交
1412
	/* Check to see if link went down during discovery */
J
James Smart 已提交
1413
	if (lpfc_els_chk_latt(vport))
已提交
1414 1415
		goto out;

1416 1417 1418 1419 1420 1421 1422 1423 1424
	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;
	}

已提交
1425 1426
	if (irsp->ulpStatus) {
		/* Check for retry */
J
James Smart 已提交
1427
		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
已提交
1428 1429 1430 1431
			/* ELS command is being retried */
			goto out;
		/* LOGO failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
J
James Smart 已提交
1432
		if (lpfc_error_lost_link(irsp))
已提交
1433
			goto out;
J
James Smart 已提交
1434
		else
J
James Smart 已提交
1435
			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1436
						NLP_EVT_CMPL_LOGO);
已提交
1437
	} else {
1438 1439 1440
		/* Good status, call state machine.
		 * This will unregister the rpi if needed.
		 */
J
James Smart 已提交
1441
		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1442
					NLP_EVT_CMPL_LOGO);
已提交
1443 1444 1445 1446 1447 1448 1449 1450
	}

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

int
J
James Smart 已提交
1451
lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1452 1453
		    uint8_t retry)
{
J
James Smart 已提交
1454 1455
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
1456 1457 1458 1459 1460 1461
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;
1462
	int rc;
已提交
1463 1464 1465 1466

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

1467 1468 1469 1470 1471 1472 1473
	spin_lock_irq(shost->host_lock);
	if (ndlp->nlp_flag & NLP_LOGO_SND) {
		spin_unlock_irq(shost->host_lock);
		return 0;
	}
	spin_unlock_irq(shost->host_lock);

1474
	cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
J
James Smart 已提交
1475 1476
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_LOGO);
1477
	if (!elsiocb)
1478
		return 1;
已提交
1479 1480 1481 1482

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

	/* Fill in LOGO payload */
J
James Smart 已提交
1486
	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
1487 1488
	pcmd += sizeof(uint32_t);
	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
已提交
1489

J
James Smart 已提交
1490 1491 1492 1493
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue LOGO:      did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1494 1495
	phba->fc_stat.elsXmitLOGO++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
J
James Smart 已提交
1496
	spin_lock_irq(shost->host_lock);
已提交
1497
	ndlp->nlp_flag |= NLP_LOGO_SND;
J
James Smart 已提交
1498
	spin_unlock_irq(shost->host_lock);
1499 1500 1501
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);

	if (rc == IOCB_ERROR) {
J
James Smart 已提交
1502
		spin_lock_irq(shost->host_lock);
已提交
1503
		ndlp->nlp_flag &= ~NLP_LOGO_SND;
J
James Smart 已提交
1504
		spin_unlock_irq(shost->host_lock);
已提交
1505
		lpfc_els_free_iocb(phba, elsiocb);
1506
		return 1;
已提交
1507
	}
1508
	return 0;
已提交
1509 1510 1511
}

static void
J
James Smart 已提交
1512 1513
lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_iocbq *rspiocb)
已提交
1514
{
J
James Smart 已提交
1515
	struct lpfc_vport *vport = cmdiocb->vport;
已提交
1516 1517 1518 1519
	IOCB_t *irsp;

	irsp = &rspiocb->iocb;

J
James Smart 已提交
1520 1521 1522 1523
	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);
已提交
1524
	/* ELS cmd tag <ulpIoTag> completes */
1525 1526 1527 1528
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
			 irsp->ulpIoTag, irsp->ulpStatus,
			 irsp->un.ulpWord[4], irsp->ulpTimeout);
已提交
1529
	/* Check to see if link went down during discovery */
J
James Smart 已提交
1530
	lpfc_els_chk_latt(vport);
已提交
1531 1532 1533 1534 1535
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
J
James Smart 已提交
1536
lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
已提交
1537
{
J
James Smart 已提交
1538
	struct lpfc_hba  *phba = vport->phba;
已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
	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 */
1549
	cmdsize = (sizeof(uint32_t) + sizeof(SCR));
1550 1551 1552
	ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
	if (!ndlp)
		return 1;
已提交
1553

J
James Smart 已提交
1554 1555 1556 1557
	lpfc_nlp_init(vport, ndlp, nportid);

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

1559
	if (!elsiocb) {
1560
		lpfc_nlp_put(ndlp);
1561
		return 1;
已提交
1562 1563 1564 1565 1566 1567
	}

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

	*((uint32_t *) (pcmd)) = ELS_CMD_SCR;
1568
	pcmd += sizeof(uint32_t);
已提交
1569 1570

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

J
James Smart 已提交
1574 1575 1576 1577
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue SCR:       did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1578 1579 1580
	phba->fc_stat.elsXmitSCR++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1581
		lpfc_nlp_put(ndlp);
已提交
1582
		lpfc_els_free_iocb(phba, elsiocb);
1583
		return 1;
已提交
1584
	}
1585
	lpfc_nlp_put(ndlp);
1586
	return 0;
已提交
1587 1588 1589
}

static int
J
James Smart 已提交
1590
lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
已提交
1591
{
J
James Smart 已提交
1592
	struct lpfc_hba  *phba = vport->phba;
已提交
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
	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 */
1606
	cmdsize = (sizeof(uint32_t) + sizeof(FARP));
1607 1608 1609
	ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
	if (!ndlp)
		return 1;
已提交
1610

J
James Smart 已提交
1611 1612 1613 1614
	lpfc_nlp_init(vport, ndlp, nportid);

	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_RNID);
1615
	if (!elsiocb) {
1616
		lpfc_nlp_put(ndlp);
1617
		return 1;
已提交
1618 1619 1620 1621 1622 1623
	}

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

	*((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
1624
	pcmd += sizeof(uint32_t);
已提交
1625 1626 1627

	/* Fill in FARPR payload */
	fp = (FARP *) (pcmd);
1628
	memset(fp, 0, sizeof(FARP));
已提交
1629 1630
	lp = (uint32_t *) pcmd;
	*lp++ = be32_to_cpu(nportid);
J
James Smart 已提交
1631
	*lp++ = be32_to_cpu(vport->fc_myDID);
已提交
1632 1633 1634
	fp->Rflags = 0;
	fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);

1635 1636
	memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
	memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
J
James Smart 已提交
1637 1638
	ondlp = lpfc_findnode_did(vport, nportid);
	if (ondlp) {
已提交
1639
		memcpy(&fp->OportName, &ondlp->nlp_portname,
1640
		       sizeof(struct lpfc_name));
已提交
1641
		memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
1642
		       sizeof(struct lpfc_name));
已提交
1643 1644
	}

J
James Smart 已提交
1645 1646 1647 1648
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue FARPR:     did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1649 1650 1651
	phba->fc_stat.elsXmitFARPR++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1652
		lpfc_nlp_put(ndlp);
已提交
1653
		lpfc_els_free_iocb(phba, elsiocb);
1654
		return 1;
已提交
1655
	}
1656
	lpfc_nlp_put(ndlp);
1657
	return 0;
已提交
1658 1659
}

1660
void
J
James Smart 已提交
1661
lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
1662
{
J
James Smart 已提交
1663 1664 1665
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	spin_lock_irq(shost->host_lock);
1666
	nlp->nlp_flag &= ~NLP_DELAY_TMO;
J
James Smart 已提交
1667
	spin_unlock_irq(shost->host_lock);
1668 1669 1670 1671 1672 1673 1674
	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 已提交
1675
		spin_lock_irq(shost->host_lock);
1676
		nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
J
James Smart 已提交
1677 1678
		spin_unlock_irq(shost->host_lock);
		if (vport->num_disc_nodes) {
1679 1680 1681
			/* Check to see if there are more
			 * PLOGIs to be sent
			 */
J
James Smart 已提交
1682 1683 1684 1685 1686 1687 1688
			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);
1689
				lpfc_end_rscn(vport);
1690 1691 1692 1693 1694 1695
			}
		}
	}
	return;
}

已提交
1696 1697 1698
void
lpfc_els_retry_delay(unsigned long ptr)
{
J
James Smart 已提交
1699 1700 1701
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
	struct lpfc_vport *vport = ndlp->vport;
	struct lpfc_hba   *phba = vport->phba;
1702
	unsigned long flags;
J
James Smart 已提交
1703
	struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
已提交
1704

J
James Smart 已提交
1705 1706
	ndlp = (struct lpfc_nodelist *) ptr;
	phba = ndlp->vport->phba;
已提交
1707 1708
	evtp = &ndlp->els_retry_evt;

1709
	spin_lock_irqsave(&phba->hbalock, flags);
已提交
1710
	if (!list_empty(&evtp->evt_listp)) {
1711
		spin_unlock_irqrestore(&phba->hbalock, flags);
已提交
1712 1713 1714 1715 1716 1717 1718
		return;
	}

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

1721
	spin_unlock_irqrestore(&phba->hbalock, flags);
已提交
1722 1723 1724 1725 1726 1727
	return;
}

void
lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
{
J
James Smart 已提交
1728 1729 1730
	struct lpfc_vport *vport = ndlp->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	uint32_t cmd, did, retry;
已提交
1731

J
James Smart 已提交
1732
	spin_lock_irq(shost->host_lock);
1733 1734 1735
	did = ndlp->nlp_DID;
	cmd = ndlp->nlp_last_elscmd;
	ndlp->nlp_last_elscmd = 0;
已提交
1736 1737

	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
J
James Smart 已提交
1738
		spin_unlock_irq(shost->host_lock);
已提交
1739 1740 1741 1742
		return;
	}

	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
J
James Smart 已提交
1743
	spin_unlock_irq(shost->host_lock);
1744 1745 1746 1747 1748 1749
	/*
	 * 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);
已提交
1750 1751 1752 1753
	retry = ndlp->nlp_retry;

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

static int
J
James Smart 已提交
1788 1789
lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
	       struct lpfc_iocbq *rspiocb)
已提交
1790
{
J
James Smart 已提交
1791 1792 1793 1794 1795
	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;
已提交
1796 1797
	uint32_t *elscmd;
	struct ls_rjt stat;
J
James Smart 已提交
1798
	int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
1799
	int logerr = 0;
J
James Smart 已提交
1800
	uint32_t cmd = 0;
1801
	uint32_t did;
已提交
1802

1803

已提交
1804 1805 1806 1807 1808 1809 1810 1811 1812
	/* Note: context2 may be 0 for internal driver abort
	 * of delays ELS command.
	 */

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

1813
	if (ndlp)
1814 1815 1816 1817
		did = ndlp->nlp_DID;
	else {
		/* We should only hit this case for retrying PLOGI */
		did = irsp->un.elsreq64.remoteID;
J
James Smart 已提交
1818
		ndlp = lpfc_findnode_did(vport, did);
1819 1820 1821 1822
		if (!ndlp && (cmd != ELS_CMD_PLOGI))
			return 1;
	}

J
James Smart 已提交
1823 1824 1825 1826
	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);

已提交
1827 1828 1829 1830 1831 1832 1833 1834
	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 已提交
1835
			if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
1836
				delay = 1000;
已提交
1837 1838 1839
			retry = 1;
			break;

1840 1841 1842
		case IOERR_ILLEGAL_COMMAND:
			if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) &&
			    (cmd == ELS_CMD_FDISC)) {
1843 1844 1845
				lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
						 "0124 FDISC failed (3/6) "
						 "retrying...\n");
1846 1847
				lpfc_mbx_unreg_vpi(vport);
				retry = 1;
1848 1849 1850 1851
				/* FDISC retry policy */
				maxretry = 48;
				if (cmdiocb->retry >= 32)
					delay = 1000;
1852 1853 1854
			}
			break;

已提交
1855
		case IOERR_NO_RESOURCES:
1856
			logerr = 1; /* HBA out of resources */
J
James Smart 已提交
1857 1858 1859 1860 1861 1862 1863
			retry = 1;
			if (cmdiocb->retry > 100)
				delay = 100;
			maxretry = 250;
			break;

		case IOERR_ILLEGAL_FRAME:
1864
			delay = 100;
已提交
1865 1866 1867
			retry = 1;
			break;

J
James Smart 已提交
1868
		case IOERR_SEQUENCE_TIMEOUT:
已提交
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
		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:
1885
		logerr = 1; /* Fabric / Remote NPort out of resources */
已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
		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) {
1899
					delay = 1000;
已提交
1900 1901 1902 1903 1904 1905
					maxretry = 48;
				}
				retry = 1;
				break;
			}
			if (cmd == ELS_CMD_PLOGI) {
1906
				delay = 1000;
已提交
1907 1908 1909 1910
				maxretry = lpfc_max_els_tries + 1;
				retry = 1;
				break;
			}
1911 1912 1913
			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
			  (cmd == ELS_CMD_FDISC) &&
			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
1914 1915 1916 1917
				lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
						 "0125 FDISC Failed (x%x). "
						 "Fabric out of resources\n",
						 stat.un.lsRjtError);
1918 1919 1920
				lpfc_vport_set_state(vport,
						     FC_VPORT_NO_FABRIC_RSCS);
			}
已提交
1921 1922 1923
			break;

		case LSRJT_LOGICAL_BSY:
J
James Smart 已提交
1924 1925
			if ((cmd == ELS_CMD_PLOGI) ||
			    (cmd == ELS_CMD_PRLI)) {
1926
				delay = 1000;
已提交
1927
				maxretry = 48;
1928
			} else if (cmd == ELS_CMD_FDISC) {
1929 1930 1931 1932
				/* FDISC retry policy */
				maxretry = 48;
				if (cmdiocb->retry >= 32)
					delay = 1000;
已提交
1933 1934 1935
			}
			retry = 1;
			break;
1936 1937 1938 1939 1940 1941 1942 1943

		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))
			  ) {
1944 1945 1946 1947
				lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
						 "0123 FDISC Failed (x%x). "
						 "Fabric Detected Bad WWN\n",
						 stat.un.lsRjtError);
1948 1949 1950 1951
				lpfc_vport_set_state(vport,
						     FC_VPORT_FABRIC_REJ_WWN);
			}
			break;
已提交
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
		}
		break;

	case IOSTAT_INTERMED_RSP:
	case IOSTAT_BA_RJT:
		break;

	default:
		break;
	}

1963
	if (did == FDMI_DID)
已提交
1964 1965
		retry = 1;

1966 1967 1968 1969 1970 1971 1972 1973 1974
	if ((cmd == ELS_CMD_FLOGI) &&
	    (phba->fc_topology != TOPOLOGY_LOOP)) {
		/* FLOGI retry policy */
		retry = 1;
		maxretry = 48;
		if (cmdiocb->retry >= 32)
			delay = 1000;
	}

已提交
1975 1976 1977 1978 1979
	if ((++cmdiocb->retry) >= maxretry) {
		phba->fc_stat.elsRetryExceeded++;
		retry = 0;
	}

1980 1981 1982
	if ((vport->load_flag & FC_UNLOADING) != 0)
		retry = 0;

已提交
1983 1984 1985
	if (retry) {

		/* Retry ELS command <elsCmd> to remote NPORT <did> */
1986 1987 1988 1989
		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
				 "0107 Retry ELS command x%x to remote "
				 "NPORT x%x Data: x%x x%x\n",
				 cmd, did, cmdiocb->retry, delay);
已提交
1990

J
James Smart 已提交
1991 1992 1993 1994 1995
		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 */

已提交
1996
			/* If discovery / RSCN timer is running, reset it */
J
James Smart 已提交
1997
			if (timer_pending(&vport->fc_disctmo) ||
1998
			    (vport->fc_flag & FC_RSCN_MODE))
J
James Smart 已提交
1999
				lpfc_set_disctmo(vport);
已提交
2000 2001 2002
		}

		phba->fc_stat.elsXmitRetry++;
2003
		if (ndlp && delay) {
已提交
2004 2005 2006
			phba->fc_stat.elsDelayRetry++;
			ndlp->nlp_retry = cmdiocb->retry;

2007 2008 2009
			/* delay is specified in milliseconds */
			mod_timer(&ndlp->nlp_delayfunc,
				jiffies + msecs_to_jiffies(delay));
J
James Smart 已提交
2010
			spin_lock_irq(shost->host_lock);
已提交
2011
			ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
2012
			spin_unlock_irq(shost->host_lock);
已提交
2013

2014
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2015 2016 2017 2018 2019 2020
			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);
已提交
2021 2022
			ndlp->nlp_last_elscmd = cmd;

2023
			return 1;
已提交
2024 2025 2026
		}
		switch (cmd) {
		case ELS_CMD_FLOGI:
J
James Smart 已提交
2027
			lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
2028
			return 1;
2029 2030 2031
		case ELS_CMD_FDISC:
			lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
			return 1;
已提交
2032
		case ELS_CMD_PLOGI:
2033 2034
			if (ndlp) {
				ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2035
				lpfc_nlp_set_state(vport, ndlp,
2036
						   NLP_STE_PLOGI_ISSUE);
2037
			}
J
James Smart 已提交
2038
			lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2039
			return 1;
已提交
2040
		case ELS_CMD_ADISC:
2041
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2042 2043
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2044
			return 1;
已提交
2045
		case ELS_CMD_PRLI:
2046
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2047 2048
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
			lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
2049
			return 1;
已提交
2050
		case ELS_CMD_LOGO:
2051
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2052 2053
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
			lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2054
			return 1;
已提交
2055 2056 2057
		}
	}
	/* No retry ELS command <elsCmd> to remote NPORT <did> */
2058 2059 2060 2061 2062 2063 2064 2065 2066
	if (logerr) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
			 "0137 No retry ELS command x%x to remote "
			 "NPORT x%x: Out of Resources: Error:x%x/%x\n",
			 cmd, did, irsp->ulpStatus,
			 irsp->un.ulpWord[4]);
	}
	else {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2067 2068 2069 2070
			 "0108 No retry ELS command x%x to remote "
			 "NPORT x%x Retried:%d Error:x%x/%x\n",
			 cmd, did, cmdiocb->retry, irsp->ulpStatus,
			 irsp->un.ulpWord[4]);
2071
	}
2072
	return 0;
已提交
2073 2074
}

2075
static int
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
{
	struct lpfc_dmabuf *buf_ptr;

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

2093
static int
2094 2095 2096 2097 2098 2099 2100
lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
{
	lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
	kfree(buf_ptr);
	return 0;
}

已提交
2101
int
2102
lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
已提交
2103 2104
{
	struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2105 2106 2107 2108 2109 2110
	struct lpfc_nodelist *ndlp;

	ndlp = (struct lpfc_nodelist *)elsiocb->context1;
	if (ndlp) {
		if (ndlp->nlp_flag & NLP_DEFER_RM) {
			lpfc_nlp_put(ndlp);
已提交
2111

2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
			/* If the ndlp is not being used by another discovery
			 * thread, free it.
			 */
			if (!lpfc_nlp_not_used(ndlp)) {
				/* If ndlp is being used by another discovery
				 * thread, just clear NLP_DEFER_RM
				 */
				ndlp->nlp_flag &= ~NLP_DEFER_RM;
			}
		}
		else
			lpfc_nlp_put(ndlp);
2124 2125
		elsiocb->context1 = NULL;
	}
已提交
2126 2127
	/* context2  = cmd,  context2->next = rsp, context3 = bpl */
	if (elsiocb->context2) {
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
		if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
			/* Firmware could still be in progress of DMAing
			 * payload, so don't free data buffer till after
			 * a hbeat.
			 */
			elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
			buf_ptr = elsiocb->context2;
			elsiocb->context2 = NULL;
			if (buf_ptr) {
				buf_ptr1 = NULL;
				spin_lock_irq(&phba->hbalock);
				if (!list_empty(&buf_ptr->list)) {
					list_remove_head(&buf_ptr->list,
						buf_ptr1, struct lpfc_dmabuf,
						list);
					INIT_LIST_HEAD(&buf_ptr1->list);
					list_add_tail(&buf_ptr1->list,
						&phba->elsbuf);
					phba->elsbuf_cnt++;
				}
				INIT_LIST_HEAD(&buf_ptr->list);
				list_add_tail(&buf_ptr->list, &phba->elsbuf);
				phba->elsbuf_cnt++;
				spin_unlock_irq(&phba->hbalock);
			}
		} else {
			buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
			lpfc_els_free_data(phba, buf_ptr1);
		}
已提交
2157 2158 2159 2160
	}

	if (elsiocb->context3) {
		buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
2161
		lpfc_els_free_bpl(phba, buf_ptr);
已提交
2162
	}
2163
	lpfc_sli_release_iocbq(phba, elsiocb);
已提交
2164 2165 2166 2167
	return 0;
}

static void
J
James Smart 已提交
2168 2169
lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		       struct lpfc_iocbq *rspiocb)
已提交
2170
{
J
James Smart 已提交
2171 2172
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	struct lpfc_vport *vport = cmdiocb->vport;
J
James Smart 已提交
2173 2174 2175 2176 2177 2178
	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);
已提交
2179
	/* ACC to LOGO completes to NPort <nlp_DID> */
2180 2181 2182 2183 2184
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0109 ACC to LOGO completes to NPort x%x "
			 "Data: x%x x%x x%x\n",
			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
			 ndlp->nlp_rpi);
2185 2186 2187 2188 2189 2190 2191 2192 2193

	if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
		/* NPort Recovery mode or node is just allocated */
		if (!lpfc_nlp_not_used(ndlp)) {
			/* If the ndlp is being used by another discovery
			 * thread, just unregister the RPI.
			 */
			lpfc_unreg_rpi(vport, ndlp);
		}
已提交
2194 2195 2196 2197 2198
	}
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

J
James Smart 已提交
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
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);
2209 2210
	if (ndlp) {
		lpfc_nlp_put(ndlp);
2211

2212 2213 2214 2215 2216 2217
		/* This is the end of the default RPI cleanup logic for this
		 * ndlp. If no other discovery threads are using this ndlp.
		 * we should free all resources associated with it.
		 */
		lpfc_nlp_not_used(ndlp);
	}
J
James Smart 已提交
2218 2219 2220
	return;
}

已提交
2221
static void
J
James Smart 已提交
2222
lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2223
		  struct lpfc_iocbq *rspiocb)
已提交
2224
{
J
James Smart 已提交
2225 2226 2227
	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;
2228 2229
	IOCB_t  *irsp;
	uint8_t *pcmd;
已提交
2230
	LPFC_MBOXQ_t *mbox = NULL;
J
James Smart 已提交
2231
	struct lpfc_dmabuf *mp = NULL;
2232
	uint32_t ls_rjt = 0;
已提交
2233

J
James Smart 已提交
2234 2235
	irsp = &rspiocb->iocb;

已提交
2236 2237 2238
	if (cmdiocb->context_un.mbox)
		mbox = cmdiocb->context_un.mbox;

2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
	/* First determine if this is a LS_RJT cmpl */
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
	if (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT) {
		/* A LS_RJT associated with Default RPI cleanup
		 * has its own seperate code path.
		 */
		if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
			ls_rjt = 1;
	}

已提交
2249
	/* Check to see if link went down during discovery */
J
James Smart 已提交
2250
	if (!ndlp || lpfc_els_chk_latt(vport)) {
已提交
2251
		if (mbox) {
2252 2253 2254 2255 2256
			mp = (struct lpfc_dmabuf *) mbox->context1;
			if (mp) {
				lpfc_mbuf_free(phba, mp->virt, mp->phys);
				kfree(mp);
			}
2257
			mempool_free(mbox, phba->mbox_mem_pool);
已提交
2258
		}
2259 2260 2261
		if (ndlp && (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
			if (lpfc_nlp_not_used(ndlp))
				ndlp = NULL;
已提交
2262 2263 2264
		goto out;
	}

J
James Smart 已提交
2265
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2266
		"ELS rsp cmpl:    status:x%x/x%x did:x%x",
J
James Smart 已提交
2267
		irsp->ulpStatus, irsp->un.ulpWord[4],
2268
		cmdiocb->iocb.un.elsreq64.remoteID);
已提交
2269
	/* ELS response tag <ulpIoTag> completes */
2270 2271 2272 2273 2274 2275 2276
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0110 ELS response tag x%x completes "
			 "Data: x%x x%x x%x x%x x%x x%x x%x\n",
			 cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
			 rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
			 ndlp->nlp_rpi);
已提交
2277 2278 2279
	if (mbox) {
		if ((rspiocb->iocb.ulpStatus == 0)
		    && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
J
James Smart 已提交
2280
			lpfc_unreg_rpi(vport, ndlp);
2281
			mbox->context2 = lpfc_nlp_get(ndlp);
J
James Smart 已提交
2282
			mbox->vport = vport;
J
James Smart 已提交
2283 2284 2285 2286 2287 2288 2289 2290
			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 已提交
2291
					   NLP_STE_REG_LOGIN_ISSUE);
J
James Smart 已提交
2292
			}
2293
			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
已提交
2294 2295 2296
			    != MBX_NOT_FINISHED) {
				goto out;
			}
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306

			/* ELS rsp: Cannot issue reg_login for <NPortid> */
			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				"0138 ELS rsp: Cannot issue reg_login for x%x "
				"Data: x%x x%x x%x\n",
				ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
				ndlp->nlp_rpi);

			if (lpfc_nlp_not_used(ndlp))
				ndlp = NULL;
已提交
2307
		} else {
J
James Smart 已提交
2308 2309 2310
			/* Do not drop node for lpfc_els_abort'ed ELS cmds */
			if (!lpfc_error_lost_link(irsp) &&
			    ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
2311 2312
				if (lpfc_nlp_not_used(ndlp))
					ndlp = NULL;
已提交
2313 2314
			}
		}
2315 2316 2317 2318 2319 2320
		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);
已提交
2321 2322 2323
	}
out:
	if (ndlp) {
J
James Smart 已提交
2324
		spin_lock_irq(shost->host_lock);
J
James Smart 已提交
2325
		ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
J
James Smart 已提交
2326
		spin_unlock_irq(shost->host_lock);
2327 2328 2329 2330 2331 2332 2333 2334

		/* If the node is not being used by another discovery thread,
		 * and we are sending a reject, we are done with it.
		 * Release driver reference count here and free associated
		 * resources.
		 */
		if (ls_rjt)
			lpfc_nlp_not_used(ndlp);
已提交
2335
	}
2336

已提交
2337 2338 2339 2340 2341
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
J
James Smart 已提交
2342 2343
lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
		 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
2344
		 LPFC_MBOXQ_t *mbox)
已提交
2345
{
J
James Smart 已提交
2346 2347
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
2348 2349 2350 2351 2352 2353 2354 2355
	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;
2356
	ELS_PKT *els_pkt_ptr;
已提交
2357 2358 2359 2360 2361 2362 2363

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

	switch (flag) {
	case ELS_CMD_ACC:
2364
		cmdsize = sizeof(uint32_t);
J
James Smart 已提交
2365 2366
		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2367
		if (!elsiocb) {
J
James Smart 已提交
2368
			spin_lock_irq(shost->host_lock);
2369
			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
J
James Smart 已提交
2370
			spin_unlock_irq(shost->host_lock);
2371
			return 1;
已提交
2372
		}
J
James Smart 已提交
2373

已提交
2374 2375 2376 2377
		icmd = &elsiocb->iocb;
		icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2378
		pcmd += sizeof(uint32_t);
J
James Smart 已提交
2379 2380 2381 2382

		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);
已提交
2383 2384
		break;
	case ELS_CMD_PLOGI:
2385
		cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
J
James Smart 已提交
2386 2387
		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2388
		if (!elsiocb)
2389
			return 1;
2390

已提交
2391 2392 2393 2394 2395 2396 2397 2398
		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;
2399 2400
		pcmd += sizeof(uint32_t);
		memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
J
James Smart 已提交
2401 2402 2403 2404

		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);
已提交
2405
		break;
2406
	case ELS_CMD_PRLO:
2407
		cmdsize = sizeof(uint32_t) + sizeof(PRLO);
J
James Smart 已提交
2408
		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2409 2410 2411 2412 2413 2414 2415 2416 2417
					     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,
2418
		       sizeof(uint32_t) + sizeof(PRLO));
2419 2420 2421
		*((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 已提交
2422 2423 2424 2425

		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);
2426
		break;
已提交
2427
	default:
2428
		return 1;
已提交
2429 2430
	}
	/* Xmit ELS ACC response tag <ulpIoTag> */
2431 2432 2433 2434 2435 2436
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
			 "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
			 elsiocb->iotag, elsiocb->iocb.ulpContext,
			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
			 ndlp->nlp_rpi);
已提交
2437
	if (ndlp->nlp_flag & NLP_LOGO_ACC) {
J
James Smart 已提交
2438
		spin_lock_irq(shost->host_lock);
2439
		ndlp->nlp_flag &= ~NLP_LOGO_ACC;
J
James Smart 已提交
2440
		spin_unlock_irq(shost->host_lock);
已提交
2441 2442
		elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
	} else {
J
James Smart 已提交
2443
		elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2444 2445 2446 2447 2448 2449
	}

	phba->fc_stat.elsXmitACC++;
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2450
		return 1;
已提交
2451
	}
2452
	return 0;
已提交
2453 2454 2455
}

int
J
James Smart 已提交
2456
lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
J
James Smart 已提交
2457 2458
		    struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
		    LPFC_MBOXQ_t *mbox)
已提交
2459
{
J
James Smart 已提交
2460
	struct lpfc_hba  *phba = vport->phba;
已提交
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472
	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 */

2473
	cmdsize = 2 * sizeof(uint32_t);
J
James Smart 已提交
2474 2475
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
2476
	if (!elsiocb)
2477
		return 1;
已提交
2478 2479 2480 2481 2482 2483 2484

	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;
2485
	pcmd += sizeof(uint32_t);
已提交
2486 2487
	*((uint32_t *) (pcmd)) = rejectError;

2488
	if (mbox)
J
James Smart 已提交
2489 2490
		elsiocb->context_un.mbox = mbox;

已提交
2491
	/* Xmit ELS RJT <err> response tag <ulpIoTag> */
2492 2493 2494 2495 2496 2497 2498
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "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",
			 rejectError, elsiocb->iotag,
			 elsiocb->iocb.ulpContext, ndlp->nlp_DID,
			 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
J
James Smart 已提交
2499 2500 2501 2502
	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);

已提交
2503
	phba->fc_stat.elsXmitLSRJT++;
J
James Smart 已提交
2504
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2505
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2506

已提交
2507 2508
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2509
		return 1;
已提交
2510
	}
2511
	return 0;
已提交
2512 2513 2514
}

int
J
James Smart 已提交
2515 2516
lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
		       struct lpfc_nodelist *ndlp)
已提交
2517
{
J
James Smart 已提交
2518 2519 2520
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_sli  *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
已提交
2521
	ADISC *ap;
J
James Smart 已提交
2522
	IOCB_t *icmd, *oldcmd;
已提交
2523 2524 2525 2526 2527
	struct lpfc_iocbq *elsiocb;
	uint8_t *pcmd;
	uint16_t cmdsize;
	int rc;

2528
	cmdsize = sizeof(uint32_t) + sizeof(ADISC);
J
James Smart 已提交
2529 2530
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
2531
	if (!elsiocb)
2532
		return 1;
已提交
2533

2534 2535 2536 2537
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */

已提交
2538
	/* Xmit ADISC ACC response tag <ulpIoTag> */
2539 2540 2541 2542 2543 2544
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0130 Xmit ADISC ACC response iotag x%x xri: "
			 "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
			 elsiocb->iotag, elsiocb->iocb.ulpContext,
			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
			 ndlp->nlp_rpi);
已提交
2545 2546 2547
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2548
	pcmd += sizeof(uint32_t);
已提交
2549 2550 2551

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

J
James Smart 已提交
2556 2557 2558 2559
	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);

已提交
2560
	phba->fc_stat.elsXmitACC++;
J
James Smart 已提交
2561
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2562 2563 2564
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2565
		return 1;
已提交
2566
	}
2567
	return 0;
已提交
2568 2569 2570
}

int
J
James Smart 已提交
2571
lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
2572
		      struct lpfc_nodelist *ndlp)
已提交
2573
{
J
James Smart 已提交
2574
	struct lpfc_hba  *phba = vport->phba;
已提交
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
	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 */

2589
	cmdsize = sizeof(uint32_t) + sizeof(PRLI);
J
James Smart 已提交
2590
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2591
		ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
2592 2593
	if (!elsiocb)
		return 1;
已提交
2594

2595 2596 2597
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
已提交
2598
	/* Xmit PRLI ACC response tag <ulpIoTag> */
2599 2600 2601 2602 2603 2604
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
			 elsiocb->iotag, elsiocb->iocb.ulpContext,
			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
			 ndlp->nlp_rpi);
已提交
2605 2606 2607
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	*((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
2608
	pcmd += sizeof(uint32_t);
已提交
2609 2610

	/* For PRLI, remainder of payload is PRLI parameter page */
2611
	memset(pcmd, 0, sizeof(PRLI));
已提交
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632

	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 已提交
2633 2634 2635 2636
	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);

已提交
2637
	phba->fc_stat.elsXmitACC++;
J
James Smart 已提交
2638
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2639 2640 2641 2642

	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2643
		return 1;
已提交
2644
	}
2645
	return 0;
已提交
2646 2647 2648
}

static int
J
James Smart 已提交
2649
lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
2650
		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
已提交
2651
{
J
James Smart 已提交
2652
	struct lpfc_hba  *phba = vport->phba;
已提交
2653
	RNID *rn;
J
James Smart 已提交
2654
	IOCB_t *icmd, *oldcmd;
已提交
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
	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];

2665 2666
	cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
					+ (2 * sizeof(struct lpfc_name));
已提交
2667
	if (format)
2668
		cmdsize += sizeof(RNID_TOP_DISC);
已提交
2669

J
James Smart 已提交
2670 2671
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
2672
	if (!elsiocb)
2673
		return 1;
已提交
2674

2675 2676 2677
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
已提交
2678
	/* Xmit RNID ACC response tag <ulpIoTag> */
2679 2680 2681
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
			 elsiocb->iotag, elsiocb->iocb.ulpContext);
已提交
2682 2683
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2684
	pcmd += sizeof(uint32_t);
已提交
2685

2686
	memset(pcmd, 0, sizeof(RNID));
已提交
2687 2688
	rn = (RNID *) (pcmd);
	rn->Format = format;
2689 2690 2691
	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));
已提交
2692 2693 2694 2695 2696
	switch (format) {
	case 0:
		rn->SpecificLen = 0;
		break;
	case RNID_TOPOLOGY_DISC:
2697
		rn->SpecificLen = sizeof(RNID_TOP_DISC);
已提交
2698
		memcpy(&rn->un.topologyDisc.portName,
2699
		       &vport->fc_portname, sizeof(struct lpfc_name));
已提交
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709
		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 已提交
2710 2711 2712 2713
	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);

已提交
2714
	phba->fc_stat.elsXmitACC++;
J
James Smart 已提交
2715
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2716
	lpfc_nlp_put(ndlp);
已提交
2717 2718 2719 2720 2721 2722
	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);
2723
		return 1;
已提交
2724
	}
2725
	return 0;
已提交
2726 2727 2728
}

int
J
James Smart 已提交
2729
lpfc_els_disc_adisc(struct lpfc_vport *vport)
已提交
2730
{
J
James Smart 已提交
2731
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
2732
	struct lpfc_nodelist *ndlp, *next_ndlp;
J
James Smart 已提交
2733
	int sentadisc = 0;
已提交
2734

2735
	/* go thru NPR nodes and issue any remaining ELS ADISCs */
J
James Smart 已提交
2736
	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2737 2738 2739
		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 已提交
2740
			spin_lock_irq(shost->host_lock);
2741
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
2742
			spin_unlock_irq(shost->host_lock);
2743
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2744 2745
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
2746
			sentadisc++;
J
James Smart 已提交
2747 2748
			vport->num_disc_nodes++;
			if (vport->num_disc_nodes >=
2749
			    vport->cfg_discovery_threads) {
J
James Smart 已提交
2750 2751 2752
				spin_lock_irq(shost->host_lock);
				vport->fc_flag |= FC_NLP_MORE;
				spin_unlock_irq(shost->host_lock);
2753
				break;
已提交
2754 2755 2756 2757
			}
		}
	}
	if (sentadisc == 0) {
J
James Smart 已提交
2758 2759 2760
		spin_lock_irq(shost->host_lock);
		vport->fc_flag &= ~FC_NLP_MORE;
		spin_unlock_irq(shost->host_lock);
已提交
2761
	}
2762
	return sentadisc;
已提交
2763 2764 2765
}

int
J
James Smart 已提交
2766
lpfc_els_disc_plogi(struct lpfc_vport *vport)
已提交
2767
{
J
James Smart 已提交
2768
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
2769
	struct lpfc_nodelist *ndlp, *next_ndlp;
J
James Smart 已提交
2770
	int sentplogi = 0;
已提交
2771

J
James Smart 已提交
2772 2773
	/* go thru NPR nodes and issue any remaining ELS PLOGIs */
	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2774 2775 2776 2777 2778
		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 已提交
2779 2780
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2781
			sentplogi++;
J
James Smart 已提交
2782 2783
			vport->num_disc_nodes++;
			if (vport->num_disc_nodes >=
2784
			    vport->cfg_discovery_threads) {
J
James Smart 已提交
2785 2786 2787
				spin_lock_irq(shost->host_lock);
				vport->fc_flag |= FC_NLP_MORE;
				spin_unlock_irq(shost->host_lock);
2788
				break;
已提交
2789 2790 2791
			}
		}
	}
2792 2793 2794 2795
	if (sentplogi) {
		lpfc_set_disctmo(vport);
	}
	else {
J
James Smart 已提交
2796 2797 2798
		spin_lock_irq(shost->host_lock);
		vport->fc_flag &= ~FC_NLP_MORE;
		spin_unlock_irq(shost->host_lock);
已提交
2799
	}
2800
	return sentplogi;
已提交
2801 2802
}

2803
void
J
James Smart 已提交
2804
lpfc_els_flush_rscn(struct lpfc_vport *vport)
已提交
2805
{
J
James Smart 已提交
2806 2807
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
2808 2809
	int i;

J
James Smart 已提交
2810
	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
2811
		lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
J
James Smart 已提交
2812
		vport->fc_rscn_id_list[i] = NULL;
已提交
2813
	}
J
James Smart 已提交
2814 2815 2816 2817 2818
	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);
已提交
2819 2820 2821
}

int
J
James Smart 已提交
2822
lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
已提交
2823 2824 2825 2826
{
	D_ID ns_did;
	D_ID rscn_did;
	uint32_t *lp;
2827
	uint32_t payload_len, i;
已提交
2828 2829 2830 2831 2832

	ns_did.un.word = did;

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

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

J
James Smart 已提交
2839
	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
2840 2841 2842
		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 */
已提交
2843
		while (payload_len) {
2844 2845
			rscn_did.un.word = be32_to_cpu(*lp++);
			payload_len -= sizeof(uint32_t);
已提交
2846 2847
			switch (rscn_did.un.b.resv) {
			case 0:	/* Single N_Port ID effected */
J
James Smart 已提交
2848
				if (ns_did.un.word == rscn_did.un.word)
2849
					return did;
已提交
2850 2851 2852 2853
				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))
2854
					return did;
已提交
2855 2856 2857
				break;
			case 2:	/* Whole N_Port Domain effected */
				if (ns_did.un.b.domain == rscn_did.un.b.domain)
2858
					return did;
已提交
2859 2860
				break;
			default:
J
James Smart 已提交
2861
				/* Unknown Identifier in RSCN node */
2862 2863 2864 2865
				lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
						 "0217 Unknown Identifier in "
						 "RSCN payload Data: x%x\n",
						 rscn_did.un.word);
2866 2867
			case 3:	/* Whole Fabric effected */
				return did;
已提交
2868 2869
			}
		}
2870 2871
	}
	return 0;
已提交
2872 2873 2874
}

static int
J
James Smart 已提交
2875
lpfc_rscn_recovery_check(struct lpfc_vport *vport)
已提交
2876
{
2877
	struct lpfc_nodelist *ndlp = NULL;
已提交
2878 2879

	/* Look at all nodes effected by pending RSCNs and move
2880
	 * them to NPR state.
已提交
2881 2882
	 */

J
James Smart 已提交
2883
	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2884
		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE ||
J
James Smart 已提交
2885
		    lpfc_rscn_payload_check(vport, ndlp->nlp_DID) == 0)
2886
			continue;
已提交
2887

J
James Smart 已提交
2888
		lpfc_disc_state_machine(vport, ndlp, NULL,
2889
						NLP_EVT_DEVICE_RECOVERY);
2890

2891 2892 2893 2894 2895
		/*
		 * Make sure NLP_DELAY_TMO is NOT running after a device
		 * recovery event.
		 */
		if (ndlp->nlp_flag & NLP_DELAY_TMO)
J
James Smart 已提交
2896
			lpfc_cancel_retry_delay_tmo(vport, ndlp);
已提交
2897
	}
2898

2899
	return 0;
已提交
2900 2901 2902
}

static int
J
James Smart 已提交
2903
lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
2904
		  struct lpfc_nodelist *ndlp)
已提交
2905
{
J
James Smart 已提交
2906 2907
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
2908
	struct lpfc_dmabuf *pcmd;
2909
	uint32_t *lp, *datap;
已提交
2910
	IOCB_t *icmd;
2911 2912 2913
	uint32_t payload_len, length, nportid, *cmd;
	int rscn_cnt = vport->fc_rscn_id_cnt;
	int rscn_id = 0, hba_id = 0;
2914
	int i;
已提交
2915 2916 2917 2918 2919

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

2920 2921
	payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
	payload_len -= sizeof(uint32_t);	/* take off word 0 */
已提交
2922
	/* RSCN received */
2923 2924 2925
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0214 RSCN received Data: x%x x%x x%x x%x\n",
			 vport->fc_flag, payload_len, *lp, rscn_cnt);
2926
	for (i = 0; i < payload_len/sizeof(uint32_t); i++)
J
James Smart 已提交
2927
		fc_host_post_event(shost, fc_get_event_number(),
2928 2929
			FCH_EVT_RSCN, lp[i]);

已提交
2930 2931 2932
	/* If we are about to begin discovery, just ACC the RSCN.
	 * Discovery processing will satisfy it.
	 */
J
James Smart 已提交
2933
	if (vport->port_state <= LPFC_NS_QRY) {
J
James Smart 已提交
2934 2935 2936 2937
		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);

2938
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2939
		return 0;
已提交
2940 2941
	}

2942 2943 2944 2945
	/* 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) &&
2946
		!(vport->cfg_peer_port_login)) {
2947 2948 2949 2950 2951 2952 2953
		i = payload_len;
		datap = lp;
		while (i > 0) {
			nportid = *datap++;
			nportid = ((be32_to_cpu(nportid)) & Mask_DID);
			i -= sizeof(uint32_t);
			rscn_id++;
2954 2955
			if (lpfc_find_vport_by_did(phba, nportid))
				hba_id++;
2956 2957 2958
		}
		if (rscn_id == hba_id) {
			/* ALL NPortIDs in RSCN are on HBA */
2959 2960 2961 2962 2963
			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
					 "0214 Ignore RSCN "
					 "Data: x%x x%x x%x x%x\n",
					 vport->fc_flag, payload_len,
					 *lp, rscn_cnt);
J
James Smart 已提交
2964 2965 2966 2967 2968
			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);

2969
			lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
2970
				ndlp, NULL);
2971 2972 2973 2974
			return 0;
		}
	}

已提交
2975 2976 2977
	/* If we are already processing an RSCN, save the received
	 * RSCN payload buffer, cmdiocb->context2 to process later.
	 */
J
James Smart 已提交
2978
	if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
J
James Smart 已提交
2979 2980 2981 2982
		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);

2983
		spin_lock_irq(shost->host_lock);
2984 2985
		vport->fc_flag |= FC_RSCN_DEFERRED;
		if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
J
James Smart 已提交
2986 2987 2988
		    !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
			vport->fc_flag |= FC_RSCN_MODE;
			spin_unlock_irq(shost->host_lock);
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006
			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;
			}
已提交
3007 3008

			/* Deferred RSCN */
3009 3010 3011 3012 3013
			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
					 "0235 Deferred RSCN "
					 "Data: x%x x%x x%x\n",
					 vport->fc_rscn_id_cnt, vport->fc_flag,
					 vport->port_state);
已提交
3014
		} else {
J
James Smart 已提交
3015 3016
			vport->fc_flag |= FC_RSCN_DISCOVERY;
			spin_unlock_irq(shost->host_lock);
已提交
3017
			/* ReDiscovery RSCN */
3018 3019 3020 3021 3022
			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
					 "0234 ReDiscovery RSCN "
					 "Data: x%x x%x x%x\n",
					 vport->fc_rscn_id_cnt, vport->fc_flag,
					 vport->port_state);
已提交
3023 3024
		}
		/* Send back ACC */
3025
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
3026 3027

		/* send RECOVERY event for ALL nodes that match RSCN payload */
J
James Smart 已提交
3028
		lpfc_rscn_recovery_check(vport);
3029
		spin_lock_irq(shost->host_lock);
3030
		vport->fc_flag &= ~FC_RSCN_DEFERRED;
3031
		spin_unlock_irq(shost->host_lock);
3032
		return 0;
已提交
3033 3034
	}

J
James Smart 已提交
3035 3036 3037 3038
	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 已提交
3039 3040 3041 3042
	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;
已提交
3043 3044 3045 3046 3047 3048
	/*
	 * If we zero, cmdiocb->context2, the calling routine will
	 * not try to free it.
	 */
	cmdiocb->context2 = NULL;

J
James Smart 已提交
3049
	lpfc_set_disctmo(vport);
已提交
3050 3051

	/* Send back ACC */
3052
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
3053 3054

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

J
James Smart 已提交
3057
	return lpfc_els_handle_rscn(vport);
已提交
3058 3059 3060
}

int
J
James Smart 已提交
3061
lpfc_els_handle_rscn(struct lpfc_vport *vport)
已提交
3062 3063
{
	struct lpfc_nodelist *ndlp;
J
James Smart 已提交
3064
	struct lpfc_hba *phba = vport->phba;
已提交
3065

3066 3067 3068 3069 3070 3071
	/* Ignore RSCN if the port is being torn down. */
	if (vport->load_flag & FC_UNLOADING) {
		lpfc_els_flush_rscn(vport);
		return 0;
	}

已提交
3072
	/* Start timer for RSCN processing */
J
James Smart 已提交
3073
	lpfc_set_disctmo(vport);
已提交
3074 3075

	/* RSCN processed */
3076 3077 3078 3079
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0215 RSCN processed Data: x%x x%x x%x x%x\n",
			 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
			 vport->port_state);
已提交
3080 3081

	/* To process RSCN, first compare RSCN data with NameServer */
J
James Smart 已提交
3082
	vport->fc_ns_retry = 0;
3083 3084
	vport->num_disc_nodes = 0;

J
James Smart 已提交
3085
	ndlp = lpfc_findnode_did(vport, NameServer_DID);
3086
	if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
已提交
3087
		/* Good ndlp, issue CT Request to NameServer */
3088
		if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
已提交
3089 3090
			/* Wait for NameServer query cmpl before we can
			   continue */
3091
			return 1;
已提交
3092 3093 3094
	} else {
		/* If login to NameServer does not exist, issue one */
		/* Good status, issue PLOGI to NameServer */
J
James Smart 已提交
3095 3096
		ndlp = lpfc_findnode_did(vport, NameServer_DID);
		if (ndlp)
已提交
3097 3098
			/* Wait for NameServer login cmpl before we can
			   continue */
3099
			return 1;
J
James Smart 已提交
3100

3101 3102
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp) {
J
James Smart 已提交
3103
			lpfc_els_flush_rscn(vport);
3104
			return 0;
已提交
3105
		} else {
J
James Smart 已提交
3106
			lpfc_nlp_init(vport, ndlp, NameServer_DID);
已提交
3107
			ndlp->nlp_type |= NLP_FABRIC;
3108
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
3109 3110
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, NameServer_DID, 0);
已提交
3111 3112
			/* Wait for NameServer login cmpl before we can
			   continue */
3113
			return 1;
已提交
3114 3115 3116
		}
	}

J
James Smart 已提交
3117
	lpfc_els_flush_rscn(vport);
3118
	return 0;
已提交
3119 3120 3121
}

static int
J
James Smart 已提交
3122
lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3123
		   struct lpfc_nodelist *ndlp)
已提交
3124
{
J
James Smart 已提交
3125 3126
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
	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 已提交
3141
	lpfc_set_disctmo(vport);
已提交
3142 3143 3144 3145 3146 3147 3148

	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 */
3149 3150 3151 3152
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0113 An FLOGI ELS command x%x was "
				 "received from DID x%x in Loop Mode\n",
				 cmd, did);
3153
		return 1;
已提交
3154 3155 3156 3157
	}

	did = Fabric_DID;

J
James Smart 已提交
3158
	if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3))) {
已提交
3159 3160 3161 3162
		/* For a FLOGI we accept, then if our portname is greater
		 * then the remote portname we initiate Nport login.
		 */

J
James Smart 已提交
3163
		rc = memcmp(&vport->fc_portname, &sp->portName,
3164
			    sizeof(struct lpfc_name));
已提交
3165 3166

		if (!rc) {
J
James Smart 已提交
3167 3168
			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
			if (!mbox)
3169
				return 1;
J
James Smart 已提交
3170

已提交
3171 3172 3173 3174 3175 3176
			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;
3177
			mbox->vport = vport;
3178
			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3179
			lpfc_set_loopback_flag(phba);
已提交
3180
			if (rc == MBX_NOT_FINISHED) {
3181
				mempool_free(mbox, phba->mbox_mem_pool);
已提交
3182
			}
3183
			return 1;
3184
		} else if (rc > 0) {	/* greater than */
J
James Smart 已提交
3185 3186 3187
			spin_lock_irq(shost->host_lock);
			vport->fc_flag |= FC_PT2PT_PLOGI;
			spin_unlock_irq(shost->host_lock);
已提交
3188
		}
J
James Smart 已提交
3189 3190 3191 3192
		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);
已提交
3193 3194 3195 3196 3197 3198
	} 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 已提交
3199 3200
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
3201
		return 1;
已提交
3202 3203 3204
	}

	/* Send back ACC */
3205
	lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
已提交
3206

3207
	return 0;
已提交
3208 3209 3210
}

static int
J
James Smart 已提交
3211 3212
lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_nodelist *ndlp)
已提交
3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234
{
	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 已提交
3235
		lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
已提交
3236 3237 3238 3239 3240 3241 3242
		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 已提交
3243 3244
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
已提交
3245
	}
3246
	return 0;
已提交
3247 3248 3249
}

static int
J
James Smart 已提交
3250 3251
lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_nodelist *ndlp)
3252 3253 3254 3255 3256 3257 3258 3259
{
	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 已提交
3260
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3261 3262 3263
	return 0;
}

3264
static void
3265
lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3266
{
J
James Smart 已提交
3267 3268
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281
	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 已提交
3282 3283
	pmb->context1 = NULL;
	pmb->context2 = NULL;
3284 3285

	if (mb->mbxStatus) {
3286
		mempool_free(pmb, phba->mbox_mem_pool);
3287 3288 3289 3290
		return;
	}

	cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
3291
	mempool_free(pmb, phba->mbox_mem_pool);
J
James Smart 已提交
3292 3293 3294
	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
				     lpfc_max_els_tries, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
3295
	lpfc_nlp_put(ndlp);
3296
	if (!elsiocb)
3297 3298 3299 3300 3301 3302 3303
		return;

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

	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3304
	pcmd += sizeof(uint32_t); /* Skip past command */
3305 3306 3307 3308 3309 3310
	rps_rsp = (RPS_RSP *)pcmd;

	if (phba->fc_topology != TOPOLOGY_LOOP)
		status = 0x10;
	else
		status = 0x8;
J
James Smart 已提交
3311
	if (phba->pport->fc_flag & FC_FABRIC)
3312 3313 3314
		status |= 0x4;

	rps_rsp->rsvd1 = 0;
3315 3316 3317 3318 3319 3320 3321
	rps_rsp->portStatus = cpu_to_be16(status);
	rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
	rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
	rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
	rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
	rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
	rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
3322
	/* Xmit ELS RPS ACC response tag <ulpIoTag> */
3323 3324 3325 3326 3327 3328
	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
			 "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",
			 elsiocb->iotag, elsiocb->iocb.ulpContext,
			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
			 ndlp->nlp_rpi);
J
James Smart 已提交
3329
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3330
	phba->fc_stat.elsXmitACC++;
3331
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR)
3332 3333 3334 3335 3336
		lpfc_els_free_iocb(phba, elsiocb);
	return;
}

static int
J
James Smart 已提交
3337 3338
lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		 struct lpfc_nodelist *ndlp)
已提交
3339
{
J
James Smart 已提交
3340
	struct lpfc_hba *phba = vport->phba;
已提交
3341
	uint32_t *lp;
3342 3343 3344 3345 3346 3347
	uint8_t flag;
	LPFC_MBOXQ_t *mbox;
	struct lpfc_dmabuf *pcmd;
	RPS *rps;
	struct ls_rjt stat;

3348 3349
	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3350 3351 3352 3353
		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 已提交
3354 3355
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
3356 3357 3358 3359 3360 3361 3362 3363 3364
	}

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

3368 3369
		printk("Fix me....\n");
		dump_stack();
J
James Smart 已提交
3370 3371
		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
		if (mbox) {
3372 3373
			lpfc_read_lnk_stat(phba, mbox);
			mbox->context1 =
3374
			    (void *)((unsigned long) cmdiocb->iocb.ulpContext);
3375
			mbox->context2 = lpfc_nlp_get(ndlp);
3376
			mbox->vport = vport;
3377
			mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
3378 3379
			if (lpfc_sli_issue_mbox (phba, mbox, MBX_NOWAIT)
				!= MBX_NOT_FINISHED)
3380 3381
				/* Mbox completion will send ELS Response */
				return 0;
J
James Smart 已提交
3382

3383
			lpfc_nlp_put(ndlp);
3384 3385 3386 3387 3388 3389 3390
			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 已提交
3391
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3392 3393 3394
	return 0;
}

3395
static int
J
James Smart 已提交
3396 3397
lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
		     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3398
{
J
James Smart 已提交
3399 3400
	struct lpfc_hba *phba = vport->phba;
	IOCB_t *icmd, *oldcmd;
3401 3402
	RPL_RSP rpl_rsp;
	struct lpfc_iocbq *elsiocb;
J
James Smart 已提交
3403 3404
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3405
	uint8_t *pcmd;
已提交
3406

J
James Smart 已提交
3407 3408
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
3409

3410
	if (!elsiocb)
3411
		return 1;
3412

3413 3414 3415 3416 3417 3418
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */

	pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3419
	pcmd += sizeof(uint16_t);
3420 3421 3422 3423 3424 3425 3426
	*((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 已提交
3427 3428
	rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
	memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
3429 3430 3431
	    sizeof(struct lpfc_name));
	memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
	/* Xmit ELS RPL ACC response tag <ulpIoTag> */
3432 3433 3434 3435 3436 3437 3438
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "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",
			 elsiocb->iotag, elsiocb->iocb.ulpContext,
			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
			 ndlp->nlp_rpi);
J
James Smart 已提交
3439
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3440 3441 3442 3443 3444 3445 3446 3447 3448
	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 已提交
3449 3450
lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		 struct lpfc_nodelist *ndlp)
3451 3452 3453 3454 3455 3456 3457 3458
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	uint32_t maxsize;
	uint16_t cmdsize;
	RPL *rpl;
	struct ls_rjt stat;

3459 3460
	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3461 3462 3463 3464
		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 已提交
3465 3466
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
3467 3468
	}

已提交
3469 3470
	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;
3471
	rpl = (RPL *) (lp + 1);
已提交
3472

3473
	maxsize = be32_to_cpu(rpl->maxsize);
已提交
3474

3475 3476 3477 3478 3479
	/* 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);
3480
	} else {
3481 3482
		cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
	}
J
James Smart 已提交
3483
	lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
已提交
3484 3485 3486 3487 3488

	return 0;
}

static int
J
James Smart 已提交
3489 3490
lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_nodelist *ndlp)
已提交
3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505
{
	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> */
3506 3507
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0601 FARP-REQ received from DID x%x\n", did);
已提交
3508 3509
	/* We will only support match on WWPN or WWNN */
	if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
3510
		return 0;
已提交
3511 3512 3513 3514 3515
	}

	cnt = 0;
	/* If this FARP command is searching for my portname */
	if (fp->Mflags & FARP_MATCH_PORT) {
J
James Smart 已提交
3516
		if (memcmp(&fp->RportName, &vport->fc_portname,
3517
			   sizeof(struct lpfc_name)) == 0)
已提交
3518 3519 3520 3521 3522
			cnt = 1;
	}

	/* If this FARP command is searching for my nodename */
	if (fp->Mflags & FARP_MATCH_NODE) {
J
James Smart 已提交
3523
		if (memcmp(&fp->RnodeName, &vport->fc_nodename,
3524
			   sizeof(struct lpfc_name)) == 0)
已提交
3525 3526 3527 3528 3529 3530 3531 3532
			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) {
3533
				ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
3534
				lpfc_nlp_set_state(vport, ndlp,
3535
						   NLP_STE_PLOGI_ISSUE);
J
James Smart 已提交
3536
				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
3537 3538 3539
			}

			/* Send a FARP response to that node */
J
James Smart 已提交
3540 3541
			if (fp->Rflags & FARP_REQUEST_FARPR)
				lpfc_issue_els_farpr(vport, did, 0);
已提交
3542 3543
		}
	}
3544
	return 0;
已提交
3545 3546 3547
}

static int
J
James Smart 已提交
3548 3549
lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		   struct lpfc_nodelist  *ndlp)
已提交
3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	IOCB_t *icmd;
	uint32_t cmd, did;

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

	cmd = *lp++;
	/* FARP-RSP received from DID <did> */
3563 3564
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0600 FARP-RSP received from DID x%x\n", did);
已提交
3565
	/* ACCEPT the Farp resp request */
3566
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
3567 3568 3569 3570 3571

	return 0;
}

static int
J
James Smart 已提交
3572 3573
lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		 struct lpfc_nodelist *fan_ndlp)
已提交
3574 3575 3576 3577 3578
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	IOCB_t *icmd;
	uint32_t cmd, did;
3579 3580
	FAN *fp;
	struct lpfc_nodelist *ndlp, *next_ndlp;
J
James Smart 已提交
3581
	struct lpfc_hba *phba = vport->phba;
3582 3583

	/* FAN received */
3584 3585
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0265 FAN received\n");
已提交
3586 3587
	icmd = &cmdiocb->iocb;
	did = icmd->un.elsreq64.remoteID;
3588 3589
	pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
	lp = (uint32_t *)pcmd->virt;
已提交
3590 3591

	cmd = *lp++;
3592
	fp = (FAN *) lp;
已提交
3593

3594
	/* FAN received; Fan does not have a reply sequence */
已提交
3595

J
James Smart 已提交
3596
	if (phba->pport->port_state == LPFC_LOCAL_CFG_LINK) {
3597 3598 3599 3600 3601 3602 3603
		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
已提交
3604
			 */
3605 3606

			list_for_each_entry_safe(ndlp, next_ndlp,
J
James Smart 已提交
3607
						 &vport->fc_nodes, nlp_listp) {
3608 3609
				if (ndlp->nlp_state != NLP_STE_NPR_NODE)
					continue;
3610 3611 3612 3613 3614
				if (ndlp->nlp_type & NLP_FABRIC) {
					/*
					 * Clean up old Fabric, Nameserver and
					 * other NLP_FABRIC logins
					 */
J
James Smart 已提交
3615
					lpfc_drop_node(vport, ndlp);
3616

3617
				} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
3618 3619 3620
					/* Fail outstanding I/O now since this
					 * device is marked for PLOGI
					 */
J
James Smart 已提交
3621
					lpfc_unreg_rpi(vport, ndlp);
3622 3623 3624
				}
			}

J
James Smart 已提交
3625
			lpfc_initial_flogi(vport);
3626
			return 0;
已提交
3627
		}
3628 3629 3630
		/* Discovery not needed,
		 * move the nodes to their original state.
		 */
J
James Smart 已提交
3631
		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
3632 3633 3634
					 nlp_listp) {
			if (ndlp->nlp_state != NLP_STE_NPR_NODE)
				continue;
已提交
3635

3636 3637 3638
			switch (ndlp->nlp_prev_state) {
			case NLP_STE_UNMAPPED_NODE:
				ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
3639
				lpfc_nlp_set_state(vport, ndlp,
3640
						   NLP_STE_UNMAPPED_NODE);
3641 3642 3643 3644
				break;

			case NLP_STE_MAPPED_NODE:
				ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
J
James Smart 已提交
3645
				lpfc_nlp_set_state(vport, ndlp,
3646
						   NLP_STE_MAPPED_NODE);
3647 3648 3649 3650 3651 3652 3653 3654
				break;

			default:
				break;
			}
		}

		/* Start discovery - this should just do CLEAR_LA */
J
James Smart 已提交
3655
		lpfc_disc_start(vport);
已提交
3656
	}
3657
	return 0;
已提交
3658 3659 3660 3661 3662
}

void
lpfc_els_timeout(unsigned long ptr)
{
J
James Smart 已提交
3663 3664
	struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
	struct lpfc_hba   *phba = vport->phba;
已提交
3665 3666
	unsigned long iflag;

J
James Smart 已提交
3667 3668 3669
	spin_lock_irqsave(&vport->work_port_lock, iflag);
	if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
		vport->work_port_events |= WORKER_ELS_TMO;
3670 3671 3672
		spin_unlock_irqrestore(&vport->work_port_lock, iflag);

		spin_lock_irqsave(&phba->hbalock, iflag);
已提交
3673
		if (phba->work_wait)
3674 3675
			lpfc_worker_wake_up(phba);
		spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
3676
	}
3677 3678
	else
		spin_unlock_irqrestore(&vport->work_port_lock, iflag);
已提交
3679 3680 3681 3682
	return;
}

void
J
James Smart 已提交
3683
lpfc_els_timeout_handler(struct lpfc_vport *vport)
已提交
3684
{
J
James Smart 已提交
3685
	struct lpfc_hba  *phba = vport->phba;
已提交
3686 3687 3688 3689
	struct lpfc_sli_ring *pring;
	struct lpfc_iocbq *tmp_iocb, *piocb;
	IOCB_t *cmd = NULL;
	struct lpfc_dmabuf *pcmd;
J
James Smart 已提交
3690
	uint32_t els_command = 0;
已提交
3691
	uint32_t timeout;
J
James Smart 已提交
3692
	uint32_t remote_ID = 0xffffffff;
已提交
3693 3694

	/* If the timer is already canceled do nothing */
J
James Smart 已提交
3695
	if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
已提交
3696 3697
		return;
	}
J
James Smart 已提交
3698
	spin_lock_irq(&phba->hbalock);
已提交
3699 3700 3701 3702 3703 3704 3705
	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 已提交
3706 3707 3708
		if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
		    piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
		    piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
已提交
3709
			continue;
J
James Smart 已提交
3710 3711 3712 3713

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

已提交
3714
		pcmd = (struct lpfc_dmabuf *) piocb->context2;
J
James Smart 已提交
3715 3716
		if (pcmd)
			els_command = *(uint32_t *) (pcmd->virt);
已提交
3717

3718 3719 3720 3721 3722 3723
		if (els_command == ELS_CMD_FARP ||
		    els_command == ELS_CMD_FARPR ||
		    els_command == ELS_CMD_FDISC)
			continue;

		if (vport != piocb->vport)
已提交
3724 3725 3726
			continue;

		if (piocb->drvrTimeout > 0) {
3727
			if (piocb->drvrTimeout >= timeout)
已提交
3728
				piocb->drvrTimeout -= timeout;
3729
			else
已提交
3730 3731 3732 3733
				piocb->drvrTimeout = 0;
			continue;
		}

J
James Smart 已提交
3734 3735
		remote_ID = 0xffffffff;
		if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
已提交
3736
			remote_ID = cmd->un.elsreq64.remoteID;
J
James Smart 已提交
3737 3738 3739 3740 3741
		else {
			struct lpfc_nodelist *ndlp;
			ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
			if (ndlp)
				remote_ID = ndlp->nlp_DID;
已提交
3742
		}
3743 3744 3745 3746
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0127 ELS timeout Data: x%x x%x x%x "
				 "x%x\n", els_command,
				 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
3747
		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
已提交
3748
	}
J
James Smart 已提交
3749
	spin_unlock_irq(&phba->hbalock);
3750

J
James Smart 已提交
3751 3752
	if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
		mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
已提交
3753 3754 3755
}

void
J
James Smart 已提交
3756
lpfc_els_flush_cmd(struct lpfc_vport *vport)
已提交
3757
{
3758
	LIST_HEAD(completions);
J
James Smart 已提交
3759
	struct lpfc_hba  *phba = vport->phba;
3760
	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
已提交
3761 3762
	struct lpfc_iocbq *tmp_iocb, *piocb;
	IOCB_t *cmd = NULL;
3763 3764

	lpfc_fabric_abort_vport(vport);
已提交
3765

J
James Smart 已提交
3766
	spin_lock_irq(&phba->hbalock);
已提交
3767 3768 3769 3770 3771 3772 3773 3774
	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 */
3775 3776 3777 3778
		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)
已提交
3779 3780
			continue;

J
James Smart 已提交
3781 3782 3783
		if (piocb->vport != vport)
			continue;

3784
		list_move_tail(&piocb->list, &completions);
3785
		pring->txq_cnt--;
已提交
3786 3787 3788 3789 3790 3791 3792
	}

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

J
James Smart 已提交
3793 3794 3795
		if (piocb->vport != vport)
			continue;

3796
		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
已提交
3797
	}
J
James Smart 已提交
3798
	spin_unlock_irq(&phba->hbalock);
3799

J
James Smart 已提交
3800
	while (!list_empty(&completions)) {
3801 3802
		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
		cmd = &piocb->iocb;
3803
		list_del_init(&piocb->list);
3804

J
James Smart 已提交
3805 3806 3807
		if (!piocb->iocb_cmpl)
			lpfc_sli_release_iocbq(phba, piocb);
		else {
3808 3809 3810
			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
			(piocb->iocb_cmpl) (phba, piocb, piocb);
J
James Smart 已提交
3811
		}
3812 3813
	}

已提交
3814 3815 3816
	return;
}

3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860
void
lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
{
	LIST_HEAD(completions);
	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
	struct lpfc_iocbq *tmp_iocb, *piocb;
	IOCB_t *cmd = NULL;

	lpfc_fabric_abort_hba(phba);
	spin_lock_irq(&phba->hbalock);
	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
		cmd = &piocb->iocb;
		if (piocb->iocb_flag & LPFC_IO_LIBDFC)
			continue;
		/* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
		if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
		    cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
		    cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
		    cmd->ulpCommand == CMD_ABORT_XRI_CN)
			continue;
		list_move_tail(&piocb->list, &completions);
		pring->txq_cnt--;
	}
	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
		if (piocb->iocb_flag & LPFC_IO_LIBDFC)
			continue;
		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
	}
	spin_unlock_irq(&phba->hbalock);
	while (!list_empty(&completions)) {
		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
		cmd = &piocb->iocb;
		list_del_init(&piocb->list);
		if (!piocb->iocb_cmpl)
			lpfc_sli_release_iocbq(phba, piocb);
		else {
			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
			(piocb->iocb_cmpl) (phba, piocb, piocb);
		}
	}
	return;
}

3861 3862
static void
lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3863
		      struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
已提交
3864
{
3865
	struct Scsi_Host  *shost;
已提交
3866 3867
	struct lpfc_nodelist *ndlp;
	struct ls_rjt stat;
3868
	uint32_t *payload;
J
James Smart 已提交
3869
	uint32_t cmd, did, newnode, rjt_err = 0;
3870
	IOCB_t *icmd = &elsiocb->iocb;
已提交
3871

3872
	if (vport == NULL || elsiocb->context2 == NULL)
已提交
3873
		goto dropit;
J
James Smart 已提交
3874

已提交
3875
	newnode = 0;
3876 3877
	payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
	cmd = *payload;
3878 3879
	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
		lpfc_post_buffer(phba, pring, 1, 1);
已提交
3880

J
James Smart 已提交
3881 3882 3883 3884 3885
	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);
已提交
3886
		goto dropit;
J
James Smart 已提交
3887
	}
已提交
3888 3889

	/* Check to see if link went down during discovery */
3890
	if (lpfc_els_chk_latt(vport))
已提交
3891 3892
		goto dropit;

3893 3894 3895 3896
	/* Ignore traffic recevied during vport shutdown. */
	if (vport->load_flag & FC_UNLOADING)
		goto dropit;

J
James Smart 已提交
3897
	ndlp = lpfc_findnode_did(vport, did);
3898
	if (!ndlp) {
已提交
3899
		/* Cannot find existing Fabric ndlp, so allocate a new one */
3900
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3901
		if (!ndlp)
已提交
3902 3903
			goto dropit;

J
James Smart 已提交
3904
		lpfc_nlp_init(vport, ndlp, did);
3905
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
3906 3907 3908 3909 3910
		newnode = 1;
		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK) {
			ndlp->nlp_type |= NLP_FABRIC;
		}
	}
3911 3912 3913 3914 3915 3916 3917 3918
	else {
		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
			/* This is simular to the new node path */
			lpfc_nlp_get(ndlp);
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
			newnode = 1;
		}
	}
已提交
3919 3920

	phba->fc_stat.elsRcvFrame++;
3921 3922 3923
	if (elsiocb->context1)
		lpfc_nlp_put(elsiocb->context1);
	elsiocb->context1 = lpfc_nlp_get(ndlp);
J
James Smart 已提交
3924
	elsiocb->vport = vport;
已提交
3925 3926 3927 3928 3929

	if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
		cmd &= ELS_CMD_MASK;
	}
	/* ELS command <elsCmd> received from NPORT <did> */
3930 3931 3932
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0112 ELS command x%x received from NPORT x%x "
			 "Data: x%x\n", cmd, did, vport->port_state);
已提交
3933 3934
	switch (cmd) {
	case ELS_CMD_PLOGI:
J
James Smart 已提交
3935 3936 3937 3938
		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);

已提交
3939
		phba->fc_stat.elsRcvPLOGI++;
J
James Smart 已提交
3940 3941 3942 3943
		ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);

		if (vport->port_state < LPFC_DISC_AUTH) {
			rjt_err = LSRJT_UNABLE_TPC;
已提交
3944 3945
			break;
		}
3946 3947 3948 3949 3950 3951

		shost = lpfc_shost_from_vport(vport);
		spin_lock_irq(shost->host_lock);
		ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
		spin_unlock_irq(shost->host_lock);

J
James Smart 已提交
3952 3953
		lpfc_disc_state_machine(vport, ndlp, elsiocb,
					NLP_EVT_RCV_PLOGI);
J
James Smart 已提交
3954

已提交
3955 3956
		break;
	case ELS_CMD_FLOGI:
J
James Smart 已提交
3957 3958 3959 3960
		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);

已提交
3961
		phba->fc_stat.elsRcvFLOGI++;
3962
		lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
3963
		if (newnode)
3964
			lpfc_nlp_put(ndlp);
已提交
3965 3966
		break;
	case ELS_CMD_LOGO:
J
James Smart 已提交
3967 3968 3969 3970
		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);

已提交
3971
		phba->fc_stat.elsRcvLOGO++;
J
James Smart 已提交
3972
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
3973
			rjt_err = LSRJT_UNABLE_TPC;
已提交
3974 3975
			break;
		}
J
James Smart 已提交
3976
		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
已提交
3977 3978
		break;
	case ELS_CMD_PRLO:
J
James Smart 已提交
3979 3980 3981 3982
		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);

已提交
3983
		phba->fc_stat.elsRcvPRLO++;
J
James Smart 已提交
3984
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
3985
			rjt_err = LSRJT_UNABLE_TPC;
已提交
3986 3987
			break;
		}
J
James Smart 已提交
3988
		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
已提交
3989 3990 3991
		break;
	case ELS_CMD_RSCN:
		phba->fc_stat.elsRcvRSCN++;
3992
		lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
3993
		if (newnode)
3994
			lpfc_nlp_put(ndlp);
已提交
3995 3996
		break;
	case ELS_CMD_ADISC:
J
James Smart 已提交
3997 3998 3999 4000
		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);

已提交
4001
		phba->fc_stat.elsRcvADISC++;
J
James Smart 已提交
4002
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
4003
			rjt_err = LSRJT_UNABLE_TPC;
已提交
4004 4005
			break;
		}
J
James Smart 已提交
4006 4007
		lpfc_disc_state_machine(vport, ndlp, elsiocb,
					NLP_EVT_RCV_ADISC);
已提交
4008 4009
		break;
	case ELS_CMD_PDISC:
J
James Smart 已提交
4010 4011 4012 4013
		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);

已提交
4014
		phba->fc_stat.elsRcvPDISC++;
J
James Smart 已提交
4015
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
4016
			rjt_err = LSRJT_UNABLE_TPC;
已提交
4017 4018
			break;
		}
J
James Smart 已提交
4019 4020
		lpfc_disc_state_machine(vport, ndlp, elsiocb,
					NLP_EVT_RCV_PDISC);
已提交
4021 4022
		break;
	case ELS_CMD_FARPR:
J
James Smart 已提交
4023 4024 4025 4026
		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);

已提交
4027
		phba->fc_stat.elsRcvFARPR++;
J
James Smart 已提交
4028
		lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
已提交
4029 4030
		break;
	case ELS_CMD_FARP:
J
James Smart 已提交
4031 4032 4033 4034
		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);

已提交
4035
		phba->fc_stat.elsRcvFARP++;
J
James Smart 已提交
4036
		lpfc_els_rcv_farp(vport, elsiocb, ndlp);
已提交
4037 4038
		break;
	case ELS_CMD_FAN:
J
James Smart 已提交
4039 4040 4041 4042
		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);

已提交
4043
		phba->fc_stat.elsRcvFAN++;
J
James Smart 已提交
4044
		lpfc_els_rcv_fan(vport, elsiocb, ndlp);
已提交
4045 4046
		break;
	case ELS_CMD_PRLI:
J
James Smart 已提交
4047 4048 4049 4050
		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);

已提交
4051
		phba->fc_stat.elsRcvPRLI++;
J
James Smart 已提交
4052
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
4053
			rjt_err = LSRJT_UNABLE_TPC;
已提交
4054 4055
			break;
		}
J
James Smart 已提交
4056
		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
已提交
4057
		break;
4058
	case ELS_CMD_LIRR:
J
James Smart 已提交
4059 4060 4061 4062
		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);

4063
		phba->fc_stat.elsRcvLIRR++;
J
James Smart 已提交
4064
		lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
4065
		if (newnode)
4066
			lpfc_nlp_put(ndlp);
4067 4068
		break;
	case ELS_CMD_RPS:
J
James Smart 已提交
4069 4070 4071 4072
		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);

4073
		phba->fc_stat.elsRcvRPS++;
J
James Smart 已提交
4074
		lpfc_els_rcv_rps(vport, elsiocb, ndlp);
4075
		if (newnode)
4076
			lpfc_nlp_put(ndlp);
4077 4078
		break;
	case ELS_CMD_RPL:
J
James Smart 已提交
4079 4080 4081 4082
		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);

4083
		phba->fc_stat.elsRcvRPL++;
J
James Smart 已提交
4084
		lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
4085
		if (newnode)
4086
			lpfc_nlp_put(ndlp);
4087
		break;
已提交
4088
	case ELS_CMD_RNID:
J
James Smart 已提交
4089 4090 4091 4092
		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);

已提交
4093
		phba->fc_stat.elsRcvRNID++;
J
James Smart 已提交
4094
		lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
4095
		if (newnode)
4096
			lpfc_nlp_put(ndlp);
已提交
4097 4098
		break;
	default:
J
James Smart 已提交
4099 4100 4101 4102
		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);

已提交
4103
		/* Unsupported ELS command, reject */
J
James Smart 已提交
4104
		rjt_err = LSRJT_INVALID_CMD;
已提交
4105 4106

		/* Unknown ELS command <elsCmd> received from NPORT <did> */
4107 4108 4109
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0115 Unknown ELS command x%x "
				 "received from NPORT x%x\n", cmd, did);
4110
		if (newnode)
4111
			lpfc_nlp_put(ndlp);
已提交
4112 4113 4114 4115 4116
		break;
	}

	/* check if need to LS_RJT received ELS cmd */
	if (rjt_err) {
4117
		memset(&stat, 0, sizeof(stat));
J
James Smart 已提交
4118
		stat.un.b.lsRjtRsnCode = rjt_err;
4119
		stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
J
James Smart 已提交
4120 4121
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
			NULL);
已提交
4122 4123
	}

4124 4125 4126
	return;

dropit:
4127 4128
	if (vport && !(vport->load_flag & FC_UNLOADING))
		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
4129
			"(%d):0111 Dropping received ELS cmd "
4130
			"Data: x%x x%x x%x\n",
4131
			vport->vpi, icmd->ulpStatus,
4132
			icmd->un.ulpWord[4], icmd->ulpTimeout);
4133 4134 4135
	phba->fc_stat.elsRcvDrop++;
}

4136 4137 4138 4139
static struct lpfc_vport *
lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
{
	struct lpfc_vport *vport;
4140
	unsigned long flags;
4141

4142
	spin_lock_irqsave(&phba->hbalock, flags);
4143
	list_for_each_entry(vport, &phba->port_list, listentry) {
4144 4145
		if (vport->vpi == vpi) {
			spin_unlock_irqrestore(&phba->hbalock, flags);
4146
			return vport;
4147
		}
4148
	}
4149
	spin_unlock_irqrestore(&phba->hbalock, flags);
4150 4151
	return NULL;
}
4152 4153 4154 4155 4156 4157 4158 4159

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;
4160 4161 4162 4163 4164
	struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
	struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;

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

4166 4167 4168 4169
	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) {
4170 4171
		phba->fc_stat.NoRcvBuf++;
		/* Not enough posted buffers; Try posting more buffers */
4172
		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
4173 4174 4175 4176
			lpfc_post_buffer(phba, pring, 0, 1);
		return;
	}

4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189
	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.
				 */
4190 4191 4192
	if (icmd->ulpBdeCount == 0)
		return;

4193 4194 4195
				/* type of ELS cmd is first 32bit word
				 * in packet
				 */
4196
	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4197
		elsiocb->context2 = bdeBuf1;
4198 4199 4200
	} else {
		paddr = getPaddr(icmd->un.cont64[0].addrHigh,
				 icmd->un.cont64[0].addrLow);
4201 4202
		elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
							     paddr);
4203 4204
	}

4205 4206 4207 4208 4209
	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.
	 */
4210 4211
	lpfc_nlp_put(elsiocb->context1);
	elsiocb->context1 = NULL;
已提交
4212
	if (elsiocb->context2) {
4213 4214
		lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
		elsiocb->context2 = NULL;
已提交
4215
	}
4216 4217

	/* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
4218
	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
4219
	    icmd->ulpBdeCount == 2) {
4220 4221
		elsiocb->context2 = bdeBuf2;
		lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
4222 4223
		/* free mp if we are done with it */
		if (elsiocb->context2) {
4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243
			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);
4244 4245
			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
					 "0251 NameServer login: no memory\n");
4246 4247 4248 4249 4250 4251 4252 4253 4254 4255
			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);
4256 4257
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0252 Cannot issue NameServer login\n");
4258 4259 4260
		return;
	}

4261
	if (vport->cfg_fdmi_on) {
4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283
		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;

4284
	spin_lock_irq(shost->host_lock);
4285
	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4286
	spin_unlock_irq(shost->host_lock);
4287 4288 4289
	lpfc_nlp_put(ndlp);

	if (mb->mbxStatus) {
4290 4291 4292
		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
				 "0915 Register VPI failed: 0x%x\n",
				 mb->mbxStatus);
4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306

		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);
4307
			spin_lock_irq(shost->host_lock);
4308
			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4309
			spin_unlock_irq(shost->host_lock);
4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323
			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;
}

A
Adrian Bunk 已提交
4324
static void
4325 4326 4327
lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
			struct lpfc_nodelist *ndlp)
{
4328
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4329 4330 4331 4332 4333 4334 4335 4336
	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;
4337
		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4338 4339
		    == MBX_NOT_FINISHED) {
			mempool_free(mbox, phba->mbox_mem_pool);
4340
			spin_lock_irq(shost->host_lock);
4341
			vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4342
			spin_unlock_irq(shost->host_lock);
4343 4344

			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4345 4346
			lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
				"0253 Register VPI: Can't send mbox\n");
4347 4348 4349 4350
		}
	} else {
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);

4351 4352
		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
				 "0254 Register VPI: no memory\n");
4353

4354
		spin_lock_irq(shost->host_lock);
4355
		vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4356
		spin_unlock_irq(shost->host_lock);
4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372
		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;

4373 4374 4375 4376
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
			 irsp->ulpStatus, irsp->un.ulpWord[4],
			 vport->fc_prevDID);
4377 4378 4379 4380 4381 4382 4383 4384
	/* 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 已提交
4385 4386 4387 4388
	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);

4389 4390 4391 4392 4393
	if (irsp->ulpStatus) {
		/* Check for retry */
		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
			goto out;
		/* FDISC failed */
4394 4395 4396
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0124 FDISC failed. (%d/%d)\n",
				 irsp->ulpStatus, irsp->un.ulpWord[4]);
4397 4398 4399 4400 4401 4402 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
		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);
4429
			spin_lock_irq(shost->host_lock);
4430
			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4431
			spin_unlock_irq(shost->host_lock);
4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445
		}

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

A
Adrian Bunk 已提交
4446
static int
4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463
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);
4464 4465
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0255 Issue FDISC: no IOCB\n");
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 4498 4499 4500 4501 4502
		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 已提交
4503 4504 4505 4506
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue FDISC:     did:x%x",
		did, 0, 0);

4507 4508 4509 4510
	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);
4511 4512
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0256 Issue FDISC: Cannot send IOCB\n");
4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524
		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 已提交
4525 4526 4527 4528 4529 4530
	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);
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 4559 4560 4561 4562

	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 已提交
4563 4564 4565 4566
	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);

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 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625
	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 已提交
4626 4627 4628 4629
		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
			"Fabric sched1:   ste:x%x",
			iocb->vport->port_state, 0, 0);

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 4678 4679 4680 4681 4682 4683 4684 4685 4686
		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);
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 4712 4713 4714 4715 4716 4717 4718
			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);
	}
}

A
Adrian Bunk 已提交
4719
static int
4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739
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 已提交
4740 4741 4742 4743
		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
			"Fabric sched2:   ste:x%x",
			iocb->vport->port_state, 0, 0);

4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762
		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;
}


A
Adrian Bunk 已提交
4763
static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805
{
	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);
4806
		}
已提交
4807
	}
4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839
	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);
	}
已提交
4840
}
4841 4842


A
Adrian Bunk 已提交
4843
#if 0
4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873
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);
	}
}
A
Adrian Bunk 已提交
4874
#endif  /*  0  */
4875 4876