lpfc_els.c 137.9 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-2008 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

	if (elsiocb == NULL)
		return NULL;
116

已提交
117 118 119 120
	icmd = &elsiocb->iocb;

	/* fill in BDEs for command */
	/* Allocate buffer for command payload */
121 122 123
	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
	if (pcmd)
		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
124 125
	if (!pcmd || !pcmd->virt)
		goto els_iocb_free_pcmb_exit;
已提交
126 127 128 129 130

	INIT_LIST_HEAD(&pcmd->list);

	/* Allocate buffer for response payload */
	if (expectRsp) {
131
		prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
已提交
132 133 134
		if (prsp)
			prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
						     &prsp->phys);
135 136
		if (!prsp || !prsp->virt)
			goto els_iocb_free_prsp_exit;
已提交
137
		INIT_LIST_HEAD(&prsp->list);
138
	} else
已提交
139 140 141
		prsp = NULL;

	/* Allocate buffer for Buffer ptr list */
142
	pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
已提交
143
	if (pbuflist)
144 145
		pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
						 &pbuflist->phys);
146 147
	if (!pbuflist || !pbuflist->virt)
		goto els_iocb_free_pbuf_exit;
已提交
148 149 150 151 152 153

	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 已提交
154
	icmd->un.elsreq64.remoteID = did;	/* DID */
已提交
155
	if (expectRsp) {
156
		icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
已提交
157
		icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
158
		icmd->ulpTimeout = phba->fc_ratov * 2;
已提交
159
	} else {
160
		icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
已提交
161 162 163 164 165 166
		icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
	}
	icmd->ulpBdeCount = 1;
	icmd->ulpLe = 1;
	icmd->ulpClass = CLASS3;

167 168 169 170 171 172 173 174 175
	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;
	}

已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	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);
	}

192
	/* prevent preparing iocb with NULL ndlp reference */
193
	elsiocb->context1 = lpfc_nlp_get(ndlp);
194 195
	if (!elsiocb->context1)
		goto els_iocb_free_pbuf_exit;
196 197
	elsiocb->context2 = pcmd;
	elsiocb->context3 = pbuflist;
已提交
198
	elsiocb->retry = retry;
J
James Smart 已提交
199
	elsiocb->vport = vport;
已提交
200 201 202 203 204 205 206
	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> */
207 208 209 210 211
		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);
已提交
212 213
	} else {
		/* Xmit ELS response <elsCmd> to remote NPORT <did> */
214 215 216 217 218
		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);
已提交
219
	}
220
	return elsiocb;
已提交
221

222 223 224 225 226 227 228 229 230 231 232 233 234
els_iocb_free_pbuf_exit:
	lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
	kfree(pbuflist);

els_iocb_free_prsp_exit:
	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
	kfree(prsp);

els_iocb_free_pcmb_exit:
	kfree(pcmd);
	lpfc_sli_release_iocbq(phba, elsiocb);
	return NULL;
}
已提交
235 236

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

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

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

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

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

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

	mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
	mbox->vport = vport;
285 286 287
	/* increment the reference count on ndlp to hold reference
	 * for the callback routine.
	 */
288 289
	mbox->context2 = lpfc_nlp_get(ndlp);

290
	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
291 292
	if (rc == MBX_NOT_FINISHED) {
		err = 6;
293
		goto fail_issue_reg_login;
294
	}
295 296 297 298

	return 0;

fail_issue_reg_login:
299 300 301
	/* decrement the reference count on ndlp just incremented
	 * for the failed mbox command.
	 */
302 303 304 305 306 307 308 309 310
	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);
311
	lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
312
		"0249 Cannot issue Register Fabric login: Err %d\n", err);
313 314 315 316 317 318 319 320 321 322 323 324
	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 已提交
325 326 327
	spin_lock_irq(shost->host_lock);
	vport->fc_flag |= FC_FABRIC;
	spin_unlock_irq(shost->host_lock);
已提交
328 329 330 331 332 333 334 335

	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 已提交
336 337 338
		spin_lock_irq(shost->host_lock);
		vport->fc_flag |= FC_PUBLIC_LOOP;
		spin_unlock_irq(shost->host_lock);
已提交
339 340 341 342 343
	} 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 已提交
344
		vport->fc_sparam.cmn.altBbCredit = 1;
已提交
345 346
	}

J
James Smart 已提交
347
	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
已提交
348
	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
349
	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
已提交
350 351 352 353 354 355 356 357 358 359 360 361 362
	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));

363 364
	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
		if (sp->cmn.response_multiple_NPort) {
365 366 367 368 369
			lpfc_printf_vlog(vport, KERN_WARNING,
					 LOG_ELS | LOG_VPORT,
					 "1816 FLOGI NPIV supported, "
					 "response data 0x%x\n",
					 sp->cmn.response_multiple_NPort);
370 371 372
			phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
		} else {
			/* Because we asked f/w for NPIV it still expects us
373 374 375 376 377
			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");
378 379 380
			phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
		}
	}
已提交
381

382 383
	if ((vport->fc_prevDID != vport->fc_myDID) &&
		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
已提交
384

385 386 387 388 389
		/* 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) {
390 391
			if (!NLP_CHK_NODE_ACT(ndlp))
				continue;
392 393 394 395 396 397 398 399 400 401
			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);
402
			spin_lock_irq(shost->host_lock);
403
			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
404
			spin_unlock_irq(shost->host_lock);
405 406
		}
	}
已提交
407

408
	lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
已提交
409

410 411 412 413 414 415
	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);
已提交
416 417 418 419 420 421 422
	return 0;
}

/*
 * We FLOGIed into an NPort, initiate pt2pt protocol
 */
static int
J
James Smart 已提交
423 424
lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
			  struct serv_parm *sp)
已提交
425
{
J
James Smart 已提交
426 427
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
428 429 430
	LPFC_MBOXQ_t *mbox;
	int rc;

J
James Smart 已提交
431 432 433
	spin_lock_irq(shost->host_lock);
	vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
	spin_unlock_irq(shost->host_lock);
已提交
434 435 436

	phba->fc_edtov = FF_DEF_EDTOV;
	phba->fc_ratov = FF_DEF_RATOV;
J
James Smart 已提交
437
	rc = memcmp(&vport->fc_portname, &sp->portName,
438
		    sizeof(vport->fc_portname));
已提交
439 440
	if (rc >= 0) {
		/* This side will initiate the PLOGI */
J
James Smart 已提交
441 442 443
		spin_lock_irq(shost->host_lock);
		vport->fc_flag |= FC_PT2PT_PLOGI;
		spin_unlock_irq(shost->host_lock);
已提交
444 445 446 447 448 449 450 451

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

		/* not equal */
		if (rc)
J
James Smart 已提交
452
			vport->fc_myDID = PT2PT_LocalID;
已提交
453 454 455 456 457 458 459 460

		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;
461
		mbox->vport = vport;
462
		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
已提交
463 464 465 466
		if (rc == MBX_NOT_FINISHED) {
			mempool_free(mbox, phba->mbox_mem_pool);
			goto fail;
		}
467 468 469
		/* Decrement ndlp reference count indicating that ndlp can be
		 * safely released when other references to it are done.
		 */
470
		lpfc_nlp_put(ndlp);
已提交
471

J
James Smart 已提交
472
		ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
已提交
473 474 475 476 477 478 479 480
		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 已提交
481
			lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
482 483 484 485 486
		} else if (!NLP_CHK_NODE_ACT(ndlp)) {
			ndlp = lpfc_enable_node(vport, ndlp,
						NLP_STE_UNUSED_NODE);
			if(!ndlp)
				goto fail;
已提交
487 488 489
		}

		memcpy(&ndlp->nlp_portname, &sp->portName,
J
James Smart 已提交
490
		       sizeof(struct lpfc_name));
已提交
491
		memcpy(&ndlp->nlp_nodename, &sp->nodeName,
J
James Smart 已提交
492
		       sizeof(struct lpfc_name));
493
		/* Set state will put ndlp onto node list if not already done */
J
James Smart 已提交
494 495
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		spin_lock_irq(shost->host_lock);
已提交
496
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
497
		spin_unlock_irq(shost->host_lock);
498 499 500 501 502
	} else
		/* This side will wait for the PLOGI, decrement ndlp reference
		 * count indicating that ndlp can be released when other
		 * references to it are done.
		 */
503
		lpfc_nlp_put(ndlp);
已提交
504

505 506 507
	/* If we are pt2pt with another NPort, force NPIV off! */
	phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;

J
James Smart 已提交
508 509 510
	spin_lock_irq(shost->host_lock);
	vport->fc_flag |= FC_PT2PT;
	spin_unlock_irq(shost->host_lock);
已提交
511 512

	/* Start discovery - this should just do CLEAR_LA */
J
James Smart 已提交
513
	lpfc_disc_start(vport);
已提交
514
	return 0;
515
fail:
已提交
516 517 518 519
	return -ENXIO;
}

static void
520 521
lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		    struct lpfc_iocbq *rspiocb)
已提交
522
{
J
James Smart 已提交
523 524
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
525 526 527 528 529 530 531
	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 已提交
532
	if (lpfc_els_chk_latt(vport)) {
533 534 535
		/* One additional decrement on node reference count to
		 * trigger the release of the node
		 */
536
		lpfc_nlp_put(ndlp);
已提交
537 538 539
		goto out;
	}

J
James Smart 已提交
540 541 542 543 544
	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);

已提交
545 546
	if (irsp->ulpStatus) {
		/* Check for retry */
J
James Smart 已提交
547
		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
已提交
548
			goto out;
J
James Smart 已提交
549

已提交
550
		/* FLOGI failed, so there is no fabric */
J
James Smart 已提交
551 552 553
		spin_lock_irq(shost->host_lock);
		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
		spin_unlock_irq(shost->host_lock);
已提交
554

555
		/* If private loop, then allow max outstanding els to be
已提交
556 557 558 559
		 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
		 * alpa map would take too long otherwise.
		 */
		if (phba->alpa_map[0] == 0) {
560
			vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
已提交
561 562 563
		}

		/* FLOGI failure */
564 565 566 567 568
		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);
已提交
569 570 571 572 573 574 575 576 577 578 579 580
		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 */
581 582 583 584 585
	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);
已提交
586

J
James Smart 已提交
587
	if (vport->port_state == LPFC_FLOGI) {
已提交
588 589 590 591 592
		/*
		 * If Common Service Parameters indicate Nport
		 * we are point to point, if Fport we are Fabric.
		 */
		if (sp->cmn.fPort)
J
James Smart 已提交
593
			rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
已提交
594
		else
J
James Smart 已提交
595
			rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
已提交
596 597 598 599 600 601

		if (!rc)
			goto out;
	}

flogifail:
602
	lpfc_nlp_put(ndlp);
已提交
603

J
James Smart 已提交
604
	if (!lpfc_error_lost_link(irsp)) {
已提交
605
		/* FLOGI failed, so just use loop map to make discovery list */
J
James Smart 已提交
606
		lpfc_disc_list_loopmap(vport);
已提交
607 608

		/* Start discovery */
J
James Smart 已提交
609
		lpfc_disc_start(vport);
610 611 612 613 614 615
	} 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);
已提交
616 617 618 619 620 621
	}
out:
	lpfc_els_free_iocb(phba, cmdiocb);
}

static int
J
James Smart 已提交
622
lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
623 624
		     uint8_t retry)
{
J
James Smart 已提交
625
	struct lpfc_hba  *phba = vport->phba;
已提交
626 627 628 629 630 631 632 633 634 635 636
	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];

637
	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
J
James Smart 已提交
638 639
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_FLOGI);
640

641
	if (!elsiocb)
642
		return 1;
已提交
643 644 645 646 647 648

	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;
649 650
	pcmd += sizeof(uint32_t);
	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
已提交
651 652 653 654 655 656 657 658 659 660 661 662 663
	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;

664 665 666 667 668 669 670 671
	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 已提交
672 673 674 675 676
	if (phba->fc_topology != TOPOLOGY_LOOP) {
		icmd->un.elsreq64.myID = 0;
		icmd->un.elsreq64.fl = 1;
	}

已提交
677 678
	tmo = phba->fc_ratov;
	phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
J
James Smart 已提交
679
	lpfc_set_disctmo(vport);
已提交
680 681 682 683
	phba->fc_ratov = tmo;

	phba->fc_stat.elsXmitFLOGI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
J
James Smart 已提交
684 685 686 687 688

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

689
	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
已提交
690 691
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
692
		return 1;
已提交
693
	}
694
	return 0;
已提交
695 696 697
}

int
J
James Smart 已提交
698
lpfc_els_abort_flogi(struct lpfc_hba *phba)
已提交
699 700 701 702 703 704 705 706
{
	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,
707 708
			"0201 Abort outstanding I/O on NPort x%x\n",
			Fabric_DID);
已提交
709 710 711 712 713 714 715

	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 已提交
716
	spin_lock_irq(&phba->hbalock);
已提交
717 718
	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
		icmd = &iocb->iocb;
J
James Smart 已提交
719 720
		if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
		    icmd->un.elsreq64.bdl.ulpIoTag32) {
已提交
721
			ndlp = (struct lpfc_nodelist *)(iocb->context1);
722 723
			if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
			    (ndlp->nlp_DID == Fabric_DID))
724
				lpfc_sli_issue_abort_iotag(phba, pring, iocb);
已提交
725 726
		}
	}
J
James Smart 已提交
727
	spin_unlock_irq(&phba->hbalock);
已提交
728 729 730 731 732

	return 0;
}

int
J
James Smart 已提交
733
lpfc_initial_flogi(struct lpfc_vport *vport)
已提交
734
{
J
James Smart 已提交
735
	struct lpfc_hba *phba = vport->phba;
已提交
736 737
	struct lpfc_nodelist *ndlp;

738 739 740
	vport->port_state = LPFC_FLOGI;
	lpfc_set_disctmo(vport);

741
	/* First look for the Fabric ndlp */
J
James Smart 已提交
742
	ndlp = lpfc_findnode_did(vport, Fabric_DID);
743
	if (!ndlp) {
已提交
744
		/* Cannot find existing Fabric ndlp, so allocate a new one */
745 746 747
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp)
			return 0;
J
James Smart 已提交
748
		lpfc_nlp_init(vport, ndlp, Fabric_DID);
749 750 751 752 753 754 755
		/* Put ndlp onto node list */
		lpfc_enqueue_node(vport, ndlp);
	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
		/* re-setup ndlp without removing from node list */
		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
		if (!ndlp)
			return 0;
已提交
756
	}
757

758
	if (lpfc_issue_els_flogi(vport, ndlp, 0))
759 760 761
		/* This decrement of reference count to node shall kick off
		 * the release of the node.
		 */
762
		lpfc_nlp_put(ndlp);
763

764
	return 1;
已提交
765 766
}

767 768 769 770 771 772 773 774 775 776 777 778 779 780
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);
781 782 783 784 785 786 787
		/* Put ndlp onto node list */
		lpfc_enqueue_node(vport, ndlp);
	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
		/* re-setup ndlp without removing from node list */
		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
		if (!ndlp)
			return 0;
788
	}
789

790
	if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
791 792 793
		/* decrement node reference count to trigger the release of
		 * the node.
		 */
794
		lpfc_nlp_put(ndlp);
795
		return 0;
796 797 798
	}
	return 1;
}
799 800

void
J
James Smart 已提交
801
lpfc_more_plogi(struct lpfc_vport *vport)
已提交
802 803 804
{
	int sentplogi;

J
James Smart 已提交
805 806
	if (vport->num_disc_nodes)
		vport->num_disc_nodes--;
已提交
807 808

	/* Continue discovery with <num_disc_nodes> PLOGIs to go */
809 810 811 812 813
	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);
已提交
814
	/* Check to see if there are more PLOGIs to be sent */
J
James Smart 已提交
815 816 817 818
	if (vport->fc_flag & FC_NLP_MORE)
		/* go thru NPR nodes and issue any remaining ELS PLOGIs */
		sentplogi = lpfc_els_disc_plogi(vport);

已提交
819 820 821
	return;
}

822
static struct lpfc_nodelist *
823
lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
824 825
			 struct lpfc_nodelist *ndlp)
{
J
James Smart 已提交
826
	struct lpfc_vport    *vport = ndlp->vport;
827
	struct lpfc_nodelist *new_ndlp;
828 829
	struct lpfc_rport_data *rdata;
	struct fc_rport *rport;
830
	struct serv_parm *sp;
831
	uint8_t  name[sizeof(struct lpfc_name)];
832
	uint32_t rc, keepDID = 0;
833

J
James Smart 已提交
834 835 836 837 838 839
	/* 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;

840
	sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
841
	memset(name, 0, sizeof(struct lpfc_name));
842

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

848
	if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
849 850 851
		return ndlp;

	if (!new_ndlp) {
J
James Smart 已提交
852 853
		rc = memcmp(&ndlp->nlp_portname, name,
			    sizeof(struct lpfc_name));
854 855
		if (!rc)
			return ndlp;
856 857 858
		new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
		if (!new_ndlp)
			return ndlp;
J
James Smart 已提交
859
		lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
860
	} else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
861 862 863 864
		rc = memcmp(&ndlp->nlp_portname, name,
			    sizeof(struct lpfc_name));
		if (!rc)
			return ndlp;
865 866 867 868
		new_ndlp = lpfc_enable_node(vport, new_ndlp,
						NLP_STE_UNUSED_NODE);
		if (!new_ndlp)
			return ndlp;
869 870 871
		keepDID = new_ndlp->nlp_DID;
	} else
		keepDID = new_ndlp->nlp_DID;
872

J
James Smart 已提交
873
	lpfc_unreg_rpi(vport, new_ndlp);
874
	new_ndlp->nlp_DID = ndlp->nlp_DID;
875
	new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
876 877 878 879 880

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

881
	/* Set state will put new_ndlp on to node list if not already done */
J
James Smart 已提交
882
	lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
883

J
James Smart 已提交
884
	/* Move this back to NPR state */
885 886 887 888
	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.
		 */
889 890 891 892 893 894 895 896 897 898 899 900 901

		/* 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;
		}
902 903 904 905 906 907 908 909 910
		/* We shall actually free the ndlp with both nlp_DID and
		 * nlp_portname fields equals 0 to avoid any ndlp on the
		 * nodelist never to be used.
		 */
		if (ndlp->nlp_DID == 0) {
			spin_lock_irq(&phba->ndlp_lock);
			NLP_SET_FREE_REQ(ndlp);
			spin_unlock_irq(&phba->ndlp_lock);
		}
911

912 913
		/* Two ndlps cannot have the same did on the nodelist */
		ndlp->nlp_DID = keepDID;
J
James Smart 已提交
914
		lpfc_drop_node(vport, ndlp);
915
	}
916
	else {
J
James Smart 已提交
917
		lpfc_unreg_rpi(vport, ndlp);
918 919
		/* Two ndlps cannot have the same did */
		ndlp->nlp_DID = keepDID;
J
James Smart 已提交
920
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
921
	}
922 923 924
	return new_ndlp;
}

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
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);
		}
	}
}

已提交
946
static void
J
James Smart 已提交
947 948
lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		    struct lpfc_iocbq *rspiocb)
已提交
949
{
J
James Smart 已提交
950 951
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
952 953
	IOCB_t *irsp;
	struct lpfc_nodelist *ndlp;
954
	struct lpfc_dmabuf *prsp;
已提交
955 956 957 958 959 960
	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 已提交
961 962 963 964 965
	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 已提交
966
	ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
967
	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
968 969 970 971 972 973
		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);
974
		goto out;
975
	}
已提交
976 977 978 979

	/* Since ndlp can be freed in the disc state machine, note if this node
	 * is being used during discovery.
	 */
J
James Smart 已提交
980
	spin_lock_irq(shost->host_lock);
已提交
981
	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
982
	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
J
James Smart 已提交
983
	spin_unlock_irq(shost->host_lock);
已提交
984 985 986
	rc   = 0;

	/* PLOGI completes to NPort <nlp_DID> */
987 988 989 990 991
	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);
已提交
992
	/* Check to see if link went down during discovery */
J
James Smart 已提交
993 994
	if (lpfc_els_chk_latt(vport)) {
		spin_lock_irq(shost->host_lock);
已提交
995
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
996
		spin_unlock_irq(shost->host_lock);
已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
		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 已提交
1009
				spin_lock_irq(shost->host_lock);
已提交
1010
				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
1011
				spin_unlock_irq(shost->host_lock);
已提交
1012 1013 1014 1015 1016
			}
			goto out;
		}
		/* PLOGI failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1017
		if (lpfc_error_lost_link(irsp))
1018
			rc = NLP_STE_FREED_NODE;
1019
		else
J
James Smart 已提交
1020
			rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1021
						     NLP_EVT_CMPL_PLOGI);
已提交
1022 1023
	} else {
		/* Good status, call state machine */
1024
		prsp = list_entry(((struct lpfc_dmabuf *)
1025 1026 1027
				   cmdiocb->context2)->list.next,
				  struct lpfc_dmabuf, list);
		ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
J
James Smart 已提交
1028
		rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1029
					     NLP_EVT_CMPL_PLOGI);
已提交
1030 1031
	}

J
James Smart 已提交
1032
	if (disc && vport->num_disc_nodes) {
已提交
1033
		/* Check to see if there are more PLOGIs to be sent */
J
James Smart 已提交
1034
		lpfc_more_plogi(vport);
已提交
1035

J
James Smart 已提交
1036 1037 1038 1039
		if (vport->num_disc_nodes == 0) {
			spin_lock_irq(shost->host_lock);
			vport->fc_flag &= ~FC_NDISC_ACTIVE;
			spin_unlock_irq(shost->host_lock);
已提交
1040

J
James Smart 已提交
1041
			lpfc_can_disctmo(vport);
1042
			lpfc_end_rscn(vport);
已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051
		}
	}

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

int
J
James Smart 已提交
1052
lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
已提交
1053
{
J
James Smart 已提交
1054
	struct lpfc_hba  *phba = vport->phba;
已提交
1055 1056
	struct serv_parm *sp;
	IOCB_t *icmd;
1057
	struct lpfc_nodelist *ndlp;
已提交
1058 1059 1060 1061 1062
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;
1063
	int ret;
已提交
1064 1065 1066 1067

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

1068
	ndlp = lpfc_findnode_did(vport, did);
1069 1070
	if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
		ndlp = NULL;
1071

1072
	/* If ndlp is not NULL, we will bump the reference count on it */
1073
	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1074
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
J
James Smart 已提交
1075
				     ELS_CMD_PLOGI);
1076 1077
	if (!elsiocb)
		return 1;
已提交
1078 1079 1080 1081 1082 1083

	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;
1084 1085
	pcmd += sizeof(uint32_t);
	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
已提交
1086 1087 1088 1089 1090 1091 1092 1093
	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 已提交
1094 1095 1096 1097
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue PLOGI:     did:x%x",
		did, 0, 0);

已提交
1098 1099
	phba->fc_stat.elsXmitPLOGI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1100 1101 1102
	ret = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);

	if (ret == IOCB_ERROR) {
已提交
1103
		lpfc_els_free_iocb(phba, elsiocb);
1104
		return 1;
已提交
1105
	}
1106
	return 0;
已提交
1107 1108 1109
}

static void
J
James Smart 已提交
1110 1111
lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		   struct lpfc_iocbq *rspiocb)
已提交
1112
{
J
James Smart 已提交
1113 1114
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	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 已提交
1125
	spin_lock_irq(shost->host_lock);
已提交
1126
	ndlp->nlp_flag &= ~NLP_PRLI_SND;
J
James Smart 已提交
1127
	spin_unlock_irq(shost->host_lock);
已提交
1128

J
James Smart 已提交
1129 1130 1131 1132
	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);
已提交
1133
	/* PRLI completes to NPort <nlp_DID> */
1134 1135 1136 1137 1138
	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);
已提交
1139

J
James Smart 已提交
1140
	vport->fc_prli_sent--;
已提交
1141
	/* Check to see if link went down during discovery */
J
James Smart 已提交
1142
	if (lpfc_els_chk_latt(vport))
已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
		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 */
1153
		if (lpfc_error_lost_link(irsp))
已提交
1154
			goto out;
1155
		else
J
James Smart 已提交
1156
			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1157
						NLP_EVT_CMPL_PRLI);
1158
	} else
已提交
1159
		/* Good status, call state machine */
J
James Smart 已提交
1160
		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1161
					NLP_EVT_CMPL_PRLI);
已提交
1162 1163 1164 1165 1166 1167
out:
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
J
James Smart 已提交
1168
lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1169 1170
		    uint8_t retry)
{
J
James Smart 已提交
1171 1172
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba *phba = vport->phba;
已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
	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 */

1184
	cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
J
James Smart 已提交
1185 1186
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_PRLI);
1187
	if (!elsiocb)
1188
		return 1;
已提交
1189 1190 1191 1192 1193

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

	/* For PRLI request, remainder of payload is service parameters */
1194
	memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
已提交
1195
	*((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1196
	pcmd += sizeof(uint32_t);
已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215

	/* 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 已提交
1216 1217 1218 1219
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue PRLI:      did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1220 1221
	phba->fc_stat.elsXmitPRLI++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
J
James Smart 已提交
1222
	spin_lock_irq(shost->host_lock);
已提交
1223
	ndlp->nlp_flag |= NLP_PRLI_SND;
J
James Smart 已提交
1224
	spin_unlock_irq(shost->host_lock);
已提交
1225
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
J
James Smart 已提交
1226
		spin_lock_irq(shost->host_lock);
已提交
1227
		ndlp->nlp_flag &= ~NLP_PRLI_SND;
J
James Smart 已提交
1228
		spin_unlock_irq(shost->host_lock);
已提交
1229
		lpfc_els_free_iocb(phba, elsiocb);
1230
		return 1;
已提交
1231
	}
J
James Smart 已提交
1232
	vport->fc_prli_sent++;
1233
	return 0;
已提交
1234 1235
}

1236
void
J
James Smart 已提交
1237
lpfc_more_adisc(struct lpfc_vport *vport)
已提交
1238 1239 1240
{
	int sentadisc;

J
James Smart 已提交
1241 1242
	if (vport->num_disc_nodes)
		vport->num_disc_nodes--;
已提交
1243
	/* Continue discovery with <num_disc_nodes> ADISCs to go */
1244 1245 1246 1247 1248
	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);
已提交
1249
	/* Check to see if there are more ADISCs to be sent */
J
James Smart 已提交
1250 1251 1252 1253
	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);
已提交
1254 1255 1256 1257 1258
	}
	return;
}

static void
J
James Smart 已提交
1259
lpfc_rscn_disc(struct lpfc_vport *vport)
已提交
1260
{
J
James Smart 已提交
1261 1262
	lpfc_can_disctmo(vport);

已提交
1263
	/* RSCN discovery */
J
James Smart 已提交
1264 1265 1266
	/* go thru NPR nodes and issue ELS PLOGIs */
	if (vport->fc_npr_cnt)
		if (lpfc_els_disc_plogi(vport))
已提交
1267
			return;
J
James Smart 已提交
1268

1269
	lpfc_end_rscn(vport);
已提交
1270 1271 1272
}

static void
J
James Smart 已提交
1273 1274
lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		    struct lpfc_iocbq *rspiocb)
已提交
1275
{
J
James Smart 已提交
1276 1277
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
1278 1279
	IOCB_t *irsp;
	struct lpfc_nodelist *ndlp;
J
James Smart 已提交
1280
	int  disc;
已提交
1281 1282 1283 1284 1285 1286 1287

	/* 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 已提交
1288 1289 1290 1291 1292
	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);

已提交
1293 1294 1295
	/* Since ndlp can be freed in the disc state machine, note if this node
	 * is being used during discovery.
	 */
J
James Smart 已提交
1296
	spin_lock_irq(shost->host_lock);
已提交
1297
	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1298
	ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
J
James Smart 已提交
1299
	spin_unlock_irq(shost->host_lock);
已提交
1300
	/* ADISC completes to NPort <nlp_DID> */
1301 1302 1303 1304 1305
	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);
已提交
1306
	/* Check to see if link went down during discovery */
J
James Smart 已提交
1307 1308
	if (lpfc_els_chk_latt(vport)) {
		spin_lock_irq(shost->host_lock);
已提交
1309
		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
1310
		spin_unlock_irq(shost->host_lock);
已提交
1311 1312 1313 1314 1315 1316 1317 1318
		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 已提交
1319
				spin_lock_irq(shost->host_lock);
已提交
1320
				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
J
James Smart 已提交
1321 1322
				spin_unlock_irq(shost->host_lock);
				lpfc_set_disctmo(vport);
已提交
1323 1324 1325 1326 1327
			}
			goto out;
		}
		/* ADISC failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1328
		if (!lpfc_error_lost_link(irsp))
J
James Smart 已提交
1329
			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
J
James Smart 已提交
1330
						NLP_EVT_CMPL_ADISC);
1331
	} else
已提交
1332
		/* Good status, call state machine */
J
James Smart 已提交
1333
		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
已提交
1334 1335
					NLP_EVT_CMPL_ADISC);

J
James Smart 已提交
1336
	if (disc && vport->num_disc_nodes) {
已提交
1337
		/* Check to see if there are more ADISCs to be sent */
J
James Smart 已提交
1338
		lpfc_more_adisc(vport);
已提交
1339 1340

		/* Check to see if we are done with ADISC authentication */
J
James Smart 已提交
1341
		if (vport->num_disc_nodes == 0) {
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
			/* 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 */
1358
				if (vport->port_type == LPFC_PHYSICAL_PORT)
J
James Smart 已提交
1359
					lpfc_issue_clear_la(phba, vport);
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376

				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;
已提交
1377
			} else {
J
James Smart 已提交
1378
				lpfc_rscn_disc(vport);
已提交
1379 1380 1381 1382 1383 1384 1385 1386 1387
			}
		}
	}
out:
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
J
James Smart 已提交
1388
lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1389 1390
		     uint8_t retry)
{
J
James Smart 已提交
1391 1392
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
1393 1394 1395
	ADISC *ap;
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
J
James Smart 已提交
1396 1397
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
已提交
1398 1399 1400
	uint8_t *pcmd;
	uint16_t cmdsize;

1401
	cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
J
James Smart 已提交
1402 1403
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ADISC);
1404
	if (!elsiocb)
1405
		return 1;
已提交
1406 1407 1408 1409 1410 1411

	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;
1412
	pcmd += sizeof(uint32_t);
已提交
1413 1414 1415 1416

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

J
James Smart 已提交
1421 1422 1423 1424
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue ADISC:     did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1425 1426
	phba->fc_stat.elsXmitADISC++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
J
James Smart 已提交
1427
	spin_lock_irq(shost->host_lock);
已提交
1428
	ndlp->nlp_flag |= NLP_ADISC_SND;
J
James Smart 已提交
1429
	spin_unlock_irq(shost->host_lock);
已提交
1430
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
J
James Smart 已提交
1431
		spin_lock_irq(shost->host_lock);
已提交
1432
		ndlp->nlp_flag &= ~NLP_ADISC_SND;
J
James Smart 已提交
1433
		spin_unlock_irq(shost->host_lock);
已提交
1434
		lpfc_els_free_iocb(phba, elsiocb);
1435
		return 1;
已提交
1436
	}
1437
	return 0;
已提交
1438 1439 1440
}

static void
J
James Smart 已提交
1441 1442
lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		   struct lpfc_iocbq *rspiocb)
已提交
1443
{
J
James Smart 已提交
1444 1445 1446
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	struct lpfc_vport *vport = ndlp->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
已提交
1447 1448 1449 1450 1451 1452 1453 1454
	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 已提交
1455
	spin_lock_irq(shost->host_lock);
已提交
1456
	ndlp->nlp_flag &= ~NLP_LOGO_SND;
J
James Smart 已提交
1457
	spin_unlock_irq(shost->host_lock);
已提交
1458

J
James Smart 已提交
1459 1460 1461 1462
	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);
已提交
1463
	/* LOGO completes to NPort <nlp_DID> */
1464 1465 1466 1467 1468
	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);
已提交
1469
	/* Check to see if link went down during discovery */
J
James Smart 已提交
1470
	if (lpfc_els_chk_latt(vport))
已提交
1471 1472
		goto out;

1473 1474 1475 1476 1477 1478 1479 1480 1481
	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;
	}

已提交
1482 1483
	if (irsp->ulpStatus) {
		/* Check for retry */
J
James Smart 已提交
1484
		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
已提交
1485 1486 1487 1488
			/* ELS command is being retried */
			goto out;
		/* LOGO failed */
		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
J
James Smart 已提交
1489
		if (lpfc_error_lost_link(irsp))
已提交
1490
			goto out;
J
James Smart 已提交
1491
		else
J
James Smart 已提交
1492
			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1493
						NLP_EVT_CMPL_LOGO);
1494
	} else
1495 1496 1497
		/* Good status, call state machine.
		 * This will unregister the rpi if needed.
		 */
J
James Smart 已提交
1498
		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1499
					NLP_EVT_CMPL_LOGO);
已提交
1500 1501 1502 1503 1504 1505
out:
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
J
James Smart 已提交
1506
lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
已提交
1507 1508
		    uint8_t retry)
{
J
James Smart 已提交
1509 1510
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
1511 1512 1513 1514 1515 1516
	IOCB_t *icmd;
	struct lpfc_iocbq *elsiocb;
	struct lpfc_sli_ring *pring;
	struct lpfc_sli *psli;
	uint8_t *pcmd;
	uint16_t cmdsize;
1517
	int rc;
已提交
1518 1519 1520 1521

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

1522 1523 1524 1525 1526 1527 1528
	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);

1529
	cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
J
James Smart 已提交
1530 1531
	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_LOGO);
1532
	if (!elsiocb)
1533
		return 1;
已提交
1534 1535 1536 1537

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

	/* Fill in LOGO payload */
J
James Smart 已提交
1541
	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
1542 1543
	pcmd += sizeof(uint32_t);
	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
已提交
1544

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

已提交
1549 1550
	phba->fc_stat.elsXmitLOGO++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
J
James Smart 已提交
1551
	spin_lock_irq(shost->host_lock);
已提交
1552
	ndlp->nlp_flag |= NLP_LOGO_SND;
J
James Smart 已提交
1553
	spin_unlock_irq(shost->host_lock);
1554 1555 1556
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);

	if (rc == IOCB_ERROR) {
J
James Smart 已提交
1557
		spin_lock_irq(shost->host_lock);
已提交
1558
		ndlp->nlp_flag &= ~NLP_LOGO_SND;
J
James Smart 已提交
1559
		spin_unlock_irq(shost->host_lock);
已提交
1560
		lpfc_els_free_iocb(phba, elsiocb);
1561
		return 1;
已提交
1562
	}
1563
	return 0;
已提交
1564 1565 1566
}

static void
J
James Smart 已提交
1567 1568
lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_iocbq *rspiocb)
已提交
1569
{
J
James Smart 已提交
1570
	struct lpfc_vport *vport = cmdiocb->vport;
已提交
1571 1572 1573 1574
	IOCB_t *irsp;

	irsp = &rspiocb->iocb;

J
James Smart 已提交
1575 1576 1577 1578
	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);
已提交
1579
	/* ELS cmd tag <ulpIoTag> completes */
1580 1581 1582 1583
	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);
已提交
1584
	/* Check to see if link went down during discovery */
J
James Smart 已提交
1585
	lpfc_els_chk_latt(vport);
已提交
1586 1587 1588 1589 1590
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
J
James Smart 已提交
1591
lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
已提交
1592
{
J
James Smart 已提交
1593
	struct lpfc_hba  *phba = vport->phba;
已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
	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 */
1604
	cmdsize = (sizeof(uint32_t) + sizeof(SCR));
已提交
1605

1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
	ndlp = lpfc_findnode_did(vport, nportid);
	if (!ndlp) {
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp)
			return 1;
		lpfc_nlp_init(vport, ndlp, nportid);
		lpfc_enqueue_node(vport, ndlp);
	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
		if (!ndlp)
			return 1;
	}
J
James Smart 已提交
1618 1619 1620

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

1622
	if (!elsiocb) {
1623 1624 1625
		/* This will trigger the release of the node just
		 * allocated
		 */
1626
		lpfc_nlp_put(ndlp);
1627
		return 1;
已提交
1628 1629 1630 1631 1632 1633
	}

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

	*((uint32_t *) (pcmd)) = ELS_CMD_SCR;
1634
	pcmd += sizeof(uint32_t);
已提交
1635 1636

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

J
James Smart 已提交
1640 1641 1642 1643
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue SCR:       did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1644 1645 1646
	phba->fc_stat.elsXmitSCR++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1647 1648 1649 1650
		/* The additional lpfc_nlp_put will cause the following
		 * lpfc_els_free_iocb routine to trigger the rlease of
		 * the node.
		 */
1651
		lpfc_nlp_put(ndlp);
已提交
1652
		lpfc_els_free_iocb(phba, elsiocb);
1653
		return 1;
已提交
1654
	}
1655 1656 1657
	/* This will cause the callback-function lpfc_cmpl_els_cmd to
	 * trigger the release of node.
	 */
1658
	lpfc_nlp_put(ndlp);
1659
	return 0;
已提交
1660 1661 1662
}

static int
J
James Smart 已提交
1663
lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
已提交
1664
{
J
James Smart 已提交
1665
	struct lpfc_hba  *phba = vport->phba;
已提交
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
	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 */
1679
	cmdsize = (sizeof(uint32_t) + sizeof(FARP));
已提交
1680

1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
	ndlp = lpfc_findnode_did(vport, nportid);
	if (!ndlp) {
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
		if (!ndlp)
			return 1;
		lpfc_nlp_init(vport, ndlp, nportid);
		lpfc_enqueue_node(vport, ndlp);
	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
		if (!ndlp)
			return 1;
	}
J
James Smart 已提交
1693 1694 1695

	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_RNID);
1696
	if (!elsiocb) {
1697 1698 1699
		/* This will trigger the release of the node just
		 * allocated
		 */
1700
		lpfc_nlp_put(ndlp);
1701
		return 1;
已提交
1702 1703 1704 1705 1706 1707
	}

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

	*((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
1708
	pcmd += sizeof(uint32_t);
已提交
1709 1710 1711

	/* Fill in FARPR payload */
	fp = (FARP *) (pcmd);
1712
	memset(fp, 0, sizeof(FARP));
已提交
1713 1714
	lp = (uint32_t *) pcmd;
	*lp++ = be32_to_cpu(nportid);
J
James Smart 已提交
1715
	*lp++ = be32_to_cpu(vport->fc_myDID);
已提交
1716 1717 1718
	fp->Rflags = 0;
	fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);

1719 1720
	memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
	memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
J
James Smart 已提交
1721
	ondlp = lpfc_findnode_did(vport, nportid);
1722
	if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
已提交
1723
		memcpy(&fp->OportName, &ondlp->nlp_portname,
1724
		       sizeof(struct lpfc_name));
已提交
1725
		memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
1726
		       sizeof(struct lpfc_name));
已提交
1727 1728
	}

J
James Smart 已提交
1729 1730 1731 1732
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue FARPR:     did:x%x",
		ndlp->nlp_DID, 0, 0);

已提交
1733 1734 1735
	phba->fc_stat.elsXmitFARPR++;
	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1736 1737 1738 1739
		/* The additional lpfc_nlp_put will cause the following
		 * lpfc_els_free_iocb routine to trigger the release of
		 * the node.
		 */
1740
		lpfc_nlp_put(ndlp);
已提交
1741
		lpfc_els_free_iocb(phba, elsiocb);
1742
		return 1;
已提交
1743
	}
1744 1745 1746
	/* This will cause the callback-function lpfc_cmpl_els_cmd to
	 * trigger the release of the node.
	 */
1747
	lpfc_nlp_put(ndlp);
1748
	return 0;
已提交
1749 1750
}

1751
void
J
James Smart 已提交
1752
lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
1753
{
J
James Smart 已提交
1754
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1755
	struct lpfc_work_evt *evtp;
J
James Smart 已提交
1756

1757 1758
	if (!(nlp->nlp_flag & NLP_DELAY_TMO))
		return;
J
James Smart 已提交
1759
	spin_lock_irq(shost->host_lock);
1760
	nlp->nlp_flag &= ~NLP_DELAY_TMO;
J
James Smart 已提交
1761
	spin_unlock_irq(shost->host_lock);
1762 1763
	del_timer_sync(&nlp->nlp_delayfunc);
	nlp->nlp_last_elscmd = 0;
1764
	if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
1765
		list_del_init(&nlp->els_retry_evt.evt_listp);
1766 1767 1768 1769
		/* Decrement nlp reference count held for the delayed retry */
		evtp = &nlp->els_retry_evt;
		lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
	}
1770
	if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
J
James Smart 已提交
1771
		spin_lock_irq(shost->host_lock);
1772
		nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
J
James Smart 已提交
1773 1774
		spin_unlock_irq(shost->host_lock);
		if (vport->num_disc_nodes) {
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
			if (vport->port_state < LPFC_VPORT_READY) {
				/* Check if there are more ADISCs to be sent */
				lpfc_more_adisc(vport);
				if ((vport->num_disc_nodes == 0) &&
				    (vport->fc_npr_cnt))
					lpfc_els_disc_plogi(vport);
			} else {
				/* Check if there are more PLOGIs to be sent */
				lpfc_more_plogi(vport);
			}
J
James Smart 已提交
1785 1786 1787 1788 1789
			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);
1790
				lpfc_end_rscn(vport);
1791 1792 1793 1794 1795 1796
			}
		}
	}
	return;
}

已提交
1797 1798 1799
void
lpfc_els_retry_delay(unsigned long ptr)
{
J
James Smart 已提交
1800 1801 1802
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
	struct lpfc_vport *vport = ndlp->vport;
	struct lpfc_hba   *phba = vport->phba;
1803
	unsigned long flags;
J
James Smart 已提交
1804
	struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
已提交
1805

1806
	spin_lock_irqsave(&phba->hbalock, flags);
已提交
1807
	if (!list_empty(&evtp->evt_listp)) {
1808
		spin_unlock_irqrestore(&phba->hbalock, flags);
已提交
1809 1810 1811
		return;
	}

1812 1813 1814 1815
	/* We need to hold the node by incrementing the reference
	 * count until the queued work is done
	 */
	evtp->evt_arg1  = lpfc_nlp_get(ndlp);
1816 1817 1818
	if (evtp->evt_arg1) {
		evtp->evt = LPFC_EVT_ELS_RETRY;
		list_add_tail(&evtp->evt_listp, &phba->work_list);
1819
		lpfc_worker_wake_up(phba);
1820
	}
1821
	spin_unlock_irqrestore(&phba->hbalock, flags);
已提交
1822 1823 1824 1825 1826 1827
	return;
}

void
lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
{
J
James Smart 已提交
1828 1829 1830
	struct lpfc_vport *vport = ndlp->vport;
	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
	uint32_t cmd, did, retry;
已提交
1831

J
James Smart 已提交
1832
	spin_lock_irq(shost->host_lock);
1833 1834 1835
	did = ndlp->nlp_DID;
	cmd = ndlp->nlp_last_elscmd;
	ndlp->nlp_last_elscmd = 0;
已提交
1836 1837

	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
J
James Smart 已提交
1838
		spin_unlock_irq(shost->host_lock);
已提交
1839 1840 1841 1842
		return;
	}

	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
J
James Smart 已提交
1843
	spin_unlock_irq(shost->host_lock);
1844 1845 1846 1847 1848 1849
	/*
	 * 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);
已提交
1850 1851 1852 1853
	retry = ndlp->nlp_retry;

	switch (cmd) {
	case ELS_CMD_FLOGI:
J
James Smart 已提交
1854
		lpfc_issue_els_flogi(vport, ndlp, retry);
已提交
1855 1856
		break;
	case ELS_CMD_PLOGI:
J
James Smart 已提交
1857
		if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
1858
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
1859
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1860
		}
已提交
1861 1862
		break;
	case ELS_CMD_ADISC:
J
James Smart 已提交
1863
		if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
1864
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
1865
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
1866
		}
已提交
1867 1868
		break;
	case ELS_CMD_PRLI:
J
James Smart 已提交
1869
		if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
1870
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
1871
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1872
		}
已提交
1873 1874
		break;
	case ELS_CMD_LOGO:
J
James Smart 已提交
1875
		if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
1876
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
1877
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1878
		}
已提交
1879
		break;
1880 1881 1882
	case ELS_CMD_FDISC:
		lpfc_issue_els_fdisc(vport, ndlp, retry);
		break;
已提交
1883 1884 1885 1886 1887
	}
	return;
}

static int
J
James Smart 已提交
1888 1889
lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
	       struct lpfc_iocbq *rspiocb)
已提交
1890
{
J
James Smart 已提交
1891 1892 1893 1894 1895
	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;
已提交
1896 1897
	uint32_t *elscmd;
	struct ls_rjt stat;
J
James Smart 已提交
1898
	int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
1899
	int logerr = 0;
J
James Smart 已提交
1900
	uint32_t cmd = 0;
1901
	uint32_t did;
已提交
1902

1903

已提交
1904 1905 1906 1907 1908 1909 1910 1911 1912
	/* Note: context2 may be 0 for internal driver abort
	 * of delays ELS command.
	 */

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

1913
	if (ndlp && NLP_CHK_NODE_ACT(ndlp))
1914 1915 1916 1917
		did = ndlp->nlp_DID;
	else {
		/* We should only hit this case for retrying PLOGI */
		did = irsp->un.elsreq64.remoteID;
J
James Smart 已提交
1918
		ndlp = lpfc_findnode_did(vport, did);
1919 1920
		if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
		    && (cmd != ELS_CMD_PLOGI))
1921 1922 1923
			return 1;
	}

J
James Smart 已提交
1924 1925 1926 1927
	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);

已提交
1928 1929 1930 1931 1932 1933 1934 1935
	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 已提交
1936
			if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
1937
				delay = 1000;
已提交
1938 1939 1940
			retry = 1;
			break;

1941
		case IOERR_ILLEGAL_COMMAND:
1942 1943 1944 1945 1946 1947 1948 1949 1950
			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
					 "0124 Retry illegal cmd x%x "
					 "retry:x%x delay:x%x\n",
					 cmd, cmdiocb->retry, delay);
			retry = 1;
			/* All command's retry policy */
			maxretry = 8;
			if (cmdiocb->retry > 2)
				delay = 1000;
1951 1952
			break;

已提交
1953
		case IOERR_NO_RESOURCES:
1954
			logerr = 1; /* HBA out of resources */
J
James Smart 已提交
1955 1956 1957 1958 1959 1960 1961
			retry = 1;
			if (cmdiocb->retry > 100)
				delay = 100;
			maxretry = 250;
			break;

		case IOERR_ILLEGAL_FRAME:
1962
			delay = 100;
已提交
1963 1964 1965
			retry = 1;
			break;

J
James Smart 已提交
1966
		case IOERR_SEQUENCE_TIMEOUT:
已提交
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
		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:
1983
		logerr = 1; /* Fabric / Remote NPort out of resources */
已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
		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) {
1997
					delay = 1000;
已提交
1998 1999 2000 2001 2002 2003
					maxretry = 48;
				}
				retry = 1;
				break;
			}
			if (cmd == ELS_CMD_PLOGI) {
2004
				delay = 1000;
已提交
2005 2006 2007 2008
				maxretry = lpfc_max_els_tries + 1;
				retry = 1;
				break;
			}
2009 2010 2011
			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
			  (cmd == ELS_CMD_FDISC) &&
			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
2012 2013 2014 2015
				lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
						 "0125 FDISC Failed (x%x). "
						 "Fabric out of resources\n",
						 stat.un.lsRjtError);
2016 2017 2018
				lpfc_vport_set_state(vport,
						     FC_VPORT_NO_FABRIC_RSCS);
			}
已提交
2019 2020 2021
			break;

		case LSRJT_LOGICAL_BSY:
J
James Smart 已提交
2022 2023
			if ((cmd == ELS_CMD_PLOGI) ||
			    (cmd == ELS_CMD_PRLI)) {
2024
				delay = 1000;
已提交
2025
				maxretry = 48;
2026
			} else if (cmd == ELS_CMD_FDISC) {
2027 2028 2029 2030
				/* FDISC retry policy */
				maxretry = 48;
				if (cmdiocb->retry >= 32)
					delay = 1000;
已提交
2031 2032 2033
			}
			retry = 1;
			break;
2034 2035

		case LSRJT_LOGICAL_ERR:
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
			/* There are some cases where switches return this
			 * error when they are not ready and should be returning
			 * Logical Busy. We should delay every time.
			 */
			if (cmd == ELS_CMD_FDISC &&
			    stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
				maxretry = 3;
				delay = 1000;
				retry = 1;
				break;
			}
2047 2048 2049 2050 2051 2052
		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))
			  ) {
2053 2054 2055 2056
				lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
						 "0123 FDISC Failed (x%x). "
						 "Fabric Detected Bad WWN\n",
						 stat.un.lsRjtError);
2057 2058 2059 2060
				lpfc_vport_set_state(vport,
						     FC_VPORT_FABRIC_REJ_WWN);
			}
			break;
已提交
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
		}
		break;

	case IOSTAT_INTERMED_RSP:
	case IOSTAT_BA_RJT:
		break;

	default:
		break;
	}

2072
	if (did == FDMI_DID)
已提交
2073 2074
		retry = 1;

2075
	if ((cmd == ELS_CMD_FLOGI) &&
2076 2077
	    (phba->fc_topology != TOPOLOGY_LOOP) &&
	    !lpfc_error_lost_link(irsp)) {
2078 2079 2080 2081 2082 2083 2084
		/* FLOGI retry policy */
		retry = 1;
		maxretry = 48;
		if (cmdiocb->retry >= 32)
			delay = 1000;
	}

已提交
2085 2086 2087 2088 2089
	if ((++cmdiocb->retry) >= maxretry) {
		phba->fc_stat.elsRetryExceeded++;
		retry = 0;
	}

2090 2091 2092
	if ((vport->load_flag & FC_UNLOADING) != 0)
		retry = 0;

已提交
2093 2094 2095
	if (retry) {

		/* Retry ELS command <elsCmd> to remote NPORT <did> */
2096 2097 2098 2099
		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);
已提交
2100

J
James Smart 已提交
2101 2102 2103 2104 2105
		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 */

已提交
2106
			/* If discovery / RSCN timer is running, reset it */
J
James Smart 已提交
2107
			if (timer_pending(&vport->fc_disctmo) ||
2108
			    (vport->fc_flag & FC_RSCN_MODE))
J
James Smart 已提交
2109
				lpfc_set_disctmo(vport);
已提交
2110 2111 2112
		}

		phba->fc_stat.elsXmitRetry++;
2113
		if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
已提交
2114 2115 2116
			phba->fc_stat.elsDelayRetry++;
			ndlp->nlp_retry = cmdiocb->retry;

2117 2118 2119
			/* delay is specified in milliseconds */
			mod_timer(&ndlp->nlp_delayfunc,
				jiffies + msecs_to_jiffies(delay));
J
James Smart 已提交
2120
			spin_lock_irq(shost->host_lock);
已提交
2121
			ndlp->nlp_flag |= NLP_DELAY_TMO;
J
James Smart 已提交
2122
			spin_unlock_irq(shost->host_lock);
已提交
2123

2124
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2125 2126 2127 2128 2129 2130
			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);
已提交
2131 2132
			ndlp->nlp_last_elscmd = cmd;

2133
			return 1;
已提交
2134 2135 2136
		}
		switch (cmd) {
		case ELS_CMD_FLOGI:
J
James Smart 已提交
2137
			lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
2138
			return 1;
2139 2140 2141
		case ELS_CMD_FDISC:
			lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
			return 1;
已提交
2142
		case ELS_CMD_PLOGI:
2143
			if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2144
				ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2145
				lpfc_nlp_set_state(vport, ndlp,
2146
						   NLP_STE_PLOGI_ISSUE);
2147
			}
J
James Smart 已提交
2148
			lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2149
			return 1;
已提交
2150
		case ELS_CMD_ADISC:
2151
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2152 2153
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2154
			return 1;
已提交
2155
		case ELS_CMD_PRLI:
2156
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2157 2158
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
			lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
2159
			return 1;
已提交
2160
		case ELS_CMD_LOGO:
2161
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2162 2163
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
			lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2164
			return 1;
已提交
2165 2166 2167
		}
	}
	/* No retry ELS command <elsCmd> to remote NPORT <did> */
2168 2169 2170 2171 2172 2173 2174 2175 2176
	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,
2177 2178 2179 2180
			 "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]);
2181
	}
2182
	return 0;
已提交
2183 2184
}

2185
static int
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202
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;
}

2203
static int
2204 2205 2206 2207 2208 2209 2210
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;
}

已提交
2211
int
2212
lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
已提交
2213 2214
{
	struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2215 2216 2217 2218 2219 2220
	struct lpfc_nodelist *ndlp;

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

2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
			/* 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);
2234 2235
		elsiocb->context1 = NULL;
	}
已提交
2236 2237
	/* context2  = cmd,  context2->next = rsp, context3 = bpl */
	if (elsiocb->context2) {
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
		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);
		}
已提交
2267 2268 2269 2270
	}

	if (elsiocb->context3) {
		buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
2271
		lpfc_els_free_bpl(phba, buf_ptr);
已提交
2272
	}
2273
	lpfc_sli_release_iocbq(phba, elsiocb);
已提交
2274 2275 2276 2277
	return 0;
}

static void
J
James Smart 已提交
2278 2279
lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		       struct lpfc_iocbq *rspiocb)
已提交
2280
{
J
James Smart 已提交
2281 2282
	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
	struct lpfc_vport *vport = cmdiocb->vport;
J
James Smart 已提交
2283 2284 2285 2286 2287 2288
	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);
已提交
2289
	/* ACC to LOGO completes to NPort <nlp_DID> */
2290 2291 2292 2293 2294
	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);
2295 2296 2297 2298 2299 2300 2301 2302

	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);
2303 2304 2305 2306 2307
		} else {
			/* Indicate the node has already released, should
			 * not reference to it from within lpfc_els_free_iocb.
			 */
			cmdiocb->context1 = NULL;
2308
		}
已提交
2309 2310 2311 2312 2313
	}
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

J
James Smart 已提交
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
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);
2324
	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2325 2326 2327 2328 2329 2330 2331
		lpfc_nlp_put(ndlp);
		/* 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 已提交
2332 2333 2334
	return;
}

已提交
2335
static void
J
James Smart 已提交
2336
lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2337
		  struct lpfc_iocbq *rspiocb)
已提交
2338
{
J
James Smart 已提交
2339 2340 2341
	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;
2342 2343
	IOCB_t  *irsp;
	uint8_t *pcmd;
已提交
2344
	LPFC_MBOXQ_t *mbox = NULL;
J
James Smart 已提交
2345
	struct lpfc_dmabuf *mp = NULL;
2346
	uint32_t ls_rjt = 0;
已提交
2347

J
James Smart 已提交
2348 2349
	irsp = &rspiocb->iocb;

已提交
2350 2351 2352
	if (cmdiocb->context_un.mbox)
		mbox = cmdiocb->context_un.mbox;

2353 2354 2355
	/* First determine if this is a LS_RJT cmpl. Note, this callback
	 * function can have cmdiocb->contest1 (ndlp) field set to NULL.
	 */
2356
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
2357 2358
	if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
	    (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
2359 2360
		/* A LS_RJT associated with Default RPI cleanup has its own
		 * seperate code path.
2361 2362 2363 2364 2365
		 */
		if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
			ls_rjt = 1;
	}

已提交
2366
	/* Check to see if link went down during discovery */
2367
	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
已提交
2368
		if (mbox) {
2369 2370 2371 2372 2373
			mp = (struct lpfc_dmabuf *) mbox->context1;
			if (mp) {
				lpfc_mbuf_free(phba, mp->virt, mp->phys);
				kfree(mp);
			}
2374
			mempool_free(mbox, phba->mbox_mem_pool);
已提交
2375
		}
2376 2377
		if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
		    (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
2378
			if (lpfc_nlp_not_used(ndlp)) {
2379
				ndlp = NULL;
2380 2381 2382 2383 2384 2385
				/* Indicate the node has already released,
				 * should not reference to it from within
				 * the routine lpfc_els_free_iocb.
				 */
				cmdiocb->context1 = NULL;
			}
已提交
2386 2387 2388
		goto out;
	}

J
James Smart 已提交
2389
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2390
		"ELS rsp cmpl:    status:x%x/x%x did:x%x",
J
James Smart 已提交
2391
		irsp->ulpStatus, irsp->un.ulpWord[4],
2392
		cmdiocb->iocb.un.elsreq64.remoteID);
已提交
2393
	/* ELS response tag <ulpIoTag> completes */
2394 2395 2396 2397 2398 2399 2400
	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);
已提交
2401 2402 2403
	if (mbox) {
		if ((rspiocb->iocb.ulpStatus == 0)
		    && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
J
James Smart 已提交
2404
			lpfc_unreg_rpi(vport, ndlp);
2405 2406 2407
			/* Increment reference count to ndlp to hold the
			 * reference to ndlp for the callback function.
			 */
2408
			mbox->context2 = lpfc_nlp_get(ndlp);
J
James Smart 已提交
2409
			mbox->vport = vport;
J
James Smart 已提交
2410 2411 2412 2413 2414 2415 2416 2417
			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 已提交
2418
					   NLP_STE_REG_LOGIN_ISSUE);
J
James Smart 已提交
2419
			}
2420
			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
2421
			    != MBX_NOT_FINISHED)
已提交
2422
				goto out;
2423 2424 2425 2426 2427
			else
				/* Decrement the ndlp reference count we
				 * set for this failed mailbox command.
				 */
				lpfc_nlp_put(ndlp);
2428 2429 2430 2431 2432 2433 2434 2435

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

2436
			if (lpfc_nlp_not_used(ndlp)) {
2437
				ndlp = NULL;
2438 2439 2440 2441 2442 2443
				/* Indicate node has already been released,
				 * should not reference to it from within
				 * the routine lpfc_els_free_iocb.
				 */
				cmdiocb->context1 = NULL;
			}
已提交
2444
		} else {
J
James Smart 已提交
2445 2446 2447
			/* Do not drop node for lpfc_els_abort'ed ELS cmds */
			if (!lpfc_error_lost_link(irsp) &&
			    ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
2448
				if (lpfc_nlp_not_used(ndlp)) {
2449
					ndlp = NULL;
2450 2451 2452 2453 2454 2455 2456
					/* Indicate node has already been
					 * released, should not reference
					 * to it from within the routine
					 * lpfc_els_free_iocb.
					 */
					cmdiocb->context1 = NULL;
				}
已提交
2457 2458
			}
		}
2459 2460 2461 2462 2463 2464
		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);
已提交
2465 2466
	}
out:
2467
	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
J
James Smart 已提交
2468
		spin_lock_irq(shost->host_lock);
J
James Smart 已提交
2469
		ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
J
James Smart 已提交
2470
		spin_unlock_irq(shost->host_lock);
2471 2472 2473 2474 2475 2476 2477

		/* 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)
2478 2479 2480 2481 2482 2483
			if (lpfc_nlp_not_used(ndlp))
				/* Indicate node has already been released,
				 * should not reference to it from within
				 * the routine lpfc_els_free_iocb.
				 */
				cmdiocb->context1 = NULL;
已提交
2484
	}
2485

已提交
2486 2487 2488 2489 2490
	lpfc_els_free_iocb(phba, cmdiocb);
	return;
}

int
J
James Smart 已提交
2491 2492
lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
		 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
2493
		 LPFC_MBOXQ_t *mbox)
已提交
2494
{
J
James Smart 已提交
2495 2496
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
2497 2498 2499 2500 2501 2502 2503 2504
	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;
2505
	ELS_PKT *els_pkt_ptr;
已提交
2506 2507 2508 2509 2510 2511 2512

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

	switch (flag) {
	case ELS_CMD_ACC:
2513
		cmdsize = sizeof(uint32_t);
J
James Smart 已提交
2514 2515
		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2516
		if (!elsiocb) {
J
James Smart 已提交
2517
			spin_lock_irq(shost->host_lock);
2518
			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
J
James Smart 已提交
2519
			spin_unlock_irq(shost->host_lock);
2520
			return 1;
已提交
2521
		}
J
James Smart 已提交
2522

已提交
2523 2524 2525 2526
		icmd = &elsiocb->iocb;
		icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2527
		pcmd += sizeof(uint32_t);
J
James Smart 已提交
2528 2529 2530 2531

		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);
已提交
2532 2533
		break;
	case ELS_CMD_PLOGI:
2534
		cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
J
James Smart 已提交
2535 2536
		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2537
		if (!elsiocb)
2538
			return 1;
2539

已提交
2540 2541 2542 2543 2544 2545 2546 2547
		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;
2548 2549
		pcmd += sizeof(uint32_t);
		memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
J
James Smart 已提交
2550 2551 2552 2553

		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);
已提交
2554
		break;
2555
	case ELS_CMD_PRLO:
2556
		cmdsize = sizeof(uint32_t) + sizeof(PRLO);
J
James Smart 已提交
2557
		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2558 2559 2560 2561 2562 2563 2564 2565 2566
					     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,
2567
		       sizeof(uint32_t) + sizeof(PRLO));
2568 2569 2570
		*((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 已提交
2571 2572 2573 2574

		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);
2575
		break;
已提交
2576
	default:
2577
		return 1;
已提交
2578 2579
	}
	/* Xmit ELS ACC response tag <ulpIoTag> */
2580 2581 2582 2583 2584 2585
	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);
已提交
2586
	if (ndlp->nlp_flag & NLP_LOGO_ACC) {
J
James Smart 已提交
2587
		spin_lock_irq(shost->host_lock);
2588
		ndlp->nlp_flag &= ~NLP_LOGO_ACC;
J
James Smart 已提交
2589
		spin_unlock_irq(shost->host_lock);
已提交
2590 2591
		elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
	} else {
J
James Smart 已提交
2592
		elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2593 2594 2595 2596 2597 2598
	}

	phba->fc_stat.elsXmitACC++;
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2599
		return 1;
已提交
2600
	}
2601
	return 0;
已提交
2602 2603 2604
}

int
J
James Smart 已提交
2605
lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
J
James Smart 已提交
2606 2607
		    struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
		    LPFC_MBOXQ_t *mbox)
已提交
2608
{
J
James Smart 已提交
2609
	struct lpfc_hba  *phba = vport->phba;
已提交
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
	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 */

2622
	cmdsize = 2 * sizeof(uint32_t);
J
James Smart 已提交
2623 2624
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
2625
	if (!elsiocb)
2626
		return 1;
已提交
2627 2628 2629 2630 2631 2632 2633

	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;
2634
	pcmd += sizeof(uint32_t);
已提交
2635 2636
	*((uint32_t *) (pcmd)) = rejectError;

2637
	if (mbox)
J
James Smart 已提交
2638 2639
		elsiocb->context_un.mbox = mbox;

已提交
2640
	/* Xmit ELS RJT <err> response tag <ulpIoTag> */
2641 2642 2643 2644 2645 2646 2647
	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 已提交
2648 2649 2650 2651
	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);

已提交
2652
	phba->fc_stat.elsXmitLSRJT++;
J
James Smart 已提交
2653
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2654
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2655

已提交
2656 2657
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2658
		return 1;
已提交
2659
	}
2660
	return 0;
已提交
2661 2662 2663
}

int
J
James Smart 已提交
2664 2665
lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
		       struct lpfc_nodelist *ndlp)
已提交
2666
{
J
James Smart 已提交
2667 2668 2669
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_sli  *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
已提交
2670
	ADISC *ap;
J
James Smart 已提交
2671
	IOCB_t *icmd, *oldcmd;
已提交
2672 2673 2674 2675 2676
	struct lpfc_iocbq *elsiocb;
	uint8_t *pcmd;
	uint16_t cmdsize;
	int rc;

2677
	cmdsize = sizeof(uint32_t) + sizeof(ADISC);
J
James Smart 已提交
2678 2679
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
2680
	if (!elsiocb)
2681
		return 1;
已提交
2682

2683 2684 2685 2686
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */

已提交
2687
	/* Xmit ADISC ACC response tag <ulpIoTag> */
2688 2689 2690 2691 2692 2693
	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);
已提交
2694 2695 2696
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2697
	pcmd += sizeof(uint32_t);
已提交
2698 2699 2700

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

J
James Smart 已提交
2705 2706 2707 2708
	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);

已提交
2709
	phba->fc_stat.elsXmitACC++;
J
James Smart 已提交
2710
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2711 2712 2713
	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2714
		return 1;
已提交
2715
	}
2716
	return 0;
已提交
2717 2718 2719
}

int
J
James Smart 已提交
2720
lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
2721
		      struct lpfc_nodelist *ndlp)
已提交
2722
{
J
James Smart 已提交
2723
	struct lpfc_hba  *phba = vport->phba;
已提交
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
	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 */

2738
	cmdsize = sizeof(uint32_t) + sizeof(PRLI);
J
James Smart 已提交
2739
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2740
		ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
2741 2742
	if (!elsiocb)
		return 1;
已提交
2743

2744 2745 2746
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
已提交
2747
	/* Xmit PRLI ACC response tag <ulpIoTag> */
2748 2749 2750 2751 2752 2753
	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);
已提交
2754 2755 2756
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);

	*((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
2757
	pcmd += sizeof(uint32_t);
已提交
2758 2759

	/* For PRLI, remainder of payload is PRLI parameter page */
2760
	memset(pcmd, 0, sizeof(PRLI));
已提交
2761 2762 2763 2764

	npr = (PRLI *) pcmd;
	vpd = &phba->vpd;
	/*
2765 2766
	 * If the remote port is a target and our firmware version is 3.20 or
	 * later, set the following bits for FC-TAPE support.
已提交
2767
	 */
2768 2769
	if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
	    (vpd->rev.feaLevelHigh >= 0x02)) {
已提交
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782
		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 已提交
2783 2784 2785 2786
	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);

已提交
2787
	phba->fc_stat.elsXmitACC++;
J
James Smart 已提交
2788
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
已提交
2789 2790 2791 2792

	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_els_free_iocb(phba, elsiocb);
2793
		return 1;
已提交
2794
	}
2795
	return 0;
已提交
2796 2797 2798
}

static int
J
James Smart 已提交
2799
lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
2800
		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
已提交
2801
{
J
James Smart 已提交
2802
	struct lpfc_hba  *phba = vport->phba;
已提交
2803
	RNID *rn;
J
James Smart 已提交
2804
	IOCB_t *icmd, *oldcmd;
已提交
2805 2806 2807 2808 2809 2810 2811 2812 2813 2814
	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];

2815 2816
	cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
					+ (2 * sizeof(struct lpfc_name));
已提交
2817
	if (format)
2818
		cmdsize += sizeof(RNID_TOP_DISC);
已提交
2819

J
James Smart 已提交
2820 2821
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
2822
	if (!elsiocb)
2823
		return 1;
已提交
2824

2825 2826 2827
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
已提交
2828
	/* Xmit RNID ACC response tag <ulpIoTag> */
2829 2830 2831
	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);
已提交
2832 2833
	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2834
	pcmd += sizeof(uint32_t);
已提交
2835

2836
	memset(pcmd, 0, sizeof(RNID));
已提交
2837 2838
	rn = (RNID *) (pcmd);
	rn->Format = format;
2839 2840 2841
	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));
已提交
2842 2843 2844 2845 2846
	switch (format) {
	case 0:
		rn->SpecificLen = 0;
		break;
	case RNID_TOPOLOGY_DISC:
2847
		rn->SpecificLen = sizeof(RNID_TOP_DISC);
已提交
2848
		memcpy(&rn->un.topologyDisc.portName,
2849
		       &vport->fc_portname, sizeof(struct lpfc_name));
已提交
2850 2851 2852 2853 2854 2855 2856 2857 2858 2859
		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 已提交
2860 2861 2862 2863
	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);

已提交
2864
	phba->fc_stat.elsXmitACC++;
J
James Smart 已提交
2865
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2866
	lpfc_nlp_put(ndlp);
已提交
2867 2868 2869 2870 2871 2872
	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);
2873
		return 1;
已提交
2874
	}
2875
	return 0;
已提交
2876 2877 2878
}

int
J
James Smart 已提交
2879
lpfc_els_disc_adisc(struct lpfc_vport *vport)
已提交
2880
{
J
James Smart 已提交
2881
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
2882
	struct lpfc_nodelist *ndlp, *next_ndlp;
J
James Smart 已提交
2883
	int sentadisc = 0;
已提交
2884

2885
	/* go thru NPR nodes and issue any remaining ELS ADISCs */
J
James Smart 已提交
2886
	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2887 2888
		if (!NLP_CHK_NODE_ACT(ndlp))
			continue;
2889 2890 2891
		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 已提交
2892
			spin_lock_irq(shost->host_lock);
2893
			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
J
James Smart 已提交
2894
			spin_unlock_irq(shost->host_lock);
2895
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
2896 2897
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
			lpfc_issue_els_adisc(vport, ndlp, 0);
2898
			sentadisc++;
J
James Smart 已提交
2899 2900
			vport->num_disc_nodes++;
			if (vport->num_disc_nodes >=
2901
			    vport->cfg_discovery_threads) {
J
James Smart 已提交
2902 2903 2904
				spin_lock_irq(shost->host_lock);
				vport->fc_flag |= FC_NLP_MORE;
				spin_unlock_irq(shost->host_lock);
2905
				break;
已提交
2906 2907 2908 2909
			}
		}
	}
	if (sentadisc == 0) {
J
James Smart 已提交
2910 2911 2912
		spin_lock_irq(shost->host_lock);
		vport->fc_flag &= ~FC_NLP_MORE;
		spin_unlock_irq(shost->host_lock);
已提交
2913
	}
2914
	return sentadisc;
已提交
2915 2916 2917
}

int
J
James Smart 已提交
2918
lpfc_els_disc_plogi(struct lpfc_vport *vport)
已提交
2919
{
J
James Smart 已提交
2920
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
2921
	struct lpfc_nodelist *ndlp, *next_ndlp;
J
James Smart 已提交
2922
	int sentplogi = 0;
已提交
2923

J
James Smart 已提交
2924 2925
	/* go thru NPR nodes and issue any remaining ELS PLOGIs */
	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2926 2927
		if (!NLP_CHK_NODE_ACT(ndlp))
			continue;
2928 2929 2930 2931 2932
		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 已提交
2933 2934
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2935
			sentplogi++;
J
James Smart 已提交
2936 2937
			vport->num_disc_nodes++;
			if (vport->num_disc_nodes >=
2938
			    vport->cfg_discovery_threads) {
J
James Smart 已提交
2939 2940 2941
				spin_lock_irq(shost->host_lock);
				vport->fc_flag |= FC_NLP_MORE;
				spin_unlock_irq(shost->host_lock);
2942
				break;
已提交
2943 2944 2945
			}
		}
	}
2946 2947 2948 2949
	if (sentplogi) {
		lpfc_set_disctmo(vport);
	}
	else {
J
James Smart 已提交
2950 2951 2952
		spin_lock_irq(shost->host_lock);
		vport->fc_flag &= ~FC_NLP_MORE;
		spin_unlock_irq(shost->host_lock);
已提交
2953
	}
2954
	return sentplogi;
已提交
2955 2956
}

2957
void
J
James Smart 已提交
2958
lpfc_els_flush_rscn(struct lpfc_vport *vport)
已提交
2959
{
J
James Smart 已提交
2960 2961
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
2962 2963
	int i;

2964 2965 2966 2967 2968 2969 2970 2971 2972 2973
	spin_lock_irq(shost->host_lock);
	if (vport->fc_rscn_flush) {
		/* Another thread is walking fc_rscn_id_list on this vport */
		spin_unlock_irq(shost->host_lock);
		return;
	}
	/* Indicate we are walking lpfc_els_flush_rscn on this vport */
	vport->fc_rscn_flush = 1;
	spin_unlock_irq(shost->host_lock);

J
James Smart 已提交
2974
	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
2975
		lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
J
James Smart 已提交
2976
		vport->fc_rscn_id_list[i] = NULL;
已提交
2977
	}
J
James Smart 已提交
2978 2979 2980 2981 2982
	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);
2983 2984
	/* Indicate we are done walking this fc_rscn_id_list */
	vport->fc_rscn_flush = 0;
已提交
2985 2986 2987
}

int
J
James Smart 已提交
2988
lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
已提交
2989 2990 2991 2992
{
	D_ID ns_did;
	D_ID rscn_did;
	uint32_t *lp;
2993
	uint32_t payload_len, i;
2994
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
已提交
2995 2996 2997 2998 2999

	ns_did.un.word = did;

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

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

3006 3007 3008 3009 3010 3011 3012 3013 3014
	spin_lock_irq(shost->host_lock);
	if (vport->fc_rscn_flush) {
		/* Another thread is walking fc_rscn_id_list on this vport */
		spin_unlock_irq(shost->host_lock);
		return 0;
	}
	/* Indicate we are walking fc_rscn_id_list on this vport */
	vport->fc_rscn_flush = 1;
	spin_unlock_irq(shost->host_lock);
J
James Smart 已提交
3015
	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
3016 3017 3018
		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 */
已提交
3019
		while (payload_len) {
3020 3021
			rscn_did.un.word = be32_to_cpu(*lp++);
			payload_len -= sizeof(uint32_t);
已提交
3022 3023
			switch (rscn_did.un.b.resv) {
			case 0:	/* Single N_Port ID effected */
J
James Smart 已提交
3024
				if (ns_did.un.word == rscn_did.un.word)
3025
					goto return_did_out;
已提交
3026 3027 3028 3029
				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))
3030
					goto return_did_out;
已提交
3031 3032 3033
				break;
			case 2:	/* Whole N_Port Domain effected */
				if (ns_did.un.b.domain == rscn_did.un.b.domain)
3034
					goto return_did_out;
已提交
3035 3036
				break;
			default:
J
James Smart 已提交
3037
				/* Unknown Identifier in RSCN node */
3038 3039 3040 3041
				lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
						 "0217 Unknown Identifier in "
						 "RSCN payload Data: x%x\n",
						 rscn_did.un.word);
3042
			case 3:	/* Whole Fabric effected */
3043
				goto return_did_out;
已提交
3044 3045
			}
		}
3046
	}
3047 3048
	/* Indicate we are done with walking fc_rscn_id_list on this vport */
	vport->fc_rscn_flush = 0;
3049
	return 0;
3050 3051 3052 3053
return_did_out:
	/* Indicate we are done with walking fc_rscn_id_list on this vport */
	vport->fc_rscn_flush = 0;
	return did;
已提交
3054 3055 3056
}

static int
J
James Smart 已提交
3057
lpfc_rscn_recovery_check(struct lpfc_vport *vport)
已提交
3058
{
3059
	struct lpfc_nodelist *ndlp = NULL;
已提交
3060

3061
	/* Move all affected nodes by pending RSCNs to NPR state. */
J
James Smart 已提交
3062
	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3063
		if (!NLP_CHK_NODE_ACT(ndlp) ||
3064 3065
		    (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
		    !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
3066
			continue;
J
James Smart 已提交
3067
		lpfc_disc_state_machine(vport, ndlp, NULL,
3068 3069
					NLP_EVT_DEVICE_RECOVERY);
		lpfc_cancel_retry_delay_tmo(vport, ndlp);
已提交
3070
	}
3071
	return 0;
已提交
3072 3073 3074
}

static int
J
James Smart 已提交
3075
lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3076
		  struct lpfc_nodelist *ndlp)
已提交
3077
{
J
James Smart 已提交
3078 3079
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
3080
	struct lpfc_dmabuf *pcmd;
3081
	uint32_t *lp, *datap;
已提交
3082
	IOCB_t *icmd;
3083
	uint32_t payload_len, length, nportid, *cmd;
3084
	int rscn_cnt;
3085
	int rscn_id = 0, hba_id = 0;
3086
	int i;
已提交
3087 3088 3089 3090 3091

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

3092 3093
	payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
	payload_len -= sizeof(uint32_t);	/* take off word 0 */
已提交
3094
	/* RSCN received */
3095 3096
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0214 RSCN received Data: x%x x%x x%x x%x\n",
3097 3098
			 vport->fc_flag, payload_len, *lp,
			 vport->fc_rscn_id_cnt);
3099
	for (i = 0; i < payload_len/sizeof(uint32_t); i++)
J
James Smart 已提交
3100
		fc_host_post_event(shost, fc_get_event_number(),
3101 3102
			FCH_EVT_RSCN, lp[i]);

已提交
3103 3104 3105
	/* If we are about to begin discovery, just ACC the RSCN.
	 * Discovery processing will satisfy it.
	 */
J
James Smart 已提交
3106
	if (vport->port_state <= LPFC_NS_QRY) {
J
James Smart 已提交
3107 3108 3109 3110
		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);

3111
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
3112
		return 0;
已提交
3113 3114
	}

3115 3116 3117 3118
	/* 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) &&
3119
		!(vport->cfg_peer_port_login)) {
3120 3121 3122 3123 3124 3125 3126
		i = payload_len;
		datap = lp;
		while (i > 0) {
			nportid = *datap++;
			nportid = ((be32_to_cpu(nportid)) & Mask_DID);
			i -= sizeof(uint32_t);
			rscn_id++;
3127 3128
			if (lpfc_find_vport_by_did(phba, nportid))
				hba_id++;
3129 3130 3131
		}
		if (rscn_id == hba_id) {
			/* ALL NPortIDs in RSCN are on HBA */
3132 3133 3134 3135
			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,
3136
					 *lp, vport->fc_rscn_id_cnt);
J
James Smart 已提交
3137 3138 3139 3140 3141
			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);

3142
			lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
3143
				ndlp, NULL);
3144 3145 3146 3147
			return 0;
		}
	}

3148 3149 3150 3151 3152
	spin_lock_irq(shost->host_lock);
	if (vport->fc_rscn_flush) {
		/* Another thread is walking fc_rscn_id_list on this vport */
		spin_unlock_irq(shost->host_lock);
		vport->fc_flag |= FC_RSCN_DISCOVERY;
3153 3154
		/* Send back ACC */
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
3155 3156 3157 3158 3159 3160 3161
		return 0;
	}
	/* Indicate we are walking fc_rscn_id_list on this vport */
	vport->fc_rscn_flush = 1;
	spin_unlock_irq(shost->host_lock);
	/* Get the array count after sucessfully have the token */
	rscn_cnt = vport->fc_rscn_id_cnt;
已提交
3162 3163 3164
	/* If we are already processing an RSCN, save the received
	 * RSCN payload buffer, cmdiocb->context2 to process later.
	 */
J
James Smart 已提交
3165
	if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
J
James Smart 已提交
3166 3167 3168 3169
		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);

3170
		spin_lock_irq(shost->host_lock);
3171 3172
		vport->fc_flag |= FC_RSCN_DEFERRED;
		if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
J
James Smart 已提交
3173 3174 3175
		    !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
			vport->fc_flag |= FC_RSCN_MODE;
			spin_unlock_irq(shost->host_lock);
3176 3177 3178 3179 3180 3181 3182
			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;
3183
				*cmd |= cpu_to_be32(payload_len + length);
3184 3185 3186 3187 3188 3189 3190 3191 3192 3193
				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;
			}
已提交
3194
			/* Deferred RSCN */
3195 3196 3197 3198 3199
			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);
已提交
3200
		} else {
J
James Smart 已提交
3201 3202
			vport->fc_flag |= FC_RSCN_DISCOVERY;
			spin_unlock_irq(shost->host_lock);
已提交
3203
			/* ReDiscovery RSCN */
3204 3205 3206 3207 3208
			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);
已提交
3209
		}
3210 3211
		/* Indicate we are done walking fc_rscn_id_list on this vport */
		vport->fc_rscn_flush = 0;
已提交
3212
		/* Send back ACC */
3213
		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
3214
		/* send RECOVERY event for ALL nodes that match RSCN payload */
J
James Smart 已提交
3215
		lpfc_rscn_recovery_check(vport);
3216
		spin_lock_irq(shost->host_lock);
3217
		vport->fc_flag &= ~FC_RSCN_DEFERRED;
3218
		spin_unlock_irq(shost->host_lock);
3219
		return 0;
已提交
3220
	}
J
James Smart 已提交
3221 3222 3223 3224
	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 已提交
3225 3226 3227 3228
	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;
3229 3230
	/* Indicate we are done walking fc_rscn_id_list on this vport */
	vport->fc_rscn_flush = 0;
已提交
3231 3232 3233 3234 3235
	/*
	 * If we zero, cmdiocb->context2, the calling routine will
	 * not try to free it.
	 */
	cmdiocb->context2 = NULL;
J
James Smart 已提交
3236
	lpfc_set_disctmo(vport);
已提交
3237
	/* Send back ACC */
3238
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
3239
	/* send RECOVERY event for ALL nodes that match RSCN payload */
J
James Smart 已提交
3240 3241
	lpfc_rscn_recovery_check(vport);
	return lpfc_els_handle_rscn(vport);
已提交
3242 3243 3244
}

int
J
James Smart 已提交
3245
lpfc_els_handle_rscn(struct lpfc_vport *vport)
已提交
3246 3247
{
	struct lpfc_nodelist *ndlp;
J
James Smart 已提交
3248
	struct lpfc_hba *phba = vport->phba;
已提交
3249

3250 3251 3252 3253 3254 3255
	/* Ignore RSCN if the port is being torn down. */
	if (vport->load_flag & FC_UNLOADING) {
		lpfc_els_flush_rscn(vport);
		return 0;
	}

已提交
3256
	/* Start timer for RSCN processing */
J
James Smart 已提交
3257
	lpfc_set_disctmo(vport);
已提交
3258 3259

	/* RSCN processed */
3260 3261 3262 3263
	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);
已提交
3264 3265

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

J
James Smart 已提交
3269
	ndlp = lpfc_findnode_did(vport, NameServer_DID);
3270 3271
	if (ndlp && NLP_CHK_NODE_ACT(ndlp)
	    && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
已提交
3272
		/* Good ndlp, issue CT Request to NameServer */
3273
		if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
已提交
3274 3275
			/* Wait for NameServer query cmpl before we can
			   continue */
3276
			return 1;
已提交
3277 3278 3279
	} else {
		/* If login to NameServer does not exist, issue one */
		/* Good status, issue PLOGI to NameServer */
J
James Smart 已提交
3280
		ndlp = lpfc_findnode_did(vport, NameServer_DID);
3281
		if (ndlp && NLP_CHK_NODE_ACT(ndlp))
已提交
3282 3283
			/* Wait for NameServer login cmpl before we can
			   continue */
3284
			return 1;
J
James Smart 已提交
3285

3286 3287 3288 3289 3290 3291 3292 3293
		if (ndlp) {
			ndlp = lpfc_enable_node(vport, ndlp,
						NLP_STE_PLOGI_ISSUE);
			if (!ndlp) {
				lpfc_els_flush_rscn(vport);
				return 0;
			}
			ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
已提交
3294
		} else {
3295 3296 3297 3298 3299
			ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
			if (!ndlp) {
				lpfc_els_flush_rscn(vport);
				return 0;
			}
J
James Smart 已提交
3300
			lpfc_nlp_init(vport, ndlp, NameServer_DID);
3301
			ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
3302
			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
已提交
3303
		}
3304 3305 3306 3307 3308 3309
		ndlp->nlp_type |= NLP_FABRIC;
		lpfc_issue_els_plogi(vport, NameServer_DID, 0);
		/* Wait for NameServer login cmpl before we can
		 * continue
		 */
		return 1;
已提交
3310 3311
	}

J
James Smart 已提交
3312
	lpfc_els_flush_rscn(vport);
3313
	return 0;
已提交
3314 3315 3316
}

static int
J
James Smart 已提交
3317
lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3318
		   struct lpfc_nodelist *ndlp)
已提交
3319
{
J
James Smart 已提交
3320 3321
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_hba  *phba = vport->phba;
已提交
3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335
	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 已提交
3336
	lpfc_set_disctmo(vport);
已提交
3337 3338 3339 3340 3341 3342 3343

	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 */
3344 3345 3346 3347
		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);
3348
		return 1;
已提交
3349 3350 3351 3352
	}

	did = Fabric_DID;

J
James Smart 已提交
3353
	if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3))) {
已提交
3354 3355 3356 3357
		/* For a FLOGI we accept, then if our portname is greater
		 * then the remote portname we initiate Nport login.
		 */

J
James Smart 已提交
3358
		rc = memcmp(&vport->fc_portname, &sp->portName,
3359
			    sizeof(struct lpfc_name));
已提交
3360 3361

		if (!rc) {
J
James Smart 已提交
3362 3363
			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
			if (!mbox)
3364
				return 1;
J
James Smart 已提交
3365

已提交
3366 3367 3368 3369 3370 3371
			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;
3372
			mbox->vport = vport;
3373
			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3374
			lpfc_set_loopback_flag(phba);
已提交
3375
			if (rc == MBX_NOT_FINISHED) {
3376
				mempool_free(mbox, phba->mbox_mem_pool);
已提交
3377
			}
3378
			return 1;
3379
		} else if (rc > 0) {	/* greater than */
J
James Smart 已提交
3380 3381 3382
			spin_lock_irq(shost->host_lock);
			vport->fc_flag |= FC_PT2PT_PLOGI;
			spin_unlock_irq(shost->host_lock);
已提交
3383
		}
J
James Smart 已提交
3384 3385 3386 3387
		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);
已提交
3388 3389 3390 3391 3392 3393
	} 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 已提交
3394 3395
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
3396
		return 1;
已提交
3397 3398 3399
	}

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

3402
	return 0;
已提交
3403 3404 3405
}

static int
J
James Smart 已提交
3406 3407
lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_nodelist *ndlp)
已提交
3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429
{
	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 已提交
3430
		lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
已提交
3431 3432 3433 3434 3435 3436 3437
		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 已提交
3438 3439
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
已提交
3440
	}
3441
	return 0;
已提交
3442 3443 3444
}

static int
J
James Smart 已提交
3445 3446
lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_nodelist *ndlp)
3447 3448 3449 3450 3451 3452 3453 3454
{
	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 已提交
3455
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3456 3457 3458
	return 0;
}

3459
static void
3460
lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3461
{
J
James Smart 已提交
3462 3463
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476
	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 已提交
3477 3478
	pmb->context1 = NULL;
	pmb->context2 = NULL;
3479 3480

	if (mb->mbxStatus) {
3481
		mempool_free(pmb, phba->mbox_mem_pool);
3482 3483 3484 3485
		return;
	}

	cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
3486
	mempool_free(pmb, phba->mbox_mem_pool);
J
James Smart 已提交
3487 3488 3489
	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
				     lpfc_max_els_tries, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
3490 3491

	/* Decrement the ndlp reference count from previous mbox command */
3492
	lpfc_nlp_put(ndlp);
3493

3494
	if (!elsiocb)
3495 3496 3497 3498 3499 3500 3501
		return;

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

	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3502
	pcmd += sizeof(uint32_t); /* Skip past command */
3503 3504 3505 3506 3507 3508
	rps_rsp = (RPS_RSP *)pcmd;

	if (phba->fc_topology != TOPOLOGY_LOOP)
		status = 0x10;
	else
		status = 0x8;
J
James Smart 已提交
3509
	if (phba->pport->fc_flag & FC_FABRIC)
3510 3511 3512
		status |= 0x4;

	rps_rsp->rsvd1 = 0;
3513 3514 3515 3516 3517 3518 3519
	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);
3520
	/* Xmit ELS RPS ACC response tag <ulpIoTag> */
3521 3522 3523 3524 3525 3526
	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 已提交
3527
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3528
	phba->fc_stat.elsXmitACC++;
3529
	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR)
3530 3531 3532 3533 3534
		lpfc_els_free_iocb(phba, elsiocb);
	return;
}

static int
J
James Smart 已提交
3535 3536
lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		 struct lpfc_nodelist *ndlp)
已提交
3537
{
J
James Smart 已提交
3538
	struct lpfc_hba *phba = vport->phba;
已提交
3539
	uint32_t *lp;
3540 3541 3542 3543 3544 3545
	uint8_t flag;
	LPFC_MBOXQ_t *mbox;
	struct lpfc_dmabuf *pcmd;
	RPS *rps;
	struct ls_rjt stat;

3546 3547
	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3548 3549 3550 3551
		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 已提交
3552 3553
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
3554 3555 3556 3557 3558 3559 3560 3561 3562
	}

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

3566 3567
		printk("Fix me....\n");
		dump_stack();
J
James Smart 已提交
3568 3569
		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
		if (mbox) {
3570 3571
			lpfc_read_lnk_stat(phba, mbox);
			mbox->context1 =
3572
			    (void *)((unsigned long) cmdiocb->iocb.ulpContext);
3573
			mbox->context2 = lpfc_nlp_get(ndlp);
3574
			mbox->vport = vport;
3575
			mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
3576
			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
3577
				!= MBX_NOT_FINISHED)
3578 3579
				/* Mbox completion will send ELS Response */
				return 0;
3580 3581 3582
			/* Decrement reference count used for the failed mbox
			 * command.
			 */
3583
			lpfc_nlp_put(ndlp);
3584 3585 3586 3587 3588 3589 3590
			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 已提交
3591
	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3592 3593 3594
	return 0;
}

3595
static int
J
James Smart 已提交
3596 3597
lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
		     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3598
{
J
James Smart 已提交
3599 3600
	struct lpfc_hba *phba = vport->phba;
	IOCB_t *icmd, *oldcmd;
3601 3602
	RPL_RSP rpl_rsp;
	struct lpfc_iocbq *elsiocb;
J
James Smart 已提交
3603 3604
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3605
	uint8_t *pcmd;
已提交
3606

J
James Smart 已提交
3607 3608
	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
				     ndlp->nlp_DID, ELS_CMD_ACC);
3609

3610
	if (!elsiocb)
3611
		return 1;
3612

3613 3614 3615 3616 3617 3618
	icmd = &elsiocb->iocb;
	oldcmd = &oldiocb->iocb;
	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */

	pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3619
	pcmd += sizeof(uint16_t);
3620 3621 3622 3623 3624 3625 3626
	*((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 已提交
3627 3628
	rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
	memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
3629 3630 3631
	    sizeof(struct lpfc_name));
	memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
	/* Xmit ELS RPL ACC response tag <ulpIoTag> */
3632 3633 3634 3635 3636 3637 3638
	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 已提交
3639
	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3640 3641 3642 3643 3644 3645 3646 3647 3648
	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 已提交
3649 3650
lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		 struct lpfc_nodelist *ndlp)
3651 3652 3653 3654 3655 3656 3657 3658
{
	struct lpfc_dmabuf *pcmd;
	uint32_t *lp;
	uint32_t maxsize;
	uint16_t cmdsize;
	RPL *rpl;
	struct ls_rjt stat;

3659 3660
	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3661 3662 3663 3664
		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 已提交
3665 3666
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
			NULL);
3667 3668
	}

已提交
3669 3670
	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
	lp = (uint32_t *) pcmd->virt;
3671
	rpl = (RPL *) (lp + 1);
已提交
3672

3673
	maxsize = be32_to_cpu(rpl->maxsize);
已提交
3674

3675 3676 3677 3678 3679
	/* 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);
3680
	} else {
3681 3682
		cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
	}
J
James Smart 已提交
3683
	lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
已提交
3684 3685 3686 3687 3688

	return 0;
}

static int
J
James Smart 已提交
3689 3690
lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		  struct lpfc_nodelist *ndlp)
已提交
3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705
{
	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> */
3706 3707
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0601 FARP-REQ received from DID x%x\n", did);
已提交
3708 3709
	/* We will only support match on WWPN or WWNN */
	if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
3710
		return 0;
已提交
3711 3712 3713 3714 3715
	}

	cnt = 0;
	/* If this FARP command is searching for my portname */
	if (fp->Mflags & FARP_MATCH_PORT) {
J
James Smart 已提交
3716
		if (memcmp(&fp->RportName, &vport->fc_portname,
3717
			   sizeof(struct lpfc_name)) == 0)
已提交
3718 3719 3720 3721 3722
			cnt = 1;
	}

	/* If this FARP command is searching for my nodename */
	if (fp->Mflags & FARP_MATCH_NODE) {
J
James Smart 已提交
3723
		if (memcmp(&fp->RnodeName, &vport->fc_nodename,
3724
			   sizeof(struct lpfc_name)) == 0)
已提交
3725 3726 3727 3728 3729 3730 3731 3732
			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) {
3733
				ndlp->nlp_prev_state = ndlp->nlp_state;
J
James Smart 已提交
3734
				lpfc_nlp_set_state(vport, ndlp,
3735
						   NLP_STE_PLOGI_ISSUE);
J
James Smart 已提交
3736
				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
已提交
3737 3738 3739
			}

			/* Send a FARP response to that node */
J
James Smart 已提交
3740 3741
			if (fp->Rflags & FARP_REQUEST_FARPR)
				lpfc_issue_els_farpr(vport, did, 0);
已提交
3742 3743
		}
	}
3744
	return 0;
已提交
3745 3746 3747
}

static int
J
James Smart 已提交
3748 3749
lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		   struct lpfc_nodelist  *ndlp)
已提交
3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762
{
	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> */
3763 3764
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0600 FARP-RSP received from DID x%x\n", did);
已提交
3765
	/* ACCEPT the Farp resp request */
3766
	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
已提交
3767 3768 3769 3770 3771

	return 0;
}

static int
J
James Smart 已提交
3772 3773
lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
		 struct lpfc_nodelist *fan_ndlp)
已提交
3774
{
3775
	struct lpfc_hba *phba = vport->phba;
已提交
3776
	uint32_t *lp;
3777
	FAN *fp;
已提交
3778

3779 3780 3781
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
	lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
	fp = (FAN *) ++lp;
3782
	/* FAN received; Fan does not have a reply sequence */
3783 3784
	if ((vport == phba->pport) &&
	    (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
3785
		if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
3786
			    sizeof(struct lpfc_name))) ||
3787
		    (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
3788 3789
			    sizeof(struct lpfc_name)))) {
			/* This port has switched fabrics. FLOGI is required */
J
James Smart 已提交
3790
			lpfc_initial_flogi(vport);
3791 3792 3793 3794
		} else {
			/* FAN verified - skip FLOGI */
			vport->fc_myDID = vport->fc_prevDID;
			lpfc_issue_fabric_reglogin(vport);
3795
		}
已提交
3796
	}
3797
	return 0;
已提交
3798 3799 3800 3801 3802
}

void
lpfc_els_timeout(unsigned long ptr)
{
J
James Smart 已提交
3803 3804
	struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
	struct lpfc_hba   *phba = vport->phba;
3805
	uint32_t tmo_posted;
已提交
3806 3807
	unsigned long iflag;

J
James Smart 已提交
3808
	spin_lock_irqsave(&vport->work_port_lock, iflag);
3809 3810
	tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
	if (!tmo_posted)
J
James Smart 已提交
3811
		vport->work_port_events |= WORKER_ELS_TMO;
3812
	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3813

3814 3815
	if (!tmo_posted)
		lpfc_worker_wake_up(phba);
已提交
3816 3817 3818 3819
	return;
}

void
J
James Smart 已提交
3820
lpfc_els_timeout_handler(struct lpfc_vport *vport)
已提交
3821
{
J
James Smart 已提交
3822
	struct lpfc_hba  *phba = vport->phba;
已提交
3823 3824 3825 3826
	struct lpfc_sli_ring *pring;
	struct lpfc_iocbq *tmp_iocb, *piocb;
	IOCB_t *cmd = NULL;
	struct lpfc_dmabuf *pcmd;
J
James Smart 已提交
3827
	uint32_t els_command = 0;
已提交
3828
	uint32_t timeout;
J
James Smart 已提交
3829
	uint32_t remote_ID = 0xffffffff;
已提交
3830 3831

	/* If the timer is already canceled do nothing */
J
James Smart 已提交
3832
	if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
已提交
3833 3834
		return;
	}
J
James Smart 已提交
3835
	spin_lock_irq(&phba->hbalock);
已提交
3836 3837 3838 3839 3840 3841 3842
	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 已提交
3843 3844 3845
		if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
		    piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
		    piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
已提交
3846
			continue;
J
James Smart 已提交
3847 3848 3849 3850

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

已提交
3851
		pcmd = (struct lpfc_dmabuf *) piocb->context2;
J
James Smart 已提交
3852 3853
		if (pcmd)
			els_command = *(uint32_t *) (pcmd->virt);
已提交
3854

3855 3856 3857 3858 3859 3860
		if (els_command == ELS_CMD_FARP ||
		    els_command == ELS_CMD_FARPR ||
		    els_command == ELS_CMD_FDISC)
			continue;

		if (vport != piocb->vport)
已提交
3861 3862 3863
			continue;

		if (piocb->drvrTimeout > 0) {
3864
			if (piocb->drvrTimeout >= timeout)
已提交
3865
				piocb->drvrTimeout -= timeout;
3866
			else
已提交
3867 3868 3869 3870
				piocb->drvrTimeout = 0;
			continue;
		}

J
James Smart 已提交
3871 3872
		remote_ID = 0xffffffff;
		if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
已提交
3873
			remote_ID = cmd->un.elsreq64.remoteID;
J
James Smart 已提交
3874 3875 3876
		else {
			struct lpfc_nodelist *ndlp;
			ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
3877
			if (ndlp && NLP_CHK_NODE_ACT(ndlp))
J
James Smart 已提交
3878
				remote_ID = ndlp->nlp_DID;
已提交
3879
		}
3880 3881 3882 3883
		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);
3884
		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
已提交
3885
	}
J
James Smart 已提交
3886
	spin_unlock_irq(&phba->hbalock);
3887

J
James Smart 已提交
3888 3889
	if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
		mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
已提交
3890 3891 3892
}

void
J
James Smart 已提交
3893
lpfc_els_flush_cmd(struct lpfc_vport *vport)
已提交
3894
{
3895
	LIST_HEAD(completions);
J
James Smart 已提交
3896
	struct lpfc_hba  *phba = vport->phba;
3897
	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
已提交
3898 3899
	struct lpfc_iocbq *tmp_iocb, *piocb;
	IOCB_t *cmd = NULL;
3900 3901

	lpfc_fabric_abort_vport(vport);
已提交
3902

J
James Smart 已提交
3903
	spin_lock_irq(&phba->hbalock);
已提交
3904 3905 3906 3907 3908 3909 3910 3911
	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 */
3912 3913 3914 3915
		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)
已提交
3916 3917
			continue;

J
James Smart 已提交
3918 3919 3920
		if (piocb->vport != vport)
			continue;

3921
		list_move_tail(&piocb->list, &completions);
3922
		pring->txq_cnt--;
已提交
3923 3924 3925 3926 3927 3928 3929
	}

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

J
James Smart 已提交
3930 3931 3932
		if (piocb->vport != vport)
			continue;

3933
		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
已提交
3934
	}
J
James Smart 已提交
3935
	spin_unlock_irq(&phba->hbalock);
3936

J
James Smart 已提交
3937
	while (!list_empty(&completions)) {
3938 3939
		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
		cmd = &piocb->iocb;
3940
		list_del_init(&piocb->list);
3941

J
James Smart 已提交
3942 3943 3944
		if (!piocb->iocb_cmpl)
			lpfc_sli_release_iocbq(phba, piocb);
		else {
3945 3946 3947
			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
			(piocb->iocb_cmpl) (phba, piocb, piocb);
J
James Smart 已提交
3948
		}
3949 3950
	}

已提交
3951 3952 3953
	return;
}

3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997
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;
}

3998 3999
static void
lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4000
		      struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
已提交
4001
{
4002
	struct Scsi_Host  *shost;
已提交
4003 4004
	struct lpfc_nodelist *ndlp;
	struct ls_rjt stat;
4005
	uint32_t *payload;
J
James Smart 已提交
4006
	uint32_t cmd, did, newnode, rjt_err = 0;
4007
	IOCB_t *icmd = &elsiocb->iocb;
已提交
4008

4009
	if (!vport || !(elsiocb->context2))
已提交
4010
		goto dropit;
J
James Smart 已提交
4011

已提交
4012
	newnode = 0;
4013 4014
	payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
	cmd = *payload;
4015 4016
	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
		lpfc_post_buffer(phba, pring, 1, 1);
已提交
4017

J
James Smart 已提交
4018 4019 4020 4021 4022
	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);
已提交
4023
		goto dropit;
J
James Smart 已提交
4024
	}
已提交
4025 4026

	/* Check to see if link went down during discovery */
4027
	if (lpfc_els_chk_latt(vport))
已提交
4028 4029
		goto dropit;

4030 4031 4032 4033
	/* Ignore traffic recevied during vport shutdown. */
	if (vport->load_flag & FC_UNLOADING)
		goto dropit;

J
James Smart 已提交
4034
	ndlp = lpfc_findnode_did(vport, did);
4035
	if (!ndlp) {
已提交
4036
		/* Cannot find existing Fabric ndlp, so allocate a new one */
4037
		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4038
		if (!ndlp)
已提交
4039 4040
			goto dropit;

J
James Smart 已提交
4041
		lpfc_nlp_init(vport, ndlp, did);
4042
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
已提交
4043
		newnode = 1;
4044
		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
已提交
4045
			ndlp->nlp_type |= NLP_FABRIC;
4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061
	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
		ndlp = lpfc_enable_node(vport, ndlp,
					NLP_STE_UNUSED_NODE);
		if (!ndlp)
			goto dropit;
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		newnode = 1;
		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
			ndlp->nlp_type |= NLP_FABRIC;
	} else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
		/* This is similar to the new node path */
		ndlp = lpfc_nlp_get(ndlp);
		if (!ndlp)
			goto dropit;
		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
		newnode = 1;
4062
	}
已提交
4063 4064

	phba->fc_stat.elsRcvFrame++;
4065 4066
	if (elsiocb->context1)
		lpfc_nlp_put(elsiocb->context1);
4067

4068
	elsiocb->context1 = lpfc_nlp_get(ndlp);
J
James Smart 已提交
4069
	elsiocb->vport = vport;
已提交
4070 4071 4072 4073 4074

	if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
		cmd &= ELS_CMD_MASK;
	}
	/* ELS command <elsCmd> received from NPORT <did> */
4075 4076 4077
	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);
已提交
4078 4079
	switch (cmd) {
	case ELS_CMD_PLOGI:
J
James Smart 已提交
4080 4081 4082 4083
		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);

已提交
4084
		phba->fc_stat.elsRcvPLOGI++;
J
James Smart 已提交
4085 4086 4087
		ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);

		if (vport->port_state < LPFC_DISC_AUTH) {
4088 4089 4090 4091 4092 4093 4094 4095 4096
			if (!(phba->pport->fc_flag & FC_PT2PT) ||
				(phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
				rjt_err = LSRJT_UNABLE_TPC;
				break;
			}
			/* We get here, and drop thru, if we are PT2PT with
			 * another NPort and the other side has initiated
			 * the PLOGI before responding to our FLOGI.
			 */
已提交
4097
		}
4098 4099 4100 4101 4102 4103

		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 已提交
4104 4105
		lpfc_disc_state_machine(vport, ndlp, elsiocb,
					NLP_EVT_RCV_PLOGI);
J
James Smart 已提交
4106

已提交
4107 4108
		break;
	case ELS_CMD_FLOGI:
J
James Smart 已提交
4109 4110 4111 4112
		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);

已提交
4113
		phba->fc_stat.elsRcvFLOGI++;
4114
		lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
4115
		if (newnode)
4116
			lpfc_nlp_put(ndlp);
已提交
4117 4118
		break;
	case ELS_CMD_LOGO:
J
James Smart 已提交
4119 4120 4121 4122
		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);

已提交
4123
		phba->fc_stat.elsRcvLOGO++;
J
James Smart 已提交
4124
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
4125
			rjt_err = LSRJT_UNABLE_TPC;
已提交
4126 4127
			break;
		}
J
James Smart 已提交
4128
		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
已提交
4129 4130
		break;
	case ELS_CMD_PRLO:
J
James Smart 已提交
4131 4132 4133 4134
		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);

已提交
4135
		phba->fc_stat.elsRcvPRLO++;
J
James Smart 已提交
4136
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
4137
			rjt_err = LSRJT_UNABLE_TPC;
已提交
4138 4139
			break;
		}
J
James Smart 已提交
4140
		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
已提交
4141 4142 4143
		break;
	case ELS_CMD_RSCN:
		phba->fc_stat.elsRcvRSCN++;
4144
		lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
4145
		if (newnode)
4146
			lpfc_nlp_put(ndlp);
已提交
4147 4148
		break;
	case ELS_CMD_ADISC:
J
James Smart 已提交
4149 4150 4151 4152
		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);

已提交
4153
		phba->fc_stat.elsRcvADISC++;
J
James Smart 已提交
4154
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
4155
			rjt_err = LSRJT_UNABLE_TPC;
已提交
4156 4157
			break;
		}
J
James Smart 已提交
4158 4159
		lpfc_disc_state_machine(vport, ndlp, elsiocb,
					NLP_EVT_RCV_ADISC);
已提交
4160 4161
		break;
	case ELS_CMD_PDISC:
J
James Smart 已提交
4162 4163 4164 4165
		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);

已提交
4166
		phba->fc_stat.elsRcvPDISC++;
J
James Smart 已提交
4167
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
4168
			rjt_err = LSRJT_UNABLE_TPC;
已提交
4169 4170
			break;
		}
J
James Smart 已提交
4171 4172
		lpfc_disc_state_machine(vport, ndlp, elsiocb,
					NLP_EVT_RCV_PDISC);
已提交
4173 4174
		break;
	case ELS_CMD_FARPR:
J
James Smart 已提交
4175 4176 4177 4178
		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);

已提交
4179
		phba->fc_stat.elsRcvFARPR++;
J
James Smart 已提交
4180
		lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
已提交
4181 4182
		break;
	case ELS_CMD_FARP:
J
James Smart 已提交
4183 4184 4185 4186
		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);

已提交
4187
		phba->fc_stat.elsRcvFARP++;
J
James Smart 已提交
4188
		lpfc_els_rcv_farp(vport, elsiocb, ndlp);
已提交
4189 4190
		break;
	case ELS_CMD_FAN:
J
James Smart 已提交
4191 4192 4193 4194
		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);

已提交
4195
		phba->fc_stat.elsRcvFAN++;
J
James Smart 已提交
4196
		lpfc_els_rcv_fan(vport, elsiocb, ndlp);
已提交
4197 4198
		break;
	case ELS_CMD_PRLI:
J
James Smart 已提交
4199 4200 4201 4202
		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);

已提交
4203
		phba->fc_stat.elsRcvPRLI++;
J
James Smart 已提交
4204
		if (vport->port_state < LPFC_DISC_AUTH) {
J
James Smart 已提交
4205
			rjt_err = LSRJT_UNABLE_TPC;
已提交
4206 4207
			break;
		}
J
James Smart 已提交
4208
		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
已提交
4209
		break;
4210
	case ELS_CMD_LIRR:
J
James Smart 已提交
4211 4212 4213 4214
		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);

4215
		phba->fc_stat.elsRcvLIRR++;
J
James Smart 已提交
4216
		lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
4217
		if (newnode)
4218
			lpfc_nlp_put(ndlp);
4219 4220
		break;
	case ELS_CMD_RPS:
J
James Smart 已提交
4221 4222 4223 4224
		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);

4225
		phba->fc_stat.elsRcvRPS++;
J
James Smart 已提交
4226
		lpfc_els_rcv_rps(vport, elsiocb, ndlp);
4227
		if (newnode)
4228
			lpfc_nlp_put(ndlp);
4229 4230
		break;
	case ELS_CMD_RPL:
J
James Smart 已提交
4231 4232 4233 4234
		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);

4235
		phba->fc_stat.elsRcvRPL++;
J
James Smart 已提交
4236
		lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
4237
		if (newnode)
4238
			lpfc_nlp_put(ndlp);
4239
		break;
已提交
4240
	case ELS_CMD_RNID:
J
James Smart 已提交
4241 4242 4243 4244
		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);

已提交
4245
		phba->fc_stat.elsRcvRNID++;
J
James Smart 已提交
4246
		lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
4247
		if (newnode)
4248
			lpfc_nlp_put(ndlp);
已提交
4249 4250
		break;
	default:
J
James Smart 已提交
4251 4252 4253 4254
		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);

已提交
4255
		/* Unsupported ELS command, reject */
J
James Smart 已提交
4256
		rjt_err = LSRJT_INVALID_CMD;
已提交
4257 4258

		/* Unknown ELS command <elsCmd> received from NPORT <did> */
4259 4260 4261
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0115 Unknown ELS command x%x "
				 "received from NPORT x%x\n", cmd, did);
4262
		if (newnode)
4263
			lpfc_nlp_put(ndlp);
已提交
4264 4265 4266 4267 4268
		break;
	}

	/* check if need to LS_RJT received ELS cmd */
	if (rjt_err) {
4269
		memset(&stat, 0, sizeof(stat));
J
James Smart 已提交
4270
		stat.un.b.lsRjtRsnCode = rjt_err;
4271
		stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
J
James Smart 已提交
4272 4273
		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
			NULL);
已提交
4274 4275
	}

4276 4277 4278
	return;

dropit:
4279 4280
	if (vport && !(vport->load_flag & FC_UNLOADING))
		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
4281
			"(%d):0111 Dropping received ELS cmd "
4282
			"Data: x%x x%x x%x\n",
4283
			vport->vpi, icmd->ulpStatus,
4284
			icmd->un.ulpWord[4], icmd->ulpTimeout);
4285 4286 4287
	phba->fc_stat.elsRcvDrop++;
}

4288 4289 4290 4291
static struct lpfc_vport *
lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
{
	struct lpfc_vport *vport;
4292
	unsigned long flags;
4293

4294
	spin_lock_irqsave(&phba->hbalock, flags);
4295
	list_for_each_entry(vport, &phba->port_list, listentry) {
4296 4297
		if (vport->vpi == vpi) {
			spin_unlock_irqrestore(&phba->hbalock, flags);
4298
			return vport;
4299
		}
4300
	}
4301
	spin_unlock_irqrestore(&phba->hbalock, flags);
4302 4303
	return NULL;
}
4304 4305 4306 4307 4308 4309 4310 4311

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;
4312 4313 4314 4315 4316
	struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
	struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;

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

4318 4319 4320 4321
	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) {
4322 4323
		phba->fc_stat.NoRcvBuf++;
		/* Not enough posted buffers; Try posting more buffers */
4324
		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
4325 4326 4327 4328
			lpfc_post_buffer(phba, pring, 0, 1);
		return;
	}

4329 4330 4331 4332 4333 4334 4335 4336 4337 4338
	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);
		}
	}
4339 4340 4341
	/* If there are no BDEs associated
	 * with this IOCB, there is nothing to do.
	 */
4342 4343 4344
	if (icmd->ulpBdeCount == 0)
		return;

4345 4346 4347
	/* type of ELS cmd is first 32bit word
	 * in packet
	 */
4348
	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4349
		elsiocb->context2 = bdeBuf1;
4350 4351 4352
	} else {
		paddr = getPaddr(icmd->un.cont64[0].addrHigh,
				 icmd->un.cont64[0].addrLow);
4353 4354
		elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
							     paddr);
4355 4356
	}

4357 4358 4359 4360 4361
	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.
	 */
4362 4363
	lpfc_nlp_put(elsiocb->context1);
	elsiocb->context1 = NULL;
已提交
4364
	if (elsiocb->context2) {
4365 4366
		lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
		elsiocb->context2 = NULL;
已提交
4367
	}
4368 4369

	/* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
4370
	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
4371
	    icmd->ulpBdeCount == 2) {
4372 4373
		elsiocb->context2 = bdeBuf2;
		lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
4374 4375
		/* free mp if we are done with it */
		if (elsiocb->context2) {
4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395
			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);
4396 4397
			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
					 "0251 NameServer login: no memory\n");
4398 4399 4400
			return;
		}
		lpfc_nlp_init(vport, ndlp, NameServer_DID);
4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412
	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
		if (!ndlp) {
			if (phba->fc_topology == TOPOLOGY_LOOP) {
				lpfc_disc_start(vport);
				return;
			}
			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
					"0348 NameServer login: node freed\n");
			return;
		}
4413
	}
4414
	ndlp->nlp_type |= NLP_FABRIC;
4415 4416 4417 4418 4419

	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);
4420 4421
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0252 Cannot issue NameServer login\n");
4422 4423 4424
		return;
	}

4425
	if (vport->cfg_fdmi_on) {
4426 4427 4428 4429 4430
		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;
4431 4432
			lpfc_nlp_set_state(vport, ndlp_fdmi,
				NLP_STE_PLOGI_ISSUE);
4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447
			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;

4448
	spin_lock_irq(shost->host_lock);
4449
	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4450
	spin_unlock_irq(shost->host_lock);
4451 4452

	if (mb->mbxStatus) {
4453 4454 4455
		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
				 "0915 Register VPI failed: 0x%x\n",
				 mb->mbxStatus);
4456 4457 4458 4459

		switch (mb->mbxStatus) {
		case 0x11:	/* unsupported feature */
		case 0x9603:	/* max_vpi exceeded */
4460
		case 0x9602:	/* Link event since CLEAR_LA */
4461 4462 4463 4464 4465 4466 4467 4468 4469 4470
			/* 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);
4471
			spin_lock_irq(shost->host_lock);
4472
			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4473
			spin_unlock_irq(shost->host_lock);
4474 4475 4476 4477
			if (vport->port_type == LPFC_PHYSICAL_PORT)
				lpfc_initial_flogi(vport);
			else
				lpfc_initial_fdisc(vport);
4478 4479 4480 4481 4482 4483 4484 4485 4486
			break;
		}

	} else {
		if (vport == phba->pport)
			lpfc_issue_fabric_reglogin(vport);
		else
			lpfc_do_scr_ns_plogi(phba, vport);
	}
4487 4488 4489 4490 4491 4492

	/* Now, we decrement the ndlp reference count held for this
	 * callback function
	 */
	lpfc_nlp_put(ndlp);

4493 4494 4495 4496
	mempool_free(pmb, phba->mbox_mem_pool);
	return;
}

A
Adrian Bunk 已提交
4497
static void
4498 4499 4500
lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
			struct lpfc_nodelist *ndlp)
{
4501
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4502 4503 4504 4505 4506 4507 4508 4509
	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;
4510
		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4511
		    == MBX_NOT_FINISHED) {
4512 4513 4514 4515
			/* mailbox command not success, decrement ndlp
			 * reference count for this command
			 */
			lpfc_nlp_put(ndlp);
4516 4517
			mempool_free(mbox, phba->mbox_mem_pool);

4518 4519
			lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
				"0253 Register VPI: Can't send mbox\n");
4520
			goto mbox_err_exit;
4521 4522
		}
	} else {
4523 4524
		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
				 "0254 Register VPI: no memory\n");
4525
		goto mbox_err_exit;
4526
	}
4527 4528 4529 4530 4531 4532 4533 4534
	return;

mbox_err_exit:
	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
	spin_lock_irq(shost->host_lock);
	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
	spin_unlock_irq(shost->host_lock);
	return;
4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548
}

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;

4549 4550 4551 4552
	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);
4553 4554 4555 4556 4557 4558 4559 4560
	/* 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 已提交
4561 4562 4563 4564
	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);

4565 4566 4567 4568 4569
	if (irsp->ulpStatus) {
		/* Check for retry */
		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
			goto out;
		/* FDISC failed */
4570 4571 4572
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0124 FDISC failed. (%d/%d)\n",
				 irsp->ulpStatus, irsp->un.ulpWord[4]);
4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594
		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) {
4595 4596 4597
				if (!NLP_CHK_NODE_ACT(ndlp) ||
				    (np->nlp_state != NLP_STE_NPR_NODE) ||
				    !(np->nlp_flag & NLP_NPR_ADISC))
4598 4599 4600 4601 4602 4603 4604
					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);
4605
			spin_lock_irq(shost->host_lock);
4606
			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4607
			spin_unlock_irq(shost->host_lock);
4608 4609 4610 4611 4612 4613 4614
		}

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

4615 4616
		/* Unconditionaly kick off releasing fabric node for vports */
		lpfc_nlp_put(ndlp);
4617 4618 4619 4620 4621 4622
	}

out:
	lpfc_els_free_iocb(phba, cmdiocb);
}

A
Adrian Bunk 已提交
4623
static int
4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640
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);
4641 4642
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0255 Issue FDISC: no IOCB\n");
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
		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 已提交
4680 4681 4682 4683
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
		"Issue FDISC:     did:x%x",
		did, 0, 0);

4684 4685 4686 4687
	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);
4688 4689
		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
				 "0256 Issue FDISC: Cannot send IOCB\n");
4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701
		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 已提交
4702
	IOCB_t *irsp;
4703 4704
	struct lpfc_nodelist *ndlp;
	ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
J
James Smart 已提交
4705 4706 4707 4708 4709

	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);
4710 4711 4712

	lpfc_els_free_iocb(phba, cmdiocb);
	vport->unreg_vpi_cmpl = VPORT_ERROR;
4713 4714 4715

	/* Trigger the release of the ndlp after logo */
	lpfc_nlp_put(ndlp);
4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744
}

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 已提交
4745 4746 4747 4748
	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);

4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768
	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;
4769

4770 4771 4772 4773 4774 4775
	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);

4776 4777 4778
	if (!tmo_posted)
		lpfc_worker_wake_up(phba);
	return;
4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792
}

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);
4793
	/* Post any pending iocb to the SLI layer */
4794 4795 4796 4797
	if (atomic_read(&phba->fabric_iocb_count) == 0) {
		list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
				 list);
		if (iocb)
4798
			/* Increment fabric iocb count to hold the position */
4799 4800 4801 4802 4803 4804 4805 4806
			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 已提交
4807 4808 4809 4810
		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
			"Fabric sched1:   ste:x%x",
			iocb->vport->port_state, 0, 0);

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 4840 4841 4842 4843 4844
		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);
4845
	/* Start a timer to unblock fabric iocbs after 100ms */
4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865
	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);
4866
			}
4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892
			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)) {
4893 4894
		/* Post any pending iocbs to HBA */
		lpfc_resume_fabric_iocbs(phba);
4895 4896 4897
	}
}

A
Adrian Bunk 已提交
4898
static int
4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912
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);

4913 4914 4915
	if (ready)
		/* Increment fabric iocb count to hold the position */
		atomic_inc(&phba->fabric_iocb_count);
4916 4917 4918 4919 4920 4921
	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 已提交
4922 4923 4924 4925
		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
			"Fabric sched2:   ste:x%x",
			iocb->vport->port_state, 0, 0);

4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943
		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 已提交
4944
static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986
{
	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);
4987
		}
已提交
4988
	}
4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020
	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);
	}
已提交
5021
}