File Index
Files and directories
Sign in
FILE

lpfc_els.c

File preview with syntax highlighting.

linux-6.19.14/drivers/scsi/lpfc/lpfc_els.c C
    1/*******************************************************************
    2 * This file is part of the Emulex Linux Device Driver for         *
    3 * Fibre Channel Host Bus Adapters.                                *
    4 * Copyright (C) 2017-2025 Broadcom. All Rights Reserved. The term *
    5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
    6 * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
    7 * EMULEX and SLI are trademarks of Emulex.                        *
    8 * www.broadcom.com                                                *
    9 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
   10 *                                                                 *
   11 * This program is free software; you can redistribute it and/or   *
   12 * modify it under the terms of version 2 of the GNU General       *
   13 * Public License as published by the Free Software Foundation.    *
   14 * This program is distributed in the hope that it will be useful. *
   15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
   16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
   17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
   18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
   19 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
   20 * more details, a copy of which can be found in the file COPYING  *
   21 * included with this package.                                     *
   22 *******************************************************************/
   23/* See Fibre Channel protocol T11 FC-LS for details */
   24#include <linux/blkdev.h>
   25#include <linux/pci.h>
   26#include <linux/slab.h>
   27#include <linux/interrupt.h>
   28#include <linux/delay.h>
   29
   30#include <scsi/scsi.h>
   31#include <scsi/scsi_device.h>
   32#include <scsi/scsi_host.h>
   33#include <scsi/scsi_transport_fc.h>
   34#include <uapi/scsi/fc/fc_fs.h>
   35#include <uapi/scsi/fc/fc_els.h>
   36
   37#include "lpfc_hw4.h"
   38#include "lpfc_hw.h"
   39#include "lpfc_sli.h"
   40#include "lpfc_sli4.h"
   41#include "lpfc_nl.h"
   42#include "lpfc_disc.h"
   43#include "lpfc_scsi.h"
   44#include "lpfc.h"
   45#include "lpfc_logmsg.h"
   46#include "lpfc_crtn.h"
   47#include "lpfc_vport.h"
   48#include "lpfc_debugfs.h"
   49
   50static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
   51			  struct lpfc_iocbq *);
   52static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
   53			struct lpfc_iocbq *);
   54static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
   55static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
   56				struct lpfc_nodelist *ndlp, uint8_t retry);
   57static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
   58				  struct lpfc_iocbq *iocb);
   59static void lpfc_cmpl_els_edc(struct lpfc_hba *phba,
   60			      struct lpfc_iocbq *cmdiocb,
   61			      struct lpfc_iocbq *rspiocb);
   62static void lpfc_cmpl_els_uvem(struct lpfc_hba *, struct lpfc_iocbq *,
   63			       struct lpfc_iocbq *);
   64
   65static int lpfc_max_els_tries = 3;
   66
   67static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport);
   68static void lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max);
   69static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid);
   70
   71/**
   72 * lpfc_els_chk_latt - Check host link attention event for a vport
   73 * @vport: pointer to a host virtual N_Port data structure.
   74 *
   75 * This routine checks whether there is an outstanding host link
   76 * attention event during the discovery process with the @vport. It is done
   77 * by reading the HBA's Host Attention (HA) register. If there is any host
   78 * link attention events during this @vport's discovery process, the @vport
   79 * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
   80 * be issued if the link state is not already in host link cleared state,
   81 * and a return code shall indicate whether the host link attention event
   82 * had happened.
   83 *
   84 * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
   85 * state in LPFC_VPORT_READY, the request for checking host link attention
   86 * event will be ignored and a return code shall indicate no host link
   87 * attention event had happened.
   88 *
   89 * Return codes
   90 *   0 - no host link attention event happened
   91 *   1 - host link attention event happened
   92 **/
   93int
   94lpfc_els_chk_latt(struct lpfc_vport *vport)
   95{
   96	struct lpfc_hba  *phba = vport->phba;
   97	uint32_t ha_copy;
   98
   99	if (vport->port_state >= LPFC_VPORT_READY ||
  100	    phba->link_state == LPFC_LINK_DOWN ||
  101	    phba->sli_rev > LPFC_SLI_REV3)
  102		return 0;
  103
  104	/* Read the HBA Host Attention Register */
  105	if (lpfc_readl(phba->HAregaddr, &ha_copy))
  106		return 1;
  107
  108	if (!(ha_copy & HA_LATT))
  109		return 0;
  110
  111	/* Pending Link Event during Discovery */
  112	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  113			 "0237 Pending Link Event during "
  114			 "Discovery: State x%x\n",
  115			 phba->pport->port_state);
  116
  117	/* CLEAR_LA should re-enable link attention events and
  118	 * we should then immediately take a LATT event. The
  119	 * LATT processing should call lpfc_linkdown() which
  120	 * will cleanup any left over in-progress discovery
  121	 * events.
  122	 */
  123	set_bit(FC_ABORT_DISCOVERY, &vport->fc_flag);
  124
  125	if (phba->link_state != LPFC_CLEAR_LA)
  126		lpfc_issue_clear_la(phba, vport);
  127
  128	return 1;
  129}
  130
  131static bool lpfc_is_els_acc_rsp(struct lpfc_dmabuf *buf)
  132{
  133	struct fc_els_ls_acc *rsp = buf->virt;
  134
  135	if (rsp && rsp->la_cmd == ELS_LS_ACC)
  136		return true;
  137	return false;
  138}
  139
  140/**
  141 * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
  142 * @vport: pointer to a host virtual N_Port data structure.
  143 * @expect_rsp: flag indicating whether response is expected.
  144 * @cmd_size: size of the ELS command.
  145 * @retry: number of retries to the command when it fails.
  146 * @ndlp: pointer to a node-list data structure.
  147 * @did: destination identifier.
  148 * @elscmd: the ELS command code.
  149 *
  150 * This routine is used for allocating a lpfc-IOCB data structure from
  151 * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
  152 * passed into the routine for discovery state machine to issue an Extended
  153 * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
  154 * and preparation routine that is used by all the discovery state machine
  155 * routines and the ELS command-specific fields will be later set up by
  156 * the individual discovery machine routines after calling this routine
  157 * allocating and preparing a generic IOCB data structure. It fills in the
  158 * Buffer Descriptor Entries (BDEs), allocates buffers for both command
  159 * payload and response payload (if expected). The reference count on the
  160 * ndlp is incremented by 1 and the reference to the ndlp is put into
  161 * ndlp of the IOCB data structure for this IOCB to hold the ndlp
  162 * reference for the command's callback function to access later.
  163 *
  164 * Return code
  165 *   Pointer to the newly allocated/prepared els iocb data structure
  166 *   NULL - when els iocb data structure allocation/preparation failed
  167 **/
  168struct lpfc_iocbq *
  169lpfc_prep_els_iocb(struct lpfc_vport *vport, u8 expect_rsp,
  170		   u16 cmd_size, u8 retry,
  171		   struct lpfc_nodelist *ndlp, u32 did,
  172		   u32 elscmd)
  173{
  174	struct lpfc_hba  *phba = vport->phba;
  175	struct lpfc_iocbq *elsiocb;
  176	struct lpfc_dmabuf *pcmd, *prsp, *pbuflist, *bmp;
  177	struct ulp_bde64_le *bpl;
  178	u32 timeout = 0;
  179
  180	if (!lpfc_is_link_up(phba))
  181		return NULL;
  182
  183	/* Allocate buffer for  command iocb */
  184	elsiocb = lpfc_sli_get_iocbq(phba);
  185	if (!elsiocb)
  186		return NULL;
  187
  188	/*
  189	 * If this command is for fabric controller and HBA running
  190	 * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
  191	 */
  192	if (did == Fabric_DID &&
  193	    test_bit(HBA_FIP_SUPPORT, &phba->hba_flag) &&
  194	    (elscmd == ELS_CMD_FLOGI ||
  195	     elscmd == ELS_CMD_FDISC ||
  196	     elscmd == ELS_CMD_LOGO))
  197		switch (elscmd) {
  198		case ELS_CMD_FLOGI:
  199			elsiocb->cmd_flag |=
  200				((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
  201				 & LPFC_FIP_ELS_ID_MASK);
  202			break;
  203		case ELS_CMD_FDISC:
  204			elsiocb->cmd_flag |=
  205				((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
  206				 & LPFC_FIP_ELS_ID_MASK);
  207			break;
  208		case ELS_CMD_LOGO:
  209			elsiocb->cmd_flag |=
  210				((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
  211				 & LPFC_FIP_ELS_ID_MASK);
  212			break;
  213		}
  214	else
  215		elsiocb->cmd_flag &= ~LPFC_FIP_ELS_ID_MASK;
  216
  217	/* fill in BDEs for command */
  218	/* Allocate buffer for command payload */
  219	pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
  220	if (pcmd)
  221		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
  222	if (!pcmd || !pcmd->virt)
  223		goto els_iocb_free_pcmb_exit;
  224
  225	INIT_LIST_HEAD(&pcmd->list);
  226
  227	/* Allocate buffer for response payload */
  228	if (expect_rsp) {
  229		prsp = kmalloc(sizeof(*prsp), GFP_KERNEL);
  230		if (prsp)
  231			prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
  232						     &prsp->phys);
  233		if (!prsp || !prsp->virt)
  234			goto els_iocb_free_prsp_exit;
  235		INIT_LIST_HEAD(&prsp->list);
  236	} else {
  237		prsp = NULL;
  238	}
  239
  240	/* Allocate buffer for Buffer ptr list */
  241	pbuflist = kmalloc(sizeof(*pbuflist), GFP_KERNEL);
  242	if (pbuflist)
  243		pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
  244						 &pbuflist->phys);
  245	if (!pbuflist || !pbuflist->virt)
  246		goto els_iocb_free_pbuf_exit;
  247
  248	INIT_LIST_HEAD(&pbuflist->list);
  249
  250	if (expect_rsp) {
  251		switch (elscmd) {
  252		case ELS_CMD_FLOGI:
  253			timeout = FF_DEF_RATOV * 2;
  254			break;
  255		case ELS_CMD_LOGO:
  256			timeout = phba->fc_ratov;
  257			break;
  258		default:
  259			timeout = phba->fc_ratov * 2;
  260		}
  261
  262		/* Fill SGE for the num bde count */
  263		elsiocb->num_bdes = 2;
  264	}
  265
  266	if (phba->sli_rev == LPFC_SLI_REV4)
  267		bmp = pcmd;
  268	else
  269		bmp = pbuflist;
  270
  271	lpfc_sli_prep_els_req_rsp(phba, elsiocb, vport, bmp, cmd_size, did,
  272				  elscmd, timeout, expect_rsp);
  273
  274	bpl = (struct ulp_bde64_le *)pbuflist->virt;
  275	bpl->addr_low = cpu_to_le32(putPaddrLow(pcmd->phys));
  276	bpl->addr_high = cpu_to_le32(putPaddrHigh(pcmd->phys));
  277	bpl->type_size = cpu_to_le32(cmd_size);
  278	bpl->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
  279
  280	if (expect_rsp) {
  281		bpl++;
  282		bpl->addr_low = cpu_to_le32(putPaddrLow(prsp->phys));
  283		bpl->addr_high = cpu_to_le32(putPaddrHigh(prsp->phys));
  284		bpl->type_size = cpu_to_le32(FCELSSIZE);
  285		bpl->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
  286	}
  287
  288	elsiocb->cmd_dmabuf = pcmd;
  289	elsiocb->bpl_dmabuf = pbuflist;
  290	elsiocb->retry = retry;
  291	elsiocb->vport = vport;
  292	elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
  293
  294	if (prsp)
  295		list_add(&prsp->list, &pcmd->list);
  296	if (expect_rsp) {
  297		/* Xmit ELS command <elsCmd> to remote NPORT <did> */
  298		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
  299				 "0116 Xmit ELS command x%x to remote "
  300				 "NPORT x%x I/O tag: x%x, port state:x%x "
  301				 "rpi x%x fc_flag:x%lx\n",
  302				 elscmd, did, elsiocb->iotag,
  303				 vport->port_state, ndlp->nlp_rpi,
  304				 vport->fc_flag);
  305	} else {
  306		/* Xmit ELS response <elsCmd> to remote NPORT <did> */
  307		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
  308				 "0117 Xmit ELS response x%x to remote "
  309				 "NPORT x%x I/O tag: x%x, size: x%x "
  310				 "port_state x%x  rpi x%x fc_flag x%lx\n",
  311				 elscmd, ndlp->nlp_DID, elsiocb->iotag,
  312				 cmd_size, vport->port_state,
  313				 ndlp->nlp_rpi, vport->fc_flag);
  314	}
  315
  316	return elsiocb;
  317
  318els_iocb_free_pbuf_exit:
  319	if (expect_rsp)
  320		lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
  321	kfree(pbuflist);
  322
  323els_iocb_free_prsp_exit:
  324	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
  325	kfree(prsp);
  326
  327els_iocb_free_pcmb_exit:
  328	kfree(pcmd);
  329	lpfc_sli_release_iocbq(phba, elsiocb);
  330	return NULL;
  331}
  332
  333/**
  334 * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
  335 * @vport: pointer to a host virtual N_Port data structure.
  336 *
  337 * This routine issues a fabric registration login for a @vport. An
  338 * active ndlp node with Fabric_DID must already exist for this @vport.
  339 * The routine invokes two mailbox commands to carry out fabric registration
  340 * login through the HBA firmware: the first mailbox command requests the
  341 * HBA to perform link configuration for the @vport; and the second mailbox
  342 * command requests the HBA to perform the actual fabric registration login
  343 * with the @vport.
  344 *
  345 * Return code
  346 *   0 - successfully issued fabric registration login for @vport
  347 *   -ENXIO -- failed to issue fabric registration login for @vport
  348 **/
  349int
  350lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
  351{
  352	struct lpfc_hba  *phba = vport->phba;
  353	LPFC_MBOXQ_t *mbox;
  354	struct lpfc_nodelist *ndlp;
  355	struct serv_parm *sp;
  356	int rc;
  357	int err = 0;
  358
  359	sp = &phba->fc_fabparam;
  360	ndlp = lpfc_findnode_did(vport, Fabric_DID);
  361	if (!ndlp) {
  362		err = 1;
  363		goto fail;
  364	}
  365
  366	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  367	if (!mbox) {
  368		err = 2;
  369		goto fail;
  370	}
  371
  372	vport->port_state = LPFC_FABRIC_CFG_LINK;
  373	lpfc_config_link(phba, mbox);
  374	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  375	mbox->vport = vport;
  376
  377	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  378	if (rc == MBX_NOT_FINISHED) {
  379		err = 3;
  380		goto fail_free_mbox;
  381	}
  382
  383	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  384	if (!mbox) {
  385		err = 4;
  386		goto fail;
  387	}
  388	rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
  389			  ndlp->nlp_rpi);
  390	if (rc) {
  391		err = 5;
  392		goto fail_free_mbox;
  393	}
  394
  395	mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
  396	mbox->vport = vport;
  397	/* increment the reference count on ndlp to hold reference
  398	 * for the callback routine.
  399	 */
  400	mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
  401	if (!mbox->ctx_ndlp) {
  402		err = 6;
  403		goto fail_free_mbox;
  404	}
  405
  406	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  407	if (rc == MBX_NOT_FINISHED) {
  408		err = 7;
  409		goto fail_issue_reg_login;
  410	}
  411
  412	return 0;
  413
  414fail_issue_reg_login:
  415	/* decrement the reference count on ndlp just incremented
  416	 * for the failed mbox command.
  417	 */
  418	lpfc_nlp_put(ndlp);
  419fail_free_mbox:
  420	lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
  421fail:
  422	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  423	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  424			 "0249 Cannot issue Register Fabric login: Err %d\n",
  425			 err);
  426	return -ENXIO;
  427}
  428
  429/**
  430 * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
  431 * @vport: pointer to a host virtual N_Port data structure.
  432 *
  433 * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
  434 * the @vport. This mailbox command is necessary for SLI4 port only.
  435 *
  436 * Return code
  437 *   0 - successfully issued REG_VFI for @vport
  438 *   A failure code otherwise.
  439 **/
  440int
  441lpfc_issue_reg_vfi(struct lpfc_vport *vport)
  442{
  443	struct lpfc_hba  *phba = vport->phba;
  444	LPFC_MBOXQ_t *mboxq = NULL;
  445	struct lpfc_nodelist *ndlp;
  446	struct lpfc_dmabuf *dmabuf = NULL;
  447	int rc = 0;
  448
  449	/* move forward in case of SLI4 FC port loopback test and pt2pt mode */
  450	if ((phba->sli_rev == LPFC_SLI_REV4) &&
  451	    !(phba->link_flag & LS_LOOPBACK_MODE) &&
  452	    !test_bit(FC_PT2PT, &vport->fc_flag)) {
  453		ndlp = lpfc_findnode_did(vport, Fabric_DID);
  454		if (!ndlp) {
  455			rc = -ENODEV;
  456			goto fail;
  457		}
  458	}
  459
  460	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  461	if (!mboxq) {
  462		rc = -ENOMEM;
  463		goto fail;
  464	}
  465
  466	/* Supply CSP's only if we are fabric connect or pt-to-pt connect */
  467	if (test_bit(FC_FABRIC, &vport->fc_flag) ||
  468	    test_bit(FC_PT2PT, &vport->fc_flag)) {
  469		rc = lpfc_mbox_rsrc_prep(phba, mboxq);
  470		if (rc) {
  471			rc = -ENOMEM;
  472			goto fail_mbox;
  473		}
  474		dmabuf = mboxq->ctx_buf;
  475		memcpy(dmabuf->virt, &phba->fc_fabparam,
  476		       sizeof(struct serv_parm));
  477	}
  478
  479	vport->port_state = LPFC_FABRIC_CFG_LINK;
  480	if (dmabuf) {
  481		lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
  482		/* lpfc_reg_vfi memsets the mailbox.  Restore the ctx_buf. */
  483		mboxq->ctx_buf = dmabuf;
  484	} else {
  485		lpfc_reg_vfi(mboxq, vport, 0);
  486	}
  487
  488	mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
  489	mboxq->vport = vport;
  490	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  491	if (rc == MBX_NOT_FINISHED) {
  492		rc = -ENXIO;
  493		goto fail_mbox;
  494	}
  495	return 0;
  496
  497fail_mbox:
  498	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
  499fail:
  500	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  501	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  502			 "0289 Issue Register VFI failed: Err %d\n", rc);
  503	return rc;
  504}
  505
  506/**
  507 * lpfc_issue_unreg_vfi - Unregister VFI for this vport's fabric login
  508 * @vport: pointer to a host virtual N_Port data structure.
  509 *
  510 * This routine issues a UNREG_VFI mailbox with the vfi, vpi, fcfi triplet for
  511 * the @vport. This mailbox command is necessary for SLI4 port only.
  512 *
  513 * Return code
  514 *   0 - successfully issued REG_VFI for @vport
  515 *   A failure code otherwise.
  516 **/
  517int
  518lpfc_issue_unreg_vfi(struct lpfc_vport *vport)
  519{
  520	struct lpfc_hba *phba = vport->phba;
  521	LPFC_MBOXQ_t *mboxq;
  522	int rc;
  523
  524	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  525	if (!mboxq) {
  526		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  527				"2556 UNREG_VFI mbox allocation failed"
  528				"HBA state x%x\n", phba->pport->port_state);
  529		return -ENOMEM;
  530	}
  531
  532	lpfc_unreg_vfi(mboxq, vport);
  533	mboxq->vport = vport;
  534	mboxq->mbox_cmpl = lpfc_unregister_vfi_cmpl;
  535
  536	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  537	if (rc == MBX_NOT_FINISHED) {
  538		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  539				"2557 UNREG_VFI issue mbox failed rc x%x "
  540				"HBA state x%x\n",
  541				rc, phba->pport->port_state);
  542		mempool_free(mboxq, phba->mbox_mem_pool);
  543		return -EIO;
  544	}
  545
  546	clear_bit(FC_VFI_REGISTERED, &vport->fc_flag);
  547	return 0;
  548}
  549
  550/**
  551 * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
  552 * @vport: pointer to a host virtual N_Port data structure.
  553 * @sp: pointer to service parameter data structure.
  554 *
  555 * This routine is called from FLOGI/FDISC completion handler functions.
  556 * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
  557 * node nodename is changed in the completion service parameter else return
  558 * 0. This function also set flag in the vport data structure to delay
  559 * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
  560 * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
  561 * node nodename is changed in the completion service parameter.
  562 *
  563 * Return code
  564 *   0 - FCID and Fabric Nodename and Fabric portname is not changed.
  565 *   1 - FCID or Fabric Nodename or Fabric portname is changed.
  566 *
  567 **/
  568static uint8_t
  569lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
  570		struct serv_parm *sp)
  571{
  572	struct lpfc_hba *phba = vport->phba;
  573	uint8_t fabric_param_changed = 0;
  574
  575	if ((vport->fc_prevDID != vport->fc_myDID) ||
  576		memcmp(&vport->fabric_portname, &sp->portName,
  577			sizeof(struct lpfc_name)) ||
  578		memcmp(&vport->fabric_nodename, &sp->nodeName,
  579			sizeof(struct lpfc_name)) ||
  580		(vport->vport_flag & FAWWPN_PARAM_CHG)) {
  581		fabric_param_changed = 1;
  582		vport->vport_flag &= ~FAWWPN_PARAM_CHG;
  583	}
  584	/*
  585	 * Word 1 Bit 31 in common service parameter is overloaded.
  586	 * Word 1 Bit 31 in FLOGI request is multiple NPort request
  587	 * Word 1 Bit 31 in FLOGI response is clean address bit
  588	 *
  589	 * If fabric parameter is changed and clean address bit is
  590	 * cleared delay nport discovery if
  591	 * - vport->fc_prevDID != 0 (not initial discovery) OR
  592	 * - lpfc_delay_discovery module parameter is set.
  593	 */
  594	if (fabric_param_changed && !sp->cmn.clean_address_bit &&
  595	    (vport->fc_prevDID || phba->cfg_delay_discovery))
  596		set_bit(FC_DISC_DELAYED, &vport->fc_flag);
  597
  598	return fabric_param_changed;
  599}
  600
  601
  602/**
  603 * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
  604 * @vport: pointer to a host virtual N_Port data structure.
  605 * @ndlp: pointer to a node-list data structure.
  606 * @sp: pointer to service parameter data structure.
  607 * @ulp_word4: command response value
  608 *
  609 * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
  610 * function to handle the completion of a Fabric Login (FLOGI) into a fabric
  611 * port in a fabric topology. It properly sets up the parameters to the @ndlp
  612 * from the IOCB response. It also check the newly assigned N_Port ID to the
  613 * @vport against the previously assigned N_Port ID. If it is different from
  614 * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
  615 * is invoked on all the remaining nodes with the @vport to unregister the
  616 * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
  617 * is invoked to register login to the fabric.
  618 *
  619 * Return code
  620 *   0 - Success (currently, always return 0)
  621 **/
  622static int
  623lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  624			   struct serv_parm *sp, uint32_t ulp_word4)
  625{
  626	struct lpfc_hba  *phba = vport->phba;
  627	struct lpfc_nodelist *np;
  628	struct lpfc_nodelist *next_np;
  629	uint8_t fabric_param_changed;
  630
  631	set_bit(FC_FABRIC, &vport->fc_flag);
  632
  633	phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
  634	if (sp->cmn.edtovResolution)	/* E_D_TOV ticks are in nanoseconds */
  635		phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
  636
  637	phba->fc_edtovResol = sp->cmn.edtovResolution;
  638	phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
  639
  640	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP)
  641		set_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
  642
  643	vport->fc_myDID = ulp_word4 & Mask_DID;
  644	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
  645	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
  646	ndlp->nlp_class_sup = 0;
  647	if (sp->cls1.classValid)
  648		ndlp->nlp_class_sup |= FC_COS_CLASS1;
  649	if (sp->cls2.classValid)
  650		ndlp->nlp_class_sup |= FC_COS_CLASS2;
  651	if (sp->cls3.classValid)
  652		ndlp->nlp_class_sup |= FC_COS_CLASS3;
  653	ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
  654				sp->cmn.bbRcvSizeLsb;
  655
  656	fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
  657	if (fabric_param_changed) {
  658		/* Reset FDMI attribute masks based on config parameter */
  659		if (phba->cfg_enable_SmartSAN ||
  660		    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
  661			/* Setup appropriate attribute masks */
  662			vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
  663			if (phba->cfg_enable_SmartSAN)
  664				vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
  665			else
  666				vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
  667		} else {
  668			vport->fdmi_hba_mask = 0;
  669			vport->fdmi_port_mask = 0;
  670		}
  671
  672	}
  673	memcpy(&vport->fabric_portname, &sp->portName,
  674			sizeof(struct lpfc_name));
  675	memcpy(&vport->fabric_nodename, &sp->nodeName,
  676			sizeof(struct lpfc_name));
  677	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
  678
  679	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
  680		if (sp->cmn.response_multiple_NPort) {
  681			lpfc_printf_vlog(vport, KERN_WARNING,
  682					 LOG_ELS | LOG_VPORT,
  683					 "1816 FLOGI NPIV supported, "
  684					 "response data 0x%x\n",
  685					 sp->cmn.response_multiple_NPort);
  686			spin_lock_irq(&phba->hbalock);
  687			phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
  688			spin_unlock_irq(&phba->hbalock);
  689		} else {
  690			/* Because we asked f/w for NPIV it still expects us
  691			to call reg_vnpid at least for the physical host */
  692			lpfc_printf_vlog(vport, KERN_WARNING,
  693					 LOG_ELS | LOG_VPORT,
  694					 "1817 Fabric does not support NPIV "
  695					 "- configuring single port mode.\n");
  696			spin_lock_irq(&phba->hbalock);
  697			phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
  698			spin_unlock_irq(&phba->hbalock);
  699		}
  700	}
  701
  702	/*
  703	 * For FC we need to do some special processing because of the SLI
  704	 * Port's default settings of the Common Service Parameters.
  705	 */
  706	if ((phba->sli_rev == LPFC_SLI_REV4) &&
  707	    (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)) {
  708		/* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
  709		if (fabric_param_changed)
  710			lpfc_unregister_fcf_prep(phba);
  711
  712		/* This should just update the VFI CSPs*/
  713		if (test_bit(FC_VFI_REGISTERED, &vport->fc_flag))
  714			lpfc_issue_reg_vfi(vport);
  715	}
  716
  717	if (fabric_param_changed &&
  718		!test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
  719
  720		/* If our NportID changed, we need to ensure all
  721		 * remaining NPORTs get unreg_login'ed.
  722		 */
  723		list_for_each_entry_safe(np, next_np,
  724					&vport->fc_nodes, nlp_listp) {
  725			if ((np->nlp_state != NLP_STE_NPR_NODE) ||
  726			    !test_bit(NLP_NPR_ADISC, &np->nlp_flag))
  727				continue;
  728			clear_bit(NLP_NPR_ADISC, &np->nlp_flag);
  729			lpfc_unreg_rpi(vport, np);
  730		}
  731		lpfc_cleanup_pending_mbox(vport);
  732
  733		if (phba->sli_rev == LPFC_SLI_REV4) {
  734			lpfc_sli4_unreg_all_rpis(vport);
  735			lpfc_mbx_unreg_vpi(vport);
  736			set_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag);
  737		}
  738
  739		/*
  740		 * For SLI3 and SLI4, the VPI needs to be reregistered in
  741		 * response to this fabric parameter change event.
  742		 */
  743		set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
  744	} else if ((phba->sli_rev == LPFC_SLI_REV4) &&
  745		   !test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
  746			/*
  747			 * Driver needs to re-reg VPI in order for f/w
  748			 * to update the MAC address.
  749			 */
  750			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  751			lpfc_register_new_vport(phba, vport, ndlp);
  752			return 0;
  753	}
  754
  755	if (phba->sli_rev < LPFC_SLI_REV4) {
  756		lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
  757		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
  758		    test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag))
  759			lpfc_register_new_vport(phba, vport, ndlp);
  760		else
  761			lpfc_issue_fabric_reglogin(vport);
  762	} else {
  763		ndlp->nlp_type |= NLP_FABRIC;
  764		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  765		if ((!test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) &&
  766		    (vport->vpi_state & LPFC_VPI_REGISTERED)) {
  767			lpfc_start_fdiscs(phba);
  768			lpfc_do_scr_ns_plogi(phba, vport);
  769		} else if (test_bit(FC_VFI_REGISTERED, &vport->fc_flag))
  770			lpfc_issue_init_vpi(vport);
  771		else {
  772			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
  773					"3135 Need register VFI: (x%x/%x)\n",
  774					vport->fc_prevDID, vport->fc_myDID);
  775			lpfc_issue_reg_vfi(vport);
  776		}
  777	}
  778	return 0;
  779}
  780
  781/**
  782 * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
  783 * @vport: pointer to a host virtual N_Port data structure.
  784 * @ndlp: pointer to a node-list data structure.
  785 * @sp: pointer to service parameter data structure.
  786 *
  787 * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
  788 * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
  789 * in a point-to-point topology. First, the @vport's N_Port Name is compared
  790 * with the received N_Port Name: if the @vport's N_Port Name is greater than
  791 * the received N_Port Name lexicographically, this node shall assign local
  792 * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
  793 * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
  794 * this node shall just wait for the remote node to issue PLOGI and assign
  795 * N_Port IDs.
  796 *
  797 * Return code
  798 *   0 - Success
  799 *   -ENXIO - Fail
  800 **/
  801static int
  802lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  803			  struct serv_parm *sp)
  804{
  805	struct lpfc_hba  *phba = vport->phba;
  806	LPFC_MBOXQ_t *mbox;
  807	int rc;
  808
  809	clear_bit(FC_FABRIC, &vport->fc_flag);
  810	clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
  811	set_bit(FC_PT2PT, &vport->fc_flag);
  812
  813	/* If we are pt2pt with another NPort, force NPIV off! */
  814	phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
  815
  816	/* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
  817	if ((phba->sli_rev == LPFC_SLI_REV4) && phba->fc_topology_changed) {
  818		lpfc_unregister_fcf_prep(phba);
  819		clear_bit(FC_VFI_REGISTERED, &vport->fc_flag);
  820		phba->fc_topology_changed = 0;
  821	}
  822
  823	rc = memcmp(&vport->fc_portname, &sp->portName,
  824		    sizeof(vport->fc_portname));
  825
  826	if (rc >= 0) {
  827		/* This side will initiate the PLOGI */
  828		set_bit(FC_PT2PT_PLOGI, &vport->fc_flag);
  829
  830		/*
  831		 * N_Port ID cannot be 0, set our Id to LocalID
  832		 * the other side will be RemoteID.
  833		 */
  834
  835		/* not equal */
  836		if (rc)
  837			vport->fc_myDID = PT2PT_LocalID;
  838
  839		/* If not registered with a transport, decrement ndlp reference
  840		 * count indicating that ndlp can be safely released when other
  841		 * references are removed.
  842		 */
  843		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)))
  844			lpfc_nlp_put(ndlp);
  845
  846		ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
  847		if (!ndlp) {
  848			/*
  849			 * Cannot find existing Fabric ndlp, so allocate a
  850			 * new one
  851			 */
  852			ndlp = lpfc_nlp_init(vport, PT2PT_RemoteID);
  853			if (!ndlp)
  854				goto fail;
  855		}
  856
  857		memcpy(&ndlp->nlp_portname, &sp->portName,
  858		       sizeof(struct lpfc_name));
  859		memcpy(&ndlp->nlp_nodename, &sp->nodeName,
  860		       sizeof(struct lpfc_name));
  861		/* Set state will put ndlp onto node list if not already done */
  862		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  863		set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
  864
  865		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  866		if (!mbox)
  867			goto fail;
  868
  869		lpfc_config_link(phba, mbox);
  870
  871		mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
  872		mbox->vport = vport;
  873		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  874		if (rc == MBX_NOT_FINISHED) {
  875			mempool_free(mbox, phba->mbox_mem_pool);
  876			goto fail;
  877		}
  878	} else {
  879		/* This side will wait for the PLOGI. If not registered with
  880		 * a transport, decrement node reference count indicating that
  881		 * ndlp can be released when other references are removed.
  882		 */
  883		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)))
  884			lpfc_nlp_put(ndlp);
  885
  886		/* Start discovery - this should just do CLEAR_LA */
  887		lpfc_disc_start(vport);
  888	}
  889
  890	return 0;
  891fail:
  892	return -ENXIO;
  893}
  894
  895/**
  896 * lpfc_cmpl_els_flogi - Completion callback function for flogi
  897 * @phba: pointer to lpfc hba data structure.
  898 * @cmdiocb: pointer to lpfc command iocb data structure.
  899 * @rspiocb: pointer to lpfc response iocb data structure.
  900 *
  901 * This routine is the top-level completion callback function for issuing
  902 * a Fabric Login (FLOGI) command. If the response IOCB reported error,
  903 * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
  904 * retry has been made (either immediately or delayed with lpfc_els_retry()
  905 * returning 1), the command IOCB will be released and function returned.
  906 * If the retry attempt has been given up (possibly reach the maximum
  907 * number of retries), one additional decrement of ndlp reference shall be
  908 * invoked before going out after releasing the command IOCB. This will
  909 * actually release the remote node (Note, lpfc_els_free_iocb() will also
  910 * invoke one decrement of ndlp reference count). If no error reported in
  911 * the IOCB status, the command Port ID field is used to determine whether
  912 * this is a point-to-point topology or a fabric topology: if the Port ID
  913 * field is assigned, it is a fabric topology; otherwise, it is a
  914 * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
  915 * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
  916 * specific topology completion conditions.
  917 **/
  918static void
  919lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  920		    struct lpfc_iocbq *rspiocb)
  921{
  922	struct lpfc_vport *vport = cmdiocb->vport;
  923	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
  924	IOCB_t *irsp;
  925	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf, *prsp;
  926	struct serv_parm *sp;
  927	uint16_t fcf_index;
  928	int rc;
  929	u32 ulp_status, ulp_word4, tmo;
  930	bool flogi_in_retry = false;
  931
  932	/* Check to see if link went down during discovery */
  933	if (lpfc_els_chk_latt(vport)) {
  934		/* One additional decrement on node reference count to
  935		 * trigger the release of the node.  Make sure the ndlp
  936		 * is marked NLP_DROPPED.
  937		 */
  938		if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag) &&
  939		    !test_bit(NLP_DROPPED, &ndlp->nlp_flag) &&
  940		    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD)) {
  941			set_bit(NLP_DROPPED, &ndlp->nlp_flag);
  942			lpfc_nlp_put(ndlp);
  943		}
  944		goto out;
  945	}
  946
  947	ulp_status = get_job_ulpstatus(phba, rspiocb);
  948	ulp_word4 = get_job_word4(phba, rspiocb);
  949
  950	if (phba->sli_rev == LPFC_SLI_REV4) {
  951		tmo = get_wqe_tmo(cmdiocb);
  952	} else {
  953		irsp = &rspiocb->iocb;
  954		tmo = irsp->ulpTimeout;
  955	}
  956
  957	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  958		"FLOGI cmpl:      status:x%x/x%x state:x%x",
  959		ulp_status, ulp_word4,
  960		vport->port_state);
  961
  962	if (ulp_status) {
  963		/*
  964		 * In case of FIP mode, perform roundrobin FCF failover
  965		 * due to new FCF discovery
  966		 */
  967		if (test_bit(HBA_FIP_SUPPORT, &phba->hba_flag) &&
  968		    (phba->fcf.fcf_flag & FCF_DISCOVERY)) {
  969			if (phba->link_state < LPFC_LINK_UP)
  970				goto stop_rr_fcf_flogi;
  971			if ((phba->fcoe_cvl_eventtag_attn ==
  972			     phba->fcoe_cvl_eventtag) &&
  973			    (ulp_status == IOSTAT_LOCAL_REJECT) &&
  974			    ((ulp_word4 & IOERR_PARAM_MASK) ==
  975			    IOERR_SLI_ABORTED))
  976				goto stop_rr_fcf_flogi;
  977			else
  978				phba->fcoe_cvl_eventtag_attn =
  979					phba->fcoe_cvl_eventtag;
  980			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
  981					"2611 FLOGI FCF (x%x), "
  982					"status:x%x/x%x, tmo:x%x, perform "
  983					"roundrobin FCF failover\n",
  984					phba->fcf.current_rec.fcf_indx,
  985					ulp_status, ulp_word4, tmo);
  986			lpfc_sli4_set_fcf_flogi_fail(phba,
  987					phba->fcf.current_rec.fcf_indx);
  988			fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
  989			rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
  990			if (rc)
  991				goto out;
  992		}
  993
  994stop_rr_fcf_flogi:
  995		/* FLOGI failure */
  996		if (!(ulp_status == IOSTAT_LOCAL_REJECT &&
  997		      ((ulp_word4 & IOERR_PARAM_MASK) ==
  998					IOERR_LOOP_OPEN_FAILURE)))
  999			lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
 1000				      "2858 FLOGI Status:x%x/x%x TMO"
 1001				      ":x%x Data x%lx x%x x%lx x%x\n",
 1002				      ulp_status, ulp_word4, tmo,
 1003				      phba->hba_flag, phba->fcf.fcf_flag,
 1004				      ndlp->nlp_flag, ndlp->fc4_xpt_flags);
 1005
 1006		/* Check for retry */
 1007		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
 1008			/* Address a timing race with dev_loss.  If dev_loss
 1009			 * is active on this FPort node, put the initial ref
 1010			 * count back to stop premature node release actions.
 1011			 */
 1012			lpfc_check_nlp_post_devloss(vport, ndlp);
 1013			flogi_in_retry = true;
 1014			goto out;
 1015		}
 1016
 1017		/* The FLOGI will not be retried.  If the FPort node is not
 1018		 * registered with the SCSI transport, remove the initial
 1019		 * reference to trigger node release.
 1020		 */
 1021		if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag) &&
 1022		    !test_bit(NLP_DROPPED, &ndlp->nlp_flag) &&
 1023		    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD)) {
 1024			set_bit(NLP_DROPPED, &ndlp->nlp_flag);
 1025			lpfc_nlp_put(ndlp);
 1026		}
 1027
 1028		lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
 1029				 "0150 FLOGI Status:x%x/x%x "
 1030				 "xri x%x iotag x%x TMO:x%x refcnt %d\n",
 1031				 ulp_status, ulp_word4, cmdiocb->sli4_xritag,
 1032				 cmdiocb->iotag, tmo, kref_read(&ndlp->kref));
 1033
 1034		/* If this is not a loop open failure, bail out */
 1035		if (!(ulp_status == IOSTAT_LOCAL_REJECT &&
 1036		      ((ulp_word4 & IOERR_PARAM_MASK) ==
 1037					IOERR_LOOP_OPEN_FAILURE))) {
 1038			/* Warn FLOGI status */
 1039			lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
 1040				      "0100 FLOGI Status:x%x/x%x "
 1041				      "TMO:x%x\n",
 1042				      ulp_status, ulp_word4, tmo);
 1043			goto flogifail;
 1044		}
 1045
 1046		/* FLOGI failed, so there is no fabric */
 1047		clear_bit(FC_FABRIC, &vport->fc_flag);
 1048		clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
 1049		clear_bit(FC_PT2PT_NO_NVME, &vport->fc_flag);
 1050
 1051		/* If private loop, then allow max outstanding els to be
 1052		 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
 1053		 * alpa map would take too long otherwise.
 1054		 */
 1055		if (phba->alpa_map[0] == 0)
 1056			vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
 1057		if ((phba->sli_rev == LPFC_SLI_REV4) &&
 1058		    (!test_bit(FC_VFI_REGISTERED, &vport->fc_flag) ||
 1059		     (vport->fc_prevDID != vport->fc_myDID) ||
 1060			phba->fc_topology_changed)) {
 1061			if (test_bit(FC_VFI_REGISTERED, &vport->fc_flag)) {
 1062				if (phba->fc_topology_changed) {
 1063					lpfc_unregister_fcf_prep(phba);
 1064					clear_bit(FC_VFI_REGISTERED,
 1065						  &vport->fc_flag);
 1066					phba->fc_topology_changed = 0;
 1067				} else {
 1068					lpfc_sli4_unreg_all_rpis(vport);
 1069				}
 1070			}
 1071
 1072			/* Do not register VFI if the driver aborted FLOGI */
 1073			if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4))
 1074				lpfc_issue_reg_vfi(vport);
 1075
 1076			goto out;
 1077		}
 1078		goto flogifail;
 1079	}
 1080	clear_bit(FC_VPORT_CVL_RCVD, &vport->fc_flag);
 1081	clear_bit(FC_VPORT_LOGO_RCVD, &vport->fc_flag);
 1082
 1083	/*
 1084	 * The FLOGI succeeded.  Sync the data for the CPU before
 1085	 * accessing it.
 1086	 */
 1087	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
 1088	if (!prsp)
 1089		goto out;
 1090	if (!lpfc_is_els_acc_rsp(prsp))
 1091		goto out;
 1092	sp = prsp->virt + sizeof(uint32_t);
 1093
 1094	/* FLOGI completes successfully */
 1095	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 1096			 "0101 FLOGI completes successfully, I/O tag:x%x "
 1097			 "xri x%x Data: x%x x%x x%x x%x x%x x%lx x%x %d\n",
 1098			 cmdiocb->iotag, cmdiocb->sli4_xritag,
 1099			 ulp_word4, sp->cmn.e_d_tov,
 1100			 sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
 1101			 vport->port_state, vport->fc_flag,
 1102			 sp->cmn.priority_tagging, kref_read(&ndlp->kref));
 1103
 1104	/* reinitialize the VMID datastructure before returning */
 1105	if (lpfc_is_vmid_enabled(phba)) {
 1106		lpfc_reinit_vmid(vport);
 1107		vport->vmid_flag = 0;
 1108	}
 1109	if (sp->cmn.priority_tagging)
 1110		vport->phba->pport->vmid_flag |= (LPFC_VMID_ISSUE_QFPA |
 1111						  LPFC_VMID_TYPE_PRIO);
 1112
 1113	/*
 1114	 * Address a timing race with dev_loss.  If dev_loss is active on
 1115	 * this FPort node, put the initial ref count back to stop premature
 1116	 * node release actions.
 1117	 */
 1118	lpfc_check_nlp_post_devloss(vport, ndlp);
 1119	if (vport->port_state == LPFC_FLOGI) {
 1120		/*
 1121		 * If Common Service Parameters indicate Nport
 1122		 * we are point to point, if Fport we are Fabric.
 1123		 */
 1124		if (sp->cmn.fPort)
 1125			rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp,
 1126							ulp_word4);
 1127		else if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag))
 1128			rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
 1129		else {
 1130			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 1131				"2831 FLOGI response with cleared Fabric "
 1132				"bit fcf_index 0x%x "
 1133				"Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
 1134				"Fabric Name "
 1135				"%02x%02x%02x%02x%02x%02x%02x%02x\n",
 1136				phba->fcf.current_rec.fcf_indx,
 1137				phba->fcf.current_rec.switch_name[0],
 1138				phba->fcf.current_rec.switch_name[1],
 1139				phba->fcf.current_rec.switch_name[2],
 1140				phba->fcf.current_rec.switch_name[3],
 1141				phba->fcf.current_rec.switch_name[4],
 1142				phba->fcf.current_rec.switch_name[5],
 1143				phba->fcf.current_rec.switch_name[6],
 1144				phba->fcf.current_rec.switch_name[7],
 1145				phba->fcf.current_rec.fabric_name[0],
 1146				phba->fcf.current_rec.fabric_name[1],
 1147				phba->fcf.current_rec.fabric_name[2],
 1148				phba->fcf.current_rec.fabric_name[3],
 1149				phba->fcf.current_rec.fabric_name[4],
 1150				phba->fcf.current_rec.fabric_name[5],
 1151				phba->fcf.current_rec.fabric_name[6],
 1152				phba->fcf.current_rec.fabric_name[7]);
 1153
 1154			lpfc_nlp_put(ndlp);
 1155			spin_lock_irq(&phba->hbalock);
 1156			phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
 1157			spin_unlock_irq(&phba->hbalock);
 1158			clear_bit(FCF_RR_INPROG, &phba->hba_flag);
 1159			clear_bit(HBA_DEVLOSS_TMO, &phba->hba_flag);
 1160			phba->fcf.fcf_redisc_attempted = 0; /* reset */
 1161			goto out;
 1162		}
 1163		if (!rc) {
 1164			/* Mark the FCF discovery process done */
 1165			if (test_bit(HBA_FIP_SUPPORT, &phba->hba_flag))
 1166				lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
 1167						LOG_ELS,
 1168						"2769 FLOGI to FCF (x%x) "
 1169						"completed successfully\n",
 1170						phba->fcf.current_rec.fcf_indx);
 1171			spin_lock_irq(&phba->hbalock);
 1172			phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
 1173			spin_unlock_irq(&phba->hbalock);
 1174			clear_bit(FCF_RR_INPROG, &phba->hba_flag);
 1175			clear_bit(HBA_DEVLOSS_TMO, &phba->hba_flag);
 1176			phba->fcf.fcf_redisc_attempted = 0; /* reset */
 1177			goto out;
 1178		}
 1179	} else if (vport->port_state > LPFC_FLOGI &&
 1180		   test_bit(FC_PT2PT, &vport->fc_flag)) {
 1181		/*
 1182		 * In a p2p topology, it is possible that discovery has
 1183		 * already progressed, and this completion can be ignored.
 1184		 * Recheck the indicated topology.
 1185		 */
 1186		if (!sp->cmn.fPort)
 1187			goto out;
 1188	}
 1189
 1190flogifail:
 1191	spin_lock_irq(&phba->hbalock);
 1192	phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
 1193	spin_unlock_irq(&phba->hbalock);
 1194
 1195	if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4)) {
 1196		/* FLOGI failed, so just use loop map to make discovery list */
 1197		lpfc_disc_list_loopmap(vport);
 1198
 1199		/* Start discovery */
 1200		lpfc_disc_start(vport);
 1201	} else if (((ulp_status != IOSTAT_LOCAL_REJECT) ||
 1202			(((ulp_word4 & IOERR_PARAM_MASK) !=
 1203			 IOERR_SLI_ABORTED) &&
 1204			((ulp_word4 & IOERR_PARAM_MASK) !=
 1205			 IOERR_SLI_DOWN))) &&
 1206			(phba->link_state != LPFC_CLEAR_LA)) {
 1207		/* If FLOGI failed enable link interrupt. */
 1208		lpfc_issue_clear_la(phba, vport);
 1209	}
 1210out:
 1211	if (!flogi_in_retry)
 1212		clear_bit(HBA_FLOGI_OUTSTANDING, &phba->hba_flag);
 1213
 1214	lpfc_els_free_iocb(phba, cmdiocb);
 1215	lpfc_nlp_put(ndlp);
 1216}
 1217
 1218/**
 1219 * lpfc_cmpl_els_link_down - Completion callback function for ELS command
 1220 *                           aborted during a link down
 1221 * @phba: pointer to lpfc hba data structure.
 1222 * @cmdiocb: pointer to lpfc command iocb data structure.
 1223 * @rspiocb: pointer to lpfc response iocb data structure.
 1224 *
 1225 */
 1226static void
 1227lpfc_cmpl_els_link_down(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 1228			struct lpfc_iocbq *rspiocb)
 1229{
 1230	uint32_t *pcmd;
 1231	uint32_t cmd;
 1232	u32 ulp_status, ulp_word4;
 1233
 1234	pcmd = (uint32_t *)cmdiocb->cmd_dmabuf->virt;
 1235	cmd = *pcmd;
 1236
 1237	ulp_status = get_job_ulpstatus(phba, rspiocb);
 1238	ulp_word4 = get_job_word4(phba, rspiocb);
 1239
 1240	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
 1241			"6445 ELS completes after LINK_DOWN: "
 1242			" Status %x/%x cmd x%x flg x%x iotag x%x\n",
 1243			ulp_status, ulp_word4, cmd,
 1244			cmdiocb->cmd_flag, cmdiocb->iotag);
 1245
 1246	if (cmdiocb->cmd_flag & LPFC_IO_FABRIC) {
 1247		cmdiocb->cmd_flag &= ~LPFC_IO_FABRIC;
 1248		atomic_dec(&phba->fabric_iocb_count);
 1249	}
 1250	lpfc_els_free_iocb(phba, cmdiocb);
 1251}
 1252
 1253/**
 1254 * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
 1255 * @vport: pointer to a host virtual N_Port data structure.
 1256 * @ndlp: pointer to a node-list data structure.
 1257 * @retry: number of retries to the command IOCB.
 1258 *
 1259 * This routine issues a Fabric Login (FLOGI) Request ELS command
 1260 * for a @vport. The initiator service parameters are put into the payload
 1261 * of the FLOGI Request IOCB and the top-level callback function pointer
 1262 * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
 1263 * function field. The lpfc_issue_fabric_iocb routine is invoked to send
 1264 * out FLOGI ELS command with one outstanding fabric IOCB at a time.
 1265 *
 1266 * Note that the ndlp reference count will be incremented by 1 for holding the
 1267 * ndlp and the reference to ndlp will be stored into the ndlp field of
 1268 * the IOCB for the completion callback function to the FLOGI ELS command.
 1269 *
 1270 * Return code
 1271 *   0 - successfully issued flogi iocb for @vport
 1272 *   1 - failed to issue flogi iocb for @vport
 1273 **/
 1274static int
 1275lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 1276		     uint8_t retry)
 1277{
 1278	struct lpfc_hba  *phba = vport->phba;
 1279	struct serv_parm *sp;
 1280	union lpfc_wqe128 *wqe = NULL;
 1281	IOCB_t *icmd = NULL;
 1282	struct lpfc_iocbq *elsiocb;
 1283	struct lpfc_iocbq defer_flogi_acc;
 1284	u8 *pcmd, ct;
 1285	uint16_t cmdsize;
 1286	uint32_t tmo, did;
 1287	int rc;
 1288
 1289	/* It's possible for lpfc to reissue a FLOGI on an ndlp that is marked
 1290	 * NLP_DROPPED.  This happens when the FLOGI completed with the XB bit
 1291	 * set causing lpfc to reference the ndlp until the XRI_ABORTED CQE is
 1292	 * issued. The time window for the XRI_ABORTED CQE can be as much as
 1293	 * 2*2*RA_TOV allowing for ndlp reuse of this type when the link is
 1294	 * cycling quickly.  When true, restore the initial reference and remove
 1295	 * the NLP_DROPPED flag as lpfc is retrying.
 1296	 */
 1297	if (test_and_clear_bit(NLP_DROPPED, &ndlp->nlp_flag)) {
 1298		if (!lpfc_nlp_get(ndlp))
 1299			return 1;
 1300	}
 1301
 1302	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
 1303	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
 1304				     ndlp->nlp_DID, ELS_CMD_FLOGI);
 1305
 1306	if (!elsiocb)
 1307		return 1;
 1308
 1309	wqe = &elsiocb->wqe;
 1310	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 1311	icmd = &elsiocb->iocb;
 1312
 1313	/* For FLOGI request, remainder of payload is service parameters */
 1314	*((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
 1315	pcmd += sizeof(uint32_t);
 1316	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
 1317	sp = (struct serv_parm *) pcmd;
 1318
 1319	/* Setup CSPs accordingly for Fabric */
 1320	sp->cmn.e_d_tov = 0;
 1321	sp->cmn.w2.r_a_tov = 0;
 1322	sp->cmn.virtual_fabric_support = 0;
 1323	sp->cls1.classValid = 0;
 1324	if (sp->cmn.fcphLow < FC_PH3)
 1325		sp->cmn.fcphLow = FC_PH3;
 1326	if (sp->cmn.fcphHigh < FC_PH3)
 1327		sp->cmn.fcphHigh = FC_PH3;
 1328
 1329	/* Determine if switch supports priority tagging */
 1330	if (phba->cfg_vmid_priority_tagging) {
 1331		sp->cmn.priority_tagging = 1;
 1332		/* lpfc_vmid_host_uuid is combination of wwpn and wwnn */
 1333		if (!memchr_inv(vport->lpfc_vmid_host_uuid, 0,
 1334				sizeof(vport->lpfc_vmid_host_uuid))) {
 1335			memcpy(vport->lpfc_vmid_host_uuid, phba->wwpn,
 1336			       sizeof(phba->wwpn));
 1337			memcpy(&vport->lpfc_vmid_host_uuid[8], phba->wwnn,
 1338			       sizeof(phba->wwnn));
 1339		}
 1340	}
 1341
 1342	if  (phba->sli_rev == LPFC_SLI_REV4) {
 1343		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
 1344		    LPFC_SLI_INTF_IF_TYPE_0) {
 1345			/* FLOGI needs to be 3 for WQE FCFI */
 1346			ct = SLI4_CT_FCFI;
 1347			bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
 1348
 1349			/* Set the fcfi to the fcfi we registered with */
 1350			bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
 1351			       phba->fcf.fcfi);
 1352		}
 1353
 1354		/* Can't do SLI4 class2 without support sequence coalescing */
 1355		sp->cls2.classValid = 0;
 1356		sp->cls2.seqDelivery = 0;
 1357
 1358		/* Fill out Auxiliary Parameter Data */
 1359		if (phba->pni) {
 1360			sp->aux.flags =
 1361				AUX_PARM_DATA_VALID | AUX_PARM_PNI_VALID;
 1362			sp->aux.pni = cpu_to_be64(phba->pni);
 1363			sp->aux.npiv_cnt = cpu_to_be16(phba->max_vpi - 1);
 1364		}
 1365	} else {
 1366		/* Historical, setting sequential-delivery bit for SLI3 */
 1367		sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
 1368		sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
 1369		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
 1370			sp->cmn.request_multiple_Nport = 1;
 1371			/* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
 1372			icmd->ulpCt_h = 1;
 1373			icmd->ulpCt_l = 0;
 1374		} else {
 1375			sp->cmn.request_multiple_Nport = 0;
 1376		}
 1377
 1378		if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
 1379			icmd->un.elsreq64.myID = 0;
 1380			icmd->un.elsreq64.fl = 1;
 1381		}
 1382	}
 1383
 1384	tmo = phba->fc_ratov;
 1385	phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
 1386	lpfc_set_disctmo(vport);
 1387	phba->fc_ratov = tmo;
 1388
 1389	phba->fc_stat.elsXmitFLOGI++;
 1390	elsiocb->cmd_cmpl = lpfc_cmpl_els_flogi;
 1391
 1392	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 1393		"Issue FLOGI:     opt:x%x",
 1394		phba->sli3_options, 0, 0);
 1395
 1396	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 1397	if (!elsiocb->ndlp) {
 1398		lpfc_els_free_iocb(phba, elsiocb);
 1399		return 1;
 1400	}
 1401
 1402	/* Avoid race with FLOGI completion and hba_flags. */
 1403	set_bit(HBA_FLOGI_ISSUED, &phba->hba_flag);
 1404	set_bit(HBA_FLOGI_OUTSTANDING, &phba->hba_flag);
 1405
 1406	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
 1407	if (rc == IOCB_ERROR) {
 1408		clear_bit(HBA_FLOGI_ISSUED, &phba->hba_flag);
 1409		clear_bit(HBA_FLOGI_OUTSTANDING, &phba->hba_flag);
 1410		lpfc_els_free_iocb(phba, elsiocb);
 1411		lpfc_nlp_put(ndlp);
 1412		return 1;
 1413	}
 1414
 1415	/* Clear external loopback plug detected flag */
 1416	phba->link_flag &= ~LS_EXTERNAL_LOOPBACK;
 1417
 1418	/* Check for a deferred FLOGI ACC condition */
 1419	if (phba->defer_flogi_acc.flag) {
 1420		/* lookup ndlp for received FLOGI */
 1421		ndlp = lpfc_findnode_did(vport, 0);
 1422		if (!ndlp)
 1423			return 0;
 1424
 1425		did = vport->fc_myDID;
 1426		vport->fc_myDID = Fabric_DID;
 1427
 1428		memset(&defer_flogi_acc, 0, sizeof(struct lpfc_iocbq));
 1429
 1430		if (phba->sli_rev == LPFC_SLI_REV4) {
 1431			bf_set(wqe_ctxt_tag,
 1432			       &defer_flogi_acc.wqe.xmit_els_rsp.wqe_com,
 1433			       phba->defer_flogi_acc.rx_id);
 1434			bf_set(wqe_rcvoxid,
 1435			       &defer_flogi_acc.wqe.xmit_els_rsp.wqe_com,
 1436			       phba->defer_flogi_acc.ox_id);
 1437		} else {
 1438			icmd = &defer_flogi_acc.iocb;
 1439			icmd->ulpContext = phba->defer_flogi_acc.rx_id;
 1440			icmd->unsli3.rcvsli3.ox_id =
 1441				phba->defer_flogi_acc.ox_id;
 1442		}
 1443
 1444		/* The LS_ACC completion needs to drop the initial reference.
 1445		 * This is a special case for Pt2Pt because both FLOGIs need
 1446		 * to complete and lpfc defers the LS_ACC when the remote
 1447		 * FLOGI arrives before the driver's FLOGI.
 1448		 */
 1449		set_bit(NLP_FLOGI_DFR_ACC, &ndlp->nlp_flag);
 1450
 1451		/* Send deferred FLOGI ACC */
 1452		lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, &defer_flogi_acc,
 1453				 ndlp, NULL);
 1454
 1455		phba->defer_flogi_acc.flag = false;
 1456
 1457		/* Decrement the held ndlp that was incremented when the
 1458		 * deferred flogi acc flag was set.
 1459		 */
 1460		if (phba->defer_flogi_acc.ndlp) {
 1461			lpfc_nlp_put(phba->defer_flogi_acc.ndlp);
 1462			phba->defer_flogi_acc.ndlp = NULL;
 1463		}
 1464
 1465		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 1466				 "3354 Xmit deferred FLOGI ACC: rx_id: x%x,"
 1467				 " ox_id: x%x, ndlp x%px hba_flag x%lx\n",
 1468				 phba->defer_flogi_acc.rx_id,
 1469				 phba->defer_flogi_acc.ox_id,
 1470				 phba->defer_flogi_acc.ndlp,
 1471				 phba->hba_flag);
 1472
 1473		vport->fc_myDID = did;
 1474	}
 1475
 1476	return 0;
 1477}
 1478
 1479/**
 1480 * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
 1481 * @phba: pointer to lpfc hba data structure.
 1482 *
 1483 * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
 1484 * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
 1485 * list and issues an abort IOCB commond on each outstanding IOCB that
 1486 * contains a active Fabric_DID ndlp. Note that this function is to issue
 1487 * the abort IOCB command on all the outstanding IOCBs, thus when this
 1488 * function returns, it does not guarantee all the IOCBs are actually aborted.
 1489 *
 1490 * Return code
 1491 *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
 1492 **/
 1493int
 1494lpfc_els_abort_flogi(struct lpfc_hba *phba)
 1495{
 1496	struct lpfc_sli_ring *pring;
 1497	struct lpfc_iocbq *iocb, *next_iocb;
 1498	struct lpfc_nodelist *ndlp;
 1499	u32 ulp_command;
 1500
 1501	/* Abort outstanding I/O on NPort <nlp_DID> */
 1502	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
 1503			"0201 Abort outstanding I/O on NPort x%x\n",
 1504			Fabric_DID);
 1505
 1506	pring = lpfc_phba_elsring(phba);
 1507	if (unlikely(!pring))
 1508		return -EIO;
 1509
 1510	/*
 1511	 * Check the txcmplq for an iocb that matches the nport the driver is
 1512	 * searching for.
 1513	 */
 1514	spin_lock_irq(&phba->hbalock);
 1515	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
 1516		ulp_command = get_job_cmnd(phba, iocb);
 1517		if (ulp_command == CMD_ELS_REQUEST64_CR) {
 1518			ndlp = iocb->ndlp;
 1519			if (ndlp && ndlp->nlp_DID == Fabric_DID) {
 1520				if (test_bit(FC_PT2PT, &phba->pport->fc_flag) &&
 1521				    !test_bit(FC_PT2PT_PLOGI,
 1522					      &phba->pport->fc_flag))
 1523					iocb->fabric_cmd_cmpl =
 1524						lpfc_ignore_els_cmpl;
 1525				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
 1526							   NULL);
 1527			}
 1528		}
 1529	}
 1530	/* Make sure HBA is alive */
 1531	lpfc_issue_hb_tmo(phba);
 1532
 1533	spin_unlock_irq(&phba->hbalock);
 1534
 1535	return 0;
 1536}
 1537
 1538/**
 1539 * lpfc_initial_flogi - Issue an initial fabric login for a vport
 1540 * @vport: pointer to a host virtual N_Port data structure.
 1541 *
 1542 * This routine issues an initial Fabric Login (FLOGI) for the @vport
 1543 * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
 1544 * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
 1545 * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
 1546 * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
 1547 * is then invoked with the @vport and the ndlp to perform the FLOGI for the
 1548 * @vport.
 1549 *
 1550 * Return code
 1551 *   0 - failed to issue initial flogi for @vport
 1552 *   1 - successfully issued initial flogi for @vport
 1553 **/
 1554int
 1555lpfc_initial_flogi(struct lpfc_vport *vport)
 1556{
 1557	struct lpfc_nodelist *ndlp;
 1558
 1559	vport->port_state = LPFC_FLOGI;
 1560	lpfc_set_disctmo(vport);
 1561
 1562	/* First look for the Fabric ndlp */
 1563	ndlp = lpfc_findnode_did(vport, Fabric_DID);
 1564	if (!ndlp) {
 1565		/* Cannot find existing Fabric ndlp, so allocate a new one */
 1566		ndlp = lpfc_nlp_init(vport, Fabric_DID);
 1567		if (!ndlp)
 1568			return 0;
 1569		/* Set the node type */
 1570		ndlp->nlp_type |= NLP_FABRIC;
 1571
 1572		/* Put ndlp onto node list */
 1573		lpfc_enqueue_node(vport, ndlp);
 1574	}
 1575
 1576	/* Reset the Fabric flag, topology change may have happened */
 1577	clear_bit(FC_FABRIC, &vport->fc_flag);
 1578	if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
 1579		/* A node reference should be retained while registered with a
 1580		 * transport or dev-loss-evt work is pending.
 1581		 * Otherwise, decrement node reference to trigger release.
 1582		 */
 1583		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
 1584		    !test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
 1585			lpfc_nlp_put(ndlp);
 1586		return 0;
 1587	}
 1588	return 1;
 1589}
 1590
 1591/**
 1592 * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
 1593 * @vport: pointer to a host virtual N_Port data structure.
 1594 *
 1595 * This routine issues an initial Fabric Discover (FDISC) for the @vport
 1596 * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
 1597 * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
 1598 * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
 1599 * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
 1600 * is then invoked with the @vport and the ndlp to perform the FDISC for the
 1601 * @vport.
 1602 *
 1603 * Return code
 1604 *   0 - failed to issue initial fdisc for @vport
 1605 *   1 - successfully issued initial fdisc for @vport
 1606 **/
 1607int
 1608lpfc_initial_fdisc(struct lpfc_vport *vport)
 1609{
 1610	struct lpfc_nodelist *ndlp;
 1611
 1612	/* First look for the Fabric ndlp */
 1613	ndlp = lpfc_findnode_did(vport, Fabric_DID);
 1614	if (!ndlp) {
 1615		/* Cannot find existing Fabric ndlp, so allocate a new one */
 1616		ndlp = lpfc_nlp_init(vport, Fabric_DID);
 1617		if (!ndlp)
 1618			return 0;
 1619
 1620		/* NPIV is only supported in Fabrics. */
 1621		ndlp->nlp_type |= NLP_FABRIC;
 1622
 1623		/* Put ndlp onto node list */
 1624		lpfc_enqueue_node(vport, ndlp);
 1625	}
 1626
 1627	if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
 1628		/* A node reference should be retained while registered with a
 1629		 * transport or dev-loss-evt work is pending.
 1630		 * Otherwise, decrement node reference to trigger release.
 1631		 */
 1632		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
 1633		    !test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
 1634			lpfc_nlp_put(ndlp);
 1635		return 0;
 1636	}
 1637	return 1;
 1638}
 1639
 1640/**
 1641 * lpfc_more_plogi - Check and issue remaining plogis for a vport
 1642 * @vport: pointer to a host virtual N_Port data structure.
 1643 *
 1644 * This routine checks whether there are more remaining Port Logins
 1645 * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
 1646 * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
 1647 * to issue ELS PLOGIs up to the configured discover threads with the
 1648 * @vport (@vport->cfg_discovery_threads). The function also decrement
 1649 * the @vport's num_disc_node by 1 if it is not already 0.
 1650 **/
 1651void
 1652lpfc_more_plogi(struct lpfc_vport *vport)
 1653{
 1654	if (vport->num_disc_nodes)
 1655		vport->num_disc_nodes--;
 1656
 1657	/* Continue discovery with <num_disc_nodes> PLOGIs to go */
 1658	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 1659			 "0232 Continue discovery with %d PLOGIs to go "
 1660			 "Data: x%x x%lx x%x\n",
 1661			 vport->num_disc_nodes,
 1662			 atomic_read(&vport->fc_plogi_cnt),
 1663			 vport->fc_flag, vport->port_state);
 1664	/* Check to see if there are more PLOGIs to be sent */
 1665	if (test_bit(FC_NLP_MORE, &vport->fc_flag))
 1666		/* go thru NPR nodes and issue any remaining ELS PLOGIs */
 1667		lpfc_els_disc_plogi(vport);
 1668
 1669	return;
 1670}
 1671
 1672/**
 1673 * lpfc_plogi_confirm_nport - Confirm plogi wwpn matches stored ndlp
 1674 * @phba: pointer to lpfc hba data structure.
 1675 * @prsp: pointer to response IOCB payload.
 1676 * @ndlp: pointer to a node-list data structure.
 1677 *
 1678 * This routine checks and indicates whether the WWPN of an N_Port, retrieved
 1679 * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
 1680 * The following cases are considered N_Port confirmed:
 1681 * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
 1682 * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
 1683 * it does not have WWPN assigned either. If the WWPN is confirmed, the
 1684 * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
 1685 * 1) if there is a node on vport list other than the @ndlp with the same
 1686 * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
 1687 * on that node to release the RPI associated with the node; 2) if there is
 1688 * no node found on vport list with the same WWPN of the N_Port PLOGI logged
 1689 * into, a new node shall be allocated (or activated). In either case, the
 1690 * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
 1691 * be released and the new_ndlp shall be put on to the vport node list and
 1692 * its pointer returned as the confirmed node.
 1693 *
 1694 * Note that before the @ndlp got "released", the keepDID from not-matching
 1695 * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
 1696 * of the @ndlp. This is because the release of @ndlp is actually to put it
 1697 * into an inactive state on the vport node list and the vport node list
 1698 * management algorithm does not allow two node with a same DID.
 1699 *
 1700 * Return code
 1701 *   pointer to the PLOGI N_Port @ndlp
 1702 **/
 1703static struct lpfc_nodelist *
 1704lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
 1705			 struct lpfc_nodelist *ndlp)
 1706{
 1707	struct lpfc_vport *vport = ndlp->vport;
 1708	struct lpfc_nodelist *new_ndlp;
 1709	struct serv_parm *sp;
 1710	uint8_t  name[sizeof(struct lpfc_name)];
 1711	uint32_t keepDID = 0;
 1712	int rc;
 1713	unsigned long keep_nlp_flag = 0, keep_new_nlp_flag = 0;
 1714	uint16_t keep_nlp_state;
 1715	u32 keep_nlp_fc4_type = 0;
 1716	struct lpfc_nvme_rport *keep_nrport = NULL;
 1717	unsigned long *active_rrqs_xri_bitmap = NULL;
 1718
 1719	sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
 1720	memset(name, 0, sizeof(struct lpfc_name));
 1721
 1722	/* Now we find out if the NPort we are logging into, matches the WWPN
 1723	 * we have for that ndlp. If not, we have some work to do.
 1724	 */
 1725	new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
 1726
 1727	/* return immediately if the WWPN matches ndlp */
 1728	if (new_ndlp == ndlp)
 1729		return ndlp;
 1730
 1731	if (phba->sli_rev == LPFC_SLI_REV4) {
 1732		active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
 1733						       GFP_KERNEL);
 1734		if (active_rrqs_xri_bitmap)
 1735			memset(active_rrqs_xri_bitmap, 0,
 1736			       phba->cfg_rrq_xri_bitmap_sz);
 1737	}
 1738
 1739	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
 1740			 "3178 PLOGI confirm: ndlp x%x x%lx x%x: "
 1741			 "new_ndlp x%x x%lx x%x\n",
 1742			 ndlp->nlp_DID, ndlp->nlp_flag,  ndlp->nlp_fc4_type,
 1743			 (new_ndlp ? new_ndlp->nlp_DID : 0),
 1744			 (new_ndlp ? new_ndlp->nlp_flag : 0),
 1745			 (new_ndlp ? new_ndlp->nlp_fc4_type : 0));
 1746
 1747	if (!new_ndlp) {
 1748		rc = memcmp(&ndlp->nlp_portname, name,
 1749			    sizeof(struct lpfc_name));
 1750		if (!rc) {
 1751			if (active_rrqs_xri_bitmap)
 1752				mempool_free(active_rrqs_xri_bitmap,
 1753					     phba->active_rrq_pool);
 1754			return ndlp;
 1755		}
 1756		new_ndlp = lpfc_nlp_init(vport, ndlp->nlp_DID);
 1757		if (!new_ndlp) {
 1758			if (active_rrqs_xri_bitmap)
 1759				mempool_free(active_rrqs_xri_bitmap,
 1760					     phba->active_rrq_pool);
 1761			return ndlp;
 1762		}
 1763	} else {
 1764		if (phba->sli_rev == LPFC_SLI_REV4 &&
 1765		    active_rrqs_xri_bitmap)
 1766			memcpy(active_rrqs_xri_bitmap,
 1767			       new_ndlp->active_rrqs_xri_bitmap,
 1768			       phba->cfg_rrq_xri_bitmap_sz);
 1769
 1770		/*
 1771		 * Unregister from backend if not done yet. Could have been
 1772		 * skipped due to ADISC
 1773		 */
 1774		lpfc_nlp_unreg_node(vport, new_ndlp);
 1775	}
 1776
 1777	keepDID = new_ndlp->nlp_DID;
 1778
 1779	/* At this point in this routine, we know new_ndlp will be
 1780	 * returned. however, any previous GID_FTs that were done
 1781	 * would have updated nlp_fc4_type in ndlp, so we must ensure
 1782	 * new_ndlp has the right value.
 1783	 */
 1784	if (test_bit(FC_FABRIC, &vport->fc_flag)) {
 1785		keep_nlp_fc4_type = new_ndlp->nlp_fc4_type;
 1786		new_ndlp->nlp_fc4_type = ndlp->nlp_fc4_type;
 1787	}
 1788
 1789	lpfc_unreg_rpi(vport, new_ndlp);
 1790	new_ndlp->nlp_DID = ndlp->nlp_DID;
 1791	new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
 1792	if (phba->sli_rev == LPFC_SLI_REV4)
 1793		memcpy(new_ndlp->active_rrqs_xri_bitmap,
 1794		       ndlp->active_rrqs_xri_bitmap,
 1795		       phba->cfg_rrq_xri_bitmap_sz);
 1796
 1797	/* Lock both ndlps */
 1798	spin_lock_irq(&ndlp->lock);
 1799	spin_lock_irq(&new_ndlp->lock);
 1800	keep_new_nlp_flag = new_ndlp->nlp_flag;
 1801	keep_nlp_flag = ndlp->nlp_flag;
 1802	new_ndlp->nlp_flag = ndlp->nlp_flag;
 1803
 1804	/* if new_ndlp had NLP_UNREG_INP set, keep it */
 1805	if (test_bit(NLP_UNREG_INP, &keep_new_nlp_flag))
 1806		set_bit(NLP_UNREG_INP, &new_ndlp->nlp_flag);
 1807	else
 1808		clear_bit(NLP_UNREG_INP, &new_ndlp->nlp_flag);
 1809
 1810	/* if new_ndlp had NLP_RPI_REGISTERED set, keep it */
 1811	if (test_bit(NLP_RPI_REGISTERED, &keep_new_nlp_flag))
 1812		set_bit(NLP_RPI_REGISTERED, &new_ndlp->nlp_flag);
 1813	else
 1814		clear_bit(NLP_RPI_REGISTERED, &new_ndlp->nlp_flag);
 1815
 1816	/*
 1817	 * Retain the DROPPED flag. This will take care of the init
 1818	 * refcount when affecting the state change
 1819	 */
 1820	if (test_bit(NLP_DROPPED, &keep_new_nlp_flag))
 1821		set_bit(NLP_DROPPED, &new_ndlp->nlp_flag);
 1822	else
 1823		clear_bit(NLP_DROPPED, &new_ndlp->nlp_flag);
 1824
 1825	ndlp->nlp_flag = keep_new_nlp_flag;
 1826
 1827	/* if ndlp had NLP_UNREG_INP set, keep it */
 1828	if (test_bit(NLP_UNREG_INP, &keep_nlp_flag))
 1829		set_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
 1830	else
 1831		clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
 1832
 1833	/* if ndlp had NLP_RPI_REGISTERED set, keep it */
 1834	if (test_bit(NLP_RPI_REGISTERED, &keep_nlp_flag))
 1835		set_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag);
 1836	else
 1837		clear_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag);
 1838
 1839	/*
 1840	 * Retain the DROPPED flag. This will take care of the init
 1841	 * refcount when affecting the state change
 1842	 */
 1843	if (test_bit(NLP_DROPPED, &keep_nlp_flag))
 1844		set_bit(NLP_DROPPED, &ndlp->nlp_flag);
 1845	else
 1846		clear_bit(NLP_DROPPED, &ndlp->nlp_flag);
 1847
 1848	spin_unlock_irq(&new_ndlp->lock);
 1849	spin_unlock_irq(&ndlp->lock);
 1850
 1851	/* Set nlp_states accordingly */
 1852	keep_nlp_state = new_ndlp->nlp_state;
 1853	lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
 1854
 1855	/* interchange the nvme remoteport structs */
 1856	keep_nrport = new_ndlp->nrport;
 1857	new_ndlp->nrport = ndlp->nrport;
 1858
 1859	/* Move this back to NPR state */
 1860	if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
 1861		/* The ndlp doesn't have a portname yet, but does have an
 1862		 * NPort ID.  The new_ndlp portname matches the Rport's
 1863		 * portname.  Reinstantiate the new_ndlp and reset the ndlp.
 1864		 */
 1865		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 1866			 "3179 PLOGI confirm NEW: %x %x\n",
 1867			 new_ndlp->nlp_DID, keepDID);
 1868
 1869		/* Two ndlps cannot have the same did on the nodelist.
 1870		 * The KeepDID and keep_nlp_fc4_type need to be swapped
 1871		 * because ndlp is inflight with no WWPN.
 1872		 */
 1873		ndlp->nlp_DID = keepDID;
 1874		ndlp->nlp_fc4_type = keep_nlp_fc4_type;
 1875		lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
 1876		if (phba->sli_rev == LPFC_SLI_REV4 &&
 1877		    active_rrqs_xri_bitmap)
 1878			memcpy(ndlp->active_rrqs_xri_bitmap,
 1879			       active_rrqs_xri_bitmap,
 1880			       phba->cfg_rrq_xri_bitmap_sz);
 1881
 1882	} else {
 1883		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 1884			 "3180 PLOGI confirm SWAP: %x %x\n",
 1885			 new_ndlp->nlp_DID, keepDID);
 1886
 1887		lpfc_unreg_rpi(vport, ndlp);
 1888
 1889		/* The ndlp and new_ndlp both have WWPNs but are swapping
 1890		 * NPort Ids and attributes.
 1891		 */
 1892		ndlp->nlp_DID = keepDID;
 1893		ndlp->nlp_fc4_type = keep_nlp_fc4_type;
 1894
 1895		if (phba->sli_rev == LPFC_SLI_REV4 &&
 1896		    active_rrqs_xri_bitmap)
 1897			memcpy(ndlp->active_rrqs_xri_bitmap,
 1898			       active_rrqs_xri_bitmap,
 1899			       phba->cfg_rrq_xri_bitmap_sz);
 1900
 1901		/* Since we are switching over to the new_ndlp,
 1902		 * reset the old ndlp state
 1903		 */
 1904		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
 1905		    (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
 1906			keep_nlp_state = NLP_STE_NPR_NODE;
 1907		lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
 1908		ndlp->nrport = keep_nrport;
 1909	}
 1910
 1911	/*
 1912	 * If ndlp is not associated with any rport we can drop it here else
 1913	 * let dev_loss_tmo_callbk trigger DEVICE_RM event
 1914	 */
 1915	if (!ndlp->rport && (ndlp->nlp_state == NLP_STE_NPR_NODE))
 1916		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
 1917
 1918	if (phba->sli_rev == LPFC_SLI_REV4 &&
 1919	    active_rrqs_xri_bitmap)
 1920		mempool_free(active_rrqs_xri_bitmap,
 1921			     phba->active_rrq_pool);
 1922
 1923	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
 1924			 "3173 PLOGI confirm exit: new_ndlp x%x x%lx x%x\n",
 1925			 new_ndlp->nlp_DID, new_ndlp->nlp_flag,
 1926			 new_ndlp->nlp_fc4_type);
 1927
 1928	return new_ndlp;
 1929}
 1930
 1931/**
 1932 * lpfc_end_rscn - Check and handle more rscn for a vport
 1933 * @vport: pointer to a host virtual N_Port data structure.
 1934 *
 1935 * This routine checks whether more Registration State Change
 1936 * Notifications (RSCNs) came in while the discovery state machine was in
 1937 * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
 1938 * invoked to handle the additional RSCNs for the @vport. Otherwise, the
 1939 * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
 1940 * handling the RSCNs.
 1941 **/
 1942void
 1943lpfc_end_rscn(struct lpfc_vport *vport)
 1944{
 1945
 1946	if (test_bit(FC_RSCN_MODE, &vport->fc_flag)) {
 1947		/*
 1948		 * Check to see if more RSCNs came in while we were
 1949		 * processing this one.
 1950		 */
 1951		if (vport->fc_rscn_id_cnt ||
 1952		    test_bit(FC_RSCN_DISCOVERY, &vport->fc_flag))
 1953			lpfc_els_handle_rscn(vport);
 1954		else
 1955			clear_bit(FC_RSCN_MODE, &vport->fc_flag);
 1956	}
 1957}
 1958
 1959/**
 1960 * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
 1961 * @phba: pointer to lpfc hba data structure.
 1962 * @cmdiocb: pointer to lpfc command iocb data structure.
 1963 * @rspiocb: pointer to lpfc response iocb data structure.
 1964 *
 1965 * This routine will call the clear rrq function to free the rrq and
 1966 * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
 1967 * exist then the clear_rrq is still called because the rrq needs to
 1968 * be freed.
 1969 **/
 1970
 1971static void
 1972lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 1973		  struct lpfc_iocbq *rspiocb)
 1974{
 1975	struct lpfc_vport *vport = cmdiocb->vport;
 1976	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
 1977	struct lpfc_node_rrq *rrq;
 1978	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
 1979	u32 ulp_word4 = get_job_word4(phba, rspiocb);
 1980
 1981	/* we pass cmdiocb to state machine which needs rspiocb as well */
 1982	rrq = cmdiocb->context_un.rrq;
 1983	cmdiocb->rsp_iocb = rspiocb;
 1984
 1985	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 1986		"RRQ cmpl:      status:x%x/x%x did:x%x",
 1987		ulp_status, ulp_word4,
 1988		get_job_els_rsp64_did(phba, cmdiocb));
 1989
 1990
 1991	/* rrq completes to NPort <nlp_DID> */
 1992	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 1993			 "2880 RRQ completes to DID x%x "
 1994			 "Data: x%x x%x x%x x%x x%x\n",
 1995			 ndlp->nlp_DID, ulp_status, ulp_word4,
 1996			 get_wqe_tmo(cmdiocb), rrq->xritag, rrq->rxid);
 1997
 1998	if (ulp_status) {
 1999		/* Check for retry */
 2000		/* Warn RRQ status Don't print the vport to vport rjts */
 2001		if (ulp_status != IOSTAT_LS_RJT ||
 2002		    (((ulp_word4) >> 16 != LSRJT_INVALID_CMD) &&
 2003		     ((ulp_word4) >> 16 != LSRJT_UNABLE_TPC)) ||
 2004		    (phba)->pport->cfg_log_verbose & LOG_ELS)
 2005			lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
 2006				      "2881 RRQ DID:%06X Status:"
 2007				      "x%x/x%x\n",
 2008				      ndlp->nlp_DID, ulp_status,
 2009				      ulp_word4);
 2010	}
 2011
 2012	lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
 2013	lpfc_els_free_iocb(phba, cmdiocb);
 2014	lpfc_nlp_put(ndlp);
 2015	return;
 2016}
 2017/**
 2018 * lpfc_cmpl_els_plogi - Completion callback function for plogi
 2019 * @phba: pointer to lpfc hba data structure.
 2020 * @cmdiocb: pointer to lpfc command iocb data structure.
 2021 * @rspiocb: pointer to lpfc response iocb data structure.
 2022 *
 2023 * This routine is the completion callback function for issuing the Port
 2024 * Login (PLOGI) command. For PLOGI completion, there must be an active
 2025 * ndlp on the vport node list that matches the remote node ID from the
 2026 * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
 2027 * ignored and command IOCB released. The PLOGI response IOCB status is
 2028 * checked for error conditions. If there is error status reported, PLOGI
 2029 * retry shall be attempted by invoking the lpfc_els_retry() routine.
 2030 * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
 2031 * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
 2032 * (DSM) is set for this PLOGI completion. Finally, it checks whether
 2033 * there are additional N_Port nodes with the vport that need to perform
 2034 * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
 2035 * PLOGIs.
 2036 **/
 2037static void
 2038lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 2039		    struct lpfc_iocbq *rspiocb)
 2040{
 2041	struct lpfc_vport *vport = cmdiocb->vport;
 2042	IOCB_t *irsp;
 2043	struct lpfc_nodelist *ndlp, *free_ndlp;
 2044	struct lpfc_dmabuf *prsp;
 2045	bool disc;
 2046	struct serv_parm *sp = NULL;
 2047	u32 ulp_status, ulp_word4, did, iotag;
 2048	bool release_node = false;
 2049
 2050	/* we pass cmdiocb to state machine which needs rspiocb as well */
 2051	cmdiocb->rsp_iocb = rspiocb;
 2052
 2053	ulp_status = get_job_ulpstatus(phba, rspiocb);
 2054	ulp_word4 = get_job_word4(phba, rspiocb);
 2055	did = get_job_els_rsp64_did(phba, cmdiocb);
 2056
 2057	if (phba->sli_rev == LPFC_SLI_REV4) {
 2058		iotag = get_wqe_reqtag(cmdiocb);
 2059	} else {
 2060		irsp = &rspiocb->iocb;
 2061		iotag = irsp->ulpIoTag;
 2062	}
 2063
 2064	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 2065		"PLOGI cmpl:      status:x%x/x%x did:x%x",
 2066		ulp_status, ulp_word4, did);
 2067
 2068	ndlp = lpfc_findnode_did(vport, did);
 2069	if (!ndlp) {
 2070		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 2071				 "0136 PLOGI completes to NPort x%x "
 2072				 "with no ndlp. Data: x%x x%x x%x\n",
 2073				 did, ulp_status, ulp_word4, iotag);
 2074		goto out_freeiocb;
 2075	}
 2076
 2077	/* Since ndlp can be freed in the disc state machine, note if this node
 2078	 * is being used during discovery.
 2079	 */
 2080	disc = test_and_clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 2081
 2082	/* PLOGI completes to NPort <nlp_DID> */
 2083	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 2084			 "0102 PLOGI completes to NPort x%06x "
 2085			 "IoTag x%x Data: x%x x%x x%x x%x x%x\n",
 2086			 ndlp->nlp_DID, iotag,
 2087			 ndlp->nlp_fc4_type,
 2088			 ulp_status, ulp_word4,
 2089			 disc, vport->num_disc_nodes);
 2090
 2091	/* Check to see if link went down during discovery */
 2092	if (lpfc_els_chk_latt(vport)) {
 2093		set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 2094		goto out;
 2095	}
 2096
 2097	if (ulp_status) {
 2098		/* Check for retry */
 2099		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
 2100			/* ELS command is being retried */
 2101			if (disc)
 2102				set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 2103			goto out;
 2104		}
 2105		/* Warn PLOGI status Don't print the vport to vport rjts */
 2106		if (ulp_status != IOSTAT_LS_RJT ||
 2107		    (((ulp_word4) >> 16 != LSRJT_INVALID_CMD) &&
 2108		     ((ulp_word4) >> 16 != LSRJT_UNABLE_TPC)) ||
 2109		    (phba)->pport->cfg_log_verbose & LOG_ELS)
 2110			lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
 2111				      "2753 PLOGI DID:%06X "
 2112				      "Status:x%x/x%x\n",
 2113				      ndlp->nlp_DID, ulp_status,
 2114				      ulp_word4);
 2115
 2116		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
 2117		if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4))
 2118			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
 2119						NLP_EVT_CMPL_PLOGI);
 2120
 2121		/* If a PLOGI collision occurred, the node needs to continue
 2122		 * with the reglogin process.
 2123		 */
 2124		spin_lock_irq(&ndlp->lock);
 2125		if ((test_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag) ||
 2126		     test_bit(NLP_RCV_PLOGI, &ndlp->nlp_flag)) &&
 2127		    ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE) {
 2128			spin_unlock_irq(&ndlp->lock);
 2129			goto out;
 2130		}
 2131
 2132		/* No PLOGI collision and the node is not registered with the
 2133		 * scsi or nvme transport. It is no longer an active node. Just
 2134		 * start the device remove process.
 2135		 */
 2136		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
 2137			clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 2138			if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
 2139				release_node = true;
 2140		}
 2141		spin_unlock_irq(&ndlp->lock);
 2142
 2143		if (release_node)
 2144			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
 2145						NLP_EVT_DEVICE_RM);
 2146	} else {
 2147		/* Good status, call state machine */
 2148		prsp = list_get_first(&cmdiocb->cmd_dmabuf->list,
 2149				      struct lpfc_dmabuf, list);
 2150		if (!prsp)
 2151			goto out;
 2152		if (!lpfc_is_els_acc_rsp(prsp))
 2153			goto out;
 2154		ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
 2155
 2156		sp = (struct serv_parm *)((u8 *)prsp->virt +
 2157					  sizeof(u32));
 2158
 2159		ndlp->vmid_support = 0;
 2160		if ((phba->cfg_vmid_app_header && sp->cmn.app_hdr_support) ||
 2161		    (phba->cfg_vmid_priority_tagging &&
 2162		     sp->cmn.priority_tagging)) {
 2163			lpfc_printf_log(phba, KERN_DEBUG, LOG_ELS,
 2164					"4018 app_hdr_support %d tagging %d DID x%x\n",
 2165					sp->cmn.app_hdr_support,
 2166					sp->cmn.priority_tagging,
 2167					ndlp->nlp_DID);
 2168			/* if the dest port supports VMID, mark it in ndlp */
 2169			ndlp->vmid_support = 1;
 2170		}
 2171
 2172		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
 2173					NLP_EVT_CMPL_PLOGI);
 2174	}
 2175
 2176	if (disc && vport->num_disc_nodes) {
 2177		/* Check to see if there are more PLOGIs to be sent */
 2178		lpfc_more_plogi(vport);
 2179
 2180		if (vport->num_disc_nodes == 0) {
 2181			clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
 2182
 2183			lpfc_can_disctmo(vport);
 2184			lpfc_end_rscn(vport);
 2185		}
 2186	}
 2187
 2188out:
 2189	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
 2190			      "PLOGI Cmpl PUT:     did:x%x refcnt %d",
 2191			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
 2192
 2193out_freeiocb:
 2194	/* Release the reference on the original I/O request. */
 2195	free_ndlp = cmdiocb->ndlp;
 2196
 2197	lpfc_els_free_iocb(phba, cmdiocb);
 2198	lpfc_nlp_put(free_ndlp);
 2199	return;
 2200}
 2201
 2202/**
 2203 * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
 2204 * @vport: pointer to a host virtual N_Port data structure.
 2205 * @did: destination port identifier.
 2206 * @retry: number of retries to the command IOCB.
 2207 *
 2208 * This routine issues a Port Login (PLOGI) command to a remote N_Port
 2209 * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
 2210 * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
 2211 * This routine constructs the proper fields of the PLOGI IOCB and invokes
 2212 * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
 2213 *
 2214 * Note that the ndlp reference count will be incremented by 1 for holding
 2215 * the ndlp and the reference to ndlp will be stored into the ndlp field
 2216 * of the IOCB for the completion callback function to the PLOGI ELS command.
 2217 *
 2218 * Return code
 2219 *   0 - Successfully issued a plogi for @vport
 2220 *   1 - failed to issue a plogi for @vport
 2221 **/
 2222int
 2223lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
 2224{
 2225	struct lpfc_hba  *phba = vport->phba;
 2226	struct serv_parm *sp;
 2227	struct lpfc_nodelist *ndlp;
 2228	struct lpfc_iocbq *elsiocb;
 2229	uint8_t *pcmd;
 2230	uint16_t cmdsize;
 2231	int ret;
 2232
 2233	ndlp = lpfc_findnode_did(vport, did);
 2234	if (!ndlp)
 2235		return 1;
 2236
 2237	/* Defer the processing of the issue PLOGI until after the
 2238	 * outstanding UNREG_RPI mbox command completes, unless we
 2239	 * are going offline. This logic does not apply for Fabric DIDs
 2240	 */
 2241	if ((test_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag) ||
 2242	     test_bit(NLP_UNREG_INP, &ndlp->nlp_flag)) &&
 2243	    ((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
 2244	    !test_bit(FC_OFFLINE_MODE, &vport->fc_flag)) {
 2245		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 2246				 "4110 Issue PLOGI x%x deferred "
 2247				 "on NPort x%x rpi x%x flg x%lx Data:"
 2248				 " x%px\n",
 2249				 ndlp->nlp_defer_did, ndlp->nlp_DID,
 2250				 ndlp->nlp_rpi, ndlp->nlp_flag, ndlp);
 2251
 2252		/* We can only defer 1st PLOGI */
 2253		if (ndlp->nlp_defer_did == NLP_EVT_NOTHING_PENDING)
 2254			ndlp->nlp_defer_did = did;
 2255		return 0;
 2256	}
 2257
 2258	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
 2259	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
 2260				     ELS_CMD_PLOGI);
 2261	if (!elsiocb)
 2262		return 1;
 2263
 2264	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 2265
 2266	/* For PLOGI request, remainder of payload is service parameters */
 2267	*((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
 2268	pcmd += sizeof(uint32_t);
 2269	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
 2270	sp = (struct serv_parm *) pcmd;
 2271
 2272	/*
 2273	 * If we are a N-port connected to a Fabric, fix-up paramm's so logins
 2274	 * to device on remote loops work.
 2275	 */
 2276	if (test_bit(FC_FABRIC, &vport->fc_flag) &&
 2277	    !test_bit(FC_PUBLIC_LOOP, &vport->fc_flag))
 2278		sp->cmn.altBbCredit = 1;
 2279
 2280	if (sp->cmn.fcphLow < FC_PH_4_3)
 2281		sp->cmn.fcphLow = FC_PH_4_3;
 2282
 2283	if (sp->cmn.fcphHigh < FC_PH3)
 2284		sp->cmn.fcphHigh = FC_PH3;
 2285
 2286	sp->cmn.valid_vendor_ver_level = 0;
 2287	memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
 2288	if (!test_bit(FC_PT2PT, &vport->fc_flag))
 2289		sp->cmn.bbRcvSizeMsb &= 0xF;
 2290
 2291	/* Check if the destination port supports VMID */
 2292	ndlp->vmid_support = 0;
 2293	if (vport->vmid_priority_tagging)
 2294		sp->cmn.priority_tagging = 1;
 2295	else if (phba->cfg_vmid_app_header &&
 2296		 bf_get(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags))
 2297		sp->cmn.app_hdr_support = 1;
 2298
 2299	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 2300		"Issue PLOGI:     did:x%x",
 2301		did, 0, 0);
 2302
 2303	/* If our firmware supports this feature, convey that
 2304	 * information to the target using the vendor specific field.
 2305	 */
 2306	if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
 2307		sp->cmn.valid_vendor_ver_level = 1;
 2308		sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
 2309		sp->un.vv.flags = cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
 2310	}
 2311
 2312	phba->fc_stat.elsXmitPLOGI++;
 2313	elsiocb->cmd_cmpl = lpfc_cmpl_els_plogi;
 2314
 2315	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 2316			      "Issue PLOGI:     did:x%x refcnt %d",
 2317			      did, kref_read(&ndlp->kref), 0);
 2318	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 2319	if (!elsiocb->ndlp) {
 2320		lpfc_els_free_iocb(phba, elsiocb);
 2321		return 1;
 2322	}
 2323
 2324	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 2325	if (ret) {
 2326		lpfc_els_free_iocb(phba, elsiocb);
 2327		lpfc_nlp_put(ndlp);
 2328		return 1;
 2329	}
 2330
 2331	return 0;
 2332}
 2333
 2334/**
 2335 * lpfc_cmpl_els_prli - Completion callback function for prli
 2336 * @phba: pointer to lpfc hba data structure.
 2337 * @cmdiocb: pointer to lpfc command iocb data structure.
 2338 * @rspiocb: pointer to lpfc response iocb data structure.
 2339 *
 2340 * This routine is the completion callback function for a Process Login
 2341 * (PRLI) ELS command. The PRLI response IOCB status is checked for error
 2342 * status. If there is error status reported, PRLI retry shall be attempted
 2343 * by invoking the lpfc_els_retry() routine. Otherwise, the state
 2344 * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
 2345 * ndlp to mark the PRLI completion.
 2346 **/
 2347static void
 2348lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 2349		   struct lpfc_iocbq *rspiocb)
 2350{
 2351	struct lpfc_vport *vport = cmdiocb->vport;
 2352	struct lpfc_nodelist *ndlp;
 2353	char *mode;
 2354	u32 ulp_status;
 2355	u32 ulp_word4;
 2356	bool release_node = false;
 2357
 2358	/* we pass cmdiocb to state machine which needs rspiocb as well */
 2359	cmdiocb->rsp_iocb = rspiocb;
 2360
 2361	ndlp = cmdiocb->ndlp;
 2362
 2363	ulp_status = get_job_ulpstatus(phba, rspiocb);
 2364	ulp_word4 = get_job_word4(phba, rspiocb);
 2365
 2366	clear_bit(NLP_PRLI_SND, &ndlp->nlp_flag);
 2367
 2368	/* Driver supports multiple FC4 types.  Counters matter. */
 2369	spin_lock_irq(&ndlp->lock);
 2370	vport->fc_prli_sent--;
 2371	ndlp->fc4_prli_sent--;
 2372	spin_unlock_irq(&ndlp->lock);
 2373
 2374	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 2375		"PRLI cmpl:       status:x%x/x%x did:x%x",
 2376		ulp_status, ulp_word4,
 2377		ndlp->nlp_DID);
 2378
 2379	/* PRLI completes to NPort <nlp_DID> */
 2380	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 2381			 "0103 PRLI completes to NPort x%06x "
 2382			 "Data: x%x x%x x%x x%x x%x\n",
 2383			 ndlp->nlp_DID, ulp_status, ulp_word4,
 2384			 vport->num_disc_nodes, ndlp->fc4_prli_sent,
 2385			 ndlp->fc4_xpt_flags);
 2386
 2387	/* Check to see if link went down during discovery */
 2388	if (lpfc_els_chk_latt(vport))
 2389		goto out;
 2390
 2391	if (ulp_status) {
 2392		/* Check for retry */
 2393		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
 2394			/* ELS command is being retried */
 2395			goto out;
 2396		}
 2397
 2398		/* If we don't send GFT_ID to Fabric, a PRLI error
 2399		 * could be expected.
 2400		 */
 2401		if (test_bit(FC_FABRIC, &vport->fc_flag) ||
 2402		    vport->cfg_enable_fc4_type != LPFC_ENABLE_BOTH)
 2403			mode = KERN_WARNING;
 2404		else
 2405			mode = KERN_INFO;
 2406
 2407		/* Warn PRLI status */
 2408		lpfc_vlog_msg(vport, mode, LOG_ELS,
 2409				 "2754 PRLI DID:%06X Status:x%x/x%x, "
 2410				 "data: x%x x%x x%lx\n",
 2411				 ndlp->nlp_DID, ulp_status,
 2412				 ulp_word4, ndlp->nlp_state,
 2413				 ndlp->fc4_prli_sent, ndlp->nlp_flag);
 2414
 2415		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
 2416		if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4))
 2417			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
 2418						NLP_EVT_CMPL_PRLI);
 2419
 2420		/* The following condition catches an inflight transition
 2421		 * mismatch typically caused by an RSCN. Skip any
 2422		 * processing to allow recovery.
 2423		 */
 2424		if ((ndlp->nlp_state >= NLP_STE_PLOGI_ISSUE &&
 2425		     ndlp->nlp_state <= NLP_STE_REG_LOGIN_ISSUE) ||
 2426		    (ndlp->nlp_state == NLP_STE_NPR_NODE &&
 2427		     test_bit(NLP_DELAY_TMO, &ndlp->nlp_flag))) {
 2428			lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
 2429					 "2784 PRLI cmpl: Allow Node recovery "
 2430					 "DID x%06x nstate x%x nflag x%lx\n",
 2431					 ndlp->nlp_DID, ndlp->nlp_state,
 2432					 ndlp->nlp_flag);
 2433			goto out;
 2434		}
 2435
 2436		/*
 2437		 * For P2P topology, retain the node so that PLOGI can be
 2438		 * attempted on it again.
 2439		 */
 2440		if (test_bit(FC_PT2PT, &vport->fc_flag))
 2441			goto out;
 2442
 2443		/* As long as this node is not registered with the SCSI
 2444		 * or NVMe transport and no other PRLIs are outstanding,
 2445		 * it is no longer an active node.  Otherwise devloss
 2446		 * handles the final cleanup.
 2447		 */
 2448		spin_lock_irq(&ndlp->lock);
 2449		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
 2450		    !ndlp->fc4_prli_sent) {
 2451			clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 2452			if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
 2453				release_node = true;
 2454		}
 2455		spin_unlock_irq(&ndlp->lock);
 2456
 2457		if (release_node)
 2458			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
 2459						NLP_EVT_DEVICE_RM);
 2460	} else {
 2461		/* Good status, call state machine.  However, if another
 2462		 * PRLI is outstanding, don't call the state machine
 2463		 * because final disposition to Mapped or Unmapped is
 2464		 * completed there.
 2465		 */
 2466		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
 2467					NLP_EVT_CMPL_PRLI);
 2468	}
 2469
 2470out:
 2471	lpfc_els_free_iocb(phba, cmdiocb);
 2472	lpfc_nlp_put(ndlp);
 2473	return;
 2474}
 2475
 2476/**
 2477 * lpfc_issue_els_prli - Issue a prli iocb command for a vport
 2478 * @vport: pointer to a host virtual N_Port data structure.
 2479 * @ndlp: pointer to a node-list data structure.
 2480 * @retry: number of retries to the command IOCB.
 2481 *
 2482 * This routine issues a Process Login (PRLI) ELS command for the
 2483 * @vport. The PRLI service parameters are set up in the payload of the
 2484 * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
 2485 * is put to the IOCB completion callback func field before invoking the
 2486 * routine lpfc_sli_issue_iocb() to send out PRLI command.
 2487 *
 2488 * Note that the ndlp reference count will be incremented by 1 for holding the
 2489 * ndlp and the reference to ndlp will be stored into the ndlp field of
 2490 * the IOCB for the completion callback function to the PRLI ELS command.
 2491 *
 2492 * Return code
 2493 *   0 - successfully issued prli iocb command for @vport
 2494 *   1 - failed to issue prli iocb command for @vport
 2495 **/
 2496int
 2497lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 2498		    uint8_t retry)
 2499{
 2500	int rc = 0;
 2501	struct lpfc_hba *phba = vport->phba;
 2502	PRLI *npr;
 2503	struct lpfc_nvme_prli *npr_nvme;
 2504	struct lpfc_iocbq *elsiocb;
 2505	uint8_t *pcmd;
 2506	uint16_t cmdsize;
 2507	u32 local_nlp_type, elscmd;
 2508
 2509	/*
 2510	 * If we are in RSCN mode, the FC4 types supported from a
 2511	 * previous GFT_ID command may not be accurate. So, if we
 2512	 * are a NVME Initiator, always look for the possibility of
 2513	 * the remote NPort beng a NVME Target.
 2514	 */
 2515	if (phba->sli_rev == LPFC_SLI_REV4 &&
 2516	    test_bit(FC_RSCN_MODE, &vport->fc_flag) &&
 2517	    vport->nvmei_support)
 2518		ndlp->nlp_fc4_type |= NLP_FC4_NVME;
 2519	local_nlp_type = ndlp->nlp_fc4_type;
 2520
 2521	/* This routine will issue 1 or 2 PRLIs, so zero all the ndlp
 2522	 * fields here before any of them can complete.
 2523	 */
 2524	ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
 2525	ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
 2526	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
 2527	clear_bit(NLP_FIRSTBURST, &ndlp->nlp_flag);
 2528	clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 2529	ndlp->nvme_fb_size = 0;
 2530
 2531 send_next_prli:
 2532	if (local_nlp_type & NLP_FC4_FCP) {
 2533		/* Payload is 4 + 16 = 20 x14 bytes. */
 2534		cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
 2535		elscmd = ELS_CMD_PRLI;
 2536	} else if (local_nlp_type & NLP_FC4_NVME) {
 2537		/* Payload is 4 + 20 = 24 x18 bytes. */
 2538		cmdsize = (sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli));
 2539		elscmd = ELS_CMD_NVMEPRLI;
 2540	} else {
 2541		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 2542				 "3083 Unknown FC_TYPE x%x ndlp x%06x\n",
 2543				 ndlp->nlp_fc4_type, ndlp->nlp_DID);
 2544		return 1;
 2545	}
 2546
 2547	/* SLI3 ports don't support NVME.  If this rport is a strict NVME
 2548	 * FC4 type, implicitly LOGO.
 2549	 */
 2550	if (phba->sli_rev == LPFC_SLI_REV3 &&
 2551	    ndlp->nlp_fc4_type == NLP_FC4_NVME) {
 2552		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 2553				 "3088 Rport fc4 type 0x%x not supported by SLI3 adapter\n",
 2554				 ndlp->nlp_type);
 2555		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
 2556		return 1;
 2557	}
 2558
 2559	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
 2560				     ndlp->nlp_DID, elscmd);
 2561	if (!elsiocb)
 2562		return 1;
 2563
 2564	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 2565
 2566	/* For PRLI request, remainder of payload is service parameters */
 2567	memset(pcmd, 0, cmdsize);
 2568
 2569	if (local_nlp_type & NLP_FC4_FCP) {
 2570		/* Remainder of payload is FCP PRLI parameter page.
 2571		 * Note: this data structure is defined as
 2572		 * BE/LE in the structure definition so no
 2573		 * byte swap call is made.
 2574		 */
 2575		*((uint32_t *)(pcmd)) = ELS_CMD_PRLI;
 2576		pcmd += sizeof(uint32_t);
 2577		npr = (PRLI *)pcmd;
 2578
 2579		/*
 2580		 * If our firmware version is 3.20 or later,
 2581		 * set the following bits for FC-TAPE support.
 2582		 */
 2583		if (phba->vpd.rev.feaLevelHigh >= 0x02) {
 2584			npr->ConfmComplAllowed = 1;
 2585			npr->Retry = 1;
 2586			npr->TaskRetryIdReq = 1;
 2587		}
 2588		npr->estabImagePair = 1;
 2589		npr->readXferRdyDis = 1;
 2590		if (vport->cfg_first_burst_size)
 2591			npr->writeXferRdyDis = 1;
 2592
 2593		/* For FCP support */
 2594		npr->prliType = PRLI_FCP_TYPE;
 2595		npr->initiatorFunc = 1;
 2596		elsiocb->cmd_flag |= LPFC_PRLI_FCP_REQ;
 2597
 2598		/* Remove FCP type - processed. */
 2599		local_nlp_type &= ~NLP_FC4_FCP;
 2600	} else if (local_nlp_type & NLP_FC4_NVME) {
 2601		/* Remainder of payload is NVME PRLI parameter page.
 2602		 * This data structure is the newer definition that
 2603		 * uses bf macros so a byte swap is required.
 2604		 */
 2605		*((uint32_t *)(pcmd)) = ELS_CMD_NVMEPRLI;
 2606		pcmd += sizeof(uint32_t);
 2607		npr_nvme = (struct lpfc_nvme_prli *)pcmd;
 2608		bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
 2609		bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
 2610		if (phba->nsler) {
 2611			bf_set(prli_nsler, npr_nvme, 1);
 2612			bf_set(prli_conf, npr_nvme, 1);
 2613		}
 2614
 2615		/* Only initiators request first burst. */
 2616		if ((phba->cfg_nvme_enable_fb) &&
 2617		    !phba->nvmet_support)
 2618			bf_set(prli_fba, npr_nvme, 1);
 2619
 2620		if (phba->nvmet_support) {
 2621			bf_set(prli_tgt, npr_nvme, 1);
 2622			bf_set(prli_disc, npr_nvme, 1);
 2623		} else {
 2624			bf_set(prli_init, npr_nvme, 1);
 2625			bf_set(prli_conf, npr_nvme, 1);
 2626		}
 2627
 2628		npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
 2629		npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
 2630		elsiocb->cmd_flag |= LPFC_PRLI_NVME_REQ;
 2631
 2632		/* Remove NVME type - processed. */
 2633		local_nlp_type &= ~NLP_FC4_NVME;
 2634	}
 2635
 2636	phba->fc_stat.elsXmitPRLI++;
 2637	elsiocb->cmd_cmpl = lpfc_cmpl_els_prli;
 2638
 2639	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 2640			      "Issue PRLI:  did:x%x refcnt %d",
 2641			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
 2642	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 2643	if (!elsiocb->ndlp) {
 2644		lpfc_els_free_iocb(phba, elsiocb);
 2645		return 1;
 2646	}
 2647
 2648	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 2649	if (rc == IOCB_ERROR) {
 2650		lpfc_els_free_iocb(phba, elsiocb);
 2651		lpfc_nlp_put(ndlp);
 2652		return 1;
 2653	}
 2654
 2655	/* The vport counters are used for lpfc_scan_finished, but
 2656	 * the ndlp is used to track outstanding PRLIs for different
 2657	 * FC4 types.
 2658	 */
 2659	set_bit(NLP_PRLI_SND, &ndlp->nlp_flag);
 2660	spin_lock_irq(&ndlp->lock);
 2661	vport->fc_prli_sent++;
 2662	ndlp->fc4_prli_sent++;
 2663	spin_unlock_irq(&ndlp->lock);
 2664
 2665	/* The driver supports 2 FC4 types.  Make sure
 2666	 * a PRLI is issued for all types before exiting.
 2667	 */
 2668	if (phba->sli_rev == LPFC_SLI_REV4 &&
 2669	    local_nlp_type & (NLP_FC4_FCP | NLP_FC4_NVME))
 2670		goto send_next_prli;
 2671	else
 2672		return 0;
 2673}
 2674
 2675/**
 2676 * lpfc_rscn_disc - Perform rscn discovery for a vport
 2677 * @vport: pointer to a host virtual N_Port data structure.
 2678 *
 2679 * This routine performs Registration State Change Notification (RSCN)
 2680 * discovery for a @vport. If the @vport's node port recovery count is not
 2681 * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
 2682 * the nodes that need recovery. If none of the PLOGI were needed through
 2683 * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
 2684 * invoked to check and handle possible more RSCN came in during the period
 2685 * of processing the current ones.
 2686 **/
 2687static void
 2688lpfc_rscn_disc(struct lpfc_vport *vport)
 2689{
 2690	lpfc_can_disctmo(vport);
 2691
 2692	/* RSCN discovery */
 2693	/* go thru NPR nodes and issue ELS PLOGIs */
 2694	if (atomic_read(&vport->fc_npr_cnt))
 2695		if (lpfc_els_disc_plogi(vport))
 2696			return;
 2697
 2698	lpfc_end_rscn(vport);
 2699}
 2700
 2701/**
 2702 * lpfc_adisc_done - Complete the adisc phase of discovery
 2703 * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
 2704 *
 2705 * This function is called when the final ADISC is completed during discovery.
 2706 * This function handles clearing link attention or issuing reg_vpi depending
 2707 * on whether npiv is enabled. This function also kicks off the PLOGI phase of
 2708 * discovery.
 2709 * This function is called with no locks held.
 2710 **/
 2711static void
 2712lpfc_adisc_done(struct lpfc_vport *vport)
 2713{
 2714	struct lpfc_hba   *phba = vport->phba;
 2715
 2716	/*
 2717	 * For NPIV, cmpl_reg_vpi will set port_state to READY,
 2718	 * and continue discovery.
 2719	 */
 2720	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
 2721	    !test_bit(FC_RSCN_MODE, &vport->fc_flag) &&
 2722	    (phba->sli_rev < LPFC_SLI_REV4)) {
 2723
 2724		/*
 2725		 * If link is down, clear_la and reg_vpi will be done after
 2726		 * flogi following a link up event
 2727		 */
 2728		if (!lpfc_is_link_up(phba))
 2729			return;
 2730
 2731		/* The ADISCs are complete.  Doesn't matter if they
 2732		 * succeeded or failed because the ADISC completion
 2733		 * routine guarantees to call the state machine and
 2734		 * the RPI is either unregistered (failed ADISC response)
 2735		 * or the RPI is still valid and the node is marked
 2736		 * mapped for a target.  The exchanges should be in the
 2737		 * correct state. This code is specific to SLI3.
 2738		 */
 2739		lpfc_issue_clear_la(phba, vport);
 2740		lpfc_issue_reg_vpi(phba, vport);
 2741		return;
 2742	}
 2743	/*
 2744	* For SLI2, we need to set port_state to READY
 2745	* and continue discovery.
 2746	*/
 2747	if (vport->port_state < LPFC_VPORT_READY) {
 2748		/* If we get here, there is nothing to ADISC */
 2749		lpfc_issue_clear_la(phba, vport);
 2750		if (!test_bit(FC_ABORT_DISCOVERY, &vport->fc_flag)) {
 2751			vport->num_disc_nodes = 0;
 2752			/* go thru NPR list, issue ELS PLOGIs */
 2753			if (atomic_read(&vport->fc_npr_cnt))
 2754				lpfc_els_disc_plogi(vport);
 2755			if (!vport->num_disc_nodes) {
 2756				clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
 2757				lpfc_can_disctmo(vport);
 2758				lpfc_end_rscn(vport);
 2759			}
 2760		}
 2761		vport->port_state = LPFC_VPORT_READY;
 2762	} else
 2763		lpfc_rscn_disc(vport);
 2764}
 2765
 2766/**
 2767 * lpfc_more_adisc - Issue more adisc as needed
 2768 * @vport: pointer to a host virtual N_Port data structure.
 2769 *
 2770 * This routine determines whether there are more ndlps on a @vport
 2771 * node list need to have Address Discover (ADISC) issued. If so, it will
 2772 * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
 2773 * remaining nodes which need to have ADISC sent.
 2774 **/
 2775void
 2776lpfc_more_adisc(struct lpfc_vport *vport)
 2777{
 2778	if (vport->num_disc_nodes)
 2779		vport->num_disc_nodes--;
 2780	/* Continue discovery with <num_disc_nodes> ADISCs to go */
 2781	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 2782			 "0210 Continue discovery with %d ADISCs to go "
 2783			 "Data: x%x x%lx x%x\n",
 2784			 vport->num_disc_nodes,
 2785			 atomic_read(&vport->fc_adisc_cnt),
 2786			 vport->fc_flag, vport->port_state);
 2787	/* Check to see if there are more ADISCs to be sent */
 2788	if (test_bit(FC_NLP_MORE, &vport->fc_flag)) {
 2789		lpfc_set_disctmo(vport);
 2790		/* go thru NPR nodes and issue any remaining ELS ADISCs */
 2791		lpfc_els_disc_adisc(vport);
 2792	}
 2793	if (!vport->num_disc_nodes)
 2794		lpfc_adisc_done(vport);
 2795	return;
 2796}
 2797
 2798/**
 2799 * lpfc_cmpl_els_adisc - Completion callback function for adisc
 2800 * @phba: pointer to lpfc hba data structure.
 2801 * @cmdiocb: pointer to lpfc command iocb data structure.
 2802 * @rspiocb: pointer to lpfc response iocb data structure.
 2803 *
 2804 * This routine is the completion function for issuing the Address Discover
 2805 * (ADISC) command. It first checks to see whether link went down during
 2806 * the discovery process. If so, the node will be marked as node port
 2807 * recovery for issuing discover IOCB by the link attention handler and
 2808 * exit. Otherwise, the response status is checked. If error was reported
 2809 * in the response status, the ADISC command shall be retried by invoking
 2810 * the lpfc_els_retry() routine. Otherwise, if no error was reported in
 2811 * the response status, the state machine is invoked to set transition
 2812 * with respect to NLP_EVT_CMPL_ADISC event.
 2813 **/
 2814static void
 2815lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 2816		    struct lpfc_iocbq *rspiocb)
 2817{
 2818	struct lpfc_vport *vport = cmdiocb->vport;
 2819	IOCB_t *irsp;
 2820	struct lpfc_nodelist *ndlp;
 2821	bool  disc;
 2822	u32 ulp_status, ulp_word4, tmo, iotag;
 2823	bool release_node = false;
 2824
 2825	/* we pass cmdiocb to state machine which needs rspiocb as well */
 2826	cmdiocb->rsp_iocb = rspiocb;
 2827
 2828	ndlp = cmdiocb->ndlp;
 2829
 2830	ulp_status = get_job_ulpstatus(phba, rspiocb);
 2831	ulp_word4 = get_job_word4(phba, rspiocb);
 2832
 2833	if (phba->sli_rev == LPFC_SLI_REV4) {
 2834		tmo = get_wqe_tmo(cmdiocb);
 2835		iotag = get_wqe_reqtag(cmdiocb);
 2836	} else {
 2837		irsp = &rspiocb->iocb;
 2838		tmo = irsp->ulpTimeout;
 2839		iotag = irsp->ulpIoTag;
 2840	}
 2841
 2842	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 2843		"ADISC cmpl:      status:x%x/x%x did:x%x",
 2844		ulp_status, ulp_word4,
 2845		ndlp->nlp_DID);
 2846
 2847	/* Since ndlp can be freed in the disc state machine, note if this node
 2848	 * is being used during discovery.
 2849	 */
 2850	disc = test_and_clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 2851	clear_bit(NLP_ADISC_SND, &ndlp->nlp_flag);
 2852	/* ADISC completes to NPort <nlp_DID> */
 2853	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 2854			 "0104 ADISC completes to NPort x%x "
 2855			 "IoTag x%x Data: x%x x%x x%x x%x x%x\n",
 2856			 ndlp->nlp_DID, iotag,
 2857			 ulp_status, ulp_word4,
 2858			 tmo, disc, vport->num_disc_nodes);
 2859
 2860	/* Check to see if link went down during discovery */
 2861	if (lpfc_els_chk_latt(vport)) {
 2862		set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 2863		goto out;
 2864	}
 2865
 2866	if (ulp_status) {
 2867		/* Check for retry */
 2868		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
 2869			/* ELS command is being retried */
 2870			if (disc) {
 2871				set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 2872				lpfc_set_disctmo(vport);
 2873			}
 2874			goto out;
 2875		}
 2876		/* Warn ADISC status */
 2877		lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
 2878			      "2755 ADISC DID:%06X Status:x%x/x%x\n",
 2879			      ndlp->nlp_DID, ulp_status,
 2880			      ulp_word4);
 2881		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
 2882					NLP_EVT_CMPL_ADISC);
 2883
 2884		/* As long as this node is not registered with the SCSI or NVMe
 2885		 * transport, it is no longer an active node. Otherwise
 2886		 * devloss handles the final cleanup.
 2887		 */
 2888		spin_lock_irq(&ndlp->lock);
 2889		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
 2890			clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 2891			if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
 2892				release_node = true;
 2893		}
 2894		spin_unlock_irq(&ndlp->lock);
 2895
 2896		if (release_node)
 2897			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
 2898						NLP_EVT_DEVICE_RM);
 2899	} else
 2900		/* Good status, call state machine */
 2901		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
 2902					NLP_EVT_CMPL_ADISC);
 2903
 2904	/* Check to see if there are more ADISCs to be sent */
 2905	if (disc && vport->num_disc_nodes)
 2906		lpfc_more_adisc(vport);
 2907out:
 2908	lpfc_els_free_iocb(phba, cmdiocb);
 2909	lpfc_nlp_put(ndlp);
 2910	return;
 2911}
 2912
 2913/**
 2914 * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
 2915 * @vport: pointer to a virtual N_Port data structure.
 2916 * @ndlp: pointer to a node-list data structure.
 2917 * @retry: number of retries to the command IOCB.
 2918 *
 2919 * This routine issues an Address Discover (ADISC) for an @ndlp on a
 2920 * @vport. It prepares the payload of the ADISC ELS command, updates the
 2921 * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
 2922 * to issue the ADISC ELS command.
 2923 *
 2924 * Note that the ndlp reference count will be incremented by 1 for holding the
 2925 * ndlp and the reference to ndlp will be stored into the ndlp field of
 2926 * the IOCB for the completion callback function to the ADISC ELS command.
 2927 *
 2928 * Return code
 2929 *   0 - successfully issued adisc
 2930 *   1 - failed to issue adisc
 2931 **/
 2932int
 2933lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 2934		     uint8_t retry)
 2935{
 2936	int rc = 0;
 2937	struct lpfc_hba  *phba = vport->phba;
 2938	ADISC *ap;
 2939	struct lpfc_iocbq *elsiocb;
 2940	uint8_t *pcmd;
 2941	uint16_t cmdsize;
 2942
 2943	cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
 2944	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
 2945				     ndlp->nlp_DID, ELS_CMD_ADISC);
 2946	if (!elsiocb)
 2947		return 1;
 2948
 2949	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 2950
 2951	/* For ADISC request, remainder of payload is service parameters */
 2952	*((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
 2953	pcmd += sizeof(uint32_t);
 2954
 2955	/* Fill in ADISC payload */
 2956	ap = (ADISC *) pcmd;
 2957	ap->hardAL_PA = phba->fc_pref_ALPA;
 2958	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
 2959	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
 2960	ap->DID = be32_to_cpu(vport->fc_myDID);
 2961
 2962	phba->fc_stat.elsXmitADISC++;
 2963	elsiocb->cmd_cmpl = lpfc_cmpl_els_adisc;
 2964	set_bit(NLP_ADISC_SND, &ndlp->nlp_flag);
 2965	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 2966	if (!elsiocb->ndlp) {
 2967		lpfc_els_free_iocb(phba, elsiocb);
 2968		goto err;
 2969	}
 2970
 2971	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 2972			      "Issue ADISC:   did:x%x refcnt %d",
 2973			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
 2974
 2975	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 2976	if (rc == IOCB_ERROR) {
 2977		lpfc_els_free_iocb(phba, elsiocb);
 2978		lpfc_nlp_put(ndlp);
 2979		goto err;
 2980	}
 2981
 2982	return 0;
 2983
 2984err:
 2985	clear_bit(NLP_ADISC_SND, &ndlp->nlp_flag);
 2986	return 1;
 2987}
 2988
 2989/**
 2990 * lpfc_cmpl_els_logo - Completion callback function for logo
 2991 * @phba: pointer to lpfc hba data structure.
 2992 * @cmdiocb: pointer to lpfc command iocb data structure.
 2993 * @rspiocb: pointer to lpfc response iocb data structure.
 2994 *
 2995 * This routine is the completion function for issuing the ELS Logout (LOGO)
 2996 * command. If no error status was reported from the LOGO response, the
 2997 * state machine of the associated ndlp shall be invoked for transition with
 2998 * respect to NLP_EVT_CMPL_LOGO event.
 2999 **/
 3000static void
 3001lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 3002		   struct lpfc_iocbq *rspiocb)
 3003{
 3004	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
 3005	struct lpfc_vport *vport = ndlp->vport;
 3006	IOCB_t *irsp;
 3007	uint32_t skip_recovery = 0;
 3008	int wake_up_waiter = 0;
 3009	u32 ulp_status;
 3010	u32 ulp_word4;
 3011	u32 tmo, iotag;
 3012
 3013	/* we pass cmdiocb to state machine which needs rspiocb as well */
 3014	cmdiocb->rsp_iocb = rspiocb;
 3015
 3016	ulp_status = get_job_ulpstatus(phba, rspiocb);
 3017	ulp_word4 = get_job_word4(phba, rspiocb);
 3018
 3019	if (phba->sli_rev == LPFC_SLI_REV4) {
 3020		tmo = get_wqe_tmo(cmdiocb);
 3021		iotag = get_wqe_reqtag(cmdiocb);
 3022	} else {
 3023		irsp = &rspiocb->iocb;
 3024		tmo = irsp->ulpTimeout;
 3025		iotag = irsp->ulpIoTag;
 3026	}
 3027
 3028	clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
 3029	if (test_and_clear_bit(NLP_WAIT_FOR_LOGO, &ndlp->save_flags))
 3030		wake_up_waiter = 1;
 3031
 3032	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 3033		"LOGO cmpl:       status:x%x/x%x did:x%x",
 3034		ulp_status, ulp_word4,
 3035		ndlp->nlp_DID);
 3036
 3037	/* LOGO completes to NPort <nlp_DID> */
 3038	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 3039			 "0105 LOGO completes to NPort x%x "
 3040			 "IoTag x%x refcnt %d nflags x%lx xflags x%x "
 3041			 "Data: x%x x%x x%x x%x\n",
 3042			 ndlp->nlp_DID, iotag,
 3043			 kref_read(&ndlp->kref), ndlp->nlp_flag,
 3044			 ndlp->fc4_xpt_flags, ulp_status, ulp_word4,
 3045			 tmo, vport->num_disc_nodes);
 3046
 3047	if (lpfc_els_chk_latt(vport)) {
 3048		skip_recovery = 1;
 3049		goto out;
 3050	}
 3051
 3052	/* The LOGO will not be retried on failure.  A LOGO was
 3053	 * issued to the remote rport and a ACC or RJT or no Answer are
 3054	 * all acceptable.  Note the failure and move forward with
 3055	 * discovery.  The PLOGI will retry.
 3056	 */
 3057	if (ulp_status) {
 3058		/* Warn LOGO status */
 3059		lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
 3060			      "2756 LOGO, No Retry DID:%06X "
 3061			      "Status:x%x/x%x\n",
 3062			      ndlp->nlp_DID, ulp_status,
 3063			      ulp_word4);
 3064
 3065		/* Call NLP_EVT_DEVICE_RM if link is down or LOGO is aborted */
 3066		if (lpfc_error_lost_link(vport, ulp_status, ulp_word4))
 3067			skip_recovery = 1;
 3068	}
 3069
 3070	/* Call state machine. This will unregister the rpi if needed. */
 3071	lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
 3072
 3073out:
 3074	/* At this point, the LOGO processing is complete. NOTE: For a
 3075	 * pt2pt topology, we are assuming the NPortID will only change
 3076	 * on link up processing. For a LOGO / PLOGI initiated by the
 3077	 * Initiator, we are assuming the NPortID is not going to change.
 3078	 */
 3079
 3080	if (wake_up_waiter && ndlp->logo_waitq)
 3081		wake_up(ndlp->logo_waitq);
 3082	/*
 3083	 * If the node is a target, the handling attempts to recover the port.
 3084	 * For any other port type, the rpi is unregistered as an implicit
 3085	 * LOGO.
 3086	 */
 3087	if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) &&
 3088	    skip_recovery == 0) {
 3089		lpfc_cancel_retry_delay_tmo(vport, ndlp);
 3090		set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 3091
 3092		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 3093				 "3187 LOGO completes to NPort x%x: Start "
 3094				 "Recovery Data: x%x x%x x%x x%x\n",
 3095				 ndlp->nlp_DID, ulp_status,
 3096				 ulp_word4, tmo,
 3097				 vport->num_disc_nodes);
 3098
 3099		lpfc_els_free_iocb(phba, cmdiocb);
 3100		lpfc_nlp_put(ndlp);
 3101
 3102		lpfc_disc_start(vport);
 3103		return;
 3104	}
 3105
 3106	/* Cleanup path for failed REG_RPI handling. If REG_RPI fails, the
 3107	 * driver sends a LOGO to the rport to cleanup.  For fabric and
 3108	 * initiator ports cleanup the node as long as it the node is not
 3109	 * register with the transport.
 3110	 */
 3111	if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
 3112		clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 3113		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
 3114					NLP_EVT_DEVICE_RM);
 3115	}
 3116
 3117	/* Driver is done with the I/O. */
 3118	lpfc_els_free_iocb(phba, cmdiocb);
 3119	lpfc_nlp_put(ndlp);
 3120}
 3121
 3122/**
 3123 * lpfc_issue_els_logo - Issue a logo to an node on a vport
 3124 * @vport: pointer to a virtual N_Port data structure.
 3125 * @ndlp: pointer to a node-list data structure.
 3126 * @retry: number of retries to the command IOCB.
 3127 *
 3128 * This routine constructs and issues an ELS Logout (LOGO) iocb command
 3129 * to a remote node, referred by an @ndlp on a @vport. It constructs the
 3130 * payload of the IOCB, properly sets up the @ndlp state, and invokes the
 3131 * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
 3132 *
 3133 * Note that the ndlp reference count will be incremented by 1 for holding the
 3134 * ndlp and the reference to ndlp will be stored into the ndlp field of
 3135 * the IOCB for the completion callback function to the LOGO ELS command.
 3136 *
 3137 * Callers of this routine are expected to unregister the RPI first
 3138 *
 3139 * Return code
 3140 *   0 - successfully issued logo
 3141 *   1 - failed to issue logo
 3142 **/
 3143int
 3144lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 3145		    uint8_t retry)
 3146{
 3147	struct lpfc_hba  *phba = vport->phba;
 3148	struct lpfc_iocbq *elsiocb;
 3149	uint8_t *pcmd;
 3150	uint16_t cmdsize;
 3151	int rc;
 3152
 3153	if (test_bit(NLP_LOGO_SND, &ndlp->nlp_flag))
 3154		return 0;
 3155
 3156	cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
 3157	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
 3158				     ndlp->nlp_DID, ELS_CMD_LOGO);
 3159	if (!elsiocb)
 3160		return 1;
 3161
 3162	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 3163	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
 3164	pcmd += sizeof(uint32_t);
 3165
 3166	/* Fill in LOGO payload */
 3167	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
 3168	pcmd += sizeof(uint32_t);
 3169	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
 3170
 3171	phba->fc_stat.elsXmitLOGO++;
 3172	elsiocb->cmd_cmpl = lpfc_cmpl_els_logo;
 3173	set_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
 3174	clear_bit(NLP_ISSUE_LOGO, &ndlp->nlp_flag);
 3175	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 3176	if (!elsiocb->ndlp) {
 3177		lpfc_els_free_iocb(phba, elsiocb);
 3178		goto err;
 3179	}
 3180
 3181	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 3182			      "Issue LOGO:      did:x%x refcnt %d",
 3183			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
 3184
 3185	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 3186	if (rc == IOCB_ERROR) {
 3187		lpfc_els_free_iocb(phba, elsiocb);
 3188		lpfc_nlp_put(ndlp);
 3189		goto err;
 3190	}
 3191
 3192	spin_lock_irq(&ndlp->lock);
 3193	ndlp->nlp_prev_state = ndlp->nlp_state;
 3194	spin_unlock_irq(&ndlp->lock);
 3195	lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
 3196	return 0;
 3197
 3198err:
 3199	clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
 3200	return 1;
 3201}
 3202
 3203/**
 3204 * lpfc_cmpl_els_cmd - Completion callback function for generic els command
 3205 * @phba: pointer to lpfc hba data structure.
 3206 * @cmdiocb: pointer to lpfc command iocb data structure.
 3207 * @rspiocb: pointer to lpfc response iocb data structure.
 3208 *
 3209 * This routine is a generic completion callback function for ELS commands.
 3210 * Specifically, it is the callback function which does not need to perform
 3211 * any command specific operations. It is currently used by the ELS command
 3212 * issuing routines for RSCN, lpfc_issue_els_rscn, and the ELS Fibre Channel
 3213 * Address Resolution Protocol Response (FARPR) routine, lpfc_issue_els_farpr().
 3214 * Other than certain debug loggings, this callback function simply invokes the
 3215 * lpfc_els_chk_latt() routine to check whether link went down during the
 3216 * discovery process.
 3217 **/
 3218static void
 3219lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 3220		  struct lpfc_iocbq *rspiocb)
 3221{
 3222	struct lpfc_vport *vport = cmdiocb->vport;
 3223	struct lpfc_nodelist *free_ndlp;
 3224	IOCB_t *irsp;
 3225	u32 ulp_status, ulp_word4, tmo, did, iotag;
 3226
 3227	ulp_status = get_job_ulpstatus(phba, rspiocb);
 3228	ulp_word4 = get_job_word4(phba, rspiocb);
 3229	did = get_job_els_rsp64_did(phba, cmdiocb);
 3230
 3231	if (phba->sli_rev == LPFC_SLI_REV4) {
 3232		tmo = get_wqe_tmo(cmdiocb);
 3233		iotag = get_wqe_reqtag(cmdiocb);
 3234	} else {
 3235		irsp = &rspiocb->iocb;
 3236		tmo = irsp->ulpTimeout;
 3237		iotag = irsp->ulpIoTag;
 3238	}
 3239
 3240	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 3241			      "ELS cmd cmpl:    status:x%x/x%x did:x%x",
 3242			      ulp_status, ulp_word4, did);
 3243
 3244	/* ELS cmd tag <ulpIoTag> completes */
 3245	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 3246			 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
 3247			 iotag, ulp_status, ulp_word4, tmo);
 3248
 3249	/* Check to see if link went down during discovery */
 3250	lpfc_els_chk_latt(vport);
 3251
 3252	free_ndlp = cmdiocb->ndlp;
 3253
 3254	lpfc_els_free_iocb(phba, cmdiocb);
 3255	lpfc_nlp_put(free_ndlp);
 3256}
 3257
 3258/**
 3259 * lpfc_reg_fab_ctrl_node - RPI register the fabric controller node.
 3260 * @vport: pointer to lpfc_vport data structure.
 3261 * @fc_ndlp: pointer to the fabric controller (0xfffffd) node.
 3262 *
 3263 * This routine registers the rpi assigned to the fabric controller
 3264 * NPort_ID (0xfffffd) with the port and moves the node to UNMAPPED
 3265 * state triggering a registration with the SCSI transport.
 3266 *
 3267 * This routine is single out because the fabric controller node
 3268 * does not receive a PLOGI.  This routine is consumed by the
 3269 * SCR and RDF ELS commands.  Callers are expected to qualify
 3270 * with SLI4 first.
 3271 **/
 3272static int
 3273lpfc_reg_fab_ctrl_node(struct lpfc_vport *vport, struct lpfc_nodelist *fc_ndlp)
 3274{
 3275	int rc;
 3276	struct lpfc_hba *phba = vport->phba;
 3277	struct lpfc_nodelist *ns_ndlp;
 3278	LPFC_MBOXQ_t *mbox;
 3279
 3280	if (test_bit(NLP_RPI_REGISTERED, &fc_ndlp->nlp_flag))
 3281		return 0;
 3282
 3283	ns_ndlp = lpfc_findnode_did(vport, NameServer_DID);
 3284	if (!ns_ndlp)
 3285		return -ENODEV;
 3286
 3287	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
 3288			 "0935 %s: Reg FC RPI x%x on FC DID x%x NSSte: x%x\n",
 3289			 __func__, fc_ndlp->nlp_rpi, fc_ndlp->nlp_DID,
 3290			 ns_ndlp->nlp_state);
 3291	if (ns_ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
 3292		return -ENODEV;
 3293
 3294	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 3295	if (!mbox) {
 3296		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
 3297				 "0936 %s: no memory for reg_login "
 3298				 "Data: x%x x%x x%lx x%x\n", __func__,
 3299				 fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
 3300				 fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
 3301		return -ENOMEM;
 3302	}
 3303	rc = lpfc_reg_rpi(phba, vport->vpi, fc_ndlp->nlp_DID,
 3304			  (u8 *)&ns_ndlp->fc_sparam, mbox, fc_ndlp->nlp_rpi);
 3305	if (rc) {
 3306		rc = -EACCES;
 3307		goto out;
 3308	}
 3309
 3310	set_bit(NLP_REG_LOGIN_SEND, &fc_ndlp->nlp_flag);
 3311	mbox->mbox_cmpl = lpfc_mbx_cmpl_fc_reg_login;
 3312	mbox->ctx_ndlp = lpfc_nlp_get(fc_ndlp);
 3313	if (!mbox->ctx_ndlp) {
 3314		rc = -ENOMEM;
 3315		goto out;
 3316	}
 3317
 3318	mbox->vport = vport;
 3319	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 3320	if (rc == MBX_NOT_FINISHED) {
 3321		rc = -ENODEV;
 3322		lpfc_nlp_put(fc_ndlp);
 3323		goto out;
 3324	}
 3325	/* Success path. Exit. */
 3326	lpfc_nlp_set_state(vport, fc_ndlp,
 3327			   NLP_STE_REG_LOGIN_ISSUE);
 3328	return 0;
 3329
 3330 out:
 3331	lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
 3332	lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
 3333			 "0938 %s: failed to format reg_login "
 3334			 "Data: x%x x%x x%lx x%x\n", __func__,
 3335			 fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
 3336			 fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
 3337	return rc;
 3338}
 3339
 3340/**
 3341 * lpfc_cmpl_els_disc_cmd - Completion callback function for Discovery ELS cmd
 3342 * @phba: pointer to lpfc hba data structure.
 3343 * @cmdiocb: pointer to lpfc command iocb data structure.
 3344 * @rspiocb: pointer to lpfc response iocb data structure.
 3345 *
 3346 * This routine is a generic completion callback function for Discovery ELS cmd.
 3347 * Currently used by the ELS command issuing routines for the ELS State Change
 3348 * Request (SCR), lpfc_issue_els_scr(), Exchange Diagnostic Capabilities (EDC),
 3349 * lpfc_issue_els_edc()  and the ELS RDF, lpfc_issue_els_rdf().
 3350 * These commands will be retried once only for ELS timeout errors.
 3351 **/
 3352static void
 3353lpfc_cmpl_els_disc_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 3354		       struct lpfc_iocbq *rspiocb)
 3355{
 3356	struct lpfc_vport *vport = cmdiocb->vport;
 3357	IOCB_t *irsp;
 3358	struct lpfc_els_rdf_rsp *prdf;
 3359	struct lpfc_dmabuf *pcmd, *prsp;
 3360	u32 *pdata;
 3361	u32 cmd;
 3362	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
 3363	u32 ulp_status, ulp_word4, tmo, did, iotag;
 3364
 3365	ulp_status = get_job_ulpstatus(phba, rspiocb);
 3366	ulp_word4 = get_job_word4(phba, rspiocb);
 3367	did = get_job_els_rsp64_did(phba, cmdiocb);
 3368
 3369	if (phba->sli_rev == LPFC_SLI_REV4) {
 3370		tmo = get_wqe_tmo(cmdiocb);
 3371		iotag = get_wqe_reqtag(cmdiocb);
 3372	} else {
 3373		irsp = &rspiocb->iocb;
 3374		tmo = irsp->ulpTimeout;
 3375		iotag = irsp->ulpIoTag;
 3376	}
 3377
 3378	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 3379		"ELS cmd cmpl:    status:x%x/x%x did:x%x",
 3380		ulp_status, ulp_word4, did);
 3381
 3382	/* ELS cmd tag <ulpIoTag> completes */
 3383	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
 3384			 "0217 ELS cmd tag x%x completes Data: x%x x%x x%x x%x\n",
 3385			 iotag, ulp_status, ulp_word4, tmo, cmdiocb->retry);
 3386
 3387	pcmd = cmdiocb->cmd_dmabuf;
 3388	if (!pcmd)
 3389		goto out;
 3390
 3391	pdata = (u32 *)pcmd->virt;
 3392	if (!pdata)
 3393		goto out;
 3394	cmd = *pdata;
 3395
 3396	/* Only 1 retry for ELS Timeout only */
 3397	if (ulp_status == IOSTAT_LOCAL_REJECT &&
 3398	    ((ulp_word4 & IOERR_PARAM_MASK) ==
 3399	    IOERR_SEQUENCE_TIMEOUT)) {
 3400		cmdiocb->retry++;
 3401		if (cmdiocb->retry <= 1) {
 3402			switch (cmd) {
 3403			case ELS_CMD_SCR:
 3404				lpfc_issue_els_scr(vport, cmdiocb->retry);
 3405				break;
 3406			case ELS_CMD_EDC:
 3407				lpfc_issue_els_edc(vport, cmdiocb->retry);
 3408				break;
 3409			case ELS_CMD_RDF:
 3410				lpfc_issue_els_rdf(vport, cmdiocb->retry);
 3411				break;
 3412			}
 3413			goto out;
 3414		}
 3415		phba->fc_stat.elsRetryExceeded++;
 3416	}
 3417	if (cmd == ELS_CMD_EDC) {
 3418		/* must be called before checking uplStatus and returning */
 3419		lpfc_cmpl_els_edc(phba, cmdiocb, rspiocb);
 3420		return;
 3421	}
 3422
 3423	if (ulp_status) {
 3424		/* ELS discovery cmd completes with error */
 3425		lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS | LOG_CGN_MGMT,
 3426				 "4203 ELS cmd x%x error: x%x x%X\n", cmd,
 3427				 ulp_status, ulp_word4);
 3428
 3429		/* In the case where the ELS cmd completes with an error and
 3430		 * the node does not have RPI registered, the node is
 3431		 * outstanding and should put its initial reference.
 3432		 */
 3433		if ((cmd == ELS_CMD_SCR || cmd == ELS_CMD_RDF) &&
 3434		    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD) &&
 3435		    !test_and_set_bit(NLP_DROPPED, &ndlp->nlp_flag))
 3436			lpfc_nlp_put(ndlp);
 3437		goto out;
 3438	}
 3439
 3440	/* The RDF response doesn't have any impact on the running driver
 3441	 * but the notification descriptors are dumped here for support.
 3442	 */
 3443	if (cmd == ELS_CMD_RDF) {
 3444		int i;
 3445
 3446		prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
 3447		if (!prsp)
 3448			goto out;
 3449
 3450		prdf = (struct lpfc_els_rdf_rsp *)prsp->virt;
 3451		if (!prdf)
 3452			goto out;
 3453		if (!lpfc_is_els_acc_rsp(prsp))
 3454			goto out;
 3455
 3456		for (i = 0; i < ELS_RDF_REG_TAG_CNT &&
 3457			    i < be32_to_cpu(prdf->reg_d1.reg_desc.count); i++)
 3458			lpfc_printf_vlog(vport, KERN_INFO,
 3459					 LOG_ELS | LOG_CGN_MGMT,
 3460					 "4677 Fabric RDF Notification Grant "
 3461					 "Data: 0x%08x Reg: %x %x\n",
 3462					 be32_to_cpu(
 3463						 prdf->reg_d1.desc_tags[i]),
 3464					 phba->cgn_reg_signal,
 3465					 phba->cgn_reg_fpin);
 3466	}
 3467
 3468out:
 3469	/* Check to see if link went down during discovery */
 3470	lpfc_els_chk_latt(vport);
 3471	lpfc_els_free_iocb(phba, cmdiocb);
 3472	lpfc_nlp_put(ndlp);
 3473	return;
 3474}
 3475
 3476/**
 3477 * lpfc_issue_els_scr - Issue a scr to an node on a vport
 3478 * @vport: pointer to a host virtual N_Port data structure.
 3479 * @retry: retry counter for the command IOCB.
 3480 *
 3481 * This routine issues a State Change Request (SCR) to a fabric node
 3482 * on a @vport. The remote node is Fabric Controller (0xfffffd). It
 3483 * first search the @vport node list to find the matching ndlp. If no such
 3484 * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
 3485 * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
 3486 * routine is invoked to send the SCR IOCB.
 3487 *
 3488 * Note that the ndlp reference count will be incremented by 1 for holding the
 3489 * ndlp and the reference to ndlp will be stored into the ndlp field of
 3490 * the IOCB for the completion callback function to the SCR ELS command.
 3491 *
 3492 * Return code
 3493 *   0 - Successfully issued scr command
 3494 *   1 - Failed to issue scr command
 3495 **/
 3496int
 3497lpfc_issue_els_scr(struct lpfc_vport *vport, uint8_t retry)
 3498{
 3499	int rc = 0;
 3500	struct lpfc_hba  *phba = vport->phba;
 3501	struct lpfc_iocbq *elsiocb;
 3502	uint8_t *pcmd;
 3503	uint16_t cmdsize;
 3504	struct lpfc_nodelist *ndlp;
 3505	bool node_created = false;
 3506
 3507	cmdsize = (sizeof(uint32_t) + sizeof(SCR));
 3508
 3509	ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
 3510	if (!ndlp) {
 3511		ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
 3512		if (!ndlp)
 3513			return 1;
 3514		lpfc_enqueue_node(vport, ndlp);
 3515		node_created = true;
 3516	}
 3517
 3518	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
 3519				     ndlp->nlp_DID, ELS_CMD_SCR);
 3520	if (!elsiocb)
 3521		goto out_node_created;
 3522
 3523	if (phba->sli_rev == LPFC_SLI_REV4) {
 3524		rc = lpfc_reg_fab_ctrl_node(vport, ndlp);
 3525		if (rc) {
 3526			lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
 3527					 "0937 %s: Failed to reg fc node, rc %d\n",
 3528					 __func__, rc);
 3529			goto out_free_iocb;
 3530		}
 3531	}
 3532	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 3533
 3534	*((uint32_t *) (pcmd)) = ELS_CMD_SCR;
 3535	pcmd += sizeof(uint32_t);
 3536
 3537	/* For SCR, remainder of payload is SCR parameter page */
 3538	memset(pcmd, 0, sizeof(SCR));
 3539	((SCR *) pcmd)->Function = SCR_FUNC_FULL;
 3540
 3541	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 3542		"Issue SCR:       did:x%x",
 3543		ndlp->nlp_DID, 0, 0);
 3544
 3545	phba->fc_stat.elsXmitSCR++;
 3546	elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
 3547	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 3548	if (!elsiocb->ndlp)
 3549		goto out_free_iocb;
 3550
 3551	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 3552			      "Issue SCR:     did:x%x refcnt %d",
 3553			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
 3554
 3555	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 3556	if (rc == IOCB_ERROR)
 3557		goto out_iocb_error;
 3558
 3559	return 0;
 3560
 3561out_iocb_error:
 3562	lpfc_nlp_put(ndlp);
 3563out_free_iocb:
 3564	lpfc_els_free_iocb(phba, elsiocb);
 3565out_node_created:
 3566	if (node_created)
 3567		lpfc_nlp_put(ndlp);
 3568	return 1;
 3569}
 3570
 3571/**
 3572 * lpfc_issue_els_rscn - Issue an RSCN to the Fabric Controller (Fabric)
 3573 *   or the other nport (pt2pt).
 3574 * @vport: pointer to a host virtual N_Port data structure.
 3575 * @retry: number of retries to the command IOCB.
 3576 *
 3577 * This routine issues a RSCN to the Fabric Controller (DID 0xFFFFFD)
 3578 *  when connected to a fabric, or to the remote port when connected
 3579 *  in point-to-point mode. When sent to the Fabric Controller, it will
 3580 *  replay the RSCN to registered recipients.
 3581 *
 3582 * Note that the ndlp reference count will be incremented by 1 for holding the
 3583 * ndlp and the reference to ndlp will be stored into the ndlp field of
 3584 * the IOCB for the completion callback function to the RSCN ELS command.
 3585 *
 3586 * Return code
 3587 *   0 - Successfully issued RSCN command
 3588 *   1 - Failed to issue RSCN command
 3589 **/
 3590int
 3591lpfc_issue_els_rscn(struct lpfc_vport *vport, uint8_t retry)
 3592{
 3593	int rc = 0;
 3594	struct lpfc_hba *phba = vport->phba;
 3595	struct lpfc_iocbq *elsiocb;
 3596	struct lpfc_nodelist *ndlp;
 3597	struct {
 3598		struct fc_els_rscn rscn;
 3599		struct fc_els_rscn_page portid;
 3600	} *event;
 3601	uint32_t nportid;
 3602	uint16_t cmdsize = sizeof(*event);
 3603
 3604	/* Not supported for private loop */
 3605	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
 3606	    !test_bit(FC_PUBLIC_LOOP, &vport->fc_flag))
 3607		return 1;
 3608
 3609	if (test_bit(FC_PT2PT, &vport->fc_flag)) {
 3610		/* find any mapped nport - that would be the other nport */
 3611		ndlp = lpfc_findnode_mapped(vport);
 3612		if (!ndlp)
 3613			return 1;
 3614	} else {
 3615		nportid = FC_FID_FCTRL;
 3616		/* find the fabric controller node */
 3617		ndlp = lpfc_findnode_did(vport, nportid);
 3618		if (!ndlp) {
 3619			/* if one didn't exist, make one */
 3620			ndlp = lpfc_nlp_init(vport, nportid);
 3621			if (!ndlp)
 3622				return 1;
 3623			lpfc_enqueue_node(vport, ndlp);
 3624		}
 3625	}
 3626
 3627	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
 3628				     ndlp->nlp_DID, ELS_CMD_RSCN_XMT);
 3629
 3630	if (!elsiocb)
 3631		return 1;
 3632
 3633	event = elsiocb->cmd_dmabuf->virt;
 3634
 3635	event->rscn.rscn_cmd = ELS_RSCN;
 3636	event->rscn.rscn_page_len = sizeof(struct fc_els_rscn_page);
 3637	event->rscn.rscn_plen = cpu_to_be16(cmdsize);
 3638
 3639	nportid = vport->fc_myDID;
 3640	/* appears that page flags must be 0 for fabric to broadcast RSCN */
 3641	event->portid.rscn_page_flags = 0;
 3642	event->portid.rscn_fid[0] = (nportid & 0x00FF0000) >> 16;
 3643	event->portid.rscn_fid[1] = (nportid & 0x0000FF00) >> 8;
 3644	event->portid.rscn_fid[2] = nportid & 0x000000FF;
 3645
 3646	phba->fc_stat.elsXmitRSCN++;
 3647	elsiocb->cmd_cmpl = lpfc_cmpl_els_cmd;
 3648	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 3649	if (!elsiocb->ndlp) {
 3650		lpfc_els_free_iocb(phba, elsiocb);
 3651		return 1;
 3652	}
 3653
 3654	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 3655			      "Issue RSCN:   did:x%x refcnt %d",
 3656			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
 3657
 3658	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 3659	if (rc == IOCB_ERROR) {
 3660		lpfc_els_free_iocb(phba, elsiocb);
 3661		lpfc_nlp_put(ndlp);
 3662		return 1;
 3663	}
 3664
 3665	return 0;
 3666}
 3667
 3668/**
 3669 * lpfc_issue_els_farpr - Issue a farp to an node on a vport
 3670 * @vport: pointer to a host virtual N_Port data structure.
 3671 * @nportid: N_Port identifier to the remote node.
 3672 * @retry: number of retries to the command IOCB.
 3673 *
 3674 * This routine issues a Fibre Channel Address Resolution Response
 3675 * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
 3676 * is passed into the function. It first search the @vport node list to find
 3677 * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
 3678 * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
 3679 * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
 3680 *
 3681 * Note that the ndlp reference count will be incremented by 1 for holding the
 3682 * ndlp and the reference to ndlp will be stored into the ndlp field of
 3683 * the IOCB for the completion callback function to the FARPR ELS command.
 3684 *
 3685 * Return code
 3686 *   0 - Successfully issued farpr command
 3687 *   1 - Failed to issue farpr command
 3688 **/
 3689static int
 3690lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
 3691{
 3692	int rc = 0;
 3693	struct lpfc_hba  *phba = vport->phba;
 3694	struct lpfc_iocbq *elsiocb;
 3695	FARP *fp;
 3696	uint8_t *pcmd;
 3697	uint32_t *lp;
 3698	uint16_t cmdsize;
 3699	struct lpfc_nodelist *ondlp;
 3700	struct lpfc_nodelist *ndlp;
 3701
 3702	cmdsize = (sizeof(uint32_t) + sizeof(FARP));
 3703
 3704	ndlp = lpfc_findnode_did(vport, nportid);
 3705	if (!ndlp) {
 3706		ndlp = lpfc_nlp_init(vport, nportid);
 3707		if (!ndlp)
 3708			return 1;
 3709		lpfc_enqueue_node(vport, ndlp);
 3710	}
 3711
 3712	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
 3713				     ndlp->nlp_DID, ELS_CMD_FARPR);
 3714	if (!elsiocb)
 3715		return 1;
 3716
 3717	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 3718
 3719	*((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
 3720	pcmd += sizeof(uint32_t);
 3721
 3722	/* Fill in FARPR payload */
 3723	fp = (FARP *) (pcmd);
 3724	memset(fp, 0, sizeof(FARP));
 3725	lp = (uint32_t *) pcmd;
 3726	*lp++ = be32_to_cpu(nportid);
 3727	*lp++ = be32_to_cpu(vport->fc_myDID);
 3728	fp->Rflags = 0;
 3729	fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
 3730
 3731	memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
 3732	memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
 3733	ondlp = lpfc_findnode_did(vport, nportid);
 3734	if (ondlp) {
 3735		memcpy(&fp->OportName, &ondlp->nlp_portname,
 3736		       sizeof(struct lpfc_name));
 3737		memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
 3738		       sizeof(struct lpfc_name));
 3739	}
 3740
 3741	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 3742		"Issue FARPR:     did:x%x",
 3743		ndlp->nlp_DID, 0, 0);
 3744
 3745	phba->fc_stat.elsXmitFARPR++;
 3746	elsiocb->cmd_cmpl = lpfc_cmpl_els_cmd;
 3747	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 3748	if (!elsiocb->ndlp) {
 3749		lpfc_els_free_iocb(phba, elsiocb);
 3750		return 1;
 3751	}
 3752
 3753	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 3754	if (rc == IOCB_ERROR) {
 3755		/* The additional lpfc_nlp_put will cause the following
 3756		 * lpfc_els_free_iocb routine to trigger the release of
 3757		 * the node.
 3758		 */
 3759		lpfc_els_free_iocb(phba, elsiocb);
 3760		lpfc_nlp_put(ndlp);
 3761		return 1;
 3762	}
 3763
 3764	return 0;
 3765}
 3766
 3767/**
 3768 * lpfc_issue_els_rdf - Register for diagnostic functions from the fabric.
 3769 * @vport: pointer to a host virtual N_Port data structure.
 3770 * @retry: retry counter for the command IOCB.
 3771 *
 3772 * This routine issues an ELS RDF to the Fabric Controller to register
 3773 * for diagnostic functions.
 3774 *
 3775 * Note that the ndlp reference count will be incremented by 1 for holding the
 3776 * ndlp and the reference to ndlp will be stored into the ndlp field of
 3777 * the IOCB for the completion callback function to the RDF ELS command.
 3778 *
 3779 * Return code
 3780 *   0 - Successfully issued rdf command
 3781 *   < 0 - Failed to issue rdf command
 3782 *   -EACCES - RDF not required for NPIV_PORT
 3783 *   -ENODEV - No fabric controller device available
 3784 *   -ENOMEM - No available memory
 3785 *   -EIO - The mailbox failed to complete successfully.
 3786 *
 3787 **/
 3788int
 3789lpfc_issue_els_rdf(struct lpfc_vport *vport, uint8_t retry)
 3790{
 3791	struct lpfc_hba *phba = vport->phba;
 3792	struct lpfc_iocbq *elsiocb;
 3793	struct lpfc_els_rdf_req *prdf;
 3794	struct lpfc_nodelist *ndlp;
 3795	uint16_t cmdsize;
 3796	int rc;
 3797	bool node_created = false;
 3798	int err;
 3799
 3800	cmdsize = sizeof(*prdf);
 3801
 3802	/* RDF ELS is not required on an NPIV VN_Port. */
 3803	if (vport->port_type == LPFC_NPIV_PORT)
 3804		return -EACCES;
 3805
 3806	ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
 3807	if (!ndlp) {
 3808		ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
 3809		if (!ndlp)
 3810			return -ENODEV;
 3811		lpfc_enqueue_node(vport, ndlp);
 3812		node_created = true;
 3813	}
 3814
 3815	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
 3816				     ndlp->nlp_DID, ELS_CMD_RDF);
 3817	if (!elsiocb) {
 3818		err = -ENOMEM;
 3819		goto out_node_created;
 3820	}
 3821
 3822	/* Configure the payload for the supported FPIN events. */
 3823	prdf = (struct lpfc_els_rdf_req *)elsiocb->cmd_dmabuf->virt;
 3824	memset(prdf, 0, cmdsize);
 3825	prdf->rdf.fpin_cmd = ELS_RDF;
 3826	prdf->rdf.desc_len = cpu_to_be32(sizeof(struct lpfc_els_rdf_req) -
 3827					 sizeof(struct fc_els_rdf_hdr));
 3828	prdf->reg_d1.reg_desc.desc_tag = cpu_to_be32(ELS_DTAG_FPIN_REGISTER);
 3829	prdf->reg_d1.reg_desc.desc_len = cpu_to_be32(
 3830				FC_TLV_DESC_LENGTH_FROM_SZ(prdf->reg_d1));
 3831	prdf->reg_d1.reg_desc.count = cpu_to_be32(ELS_RDF_REG_TAG_CNT);
 3832	prdf->reg_d1.desc_tags[0] = cpu_to_be32(ELS_DTAG_LNK_INTEGRITY);
 3833	prdf->reg_d1.desc_tags[1] = cpu_to_be32(ELS_DTAG_DELIVERY);
 3834	prdf->reg_d1.desc_tags[2] = cpu_to_be32(ELS_DTAG_PEER_CONGEST);
 3835	prdf->reg_d1.desc_tags[3] = cpu_to_be32(ELS_DTAG_CONGESTION);
 3836
 3837	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
 3838			 "6444 Xmit RDF to remote NPORT x%x Reg: %x %x\n",
 3839			 ndlp->nlp_DID, phba->cgn_reg_signal,
 3840			 phba->cgn_reg_fpin);
 3841
 3842	phba->cgn_fpin_frequency = LPFC_FPIN_INIT_FREQ;
 3843	elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
 3844	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 3845	if (!elsiocb->ndlp) {
 3846		err = -EIO;
 3847		goto out_free_iocb;
 3848	}
 3849
 3850	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 3851			      "Issue RDF:     did:x%x refcnt %d",
 3852			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
 3853
 3854	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 3855	if (rc == IOCB_ERROR) {
 3856		err = -EIO;
 3857		goto out_iocb_error;
 3858	}
 3859	return 0;
 3860
 3861out_iocb_error:
 3862	lpfc_nlp_put(ndlp);
 3863out_free_iocb:
 3864	lpfc_els_free_iocb(phba, elsiocb);
 3865out_node_created:
 3866	if (node_created)
 3867		lpfc_nlp_put(ndlp);
 3868	return err;
 3869}
 3870
 3871 /**
 3872  * lpfc_els_rcv_rdf - Receive RDF ELS request from the fabric.
 3873  * @vport: pointer to a host virtual N_Port data structure.
 3874  * @cmdiocb: pointer to lpfc command iocb data structure.
 3875  * @ndlp: pointer to a node-list data structure.
 3876  *
 3877  * A received RDF implies a possible change to fabric supported diagnostic
 3878  * functions.  This routine sends LS_ACC and then has the Nx_Port issue a new
 3879  * RDF request to reregister for supported diagnostic functions.
 3880  *
 3881  * Return code
 3882  *   0 - Success
 3883  *   -EIO - Failed to process received RDF
 3884  **/
 3885static int
 3886lpfc_els_rcv_rdf(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 3887		 struct lpfc_nodelist *ndlp)
 3888{
 3889	int rc;
 3890
 3891	rc = lpfc_els_rsp_acc(vport, ELS_CMD_RDF, cmdiocb, ndlp, NULL);
 3892	/* Send LS_ACC */
 3893	if (rc) {
 3894		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
 3895				 "1623 Failed to RDF_ACC from x%x for x%x Data: %d\n",
 3896				 ndlp->nlp_DID, vport->fc_myDID, rc);
 3897		return -EIO;
 3898	}
 3899
 3900	rc = lpfc_issue_els_rdf(vport, 0);
 3901	/* Issue new RDF for reregistering */
 3902	if (rc) {
 3903		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
 3904				 "2623 Failed to re register RDF for x%x Data: %d\n",
 3905				 vport->fc_myDID, rc);
 3906		return -EIO;
 3907	}
 3908
 3909	return 0;
 3910}
 3911
 3912/**
 3913 * lpfc_least_capable_settings - helper function for EDC rsp processing
 3914 * @phba: pointer to lpfc hba data structure.
 3915 * @pcgd: pointer to congestion detection descriptor in EDC rsp.
 3916 *
 3917 * This helper routine determines the least capable setting for
 3918 * congestion signals, signal freq, including scale, from the
 3919 * congestion detection descriptor in the EDC rsp.  The routine
 3920 * sets @phba values in preparation for a set_featues mailbox.
 3921 **/
 3922static void
 3923lpfc_least_capable_settings(struct lpfc_hba *phba,
 3924			    struct fc_diag_cg_sig_desc *pcgd)
 3925{
 3926	u32 rsp_sig_cap = 0, drv_sig_cap = 0;
 3927	u32 rsp_sig_freq_cyc = 0, rsp_sig_freq_scale = 0;
 3928
 3929	/* Get rsp signal and frequency capabilities.  */
 3930	rsp_sig_cap = be32_to_cpu(pcgd->xmt_signal_capability);
 3931	rsp_sig_freq_cyc = be16_to_cpu(pcgd->xmt_signal_frequency.count);
 3932	rsp_sig_freq_scale = be16_to_cpu(pcgd->xmt_signal_frequency.units);
 3933
 3934	/* If the Fport does not support signals. Set FPIN only */
 3935	if (rsp_sig_cap == EDC_CG_SIG_NOTSUPPORTED)
 3936		goto out_no_support;
 3937
 3938	/* Apply the xmt scale to the xmt cycle to get the correct frequency.
 3939	 * Adapter default is 100 millisSeconds.  Convert all xmt cycle values
 3940	 * to milliSeconds.
 3941	 */
 3942	switch (rsp_sig_freq_scale) {
 3943	case EDC_CG_SIGFREQ_SEC:
 3944		rsp_sig_freq_cyc *= MSEC_PER_SEC;
 3945		break;
 3946	case EDC_CG_SIGFREQ_MSEC:
 3947		rsp_sig_freq_cyc = 1;
 3948		break;
 3949	default:
 3950		goto out_no_support;
 3951	}
 3952
 3953	/* Convenient shorthand. */
 3954	drv_sig_cap = phba->cgn_reg_signal;
 3955
 3956	/* Choose the least capable frequency. */
 3957	if (rsp_sig_freq_cyc > phba->cgn_sig_freq)
 3958		phba->cgn_sig_freq = rsp_sig_freq_cyc;
 3959
 3960	/* Should be some common signals support. Settle on least capable
 3961	 * signal and adjust FPIN values. Initialize defaults to ease the
 3962	 * decision.
 3963	 */
 3964	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
 3965	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
 3966	if (rsp_sig_cap == EDC_CG_SIG_WARN_ONLY &&
 3967	    (drv_sig_cap == EDC_CG_SIG_WARN_ONLY ||
 3968	     drv_sig_cap == EDC_CG_SIG_WARN_ALARM)) {
 3969		phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
 3970		phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
 3971	}
 3972	if (rsp_sig_cap == EDC_CG_SIG_WARN_ALARM) {
 3973		if (drv_sig_cap == EDC_CG_SIG_WARN_ALARM) {
 3974			phba->cgn_reg_signal = EDC_CG_SIG_WARN_ALARM;
 3975			phba->cgn_reg_fpin = LPFC_CGN_FPIN_NONE;
 3976		}
 3977		if (drv_sig_cap == EDC_CG_SIG_WARN_ONLY) {
 3978			phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
 3979			phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
 3980		}
 3981	}
 3982
 3983	/* We are NOT recording signal frequency in congestion info buffer */
 3984	return;
 3985
 3986out_no_support:
 3987	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
 3988	phba->cgn_sig_freq = 0;
 3989	phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
 3990}
 3991
 3992DECLARE_ENUM2STR_LOOKUP(lpfc_get_tlv_dtag_nm, fc_ls_tlv_dtag,
 3993			FC_LS_TLV_DTAG_INIT);
 3994
 3995/**
 3996 * lpfc_cmpl_els_edc - Completion callback function for EDC
 3997 * @phba: pointer to lpfc hba data structure.
 3998 * @cmdiocb: pointer to lpfc command iocb data structure.
 3999 * @rspiocb: pointer to lpfc response iocb data structure.
 4000 *
 4001 * This routine is the completion callback function for issuing the Exchange
 4002 * Diagnostic Capabilities (EDC) command. The driver issues an EDC to
 4003 * notify the FPort of its Congestion and Link Fault capabilities.  This
 4004 * routine parses the FPort's response and decides on the least common
 4005 * values applicable to both FPort and NPort for Warnings and Alarms that
 4006 * are communicated via hardware signals.
 4007 **/
 4008static void
 4009lpfc_cmpl_els_edc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 4010		  struct lpfc_iocbq *rspiocb)
 4011{
 4012	IOCB_t *irsp_iocb;
 4013	struct fc_els_edc_resp *edc_rsp;
 4014	struct fc_tlv_desc *tlv;
 4015	struct fc_diag_cg_sig_desc *pcgd;
 4016	struct fc_diag_lnkflt_desc *plnkflt;
 4017	struct lpfc_dmabuf *pcmd, *prsp;
 4018	const char *dtag_nm;
 4019	u32 *pdata, dtag;
 4020	int desc_cnt = 0, bytes_remain;
 4021	bool rcv_cap_desc = false;
 4022	struct lpfc_nodelist *ndlp;
 4023	u32 ulp_status, ulp_word4, tmo, did, iotag;
 4024
 4025	ndlp = cmdiocb->ndlp;
 4026
 4027	ulp_status = get_job_ulpstatus(phba, rspiocb);
 4028	ulp_word4 = get_job_word4(phba, rspiocb);
 4029	did = get_job_els_rsp64_did(phba, rspiocb);
 4030
 4031	if (phba->sli_rev == LPFC_SLI_REV4) {
 4032		tmo = get_wqe_tmo(rspiocb);
 4033		iotag = get_wqe_reqtag(rspiocb);
 4034	} else {
 4035		irsp_iocb = &rspiocb->iocb;
 4036		tmo = irsp_iocb->ulpTimeout;
 4037		iotag = irsp_iocb->ulpIoTag;
 4038	}
 4039
 4040	lpfc_debugfs_disc_trc(phba->pport, LPFC_DISC_TRC_ELS_CMD,
 4041			      "EDC cmpl:    status:x%x/x%x did:x%x",
 4042			      ulp_status, ulp_word4, did);
 4043
 4044	/* ELS cmd tag <ulpIoTag> completes */
 4045	lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
 4046			"4201 EDC cmd tag x%x completes Data: x%x x%x x%x\n",
 4047			iotag, ulp_status, ulp_word4, tmo);
 4048
 4049	pcmd = cmdiocb->cmd_dmabuf;
 4050	if (!pcmd)
 4051		goto out;
 4052
 4053	pdata = (u32 *)pcmd->virt;
 4054	if (!pdata)
 4055		goto out;
 4056
 4057	/* Need to clear signal values, send features MB and RDF with FPIN. */
 4058	if (ulp_status)
 4059		goto out;
 4060
 4061	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
 4062	if (!prsp)
 4063		goto out;
 4064
 4065	edc_rsp = prsp->virt;
 4066	if (!edc_rsp)
 4067		goto out;
 4068
 4069	/* ELS cmd tag <ulpIoTag> completes */
 4070	lpfc_printf_log(phba, KERN_INFO,
 4071			LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
 4072			"4676 Fabric EDC Rsp: "
 4073			"0x%02x, 0x%08x\n",
 4074			edc_rsp->acc_hdr.la_cmd,
 4075			be32_to_cpu(edc_rsp->desc_list_len));
 4076
 4077	if (!lpfc_is_els_acc_rsp(prsp))
 4078		goto out;
 4079
 4080	/*
 4081	 * Payload length in bytes is the response descriptor list
 4082	 * length minus the 12 bytes of Link Service Request
 4083	 * Information descriptor in the reply.
 4084	 */
 4085	bytes_remain = be32_to_cpu(edc_rsp->desc_list_len) -
 4086				   sizeof(struct fc_els_lsri_desc);
 4087	if (bytes_remain <= 0)
 4088		goto out;
 4089
 4090	tlv = edc_rsp->desc;
 4091
 4092	/*
 4093	 * cycle through EDC diagnostic descriptors to find the
 4094	 * congestion signaling capability descriptor
 4095	 */
 4096	while (bytes_remain) {
 4097		if (bytes_remain < FC_TLV_DESC_HDR_SZ) {
 4098			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
 4099					"6461 Truncated TLV hdr on "
 4100					"Diagnostic descriptor[%d]\n",
 4101					desc_cnt);
 4102			goto out;
 4103		}
 4104
 4105		dtag = be32_to_cpu(tlv->desc_tag);
 4106		switch (dtag) {
 4107		case ELS_DTAG_LNK_FAULT_CAP:
 4108			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
 4109			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
 4110					sizeof(struct fc_diag_lnkflt_desc)) {
 4111				lpfc_printf_log(phba, KERN_WARNING,
 4112					LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
 4113					"6462 Truncated Link Fault Diagnostic "
 4114					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
 4115					desc_cnt, bytes_remain,
 4116					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
 4117					sizeof(struct fc_diag_lnkflt_desc));
 4118				goto out;
 4119			}
 4120			plnkflt = (struct fc_diag_lnkflt_desc *)tlv;
 4121			lpfc_printf_log(phba, KERN_INFO,
 4122				LOG_ELS | LOG_LDS_EVENT,
 4123				"4617 Link Fault Desc Data: 0x%08x 0x%08x "
 4124				"0x%08x 0x%08x 0x%08x\n",
 4125				be32_to_cpu(plnkflt->desc_tag),
 4126				be32_to_cpu(plnkflt->desc_len),
 4127				be32_to_cpu(
 4128					plnkflt->degrade_activate_threshold),
 4129				be32_to_cpu(
 4130					plnkflt->degrade_deactivate_threshold),
 4131				be32_to_cpu(plnkflt->fec_degrade_interval));
 4132			break;
 4133		case ELS_DTAG_CG_SIGNAL_CAP:
 4134			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
 4135			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
 4136					sizeof(struct fc_diag_cg_sig_desc)) {
 4137				lpfc_printf_log(
 4138					phba, KERN_WARNING, LOG_CGN_MGMT,
 4139					"6463 Truncated Cgn Signal Diagnostic "
 4140					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
 4141					desc_cnt, bytes_remain,
 4142					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
 4143					sizeof(struct fc_diag_cg_sig_desc));
 4144				goto out;
 4145			}
 4146
 4147			pcgd = (struct fc_diag_cg_sig_desc *)tlv;
 4148			lpfc_printf_log(
 4149				phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
 4150				"4616 CGN Desc Data: 0x%08x 0x%08x "
 4151				"0x%08x 0x%04x 0x%04x 0x%08x 0x%04x 0x%04x\n",
 4152				be32_to_cpu(pcgd->desc_tag),
 4153				be32_to_cpu(pcgd->desc_len),
 4154				be32_to_cpu(pcgd->xmt_signal_capability),
 4155				be16_to_cpu(pcgd->xmt_signal_frequency.count),
 4156				be16_to_cpu(pcgd->xmt_signal_frequency.units),
 4157				be32_to_cpu(pcgd->rcv_signal_capability),
 4158				be16_to_cpu(pcgd->rcv_signal_frequency.count),
 4159				be16_to_cpu(pcgd->rcv_signal_frequency.units));
 4160
 4161			/* Compare driver and Fport capabilities and choose
 4162			 * least common.
 4163			 */
 4164			lpfc_least_capable_settings(phba, pcgd);
 4165			rcv_cap_desc = true;
 4166			break;
 4167		default:
 4168			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
 4169			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
 4170					"4919 unknown Diagnostic "
 4171					"Descriptor[%d]: tag x%x (%s)\n",
 4172					desc_cnt, dtag, dtag_nm);
 4173		}
 4174
 4175		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
 4176		tlv = fc_tlv_next_desc(tlv);
 4177		desc_cnt++;
 4178	}
 4179
 4180out:
 4181	if (!rcv_cap_desc) {
 4182		phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
 4183		phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
 4184		phba->cgn_sig_freq = 0;
 4185		lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_CGN_MGMT,
 4186				"4202 EDC rsp error - sending RDF "
 4187				"for FPIN only.\n");
 4188	}
 4189
 4190	lpfc_config_cgn_signal(phba);
 4191
 4192	/* Check to see if link went down during discovery */
 4193	lpfc_els_chk_latt(phba->pport);
 4194	lpfc_debugfs_disc_trc(phba->pport, LPFC_DISC_TRC_ELS_CMD,
 4195			      "EDC Cmpl:     did:x%x refcnt %d",
 4196			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
 4197	lpfc_els_free_iocb(phba, cmdiocb);
 4198	lpfc_nlp_put(ndlp);
 4199}
 4200
 4201static void
 4202lpfc_format_edc_lft_desc(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
 4203{
 4204	struct fc_diag_lnkflt_desc *lft = (struct fc_diag_lnkflt_desc *)tlv;
 4205
 4206	lft->desc_tag = cpu_to_be32(ELS_DTAG_LNK_FAULT_CAP);
 4207	lft->desc_len = cpu_to_be32(
 4208		FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_diag_lnkflt_desc));
 4209
 4210	lft->degrade_activate_threshold =
 4211		cpu_to_be32(phba->degrade_activate_threshold);
 4212	lft->degrade_deactivate_threshold =
 4213		cpu_to_be32(phba->degrade_deactivate_threshold);
 4214	lft->fec_degrade_interval = cpu_to_be32(phba->fec_degrade_interval);
 4215}
 4216
 4217static void
 4218lpfc_format_edc_cgn_desc(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
 4219{
 4220	struct fc_diag_cg_sig_desc *cgd = (struct fc_diag_cg_sig_desc *)tlv;
 4221
 4222	/* We are assuming cgd was zero'ed before calling this routine */
 4223
 4224	/* Configure the congestion detection capability */
 4225	cgd->desc_tag = cpu_to_be32(ELS_DTAG_CG_SIGNAL_CAP);
 4226
 4227	/* Descriptor len doesn't include the tag or len fields. */
 4228	cgd->desc_len = cpu_to_be32(
 4229		FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_diag_cg_sig_desc));
 4230
 4231	/* xmt_signal_capability already set to EDC_CG_SIG_NOTSUPPORTED.
 4232	 * xmt_signal_frequency.count already set to 0.
 4233	 * xmt_signal_frequency.units already set to 0.
 4234	 */
 4235
 4236	if (phba->cmf_active_mode == LPFC_CFG_OFF) {
 4237		/* rcv_signal_capability already set to EDC_CG_SIG_NOTSUPPORTED.
 4238		 * rcv_signal_frequency.count already set to 0.
 4239		 * rcv_signal_frequency.units already set to 0.
 4240		 */
 4241		phba->cgn_sig_freq = 0;
 4242		return;
 4243	}
 4244	switch (phba->cgn_reg_signal) {
 4245	case EDC_CG_SIG_WARN_ONLY:
 4246		cgd->rcv_signal_capability = cpu_to_be32(EDC_CG_SIG_WARN_ONLY);
 4247		break;
 4248	case EDC_CG_SIG_WARN_ALARM:
 4249		cgd->rcv_signal_capability = cpu_to_be32(EDC_CG_SIG_WARN_ALARM);
 4250		break;
 4251	default:
 4252		/* rcv_signal_capability left 0 thus no support */
 4253		break;
 4254	}
 4255
 4256	/* We start negotiation with lpfc_fabric_cgn_frequency, after
 4257	 * the completion we settle on the higher frequency.
 4258	 */
 4259	cgd->rcv_signal_frequency.count =
 4260		cpu_to_be16(lpfc_fabric_cgn_frequency);
 4261	cgd->rcv_signal_frequency.units =
 4262		cpu_to_be16(EDC_CG_SIGFREQ_MSEC);
 4263}
 4264
 4265static bool
 4266lpfc_link_is_lds_capable(struct lpfc_hba *phba)
 4267{
 4268	if (!(phba->lmt & LMT_64Gb))
 4269		return false;
 4270	if (phba->sli_rev != LPFC_SLI_REV4)
 4271		return false;
 4272
 4273	if (phba->sli4_hba.conf_trunk) {
 4274		if (phba->trunk_link.phy_lnk_speed == LPFC_USER_LINK_SPEED_64G)
 4275			return true;
 4276	} else if (phba->fc_linkspeed == LPFC_LINK_SPEED_64GHZ) {
 4277		return true;
 4278	}
 4279	return false;
 4280}
 4281
 4282 /**
 4283  * lpfc_issue_els_edc - Exchange Diagnostic Capabilities with the fabric.
 4284  * @vport: pointer to a host virtual N_Port data structure.
 4285  * @retry: retry counter for the command iocb.
 4286  *
 4287  * This routine issues an ELS EDC to the F-Port Controller to communicate
 4288  * this N_Port's support of hardware signals in its Congestion
 4289  * Capabilities Descriptor.
 4290  *
 4291  * Note: This routine does not check if one or more signals are
 4292  * set in the cgn_reg_signal parameter.  The caller makes the
 4293  * decision to enforce cgn_reg_signal as nonzero or zero depending
 4294  * on the conditions.  During Fabric requests, the driver
 4295  * requires cgn_reg_signals to be nonzero.  But a dynamic request
 4296  * to set the congestion mode to OFF from Monitor or Manage
 4297  * would correctly issue an EDC with no signals enabled to
 4298  * turn off switch functionality and then update the FW.
 4299  *
 4300  * Return code
 4301  *   0 - Successfully issued edc command
 4302  *   1 - Failed to issue edc command
 4303  **/
 4304int
 4305lpfc_issue_els_edc(struct lpfc_vport *vport, uint8_t retry)
 4306{
 4307	struct lpfc_hba  *phba = vport->phba;
 4308	struct lpfc_iocbq *elsiocb;
 4309	struct fc_els_edc *edc_req;
 4310	struct fc_tlv_desc *tlv;
 4311	u16 cmdsize;
 4312	struct lpfc_nodelist *ndlp;
 4313	u8 *pcmd = NULL;
 4314	u32 cgn_desc_size, lft_desc_size;
 4315	int rc;
 4316
 4317	if (vport->port_type == LPFC_NPIV_PORT)
 4318		return -EACCES;
 4319
 4320	ndlp = lpfc_findnode_did(vport, Fabric_DID);
 4321	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
 4322		return -ENODEV;
 4323
 4324	cgn_desc_size = (phba->cgn_init_reg_signal) ?
 4325				sizeof(struct fc_diag_cg_sig_desc) : 0;
 4326	lft_desc_size = (lpfc_link_is_lds_capable(phba)) ?
 4327				sizeof(struct fc_diag_lnkflt_desc) : 0;
 4328	cmdsize = cgn_desc_size + lft_desc_size;
 4329
 4330	/* Skip EDC if no applicable descriptors */
 4331	if (!cmdsize)
 4332		goto try_rdf;
 4333
 4334	cmdsize += sizeof(struct fc_els_edc);
 4335	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
 4336				     ndlp->nlp_DID, ELS_CMD_EDC);
 4337	if (!elsiocb)
 4338		goto try_rdf;
 4339
 4340	/* Configure the payload for the supported Diagnostics capabilities. */
 4341	pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
 4342	memset(pcmd, 0, cmdsize);
 4343	edc_req = (struct fc_els_edc *)pcmd;
 4344	edc_req->desc_len = cpu_to_be32(cgn_desc_size + lft_desc_size);
 4345	edc_req->edc_cmd = ELS_EDC;
 4346	tlv = edc_req->desc;
 4347
 4348	if (cgn_desc_size) {
 4349		lpfc_format_edc_cgn_desc(phba, tlv);
 4350		phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
 4351		tlv = fc_tlv_next_desc(tlv);
 4352	}
 4353
 4354	if (lft_desc_size)
 4355		lpfc_format_edc_lft_desc(phba, tlv);
 4356
 4357	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
 4358			 "4623 Xmit EDC to remote "
 4359			 "NPORT x%x reg_sig x%x reg_fpin:x%x\n",
 4360			 ndlp->nlp_DID, phba->cgn_reg_signal,
 4361			 phba->cgn_reg_fpin);
 4362
 4363	elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
 4364	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 4365	if (!elsiocb->ndlp) {
 4366		lpfc_els_free_iocb(phba, elsiocb);
 4367		return -EIO;
 4368	}
 4369
 4370	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 4371			      "Issue EDC:     did:x%x refcnt %d",
 4372			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
 4373	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 4374	if (rc == IOCB_ERROR) {
 4375		/* The additional lpfc_nlp_put will cause the following
 4376		 * lpfc_els_free_iocb routine to trigger the release of
 4377		 * the node.
 4378		 */
 4379		lpfc_els_free_iocb(phba, elsiocb);
 4380		lpfc_nlp_put(ndlp);
 4381		goto try_rdf;
 4382	}
 4383	return 0;
 4384try_rdf:
 4385	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
 4386	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
 4387	rc = lpfc_issue_els_rdf(vport, 0);
 4388	return rc;
 4389}
 4390
 4391/**
 4392 * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
 4393 * @vport: pointer to a host virtual N_Port data structure.
 4394 * @nlp: pointer to a node-list data structure.
 4395 *
 4396 * This routine cancels the timer with a delayed IOCB-command retry for
 4397 * a @vport's @ndlp. It stops the timer for the delayed function retrial and
 4398 * removes the ELS retry event if it presents. In addition, if the
 4399 * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
 4400 * commands are sent for the @vport's nodes that require issuing discovery
 4401 * ADISC.
 4402 **/
 4403void
 4404lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
 4405{
 4406	struct lpfc_work_evt *evtp;
 4407
 4408	if (!test_and_clear_bit(NLP_DELAY_TMO, &nlp->nlp_flag))
 4409		return;
 4410	timer_delete_sync(&nlp->nlp_delayfunc);
 4411	nlp->nlp_last_elscmd = 0;
 4412	if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
 4413		list_del_init(&nlp->els_retry_evt.evt_listp);
 4414		/* Decrement nlp reference count held for the delayed retry */
 4415		evtp = &nlp->els_retry_evt;
 4416		lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
 4417	}
 4418	if (test_and_clear_bit(NLP_NPR_2B_DISC, &nlp->nlp_flag)) {
 4419		if (vport->num_disc_nodes) {
 4420			if (vport->port_state < LPFC_VPORT_READY) {
 4421				/* Check if there are more ADISCs to be sent */
 4422				lpfc_more_adisc(vport);
 4423			} else {
 4424				/* Check if there are more PLOGIs to be sent */
 4425				lpfc_more_plogi(vport);
 4426				if (vport->num_disc_nodes == 0) {
 4427					clear_bit(FC_NDISC_ACTIVE,
 4428						  &vport->fc_flag);
 4429					lpfc_can_disctmo(vport);
 4430					lpfc_end_rscn(vport);
 4431				}
 4432			}
 4433		}
 4434	}
 4435	return;
 4436}
 4437
 4438/**
 4439 * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
 4440 * @t: pointer to the timer function associated data (ndlp).
 4441 *
 4442 * This routine is invoked by the ndlp delayed-function timer to check
 4443 * whether there is any pending ELS retry event(s) with the node. If not, it
 4444 * simply returns. Otherwise, if there is at least one ELS delayed event, it
 4445 * adds the delayed events to the HBA work list and invokes the
 4446 * lpfc_worker_wake_up() routine to wake up worker thread to process the
 4447 * event. Note that lpfc_nlp_get() is called before posting the event to
 4448 * the work list to hold reference count of ndlp so that it guarantees the
 4449 * reference to ndlp will still be available when the worker thread gets
 4450 * to the event associated with the ndlp.
 4451 **/
 4452void
 4453lpfc_els_retry_delay(struct timer_list *t)
 4454{
 4455	struct lpfc_nodelist *ndlp = timer_container_of(ndlp, t,
 4456							nlp_delayfunc);
 4457	struct lpfc_vport *vport = ndlp->vport;
 4458	struct lpfc_hba   *phba = vport->phba;
 4459	unsigned long flags;
 4460	struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
 4461
 4462	/* Hold a node reference for outstanding queued work */
 4463	if (!lpfc_nlp_get(ndlp))
 4464		return;
 4465
 4466	spin_lock_irqsave(&phba->hbalock, flags);
 4467	if (!list_empty(&evtp->evt_listp)) {
 4468		spin_unlock_irqrestore(&phba->hbalock, flags);
 4469		lpfc_nlp_put(ndlp);
 4470		return;
 4471	}
 4472
 4473	evtp->evt_arg1 = ndlp;
 4474	evtp->evt = LPFC_EVT_ELS_RETRY;
 4475	list_add_tail(&evtp->evt_listp, &phba->work_list);
 4476	spin_unlock_irqrestore(&phba->hbalock, flags);
 4477
 4478	lpfc_worker_wake_up(phba);
 4479}
 4480
 4481/**
 4482 * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
 4483 * @ndlp: pointer to a node-list data structure.
 4484 *
 4485 * This routine is the worker-thread handler for processing the @ndlp delayed
 4486 * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
 4487 * the last ELS command from the associated ndlp and invokes the proper ELS
 4488 * function according to the delayed ELS command to retry the command.
 4489 **/
 4490void
 4491lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
 4492{
 4493	struct lpfc_vport *vport = ndlp->vport;
 4494	uint32_t cmd, retry;
 4495
 4496	spin_lock_irq(&ndlp->lock);
 4497	cmd = ndlp->nlp_last_elscmd;
 4498	ndlp->nlp_last_elscmd = 0;
 4499	spin_unlock_irq(&ndlp->lock);
 4500
 4501	if (!test_and_clear_bit(NLP_DELAY_TMO, &ndlp->nlp_flag))
 4502		return;
 4503
 4504	/*
 4505	 * If a discovery event readded nlp_delayfunc after timer
 4506	 * firing and before processing the timer, cancel the
 4507	 * nlp_delayfunc.
 4508	 */
 4509	timer_delete_sync(&ndlp->nlp_delayfunc);
 4510	retry = ndlp->nlp_retry;
 4511	ndlp->nlp_retry = 0;
 4512
 4513	switch (cmd) {
 4514	case ELS_CMD_FLOGI:
 4515		lpfc_issue_els_flogi(vport, ndlp, retry);
 4516		break;
 4517	case ELS_CMD_PLOGI:
 4518		if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
 4519			ndlp->nlp_prev_state = ndlp->nlp_state;
 4520			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
 4521		}
 4522		break;
 4523	case ELS_CMD_ADISC:
 4524		if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
 4525			ndlp->nlp_prev_state = ndlp->nlp_state;
 4526			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
 4527		}
 4528		break;
 4529	case ELS_CMD_PRLI:
 4530	case ELS_CMD_NVMEPRLI:
 4531		if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
 4532			ndlp->nlp_prev_state = ndlp->nlp_state;
 4533			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
 4534		}
 4535		break;
 4536	case ELS_CMD_LOGO:
 4537		if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
 4538			ndlp->nlp_prev_state = ndlp->nlp_state;
 4539			lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
 4540		}
 4541		break;
 4542	case ELS_CMD_FDISC:
 4543		if (!test_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag))
 4544			lpfc_issue_els_fdisc(vport, ndlp, retry);
 4545		break;
 4546	}
 4547	return;
 4548}
 4549
 4550/**
 4551 * lpfc_link_reset - Issue link reset
 4552 * @vport: pointer to a virtual N_Port data structure.
 4553 *
 4554 * This routine performs link reset by sending INIT_LINK mailbox command.
 4555 * For SLI-3 adapter, link attention interrupt is enabled before issuing
 4556 * INIT_LINK mailbox command.
 4557 *
 4558 * Return code
 4559 *   0 - Link reset initiated successfully
 4560 *   1 - Failed to initiate link reset
 4561 **/
 4562int
 4563lpfc_link_reset(struct lpfc_vport *vport)
 4564{
 4565	struct lpfc_hba *phba = vport->phba;
 4566	LPFC_MBOXQ_t *mbox;
 4567	uint32_t control;
 4568	int rc;
 4569
 4570	lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
 4571			 "2851 Attempt link reset\n");
 4572	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 4573	if (!mbox) {
 4574		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 4575				"2852 Failed to allocate mbox memory");
 4576		return 1;
 4577	}
 4578
 4579	/* Enable Link attention interrupts */
 4580	if (phba->sli_rev <= LPFC_SLI_REV3) {
 4581		spin_lock_irq(&phba->hbalock);
 4582		phba->sli.sli_flag |= LPFC_PROCESS_LA;
 4583		control = readl(phba->HCregaddr);
 4584		control |= HC_LAINT_ENA;
 4585		writel(control, phba->HCregaddr);
 4586		readl(phba->HCregaddr); /* flush */
 4587		spin_unlock_irq(&phba->hbalock);
 4588	}
 4589
 4590	lpfc_init_link(phba, mbox, phba->cfg_topology,
 4591		       phba->cfg_link_speed);
 4592	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
 4593	mbox->vport = vport;
 4594	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 4595	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
 4596		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 4597				"2853 Failed to issue INIT_LINK "
 4598				"mbox command, rc:x%x\n", rc);
 4599		mempool_free(mbox, phba->mbox_mem_pool);
 4600		return 1;
 4601	}
 4602
 4603	return 0;
 4604}
 4605
 4606/**
 4607 * lpfc_els_retry - Make retry decision on an els command iocb
 4608 * @phba: pointer to lpfc hba data structure.
 4609 * @cmdiocb: pointer to lpfc command iocb data structure.
 4610 * @rspiocb: pointer to lpfc response iocb data structure.
 4611 *
 4612 * This routine makes a retry decision on an ELS command IOCB, which has
 4613 * failed. The following ELS IOCBs use this function for retrying the command
 4614 * when previously issued command responsed with error status: FLOGI, PLOGI,
 4615 * PRLI, ADISC and FDISC. Based on the ELS command type and the
 4616 * returned error status, it makes the decision whether a retry shall be
 4617 * issued for the command, and whether a retry shall be made immediately or
 4618 * delayed. In the former case, the corresponding ELS command issuing-function
 4619 * is called to retry the command. In the later case, the ELS command shall
 4620 * be posted to the ndlp delayed event and delayed function timer set to the
 4621 * ndlp for the delayed command issusing.
 4622 *
 4623 * Return code
 4624 *   0 - No retry of els command is made
 4625 *   1 - Immediate or delayed retry of els command is made
 4626 **/
 4627static int
 4628lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 4629	       struct lpfc_iocbq *rspiocb)
 4630{
 4631	struct lpfc_vport *vport = cmdiocb->vport;
 4632	union lpfc_wqe128 *irsp = &rspiocb->wqe;
 4633	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
 4634	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
 4635	uint32_t *elscmd;
 4636	struct ls_rjt stat;
 4637	int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
 4638	int logerr = 0;
 4639	uint32_t cmd = 0;
 4640	uint32_t did;
 4641	int link_reset = 0, rc;
 4642	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
 4643	u32 ulp_word4 = get_job_word4(phba, rspiocb);
 4644	u8 rsn_code_exp = 0;
 4645
 4646
 4647	/* Note: cmd_dmabuf may be 0 for internal driver abort
 4648	 * of delays ELS command.
 4649	 */
 4650
 4651	if (pcmd && pcmd->virt) {
 4652		elscmd = (uint32_t *) (pcmd->virt);
 4653		cmd = *elscmd++;
 4654	}
 4655
 4656	if (ndlp)
 4657		did = ndlp->nlp_DID;
 4658	else {
 4659		/* We should only hit this case for retrying PLOGI */
 4660		did = get_job_els_rsp64_did(phba, rspiocb);
 4661		ndlp = lpfc_findnode_did(vport, did);
 4662		if (!ndlp && (cmd != ELS_CMD_PLOGI))
 4663			return 0;
 4664	}
 4665
 4666	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 4667		"Retry ELS:       wd7:x%x wd4:x%x did:x%x",
 4668		*(((uint32_t *)irsp) + 7), ulp_word4, did);
 4669
 4670	switch (ulp_status) {
 4671	case IOSTAT_FCP_RSP_ERROR:
 4672		break;
 4673	case IOSTAT_REMOTE_STOP:
 4674		if (phba->sli_rev == LPFC_SLI_REV4) {
 4675			/* This IO was aborted by the target, we don't
 4676			 * know the rxid and because we did not send the
 4677			 * ABTS we cannot generate and RRQ.
 4678			 */
 4679			lpfc_set_rrq_active(phba, ndlp,
 4680					 cmdiocb->sli4_lxritag, 0, 0);
 4681		}
 4682		break;
 4683	case IOSTAT_LOCAL_REJECT:
 4684		switch ((ulp_word4 & IOERR_PARAM_MASK)) {
 4685		case IOERR_LOOP_OPEN_FAILURE:
 4686			if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
 4687				delay = 1000;
 4688			retry = 1;
 4689			break;
 4690
 4691		case IOERR_ILLEGAL_COMMAND:
 4692			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 4693					 "0124 Retry illegal cmd x%x "
 4694					 "retry:x%x delay:x%x\n",
 4695					 cmd, cmdiocb->retry, delay);
 4696			retry = 1;
 4697			/* All command's retry policy */
 4698			maxretry = 8;
 4699			if (cmdiocb->retry > 2)
 4700				delay = 1000;
 4701			break;
 4702
 4703		case IOERR_NO_RESOURCES:
 4704			logerr = 1; /* HBA out of resources */
 4705			retry = 1;
 4706			if (cmdiocb->retry > 100)
 4707				delay = 100;
 4708			maxretry = 250;
 4709			break;
 4710
 4711		case IOERR_ILLEGAL_FRAME:
 4712			delay = 100;
 4713			retry = 1;
 4714			break;
 4715
 4716		case IOERR_INVALID_RPI:
 4717			if (cmd == ELS_CMD_PLOGI &&
 4718			    did == NameServer_DID) {
 4719				/* Continue forever if plogi to */
 4720				/* the nameserver fails */
 4721				maxretry = 0;
 4722				delay = 100;
 4723			} else if (cmd == ELS_CMD_PRLI &&
 4724				   ndlp->nlp_state != NLP_STE_PRLI_ISSUE) {
 4725				/* State-command disagreement.  The PRLI was
 4726				 * failed with an invalid rpi meaning there
 4727				 * some unexpected state change.  Don't retry.
 4728				 */
 4729				maxretry = 0;
 4730				retry = 0;
 4731				break;
 4732			}
 4733			retry = 1;
 4734			break;
 4735
 4736		case IOERR_SEQUENCE_TIMEOUT:
 4737			if (cmd == ELS_CMD_PLOGI &&
 4738			    did == NameServer_DID &&
 4739			    (cmdiocb->retry + 1) == maxretry) {
 4740				/* Reset the Link */
 4741				link_reset = 1;
 4742				break;
 4743			}
 4744			retry = 1;
 4745			delay = 100;
 4746			break;
 4747		case IOERR_SLI_ABORTED:
 4748			/* Retry ELS PLOGI command?
 4749			 * Possibly the rport just wasn't ready.
 4750			 */
 4751			if (cmd == ELS_CMD_PLOGI) {
 4752				/* No retry if state change */
 4753				if (ndlp &&
 4754				    ndlp->nlp_state != NLP_STE_PLOGI_ISSUE)
 4755					goto out_retry;
 4756				retry = 1;
 4757				maxretry = 2;
 4758			}
 4759			break;
 4760		}
 4761		break;
 4762
 4763	case IOSTAT_NPORT_RJT:
 4764	case IOSTAT_FABRIC_RJT:
 4765		if (ulp_word4 & RJT_UNAVAIL_TEMP) {
 4766			retry = 1;
 4767			break;
 4768		}
 4769		break;
 4770
 4771	case IOSTAT_NPORT_BSY:
 4772	case IOSTAT_FABRIC_BSY:
 4773		logerr = 1; /* Fabric / Remote NPort out of resources */
 4774		retry = 1;
 4775		break;
 4776
 4777	case IOSTAT_LS_RJT:
 4778		stat.un.ls_rjt_error_be = cpu_to_be32(ulp_word4);
 4779		/* Added for Vendor specifc support
 4780		 * Just keep retrying for these Rsn / Exp codes
 4781		 */
 4782		if (test_bit(FC_PT2PT, &vport->fc_flag) &&
 4783		    cmd == ELS_CMD_NVMEPRLI) {
 4784			switch (stat.un.b.lsRjtRsnCode) {
 4785			case LSRJT_UNABLE_TPC:
 4786			case LSRJT_INVALID_CMD:
 4787			case LSRJT_LOGICAL_ERR:
 4788			case LSRJT_CMD_UNSUPPORTED:
 4789				lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
 4790						 "0168 NVME PRLI LS_RJT "
 4791						 "reason %x port doesn't "
 4792						 "support NVME, disabling NVME\n",
 4793						 stat.un.b.lsRjtRsnCode);
 4794				retry = 0;
 4795				set_bit(FC_PT2PT_NO_NVME, &vport->fc_flag);
 4796				goto out_retry;
 4797			}
 4798		}
 4799		switch (stat.un.b.lsRjtRsnCode) {
 4800		case LSRJT_UNABLE_TPC:
 4801			/* Special case for PRLI LS_RJTs. Recall that lpfc
 4802			 * uses a single routine to issue both PRLI FC4 types.
 4803			 * If the PRLI is rejected because that FC4 type
 4804			 * isn't really supported, don't retry and cause
 4805			 * multiple transport registrations.  Otherwise, parse
 4806			 * the reason code/reason code explanation and take the
 4807			 * appropriate action.
 4808			 */
 4809			lpfc_printf_vlog(vport, KERN_INFO,
 4810					 LOG_DISCOVERY | LOG_ELS | LOG_NODE,
 4811					 "0153 ELS cmd x%x LS_RJT by x%x. "
 4812					 "RsnCode x%x RsnCodeExp x%x\n",
 4813					 cmd, did, stat.un.b.lsRjtRsnCode,
 4814					 stat.un.b.lsRjtRsnCodeExp);
 4815
 4816			switch (stat.un.b.lsRjtRsnCodeExp) {
 4817			case LSEXP_CANT_GIVE_DATA:
 4818			case LSEXP_CMD_IN_PROGRESS:
 4819				if (cmd == ELS_CMD_PLOGI) {
 4820					delay = 1000;
 4821					maxretry = 48;
 4822				}
 4823				retry = 1;
 4824				break;
 4825			case LSEXP_REQ_UNSUPPORTED:
 4826			case LSEXP_NO_RSRC_ASSIGN:
 4827				/* These explanation codes get no retry. */
 4828				if (cmd == ELS_CMD_PRLI ||
 4829				    cmd == ELS_CMD_NVMEPRLI)
 4830					break;
 4831				fallthrough;
 4832			default:
 4833				/* Limit the delay and retry action to a limited
 4834				 * cmd set.  There are other ELS commands where
 4835				 * a retry is not expected.
 4836				 */
 4837				if (cmd == ELS_CMD_PLOGI ||
 4838				    cmd == ELS_CMD_PRLI ||
 4839				    cmd == ELS_CMD_NVMEPRLI) {
 4840					delay = 1000;
 4841					maxretry = lpfc_max_els_tries + 1;
 4842					retry = 1;
 4843				}
 4844				break;
 4845			}
 4846
 4847			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
 4848			  (cmd == ELS_CMD_FDISC) &&
 4849			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
 4850				lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
 4851					      "0125 FDISC (x%x). "
 4852					      "Fabric out of resources\n",
 4853					      stat.un.lsRjtError);
 4854				lpfc_vport_set_state(vport,
 4855						     FC_VPORT_NO_FABRIC_RSCS);
 4856			}
 4857			break;
 4858
 4859		case LSRJT_LOGICAL_BSY:
 4860			rsn_code_exp = stat.un.b.lsRjtRsnCodeExp;
 4861			if ((cmd == ELS_CMD_PLOGI) ||
 4862			    (cmd == ELS_CMD_PRLI) ||
 4863			    (cmd == ELS_CMD_NVMEPRLI)) {
 4864				delay = 1000;
 4865				maxretry = 48;
 4866
 4867				/* An authentication LS_RJT reason code
 4868				 * explanation means some error in the
 4869				 * security settings end-to-end.  Reduce
 4870				 * the retry count to allow lpfc to clear
 4871				 * RSCN mode and not race with dev_loss.
 4872				 */
 4873				if (cmd == ELS_CMD_PLOGI &&
 4874				    rsn_code_exp == LSEXP_AUTH_REQ)
 4875					maxretry = 8;
 4876			} else if (cmd == ELS_CMD_FDISC) {
 4877				/* FDISC retry policy */
 4878				maxretry = 48;
 4879				if (cmdiocb->retry >= 32)
 4880					delay = 1000;
 4881			}
 4882			retry = 1;
 4883			break;
 4884
 4885		case LSRJT_LOGICAL_ERR:
 4886			/* There are some cases where switches return this
 4887			 * error when they are not ready and should be returning
 4888			 * Logical Busy. We should delay every time.
 4889			 */
 4890			if (cmd == ELS_CMD_FDISC &&
 4891			    stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
 4892				maxretry = 3;
 4893				delay = 1000;
 4894				retry = 1;
 4895			} else if (cmd == ELS_CMD_FLOGI &&
 4896				   stat.un.b.lsRjtRsnCodeExp ==
 4897						LSEXP_NOTHING_MORE) {
 4898				vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
 4899				retry = 1;
 4900				lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
 4901					      "0820 FLOGI (x%x). "
 4902					      "BBCredit Not Supported\n",
 4903					      stat.un.lsRjtError);
 4904			} else if (cmd == ELS_CMD_PLOGI) {
 4905				rsn_code_exp = stat.un.b.lsRjtRsnCodeExp;
 4906
 4907				/* An authentication LS_RJT reason code
 4908				 * explanation means some error in the
 4909				 * security settings end-to-end.  Reduce
 4910				 * the retry count to allow lpfc to clear
 4911				 * RSCN mode and not race with dev_loss.
 4912				 */
 4913				if (rsn_code_exp == LSEXP_AUTH_REQ) {
 4914					delay = 1000;
 4915					retry = 1;
 4916					maxretry = 8;
 4917				}
 4918			}
 4919			break;
 4920
 4921		case LSRJT_PROTOCOL_ERR:
 4922			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
 4923			  (cmd == ELS_CMD_FDISC) &&
 4924			  ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
 4925			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
 4926			  ) {
 4927				lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
 4928					      "0122 FDISC (x%x). "
 4929					      "Fabric Detected Bad WWN\n",
 4930					      stat.un.lsRjtError);
 4931				lpfc_vport_set_state(vport,
 4932						     FC_VPORT_FABRIC_REJ_WWN);
 4933			}
 4934			break;
 4935		case LSRJT_VENDOR_UNIQUE:
 4936			if ((stat.un.b.vendorUnique == 0x45) &&
 4937			    (cmd == ELS_CMD_FLOGI)) {
 4938				goto out_retry;
 4939			}
 4940			break;
 4941		case LSRJT_CMD_UNSUPPORTED:
 4942			/* lpfc nvmet returns this type of LS_RJT when it
 4943			 * receives an FCP PRLI because lpfc nvmet only
 4944			 * support NVME.  ELS request is terminated for FCP4
 4945			 * on this rport.
 4946			 */
 4947			if (stat.un.b.lsRjtRsnCodeExp ==
 4948			    LSEXP_REQ_UNSUPPORTED) {
 4949				if (cmd == ELS_CMD_PRLI)
 4950					goto out_retry;
 4951			}
 4952			break;
 4953		}
 4954		break;
 4955
 4956	case IOSTAT_INTERMED_RSP:
 4957	case IOSTAT_BA_RJT:
 4958		break;
 4959
 4960	default:
 4961		break;
 4962	}
 4963
 4964	if (link_reset) {
 4965		rc = lpfc_link_reset(vport);
 4966		if (rc) {
 4967			/* Do not give up. Retry PLOGI one more time and attempt
 4968			 * link reset if PLOGI fails again.
 4969			 */
 4970			retry = 1;
 4971			delay = 100;
 4972			goto out_retry;
 4973		}
 4974		return 1;
 4975	}
 4976
 4977	if (did == FDMI_DID)
 4978		retry = 1;
 4979
 4980	if ((cmd == ELS_CMD_FLOGI) &&
 4981	    (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
 4982	    !lpfc_error_lost_link(vport, ulp_status, ulp_word4)) {
 4983		/* FLOGI retry policy */
 4984		retry = 1;
 4985		/* retry FLOGI forever */
 4986		if (phba->link_flag != LS_LOOPBACK_MODE)
 4987			maxretry = 0;
 4988		else
 4989			maxretry = 2;
 4990
 4991		if (cmdiocb->retry >= 100)
 4992			delay = 5000;
 4993		else if (cmdiocb->retry >= 32)
 4994			delay = 1000;
 4995	} else if ((cmd == ELS_CMD_FDISC) &&
 4996	    !lpfc_error_lost_link(vport, ulp_status, ulp_word4)) {
 4997		/* retry FDISCs every second up to devloss */
 4998		retry = 1;
 4999		maxretry = vport->cfg_devloss_tmo;
 5000		delay = 1000;
 5001	}
 5002
 5003	cmdiocb->retry++;
 5004	if (maxretry && (cmdiocb->retry >= maxretry)) {
 5005		phba->fc_stat.elsRetryExceeded++;
 5006		retry = 0;
 5007	}
 5008
 5009	if (test_bit(FC_UNLOADING, &vport->load_flag))
 5010		retry = 0;
 5011
 5012out_retry:
 5013	if (retry) {
 5014		if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
 5015			/* Stop retrying PLOGI and FDISC if in FCF discovery */
 5016			if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
 5017				lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 5018						 "2849 Stop retry ELS command "
 5019						 "x%x to remote NPORT x%x, "
 5020						 "Data: x%x x%x\n", cmd, did,
 5021						 cmdiocb->retry, delay);
 5022				return 0;
 5023			}
 5024		}
 5025
 5026		/* Retry ELS command <elsCmd> to remote NPORT <did> */
 5027		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 5028				 "0107 Retry ELS command x%x to remote "
 5029				 "NPORT x%x Data: x%x x%x\n",
 5030				 cmd, did, cmdiocb->retry, delay);
 5031
 5032		if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
 5033			((ulp_status != IOSTAT_LOCAL_REJECT) ||
 5034			((ulp_word4 & IOERR_PARAM_MASK) !=
 5035			IOERR_NO_RESOURCES))) {
 5036			/* Don't reset timer for no resources */
 5037
 5038			/* If discovery / RSCN timer is running, reset it */
 5039			if (timer_pending(&vport->fc_disctmo) ||
 5040			    test_bit(FC_RSCN_MODE, &vport->fc_flag))
 5041				lpfc_set_disctmo(vport);
 5042		}
 5043
 5044		phba->fc_stat.elsXmitRetry++;
 5045		if (ndlp && delay) {
 5046			phba->fc_stat.elsDelayRetry++;
 5047			ndlp->nlp_retry = cmdiocb->retry;
 5048
 5049			/* delay is specified in milliseconds */
 5050			mod_timer(&ndlp->nlp_delayfunc,
 5051				jiffies + msecs_to_jiffies(delay));
 5052			set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
 5053
 5054			ndlp->nlp_prev_state = ndlp->nlp_state;
 5055			if ((cmd == ELS_CMD_PRLI) ||
 5056			    (cmd == ELS_CMD_NVMEPRLI))
 5057				lpfc_nlp_set_state(vport, ndlp,
 5058					NLP_STE_PRLI_ISSUE);
 5059			else if (cmd != ELS_CMD_ADISC)
 5060				lpfc_nlp_set_state(vport, ndlp,
 5061					NLP_STE_NPR_NODE);
 5062			ndlp->nlp_last_elscmd = cmd;
 5063
 5064			return 1;
 5065		}
 5066		switch (cmd) {
 5067		case ELS_CMD_FLOGI:
 5068			lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
 5069			return 1;
 5070		case ELS_CMD_FDISC:
 5071			lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
 5072			return 1;
 5073		case ELS_CMD_PLOGI:
 5074			if (ndlp) {
 5075				ndlp->nlp_prev_state = ndlp->nlp_state;
 5076				lpfc_nlp_set_state(vport, ndlp,
 5077						   NLP_STE_PLOGI_ISSUE);
 5078			}
 5079			lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
 5080			return 1;
 5081		case ELS_CMD_ADISC:
 5082			ndlp->nlp_prev_state = ndlp->nlp_state;
 5083			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
 5084			lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
 5085			return 1;
 5086		case ELS_CMD_PRLI:
 5087		case ELS_CMD_NVMEPRLI:
 5088			ndlp->nlp_prev_state = ndlp->nlp_state;
 5089			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
 5090			lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
 5091			return 1;
 5092		case ELS_CMD_LOGO:
 5093			ndlp->nlp_prev_state = ndlp->nlp_state;
 5094			lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
 5095			lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
 5096			return 1;
 5097		}
 5098	}
 5099	/* No retry ELS command <elsCmd> to remote NPORT <did> */
 5100	if (logerr) {
 5101		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 5102			 "0137 No retry ELS command x%x to remote "
 5103			 "NPORT x%x: Out of Resources: Error:x%x/%x "
 5104			 "IoTag x%x\n",
 5105			 cmd, did, ulp_status, ulp_word4,
 5106			 cmdiocb->iotag);
 5107	}
 5108	else {
 5109		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 5110				 "0108 No retry ELS command x%x to remote "
 5111				 "NPORT x%x Retried:%d Error:x%x/%x "
 5112				 "IoTag x%x nflags x%lx\n",
 5113				 cmd, did, cmdiocb->retry, ulp_status,
 5114				 ulp_word4, cmdiocb->iotag,
 5115				 (ndlp ? ndlp->nlp_flag : 0));
 5116	}
 5117	return 0;
 5118}
 5119
 5120/**
 5121 * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
 5122 * @phba: pointer to lpfc hba data structure.
 5123 * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
 5124 *
 5125 * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
 5126 * associated with a command IOCB back to the lpfc DMA buffer pool. It first
 5127 * checks to see whether there is a lpfc DMA buffer associated with the
 5128 * response of the command IOCB. If so, it will be released before releasing
 5129 * the lpfc DMA buffer associated with the IOCB itself.
 5130 *
 5131 * Return code
 5132 *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
 5133 **/
 5134static int
 5135lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
 5136{
 5137	struct lpfc_dmabuf *buf_ptr;
 5138
 5139	/* Free the response before processing the command. */
 5140	if (!list_empty(&buf_ptr1->list)) {
 5141		list_remove_head(&buf_ptr1->list, buf_ptr,
 5142				 struct lpfc_dmabuf,
 5143				 list);
 5144		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
 5145		kfree(buf_ptr);
 5146	}
 5147	lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
 5148	kfree(buf_ptr1);
 5149	return 0;
 5150}
 5151
 5152/**
 5153 * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
 5154 * @phba: pointer to lpfc hba data structure.
 5155 * @buf_ptr: pointer to the lpfc dma buffer data structure.
 5156 *
 5157 * This routine releases the lpfc Direct Memory Access (DMA) buffer
 5158 * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
 5159 * pool.
 5160 *
 5161 * Return code
 5162 *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
 5163 **/
 5164static int
 5165lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
 5166{
 5167	lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
 5168	kfree(buf_ptr);
 5169	return 0;
 5170}
 5171
 5172/**
 5173 * lpfc_els_free_iocb - Free a command iocb and its associated resources
 5174 * @phba: pointer to lpfc hba data structure.
 5175 * @elsiocb: pointer to lpfc els command iocb data structure.
 5176 *
 5177 * This routine frees a command IOCB and its associated resources. The
 5178 * command IOCB data structure contains the reference to various associated
 5179 * resources, these fields must be set to NULL if the associated reference
 5180 * not present:
 5181 *   cmd_dmabuf - reference to cmd.
 5182 *   cmd_dmabuf->next - reference to rsp
 5183 *   rsp_dmabuf - unused
 5184 *   bpl_dmabuf - reference to bpl
 5185 *
 5186 * It first properly decrements the reference count held on ndlp for the
 5187 * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
 5188 * set, it invokes the lpfc_els_free_data() routine to release the Direct
 5189 * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
 5190 * adds the DMA buffer the @phba data structure for the delayed release.
 5191 * If reference to the Buffer Pointer List (BPL) is present, the
 5192 * lpfc_els_free_bpl() routine is invoked to release the DMA memory
 5193 * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
 5194 * invoked to release the IOCB data structure back to @phba IOCBQ list.
 5195 *
 5196 * Return code
 5197 *   0 - Success (currently, always return 0)
 5198 **/
 5199int
 5200lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
 5201{
 5202	struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
 5203
 5204	/* The I/O iocb is complete.  Clear the node and first dmabuf */
 5205	elsiocb->ndlp = NULL;
 5206
 5207	/* cmd_dmabuf = cmd,  cmd_dmabuf->next = rsp, bpl_dmabuf = bpl */
 5208	if (elsiocb->cmd_dmabuf) {
 5209		if (elsiocb->cmd_flag & LPFC_DELAY_MEM_FREE) {
 5210			/* Firmware could still be in progress of DMAing
 5211			 * payload, so don't free data buffer till after
 5212			 * a hbeat.
 5213			 */
 5214			elsiocb->cmd_flag &= ~LPFC_DELAY_MEM_FREE;
 5215			buf_ptr = elsiocb->cmd_dmabuf;
 5216			elsiocb->cmd_dmabuf = NULL;
 5217			if (buf_ptr) {
 5218				buf_ptr1 = NULL;
 5219				spin_lock_irq(&phba->hbalock);
 5220				if (!list_empty(&buf_ptr->list)) {
 5221					list_remove_head(&buf_ptr->list,
 5222						buf_ptr1, struct lpfc_dmabuf,
 5223						list);
 5224					INIT_LIST_HEAD(&buf_ptr1->list);
 5225					list_add_tail(&buf_ptr1->list,
 5226						&phba->elsbuf);
 5227					phba->elsbuf_cnt++;
 5228				}
 5229				INIT_LIST_HEAD(&buf_ptr->list);
 5230				list_add_tail(&buf_ptr->list, &phba->elsbuf);
 5231				phba->elsbuf_cnt++;
 5232				spin_unlock_irq(&phba->hbalock);
 5233			}
 5234		} else {
 5235			buf_ptr1 = elsiocb->cmd_dmabuf;
 5236			lpfc_els_free_data(phba, buf_ptr1);
 5237		}
 5238	}
 5239
 5240	if (elsiocb->bpl_dmabuf) {
 5241		buf_ptr = elsiocb->bpl_dmabuf;
 5242		lpfc_els_free_bpl(phba, buf_ptr);
 5243	}
 5244	lpfc_sli_release_iocbq(phba, elsiocb);
 5245	return 0;
 5246}
 5247
 5248/**
 5249 * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
 5250 * @phba: pointer to lpfc hba data structure.
 5251 * @cmdiocb: pointer to lpfc command iocb data structure.
 5252 * @rspiocb: pointer to lpfc response iocb data structure.
 5253 *
 5254 * This routine is the completion callback function to the Logout (LOGO)
 5255 * Accept (ACC) Response ELS command. This routine is invoked to indicate
 5256 * the completion of the LOGO process. If the node has transitioned to NPR,
 5257 * this routine unregisters the RPI if it is still registered. The
 5258 * lpfc_els_free_iocb() is invoked to release the IOCB data structure.
 5259 **/
 5260static void
 5261lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 5262		       struct lpfc_iocbq *rspiocb)
 5263{
 5264	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
 5265	struct lpfc_vport *vport = cmdiocb->vport;
 5266	u32 ulp_status, ulp_word4;
 5267
 5268	ulp_status = get_job_ulpstatus(phba, rspiocb);
 5269	ulp_word4 = get_job_word4(phba, rspiocb);
 5270
 5271	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 5272		"ACC LOGO cmpl:   status:x%x/x%x did:x%x",
 5273		ulp_status, ulp_word4, ndlp->nlp_DID);
 5274	/* ACC to LOGO completes to NPort <nlp_DID> */
 5275	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 5276			 "0109 ACC to LOGO completes to NPort x%x refcnt %d "
 5277			 "last els x%x Data: x%lx x%x x%x\n",
 5278			 ndlp->nlp_DID, kref_read(&ndlp->kref),
 5279			 ndlp->nlp_last_elscmd, ndlp->nlp_flag, ndlp->nlp_state,
 5280			 ndlp->nlp_rpi);
 5281
 5282	/* This clause allows the LOGO ACC to complete and free resources
 5283	 * for the Fabric Domain Controller.  It does deliberately skip
 5284	 * the unreg_rpi and release rpi because some fabrics send RDP
 5285	 * requests after logging out from the initiator.
 5286	 */
 5287	if (ndlp->nlp_type & NLP_FABRIC &&
 5288	    ((ndlp->nlp_DID & WELL_KNOWN_DID_MASK) != WELL_KNOWN_DID_MASK))
 5289		goto out;
 5290
 5291	if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
 5292		if (test_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag))
 5293			lpfc_unreg_rpi(vport, ndlp);
 5294
 5295		/* If came from PRLO, then PRLO_ACC is done.
 5296		 * Start rediscovery now.
 5297		 */
 5298		if (ndlp->nlp_last_elscmd == ELS_CMD_PRLO) {
 5299			set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
 5300			ndlp->nlp_prev_state = ndlp->nlp_state;
 5301			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
 5302			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
 5303		}
 5304	}
 5305
 5306 out:
 5307	/*
 5308	 * The driver received a LOGO from the rport and has ACK'd it.
 5309	 * At this point, the driver is done so release the IOCB
 5310	 */
 5311	lpfc_els_free_iocb(phba, cmdiocb);
 5312	lpfc_nlp_put(ndlp);
 5313}
 5314
 5315/**
 5316 * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
 5317 * @phba: pointer to lpfc hba data structure.
 5318 * @pmb: pointer to the driver internal queue element for mailbox command.
 5319 *
 5320 * This routine is the completion callback function for unregister default
 5321 * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
 5322 * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
 5323 * decrements the ndlp reference count held for this completion callback
 5324 * function. After that, it invokes the lpfc_drop_node to check
 5325 * whether it is appropriate to release the node.
 5326 **/
 5327void
 5328lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 5329{
 5330	struct lpfc_nodelist *ndlp = pmb->ctx_ndlp;
 5331	u32 mbx_flag = pmb->mbox_flag;
 5332	u32 mbx_cmd = pmb->u.mb.mbxCommand;
 5333
 5334	if (ndlp) {
 5335		lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
 5336				 "0006 rpi x%x DID:%x flg:%lx %d x%px "
 5337				 "mbx_cmd x%x mbx_flag x%x x%px\n",
 5338				 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
 5339				 kref_read(&ndlp->kref), ndlp, mbx_cmd,
 5340				 mbx_flag, pmb);
 5341
 5342		/* This ends the default/temporary RPI cleanup logic for this
 5343		 * ndlp and the node and rpi needs to be released. Free the rpi
 5344		 * first on an UNREG_LOGIN and then release the final
 5345		 * references.
 5346		 */
 5347		clear_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
 5348		if (mbx_cmd == MBX_UNREG_LOGIN)
 5349			clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
 5350		lpfc_nlp_put(ndlp);
 5351		lpfc_drop_node(ndlp->vport, ndlp);
 5352	}
 5353
 5354	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
 5355}
 5356
 5357/**
 5358 * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
 5359 * @phba: pointer to lpfc hba data structure.
 5360 * @cmdiocb: pointer to lpfc command iocb data structure.
 5361 * @rspiocb: pointer to lpfc response iocb data structure.
 5362 *
 5363 * This routine is the completion callback function for ELS Response IOCB
 5364 * command. In normal case, this callback function just properly sets the
 5365 * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
 5366 * field in the command IOCB is not NULL, the referred mailbox command will
 5367 * be send out, and then invokes the lpfc_els_free_iocb() routine to release
 5368 * the IOCB.
 5369 **/
 5370static void
 5371lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 5372		  struct lpfc_iocbq *rspiocb)
 5373{
 5374	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
 5375	struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
 5376	struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
 5377	IOCB_t  *irsp;
 5378	LPFC_MBOXQ_t *mbox = NULL;
 5379	u32 ulp_status, ulp_word4, tmo, did, iotag;
 5380	u32 cmd;
 5381
 5382	if (!vport) {
 5383		lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
 5384				"3177 null vport in ELS rsp\n");
 5385		goto release;
 5386	}
 5387	if (cmdiocb->context_un.mbox)
 5388		mbox = cmdiocb->context_un.mbox;
 5389
 5390	ulp_status = get_job_ulpstatus(phba, rspiocb);
 5391	ulp_word4 = get_job_word4(phba, rspiocb);
 5392	did = get_job_els_rsp64_did(phba, cmdiocb);
 5393
 5394	if (phba->sli_rev == LPFC_SLI_REV4) {
 5395		tmo = get_wqe_tmo(cmdiocb);
 5396		iotag = get_wqe_reqtag(cmdiocb);
 5397	} else {
 5398		irsp = &rspiocb->iocb;
 5399		tmo = irsp->ulpTimeout;
 5400		iotag = irsp->ulpIoTag;
 5401	}
 5402
 5403	/* Check to see if link went down during discovery */
 5404	if (!ndlp || lpfc_els_chk_latt(vport)) {
 5405		if (mbox)
 5406			lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
 5407		goto out;
 5408	}
 5409
 5410	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 5411		"ELS rsp cmpl:    status:x%x/x%x did:x%x",
 5412		ulp_status, ulp_word4, did);
 5413	/* ELS response tag <ulpIoTag> completes */
 5414	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 5415			 "0110 ELS response tag x%x completes fc_flag x%lx"
 5416			 "Data: x%x x%x x%x x%x x%lx x%x x%x x%x %p %p\n",
 5417			 iotag, vport->fc_flag, ulp_status, ulp_word4, tmo,
 5418			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
 5419			 ndlp->nlp_rpi, kref_read(&ndlp->kref), mbox, ndlp);
 5420	if (mbox && !test_bit(FC_PT2PT, &vport->fc_flag)) {
 5421		if (ulp_status == 0 &&
 5422		    test_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag)) {
 5423			if (!lpfc_unreg_rpi(vport, ndlp) &&
 5424			    !test_bit(FC_PT2PT, &vport->fc_flag)) {
 5425				if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
 5426				    ndlp->nlp_state ==
 5427				     NLP_STE_REG_LOGIN_ISSUE) {
 5428					lpfc_printf_vlog(vport, KERN_INFO,
 5429							 LOG_DISCOVERY,
 5430							 "0314 PLOGI recov "
 5431							 "DID x%x "
 5432							 "Data: x%x x%x x%lx\n",
 5433							 ndlp->nlp_DID,
 5434							 ndlp->nlp_state,
 5435							 ndlp->nlp_rpi,
 5436							 ndlp->nlp_flag);
 5437					goto out_free_mbox;
 5438				}
 5439			}
 5440
 5441			/* Increment reference count to ndlp to hold the
 5442			 * reference to ndlp for the callback function.
 5443			 */
 5444			mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
 5445			if (!mbox->ctx_ndlp)
 5446				goto out_free_mbox;
 5447
 5448			mbox->vport = vport;
 5449			if (test_bit(NLP_RM_DFLT_RPI, &ndlp->nlp_flag)) {
 5450				mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
 5451				mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
 5452			} else {
 5453				mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
 5454				ndlp->nlp_prev_state = ndlp->nlp_state;
 5455				lpfc_nlp_set_state(vport, ndlp,
 5456					   NLP_STE_REG_LOGIN_ISSUE);
 5457			}
 5458
 5459			set_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
 5460			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
 5461			    != MBX_NOT_FINISHED)
 5462				goto out;
 5463
 5464			/* Decrement the ndlp reference count we
 5465			 * set for this failed mailbox command.
 5466			 */
 5467			lpfc_nlp_put(ndlp);
 5468			clear_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
 5469
 5470			/* ELS rsp: Cannot issue reg_login for <NPortid> */
 5471			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 5472				"0138 ELS rsp: Cannot issue reg_login for x%x "
 5473				"Data: x%lx x%x x%x\n",
 5474				ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
 5475				ndlp->nlp_rpi);
 5476		}
 5477out_free_mbox:
 5478		lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
 5479	} else if (mbox && test_bit(FC_PT2PT, &vport->fc_flag) &&
 5480		   test_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag)) {
 5481		lpfc_mbx_cmpl_reg_login(phba, mbox);
 5482		clear_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag);
 5483	}
 5484out:
 5485	if (ndlp && shost) {
 5486		if (mbox)
 5487			clear_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag);
 5488		clear_bit(NLP_RM_DFLT_RPI, &ndlp->nlp_flag);
 5489	}
 5490
 5491	/* An SLI4 NPIV instance wants to drop the node at this point under
 5492	 * these conditions because it doesn't need the login.
 5493	 */
 5494	if (phba->sli_rev == LPFC_SLI_REV4 &&
 5495	    vport->port_type == LPFC_NPIV_PORT &&
 5496	    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD)) {
 5497		if (ndlp->nlp_state != NLP_STE_PLOGI_ISSUE &&
 5498		    ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE &&
 5499		    ndlp->nlp_state != NLP_STE_PRLI_ISSUE) {
 5500			/* Drop ndlp if there is no planned or outstanding
 5501			 * issued PRLI.
 5502			 *
 5503			 * In cases when the ndlp is acting as both an initiator
 5504			 * and target function, let our issued PRLI determine
 5505			 * the final ndlp kref drop.
 5506			 */
 5507			lpfc_drop_node(vport, ndlp);
 5508		}
 5509	}
 5510
 5511	/* The driver's unsolicited deferred FLOGI ACC in Pt2Pt needs to
 5512	 * release the initial reference because the put after the free_iocb
 5513	 * call removes only the reference from the defer logic. This FLOGI
 5514	 * is never registered with the SCSI transport.
 5515	 */
 5516	if (test_bit(FC_PT2PT, &vport->fc_flag) &&
 5517	    test_and_clear_bit(NLP_FLOGI_DFR_ACC, &ndlp->nlp_flag)) {
 5518		lpfc_printf_vlog(vport, KERN_INFO,
 5519				 LOG_ELS | LOG_NODE | LOG_DISCOVERY,
 5520				 "3357 Pt2Pt Defer FLOGI ACC ndlp x%px, "
 5521				 "nflags x%lx, fc_flag x%lx\n",
 5522				 ndlp, ndlp->nlp_flag,
 5523				 vport->fc_flag);
 5524		cmd = *((u32 *)cmdiocb->cmd_dmabuf->virt);
 5525		if (cmd == ELS_CMD_ACC) {
 5526			if (!test_and_set_bit(NLP_DROPPED, &ndlp->nlp_flag))
 5527				lpfc_nlp_put(ndlp);
 5528		}
 5529	}
 5530
 5531release:
 5532	/* Release the originating I/O reference. */
 5533	lpfc_els_free_iocb(phba, cmdiocb);
 5534	lpfc_nlp_put(ndlp);
 5535	return;
 5536}
 5537
 5538/**
 5539 * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
 5540 * @vport: pointer to a host virtual N_Port data structure.
 5541 * @flag: the els command code to be accepted.
 5542 * @oldiocb: pointer to the original lpfc command iocb data structure.
 5543 * @ndlp: pointer to a node-list data structure.
 5544 * @mbox: pointer to the driver internal queue element for mailbox command.
 5545 *
 5546 * This routine prepares and issues an Accept (ACC) response IOCB
 5547 * command. It uses the @flag to properly set up the IOCB field for the
 5548 * specific ACC response command to be issued and invokes the
 5549 * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
 5550 * @mbox pointer is passed in, it will be put into the context_un.mbox
 5551 * field of the IOCB for the completion callback function to issue the
 5552 * mailbox command to the HBA later when callback is invoked.
 5553 *
 5554 * Note that the ndlp reference count will be incremented by 1 for holding the
 5555 * ndlp and the reference to ndlp will be stored into the ndlp field of
 5556 * the IOCB for the completion callback function to the corresponding
 5557 * response ELS IOCB command.
 5558 *
 5559 * Return code
 5560 *   0 - Successfully issued acc response
 5561 *   1 - Failed to issue acc response
 5562 **/
 5563int
 5564lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
 5565		 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
 5566		 LPFC_MBOXQ_t *mbox)
 5567{
 5568	struct lpfc_hba  *phba = vport->phba;
 5569	IOCB_t *icmd;
 5570	IOCB_t *oldcmd;
 5571	union lpfc_wqe128 *wqe;
 5572	union lpfc_wqe128 *oldwqe = &oldiocb->wqe;
 5573	struct lpfc_iocbq *elsiocb;
 5574	uint8_t *pcmd;
 5575	struct serv_parm *sp;
 5576	uint16_t cmdsize;
 5577	int rc;
 5578	ELS_PKT *els_pkt_ptr;
 5579	struct fc_els_rdf_resp *rdf_resp;
 5580
 5581	switch (flag) {
 5582	case ELS_CMD_ACC:
 5583		cmdsize = sizeof(uint32_t);
 5584		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
 5585					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
 5586		if (!elsiocb) {
 5587			clear_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
 5588			return 1;
 5589		}
 5590
 5591		if (phba->sli_rev == LPFC_SLI_REV4) {
 5592			wqe = &elsiocb->wqe;
 5593			/* XRI / rx_id */
 5594			bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
 5595			       bf_get(wqe_ctxt_tag,
 5596				      &oldwqe->xmit_els_rsp.wqe_com));
 5597
 5598			/* oxid */
 5599			bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 5600			       bf_get(wqe_rcvoxid,
 5601				      &oldwqe->xmit_els_rsp.wqe_com));
 5602		} else {
 5603			icmd = &elsiocb->iocb;
 5604			oldcmd = &oldiocb->iocb;
 5605			icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
 5606			icmd->unsli3.rcvsli3.ox_id =
 5607				oldcmd->unsli3.rcvsli3.ox_id;
 5608		}
 5609
 5610		pcmd = elsiocb->cmd_dmabuf->virt;
 5611		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
 5612		pcmd += sizeof(uint32_t);
 5613
 5614		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 5615			"Issue ACC:       did:x%x flg:x%lx",
 5616			ndlp->nlp_DID, ndlp->nlp_flag, 0);
 5617		break;
 5618	case ELS_CMD_FLOGI:
 5619	case ELS_CMD_PLOGI:
 5620		cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
 5621		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
 5622					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
 5623		if (!elsiocb)
 5624			return 1;
 5625
 5626		if (phba->sli_rev == LPFC_SLI_REV4) {
 5627			wqe = &elsiocb->wqe;
 5628			/* XRI / rx_id */
 5629			bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
 5630			       bf_get(wqe_ctxt_tag,
 5631				      &oldwqe->xmit_els_rsp.wqe_com));
 5632
 5633			/* oxid */
 5634			bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 5635			       bf_get(wqe_rcvoxid,
 5636				      &oldwqe->xmit_els_rsp.wqe_com));
 5637		} else {
 5638			icmd = &elsiocb->iocb;
 5639			oldcmd = &oldiocb->iocb;
 5640			icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
 5641			icmd->unsli3.rcvsli3.ox_id =
 5642				oldcmd->unsli3.rcvsli3.ox_id;
 5643		}
 5644
 5645		pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
 5646
 5647		if (mbox)
 5648			elsiocb->context_un.mbox = mbox;
 5649
 5650		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
 5651		pcmd += sizeof(uint32_t);
 5652		sp = (struct serv_parm *)pcmd;
 5653
 5654		if (flag == ELS_CMD_FLOGI) {
 5655			/* Copy the received service parameters back */
 5656			memcpy(sp, &phba->fc_fabparam,
 5657			       sizeof(struct serv_parm));
 5658
 5659			/* Clear the F_Port bit */
 5660			sp->cmn.fPort = 0;
 5661
 5662			/* Mark all class service parameters as invalid */
 5663			sp->cls1.classValid = 0;
 5664			sp->cls2.classValid = 0;
 5665			sp->cls3.classValid = 0;
 5666
 5667			/* Copy our worldwide names */
 5668			memcpy(&sp->portName, &vport->fc_sparam.portName,
 5669			       sizeof(struct lpfc_name));
 5670			memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
 5671			       sizeof(struct lpfc_name));
 5672		} else {
 5673			memcpy(pcmd, &vport->fc_sparam,
 5674			       sizeof(struct serv_parm));
 5675
 5676			sp->cmn.valid_vendor_ver_level = 0;
 5677			memset(sp->un.vendorVersion, 0,
 5678			       sizeof(sp->un.vendorVersion));
 5679			if (!test_bit(FC_PT2PT, &vport->fc_flag))
 5680				sp->cmn.bbRcvSizeMsb &= 0xF;
 5681
 5682			/* If our firmware supports this feature, convey that
 5683			 * info to the target using the vendor specific field.
 5684			 */
 5685			if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
 5686				sp->cmn.valid_vendor_ver_level = 1;
 5687				sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
 5688				sp->un.vv.flags =
 5689					cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
 5690			}
 5691		}
 5692
 5693		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 5694			"Issue ACC FLOGI/PLOGI: did:x%x flg:x%lx",
 5695			ndlp->nlp_DID, ndlp->nlp_flag, 0);
 5696		break;
 5697	case ELS_CMD_PRLO:
 5698		cmdsize = sizeof(uint32_t) + sizeof(PRLO);
 5699		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
 5700					     ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
 5701		if (!elsiocb)
 5702			return 1;
 5703
 5704		if (phba->sli_rev == LPFC_SLI_REV4) {
 5705			wqe = &elsiocb->wqe;
 5706			/* XRI / rx_id */
 5707			bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
 5708			       bf_get(wqe_ctxt_tag,
 5709				      &oldwqe->xmit_els_rsp.wqe_com));
 5710
 5711			/* oxid */
 5712			bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 5713			       bf_get(wqe_rcvoxid,
 5714				      &oldwqe->xmit_els_rsp.wqe_com));
 5715		} else {
 5716			icmd = &elsiocb->iocb;
 5717			oldcmd = &oldiocb->iocb;
 5718			icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
 5719			icmd->unsli3.rcvsli3.ox_id =
 5720				oldcmd->unsli3.rcvsli3.ox_id;
 5721		}
 5722
 5723		pcmd = (u8 *) elsiocb->cmd_dmabuf->virt;
 5724
 5725		memcpy(pcmd, oldiocb->cmd_dmabuf->virt,
 5726		       sizeof(uint32_t) + sizeof(PRLO));
 5727		*((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
 5728		els_pkt_ptr = (ELS_PKT *) pcmd;
 5729		els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
 5730
 5731		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 5732			"Issue ACC PRLO:  did:x%x flg:x%lx",
 5733			ndlp->nlp_DID, ndlp->nlp_flag, 0);
 5734		break;
 5735	case ELS_CMD_RDF:
 5736		cmdsize = sizeof(*rdf_resp);
 5737		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
 5738					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
 5739		if (!elsiocb)
 5740			return 1;
 5741
 5742		if (phba->sli_rev == LPFC_SLI_REV4) {
 5743			wqe = &elsiocb->wqe;
 5744			/* XRI / rx_id */
 5745			bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
 5746			       bf_get(wqe_ctxt_tag,
 5747				      &oldwqe->xmit_els_rsp.wqe_com));
 5748
 5749			/* oxid */
 5750			bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 5751			       bf_get(wqe_rcvoxid,
 5752				      &oldwqe->xmit_els_rsp.wqe_com));
 5753		} else {
 5754			icmd = &elsiocb->iocb;
 5755			oldcmd = &oldiocb->iocb;
 5756			icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
 5757			icmd->unsli3.rcvsli3.ox_id =
 5758				oldcmd->unsli3.rcvsli3.ox_id;
 5759		}
 5760
 5761		pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
 5762		rdf_resp = (struct fc_els_rdf_resp *)pcmd;
 5763		memset(rdf_resp, 0, sizeof(*rdf_resp));
 5764		rdf_resp->acc_hdr.la_cmd = ELS_LS_ACC;
 5765
 5766		/* FC-LS-5 specifies desc_list_len shall be set to 12 */
 5767		rdf_resp->desc_list_len = cpu_to_be32(12);
 5768
 5769		/* FC-LS-5 specifies LS REQ Information descriptor */
 5770		rdf_resp->lsri.desc_tag = cpu_to_be32(1);
 5771		rdf_resp->lsri.desc_len = cpu_to_be32(sizeof(u32));
 5772		rdf_resp->lsri.rqst_w0.cmd = ELS_RDF;
 5773		break;
 5774	default:
 5775		return 1;
 5776	}
 5777	if (test_bit(NLP_LOGO_ACC, &ndlp->nlp_flag)) {
 5778		if (!test_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag) &&
 5779		    !test_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag))
 5780			clear_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
 5781		elsiocb->cmd_cmpl = lpfc_cmpl_els_logo_acc;
 5782	} else {
 5783		elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 5784	}
 5785
 5786	phba->fc_stat.elsXmitACC++;
 5787	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 5788	if (!elsiocb->ndlp) {
 5789		lpfc_els_free_iocb(phba, elsiocb);
 5790		return 1;
 5791	}
 5792
 5793	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 5794	if (rc == IOCB_ERROR) {
 5795		lpfc_els_free_iocb(phba, elsiocb);
 5796		lpfc_nlp_put(ndlp);
 5797		return 1;
 5798	}
 5799
 5800	/* Xmit ELS ACC response tag <ulpIoTag> */
 5801	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 5802			 "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
 5803			 "XRI: x%x, DID: x%x, nlp_flag: x%lx nlp_state: x%x "
 5804			 "RPI: x%x, fc_flag x%lx refcnt %d\n",
 5805			 rc, elsiocb->iotag, elsiocb->sli4_xritag,
 5806			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
 5807			 ndlp->nlp_rpi, vport->fc_flag, kref_read(&ndlp->kref));
 5808	return 0;
 5809}
 5810
 5811/**
 5812 * lpfc_els_rsp_reject - Prepare and issue a rjt response iocb command
 5813 * @vport: pointer to a virtual N_Port data structure.
 5814 * @rejectError: reject response to issue
 5815 * @oldiocb: pointer to the original lpfc command iocb data structure.
 5816 * @ndlp: pointer to a node-list data structure.
 5817 * @mbox: pointer to the driver internal queue element for mailbox command.
 5818 *
 5819 * This routine prepares and issue an Reject (RJT) response IOCB
 5820 * command. If a @mbox pointer is passed in, it will be put into the
 5821 * context_un.mbox field of the IOCB for the completion callback function
 5822 * to issue to the HBA later.
 5823 *
 5824 * Note that the ndlp reference count will be incremented by 1 for holding the
 5825 * ndlp and the reference to ndlp will be stored into the ndlp field of
 5826 * the IOCB for the completion callback function to the reject response
 5827 * ELS IOCB command.
 5828 *
 5829 * Return code
 5830 *   0 - Successfully issued reject response
 5831 *   1 - Failed to issue reject response
 5832 **/
 5833int
 5834lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
 5835		    struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
 5836		    LPFC_MBOXQ_t *mbox)
 5837{
 5838	int rc;
 5839	struct lpfc_hba  *phba = vport->phba;
 5840	IOCB_t *icmd;
 5841	IOCB_t *oldcmd;
 5842	union lpfc_wqe128 *wqe;
 5843	struct lpfc_iocbq *elsiocb;
 5844	uint8_t *pcmd;
 5845	uint16_t cmdsize;
 5846
 5847	cmdsize = 2 * sizeof(uint32_t);
 5848	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
 5849				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
 5850	if (!elsiocb)
 5851		return 1;
 5852
 5853	if (phba->sli_rev == LPFC_SLI_REV4) {
 5854		wqe = &elsiocb->wqe;
 5855		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
 5856		       get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
 5857		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 5858		       get_job_rcvoxid(phba, oldiocb));
 5859	} else {
 5860		icmd = &elsiocb->iocb;
 5861		oldcmd = &oldiocb->iocb;
 5862		icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
 5863		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
 5864	}
 5865
 5866	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 5867
 5868	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
 5869	pcmd += sizeof(uint32_t);
 5870	*((uint32_t *) (pcmd)) = rejectError;
 5871
 5872	if (mbox)
 5873		elsiocb->context_un.mbox = mbox;
 5874
 5875	/* Xmit ELS RJT <err> response tag <ulpIoTag> */
 5876	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 5877			 "0129 Xmit ELS RJT x%x response tag x%x "
 5878			 "xri x%x, did x%x, nlp_flag x%lx, nlp_state x%x, "
 5879			 "rpi x%x\n",
 5880			 rejectError, elsiocb->iotag,
 5881			 get_job_ulpcontext(phba, elsiocb), ndlp->nlp_DID,
 5882			 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
 5883	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 5884		"Issue LS_RJT:    did:x%x flg:x%lx err:x%x",
 5885		ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
 5886
 5887	phba->fc_stat.elsXmitLSRJT++;
 5888	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 5889	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 5890	if (!elsiocb->ndlp) {
 5891		lpfc_els_free_iocb(phba, elsiocb);
 5892		return 1;
 5893	}
 5894
 5895	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 5896	if (rc == IOCB_ERROR) {
 5897		lpfc_els_free_iocb(phba, elsiocb);
 5898		lpfc_nlp_put(ndlp);
 5899		return 1;
 5900	}
 5901
 5902	return 0;
 5903}
 5904
 5905 /**
 5906  * lpfc_issue_els_edc_rsp - Exchange Diagnostic Capabilities with the fabric.
 5907  * @vport: pointer to a host virtual N_Port data structure.
 5908  * @cmdiocb: pointer to the original lpfc command iocb data structure.
 5909  * @ndlp: NPort to where rsp is directed
 5910  *
 5911  * This routine issues an EDC ACC RSP to the F-Port Controller to communicate
 5912  * this N_Port's support of hardware signals in its Congestion
 5913  * Capabilities Descriptor.
 5914  *
 5915  * Return code
 5916  *   0 - Successfully issued edc rsp command
 5917  *   1 - Failed to issue edc rsp command
 5918  **/
 5919static int
 5920lpfc_issue_els_edc_rsp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 5921		       struct lpfc_nodelist *ndlp)
 5922{
 5923	struct lpfc_hba  *phba = vport->phba;
 5924	struct fc_els_edc_resp *edc_rsp;
 5925	struct fc_tlv_desc *tlv;
 5926	struct lpfc_iocbq *elsiocb;
 5927	IOCB_t *icmd, *cmd;
 5928	union lpfc_wqe128 *wqe;
 5929	u32 cgn_desc_size, lft_desc_size;
 5930	u16 cmdsize;
 5931	uint8_t *pcmd;
 5932	int rc;
 5933
 5934	cmdsize = sizeof(struct fc_els_edc_resp);
 5935	cgn_desc_size = sizeof(struct fc_diag_cg_sig_desc);
 5936	lft_desc_size = (lpfc_link_is_lds_capable(phba)) ?
 5937				sizeof(struct fc_diag_lnkflt_desc) : 0;
 5938	cmdsize += cgn_desc_size + lft_desc_size;
 5939	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, cmdiocb->retry,
 5940				     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
 5941	if (!elsiocb)
 5942		return 1;
 5943
 5944	if (phba->sli_rev == LPFC_SLI_REV4) {
 5945		wqe = &elsiocb->wqe;
 5946		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
 5947		       get_job_ulpcontext(phba, cmdiocb)); /* Xri / rx_id */
 5948		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 5949		       get_job_rcvoxid(phba, cmdiocb));
 5950	} else {
 5951		icmd = &elsiocb->iocb;
 5952		cmd = &cmdiocb->iocb;
 5953		icmd->ulpContext = cmd->ulpContext; /* Xri / rx_id */
 5954		icmd->unsli3.rcvsli3.ox_id = cmd->unsli3.rcvsli3.ox_id;
 5955	}
 5956
 5957	pcmd = elsiocb->cmd_dmabuf->virt;
 5958	memset(pcmd, 0, cmdsize);
 5959
 5960	edc_rsp = (struct fc_els_edc_resp *)pcmd;
 5961	edc_rsp->acc_hdr.la_cmd = ELS_LS_ACC;
 5962	edc_rsp->desc_list_len = cpu_to_be32(sizeof(struct fc_els_lsri_desc) +
 5963						cgn_desc_size + lft_desc_size);
 5964	edc_rsp->lsri.desc_tag = cpu_to_be32(ELS_DTAG_LS_REQ_INFO);
 5965	edc_rsp->lsri.desc_len = cpu_to_be32(
 5966		FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_els_lsri_desc));
 5967	edc_rsp->lsri.rqst_w0.cmd = ELS_EDC;
 5968	tlv = edc_rsp->desc;
 5969	lpfc_format_edc_cgn_desc(phba, tlv);
 5970	tlv = fc_tlv_next_desc(tlv);
 5971	if (lft_desc_size)
 5972		lpfc_format_edc_lft_desc(phba, tlv);
 5973
 5974	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 5975			      "Issue EDC ACC:      did:x%x flg:x%lx refcnt %d",
 5976			      ndlp->nlp_DID, ndlp->nlp_flag,
 5977			      kref_read(&ndlp->kref));
 5978	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 5979
 5980	phba->fc_stat.elsXmitACC++;
 5981	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 5982	if (!elsiocb->ndlp) {
 5983		lpfc_els_free_iocb(phba, elsiocb);
 5984		return 1;
 5985	}
 5986
 5987	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 5988	if (rc == IOCB_ERROR) {
 5989		lpfc_els_free_iocb(phba, elsiocb);
 5990		lpfc_nlp_put(ndlp);
 5991		return 1;
 5992	}
 5993
 5994	/* Xmit ELS ACC response tag <ulpIoTag> */
 5995	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 5996			 "0152 Xmit EDC ACC response Status: x%x, IoTag: x%x, "
 5997			 "XRI: x%x, DID: x%x, nlp_flag: x%lx nlp_state: x%x "
 5998			 "RPI: x%x, fc_flag x%lx\n",
 5999			 rc, elsiocb->iotag, elsiocb->sli4_xritag,
 6000			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
 6001			 ndlp->nlp_rpi, vport->fc_flag);
 6002
 6003	return 0;
 6004}
 6005
 6006/**
 6007 * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
 6008 * @vport: pointer to a virtual N_Port data structure.
 6009 * @oldiocb: pointer to the original lpfc command iocb data structure.
 6010 * @ndlp: pointer to a node-list data structure.
 6011 *
 6012 * This routine prepares and issues an Accept (ACC) response to Address
 6013 * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
 6014 * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
 6015 *
 6016 * Note that the ndlp reference count will be incremented by 1 for holding the
 6017 * ndlp and the reference to ndlp will be stored into the ndlp field of
 6018 * the IOCB for the completion callback function to the ADISC Accept response
 6019 * ELS IOCB command.
 6020 *
 6021 * Return code
 6022 *   0 - Successfully issued acc adisc response
 6023 *   1 - Failed to issue adisc acc response
 6024 **/
 6025int
 6026lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
 6027		       struct lpfc_nodelist *ndlp)
 6028{
 6029	struct lpfc_hba  *phba = vport->phba;
 6030	ADISC *ap;
 6031	IOCB_t *icmd, *oldcmd;
 6032	union lpfc_wqe128 *wqe;
 6033	struct lpfc_iocbq *elsiocb;
 6034	uint8_t *pcmd;
 6035	uint16_t cmdsize;
 6036	int rc;
 6037	u32 ulp_context;
 6038
 6039	cmdsize = sizeof(uint32_t) + sizeof(ADISC);
 6040	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
 6041				     ndlp->nlp_DID, ELS_CMD_ACC);
 6042	if (!elsiocb)
 6043		return 1;
 6044
 6045	if (phba->sli_rev == LPFC_SLI_REV4) {
 6046		wqe = &elsiocb->wqe;
 6047		/* XRI / rx_id */
 6048		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
 6049		       get_job_ulpcontext(phba, oldiocb));
 6050		ulp_context = get_job_ulpcontext(phba, elsiocb);
 6051		/* oxid */
 6052		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 6053		       get_job_rcvoxid(phba, oldiocb));
 6054	} else {
 6055		icmd = &elsiocb->iocb;
 6056		oldcmd = &oldiocb->iocb;
 6057		icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
 6058		ulp_context = elsiocb->iocb.ulpContext;
 6059		icmd->unsli3.rcvsli3.ox_id =
 6060			oldcmd->unsli3.rcvsli3.ox_id;
 6061	}
 6062
 6063	/* Xmit ADISC ACC response tag <ulpIoTag> */
 6064	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 6065			 "0130 Xmit ADISC ACC response iotag x%x xri: "
 6066			 "x%x, did x%x, nlp_flag x%lx, nlp_state x%x rpi x%x\n",
 6067			 elsiocb->iotag, ulp_context,
 6068			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
 6069			 ndlp->nlp_rpi);
 6070	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 6071
 6072	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
 6073	pcmd += sizeof(uint32_t);
 6074
 6075	ap = (ADISC *) (pcmd);
 6076	ap->hardAL_PA = phba->fc_pref_ALPA;
 6077	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
 6078	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
 6079	ap->DID = be32_to_cpu(vport->fc_myDID);
 6080
 6081	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 6082		      "Issue ACC ADISC: did:x%x flg:x%lx refcnt %d",
 6083		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
 6084
 6085	phba->fc_stat.elsXmitACC++;
 6086	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 6087	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 6088	if (!elsiocb->ndlp) {
 6089		lpfc_els_free_iocb(phba, elsiocb);
 6090		return 1;
 6091	}
 6092
 6093	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 6094	if (rc == IOCB_ERROR) {
 6095		lpfc_els_free_iocb(phba, elsiocb);
 6096		lpfc_nlp_put(ndlp);
 6097		return 1;
 6098	}
 6099
 6100	return 0;
 6101}
 6102
 6103/**
 6104 * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
 6105 * @vport: pointer to a virtual N_Port data structure.
 6106 * @oldiocb: pointer to the original lpfc command iocb data structure.
 6107 * @ndlp: pointer to a node-list data structure.
 6108 *
 6109 * This routine prepares and issues an Accept (ACC) response to Process
 6110 * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
 6111 * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
 6112 *
 6113 * Note that the ndlp reference count will be incremented by 1 for holding the
 6114 * ndlp and the reference to ndlp will be stored into the ndlp field of
 6115 * the IOCB for the completion callback function to the PRLI Accept response
 6116 * ELS IOCB command.
 6117 *
 6118 * Return code
 6119 *   0 - Successfully issued acc prli response
 6120 *   1 - Failed to issue acc prli response
 6121 **/
 6122int
 6123lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
 6124		      struct lpfc_nodelist *ndlp)
 6125{
 6126	struct lpfc_hba  *phba = vport->phba;
 6127	PRLI *npr;
 6128	struct lpfc_nvme_prli *npr_nvme;
 6129	lpfc_vpd_t *vpd;
 6130	IOCB_t *icmd;
 6131	IOCB_t *oldcmd;
 6132	union lpfc_wqe128 *wqe;
 6133	struct lpfc_iocbq *elsiocb;
 6134	uint8_t *pcmd;
 6135	uint16_t cmdsize;
 6136	uint32_t prli_fc4_req, *req_payload;
 6137	struct lpfc_dmabuf *req_buf;
 6138	int rc;
 6139	u32 elsrspcmd, ulp_context;
 6140
 6141	/* Need the incoming PRLI payload to determine if the ACC is for an
 6142	 * FC4 or NVME PRLI type.  The PRLI type is at word 1.
 6143	 */
 6144	req_buf = oldiocb->cmd_dmabuf;
 6145	req_payload = (((uint32_t *)req_buf->virt) + 1);
 6146
 6147	/* PRLI type payload is at byte 3 for FCP or NVME. */
 6148	prli_fc4_req = be32_to_cpu(*req_payload);
 6149	prli_fc4_req = (prli_fc4_req >> 24) & 0xff;
 6150	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 6151			 "6127 PRLI_ACC:  Req Type x%x, Word1 x%08x\n",
 6152			 prli_fc4_req, *((uint32_t *)req_payload));
 6153
 6154	if (prli_fc4_req == PRLI_FCP_TYPE) {
 6155		cmdsize = sizeof(uint32_t) + sizeof(PRLI);
 6156		elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
 6157	} else if (prli_fc4_req == PRLI_NVME_TYPE) {
 6158		cmdsize = sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli);
 6159		elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_NVMEPRLI & ~ELS_RSP_MASK));
 6160	} else {
 6161		return 1;
 6162	}
 6163
 6164	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
 6165				     ndlp->nlp_DID, elsrspcmd);
 6166	if (!elsiocb)
 6167		return 1;
 6168
 6169	if (phba->sli_rev == LPFC_SLI_REV4) {
 6170		wqe = &elsiocb->wqe;
 6171		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
 6172		       get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
 6173		ulp_context = get_job_ulpcontext(phba, elsiocb);
 6174		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 6175		       get_job_rcvoxid(phba, oldiocb));
 6176	} else {
 6177		icmd = &elsiocb->iocb;
 6178		oldcmd = &oldiocb->iocb;
 6179		icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
 6180		ulp_context = elsiocb->iocb.ulpContext;
 6181		icmd->unsli3.rcvsli3.ox_id =
 6182			oldcmd->unsli3.rcvsli3.ox_id;
 6183	}
 6184
 6185	/* Xmit PRLI ACC response tag <ulpIoTag> */
 6186	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 6187			 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
 6188			 "did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x\n",
 6189			 elsiocb->iotag, ulp_context,
 6190			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
 6191			 ndlp->nlp_rpi);
 6192	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 6193	memset(pcmd, 0, cmdsize);
 6194
 6195	*((uint32_t *)(pcmd)) = elsrspcmd;
 6196	pcmd += sizeof(uint32_t);
 6197
 6198	/* For PRLI, remainder of payload is PRLI parameter page */
 6199	vpd = &phba->vpd;
 6200
 6201	if (prli_fc4_req == PRLI_FCP_TYPE) {
 6202		/*
 6203		 * If the remote port is a target and our firmware version
 6204		 * is 3.20 or later, set the following bits for FC-TAPE
 6205		 * support.
 6206		 */
 6207		npr = (PRLI *) pcmd;
 6208		if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
 6209		    (vpd->rev.feaLevelHigh >= 0x02)) {
 6210			npr->ConfmComplAllowed = 1;
 6211			npr->Retry = 1;
 6212			npr->TaskRetryIdReq = 1;
 6213		}
 6214		npr->acceptRspCode = PRLI_REQ_EXECUTED;
 6215
 6216		/* Set image pair for complementary pairs only. */
 6217		if (ndlp->nlp_type & NLP_FCP_TARGET)
 6218			npr->estabImagePair = 1;
 6219		else
 6220			npr->estabImagePair = 0;
 6221		npr->readXferRdyDis = 1;
 6222		npr->ConfmComplAllowed = 1;
 6223		npr->prliType = PRLI_FCP_TYPE;
 6224		npr->initiatorFunc = 1;
 6225
 6226		/* Xmit PRLI ACC response tag <ulpIoTag> */
 6227		lpfc_printf_vlog(vport, KERN_INFO,
 6228				 LOG_ELS | LOG_NODE | LOG_DISCOVERY,
 6229				 "6014 FCP issue PRLI ACC imgpair %d "
 6230				 "retry %d task %d\n",
 6231				 npr->estabImagePair,
 6232				 npr->Retry, npr->TaskRetryIdReq);
 6233
 6234	} else if (prli_fc4_req == PRLI_NVME_TYPE) {
 6235		/* Respond with an NVME PRLI Type */
 6236		npr_nvme = (struct lpfc_nvme_prli *) pcmd;
 6237		bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
 6238		bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
 6239		bf_set(prli_acc_rsp_code, npr_nvme, PRLI_REQ_EXECUTED);
 6240		if (phba->nvmet_support) {
 6241			bf_set(prli_tgt, npr_nvme, 1);
 6242			bf_set(prli_disc, npr_nvme, 1);
 6243			if (phba->cfg_nvme_enable_fb) {
 6244				bf_set(prli_fba, npr_nvme, 1);
 6245
 6246				/* TBD.  Target mode needs to post buffers
 6247				 * that support the configured first burst
 6248				 * byte size.
 6249				 */
 6250				bf_set(prli_fb_sz, npr_nvme,
 6251				       phba->cfg_nvmet_fb_size);
 6252			}
 6253		} else {
 6254			bf_set(prli_init, npr_nvme, 1);
 6255		}
 6256
 6257		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
 6258				 "6015 NVME issue PRLI ACC word1 x%08x "
 6259				 "word4 x%08x word5 x%08x flag x%lx, "
 6260				 "fcp_info x%x nlp_type x%x\n",
 6261				 npr_nvme->word1, npr_nvme->word4,
 6262				 npr_nvme->word5, ndlp->nlp_flag,
 6263				 ndlp->nlp_fcp_info, ndlp->nlp_type);
 6264		npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
 6265		npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
 6266		npr_nvme->word5 = cpu_to_be32(npr_nvme->word5);
 6267	} else
 6268		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 6269				 "6128 Unknown FC_TYPE x%x x%x ndlp x%06x\n",
 6270				 prli_fc4_req, ndlp->nlp_fc4_type,
 6271				 ndlp->nlp_DID);
 6272
 6273	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 6274		      "Issue ACC PRLI:  did:x%x flg:x%lx",
 6275		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
 6276
 6277	phba->fc_stat.elsXmitACC++;
 6278	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 6279	elsiocb->ndlp =  lpfc_nlp_get(ndlp);
 6280	if (!elsiocb->ndlp) {
 6281		lpfc_els_free_iocb(phba, elsiocb);
 6282		return 1;
 6283	}
 6284
 6285	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 6286	if (rc == IOCB_ERROR) {
 6287		lpfc_els_free_iocb(phba, elsiocb);
 6288		lpfc_nlp_put(ndlp);
 6289		return 1;
 6290	}
 6291
 6292	return 0;
 6293}
 6294
 6295/**
 6296 * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
 6297 * @vport: pointer to a virtual N_Port data structure.
 6298 * @format: rnid command format.
 6299 * @oldiocb: pointer to the original lpfc command iocb data structure.
 6300 * @ndlp: pointer to a node-list data structure.
 6301 *
 6302 * This routine issues a Request Node Identification Data (RNID) Accept
 6303 * (ACC) response. It constructs the RNID ACC response command according to
 6304 * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
 6305 * issue the response.
 6306 *
 6307 * Note that the ndlp reference count will be incremented by 1 for holding the
 6308 * ndlp and the reference to ndlp will be stored into the ndlp field of
 6309 * the IOCB for the completion callback function.
 6310 *
 6311 * Return code
 6312 *   0 - Successfully issued acc rnid response
 6313 *   1 - Failed to issue acc rnid response
 6314 **/
 6315static int
 6316lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
 6317		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
 6318{
 6319	struct lpfc_hba  *phba = vport->phba;
 6320	RNID *rn;
 6321	IOCB_t *icmd, *oldcmd;
 6322	union lpfc_wqe128 *wqe;
 6323	struct lpfc_iocbq *elsiocb;
 6324	uint8_t *pcmd;
 6325	uint16_t cmdsize;
 6326	int rc;
 6327	u32 ulp_context;
 6328
 6329	cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
 6330					+ (2 * sizeof(struct lpfc_name));
 6331	if (format)
 6332		cmdsize += sizeof(RNID_TOP_DISC);
 6333
 6334	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
 6335				     ndlp->nlp_DID, ELS_CMD_ACC);
 6336	if (!elsiocb)
 6337		return 1;
 6338
 6339	if (phba->sli_rev == LPFC_SLI_REV4) {
 6340		wqe = &elsiocb->wqe;
 6341		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
 6342		       get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
 6343		ulp_context = get_job_ulpcontext(phba, elsiocb);
 6344		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 6345		       get_job_rcvoxid(phba, oldiocb));
 6346	} else {
 6347		icmd = &elsiocb->iocb;
 6348		oldcmd = &oldiocb->iocb;
 6349		icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
 6350		ulp_context = elsiocb->iocb.ulpContext;
 6351		icmd->unsli3.rcvsli3.ox_id =
 6352			oldcmd->unsli3.rcvsli3.ox_id;
 6353	}
 6354
 6355	/* Xmit RNID ACC response tag <ulpIoTag> */
 6356	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 6357			 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
 6358			 elsiocb->iotag, ulp_context);
 6359	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 6360	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
 6361	pcmd += sizeof(uint32_t);
 6362
 6363	memset(pcmd, 0, sizeof(RNID));
 6364	rn = (RNID *) (pcmd);
 6365	rn->Format = format;
 6366	rn->CommonLen = (2 * sizeof(struct lpfc_name));
 6367	memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
 6368	memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
 6369	switch (format) {
 6370	case 0:
 6371		rn->SpecificLen = 0;
 6372		break;
 6373	case RNID_TOPOLOGY_DISC:
 6374		rn->SpecificLen = sizeof(RNID_TOP_DISC);
 6375		memcpy(&rn->un.topologyDisc.portName,
 6376		       &vport->fc_portname, sizeof(struct lpfc_name));
 6377		rn->un.topologyDisc.unitType = RNID_HBA;
 6378		rn->un.topologyDisc.physPort = 0;
 6379		rn->un.topologyDisc.attachedNodes = 0;
 6380		break;
 6381	default:
 6382		rn->CommonLen = 0;
 6383		rn->SpecificLen = 0;
 6384		break;
 6385	}
 6386
 6387	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 6388		      "Issue ACC RNID:  did:x%x flg:x%lx refcnt %d",
 6389		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
 6390
 6391	phba->fc_stat.elsXmitACC++;
 6392	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 6393	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 6394	if (!elsiocb->ndlp) {
 6395		lpfc_els_free_iocb(phba, elsiocb);
 6396		return 1;
 6397	}
 6398
 6399	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 6400	if (rc == IOCB_ERROR) {
 6401		lpfc_els_free_iocb(phba, elsiocb);
 6402		lpfc_nlp_put(ndlp);
 6403		return 1;
 6404	}
 6405
 6406	return 0;
 6407}
 6408
 6409/**
 6410 * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
 6411 * @vport: pointer to a virtual N_Port data structure.
 6412 * @iocb: pointer to the lpfc command iocb data structure.
 6413 * @ndlp: pointer to a node-list data structure.
 6414 *
 6415 * Return
 6416 **/
 6417static void
 6418lpfc_els_clear_rrq(struct lpfc_vport *vport,
 6419		   struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
 6420{
 6421	struct lpfc_hba  *phba = vport->phba;
 6422	uint8_t *pcmd;
 6423	struct RRQ *rrq;
 6424	uint16_t rxid;
 6425	uint16_t xri;
 6426	struct lpfc_node_rrq *prrq;
 6427
 6428
 6429	pcmd = (uint8_t *)iocb->cmd_dmabuf->virt;
 6430	pcmd += sizeof(uint32_t);
 6431	rrq = (struct RRQ *)pcmd;
 6432	rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
 6433	rxid = bf_get(rrq_rxid, rrq);
 6434
 6435	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 6436			"2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
 6437			" x%x x%x\n",
 6438			be32_to_cpu(bf_get(rrq_did, rrq)),
 6439			bf_get(rrq_oxid, rrq),
 6440			rxid,
 6441			get_wqe_reqtag(iocb),
 6442			get_job_ulpcontext(phba, iocb));
 6443
 6444	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 6445		"Clear RRQ:  did:x%x flg:x%lx exchg:x%.08x",
 6446		ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
 6447	if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
 6448		xri = bf_get(rrq_oxid, rrq);
 6449	else
 6450		xri = rxid;
 6451	prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
 6452	if (prrq)
 6453		lpfc_clr_rrq_active(phba, xri, prrq);
 6454	return;
 6455}
 6456
 6457/**
 6458 * lpfc_els_rsp_echo_acc - Issue echo acc response
 6459 * @vport: pointer to a virtual N_Port data structure.
 6460 * @data: pointer to echo data to return in the accept.
 6461 * @oldiocb: pointer to the original lpfc command iocb data structure.
 6462 * @ndlp: pointer to a node-list data structure.
 6463 *
 6464 * Return code
 6465 *   0 - Successfully issued acc echo response
 6466 *   1 - Failed to issue acc echo response
 6467 **/
 6468static int
 6469lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
 6470		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
 6471{
 6472	struct lpfc_hba  *phba = vport->phba;
 6473	IOCB_t *icmd, *oldcmd;
 6474	union lpfc_wqe128 *wqe;
 6475	struct lpfc_iocbq *elsiocb;
 6476	uint8_t *pcmd;
 6477	uint16_t cmdsize;
 6478	int rc;
 6479	u32 ulp_context;
 6480
 6481	if (phba->sli_rev == LPFC_SLI_REV4)
 6482		cmdsize = oldiocb->wcqe_cmpl.total_data_placed;
 6483	else
 6484		cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
 6485
 6486	/* The accumulated length can exceed the BPL_SIZE.  For
 6487	 * now, use this as the limit
 6488	 */
 6489	if (cmdsize > LPFC_BPL_SIZE)
 6490		cmdsize = LPFC_BPL_SIZE;
 6491	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
 6492				     ndlp->nlp_DID, ELS_CMD_ACC);
 6493	if (!elsiocb)
 6494		return 1;
 6495
 6496	if (phba->sli_rev == LPFC_SLI_REV4) {
 6497		wqe = &elsiocb->wqe;
 6498		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
 6499		       get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
 6500		ulp_context = get_job_ulpcontext(phba, elsiocb);
 6501		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 6502		       get_job_rcvoxid(phba, oldiocb));
 6503	} else {
 6504		icmd = &elsiocb->iocb;
 6505		oldcmd = &oldiocb->iocb;
 6506		icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
 6507		ulp_context = elsiocb->iocb.ulpContext;
 6508		icmd->unsli3.rcvsli3.ox_id =
 6509			oldcmd->unsli3.rcvsli3.ox_id;
 6510	}
 6511
 6512	/* Xmit ECHO ACC response tag <ulpIoTag> */
 6513	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 6514			 "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
 6515			 elsiocb->iotag, ulp_context);
 6516	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 6517	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
 6518	pcmd += sizeof(uint32_t);
 6519	memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
 6520
 6521	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
 6522		      "Issue ACC ECHO:  did:x%x flg:x%lx refcnt %d",
 6523		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
 6524
 6525	phba->fc_stat.elsXmitACC++;
 6526	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 6527	elsiocb->ndlp =  lpfc_nlp_get(ndlp);
 6528	if (!elsiocb->ndlp) {
 6529		lpfc_els_free_iocb(phba, elsiocb);
 6530		return 1;
 6531	}
 6532
 6533	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 6534	if (rc == IOCB_ERROR) {
 6535		lpfc_els_free_iocb(phba, elsiocb);
 6536		lpfc_nlp_put(ndlp);
 6537		return 1;
 6538	}
 6539
 6540	return 0;
 6541}
 6542
 6543/**
 6544 * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
 6545 * @vport: pointer to a host virtual N_Port data structure.
 6546 *
 6547 * This routine issues Address Discover (ADISC) ELS commands to those
 6548 * N_Ports which are in node port recovery state and ADISC has not been issued
 6549 * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
 6550 * lpfc_issue_els_adisc() routine, the per @vport number of discover count
 6551 * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
 6552 * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
 6553 * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
 6554 * IOCBs quit for later pick up. On the other hand, after walking through
 6555 * all the ndlps with the @vport and there is none ADISC IOCB issued, the
 6556 * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
 6557 * no more ADISC need to be sent.
 6558 *
 6559 * Return code
 6560 *    The number of N_Ports with adisc issued.
 6561 **/
 6562int
 6563lpfc_els_disc_adisc(struct lpfc_vport *vport)
 6564{
 6565	struct lpfc_nodelist *ndlp, *next_ndlp;
 6566	int sentadisc = 0;
 6567
 6568	/* go thru NPR nodes and issue any remaining ELS ADISCs */
 6569	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
 6570
 6571		if (ndlp->nlp_state != NLP_STE_NPR_NODE ||
 6572		    !test_bit(NLP_NPR_ADISC, &ndlp->nlp_flag))
 6573			continue;
 6574
 6575		clear_bit(NLP_NPR_ADISC, &ndlp->nlp_flag);
 6576
 6577		if (!test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag)) {
 6578			/* This node was marked for ADISC but was not picked
 6579			 * for discovery. This is possible if the node was
 6580			 * missing in gidft response.
 6581			 *
 6582			 * At time of marking node for ADISC, we skipped unreg
 6583			 * from backend
 6584			 */
 6585			lpfc_nlp_unreg_node(vport, ndlp);
 6586			lpfc_unreg_rpi(vport, ndlp);
 6587			continue;
 6588		}
 6589
 6590		ndlp->nlp_prev_state = ndlp->nlp_state;
 6591		lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
 6592		lpfc_issue_els_adisc(vport, ndlp, 0);
 6593		sentadisc++;
 6594		vport->num_disc_nodes++;
 6595		if (vport->num_disc_nodes >=
 6596				vport->cfg_discovery_threads) {
 6597			set_bit(FC_NLP_MORE, &vport->fc_flag);
 6598			break;
 6599		}
 6600
 6601	}
 6602	if (sentadisc == 0)
 6603		clear_bit(FC_NLP_MORE, &vport->fc_flag);
 6604	return sentadisc;
 6605}
 6606
 6607/**
 6608 * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
 6609 * @vport: pointer to a host virtual N_Port data structure.
 6610 *
 6611 * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
 6612 * which are in node port recovery state, with a @vport. Each time an ELS
 6613 * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
 6614 * the per @vport number of discover count (num_disc_nodes) shall be
 6615 * incremented. If the num_disc_nodes reaches a pre-configured threshold
 6616 * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
 6617 * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
 6618 * later pick up. On the other hand, after walking through all the ndlps with
 6619 * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
 6620 * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
 6621 * PLOGI need to be sent.
 6622 *
 6623 * Return code
 6624 *   The number of N_Ports with plogi issued.
 6625 **/
 6626int
 6627lpfc_els_disc_plogi(struct lpfc_vport *vport)
 6628{
 6629	struct lpfc_nodelist *ndlp, *next_ndlp;
 6630	int sentplogi = 0;
 6631
 6632	/* go thru NPR nodes and issue any remaining ELS PLOGIs */
 6633	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
 6634		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
 6635		    test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag) &&
 6636		    !test_bit(NLP_DELAY_TMO, &ndlp->nlp_flag) &&
 6637		    !test_bit(NLP_NPR_ADISC, &ndlp->nlp_flag)) {
 6638			ndlp->nlp_prev_state = ndlp->nlp_state;
 6639			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
 6640			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
 6641			sentplogi++;
 6642			vport->num_disc_nodes++;
 6643			if (vport->num_disc_nodes >=
 6644					vport->cfg_discovery_threads) {
 6645				set_bit(FC_NLP_MORE, &vport->fc_flag);
 6646				break;
 6647			}
 6648		}
 6649	}
 6650
 6651	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 6652			 "6452 Discover PLOGI %d flag x%lx\n",
 6653			 sentplogi, vport->fc_flag);
 6654
 6655	if (sentplogi)
 6656		lpfc_set_disctmo(vport);
 6657	else
 6658		clear_bit(FC_NLP_MORE, &vport->fc_flag);
 6659	return sentplogi;
 6660}
 6661
 6662static uint32_t
 6663lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
 6664		uint32_t word0)
 6665{
 6666
 6667	desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
 6668	desc->payload.els_req = word0;
 6669	desc->length = cpu_to_be32(sizeof(desc->payload));
 6670
 6671	return sizeof(struct fc_rdp_link_service_desc);
 6672}
 6673
 6674static uint32_t
 6675lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
 6676		uint8_t *page_a0, uint8_t *page_a2)
 6677{
 6678	uint16_t wavelength;
 6679	uint16_t temperature;
 6680	uint16_t rx_power;
 6681	uint16_t tx_bias;
 6682	uint16_t tx_power;
 6683	uint16_t vcc;
 6684	uint16_t flag = 0;
 6685	struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
 6686	struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
 6687
 6688	desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
 6689
 6690	trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
 6691			&page_a0[SSF_TRANSCEIVER_CODE_B4];
 6692	trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
 6693			&page_a0[SSF_TRANSCEIVER_CODE_B5];
 6694
 6695	if ((trasn_code_byte4->fc_sw_laser) ||
 6696	    (trasn_code_byte5->fc_sw_laser_sl) ||
 6697	    (trasn_code_byte5->fc_sw_laser_sn)) {  /* check if its short WL */
 6698		flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
 6699	} else if (trasn_code_byte4->fc_lw_laser) {
 6700		wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
 6701			page_a0[SSF_WAVELENGTH_B0];
 6702		if (wavelength == SFP_WAVELENGTH_LC1310)
 6703			flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
 6704		if (wavelength == SFP_WAVELENGTH_LL1550)
 6705			flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
 6706	}
 6707	/* check if its SFP+ */
 6708	flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
 6709			SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
 6710					<< SFP_FLAG_CT_SHIFT;
 6711
 6712	/* check if its OPTICAL */
 6713	flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
 6714			SFP_FLAG_IS_OPTICAL_PORT : 0)
 6715					<< SFP_FLAG_IS_OPTICAL_SHIFT;
 6716
 6717	temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
 6718		page_a2[SFF_TEMPERATURE_B0]);
 6719	vcc = (page_a2[SFF_VCC_B1] << 8 |
 6720		page_a2[SFF_VCC_B0]);
 6721	tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
 6722		page_a2[SFF_TXPOWER_B0]);
 6723	tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
 6724		page_a2[SFF_TX_BIAS_CURRENT_B0]);
 6725	rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
 6726		page_a2[SFF_RXPOWER_B0]);
 6727	desc->sfp_info.temperature = cpu_to_be16(temperature);
 6728	desc->sfp_info.rx_power = cpu_to_be16(rx_power);
 6729	desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
 6730	desc->sfp_info.tx_power = cpu_to_be16(tx_power);
 6731	desc->sfp_info.vcc = cpu_to_be16(vcc);
 6732
 6733	desc->sfp_info.flags = cpu_to_be16(flag);
 6734	desc->length = cpu_to_be32(sizeof(desc->sfp_info));
 6735
 6736	return sizeof(struct fc_rdp_sfp_desc);
 6737}
 6738
 6739static uint32_t
 6740lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
 6741		READ_LNK_VAR *stat)
 6742{
 6743	uint32_t type;
 6744
 6745	desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
 6746
 6747	type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
 6748
 6749	desc->info.port_type = cpu_to_be32(type);
 6750
 6751	desc->info.link_status.link_failure_cnt =
 6752		cpu_to_be32(stat->linkFailureCnt);
 6753	desc->info.link_status.loss_of_synch_cnt =
 6754		cpu_to_be32(stat->lossSyncCnt);
 6755	desc->info.link_status.loss_of_signal_cnt =
 6756		cpu_to_be32(stat->lossSignalCnt);
 6757	desc->info.link_status.primitive_seq_proto_err =
 6758		cpu_to_be32(stat->primSeqErrCnt);
 6759	desc->info.link_status.invalid_trans_word =
 6760		cpu_to_be32(stat->invalidXmitWord);
 6761	desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
 6762
 6763	desc->length = cpu_to_be32(sizeof(desc->info));
 6764
 6765	return sizeof(struct fc_rdp_link_error_status_desc);
 6766}
 6767
 6768static uint32_t
 6769lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc *desc, READ_LNK_VAR *stat,
 6770		      struct lpfc_vport *vport)
 6771{
 6772	uint32_t bbCredit;
 6773
 6774	desc->tag = cpu_to_be32(RDP_BBC_DESC_TAG);
 6775
 6776	bbCredit = vport->fc_sparam.cmn.bbCreditLsb |
 6777			(vport->fc_sparam.cmn.bbCreditMsb << 8);
 6778	desc->bbc_info.port_bbc = cpu_to_be32(bbCredit);
 6779	if (vport->phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
 6780		bbCredit = vport->phba->fc_fabparam.cmn.bbCreditLsb |
 6781			(vport->phba->fc_fabparam.cmn.bbCreditMsb << 8);
 6782		desc->bbc_info.attached_port_bbc = cpu_to_be32(bbCredit);
 6783	} else {
 6784		desc->bbc_info.attached_port_bbc = 0;
 6785	}
 6786
 6787	desc->bbc_info.rtt = 0;
 6788	desc->length = cpu_to_be32(sizeof(desc->bbc_info));
 6789
 6790	return sizeof(struct fc_rdp_bbc_desc);
 6791}
 6792
 6793static uint32_t
 6794lpfc_rdp_res_oed_temp_desc(struct lpfc_hba *phba,
 6795			   struct fc_rdp_oed_sfp_desc *desc, uint8_t *page_a2)
 6796{
 6797	uint32_t flags = 0;
 6798
 6799	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
 6800
 6801	desc->oed_info.hi_alarm = page_a2[SSF_TEMP_HIGH_ALARM];
 6802	desc->oed_info.lo_alarm = page_a2[SSF_TEMP_LOW_ALARM];
 6803	desc->oed_info.hi_warning = page_a2[SSF_TEMP_HIGH_WARNING];
 6804	desc->oed_info.lo_warning = page_a2[SSF_TEMP_LOW_WARNING];
 6805
 6806	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
 6807		flags |= RDP_OET_HIGH_ALARM;
 6808	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
 6809		flags |= RDP_OET_LOW_ALARM;
 6810	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
 6811		flags |= RDP_OET_HIGH_WARNING;
 6812	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
 6813		flags |= RDP_OET_LOW_WARNING;
 6814
 6815	flags |= ((0xf & RDP_OED_TEMPERATURE) << RDP_OED_TYPE_SHIFT);
 6816	desc->oed_info.function_flags = cpu_to_be32(flags);
 6817	desc->length = cpu_to_be32(sizeof(desc->oed_info));
 6818	return sizeof(struct fc_rdp_oed_sfp_desc);
 6819}
 6820
 6821static uint32_t
 6822lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba *phba,
 6823			      struct fc_rdp_oed_sfp_desc *desc,
 6824			      uint8_t *page_a2)
 6825{
 6826	uint32_t flags = 0;
 6827
 6828	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
 6829
 6830	desc->oed_info.hi_alarm = page_a2[SSF_VOLTAGE_HIGH_ALARM];
 6831	desc->oed_info.lo_alarm = page_a2[SSF_VOLTAGE_LOW_ALARM];
 6832	desc->oed_info.hi_warning = page_a2[SSF_VOLTAGE_HIGH_WARNING];
 6833	desc->oed_info.lo_warning = page_a2[SSF_VOLTAGE_LOW_WARNING];
 6834
 6835	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
 6836		flags |= RDP_OET_HIGH_ALARM;
 6837	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_VOLTAGE)
 6838		flags |= RDP_OET_LOW_ALARM;
 6839	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
 6840		flags |= RDP_OET_HIGH_WARNING;
 6841	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_VOLTAGE)
 6842		flags |= RDP_OET_LOW_WARNING;
 6843
 6844	flags |= ((0xf & RDP_OED_VOLTAGE) << RDP_OED_TYPE_SHIFT);
 6845	desc->oed_info.function_flags = cpu_to_be32(flags);
 6846	desc->length = cpu_to_be32(sizeof(desc->oed_info));
 6847	return sizeof(struct fc_rdp_oed_sfp_desc);
 6848}
 6849
 6850static uint32_t
 6851lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba *phba,
 6852			     struct fc_rdp_oed_sfp_desc *desc,
 6853			     uint8_t *page_a2)
 6854{
 6855	uint32_t flags = 0;
 6856
 6857	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
 6858
 6859	desc->oed_info.hi_alarm = page_a2[SSF_BIAS_HIGH_ALARM];
 6860	desc->oed_info.lo_alarm = page_a2[SSF_BIAS_LOW_ALARM];
 6861	desc->oed_info.hi_warning = page_a2[SSF_BIAS_HIGH_WARNING];
 6862	desc->oed_info.lo_warning = page_a2[SSF_BIAS_LOW_WARNING];
 6863
 6864	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXBIAS)
 6865		flags |= RDP_OET_HIGH_ALARM;
 6866	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXBIAS)
 6867		flags |= RDP_OET_LOW_ALARM;
 6868	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXBIAS)
 6869		flags |= RDP_OET_HIGH_WARNING;
 6870	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXBIAS)
 6871		flags |= RDP_OET_LOW_WARNING;
 6872
 6873	flags |= ((0xf & RDP_OED_TXBIAS) << RDP_OED_TYPE_SHIFT);
 6874	desc->oed_info.function_flags = cpu_to_be32(flags);
 6875	desc->length = cpu_to_be32(sizeof(desc->oed_info));
 6876	return sizeof(struct fc_rdp_oed_sfp_desc);
 6877}
 6878
 6879static uint32_t
 6880lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba *phba,
 6881			      struct fc_rdp_oed_sfp_desc *desc,
 6882			      uint8_t *page_a2)
 6883{
 6884	uint32_t flags = 0;
 6885
 6886	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
 6887
 6888	desc->oed_info.hi_alarm = page_a2[SSF_TXPOWER_HIGH_ALARM];
 6889	desc->oed_info.lo_alarm = page_a2[SSF_TXPOWER_LOW_ALARM];
 6890	desc->oed_info.hi_warning = page_a2[SSF_TXPOWER_HIGH_WARNING];
 6891	desc->oed_info.lo_warning = page_a2[SSF_TXPOWER_LOW_WARNING];
 6892
 6893	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXPOWER)
 6894		flags |= RDP_OET_HIGH_ALARM;
 6895	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXPOWER)
 6896		flags |= RDP_OET_LOW_ALARM;
 6897	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXPOWER)
 6898		flags |= RDP_OET_HIGH_WARNING;
 6899	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXPOWER)
 6900		flags |= RDP_OET_LOW_WARNING;
 6901
 6902	flags |= ((0xf & RDP_OED_TXPOWER) << RDP_OED_TYPE_SHIFT);
 6903	desc->oed_info.function_flags = cpu_to_be32(flags);
 6904	desc->length = cpu_to_be32(sizeof(desc->oed_info));
 6905	return sizeof(struct fc_rdp_oed_sfp_desc);
 6906}
 6907
 6908
 6909static uint32_t
 6910lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba *phba,
 6911			      struct fc_rdp_oed_sfp_desc *desc,
 6912			      uint8_t *page_a2)
 6913{
 6914	uint32_t flags = 0;
 6915
 6916	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
 6917
 6918	desc->oed_info.hi_alarm = page_a2[SSF_RXPOWER_HIGH_ALARM];
 6919	desc->oed_info.lo_alarm = page_a2[SSF_RXPOWER_LOW_ALARM];
 6920	desc->oed_info.hi_warning = page_a2[SSF_RXPOWER_HIGH_WARNING];
 6921	desc->oed_info.lo_warning = page_a2[SSF_RXPOWER_LOW_WARNING];
 6922
 6923	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_RXPOWER)
 6924		flags |= RDP_OET_HIGH_ALARM;
 6925	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_RXPOWER)
 6926		flags |= RDP_OET_LOW_ALARM;
 6927	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_RXPOWER)
 6928		flags |= RDP_OET_HIGH_WARNING;
 6929	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_RXPOWER)
 6930		flags |= RDP_OET_LOW_WARNING;
 6931
 6932	flags |= ((0xf & RDP_OED_RXPOWER) << RDP_OED_TYPE_SHIFT);
 6933	desc->oed_info.function_flags = cpu_to_be32(flags);
 6934	desc->length = cpu_to_be32(sizeof(desc->oed_info));
 6935	return sizeof(struct fc_rdp_oed_sfp_desc);
 6936}
 6937
 6938static uint32_t
 6939lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc *desc,
 6940		      uint8_t *page_a0, struct lpfc_vport *vport)
 6941{
 6942	desc->tag = cpu_to_be32(RDP_OPD_DESC_TAG);
 6943	memcpy(desc->opd_info.vendor_name, &page_a0[SSF_VENDOR_NAME], 16);
 6944	memcpy(desc->opd_info.model_number, &page_a0[SSF_VENDOR_PN], 16);
 6945	memcpy(desc->opd_info.serial_number, &page_a0[SSF_VENDOR_SN], 16);
 6946	memcpy(desc->opd_info.revision, &page_a0[SSF_VENDOR_REV], 4);
 6947	memcpy(desc->opd_info.date, &page_a0[SSF_DATE_CODE], 8);
 6948	desc->length = cpu_to_be32(sizeof(desc->opd_info));
 6949	return sizeof(struct fc_rdp_opd_sfp_desc);
 6950}
 6951
 6952static uint32_t
 6953lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc *desc, READ_LNK_VAR *stat)
 6954{
 6955	if (bf_get(lpfc_read_link_stat_gec2, stat) == 0)
 6956		return 0;
 6957	desc->tag = cpu_to_be32(RDP_FEC_DESC_TAG);
 6958
 6959	desc->info.CorrectedBlocks =
 6960		cpu_to_be32(stat->fecCorrBlkCount);
 6961	desc->info.UncorrectableBlocks =
 6962		cpu_to_be32(stat->fecUncorrBlkCount);
 6963
 6964	desc->length = cpu_to_be32(sizeof(desc->info));
 6965
 6966	return sizeof(struct fc_fec_rdp_desc);
 6967}
 6968
 6969static uint32_t
 6970lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
 6971{
 6972	uint16_t rdp_cap = 0;
 6973	uint16_t rdp_speed;
 6974
 6975	desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
 6976
 6977	switch (phba->fc_linkspeed) {
 6978	case LPFC_LINK_SPEED_1GHZ:
 6979		rdp_speed = RDP_PS_1GB;
 6980		break;
 6981	case LPFC_LINK_SPEED_2GHZ:
 6982		rdp_speed = RDP_PS_2GB;
 6983		break;
 6984	case LPFC_LINK_SPEED_4GHZ:
 6985		rdp_speed = RDP_PS_4GB;
 6986		break;
 6987	case LPFC_LINK_SPEED_8GHZ:
 6988		rdp_speed = RDP_PS_8GB;
 6989		break;
 6990	case LPFC_LINK_SPEED_10GHZ:
 6991		rdp_speed = RDP_PS_10GB;
 6992		break;
 6993	case LPFC_LINK_SPEED_16GHZ:
 6994		rdp_speed = RDP_PS_16GB;
 6995		break;
 6996	case LPFC_LINK_SPEED_32GHZ:
 6997		rdp_speed = RDP_PS_32GB;
 6998		break;
 6999	case LPFC_LINK_SPEED_64GHZ:
 7000		rdp_speed = RDP_PS_64GB;
 7001		break;
 7002	case LPFC_LINK_SPEED_128GHZ:
 7003		rdp_speed = RDP_PS_128GB;
 7004		break;
 7005	case LPFC_LINK_SPEED_256GHZ:
 7006		rdp_speed = RDP_PS_256GB;
 7007		break;
 7008	default:
 7009		rdp_speed = RDP_PS_UNKNOWN;
 7010		break;
 7011	}
 7012
 7013	desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
 7014
 7015	if (phba->lmt & LMT_256Gb)
 7016		rdp_cap |= RDP_PS_256GB;
 7017	if (phba->lmt & LMT_128Gb)
 7018		rdp_cap |= RDP_PS_128GB;
 7019	if (phba->lmt & LMT_64Gb)
 7020		rdp_cap |= RDP_PS_64GB;
 7021	if (phba->lmt & LMT_32Gb)
 7022		rdp_cap |= RDP_PS_32GB;
 7023	if (phba->lmt & LMT_16Gb)
 7024		rdp_cap |= RDP_PS_16GB;
 7025	if (phba->lmt & LMT_10Gb)
 7026		rdp_cap |= RDP_PS_10GB;
 7027	if (phba->lmt & LMT_8Gb)
 7028		rdp_cap |= RDP_PS_8GB;
 7029	if (phba->lmt & LMT_4Gb)
 7030		rdp_cap |= RDP_PS_4GB;
 7031	if (phba->lmt & LMT_2Gb)
 7032		rdp_cap |= RDP_PS_2GB;
 7033	if (phba->lmt & LMT_1Gb)
 7034		rdp_cap |= RDP_PS_1GB;
 7035
 7036	if (rdp_cap == 0)
 7037		rdp_cap = RDP_CAP_UNKNOWN;
 7038	if (phba->cfg_link_speed != LPFC_USER_LINK_SPEED_AUTO)
 7039		rdp_cap |= RDP_CAP_USER_CONFIGURED;
 7040
 7041	desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
 7042	desc->length = cpu_to_be32(sizeof(desc->info));
 7043	return sizeof(struct fc_rdp_port_speed_desc);
 7044}
 7045
 7046static uint32_t
 7047lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
 7048		struct lpfc_vport *vport)
 7049{
 7050
 7051	desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
 7052
 7053	memcpy(desc->port_names.wwnn, &vport->fc_nodename,
 7054			sizeof(desc->port_names.wwnn));
 7055
 7056	memcpy(desc->port_names.wwpn, &vport->fc_portname,
 7057			sizeof(desc->port_names.wwpn));
 7058
 7059	desc->length = cpu_to_be32(sizeof(desc->port_names));
 7060	return sizeof(struct fc_rdp_port_name_desc);
 7061}
 7062
 7063static uint32_t
 7064lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
 7065		struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
 7066{
 7067
 7068	desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
 7069	if (test_bit(FC_FABRIC, &vport->fc_flag)) {
 7070		memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
 7071		       sizeof(desc->port_names.wwnn));
 7072
 7073		memcpy(desc->port_names.wwpn, &vport->fabric_portname,
 7074		       sizeof(desc->port_names.wwpn));
 7075	} else {  /* Point to Point */
 7076		memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
 7077		       sizeof(desc->port_names.wwnn));
 7078
 7079		memcpy(desc->port_names.wwpn, &ndlp->nlp_portname,
 7080		       sizeof(desc->port_names.wwpn));
 7081	}
 7082
 7083	desc->length = cpu_to_be32(sizeof(desc->port_names));
 7084	return sizeof(struct fc_rdp_port_name_desc);
 7085}
 7086
 7087static void
 7088lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
 7089		int status)
 7090{
 7091	struct lpfc_nodelist *ndlp = rdp_context->ndlp;
 7092	struct lpfc_vport *vport = ndlp->vport;
 7093	struct lpfc_iocbq *elsiocb;
 7094	struct ulp_bde64 *bpl;
 7095	IOCB_t *icmd;
 7096	union lpfc_wqe128 *wqe;
 7097	uint8_t *pcmd;
 7098	struct ls_rjt *stat;
 7099	struct fc_rdp_res_frame *rdp_res;
 7100	uint32_t cmdsize, len;
 7101	uint16_t *flag_ptr;
 7102	int rc;
 7103	u32 ulp_context;
 7104
 7105	if (status != SUCCESS)
 7106		goto error;
 7107
 7108	/* This will change once we know the true size of the RDP payload */
 7109	cmdsize = sizeof(struct fc_rdp_res_frame);
 7110
 7111	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
 7112				lpfc_max_els_tries, rdp_context->ndlp,
 7113				rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
 7114	if (!elsiocb)
 7115		goto free_rdp_context;
 7116
 7117	ulp_context = get_job_ulpcontext(phba, elsiocb);
 7118	if (phba->sli_rev == LPFC_SLI_REV4) {
 7119		wqe = &elsiocb->wqe;
 7120		/* ox-id of the frame */
 7121		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 7122		       rdp_context->ox_id);
 7123		bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
 7124		       rdp_context->rx_id);
 7125	} else {
 7126		icmd = &elsiocb->iocb;
 7127		icmd->ulpContext = rdp_context->rx_id;
 7128		icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
 7129	}
 7130
 7131	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 7132			"2171 Xmit RDP response tag x%x xri x%x, "
 7133			"did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x",
 7134			elsiocb->iotag, ulp_context,
 7135			ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
 7136			ndlp->nlp_rpi);
 7137	rdp_res = (struct fc_rdp_res_frame *)elsiocb->cmd_dmabuf->virt;
 7138	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 7139	memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
 7140	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
 7141
 7142	/* Update Alarm and Warning */
 7143	flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_ALARM_FLAGS);
 7144	phba->sfp_alarm |= *flag_ptr;
 7145	flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_WARNING_FLAGS);
 7146	phba->sfp_warning |= *flag_ptr;
 7147
 7148	/* For RDP payload */
 7149	len = 8;
 7150	len += lpfc_rdp_res_link_service((struct fc_rdp_link_service_desc *)
 7151					 (len + pcmd), ELS_CMD_RDP);
 7152
 7153	len += lpfc_rdp_res_sfp_desc((struct fc_rdp_sfp_desc *)(len + pcmd),
 7154			rdp_context->page_a0, rdp_context->page_a2);
 7155	len += lpfc_rdp_res_speed((struct fc_rdp_port_speed_desc *)(len + pcmd),
 7156				  phba);
 7157	len += lpfc_rdp_res_link_error((struct fc_rdp_link_error_status_desc *)
 7158				       (len + pcmd), &rdp_context->link_stat);
 7159	len += lpfc_rdp_res_diag_port_names((struct fc_rdp_port_name_desc *)
 7160					     (len + pcmd), vport);
 7161	len += lpfc_rdp_res_attach_port_names((struct fc_rdp_port_name_desc *)
 7162					(len + pcmd), vport, ndlp);
 7163	len += lpfc_rdp_res_fec_desc((struct fc_fec_rdp_desc *)(len + pcmd),
 7164			&rdp_context->link_stat);
 7165	len += lpfc_rdp_res_bbc_desc((struct fc_rdp_bbc_desc *)(len + pcmd),
 7166				     &rdp_context->link_stat, vport);
 7167	len += lpfc_rdp_res_oed_temp_desc(phba,
 7168				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
 7169				rdp_context->page_a2);
 7170	len += lpfc_rdp_res_oed_voltage_desc(phba,
 7171				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
 7172				rdp_context->page_a2);
 7173	len += lpfc_rdp_res_oed_txbias_desc(phba,
 7174				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
 7175				rdp_context->page_a2);
 7176	len += lpfc_rdp_res_oed_txpower_desc(phba,
 7177				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
 7178				rdp_context->page_a2);
 7179	len += lpfc_rdp_res_oed_rxpower_desc(phba,
 7180				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
 7181				rdp_context->page_a2);
 7182	len += lpfc_rdp_res_opd_desc((struct fc_rdp_opd_sfp_desc *)(len + pcmd),
 7183				     rdp_context->page_a0, vport);
 7184
 7185	rdp_res->length = cpu_to_be32(len - 8);
 7186	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 7187
 7188	/* Now that we know the true size of the payload, update the BPL */
 7189	bpl = (struct ulp_bde64 *)elsiocb->bpl_dmabuf->virt;
 7190	bpl->tus.f.bdeSize = len;
 7191	bpl->tus.f.bdeFlags = 0;
 7192	bpl->tus.w = le32_to_cpu(bpl->tus.w);
 7193
 7194	phba->fc_stat.elsXmitACC++;
 7195	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 7196	if (!elsiocb->ndlp) {
 7197		lpfc_els_free_iocb(phba, elsiocb);
 7198		goto free_rdp_context;
 7199	}
 7200
 7201	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 7202	if (rc == IOCB_ERROR) {
 7203		lpfc_els_free_iocb(phba, elsiocb);
 7204		lpfc_nlp_put(ndlp);
 7205	}
 7206
 7207	goto free_rdp_context;
 7208
 7209error:
 7210	cmdsize = 2 * sizeof(uint32_t);
 7211	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
 7212			ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
 7213	if (!elsiocb)
 7214		goto free_rdp_context;
 7215
 7216	if (phba->sli_rev == LPFC_SLI_REV4) {
 7217		wqe = &elsiocb->wqe;
 7218		/* ox-id of the frame */
 7219		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 7220		       rdp_context->ox_id);
 7221		bf_set(wqe_ctxt_tag,
 7222		       &wqe->xmit_els_rsp.wqe_com,
 7223		       rdp_context->rx_id);
 7224	} else {
 7225		icmd = &elsiocb->iocb;
 7226		icmd->ulpContext = rdp_context->rx_id;
 7227		icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
 7228	}
 7229
 7230	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 7231
 7232	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
 7233	stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
 7234	stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
 7235
 7236	phba->fc_stat.elsXmitLSRJT++;
 7237	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 7238	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 7239	if (!elsiocb->ndlp) {
 7240		lpfc_els_free_iocb(phba, elsiocb);
 7241		goto free_rdp_context;
 7242	}
 7243
 7244	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 7245	if (rc == IOCB_ERROR) {
 7246		lpfc_els_free_iocb(phba, elsiocb);
 7247		lpfc_nlp_put(ndlp);
 7248	}
 7249
 7250free_rdp_context:
 7251	/* This reference put is for the original unsolicited RDP. If the
 7252	 * prep failed, there is no reference to remove.
 7253	 */
 7254	lpfc_nlp_put(ndlp);
 7255	kfree(rdp_context);
 7256}
 7257
 7258static int
 7259lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
 7260{
 7261	LPFC_MBOXQ_t *mbox = NULL;
 7262	int rc;
 7263
 7264	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 7265	if (!mbox) {
 7266		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
 7267				"7105 failed to allocate mailbox memory");
 7268		return 1;
 7269	}
 7270
 7271	if (lpfc_sli4_dump_page_a0(phba, mbox))
 7272		goto rdp_fail;
 7273	mbox->vport = rdp_context->ndlp->vport;
 7274	mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
 7275	mbox->ctx_u.rdp = rdp_context;
 7276	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 7277	if (rc == MBX_NOT_FINISHED) {
 7278		lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
 7279		return 1;
 7280	}
 7281
 7282	return 0;
 7283
 7284rdp_fail:
 7285	mempool_free(mbox, phba->mbox_mem_pool);
 7286	return 1;
 7287}
 7288
 7289int lpfc_get_sfp_info_wait(struct lpfc_hba *phba,
 7290			   struct lpfc_rdp_context *rdp_context)
 7291{
 7292	LPFC_MBOXQ_t *mbox = NULL;
 7293	int rc;
 7294	struct lpfc_dmabuf *mp;
 7295	struct lpfc_dmabuf *mpsave;
 7296	void *virt;
 7297	MAILBOX_t *mb;
 7298
 7299	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 7300	if (!mbox) {
 7301		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
 7302				"7205 failed to allocate mailbox memory");
 7303		return 1;
 7304	}
 7305
 7306	if (lpfc_sli4_dump_page_a0(phba, mbox))
 7307		goto sfp_fail;
 7308	mp = mbox->ctx_buf;
 7309	mpsave = mp;
 7310	virt = mp->virt;
 7311	if (phba->sli_rev < LPFC_SLI_REV4) {
 7312		mb = &mbox->u.mb;
 7313		mb->un.varDmp.cv = 1;
 7314		mb->un.varDmp.co = 1;
 7315		mb->un.varWords[2] = 0;
 7316		mb->un.varWords[3] = DMP_SFF_PAGE_A0_SIZE / 4;
 7317		mb->un.varWords[4] = 0;
 7318		mb->un.varWords[5] = 0;
 7319		mb->un.varWords[6] = 0;
 7320		mb->un.varWords[7] = 0;
 7321		mb->un.varWords[8] = 0;
 7322		mb->un.varWords[9] = 0;
 7323		mb->un.varWords[10] = 0;
 7324		mbox->in_ext_byte_len = DMP_SFF_PAGE_A0_SIZE;
 7325		mbox->out_ext_byte_len = DMP_SFF_PAGE_A0_SIZE;
 7326		mbox->mbox_offset_word = 5;
 7327		mbox->ext_buf = virt;
 7328	} else {
 7329		bf_set(lpfc_mbx_memory_dump_type3_length,
 7330		       &mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A0_SIZE);
 7331		mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
 7332		mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
 7333	}
 7334	mbox->vport = phba->pport;
 7335	rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_SLI4_CONFIG_TMO);
 7336	if (rc == MBX_NOT_FINISHED) {
 7337		rc = 1;
 7338		goto error;
 7339	}
 7340	if (rc == MBX_TIMEOUT)
 7341		goto error;
 7342	if (phba->sli_rev == LPFC_SLI_REV4)
 7343		mp = mbox->ctx_buf;
 7344	else
 7345		mp = mpsave;
 7346
 7347	if (bf_get(lpfc_mqe_status, &mbox->u.mqe)) {
 7348		rc = 1;
 7349		goto error;
 7350	}
 7351
 7352	lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a0,
 7353			     DMP_SFF_PAGE_A0_SIZE);
 7354
 7355	memset(mbox, 0, sizeof(*mbox));
 7356	memset(mp->virt, 0, DMP_SFF_PAGE_A2_SIZE);
 7357	INIT_LIST_HEAD(&mp->list);
 7358
 7359	/* save address for completion */
 7360	mbox->ctx_buf = mp;
 7361	mbox->vport = phba->pport;
 7362
 7363	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_DUMP_MEMORY);
 7364	bf_set(lpfc_mbx_memory_dump_type3_type,
 7365	       &mbox->u.mqe.un.mem_dump_type3, DMP_LMSD);
 7366	bf_set(lpfc_mbx_memory_dump_type3_link,
 7367	       &mbox->u.mqe.un.mem_dump_type3, phba->sli4_hba.physical_port);
 7368	bf_set(lpfc_mbx_memory_dump_type3_page_no,
 7369	       &mbox->u.mqe.un.mem_dump_type3, DMP_PAGE_A2);
 7370	if (phba->sli_rev < LPFC_SLI_REV4) {
 7371		mb = &mbox->u.mb;
 7372		mb->un.varDmp.cv = 1;
 7373		mb->un.varDmp.co = 1;
 7374		mb->un.varWords[2] = 0;
 7375		mb->un.varWords[3] = DMP_SFF_PAGE_A2_SIZE / 4;
 7376		mb->un.varWords[4] = 0;
 7377		mb->un.varWords[5] = 0;
 7378		mb->un.varWords[6] = 0;
 7379		mb->un.varWords[7] = 0;
 7380		mb->un.varWords[8] = 0;
 7381		mb->un.varWords[9] = 0;
 7382		mb->un.varWords[10] = 0;
 7383		mbox->in_ext_byte_len = DMP_SFF_PAGE_A2_SIZE;
 7384		mbox->out_ext_byte_len = DMP_SFF_PAGE_A2_SIZE;
 7385		mbox->mbox_offset_word = 5;
 7386		mbox->ext_buf = virt;
 7387	} else {
 7388		bf_set(lpfc_mbx_memory_dump_type3_length,
 7389		       &mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A2_SIZE);
 7390		mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
 7391		mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
 7392	}
 7393
 7394	rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_SLI4_CONFIG_TMO);
 7395
 7396	if (rc == MBX_TIMEOUT)
 7397		goto error;
 7398	if (bf_get(lpfc_mqe_status, &mbox->u.mqe)) {
 7399		rc = 1;
 7400		goto error;
 7401	}
 7402	rc = 0;
 7403
 7404	lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a2,
 7405			     DMP_SFF_PAGE_A2_SIZE);
 7406
 7407error:
 7408	if (mbox->mbox_flag & LPFC_MBX_WAKE) {
 7409		mbox->ctx_buf = mpsave;
 7410		lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
 7411	}
 7412
 7413	return rc;
 7414
 7415sfp_fail:
 7416	mempool_free(mbox, phba->mbox_mem_pool);
 7417	return 1;
 7418}
 7419
 7420/*
 7421 * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
 7422 * @vport: pointer to a host virtual N_Port data structure.
 7423 * @cmdiocb: pointer to lpfc command iocb data structure.
 7424 * @ndlp: pointer to a node-list data structure.
 7425 *
 7426 * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
 7427 * IOCB. First, the payload of the unsolicited RDP is checked.
 7428 * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
 7429 * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
 7430 * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
 7431 * gather all data and send RDP response.
 7432 *
 7433 * Return code
 7434 *   0 - Sent the acc response
 7435 *   1 - Sent the reject response.
 7436 */
 7437static int
 7438lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 7439		struct lpfc_nodelist *ndlp)
 7440{
 7441	struct lpfc_hba *phba = vport->phba;
 7442	struct lpfc_dmabuf *pcmd;
 7443	uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
 7444	struct fc_rdp_req_frame *rdp_req;
 7445	struct lpfc_rdp_context *rdp_context;
 7446	union lpfc_wqe128 *cmd = NULL;
 7447	struct ls_rjt stat;
 7448
 7449	if (phba->sli_rev < LPFC_SLI_REV4 ||
 7450	    bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
 7451						LPFC_SLI_INTF_IF_TYPE_2) {
 7452		rjt_err = LSRJT_UNABLE_TPC;
 7453		rjt_expl = LSEXP_REQ_UNSUPPORTED;
 7454		goto error;
 7455	}
 7456
 7457	if (phba->sli_rev < LPFC_SLI_REV4 ||
 7458	    test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
 7459		rjt_err = LSRJT_UNABLE_TPC;
 7460		rjt_expl = LSEXP_REQ_UNSUPPORTED;
 7461		goto error;
 7462	}
 7463
 7464	pcmd = cmdiocb->cmd_dmabuf;
 7465	rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
 7466
 7467	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 7468			 "2422 ELS RDP Request "
 7469			 "dec len %d tag x%x port_id %d len %d\n",
 7470			 be32_to_cpu(rdp_req->rdp_des_length),
 7471			 be32_to_cpu(rdp_req->nport_id_desc.tag),
 7472			 be32_to_cpu(rdp_req->nport_id_desc.nport_id),
 7473			 be32_to_cpu(rdp_req->nport_id_desc.length));
 7474
 7475	if (sizeof(struct fc_rdp_nport_desc) !=
 7476			be32_to_cpu(rdp_req->rdp_des_length))
 7477		goto rjt_logerr;
 7478	if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
 7479		goto rjt_logerr;
 7480	if (RDP_NPORT_ID_SIZE !=
 7481			be32_to_cpu(rdp_req->nport_id_desc.length))
 7482		goto rjt_logerr;
 7483	rdp_context = kzalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
 7484	if (!rdp_context) {
 7485		rjt_err = LSRJT_UNABLE_TPC;
 7486		goto error;
 7487	}
 7488
 7489	cmd = &cmdiocb->wqe;
 7490	rdp_context->ndlp = lpfc_nlp_get(ndlp);
 7491	if (!rdp_context->ndlp) {
 7492		kfree(rdp_context);
 7493		rjt_err = LSRJT_UNABLE_TPC;
 7494		goto error;
 7495	}
 7496	rdp_context->ox_id = bf_get(wqe_rcvoxid,
 7497				    &cmd->xmit_els_rsp.wqe_com);
 7498	rdp_context->rx_id = bf_get(wqe_ctxt_tag,
 7499				    &cmd->xmit_els_rsp.wqe_com);
 7500	rdp_context->cmpl = lpfc_els_rdp_cmpl;
 7501	if (lpfc_get_rdp_info(phba, rdp_context)) {
 7502		lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
 7503				 "2423 Unable to send mailbox");
 7504		kfree(rdp_context);
 7505		rjt_err = LSRJT_UNABLE_TPC;
 7506		lpfc_nlp_put(ndlp);
 7507		goto error;
 7508	}
 7509
 7510	return 0;
 7511
 7512rjt_logerr:
 7513	rjt_err = LSRJT_LOGICAL_ERR;
 7514
 7515error:
 7516	memset(&stat, 0, sizeof(stat));
 7517	stat.un.b.lsRjtRsnCode = rjt_err;
 7518	stat.un.b.lsRjtRsnCodeExp = rjt_expl;
 7519	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
 7520	return 1;
 7521}
 7522
 7523
 7524static void
 7525lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 7526{
 7527	MAILBOX_t *mb;
 7528	IOCB_t *icmd;
 7529	union lpfc_wqe128 *wqe;
 7530	uint8_t *pcmd;
 7531	struct lpfc_iocbq *elsiocb;
 7532	struct lpfc_nodelist *ndlp;
 7533	struct ls_rjt *stat;
 7534	union lpfc_sli4_cfg_shdr *shdr;
 7535	struct lpfc_lcb_context *lcb_context;
 7536	struct fc_lcb_res_frame *lcb_res;
 7537	uint32_t cmdsize, shdr_status, shdr_add_status;
 7538	int rc;
 7539
 7540	mb = &pmb->u.mb;
 7541	lcb_context = pmb->ctx_u.lcb;
 7542	ndlp = lcb_context->ndlp;
 7543	memset(&pmb->ctx_u, 0, sizeof(pmb->ctx_u));
 7544	pmb->ctx_buf = NULL;
 7545
 7546	shdr = (union lpfc_sli4_cfg_shdr *)
 7547			&pmb->u.mqe.un.beacon_config.header.cfg_shdr;
 7548	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
 7549	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
 7550
 7551	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
 7552				"0194 SET_BEACON_CONFIG mailbox "
 7553				"completed with status x%x add_status x%x,"
 7554				" mbx status x%x\n",
 7555				shdr_status, shdr_add_status, mb->mbxStatus);
 7556
 7557	if ((mb->mbxStatus != MBX_SUCCESS) || shdr_status ||
 7558	    (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE) ||
 7559	    (shdr_add_status == ADD_STATUS_INVALID_REQUEST)) {
 7560		mempool_free(pmb, phba->mbox_mem_pool);
 7561		goto error;
 7562	}
 7563
 7564	mempool_free(pmb, phba->mbox_mem_pool);
 7565	cmdsize = sizeof(struct fc_lcb_res_frame);
 7566	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
 7567			lpfc_max_els_tries, ndlp,
 7568			ndlp->nlp_DID, ELS_CMD_ACC);
 7569
 7570	/* Decrement the ndlp reference count from previous mbox command */
 7571	lpfc_nlp_put(ndlp);
 7572
 7573	if (!elsiocb)
 7574		goto free_lcb_context;
 7575
 7576	lcb_res = (struct fc_lcb_res_frame *)elsiocb->cmd_dmabuf->virt;
 7577
 7578	memset(lcb_res, 0, sizeof(struct fc_lcb_res_frame));
 7579
 7580	if (phba->sli_rev == LPFC_SLI_REV4) {
 7581		wqe = &elsiocb->wqe;
 7582		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, lcb_context->rx_id);
 7583		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 7584		       lcb_context->ox_id);
 7585	} else {
 7586		icmd = &elsiocb->iocb;
 7587		icmd->ulpContext = lcb_context->rx_id;
 7588		icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
 7589	}
 7590
 7591	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 7592	*((uint32_t *)(pcmd)) = ELS_CMD_ACC;
 7593	lcb_res->lcb_sub_command = lcb_context->sub_command;
 7594	lcb_res->lcb_type = lcb_context->type;
 7595	lcb_res->capability = lcb_context->capability;
 7596	lcb_res->lcb_frequency = lcb_context->frequency;
 7597	lcb_res->lcb_duration = lcb_context->duration;
 7598	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 7599	phba->fc_stat.elsXmitACC++;
 7600
 7601	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 7602	if (!elsiocb->ndlp) {
 7603		lpfc_els_free_iocb(phba, elsiocb);
 7604		goto out;
 7605	}
 7606
 7607	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 7608	if (rc == IOCB_ERROR) {
 7609		lpfc_els_free_iocb(phba, elsiocb);
 7610		lpfc_nlp_put(ndlp);
 7611	}
 7612 out:
 7613	kfree(lcb_context);
 7614	return;
 7615
 7616error:
 7617	cmdsize = sizeof(struct fc_lcb_res_frame);
 7618	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
 7619				     lpfc_max_els_tries, ndlp,
 7620				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
 7621	lpfc_nlp_put(ndlp);
 7622	if (!elsiocb)
 7623		goto free_lcb_context;
 7624
 7625	if (phba->sli_rev == LPFC_SLI_REV4) {
 7626		wqe = &elsiocb->wqe;
 7627		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, lcb_context->rx_id);
 7628		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 7629		       lcb_context->ox_id);
 7630	} else {
 7631		icmd = &elsiocb->iocb;
 7632		icmd->ulpContext = lcb_context->rx_id;
 7633		icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
 7634	}
 7635
 7636	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 7637
 7638	*((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
 7639	stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
 7640	stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
 7641
 7642	if (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE)
 7643		stat->un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
 7644
 7645	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 7646	phba->fc_stat.elsXmitLSRJT++;
 7647	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 7648	if (!elsiocb->ndlp) {
 7649		lpfc_els_free_iocb(phba, elsiocb);
 7650		goto free_lcb_context;
 7651	}
 7652
 7653	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 7654	if (rc == IOCB_ERROR) {
 7655		lpfc_els_free_iocb(phba, elsiocb);
 7656		lpfc_nlp_put(ndlp);
 7657	}
 7658free_lcb_context:
 7659	kfree(lcb_context);
 7660}
 7661
 7662static int
 7663lpfc_sli4_set_beacon(struct lpfc_vport *vport,
 7664		     struct lpfc_lcb_context *lcb_context,
 7665		     uint32_t beacon_state)
 7666{
 7667	struct lpfc_hba *phba = vport->phba;
 7668	union lpfc_sli4_cfg_shdr *cfg_shdr;
 7669	LPFC_MBOXQ_t *mbox = NULL;
 7670	uint32_t len;
 7671	int rc;
 7672
 7673	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 7674	if (!mbox)
 7675		return 1;
 7676
 7677	cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
 7678	len = sizeof(struct lpfc_mbx_set_beacon_config) -
 7679		sizeof(struct lpfc_sli4_cfg_mhdr);
 7680	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
 7681			 LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
 7682			 LPFC_SLI4_MBX_EMBED);
 7683	mbox->ctx_u.lcb = lcb_context;
 7684	mbox->vport = phba->pport;
 7685	mbox->mbox_cmpl = lpfc_els_lcb_rsp;
 7686	bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
 7687	       phba->sli4_hba.physical_port);
 7688	bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
 7689	       beacon_state);
 7690	mbox->u.mqe.un.beacon_config.word5 = 0;		/* Reserved */
 7691
 7692	/*
 7693	 *	Check bv1s bit before issuing the mailbox
 7694	 *	if bv1s == 1, LCB V1 supported
 7695	 *	else, LCB V0 supported
 7696	 */
 7697
 7698	if (phba->sli4_hba.pc_sli4_params.bv1s) {
 7699		/* COMMON_SET_BEACON_CONFIG_V1 */
 7700		cfg_shdr->request.word9 = BEACON_VERSION_V1;
 7701		lcb_context->capability |= LCB_CAPABILITY_DURATION;
 7702		bf_set(lpfc_mbx_set_beacon_port_type,
 7703		       &mbox->u.mqe.un.beacon_config, 0);
 7704		bf_set(lpfc_mbx_set_beacon_duration_v1,
 7705		       &mbox->u.mqe.un.beacon_config,
 7706		       be16_to_cpu(lcb_context->duration));
 7707	} else {
 7708		/* COMMON_SET_BEACON_CONFIG_V0 */
 7709		if (be16_to_cpu(lcb_context->duration) != 0) {
 7710			mempool_free(mbox, phba->mbox_mem_pool);
 7711			return 1;
 7712		}
 7713		cfg_shdr->request.word9 = BEACON_VERSION_V0;
 7714		lcb_context->capability &=  ~(LCB_CAPABILITY_DURATION);
 7715		bf_set(lpfc_mbx_set_beacon_state,
 7716		       &mbox->u.mqe.un.beacon_config, beacon_state);
 7717		bf_set(lpfc_mbx_set_beacon_port_type,
 7718		       &mbox->u.mqe.un.beacon_config, 1);
 7719		bf_set(lpfc_mbx_set_beacon_duration,
 7720		       &mbox->u.mqe.un.beacon_config,
 7721		       be16_to_cpu(lcb_context->duration));
 7722	}
 7723
 7724	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 7725	if (rc == MBX_NOT_FINISHED) {
 7726		mempool_free(mbox, phba->mbox_mem_pool);
 7727		return 1;
 7728	}
 7729
 7730	return 0;
 7731}
 7732
 7733
 7734/**
 7735 * lpfc_els_rcv_lcb - Process an unsolicited LCB
 7736 * @vport: pointer to a host virtual N_Port data structure.
 7737 * @cmdiocb: pointer to lpfc command iocb data structure.
 7738 * @ndlp: pointer to a node-list data structure.
 7739 *
 7740 * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
 7741 * First, the payload of the unsolicited LCB is checked.
 7742 * Then based on Subcommand beacon will either turn on or off.
 7743 *
 7744 * Return code
 7745 * 0 - Sent the acc response
 7746 * 1 - Sent the reject response.
 7747 **/
 7748static int
 7749lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 7750		 struct lpfc_nodelist *ndlp)
 7751{
 7752	struct lpfc_hba *phba = vport->phba;
 7753	struct lpfc_dmabuf *pcmd;
 7754	uint8_t *lp;
 7755	struct fc_lcb_request_frame *beacon;
 7756	struct lpfc_lcb_context *lcb_context;
 7757	u8 state, rjt_err = 0;
 7758	struct ls_rjt stat;
 7759
 7760	pcmd = cmdiocb->cmd_dmabuf;
 7761	lp = (uint8_t *)pcmd->virt;
 7762	beacon = (struct fc_lcb_request_frame *)pcmd->virt;
 7763
 7764	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 7765			"0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
 7766			"type x%x frequency %x duration x%x\n",
 7767			lp[0], lp[1], lp[2],
 7768			beacon->lcb_command,
 7769			beacon->lcb_sub_command,
 7770			beacon->lcb_type,
 7771			beacon->lcb_frequency,
 7772			be16_to_cpu(beacon->lcb_duration));
 7773
 7774	if (beacon->lcb_sub_command != LPFC_LCB_ON &&
 7775	    beacon->lcb_sub_command != LPFC_LCB_OFF) {
 7776		rjt_err = LSRJT_CMD_UNSUPPORTED;
 7777		goto rjt;
 7778	}
 7779
 7780	if (phba->sli_rev < LPFC_SLI_REV4  ||
 7781	    test_bit(HBA_FCOE_MODE, &phba->hba_flag) ||
 7782	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
 7783	    LPFC_SLI_INTF_IF_TYPE_2)) {
 7784		rjt_err = LSRJT_CMD_UNSUPPORTED;
 7785		goto rjt;
 7786	}
 7787
 7788	lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
 7789	if (!lcb_context) {
 7790		rjt_err = LSRJT_UNABLE_TPC;
 7791		goto rjt;
 7792	}
 7793
 7794	state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
 7795	lcb_context->sub_command = beacon->lcb_sub_command;
 7796	lcb_context->capability	= 0;
 7797	lcb_context->type = beacon->lcb_type;
 7798	lcb_context->frequency = beacon->lcb_frequency;
 7799	lcb_context->duration = beacon->lcb_duration;
 7800	lcb_context->ox_id = get_job_rcvoxid(phba, cmdiocb);
 7801	lcb_context->rx_id = get_job_ulpcontext(phba, cmdiocb);
 7802	lcb_context->ndlp = lpfc_nlp_get(ndlp);
 7803	if (!lcb_context->ndlp) {
 7804		rjt_err = LSRJT_UNABLE_TPC;
 7805		goto rjt_free;
 7806	}
 7807
 7808	if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
 7809		lpfc_printf_vlog(ndlp->vport, KERN_ERR, LOG_TRACE_EVENT,
 7810				 "0193 failed to send mail box");
 7811		lpfc_nlp_put(ndlp);
 7812		rjt_err = LSRJT_UNABLE_TPC;
 7813		goto rjt_free;
 7814	}
 7815	return 0;
 7816
 7817rjt_free:
 7818	kfree(lcb_context);
 7819rjt:
 7820	memset(&stat, 0, sizeof(stat));
 7821	stat.un.b.lsRjtRsnCode = rjt_err;
 7822	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
 7823	return 1;
 7824}
 7825
 7826
 7827/**
 7828 * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
 7829 * @vport: pointer to a host virtual N_Port data structure.
 7830 *
 7831 * This routine cleans up any Registration State Change Notification
 7832 * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
 7833 * @vport together with the host_lock is used to prevent multiple thread
 7834 * trying to access the RSCN array on a same @vport at the same time.
 7835 **/
 7836void
 7837lpfc_els_flush_rscn(struct lpfc_vport *vport)
 7838{
 7839	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 7840	struct lpfc_hba  *phba = vport->phba;
 7841	int i;
 7842
 7843	spin_lock_irq(shost->host_lock);
 7844	if (vport->fc_rscn_flush) {
 7845		/* Another thread is walking fc_rscn_id_list on this vport */
 7846		spin_unlock_irq(shost->host_lock);
 7847		return;
 7848	}
 7849	/* Indicate we are walking lpfc_els_flush_rscn on this vport */
 7850	vport->fc_rscn_flush = 1;
 7851	spin_unlock_irq(shost->host_lock);
 7852
 7853	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
 7854		lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
 7855		vport->fc_rscn_id_list[i] = NULL;
 7856	}
 7857	clear_bit(FC_RSCN_MODE, &vport->fc_flag);
 7858	clear_bit(FC_RSCN_DISCOVERY, &vport->fc_flag);
 7859	spin_lock_irq(shost->host_lock);
 7860	vport->fc_rscn_id_cnt = 0;
 7861	spin_unlock_irq(shost->host_lock);
 7862	lpfc_can_disctmo(vport);
 7863	/* Indicate we are done walking this fc_rscn_id_list */
 7864	vport->fc_rscn_flush = 0;
 7865}
 7866
 7867/**
 7868 * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
 7869 * @vport: pointer to a host virtual N_Port data structure.
 7870 * @did: remote destination port identifier.
 7871 *
 7872 * This routine checks whether there is any pending Registration State
 7873 * Configuration Notification (RSCN) to a @did on @vport.
 7874 *
 7875 * Return code
 7876 *   None zero - The @did matched with a pending rscn
 7877 *   0 - not able to match @did with a pending rscn
 7878 **/
 7879int
 7880lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
 7881{
 7882	D_ID ns_did;
 7883	D_ID rscn_did;
 7884	uint32_t *lp;
 7885	uint32_t payload_len, i;
 7886	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 7887
 7888	ns_did.un.word = did;
 7889
 7890	/* Never match fabric nodes for RSCNs */
 7891	if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
 7892		return 0;
 7893
 7894	/* If we are doing a FULL RSCN rediscovery, match everything */
 7895	if (test_bit(FC_RSCN_DISCOVERY, &vport->fc_flag))
 7896		return did;
 7897
 7898	spin_lock_irq(shost->host_lock);
 7899	if (vport->fc_rscn_flush) {
 7900		/* Another thread is walking fc_rscn_id_list on this vport */
 7901		spin_unlock_irq(shost->host_lock);
 7902		return 0;
 7903	}
 7904	/* Indicate we are walking fc_rscn_id_list on this vport */
 7905	vport->fc_rscn_flush = 1;
 7906	spin_unlock_irq(shost->host_lock);
 7907	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
 7908		lp = vport->fc_rscn_id_list[i]->virt;
 7909		payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
 7910		payload_len -= sizeof(uint32_t);	/* take off word 0 */
 7911		while (payload_len) {
 7912			rscn_did.un.word = be32_to_cpu(*lp++);
 7913			payload_len -= sizeof(uint32_t);
 7914			switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
 7915			case RSCN_ADDRESS_FORMAT_PORT:
 7916				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
 7917				    && (ns_did.un.b.area == rscn_did.un.b.area)
 7918				    && (ns_did.un.b.id == rscn_did.un.b.id))
 7919					goto return_did_out;
 7920				break;
 7921			case RSCN_ADDRESS_FORMAT_AREA:
 7922				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
 7923				    && (ns_did.un.b.area == rscn_did.un.b.area))
 7924					goto return_did_out;
 7925				break;
 7926			case RSCN_ADDRESS_FORMAT_DOMAIN:
 7927				if (ns_did.un.b.domain == rscn_did.un.b.domain)
 7928					goto return_did_out;
 7929				break;
 7930			case RSCN_ADDRESS_FORMAT_FABRIC:
 7931				goto return_did_out;
 7932			}
 7933		}
 7934	}
 7935	/* Indicate we are done with walking fc_rscn_id_list on this vport */
 7936	vport->fc_rscn_flush = 0;
 7937	return 0;
 7938return_did_out:
 7939	/* Indicate we are done with walking fc_rscn_id_list on this vport */
 7940	vport->fc_rscn_flush = 0;
 7941	return did;
 7942}
 7943
 7944/**
 7945 * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
 7946 * @vport: pointer to a host virtual N_Port data structure.
 7947 *
 7948 * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
 7949 * state machine for a @vport's nodes that are with pending RSCN (Registration
 7950 * State Change Notification).
 7951 *
 7952 * Return code
 7953 *   0 - Successful (currently alway return 0)
 7954 **/
 7955static int
 7956lpfc_rscn_recovery_check(struct lpfc_vport *vport)
 7957{
 7958	struct lpfc_nodelist *ndlp = NULL, *n;
 7959
 7960	/* Move all affected nodes by pending RSCNs to NPR state. */
 7961	list_for_each_entry_safe(ndlp, n, &vport->fc_nodes, nlp_listp) {
 7962		if (test_bit(FC_UNLOADING, &vport->load_flag)) {
 7963			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 7964					 "1000 %s Unloading set\n",
 7965					 __func__);
 7966			return 0;
 7967		}
 7968
 7969		if ((ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
 7970		    !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
 7971			continue;
 7972
 7973		/* NVME Target mode does not do RSCN Recovery. */
 7974		if (vport->phba->nvmet_support)
 7975			continue;
 7976
 7977		/* If we are in the process of doing discovery on this
 7978		 * NPort, let it continue on its own.
 7979		 */
 7980		switch (ndlp->nlp_state) {
 7981		case  NLP_STE_PLOGI_ISSUE:
 7982		case  NLP_STE_ADISC_ISSUE:
 7983		case  NLP_STE_REG_LOGIN_ISSUE:
 7984		case  NLP_STE_PRLI_ISSUE:
 7985		case  NLP_STE_LOGO_ISSUE:
 7986			continue;
 7987		}
 7988
 7989		lpfc_disc_state_machine(vport, ndlp, NULL,
 7990					NLP_EVT_DEVICE_RECOVERY);
 7991		lpfc_cancel_retry_delay_tmo(vport, ndlp);
 7992	}
 7993	return 0;
 7994}
 7995
 7996/**
 7997 * lpfc_send_rscn_event - Send an RSCN event to management application
 7998 * @vport: pointer to a host virtual N_Port data structure.
 7999 * @cmdiocb: pointer to lpfc command iocb data structure.
 8000 *
 8001 * lpfc_send_rscn_event sends an RSCN netlink event to management
 8002 * applications.
 8003 */
 8004static void
 8005lpfc_send_rscn_event(struct lpfc_vport *vport,
 8006		struct lpfc_iocbq *cmdiocb)
 8007{
 8008	struct lpfc_dmabuf *pcmd;
 8009	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 8010	uint32_t *payload_ptr;
 8011	uint32_t payload_len;
 8012	struct lpfc_rscn_event_header *rscn_event_data;
 8013
 8014	pcmd = cmdiocb->cmd_dmabuf;
 8015	payload_ptr = (uint32_t *) pcmd->virt;
 8016	payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
 8017
 8018	rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
 8019		payload_len, GFP_KERNEL);
 8020	if (!rscn_event_data) {
 8021		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 8022			"0147 Failed to allocate memory for RSCN event\n");
 8023		return;
 8024	}
 8025	rscn_event_data->event_type = FC_REG_RSCN_EVENT;
 8026	rscn_event_data->payload_length = payload_len;
 8027	memcpy(rscn_event_data->rscn_payload, payload_ptr,
 8028		payload_len);
 8029
 8030	fc_host_post_vendor_event(shost,
 8031		fc_get_event_number(),
 8032		sizeof(struct lpfc_rscn_event_header) + payload_len,
 8033		(char *)rscn_event_data,
 8034		LPFC_NL_VENDOR_ID);
 8035
 8036	kfree(rscn_event_data);
 8037}
 8038
 8039/**
 8040 * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
 8041 * @vport: pointer to a host virtual N_Port data structure.
 8042 * @cmdiocb: pointer to lpfc command iocb data structure.
 8043 * @ndlp: pointer to a node-list data structure.
 8044 *
 8045 * This routine processes an unsolicited RSCN (Registration State Change
 8046 * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
 8047 * to invoke fc_host_post_event() routine to the FC transport layer. If the
 8048 * discover state machine is about to begin discovery, it just accepts the
 8049 * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
 8050 * contains N_Port IDs for other vports on this HBA, it just accepts the
 8051 * RSCN and ignore processing it. If the state machine is in the recovery
 8052 * state, the fc_rscn_id_list of this @vport is walked and the
 8053 * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
 8054 * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
 8055 * routine is invoked to handle the RSCN event.
 8056 *
 8057 * Return code
 8058 *   0 - Just sent the acc response
 8059 *   1 - Sent the acc response and waited for name server completion
 8060 **/
 8061static int
 8062lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 8063		  struct lpfc_nodelist *ndlp)
 8064{
 8065	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 8066	struct lpfc_hba  *phba = vport->phba;
 8067	struct lpfc_dmabuf *pcmd;
 8068	uint32_t *lp, *datap;
 8069	uint32_t payload_len, length, nportid, *cmd;
 8070	int rscn_cnt;
 8071	int rscn_id = 0, hba_id = 0;
 8072	int i, tmo;
 8073
 8074	pcmd = cmdiocb->cmd_dmabuf;
 8075	lp = (uint32_t *) pcmd->virt;
 8076
 8077	payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
 8078	payload_len -= sizeof(uint32_t);	/* take off word 0 */
 8079	/* RSCN received */
 8080	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 8081			 "0214 RSCN received Data: x%lx x%x x%x x%x\n",
 8082			 vport->fc_flag, payload_len, *lp,
 8083			 vport->fc_rscn_id_cnt);
 8084
 8085	/* Send an RSCN event to the management application */
 8086	lpfc_send_rscn_event(vport, cmdiocb);
 8087
 8088	for (i = 0; i < payload_len/sizeof(uint32_t); i++)
 8089		fc_host_post_event(shost, fc_get_event_number(),
 8090			FCH_EVT_RSCN, lp[i]);
 8091
 8092	/* Check if RSCN is coming from a direct-connected remote NPort */
 8093	if (test_bit(FC_PT2PT, &vport->fc_flag)) {
 8094		/* If so, just ACC it, no other action needed for now */
 8095		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 8096				 "2024 pt2pt RSCN %08x Data: x%lx x%x\n",
 8097				 *lp, vport->fc_flag, payload_len);
 8098		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
 8099
 8100		/* Check to see if we need to NVME rescan this target
 8101		 * remoteport.
 8102		 */
 8103		if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
 8104		    ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
 8105			lpfc_nvme_rescan_port(vport, ndlp);
 8106		return 0;
 8107	}
 8108
 8109	/* If we are about to begin discovery, just ACC the RSCN.
 8110	 * Discovery processing will satisfy it.
 8111	 */
 8112	if (vport->port_state <= LPFC_NS_QRY) {
 8113		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
 8114			"RCV RSCN ignore: did:x%x/ste:x%x flg:x%lx",
 8115			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
 8116
 8117		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
 8118		return 0;
 8119	}
 8120
 8121	/* If this RSCN just contains NPortIDs for other vports on this HBA,
 8122	 * just ACC and ignore it.
 8123	 */
 8124	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
 8125		!(vport->cfg_peer_port_login)) {
 8126		i = payload_len;
 8127		datap = lp;
 8128		while (i > 0) {
 8129			nportid = *datap++;
 8130			nportid = ((be32_to_cpu(nportid)) & Mask_DID);
 8131			i -= sizeof(uint32_t);
 8132			rscn_id++;
 8133			if (lpfc_find_vport_by_did(phba, nportid))
 8134				hba_id++;
 8135		}
 8136		if (rscn_id == hba_id) {
 8137			/* ALL NPortIDs in RSCN are on HBA */
 8138			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 8139					 "0219 Ignore RSCN "
 8140					 "Data: x%lx x%x x%x x%x\n",
 8141					 vport->fc_flag, payload_len,
 8142					 *lp, vport->fc_rscn_id_cnt);
 8143			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
 8144				"RCV RSCN vport:  did:x%x/ste:x%x flg:x%lx",
 8145				ndlp->nlp_DID, vport->port_state,
 8146				ndlp->nlp_flag);
 8147
 8148			lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
 8149				ndlp, NULL);
 8150			/* Restart disctmo if its already running */
 8151			if (test_bit(FC_DISC_TMO, &vport->fc_flag)) {
 8152				tmo = ((phba->fc_ratov * 3) + 3);
 8153				mod_timer(&vport->fc_disctmo,
 8154					  jiffies + secs_to_jiffies(tmo));
 8155			}
 8156			return 0;
 8157		}
 8158	}
 8159
 8160	spin_lock_irq(shost->host_lock);
 8161	if (vport->fc_rscn_flush) {
 8162		/* Another thread is walking fc_rscn_id_list on this vport */
 8163		spin_unlock_irq(shost->host_lock);
 8164		set_bit(FC_RSCN_DISCOVERY, &vport->fc_flag);
 8165		/* Send back ACC */
 8166		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
 8167		return 0;
 8168	}
 8169	/* Indicate we are walking fc_rscn_id_list on this vport */
 8170	vport->fc_rscn_flush = 1;
 8171	spin_unlock_irq(shost->host_lock);
 8172	/* Get the array count after successfully have the token */
 8173	rscn_cnt = vport->fc_rscn_id_cnt;
 8174	/* If we are already processing an RSCN, save the received
 8175	 * RSCN payload buffer, cmdiocb->cmd_dmabuf to process later.
 8176	 */
 8177	if (test_bit(FC_RSCN_MODE, &vport->fc_flag) ||
 8178	    test_bit(FC_NDISC_ACTIVE, &vport->fc_flag)) {
 8179		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
 8180			"RCV RSCN defer:  did:x%x/ste:x%x flg:x%lx",
 8181			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
 8182
 8183		set_bit(FC_RSCN_DEFERRED, &vport->fc_flag);
 8184
 8185		/* Restart disctmo if its already running */
 8186		if (test_bit(FC_DISC_TMO, &vport->fc_flag)) {
 8187			tmo = ((phba->fc_ratov * 3) + 3);
 8188			mod_timer(&vport->fc_disctmo,
 8189				  jiffies + secs_to_jiffies(tmo));
 8190		}
 8191		if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
 8192		    !test_bit(FC_RSCN_DISCOVERY, &vport->fc_flag)) {
 8193			set_bit(FC_RSCN_MODE, &vport->fc_flag);
 8194			if (rscn_cnt) {
 8195				cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
 8196				length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
 8197			}
 8198			if ((rscn_cnt) &&
 8199			    (payload_len + length <= LPFC_BPL_SIZE)) {
 8200				*cmd &= ELS_CMD_MASK;
 8201				*cmd |= cpu_to_be32(payload_len + length);
 8202				memcpy(((uint8_t *)cmd) + length, lp,
 8203				       payload_len);
 8204			} else {
 8205				vport->fc_rscn_id_list[rscn_cnt] = pcmd;
 8206				vport->fc_rscn_id_cnt++;
 8207				/* If we zero, cmdiocb->cmd_dmabuf, the calling
 8208				 * routine will not try to free it.
 8209				 */
 8210				cmdiocb->cmd_dmabuf = NULL;
 8211			}
 8212			/* Deferred RSCN */
 8213			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 8214					 "0235 Deferred RSCN "
 8215					 "Data: x%x x%lx x%x\n",
 8216					 vport->fc_rscn_id_cnt, vport->fc_flag,
 8217					 vport->port_state);
 8218		} else {
 8219			set_bit(FC_RSCN_DISCOVERY, &vport->fc_flag);
 8220			/* ReDiscovery RSCN */
 8221			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 8222					 "0234 ReDiscovery RSCN "
 8223					 "Data: x%x x%lx x%x\n",
 8224					 vport->fc_rscn_id_cnt, vport->fc_flag,
 8225					 vport->port_state);
 8226		}
 8227		/* Indicate we are done walking fc_rscn_id_list on this vport */
 8228		vport->fc_rscn_flush = 0;
 8229		/* Send back ACC */
 8230		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
 8231		/* send RECOVERY event for ALL nodes that match RSCN payload */
 8232		lpfc_rscn_recovery_check(vport);
 8233		return 0;
 8234	}
 8235	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
 8236		"RCV RSCN:        did:x%x/ste:x%x flg:x%lx",
 8237		ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
 8238
 8239	set_bit(FC_RSCN_MODE, &vport->fc_flag);
 8240	vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
 8241	/* Indicate we are done walking fc_rscn_id_list on this vport */
 8242	vport->fc_rscn_flush = 0;
 8243	/*
 8244	 * If we zero, cmdiocb->cmd_dmabuf, the calling routine will
 8245	 * not try to free it.
 8246	 */
 8247	cmdiocb->cmd_dmabuf = NULL;
 8248	lpfc_set_disctmo(vport);
 8249	/* Send back ACC */
 8250	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
 8251	/* send RECOVERY event for ALL nodes that match RSCN payload */
 8252	lpfc_rscn_recovery_check(vport);
 8253	return lpfc_els_handle_rscn(vport);
 8254}
 8255
 8256/**
 8257 * lpfc_els_handle_rscn - Handle rscn for a vport
 8258 * @vport: pointer to a host virtual N_Port data structure.
 8259 *
 8260 * This routine handles the Registration State Configuration Notification
 8261 * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
 8262 * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
 8263 * if the ndlp to NameServer exists, a Common Transport (CT) command to the
 8264 * NameServer shall be issued. If CT command to the NameServer fails to be
 8265 * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
 8266 * RSCN activities with the @vport.
 8267 *
 8268 * Return code
 8269 *   0 - Cleaned up rscn on the @vport
 8270 *   1 - Wait for plogi to name server before proceed
 8271 **/
 8272int
 8273lpfc_els_handle_rscn(struct lpfc_vport *vport)
 8274{
 8275	struct lpfc_nodelist *ndlp;
 8276	struct lpfc_hba  *phba = vport->phba;
 8277
 8278	/* Ignore RSCN if the port is being torn down. */
 8279	if (test_bit(FC_UNLOADING, &vport->load_flag)) {
 8280		lpfc_els_flush_rscn(vport);
 8281		return 0;
 8282	}
 8283
 8284	/* Start timer for RSCN processing */
 8285	lpfc_set_disctmo(vport);
 8286
 8287	/* RSCN processed */
 8288	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 8289			 "0215 RSCN processed Data: x%lx x%x x%x x%x x%x x%x\n",
 8290			 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
 8291			 vport->port_state, vport->num_disc_nodes,
 8292			 vport->gidft_inp);
 8293
 8294	/* To process RSCN, first compare RSCN data with NameServer */
 8295	vport->fc_ns_retry = 0;
 8296	vport->num_disc_nodes = 0;
 8297
 8298	ndlp = lpfc_findnode_did(vport, NameServer_DID);
 8299	if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
 8300		/* Good ndlp, issue CT Request to NameServer.  Need to
 8301		 * know how many gidfts were issued.  If none, then just
 8302		 * flush the RSCN.  Otherwise, the outstanding requests
 8303		 * need to complete.
 8304		 */
 8305		if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT) {
 8306			if (lpfc_issue_gidft(vport) > 0)
 8307				return 1;
 8308		} else if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_PT) {
 8309			if (lpfc_issue_gidpt(vport) > 0)
 8310				return 1;
 8311		} else {
 8312			return 1;
 8313		}
 8314	} else {
 8315		/* Nameserver login in question.  Revalidate. */
 8316		if (ndlp) {
 8317			ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
 8318			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
 8319		} else {
 8320			ndlp = lpfc_nlp_init(vport, NameServer_DID);
 8321			if (!ndlp) {
 8322				lpfc_els_flush_rscn(vport);
 8323				return 0;
 8324			}
 8325			ndlp->nlp_prev_state = ndlp->nlp_state;
 8326			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
 8327		}
 8328		ndlp->nlp_type |= NLP_FABRIC;
 8329		lpfc_issue_els_plogi(vport, NameServer_DID, 0);
 8330		/* Wait for NameServer login cmpl before we can
 8331		 * continue
 8332		 */
 8333		return 1;
 8334	}
 8335
 8336	lpfc_els_flush_rscn(vport);
 8337	return 0;
 8338}
 8339
 8340/**
 8341 * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
 8342 * @vport: pointer to a host virtual N_Port data structure.
 8343 * @cmdiocb: pointer to lpfc command iocb data structure.
 8344 * @ndlp: pointer to a node-list data structure.
 8345 *
 8346 * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
 8347 * unsolicited event. An unsolicited FLOGI can be received in a point-to-
 8348 * point topology. As an unsolicited FLOGI should not be received in a loop
 8349 * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
 8350 * lpfc_check_sparm() routine is invoked to check the parameters in the
 8351 * unsolicited FLOGI. If parameters validation failed, the routine
 8352 * lpfc_els_rsp_reject() shall be called with reject reason code set to
 8353 * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
 8354 * FLOGI shall be compared with the Port WWN of the @vport to determine who
 8355 * will initiate PLOGI. The higher lexicographical value party shall has
 8356 * higher priority (as the winning port) and will initiate PLOGI and
 8357 * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
 8358 * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
 8359 * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
 8360 *
 8361 * Return code
 8362 *   0 - Successfully processed the unsolicited flogi
 8363 *   1 - Failed to process the unsolicited flogi
 8364 **/
 8365static int
 8366lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 8367		   struct lpfc_nodelist *ndlp)
 8368{
 8369	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 8370	struct lpfc_hba  *phba = vport->phba;
 8371	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
 8372	uint32_t *lp = (uint32_t *) pcmd->virt;
 8373	union lpfc_wqe128 *wqe = &cmdiocb->wqe;
 8374	struct serv_parm *sp;
 8375	LPFC_MBOXQ_t *mbox;
 8376	uint32_t cmd, did;
 8377	int rc;
 8378	unsigned long fc_flag = 0;
 8379	uint32_t port_state = 0;
 8380
 8381	/* Clear external loopback plug detected flag */
 8382	phba->link_flag &= ~LS_EXTERNAL_LOOPBACK;
 8383
 8384	cmd = *lp++;
 8385	sp = (struct serv_parm *) lp;
 8386
 8387	/* FLOGI received */
 8388
 8389	lpfc_set_disctmo(vport);
 8390
 8391	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
 8392		/* We should never receive a FLOGI in loop mode, ignore it */
 8393		did =  bf_get(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest);
 8394
 8395		/* An FLOGI ELS command <elsCmd> was received from DID <did> in
 8396		   Loop Mode */
 8397		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 8398				 "0113 An FLOGI ELS command x%x was "
 8399				 "received from DID x%x in Loop Mode\n",
 8400				 cmd, did);
 8401		return 1;
 8402	}
 8403
 8404	(void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
 8405
 8406	/*
 8407	 * If our portname is greater than the remote portname,
 8408	 * then we initiate Nport login.
 8409	 */
 8410
 8411	rc = memcmp(&vport->fc_portname, &sp->portName,
 8412		    sizeof(struct lpfc_name));
 8413
 8414	if (!rc) {
 8415		if (phba->sli_rev < LPFC_SLI_REV4) {
 8416			mbox = mempool_alloc(phba->mbox_mem_pool,
 8417					     GFP_KERNEL);
 8418			if (!mbox)
 8419				return 1;
 8420			lpfc_linkdown(phba);
 8421			lpfc_init_link(phba, mbox,
 8422				       phba->cfg_topology,
 8423				       phba->cfg_link_speed);
 8424			mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
 8425			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
 8426			mbox->vport = vport;
 8427			rc = lpfc_sli_issue_mbox(phba, mbox,
 8428						 MBX_NOWAIT);
 8429			lpfc_set_loopback_flag(phba);
 8430			if (rc == MBX_NOT_FINISHED)
 8431				mempool_free(mbox, phba->mbox_mem_pool);
 8432			return 1;
 8433		}
 8434
 8435		/* External loopback plug insertion detected */
 8436		phba->link_flag |= LS_EXTERNAL_LOOPBACK;
 8437
 8438		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_LIBDFC,
 8439				 "1119 External Loopback plug detected\n");
 8440
 8441		/* abort the flogi coming back to ourselves
 8442		 * due to external loopback on the port.
 8443		 */
 8444		lpfc_els_abort_flogi(phba);
 8445		return 0;
 8446
 8447	} else if (rc > 0) {	/* greater than */
 8448		set_bit(FC_PT2PT_PLOGI, &vport->fc_flag);
 8449
 8450		/* If we have the high WWPN we can assign our own
 8451		 * myDID; otherwise, we have to WAIT for a PLOGI
 8452		 * from the remote NPort to find out what it
 8453		 * will be.
 8454		 */
 8455		vport->fc_myDID = PT2PT_LocalID;
 8456	} else {
 8457		vport->fc_myDID = PT2PT_RemoteID;
 8458	}
 8459
 8460	/*
 8461	 * The vport state should go to LPFC_FLOGI only
 8462	 * AFTER we issue a FLOGI, not receive one.
 8463	 */
 8464	spin_lock_irq(shost->host_lock);
 8465	fc_flag = vport->fc_flag;
 8466	port_state = vport->port_state;
 8467	/* Acking an unsol FLOGI.  Count 1 for link bounce
 8468	 * work-around.
 8469	 */
 8470	vport->rcv_flogi_cnt++;
 8471	spin_unlock_irq(shost->host_lock);
 8472	set_bit(FC_PT2PT, &vport->fc_flag);
 8473	clear_bit(FC_FABRIC, &vport->fc_flag);
 8474	clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
 8475	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 8476			 "3311 Rcv Flogi PS x%x new PS x%x "
 8477			 "fc_flag x%lx new fc_flag x%lx, hba_flag x%lx\n",
 8478			 port_state, vport->port_state,
 8479			 fc_flag, vport->fc_flag, phba->hba_flag);
 8480
 8481	/*
 8482	 * We temporarily set fc_myDID to make it look like we are
 8483	 * a Fabric. This is done just so we end up with the right
 8484	 * did / sid on the FLOGI ACC rsp.
 8485	 */
 8486	did = vport->fc_myDID;
 8487	vport->fc_myDID = Fabric_DID;
 8488
 8489	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
 8490
 8491	/* Defer ACC response until AFTER we issue a FLOGI */
 8492	if (!test_bit(HBA_FLOGI_ISSUED, &phba->hba_flag)) {
 8493		phba->defer_flogi_acc.rx_id = bf_get(wqe_ctxt_tag,
 8494						     &wqe->xmit_els_rsp.wqe_com);
 8495		phba->defer_flogi_acc.ox_id = bf_get(wqe_rcvoxid,
 8496						     &wqe->xmit_els_rsp.wqe_com);
 8497
 8498		vport->fc_myDID = did;
 8499		phba->defer_flogi_acc.flag = true;
 8500
 8501		/* This nlp_get is paired with nlp_puts that reset the
 8502		 * defer_flogi_acc.flag back to false.  We need to retain
 8503		 * a kref on the ndlp until the deferred FLOGI ACC is
 8504		 * processed or cancelled.
 8505		 */
 8506		phba->defer_flogi_acc.ndlp = lpfc_nlp_get(ndlp);
 8507
 8508		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 8509				 "3344 Deferring FLOGI ACC: rx_id: x%x,"
 8510				 " ox_id: x%x, ndlp x%px, hba_flag x%lx\n",
 8511				 phba->defer_flogi_acc.rx_id,
 8512				 phba->defer_flogi_acc.ox_id,
 8513				 phba->defer_flogi_acc.ndlp,
 8514				 phba->hba_flag);
 8515		return 0;
 8516	}
 8517
 8518	/* Send back ACC */
 8519	lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
 8520
 8521	/* Now lets put fc_myDID back to what its supposed to be */
 8522	vport->fc_myDID = did;
 8523
 8524	return 0;
 8525}
 8526
 8527/**
 8528 * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
 8529 * @vport: pointer to a host virtual N_Port data structure.
 8530 * @cmdiocb: pointer to lpfc command iocb data structure.
 8531 * @ndlp: pointer to a node-list data structure.
 8532 *
 8533 * This routine processes Request Node Identification Data (RNID) IOCB
 8534 * received as an ELS unsolicited event. Only when the RNID specified format
 8535 * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
 8536 * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
 8537 * Accept (ACC) the RNID ELS command. All the other RNID formats are
 8538 * rejected by invoking the lpfc_els_rsp_reject() routine.
 8539 *
 8540 * Return code
 8541 *   0 - Successfully processed rnid iocb (currently always return 0)
 8542 **/
 8543static int
 8544lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 8545		  struct lpfc_nodelist *ndlp)
 8546{
 8547	struct lpfc_dmabuf *pcmd;
 8548	uint32_t *lp;
 8549	RNID *rn;
 8550	struct ls_rjt stat;
 8551
 8552	pcmd = cmdiocb->cmd_dmabuf;
 8553	lp = (uint32_t *) pcmd->virt;
 8554
 8555	lp++;
 8556	rn = (RNID *) lp;
 8557
 8558	/* RNID received */
 8559
 8560	switch (rn->Format) {
 8561	case 0:
 8562	case RNID_TOPOLOGY_DISC:
 8563		/* Send back ACC */
 8564		lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
 8565		break;
 8566	default:
 8567		/* Reject this request because format not supported */
 8568		stat.un.b.lsRjtRsvd0 = 0;
 8569		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
 8570		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
 8571		stat.un.b.vendorUnique = 0;
 8572		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
 8573			NULL);
 8574	}
 8575	return 0;
 8576}
 8577
 8578/**
 8579 * lpfc_els_rcv_echo - Process an unsolicited echo iocb
 8580 * @vport: pointer to a host virtual N_Port data structure.
 8581 * @cmdiocb: pointer to lpfc command iocb data structure.
 8582 * @ndlp: pointer to a node-list data structure.
 8583 *
 8584 * Return code
 8585 *   0 - Successfully processed echo iocb (currently always return 0)
 8586 **/
 8587static int
 8588lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 8589		  struct lpfc_nodelist *ndlp)
 8590{
 8591	uint8_t *pcmd;
 8592
 8593	pcmd = (uint8_t *)cmdiocb->cmd_dmabuf->virt;
 8594
 8595	/* skip over first word of echo command to find echo data */
 8596	pcmd += sizeof(uint32_t);
 8597
 8598	lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
 8599	return 0;
 8600}
 8601
 8602/**
 8603 * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
 8604 * @vport: pointer to a host virtual N_Port data structure.
 8605 * @cmdiocb: pointer to lpfc command iocb data structure.
 8606 * @ndlp: pointer to a node-list data structure.
 8607 *
 8608 * This routine processes a Link Incident Report Registration(LIRR) IOCB
 8609 * received as an ELS unsolicited event. Currently, this function just invokes
 8610 * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
 8611 *
 8612 * Return code
 8613 *   0 - Successfully processed lirr iocb (currently always return 0)
 8614 **/
 8615static int
 8616lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 8617		  struct lpfc_nodelist *ndlp)
 8618{
 8619	struct ls_rjt stat;
 8620
 8621	/* For now, unconditionally reject this command */
 8622	stat.un.b.lsRjtRsvd0 = 0;
 8623	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
 8624	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
 8625	stat.un.b.vendorUnique = 0;
 8626	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
 8627	return 0;
 8628}
 8629
 8630/**
 8631 * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
 8632 * @vport: pointer to a host virtual N_Port data structure.
 8633 * @cmdiocb: pointer to lpfc command iocb data structure.
 8634 * @ndlp: pointer to a node-list data structure.
 8635 *
 8636 * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
 8637 * received as an ELS unsolicited event. A request to RRQ shall only
 8638 * be accepted if the Originator Nx_Port N_Port_ID or the Responder
 8639 * Nx_Port N_Port_ID of the target Exchange is the same as the
 8640 * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
 8641 * not accepted, an LS_RJT with reason code "Unable to perform
 8642 * command request" and reason code explanation "Invalid Originator
 8643 * S_ID" shall be returned. For now, we just unconditionally accept
 8644 * RRQ from the target.
 8645 **/
 8646static void
 8647lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 8648		 struct lpfc_nodelist *ndlp)
 8649{
 8650	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
 8651	if (vport->phba->sli_rev == LPFC_SLI_REV4)
 8652		lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
 8653}
 8654
 8655/**
 8656 * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
 8657 * @phba: pointer to lpfc hba data structure.
 8658 * @pmb: pointer to the driver internal queue element for mailbox command.
 8659 *
 8660 * This routine is the completion callback function for the MBX_READ_LNK_STAT
 8661 * mailbox command. This callback function is to actually send the Accept
 8662 * (ACC) response to a Read Link Status (RLS) unsolicited IOCB event. It
 8663 * collects the link statistics from the completion of the MBX_READ_LNK_STAT
 8664 * mailbox command, constructs the RLS response with the link statistics
 8665 * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
 8666 * response to the RLS.
 8667 *
 8668 * Note that the ndlp reference count will be incremented by 1 for holding the
 8669 * ndlp and the reference to ndlp will be stored into the ndlp field of
 8670 * the IOCB for the completion callback function to the RLS Accept Response
 8671 * ELS IOCB command.
 8672 *
 8673 **/
 8674static void
 8675lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 8676{
 8677	int rc = 0;
 8678	MAILBOX_t *mb;
 8679	IOCB_t *icmd;
 8680	union lpfc_wqe128 *wqe;
 8681	struct RLS_RSP *rls_rsp;
 8682	uint8_t *pcmd;
 8683	struct lpfc_iocbq *elsiocb;
 8684	struct lpfc_nodelist *ndlp;
 8685	uint16_t oxid;
 8686	uint16_t rxid;
 8687	uint32_t cmdsize;
 8688	u32 ulp_context;
 8689
 8690	mb = &pmb->u.mb;
 8691
 8692	ndlp = pmb->ctx_ndlp;
 8693	rxid = (uint16_t)(pmb->ctx_u.ox_rx_id & 0xffff);
 8694	oxid = (uint16_t)((pmb->ctx_u.ox_rx_id >> 16) & 0xffff);
 8695	memset(&pmb->ctx_u, 0, sizeof(pmb->ctx_u));
 8696	pmb->ctx_ndlp = NULL;
 8697
 8698	if (mb->mbxStatus) {
 8699		mempool_free(pmb, phba->mbox_mem_pool);
 8700		return;
 8701	}
 8702
 8703	cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
 8704	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
 8705				     lpfc_max_els_tries, ndlp,
 8706				     ndlp->nlp_DID, ELS_CMD_ACC);
 8707
 8708	/* Decrement the ndlp reference count from previous mbox command */
 8709	lpfc_nlp_put(ndlp);
 8710
 8711	if (!elsiocb) {
 8712		mempool_free(pmb, phba->mbox_mem_pool);
 8713		return;
 8714	}
 8715
 8716	ulp_context = get_job_ulpcontext(phba, elsiocb);
 8717	if (phba->sli_rev == LPFC_SLI_REV4) {
 8718		wqe = &elsiocb->wqe;
 8719		/* Xri / rx_id */
 8720		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, rxid);
 8721		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com, oxid);
 8722	} else {
 8723		icmd = &elsiocb->iocb;
 8724		icmd->ulpContext = rxid;
 8725		icmd->unsli3.rcvsli3.ox_id = oxid;
 8726	}
 8727
 8728	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 8729	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
 8730	pcmd += sizeof(uint32_t); /* Skip past command */
 8731	rls_rsp = (struct RLS_RSP *)pcmd;
 8732
 8733	rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
 8734	rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
 8735	rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
 8736	rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
 8737	rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
 8738	rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
 8739	mempool_free(pmb, phba->mbox_mem_pool);
 8740	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
 8741	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
 8742			 "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
 8743			 "did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x\n",
 8744			 elsiocb->iotag, ulp_context,
 8745			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
 8746			 ndlp->nlp_rpi);
 8747	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 8748	phba->fc_stat.elsXmitACC++;
 8749	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 8750	if (!elsiocb->ndlp) {
 8751		lpfc_els_free_iocb(phba, elsiocb);
 8752		return;
 8753	}
 8754
 8755	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 8756	if (rc == IOCB_ERROR) {
 8757		lpfc_els_free_iocb(phba, elsiocb);
 8758		lpfc_nlp_put(ndlp);
 8759	}
 8760	return;
 8761}
 8762
 8763/**
 8764 * lpfc_els_rcv_rls - Process an unsolicited rls iocb
 8765 * @vport: pointer to a host virtual N_Port data structure.
 8766 * @cmdiocb: pointer to lpfc command iocb data structure.
 8767 * @ndlp: pointer to a node-list data structure.
 8768 *
 8769 * This routine processes Read Link Status (RLS) IOCB received as an
 8770 * ELS unsolicited event. It first checks the remote port state. If the
 8771 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
 8772 * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
 8773 * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
 8774 * for reading the HBA link statistics. It is for the callback function,
 8775 * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
 8776 * to actually sending out RPL Accept (ACC) response.
 8777 *
 8778 * Return codes
 8779 *   0 - Successfully processed rls iocb (currently always return 0)
 8780 **/
 8781static int
 8782lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 8783		 struct lpfc_nodelist *ndlp)
 8784{
 8785	struct lpfc_hba *phba = vport->phba;
 8786	LPFC_MBOXQ_t *mbox;
 8787	struct ls_rjt stat;
 8788	u32 ctx = get_job_ulpcontext(phba, cmdiocb);
 8789	u32 ox_id = get_job_rcvoxid(phba, cmdiocb);
 8790
 8791	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
 8792	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
 8793		/* reject the unsolicited RLS request and done with it */
 8794		goto reject_out;
 8795
 8796	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
 8797	if (mbox) {
 8798		lpfc_read_lnk_stat(phba, mbox);
 8799		mbox->ctx_u.ox_rx_id = ox_id << 16 | ctx;
 8800		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
 8801		if (!mbox->ctx_ndlp)
 8802			goto node_err;
 8803		mbox->vport = vport;
 8804		mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
 8805		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
 8806			!= MBX_NOT_FINISHED)
 8807			/* Mbox completion will send ELS Response */
 8808			return 0;
 8809		/* Decrement reference count used for the failed mbox
 8810		 * command.
 8811		 */
 8812		lpfc_nlp_put(ndlp);
 8813node_err:
 8814		mempool_free(mbox, phba->mbox_mem_pool);
 8815	}
 8816reject_out:
 8817	/* issue rejection response */
 8818	stat.un.b.lsRjtRsvd0 = 0;
 8819	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
 8820	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
 8821	stat.un.b.vendorUnique = 0;
 8822	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
 8823	return 0;
 8824}
 8825
 8826/**
 8827 * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
 8828 * @vport: pointer to a host virtual N_Port data structure.
 8829 * @cmdiocb: pointer to lpfc command iocb data structure.
 8830 * @ndlp: pointer to a node-list data structure.
 8831 *
 8832 * This routine processes Read Timeout Value (RTV) IOCB received as an
 8833 * ELS unsolicited event. It first checks the remote port state. If the
 8834 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
 8835 * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
 8836 * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
 8837 * Value (RTV) unsolicited IOCB event.
 8838 *
 8839 * Note that the ndlp reference count will be incremented by 1 for holding the
 8840 * ndlp and the reference to ndlp will be stored into the ndlp field of
 8841 * the IOCB for the completion callback function to the RTV Accept Response
 8842 * ELS IOCB command.
 8843 *
 8844 * Return codes
 8845 *   0 - Successfully processed rtv iocb (currently always return 0)
 8846 **/
 8847static int
 8848lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 8849		 struct lpfc_nodelist *ndlp)
 8850{
 8851	int rc = 0;
 8852	IOCB_t *icmd;
 8853	union lpfc_wqe128 *wqe;
 8854	struct lpfc_hba *phba = vport->phba;
 8855	struct ls_rjt stat;
 8856	struct RTV_RSP *rtv_rsp;
 8857	uint8_t *pcmd;
 8858	struct lpfc_iocbq *elsiocb;
 8859	uint32_t cmdsize;
 8860	u32 ulp_context;
 8861
 8862	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
 8863	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
 8864		/* reject the unsolicited RTV request and done with it */
 8865		goto reject_out;
 8866
 8867	cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
 8868	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
 8869				     lpfc_max_els_tries, ndlp,
 8870				     ndlp->nlp_DID, ELS_CMD_ACC);
 8871
 8872	if (!elsiocb)
 8873		return 1;
 8874
 8875	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 8876	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
 8877	pcmd += sizeof(uint32_t); /* Skip past command */
 8878
 8879	ulp_context = get_job_ulpcontext(phba, elsiocb);
 8880	/* use the command's xri in the response */
 8881	if (phba->sli_rev == LPFC_SLI_REV4) {
 8882		wqe = &elsiocb->wqe;
 8883		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
 8884		       get_job_ulpcontext(phba, cmdiocb));
 8885		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 8886		       get_job_rcvoxid(phba, cmdiocb));
 8887	} else {
 8888		icmd = &elsiocb->iocb;
 8889		icmd->ulpContext = get_job_ulpcontext(phba, cmdiocb);
 8890		icmd->unsli3.rcvsli3.ox_id = get_job_rcvoxid(phba, cmdiocb);
 8891	}
 8892
 8893	rtv_rsp = (struct RTV_RSP *)pcmd;
 8894
 8895	/* populate RTV payload */
 8896	rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
 8897	rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
 8898	bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
 8899	bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
 8900	rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
 8901
 8902	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
 8903	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
 8904			 "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
 8905			 "did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x, "
 8906			 "Data: x%x x%x x%x\n",
 8907			 elsiocb->iotag, ulp_context,
 8908			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
 8909			 ndlp->nlp_rpi,
 8910			rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
 8911	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 8912	phba->fc_stat.elsXmitACC++;
 8913	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 8914	if (!elsiocb->ndlp) {
 8915		lpfc_els_free_iocb(phba, elsiocb);
 8916		return 0;
 8917	}
 8918
 8919	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 8920	if (rc == IOCB_ERROR) {
 8921		lpfc_els_free_iocb(phba, elsiocb);
 8922		lpfc_nlp_put(ndlp);
 8923	}
 8924	return 0;
 8925
 8926reject_out:
 8927	/* issue rejection response */
 8928	stat.un.b.lsRjtRsvd0 = 0;
 8929	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
 8930	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
 8931	stat.un.b.vendorUnique = 0;
 8932	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
 8933	return 0;
 8934}
 8935
 8936/* lpfc_issue_els_rrq - Process an unsolicited rrq iocb
 8937 * @vport: pointer to a host virtual N_Port data structure.
 8938 * @ndlp: pointer to a node-list data structure.
 8939 * @did: DID of the target.
 8940 * @rrq: Pointer to the rrq struct.
 8941 *
 8942 * Build a ELS RRQ command and send it to the target. If the issue_iocb is
 8943 * successful, the completion handler will clear the RRQ.
 8944 *
 8945 * Return codes
 8946 *   0 - Successfully sent rrq els iocb.
 8947 *   1 - Failed to send rrq els iocb.
 8948 **/
 8949static int
 8950lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 8951			uint32_t did, struct lpfc_node_rrq *rrq)
 8952{
 8953	struct lpfc_hba  *phba = vport->phba;
 8954	struct RRQ *els_rrq;
 8955	struct lpfc_iocbq *elsiocb;
 8956	uint8_t *pcmd;
 8957	uint16_t cmdsize;
 8958	int ret;
 8959
 8960	if (!ndlp)
 8961		return 1;
 8962
 8963	/* If ndlp is not NULL, we will bump the reference count on it */
 8964	cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
 8965	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
 8966				     ELS_CMD_RRQ);
 8967	if (!elsiocb)
 8968		return 1;
 8969
 8970	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
 8971
 8972	/* For RRQ request, remainder of payload is Exchange IDs */
 8973	*((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
 8974	pcmd += sizeof(uint32_t);
 8975	els_rrq = (struct RRQ *) pcmd;
 8976
 8977	bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
 8978	bf_set(rrq_rxid, els_rrq, rrq->rxid);
 8979	bf_set(rrq_did, els_rrq, vport->fc_myDID);
 8980	els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
 8981	els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
 8982
 8983
 8984	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 8985		"Issue RRQ:     did:x%x",
 8986		did, rrq->xritag, rrq->rxid);
 8987	elsiocb->context_un.rrq = rrq;
 8988	elsiocb->cmd_cmpl = lpfc_cmpl_els_rrq;
 8989
 8990	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 8991	if (!elsiocb->ndlp)
 8992		goto io_err;
 8993
 8994	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 8995	if (ret == IOCB_ERROR) {
 8996		lpfc_nlp_put(ndlp);
 8997		goto io_err;
 8998	}
 8999	return 0;
 9000
 9001 io_err:
 9002	lpfc_els_free_iocb(phba, elsiocb);
 9003	return 1;
 9004}
 9005
 9006/**
 9007 * lpfc_send_rrq - Sends ELS RRQ if needed.
 9008 * @phba: pointer to lpfc hba data structure.
 9009 * @rrq: pointer to the active rrq.
 9010 *
 9011 * This routine will call the lpfc_issue_els_rrq if the rrq is
 9012 * still active for the xri. If this function returns a failure then
 9013 * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
 9014 *
 9015 * Returns 0 Success.
 9016 *         1 Failure.
 9017 **/
 9018int
 9019lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
 9020{
 9021	struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
 9022						       rrq->nlp_DID);
 9023	if (!ndlp)
 9024		return 1;
 9025
 9026	if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
 9027		return lpfc_issue_els_rrq(rrq->vport, ndlp,
 9028					 rrq->nlp_DID, rrq);
 9029	else
 9030		return 1;
 9031}
 9032
 9033/**
 9034 * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
 9035 * @vport: pointer to a host virtual N_Port data structure.
 9036 * @cmdsize: size of the ELS command.
 9037 * @oldiocb: pointer to the original lpfc command iocb data structure.
 9038 * @ndlp: pointer to a node-list data structure.
 9039 *
 9040 * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
 9041 * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
 9042 *
 9043 * Note that the ndlp reference count will be incremented by 1 for holding the
 9044 * ndlp and the reference to ndlp will be stored into the ndlp field of
 9045 * the IOCB for the completion callback function to the RPL Accept Response
 9046 * ELS command.
 9047 *
 9048 * Return code
 9049 *   0 - Successfully issued ACC RPL ELS command
 9050 *   1 - Failed to issue ACC RPL ELS command
 9051 **/
 9052static int
 9053lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
 9054		     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
 9055{
 9056	int rc = 0;
 9057	struct lpfc_hba *phba = vport->phba;
 9058	IOCB_t *icmd;
 9059	union lpfc_wqe128 *wqe;
 9060	RPL_RSP rpl_rsp;
 9061	struct lpfc_iocbq *elsiocb;
 9062	uint8_t *pcmd;
 9063	u32 ulp_context;
 9064
 9065	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
 9066				     ndlp->nlp_DID, ELS_CMD_ACC);
 9067
 9068	if (!elsiocb)
 9069		return 1;
 9070
 9071	ulp_context = get_job_ulpcontext(phba, elsiocb);
 9072	if (phba->sli_rev == LPFC_SLI_REV4) {
 9073		wqe = &elsiocb->wqe;
 9074		/* Xri / rx_id */
 9075		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
 9076		       get_job_ulpcontext(phba, oldiocb));
 9077		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 9078		       get_job_rcvoxid(phba, oldiocb));
 9079	} else {
 9080		icmd = &elsiocb->iocb;
 9081		icmd->ulpContext = get_job_ulpcontext(phba, oldiocb);
 9082		icmd->unsli3.rcvsli3.ox_id = get_job_rcvoxid(phba, oldiocb);
 9083	}
 9084
 9085	pcmd = elsiocb->cmd_dmabuf->virt;
 9086	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
 9087	pcmd += sizeof(uint16_t);
 9088	*((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
 9089	pcmd += sizeof(uint16_t);
 9090
 9091	/* Setup the RPL ACC payload */
 9092	rpl_rsp.listLen = be32_to_cpu(1);
 9093	rpl_rsp.index = 0;
 9094	rpl_rsp.port_num_blk.portNum = 0;
 9095	rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
 9096	memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
 9097	    sizeof(struct lpfc_name));
 9098	memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
 9099	/* Xmit ELS RPL ACC response tag <ulpIoTag> */
 9100	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 9101			 "0120 Xmit ELS RPL ACC response tag x%x "
 9102			 "xri x%x, did x%x, nlp_flag x%lx, nlp_state x%x, "
 9103			 "rpi x%x\n",
 9104			 elsiocb->iotag, ulp_context,
 9105			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
 9106			 ndlp->nlp_rpi);
 9107	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
 9108	phba->fc_stat.elsXmitACC++;
 9109	elsiocb->ndlp = lpfc_nlp_get(ndlp);
 9110	if (!elsiocb->ndlp) {
 9111		lpfc_els_free_iocb(phba, elsiocb);
 9112		return 1;
 9113	}
 9114
 9115	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
 9116	if (rc == IOCB_ERROR) {
 9117		lpfc_els_free_iocb(phba, elsiocb);
 9118		lpfc_nlp_put(ndlp);
 9119		return 1;
 9120	}
 9121
 9122	return 0;
 9123}
 9124
 9125/**
 9126 * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
 9127 * @vport: pointer to a host virtual N_Port data structure.
 9128 * @cmdiocb: pointer to lpfc command iocb data structure.
 9129 * @ndlp: pointer to a node-list data structure.
 9130 *
 9131 * This routine processes Read Port List (RPL) IOCB received as an ELS
 9132 * unsolicited event. It first checks the remote port state. If the remote
 9133 * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
 9134 * invokes the lpfc_els_rsp_reject() routine to send reject response.
 9135 * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
 9136 * to accept the RPL.
 9137 *
 9138 * Return code
 9139 *   0 - Successfully processed rpl iocb (currently always return 0)
 9140 **/
 9141static int
 9142lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 9143		 struct lpfc_nodelist *ndlp)
 9144{
 9145	struct lpfc_dmabuf *pcmd;
 9146	uint32_t *lp;
 9147	uint32_t maxsize;
 9148	uint16_t cmdsize;
 9149	RPL *rpl;
 9150	struct ls_rjt stat;
 9151
 9152	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
 9153	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
 9154		/* issue rejection response */
 9155		stat.un.b.lsRjtRsvd0 = 0;
 9156		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
 9157		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
 9158		stat.un.b.vendorUnique = 0;
 9159		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
 9160			NULL);
 9161		/* rejected the unsolicited RPL request and done with it */
 9162		return 0;
 9163	}
 9164
 9165	pcmd = cmdiocb->cmd_dmabuf;
 9166	lp = (uint32_t *) pcmd->virt;
 9167	rpl = (RPL *) (lp + 1);
 9168	maxsize = be32_to_cpu(rpl->maxsize);
 9169
 9170	/* We support only one port */
 9171	if ((rpl->index == 0) &&
 9172	    ((maxsize == 0) ||
 9173	     ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
 9174		cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
 9175	} else {
 9176		cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
 9177	}
 9178	lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
 9179
 9180	return 0;
 9181}
 9182
 9183/**
 9184 * lpfc_els_rcv_farp - Process an unsolicited farp request els command
 9185 * @vport: pointer to a virtual N_Port data structure.
 9186 * @cmdiocb: pointer to lpfc command iocb data structure.
 9187 * @ndlp: pointer to a node-list data structure.
 9188 *
 9189 * This routine processes Fibre Channel Address Resolution Protocol
 9190 * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
 9191 * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
 9192 * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
 9193 * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
 9194 * remote PortName is compared against the FC PortName stored in the @vport
 9195 * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
 9196 * compared against the FC NodeName stored in the @vport data structure.
 9197 * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
 9198 * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
 9199 * invoked to send out FARP Response to the remote node. Before sending the
 9200 * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
 9201 * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
 9202 * routine is invoked to log into the remote port first.
 9203 *
 9204 * Return code
 9205 *   0 - Either the FARP Match Mode not supported or successfully processed
 9206 **/
 9207static int
 9208lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 9209		  struct lpfc_nodelist *ndlp)
 9210{
 9211	struct lpfc_dmabuf *pcmd;
 9212	uint32_t *lp;
 9213	FARP *fp;
 9214	uint32_t cnt, did;
 9215
 9216	did = get_job_els_rsp64_did(vport->phba, cmdiocb);
 9217	pcmd = cmdiocb->cmd_dmabuf;
 9218	lp = (uint32_t *) pcmd->virt;
 9219
 9220	lp++;
 9221	fp = (FARP *) lp;
 9222	/* FARP-REQ received from DID <did> */
 9223	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 9224			 "0601 FARP-REQ received from DID x%x\n", did);
 9225	/* We will only support match on WWPN or WWNN */
 9226	if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
 9227		return 0;
 9228	}
 9229
 9230	cnt = 0;
 9231	/* If this FARP command is searching for my portname */
 9232	if (fp->Mflags & FARP_MATCH_PORT) {
 9233		if (memcmp(&fp->RportName, &vport->fc_portname,
 9234			   sizeof(struct lpfc_name)) == 0)
 9235			cnt = 1;
 9236	}
 9237
 9238	/* If this FARP command is searching for my nodename */
 9239	if (fp->Mflags & FARP_MATCH_NODE) {
 9240		if (memcmp(&fp->RnodeName, &vport->fc_nodename,
 9241			   sizeof(struct lpfc_name)) == 0)
 9242			cnt = 1;
 9243	}
 9244
 9245	if (cnt) {
 9246		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
 9247		   (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
 9248			/* Log back into the node before sending the FARP. */
 9249			if (fp->Rflags & FARP_REQUEST_PLOGI) {
 9250				ndlp->nlp_prev_state = ndlp->nlp_state;
 9251				lpfc_nlp_set_state(vport, ndlp,
 9252						   NLP_STE_PLOGI_ISSUE);
 9253				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
 9254			}
 9255
 9256			/* Send a FARP response to that node */
 9257			if (fp->Rflags & FARP_REQUEST_FARPR)
 9258				lpfc_issue_els_farpr(vport, did, 0);
 9259		}
 9260	}
 9261	return 0;
 9262}
 9263
 9264/**
 9265 * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
 9266 * @vport: pointer to a host virtual N_Port data structure.
 9267 * @cmdiocb: pointer to lpfc command iocb data structure.
 9268 * @ndlp: pointer to a node-list data structure.
 9269 *
 9270 * This routine processes Fibre Channel Address Resolution Protocol
 9271 * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
 9272 * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
 9273 * the FARP response request.
 9274 *
 9275 * Return code
 9276 *   0 - Successfully processed FARPR IOCB (currently always return 0)
 9277 **/
 9278static int
 9279lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 9280		   struct lpfc_nodelist  *ndlp)
 9281{
 9282	uint32_t did;
 9283
 9284	did = get_job_els_rsp64_did(vport->phba, cmdiocb);
 9285
 9286	/* FARP-RSP received from DID <did> */
 9287	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 9288			 "0600 FARP-RSP received from DID x%x\n", did);
 9289	/* ACCEPT the Farp resp request */
 9290	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
 9291
 9292	return 0;
 9293}
 9294
 9295/**
 9296 * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
 9297 * @vport: pointer to a host virtual N_Port data structure.
 9298 * @cmdiocb: pointer to lpfc command iocb data structure.
 9299 * @fan_ndlp: pointer to a node-list data structure.
 9300 *
 9301 * This routine processes a Fabric Address Notification (FAN) IOCB
 9302 * command received as an ELS unsolicited event. The FAN ELS command will
 9303 * only be processed on a physical port (i.e., the @vport represents the
 9304 * physical port). The fabric NodeName and PortName from the FAN IOCB are
 9305 * compared against those in the phba data structure. If any of those is
 9306 * different, the lpfc_initial_flogi() routine is invoked to initialize
 9307 * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
 9308 * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
 9309 * is invoked to register login to the fabric.
 9310 *
 9311 * Return code
 9312 *   0 - Successfully processed fan iocb (currently always return 0).
 9313 **/
 9314static int
 9315lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 9316		 struct lpfc_nodelist *fan_ndlp)
 9317{
 9318	struct lpfc_hba *phba = vport->phba;
 9319	uint32_t *lp;
 9320	FAN *fp;
 9321
 9322	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
 9323	lp = (uint32_t *)cmdiocb->cmd_dmabuf->virt;
 9324	fp = (FAN *) ++lp;
 9325	/* FAN received; Fan does not have a reply sequence */
 9326	if ((vport == phba->pport) &&
 9327	    (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
 9328		if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
 9329			    sizeof(struct lpfc_name))) ||
 9330		    (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
 9331			    sizeof(struct lpfc_name)))) {
 9332			/* This port has switched fabrics. FLOGI is required */
 9333			lpfc_issue_init_vfi(vport);
 9334		} else {
 9335			/* FAN verified - skip FLOGI */
 9336			vport->fc_myDID = vport->fc_prevDID;
 9337			if (phba->sli_rev < LPFC_SLI_REV4)
 9338				lpfc_issue_fabric_reglogin(vport);
 9339			else {
 9340				lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 9341					"3138 Need register VFI: (x%x/%x)\n",
 9342					vport->fc_prevDID, vport->fc_myDID);
 9343				lpfc_issue_reg_vfi(vport);
 9344			}
 9345		}
 9346	}
 9347	return 0;
 9348}
 9349
 9350/**
 9351 * lpfc_els_rcv_edc - Process an unsolicited EDC iocb
 9352 * @vport: pointer to a host virtual N_Port data structure.
 9353 * @cmdiocb: pointer to lpfc command iocb data structure.
 9354 * @ndlp: pointer to a node-list data structure.
 9355 *
 9356 * Return code
 9357 *   0 - Successfully processed echo iocb (currently always return 0)
 9358 **/
 9359static int
 9360lpfc_els_rcv_edc(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 9361		 struct lpfc_nodelist *ndlp)
 9362{
 9363	struct lpfc_hba  *phba = vport->phba;
 9364	struct fc_els_edc *edc_req;
 9365	struct fc_tlv_desc *tlv;
 9366	uint8_t *payload;
 9367	uint32_t *ptr, dtag;
 9368	const char *dtag_nm;
 9369	int desc_cnt = 0, bytes_remain;
 9370	struct fc_diag_lnkflt_desc *plnkflt;
 9371
 9372	payload = cmdiocb->cmd_dmabuf->virt;
 9373
 9374	edc_req = (struct fc_els_edc *)payload;
 9375	bytes_remain = be32_to_cpu(edc_req->desc_len);
 9376
 9377	ptr = (uint32_t *)payload;
 9378	lpfc_printf_vlog(vport, KERN_INFO,
 9379			 LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
 9380			 "3319 Rcv EDC payload len %d: x%x x%x x%x\n",
 9381			 bytes_remain, be32_to_cpu(*ptr),
 9382			 be32_to_cpu(*(ptr + 1)), be32_to_cpu(*(ptr + 2)));
 9383
 9384	/* No signal support unless there is a congestion descriptor */
 9385	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
 9386	phba->cgn_sig_freq = 0;
 9387	phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
 9388
 9389	if (bytes_remain <= 0)
 9390		goto out;
 9391
 9392	tlv = edc_req->desc;
 9393
 9394	/*
 9395	 * cycle through EDC diagnostic descriptors to find the
 9396	 * congestion signaling capability descriptor
 9397	 */
 9398	while (bytes_remain) {
 9399		if (bytes_remain < FC_TLV_DESC_HDR_SZ) {
 9400			lpfc_printf_log(phba, KERN_WARNING,
 9401					LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
 9402					"6464 Truncated TLV hdr on "
 9403					"Diagnostic descriptor[%d]\n",
 9404					desc_cnt);
 9405			goto out;
 9406		}
 9407
 9408		dtag = be32_to_cpu(tlv->desc_tag);
 9409		switch (dtag) {
 9410		case ELS_DTAG_LNK_FAULT_CAP:
 9411			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
 9412			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
 9413				sizeof(struct fc_diag_lnkflt_desc)) {
 9414				lpfc_printf_log(phba, KERN_WARNING,
 9415					LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
 9416					"6465 Truncated Link Fault Diagnostic "
 9417					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
 9418					desc_cnt, bytes_remain,
 9419					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
 9420					sizeof(struct fc_diag_lnkflt_desc));
 9421				goto out;
 9422			}
 9423			plnkflt = (struct fc_diag_lnkflt_desc *)tlv;
 9424			lpfc_printf_log(phba, KERN_INFO,
 9425				LOG_ELS | LOG_LDS_EVENT,
 9426				"4626 Link Fault Desc Data: x%08x len x%x "
 9427				"da x%x dd x%x interval x%x\n",
 9428				be32_to_cpu(plnkflt->desc_tag),
 9429				be32_to_cpu(plnkflt->desc_len),
 9430				be32_to_cpu(
 9431					plnkflt->degrade_activate_threshold),
 9432				be32_to_cpu(
 9433					plnkflt->degrade_deactivate_threshold),
 9434				be32_to_cpu(plnkflt->fec_degrade_interval));
 9435			break;
 9436		case ELS_DTAG_CG_SIGNAL_CAP:
 9437			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
 9438			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
 9439				sizeof(struct fc_diag_cg_sig_desc)) {
 9440				lpfc_printf_log(
 9441					phba, KERN_WARNING, LOG_CGN_MGMT,
 9442					"6466 Truncated cgn signal Diagnostic "
 9443					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
 9444					desc_cnt, bytes_remain,
 9445					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
 9446					sizeof(struct fc_diag_cg_sig_desc));
 9447				goto out;
 9448			}
 9449
 9450			phba->cgn_reg_fpin = phba->cgn_init_reg_fpin;
 9451			phba->cgn_reg_signal = phba->cgn_init_reg_signal;
 9452
 9453			/* We start negotiation with lpfc_fabric_cgn_frequency.
 9454			 * When we process the EDC, we will settle on the
 9455			 * higher frequency.
 9456			 */
 9457			phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
 9458
 9459			lpfc_least_capable_settings(
 9460				phba, (struct fc_diag_cg_sig_desc *)tlv);
 9461			break;
 9462		default:
 9463			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
 9464			lpfc_printf_log(phba, KERN_WARNING,
 9465					LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
 9466					"6467 unknown Diagnostic "
 9467					"Descriptor[%d]: tag x%x (%s)\n",
 9468					desc_cnt, dtag, dtag_nm);
 9469		}
 9470		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
 9471		tlv = fc_tlv_next_desc(tlv);
 9472		desc_cnt++;
 9473	}
 9474out:
 9475	/* Need to send back an ACC */
 9476	lpfc_issue_els_edc_rsp(vport, cmdiocb, ndlp);
 9477
 9478	lpfc_config_cgn_signal(phba);
 9479	return 0;
 9480}
 9481
 9482/**
 9483 * lpfc_els_timeout - Handler funciton to the els timer
 9484 * @t: timer context used to obtain the vport.
 9485 *
 9486 * This routine is invoked by the ELS timer after timeout. It posts the ELS
 9487 * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
 9488 * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
 9489 * up the worker thread. It is for the worker thread to invoke the routine
 9490 * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
 9491 **/
 9492void
 9493lpfc_els_timeout(struct timer_list *t)
 9494{
 9495	struct lpfc_vport *vport = timer_container_of(vport, t, els_tmofunc);
 9496	struct lpfc_hba   *phba = vport->phba;
 9497	uint32_t tmo_posted;
 9498	unsigned long iflag;
 9499
 9500	spin_lock_irqsave(&vport->work_port_lock, iflag);
 9501	tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
 9502	if (!tmo_posted && !test_bit(FC_UNLOADING, &vport->load_flag))
 9503		vport->work_port_events |= WORKER_ELS_TMO;
 9504	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
 9505
 9506	if (!tmo_posted && !test_bit(FC_UNLOADING, &vport->load_flag))
 9507		lpfc_worker_wake_up(phba);
 9508	return;
 9509}
 9510
 9511
 9512/**
 9513 * lpfc_els_timeout_handler - Process an els timeout event
 9514 * @vport: pointer to a virtual N_Port data structure.
 9515 *
 9516 * This routine is the actual handler function that processes an ELS timeout
 9517 * event. It walks the ELS ring to get and abort all the IOCBs (except the
 9518 * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
 9519 * invoking the lpfc_sli_issue_abort_iotag() routine.
 9520 **/
 9521void
 9522lpfc_els_timeout_handler(struct lpfc_vport *vport)
 9523{
 9524	struct lpfc_hba  *phba = vport->phba;
 9525	struct lpfc_sli_ring *pring;
 9526	struct lpfc_iocbq *tmp_iocb, *piocb;
 9527	IOCB_t *cmd = NULL;
 9528	struct lpfc_dmabuf *pcmd;
 9529	uint32_t els_command = 0;
 9530	uint32_t timeout;
 9531	uint32_t remote_ID = 0xffffffff;
 9532	LIST_HEAD(abort_list);
 9533	u32 ulp_command = 0, ulp_context = 0, did = 0, iotag = 0;
 9534
 9535
 9536	timeout = (uint32_t)(phba->fc_ratov << 1);
 9537
 9538	pring = lpfc_phba_elsring(phba);
 9539	if (unlikely(!pring))
 9540		return;
 9541
 9542	if (test_bit(FC_UNLOADING, &phba->pport->load_flag))
 9543		return;
 9544
 9545	spin_lock_irq(&phba->hbalock);
 9546	if (phba->sli_rev == LPFC_SLI_REV4)
 9547		spin_lock(&pring->ring_lock);
 9548
 9549	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
 9550		ulp_command = get_job_cmnd(phba, piocb);
 9551		ulp_context = get_job_ulpcontext(phba, piocb);
 9552		did = get_job_els_rsp64_did(phba, piocb);
 9553
 9554		if (phba->sli_rev == LPFC_SLI_REV4) {
 9555			iotag = get_wqe_reqtag(piocb);
 9556		} else {
 9557			cmd = &piocb->iocb;
 9558			iotag = cmd->ulpIoTag;
 9559		}
 9560
 9561		if ((piocb->cmd_flag & LPFC_IO_LIBDFC) != 0 ||
 9562		    ulp_command == CMD_ABORT_XRI_CX ||
 9563		    ulp_command == CMD_ABORT_XRI_CN ||
 9564		    ulp_command == CMD_CLOSE_XRI_CN)
 9565			continue;
 9566
 9567		if (piocb->vport != vport)
 9568			continue;
 9569
 9570		pcmd = piocb->cmd_dmabuf;
 9571		if (pcmd)
 9572			els_command = *(uint32_t *) (pcmd->virt);
 9573
 9574		if (els_command == ELS_CMD_FARP ||
 9575		    els_command == ELS_CMD_FARPR ||
 9576		    els_command == ELS_CMD_FDISC)
 9577			continue;
 9578
 9579		if (piocb->drvrTimeout > 0) {
 9580			if (piocb->drvrTimeout >= timeout)
 9581				piocb->drvrTimeout -= timeout;
 9582			else
 9583				piocb->drvrTimeout = 0;
 9584			continue;
 9585		}
 9586
 9587		remote_ID = 0xffffffff;
 9588		if (ulp_command != CMD_GEN_REQUEST64_CR) {
 9589			remote_ID = did;
 9590		} else {
 9591			struct lpfc_nodelist *ndlp;
 9592			ndlp = __lpfc_findnode_rpi(vport, ulp_context);
 9593			if (ndlp)
 9594				remote_ID = ndlp->nlp_DID;
 9595		}
 9596		list_add_tail(&piocb->dlist, &abort_list);
 9597	}
 9598	if (phba->sli_rev == LPFC_SLI_REV4)
 9599		spin_unlock(&pring->ring_lock);
 9600	spin_unlock_irq(&phba->hbalock);
 9601
 9602	list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
 9603		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 9604			 "0127 ELS timeout Data: x%x x%x x%x "
 9605			 "x%x\n", els_command,
 9606			 remote_ID, ulp_command, iotag);
 9607
 9608		spin_lock_irq(&phba->hbalock);
 9609		list_del_init(&piocb->dlist);
 9610		lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
 9611		spin_unlock_irq(&phba->hbalock);
 9612	}
 9613
 9614	/* Make sure HBA is alive */
 9615	lpfc_issue_hb_tmo(phba);
 9616
 9617	if (!list_empty(&pring->txcmplq))
 9618		if (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
 9619			mod_timer(&vport->els_tmofunc,
 9620				  jiffies + secs_to_jiffies(timeout));
 9621}
 9622
 9623/**
 9624 * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
 9625 * @vport: pointer to a host virtual N_Port data structure.
 9626 *
 9627 * This routine is used to clean up all the outstanding ELS commands on a
 9628 * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
 9629 * routine. After that, it walks the ELS transmit queue to remove all the
 9630 * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
 9631 * the IOCBs with a non-NULL completion callback function, the callback
 9632 * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
 9633 * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
 9634 * callback function, the IOCB will simply be released. Finally, it walks
 9635 * the ELS transmit completion queue to issue an abort IOCB to any transmit
 9636 * completion queue IOCB that is associated with the @vport and is not
 9637 * an IOCB from libdfc (i.e., the management plane IOCBs that are not
 9638 * part of the discovery state machine) out to HBA by invoking the
 9639 * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
 9640 * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
 9641 * the IOCBs are aborted when this function returns.
 9642 **/
 9643void
 9644lpfc_els_flush_cmd(struct lpfc_vport *vport)
 9645{
 9646	LIST_HEAD(abort_list);
 9647	LIST_HEAD(cancel_list);
 9648	struct lpfc_hba  *phba = vport->phba;
 9649	struct lpfc_sli_ring *pring;
 9650	struct lpfc_iocbq *tmp_iocb, *piocb;
 9651	u32 ulp_command;
 9652	unsigned long iflags = 0;
 9653	bool mbx_tmo_err;
 9654
 9655	lpfc_fabric_abort_vport(vport);
 9656
 9657	/*
 9658	 * For SLI3, only the hbalock is required.  But SLI4 needs to coordinate
 9659	 * with the ring insert operation.  Because lpfc_sli_issue_abort_iotag
 9660	 * ultimately grabs the ring_lock, the driver must splice the list into
 9661	 * a working list and release the locks before calling the abort.
 9662	 */
 9663	spin_lock_irqsave(&phba->hbalock, iflags);
 9664	pring = lpfc_phba_elsring(phba);
 9665
 9666	/* Bail out if we've no ELS wq, like in PCI error recovery case. */
 9667	if (unlikely(!pring)) {
 9668		spin_unlock_irqrestore(&phba->hbalock, iflags);
 9669		return;
 9670	}
 9671
 9672	if (phba->sli_rev == LPFC_SLI_REV4)
 9673		spin_lock(&pring->ring_lock);
 9674
 9675	mbx_tmo_err = test_bit(MBX_TMO_ERR, &phba->bit_flags);
 9676	/* First we need to issue aborts to outstanding cmds on txcmpl */
 9677	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
 9678		if (piocb->vport != vport)
 9679			continue;
 9680
 9681		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 9682				 "2243 iotag = 0x%x cmd_flag = 0x%x "
 9683				 "ulp_command = 0x%x sli_flag = 0x%x\n",
 9684				 piocb->iotag, piocb->cmd_flag,
 9685				 get_job_cmnd(phba, piocb),
 9686				 phba->sli.sli_flag);
 9687
 9688		if ((phba->sli.sli_flag & LPFC_SLI_ACTIVE) && !mbx_tmo_err) {
 9689			if (piocb->cmd_flag & LPFC_IO_LIBDFC)
 9690				continue;
 9691			if (piocb->cmd_flag & LPFC_DRIVER_ABORTED)
 9692				continue;
 9693		}
 9694
 9695		/* On the ELS ring we can have ELS_REQUESTs, ELS_RSPs,
 9696		 * or GEN_REQUESTs waiting for a CQE response.
 9697		 */
 9698		ulp_command = get_job_cmnd(phba, piocb);
 9699		if (ulp_command == CMD_ELS_REQUEST64_WQE ||
 9700		    ulp_command == CMD_XMIT_ELS_RSP64_WQE) {
 9701			list_add_tail(&piocb->dlist, &abort_list);
 9702
 9703			/* If the link is down when flushing ELS commands
 9704			 * the firmware will not complete them till after
 9705			 * the link comes back up. This may confuse
 9706			 * discovery for the new link up, so we need to
 9707			 * change the compl routine to just clean up the iocb
 9708			 * and avoid any retry logic.
 9709			 */
 9710			if (phba->link_state == LPFC_LINK_DOWN)
 9711				piocb->cmd_cmpl = lpfc_cmpl_els_link_down;
 9712		} else if (ulp_command == CMD_GEN_REQUEST64_CR ||
 9713			   mbx_tmo_err)
 9714			list_add_tail(&piocb->dlist, &abort_list);
 9715	}
 9716
 9717	if (phba->sli_rev == LPFC_SLI_REV4)
 9718		spin_unlock(&pring->ring_lock);
 9719	spin_unlock_irqrestore(&phba->hbalock, iflags);
 9720
 9721	/* Abort each txcmpl iocb on aborted list and remove the dlist links. */
 9722	list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
 9723		spin_lock_irqsave(&phba->hbalock, iflags);
 9724		list_del_init(&piocb->dlist);
 9725		if (mbx_tmo_err || !(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
 9726			list_move_tail(&piocb->list, &cancel_list);
 9727		else
 9728			lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
 9729
 9730		spin_unlock_irqrestore(&phba->hbalock, iflags);
 9731	}
 9732	if (!list_empty(&cancel_list))
 9733		lpfc_sli_cancel_iocbs(phba, &cancel_list, IOSTAT_LOCAL_REJECT,
 9734				      IOERR_SLI_ABORTED);
 9735	else
 9736		/* Make sure HBA is alive */
 9737		lpfc_issue_hb_tmo(phba);
 9738
 9739	if (!list_empty(&abort_list))
 9740		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 9741				 "3387 abort list for txq not empty\n");
 9742	INIT_LIST_HEAD(&abort_list);
 9743
 9744	spin_lock_irqsave(&phba->hbalock, iflags);
 9745	if (phba->sli_rev == LPFC_SLI_REV4)
 9746		spin_lock(&pring->ring_lock);
 9747
 9748	/* No need to abort the txq list,
 9749	 * just queue them up for lpfc_sli_cancel_iocbs
 9750	 */
 9751	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
 9752		ulp_command = get_job_cmnd(phba, piocb);
 9753
 9754		if (piocb->cmd_flag & LPFC_IO_LIBDFC)
 9755			continue;
 9756
 9757		/* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
 9758		if (ulp_command == CMD_QUE_RING_BUF_CN ||
 9759		    ulp_command == CMD_QUE_RING_BUF64_CN ||
 9760		    ulp_command == CMD_CLOSE_XRI_CN ||
 9761		    ulp_command == CMD_ABORT_XRI_CN ||
 9762		    ulp_command == CMD_ABORT_XRI_CX)
 9763			continue;
 9764
 9765		if (piocb->vport != vport)
 9766			continue;
 9767
 9768		list_del_init(&piocb->list);
 9769		list_add_tail(&piocb->list, &abort_list);
 9770	}
 9771
 9772	/* The same holds true for any FLOGI/FDISC on the fabric_iocb_list */
 9773	if (vport == phba->pport) {
 9774		list_for_each_entry_safe(piocb, tmp_iocb,
 9775					 &phba->fabric_iocb_list, list) {
 9776			list_del_init(&piocb->list);
 9777			list_add_tail(&piocb->list, &abort_list);
 9778		}
 9779	}
 9780
 9781	if (phba->sli_rev == LPFC_SLI_REV4)
 9782		spin_unlock(&pring->ring_lock);
 9783	spin_unlock_irqrestore(&phba->hbalock, iflags);
 9784
 9785	/* Cancel all the IOCBs from the completions list */
 9786	lpfc_sli_cancel_iocbs(phba, &abort_list,
 9787			      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
 9788
 9789	return;
 9790}
 9791
 9792/**
 9793 * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
 9794 * @phba: pointer to lpfc hba data structure.
 9795 *
 9796 * This routine is used to clean up all the outstanding ELS commands on a
 9797 * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
 9798 * routine. After that, it walks the ELS transmit queue to remove all the
 9799 * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
 9800 * the IOCBs with the completion callback function associated, the callback
 9801 * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
 9802 * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
 9803 * callback function associated, the IOCB will simply be released. Finally,
 9804 * it walks the ELS transmit completion queue to issue an abort IOCB to any
 9805 * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
 9806 * management plane IOCBs that are not part of the discovery state machine)
 9807 * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
 9808 **/
 9809void
 9810lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
 9811{
 9812	struct lpfc_vport *vport;
 9813
 9814	spin_lock_irq(&phba->port_list_lock);
 9815	list_for_each_entry(vport, &phba->port_list, listentry)
 9816		lpfc_els_flush_cmd(vport);
 9817	spin_unlock_irq(&phba->port_list_lock);
 9818
 9819	return;
 9820}
 9821
 9822/**
 9823 * lpfc_send_els_failure_event - Posts an ELS command failure event
 9824 * @phba: Pointer to hba context object.
 9825 * @cmdiocbp: Pointer to command iocb which reported error.
 9826 * @rspiocbp: Pointer to response iocb which reported error.
 9827 *
 9828 * This function sends an event when there is an ELS command
 9829 * failure.
 9830 **/
 9831void
 9832lpfc_send_els_failure_event(struct lpfc_hba *phba,
 9833			struct lpfc_iocbq *cmdiocbp,
 9834			struct lpfc_iocbq *rspiocbp)
 9835{
 9836	struct lpfc_vport *vport = cmdiocbp->vport;
 9837	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 9838	struct lpfc_lsrjt_event lsrjt_event;
 9839	struct lpfc_fabric_event_header fabric_event;
 9840	struct ls_rjt stat;
 9841	struct lpfc_nodelist *ndlp;
 9842	uint32_t *pcmd;
 9843	u32 ulp_status, ulp_word4;
 9844
 9845	ndlp = cmdiocbp->ndlp;
 9846	if (!ndlp)
 9847		return;
 9848
 9849	ulp_status = get_job_ulpstatus(phba, rspiocbp);
 9850	ulp_word4 = get_job_word4(phba, rspiocbp);
 9851
 9852	if (ulp_status == IOSTAT_LS_RJT) {
 9853		lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
 9854		lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
 9855		memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
 9856			sizeof(struct lpfc_name));
 9857		memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
 9858			sizeof(struct lpfc_name));
 9859		pcmd = (uint32_t *)cmdiocbp->cmd_dmabuf->virt;
 9860		lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
 9861		stat.un.ls_rjt_error_be = cpu_to_be32(ulp_word4);
 9862		lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
 9863		lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
 9864		fc_host_post_vendor_event(shost,
 9865			fc_get_event_number(),
 9866			sizeof(lsrjt_event),
 9867			(char *)&lsrjt_event,
 9868			LPFC_NL_VENDOR_ID);
 9869		return;
 9870	}
 9871	if (ulp_status == IOSTAT_NPORT_BSY ||
 9872	    ulp_status == IOSTAT_FABRIC_BSY) {
 9873		fabric_event.event_type = FC_REG_FABRIC_EVENT;
 9874		if (ulp_status == IOSTAT_NPORT_BSY)
 9875			fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
 9876		else
 9877			fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
 9878		memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
 9879			sizeof(struct lpfc_name));
 9880		memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
 9881			sizeof(struct lpfc_name));
 9882		fc_host_post_vendor_event(shost,
 9883			fc_get_event_number(),
 9884			sizeof(fabric_event),
 9885			(char *)&fabric_event,
 9886			LPFC_NL_VENDOR_ID);
 9887		return;
 9888	}
 9889
 9890}
 9891
 9892/**
 9893 * lpfc_send_els_event - Posts unsolicited els event
 9894 * @vport: Pointer to vport object.
 9895 * @ndlp: Pointer FC node object.
 9896 * @payload: ELS command code type.
 9897 *
 9898 * This function posts an event when there is an incoming
 9899 * unsolicited ELS command.
 9900 **/
 9901static void
 9902lpfc_send_els_event(struct lpfc_vport *vport,
 9903		    struct lpfc_nodelist *ndlp,
 9904		    uint32_t *payload)
 9905{
 9906	struct lpfc_els_event_header *els_data = NULL;
 9907	struct lpfc_logo_event *logo_data = NULL;
 9908	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 9909
 9910	if (*payload == ELS_CMD_LOGO) {
 9911		logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
 9912		if (!logo_data) {
 9913			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 9914				"0148 Failed to allocate memory "
 9915				"for LOGO event\n");
 9916			return;
 9917		}
 9918		els_data = &logo_data->header;
 9919	} else {
 9920		els_data = kmalloc(sizeof(struct lpfc_els_event_header),
 9921			GFP_KERNEL);
 9922		if (!els_data) {
 9923			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 9924				"0149 Failed to allocate memory "
 9925				"for ELS event\n");
 9926			return;
 9927		}
 9928	}
 9929	els_data->event_type = FC_REG_ELS_EVENT;
 9930	switch (*payload) {
 9931	case ELS_CMD_PLOGI:
 9932		els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
 9933		break;
 9934	case ELS_CMD_PRLO:
 9935		els_data->subcategory = LPFC_EVENT_PRLO_RCV;
 9936		break;
 9937	case ELS_CMD_ADISC:
 9938		els_data->subcategory = LPFC_EVENT_ADISC_RCV;
 9939		break;
 9940	case ELS_CMD_LOGO:
 9941		els_data->subcategory = LPFC_EVENT_LOGO_RCV;
 9942		/* Copy the WWPN in the LOGO payload */
 9943		memcpy(logo_data->logo_wwpn, &payload[2],
 9944			sizeof(struct lpfc_name));
 9945		break;
 9946	default:
 9947		kfree(els_data);
 9948		return;
 9949	}
 9950	memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
 9951	memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
 9952	if (*payload == ELS_CMD_LOGO) {
 9953		fc_host_post_vendor_event(shost,
 9954			fc_get_event_number(),
 9955			sizeof(struct lpfc_logo_event),
 9956			(char *)logo_data,
 9957			LPFC_NL_VENDOR_ID);
 9958		kfree(logo_data);
 9959	} else {
 9960		fc_host_post_vendor_event(shost,
 9961			fc_get_event_number(),
 9962			sizeof(struct lpfc_els_event_header),
 9963			(char *)els_data,
 9964			LPFC_NL_VENDOR_ID);
 9965		kfree(els_data);
 9966	}
 9967
 9968	return;
 9969}
 9970
 9971
 9972DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_li_event_nm, fc_fpin_li_event_types,
 9973			FC_FPIN_LI_EVT_TYPES_INIT);
 9974
 9975DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_deli_event_nm, fc_fpin_deli_event_types,
 9976			FC_FPIN_DELI_EVT_TYPES_INIT);
 9977
 9978DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_event_nm, fc_fpin_congn_event_types,
 9979			FC_FPIN_CONGN_EVT_TYPES_INIT);
 9980
 9981DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_severity_nm,
 9982			fc_fpin_congn_severity_types,
 9983			FC_FPIN_CONGN_SEVERITY_INIT);
 9984
 9985
 9986/**
 9987 * lpfc_display_fpin_wwpn - Display WWPNs accessible by the attached port
 9988 * @phba: Pointer to phba object.
 9989 * @wwnlist: Pointer to list of WWPNs in FPIN payload
 9990 * @cnt: count of WWPNs in FPIN payload
 9991 *
 9992 * This routine is called by LI and PC descriptors.
 9993 * Limit the number of WWPNs displayed to 6 log messages, 6 per log message
 9994 */
 9995static void
 9996lpfc_display_fpin_wwpn(struct lpfc_hba *phba, __be64 *wwnlist, u32 cnt)
 9997{
 9998	char buf[LPFC_FPIN_WWPN_LINE_SZ];
 9999	__be64 wwn;
10000	u64 wwpn;
10001	int i, len;
10002	int line = 0;
10003	int wcnt = 0;
10004	bool endit = false;
10005
10006	len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ, "Accessible WWPNs:");
10007	for (i = 0; i < cnt; i++) {
10008		/* Are we on the last WWPN */
10009		if (i == (cnt - 1))
10010			endit = true;
10011
10012		/* Extract the next WWPN from the payload */
10013		wwn = *wwnlist++;
10014		wwpn = be64_to_cpu(wwn);
10015		len += scnprintf(buf + len, LPFC_FPIN_WWPN_LINE_SZ - len,
10016				 " %016llx", wwpn);
10017
10018		/* Log a message if we are on the last WWPN
10019		 * or if we hit the max allowed per message.
10020		 */
10021		wcnt++;
10022		if (wcnt == LPFC_FPIN_WWPN_LINE_CNT || endit) {
10023			buf[len] = 0;
10024			lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10025					"4686 %s\n", buf);
10026
10027			/* Check if we reached the last WWPN */
10028			if (endit)
10029				return;
10030
10031			/* Limit the number of log message displayed per FPIN */
10032			line++;
10033			if (line == LPFC_FPIN_WWPN_NUM_LINE) {
10034				lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10035						"4687 %d WWPNs Truncated\n",
10036						cnt - i - 1);
10037				return;
10038			}
10039
10040			/* Start over with next log message */
10041			wcnt = 0;
10042			len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ,
10043					"Additional WWPNs:");
10044		}
10045	}
10046}
10047
10048/**
10049 * lpfc_els_rcv_fpin_li - Process an FPIN Link Integrity Event.
10050 * @phba: Pointer to phba object.
10051 * @tlv:  Pointer to the Link Integrity Notification Descriptor.
10052 *
10053 * This function processes a Link Integrity FPIN event by logging a message.
10054 **/
10055static void
10056lpfc_els_rcv_fpin_li(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
10057{
10058	struct fc_fn_li_desc *li = (struct fc_fn_li_desc *)tlv;
10059	const char *li_evt_str;
10060	u32 li_evt, cnt;
10061
10062	li_evt = be16_to_cpu(li->event_type);
10063	li_evt_str = lpfc_get_fpin_li_event_nm(li_evt);
10064	cnt = be32_to_cpu(li->pname_count);
10065
10066	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10067			"4680 FPIN Link Integrity %s (x%x) "
10068			"Detecting PN x%016llx Attached PN x%016llx "
10069			"Duration %d mSecs Count %d Port Cnt %d\n",
10070			li_evt_str, li_evt,
10071			be64_to_cpu(li->detecting_wwpn),
10072			be64_to_cpu(li->attached_wwpn),
10073			be32_to_cpu(li->event_threshold),
10074			be32_to_cpu(li->event_count), cnt);
10075
10076	lpfc_display_fpin_wwpn(phba, (__be64 *)&li->pname_list, cnt);
10077}
10078
10079/**
10080 * lpfc_els_rcv_fpin_del - Process an FPIN Delivery Event.
10081 * @phba: Pointer to hba object.
10082 * @tlv:  Pointer to the Delivery Notification Descriptor TLV
10083 *
10084 * This function processes a Delivery FPIN event by logging a message.
10085 **/
10086static void
10087lpfc_els_rcv_fpin_del(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
10088{
10089	struct fc_fn_deli_desc *del = (struct fc_fn_deli_desc *)tlv;
10090	const char *del_rsn_str;
10091	u32 del_rsn;
10092	__be32 *frame;
10093
10094	del_rsn = be16_to_cpu(del->deli_reason_code);
10095	del_rsn_str = lpfc_get_fpin_deli_event_nm(del_rsn);
10096
10097	/* Skip over desc_tag/desc_len header to payload */
10098	frame = (__be32 *)(del + 1);
10099
10100	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10101			"4681 FPIN Delivery %s (x%x) "
10102			"Detecting PN x%016llx Attached PN x%016llx "
10103			"DiscHdr0  x%08x "
10104			"DiscHdr1 x%08x DiscHdr2 x%08x DiscHdr3 x%08x "
10105			"DiscHdr4 x%08x DiscHdr5 x%08x\n",
10106			del_rsn_str, del_rsn,
10107			be64_to_cpu(del->detecting_wwpn),
10108			be64_to_cpu(del->attached_wwpn),
10109			be32_to_cpu(frame[0]),
10110			be32_to_cpu(frame[1]),
10111			be32_to_cpu(frame[2]),
10112			be32_to_cpu(frame[3]),
10113			be32_to_cpu(frame[4]),
10114			be32_to_cpu(frame[5]));
10115}
10116
10117/**
10118 * lpfc_els_rcv_fpin_peer_cgn - Process a FPIN Peer Congestion Event.
10119 * @phba: Pointer to hba object.
10120 * @tlv:  Pointer to the Peer Congestion Notification Descriptor TLV
10121 *
10122 * This function processes a Peer Congestion FPIN event by logging a message.
10123 **/
10124static void
10125lpfc_els_rcv_fpin_peer_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
10126{
10127	struct fc_fn_peer_congn_desc *pc = (struct fc_fn_peer_congn_desc *)tlv;
10128	const char *pc_evt_str;
10129	u32 pc_evt, cnt;
10130
10131	pc_evt = be16_to_cpu(pc->event_type);
10132	pc_evt_str = lpfc_get_fpin_congn_event_nm(pc_evt);
10133	cnt = be32_to_cpu(pc->pname_count);
10134
10135	/* Capture FPIN frequency */
10136	phba->cgn_fpin_frequency = be32_to_cpu(pc->event_period);
10137
10138	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_ELS,
10139			"4684 FPIN Peer Congestion %s (x%x) "
10140			"Duration %d mSecs "
10141			"Detecting PN x%016llx Attached PN x%016llx "
10142			"Impacted Port Cnt %d\n",
10143			pc_evt_str, pc_evt,
10144			be32_to_cpu(pc->event_period),
10145			be64_to_cpu(pc->detecting_wwpn),
10146			be64_to_cpu(pc->attached_wwpn),
10147			cnt);
10148
10149	lpfc_display_fpin_wwpn(phba, (__be64 *)&pc->pname_list, cnt);
10150}
10151
10152/**
10153 * lpfc_els_rcv_fpin_cgn - Process an FPIN Congestion notification
10154 * @phba: Pointer to hba object.
10155 * @tlv:  Pointer to the Congestion Notification Descriptor TLV
10156 *
10157 * This function processes an FPIN Congestion Notifiction.  The notification
10158 * could be an Alarm or Warning.  This routine feeds that data into driver's
10159 * running congestion algorithm. It also processes the FPIN by
10160 * logging a message. It returns 1 to indicate deliver this message
10161 * to the upper layer or 0 to indicate don't deliver it.
10162 **/
10163static int
10164lpfc_els_rcv_fpin_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
10165{
10166	struct lpfc_cgn_info *cp;
10167	struct fc_fn_congn_desc *cgn = (struct fc_fn_congn_desc *)tlv;
10168	const char *cgn_evt_str;
10169	u32 cgn_evt;
10170	const char *cgn_sev_str;
10171	u32 cgn_sev;
10172	uint16_t value;
10173	u32 crc;
10174	bool nm_log = false;
10175	int rc = 1;
10176
10177	cgn_evt = be16_to_cpu(cgn->event_type);
10178	cgn_evt_str = lpfc_get_fpin_congn_event_nm(cgn_evt);
10179	cgn_sev = cgn->severity;
10180	cgn_sev_str = lpfc_get_fpin_congn_severity_nm(cgn_sev);
10181
10182	/* The driver only takes action on a Credit Stall or Oversubscription
10183	 * event type to engage the IO algorithm.  The driver prints an
10184	 * unmaskable message only for Lost Credit and Credit Stall.
10185	 * TODO: Still need to have definition of host action on clear,
10186	 *       lost credit and device specific event types.
10187	 */
10188	switch (cgn_evt) {
10189	case FPIN_CONGN_LOST_CREDIT:
10190		nm_log = true;
10191		break;
10192	case FPIN_CONGN_CREDIT_STALL:
10193		nm_log = true;
10194		fallthrough;
10195	case FPIN_CONGN_OVERSUBSCRIPTION:
10196		if (cgn_evt == FPIN_CONGN_OVERSUBSCRIPTION)
10197			nm_log = false;
10198		switch (cgn_sev) {
10199		case FPIN_CONGN_SEVERITY_ERROR:
10200			/* Take action here for an Alarm event */
10201			if (phba->cmf_active_mode != LPFC_CFG_OFF) {
10202				if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_ALARM) {
10203					/* Track of alarm cnt for SYNC_WQE */
10204					atomic_inc(&phba->cgn_sync_alarm_cnt);
10205				}
10206				/* Track alarm cnt for cgn_info regardless
10207				 * of whether CMF is configured for Signals
10208				 * or FPINs.
10209				 */
10210				atomic_inc(&phba->cgn_fabric_alarm_cnt);
10211				goto cleanup;
10212			}
10213			break;
10214		case FPIN_CONGN_SEVERITY_WARNING:
10215			/* Take action here for a Warning event */
10216			if (phba->cmf_active_mode != LPFC_CFG_OFF) {
10217				if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_WARN) {
10218					/* Track of warning cnt for SYNC_WQE */
10219					atomic_inc(&phba->cgn_sync_warn_cnt);
10220				}
10221				/* Track warning cnt and freq for cgn_info
10222				 * regardless of whether CMF is configured for
10223				 * Signals or FPINs.
10224				 */
10225				atomic_inc(&phba->cgn_fabric_warn_cnt);
10226cleanup:
10227				/* Save frequency in ms */
10228				phba->cgn_fpin_frequency =
10229					be32_to_cpu(cgn->event_period);
10230				value = phba->cgn_fpin_frequency;
10231				if (phba->cgn_i) {
10232					cp = (struct lpfc_cgn_info *)
10233						phba->cgn_i->virt;
10234					cp->cgn_alarm_freq =
10235						cpu_to_le16(value);
10236					cp->cgn_warn_freq =
10237						cpu_to_le16(value);
10238					crc = lpfc_cgn_calc_crc32
10239						(cp,
10240						LPFC_CGN_INFO_SZ,
10241						LPFC_CGN_CRC32_SEED);
10242					cp->cgn_info_crc = cpu_to_le32(crc);
10243				}
10244
10245				/* Don't deliver to upper layer since
10246				 * driver took action on this tlv.
10247				 */
10248				rc = 0;
10249			}
10250			break;
10251		}
10252		break;
10253	}
10254
10255	/* Change the log level to unmaskable for the following event types. */
10256	lpfc_printf_log(phba, (nm_log ? KERN_WARNING : KERN_INFO),
10257			LOG_CGN_MGMT | LOG_ELS,
10258			"4683 FPIN CONGESTION %s type %s (x%x) Event "
10259			"Duration %d mSecs\n",
10260			cgn_sev_str, cgn_evt_str, cgn_evt,
10261			be32_to_cpu(cgn->event_period));
10262	return rc;
10263}
10264
10265void
10266lpfc_els_rcv_fpin(struct lpfc_vport *vport, void *p, u32 fpin_length)
10267{
10268	struct lpfc_hba *phba = vport->phba;
10269	struct fc_els_fpin *fpin = (struct fc_els_fpin *)p;
10270	struct fc_tlv_desc *tlv, *first_tlv, *current_tlv;
10271	const char *dtag_nm;
10272	int desc_cnt = 0, bytes_remain, cnt;
10273	u32 dtag, deliver = 0;
10274	int len;
10275
10276	/* FPINs handled only if we are in the right discovery state */
10277	if (vport->port_state < LPFC_DISC_AUTH)
10278		return;
10279
10280	/* make sure there is the full fpin header */
10281	if (fpin_length < sizeof(struct fc_els_fpin))
10282		return;
10283
10284	/* Sanity check descriptor length. The desc_len value does not
10285	 * include space for the ELS command and the desc_len fields.
10286	 */
10287	len = be32_to_cpu(fpin->desc_len);
10288	if (fpin_length < len + sizeof(struct fc_els_fpin)) {
10289		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
10290				"4671 Bad ELS FPIN length %d: %d\n",
10291				len, fpin_length);
10292		return;
10293	}
10294
10295	tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
10296	first_tlv = tlv;
10297	bytes_remain = fpin_length - offsetof(struct fc_els_fpin, fpin_desc);
10298	bytes_remain = min_t(u32, bytes_remain, be32_to_cpu(fpin->desc_len));
10299
10300	/* process each descriptor separately */
10301	while (bytes_remain >= FC_TLV_DESC_HDR_SZ &&
10302	       bytes_remain >= FC_TLV_DESC_SZ_FROM_LENGTH(tlv)) {
10303		dtag = be32_to_cpu(tlv->desc_tag);
10304		switch (dtag) {
10305		case ELS_DTAG_LNK_INTEGRITY:
10306			lpfc_els_rcv_fpin_li(phba, tlv);
10307			deliver = 1;
10308			break;
10309		case ELS_DTAG_DELIVERY:
10310			lpfc_els_rcv_fpin_del(phba, tlv);
10311			deliver = 1;
10312			break;
10313		case ELS_DTAG_PEER_CONGEST:
10314			lpfc_els_rcv_fpin_peer_cgn(phba, tlv);
10315			deliver = 1;
10316			break;
10317		case ELS_DTAG_CONGESTION:
10318			deliver = lpfc_els_rcv_fpin_cgn(phba, tlv);
10319			break;
10320		default:
10321			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
10322			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
10323					"4678 unknown FPIN descriptor[%d]: "
10324					"tag x%x (%s)\n",
10325					desc_cnt, dtag, dtag_nm);
10326
10327			/* If descriptor is bad, drop the rest of the data */
10328			return;
10329		}
10330		lpfc_cgn_update_stat(phba, dtag);
10331		cnt = be32_to_cpu(tlv->desc_len);
10332
10333		/* Sanity check descriptor length. The desc_len value does not
10334		 * include space for the desc_tag and the desc_len fields.
10335		 */
10336		len -= (cnt + sizeof(struct fc_tlv_desc));
10337		if (len < 0) {
10338			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
10339			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
10340					"4672 Bad FPIN descriptor TLV length "
10341					"%d: %d %d %s\n",
10342					cnt, len, fpin_length, dtag_nm);
10343			return;
10344		}
10345
10346		current_tlv = tlv;
10347		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
10348		tlv = fc_tlv_next_desc(tlv);
10349
10350		/* Format payload such that the FPIN delivered to the
10351		 * upper layer is a single descriptor FPIN.
10352		 */
10353		if (desc_cnt)
10354			memcpy(first_tlv, current_tlv,
10355			       (cnt + sizeof(struct fc_els_fpin)));
10356
10357		/* Adjust the length so that it only reflects a
10358		 * single descriptor FPIN.
10359		 */
10360		fpin_length = cnt + sizeof(struct fc_els_fpin);
10361		fpin->desc_len = cpu_to_be32(fpin_length);
10362		fpin_length += sizeof(struct fc_els_fpin); /* the entire FPIN */
10363
10364		/* Send every descriptor individually to the upper layer */
10365		if (deliver)
10366			fc_host_fpin_rcv(lpfc_shost_from_vport(vport),
10367					 fpin_length, (char *)fpin, 0);
10368		desc_cnt++;
10369	}
10370}
10371
10372/**
10373 * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
10374 * @phba: pointer to lpfc hba data structure.
10375 * @pring: pointer to a SLI ring.
10376 * @vport: pointer to a host virtual N_Port data structure.
10377 * @elsiocb: pointer to lpfc els command iocb data structure.
10378 *
10379 * This routine is used for processing the IOCB associated with a unsolicited
10380 * event. It first determines whether there is an existing ndlp that matches
10381 * the DID from the unsolicited IOCB. If not, it will create a new one with
10382 * the DID from the unsolicited IOCB. The ELS command from the unsolicited
10383 * IOCB is then used to invoke the proper routine and to set up proper state
10384 * of the discovery state machine.
10385 **/
10386static void
10387lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10388		      struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
10389{
10390	struct lpfc_nodelist *ndlp;
10391	struct ls_rjt stat;
10392	u32 *payload, payload_len;
10393	u32 cmd = 0, did = 0, newnode, status = 0;
10394	uint8_t rjt_exp, rjt_err = 0, init_link = 0;
10395	struct lpfc_wcqe_complete *wcqe_cmpl = NULL;
10396	LPFC_MBOXQ_t *mbox;
10397
10398	if (!vport || !elsiocb->cmd_dmabuf)
10399		goto dropit;
10400
10401	newnode = 0;
10402	wcqe_cmpl = &elsiocb->wcqe_cmpl;
10403	payload = elsiocb->cmd_dmabuf->virt;
10404	if (phba->sli_rev == LPFC_SLI_REV4)
10405		payload_len = wcqe_cmpl->total_data_placed;
10406	else
10407		payload_len = elsiocb->iocb.unsli3.rcvsli3.acc_len;
10408	status = get_job_ulpstatus(phba, elsiocb);
10409	cmd = *payload;
10410	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
10411		lpfc_sli3_post_buffer(phba, pring, 1);
10412
10413	did = get_job_els_rsp64_did(phba, elsiocb);
10414	if (status) {
10415		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10416			"RCV Unsol ELS:  status:x%x/x%x did:x%x",
10417			status, get_job_word4(phba, elsiocb), did);
10418		goto dropit;
10419	}
10420
10421	/* Check to see if link went down during discovery */
10422	if (lpfc_els_chk_latt(vport))
10423		goto dropit;
10424
10425	/* Ignore traffic received during vport shutdown. */
10426	if (test_bit(FC_UNLOADING, &vport->load_flag))
10427		goto dropit;
10428
10429	/* If NPort discovery is delayed drop incoming ELS */
10430	if (test_bit(FC_DISC_DELAYED, &vport->fc_flag) &&
10431	    cmd != ELS_CMD_PLOGI)
10432		goto dropit;
10433
10434	ndlp = lpfc_findnode_did(vport, did);
10435	if (!ndlp) {
10436		/* Cannot find existing Fabric ndlp, so allocate a new one */
10437		ndlp = lpfc_nlp_init(vport, did);
10438		if (!ndlp)
10439			goto dropit;
10440		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
10441		newnode = 1;
10442		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
10443			ndlp->nlp_type |= NLP_FABRIC;
10444	} else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
10445		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
10446		newnode = 1;
10447	}
10448
10449	phba->fc_stat.elsRcvFrame++;
10450
10451	/*
10452	 * Do not process any unsolicited ELS commands
10453	 * if the ndlp is in DEV_LOSS
10454	 */
10455	if (test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
10456		goto dropit;
10457
10458	elsiocb->ndlp = lpfc_nlp_get(ndlp);
10459	if (!elsiocb->ndlp)
10460		goto dropit;
10461	elsiocb->vport = vport;
10462
10463	if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
10464		cmd &= ELS_CMD_MASK;
10465	}
10466	/* ELS command <elsCmd> received from NPORT <did> */
10467	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10468			 "0112 ELS command x%x received from NPORT x%x "
10469			 "refcnt %d Data: x%x x%lx x%x x%x\n",
10470			 cmd, did, kref_read(&ndlp->kref), vport->port_state,
10471			 vport->fc_flag, vport->fc_myDID, vport->fc_prevDID);
10472
10473	/* reject till our FLOGI completes or PLOGI assigned DID via PT2PT */
10474	if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
10475	    (cmd != ELS_CMD_FLOGI) &&
10476	    !((cmd == ELS_CMD_PLOGI) && test_bit(FC_PT2PT, &vport->fc_flag))) {
10477		rjt_err = LSRJT_LOGICAL_BSY;
10478		rjt_exp = LSEXP_NOTHING_MORE;
10479		goto lsrjt;
10480	}
10481
10482	switch (cmd) {
10483	case ELS_CMD_PLOGI:
10484		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10485			"RCV PLOGI:       did:x%x/ste:x%x flg:x%lx",
10486			did, vport->port_state, ndlp->nlp_flag);
10487
10488		phba->fc_stat.elsRcvPLOGI++;
10489		ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
10490		if (phba->sli_rev == LPFC_SLI_REV4 &&
10491		    test_bit(FC_PT2PT, &phba->pport->fc_flag)) {
10492			vport->fc_prevDID = vport->fc_myDID;
10493			/* Our DID needs to be updated before registering
10494			 * the vfi. This is done in lpfc_rcv_plogi but
10495			 * that is called after the reg_vfi.
10496			 */
10497			vport->fc_myDID =
10498				bf_get(els_rsp64_sid,
10499				       &elsiocb->wqe.xmit_els_rsp);
10500			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10501					 "3312 Remote port assigned DID x%x "
10502					 "%x\n", vport->fc_myDID,
10503					 vport->fc_prevDID);
10504		}
10505
10506		lpfc_send_els_event(vport, ndlp, payload);
10507
10508		/* If Nport discovery is delayed, reject PLOGIs */
10509		if (test_bit(FC_DISC_DELAYED, &vport->fc_flag)) {
10510			rjt_err = LSRJT_UNABLE_TPC;
10511			rjt_exp = LSEXP_NOTHING_MORE;
10512			break;
10513		}
10514
10515		if (vport->port_state < LPFC_DISC_AUTH) {
10516			if (!test_bit(FC_PT2PT, &phba->pport->fc_flag) ||
10517			    test_bit(FC_PT2PT_PLOGI, &phba->pport->fc_flag)) {
10518				rjt_err = LSRJT_UNABLE_TPC;
10519				rjt_exp = LSEXP_NOTHING_MORE;
10520				break;
10521			}
10522		}
10523
10524		lpfc_disc_state_machine(vport, ndlp, elsiocb,
10525					NLP_EVT_RCV_PLOGI);
10526
10527		break;
10528	case ELS_CMD_FLOGI:
10529		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10530			"RCV FLOGI:       did:x%x/ste:x%x flg:x%lx",
10531			did, vport->port_state, ndlp->nlp_flag);
10532
10533		phba->fc_stat.elsRcvFLOGI++;
10534
10535		/* If the driver believes fabric discovery is done and is ready,
10536		 * bounce the link.  There is some descrepancy.
10537		 */
10538		if (vport->port_state >= LPFC_LOCAL_CFG_LINK &&
10539		    test_bit(FC_PT2PT, &vport->fc_flag) &&
10540		    vport->rcv_flogi_cnt >= 1) {
10541			rjt_err = LSRJT_LOGICAL_BSY;
10542			rjt_exp = LSEXP_NOTHING_MORE;
10543			init_link++;
10544			goto lsrjt;
10545		}
10546
10547		lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
10548		/* retain node if our response is deferred */
10549		if (phba->defer_flogi_acc.flag)
10550			break;
10551		if (newnode)
10552			lpfc_disc_state_machine(vport, ndlp, NULL,
10553					NLP_EVT_DEVICE_RM);
10554		break;
10555	case ELS_CMD_LOGO:
10556		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10557			"RCV LOGO:        did:x%x/ste:x%x flg:x%lx",
10558			did, vport->port_state, ndlp->nlp_flag);
10559
10560		phba->fc_stat.elsRcvLOGO++;
10561		lpfc_send_els_event(vport, ndlp, payload);
10562		if (vport->port_state < LPFC_DISC_AUTH) {
10563			rjt_err = LSRJT_UNABLE_TPC;
10564			rjt_exp = LSEXP_NOTHING_MORE;
10565			break;
10566		}
10567		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
10568		if (newnode)
10569			lpfc_disc_state_machine(vport, ndlp, NULL,
10570					NLP_EVT_DEVICE_RM);
10571		break;
10572	case ELS_CMD_PRLO:
10573		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10574			"RCV PRLO:        did:x%x/ste:x%x flg:x%lx",
10575			did, vport->port_state, ndlp->nlp_flag);
10576
10577		phba->fc_stat.elsRcvPRLO++;
10578		lpfc_send_els_event(vport, ndlp, payload);
10579		if (vport->port_state < LPFC_DISC_AUTH) {
10580			rjt_err = LSRJT_UNABLE_TPC;
10581			rjt_exp = LSEXP_NOTHING_MORE;
10582			break;
10583		}
10584		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
10585		break;
10586	case ELS_CMD_LCB:
10587		phba->fc_stat.elsRcvLCB++;
10588		lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
10589		break;
10590	case ELS_CMD_RDP:
10591		phba->fc_stat.elsRcvRDP++;
10592		lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
10593		break;
10594	case ELS_CMD_RSCN:
10595		phba->fc_stat.elsRcvRSCN++;
10596		lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
10597		if (newnode)
10598			lpfc_disc_state_machine(vport, ndlp, NULL,
10599					NLP_EVT_DEVICE_RM);
10600		break;
10601	case ELS_CMD_ADISC:
10602		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10603			"RCV ADISC:       did:x%x/ste:x%x flg:x%lx",
10604			did, vport->port_state, ndlp->nlp_flag);
10605
10606		lpfc_send_els_event(vport, ndlp, payload);
10607		phba->fc_stat.elsRcvADISC++;
10608		if (vport->port_state < LPFC_DISC_AUTH) {
10609			rjt_err = LSRJT_UNABLE_TPC;
10610			rjt_exp = LSEXP_NOTHING_MORE;
10611			break;
10612		}
10613		lpfc_disc_state_machine(vport, ndlp, elsiocb,
10614					NLP_EVT_RCV_ADISC);
10615		break;
10616	case ELS_CMD_PDISC:
10617		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10618			"RCV PDISC:       did:x%x/ste:x%x flg:x%lx",
10619			did, vport->port_state, ndlp->nlp_flag);
10620
10621		phba->fc_stat.elsRcvPDISC++;
10622		if (vport->port_state < LPFC_DISC_AUTH) {
10623			rjt_err = LSRJT_UNABLE_TPC;
10624			rjt_exp = LSEXP_NOTHING_MORE;
10625			break;
10626		}
10627		lpfc_disc_state_machine(vport, ndlp, elsiocb,
10628					NLP_EVT_RCV_PDISC);
10629		break;
10630	case ELS_CMD_FARPR:
10631		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10632			"RCV FARPR:       did:x%x/ste:x%x flg:x%lx",
10633			did, vport->port_state, ndlp->nlp_flag);
10634
10635		phba->fc_stat.elsRcvFARPR++;
10636		lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
10637		break;
10638	case ELS_CMD_FARP:
10639		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10640			"RCV FARP:        did:x%x/ste:x%x flg:x%lx",
10641			did, vport->port_state, ndlp->nlp_flag);
10642
10643		phba->fc_stat.elsRcvFARP++;
10644		lpfc_els_rcv_farp(vport, elsiocb, ndlp);
10645		break;
10646	case ELS_CMD_FAN:
10647		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10648			"RCV FAN:         did:x%x/ste:x%x flg:x%lx",
10649			did, vport->port_state, ndlp->nlp_flag);
10650
10651		phba->fc_stat.elsRcvFAN++;
10652		lpfc_els_rcv_fan(vport, elsiocb, ndlp);
10653		break;
10654	case ELS_CMD_PRLI:
10655	case ELS_CMD_NVMEPRLI:
10656		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10657			"RCV PRLI:        did:x%x/ste:x%x flg:x%lx",
10658			did, vport->port_state, ndlp->nlp_flag);
10659
10660		phba->fc_stat.elsRcvPRLI++;
10661		if ((vport->port_state < LPFC_DISC_AUTH) &&
10662		    test_bit(FC_FABRIC, &vport->fc_flag)) {
10663			rjt_err = LSRJT_UNABLE_TPC;
10664			rjt_exp = LSEXP_NOTHING_MORE;
10665			break;
10666		}
10667		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
10668		break;
10669	case ELS_CMD_LIRR:
10670		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10671			"RCV LIRR:        did:x%x/ste:x%x flg:x%lx",
10672			did, vport->port_state, ndlp->nlp_flag);
10673
10674		phba->fc_stat.elsRcvLIRR++;
10675		lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
10676		if (newnode)
10677			lpfc_disc_state_machine(vport, ndlp, NULL,
10678					NLP_EVT_DEVICE_RM);
10679		break;
10680	case ELS_CMD_RLS:
10681		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10682			"RCV RLS:         did:x%x/ste:x%x flg:x%lx",
10683			did, vport->port_state, ndlp->nlp_flag);
10684
10685		phba->fc_stat.elsRcvRLS++;
10686		lpfc_els_rcv_rls(vport, elsiocb, ndlp);
10687		if (newnode)
10688			lpfc_disc_state_machine(vport, ndlp, NULL,
10689					NLP_EVT_DEVICE_RM);
10690		break;
10691	case ELS_CMD_RPL:
10692		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10693			"RCV RPL:         did:x%x/ste:x%x flg:x%lx",
10694			did, vport->port_state, ndlp->nlp_flag);
10695
10696		phba->fc_stat.elsRcvRPL++;
10697		lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
10698		if (newnode)
10699			lpfc_disc_state_machine(vport, ndlp, NULL,
10700					NLP_EVT_DEVICE_RM);
10701		break;
10702	case ELS_CMD_RNID:
10703		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10704			"RCV RNID:        did:x%x/ste:x%x flg:x%lx",
10705			did, vport->port_state, ndlp->nlp_flag);
10706
10707		phba->fc_stat.elsRcvRNID++;
10708		lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
10709		if (newnode)
10710			lpfc_disc_state_machine(vport, ndlp, NULL,
10711					NLP_EVT_DEVICE_RM);
10712		break;
10713	case ELS_CMD_RTV:
10714		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10715			"RCV RTV:        did:x%x/ste:x%x flg:x%lx",
10716			did, vport->port_state, ndlp->nlp_flag);
10717		phba->fc_stat.elsRcvRTV++;
10718		lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
10719		if (newnode)
10720			lpfc_disc_state_machine(vport, ndlp, NULL,
10721					NLP_EVT_DEVICE_RM);
10722		break;
10723	case ELS_CMD_RRQ:
10724		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10725			"RCV RRQ:         did:x%x/ste:x%x flg:x%lx",
10726			did, vport->port_state, ndlp->nlp_flag);
10727
10728		phba->fc_stat.elsRcvRRQ++;
10729		lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
10730		if (newnode)
10731			lpfc_disc_state_machine(vport, ndlp, NULL,
10732					NLP_EVT_DEVICE_RM);
10733		break;
10734	case ELS_CMD_ECHO:
10735		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10736			"RCV ECHO:        did:x%x/ste:x%x flg:x%lx",
10737			did, vport->port_state, ndlp->nlp_flag);
10738
10739		phba->fc_stat.elsRcvECHO++;
10740		lpfc_els_rcv_echo(vport, elsiocb, ndlp);
10741		if (newnode)
10742			lpfc_disc_state_machine(vport, ndlp, NULL,
10743					NLP_EVT_DEVICE_RM);
10744		break;
10745	case ELS_CMD_REC:
10746		/* receive this due to exchange closed */
10747		rjt_err = LSRJT_UNABLE_TPC;
10748		rjt_exp = LSEXP_INVALID_OX_RX;
10749		break;
10750	case ELS_CMD_FPIN:
10751		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10752				      "RCV FPIN:       did:x%x/ste:x%x "
10753				      "flg:x%lx",
10754				      did, vport->port_state, ndlp->nlp_flag);
10755
10756		lpfc_els_rcv_fpin(vport, (struct fc_els_fpin *)payload,
10757				  payload_len);
10758
10759		/* There are no replies, so no rjt codes */
10760		break;
10761	case ELS_CMD_EDC:
10762		lpfc_els_rcv_edc(vport, elsiocb, ndlp);
10763		break;
10764	case ELS_CMD_RDF:
10765		phba->fc_stat.elsRcvRDF++;
10766		/* Accept RDF only from fabric controller */
10767		if (did != Fabric_Cntl_DID) {
10768			lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
10769					 "1115 Received RDF from invalid DID "
10770					 "x%x\n", did);
10771			rjt_err = LSRJT_PROTOCOL_ERR;
10772			rjt_exp = LSEXP_NOTHING_MORE;
10773			goto lsrjt;
10774		}
10775
10776		lpfc_els_rcv_rdf(vport, elsiocb, ndlp);
10777		break;
10778	default:
10779		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10780			"RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
10781			cmd, did, vport->port_state);
10782
10783		/* Unsupported ELS command, reject */
10784		rjt_err = LSRJT_CMD_UNSUPPORTED;
10785		rjt_exp = LSEXP_NOTHING_MORE;
10786
10787		/* Unknown ELS command <elsCmd> received from NPORT <did> */
10788		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
10789				 "0115 Unknown ELS command x%x "
10790				 "received from NPORT x%x\n", cmd, did);
10791		if (newnode)
10792			lpfc_disc_state_machine(vport, ndlp, NULL,
10793					NLP_EVT_DEVICE_RM);
10794		break;
10795	}
10796
10797lsrjt:
10798	/* check if need to LS_RJT received ELS cmd */
10799	if (rjt_err) {
10800		memset(&stat, 0, sizeof(stat));
10801		stat.un.b.lsRjtRsnCode = rjt_err;
10802		stat.un.b.lsRjtRsnCodeExp = rjt_exp;
10803		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
10804				    NULL);
10805		/* Remove the reference from above for new nodes. */
10806		if (newnode)
10807			lpfc_disc_state_machine(vport, ndlp, NULL,
10808					NLP_EVT_DEVICE_RM);
10809	}
10810
10811	/* Release the reference on this elsiocb, not the ndlp. */
10812	lpfc_nlp_put(elsiocb->ndlp);
10813	elsiocb->ndlp = NULL;
10814
10815	/* Special case.  Driver received an unsolicited command that
10816	 * unsupportable given the driver's current state.  Reset the
10817	 * link and start over.
10818	 */
10819	if (init_link) {
10820		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10821		if (!mbox)
10822			return;
10823		lpfc_linkdown(phba);
10824		lpfc_init_link(phba, mbox,
10825			       phba->cfg_topology,
10826			       phba->cfg_link_speed);
10827		mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
10828		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10829		mbox->vport = vport;
10830		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
10831		    MBX_NOT_FINISHED)
10832			mempool_free(mbox, phba->mbox_mem_pool);
10833	}
10834
10835	return;
10836
10837dropit:
10838	if (vport && !test_bit(FC_UNLOADING, &vport->load_flag))
10839		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10840			"0111 Dropping received ELS cmd "
10841			"Data: x%x x%x x%x x%x\n",
10842			cmd, status, get_job_word4(phba, elsiocb), did);
10843
10844	phba->fc_stat.elsRcvDrop++;
10845}
10846
10847/**
10848 * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
10849 * @phba: pointer to lpfc hba data structure.
10850 * @pring: pointer to a SLI ring.
10851 * @elsiocb: pointer to lpfc els iocb data structure.
10852 *
10853 * This routine is used to process an unsolicited event received from a SLI
10854 * (Service Level Interface) ring. The actual processing of the data buffer
10855 * associated with the unsolicited event is done by invoking the routine
10856 * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
10857 * SLI ring on which the unsolicited event was received.
10858 **/
10859void
10860lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10861		     struct lpfc_iocbq *elsiocb)
10862{
10863	struct lpfc_vport *vport = elsiocb->vport;
10864	u32 ulp_command, status, parameter, bde_count = 0;
10865	IOCB_t *icmd;
10866	struct lpfc_wcqe_complete *wcqe_cmpl = NULL;
10867	struct lpfc_dmabuf *bdeBuf1 = elsiocb->cmd_dmabuf;
10868	struct lpfc_dmabuf *bdeBuf2 = elsiocb->bpl_dmabuf;
10869	dma_addr_t paddr;
10870
10871	elsiocb->cmd_dmabuf = NULL;
10872	elsiocb->rsp_dmabuf = NULL;
10873	elsiocb->bpl_dmabuf = NULL;
10874
10875	wcqe_cmpl = &elsiocb->wcqe_cmpl;
10876	ulp_command = get_job_cmnd(phba, elsiocb);
10877	status = get_job_ulpstatus(phba, elsiocb);
10878	parameter = get_job_word4(phba, elsiocb);
10879	if (phba->sli_rev == LPFC_SLI_REV4)
10880		bde_count = wcqe_cmpl->word3;
10881	else
10882		bde_count = elsiocb->iocb.ulpBdeCount;
10883
10884	if (status == IOSTAT_NEED_BUFFER) {
10885		lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
10886	} else if (status == IOSTAT_LOCAL_REJECT &&
10887		   (parameter & IOERR_PARAM_MASK) ==
10888		   IOERR_RCV_BUFFER_WAITING) {
10889		phba->fc_stat.NoRcvBuf++;
10890		/* Not enough posted buffers; Try posting more buffers */
10891		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
10892			lpfc_sli3_post_buffer(phba, pring, 0);
10893		return;
10894	}
10895
10896	if (phba->sli_rev == LPFC_SLI_REV3) {
10897		icmd = &elsiocb->iocb;
10898		if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
10899		    (ulp_command == CMD_IOCB_RCV_ELS64_CX ||
10900		     ulp_command == CMD_IOCB_RCV_SEQ64_CX)) {
10901			if (icmd->unsli3.rcvsli3.vpi == 0xffff)
10902				vport = phba->pport;
10903			else
10904				vport = lpfc_find_vport_by_vpid(phba,
10905						icmd->unsli3.rcvsli3.vpi);
10906		}
10907	}
10908
10909	/* If there are no BDEs associated
10910	 * with this IOCB, there is nothing to do.
10911	 */
10912	if (bde_count == 0)
10913		return;
10914
10915	/* Account for SLI2 or SLI3 and later unsolicited buffering */
10916	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
10917		elsiocb->cmd_dmabuf = bdeBuf1;
10918		if (bde_count == 2)
10919			elsiocb->bpl_dmabuf = bdeBuf2;
10920	} else {
10921		icmd = &elsiocb->iocb;
10922		paddr = getPaddr(icmd->un.cont64[0].addrHigh,
10923				 icmd->un.cont64[0].addrLow);
10924		elsiocb->cmd_dmabuf = lpfc_sli_ringpostbuf_get(phba, pring,
10925							       paddr);
10926		if (bde_count == 2) {
10927			paddr = getPaddr(icmd->un.cont64[1].addrHigh,
10928					 icmd->un.cont64[1].addrLow);
10929			elsiocb->bpl_dmabuf = lpfc_sli_ringpostbuf_get(phba,
10930									pring,
10931									paddr);
10932		}
10933	}
10934
10935	lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
10936	/*
10937	 * The different unsolicited event handlers would tell us
10938	 * if they are done with "mp" by setting cmd_dmabuf/bpl_dmabuf to NULL.
10939	 */
10940	if (elsiocb->cmd_dmabuf) {
10941		lpfc_in_buf_free(phba, elsiocb->cmd_dmabuf);
10942		elsiocb->cmd_dmabuf = NULL;
10943	}
10944
10945	if (elsiocb->bpl_dmabuf) {
10946		lpfc_in_buf_free(phba, elsiocb->bpl_dmabuf);
10947		elsiocb->bpl_dmabuf = NULL;
10948	}
10949
10950}
10951
10952static void
10953lpfc_start_fdmi(struct lpfc_vport *vport)
10954{
10955	struct lpfc_nodelist *ndlp;
10956
10957	/* If this is the first time, allocate an ndlp and initialize
10958	 * it. Otherwise, make sure the node is enabled and then do the
10959	 * login.
10960	 */
10961	ndlp = lpfc_findnode_did(vport, FDMI_DID);
10962	if (!ndlp) {
10963		ndlp = lpfc_nlp_init(vport, FDMI_DID);
10964		if (ndlp) {
10965			ndlp->nlp_type |= NLP_FABRIC;
10966		} else {
10967			return;
10968		}
10969	}
10970
10971	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
10972	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
10973}
10974
10975/**
10976 * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
10977 * @phba: pointer to lpfc hba data structure.
10978 * @vport: pointer to a virtual N_Port data structure.
10979 *
10980 * This routine issues a Port Login (PLOGI) to the Name Server with
10981 * State Change Request (SCR) for a @vport. This routine will create an
10982 * ndlp for the Name Server associated to the @vport if such node does
10983 * not already exist. The PLOGI to Name Server is issued by invoking the
10984 * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
10985 * (FDMI) is configured to the @vport, a FDMI node will be created and
10986 * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
10987 **/
10988void
10989lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
10990{
10991	struct lpfc_nodelist *ndlp;
10992
10993	/*
10994	 * If lpfc_delay_discovery parameter is set and the clean address
10995	 * bit is cleared and fc fabric parameters chenged, delay FC NPort
10996	 * discovery.
10997	 */
10998	if (test_bit(FC_DISC_DELAYED, &vport->fc_flag)) {
10999		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11000				 "3334 Delay fc port discovery for %d secs\n",
11001				 phba->fc_ratov);
11002		mod_timer(&vport->delayed_disc_tmo,
11003			jiffies + secs_to_jiffies(phba->fc_ratov));
11004		return;
11005	}
11006
11007	ndlp = lpfc_findnode_did(vport, NameServer_DID);
11008	if (!ndlp) {
11009		ndlp = lpfc_nlp_init(vport, NameServer_DID);
11010		if (!ndlp) {
11011			if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
11012				lpfc_disc_start(vport);
11013				return;
11014			}
11015			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11016			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11017					 "0251 NameServer login: no memory\n");
11018			return;
11019		}
11020	}
11021
11022	ndlp->nlp_type |= NLP_FABRIC;
11023
11024	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
11025
11026	if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
11027		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11028		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11029				 "0252 Cannot issue NameServer login\n");
11030		return;
11031	}
11032
11033	if ((phba->cfg_enable_SmartSAN ||
11034	     phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT) &&
11035	    test_bit(FC_ALLOW_FDMI, &vport->load_flag))
11036		lpfc_start_fdmi(vport);
11037}
11038
11039/**
11040 * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
11041 * @phba: pointer to lpfc hba data structure.
11042 * @pmb: pointer to the driver internal queue element for mailbox command.
11043 *
11044 * This routine is the completion callback function to register new vport
11045 * mailbox command. If the new vport mailbox command completes successfully,
11046 * the fabric registration login shall be performed on physical port (the
11047 * new vport created is actually a physical port, with VPI 0) or the port
11048 * login to Name Server for State Change Request (SCR) will be performed
11049 * on virtual port (real virtual port, with VPI greater than 0).
11050 **/
11051static void
11052lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
11053{
11054	struct lpfc_vport *vport = pmb->vport;
11055	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
11056	struct lpfc_nodelist *ndlp = pmb->ctx_ndlp;
11057	MAILBOX_t *mb = &pmb->u.mb;
11058	int rc;
11059
11060	clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11061
11062	if (mb->mbxStatus) {
11063		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11064				"0915 Register VPI failed : Status: x%x"
11065				" upd bit: x%x \n", mb->mbxStatus,
11066				 mb->un.varRegVpi.upd);
11067		if (phba->sli_rev == LPFC_SLI_REV4 &&
11068			mb->un.varRegVpi.upd)
11069			goto mbox_err_exit ;
11070
11071		switch (mb->mbxStatus) {
11072		case 0x11:	/* unsupported feature */
11073		case 0x9603:	/* max_vpi exceeded */
11074		case 0x9602:	/* Link event since CLEAR_LA */
11075			/* giving up on vport registration */
11076			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11077			clear_bit(FC_FABRIC, &vport->fc_flag);
11078			clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
11079			lpfc_can_disctmo(vport);
11080			break;
11081		/* If reg_vpi fail with invalid VPI status, re-init VPI */
11082		case 0x20:
11083			set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11084			lpfc_init_vpi(phba, pmb, vport->vpi);
11085			pmb->vport = vport;
11086			pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
11087			rc = lpfc_sli_issue_mbox(phba, pmb,
11088				MBX_NOWAIT);
11089			if (rc == MBX_NOT_FINISHED) {
11090				lpfc_printf_vlog(vport, KERN_ERR,
11091						 LOG_TRACE_EVENT,
11092					"2732 Failed to issue INIT_VPI"
11093					" mailbox command\n");
11094			} else {
11095				lpfc_nlp_put(ndlp);
11096				return;
11097			}
11098			fallthrough;
11099		default:
11100			/* Try to recover from this error */
11101			if (phba->sli_rev == LPFC_SLI_REV4)
11102				lpfc_sli4_unreg_all_rpis(vport);
11103			lpfc_mbx_unreg_vpi(vport);
11104			set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11105			if (mb->mbxStatus == MBX_NOT_FINISHED)
11106				break;
11107			if ((vport->port_type == LPFC_PHYSICAL_PORT) &&
11108			    !test_bit(FC_LOGO_RCVD_DID_CHNG, &vport->fc_flag)) {
11109				if (phba->sli_rev == LPFC_SLI_REV4)
11110					lpfc_issue_init_vfi(vport);
11111				else
11112					lpfc_initial_flogi(vport);
11113			} else {
11114				lpfc_initial_fdisc(vport);
11115			}
11116			break;
11117		}
11118	} else {
11119		spin_lock_irq(shost->host_lock);
11120		vport->vpi_state |= LPFC_VPI_REGISTERED;
11121		spin_unlock_irq(shost->host_lock);
11122		if (vport == phba->pport) {
11123			if (phba->sli_rev < LPFC_SLI_REV4)
11124				lpfc_issue_fabric_reglogin(vport);
11125			else {
11126				/*
11127				 * If the physical port is instantiated using
11128				 * FDISC, do not start vport discovery.
11129				 */
11130				if (vport->port_state != LPFC_FDISC)
11131					lpfc_start_fdiscs(phba);
11132				lpfc_do_scr_ns_plogi(phba, vport);
11133			}
11134		} else {
11135			lpfc_do_scr_ns_plogi(phba, vport);
11136		}
11137	}
11138mbox_err_exit:
11139	/* Now, we decrement the ndlp reference count held for this
11140	 * callback function
11141	 */
11142	lpfc_nlp_put(ndlp);
11143
11144	mempool_free(pmb, phba->mbox_mem_pool);
11145
11146	/* reinitialize the VMID datastructure before returning.
11147	 * this is specifically for vport
11148	 */
11149	if (lpfc_is_vmid_enabled(phba))
11150		lpfc_reinit_vmid(vport);
11151	vport->vmid_flag = vport->phba->pport->vmid_flag;
11152
11153	return;
11154}
11155
11156/**
11157 * lpfc_register_new_vport - Register a new vport with a HBA
11158 * @phba: pointer to lpfc hba data structure.
11159 * @vport: pointer to a host virtual N_Port data structure.
11160 * @ndlp: pointer to a node-list data structure.
11161 *
11162 * This routine registers the @vport as a new virtual port with a HBA.
11163 * It is done through a registering vpi mailbox command.
11164 **/
11165void
11166lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
11167			struct lpfc_nodelist *ndlp)
11168{
11169	LPFC_MBOXQ_t *mbox;
11170
11171	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11172	if (mbox) {
11173		lpfc_reg_vpi(vport, mbox);
11174		mbox->vport = vport;
11175		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
11176		if (!mbox->ctx_ndlp) {
11177			mempool_free(mbox, phba->mbox_mem_pool);
11178			goto mbox_err_exit;
11179		}
11180
11181		mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
11182		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
11183		    == MBX_NOT_FINISHED) {
11184			/* mailbox command not success, decrement ndlp
11185			 * reference count for this command
11186			 */
11187			lpfc_nlp_put(ndlp);
11188			mempool_free(mbox, phba->mbox_mem_pool);
11189
11190			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11191				"0253 Register VPI: Can't send mbox\n");
11192			goto mbox_err_exit;
11193		}
11194	} else {
11195		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11196				 "0254 Register VPI: no memory\n");
11197		goto mbox_err_exit;
11198	}
11199	return;
11200
11201mbox_err_exit:
11202	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11203	clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11204	return;
11205}
11206
11207/**
11208 * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
11209 * @phba: pointer to lpfc hba data structure.
11210 *
11211 * This routine cancels the retry delay timers to all the vports.
11212 **/
11213void
11214lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
11215{
11216	struct lpfc_vport **vports;
11217	struct lpfc_nodelist *ndlp;
11218	uint32_t link_state;
11219	int i;
11220
11221	/* Treat this failure as linkdown for all vports */
11222	link_state = phba->link_state;
11223	lpfc_linkdown(phba);
11224	phba->link_state = link_state;
11225
11226	vports = lpfc_create_vport_work_array(phba);
11227
11228	if (vports) {
11229		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11230			ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
11231			if (ndlp)
11232				lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
11233			lpfc_els_flush_cmd(vports[i]);
11234		}
11235		lpfc_destroy_vport_work_array(phba, vports);
11236	}
11237}
11238
11239/**
11240 * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
11241 * @phba: pointer to lpfc hba data structure.
11242 *
11243 * This routine abort all pending discovery commands and
11244 * start a timer to retry FLOGI for the physical port
11245 * discovery.
11246 **/
11247void
11248lpfc_retry_pport_discovery(struct lpfc_hba *phba)
11249{
11250	struct lpfc_nodelist *ndlp;
11251
11252	/* Cancel the all vports retry delay retry timers */
11253	lpfc_cancel_all_vport_retry_delay_timer(phba);
11254
11255	/* If fabric require FLOGI, then re-instantiate physical login */
11256	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
11257	if (!ndlp)
11258		return;
11259
11260	mod_timer(&ndlp->nlp_delayfunc, jiffies + secs_to_jiffies(1));
11261	set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
11262	ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
11263	phba->pport->port_state = LPFC_FLOGI;
11264	return;
11265}
11266
11267/**
11268 * lpfc_fabric_login_reqd - Check if FLOGI required.
11269 * @phba: pointer to lpfc hba data structure.
11270 * @cmdiocb: pointer to FDISC command iocb.
11271 * @rspiocb: pointer to FDISC response iocb.
11272 *
11273 * This routine checks if a FLOGI is reguired for FDISC
11274 * to succeed.
11275 **/
11276static int
11277lpfc_fabric_login_reqd(struct lpfc_hba *phba,
11278		struct lpfc_iocbq *cmdiocb,
11279		struct lpfc_iocbq *rspiocb)
11280{
11281	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
11282	u32 ulp_word4 = get_job_word4(phba, rspiocb);
11283
11284	if (ulp_status != IOSTAT_FABRIC_RJT ||
11285	    ulp_word4 != RJT_LOGIN_REQUIRED)
11286		return 0;
11287	else
11288		return 1;
11289}
11290
11291/**
11292 * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
11293 * @phba: pointer to lpfc hba data structure.
11294 * @cmdiocb: pointer to lpfc command iocb data structure.
11295 * @rspiocb: pointer to lpfc response iocb data structure.
11296 *
11297 * This routine is the completion callback function to a Fabric Discover
11298 * (FDISC) ELS command. Since all the FDISC ELS commands are issued
11299 * single threaded, each FDISC completion callback function will reset
11300 * the discovery timer for all vports such that the timers will not get
11301 * unnecessary timeout. The function checks the FDISC IOCB status. If error
11302 * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
11303 * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
11304 * assigned to the vport has been changed with the completion of the FDISC
11305 * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
11306 * are unregistered from the HBA, and then the lpfc_register_new_vport()
11307 * routine is invoked to register new vport with the HBA. Otherwise, the
11308 * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
11309 * Server for State Change Request (SCR).
11310 **/
11311static void
11312lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11313		    struct lpfc_iocbq *rspiocb)
11314{
11315	struct lpfc_vport *vport = cmdiocb->vport;
11316	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
11317	struct lpfc_nodelist *np;
11318	struct lpfc_nodelist *next_np;
11319	struct lpfc_iocbq *piocb;
11320	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf, *prsp;
11321	struct serv_parm *sp;
11322	uint8_t fabric_param_changed;
11323	u32 ulp_status, ulp_word4;
11324
11325	ulp_status = get_job_ulpstatus(phba, rspiocb);
11326	ulp_word4 = get_job_word4(phba, rspiocb);
11327
11328	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
11329			 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
11330			 ulp_status, ulp_word4,
11331			 vport->fc_prevDID);
11332	/* Since all FDISCs are being single threaded, we
11333	 * must reset the discovery timer for ALL vports
11334	 * waiting to send FDISC when one completes.
11335	 */
11336	list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
11337		lpfc_set_disctmo(piocb->vport);
11338	}
11339
11340	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11341		"FDISC cmpl:      status:x%x/x%x prevdid:x%x",
11342		ulp_status, ulp_word4, vport->fc_prevDID);
11343
11344	if (ulp_status) {
11345
11346		if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
11347			lpfc_retry_pport_discovery(phba);
11348			goto out;
11349		}
11350
11351		/* Check for retry */
11352		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
11353			goto out;
11354		/* Warn FDISC status */
11355		lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
11356			      "0126 FDISC cmpl status: x%x/x%x)\n",
11357			      ulp_status, ulp_word4);
11358
11359		/* drop initial reference */
11360		if (!test_and_set_bit(NLP_DROPPED, &ndlp->nlp_flag))
11361			lpfc_nlp_put(ndlp);
11362
11363		goto fdisc_failed;
11364	}
11365
11366	lpfc_check_nlp_post_devloss(vport, ndlp);
11367
11368	clear_bit(FC_VPORT_CVL_RCVD, &vport->fc_flag);
11369	clear_bit(FC_VPORT_LOGO_RCVD, &vport->fc_flag);
11370	set_bit(FC_FABRIC, &vport->fc_flag);
11371	if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
11372		set_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
11373
11374	vport->fc_myDID = ulp_word4 & Mask_DID;
11375	lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
11376	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
11377	if (!prsp)
11378		goto out;
11379	if (!lpfc_is_els_acc_rsp(prsp))
11380		goto out;
11381
11382	sp = prsp->virt + sizeof(uint32_t);
11383	fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
11384	memcpy(&vport->fabric_portname, &sp->portName,
11385		sizeof(struct lpfc_name));
11386	memcpy(&vport->fabric_nodename, &sp->nodeName,
11387		sizeof(struct lpfc_name));
11388	if (fabric_param_changed &&
11389		!test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
11390		/* If our NportID changed, we need to ensure all
11391		 * remaining NPORTs get unreg_login'ed so we can
11392		 * issue unreg_vpi.
11393		 */
11394		list_for_each_entry_safe(np, next_np,
11395			&vport->fc_nodes, nlp_listp) {
11396			if ((np->nlp_state != NLP_STE_NPR_NODE) ||
11397			    !test_bit(NLP_NPR_ADISC, &np->nlp_flag))
11398				continue;
11399			clear_bit(NLP_NPR_ADISC, &np->nlp_flag);
11400			lpfc_unreg_rpi(vport, np);
11401		}
11402		lpfc_cleanup_pending_mbox(vport);
11403
11404		if (phba->sli_rev == LPFC_SLI_REV4)
11405			lpfc_sli4_unreg_all_rpis(vport);
11406
11407		lpfc_mbx_unreg_vpi(vport);
11408		set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11409		if (phba->sli_rev == LPFC_SLI_REV4)
11410			set_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag);
11411		else
11412			set_bit(FC_LOGO_RCVD_DID_CHNG, &vport->fc_flag);
11413	} else if ((phba->sli_rev == LPFC_SLI_REV4) &&
11414		   !test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
11415		/*
11416		 * Driver needs to re-reg VPI in order for f/w
11417		 * to update the MAC address.
11418		 */
11419		lpfc_register_new_vport(phba, vport, ndlp);
11420		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
11421		goto out;
11422	}
11423
11424	if (test_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag))
11425		lpfc_issue_init_vpi(vport);
11426	else if (test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag))
11427		lpfc_register_new_vport(phba, vport, ndlp);
11428	else
11429		lpfc_do_scr_ns_plogi(phba, vport);
11430
11431	/* The FDISC completed successfully. Move the fabric ndlp to
11432	 * UNMAPPED state and register with the transport.
11433	 */
11434	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
11435	goto out;
11436
11437fdisc_failed:
11438	if (vport->fc_vport &&
11439	    (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS))
11440		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11441	/* Cancel discovery timer */
11442	lpfc_can_disctmo(vport);
11443out:
11444	lpfc_els_free_iocb(phba, cmdiocb);
11445	lpfc_nlp_put(ndlp);
11446}
11447
11448/**
11449 * lpfc_issue_els_fdisc - Issue a fdisc iocb command
11450 * @vport: pointer to a virtual N_Port data structure.
11451 * @ndlp: pointer to a node-list data structure.
11452 * @retry: number of retries to the command IOCB.
11453 *
11454 * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
11455 * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
11456 * routine to issue the IOCB, which makes sure only one outstanding fabric
11457 * IOCB will be sent off HBA at any given time.
11458 *
11459 * Note that the ndlp reference count will be incremented by 1 for holding the
11460 * ndlp and the reference to ndlp will be stored into the ndlp field of
11461 * the IOCB for the completion callback function to the FDISC ELS command.
11462 *
11463 * Return code
11464 *   0 - Successfully issued fdisc iocb command
11465 *   1 - Failed to issue fdisc iocb command
11466 **/
11467static int
11468lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
11469		     uint8_t retry)
11470{
11471	struct lpfc_hba *phba = vport->phba;
11472	IOCB_t *icmd;
11473	union lpfc_wqe128 *wqe = NULL;
11474	struct lpfc_iocbq *elsiocb;
11475	struct serv_parm *sp;
11476	uint8_t *pcmd;
11477	uint16_t cmdsize;
11478	int did = ndlp->nlp_DID;
11479	int rc;
11480
11481	vport->port_state = LPFC_FDISC;
11482	vport->fc_myDID = 0;
11483	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
11484	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
11485				     ELS_CMD_FDISC);
11486	if (!elsiocb) {
11487		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11488		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11489				 "0255 Issue FDISC: no IOCB\n");
11490		return 1;
11491	}
11492
11493	if (phba->sli_rev == LPFC_SLI_REV4) {
11494		wqe = &elsiocb->wqe;
11495		bf_set(els_req64_sid, &wqe->els_req, 0);
11496		bf_set(els_req64_sp, &wqe->els_req, 1);
11497	} else {
11498		icmd = &elsiocb->iocb;
11499		icmd->un.elsreq64.myID = 0;
11500		icmd->un.elsreq64.fl = 1;
11501		icmd->ulpCt_h = 1;
11502		icmd->ulpCt_l = 0;
11503	}
11504
11505	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
11506	*((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
11507	pcmd += sizeof(uint32_t); /* CSP Word 1 */
11508	memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
11509	sp = (struct serv_parm *) pcmd;
11510	/* Setup CSPs accordingly for Fabric */
11511	sp->cmn.e_d_tov = 0;
11512	sp->cmn.w2.r_a_tov = 0;
11513	sp->cmn.virtual_fabric_support = 0;
11514	sp->cls1.classValid = 0;
11515	sp->cls2.seqDelivery = 1;
11516	sp->cls3.seqDelivery = 1;
11517
11518	/* Fill out Auxiliary Parameter Data */
11519	if (phba->pni) {
11520		sp->aux.flags =
11521			AUX_PARM_DATA_VALID | AUX_PARM_PNI_VALID;
11522		sp->aux.pni = cpu_to_be64(phba->pni);
11523	}
11524
11525	pcmd += sizeof(uint32_t); /* CSP Word 2 */
11526	pcmd += sizeof(uint32_t); /* CSP Word 3 */
11527	pcmd += sizeof(uint32_t); /* CSP Word 4 */
11528	pcmd += sizeof(uint32_t); /* Port Name */
11529	memcpy(pcmd, &vport->fc_portname, 8);
11530	pcmd += sizeof(uint32_t); /* Node Name */
11531	pcmd += sizeof(uint32_t); /* Node Name */
11532	memcpy(pcmd, &vport->fc_nodename, 8);
11533	sp->cmn.valid_vendor_ver_level = 0;
11534	memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
11535	lpfc_set_disctmo(vport);
11536
11537	phba->fc_stat.elsXmitFDISC++;
11538	elsiocb->cmd_cmpl = lpfc_cmpl_els_fdisc;
11539
11540	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11541		"Issue FDISC:     did:x%x",
11542		did, 0, 0);
11543
11544	elsiocb->ndlp = lpfc_nlp_get(ndlp);
11545	if (!elsiocb->ndlp)
11546		goto err_out;
11547
11548	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
11549	if (rc == IOCB_ERROR) {
11550		lpfc_nlp_put(ndlp);
11551		goto err_out;
11552	}
11553
11554	lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
11555	return 0;
11556
11557 err_out:
11558	lpfc_els_free_iocb(phba, elsiocb);
11559	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11560	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11561			 "0256 Issue FDISC: Cannot send IOCB\n");
11562	return 1;
11563}
11564
11565/**
11566 * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
11567 * @phba: pointer to lpfc hba data structure.
11568 * @cmdiocb: pointer to lpfc command iocb data structure.
11569 * @rspiocb: pointer to lpfc response iocb data structure.
11570 *
11571 * This routine is the completion callback function to the issuing of a LOGO
11572 * ELS command off a vport. It frees the command IOCB and then decrement the
11573 * reference count held on ndlp for this completion function, indicating that
11574 * the reference to the ndlp is no long needed. Note that the
11575 * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
11576 * callback function and an additional explicit ndlp reference decrementation
11577 * will trigger the actual release of the ndlp.
11578 **/
11579static void
11580lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11581			struct lpfc_iocbq *rspiocb)
11582{
11583	struct lpfc_vport *vport = cmdiocb->vport;
11584	IOCB_t *irsp;
11585	struct lpfc_nodelist *ndlp;
11586	u32 ulp_status, ulp_word4, did, tmo;
11587
11588	ndlp = cmdiocb->ndlp;
11589
11590	ulp_status = get_job_ulpstatus(phba, rspiocb);
11591	ulp_word4 = get_job_word4(phba, rspiocb);
11592
11593	if (phba->sli_rev == LPFC_SLI_REV4) {
11594		did = get_job_els_rsp64_did(phba, cmdiocb);
11595		tmo = get_wqe_tmo(cmdiocb);
11596	} else {
11597		irsp = &rspiocb->iocb;
11598		did = get_job_els_rsp64_did(phba, rspiocb);
11599		tmo = irsp->ulpTimeout;
11600	}
11601
11602	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11603		"LOGO npiv cmpl:  status:x%x/x%x did:x%x",
11604		ulp_status, ulp_word4, did);
11605
11606	/* NPIV LOGO completes to NPort <nlp_DID> */
11607	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
11608			 "2928 NPIV LOGO completes to NPort x%x "
11609			 "Data: x%x x%x x%x x%x x%x x%lx x%x\n",
11610			 ndlp->nlp_DID, ulp_status, ulp_word4,
11611			 tmo, vport->num_disc_nodes,
11612			 kref_read(&ndlp->kref), ndlp->nlp_flag,
11613			 ndlp->fc4_xpt_flags);
11614
11615	if (ulp_status == IOSTAT_SUCCESS) {
11616		clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
11617		clear_bit(FC_FABRIC, &vport->fc_flag);
11618		lpfc_can_disctmo(vport);
11619	}
11620
11621	if (test_bit(NLP_WAIT_FOR_LOGO, &ndlp->save_flags)) {
11622		/* Wake up lpfc_vport_delete if waiting...*/
11623		if (ndlp->logo_waitq)
11624			wake_up(ndlp->logo_waitq);
11625		clear_bit(NLP_ISSUE_LOGO, &ndlp->nlp_flag);
11626		clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
11627		clear_bit(NLP_WAIT_FOR_LOGO, &ndlp->save_flags);
11628	}
11629
11630	/* Safe to release resources now. */
11631	lpfc_els_free_iocb(phba, cmdiocb);
11632	lpfc_nlp_put(ndlp);
11633}
11634
11635/**
11636 * lpfc_issue_els_npiv_logo - Issue a logo off a vport
11637 * @vport: pointer to a virtual N_Port data structure.
11638 * @ndlp: pointer to a node-list data structure.
11639 *
11640 * This routine issues a LOGO ELS command to an @ndlp off a @vport.
11641 *
11642 * Note that the ndlp reference count will be incremented by 1 for holding the
11643 * ndlp and the reference to ndlp will be stored into the ndlp field of
11644 * the IOCB for the completion callback function to the LOGO ELS command.
11645 *
11646 * Return codes
11647 *   0 - Successfully issued logo off the @vport
11648 *   1 - Failed to issue logo off the @vport
11649 **/
11650int
11651lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
11652{
11653	int rc = 0;
11654	struct lpfc_hba  *phba = vport->phba;
11655	struct lpfc_iocbq *elsiocb;
11656	uint8_t *pcmd;
11657	uint16_t cmdsize;
11658
11659	cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
11660	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
11661				     ELS_CMD_LOGO);
11662	if (!elsiocb)
11663		return 1;
11664
11665	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
11666	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
11667	pcmd += sizeof(uint32_t);
11668
11669	/* Fill in LOGO payload */
11670	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
11671	pcmd += sizeof(uint32_t);
11672	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
11673
11674	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11675		"Issue LOGO npiv  did:x%x flg:x%lx",
11676		ndlp->nlp_DID, ndlp->nlp_flag, 0);
11677
11678	elsiocb->cmd_cmpl = lpfc_cmpl_els_npiv_logo;
11679	set_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
11680	elsiocb->ndlp = lpfc_nlp_get(ndlp);
11681	if (!elsiocb->ndlp) {
11682		lpfc_els_free_iocb(phba, elsiocb);
11683		goto err;
11684	}
11685
11686	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
11687	if (rc == IOCB_ERROR) {
11688		lpfc_els_free_iocb(phba, elsiocb);
11689		lpfc_nlp_put(ndlp);
11690		goto err;
11691	}
11692	return 0;
11693
11694err:
11695	clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
11696	return 1;
11697}
11698
11699/**
11700 * lpfc_fabric_block_timeout - Handler function to the fabric block timer
11701 * @t: timer context used to obtain the lpfc hba.
11702 *
11703 * This routine is invoked by the fabric iocb block timer after
11704 * timeout. It posts the fabric iocb block timeout event by setting the
11705 * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
11706 * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
11707 * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
11708 * posted event WORKER_FABRIC_BLOCK_TMO.
11709 **/
11710void
11711lpfc_fabric_block_timeout(struct timer_list *t)
11712{
11713	struct lpfc_hba  *phba = timer_container_of(phba, t,
11714						    fabric_block_timer);
11715	unsigned long iflags;
11716	uint32_t tmo_posted;
11717
11718	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11719	tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
11720	if (!tmo_posted)
11721		phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
11722	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11723
11724	if (!tmo_posted)
11725		lpfc_worker_wake_up(phba);
11726	return;
11727}
11728
11729/**
11730 * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
11731 * @phba: pointer to lpfc hba data structure.
11732 *
11733 * This routine issues one fabric iocb from the driver internal list to
11734 * the HBA. It first checks whether it's ready to issue one fabric iocb to
11735 * the HBA (whether there is no outstanding fabric iocb). If so, it shall
11736 * remove one pending fabric iocb from the driver internal list and invokes
11737 * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
11738 **/
11739static void
11740lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
11741{
11742	struct lpfc_iocbq *iocb;
11743	unsigned long iflags;
11744	int ret;
11745
11746repeat:
11747	iocb = NULL;
11748	spin_lock_irqsave(&phba->hbalock, iflags);
11749	/* Post any pending iocb to the SLI layer */
11750	if (atomic_read(&phba->fabric_iocb_count) == 0) {
11751		list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
11752				 list);
11753		if (iocb)
11754			/* Increment fabric iocb count to hold the position */
11755			atomic_inc(&phba->fabric_iocb_count);
11756	}
11757	spin_unlock_irqrestore(&phba->hbalock, iflags);
11758	if (iocb) {
11759		iocb->fabric_cmd_cmpl = iocb->cmd_cmpl;
11760		iocb->cmd_cmpl = lpfc_cmpl_fabric_iocb;
11761		iocb->cmd_flag |= LPFC_IO_FABRIC;
11762
11763		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11764				      "Fabric sched1:   ste:x%x",
11765				      iocb->vport->port_state, 0, 0);
11766
11767		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11768
11769		if (ret == IOCB_ERROR) {
11770			iocb->cmd_cmpl = iocb->fabric_cmd_cmpl;
11771			iocb->fabric_cmd_cmpl = NULL;
11772			iocb->cmd_flag &= ~LPFC_IO_FABRIC;
11773			set_job_ulpstatus(iocb, IOSTAT_LOCAL_REJECT);
11774			iocb->wcqe_cmpl.parameter = IOERR_SLI_ABORTED;
11775			iocb->cmd_cmpl(phba, iocb, iocb);
11776
11777			atomic_dec(&phba->fabric_iocb_count);
11778			goto repeat;
11779		}
11780	}
11781}
11782
11783/**
11784 * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
11785 * @phba: pointer to lpfc hba data structure.
11786 *
11787 * This routine unblocks the  issuing fabric iocb command. The function
11788 * will clear the fabric iocb block bit and then invoke the routine
11789 * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
11790 * from the driver internal fabric iocb list.
11791 **/
11792void
11793lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
11794{
11795	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11796
11797	lpfc_resume_fabric_iocbs(phba);
11798	return;
11799}
11800
11801/**
11802 * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
11803 * @phba: pointer to lpfc hba data structure.
11804 *
11805 * This routine blocks the issuing fabric iocb for a specified amount of
11806 * time (currently 100 ms). This is done by set the fabric iocb block bit
11807 * and set up a timeout timer for 100ms. When the block bit is set, no more
11808 * fabric iocb will be issued out of the HBA.
11809 **/
11810static void
11811lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
11812{
11813	int blocked;
11814
11815	blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11816	/* Start a timer to unblock fabric iocbs after 100ms */
11817	if (!blocked)
11818		mod_timer(&phba->fabric_block_timer,
11819			  jiffies + msecs_to_jiffies(100));
11820
11821	return;
11822}
11823
11824/**
11825 * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
11826 * @phba: pointer to lpfc hba data structure.
11827 * @cmdiocb: pointer to lpfc command iocb data structure.
11828 * @rspiocb: pointer to lpfc response iocb data structure.
11829 *
11830 * This routine is the callback function that is put to the fabric iocb's
11831 * callback function pointer (iocb->cmd_cmpl). The original iocb's callback
11832 * function pointer has been stored in iocb->fabric_cmd_cmpl. This callback
11833 * function first restores and invokes the original iocb's callback function
11834 * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
11835 * fabric bound iocb from the driver internal fabric iocb list onto the wire.
11836 **/
11837static void
11838lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11839		      struct lpfc_iocbq *rspiocb)
11840{
11841	struct ls_rjt stat;
11842	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
11843	u32 ulp_word4 = get_job_word4(phba, rspiocb);
11844
11845	WARN_ON((cmdiocb->cmd_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC);
11846
11847	switch (ulp_status) {
11848		case IOSTAT_NPORT_RJT:
11849		case IOSTAT_FABRIC_RJT:
11850			if (ulp_word4 & RJT_UNAVAIL_TEMP)
11851				lpfc_block_fabric_iocbs(phba);
11852			break;
11853
11854		case IOSTAT_NPORT_BSY:
11855		case IOSTAT_FABRIC_BSY:
11856			lpfc_block_fabric_iocbs(phba);
11857			break;
11858
11859		case IOSTAT_LS_RJT:
11860			stat.un.ls_rjt_error_be =
11861				cpu_to_be32(ulp_word4);
11862			if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
11863				(stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
11864				lpfc_block_fabric_iocbs(phba);
11865			break;
11866	}
11867
11868	BUG_ON(atomic_read(&phba->fabric_iocb_count) == 0);
11869
11870	cmdiocb->cmd_cmpl = cmdiocb->fabric_cmd_cmpl;
11871	cmdiocb->fabric_cmd_cmpl = NULL;
11872	cmdiocb->cmd_flag &= ~LPFC_IO_FABRIC;
11873	cmdiocb->cmd_cmpl(phba, cmdiocb, rspiocb);
11874
11875	atomic_dec(&phba->fabric_iocb_count);
11876	if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
11877		/* Post any pending iocbs to HBA */
11878		lpfc_resume_fabric_iocbs(phba);
11879	}
11880}
11881
11882/**
11883 * lpfc_issue_fabric_iocb - Issue a fabric iocb command
11884 * @phba: pointer to lpfc hba data structure.
11885 * @iocb: pointer to lpfc command iocb data structure.
11886 *
11887 * This routine is used as the top-level API for issuing a fabric iocb command
11888 * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
11889 * function makes sure that only one fabric bound iocb will be outstanding at
11890 * any given time. As such, this function will first check to see whether there
11891 * is already an outstanding fabric iocb on the wire. If so, it will put the
11892 * newly issued iocb onto the driver internal fabric iocb list, waiting to be
11893 * issued later. Otherwise, it will issue the iocb on the wire and update the
11894 * fabric iocb count it indicate that there is one fabric iocb on the wire.
11895 *
11896 * Note, this implementation has a potential sending out fabric IOCBs out of
11897 * order. The problem is caused by the construction of the "ready" boolen does
11898 * not include the condition that the internal fabric IOCB list is empty. As
11899 * such, it is possible a fabric IOCB issued by this routine might be "jump"
11900 * ahead of the fabric IOCBs in the internal list.
11901 *
11902 * Return code
11903 *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
11904 *   IOCB_ERROR - failed to issue fabric iocb
11905 **/
11906static int
11907lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
11908{
11909	unsigned long iflags;
11910	int ready;
11911	int ret;
11912
11913	BUG_ON(atomic_read(&phba->fabric_iocb_count) > 1);
11914
11915	spin_lock_irqsave(&phba->hbalock, iflags);
11916	ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
11917		!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11918
11919	if (ready)
11920		/* Increment fabric iocb count to hold the position */
11921		atomic_inc(&phba->fabric_iocb_count);
11922	spin_unlock_irqrestore(&phba->hbalock, iflags);
11923	if (ready) {
11924		iocb->fabric_cmd_cmpl = iocb->cmd_cmpl;
11925		iocb->cmd_cmpl = lpfc_cmpl_fabric_iocb;
11926		iocb->cmd_flag |= LPFC_IO_FABRIC;
11927
11928		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11929				      "Fabric sched2:   ste:x%x",
11930				      iocb->vport->port_state, 0, 0);
11931
11932		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11933
11934		if (ret == IOCB_ERROR) {
11935			iocb->cmd_cmpl = iocb->fabric_cmd_cmpl;
11936			iocb->fabric_cmd_cmpl = NULL;
11937			iocb->cmd_flag &= ~LPFC_IO_FABRIC;
11938			atomic_dec(&phba->fabric_iocb_count);
11939		}
11940	} else {
11941		spin_lock_irqsave(&phba->hbalock, iflags);
11942		list_add_tail(&iocb->list, &phba->fabric_iocb_list);
11943		spin_unlock_irqrestore(&phba->hbalock, iflags);
11944		ret = IOCB_SUCCESS;
11945	}
11946	return ret;
11947}
11948
11949/**
11950 * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
11951 * @vport: pointer to a virtual N_Port data structure.
11952 *
11953 * This routine aborts all the IOCBs associated with a @vport from the
11954 * driver internal fabric IOCB list. The list contains fabric IOCBs to be
11955 * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
11956 * list, removes each IOCB associated with the @vport off the list, set the
11957 * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
11958 * associated with the IOCB.
11959 **/
11960static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
11961{
11962	LIST_HEAD(completions);
11963	struct lpfc_hba  *phba = vport->phba;
11964	struct lpfc_iocbq *tmp_iocb, *piocb;
11965
11966	spin_lock_irq(&phba->hbalock);
11967	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
11968				 list) {
11969
11970		if (piocb->vport != vport)
11971			continue;
11972
11973		list_move_tail(&piocb->list, &completions);
11974	}
11975	spin_unlock_irq(&phba->hbalock);
11976
11977	/* Cancel all the IOCBs from the completions list */
11978	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11979			      IOERR_SLI_ABORTED);
11980}
11981
11982/**
11983 * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
11984 * @ndlp: pointer to a node-list data structure.
11985 *
11986 * This routine aborts all the IOCBs associated with an @ndlp from the
11987 * driver internal fabric IOCB list. The list contains fabric IOCBs to be
11988 * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
11989 * list, removes each IOCB associated with the @ndlp off the list, set the
11990 * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
11991 * associated with the IOCB.
11992 **/
11993void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
11994{
11995	LIST_HEAD(completions);
11996	struct lpfc_hba  *phba = ndlp->phba;
11997	struct lpfc_iocbq *tmp_iocb, *piocb;
11998	struct lpfc_sli_ring *pring;
11999
12000	pring = lpfc_phba_elsring(phba);
12001
12002	if (unlikely(!pring))
12003		return;
12004
12005	spin_lock_irq(&phba->hbalock);
12006	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
12007				 list) {
12008		if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
12009
12010			list_move_tail(&piocb->list, &completions);
12011		}
12012	}
12013	spin_unlock_irq(&phba->hbalock);
12014
12015	/* Cancel all the IOCBs from the completions list */
12016	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12017			      IOERR_SLI_ABORTED);
12018}
12019
12020/**
12021 * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
12022 * @phba: pointer to lpfc hba data structure.
12023 *
12024 * This routine aborts all the IOCBs currently on the driver internal
12025 * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
12026 * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
12027 * list, removes IOCBs off the list, set the status field to
12028 * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
12029 * the IOCB.
12030 **/
12031void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
12032{
12033	LIST_HEAD(completions);
12034
12035	spin_lock_irq(&phba->hbalock);
12036	list_splice_init(&phba->fabric_iocb_list, &completions);
12037	spin_unlock_irq(&phba->hbalock);
12038
12039	/* Cancel all the IOCBs from the completions list */
12040	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12041			      IOERR_SLI_ABORTED);
12042}
12043
12044/**
12045 * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
12046 * @vport: pointer to lpfc vport data structure.
12047 *
12048 * This routine is invoked by the vport cleanup for deletions and the cleanup
12049 * for an ndlp on removal.
12050 **/
12051void
12052lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
12053{
12054	struct lpfc_hba *phba = vport->phba;
12055	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
12056	struct lpfc_nodelist *ndlp = NULL;
12057	unsigned long iflag = 0;
12058
12059	spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
12060	list_for_each_entry_safe(sglq_entry, sglq_next,
12061			&phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
12062		if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport) {
12063			lpfc_nlp_put(sglq_entry->ndlp);
12064			ndlp = sglq_entry->ndlp;
12065			sglq_entry->ndlp = NULL;
12066
12067			/* If the xri on the abts_els_sgl list is for the Fport
12068			 * node and the vport is unloading, the xri aborted wcqe
12069			 * likely isn't coming back.  Just release the sgl.
12070			 */
12071			if (test_bit(FC_UNLOADING, &vport->load_flag) &&
12072			    ndlp->nlp_DID == Fabric_DID) {
12073				list_del(&sglq_entry->list);
12074				sglq_entry->state = SGL_FREED;
12075				list_add_tail(&sglq_entry->list,
12076					&phba->sli4_hba.lpfc_els_sgl_list);
12077			}
12078		}
12079	}
12080	spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
12081	return;
12082}
12083
12084/**
12085 * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
12086 * @phba: pointer to lpfc hba data structure.
12087 * @axri: pointer to the els xri abort wcqe structure.
12088 *
12089 * This routine is invoked by the worker thread to process a SLI4 slow-path
12090 * ELS aborted xri.
12091 **/
12092void
12093lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
12094			  struct sli4_wcqe_xri_aborted *axri)
12095{
12096	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
12097	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
12098	uint16_t lxri = 0;
12099
12100	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
12101	unsigned long iflag = 0;
12102	struct lpfc_nodelist *ndlp;
12103	struct lpfc_sli_ring *pring;
12104
12105	pring = lpfc_phba_elsring(phba);
12106
12107	spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
12108	list_for_each_entry_safe(sglq_entry, sglq_next,
12109			&phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
12110		if (sglq_entry->sli4_xritag == xri) {
12111			list_del(&sglq_entry->list);
12112			ndlp = sglq_entry->ndlp;
12113			sglq_entry->ndlp = NULL;
12114			list_add_tail(&sglq_entry->list,
12115				&phba->sli4_hba.lpfc_els_sgl_list);
12116			sglq_entry->state = SGL_FREED;
12117			spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock,
12118					       iflag);
12119			lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI |
12120					LOG_DISCOVERY | LOG_NODE,
12121					"0732 ELS XRI ABORT on Node: ndlp=x%px "
12122					"xri=x%x\n",
12123					ndlp, xri);
12124			if (ndlp) {
12125				lpfc_set_rrq_active(phba, ndlp,
12126					sglq_entry->sli4_lxritag,
12127					rxid, 1);
12128				lpfc_nlp_put(ndlp);
12129			}
12130
12131			/* Check if TXQ queue needs to be serviced */
12132			if (pring && !list_empty(&pring->txq))
12133				lpfc_worker_wake_up(phba);
12134			return;
12135		}
12136	}
12137	spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
12138	lxri = lpfc_sli4_xri_inrange(phba, xri);
12139	if (lxri == NO_XRI)
12140		return;
12141
12142	spin_lock_irqsave(&phba->hbalock, iflag);
12143	sglq_entry = __lpfc_get_active_sglq(phba, lxri);
12144	if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
12145		spin_unlock_irqrestore(&phba->hbalock, iflag);
12146		return;
12147	}
12148	sglq_entry->state = SGL_XRI_ABORTED;
12149	spin_unlock_irqrestore(&phba->hbalock, iflag);
12150	return;
12151}
12152
12153/* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
12154 * @vport: pointer to virtual port object.
12155 * @ndlp: nodelist pointer for the impacted node.
12156 *
12157 * The driver calls this routine in response to an SLI4 XRI ABORT CQE
12158 * or an SLI3 ASYNC_STATUS_CN event from the port.  For either event,
12159 * the driver is required to send a LOGO to the remote node before it
12160 * attempts to recover its login to the remote node.
12161 */
12162void
12163lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
12164			   struct lpfc_nodelist *ndlp)
12165{
12166	struct Scsi_Host *shost;
12167	struct lpfc_hba *phba;
12168	unsigned long flags = 0;
12169
12170	shost = lpfc_shost_from_vport(vport);
12171	phba = vport->phba;
12172	if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
12173		lpfc_printf_log(phba, KERN_INFO,
12174				LOG_SLI, "3093 No rport recovery needed. "
12175				"rport in state 0x%x\n", ndlp->nlp_state);
12176		return;
12177	}
12178	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12179			"3094 Start rport recovery on shost id 0x%x "
12180			"fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
12181			"flag 0x%lx\n",
12182			shost->host_no, ndlp->nlp_DID,
12183			vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
12184			ndlp->nlp_flag);
12185	/*
12186	 * The rport is not responding.  Remove the FCP-2 flag to prevent
12187	 * an ADISC in the follow-up recovery code.
12188	 */
12189	spin_lock_irqsave(&ndlp->lock, flags);
12190	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
12191	spin_unlock_irqrestore(&ndlp->lock, flags);
12192	set_bit(NLP_ISSUE_LOGO, &ndlp->nlp_flag);
12193	lpfc_unreg_rpi(vport, ndlp);
12194}
12195
12196static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport)
12197{
12198	bitmap_zero(vport->vmid_priority_range, LPFC_VMID_MAX_PRIORITY_RANGE);
12199}
12200
12201static void
12202lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max)
12203{
12204	u32 i;
12205
12206	if ((min > max) || (max > LPFC_VMID_MAX_PRIORITY_RANGE))
12207		return;
12208
12209	for (i = min; i <= max; i++)
12210		set_bit(i, vport->vmid_priority_range);
12211}
12212
12213static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid)
12214{
12215	set_bit(ctcl_vmid, vport->vmid_priority_range);
12216}
12217
12218u32 lpfc_vmid_get_cs_ctl(struct lpfc_vport *vport)
12219{
12220	u32 i;
12221
12222	i = find_first_bit(vport->vmid_priority_range,
12223			   LPFC_VMID_MAX_PRIORITY_RANGE);
12224
12225	if (i == LPFC_VMID_MAX_PRIORITY_RANGE)
12226		return 0;
12227
12228	clear_bit(i, vport->vmid_priority_range);
12229	return i;
12230}
12231
12232#define MAX_PRIORITY_DESC	255
12233
12234static void
12235lpfc_cmpl_els_qfpa(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12236		   struct lpfc_iocbq *rspiocb)
12237{
12238	struct lpfc_vport *vport = cmdiocb->vport;
12239	struct priority_range_desc *desc;
12240	struct lpfc_dmabuf *prsp = NULL;
12241	struct lpfc_vmid_priority_range *vmid_range = NULL;
12242	u32 *data;
12243	struct lpfc_dmabuf *dmabuf = cmdiocb->cmd_dmabuf;
12244	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12245	u32 ulp_word4 = get_job_word4(phba, rspiocb);
12246	u8 *pcmd, max_desc;
12247	u32 len, i;
12248	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
12249
12250	prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
12251	if (!prsp)
12252		goto out;
12253
12254	pcmd = prsp->virt;
12255	data = (u32 *)pcmd;
12256	if (data[0] == ELS_CMD_LS_RJT) {
12257		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
12258				 "3277 QFPA LS_RJT x%x  x%x\n",
12259				 data[0], data[1]);
12260		goto out;
12261	}
12262	if (ulp_status) {
12263		lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
12264				 "6529 QFPA failed with status x%x  x%x\n",
12265				 ulp_status, ulp_word4);
12266		goto out;
12267	}
12268
12269	if (!vport->qfpa_res) {
12270		max_desc = FCELSSIZE / sizeof(*vport->qfpa_res);
12271		vport->qfpa_res = kcalloc(max_desc, sizeof(*vport->qfpa_res),
12272					  GFP_KERNEL);
12273		if (!vport->qfpa_res)
12274			goto out;
12275	}
12276
12277	len = *((u32 *)(pcmd + 4));
12278	len = be32_to_cpu(len);
12279	memcpy(vport->qfpa_res, pcmd, len + 8);
12280	len = len / LPFC_PRIORITY_RANGE_DESC_SIZE;
12281
12282	desc = (struct priority_range_desc *)(pcmd + 8);
12283	vmid_range = vport->vmid_priority.vmid_range;
12284	if (!vmid_range) {
12285		vmid_range = kcalloc(MAX_PRIORITY_DESC, sizeof(*vmid_range),
12286				     GFP_KERNEL);
12287		if (!vmid_range) {
12288			kfree(vport->qfpa_res);
12289			goto out;
12290		}
12291		vport->vmid_priority.vmid_range = vmid_range;
12292	}
12293	vport->vmid_priority.num_descriptors = len;
12294
12295	for (i = 0; i < len; i++, vmid_range++, desc++) {
12296		lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
12297				 "6539 vmid values low=%d, high=%d, qos=%d, "
12298				 "local ve id=%d\n", desc->lo_range,
12299				 desc->hi_range, desc->qos_priority,
12300				 desc->local_ve_id);
12301
12302		vmid_range->low = desc->lo_range << 1;
12303		if (desc->local_ve_id == QFPA_ODD_ONLY)
12304			vmid_range->low++;
12305		if (desc->qos_priority)
12306			vport->vmid_flag |= LPFC_VMID_QOS_ENABLED;
12307		vmid_range->qos = desc->qos_priority;
12308
12309		vmid_range->high = desc->hi_range << 1;
12310		if ((desc->local_ve_id == QFPA_ODD_ONLY) ||
12311		    (desc->local_ve_id == QFPA_EVEN_ODD))
12312			vmid_range->high++;
12313	}
12314	lpfc_init_cs_ctl_bitmap(vport);
12315	for (i = 0; i < vport->vmid_priority.num_descriptors; i++) {
12316		lpfc_vmid_set_cs_ctl_range(vport,
12317				vport->vmid_priority.vmid_range[i].low,
12318				vport->vmid_priority.vmid_range[i].high);
12319	}
12320
12321	vport->vmid_flag |= LPFC_VMID_QFPA_CMPL;
12322 out:
12323	lpfc_els_free_iocb(phba, cmdiocb);
12324	lpfc_nlp_put(ndlp);
12325}
12326
12327int lpfc_issue_els_qfpa(struct lpfc_vport *vport)
12328{
12329	struct lpfc_hba *phba = vport->phba;
12330	struct lpfc_nodelist *ndlp;
12331	struct lpfc_iocbq *elsiocb;
12332	u8 *pcmd;
12333	int ret;
12334
12335	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
12336	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12337		return -ENXIO;
12338
12339	elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_QFPA_SIZE, 2, ndlp,
12340				     ndlp->nlp_DID, ELS_CMD_QFPA);
12341	if (!elsiocb)
12342		return -ENOMEM;
12343
12344	pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
12345
12346	*((u32 *)(pcmd)) = ELS_CMD_QFPA;
12347	pcmd += 4;
12348
12349	elsiocb->cmd_cmpl = lpfc_cmpl_els_qfpa;
12350
12351	elsiocb->ndlp = lpfc_nlp_get(ndlp);
12352	if (!elsiocb->ndlp) {
12353		lpfc_els_free_iocb(vport->phba, elsiocb);
12354		return -ENXIO;
12355	}
12356
12357	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 2);
12358	if (ret != IOCB_SUCCESS) {
12359		lpfc_els_free_iocb(phba, elsiocb);
12360		lpfc_nlp_put(ndlp);
12361		return -EIO;
12362	}
12363	vport->vmid_flag &= ~LPFC_VMID_QOS_ENABLED;
12364	return 0;
12365}
12366
12367int
12368lpfc_vmid_uvem(struct lpfc_vport *vport,
12369	       struct lpfc_vmid *vmid, bool instantiated)
12370{
12371	struct lpfc_vem_id_desc *vem_id_desc;
12372	struct lpfc_nodelist *ndlp;
12373	struct lpfc_iocbq *elsiocb;
12374	struct instantiated_ve_desc *inst_desc;
12375	struct lpfc_vmid_context *vmid_context;
12376	u8 *pcmd;
12377	u32 *len;
12378	int ret = 0;
12379
12380	ndlp = lpfc_findnode_did(vport, Fabric_DID);
12381	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12382		return -ENXIO;
12383
12384	vmid_context = kmalloc(sizeof(*vmid_context), GFP_KERNEL);
12385	if (!vmid_context)
12386		return -ENOMEM;
12387	elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_UVEM_SIZE, 2,
12388				     ndlp, Fabric_DID, ELS_CMD_UVEM);
12389	if (!elsiocb)
12390		goto out;
12391
12392	lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
12393			 "3427 Host vmid %s %d\n",
12394			 vmid->host_vmid, instantiated);
12395	vmid_context->vmp = vmid;
12396	vmid_context->nlp = ndlp;
12397	vmid_context->instantiated = instantiated;
12398	elsiocb->vmid_tag.vmid_context = vmid_context;
12399	pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
12400
12401	if (!memchr_inv(vport->lpfc_vmid_host_uuid, 0,
12402			sizeof(vport->lpfc_vmid_host_uuid)))
12403		memcpy(vport->lpfc_vmid_host_uuid, vmid->host_vmid,
12404		       sizeof(vport->lpfc_vmid_host_uuid));
12405
12406	*((u32 *)(pcmd)) = ELS_CMD_UVEM;
12407	len = (u32 *)(pcmd + 4);
12408	*len = cpu_to_be32(LPFC_UVEM_SIZE - 8);
12409
12410	vem_id_desc = (struct lpfc_vem_id_desc *)(pcmd + 8);
12411	vem_id_desc->tag = be32_to_cpu(VEM_ID_DESC_TAG);
12412	vem_id_desc->length = be32_to_cpu(LPFC_UVEM_VEM_ID_DESC_SIZE);
12413	memcpy(vem_id_desc->vem_id, vport->lpfc_vmid_host_uuid,
12414	       sizeof(vem_id_desc->vem_id));
12415
12416	inst_desc = (struct instantiated_ve_desc *)(pcmd + 32);
12417	inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
12418	inst_desc->length = be32_to_cpu(LPFC_UVEM_VE_MAP_DESC_SIZE);
12419	memcpy(inst_desc->global_vem_id, vmid->host_vmid,
12420	       sizeof(inst_desc->global_vem_id));
12421
12422	bf_set(lpfc_instantiated_nport_id, inst_desc, vport->fc_myDID);
12423	bf_set(lpfc_instantiated_local_id, inst_desc,
12424	       vmid->un.cs_ctl_vmid);
12425	if (instantiated) {
12426		inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
12427	} else {
12428		inst_desc->tag = be32_to_cpu(DEINSTANTIATED_VE_DESC_TAG);
12429		lpfc_vmid_put_cs_ctl(vport, vmid->un.cs_ctl_vmid);
12430	}
12431	inst_desc->word6 = cpu_to_be32(inst_desc->word6);
12432
12433	elsiocb->cmd_cmpl = lpfc_cmpl_els_uvem;
12434
12435	elsiocb->ndlp = lpfc_nlp_get(ndlp);
12436	if (!elsiocb->ndlp) {
12437		lpfc_els_free_iocb(vport->phba, elsiocb);
12438		goto out;
12439	}
12440
12441	ret = lpfc_sli_issue_iocb(vport->phba, LPFC_ELS_RING, elsiocb, 0);
12442	if (ret != IOCB_SUCCESS) {
12443		lpfc_els_free_iocb(vport->phba, elsiocb);
12444		lpfc_nlp_put(ndlp);
12445		goto out;
12446	}
12447
12448	return 0;
12449 out:
12450	kfree(vmid_context);
12451	return -EIO;
12452}
12453
12454static void
12455lpfc_cmpl_els_uvem(struct lpfc_hba *phba, struct lpfc_iocbq *icmdiocb,
12456		   struct lpfc_iocbq *rspiocb)
12457{
12458	struct lpfc_vport *vport = icmdiocb->vport;
12459	struct lpfc_dmabuf *prsp = NULL;
12460	struct lpfc_vmid_context *vmid_context =
12461	    icmdiocb->vmid_tag.vmid_context;
12462	struct lpfc_nodelist *ndlp = icmdiocb->ndlp;
12463	u8 *pcmd;
12464	u32 *data;
12465	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12466	u32 ulp_word4 = get_job_word4(phba, rspiocb);
12467	struct lpfc_dmabuf *dmabuf = icmdiocb->cmd_dmabuf;
12468	struct lpfc_vmid *vmid;
12469
12470	vmid = vmid_context->vmp;
12471	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12472		ndlp = NULL;
12473
12474	prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
12475	if (!prsp)
12476		goto out;
12477	pcmd = prsp->virt;
12478	data = (u32 *)pcmd;
12479	if (data[0] == ELS_CMD_LS_RJT) {
12480		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
12481				 "4532 UVEM LS_RJT %x %x\n", data[0], data[1]);
12482		goto out;
12483	}
12484	if (ulp_status) {
12485		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
12486				 "4533 UVEM error status %x: %x\n",
12487				 ulp_status, ulp_word4);
12488		goto out;
12489	}
12490	spin_lock(&phba->hbalock);
12491	/* Set IN USE flag */
12492	vport->vmid_flag |= LPFC_VMID_IN_USE;
12493	phba->pport->vmid_flag |= LPFC_VMID_IN_USE;
12494	spin_unlock(&phba->hbalock);
12495
12496	if (vmid_context->instantiated) {
12497		write_lock(&vport->vmid_lock);
12498		vmid->flag |= LPFC_VMID_REGISTERED;
12499		vmid->flag &= ~LPFC_VMID_REQ_REGISTER;
12500		write_unlock(&vport->vmid_lock);
12501	}
12502
12503 out:
12504	kfree(vmid_context);
12505	lpfc_els_free_iocb(phba, icmdiocb);
12506	lpfc_nlp_put(ndlp);
12507}
~/ Read-only file preview.