linux/drivers/scsi/lpfc/lpfc_els.c
<<
>>
Prefs
   1/*******************************************************************
   2 * This file is part of the Emulex Linux Device Driver for         *
   3 * Fibre Channel Host Bus Adapters.                                *
   4 * Copyright (C) 2017-2021 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_uvem(struct lpfc_hba *, struct lpfc_iocbq *,
  60                               struct lpfc_iocbq *);
  61
  62static int lpfc_max_els_tries = 3;
  63
  64static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport);
  65static void lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max);
  66static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid);
  67
  68/**
  69 * lpfc_els_chk_latt - Check host link attention event for a vport
  70 * @vport: pointer to a host virtual N_Port data structure.
  71 *
  72 * This routine checks whether there is an outstanding host link
  73 * attention event during the discovery process with the @vport. It is done
  74 * by reading the HBA's Host Attention (HA) register. If there is any host
  75 * link attention events during this @vport's discovery process, the @vport
  76 * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
  77 * be issued if the link state is not already in host link cleared state,
  78 * and a return code shall indicate whether the host link attention event
  79 * had happened.
  80 *
  81 * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
  82 * state in LPFC_VPORT_READY, the request for checking host link attention
  83 * event will be ignored and a return code shall indicate no host link
  84 * attention event had happened.
  85 *
  86 * Return codes
  87 *   0 - no host link attention event happened
  88 *   1 - host link attention event happened
  89 **/
  90int
  91lpfc_els_chk_latt(struct lpfc_vport *vport)
  92{
  93        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  94        struct lpfc_hba  *phba = vport->phba;
  95        uint32_t ha_copy;
  96
  97        if (vport->port_state >= LPFC_VPORT_READY ||
  98            phba->link_state == LPFC_LINK_DOWN ||
  99            phba->sli_rev > LPFC_SLI_REV3)
 100                return 0;
 101
 102        /* Read the HBA Host Attention Register */
 103        if (lpfc_readl(phba->HAregaddr, &ha_copy))
 104                return 1;
 105
 106        if (!(ha_copy & HA_LATT))
 107                return 0;
 108
 109        /* Pending Link Event during Discovery */
 110        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 111                         "0237 Pending Link Event during "
 112                         "Discovery: State x%x\n",
 113                         phba->pport->port_state);
 114
 115        /* CLEAR_LA should re-enable link attention events and
 116         * we should then immediately take a LATT event. The
 117         * LATT processing should call lpfc_linkdown() which
 118         * will cleanup any left over in-progress discovery
 119         * events.
 120         */
 121        spin_lock_irq(shost->host_lock);
 122        vport->fc_flag |= FC_ABORT_DISCOVERY;
 123        spin_unlock_irq(shost->host_lock);
 124
 125        if (phba->link_state != LPFC_CLEAR_LA)
 126                lpfc_issue_clear_la(phba, vport);
 127
 128        return 1;
 129}
 130
 131/**
 132 * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
 133 * @vport: pointer to a host virtual N_Port data structure.
 134 * @expectRsp: flag indicating whether response is expected.
 135 * @cmdSize: size of the ELS command.
 136 * @retry: number of retries to the command IOCB when it fails.
 137 * @ndlp: pointer to a node-list data structure.
 138 * @did: destination identifier.
 139 * @elscmd: the ELS command code.
 140 *
 141 * This routine is used for allocating a lpfc-IOCB data structure from
 142 * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
 143 * passed into the routine for discovery state machine to issue an Extended
 144 * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
 145 * and preparation routine that is used by all the discovery state machine
 146 * routines and the ELS command-specific fields will be later set up by
 147 * the individual discovery machine routines after calling this routine
 148 * allocating and preparing a generic IOCB data structure. It fills in the
 149 * Buffer Descriptor Entries (BDEs), allocates buffers for both command
 150 * payload and response payload (if expected). The reference count on the
 151 * ndlp is incremented by 1 and the reference to the ndlp is put into
 152 * context1 of the IOCB data structure for this IOCB to hold the ndlp
 153 * reference for the command's callback function to access later.
 154 *
 155 * Return code
 156 *   Pointer to the newly allocated/prepared els iocb data structure
 157 *   NULL - when els iocb data structure allocation/preparation failed
 158 **/
 159struct lpfc_iocbq *
 160lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
 161                   uint16_t cmdSize, uint8_t retry,
 162                   struct lpfc_nodelist *ndlp, uint32_t did,
 163                   uint32_t elscmd)
 164{
 165        struct lpfc_hba  *phba = vport->phba;
 166        struct lpfc_iocbq *elsiocb;
 167        struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
 168        struct ulp_bde64 *bpl;
 169        IOCB_t *icmd;
 170
 171
 172        if (!lpfc_is_link_up(phba))
 173                return NULL;
 174
 175        /* Allocate buffer for  command iocb */
 176        elsiocb = lpfc_sli_get_iocbq(phba);
 177
 178        if (elsiocb == NULL)
 179                return NULL;
 180
 181        /*
 182         * If this command is for fabric controller and HBA running
 183         * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
 184         */
 185        if ((did == Fabric_DID) &&
 186                (phba->hba_flag & HBA_FIP_SUPPORT) &&
 187                ((elscmd == ELS_CMD_FLOGI) ||
 188                 (elscmd == ELS_CMD_FDISC) ||
 189                 (elscmd == ELS_CMD_LOGO)))
 190                switch (elscmd) {
 191                case ELS_CMD_FLOGI:
 192                elsiocb->iocb_flag |=
 193                        ((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
 194                                        & LPFC_FIP_ELS_ID_MASK);
 195                break;
 196                case ELS_CMD_FDISC:
 197                elsiocb->iocb_flag |=
 198                        ((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
 199                                        & LPFC_FIP_ELS_ID_MASK);
 200                break;
 201                case ELS_CMD_LOGO:
 202                elsiocb->iocb_flag |=
 203                        ((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
 204                                        & LPFC_FIP_ELS_ID_MASK);
 205                break;
 206                }
 207        else
 208                elsiocb->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
 209
 210        icmd = &elsiocb->iocb;
 211
 212        /* fill in BDEs for command */
 213        /* Allocate buffer for command payload */
 214        pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
 215        if (pcmd)
 216                pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
 217        if (!pcmd || !pcmd->virt)
 218                goto els_iocb_free_pcmb_exit;
 219
 220        INIT_LIST_HEAD(&pcmd->list);
 221
 222        /* Allocate buffer for response payload */
 223        if (expectRsp) {
 224                prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
 225                if (prsp)
 226                        prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
 227                                                     &prsp->phys);
 228                if (!prsp || !prsp->virt)
 229                        goto els_iocb_free_prsp_exit;
 230                INIT_LIST_HEAD(&prsp->list);
 231        } else
 232                prsp = NULL;
 233
 234        /* Allocate buffer for Buffer ptr list */
 235        pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
 236        if (pbuflist)
 237                pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
 238                                                 &pbuflist->phys);
 239        if (!pbuflist || !pbuflist->virt)
 240                goto els_iocb_free_pbuf_exit;
 241
 242        INIT_LIST_HEAD(&pbuflist->list);
 243
 244        if (expectRsp) {
 245                icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
 246                icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
 247                icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
 248                icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
 249
 250                icmd->un.elsreq64.remoteID = did;               /* DID */
 251                icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
 252                if (elscmd == ELS_CMD_FLOGI)
 253                        icmd->ulpTimeout = FF_DEF_RATOV * 2;
 254                else if (elscmd == ELS_CMD_LOGO)
 255                        icmd->ulpTimeout = phba->fc_ratov;
 256                else
 257                        icmd->ulpTimeout = phba->fc_ratov * 2;
 258        } else {
 259                icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
 260                icmd->un.xseq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
 261                icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
 262                icmd->un.xseq64.bdl.bdeSize = sizeof(struct ulp_bde64);
 263                icmd->un.xseq64.xmit_els_remoteID = did;        /* DID */
 264                icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
 265        }
 266        icmd->ulpBdeCount = 1;
 267        icmd->ulpLe = 1;
 268        icmd->ulpClass = CLASS3;
 269
 270        /*
 271         * If we have NPIV enabled, we want to send ELS traffic by VPI.
 272         * For SLI4, since the driver controls VPIs we also want to include
 273         * all ELS pt2pt protocol traffic as well.
 274         */
 275        if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
 276                ((phba->sli_rev == LPFC_SLI_REV4) &&
 277                    (vport->fc_flag & FC_PT2PT))) {
 278
 279                if (expectRsp) {
 280                        icmd->un.elsreq64.myID = vport->fc_myDID;
 281
 282                        /* For ELS_REQUEST64_CR, use the VPI by default */
 283                        icmd->ulpContext = phba->vpi_ids[vport->vpi];
 284                }
 285
 286                icmd->ulpCt_h = 0;
 287                /* The CT field must be 0=INVALID_RPI for the ECHO cmd */
 288                if (elscmd == ELS_CMD_ECHO)
 289                        icmd->ulpCt_l = 0; /* context = invalid RPI */
 290                else
 291                        icmd->ulpCt_l = 1; /* context = VPI */
 292        }
 293
 294        bpl = (struct ulp_bde64 *) pbuflist->virt;
 295        bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
 296        bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
 297        bpl->tus.f.bdeSize = cmdSize;
 298        bpl->tus.f.bdeFlags = 0;
 299        bpl->tus.w = le32_to_cpu(bpl->tus.w);
 300
 301        if (expectRsp) {
 302                bpl++;
 303                bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
 304                bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
 305                bpl->tus.f.bdeSize = FCELSSIZE;
 306                bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
 307                bpl->tus.w = le32_to_cpu(bpl->tus.w);
 308        }
 309
 310        elsiocb->context2 = pcmd;
 311        elsiocb->context3 = pbuflist;
 312        elsiocb->retry = retry;
 313        elsiocb->vport = vport;
 314        elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
 315
 316        if (prsp) {
 317                list_add(&prsp->list, &pcmd->list);
 318        }
 319        if (expectRsp) {
 320                /* Xmit ELS command <elsCmd> to remote NPORT <did> */
 321                lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 322                                 "0116 Xmit ELS command x%x to remote "
 323                                 "NPORT x%x I/O tag: x%x, port state:x%x "
 324                                 "rpi x%x fc_flag:x%x nlp_flag:x%x vport:x%p\n",
 325                                 elscmd, did, elsiocb->iotag,
 326                                 vport->port_state, ndlp->nlp_rpi,
 327                                 vport->fc_flag, ndlp->nlp_flag, vport);
 328        } else {
 329                /* Xmit ELS response <elsCmd> to remote NPORT <did> */
 330                lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 331                                 "0117 Xmit ELS response x%x to remote "
 332                                 "NPORT x%x I/O tag: x%x, size: x%x "
 333                                 "port_state x%x  rpi x%x fc_flag x%x\n",
 334                                 elscmd, ndlp->nlp_DID, elsiocb->iotag,
 335                                 cmdSize, vport->port_state,
 336                                 ndlp->nlp_rpi, vport->fc_flag);
 337        }
 338        return elsiocb;
 339
 340els_iocb_free_pbuf_exit:
 341        if (expectRsp)
 342                lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
 343        kfree(pbuflist);
 344
 345els_iocb_free_prsp_exit:
 346        lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
 347        kfree(prsp);
 348
 349els_iocb_free_pcmb_exit:
 350        kfree(pcmd);
 351        lpfc_sli_release_iocbq(phba, elsiocb);
 352        return NULL;
 353}
 354
 355/**
 356 * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
 357 * @vport: pointer to a host virtual N_Port data structure.
 358 *
 359 * This routine issues a fabric registration login for a @vport. An
 360 * active ndlp node with Fabric_DID must already exist for this @vport.
 361 * The routine invokes two mailbox commands to carry out fabric registration
 362 * login through the HBA firmware: the first mailbox command requests the
 363 * HBA to perform link configuration for the @vport; and the second mailbox
 364 * command requests the HBA to perform the actual fabric registration login
 365 * with the @vport.
 366 *
 367 * Return code
 368 *   0 - successfully issued fabric registration login for @vport
 369 *   -ENXIO -- failed to issue fabric registration login for @vport
 370 **/
 371int
 372lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
 373{
 374        struct lpfc_hba  *phba = vport->phba;
 375        LPFC_MBOXQ_t *mbox;
 376        struct lpfc_dmabuf *mp;
 377        struct lpfc_nodelist *ndlp;
 378        struct serv_parm *sp;
 379        int rc;
 380        int err = 0;
 381
 382        sp = &phba->fc_fabparam;
 383        ndlp = lpfc_findnode_did(vport, Fabric_DID);
 384        if (!ndlp) {
 385                err = 1;
 386                goto fail;
 387        }
 388
 389        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 390        if (!mbox) {
 391                err = 2;
 392                goto fail;
 393        }
 394
 395        vport->port_state = LPFC_FABRIC_CFG_LINK;
 396        lpfc_config_link(phba, mbox);
 397        mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
 398        mbox->vport = vport;
 399
 400        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 401        if (rc == MBX_NOT_FINISHED) {
 402                err = 3;
 403                goto fail_free_mbox;
 404        }
 405
 406        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 407        if (!mbox) {
 408                err = 4;
 409                goto fail;
 410        }
 411        rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
 412                          ndlp->nlp_rpi);
 413        if (rc) {
 414                err = 5;
 415                goto fail_free_mbox;
 416        }
 417
 418        mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
 419        mbox->vport = vport;
 420        /* increment the reference count on ndlp to hold reference
 421         * for the callback routine.
 422         */
 423        mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
 424        if (!mbox->ctx_ndlp) {
 425                err = 6;
 426                goto fail_no_ndlp;
 427        }
 428
 429        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 430        if (rc == MBX_NOT_FINISHED) {
 431                err = 7;
 432                goto fail_issue_reg_login;
 433        }
 434
 435        return 0;
 436
 437fail_issue_reg_login:
 438        /* decrement the reference count on ndlp just incremented
 439         * for the failed mbox command.
 440         */
 441        lpfc_nlp_put(ndlp);
 442fail_no_ndlp:
 443        mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
 444        lpfc_mbuf_free(phba, mp->virt, mp->phys);
 445        kfree(mp);
 446fail_free_mbox:
 447        mempool_free(mbox, phba->mbox_mem_pool);
 448
 449fail:
 450        lpfc_vport_set_state(vport, FC_VPORT_FAILED);
 451        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 452                         "0249 Cannot issue Register Fabric login: Err %d\n",
 453                         err);
 454        return -ENXIO;
 455}
 456
 457/**
 458 * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
 459 * @vport: pointer to a host virtual N_Port data structure.
 460 *
 461 * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
 462 * the @vport. This mailbox command is necessary for SLI4 port only.
 463 *
 464 * Return code
 465 *   0 - successfully issued REG_VFI for @vport
 466 *   A failure code otherwise.
 467 **/
 468int
 469lpfc_issue_reg_vfi(struct lpfc_vport *vport)
 470{
 471        struct lpfc_hba  *phba = vport->phba;
 472        LPFC_MBOXQ_t *mboxq = NULL;
 473        struct lpfc_nodelist *ndlp;
 474        struct lpfc_dmabuf *dmabuf = NULL;
 475        int rc = 0;
 476
 477        /* move forward in case of SLI4 FC port loopback test and pt2pt mode */
 478        if ((phba->sli_rev == LPFC_SLI_REV4) &&
 479            !(phba->link_flag & LS_LOOPBACK_MODE) &&
 480            !(vport->fc_flag & FC_PT2PT)) {
 481                ndlp = lpfc_findnode_did(vport, Fabric_DID);
 482                if (!ndlp) {
 483                        rc = -ENODEV;
 484                        goto fail;
 485                }
 486        }
 487
 488        mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 489        if (!mboxq) {
 490                rc = -ENOMEM;
 491                goto fail;
 492        }
 493
 494        /* Supply CSP's only if we are fabric connect or pt-to-pt connect */
 495        if ((vport->fc_flag & FC_FABRIC) || (vport->fc_flag & FC_PT2PT)) {
 496                dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
 497                if (!dmabuf) {
 498                        rc = -ENOMEM;
 499                        goto fail;
 500                }
 501                dmabuf->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &dmabuf->phys);
 502                if (!dmabuf->virt) {
 503                        rc = -ENOMEM;
 504                        goto fail;
 505                }
 506                memcpy(dmabuf->virt, &phba->fc_fabparam,
 507                       sizeof(struct serv_parm));
 508        }
 509
 510        vport->port_state = LPFC_FABRIC_CFG_LINK;
 511        if (dmabuf)
 512                lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
 513        else
 514                lpfc_reg_vfi(mboxq, vport, 0);
 515
 516        mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
 517        mboxq->vport = vport;
 518        mboxq->ctx_buf = dmabuf;
 519        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
 520        if (rc == MBX_NOT_FINISHED) {
 521                rc = -ENXIO;
 522                goto fail;
 523        }
 524        return 0;
 525
 526fail:
 527        if (mboxq)
 528                mempool_free(mboxq, phba->mbox_mem_pool);
 529        if (dmabuf) {
 530                if (dmabuf->virt)
 531                        lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
 532                kfree(dmabuf);
 533        }
 534
 535        lpfc_vport_set_state(vport, FC_VPORT_FAILED);
 536        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 537                         "0289 Issue Register VFI failed: Err %d\n", rc);
 538        return rc;
 539}
 540
 541/**
 542 * lpfc_issue_unreg_vfi - Unregister VFI for this vport's fabric login
 543 * @vport: pointer to a host virtual N_Port data structure.
 544 *
 545 * This routine issues a UNREG_VFI mailbox with the vfi, vpi, fcfi triplet for
 546 * the @vport. This mailbox command is necessary for SLI4 port only.
 547 *
 548 * Return code
 549 *   0 - successfully issued REG_VFI for @vport
 550 *   A failure code otherwise.
 551 **/
 552int
 553lpfc_issue_unreg_vfi(struct lpfc_vport *vport)
 554{
 555        struct lpfc_hba *phba = vport->phba;
 556        struct Scsi_Host *shost;
 557        LPFC_MBOXQ_t *mboxq;
 558        int rc;
 559
 560        mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 561        if (!mboxq) {
 562                lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 563                                "2556 UNREG_VFI mbox allocation failed"
 564                                "HBA state x%x\n", phba->pport->port_state);
 565                return -ENOMEM;
 566        }
 567
 568        lpfc_unreg_vfi(mboxq, vport);
 569        mboxq->vport = vport;
 570        mboxq->mbox_cmpl = lpfc_unregister_vfi_cmpl;
 571
 572        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
 573        if (rc == MBX_NOT_FINISHED) {
 574                lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 575                                "2557 UNREG_VFI issue mbox failed rc x%x "
 576                                "HBA state x%x\n",
 577                                rc, phba->pport->port_state);
 578                mempool_free(mboxq, phba->mbox_mem_pool);
 579                return -EIO;
 580        }
 581
 582        shost = lpfc_shost_from_vport(vport);
 583        spin_lock_irq(shost->host_lock);
 584        vport->fc_flag &= ~FC_VFI_REGISTERED;
 585        spin_unlock_irq(shost->host_lock);
 586        return 0;
 587}
 588
 589/**
 590 * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
 591 * @vport: pointer to a host virtual N_Port data structure.
 592 * @sp: pointer to service parameter data structure.
 593 *
 594 * This routine is called from FLOGI/FDISC completion handler functions.
 595 * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
 596 * node nodename is changed in the completion service parameter else return
 597 * 0. This function also set flag in the vport data structure to delay
 598 * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
 599 * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
 600 * node nodename is changed in the completion service parameter.
 601 *
 602 * Return code
 603 *   0 - FCID and Fabric Nodename and Fabric portname is not changed.
 604 *   1 - FCID or Fabric Nodename or Fabric portname is changed.
 605 *
 606 **/
 607static uint8_t
 608lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
 609                struct serv_parm *sp)
 610{
 611        struct lpfc_hba *phba = vport->phba;
 612        uint8_t fabric_param_changed = 0;
 613        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 614
 615        if ((vport->fc_prevDID != vport->fc_myDID) ||
 616                memcmp(&vport->fabric_portname, &sp->portName,
 617                        sizeof(struct lpfc_name)) ||
 618                memcmp(&vport->fabric_nodename, &sp->nodeName,
 619                        sizeof(struct lpfc_name)) ||
 620                (vport->vport_flag & FAWWPN_PARAM_CHG)) {
 621                fabric_param_changed = 1;
 622                vport->vport_flag &= ~FAWWPN_PARAM_CHG;
 623        }
 624        /*
 625         * Word 1 Bit 31 in common service parameter is overloaded.
 626         * Word 1 Bit 31 in FLOGI request is multiple NPort request
 627         * Word 1 Bit 31 in FLOGI response is clean address bit
 628         *
 629         * If fabric parameter is changed and clean address bit is
 630         * cleared delay nport discovery if
 631         * - vport->fc_prevDID != 0 (not initial discovery) OR
 632         * - lpfc_delay_discovery module parameter is set.
 633         */
 634        if (fabric_param_changed && !sp->cmn.clean_address_bit &&
 635            (vport->fc_prevDID || phba->cfg_delay_discovery)) {
 636                spin_lock_irq(shost->host_lock);
 637                vport->fc_flag |= FC_DISC_DELAYED;
 638                spin_unlock_irq(shost->host_lock);
 639        }
 640
 641        return fabric_param_changed;
 642}
 643
 644
 645/**
 646 * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
 647 * @vport: pointer to a host virtual N_Port data structure.
 648 * @ndlp: pointer to a node-list data structure.
 649 * @sp: pointer to service parameter data structure.
 650 * @irsp: pointer to the IOCB within the lpfc response IOCB.
 651 *
 652 * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
 653 * function to handle the completion of a Fabric Login (FLOGI) into a fabric
 654 * port in a fabric topology. It properly sets up the parameters to the @ndlp
 655 * from the IOCB response. It also check the newly assigned N_Port ID to the
 656 * @vport against the previously assigned N_Port ID. If it is different from
 657 * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
 658 * is invoked on all the remaining nodes with the @vport to unregister the
 659 * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
 660 * is invoked to register login to the fabric.
 661 *
 662 * Return code
 663 *   0 - Success (currently, always return 0)
 664 **/
 665static int
 666lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 667                           struct serv_parm *sp, IOCB_t *irsp)
 668{
 669        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 670        struct lpfc_hba  *phba = vport->phba;
 671        struct lpfc_nodelist *np;
 672        struct lpfc_nodelist *next_np;
 673        uint8_t fabric_param_changed;
 674
 675        spin_lock_irq(shost->host_lock);
 676        vport->fc_flag |= FC_FABRIC;
 677        spin_unlock_irq(shost->host_lock);
 678
 679        phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
 680        if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
 681                phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
 682
 683        phba->fc_edtovResol = sp->cmn.edtovResolution;
 684        phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
 685
 686        if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
 687                spin_lock_irq(shost->host_lock);
 688                vport->fc_flag |= FC_PUBLIC_LOOP;
 689                spin_unlock_irq(shost->host_lock);
 690        }
 691
 692        vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
 693        memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
 694        memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
 695        ndlp->nlp_class_sup = 0;
 696        if (sp->cls1.classValid)
 697                ndlp->nlp_class_sup |= FC_COS_CLASS1;
 698        if (sp->cls2.classValid)
 699                ndlp->nlp_class_sup |= FC_COS_CLASS2;
 700        if (sp->cls3.classValid)
 701                ndlp->nlp_class_sup |= FC_COS_CLASS3;
 702        if (sp->cls4.classValid)
 703                ndlp->nlp_class_sup |= FC_COS_CLASS4;
 704        ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
 705                                sp->cmn.bbRcvSizeLsb;
 706
 707        fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
 708        if (fabric_param_changed) {
 709                /* Reset FDMI attribute masks based on config parameter */
 710                if (phba->cfg_enable_SmartSAN ||
 711                    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
 712                        /* Setup appropriate attribute masks */
 713                        vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
 714                        if (phba->cfg_enable_SmartSAN)
 715                                vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
 716                        else
 717                                vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
 718                } else {
 719                        vport->fdmi_hba_mask = 0;
 720                        vport->fdmi_port_mask = 0;
 721                }
 722
 723        }
 724        memcpy(&vport->fabric_portname, &sp->portName,
 725                        sizeof(struct lpfc_name));
 726        memcpy(&vport->fabric_nodename, &sp->nodeName,
 727                        sizeof(struct lpfc_name));
 728        memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
 729
 730        if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
 731                if (sp->cmn.response_multiple_NPort) {
 732                        lpfc_printf_vlog(vport, KERN_WARNING,
 733                                         LOG_ELS | LOG_VPORT,
 734                                         "1816 FLOGI NPIV supported, "
 735                                         "response data 0x%x\n",
 736                                         sp->cmn.response_multiple_NPort);
 737                        spin_lock_irq(&phba->hbalock);
 738                        phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
 739                        spin_unlock_irq(&phba->hbalock);
 740                } else {
 741                        /* Because we asked f/w for NPIV it still expects us
 742                        to call reg_vnpid at least for the physical host */
 743                        lpfc_printf_vlog(vport, KERN_WARNING,
 744                                         LOG_ELS | LOG_VPORT,
 745                                         "1817 Fabric does not support NPIV "
 746                                         "- configuring single port mode.\n");
 747                        spin_lock_irq(&phba->hbalock);
 748                        phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
 749                        spin_unlock_irq(&phba->hbalock);
 750                }
 751        }
 752
 753        /*
 754         * For FC we need to do some special processing because of the SLI
 755         * Port's default settings of the Common Service Parameters.
 756         */
 757        if ((phba->sli_rev == LPFC_SLI_REV4) &&
 758            (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)) {
 759                /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
 760                if (fabric_param_changed)
 761                        lpfc_unregister_fcf_prep(phba);
 762
 763                /* This should just update the VFI CSPs*/
 764                if (vport->fc_flag & FC_VFI_REGISTERED)
 765                        lpfc_issue_reg_vfi(vport);
 766        }
 767
 768        if (fabric_param_changed &&
 769                !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
 770
 771                /* If our NportID changed, we need to ensure all
 772                 * remaining NPORTs get unreg_login'ed.
 773                 */
 774                list_for_each_entry_safe(np, next_np,
 775                                        &vport->fc_nodes, nlp_listp) {
 776                        if ((np->nlp_state != NLP_STE_NPR_NODE) ||
 777                                   !(np->nlp_flag & NLP_NPR_ADISC))
 778                                continue;
 779                        spin_lock_irq(&np->lock);
 780                        np->nlp_flag &= ~NLP_NPR_ADISC;
 781                        spin_unlock_irq(&np->lock);
 782                        lpfc_unreg_rpi(vport, np);
 783                }
 784                lpfc_cleanup_pending_mbox(vport);
 785
 786                if (phba->sli_rev == LPFC_SLI_REV4) {
 787                        lpfc_sli4_unreg_all_rpis(vport);
 788                        lpfc_mbx_unreg_vpi(vport);
 789                        spin_lock_irq(shost->host_lock);
 790                        vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
 791                        spin_unlock_irq(shost->host_lock);
 792                }
 793
 794                /*
 795                 * For SLI3 and SLI4, the VPI needs to be reregistered in
 796                 * response to this fabric parameter change event.
 797                 */
 798                spin_lock_irq(shost->host_lock);
 799                vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
 800                spin_unlock_irq(shost->host_lock);
 801        } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
 802                !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
 803                        /*
 804                         * Driver needs to re-reg VPI in order for f/w
 805                         * to update the MAC address.
 806                         */
 807                        lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
 808                        lpfc_register_new_vport(phba, vport, ndlp);
 809                        return 0;
 810        }
 811
 812        if (phba->sli_rev < LPFC_SLI_REV4) {
 813                lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
 814                if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
 815                    vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
 816                        lpfc_register_new_vport(phba, vport, ndlp);
 817                else
 818                        lpfc_issue_fabric_reglogin(vport);
 819        } else {
 820                ndlp->nlp_type |= NLP_FABRIC;
 821                lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
 822                if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
 823                        (vport->vpi_state & LPFC_VPI_REGISTERED)) {
 824                        lpfc_start_fdiscs(phba);
 825                        lpfc_do_scr_ns_plogi(phba, vport);
 826                } else if (vport->fc_flag & FC_VFI_REGISTERED)
 827                        lpfc_issue_init_vpi(vport);
 828                else {
 829                        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 830                                        "3135 Need register VFI: (x%x/%x)\n",
 831                                        vport->fc_prevDID, vport->fc_myDID);
 832                        lpfc_issue_reg_vfi(vport);
 833                }
 834        }
 835        return 0;
 836}
 837
 838/**
 839 * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
 840 * @vport: pointer to a host virtual N_Port data structure.
 841 * @ndlp: pointer to a node-list data structure.
 842 * @sp: pointer to service parameter data structure.
 843 *
 844 * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
 845 * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
 846 * in a point-to-point topology. First, the @vport's N_Port Name is compared
 847 * with the received N_Port Name: if the @vport's N_Port Name is greater than
 848 * the received N_Port Name lexicographically, this node shall assign local
 849 * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
 850 * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
 851 * this node shall just wait for the remote node to issue PLOGI and assign
 852 * N_Port IDs.
 853 *
 854 * Return code
 855 *   0 - Success
 856 *   -ENXIO - Fail
 857 **/
 858static int
 859lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 860                          struct serv_parm *sp)
 861{
 862        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 863        struct lpfc_hba  *phba = vport->phba;
 864        LPFC_MBOXQ_t *mbox;
 865        int rc;
 866
 867        spin_lock_irq(shost->host_lock);
 868        vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
 869        vport->fc_flag |= FC_PT2PT;
 870        spin_unlock_irq(shost->host_lock);
 871
 872        /* If we are pt2pt with another NPort, force NPIV off! */
 873        phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
 874
 875        /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
 876        if ((phba->sli_rev == LPFC_SLI_REV4) && phba->fc_topology_changed) {
 877                lpfc_unregister_fcf_prep(phba);
 878
 879                spin_lock_irq(shost->host_lock);
 880                vport->fc_flag &= ~FC_VFI_REGISTERED;
 881                spin_unlock_irq(shost->host_lock);
 882                phba->fc_topology_changed = 0;
 883        }
 884
 885        rc = memcmp(&vport->fc_portname, &sp->portName,
 886                    sizeof(vport->fc_portname));
 887
 888        if (rc >= 0) {
 889                /* This side will initiate the PLOGI */
 890                spin_lock_irq(shost->host_lock);
 891                vport->fc_flag |= FC_PT2PT_PLOGI;
 892                spin_unlock_irq(shost->host_lock);
 893
 894                /*
 895                 * N_Port ID cannot be 0, set our Id to LocalID
 896                 * the other side will be RemoteID.
 897                 */
 898
 899                /* not equal */
 900                if (rc)
 901                        vport->fc_myDID = PT2PT_LocalID;
 902
 903                /* Decrement ndlp reference count indicating that ndlp can be
 904                 * safely released when other references to it are done.
 905                 */
 906                lpfc_nlp_put(ndlp);
 907
 908                ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
 909                if (!ndlp) {
 910                        /*
 911                         * Cannot find existing Fabric ndlp, so allocate a
 912                         * new one
 913                         */
 914                        ndlp = lpfc_nlp_init(vport, PT2PT_RemoteID);
 915                        if (!ndlp)
 916                                goto fail;
 917                }
 918
 919                memcpy(&ndlp->nlp_portname, &sp->portName,
 920                       sizeof(struct lpfc_name));
 921                memcpy(&ndlp->nlp_nodename, &sp->nodeName,
 922                       sizeof(struct lpfc_name));
 923                /* Set state will put ndlp onto node list if not already done */
 924                lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
 925                spin_lock_irq(&ndlp->lock);
 926                ndlp->nlp_flag |= NLP_NPR_2B_DISC;
 927                spin_unlock_irq(&ndlp->lock);
 928
 929                mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 930                if (!mbox)
 931                        goto fail;
 932
 933                lpfc_config_link(phba, mbox);
 934
 935                mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
 936                mbox->vport = vport;
 937                rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 938                if (rc == MBX_NOT_FINISHED) {
 939                        mempool_free(mbox, phba->mbox_mem_pool);
 940                        goto fail;
 941                }
 942        } else {
 943                /* This side will wait for the PLOGI, decrement ndlp reference
 944                 * count indicating that ndlp can be released when other
 945                 * references to it are done.
 946                 */
 947                lpfc_nlp_put(ndlp);
 948
 949                /* Start discovery - this should just do CLEAR_LA */
 950                lpfc_disc_start(vport);
 951        }
 952
 953        return 0;
 954fail:
 955        return -ENXIO;
 956}
 957
 958/**
 959 * lpfc_cmpl_els_flogi - Completion callback function for flogi
 960 * @phba: pointer to lpfc hba data structure.
 961 * @cmdiocb: pointer to lpfc command iocb data structure.
 962 * @rspiocb: pointer to lpfc response iocb data structure.
 963 *
 964 * This routine is the top-level completion callback function for issuing
 965 * a Fabric Login (FLOGI) command. If the response IOCB reported error,
 966 * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
 967 * retry has been made (either immediately or delayed with lpfc_els_retry()
 968 * returning 1), the command IOCB will be released and function returned.
 969 * If the retry attempt has been given up (possibly reach the maximum
 970 * number of retries), one additional decrement of ndlp reference shall be
 971 * invoked before going out after releasing the command IOCB. This will
 972 * actually release the remote node (Note, lpfc_els_free_iocb() will also
 973 * invoke one decrement of ndlp reference count). If no error reported in
 974 * the IOCB status, the command Port ID field is used to determine whether
 975 * this is a point-to-point topology or a fabric topology: if the Port ID
 976 * field is assigned, it is a fabric topology; otherwise, it is a
 977 * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
 978 * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
 979 * specific topology completion conditions.
 980 **/
 981static void
 982lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 983                    struct lpfc_iocbq *rspiocb)
 984{
 985        struct lpfc_vport *vport = cmdiocb->vport;
 986        struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
 987        IOCB_t *irsp = &rspiocb->iocb;
 988        struct lpfc_nodelist *ndlp = cmdiocb->context1;
 989        struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
 990        struct serv_parm *sp;
 991        uint16_t fcf_index;
 992        int rc;
 993
 994        /* Check to see if link went down during discovery */
 995        if (lpfc_els_chk_latt(vport)) {
 996                /* One additional decrement on node reference count to
 997                 * trigger the release of the node
 998                 */
 999                lpfc_nlp_put(ndlp);
1000                goto out;
1001        }
1002
1003        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1004                "FLOGI cmpl:      status:x%x/x%x state:x%x",
1005                irsp->ulpStatus, irsp->un.ulpWord[4],
1006                vport->port_state);
1007
1008        if (irsp->ulpStatus) {
1009                /*
1010                 * In case of FIP mode, perform roundrobin FCF failover
1011                 * due to new FCF discovery
1012                 */
1013                if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
1014                    (phba->fcf.fcf_flag & FCF_DISCOVERY)) {
1015                        if (phba->link_state < LPFC_LINK_UP)
1016                                goto stop_rr_fcf_flogi;
1017                        if ((phba->fcoe_cvl_eventtag_attn ==
1018                             phba->fcoe_cvl_eventtag) &&
1019                            (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1020                            ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1021                            IOERR_SLI_ABORTED))
1022                                goto stop_rr_fcf_flogi;
1023                        else
1024                                phba->fcoe_cvl_eventtag_attn =
1025                                        phba->fcoe_cvl_eventtag;
1026                        lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1027                                        "2611 FLOGI failed on FCF (x%x), "
1028                                        "status:x%x/x%x, tmo:x%x, perform "
1029                                        "roundrobin FCF failover\n",
1030                                        phba->fcf.current_rec.fcf_indx,
1031                                        irsp->ulpStatus, irsp->un.ulpWord[4],
1032                                        irsp->ulpTimeout);
1033                        lpfc_sli4_set_fcf_flogi_fail(phba,
1034                                        phba->fcf.current_rec.fcf_indx);
1035                        fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
1036                        rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
1037                        if (rc)
1038                                goto out;
1039                }
1040
1041stop_rr_fcf_flogi:
1042                /* FLOGI failure */
1043                if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1044                      ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1045                                        IOERR_LOOP_OPEN_FAILURE)))
1046                        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1047                                         "2858 FLOGI failure Status:x%x/x%x TMO"
1048                                         ":x%x Data x%x x%x\n",
1049                                         irsp->ulpStatus, irsp->un.ulpWord[4],
1050                                         irsp->ulpTimeout, phba->hba_flag,
1051                                         phba->fcf.fcf_flag);
1052
1053                /* Check for retry */
1054                if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1055                        goto out;
1056
1057                lpfc_printf_vlog(vport, KERN_WARNING, LOG_TRACE_EVENT,
1058                                 "0150 FLOGI failure Status:x%x/x%x "
1059                                 "xri x%x TMO:x%x\n",
1060                                 irsp->ulpStatus, irsp->un.ulpWord[4],
1061                                 cmdiocb->sli4_xritag, irsp->ulpTimeout);
1062
1063                /* If this is not a loop open failure, bail out */
1064                if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1065                      ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1066                                        IOERR_LOOP_OPEN_FAILURE)))
1067                        goto flogifail;
1068
1069                /* FLOGI failed, so there is no fabric */
1070                spin_lock_irq(shost->host_lock);
1071                vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1072                spin_unlock_irq(shost->host_lock);
1073
1074                /* If private loop, then allow max outstanding els to be
1075                 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
1076                 * alpa map would take too long otherwise.
1077                 */
1078                if (phba->alpa_map[0] == 0)
1079                        vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
1080                if ((phba->sli_rev == LPFC_SLI_REV4) &&
1081                    (!(vport->fc_flag & FC_VFI_REGISTERED) ||
1082                     (vport->fc_prevDID != vport->fc_myDID) ||
1083                        phba->fc_topology_changed)) {
1084                        if (vport->fc_flag & FC_VFI_REGISTERED) {
1085                                if (phba->fc_topology_changed) {
1086                                        lpfc_unregister_fcf_prep(phba);
1087                                        spin_lock_irq(shost->host_lock);
1088                                        vport->fc_flag &= ~FC_VFI_REGISTERED;
1089                                        spin_unlock_irq(shost->host_lock);
1090                                        phba->fc_topology_changed = 0;
1091                                } else {
1092                                        lpfc_sli4_unreg_all_rpis(vport);
1093                                }
1094                        }
1095
1096                        /* Do not register VFI if the driver aborted FLOGI */
1097                        if (!lpfc_error_lost_link(irsp))
1098                                lpfc_issue_reg_vfi(vport);
1099
1100                        lpfc_nlp_put(ndlp);
1101                        goto out;
1102                }
1103                goto flogifail;
1104        }
1105        spin_lock_irq(shost->host_lock);
1106        vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
1107        vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
1108        spin_unlock_irq(shost->host_lock);
1109
1110        /*
1111         * The FLogI succeeded.  Sync the data for the CPU before
1112         * accessing it.
1113         */
1114        prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1115        if (!prsp)
1116                goto out;
1117        sp = prsp->virt + sizeof(uint32_t);
1118
1119        /* FLOGI completes successfully */
1120        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1121                         "0101 FLOGI completes successfully, I/O tag:x%x, "
1122                         "xri x%x Data: x%x x%x x%x x%x x%x x%x x%x\n",
1123                         cmdiocb->iotag, cmdiocb->sli4_xritag,
1124                         irsp->un.ulpWord[4], sp->cmn.e_d_tov,
1125                         sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
1126                         vport->port_state, vport->fc_flag,
1127                         sp->cmn.priority_tagging);
1128
1129        if (sp->cmn.priority_tagging)
1130                vport->vmid_flag |= LPFC_VMID_ISSUE_QFPA;
1131
1132        if (vport->port_state == LPFC_FLOGI) {
1133                /*
1134                 * If Common Service Parameters indicate Nport
1135                 * we are point to point, if Fport we are Fabric.
1136                 */
1137                if (sp->cmn.fPort)
1138                        rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
1139                else if (!(phba->hba_flag & HBA_FCOE_MODE))
1140                        rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
1141                else {
1142                        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1143                                "2831 FLOGI response with cleared Fabric "
1144                                "bit fcf_index 0x%x "
1145                                "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
1146                                "Fabric Name "
1147                                "%02x%02x%02x%02x%02x%02x%02x%02x\n",
1148                                phba->fcf.current_rec.fcf_indx,
1149                                phba->fcf.current_rec.switch_name[0],
1150                                phba->fcf.current_rec.switch_name[1],
1151                                phba->fcf.current_rec.switch_name[2],
1152                                phba->fcf.current_rec.switch_name[3],
1153                                phba->fcf.current_rec.switch_name[4],
1154                                phba->fcf.current_rec.switch_name[5],
1155                                phba->fcf.current_rec.switch_name[6],
1156                                phba->fcf.current_rec.switch_name[7],
1157                                phba->fcf.current_rec.fabric_name[0],
1158                                phba->fcf.current_rec.fabric_name[1],
1159                                phba->fcf.current_rec.fabric_name[2],
1160                                phba->fcf.current_rec.fabric_name[3],
1161                                phba->fcf.current_rec.fabric_name[4],
1162                                phba->fcf.current_rec.fabric_name[5],
1163                                phba->fcf.current_rec.fabric_name[6],
1164                                phba->fcf.current_rec.fabric_name[7]);
1165
1166                        lpfc_nlp_put(ndlp);
1167                        spin_lock_irq(&phba->hbalock);
1168                        phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1169                        phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1170                        spin_unlock_irq(&phba->hbalock);
1171                        phba->fcf.fcf_redisc_attempted = 0; /* reset */
1172                        goto out;
1173                }
1174                if (!rc) {
1175                        /* Mark the FCF discovery process done */
1176                        if (phba->hba_flag & HBA_FIP_SUPPORT)
1177                                lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
1178                                                LOG_ELS,
1179                                                "2769 FLOGI to FCF (x%x) "
1180                                                "completed successfully\n",
1181                                                phba->fcf.current_rec.fcf_indx);
1182                        spin_lock_irq(&phba->hbalock);
1183                        phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1184                        phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1185                        spin_unlock_irq(&phba->hbalock);
1186                        phba->fcf.fcf_redisc_attempted = 0; /* reset */
1187                        goto out;
1188                }
1189        } else if (vport->port_state > LPFC_FLOGI &&
1190                   vport->fc_flag & FC_PT2PT) {
1191                /*
1192                 * In a p2p topology, it is possible that discovery has
1193                 * already progressed, and this completion can be ignored.
1194                 * Recheck the indicated topology.
1195                 */
1196                if (!sp->cmn.fPort)
1197                        goto out;
1198        }
1199
1200flogifail:
1201        spin_lock_irq(&phba->hbalock);
1202        phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1203        spin_unlock_irq(&phba->hbalock);
1204
1205        if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)))
1206                lpfc_nlp_put(ndlp);
1207        if (!lpfc_error_lost_link(irsp)) {
1208                /* FLOGI failed, so just use loop map to make discovery list */
1209                lpfc_disc_list_loopmap(vport);
1210
1211                /* Start discovery */
1212                lpfc_disc_start(vport);
1213        } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
1214                        (((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1215                         IOERR_SLI_ABORTED) &&
1216                        ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1217                         IOERR_SLI_DOWN))) &&
1218                        (phba->link_state != LPFC_CLEAR_LA)) {
1219                /* If FLOGI failed enable link interrupt. */
1220                lpfc_issue_clear_la(phba, vport);
1221        }
1222out:
1223        phba->hba_flag &= ~HBA_FLOGI_OUTSTANDING;
1224        lpfc_els_free_iocb(phba, cmdiocb);
1225        lpfc_nlp_put(ndlp);
1226}
1227
1228/**
1229 * lpfc_cmpl_els_link_down - Completion callback function for ELS command
1230 *                           aborted during a link down
1231 * @phba: pointer to lpfc hba data structure.
1232 * @cmdiocb: pointer to lpfc command iocb data structure.
1233 * @rspiocb: pointer to lpfc response iocb data structure.
1234 *
1235 */
1236static void
1237lpfc_cmpl_els_link_down(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1238                        struct lpfc_iocbq *rspiocb)
1239{
1240        IOCB_t *irsp;
1241        uint32_t *pcmd;
1242        uint32_t cmd;
1243
1244        pcmd = (uint32_t *)(((struct lpfc_dmabuf *)cmdiocb->context2)->virt);
1245        cmd = *pcmd;
1246        irsp = &rspiocb->iocb;
1247
1248        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1249                        "6445 ELS completes after LINK_DOWN: "
1250                        " Status %x/%x cmd x%x flg x%x\n",
1251                        irsp->ulpStatus, irsp->un.ulpWord[4], cmd,
1252                        cmdiocb->iocb_flag);
1253
1254        if (cmdiocb->iocb_flag & LPFC_IO_FABRIC) {
1255                cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
1256                atomic_dec(&phba->fabric_iocb_count);
1257        }
1258        lpfc_els_free_iocb(phba, cmdiocb);
1259}
1260
1261/**
1262 * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
1263 * @vport: pointer to a host virtual N_Port data structure.
1264 * @ndlp: pointer to a node-list data structure.
1265 * @retry: number of retries to the command IOCB.
1266 *
1267 * This routine issues a Fabric Login (FLOGI) Request ELS command
1268 * for a @vport. The initiator service parameters are put into the payload
1269 * of the FLOGI Request IOCB and the top-level callback function pointer
1270 * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1271 * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1272 * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1273 *
1274 * Note that the ndlp reference count will be incremented by 1 for holding the
1275 * ndlp and the reference to ndlp will be stored into the context1 field of
1276 * the IOCB for the completion callback function to the FLOGI ELS command.
1277 *
1278 * Return code
1279 *   0 - successfully issued flogi iocb for @vport
1280 *   1 - failed to issue flogi iocb for @vport
1281 **/
1282static int
1283lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1284                     uint8_t retry)
1285{
1286        struct lpfc_hba  *phba = vport->phba;
1287        struct serv_parm *sp;
1288        IOCB_t *icmd;
1289        struct lpfc_iocbq *elsiocb;
1290        struct lpfc_iocbq defer_flogi_acc;
1291        uint8_t *pcmd;
1292        uint16_t cmdsize;
1293        uint32_t tmo, did;
1294        int rc;
1295
1296        cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1297        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1298                                     ndlp->nlp_DID, ELS_CMD_FLOGI);
1299
1300        if (!elsiocb)
1301                return 1;
1302
1303        icmd = &elsiocb->iocb;
1304        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1305
1306        /* For FLOGI request, remainder of payload is service parameters */
1307        *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1308        pcmd += sizeof(uint32_t);
1309        memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1310        sp = (struct serv_parm *) pcmd;
1311
1312        /* Setup CSPs accordingly for Fabric */
1313        sp->cmn.e_d_tov = 0;
1314        sp->cmn.w2.r_a_tov = 0;
1315        sp->cmn.virtual_fabric_support = 0;
1316        sp->cls1.classValid = 0;
1317        if (sp->cmn.fcphLow < FC_PH3)
1318                sp->cmn.fcphLow = FC_PH3;
1319        if (sp->cmn.fcphHigh < FC_PH3)
1320                sp->cmn.fcphHigh = FC_PH3;
1321
1322        /* Determine if switch supports priority tagging */
1323        if (phba->cfg_vmid_priority_tagging) {
1324                sp->cmn.priority_tagging = 1;
1325                /* lpfc_vmid_host_uuid is combination of wwpn and wwnn */
1326                if (uuid_is_null((uuid_t *)vport->lpfc_vmid_host_uuid)) {
1327                        memcpy(vport->lpfc_vmid_host_uuid, phba->wwpn,
1328                               sizeof(phba->wwpn));
1329                        memcpy(&vport->lpfc_vmid_host_uuid[8], phba->wwnn,
1330                               sizeof(phba->wwnn));
1331                }
1332        }
1333
1334        if  (phba->sli_rev == LPFC_SLI_REV4) {
1335                if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1336                    LPFC_SLI_INTF_IF_TYPE_0) {
1337                        elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1338                        elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1339                        /* FLOGI needs to be 3 for WQE FCFI */
1340                        /* Set the fcfi to the fcfi we registered with */
1341                        elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1342                }
1343                /* Can't do SLI4 class2 without support sequence coalescing */
1344                sp->cls2.classValid = 0;
1345                sp->cls2.seqDelivery = 0;
1346        } else {
1347                /* Historical, setting sequential-delivery bit for SLI3 */
1348                sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
1349                sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
1350                if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1351                        sp->cmn.request_multiple_Nport = 1;
1352                        /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1353                        icmd->ulpCt_h = 1;
1354                        icmd->ulpCt_l = 0;
1355                } else
1356                        sp->cmn.request_multiple_Nport = 0;
1357        }
1358
1359        if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
1360                icmd->un.elsreq64.myID = 0;
1361                icmd->un.elsreq64.fl = 1;
1362        }
1363
1364        tmo = phba->fc_ratov;
1365        phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1366        lpfc_set_disctmo(vport);
1367        phba->fc_ratov = tmo;
1368
1369        phba->fc_stat.elsXmitFLOGI++;
1370        elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
1371
1372        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1373                "Issue FLOGI:     opt:x%x",
1374                phba->sli3_options, 0, 0);
1375
1376        elsiocb->context1 = lpfc_nlp_get(ndlp);
1377        if (!elsiocb->context1) {
1378                lpfc_els_free_iocb(phba, elsiocb);
1379                return 1;
1380        }
1381
1382        rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1383        if (rc == IOCB_ERROR) {
1384                lpfc_els_free_iocb(phba, elsiocb);
1385                lpfc_nlp_put(ndlp);
1386                return 1;
1387        }
1388
1389        phba->hba_flag |= (HBA_FLOGI_ISSUED | HBA_FLOGI_OUTSTANDING);
1390
1391        /* Check for a deferred FLOGI ACC condition */
1392        if (phba->defer_flogi_acc_flag) {
1393                did = vport->fc_myDID;
1394                vport->fc_myDID = Fabric_DID;
1395
1396                memset(&defer_flogi_acc, 0, sizeof(struct lpfc_iocbq));
1397
1398                defer_flogi_acc.iocb.ulpContext = phba->defer_flogi_acc_rx_id;
1399                defer_flogi_acc.iocb.unsli3.rcvsli3.ox_id =
1400                                                phba->defer_flogi_acc_ox_id;
1401
1402                lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1403                                 "3354 Xmit deferred FLOGI ACC: rx_id: x%x,"
1404                                 " ox_id: x%x, hba_flag x%x\n",
1405                                 phba->defer_flogi_acc_rx_id,
1406                                 phba->defer_flogi_acc_ox_id, phba->hba_flag);
1407
1408                /* Send deferred FLOGI ACC */
1409                lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, &defer_flogi_acc,
1410                                 ndlp, NULL);
1411
1412                phba->defer_flogi_acc_flag = false;
1413
1414                vport->fc_myDID = did;
1415        }
1416
1417        return 0;
1418}
1419
1420/**
1421 * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1422 * @phba: pointer to lpfc hba data structure.
1423 *
1424 * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1425 * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1426 * list and issues an abort IOCB commond on each outstanding IOCB that
1427 * contains a active Fabric_DID ndlp. Note that this function is to issue
1428 * the abort IOCB command on all the outstanding IOCBs, thus when this
1429 * function returns, it does not guarantee all the IOCBs are actually aborted.
1430 *
1431 * Return code
1432 *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1433 **/
1434int
1435lpfc_els_abort_flogi(struct lpfc_hba *phba)
1436{
1437        struct lpfc_sli_ring *pring;
1438        struct lpfc_iocbq *iocb, *next_iocb;
1439        struct lpfc_nodelist *ndlp;
1440        IOCB_t *icmd;
1441
1442        /* Abort outstanding I/O on NPort <nlp_DID> */
1443        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1444                        "0201 Abort outstanding I/O on NPort x%x\n",
1445                        Fabric_DID);
1446
1447        pring = lpfc_phba_elsring(phba);
1448        if (unlikely(!pring))
1449                return -EIO;
1450
1451        /*
1452         * Check the txcmplq for an iocb that matches the nport the driver is
1453         * searching for.
1454         */
1455        spin_lock_irq(&phba->hbalock);
1456        list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1457                icmd = &iocb->iocb;
1458                if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
1459                        ndlp = (struct lpfc_nodelist *)(iocb->context1);
1460                        if (ndlp && ndlp->nlp_DID == Fabric_DID) {
1461                                if ((phba->pport->fc_flag & FC_PT2PT) &&
1462                                    !(phba->pport->fc_flag & FC_PT2PT_PLOGI))
1463                                        iocb->fabric_iocb_cmpl =
1464                                                lpfc_ignore_els_cmpl;
1465                                lpfc_sli_issue_abort_iotag(phba, pring, iocb,
1466                                                           NULL);
1467                        }
1468                }
1469        }
1470        /* Make sure HBA is alive */
1471        lpfc_issue_hb_tmo(phba);
1472
1473        spin_unlock_irq(&phba->hbalock);
1474
1475        return 0;
1476}
1477
1478/**
1479 * lpfc_initial_flogi - Issue an initial fabric login for a vport
1480 * @vport: pointer to a host virtual N_Port data structure.
1481 *
1482 * This routine issues an initial Fabric Login (FLOGI) for the @vport
1483 * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1484 * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1485 * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1486 * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1487 * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1488 * @vport.
1489 *
1490 * Return code
1491 *   0 - failed to issue initial flogi for @vport
1492 *   1 - successfully issued initial flogi for @vport
1493 **/
1494int
1495lpfc_initial_flogi(struct lpfc_vport *vport)
1496{
1497        struct lpfc_nodelist *ndlp;
1498
1499        vport->port_state = LPFC_FLOGI;
1500        lpfc_set_disctmo(vport);
1501
1502        /* First look for the Fabric ndlp */
1503        ndlp = lpfc_findnode_did(vport, Fabric_DID);
1504        if (!ndlp) {
1505                /* Cannot find existing Fabric ndlp, so allocate a new one */
1506                ndlp = lpfc_nlp_init(vport, Fabric_DID);
1507                if (!ndlp)
1508                        return 0;
1509                /* Set the node type */
1510                ndlp->nlp_type |= NLP_FABRIC;
1511
1512                /* Put ndlp onto node list */
1513                lpfc_enqueue_node(vport, ndlp);
1514        }
1515
1516        if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1517                /* This decrement of reference count to node shall kick off
1518                 * the release of the node.
1519                 */
1520                lpfc_nlp_put(ndlp);
1521                return 0;
1522        }
1523        return 1;
1524}
1525
1526/**
1527 * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1528 * @vport: pointer to a host virtual N_Port data structure.
1529 *
1530 * This routine issues an initial Fabric Discover (FDISC) for the @vport
1531 * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1532 * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1533 * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1534 * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1535 * is then invoked with the @vport and the ndlp to perform the FDISC for the
1536 * @vport.
1537 *
1538 * Return code
1539 *   0 - failed to issue initial fdisc for @vport
1540 *   1 - successfully issued initial fdisc for @vport
1541 **/
1542int
1543lpfc_initial_fdisc(struct lpfc_vport *vport)
1544{
1545        struct lpfc_nodelist *ndlp;
1546
1547        /* First look for the Fabric ndlp */
1548        ndlp = lpfc_findnode_did(vport, Fabric_DID);
1549        if (!ndlp) {
1550                /* Cannot find existing Fabric ndlp, so allocate a new one */
1551                ndlp = lpfc_nlp_init(vport, Fabric_DID);
1552                if (!ndlp)
1553                        return 0;
1554
1555                /* NPIV is only supported in Fabrics. */
1556                ndlp->nlp_type |= NLP_FABRIC;
1557
1558                /* Put ndlp onto node list */
1559                lpfc_enqueue_node(vport, ndlp);
1560        }
1561
1562        if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1563                /* decrement node reference count to trigger the release of
1564                 * the node.
1565                 */
1566                lpfc_nlp_put(ndlp);
1567                return 0;
1568        }
1569        return 1;
1570}
1571
1572/**
1573 * lpfc_more_plogi - Check and issue remaining plogis for a vport
1574 * @vport: pointer to a host virtual N_Port data structure.
1575 *
1576 * This routine checks whether there are more remaining Port Logins
1577 * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1578 * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1579 * to issue ELS PLOGIs up to the configured discover threads with the
1580 * @vport (@vport->cfg_discovery_threads). The function also decrement
1581 * the @vport's num_disc_node by 1 if it is not already 0.
1582 **/
1583void
1584lpfc_more_plogi(struct lpfc_vport *vport)
1585{
1586        if (vport->num_disc_nodes)
1587                vport->num_disc_nodes--;
1588
1589        /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1590        lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1591                         "0232 Continue discovery with %d PLOGIs to go "
1592                         "Data: x%x x%x x%x\n",
1593                         vport->num_disc_nodes, vport->fc_plogi_cnt,
1594                         vport->fc_flag, vport->port_state);
1595        /* Check to see if there are more PLOGIs to be sent */
1596        if (vport->fc_flag & FC_NLP_MORE)
1597                /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1598                lpfc_els_disc_plogi(vport);
1599
1600        return;
1601}
1602
1603/**
1604 * lpfc_plogi_confirm_nport - Confirm plogi wwpn matches stored ndlp
1605 * @phba: pointer to lpfc hba data structure.
1606 * @prsp: pointer to response IOCB payload.
1607 * @ndlp: pointer to a node-list data structure.
1608 *
1609 * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1610 * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1611 * The following cases are considered N_Port confirmed:
1612 * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1613 * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1614 * it does not have WWPN assigned either. If the WWPN is confirmed, the
1615 * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1616 * 1) if there is a node on vport list other than the @ndlp with the same
1617 * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1618 * on that node to release the RPI associated with the node; 2) if there is
1619 * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1620 * into, a new node shall be allocated (or activated). In either case, the
1621 * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1622 * be released and the new_ndlp shall be put on to the vport node list and
1623 * its pointer returned as the confirmed node.
1624 *
1625 * Note that before the @ndlp got "released", the keepDID from not-matching
1626 * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1627 * of the @ndlp. This is because the release of @ndlp is actually to put it
1628 * into an inactive state on the vport node list and the vport node list
1629 * management algorithm does not allow two node with a same DID.
1630 *
1631 * Return code
1632 *   pointer to the PLOGI N_Port @ndlp
1633 **/
1634static struct lpfc_nodelist *
1635lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1636                         struct lpfc_nodelist *ndlp)
1637{
1638        struct lpfc_vport *vport = ndlp->vport;
1639        struct lpfc_nodelist *new_ndlp;
1640        struct serv_parm *sp;
1641        uint8_t  name[sizeof(struct lpfc_name)];
1642        uint32_t keepDID = 0, keep_nlp_flag = 0;
1643        uint32_t keep_new_nlp_flag = 0;
1644        uint16_t keep_nlp_state;
1645        u32 keep_nlp_fc4_type = 0;
1646        struct lpfc_nvme_rport *keep_nrport = NULL;
1647        unsigned long *active_rrqs_xri_bitmap = NULL;
1648
1649        /* Fabric nodes can have the same WWPN so we don't bother searching
1650         * by WWPN.  Just return the ndlp that was given to us.
1651         */
1652        if (ndlp->nlp_type & NLP_FABRIC)
1653                return ndlp;
1654
1655        sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1656        memset(name, 0, sizeof(struct lpfc_name));
1657
1658        /* Now we find out if the NPort we are logging into, matches the WWPN
1659         * we have for that ndlp. If not, we have some work to do.
1660         */
1661        new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1662
1663        /* return immediately if the WWPN matches ndlp */
1664        if (!new_ndlp || (new_ndlp == ndlp))
1665                return ndlp;
1666
1667        if (phba->sli_rev == LPFC_SLI_REV4) {
1668                active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
1669                                                       GFP_KERNEL);
1670                if (active_rrqs_xri_bitmap)
1671                        memset(active_rrqs_xri_bitmap, 0,
1672                               phba->cfg_rrq_xri_bitmap_sz);
1673        }
1674
1675        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1676                         "3178 PLOGI confirm: ndlp x%x x%x x%x: "
1677                         "new_ndlp x%x x%x x%x\n",
1678                         ndlp->nlp_DID, ndlp->nlp_flag,  ndlp->nlp_fc4_type,
1679                         (new_ndlp ? new_ndlp->nlp_DID : 0),
1680                         (new_ndlp ? new_ndlp->nlp_flag : 0),
1681                         (new_ndlp ? new_ndlp->nlp_fc4_type : 0));
1682
1683        keepDID = new_ndlp->nlp_DID;
1684
1685        if (phba->sli_rev == LPFC_SLI_REV4 && active_rrqs_xri_bitmap)
1686                memcpy(active_rrqs_xri_bitmap, new_ndlp->active_rrqs_xri_bitmap,
1687                       phba->cfg_rrq_xri_bitmap_sz);
1688
1689        /* At this point in this routine, we know new_ndlp will be
1690         * returned. however, any previous GID_FTs that were done
1691         * would have updated nlp_fc4_type in ndlp, so we must ensure
1692         * new_ndlp has the right value.
1693         */
1694        if (vport->fc_flag & FC_FABRIC) {
1695                keep_nlp_fc4_type = new_ndlp->nlp_fc4_type;
1696                new_ndlp->nlp_fc4_type = ndlp->nlp_fc4_type;
1697        }
1698
1699        lpfc_unreg_rpi(vport, new_ndlp);
1700        new_ndlp->nlp_DID = ndlp->nlp_DID;
1701        new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1702        if (phba->sli_rev == LPFC_SLI_REV4)
1703                memcpy(new_ndlp->active_rrqs_xri_bitmap,
1704                       ndlp->active_rrqs_xri_bitmap,
1705                       phba->cfg_rrq_xri_bitmap_sz);
1706
1707        /* Lock both ndlps */
1708        spin_lock_irq(&ndlp->lock);
1709        spin_lock_irq(&new_ndlp->lock);
1710        keep_new_nlp_flag = new_ndlp->nlp_flag;
1711        keep_nlp_flag = ndlp->nlp_flag;
1712        new_ndlp->nlp_flag = ndlp->nlp_flag;
1713
1714        /* if new_ndlp had NLP_UNREG_INP set, keep it */
1715        if (keep_new_nlp_flag & NLP_UNREG_INP)
1716                new_ndlp->nlp_flag |= NLP_UNREG_INP;
1717        else
1718                new_ndlp->nlp_flag &= ~NLP_UNREG_INP;
1719
1720        /* if new_ndlp had NLP_RPI_REGISTERED set, keep it */
1721        if (keep_new_nlp_flag & NLP_RPI_REGISTERED)
1722                new_ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1723        else
1724                new_ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1725
1726        /*
1727         * Retain the DROPPED flag. This will take care of the init
1728         * refcount when affecting the state change
1729         */
1730        if (keep_new_nlp_flag & NLP_DROPPED)
1731                new_ndlp->nlp_flag |= NLP_DROPPED;
1732        else
1733                new_ndlp->nlp_flag &= ~NLP_DROPPED;
1734
1735        ndlp->nlp_flag = keep_new_nlp_flag;
1736
1737        /* if ndlp had NLP_UNREG_INP set, keep it */
1738        if (keep_nlp_flag & NLP_UNREG_INP)
1739                ndlp->nlp_flag |= NLP_UNREG_INP;
1740        else
1741                ndlp->nlp_flag &= ~NLP_UNREG_INP;
1742
1743        /* if ndlp had NLP_RPI_REGISTERED set, keep it */
1744        if (keep_nlp_flag & NLP_RPI_REGISTERED)
1745                ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1746        else
1747                ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1748
1749        /*
1750         * Retain the DROPPED flag. This will take care of the init
1751         * refcount when affecting the state change
1752         */
1753        if (keep_nlp_flag & NLP_DROPPED)
1754                ndlp->nlp_flag |= NLP_DROPPED;
1755        else
1756                ndlp->nlp_flag &= ~NLP_DROPPED;
1757
1758        spin_unlock_irq(&new_ndlp->lock);
1759        spin_unlock_irq(&ndlp->lock);
1760
1761        /* Set nlp_states accordingly */
1762        keep_nlp_state = new_ndlp->nlp_state;
1763        lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1764
1765        /* interchange the nvme remoteport structs */
1766        keep_nrport = new_ndlp->nrport;
1767        new_ndlp->nrport = ndlp->nrport;
1768
1769        /* Move this back to NPR state */
1770        if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1771                /* The new_ndlp is replacing ndlp totally, so we need
1772                 * to put ndlp on UNUSED list and try to free it.
1773                 */
1774                lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1775                         "3179 PLOGI confirm NEW: %x %x\n",
1776                         new_ndlp->nlp_DID, keepDID);
1777
1778                /* Two ndlps cannot have the same did on the nodelist.
1779                 * Note: for this case, ndlp has a NULL WWPN so setting
1780                 * the nlp_fc4_type isn't required.
1781                 */
1782                ndlp->nlp_DID = keepDID;
1783                lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1784                if (phba->sli_rev == LPFC_SLI_REV4 &&
1785                    active_rrqs_xri_bitmap)
1786                        memcpy(ndlp->active_rrqs_xri_bitmap,
1787                               active_rrqs_xri_bitmap,
1788                               phba->cfg_rrq_xri_bitmap_sz);
1789
1790        } else {
1791                lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1792                         "3180 PLOGI confirm SWAP: %x %x\n",
1793                         new_ndlp->nlp_DID, keepDID);
1794
1795                lpfc_unreg_rpi(vport, ndlp);
1796
1797                /* Two ndlps cannot have the same did and the fc4
1798                 * type must be transferred because the ndlp is in
1799                 * flight.
1800                 */
1801                ndlp->nlp_DID = keepDID;
1802                ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1803
1804                if (phba->sli_rev == LPFC_SLI_REV4 &&
1805                    active_rrqs_xri_bitmap)
1806                        memcpy(ndlp->active_rrqs_xri_bitmap,
1807                               active_rrqs_xri_bitmap,
1808                               phba->cfg_rrq_xri_bitmap_sz);
1809
1810                /* Since we are switching over to the new_ndlp,
1811                 * reset the old ndlp state
1812                 */
1813                if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
1814                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
1815                        keep_nlp_state = NLP_STE_NPR_NODE;
1816                lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1817                ndlp->nrport = keep_nrport;
1818        }
1819
1820        /*
1821         * If ndlp is not associated with any rport we can drop it here else
1822         * let dev_loss_tmo_callbk trigger DEVICE_RM event
1823         */
1824        if (!ndlp->rport && (ndlp->nlp_state == NLP_STE_NPR_NODE))
1825                lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
1826
1827        if (phba->sli_rev == LPFC_SLI_REV4 &&
1828            active_rrqs_xri_bitmap)
1829                mempool_free(active_rrqs_xri_bitmap,
1830                             phba->active_rrq_pool);
1831
1832        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1833                         "3173 PLOGI confirm exit: new_ndlp x%x x%x x%x\n",
1834                         new_ndlp->nlp_DID, new_ndlp->nlp_flag,
1835                         new_ndlp->nlp_fc4_type);
1836
1837        return new_ndlp;
1838}
1839
1840/**
1841 * lpfc_end_rscn - Check and handle more rscn for a vport
1842 * @vport: pointer to a host virtual N_Port data structure.
1843 *
1844 * This routine checks whether more Registration State Change
1845 * Notifications (RSCNs) came in while the discovery state machine was in
1846 * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1847 * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1848 * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1849 * handling the RSCNs.
1850 **/
1851void
1852lpfc_end_rscn(struct lpfc_vport *vport)
1853{
1854        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1855
1856        if (vport->fc_flag & FC_RSCN_MODE) {
1857                /*
1858                 * Check to see if more RSCNs came in while we were
1859                 * processing this one.
1860                 */
1861                if (vport->fc_rscn_id_cnt ||
1862                    (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1863                        lpfc_els_handle_rscn(vport);
1864                else {
1865                        spin_lock_irq(shost->host_lock);
1866                        vport->fc_flag &= ~FC_RSCN_MODE;
1867                        spin_unlock_irq(shost->host_lock);
1868                }
1869        }
1870}
1871
1872/**
1873 * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1874 * @phba: pointer to lpfc hba data structure.
1875 * @cmdiocb: pointer to lpfc command iocb data structure.
1876 * @rspiocb: pointer to lpfc response iocb data structure.
1877 *
1878 * This routine will call the clear rrq function to free the rrq and
1879 * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1880 * exist then the clear_rrq is still called because the rrq needs to
1881 * be freed.
1882 **/
1883
1884static void
1885lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1886                    struct lpfc_iocbq *rspiocb)
1887{
1888        struct lpfc_vport *vport = cmdiocb->vport;
1889        IOCB_t *irsp;
1890        struct lpfc_nodelist *ndlp = cmdiocb->context1;
1891        struct lpfc_node_rrq *rrq;
1892
1893        /* we pass cmdiocb to state machine which needs rspiocb as well */
1894        rrq = cmdiocb->context_un.rrq;
1895        cmdiocb->context_un.rsp_iocb = rspiocb;
1896
1897        irsp = &rspiocb->iocb;
1898        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1899                "RRQ cmpl:      status:x%x/x%x did:x%x",
1900                irsp->ulpStatus, irsp->un.ulpWord[4],
1901                irsp->un.elsreq64.remoteID);
1902
1903        /* rrq completes to NPort <nlp_DID> */
1904        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1905                         "2880 RRQ completes to DID x%x "
1906                         "Data: x%x x%x x%x x%x x%x\n",
1907                         irsp->un.elsreq64.remoteID,
1908                         irsp->ulpStatus, irsp->un.ulpWord[4],
1909                         irsp->ulpTimeout, rrq->xritag, rrq->rxid);
1910
1911        if (irsp->ulpStatus) {
1912                /* Check for retry */
1913                /* RRQ failed Don't print the vport to vport rjts */
1914                if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1915                        (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1916                        ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1917                        (phba)->pport->cfg_log_verbose & LOG_ELS)
1918                        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1919                                         "2881 RRQ failure DID:%06X Status:"
1920                                         "x%x/x%x\n",
1921                                         ndlp->nlp_DID, irsp->ulpStatus,
1922                                         irsp->un.ulpWord[4]);
1923        }
1924
1925        lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1926        lpfc_els_free_iocb(phba, cmdiocb);
1927        lpfc_nlp_put(ndlp);
1928        return;
1929}
1930/**
1931 * lpfc_cmpl_els_plogi - Completion callback function for plogi
1932 * @phba: pointer to lpfc hba data structure.
1933 * @cmdiocb: pointer to lpfc command iocb data structure.
1934 * @rspiocb: pointer to lpfc response iocb data structure.
1935 *
1936 * This routine is the completion callback function for issuing the Port
1937 * Login (PLOGI) command. For PLOGI completion, there must be an active
1938 * ndlp on the vport node list that matches the remote node ID from the
1939 * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
1940 * ignored and command IOCB released. The PLOGI response IOCB status is
1941 * checked for error conditions. If there is error status reported, PLOGI
1942 * retry shall be attempted by invoking the lpfc_els_retry() routine.
1943 * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1944 * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1945 * (DSM) is set for this PLOGI completion. Finally, it checks whether
1946 * there are additional N_Port nodes with the vport that need to perform
1947 * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1948 * PLOGIs.
1949 **/
1950static void
1951lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1952                    struct lpfc_iocbq *rspiocb)
1953{
1954        struct lpfc_vport *vport = cmdiocb->vport;
1955        struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1956        IOCB_t *irsp;
1957        struct lpfc_nodelist *ndlp, *free_ndlp;
1958        struct lpfc_dmabuf *prsp;
1959        int disc;
1960        struct serv_parm *sp = NULL;
1961
1962        /* we pass cmdiocb to state machine which needs rspiocb as well */
1963        cmdiocb->context_un.rsp_iocb = rspiocb;
1964
1965        irsp = &rspiocb->iocb;
1966        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1967                "PLOGI cmpl:      status:x%x/x%x did:x%x",
1968                irsp->ulpStatus, irsp->un.ulpWord[4],
1969                irsp->un.elsreq64.remoteID);
1970
1971        ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1972        if (!ndlp) {
1973                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1974                                 "0136 PLOGI completes to NPort x%x "
1975                                 "with no ndlp. Data: x%x x%x x%x\n",
1976                                 irsp->un.elsreq64.remoteID,
1977                                 irsp->ulpStatus, irsp->un.ulpWord[4],
1978                                 irsp->ulpIoTag);
1979                goto out_freeiocb;
1980        }
1981
1982        /* Since ndlp can be freed in the disc state machine, note if this node
1983         * is being used during discovery.
1984         */
1985        spin_lock_irq(&ndlp->lock);
1986        disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1987        ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1988        spin_unlock_irq(&ndlp->lock);
1989
1990        /* PLOGI completes to NPort <nlp_DID> */
1991        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1992                         "0102 PLOGI completes to NPort x%06x "
1993                         "Data: x%x x%x x%x x%x x%x\n",
1994                         ndlp->nlp_DID, ndlp->nlp_fc4_type,
1995                         irsp->ulpStatus, irsp->un.ulpWord[4],
1996                         disc, vport->num_disc_nodes);
1997
1998        /* Check to see if link went down during discovery */
1999        if (lpfc_els_chk_latt(vport)) {
2000                spin_lock_irq(&ndlp->lock);
2001                ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2002                spin_unlock_irq(&ndlp->lock);
2003                goto out;
2004        }
2005
2006        if (irsp->ulpStatus) {
2007                /* Check for retry */
2008                if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2009                        /* ELS command is being retried */
2010                        if (disc) {
2011                                spin_lock_irq(&ndlp->lock);
2012                                ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2013                                spin_unlock_irq(&ndlp->lock);
2014                        }
2015                        goto out;
2016                }
2017                /* PLOGI failed Don't print the vport to vport rjts */
2018                if (irsp->ulpStatus != IOSTAT_LS_RJT ||
2019                        (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
2020                        ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
2021                        (phba)->pport->cfg_log_verbose & LOG_ELS)
2022                        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2023                                 "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
2024                                 ndlp->nlp_DID, irsp->ulpStatus,
2025                                 irsp->un.ulpWord[4]);
2026
2027                /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2028                if (lpfc_error_lost_link(irsp))
2029                        goto check_plogi;
2030                else
2031                        lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2032                                                NLP_EVT_CMPL_PLOGI);
2033
2034                /* If a PLOGI collision occurred, the node needs to continue
2035                 * with the reglogin process.
2036                 */
2037                spin_lock_irq(&ndlp->lock);
2038                if ((ndlp->nlp_flag & (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI)) &&
2039                    ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE) {
2040                        spin_unlock_irq(&ndlp->lock);
2041                        goto out;
2042                }
2043                spin_unlock_irq(&ndlp->lock);
2044
2045                /* No PLOGI collision and the node is not registered with the
2046                 * scsi or nvme transport. It is no longer an active node. Just
2047                 * start the device remove process.
2048                 */
2049                if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
2050                        spin_lock_irq(&ndlp->lock);
2051                        ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2052                        spin_unlock_irq(&ndlp->lock);
2053                        lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2054                                                NLP_EVT_DEVICE_RM);
2055                }
2056        } else {
2057                /* Good status, call state machine */
2058                prsp = list_entry(((struct lpfc_dmabuf *)
2059                                   cmdiocb->context2)->list.next,
2060                                  struct lpfc_dmabuf, list);
2061                ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
2062
2063                sp = (struct serv_parm *)((u8 *)prsp->virt +
2064                                          sizeof(u32));
2065
2066                ndlp->vmid_support = 0;
2067                if ((phba->cfg_vmid_app_header && sp->cmn.app_hdr_support) ||
2068                    (phba->cfg_vmid_priority_tagging &&
2069                     sp->cmn.priority_tagging)) {
2070                        lpfc_printf_log(phba, KERN_DEBUG, LOG_ELS,
2071                                        "4018 app_hdr_support %d tagging %d DID x%x\n",
2072                                        sp->cmn.app_hdr_support,
2073                                        sp->cmn.priority_tagging,
2074                                        ndlp->nlp_DID);
2075                        /* if the dest port supports VMID, mark it in ndlp */
2076                        ndlp->vmid_support = 1;
2077                }
2078
2079                lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2080                                        NLP_EVT_CMPL_PLOGI);
2081        }
2082
2083 check_plogi:
2084        if (disc && vport->num_disc_nodes) {
2085                /* Check to see if there are more PLOGIs to be sent */
2086                lpfc_more_plogi(vport);
2087
2088                if (vport->num_disc_nodes == 0) {
2089                        spin_lock_irq(shost->host_lock);
2090                        vport->fc_flag &= ~FC_NDISC_ACTIVE;
2091                        spin_unlock_irq(shost->host_lock);
2092
2093                        lpfc_can_disctmo(vport);
2094                        lpfc_end_rscn(vport);
2095                }
2096        }
2097
2098out:
2099        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2100                              "PLOGI Cmpl PUT:     did:x%x refcnt %d",
2101                              ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2102
2103out_freeiocb:
2104        /* Release the reference on the original I/O request. */
2105        free_ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
2106
2107        lpfc_els_free_iocb(phba, cmdiocb);
2108        lpfc_nlp_put(free_ndlp);
2109        return;
2110}
2111
2112/**
2113 * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
2114 * @vport: pointer to a host virtual N_Port data structure.
2115 * @did: destination port identifier.
2116 * @retry: number of retries to the command IOCB.
2117 *
2118 * This routine issues a Port Login (PLOGI) command to a remote N_Port
2119 * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
2120 * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
2121 * This routine constructs the proper fields of the PLOGI IOCB and invokes
2122 * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
2123 *
2124 * Note that the ndlp reference count will be incremented by 1 for holding
2125 * the ndlp and the reference to ndlp will be stored into the context1 field
2126 * of the IOCB for the completion callback function to the PLOGI ELS command.
2127 *
2128 * Return code
2129 *   0 - Successfully issued a plogi for @vport
2130 *   1 - failed to issue a plogi for @vport
2131 **/
2132int
2133lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
2134{
2135        struct lpfc_hba  *phba = vport->phba;
2136        struct serv_parm *sp;
2137        struct lpfc_nodelist *ndlp;
2138        struct lpfc_iocbq *elsiocb;
2139        uint8_t *pcmd;
2140        uint16_t cmdsize;
2141        int ret;
2142
2143        ndlp = lpfc_findnode_did(vport, did);
2144        if (!ndlp)
2145                return 1;
2146
2147        /* Defer the processing of the issue PLOGI until after the
2148         * outstanding UNREG_RPI mbox command completes, unless we
2149         * are going offline. This logic does not apply for Fabric DIDs
2150         */
2151        if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2152            ((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
2153            !(vport->fc_flag & FC_OFFLINE_MODE)) {
2154                lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2155                                 "4110 Issue PLOGI x%x deferred "
2156                                 "on NPort x%x rpi x%x Data: x%px\n",
2157                                 ndlp->nlp_defer_did, ndlp->nlp_DID,
2158                                 ndlp->nlp_rpi, ndlp);
2159
2160                /* We can only defer 1st PLOGI */
2161                if (ndlp->nlp_defer_did == NLP_EVT_NOTHING_PENDING)
2162                        ndlp->nlp_defer_did = did;
2163                return 0;
2164        }
2165
2166        cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
2167        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
2168                                     ELS_CMD_PLOGI);
2169        if (!elsiocb)
2170                return 1;
2171
2172        spin_lock_irq(&ndlp->lock);
2173        ndlp->nlp_flag &= ~NLP_FCP_PRLI_RJT;
2174        spin_unlock_irq(&ndlp->lock);
2175
2176        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2177
2178        /* For PLOGI request, remainder of payload is service parameters */
2179        *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
2180        pcmd += sizeof(uint32_t);
2181        memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2182        sp = (struct serv_parm *) pcmd;
2183
2184        /*
2185         * If we are a N-port connected to a Fabric, fix-up paramm's so logins
2186         * to device on remote loops work.
2187         */
2188        if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
2189                sp->cmn.altBbCredit = 1;
2190
2191        if (sp->cmn.fcphLow < FC_PH_4_3)
2192                sp->cmn.fcphLow = FC_PH_4_3;
2193
2194        if (sp->cmn.fcphHigh < FC_PH3)
2195                sp->cmn.fcphHigh = FC_PH3;
2196
2197        sp->cmn.valid_vendor_ver_level = 0;
2198        memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
2199        sp->cmn.bbRcvSizeMsb &= 0xF;
2200
2201        /* Check if the destination port supports VMID */
2202        ndlp->vmid_support = 0;
2203        if (vport->vmid_priority_tagging)
2204                sp->cmn.priority_tagging = 1;
2205        else if (phba->cfg_vmid_app_header &&
2206                 bf_get(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags))
2207                sp->cmn.app_hdr_support = 1;
2208
2209        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2210                "Issue PLOGI:     did:x%x",
2211                did, 0, 0);
2212
2213        /* If our firmware supports this feature, convey that
2214         * information to the target using the vendor specific field.
2215         */
2216        if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
2217                sp->cmn.valid_vendor_ver_level = 1;
2218                sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
2219                sp->un.vv.flags = cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
2220        }
2221
2222        phba->fc_stat.elsXmitPLOGI++;
2223        elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
2224
2225        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2226                              "Issue PLOGI:     did:x%x refcnt %d",
2227                              did, kref_read(&ndlp->kref), 0);
2228        elsiocb->context1 = lpfc_nlp_get(ndlp);
2229        if (!elsiocb->context1) {
2230                lpfc_els_free_iocb(phba, elsiocb);
2231                return 1;
2232        }
2233
2234        ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2235        if (ret) {
2236                lpfc_els_free_iocb(phba, elsiocb);
2237                lpfc_nlp_put(ndlp);
2238                return 1;
2239        }
2240
2241        return 0;
2242}
2243
2244/**
2245 * lpfc_cmpl_els_prli - Completion callback function for prli
2246 * @phba: pointer to lpfc hba data structure.
2247 * @cmdiocb: pointer to lpfc command iocb data structure.
2248 * @rspiocb: pointer to lpfc response iocb data structure.
2249 *
2250 * This routine is the completion callback function for a Process Login
2251 * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2252 * status. If there is error status reported, PRLI retry shall be attempted
2253 * by invoking the lpfc_els_retry() routine. Otherwise, the state
2254 * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2255 * ndlp to mark the PRLI completion.
2256 **/
2257static void
2258lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2259                   struct lpfc_iocbq *rspiocb)
2260{
2261        struct lpfc_vport *vport = cmdiocb->vport;
2262        IOCB_t *irsp;
2263        struct lpfc_nodelist *ndlp;
2264        char *mode;
2265        u32 loglevel;
2266
2267        /* we pass cmdiocb to state machine which needs rspiocb as well */
2268        cmdiocb->context_un.rsp_iocb = rspiocb;
2269
2270        irsp = &(rspiocb->iocb);
2271        ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2272        spin_lock_irq(&ndlp->lock);
2273        ndlp->nlp_flag &= ~NLP_PRLI_SND;
2274
2275        /* Driver supports multiple FC4 types.  Counters matter. */
2276        vport->fc_prli_sent--;
2277        ndlp->fc4_prli_sent--;
2278        spin_unlock_irq(&ndlp->lock);
2279
2280        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2281                "PRLI cmpl:       status:x%x/x%x did:x%x",
2282                irsp->ulpStatus, irsp->un.ulpWord[4],
2283                ndlp->nlp_DID);
2284
2285        /* PRLI completes to NPort <nlp_DID> */
2286        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2287                         "0103 PRLI completes to NPort x%06x "
2288                         "Data: x%x x%x x%x x%x\n",
2289                         ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2290                         vport->num_disc_nodes, ndlp->fc4_prli_sent);
2291
2292        /* Check to see if link went down during discovery */
2293        if (lpfc_els_chk_latt(vport))
2294                goto out;
2295
2296        if (irsp->ulpStatus) {
2297                /* Check for retry */
2298                if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2299                        /* ELS command is being retried */
2300                        goto out;
2301                }
2302
2303                /* If we don't send GFT_ID to Fabric, a PRLI error
2304                 * could be expected.
2305                 */
2306                if ((vport->fc_flag & FC_FABRIC) ||
2307                    (vport->cfg_enable_fc4_type != LPFC_ENABLE_BOTH)) {
2308                        mode = KERN_ERR;
2309                        loglevel =  LOG_TRACE_EVENT;
2310                } else {
2311                        mode = KERN_INFO;
2312                        loglevel =  LOG_ELS;
2313                }
2314
2315                /* PRLI failed */
2316                lpfc_printf_vlog(vport, mode, loglevel,
2317                                 "2754 PRLI failure DID:%06X Status:x%x/x%x, "
2318                                 "data: x%x\n",
2319                                 ndlp->nlp_DID, irsp->ulpStatus,
2320                                 irsp->un.ulpWord[4], ndlp->fc4_prli_sent);
2321
2322                /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2323                if (!lpfc_error_lost_link(irsp))
2324                        lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2325                                                NLP_EVT_CMPL_PRLI);
2326
2327                /* As long as this node is not registered with the SCSI
2328                 * or NVMe transport and no other PRLIs are outstanding,
2329                 * it is no longer an active node.  Otherwise devloss
2330                 * handles the final cleanup.
2331                 */
2332                if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
2333                    !ndlp->fc4_prli_sent) {
2334                        spin_lock_irq(&ndlp->lock);
2335                        ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2336                        spin_unlock_irq(&ndlp->lock);
2337                        lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2338                                                NLP_EVT_DEVICE_RM);
2339                }
2340        } else {
2341                /* Good status, call state machine.  However, if another
2342                 * PRLI is outstanding, don't call the state machine
2343                 * because final disposition to Mapped or Unmapped is
2344                 * completed there.
2345                 */
2346                lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2347                                        NLP_EVT_CMPL_PRLI);
2348        }
2349
2350out:
2351        lpfc_els_free_iocb(phba, cmdiocb);
2352        lpfc_nlp_put(ndlp);
2353        return;
2354}
2355
2356/**
2357 * lpfc_issue_els_prli - Issue a prli iocb command for a vport
2358 * @vport: pointer to a host virtual N_Port data structure.
2359 * @ndlp: pointer to a node-list data structure.
2360 * @retry: number of retries to the command IOCB.
2361 *
2362 * This routine issues a Process Login (PRLI) ELS command for the
2363 * @vport. The PRLI service parameters are set up in the payload of the
2364 * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
2365 * is put to the IOCB completion callback func field before invoking the
2366 * routine lpfc_sli_issue_iocb() to send out PRLI command.
2367 *
2368 * Note that the ndlp reference count will be incremented by 1 for holding the
2369 * ndlp and the reference to ndlp will be stored into the context1 field of
2370 * the IOCB for the completion callback function to the PRLI ELS command.
2371 *
2372 * Return code
2373 *   0 - successfully issued prli iocb command for @vport
2374 *   1 - failed to issue prli iocb command for @vport
2375 **/
2376int
2377lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2378                    uint8_t retry)
2379{
2380        int rc = 0;
2381        struct lpfc_hba *phba = vport->phba;
2382        PRLI *npr;
2383        struct lpfc_nvme_prli *npr_nvme;
2384        struct lpfc_iocbq *elsiocb;
2385        uint8_t *pcmd;
2386        uint16_t cmdsize;
2387        u32 local_nlp_type, elscmd;
2388
2389        /*
2390         * If we are in RSCN mode, the FC4 types supported from a
2391         * previous GFT_ID command may not be accurate. So, if we
2392         * are a NVME Initiator, always look for the possibility of
2393         * the remote NPort beng a NVME Target.
2394         */
2395        if (phba->sli_rev == LPFC_SLI_REV4 &&
2396            vport->fc_flag & FC_RSCN_MODE &&
2397            vport->nvmei_support)
2398                ndlp->nlp_fc4_type |= NLP_FC4_NVME;
2399        local_nlp_type = ndlp->nlp_fc4_type;
2400
2401        /* This routine will issue 1 or 2 PRLIs, so zero all the ndlp
2402         * fields here before any of them can complete.
2403         */
2404        ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2405        ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
2406        ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
2407        ndlp->nlp_flag &= ~(NLP_FIRSTBURST | NLP_NPR_2B_DISC);
2408        ndlp->nvme_fb_size = 0;
2409
2410 send_next_prli:
2411        if (local_nlp_type & NLP_FC4_FCP) {
2412                /* Payload is 4 + 16 = 20 x14 bytes. */
2413                cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2414                elscmd = ELS_CMD_PRLI;
2415        } else if (local_nlp_type & NLP_FC4_NVME) {
2416                /* Payload is 4 + 20 = 24 x18 bytes. */
2417                cmdsize = (sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli));
2418                elscmd = ELS_CMD_NVMEPRLI;
2419        } else {
2420                lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2421                                 "3083 Unknown FC_TYPE x%x ndlp x%06x\n",
2422                                 ndlp->nlp_fc4_type, ndlp->nlp_DID);
2423                return 1;
2424        }
2425
2426        /* SLI3 ports don't support NVME.  If this rport is a strict NVME
2427         * FC4 type, implicitly LOGO.
2428         */
2429        if (phba->sli_rev == LPFC_SLI_REV3 &&
2430            ndlp->nlp_fc4_type == NLP_FC4_NVME) {
2431                lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2432                                 "3088 Rport fc4 type 0x%x not supported by SLI3 adapter\n",
2433                                 ndlp->nlp_type);
2434                lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
2435                return 1;
2436        }
2437
2438        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2439                                     ndlp->nlp_DID, elscmd);
2440        if (!elsiocb)
2441                return 1;
2442
2443        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2444
2445        /* For PRLI request, remainder of payload is service parameters */
2446        memset(pcmd, 0, cmdsize);
2447
2448        if (local_nlp_type & NLP_FC4_FCP) {
2449                /* Remainder of payload is FCP PRLI parameter page.
2450                 * Note: this data structure is defined as
2451                 * BE/LE in the structure definition so no
2452                 * byte swap call is made.
2453                 */
2454                *((uint32_t *)(pcmd)) = ELS_CMD_PRLI;
2455                pcmd += sizeof(uint32_t);
2456                npr = (PRLI *)pcmd;
2457
2458                /*
2459                 * If our firmware version is 3.20 or later,
2460                 * set the following bits for FC-TAPE support.
2461                 */
2462                if (phba->vpd.rev.feaLevelHigh >= 0x02) {
2463                        npr->ConfmComplAllowed = 1;
2464                        npr->Retry = 1;
2465                        npr->TaskRetryIdReq = 1;
2466                }
2467                npr->estabImagePair = 1;
2468                npr->readXferRdyDis = 1;
2469                if (vport->cfg_first_burst_size)
2470                        npr->writeXferRdyDis = 1;
2471
2472                /* For FCP support */
2473                npr->prliType = PRLI_FCP_TYPE;
2474                npr->initiatorFunc = 1;
2475                elsiocb->iocb_flag |= LPFC_PRLI_FCP_REQ;
2476
2477                /* Remove FCP type - processed. */
2478                local_nlp_type &= ~NLP_FC4_FCP;
2479        } else if (local_nlp_type & NLP_FC4_NVME) {
2480                /* Remainder of payload is NVME PRLI parameter page.
2481                 * This data structure is the newer definition that
2482                 * uses bf macros so a byte swap is required.
2483                 */
2484                *((uint32_t *)(pcmd)) = ELS_CMD_NVMEPRLI;
2485                pcmd += sizeof(uint32_t);
2486                npr_nvme = (struct lpfc_nvme_prli *)pcmd;
2487                bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
2488                bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
2489                if (phba->nsler) {
2490                        bf_set(prli_nsler, npr_nvme, 1);
2491                        bf_set(prli_conf, npr_nvme, 1);
2492                }
2493
2494                /* Only initiators request first burst. */
2495                if ((phba->cfg_nvme_enable_fb) &&
2496                    !phba->nvmet_support)
2497                        bf_set(prli_fba, npr_nvme, 1);
2498
2499                if (phba->nvmet_support) {
2500                        bf_set(prli_tgt, npr_nvme, 1);
2501                        bf_set(prli_disc, npr_nvme, 1);
2502                } else {
2503                        bf_set(prli_init, npr_nvme, 1);
2504                        bf_set(prli_conf, npr_nvme, 1);
2505                }
2506
2507                npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
2508                npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
2509                elsiocb->iocb_flag |= LPFC_PRLI_NVME_REQ;
2510
2511                /* Remove NVME type - processed. */
2512                local_nlp_type &= ~NLP_FC4_NVME;
2513        }
2514
2515        phba->fc_stat.elsXmitPRLI++;
2516        elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
2517        spin_lock_irq(&ndlp->lock);
2518        ndlp->nlp_flag |= NLP_PRLI_SND;
2519
2520        /* The vport counters are used for lpfc_scan_finished, but
2521         * the ndlp is used to track outstanding PRLIs for different
2522         * FC4 types.
2523         */
2524        vport->fc_prli_sent++;
2525        ndlp->fc4_prli_sent++;
2526        spin_unlock_irq(&ndlp->lock);
2527
2528        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2529                              "Issue PRLI:  did:x%x refcnt %d",
2530                              ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2531        elsiocb->context1 = lpfc_nlp_get(ndlp);
2532        if (!elsiocb->context1) {
2533                lpfc_els_free_iocb(phba, elsiocb);
2534                goto err;
2535        }
2536
2537        rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2538        if (rc == IOCB_ERROR) {
2539                lpfc_els_free_iocb(phba, elsiocb);
2540                lpfc_nlp_put(ndlp);
2541                goto err;
2542        }
2543
2544
2545        /* The driver supports 2 FC4 types.  Make sure
2546         * a PRLI is issued for all types before exiting.
2547         */
2548        if (phba->sli_rev == LPFC_SLI_REV4 &&
2549            local_nlp_type & (NLP_FC4_FCP | NLP_FC4_NVME))
2550                goto send_next_prli;
2551        else
2552                return 0;
2553
2554err:
2555        spin_lock_irq(&ndlp->lock);
2556        ndlp->nlp_flag &= ~NLP_PRLI_SND;
2557        spin_unlock_irq(&ndlp->lock);
2558        return 1;
2559}
2560
2561/**
2562 * lpfc_rscn_disc - Perform rscn discovery for a vport
2563 * @vport: pointer to a host virtual N_Port data structure.
2564 *
2565 * This routine performs Registration State Change Notification (RSCN)
2566 * discovery for a @vport. If the @vport's node port recovery count is not
2567 * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
2568 * the nodes that need recovery. If none of the PLOGI were needed through
2569 * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
2570 * invoked to check and handle possible more RSCN came in during the period
2571 * of processing the current ones.
2572 **/
2573static void
2574lpfc_rscn_disc(struct lpfc_vport *vport)
2575{
2576        lpfc_can_disctmo(vport);
2577
2578        /* RSCN discovery */
2579        /* go thru NPR nodes and issue ELS PLOGIs */
2580        if (vport->fc_npr_cnt)
2581                if (lpfc_els_disc_plogi(vport))
2582                        return;
2583
2584        lpfc_end_rscn(vport);
2585}
2586
2587/**
2588 * lpfc_adisc_done - Complete the adisc phase of discovery
2589 * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2590 *
2591 * This function is called when the final ADISC is completed during discovery.
2592 * This function handles clearing link attention or issuing reg_vpi depending
2593 * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2594 * discovery.
2595 * This function is called with no locks held.
2596 **/
2597static void
2598lpfc_adisc_done(struct lpfc_vport *vport)
2599{
2600        struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
2601        struct lpfc_hba   *phba = vport->phba;
2602
2603        /*
2604         * For NPIV, cmpl_reg_vpi will set port_state to READY,
2605         * and continue discovery.
2606         */
2607        if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2608            !(vport->fc_flag & FC_RSCN_MODE) &&
2609            (phba->sli_rev < LPFC_SLI_REV4)) {
2610                /* The ADISCs are complete.  Doesn't matter if they
2611                 * succeeded or failed because the ADISC completion
2612                 * routine guarantees to call the state machine and
2613                 * the RPI is either unregistered (failed ADISC response)
2614                 * or the RPI is still valid and the node is marked
2615                 * mapped for a target.  The exchanges should be in the
2616                 * correct state. This code is specific to SLI3.
2617                 */
2618                lpfc_issue_clear_la(phba, vport);
2619                lpfc_issue_reg_vpi(phba, vport);
2620                return;
2621        }
2622        /*
2623        * For SLI2, we need to set port_state to READY
2624        * and continue discovery.
2625        */
2626        if (vport->port_state < LPFC_VPORT_READY) {
2627                /* If we get here, there is nothing to ADISC */
2628                lpfc_issue_clear_la(phba, vport);
2629                if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2630                        vport->num_disc_nodes = 0;
2631                        /* go thru NPR list, issue ELS PLOGIs */
2632                        if (vport->fc_npr_cnt)
2633                                lpfc_els_disc_plogi(vport);
2634                        if (!vport->num_disc_nodes) {
2635                                spin_lock_irq(shost->host_lock);
2636                                vport->fc_flag &= ~FC_NDISC_ACTIVE;
2637                                spin_unlock_irq(shost->host_lock);
2638                                lpfc_can_disctmo(vport);
2639                                lpfc_end_rscn(vport);
2640                        }
2641                }
2642                vport->port_state = LPFC_VPORT_READY;
2643        } else
2644                lpfc_rscn_disc(vport);
2645}
2646
2647/**
2648 * lpfc_more_adisc - Issue more adisc as needed
2649 * @vport: pointer to a host virtual N_Port data structure.
2650 *
2651 * This routine determines whether there are more ndlps on a @vport
2652 * node list need to have Address Discover (ADISC) issued. If so, it will
2653 * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2654 * remaining nodes which need to have ADISC sent.
2655 **/
2656void
2657lpfc_more_adisc(struct lpfc_vport *vport)
2658{
2659        if (vport->num_disc_nodes)
2660                vport->num_disc_nodes--;
2661        /* Continue discovery with <num_disc_nodes> ADISCs to go */
2662        lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2663                         "0210 Continue discovery with %d ADISCs to go "
2664                         "Data: x%x x%x x%x\n",
2665                         vport->num_disc_nodes, vport->fc_adisc_cnt,
2666                         vport->fc_flag, vport->port_state);
2667        /* Check to see if there are more ADISCs to be sent */
2668        if (vport->fc_flag & FC_NLP_MORE) {
2669                lpfc_set_disctmo(vport);
2670                /* go thru NPR nodes and issue any remaining ELS ADISCs */
2671                lpfc_els_disc_adisc(vport);
2672        }
2673        if (!vport->num_disc_nodes)
2674                lpfc_adisc_done(vport);
2675        return;
2676}
2677
2678/**
2679 * lpfc_cmpl_els_adisc - Completion callback function for adisc
2680 * @phba: pointer to lpfc hba data structure.
2681 * @cmdiocb: pointer to lpfc command iocb data structure.
2682 * @rspiocb: pointer to lpfc response iocb data structure.
2683 *
2684 * This routine is the completion function for issuing the Address Discover
2685 * (ADISC) command. It first checks to see whether link went down during
2686 * the discovery process. If so, the node will be marked as node port
2687 * recovery for issuing discover IOCB by the link attention handler and
2688 * exit. Otherwise, the response status is checked. If error was reported
2689 * in the response status, the ADISC command shall be retried by invoking
2690 * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2691 * the response status, the state machine is invoked to set transition
2692 * with respect to NLP_EVT_CMPL_ADISC event.
2693 **/
2694static void
2695lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2696                    struct lpfc_iocbq *rspiocb)
2697{
2698        struct lpfc_vport *vport = cmdiocb->vport;
2699        IOCB_t *irsp;
2700        struct lpfc_nodelist *ndlp;
2701        int  disc;
2702
2703        /* we pass cmdiocb to state machine which needs rspiocb as well */
2704        cmdiocb->context_un.rsp_iocb = rspiocb;
2705
2706        irsp = &(rspiocb->iocb);
2707        ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2708
2709        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2710                "ADISC cmpl:      status:x%x/x%x did:x%x",
2711                irsp->ulpStatus, irsp->un.ulpWord[4],
2712                ndlp->nlp_DID);
2713
2714        /* Since ndlp can be freed in the disc state machine, note if this node
2715         * is being used during discovery.
2716         */
2717        spin_lock_irq(&ndlp->lock);
2718        disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2719        ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
2720        spin_unlock_irq(&ndlp->lock);
2721        /* ADISC completes to NPort <nlp_DID> */
2722        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2723                         "0104 ADISC completes to NPort x%x "
2724                         "Data: x%x x%x x%x x%x x%x\n",
2725                         ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2726                         irsp->ulpTimeout, disc, vport->num_disc_nodes);
2727        /* Check to see if link went down during discovery */
2728        if (lpfc_els_chk_latt(vport)) {
2729                spin_lock_irq(&ndlp->lock);
2730                ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2731                spin_unlock_irq(&ndlp->lock);
2732                goto out;
2733        }
2734
2735        if (irsp->ulpStatus) {
2736                /* Check for retry */
2737                if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2738                        /* ELS command is being retried */
2739                        if (disc) {
2740                                spin_lock_irq(&ndlp->lock);
2741                                ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2742                                spin_unlock_irq(&ndlp->lock);
2743                                lpfc_set_disctmo(vport);
2744                        }
2745                        goto out;
2746                }
2747                /* ADISC failed */
2748                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2749                                 "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2750                                 ndlp->nlp_DID, irsp->ulpStatus,
2751                                 irsp->un.ulpWord[4]);
2752                /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2753                if (lpfc_error_lost_link(irsp))
2754                        goto check_adisc;
2755                else
2756                        lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2757                                                NLP_EVT_CMPL_ADISC);
2758
2759                /* As long as this node is not registered with the SCSI or NVMe
2760                 * transport, it is no longer an active node. Otherwise
2761                 * devloss handles the final cleanup.
2762                 */
2763                if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
2764                        spin_lock_irq(&ndlp->lock);
2765                        ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2766                        spin_unlock_irq(&ndlp->lock);
2767                        lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2768                                                NLP_EVT_DEVICE_RM);
2769                }
2770        } else
2771                /* Good status, call state machine */
2772                lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2773                                        NLP_EVT_CMPL_ADISC);
2774
2775 check_adisc:
2776        /* Check to see if there are more ADISCs to be sent */
2777        if (disc && vport->num_disc_nodes)
2778                lpfc_more_adisc(vport);
2779out:
2780        lpfc_els_free_iocb(phba, cmdiocb);
2781        lpfc_nlp_put(ndlp);
2782        return;
2783}
2784
2785/**
2786 * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2787 * @vport: pointer to a virtual N_Port data structure.
2788 * @ndlp: pointer to a node-list data structure.
2789 * @retry: number of retries to the command IOCB.
2790 *
2791 * This routine issues an Address Discover (ADISC) for an @ndlp on a
2792 * @vport. It prepares the payload of the ADISC ELS command, updates the
2793 * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2794 * to issue the ADISC ELS command.
2795 *
2796 * Note that the ndlp reference count will be incremented by 1 for holding the
2797 * ndlp and the reference to ndlp will be stored into the context1 field of
2798 * the IOCB for the completion callback function to the ADISC ELS command.
2799 *
2800 * Return code
2801 *   0 - successfully issued adisc
2802 *   1 - failed to issue adisc
2803 **/
2804int
2805lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2806                     uint8_t retry)
2807{
2808        int rc = 0;
2809        struct lpfc_hba  *phba = vport->phba;
2810        ADISC *ap;
2811        struct lpfc_iocbq *elsiocb;
2812        uint8_t *pcmd;
2813        uint16_t cmdsize;
2814
2815        cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2816        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2817                                     ndlp->nlp_DID, ELS_CMD_ADISC);
2818        if (!elsiocb)
2819                return 1;
2820
2821        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2822
2823        /* For ADISC request, remainder of payload is service parameters */
2824        *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2825        pcmd += sizeof(uint32_t);
2826
2827        /* Fill in ADISC payload */
2828        ap = (ADISC *) pcmd;
2829        ap->hardAL_PA = phba->fc_pref_ALPA;
2830        memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2831        memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2832        ap->DID = be32_to_cpu(vport->fc_myDID);
2833
2834        phba->fc_stat.elsXmitADISC++;
2835        elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2836        spin_lock_irq(&ndlp->lock);
2837        ndlp->nlp_flag |= NLP_ADISC_SND;
2838        spin_unlock_irq(&ndlp->lock);
2839        elsiocb->context1 = lpfc_nlp_get(ndlp);
2840        if (!elsiocb->context1) {
2841                lpfc_els_free_iocb(phba, elsiocb);
2842                goto err;
2843        }
2844
2845        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2846                              "Issue ADISC:   did:x%x refcnt %d",
2847                              ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2848        rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2849        if (rc == IOCB_ERROR) {
2850                lpfc_els_free_iocb(phba, elsiocb);
2851                lpfc_nlp_put(ndlp);
2852                goto err;
2853        }
2854
2855        return 0;
2856
2857err:
2858        spin_lock_irq(&ndlp->lock);
2859        ndlp->nlp_flag &= ~NLP_ADISC_SND;
2860        spin_unlock_irq(&ndlp->lock);
2861        return 1;
2862}
2863
2864/**
2865 * lpfc_cmpl_els_logo - Completion callback function for logo
2866 * @phba: pointer to lpfc hba data structure.
2867 * @cmdiocb: pointer to lpfc command iocb data structure.
2868 * @rspiocb: pointer to lpfc response iocb data structure.
2869 *
2870 * This routine is the completion function for issuing the ELS Logout (LOGO)
2871 * command. If no error status was reported from the LOGO response, the
2872 * state machine of the associated ndlp shall be invoked for transition with
2873 * respect to NLP_EVT_CMPL_LOGO event.
2874 **/
2875static void
2876lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2877                   struct lpfc_iocbq *rspiocb)
2878{
2879        struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2880        struct lpfc_vport *vport = ndlp->vport;
2881        IOCB_t *irsp;
2882        unsigned long flags;
2883        uint32_t skip_recovery = 0;
2884        int wake_up_waiter = 0;
2885
2886        /* we pass cmdiocb to state machine which needs rspiocb as well */
2887        cmdiocb->context_un.rsp_iocb = rspiocb;
2888
2889        irsp = &(rspiocb->iocb);
2890        spin_lock_irq(&ndlp->lock);
2891        ndlp->nlp_flag &= ~NLP_LOGO_SND;
2892        if (ndlp->upcall_flags & NLP_WAIT_FOR_LOGO) {
2893                wake_up_waiter = 1;
2894                ndlp->upcall_flags &= ~NLP_WAIT_FOR_LOGO;
2895        }
2896        spin_unlock_irq(&ndlp->lock);
2897
2898        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2899                "LOGO cmpl:       status:x%x/x%x did:x%x",
2900                irsp->ulpStatus, irsp->un.ulpWord[4],
2901                ndlp->nlp_DID);
2902
2903        /* LOGO completes to NPort <nlp_DID> */
2904        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2905                         "0105 LOGO completes to NPort x%x "
2906                         "refcnt %d nflags x%x Data: x%x x%x x%x x%x\n",
2907                         ndlp->nlp_DID, kref_read(&ndlp->kref), ndlp->nlp_flag,
2908                         irsp->ulpStatus, irsp->un.ulpWord[4],
2909                         irsp->ulpTimeout, vport->num_disc_nodes);
2910
2911        if (lpfc_els_chk_latt(vport)) {
2912                skip_recovery = 1;
2913                goto out;
2914        }
2915
2916        /* The LOGO will not be retried on failure.  A LOGO was
2917         * issued to the remote rport and a ACC or RJT or no Answer are
2918         * all acceptable.  Note the failure and move forward with
2919         * discovery.  The PLOGI will retry.
2920         */
2921        if (irsp->ulpStatus) {
2922                /* LOGO failed */
2923                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2924                                 "2756 LOGO failure, No Retry DID:%06X Status:x%x/x%x\n",
2925                                 ndlp->nlp_DID, irsp->ulpStatus,
2926                                 irsp->un.ulpWord[4]);
2927                /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2928                if (lpfc_error_lost_link(irsp)) {
2929                        skip_recovery = 1;
2930                        goto out;
2931                }
2932        }
2933
2934        /* Call state machine. This will unregister the rpi if needed. */
2935        lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
2936
2937        /* The driver sets this flag for an NPIV instance that doesn't want to
2938         * log into the remote port.
2939         */
2940        if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2941                spin_lock_irq(&ndlp->lock);
2942                if (phba->sli_rev == LPFC_SLI_REV4)
2943                        ndlp->nlp_flag |= NLP_RELEASE_RPI;
2944                ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2945                spin_unlock_irq(&ndlp->lock);
2946                lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2947                                        NLP_EVT_DEVICE_RM);
2948                lpfc_els_free_iocb(phba, cmdiocb);
2949                lpfc_nlp_put(ndlp);
2950
2951                /* Presume the node was released. */
2952                return;
2953        }
2954
2955out:
2956        /* Driver is done with the IO.  */
2957        lpfc_els_free_iocb(phba, cmdiocb);
2958        lpfc_nlp_put(ndlp);
2959
2960        /* At this point, the LOGO processing is complete. NOTE: For a
2961         * pt2pt topology, we are assuming the NPortID will only change
2962         * on link up processing. For a LOGO / PLOGI initiated by the
2963         * Initiator, we are assuming the NPortID is not going to change.
2964         */
2965
2966        if (wake_up_waiter && ndlp->logo_waitq)
2967                wake_up(ndlp->logo_waitq);
2968        /*
2969         * If the node is a target, the handling attempts to recover the port.
2970         * For any other port type, the rpi is unregistered as an implicit
2971         * LOGO.
2972         */
2973        if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) &&
2974            skip_recovery == 0) {
2975                lpfc_cancel_retry_delay_tmo(vport, ndlp);
2976                spin_lock_irqsave(&ndlp->lock, flags);
2977                ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2978                spin_unlock_irqrestore(&ndlp->lock, flags);
2979
2980                lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2981                                 "3187 LOGO completes to NPort x%x: Start "
2982                                 "Recovery Data: x%x x%x x%x x%x\n",
2983                                 ndlp->nlp_DID, irsp->ulpStatus,
2984                                 irsp->un.ulpWord[4], irsp->ulpTimeout,
2985                                 vport->num_disc_nodes);
2986                lpfc_disc_start(vport);
2987                return;
2988        }
2989
2990        /* Cleanup path for failed REG_RPI handling. If REG_RPI fails, the
2991         * driver sends a LOGO to the rport to cleanup.  For fabric and
2992         * initiator ports cleanup the node as long as it the node is not
2993         * register with the transport.
2994         */
2995        if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
2996                spin_lock_irq(&ndlp->lock);
2997                ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2998                spin_unlock_irq(&ndlp->lock);
2999                lpfc_disc_state_machine(vport, ndlp, cmdiocb,
3000                                        NLP_EVT_DEVICE_RM);
3001        }
3002}
3003
3004/**
3005 * lpfc_issue_els_logo - Issue a logo to an node on a vport
3006 * @vport: pointer to a virtual N_Port data structure.
3007 * @ndlp: pointer to a node-list data structure.
3008 * @retry: number of retries to the command IOCB.
3009 *
3010 * This routine constructs and issues an ELS Logout (LOGO) iocb command
3011 * to a remote node, referred by an @ndlp on a @vport. It constructs the
3012 * payload of the IOCB, properly sets up the @ndlp state, and invokes the
3013 * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
3014 *
3015 * Note that the ndlp reference count will be incremented by 1 for holding the
3016 * ndlp and the reference to ndlp will be stored into the context1 field of
3017 * the IOCB for the completion callback function to the LOGO ELS command.
3018 *
3019 * Callers of this routine are expected to unregister the RPI first
3020 *
3021 * Return code
3022 *   0 - successfully issued logo
3023 *   1 - failed to issue logo
3024 **/
3025int
3026lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3027                    uint8_t retry)
3028{
3029        struct lpfc_hba  *phba = vport->phba;
3030        struct lpfc_iocbq *elsiocb;
3031        uint8_t *pcmd;
3032        uint16_t cmdsize;
3033        int rc;
3034
3035        spin_lock_irq(&ndlp->lock);
3036        if (ndlp->nlp_flag & NLP_LOGO_SND) {
3037                spin_unlock_irq(&ndlp->lock);
3038                return 0;
3039        }
3040        spin_unlock_irq(&ndlp->lock);
3041
3042        cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
3043        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3044                                     ndlp->nlp_DID, ELS_CMD_LOGO);
3045        if (!elsiocb)
3046                return 1;
3047
3048        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3049        *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
3050        pcmd += sizeof(uint32_t);
3051
3052        /* Fill in LOGO payload */
3053        *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
3054        pcmd += sizeof(uint32_t);
3055        memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
3056
3057        phba->fc_stat.elsXmitLOGO++;
3058        elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
3059        spin_lock_irq(&ndlp->lock);
3060        ndlp->nlp_flag |= NLP_LOGO_SND;
3061        ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
3062        spin_unlock_irq(&ndlp->lock);
3063        elsiocb->context1 = lpfc_nlp_get(ndlp);
3064        if (!elsiocb->context1) {
3065                lpfc_els_free_iocb(phba, elsiocb);
3066                goto err;
3067        }
3068
3069        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3070                              "Issue LOGO:      did:x%x refcnt %d",
3071                              ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3072        rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3073        if (rc == IOCB_ERROR) {
3074                lpfc_els_free_iocb(phba, elsiocb);
3075                lpfc_nlp_put(ndlp);
3076                goto err;
3077        }
3078
3079        spin_lock_irq(&ndlp->lock);
3080        ndlp->nlp_prev_state = ndlp->nlp_state;
3081        spin_unlock_irq(&ndlp->lock);
3082        lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3083        return 0;
3084
3085err:
3086        spin_lock_irq(&ndlp->lock);
3087        ndlp->nlp_flag &= ~NLP_LOGO_SND;
3088        spin_unlock_irq(&ndlp->lock);
3089        return 1;
3090}
3091
3092/**
3093 * lpfc_cmpl_els_cmd - Completion callback function for generic els command
3094 * @phba: pointer to lpfc hba data structure.
3095 * @cmdiocb: pointer to lpfc command iocb data structure.
3096 * @rspiocb: pointer to lpfc response iocb data structure.
3097 *
3098 * This routine is a generic completion callback function for ELS commands.
3099 * Specifically, it is the callback function which does not need to perform
3100 * any command specific operations. It is currently used by the ELS command
3101 * issuing routines for RSCN, lpfc_issue_els_rscn, and the ELS Fibre Channel
3102 * Address Resolution Protocol Response (FARPR) routine, lpfc_issue_els_farpr().
3103 * Other than certain debug loggings, this callback function simply invokes the
3104 * lpfc_els_chk_latt() routine to check whether link went down during the
3105 * discovery process.
3106 **/
3107static void
3108lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3109                  struct lpfc_iocbq *rspiocb)
3110{
3111        struct lpfc_vport *vport = cmdiocb->vport;
3112        struct lpfc_nodelist *free_ndlp;
3113        IOCB_t *irsp;
3114
3115        irsp = &rspiocb->iocb;
3116
3117        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3118                              "ELS cmd cmpl:    status:x%x/x%x did:x%x",
3119                              irsp->ulpStatus, irsp->un.ulpWord[4],
3120                              irsp->un.elsreq64.remoteID);
3121
3122        /* ELS cmd tag <ulpIoTag> completes */
3123        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3124                         "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
3125                         irsp->ulpIoTag, irsp->ulpStatus,
3126                         irsp->un.ulpWord[4], irsp->ulpTimeout);
3127
3128        /* Check to see if link went down during discovery */
3129        lpfc_els_chk_latt(vport);
3130
3131        free_ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
3132
3133        lpfc_els_free_iocb(phba, cmdiocb);
3134        lpfc_nlp_put(free_ndlp);
3135}
3136
3137/**
3138 * lpfc_reg_fab_ctrl_node - RPI register the fabric controller node.
3139 * @vport: pointer to lpfc_vport data structure.
3140 * @fc_ndlp: pointer to the fabric controller (0xfffffd) node.
3141 *
3142 * This routine registers the rpi assigned to the fabric controller
3143 * NPort_ID (0xfffffd) with the port and moves the node to UNMAPPED
3144 * state triggering a registration with the SCSI transport.
3145 *
3146 * This routine is single out because the fabric controller node
3147 * does not receive a PLOGI.  This routine is consumed by the
3148 * SCR and RDF ELS commands.  Callers are expected to qualify
3149 * with SLI4 first.
3150 **/
3151static int
3152lpfc_reg_fab_ctrl_node(struct lpfc_vport *vport, struct lpfc_nodelist *fc_ndlp)
3153{
3154        int rc = 0;
3155        struct lpfc_hba *phba = vport->phba;
3156        struct lpfc_nodelist *ns_ndlp;
3157        LPFC_MBOXQ_t *mbox;
3158        struct lpfc_dmabuf *mp;
3159
3160        if (fc_ndlp->nlp_flag & NLP_RPI_REGISTERED)
3161                return rc;
3162
3163        ns_ndlp = lpfc_findnode_did(vport, NameServer_DID);
3164        if (!ns_ndlp)
3165                return -ENODEV;
3166
3167        lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3168                         "0935 %s: Reg FC RPI x%x on FC DID x%x NSSte: x%x\n",
3169                         __func__, fc_ndlp->nlp_rpi, fc_ndlp->nlp_DID,
3170                         ns_ndlp->nlp_state);
3171        if (ns_ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
3172                return -ENODEV;
3173
3174        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3175        if (!mbox) {
3176                lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3177                                 "0936 %s: no memory for reg_login "
3178                                 "Data: x%x x%x x%x x%x\n", __func__,
3179                                 fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
3180                                 fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
3181                return -ENOMEM;
3182        }
3183        rc = lpfc_reg_rpi(phba, vport->vpi, fc_ndlp->nlp_DID,
3184                          (u8 *)&vport->fc_sparam, mbox, fc_ndlp->nlp_rpi);
3185        if (rc) {
3186                rc = -EACCES;
3187                goto out;
3188        }
3189
3190        fc_ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
3191        mbox->mbox_cmpl = lpfc_mbx_cmpl_fc_reg_login;
3192        mbox->ctx_ndlp = lpfc_nlp_get(fc_ndlp);
3193        if (!mbox->ctx_ndlp) {
3194                rc = -ENOMEM;
3195                goto out_mem;
3196        }
3197
3198        mbox->vport = vport;
3199        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3200        if (rc == MBX_NOT_FINISHED) {
3201                rc = -ENODEV;
3202                lpfc_nlp_put(fc_ndlp);
3203                goto out_mem;
3204        }
3205        /* Success path. Exit. */
3206        lpfc_nlp_set_state(vport, fc_ndlp,
3207                           NLP_STE_REG_LOGIN_ISSUE);
3208        return 0;
3209
3210 out_mem:
3211        fc_ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3212        mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
3213        lpfc_mbuf_free(phba, mp->virt, mp->phys);
3214        kfree(mp);
3215
3216 out:
3217        mempool_free(mbox, phba->mbox_mem_pool);
3218        lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3219                         "0938 %s: failed to format reg_login "
3220                         "Data: x%x x%x x%x x%x\n", __func__,
3221                         fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
3222                         fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
3223        return rc;
3224}
3225
3226/**
3227 * lpfc_cmpl_els_disc_cmd - Completion callback function for Discovery ELS cmd
3228 * @phba: pointer to lpfc hba data structure.
3229 * @cmdiocb: pointer to lpfc command iocb data structure.
3230 * @rspiocb: pointer to lpfc response iocb data structure.
3231 *
3232 * This routine is a generic completion callback function for Discovery ELS cmd.
3233 * Currently used by the ELS command issuing routines for the ELS State Change
3234 * Request (SCR), lpfc_issue_els_scr() and the ELS RDF, lpfc_issue_els_rdf().
3235 * These commands will be retried once only for ELS timeout errors.
3236 **/
3237static void
3238lpfc_cmpl_els_disc_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3239                       struct lpfc_iocbq *rspiocb)
3240{
3241        struct lpfc_vport *vport = cmdiocb->vport;
3242        IOCB_t *irsp;
3243        struct lpfc_els_rdf_rsp *prdf;
3244        struct lpfc_dmabuf *pcmd, *prsp;
3245        u32 *pdata;
3246        u32 cmd;
3247        struct lpfc_nodelist *ndlp = cmdiocb->context1;
3248
3249        irsp = &rspiocb->iocb;
3250
3251        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3252                "ELS cmd cmpl:    status:x%x/x%x did:x%x",
3253                irsp->ulpStatus, irsp->un.ulpWord[4],
3254                irsp->un.elsreq64.remoteID);
3255        /* ELS cmd tag <ulpIoTag> completes */
3256        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3257                         "0217 ELS cmd tag x%x completes Data: x%x x%x x%x "
3258                         "x%x\n",
3259                         irsp->ulpIoTag, irsp->ulpStatus,
3260                         irsp->un.ulpWord[4], irsp->ulpTimeout,
3261                         cmdiocb->retry);
3262
3263        pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
3264        if (!pcmd)
3265                goto out;
3266
3267        pdata = (u32 *)pcmd->virt;
3268        if (!pdata)
3269                goto out;
3270        cmd = *pdata;
3271
3272        /* Only 1 retry for ELS Timeout only */
3273        if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
3274            ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3275            IOERR_SEQUENCE_TIMEOUT)) {
3276                cmdiocb->retry++;
3277                if (cmdiocb->retry <= 1) {
3278                        switch (cmd) {
3279                        case ELS_CMD_SCR:
3280                                lpfc_issue_els_scr(vport, cmdiocb->retry);
3281                                break;
3282                        case ELS_CMD_RDF:
3283                                cmdiocb->context1 = NULL; /* save ndlp refcnt */
3284                                lpfc_issue_els_rdf(vport, cmdiocb->retry);
3285                                break;
3286                        }
3287                        goto out;
3288                }
3289                phba->fc_stat.elsRetryExceeded++;
3290        }
3291        if (irsp->ulpStatus) {
3292                /* ELS discovery cmd completes with error */
3293                lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
3294                                 "4203 ELS cmd x%x error: x%x x%X\n", cmd,
3295                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
3296                goto out;
3297        }
3298
3299        /* The RDF response doesn't have any impact on the running driver
3300         * but the notification descriptors are dumped here for support.
3301         */
3302        if (cmd == ELS_CMD_RDF) {
3303                int i;
3304
3305                prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
3306                if (!prsp)
3307                        goto out;
3308
3309                prdf = (struct lpfc_els_rdf_rsp *)prsp->virt;
3310                if (!prdf)
3311                        goto out;
3312
3313                for (i = 0; i < ELS_RDF_REG_TAG_CNT &&
3314                            i < be32_to_cpu(prdf->reg_d1.reg_desc.count); i++)
3315                        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3316                                 "4677 Fabric RDF Notification Grant Data: "
3317                                 "0x%08x\n",
3318                                 be32_to_cpu(
3319                                        prdf->reg_d1.desc_tags[i]));
3320        }
3321
3322out:
3323        /* Check to see if link went down during discovery */
3324        lpfc_els_chk_latt(vport);
3325        lpfc_els_free_iocb(phba, cmdiocb);
3326        lpfc_nlp_put(ndlp);
3327        return;
3328}
3329
3330/**
3331 * lpfc_issue_els_scr - Issue a scr to an node on a vport
3332 * @vport: pointer to a host virtual N_Port data structure.
3333 * @retry: retry counter for the command IOCB.
3334 *
3335 * This routine issues a State Change Request (SCR) to a fabric node
3336 * on a @vport. The remote node is Fabric Controller (0xfffffd). It
3337 * first search the @vport node list to find the matching ndlp. If no such
3338 * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
3339 * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
3340 * routine is invoked to send the SCR IOCB.
3341 *
3342 * Note that the ndlp reference count will be incremented by 1 for holding the
3343 * ndlp and the reference to ndlp will be stored into the context1 field of
3344 * the IOCB for the completion callback function to the SCR ELS command.
3345 *
3346 * Return code
3347 *   0 - Successfully issued scr command
3348 *   1 - Failed to issue scr command
3349 **/
3350int
3351lpfc_issue_els_scr(struct lpfc_vport *vport, uint8_t retry)
3352{
3353        int rc = 0;
3354        struct lpfc_hba  *phba = vport->phba;
3355        struct lpfc_iocbq *elsiocb;
3356        uint8_t *pcmd;
3357        uint16_t cmdsize;
3358        struct lpfc_nodelist *ndlp;
3359
3360        cmdsize = (sizeof(uint32_t) + sizeof(SCR));
3361
3362        ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3363        if (!ndlp) {
3364                ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3365                if (!ndlp)
3366                        return 1;
3367                lpfc_enqueue_node(vport, ndlp);
3368        }
3369
3370        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3371                                     ndlp->nlp_DID, ELS_CMD_SCR);
3372        if (!elsiocb)
3373                return 1;
3374
3375        if (phba->sli_rev == LPFC_SLI_REV4) {
3376                rc = lpfc_reg_fab_ctrl_node(vport, ndlp);
3377                if (rc) {
3378                        lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3379                                         "0937 %s: Failed to reg fc node, rc %d\n",
3380                                         __func__, rc);
3381                        return 1;
3382                }
3383        }
3384        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3385
3386        *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
3387        pcmd += sizeof(uint32_t);
3388
3389        /* For SCR, remainder of payload is SCR parameter page */
3390        memset(pcmd, 0, sizeof(SCR));
3391        ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
3392
3393        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3394                "Issue SCR:       did:x%x",
3395                ndlp->nlp_DID, 0, 0);
3396
3397        phba->fc_stat.elsXmitSCR++;
3398        elsiocb->iocb_cmpl = lpfc_cmpl_els_disc_cmd;
3399        elsiocb->context1 = lpfc_nlp_get(ndlp);
3400        if (!elsiocb->context1) {
3401                lpfc_els_free_iocb(phba, elsiocb);
3402                return 1;
3403        }
3404
3405        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3406                              "Issue SCR:     did:x%x refcnt %d",
3407                              ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3408
3409        rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3410        if (rc == IOCB_ERROR) {
3411                lpfc_els_free_iocb(phba, elsiocb);
3412                lpfc_nlp_put(ndlp);
3413                return 1;
3414        }
3415
3416        /* Keep the ndlp just in case RDF is being sent */
3417        return 0;
3418}
3419
3420/**
3421 * lpfc_issue_els_rscn - Issue an RSCN to the Fabric Controller (Fabric)
3422 *   or the other nport (pt2pt).
3423 * @vport: pointer to a host virtual N_Port data structure.
3424 * @retry: number of retries to the command IOCB.
3425 *
3426 * This routine issues a RSCN to the Fabric Controller (DID 0xFFFFFD)
3427 *  when connected to a fabric, or to the remote port when connected
3428 *  in point-to-point mode. When sent to the Fabric Controller, it will
3429 *  replay the RSCN to registered recipients.
3430 *
3431 * Note that the ndlp reference count will be incremented by 1 for holding the
3432 * ndlp and the reference to ndlp will be stored into the context1 field of
3433 * the IOCB for the completion callback function to the RSCN ELS command.
3434 *
3435 * Return code
3436 *   0 - Successfully issued RSCN command
3437 *   1 - Failed to issue RSCN command
3438 **/
3439int
3440lpfc_issue_els_rscn(struct lpfc_vport *vport, uint8_t retry)
3441{
3442        int rc = 0;
3443        struct lpfc_hba *phba = vport->phba;
3444        struct lpfc_iocbq *elsiocb;
3445        struct lpfc_nodelist *ndlp;
3446        struct {
3447                struct fc_els_rscn rscn;
3448                struct fc_els_rscn_page portid;
3449        } *event;
3450        uint32_t nportid;
3451        uint16_t cmdsize = sizeof(*event);
3452
3453        /* Not supported for private loop */
3454        if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
3455            !(vport->fc_flag & FC_PUBLIC_LOOP))
3456                return 1;
3457
3458        if (vport->fc_flag & FC_PT2PT) {
3459                /* find any mapped nport - that would be the other nport */
3460                ndlp = lpfc_findnode_mapped(vport);
3461                if (!ndlp)
3462                        return 1;
3463        } else {
3464                nportid = FC_FID_FCTRL;
3465                /* find the fabric controller node */
3466                ndlp = lpfc_findnode_did(vport, nportid);
3467                if (!ndlp) {
3468                        /* if one didn't exist, make one */
3469                        ndlp = lpfc_nlp_init(vport, nportid);
3470                        if (!ndlp)
3471                                return 1;
3472                        lpfc_enqueue_node(vport, ndlp);
3473                }
3474        }
3475
3476        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3477                                     ndlp->nlp_DID, ELS_CMD_RSCN_XMT);
3478
3479        if (!elsiocb)
3480                return 1;
3481
3482        event = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
3483
3484        event->rscn.rscn_cmd = ELS_RSCN;
3485        event->rscn.rscn_page_len = sizeof(struct fc_els_rscn_page);
3486        event->rscn.rscn_plen = cpu_to_be16(cmdsize);
3487
3488        nportid = vport->fc_myDID;
3489        /* appears that page flags must be 0 for fabric to broadcast RSCN */
3490        event->portid.rscn_page_flags = 0;
3491        event->portid.rscn_fid[0] = (nportid & 0x00FF0000) >> 16;
3492        event->portid.rscn_fid[1] = (nportid & 0x0000FF00) >> 8;
3493        event->portid.rscn_fid[2] = nportid & 0x000000FF;
3494
3495        phba->fc_stat.elsXmitRSCN++;
3496        elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3497        elsiocb->context1 = lpfc_nlp_get(ndlp);
3498        if (!elsiocb->context1) {
3499                lpfc_els_free_iocb(phba, elsiocb);
3500                return 1;
3501        }
3502
3503        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3504                              "Issue RSCN:       did:x%x",
3505                              ndlp->nlp_DID, 0, 0);
3506
3507        rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3508        if (rc == IOCB_ERROR) {
3509                lpfc_els_free_iocb(phba, elsiocb);
3510                lpfc_nlp_put(ndlp);
3511                return 1;
3512        }
3513
3514        /* This will cause the callback-function lpfc_cmpl_els_cmd to
3515         * trigger the release of node.
3516         */
3517        if (!(vport->fc_flag & FC_PT2PT))
3518                lpfc_nlp_put(ndlp);
3519        return 0;
3520}
3521
3522/**
3523 * lpfc_issue_els_farpr - Issue a farp to an node on a vport
3524 * @vport: pointer to a host virtual N_Port data structure.
3525 * @nportid: N_Port identifier to the remote node.
3526 * @retry: number of retries to the command IOCB.
3527 *
3528 * This routine issues a Fibre Channel Address Resolution Response
3529 * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
3530 * is passed into the function. It first search the @vport node list to find
3531 * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
3532 * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
3533 * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
3534 *
3535 * Note that the ndlp reference count will be incremented by 1 for holding the
3536 * ndlp and the reference to ndlp will be stored into the context1 field of
3537 * the IOCB for the completion callback function to the FARPR ELS command.
3538 *
3539 * Return code
3540 *   0 - Successfully issued farpr command
3541 *   1 - Failed to issue farpr command
3542 **/
3543static int
3544lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
3545{
3546        int rc = 0;
3547        struct lpfc_hba  *phba = vport->phba;
3548        struct lpfc_iocbq *elsiocb;
3549        FARP *fp;
3550        uint8_t *pcmd;
3551        uint32_t *lp;
3552        uint16_t cmdsize;
3553        struct lpfc_nodelist *ondlp;
3554        struct lpfc_nodelist *ndlp;
3555
3556        cmdsize = (sizeof(uint32_t) + sizeof(FARP));
3557
3558        ndlp = lpfc_findnode_did(vport, nportid);
3559        if (!ndlp) {
3560                ndlp = lpfc_nlp_init(vport, nportid);
3561                if (!ndlp)
3562                        return 1;
3563                lpfc_enqueue_node(vport, ndlp);
3564        }
3565
3566        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3567                                     ndlp->nlp_DID, ELS_CMD_RNID);
3568        if (!elsiocb)
3569                return 1;
3570
3571        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3572
3573        *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
3574        pcmd += sizeof(uint32_t);
3575
3576        /* Fill in FARPR payload */
3577        fp = (FARP *) (pcmd);
3578        memset(fp, 0, sizeof(FARP));
3579        lp = (uint32_t *) pcmd;
3580        *lp++ = be32_to_cpu(nportid);
3581        *lp++ = be32_to_cpu(vport->fc_myDID);
3582        fp->Rflags = 0;
3583        fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
3584
3585        memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
3586        memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3587        ondlp = lpfc_findnode_did(vport, nportid);
3588        if (ondlp) {
3589                memcpy(&fp->OportName, &ondlp->nlp_portname,
3590                       sizeof(struct lpfc_name));
3591                memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
3592                       sizeof(struct lpfc_name));
3593        }
3594
3595        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3596                "Issue FARPR:     did:x%x",
3597                ndlp->nlp_DID, 0, 0);
3598
3599        phba->fc_stat.elsXmitFARPR++;
3600        elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3601        elsiocb->context1 = lpfc_nlp_get(ndlp);
3602        if (!elsiocb->context1) {
3603                lpfc_els_free_iocb(phba, elsiocb);
3604                return 1;
3605        }
3606
3607        rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3608        if (rc == IOCB_ERROR) {
3609                /* The additional lpfc_nlp_put will cause the following
3610                 * lpfc_els_free_iocb routine to trigger the release of
3611                 * the node.
3612                 */
3613                lpfc_els_free_iocb(phba, elsiocb);
3614                lpfc_nlp_put(ndlp);
3615                return 1;
3616        }
3617        /* This will cause the callback-function lpfc_cmpl_els_cmd to
3618         * trigger the release of the node.
3619         */
3620        /* Don't release reference count as RDF is likely outstanding */
3621        return 0;
3622}
3623
3624/**
3625 * lpfc_issue_els_rdf - Register for diagnostic functions from the fabric.
3626 * @vport: pointer to a host virtual N_Port data structure.
3627 * @retry: retry counter for the command IOCB.
3628 *
3629 * This routine issues an ELS RDF to the Fabric Controller to register
3630 * for diagnostic functions.
3631 *
3632 * Note that the ndlp reference count will be incremented by 1 for holding the
3633 * ndlp and the reference to ndlp will be stored into the context1 field of
3634 * the IOCB for the completion callback function to the RDF ELS command.
3635 *
3636 * Return code
3637 *   0 - Successfully issued rdf command
3638 *   1 - Failed to issue rdf command
3639 **/
3640int
3641lpfc_issue_els_rdf(struct lpfc_vport *vport, uint8_t retry)
3642{
3643        struct lpfc_hba *phba = vport->phba;
3644        struct lpfc_iocbq *elsiocb;
3645        struct lpfc_els_rdf_req *prdf;
3646        struct lpfc_nodelist *ndlp;
3647        uint16_t cmdsize;
3648        int rc;
3649
3650        cmdsize = sizeof(*prdf);
3651
3652        ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3653        if (!ndlp) {
3654                ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3655                if (!ndlp)
3656                        return -ENODEV;
3657                lpfc_enqueue_node(vport, ndlp);
3658        }
3659
3660        /* RDF ELS is not required on an NPIV VN_Port.  */
3661        if (vport->port_type == LPFC_NPIV_PORT) {
3662                lpfc_nlp_put(ndlp);
3663                return -EACCES;
3664        }
3665
3666        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3667                                     ndlp->nlp_DID, ELS_CMD_RDF);
3668        if (!elsiocb)
3669                return -ENOMEM;
3670
3671        if (phba->sli_rev == LPFC_SLI_REV4 &&
3672            !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
3673                lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3674                                 "0939 %s: FC_NODE x%x RPI x%x flag x%x "
3675                                 "ste x%x type x%x Not registered\n",
3676                                 __func__, ndlp->nlp_DID, ndlp->nlp_rpi,
3677                                 ndlp->nlp_flag, ndlp->nlp_state,
3678                                 ndlp->nlp_type);
3679                return -ENODEV;
3680        }
3681
3682        /* Configure the payload for the supported FPIN events. */
3683        prdf = (struct lpfc_els_rdf_req *)
3684                (((struct lpfc_dmabuf *)elsiocb->context2)->virt);
3685        memset(prdf, 0, cmdsize);
3686        prdf->rdf.fpin_cmd = ELS_RDF;
3687        prdf->rdf.desc_len = cpu_to_be32(sizeof(struct lpfc_els_rdf_req) -
3688                                         sizeof(struct fc_els_rdf));
3689        prdf->reg_d1.reg_desc.desc_tag = cpu_to_be32(ELS_DTAG_FPIN_REGISTER);
3690        prdf->reg_d1.reg_desc.desc_len = cpu_to_be32(
3691                                FC_TLV_DESC_LENGTH_FROM_SZ(prdf->reg_d1));
3692        prdf->reg_d1.reg_desc.count = cpu_to_be32(ELS_RDF_REG_TAG_CNT);
3693        prdf->reg_d1.desc_tags[0] = cpu_to_be32(ELS_DTAG_LNK_INTEGRITY);
3694        prdf->reg_d1.desc_tags[1] = cpu_to_be32(ELS_DTAG_DELIVERY);
3695        prdf->reg_d1.desc_tags[2] = cpu_to_be32(ELS_DTAG_PEER_CONGEST);
3696        prdf->reg_d1.desc_tags[3] = cpu_to_be32(ELS_DTAG_CONGESTION);
3697
3698        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3699                         "6444 Xmit RDF to remote NPORT x%x\n",
3700                         ndlp->nlp_DID);
3701
3702        elsiocb->iocb_cmpl = lpfc_cmpl_els_disc_cmd;
3703        elsiocb->context1 = lpfc_nlp_get(ndlp);
3704        if (!elsiocb->context1) {
3705                lpfc_els_free_iocb(phba, elsiocb);
3706                return -EIO;
3707        }
3708
3709        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3710                              "Issue RDF:     did:x%x refcnt %d",
3711                              ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3712
3713        rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3714        if (rc == IOCB_ERROR) {
3715                lpfc_els_free_iocb(phba, elsiocb);
3716                lpfc_nlp_put(ndlp);
3717                return -EIO;
3718        }
3719        return 0;
3720}
3721
3722 /**
3723  * lpfc_els_rcv_rdf - Receive RDF ELS request from the fabric.
3724  * @vport: pointer to a host virtual N_Port data structure.
3725  * @cmdiocb: pointer to lpfc command iocb data structure.
3726  * @ndlp: pointer to a node-list data structure.
3727  *
3728  * A received RDF implies a possible change to fabric supported diagnostic
3729  * functions.  This routine sends LS_ACC and then has the Nx_Port issue a new
3730  * RDF request to reregister for supported diagnostic functions.
3731  *
3732  * Return code
3733  *   0 - Success
3734  *   -EIO - Failed to process received RDF
3735  **/
3736static int
3737lpfc_els_rcv_rdf(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3738                 struct lpfc_nodelist *ndlp)
3739{
3740        /* Send LS_ACC */
3741        if (lpfc_els_rsp_acc(vport, ELS_CMD_RDF, cmdiocb, ndlp, NULL)) {
3742                lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3743                                 "1623 Failed to RDF_ACC from x%x for x%x\n",
3744                                 ndlp->nlp_DID, vport->fc_myDID);
3745                return -EIO;
3746        }
3747
3748        /* Issue new RDF for reregistering */
3749        if (lpfc_issue_els_rdf(vport, 0)) {
3750                lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3751                                 "2623 Failed to re register RDF for x%x\n",
3752                                 vport->fc_myDID);
3753                return -EIO;
3754        }
3755
3756        return 0;
3757}
3758
3759/**
3760 * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
3761 * @vport: pointer to a host virtual N_Port data structure.
3762 * @nlp: pointer to a node-list data structure.
3763 *
3764 * This routine cancels the timer with a delayed IOCB-command retry for
3765 * a @vport's @ndlp. It stops the timer for the delayed function retrial and
3766 * removes the ELS retry event if it presents. In addition, if the
3767 * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
3768 * commands are sent for the @vport's nodes that require issuing discovery
3769 * ADISC.
3770 **/
3771void
3772lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
3773{
3774        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3775        struct lpfc_work_evt *evtp;
3776
3777        if (!(nlp->nlp_flag & NLP_DELAY_TMO))
3778                return;
3779        spin_lock_irq(&nlp->lock);
3780        nlp->nlp_flag &= ~NLP_DELAY_TMO;
3781        spin_unlock_irq(&nlp->lock);
3782        del_timer_sync(&nlp->nlp_delayfunc);
3783        nlp->nlp_last_elscmd = 0;
3784        if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
3785                list_del_init(&nlp->els_retry_evt.evt_listp);
3786                /* Decrement nlp reference count held for the delayed retry */
3787                evtp = &nlp->els_retry_evt;
3788                lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
3789        }
3790        if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
3791                spin_lock_irq(&nlp->lock);
3792                nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
3793                spin_unlock_irq(&nlp->lock);
3794                if (vport->num_disc_nodes) {
3795                        if (vport->port_state < LPFC_VPORT_READY) {
3796                                /* Check if there are more ADISCs to be sent */
3797                                lpfc_more_adisc(vport);
3798                        } else {
3799                                /* Check if there are more PLOGIs to be sent */
3800                                lpfc_more_plogi(vport);
3801                                if (vport->num_disc_nodes == 0) {
3802                                        spin_lock_irq(shost->host_lock);
3803                                        vport->fc_flag &= ~FC_NDISC_ACTIVE;
3804                                        spin_unlock_irq(shost->host_lock);
3805                                        lpfc_can_disctmo(vport);
3806                                        lpfc_end_rscn(vport);
3807                                }
3808                        }
3809                }
3810        }
3811        return;
3812}
3813
3814/**
3815 * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
3816 * @t: pointer to the timer function associated data (ndlp).
3817 *
3818 * This routine is invoked by the ndlp delayed-function timer to check
3819 * whether there is any pending ELS retry event(s) with the node. If not, it
3820 * simply returns. Otherwise, if there is at least one ELS delayed event, it
3821 * adds the delayed events to the HBA work list and invokes the
3822 * lpfc_worker_wake_up() routine to wake up worker thread to process the
3823 * event. Note that lpfc_nlp_get() is called before posting the event to
3824 * the work list to hold reference count of ndlp so that it guarantees the
3825 * reference to ndlp will still be available when the worker thread gets
3826 * to the event associated with the ndlp.
3827 **/
3828void
3829lpfc_els_retry_delay(struct timer_list *t)
3830{
3831        struct lpfc_nodelist *ndlp = from_timer(ndlp, t, nlp_delayfunc);
3832        struct lpfc_vport *vport = ndlp->vport;
3833        struct lpfc_hba   *phba = vport->phba;
3834        unsigned long flags;
3835        struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
3836
3837        spin_lock_irqsave(&phba->hbalock, flags);
3838        if (!list_empty(&evtp->evt_listp)) {
3839                spin_unlock_irqrestore(&phba->hbalock, flags);
3840                return;
3841        }
3842
3843        /* We need to hold the node by incrementing the reference
3844         * count until the queued work is done
3845         */
3846        evtp->evt_arg1  = lpfc_nlp_get(ndlp);
3847        if (evtp->evt_arg1) {
3848                evtp->evt = LPFC_EVT_ELS_RETRY;
3849                list_add_tail(&evtp->evt_listp, &phba->work_list);
3850                lpfc_worker_wake_up(phba);
3851        }
3852        spin_unlock_irqrestore(&phba->hbalock, flags);
3853        return;
3854}
3855
3856/**
3857 * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
3858 * @ndlp: pointer to a node-list data structure.
3859 *
3860 * This routine is the worker-thread handler for processing the @ndlp delayed
3861 * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
3862 * the last ELS command from the associated ndlp and invokes the proper ELS
3863 * function according to the delayed ELS command to retry the command.
3864 **/
3865void
3866lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
3867{
3868        struct lpfc_vport *vport = ndlp->vport;
3869        uint32_t cmd, retry;
3870
3871        spin_lock_irq(&ndlp->lock);
3872        cmd = ndlp->nlp_last_elscmd;
3873        ndlp->nlp_last_elscmd = 0;
3874
3875        if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
3876                spin_unlock_irq(&ndlp->lock);
3877                return;
3878        }
3879
3880        ndlp->nlp_flag &= ~NLP_DELAY_TMO;
3881        spin_unlock_irq(&ndlp->lock);
3882        /*
3883         * If a discovery event readded nlp_delayfunc after timer
3884         * firing and before processing the timer, cancel the
3885         * nlp_delayfunc.
3886         */
3887        del_timer_sync(&ndlp->nlp_delayfunc);
3888        retry = ndlp->nlp_retry;
3889        ndlp->nlp_retry = 0;
3890
3891        switch (cmd) {
3892        case ELS_CMD_FLOGI:
3893                lpfc_issue_els_flogi(vport, ndlp, retry);
3894                break;
3895        case ELS_CMD_PLOGI:
3896                if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
3897                        ndlp->nlp_prev_state = ndlp->nlp_state;
3898                        lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3899                }
3900                break;
3901        case ELS_CMD_ADISC:
3902                if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
3903                        ndlp->nlp_prev_state = ndlp->nlp_state;
3904                        lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3905                }
3906                break;
3907        case ELS_CMD_PRLI:
3908        case ELS_CMD_NVMEPRLI:
3909                if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
3910                        ndlp->nlp_prev_state = ndlp->nlp_state;
3911                        lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3912                }
3913                break;
3914        case ELS_CMD_LOGO:
3915                if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
3916                        ndlp->nlp_prev_state = ndlp->nlp_state;
3917                        lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3918                }
3919                break;
3920        case ELS_CMD_FDISC:
3921                if (!(vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI))
3922                        lpfc_issue_els_fdisc(vport, ndlp, retry);
3923                break;
3924        }
3925        return;
3926}
3927
3928/**
3929 * lpfc_link_reset - Issue link reset
3930 * @vport: pointer to a virtual N_Port data structure.
3931 *
3932 * This routine performs link reset by sending INIT_LINK mailbox command.
3933 * For SLI-3 adapter, link attention interrupt is enabled before issuing
3934 * INIT_LINK mailbox command.
3935 *
3936 * Return code
3937 *   0 - Link reset initiated successfully
3938 *   1 - Failed to initiate link reset
3939 **/
3940int
3941lpfc_link_reset(struct lpfc_vport *vport)
3942{
3943        struct lpfc_hba *phba = vport->phba;
3944        LPFC_MBOXQ_t *mbox;
3945        uint32_t control;
3946        int rc;
3947
3948        lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3949                         "2851 Attempt link reset\n");
3950        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3951        if (!mbox) {
3952                lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3953                                "2852 Failed to allocate mbox memory");
3954                return 1;
3955        }
3956
3957        /* Enable Link attention interrupts */
3958        if (phba->sli_rev <= LPFC_SLI_REV3) {
3959                spin_lock_irq(&phba->hbalock);
3960                phba->sli.sli_flag |= LPFC_PROCESS_LA;
3961                control = readl(phba->HCregaddr);
3962                control |= HC_LAINT_ENA;
3963                writel(control, phba->HCregaddr);
3964                readl(phba->HCregaddr); /* flush */
3965                spin_unlock_irq(&phba->hbalock);
3966        }
3967
3968        lpfc_init_link(phba, mbox, phba->cfg_topology,
3969                       phba->cfg_link_speed);
3970        mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3971        mbox->vport = vport;
3972        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3973        if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
3974                lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3975                                "2853 Failed to issue INIT_LINK "
3976                                "mbox command, rc:x%x\n", rc);
3977                mempool_free(mbox, phba->mbox_mem_pool);
3978                return 1;
3979        }
3980
3981        return 0;
3982}
3983
3984/**
3985 * lpfc_els_retry - Make retry decision on an els command iocb
3986 * @phba: pointer to lpfc hba data structure.
3987 * @cmdiocb: pointer to lpfc command iocb data structure.
3988 * @rspiocb: pointer to lpfc response iocb data structure.
3989 *
3990 * This routine makes a retry decision on an ELS command IOCB, which has
3991 * failed. The following ELS IOCBs use this function for retrying the command
3992 * when previously issued command responsed with error status: FLOGI, PLOGI,
3993 * PRLI, ADISC and FDISC. Based on the ELS command type and the
3994 * returned error status, it makes the decision whether a retry shall be
3995 * issued for the command, and whether a retry shall be made immediately or
3996 * delayed. In the former case, the corresponding ELS command issuing-function
3997 * is called to retry the command. In the later case, the ELS command shall
3998 * be posted to the ndlp delayed event and delayed function timer set to the
3999 * ndlp for the delayed command issusing.
4000 *
4001 * Return code
4002 *   0 - No retry of els command is made
4003 *   1 - Immediate or delayed retry of els command is made
4004 **/
4005static int
4006lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4007               struct lpfc_iocbq *rspiocb)
4008{
4009        struct lpfc_vport *vport = cmdiocb->vport;
4010        IOCB_t *irsp = &rspiocb->iocb;
4011        struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4012        struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4013        uint32_t *elscmd;
4014        struct ls_rjt stat;
4015        int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
4016        int logerr = 0;
4017        uint32_t cmd = 0;
4018        uint32_t did;
4019        int link_reset = 0, rc;
4020
4021
4022        /* Note: context2 may be 0 for internal driver abort
4023         * of delays ELS command.
4024         */
4025
4026        if (pcmd && pcmd->virt) {
4027                elscmd = (uint32_t *) (pcmd->virt);
4028                cmd = *elscmd++;
4029        }
4030
4031        if (ndlp)
4032                did = ndlp->nlp_DID;
4033        else {
4034                /* We should only hit this case for retrying PLOGI */
4035                did = irsp->un.elsreq64.remoteID;
4036                ndlp = lpfc_findnode_did(vport, did);
4037                if (!ndlp && (cmd != ELS_CMD_PLOGI))
4038                        return 0;
4039        }
4040
4041        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4042                "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
4043                *(((uint32_t *)irsp) + 7), irsp->un.ulpWord[4], did);
4044
4045        switch (irsp->ulpStatus) {
4046        case IOSTAT_FCP_RSP_ERROR:
4047                break;
4048        case IOSTAT_REMOTE_STOP:
4049                if (phba->sli_rev == LPFC_SLI_REV4) {
4050                        /* This IO was aborted by the target, we don't
4051                         * know the rxid and because we did not send the
4052                         * ABTS we cannot generate and RRQ.
4053                         */
4054                        lpfc_set_rrq_active(phba, ndlp,
4055                                         cmdiocb->sli4_lxritag, 0, 0);
4056                }
4057                break;
4058        case IOSTAT_LOCAL_REJECT:
4059                switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
4060                case IOERR_LOOP_OPEN_FAILURE:
4061                        if (cmd == ELS_CMD_FLOGI) {
4062                                if (PCI_DEVICE_ID_HORNET ==
4063                                        phba->pcidev->device) {
4064                                        phba->fc_topology = LPFC_TOPOLOGY_LOOP;
4065                                        phba->pport->fc_myDID = 0;
4066                                        phba->alpa_map[0] = 0;
4067                                        phba->alpa_map[1] = 0;
4068                                }
4069                        }
4070                        if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
4071                                delay = 1000;
4072                        retry = 1;
4073                        break;
4074
4075                case IOERR_ILLEGAL_COMMAND:
4076                        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4077                                         "0124 Retry illegal cmd x%x "
4078                                         "retry:x%x delay:x%x\n",
4079                                         cmd, cmdiocb->retry, delay);
4080                        retry = 1;
4081                        /* All command's retry policy */
4082                        maxretry = 8;
4083                        if (cmdiocb->retry > 2)
4084                                delay = 1000;
4085                        break;
4086
4087                case IOERR_NO_RESOURCES:
4088                        logerr = 1; /* HBA out of resources */
4089                        retry = 1;
4090                        if (cmdiocb->retry > 100)
4091                                delay = 100;
4092                        maxretry = 250;
4093                        break;
4094
4095                case IOERR_ILLEGAL_FRAME:
4096                        delay = 100;
4097                        retry = 1;
4098                        break;
4099
4100                case IOERR_INVALID_RPI:
4101                        if (cmd == ELS_CMD_PLOGI &&
4102                            did == NameServer_DID) {
4103                                /* Continue forever if plogi to */
4104                                /* the nameserver fails */
4105                                maxretry = 0;
4106                                delay = 100;
4107                        }
4108                        retry = 1;
4109                        break;
4110
4111                case IOERR_SEQUENCE_TIMEOUT:
4112                        if (cmd == ELS_CMD_PLOGI &&
4113                            did == NameServer_DID &&
4114                            (cmdiocb->retry + 1) == maxretry) {
4115                                /* Reset the Link */
4116                                link_reset = 1;
4117                                break;
4118                        }
4119                        retry = 1;
4120                        delay = 100;
4121                        break;
4122                }
4123                break;
4124
4125        case IOSTAT_NPORT_RJT:
4126        case IOSTAT_FABRIC_RJT:
4127                if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
4128                        retry = 1;
4129                        break;
4130                }
4131                break;
4132
4133        case IOSTAT_NPORT_BSY:
4134        case IOSTAT_FABRIC_BSY:
4135                logerr = 1; /* Fabric / Remote NPort out of resources */
4136                retry = 1;
4137                break;
4138
4139        case IOSTAT_LS_RJT:
4140                stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
4141                /* Added for Vendor specifc support
4142                 * Just keep retrying for these Rsn / Exp codes
4143                 */
4144                switch (stat.un.b.lsRjtRsnCode) {
4145                case LSRJT_UNABLE_TPC:
4146                        /* The driver has a VALID PLOGI but the rport has
4147                         * rejected the PRLI - can't do it now.  Delay
4148                         * for 1 second and try again.
4149                         *
4150                         * However, if explanation is REQ_UNSUPPORTED there's
4151                         * no point to retry PRLI.
4152                         */
4153                        if ((cmd == ELS_CMD_PRLI || cmd == ELS_CMD_NVMEPRLI) &&
4154                            stat.un.b.lsRjtRsnCodeExp !=
4155                            LSEXP_REQ_UNSUPPORTED) {
4156                                delay = 1000;
4157                                maxretry = lpfc_max_els_tries + 1;
4158                                retry = 1;
4159                                break;
4160                        }
4161
4162                        /* Legacy bug fix code for targets with PLOGI delays. */
4163                        if (stat.un.b.lsRjtRsnCodeExp ==
4164                            LSEXP_CMD_IN_PROGRESS) {
4165                                if (cmd == ELS_CMD_PLOGI) {
4166                                        delay = 1000;
4167                                        maxretry = 48;
4168                                }
4169                                retry = 1;
4170                                break;
4171                        }
4172                        if (stat.un.b.lsRjtRsnCodeExp ==
4173                            LSEXP_CANT_GIVE_DATA) {
4174                                if (cmd == ELS_CMD_PLOGI) {
4175                                        delay = 1000;
4176                                        maxretry = 48;
4177                                }
4178                                retry = 1;
4179                                break;
4180                        }
4181                        if (cmd == ELS_CMD_PLOGI) {
4182                                delay = 1000;
4183                                maxretry = lpfc_max_els_tries + 1;
4184                                retry = 1;
4185                                break;
4186                        }
4187                        if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4188                          (cmd == ELS_CMD_FDISC) &&
4189                          (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
4190                                lpfc_printf_vlog(vport, KERN_ERR,
4191                                                 LOG_TRACE_EVENT,
4192                                                 "0125 FDISC Failed (x%x). "
4193                                                 "Fabric out of resources\n",
4194                                                 stat.un.lsRjtError);
4195                                lpfc_vport_set_state(vport,
4196                                                     FC_VPORT_NO_FABRIC_RSCS);
4197                        }
4198                        break;
4199
4200                case LSRJT_LOGICAL_BSY:
4201                        if ((cmd == ELS_CMD_PLOGI) ||
4202                            (cmd == ELS_CMD_PRLI) ||
4203                            (cmd == ELS_CMD_NVMEPRLI)) {
4204                                delay = 1000;
4205                                maxretry = 48;
4206                        } else if (cmd == ELS_CMD_FDISC) {
4207                                /* FDISC retry policy */
4208                                maxretry = 48;
4209                                if (cmdiocb->retry >= 32)
4210                                        delay = 1000;
4211                        }
4212                        retry = 1;
4213                        break;
4214
4215                case LSRJT_LOGICAL_ERR:
4216                        /* There are some cases where switches return this
4217                         * error when they are not ready and should be returning
4218                         * Logical Busy. We should delay every time.
4219                         */
4220                        if (cmd == ELS_CMD_FDISC &&
4221                            stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
4222                                maxretry = 3;
4223                                delay = 1000;
4224                                retry = 1;
4225                        } else if (cmd == ELS_CMD_FLOGI &&
4226                                   stat.un.b.lsRjtRsnCodeExp ==
4227                                                LSEXP_NOTHING_MORE) {
4228                                vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
4229                                retry = 1;
4230                                lpfc_printf_vlog(vport, KERN_ERR,
4231                                                 LOG_TRACE_EVENT,
4232                                                 "0820 FLOGI Failed (x%x). "
4233                                                 "BBCredit Not Supported\n",
4234                                                 stat.un.lsRjtError);
4235                        }
4236                        break;
4237
4238                case LSRJT_PROTOCOL_ERR:
4239                        if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4240                          (cmd == ELS_CMD_FDISC) &&
4241                          ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
4242                          (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
4243                          ) {
4244                                lpfc_printf_vlog(vport, KERN_ERR,
4245                                                 LOG_TRACE_EVENT,
4246                                                 "0122 FDISC Failed (x%x). "
4247                                                 "Fabric Detected Bad WWN\n",
4248                                                 stat.un.lsRjtError);
4249                                lpfc_vport_set_state(vport,
4250                                                     FC_VPORT_FABRIC_REJ_WWN);
4251                        }
4252                        break;
4253                case LSRJT_VENDOR_UNIQUE:
4254                        if ((stat.un.b.vendorUnique == 0x45) &&
4255                            (cmd == ELS_CMD_FLOGI)) {
4256                                goto out_retry;
4257                        }
4258                        break;
4259                case LSRJT_CMD_UNSUPPORTED:
4260                        /* lpfc nvmet returns this type of LS_RJT when it
4261                         * receives an FCP PRLI because lpfc nvmet only
4262                         * support NVME.  ELS request is terminated for FCP4
4263                         * on this rport.
4264                         */
4265                        if (stat.un.b.lsRjtRsnCodeExp ==
4266                            LSEXP_REQ_UNSUPPORTED && cmd == ELS_CMD_PRLI) {
4267                                spin_lock_irq(&ndlp->lock);
4268                                ndlp->nlp_flag |= NLP_FCP_PRLI_RJT;
4269                                spin_unlock_irq(&ndlp->lock);
4270                                retry = 0;
4271                                goto out_retry;
4272                        }
4273                        break;
4274                }
4275                break;
4276
4277        case IOSTAT_INTERMED_RSP:
4278        case IOSTAT_BA_RJT:
4279                break;
4280
4281        default:
4282                break;
4283        }
4284
4285        if (link_reset) {
4286                rc = lpfc_link_reset(vport);
4287                if (rc) {
4288                        /* Do not give up. Retry PLOGI one more time and attempt
4289                         * link reset if PLOGI fails again.
4290                         */
4291                        retry = 1;
4292                        delay = 100;
4293                        goto out_retry;
4294                }
4295                return 1;
4296        }
4297
4298        if (did == FDMI_DID)
4299                retry = 1;
4300
4301        if ((cmd == ELS_CMD_FLOGI) &&
4302            (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
4303            !lpfc_error_lost_link(irsp)) {
4304                /* FLOGI retry policy */
4305                retry = 1;
4306                /* retry FLOGI forever */
4307                if (phba->link_flag != LS_LOOPBACK_MODE)
4308                        maxretry = 0;
4309                else
4310                        maxretry = 2;
4311
4312                if (cmdiocb->retry >= 100)
4313                        delay = 5000;
4314                else if (cmdiocb->retry >= 32)
4315                        delay = 1000;
4316        } else if ((cmd == ELS_CMD_FDISC) && !lpfc_error_lost_link(irsp)) {
4317                /* retry FDISCs every second up to devloss */
4318                retry = 1;
4319                maxretry = vport->cfg_devloss_tmo;
4320                delay = 1000;
4321        }
4322
4323        cmdiocb->retry++;
4324        if (maxretry && (cmdiocb->retry >= maxretry)) {
4325                phba->fc_stat.elsRetryExceeded++;
4326                retry = 0;
4327        }
4328
4329        if ((vport->load_flag & FC_UNLOADING) != 0)
4330                retry = 0;
4331
4332out_retry:
4333        if (retry) {
4334                if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
4335                        /* Stop retrying PLOGI and FDISC if in FCF discovery */