Back to home page

OSCL-LXR

 
 

    


0001 /*******************************************************************
0002  * This file is part of the Emulex Linux Device Driver for         *
0003  * Fibre Channel Host Bus Adapters.                                *
0004  * Copyright (C) 2017-2022 Broadcom. All Rights Reserved. The term *
0005  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
0006  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
0007  * EMULEX and SLI are trademarks of Emulex.                        *
0008  * www.broadcom.com                                                *
0009  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
0010  *                                                                 *
0011  * This program is free software; you can redistribute it and/or   *
0012  * modify it under the terms of version 2 of the GNU General       *
0013  * Public License as published by the Free Software Foundation.    *
0014  * This program is distributed in the hope that it will be useful. *
0015  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
0016  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
0017  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
0018  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
0019  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
0020  * more details, a copy of which can be found in the file COPYING  *
0021  * included with this package.                                     *
0022  *******************************************************************/
0023 /* See Fibre Channel protocol T11 FC-LS for details */
0024 #include <linux/blkdev.h>
0025 #include <linux/pci.h>
0026 #include <linux/slab.h>
0027 #include <linux/interrupt.h>
0028 #include <linux/delay.h>
0029 
0030 #include <scsi/scsi.h>
0031 #include <scsi/scsi_device.h>
0032 #include <scsi/scsi_host.h>
0033 #include <scsi/scsi_transport_fc.h>
0034 #include <uapi/scsi/fc/fc_fs.h>
0035 #include <uapi/scsi/fc/fc_els.h>
0036 
0037 #include "lpfc_hw4.h"
0038 #include "lpfc_hw.h"
0039 #include "lpfc_sli.h"
0040 #include "lpfc_sli4.h"
0041 #include "lpfc_nl.h"
0042 #include "lpfc_disc.h"
0043 #include "lpfc_scsi.h"
0044 #include "lpfc.h"
0045 #include "lpfc_logmsg.h"
0046 #include "lpfc_crtn.h"
0047 #include "lpfc_vport.h"
0048 #include "lpfc_debugfs.h"
0049 
0050 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
0051               struct lpfc_iocbq *);
0052 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
0053             struct lpfc_iocbq *);
0054 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
0055 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
0056                 struct lpfc_nodelist *ndlp, uint8_t retry);
0057 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
0058                   struct lpfc_iocbq *iocb);
0059 static void lpfc_cmpl_els_edc(struct lpfc_hba *phba,
0060                   struct lpfc_iocbq *cmdiocb,
0061                   struct lpfc_iocbq *rspiocb);
0062 static void lpfc_cmpl_els_uvem(struct lpfc_hba *, struct lpfc_iocbq *,
0063                    struct lpfc_iocbq *);
0064 
0065 static int lpfc_max_els_tries = 3;
0066 
0067 static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport);
0068 static void lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max);
0069 static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid);
0070 
0071 /**
0072  * lpfc_els_chk_latt - Check host link attention event for a vport
0073  * @vport: pointer to a host virtual N_Port data structure.
0074  *
0075  * This routine checks whether there is an outstanding host link
0076  * attention event during the discovery process with the @vport. It is done
0077  * by reading the HBA's Host Attention (HA) register. If there is any host
0078  * link attention events during this @vport's discovery process, the @vport
0079  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
0080  * be issued if the link state is not already in host link cleared state,
0081  * and a return code shall indicate whether the host link attention event
0082  * had happened.
0083  *
0084  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
0085  * state in LPFC_VPORT_READY, the request for checking host link attention
0086  * event will be ignored and a return code shall indicate no host link
0087  * attention event had happened.
0088  *
0089  * Return codes
0090  *   0 - no host link attention event happened
0091  *   1 - host link attention event happened
0092  **/
0093 int
0094 lpfc_els_chk_latt(struct lpfc_vport *vport)
0095 {
0096     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
0097     struct lpfc_hba  *phba = vport->phba;
0098     uint32_t ha_copy;
0099 
0100     if (vport->port_state >= LPFC_VPORT_READY ||
0101         phba->link_state == LPFC_LINK_DOWN ||
0102         phba->sli_rev > LPFC_SLI_REV3)
0103         return 0;
0104 
0105     /* Read the HBA Host Attention Register */
0106     if (lpfc_readl(phba->HAregaddr, &ha_copy))
0107         return 1;
0108 
0109     if (!(ha_copy & HA_LATT))
0110         return 0;
0111 
0112     /* Pending Link Event during Discovery */
0113     lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0114              "0237 Pending Link Event during "
0115              "Discovery: State x%x\n",
0116              phba->pport->port_state);
0117 
0118     /* CLEAR_LA should re-enable link attention events and
0119      * we should then immediately take a LATT event. The
0120      * LATT processing should call lpfc_linkdown() which
0121      * will cleanup any left over in-progress discovery
0122      * events.
0123      */
0124     spin_lock_irq(shost->host_lock);
0125     vport->fc_flag |= FC_ABORT_DISCOVERY;
0126     spin_unlock_irq(shost->host_lock);
0127 
0128     if (phba->link_state != LPFC_CLEAR_LA)
0129         lpfc_issue_clear_la(phba, vport);
0130 
0131     return 1;
0132 }
0133 
0134 /**
0135  * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
0136  * @vport: pointer to a host virtual N_Port data structure.
0137  * @expect_rsp: flag indicating whether response is expected.
0138  * @cmd_size: size of the ELS command.
0139  * @retry: number of retries to the command when it fails.
0140  * @ndlp: pointer to a node-list data structure.
0141  * @did: destination identifier.
0142  * @elscmd: the ELS command code.
0143  *
0144  * This routine is used for allocating a lpfc-IOCB data structure from
0145  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
0146  * passed into the routine for discovery state machine to issue an Extended
0147  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
0148  * and preparation routine that is used by all the discovery state machine
0149  * routines and the ELS command-specific fields will be later set up by
0150  * the individual discovery machine routines after calling this routine
0151  * allocating and preparing a generic IOCB data structure. It fills in the
0152  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
0153  * payload and response payload (if expected). The reference count on the
0154  * ndlp is incremented by 1 and the reference to the ndlp is put into
0155  * ndlp of the IOCB data structure for this IOCB to hold the ndlp
0156  * reference for the command's callback function to access later.
0157  *
0158  * Return code
0159  *   Pointer to the newly allocated/prepared els iocb data structure
0160  *   NULL - when els iocb data structure allocation/preparation failed
0161  **/
0162 struct lpfc_iocbq *
0163 lpfc_prep_els_iocb(struct lpfc_vport *vport, u8 expect_rsp,
0164            u16 cmd_size, u8 retry,
0165            struct lpfc_nodelist *ndlp, u32 did,
0166            u32 elscmd)
0167 {
0168     struct lpfc_hba  *phba = vport->phba;
0169     struct lpfc_iocbq *elsiocb;
0170     struct lpfc_dmabuf *pcmd, *prsp, *pbuflist, *bmp;
0171     struct ulp_bde64_le *bpl;
0172     u32 timeout = 0;
0173 
0174     if (!lpfc_is_link_up(phba))
0175         return NULL;
0176 
0177     /* Allocate buffer for  command iocb */
0178     elsiocb = lpfc_sli_get_iocbq(phba);
0179     if (!elsiocb)
0180         return NULL;
0181 
0182     /*
0183      * If this command is for fabric controller and HBA running
0184      * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
0185      */
0186     if ((did == Fabric_DID) &&
0187         (phba->hba_flag & HBA_FIP_SUPPORT) &&
0188         ((elscmd == ELS_CMD_FLOGI) ||
0189          (elscmd == ELS_CMD_FDISC) ||
0190          (elscmd == ELS_CMD_LOGO)))
0191         switch (elscmd) {
0192         case ELS_CMD_FLOGI:
0193             elsiocb->cmd_flag |=
0194                 ((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
0195                  & LPFC_FIP_ELS_ID_MASK);
0196             break;
0197         case ELS_CMD_FDISC:
0198             elsiocb->cmd_flag |=
0199                 ((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
0200                  & LPFC_FIP_ELS_ID_MASK);
0201             break;
0202         case ELS_CMD_LOGO:
0203             elsiocb->cmd_flag |=
0204                 ((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
0205                  & LPFC_FIP_ELS_ID_MASK);
0206             break;
0207         }
0208     else
0209         elsiocb->cmd_flag &= ~LPFC_FIP_ELS_ID_MASK;
0210 
0211     /* fill in BDEs for command */
0212     /* Allocate buffer for command payload */
0213     pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
0214     if (pcmd)
0215         pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
0216     if (!pcmd || !pcmd->virt)
0217         goto els_iocb_free_pcmb_exit;
0218 
0219     INIT_LIST_HEAD(&pcmd->list);
0220 
0221     /* Allocate buffer for response payload */
0222     if (expect_rsp) {
0223         prsp = kmalloc(sizeof(*prsp), GFP_KERNEL);
0224         if (prsp)
0225             prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
0226                              &prsp->phys);
0227         if (!prsp || !prsp->virt)
0228             goto els_iocb_free_prsp_exit;
0229         INIT_LIST_HEAD(&prsp->list);
0230     } else {
0231         prsp = NULL;
0232     }
0233 
0234     /* Allocate buffer for Buffer ptr list */
0235     pbuflist = kmalloc(sizeof(*pbuflist), GFP_KERNEL);
0236     if (pbuflist)
0237         pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
0238                          &pbuflist->phys);
0239     if (!pbuflist || !pbuflist->virt)
0240         goto els_iocb_free_pbuf_exit;
0241 
0242     INIT_LIST_HEAD(&pbuflist->list);
0243 
0244     if (expect_rsp) {
0245         switch (elscmd) {
0246         case ELS_CMD_FLOGI:
0247             timeout = FF_DEF_RATOV * 2;
0248             break;
0249         case ELS_CMD_LOGO:
0250             timeout = phba->fc_ratov;
0251             break;
0252         default:
0253             timeout = phba->fc_ratov * 2;
0254         }
0255 
0256         /* Fill SGE for the num bde count */
0257         elsiocb->num_bdes = 2;
0258     }
0259 
0260     if (phba->sli_rev == LPFC_SLI_REV4)
0261         bmp = pcmd;
0262     else
0263         bmp = pbuflist;
0264 
0265     lpfc_sli_prep_els_req_rsp(phba, elsiocb, vport, bmp, cmd_size, did,
0266                   elscmd, timeout, expect_rsp);
0267 
0268     bpl = (struct ulp_bde64_le *)pbuflist->virt;
0269     bpl->addr_low = cpu_to_le32(putPaddrLow(pcmd->phys));
0270     bpl->addr_high = cpu_to_le32(putPaddrHigh(pcmd->phys));
0271     bpl->type_size = cpu_to_le32(cmd_size);
0272     bpl->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
0273 
0274     if (expect_rsp) {
0275         bpl++;
0276         bpl->addr_low = cpu_to_le32(putPaddrLow(prsp->phys));
0277         bpl->addr_high = cpu_to_le32(putPaddrHigh(prsp->phys));
0278         bpl->type_size = cpu_to_le32(FCELSSIZE);
0279         bpl->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
0280     }
0281 
0282     elsiocb->cmd_dmabuf = pcmd;
0283     elsiocb->bpl_dmabuf = pbuflist;
0284     elsiocb->retry = retry;
0285     elsiocb->vport = vport;
0286     elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
0287 
0288     if (prsp)
0289         list_add(&prsp->list, &pcmd->list);
0290     if (expect_rsp) {
0291         /* Xmit ELS command <elsCmd> to remote NPORT <did> */
0292         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
0293                  "0116 Xmit ELS command x%x to remote "
0294                  "NPORT x%x I/O tag: x%x, port state:x%x "
0295                  "rpi x%x fc_flag:x%x\n",
0296                  elscmd, did, elsiocb->iotag,
0297                  vport->port_state, ndlp->nlp_rpi,
0298                  vport->fc_flag);
0299     } else {
0300         /* Xmit ELS response <elsCmd> to remote NPORT <did> */
0301         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
0302                  "0117 Xmit ELS response x%x to remote "
0303                  "NPORT x%x I/O tag: x%x, size: x%x "
0304                  "port_state x%x  rpi x%x fc_flag x%x\n",
0305                  elscmd, ndlp->nlp_DID, elsiocb->iotag,
0306                  cmd_size, vport->port_state,
0307                  ndlp->nlp_rpi, vport->fc_flag);
0308     }
0309 
0310     return elsiocb;
0311 
0312 els_iocb_free_pbuf_exit:
0313     if (expect_rsp)
0314         lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
0315     kfree(pbuflist);
0316 
0317 els_iocb_free_prsp_exit:
0318     lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
0319     kfree(prsp);
0320 
0321 els_iocb_free_pcmb_exit:
0322     kfree(pcmd);
0323     lpfc_sli_release_iocbq(phba, elsiocb);
0324     return NULL;
0325 }
0326 
0327 /**
0328  * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
0329  * @vport: pointer to a host virtual N_Port data structure.
0330  *
0331  * This routine issues a fabric registration login for a @vport. An
0332  * active ndlp node with Fabric_DID must already exist for this @vport.
0333  * The routine invokes two mailbox commands to carry out fabric registration
0334  * login through the HBA firmware: the first mailbox command requests the
0335  * HBA to perform link configuration for the @vport; and the second mailbox
0336  * command requests the HBA to perform the actual fabric registration login
0337  * with the @vport.
0338  *
0339  * Return code
0340  *   0 - successfully issued fabric registration login for @vport
0341  *   -ENXIO -- failed to issue fabric registration login for @vport
0342  **/
0343 int
0344 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
0345 {
0346     struct lpfc_hba  *phba = vport->phba;
0347     LPFC_MBOXQ_t *mbox;
0348     struct lpfc_nodelist *ndlp;
0349     struct serv_parm *sp;
0350     int rc;
0351     int err = 0;
0352 
0353     sp = &phba->fc_fabparam;
0354     ndlp = lpfc_findnode_did(vport, Fabric_DID);
0355     if (!ndlp) {
0356         err = 1;
0357         goto fail;
0358     }
0359 
0360     mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
0361     if (!mbox) {
0362         err = 2;
0363         goto fail;
0364     }
0365 
0366     vport->port_state = LPFC_FABRIC_CFG_LINK;
0367     lpfc_config_link(phba, mbox);
0368     mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
0369     mbox->vport = vport;
0370 
0371     rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
0372     if (rc == MBX_NOT_FINISHED) {
0373         err = 3;
0374         goto fail_free_mbox;
0375     }
0376 
0377     mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
0378     if (!mbox) {
0379         err = 4;
0380         goto fail;
0381     }
0382     rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
0383               ndlp->nlp_rpi);
0384     if (rc) {
0385         err = 5;
0386         goto fail_free_mbox;
0387     }
0388 
0389     mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
0390     mbox->vport = vport;
0391     /* increment the reference count on ndlp to hold reference
0392      * for the callback routine.
0393      */
0394     mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
0395     if (!mbox->ctx_ndlp) {
0396         err = 6;
0397         goto fail_free_mbox;
0398     }
0399 
0400     rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
0401     if (rc == MBX_NOT_FINISHED) {
0402         err = 7;
0403         goto fail_issue_reg_login;
0404     }
0405 
0406     return 0;
0407 
0408 fail_issue_reg_login:
0409     /* decrement the reference count on ndlp just incremented
0410      * for the failed mbox command.
0411      */
0412     lpfc_nlp_put(ndlp);
0413 fail_free_mbox:
0414     lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
0415 fail:
0416     lpfc_vport_set_state(vport, FC_VPORT_FAILED);
0417     lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0418              "0249 Cannot issue Register Fabric login: Err %d\n",
0419              err);
0420     return -ENXIO;
0421 }
0422 
0423 /**
0424  * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
0425  * @vport: pointer to a host virtual N_Port data structure.
0426  *
0427  * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
0428  * the @vport. This mailbox command is necessary for SLI4 port only.
0429  *
0430  * Return code
0431  *   0 - successfully issued REG_VFI for @vport
0432  *   A failure code otherwise.
0433  **/
0434 int
0435 lpfc_issue_reg_vfi(struct lpfc_vport *vport)
0436 {
0437     struct lpfc_hba  *phba = vport->phba;
0438     LPFC_MBOXQ_t *mboxq = NULL;
0439     struct lpfc_nodelist *ndlp;
0440     struct lpfc_dmabuf *dmabuf = NULL;
0441     int rc = 0;
0442 
0443     /* move forward in case of SLI4 FC port loopback test and pt2pt mode */
0444     if ((phba->sli_rev == LPFC_SLI_REV4) &&
0445         !(phba->link_flag & LS_LOOPBACK_MODE) &&
0446         !(vport->fc_flag & FC_PT2PT)) {
0447         ndlp = lpfc_findnode_did(vport, Fabric_DID);
0448         if (!ndlp) {
0449             rc = -ENODEV;
0450             goto fail;
0451         }
0452     }
0453 
0454     mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
0455     if (!mboxq) {
0456         rc = -ENOMEM;
0457         goto fail;
0458     }
0459 
0460     /* Supply CSP's only if we are fabric connect or pt-to-pt connect */
0461     if ((vport->fc_flag & FC_FABRIC) || (vport->fc_flag & FC_PT2PT)) {
0462         rc = lpfc_mbox_rsrc_prep(phba, mboxq);
0463         if (rc) {
0464             rc = -ENOMEM;
0465             goto fail_mbox;
0466         }
0467         dmabuf = mboxq->ctx_buf;
0468         memcpy(dmabuf->virt, &phba->fc_fabparam,
0469                sizeof(struct serv_parm));
0470     }
0471 
0472     vport->port_state = LPFC_FABRIC_CFG_LINK;
0473     if (dmabuf) {
0474         lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
0475         /* lpfc_reg_vfi memsets the mailbox.  Restore the ctx_buf. */
0476         mboxq->ctx_buf = dmabuf;
0477     } else {
0478         lpfc_reg_vfi(mboxq, vport, 0);
0479     }
0480 
0481     mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
0482     mboxq->vport = vport;
0483     rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
0484     if (rc == MBX_NOT_FINISHED) {
0485         rc = -ENXIO;
0486         goto fail_mbox;
0487     }
0488     return 0;
0489 
0490 fail_mbox:
0491     lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
0492 fail:
0493     lpfc_vport_set_state(vport, FC_VPORT_FAILED);
0494     lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0495              "0289 Issue Register VFI failed: Err %d\n", rc);
0496     return rc;
0497 }
0498 
0499 /**
0500  * lpfc_issue_unreg_vfi - Unregister VFI for this vport's fabric login
0501  * @vport: pointer to a host virtual N_Port data structure.
0502  *
0503  * This routine issues a UNREG_VFI mailbox with the vfi, vpi, fcfi triplet for
0504  * the @vport. This mailbox command is necessary for SLI4 port only.
0505  *
0506  * Return code
0507  *   0 - successfully issued REG_VFI for @vport
0508  *   A failure code otherwise.
0509  **/
0510 int
0511 lpfc_issue_unreg_vfi(struct lpfc_vport *vport)
0512 {
0513     struct lpfc_hba *phba = vport->phba;
0514     struct Scsi_Host *shost;
0515     LPFC_MBOXQ_t *mboxq;
0516     int rc;
0517 
0518     mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
0519     if (!mboxq) {
0520         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
0521                 "2556 UNREG_VFI mbox allocation failed"
0522                 "HBA state x%x\n", phba->pport->port_state);
0523         return -ENOMEM;
0524     }
0525 
0526     lpfc_unreg_vfi(mboxq, vport);
0527     mboxq->vport = vport;
0528     mboxq->mbox_cmpl = lpfc_unregister_vfi_cmpl;
0529 
0530     rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
0531     if (rc == MBX_NOT_FINISHED) {
0532         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
0533                 "2557 UNREG_VFI issue mbox failed rc x%x "
0534                 "HBA state x%x\n",
0535                 rc, phba->pport->port_state);
0536         mempool_free(mboxq, phba->mbox_mem_pool);
0537         return -EIO;
0538     }
0539 
0540     shost = lpfc_shost_from_vport(vport);
0541     spin_lock_irq(shost->host_lock);
0542     vport->fc_flag &= ~FC_VFI_REGISTERED;
0543     spin_unlock_irq(shost->host_lock);
0544     return 0;
0545 }
0546 
0547 /**
0548  * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
0549  * @vport: pointer to a host virtual N_Port data structure.
0550  * @sp: pointer to service parameter data structure.
0551  *
0552  * This routine is called from FLOGI/FDISC completion handler functions.
0553  * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
0554  * node nodename is changed in the completion service parameter else return
0555  * 0. This function also set flag in the vport data structure to delay
0556  * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
0557  * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
0558  * node nodename is changed in the completion service parameter.
0559  *
0560  * Return code
0561  *   0 - FCID and Fabric Nodename and Fabric portname is not changed.
0562  *   1 - FCID or Fabric Nodename or Fabric portname is changed.
0563  *
0564  **/
0565 static uint8_t
0566 lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
0567         struct serv_parm *sp)
0568 {
0569     struct lpfc_hba *phba = vport->phba;
0570     uint8_t fabric_param_changed = 0;
0571     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
0572 
0573     if ((vport->fc_prevDID != vport->fc_myDID) ||
0574         memcmp(&vport->fabric_portname, &sp->portName,
0575             sizeof(struct lpfc_name)) ||
0576         memcmp(&vport->fabric_nodename, &sp->nodeName,
0577             sizeof(struct lpfc_name)) ||
0578         (vport->vport_flag & FAWWPN_PARAM_CHG)) {
0579         fabric_param_changed = 1;
0580         vport->vport_flag &= ~FAWWPN_PARAM_CHG;
0581     }
0582     /*
0583      * Word 1 Bit 31 in common service parameter is overloaded.
0584      * Word 1 Bit 31 in FLOGI request is multiple NPort request
0585      * Word 1 Bit 31 in FLOGI response is clean address bit
0586      *
0587      * If fabric parameter is changed and clean address bit is
0588      * cleared delay nport discovery if
0589      * - vport->fc_prevDID != 0 (not initial discovery) OR
0590      * - lpfc_delay_discovery module parameter is set.
0591      */
0592     if (fabric_param_changed && !sp->cmn.clean_address_bit &&
0593         (vport->fc_prevDID || phba->cfg_delay_discovery)) {
0594         spin_lock_irq(shost->host_lock);
0595         vport->fc_flag |= FC_DISC_DELAYED;
0596         spin_unlock_irq(shost->host_lock);
0597     }
0598 
0599     return fabric_param_changed;
0600 }
0601 
0602 
0603 /**
0604  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
0605  * @vport: pointer to a host virtual N_Port data structure.
0606  * @ndlp: pointer to a node-list data structure.
0607  * @sp: pointer to service parameter data structure.
0608  * @ulp_word4: command response value
0609  *
0610  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
0611  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
0612  * port in a fabric topology. It properly sets up the parameters to the @ndlp
0613  * from the IOCB response. It also check the newly assigned N_Port ID to the
0614  * @vport against the previously assigned N_Port ID. If it is different from
0615  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
0616  * is invoked on all the remaining nodes with the @vport to unregister the
0617  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
0618  * is invoked to register login to the fabric.
0619  *
0620  * Return code
0621  *   0 - Success (currently, always return 0)
0622  **/
0623 static int
0624 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
0625                struct serv_parm *sp, uint32_t ulp_word4)
0626 {
0627     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
0628     struct lpfc_hba  *phba = vport->phba;
0629     struct lpfc_nodelist *np;
0630     struct lpfc_nodelist *next_np;
0631     uint8_t fabric_param_changed;
0632 
0633     spin_lock_irq(shost->host_lock);
0634     vport->fc_flag |= FC_FABRIC;
0635     spin_unlock_irq(shost->host_lock);
0636 
0637     phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
0638     if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
0639         phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
0640 
0641     phba->fc_edtovResol = sp->cmn.edtovResolution;
0642     phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
0643 
0644     if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
0645         spin_lock_irq(shost->host_lock);
0646         vport->fc_flag |= FC_PUBLIC_LOOP;
0647         spin_unlock_irq(shost->host_lock);
0648     }
0649 
0650     vport->fc_myDID = ulp_word4 & Mask_DID;
0651     memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
0652     memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
0653     ndlp->nlp_class_sup = 0;
0654     if (sp->cls1.classValid)
0655         ndlp->nlp_class_sup |= FC_COS_CLASS1;
0656     if (sp->cls2.classValid)
0657         ndlp->nlp_class_sup |= FC_COS_CLASS2;
0658     if (sp->cls3.classValid)
0659         ndlp->nlp_class_sup |= FC_COS_CLASS3;
0660     if (sp->cls4.classValid)
0661         ndlp->nlp_class_sup |= FC_COS_CLASS4;
0662     ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
0663                 sp->cmn.bbRcvSizeLsb;
0664 
0665     fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
0666     if (fabric_param_changed) {
0667         /* Reset FDMI attribute masks based on config parameter */
0668         if (phba->cfg_enable_SmartSAN ||
0669             (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
0670             /* Setup appropriate attribute masks */
0671             vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
0672             if (phba->cfg_enable_SmartSAN)
0673                 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
0674             else
0675                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
0676         } else {
0677             vport->fdmi_hba_mask = 0;
0678             vport->fdmi_port_mask = 0;
0679         }
0680 
0681     }
0682     memcpy(&vport->fabric_portname, &sp->portName,
0683             sizeof(struct lpfc_name));
0684     memcpy(&vport->fabric_nodename, &sp->nodeName,
0685             sizeof(struct lpfc_name));
0686     memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
0687 
0688     if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
0689         if (sp->cmn.response_multiple_NPort) {
0690             lpfc_printf_vlog(vport, KERN_WARNING,
0691                      LOG_ELS | LOG_VPORT,
0692                      "1816 FLOGI NPIV supported, "
0693                      "response data 0x%x\n",
0694                      sp->cmn.response_multiple_NPort);
0695             spin_lock_irq(&phba->hbalock);
0696             phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
0697             spin_unlock_irq(&phba->hbalock);
0698         } else {
0699             /* Because we asked f/w for NPIV it still expects us
0700             to call reg_vnpid at least for the physical host */
0701             lpfc_printf_vlog(vport, KERN_WARNING,
0702                      LOG_ELS | LOG_VPORT,
0703                      "1817 Fabric does not support NPIV "
0704                      "- configuring single port mode.\n");
0705             spin_lock_irq(&phba->hbalock);
0706             phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
0707             spin_unlock_irq(&phba->hbalock);
0708         }
0709     }
0710 
0711     /*
0712      * For FC we need to do some special processing because of the SLI
0713      * Port's default settings of the Common Service Parameters.
0714      */
0715     if ((phba->sli_rev == LPFC_SLI_REV4) &&
0716         (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)) {
0717         /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
0718         if (fabric_param_changed)
0719             lpfc_unregister_fcf_prep(phba);
0720 
0721         /* This should just update the VFI CSPs*/
0722         if (vport->fc_flag & FC_VFI_REGISTERED)
0723             lpfc_issue_reg_vfi(vport);
0724     }
0725 
0726     if (fabric_param_changed &&
0727         !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
0728 
0729         /* If our NportID changed, we need to ensure all
0730          * remaining NPORTs get unreg_login'ed.
0731          */
0732         list_for_each_entry_safe(np, next_np,
0733                     &vport->fc_nodes, nlp_listp) {
0734             if ((np->nlp_state != NLP_STE_NPR_NODE) ||
0735                    !(np->nlp_flag & NLP_NPR_ADISC))
0736                 continue;
0737             spin_lock_irq(&np->lock);
0738             np->nlp_flag &= ~NLP_NPR_ADISC;
0739             spin_unlock_irq(&np->lock);
0740             lpfc_unreg_rpi(vport, np);
0741         }
0742         lpfc_cleanup_pending_mbox(vport);
0743 
0744         if (phba->sli_rev == LPFC_SLI_REV4) {
0745             lpfc_sli4_unreg_all_rpis(vport);
0746             lpfc_mbx_unreg_vpi(vport);
0747             spin_lock_irq(shost->host_lock);
0748             vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
0749             spin_unlock_irq(shost->host_lock);
0750         }
0751 
0752         /*
0753          * For SLI3 and SLI4, the VPI needs to be reregistered in
0754          * response to this fabric parameter change event.
0755          */
0756         spin_lock_irq(shost->host_lock);
0757         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
0758         spin_unlock_irq(shost->host_lock);
0759     } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
0760         !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
0761             /*
0762              * Driver needs to re-reg VPI in order for f/w
0763              * to update the MAC address.
0764              */
0765             lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
0766             lpfc_register_new_vport(phba, vport, ndlp);
0767             return 0;
0768     }
0769 
0770     if (phba->sli_rev < LPFC_SLI_REV4) {
0771         lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
0772         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
0773             vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
0774             lpfc_register_new_vport(phba, vport, ndlp);
0775         else
0776             lpfc_issue_fabric_reglogin(vport);
0777     } else {
0778         ndlp->nlp_type |= NLP_FABRIC;
0779         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
0780         if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
0781             (vport->vpi_state & LPFC_VPI_REGISTERED)) {
0782             lpfc_start_fdiscs(phba);
0783             lpfc_do_scr_ns_plogi(phba, vport);
0784         } else if (vport->fc_flag & FC_VFI_REGISTERED)
0785             lpfc_issue_init_vpi(vport);
0786         else {
0787             lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
0788                     "3135 Need register VFI: (x%x/%x)\n",
0789                     vport->fc_prevDID, vport->fc_myDID);
0790             lpfc_issue_reg_vfi(vport);
0791         }
0792     }
0793     return 0;
0794 }
0795 
0796 /**
0797  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
0798  * @vport: pointer to a host virtual N_Port data structure.
0799  * @ndlp: pointer to a node-list data structure.
0800  * @sp: pointer to service parameter data structure.
0801  *
0802  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
0803  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
0804  * in a point-to-point topology. First, the @vport's N_Port Name is compared
0805  * with the received N_Port Name: if the @vport's N_Port Name is greater than
0806  * the received N_Port Name lexicographically, this node shall assign local
0807  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
0808  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
0809  * this node shall just wait for the remote node to issue PLOGI and assign
0810  * N_Port IDs.
0811  *
0812  * Return code
0813  *   0 - Success
0814  *   -ENXIO - Fail
0815  **/
0816 static int
0817 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
0818               struct serv_parm *sp)
0819 {
0820     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
0821     struct lpfc_hba  *phba = vport->phba;
0822     LPFC_MBOXQ_t *mbox;
0823     int rc;
0824 
0825     spin_lock_irq(shost->host_lock);
0826     vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
0827     vport->fc_flag |= FC_PT2PT;
0828     spin_unlock_irq(shost->host_lock);
0829 
0830     /* If we are pt2pt with another NPort, force NPIV off! */
0831     phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
0832 
0833     /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
0834     if ((phba->sli_rev == LPFC_SLI_REV4) && phba->fc_topology_changed) {
0835         lpfc_unregister_fcf_prep(phba);
0836 
0837         spin_lock_irq(shost->host_lock);
0838         vport->fc_flag &= ~FC_VFI_REGISTERED;
0839         spin_unlock_irq(shost->host_lock);
0840         phba->fc_topology_changed = 0;
0841     }
0842 
0843     rc = memcmp(&vport->fc_portname, &sp->portName,
0844             sizeof(vport->fc_portname));
0845 
0846     if (rc >= 0) {
0847         /* This side will initiate the PLOGI */
0848         spin_lock_irq(shost->host_lock);
0849         vport->fc_flag |= FC_PT2PT_PLOGI;
0850         spin_unlock_irq(shost->host_lock);
0851 
0852         /*
0853          * N_Port ID cannot be 0, set our Id to LocalID
0854          * the other side will be RemoteID.
0855          */
0856 
0857         /* not equal */
0858         if (rc)
0859             vport->fc_myDID = PT2PT_LocalID;
0860 
0861         /* If not registered with a transport, decrement ndlp reference
0862          * count indicating that ndlp can be safely released when other
0863          * references are removed.
0864          */
0865         if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)))
0866             lpfc_nlp_put(ndlp);
0867 
0868         ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
0869         if (!ndlp) {
0870             /*
0871              * Cannot find existing Fabric ndlp, so allocate a
0872              * new one
0873              */
0874             ndlp = lpfc_nlp_init(vport, PT2PT_RemoteID);
0875             if (!ndlp)
0876                 goto fail;
0877         }
0878 
0879         memcpy(&ndlp->nlp_portname, &sp->portName,
0880                sizeof(struct lpfc_name));
0881         memcpy(&ndlp->nlp_nodename, &sp->nodeName,
0882                sizeof(struct lpfc_name));
0883         /* Set state will put ndlp onto node list if not already done */
0884         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
0885         spin_lock_irq(&ndlp->lock);
0886         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
0887         spin_unlock_irq(&ndlp->lock);
0888 
0889         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
0890         if (!mbox)
0891             goto fail;
0892 
0893         lpfc_config_link(phba, mbox);
0894 
0895         mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
0896         mbox->vport = vport;
0897         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
0898         if (rc == MBX_NOT_FINISHED) {
0899             mempool_free(mbox, phba->mbox_mem_pool);
0900             goto fail;
0901         }
0902     } else {
0903         /* This side will wait for the PLOGI. If not registered with
0904          * a transport, decrement node reference count indicating that
0905          * ndlp can be released when other references are removed.
0906          */
0907         if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)))
0908             lpfc_nlp_put(ndlp);
0909 
0910         /* Start discovery - this should just do CLEAR_LA */
0911         lpfc_disc_start(vport);
0912     }
0913 
0914     return 0;
0915 fail:
0916     return -ENXIO;
0917 }
0918 
0919 /**
0920  * lpfc_cmpl_els_flogi - Completion callback function for flogi
0921  * @phba: pointer to lpfc hba data structure.
0922  * @cmdiocb: pointer to lpfc command iocb data structure.
0923  * @rspiocb: pointer to lpfc response iocb data structure.
0924  *
0925  * This routine is the top-level completion callback function for issuing
0926  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
0927  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
0928  * retry has been made (either immediately or delayed with lpfc_els_retry()
0929  * returning 1), the command IOCB will be released and function returned.
0930  * If the retry attempt has been given up (possibly reach the maximum
0931  * number of retries), one additional decrement of ndlp reference shall be
0932  * invoked before going out after releasing the command IOCB. This will
0933  * actually release the remote node (Note, lpfc_els_free_iocb() will also
0934  * invoke one decrement of ndlp reference count). If no error reported in
0935  * the IOCB status, the command Port ID field is used to determine whether
0936  * this is a point-to-point topology or a fabric topology: if the Port ID
0937  * field is assigned, it is a fabric topology; otherwise, it is a
0938  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
0939  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
0940  * specific topology completion conditions.
0941  **/
0942 static void
0943 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
0944             struct lpfc_iocbq *rspiocb)
0945 {
0946     struct lpfc_vport *vport = cmdiocb->vport;
0947     struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
0948     struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
0949     IOCB_t *irsp;
0950     struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf, *prsp;
0951     struct serv_parm *sp;
0952     uint16_t fcf_index;
0953     int rc;
0954     u32 ulp_status, ulp_word4, tmo;
0955 
0956     /* Check to see if link went down during discovery */
0957     if (lpfc_els_chk_latt(vport)) {
0958         /* One additional decrement on node reference count to
0959          * trigger the release of the node
0960          */
0961         if (!(ndlp->fc4_xpt_flags & SCSI_XPT_REGD))
0962             lpfc_nlp_put(ndlp);
0963         goto out;
0964     }
0965 
0966     ulp_status = get_job_ulpstatus(phba, rspiocb);
0967     ulp_word4 = get_job_word4(phba, rspiocb);
0968 
0969     if (phba->sli_rev == LPFC_SLI_REV4) {
0970         tmo = get_wqe_tmo(cmdiocb);
0971     } else {
0972         irsp = &rspiocb->iocb;
0973         tmo = irsp->ulpTimeout;
0974     }
0975 
0976     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
0977         "FLOGI cmpl:      status:x%x/x%x state:x%x",
0978         ulp_status, ulp_word4,
0979         vport->port_state);
0980 
0981     if (ulp_status) {
0982         /*
0983          * In case of FIP mode, perform roundrobin FCF failover
0984          * due to new FCF discovery
0985          */
0986         if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
0987             (phba->fcf.fcf_flag & FCF_DISCOVERY)) {
0988             if (phba->link_state < LPFC_LINK_UP)
0989                 goto stop_rr_fcf_flogi;
0990             if ((phba->fcoe_cvl_eventtag_attn ==
0991                  phba->fcoe_cvl_eventtag) &&
0992                 (ulp_status == IOSTAT_LOCAL_REJECT) &&
0993                 ((ulp_word4 & IOERR_PARAM_MASK) ==
0994                 IOERR_SLI_ABORTED))
0995                 goto stop_rr_fcf_flogi;
0996             else
0997                 phba->fcoe_cvl_eventtag_attn =
0998                     phba->fcoe_cvl_eventtag;
0999             lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1000                     "2611 FLOGI failed on FCF (x%x), "
1001                     "status:x%x/x%x, tmo:x%x, perform "
1002                     "roundrobin FCF failover\n",
1003                     phba->fcf.current_rec.fcf_indx,
1004                     ulp_status, ulp_word4, tmo);
1005             lpfc_sli4_set_fcf_flogi_fail(phba,
1006                     phba->fcf.current_rec.fcf_indx);
1007             fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
1008             rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
1009             if (rc)
1010                 goto out;
1011         }
1012 
1013 stop_rr_fcf_flogi:
1014         /* FLOGI failure */
1015         if (!(ulp_status == IOSTAT_LOCAL_REJECT &&
1016               ((ulp_word4 & IOERR_PARAM_MASK) ==
1017                     IOERR_LOOP_OPEN_FAILURE)))
1018             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1019                      "2858 FLOGI failure Status:x%x/x%x TMO"
1020                      ":x%x Data x%x x%x\n",
1021                      ulp_status, ulp_word4, tmo,
1022                      phba->hba_flag, phba->fcf.fcf_flag);
1023 
1024         /* Check for retry */
1025         if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1026             goto out;
1027 
1028         lpfc_printf_vlog(vport, KERN_WARNING, LOG_TRACE_EVENT,
1029                  "0150 FLOGI failure Status:x%x/x%x "
1030                  "xri x%x TMO:x%x refcnt %d\n",
1031                  ulp_status, ulp_word4, cmdiocb->sli4_xritag,
1032                  tmo, kref_read(&ndlp->kref));
1033 
1034         /* If this is not a loop open failure, bail out */
1035         if (!(ulp_status == IOSTAT_LOCAL_REJECT &&
1036               ((ulp_word4 & IOERR_PARAM_MASK) ==
1037                     IOERR_LOOP_OPEN_FAILURE))) {
1038             /* FLOGI failure */
1039             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1040                      "0100 FLOGI failure Status:x%x/x%x "
1041                      "TMO:x%x\n",
1042                      ulp_status, ulp_word4, tmo);
1043             goto flogifail;
1044         }
1045 
1046         /* FLOGI failed, so there is no fabric */
1047         spin_lock_irq(shost->host_lock);
1048         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP |
1049                     FC_PT2PT_NO_NVME);
1050         spin_unlock_irq(shost->host_lock);
1051 
1052         /* If private loop, then allow max outstanding els to be
1053          * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
1054          * alpa map would take too long otherwise.
1055          */
1056         if (phba->alpa_map[0] == 0)
1057             vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
1058         if ((phba->sli_rev == LPFC_SLI_REV4) &&
1059             (!(vport->fc_flag & FC_VFI_REGISTERED) ||
1060              (vport->fc_prevDID != vport->fc_myDID) ||
1061             phba->fc_topology_changed)) {
1062             if (vport->fc_flag & FC_VFI_REGISTERED) {
1063                 if (phba->fc_topology_changed) {
1064                     lpfc_unregister_fcf_prep(phba);
1065                     spin_lock_irq(shost->host_lock);
1066                     vport->fc_flag &= ~FC_VFI_REGISTERED;
1067                     spin_unlock_irq(shost->host_lock);
1068                     phba->fc_topology_changed = 0;
1069                 } else {
1070                     lpfc_sli4_unreg_all_rpis(vport);
1071                 }
1072             }
1073 
1074             /* Do not register VFI if the driver aborted FLOGI */
1075             if (!lpfc_error_lost_link(ulp_status, ulp_word4))
1076                 lpfc_issue_reg_vfi(vport);
1077 
1078             lpfc_nlp_put(ndlp);
1079             goto out;
1080         }
1081         goto flogifail;
1082     }
1083     spin_lock_irq(shost->host_lock);
1084     vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
1085     vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
1086     spin_unlock_irq(shost->host_lock);
1087 
1088     /*
1089      * The FLogI succeeded.  Sync the data for the CPU before
1090      * accessing it.
1091      */
1092     prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1093     if (!prsp)
1094         goto out;
1095     sp = prsp->virt + sizeof(uint32_t);
1096 
1097     /* FLOGI completes successfully */
1098     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1099              "0101 FLOGI completes successfully, I/O tag:x%x "
1100              "xri x%x Data: x%x x%x x%x x%x x%x x%x x%x %d\n",
1101              cmdiocb->iotag, cmdiocb->sli4_xritag,
1102              ulp_word4, sp->cmn.e_d_tov,
1103              sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
1104              vport->port_state, vport->fc_flag,
1105              sp->cmn.priority_tagging, kref_read(&ndlp->kref));
1106 
1107     if (sp->cmn.priority_tagging)
1108         vport->phba->pport->vmid_flag |= (LPFC_VMID_ISSUE_QFPA |
1109                           LPFC_VMID_TYPE_PRIO);
1110 
1111     if (vport->port_state == LPFC_FLOGI) {
1112         /*
1113          * If Common Service Parameters indicate Nport
1114          * we are point to point, if Fport we are Fabric.
1115          */
1116         if (sp->cmn.fPort)
1117             rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp,
1118                             ulp_word4);
1119         else if (!(phba->hba_flag & HBA_FCOE_MODE))
1120             rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
1121         else {
1122             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1123                 "2831 FLOGI response with cleared Fabric "
1124                 "bit fcf_index 0x%x "
1125                 "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
1126                 "Fabric Name "
1127                 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
1128                 phba->fcf.current_rec.fcf_indx,
1129                 phba->fcf.current_rec.switch_name[0],
1130                 phba->fcf.current_rec.switch_name[1],
1131                 phba->fcf.current_rec.switch_name[2],
1132                 phba->fcf.current_rec.switch_name[3],
1133                 phba->fcf.current_rec.switch_name[4],
1134                 phba->fcf.current_rec.switch_name[5],
1135                 phba->fcf.current_rec.switch_name[6],
1136                 phba->fcf.current_rec.switch_name[7],
1137                 phba->fcf.current_rec.fabric_name[0],
1138                 phba->fcf.current_rec.fabric_name[1],
1139                 phba->fcf.current_rec.fabric_name[2],
1140                 phba->fcf.current_rec.fabric_name[3],
1141                 phba->fcf.current_rec.fabric_name[4],
1142                 phba->fcf.current_rec.fabric_name[5],
1143                 phba->fcf.current_rec.fabric_name[6],
1144                 phba->fcf.current_rec.fabric_name[7]);
1145 
1146             lpfc_nlp_put(ndlp);
1147             spin_lock_irq(&phba->hbalock);
1148             phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1149             phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1150             spin_unlock_irq(&phba->hbalock);
1151             phba->fcf.fcf_redisc_attempted = 0; /* reset */
1152             goto out;
1153         }
1154         if (!rc) {
1155             /* Mark the FCF discovery process done */
1156             if (phba->hba_flag & HBA_FIP_SUPPORT)
1157                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
1158                         LOG_ELS,
1159                         "2769 FLOGI to FCF (x%x) "
1160                         "completed successfully\n",
1161                         phba->fcf.current_rec.fcf_indx);
1162             spin_lock_irq(&phba->hbalock);
1163             phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1164             phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1165             spin_unlock_irq(&phba->hbalock);
1166             phba->fcf.fcf_redisc_attempted = 0; /* reset */
1167             goto out;
1168         }
1169     } else if (vport->port_state > LPFC_FLOGI &&
1170            vport->fc_flag & FC_PT2PT) {
1171         /*
1172          * In a p2p topology, it is possible that discovery has
1173          * already progressed, and this completion can be ignored.
1174          * Recheck the indicated topology.
1175          */
1176         if (!sp->cmn.fPort)
1177             goto out;
1178     }
1179 
1180 flogifail:
1181     spin_lock_irq(&phba->hbalock);
1182     phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1183     spin_unlock_irq(&phba->hbalock);
1184 
1185     if (!lpfc_error_lost_link(ulp_status, ulp_word4)) {
1186         /* FLOGI failed, so just use loop map to make discovery list */
1187         lpfc_disc_list_loopmap(vport);
1188 
1189         /* Start discovery */
1190         lpfc_disc_start(vport);
1191     } else if (((ulp_status != IOSTAT_LOCAL_REJECT) ||
1192             (((ulp_word4 & IOERR_PARAM_MASK) !=
1193              IOERR_SLI_ABORTED) &&
1194             ((ulp_word4 & IOERR_PARAM_MASK) !=
1195              IOERR_SLI_DOWN))) &&
1196             (phba->link_state != LPFC_CLEAR_LA)) {
1197         /* If FLOGI failed enable link interrupt. */
1198         lpfc_issue_clear_la(phba, vport);
1199     }
1200 out:
1201     phba->hba_flag &= ~HBA_FLOGI_OUTSTANDING;
1202     lpfc_els_free_iocb(phba, cmdiocb);
1203     lpfc_nlp_put(ndlp);
1204 }
1205 
1206 /**
1207  * lpfc_cmpl_els_link_down - Completion callback function for ELS command
1208  *                           aborted during a link down
1209  * @phba: pointer to lpfc hba data structure.
1210  * @cmdiocb: pointer to lpfc command iocb data structure.
1211  * @rspiocb: pointer to lpfc response iocb data structure.
1212  *
1213  */
1214 static void
1215 lpfc_cmpl_els_link_down(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1216             struct lpfc_iocbq *rspiocb)
1217 {
1218     uint32_t *pcmd;
1219     uint32_t cmd;
1220     u32 ulp_status, ulp_word4;
1221 
1222     pcmd = (uint32_t *)cmdiocb->cmd_dmabuf->virt;
1223     cmd = *pcmd;
1224 
1225     ulp_status = get_job_ulpstatus(phba, rspiocb);
1226     ulp_word4 = get_job_word4(phba, rspiocb);
1227 
1228     lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1229             "6445 ELS completes after LINK_DOWN: "
1230             " Status %x/%x cmd x%x flg x%x\n",
1231             ulp_status, ulp_word4, cmd,
1232             cmdiocb->cmd_flag);
1233 
1234     if (cmdiocb->cmd_flag & LPFC_IO_FABRIC) {
1235         cmdiocb->cmd_flag &= ~LPFC_IO_FABRIC;
1236         atomic_dec(&phba->fabric_iocb_count);
1237     }
1238     lpfc_els_free_iocb(phba, cmdiocb);
1239 }
1240 
1241 /**
1242  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
1243  * @vport: pointer to a host virtual N_Port data structure.
1244  * @ndlp: pointer to a node-list data structure.
1245  * @retry: number of retries to the command IOCB.
1246  *
1247  * This routine issues a Fabric Login (FLOGI) Request ELS command
1248  * for a @vport. The initiator service parameters are put into the payload
1249  * of the FLOGI Request IOCB and the top-level callback function pointer
1250  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1251  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1252  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1253  *
1254  * Note that the ndlp reference count will be incremented by 1 for holding the
1255  * ndlp and the reference to ndlp will be stored into the ndlp field of
1256  * the IOCB for the completion callback function to the FLOGI ELS command.
1257  *
1258  * Return code
1259  *   0 - successfully issued flogi iocb for @vport
1260  *   1 - failed to issue flogi iocb for @vport
1261  **/
1262 static int
1263 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1264              uint8_t retry)
1265 {
1266     struct lpfc_hba  *phba = vport->phba;
1267     struct serv_parm *sp;
1268     union lpfc_wqe128 *wqe = NULL;
1269     IOCB_t *icmd = NULL;
1270     struct lpfc_iocbq *elsiocb;
1271     struct lpfc_iocbq defer_flogi_acc;
1272     u8 *pcmd, ct;
1273     uint16_t cmdsize;
1274     uint32_t tmo, did;
1275     int rc;
1276 
1277     cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1278     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1279                      ndlp->nlp_DID, ELS_CMD_FLOGI);
1280 
1281     if (!elsiocb)
1282         return 1;
1283 
1284     wqe = &elsiocb->wqe;
1285     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
1286     icmd = &elsiocb->iocb;
1287 
1288     /* For FLOGI request, remainder of payload is service parameters */
1289     *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1290     pcmd += sizeof(uint32_t);
1291     memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1292     sp = (struct serv_parm *) pcmd;
1293 
1294     /* Setup CSPs accordingly for Fabric */
1295     sp->cmn.e_d_tov = 0;
1296     sp->cmn.w2.r_a_tov = 0;
1297     sp->cmn.virtual_fabric_support = 0;
1298     sp->cls1.classValid = 0;
1299     if (sp->cmn.fcphLow < FC_PH3)
1300         sp->cmn.fcphLow = FC_PH3;
1301     if (sp->cmn.fcphHigh < FC_PH3)
1302         sp->cmn.fcphHigh = FC_PH3;
1303 
1304     /* Determine if switch supports priority tagging */
1305     if (phba->cfg_vmid_priority_tagging) {
1306         sp->cmn.priority_tagging = 1;
1307         /* lpfc_vmid_host_uuid is combination of wwpn and wwnn */
1308         if (uuid_is_null((uuid_t *)vport->lpfc_vmid_host_uuid)) {
1309             memcpy(vport->lpfc_vmid_host_uuid, phba->wwpn,
1310                    sizeof(phba->wwpn));
1311             memcpy(&vport->lpfc_vmid_host_uuid[8], phba->wwnn,
1312                    sizeof(phba->wwnn));
1313         }
1314     }
1315 
1316     if  (phba->sli_rev == LPFC_SLI_REV4) {
1317         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1318             LPFC_SLI_INTF_IF_TYPE_0) {
1319             /* FLOGI needs to be 3 for WQE FCFI */
1320             ct = SLI4_CT_FCFI;
1321             bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
1322 
1323             /* Set the fcfi to the fcfi we registered with */
1324             bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
1325                    phba->fcf.fcfi);
1326         }
1327 
1328         /* Can't do SLI4 class2 without support sequence coalescing */
1329         sp->cls2.classValid = 0;
1330         sp->cls2.seqDelivery = 0;
1331     } else {
1332         /* Historical, setting sequential-delivery bit for SLI3 */
1333         sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
1334         sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
1335         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1336             sp->cmn.request_multiple_Nport = 1;
1337             /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1338             icmd->ulpCt_h = 1;
1339             icmd->ulpCt_l = 0;
1340         } else {
1341             sp->cmn.request_multiple_Nport = 0;
1342         }
1343 
1344         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
1345             icmd->un.elsreq64.myID = 0;
1346             icmd->un.elsreq64.fl = 1;
1347         }
1348     }
1349 
1350     tmo = phba->fc_ratov;
1351     phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1352     lpfc_set_disctmo(vport);
1353     phba->fc_ratov = tmo;
1354 
1355     phba->fc_stat.elsXmitFLOGI++;
1356     elsiocb->cmd_cmpl = lpfc_cmpl_els_flogi;
1357 
1358     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1359         "Issue FLOGI:     opt:x%x",
1360         phba->sli3_options, 0, 0);
1361 
1362     elsiocb->ndlp = lpfc_nlp_get(ndlp);
1363     if (!elsiocb->ndlp) {
1364         lpfc_els_free_iocb(phba, elsiocb);
1365         return 1;
1366     }
1367 
1368     rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1369     if (rc == IOCB_ERROR) {
1370         lpfc_els_free_iocb(phba, elsiocb);
1371         lpfc_nlp_put(ndlp);
1372         return 1;
1373     }
1374 
1375     phba->hba_flag |= (HBA_FLOGI_ISSUED | HBA_FLOGI_OUTSTANDING);
1376 
1377     /* Clear external loopback plug detected flag */
1378     phba->link_flag &= ~LS_EXTERNAL_LOOPBACK;
1379 
1380     /* Check for a deferred FLOGI ACC condition */
1381     if (phba->defer_flogi_acc_flag) {
1382         /* lookup ndlp for received FLOGI */
1383         ndlp = lpfc_findnode_did(vport, 0);
1384         if (!ndlp)
1385             return 0;
1386 
1387         did = vport->fc_myDID;
1388         vport->fc_myDID = Fabric_DID;
1389 
1390         memset(&defer_flogi_acc, 0, sizeof(struct lpfc_iocbq));
1391 
1392         if (phba->sli_rev == LPFC_SLI_REV4) {
1393             bf_set(wqe_ctxt_tag,
1394                    &defer_flogi_acc.wqe.xmit_els_rsp.wqe_com,
1395                    phba->defer_flogi_acc_rx_id);
1396             bf_set(wqe_rcvoxid,
1397                    &defer_flogi_acc.wqe.xmit_els_rsp.wqe_com,
1398                    phba->defer_flogi_acc_ox_id);
1399         } else {
1400             icmd = &defer_flogi_acc.iocb;
1401             icmd->ulpContext = phba->defer_flogi_acc_rx_id;
1402             icmd->unsli3.rcvsli3.ox_id =
1403                 phba->defer_flogi_acc_ox_id;
1404         }
1405 
1406         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1407                  "3354 Xmit deferred FLOGI ACC: rx_id: x%x,"
1408                  " ox_id: x%x, hba_flag x%x\n",
1409                  phba->defer_flogi_acc_rx_id,
1410                  phba->defer_flogi_acc_ox_id, phba->hba_flag);
1411 
1412         /* Send deferred FLOGI ACC */
1413         lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, &defer_flogi_acc,
1414                  ndlp, NULL);
1415 
1416         phba->defer_flogi_acc_flag = false;
1417         vport->fc_myDID = did;
1418 
1419         /* Decrement ndlp reference count to indicate the node can be
1420          * released when other references are removed.
1421          */
1422         lpfc_nlp_put(ndlp);
1423     }
1424 
1425     return 0;
1426 }
1427 
1428 /**
1429  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1430  * @phba: pointer to lpfc hba data structure.
1431  *
1432  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1433  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1434  * list and issues an abort IOCB commond on each outstanding IOCB that
1435  * contains a active Fabric_DID ndlp. Note that this function is to issue
1436  * the abort IOCB command on all the outstanding IOCBs, thus when this
1437  * function returns, it does not guarantee all the IOCBs are actually aborted.
1438  *
1439  * Return code
1440  *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1441  **/
1442 int
1443 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1444 {
1445     struct lpfc_sli_ring *pring;
1446     struct lpfc_iocbq *iocb, *next_iocb;
1447     struct lpfc_nodelist *ndlp;
1448     u32 ulp_command;
1449 
1450     /* Abort outstanding I/O on NPort <nlp_DID> */
1451     lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1452             "0201 Abort outstanding I/O on NPort x%x\n",
1453             Fabric_DID);
1454 
1455     pring = lpfc_phba_elsring(phba);
1456     if (unlikely(!pring))
1457         return -EIO;
1458 
1459     /*
1460      * Check the txcmplq for an iocb that matches the nport the driver is
1461      * searching for.
1462      */
1463     spin_lock_irq(&phba->hbalock);
1464     list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1465         ulp_command = get_job_cmnd(phba, iocb);
1466         if (ulp_command == CMD_ELS_REQUEST64_CR) {
1467             ndlp = iocb->ndlp;
1468             if (ndlp && ndlp->nlp_DID == Fabric_DID) {
1469                 if ((phba->pport->fc_flag & FC_PT2PT) &&
1470                     !(phba->pport->fc_flag & FC_PT2PT_PLOGI))
1471                     iocb->fabric_cmd_cmpl =
1472                         lpfc_ignore_els_cmpl;
1473                 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
1474                                NULL);
1475             }
1476         }
1477     }
1478     /* Make sure HBA is alive */
1479     lpfc_issue_hb_tmo(phba);
1480 
1481     spin_unlock_irq(&phba->hbalock);
1482 
1483     return 0;
1484 }
1485 
1486 /**
1487  * lpfc_initial_flogi - Issue an initial fabric login for a vport
1488  * @vport: pointer to a host virtual N_Port data structure.
1489  *
1490  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1491  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1492  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1493  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1494  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1495  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1496  * @vport.
1497  *
1498  * Return code
1499  *   0 - failed to issue initial flogi for @vport
1500  *   1 - successfully issued initial flogi for @vport
1501  **/
1502 int
1503 lpfc_initial_flogi(struct lpfc_vport *vport)
1504 {
1505     struct lpfc_nodelist *ndlp;
1506 
1507     vport->port_state = LPFC_FLOGI;
1508     lpfc_set_disctmo(vport);
1509 
1510     /* First look for the Fabric ndlp */
1511     ndlp = lpfc_findnode_did(vport, Fabric_DID);
1512     if (!ndlp) {
1513         /* Cannot find existing Fabric ndlp, so allocate a new one */
1514         ndlp = lpfc_nlp_init(vport, Fabric_DID);
1515         if (!ndlp)
1516             return 0;
1517         /* Set the node type */
1518         ndlp->nlp_type |= NLP_FABRIC;
1519 
1520         /* Put ndlp onto node list */
1521         lpfc_enqueue_node(vport, ndlp);
1522     }
1523 
1524     /* Reset the Fabric flag, topology change may have happened */
1525     vport->fc_flag &= ~FC_FABRIC;
1526     if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1527         /* A node reference should be retained while registered with a
1528          * transport or dev-loss-evt work is pending.
1529          * Otherwise, decrement node reference to trigger release.
1530          */
1531         if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
1532             !(ndlp->nlp_flag & NLP_IN_DEV_LOSS))
1533             lpfc_nlp_put(ndlp);
1534         return 0;
1535     }
1536     return 1;
1537 }
1538 
1539 /**
1540  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1541  * @vport: pointer to a host virtual N_Port data structure.
1542  *
1543  * This routine issues an initial Fabric Discover (FDISC) for the @vport
1544  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1545  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1546  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1547  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1548  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1549  * @vport.
1550  *
1551  * Return code
1552  *   0 - failed to issue initial fdisc for @vport
1553  *   1 - successfully issued initial fdisc for @vport
1554  **/
1555 int
1556 lpfc_initial_fdisc(struct lpfc_vport *vport)
1557 {
1558     struct lpfc_nodelist *ndlp;
1559 
1560     /* First look for the Fabric ndlp */
1561     ndlp = lpfc_findnode_did(vport, Fabric_DID);
1562     if (!ndlp) {
1563         /* Cannot find existing Fabric ndlp, so allocate a new one */
1564         ndlp = lpfc_nlp_init(vport, Fabric_DID);
1565         if (!ndlp)
1566             return 0;
1567 
1568         /* NPIV is only supported in Fabrics. */
1569         ndlp->nlp_type |= NLP_FABRIC;
1570 
1571         /* Put ndlp onto node list */
1572         lpfc_enqueue_node(vport, ndlp);
1573     }
1574 
1575     if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1576         /* A node reference should be retained while registered with a
1577          * transport or dev-loss-evt work is pending.
1578          * Otherwise, decrement node reference to trigger release.
1579          */
1580         if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
1581             !(ndlp->nlp_flag & NLP_IN_DEV_LOSS))
1582             lpfc_nlp_put(ndlp);
1583         return 0;
1584     }
1585     return 1;
1586 }
1587 
1588 /**
1589  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1590  * @vport: pointer to a host virtual N_Port data structure.
1591  *
1592  * This routine checks whether there are more remaining Port Logins
1593  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1594  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1595  * to issue ELS PLOGIs up to the configured discover threads with the
1596  * @vport (@vport->cfg_discovery_threads). The function also decrement
1597  * the @vport's num_disc_node by 1 if it is not already 0.
1598  **/
1599 void
1600 lpfc_more_plogi(struct lpfc_vport *vport)
1601 {
1602     if (vport->num_disc_nodes)
1603         vport->num_disc_nodes--;
1604 
1605     /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1606     lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1607              "0232 Continue discovery with %d PLOGIs to go "
1608              "Data: x%x x%x x%x\n",
1609              vport->num_disc_nodes, vport->fc_plogi_cnt,
1610              vport->fc_flag, vport->port_state);
1611     /* Check to see if there are more PLOGIs to be sent */
1612     if (vport->fc_flag & FC_NLP_MORE)
1613         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1614         lpfc_els_disc_plogi(vport);
1615 
1616     return;
1617 }
1618 
1619 /**
1620  * lpfc_plogi_confirm_nport - Confirm plogi wwpn matches stored ndlp
1621  * @phba: pointer to lpfc hba data structure.
1622  * @prsp: pointer to response IOCB payload.
1623  * @ndlp: pointer to a node-list data structure.
1624  *
1625  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1626  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1627  * The following cases are considered N_Port confirmed:
1628  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1629  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1630  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1631  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1632  * 1) if there is a node on vport list other than the @ndlp with the same
1633  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1634  * on that node to release the RPI associated with the node; 2) if there is
1635  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1636  * into, a new node shall be allocated (or activated). In either case, the
1637  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1638  * be released and the new_ndlp shall be put on to the vport node list and
1639  * its pointer returned as the confirmed node.
1640  *
1641  * Note that before the @ndlp got "released", the keepDID from not-matching
1642  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1643  * of the @ndlp. This is because the release of @ndlp is actually to put it
1644  * into an inactive state on the vport node list and the vport node list
1645  * management algorithm does not allow two node with a same DID.
1646  *
1647  * Return code
1648  *   pointer to the PLOGI N_Port @ndlp
1649  **/
1650 static struct lpfc_nodelist *
1651 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1652              struct lpfc_nodelist *ndlp)
1653 {
1654     struct lpfc_vport *vport = ndlp->vport;
1655     struct lpfc_nodelist *new_ndlp;
1656     struct serv_parm *sp;
1657     uint8_t  name[sizeof(struct lpfc_name)];
1658     uint32_t keepDID = 0, keep_nlp_flag = 0;
1659     uint32_t keep_new_nlp_flag = 0;
1660     uint16_t keep_nlp_state;
1661     u32 keep_nlp_fc4_type = 0;
1662     struct lpfc_nvme_rport *keep_nrport = NULL;
1663     unsigned long *active_rrqs_xri_bitmap = NULL;
1664 
1665     /* Fabric nodes can have the same WWPN so we don't bother searching
1666      * by WWPN.  Just return the ndlp that was given to us.
1667      */
1668     if (ndlp->nlp_type & NLP_FABRIC)
1669         return ndlp;
1670 
1671     sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1672     memset(name, 0, sizeof(struct lpfc_name));
1673 
1674     /* Now we find out if the NPort we are logging into, matches the WWPN
1675      * we have for that ndlp. If not, we have some work to do.
1676      */
1677     new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1678 
1679     /* return immediately if the WWPN matches ndlp */
1680     if (!new_ndlp || (new_ndlp == ndlp))
1681         return ndlp;
1682 
1683     /*
1684      * Unregister from backend if not done yet. Could have been skipped
1685      * due to ADISC
1686      */
1687     lpfc_nlp_unreg_node(vport, new_ndlp);
1688 
1689     if (phba->sli_rev == LPFC_SLI_REV4) {
1690         active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
1691                                GFP_KERNEL);
1692         if (active_rrqs_xri_bitmap)
1693             memset(active_rrqs_xri_bitmap, 0,
1694                    phba->cfg_rrq_xri_bitmap_sz);
1695     }
1696 
1697     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1698              "3178 PLOGI confirm: ndlp x%x x%x x%x: "
1699              "new_ndlp x%x x%x x%x\n",
1700              ndlp->nlp_DID, ndlp->nlp_flag,  ndlp->nlp_fc4_type,
1701              (new_ndlp ? new_ndlp->nlp_DID : 0),
1702              (new_ndlp ? new_ndlp->nlp_flag : 0),
1703              (new_ndlp ? new_ndlp->nlp_fc4_type : 0));
1704 
1705     keepDID = new_ndlp->nlp_DID;
1706 
1707     if (phba->sli_rev == LPFC_SLI_REV4 && active_rrqs_xri_bitmap)
1708         memcpy(active_rrqs_xri_bitmap, new_ndlp->active_rrqs_xri_bitmap,
1709                phba->cfg_rrq_xri_bitmap_sz);
1710 
1711     /* At this point in this routine, we know new_ndlp will be
1712      * returned. however, any previous GID_FTs that were done
1713      * would have updated nlp_fc4_type in ndlp, so we must ensure
1714      * new_ndlp has the right value.
1715      */
1716     if (vport->fc_flag & FC_FABRIC) {
1717         keep_nlp_fc4_type = new_ndlp->nlp_fc4_type;
1718         new_ndlp->nlp_fc4_type = ndlp->nlp_fc4_type;
1719     }
1720 
1721     lpfc_unreg_rpi(vport, new_ndlp);
1722     new_ndlp->nlp_DID = ndlp->nlp_DID;
1723     new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1724     if (phba->sli_rev == LPFC_SLI_REV4)
1725         memcpy(new_ndlp->active_rrqs_xri_bitmap,
1726                ndlp->active_rrqs_xri_bitmap,
1727                phba->cfg_rrq_xri_bitmap_sz);
1728 
1729     /* Lock both ndlps */
1730     spin_lock_irq(&ndlp->lock);
1731     spin_lock_irq(&new_ndlp->lock);
1732     keep_new_nlp_flag = new_ndlp->nlp_flag;
1733     keep_nlp_flag = ndlp->nlp_flag;
1734     new_ndlp->nlp_flag = ndlp->nlp_flag;
1735 
1736     /* if new_ndlp had NLP_UNREG_INP set, keep it */
1737     if (keep_new_nlp_flag & NLP_UNREG_INP)
1738         new_ndlp->nlp_flag |= NLP_UNREG_INP;
1739     else
1740         new_ndlp->nlp_flag &= ~NLP_UNREG_INP;
1741 
1742     /* if new_ndlp had NLP_RPI_REGISTERED set, keep it */
1743     if (keep_new_nlp_flag & NLP_RPI_REGISTERED)
1744         new_ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1745     else
1746         new_ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1747 
1748     /*
1749      * Retain the DROPPED flag. This will take care of the init
1750      * refcount when affecting the state change
1751      */
1752     if (keep_new_nlp_flag & NLP_DROPPED)
1753         new_ndlp->nlp_flag |= NLP_DROPPED;
1754     else
1755         new_ndlp->nlp_flag &= ~NLP_DROPPED;
1756 
1757     ndlp->nlp_flag = keep_new_nlp_flag;
1758 
1759     /* if ndlp had NLP_UNREG_INP set, keep it */
1760     if (keep_nlp_flag & NLP_UNREG_INP)
1761         ndlp->nlp_flag |= NLP_UNREG_INP;
1762     else
1763         ndlp->nlp_flag &= ~NLP_UNREG_INP;
1764 
1765     /* if ndlp had NLP_RPI_REGISTERED set, keep it */
1766     if (keep_nlp_flag & NLP_RPI_REGISTERED)
1767         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1768     else
1769         ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1770 
1771     /*
1772      * Retain the DROPPED flag. This will take care of the init
1773      * refcount when affecting the state change
1774      */
1775     if (keep_nlp_flag & NLP_DROPPED)
1776         ndlp->nlp_flag |= NLP_DROPPED;
1777     else
1778         ndlp->nlp_flag &= ~NLP_DROPPED;
1779 
1780     spin_unlock_irq(&new_ndlp->lock);
1781     spin_unlock_irq(&ndlp->lock);
1782 
1783     /* Set nlp_states accordingly */
1784     keep_nlp_state = new_ndlp->nlp_state;
1785     lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1786 
1787     /* interchange the nvme remoteport structs */
1788     keep_nrport = new_ndlp->nrport;
1789     new_ndlp->nrport = ndlp->nrport;
1790 
1791     /* Move this back to NPR state */
1792     if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1793         /* The ndlp doesn't have a portname yet, but does have an
1794          * NPort ID.  The new_ndlp portname matches the Rport's
1795          * portname.  Reinstantiate the new_ndlp and reset the ndlp.
1796          */
1797         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1798              "3179 PLOGI confirm NEW: %x %x\n",
1799              new_ndlp->nlp_DID, keepDID);
1800 
1801         /* Two ndlps cannot have the same did on the nodelist.
1802          * The KeepDID and keep_nlp_fc4_type need to be swapped
1803          * because ndlp is inflight with no WWPN.
1804          */
1805         ndlp->nlp_DID = keepDID;
1806         ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1807         lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1808         if (phba->sli_rev == LPFC_SLI_REV4 &&
1809             active_rrqs_xri_bitmap)
1810             memcpy(ndlp->active_rrqs_xri_bitmap,
1811                    active_rrqs_xri_bitmap,
1812                    phba->cfg_rrq_xri_bitmap_sz);
1813 
1814     } else {
1815         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1816              "3180 PLOGI confirm SWAP: %x %x\n",
1817              new_ndlp->nlp_DID, keepDID);
1818 
1819         lpfc_unreg_rpi(vport, ndlp);
1820 
1821         /* The ndlp and new_ndlp both have WWPNs but are swapping
1822          * NPort Ids and attributes.
1823          */
1824         ndlp->nlp_DID = keepDID;
1825         ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1826 
1827         if (phba->sli_rev == LPFC_SLI_REV4 &&
1828             active_rrqs_xri_bitmap)
1829             memcpy(ndlp->active_rrqs_xri_bitmap,
1830                    active_rrqs_xri_bitmap,
1831                    phba->cfg_rrq_xri_bitmap_sz);
1832 
1833         /* Since we are switching over to the new_ndlp,
1834          * reset the old ndlp state
1835          */
1836         if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
1837             (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
1838             keep_nlp_state = NLP_STE_NPR_NODE;
1839         lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1840         ndlp->nrport = keep_nrport;
1841     }
1842 
1843     /*
1844      * If ndlp is not associated with any rport we can drop it here else
1845      * let dev_loss_tmo_callbk trigger DEVICE_RM event
1846      */
1847     if (!ndlp->rport && (ndlp->nlp_state == NLP_STE_NPR_NODE))
1848         lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
1849 
1850     if (phba->sli_rev == LPFC_SLI_REV4 &&
1851         active_rrqs_xri_bitmap)
1852         mempool_free(active_rrqs_xri_bitmap,
1853                  phba->active_rrq_pool);
1854 
1855     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1856              "3173 PLOGI confirm exit: new_ndlp x%x x%x x%x\n",
1857              new_ndlp->nlp_DID, new_ndlp->nlp_flag,
1858              new_ndlp->nlp_fc4_type);
1859 
1860     return new_ndlp;
1861 }
1862 
1863 /**
1864  * lpfc_end_rscn - Check and handle more rscn for a vport
1865  * @vport: pointer to a host virtual N_Port data structure.
1866  *
1867  * This routine checks whether more Registration State Change
1868  * Notifications (RSCNs) came in while the discovery state machine was in
1869  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1870  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1871  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1872  * handling the RSCNs.
1873  **/
1874 void
1875 lpfc_end_rscn(struct lpfc_vport *vport)
1876 {
1877     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1878 
1879     if (vport->fc_flag & FC_RSCN_MODE) {
1880         /*
1881          * Check to see if more RSCNs came in while we were
1882          * processing this one.
1883          */
1884         if (vport->fc_rscn_id_cnt ||
1885             (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1886             lpfc_els_handle_rscn(vport);
1887         else {
1888             spin_lock_irq(shost->host_lock);
1889             vport->fc_flag &= ~FC_RSCN_MODE;
1890             spin_unlock_irq(shost->host_lock);
1891         }
1892     }
1893 }
1894 
1895 /**
1896  * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1897  * @phba: pointer to lpfc hba data structure.
1898  * @cmdiocb: pointer to lpfc command iocb data structure.
1899  * @rspiocb: pointer to lpfc response iocb data structure.
1900  *
1901  * This routine will call the clear rrq function to free the rrq and
1902  * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1903  * exist then the clear_rrq is still called because the rrq needs to
1904  * be freed.
1905  **/
1906 
1907 static void
1908 lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1909           struct lpfc_iocbq *rspiocb)
1910 {
1911     struct lpfc_vport *vport = cmdiocb->vport;
1912     struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
1913     struct lpfc_node_rrq *rrq;
1914     u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
1915     u32 ulp_word4 = get_job_word4(phba, rspiocb);
1916 
1917     /* we pass cmdiocb to state machine which needs rspiocb as well */
1918     rrq = cmdiocb->context_un.rrq;
1919     cmdiocb->rsp_iocb = rspiocb;
1920 
1921     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1922         "RRQ cmpl:      status:x%x/x%x did:x%x",
1923         ulp_status, ulp_word4,
1924         get_job_els_rsp64_did(phba, cmdiocb));
1925 
1926 
1927     /* rrq completes to NPort <nlp_DID> */
1928     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1929              "2880 RRQ completes to DID x%x "
1930              "Data: x%x x%x x%x x%x x%x\n",
1931              ndlp->nlp_DID, ulp_status, ulp_word4,
1932              get_wqe_tmo(cmdiocb), rrq->xritag, rrq->rxid);
1933 
1934     if (ulp_status) {
1935         /* Check for retry */
1936         /* RRQ failed Don't print the vport to vport rjts */
1937         if (ulp_status != IOSTAT_LS_RJT ||
1938             (((ulp_word4) >> 16 != LSRJT_INVALID_CMD) &&
1939              ((ulp_word4) >> 16 != LSRJT_UNABLE_TPC)) ||
1940             (phba)->pport->cfg_log_verbose & LOG_ELS)
1941             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1942                      "2881 RRQ failure DID:%06X Status:"
1943                      "x%x/x%x\n",
1944                      ndlp->nlp_DID, ulp_status,
1945                      ulp_word4);
1946     }
1947 
1948     lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1949     lpfc_els_free_iocb(phba, cmdiocb);
1950     lpfc_nlp_put(ndlp);
1951     return;
1952 }
1953 /**
1954  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1955  * @phba: pointer to lpfc hba data structure.
1956  * @cmdiocb: pointer to lpfc command iocb data structure.
1957  * @rspiocb: pointer to lpfc response iocb data structure.
1958  *
1959  * This routine is the completion callback function for issuing the Port
1960  * Login (PLOGI) command. For PLOGI completion, there must be an active
1961  * ndlp on the vport node list that matches the remote node ID from the
1962  * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
1963  * ignored and command IOCB released. The PLOGI response IOCB status is
1964  * checked for error conditions. If there is error status reported, PLOGI
1965  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1966  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1967  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1968  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1969  * there are additional N_Port nodes with the vport that need to perform
1970  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1971  * PLOGIs.
1972  **/
1973 static void
1974 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1975             struct lpfc_iocbq *rspiocb)
1976 {
1977     struct lpfc_vport *vport = cmdiocb->vport;
1978     struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1979     IOCB_t *irsp;
1980     struct lpfc_nodelist *ndlp, *free_ndlp;
1981     struct lpfc_dmabuf *prsp;
1982     int disc;
1983     struct serv_parm *sp = NULL;
1984     u32 ulp_status, ulp_word4, did, iotag;
1985     bool release_node = false;
1986 
1987     /* we pass cmdiocb to state machine which needs rspiocb as well */
1988     cmdiocb->rsp_iocb = rspiocb;
1989 
1990     ulp_status = get_job_ulpstatus(phba, rspiocb);
1991     ulp_word4 = get_job_word4(phba, rspiocb);
1992     did = get_job_els_rsp64_did(phba, cmdiocb);
1993 
1994     if (phba->sli_rev == LPFC_SLI_REV4) {
1995         iotag = get_wqe_reqtag(cmdiocb);
1996     } else {
1997         irsp = &rspiocb->iocb;
1998         iotag = irsp->ulpIoTag;
1999     }
2000 
2001     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2002         "PLOGI cmpl:      status:x%x/x%x did:x%x",
2003         ulp_status, ulp_word4, did);
2004 
2005     ndlp = lpfc_findnode_did(vport, did);
2006     if (!ndlp) {
2007         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2008                  "0136 PLOGI completes to NPort x%x "
2009                  "with no ndlp. Data: x%x x%x x%x\n",
2010                  did, ulp_status, ulp_word4, iotag);
2011         goto out_freeiocb;
2012     }
2013 
2014     /* Since ndlp can be freed in the disc state machine, note if this node
2015      * is being used during discovery.
2016      */
2017     spin_lock_irq(&ndlp->lock);
2018     disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2019     ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2020     spin_unlock_irq(&ndlp->lock);
2021 
2022     /* PLOGI completes to NPort <nlp_DID> */
2023     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2024              "0102 PLOGI completes to NPort x%06x "
2025              "Data: x%x x%x x%x x%x x%x\n",
2026              ndlp->nlp_DID, ndlp->nlp_fc4_type,
2027              ulp_status, ulp_word4,
2028              disc, vport->num_disc_nodes);
2029 
2030     /* Check to see if link went down during discovery */
2031     if (lpfc_els_chk_latt(vport)) {
2032         spin_lock_irq(&ndlp->lock);
2033         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2034         spin_unlock_irq(&ndlp->lock);
2035         goto out;
2036     }
2037 
2038     if (ulp_status) {
2039         /* Check for retry */
2040         if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2041             /* ELS command is being retried */
2042             if (disc) {
2043                 spin_lock_irq(&ndlp->lock);
2044                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2045                 spin_unlock_irq(&ndlp->lock);
2046             }
2047             goto out;
2048         }
2049         /* PLOGI failed Don't print the vport to vport rjts */
2050         if (ulp_status != IOSTAT_LS_RJT ||
2051             (((ulp_word4) >> 16 != LSRJT_INVALID_CMD) &&
2052              ((ulp_word4) >> 16 != LSRJT_UNABLE_TPC)) ||
2053             (phba)->pport->cfg_log_verbose & LOG_ELS)
2054             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2055                      "2753 PLOGI failure DID:%06X "
2056                      "Status:x%x/x%x\n",
2057                      ndlp->nlp_DID, ulp_status,
2058                      ulp_word4);
2059 
2060         /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2061         if (!lpfc_error_lost_link(ulp_status, ulp_word4))
2062             lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2063                         NLP_EVT_CMPL_PLOGI);
2064 
2065         /* If a PLOGI collision occurred, the node needs to continue
2066          * with the reglogin process.
2067          */
2068         spin_lock_irq(&ndlp->lock);
2069         if ((ndlp->nlp_flag & (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI)) &&
2070             ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE) {
2071             spin_unlock_irq(&ndlp->lock);
2072             goto out;
2073         }
2074 
2075         /* No PLOGI collision and the node is not registered with the
2076          * scsi or nvme transport. It is no longer an active node. Just
2077          * start the device remove process.
2078          */
2079         if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
2080             ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2081             if (!(ndlp->nlp_flag & NLP_IN_DEV_LOSS))
2082                 release_node = true;
2083         }
2084         spin_unlock_irq(&ndlp->lock);
2085 
2086         if (release_node)
2087             lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2088                         NLP_EVT_DEVICE_RM);
2089     } else {
2090         /* Good status, call state machine */
2091         prsp = list_entry(cmdiocb->cmd_dmabuf->list.next,
2092                   struct lpfc_dmabuf, list);
2093         ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
2094 
2095         sp = (struct serv_parm *)((u8 *)prsp->virt +
2096                       sizeof(u32));
2097 
2098         ndlp->vmid_support = 0;
2099         if ((phba->cfg_vmid_app_header && sp->cmn.app_hdr_support) ||
2100             (phba->cfg_vmid_priority_tagging &&
2101              sp->cmn.priority_tagging)) {
2102             lpfc_printf_log(phba, KERN_DEBUG, LOG_ELS,
2103                     "4018 app_hdr_support %d tagging %d DID x%x\n",
2104                     sp->cmn.app_hdr_support,
2105                     sp->cmn.priority_tagging,
2106                     ndlp->nlp_DID);
2107             /* if the dest port supports VMID, mark it in ndlp */
2108             ndlp->vmid_support = 1;
2109         }
2110 
2111         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2112                     NLP_EVT_CMPL_PLOGI);
2113     }
2114 
2115     if (disc && vport->num_disc_nodes) {
2116         /* Check to see if there are more PLOGIs to be sent */
2117         lpfc_more_plogi(vport);
2118 
2119         if (vport->num_disc_nodes == 0) {
2120             spin_lock_irq(shost->host_lock);
2121             vport->fc_flag &= ~FC_NDISC_ACTIVE;
2122             spin_unlock_irq(shost->host_lock);
2123 
2124             lpfc_can_disctmo(vport);
2125             lpfc_end_rscn(vport);
2126         }
2127     }
2128 
2129 out:
2130     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2131                   "PLOGI Cmpl PUT:     did:x%x refcnt %d",
2132                   ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2133 
2134 out_freeiocb:
2135     /* Release the reference on the original I/O request. */
2136     free_ndlp = cmdiocb->ndlp;
2137 
2138     lpfc_els_free_iocb(phba, cmdiocb);
2139     lpfc_nlp_put(free_ndlp);
2140     return;
2141 }
2142 
2143 /**
2144  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
2145  * @vport: pointer to a host virtual N_Port data structure.
2146  * @did: destination port identifier.
2147  * @retry: number of retries to the command IOCB.
2148  *
2149  * This routine issues a Port Login (PLOGI) command to a remote N_Port
2150  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
2151  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
2152  * This routine constructs the proper fields of the PLOGI IOCB and invokes
2153  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
2154  *
2155  * Note that the ndlp reference count will be incremented by 1 for holding
2156  * the ndlp and the reference to ndlp will be stored into the ndlp field
2157  * of the IOCB for the completion callback function to the PLOGI ELS command.
2158  *
2159  * Return code
2160  *   0 - Successfully issued a plogi for @vport
2161  *   1 - failed to issue a plogi for @vport
2162  **/
2163 int
2164 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
2165 {
2166     struct lpfc_hba  *phba = vport->phba;
2167     struct serv_parm *sp;
2168     struct lpfc_nodelist *ndlp;
2169     struct lpfc_iocbq *elsiocb;
2170     uint8_t *pcmd;
2171     uint16_t cmdsize;
2172     int ret;
2173 
2174     ndlp = lpfc_findnode_did(vport, did);
2175     if (!ndlp)
2176         return 1;
2177 
2178     /* Defer the processing of the issue PLOGI until after the
2179      * outstanding UNREG_RPI mbox command completes, unless we
2180      * are going offline. This logic does not apply for Fabric DIDs
2181      */
2182     if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2183         ((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
2184         !(vport->fc_flag & FC_OFFLINE_MODE)) {
2185         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2186                  "4110 Issue PLOGI x%x deferred "
2187                  "on NPort x%x rpi x%x Data: x%px\n",
2188                  ndlp->nlp_defer_did, ndlp->nlp_DID,
2189                  ndlp->nlp_rpi, ndlp);
2190 
2191         /* We can only defer 1st PLOGI */
2192         if (ndlp->nlp_defer_did == NLP_EVT_NOTHING_PENDING)
2193             ndlp->nlp_defer_did = did;
2194         return 0;
2195     }
2196 
2197     cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
2198     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
2199                      ELS_CMD_PLOGI);
2200     if (!elsiocb)
2201         return 1;
2202 
2203     spin_lock_irq(&ndlp->lock);
2204     ndlp->nlp_flag &= ~NLP_FCP_PRLI_RJT;
2205     spin_unlock_irq(&ndlp->lock);
2206 
2207     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
2208 
2209     /* For PLOGI request, remainder of payload is service parameters */
2210     *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
2211     pcmd += sizeof(uint32_t);
2212     memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2213     sp = (struct serv_parm *) pcmd;
2214 
2215     /*
2216      * If we are a N-port connected to a Fabric, fix-up paramm's so logins
2217      * to device on remote loops work.
2218      */
2219     if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
2220         sp->cmn.altBbCredit = 1;
2221 
2222     if (sp->cmn.fcphLow < FC_PH_4_3)
2223         sp->cmn.fcphLow = FC_PH_4_3;
2224 
2225     if (sp->cmn.fcphHigh < FC_PH3)
2226         sp->cmn.fcphHigh = FC_PH3;
2227 
2228     sp->cmn.valid_vendor_ver_level = 0;
2229     memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
2230     sp->cmn.bbRcvSizeMsb &= 0xF;
2231 
2232     /* Check if the destination port supports VMID */
2233     ndlp->vmid_support = 0;
2234     if (vport->vmid_priority_tagging)
2235         sp->cmn.priority_tagging = 1;
2236     else if (phba->cfg_vmid_app_header &&
2237          bf_get(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags))
2238         sp->cmn.app_hdr_support = 1;
2239 
2240     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2241         "Issue PLOGI:     did:x%x",
2242         did, 0, 0);
2243 
2244     /* If our firmware supports this feature, convey that
2245      * information to the target using the vendor specific field.
2246      */
2247     if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
2248         sp->cmn.valid_vendor_ver_level = 1;
2249         sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
2250         sp->un.vv.flags = cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
2251     }
2252 
2253     phba->fc_stat.elsXmitPLOGI++;
2254     elsiocb->cmd_cmpl = lpfc_cmpl_els_plogi;
2255 
2256     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2257                   "Issue PLOGI:     did:x%x refcnt %d",
2258                   did, kref_read(&ndlp->kref), 0);
2259     elsiocb->ndlp = lpfc_nlp_get(ndlp);
2260     if (!elsiocb->ndlp) {
2261         lpfc_els_free_iocb(phba, elsiocb);
2262         return 1;
2263     }
2264 
2265     ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2266     if (ret) {
2267         lpfc_els_free_iocb(phba, elsiocb);
2268         lpfc_nlp_put(ndlp);
2269         return 1;
2270     }
2271 
2272     return 0;
2273 }
2274 
2275 /**
2276  * lpfc_cmpl_els_prli - Completion callback function for prli
2277  * @phba: pointer to lpfc hba data structure.
2278  * @cmdiocb: pointer to lpfc command iocb data structure.
2279  * @rspiocb: pointer to lpfc response iocb data structure.
2280  *
2281  * This routine is the completion callback function for a Process Login
2282  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2283  * status. If there is error status reported, PRLI retry shall be attempted
2284  * by invoking the lpfc_els_retry() routine. Otherwise, the state
2285  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2286  * ndlp to mark the PRLI completion.
2287  **/
2288 static void
2289 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2290            struct lpfc_iocbq *rspiocb)
2291 {
2292     struct lpfc_vport *vport = cmdiocb->vport;
2293     struct lpfc_nodelist *ndlp;
2294     char *mode;
2295     u32 loglevel;
2296     u32 ulp_status;
2297     u32 ulp_word4;
2298     bool release_node = false;
2299 
2300     /* we pass cmdiocb to state machine which needs rspiocb as well */
2301     cmdiocb->rsp_iocb = rspiocb;
2302 
2303     ndlp = cmdiocb->ndlp;
2304 
2305     ulp_status = get_job_ulpstatus(phba, rspiocb);
2306     ulp_word4 = get_job_word4(phba, rspiocb);
2307 
2308     spin_lock_irq(&ndlp->lock);
2309     ndlp->nlp_flag &= ~NLP_PRLI_SND;
2310 
2311     /* Driver supports multiple FC4 types.  Counters matter. */
2312     vport->fc_prli_sent--;
2313     ndlp->fc4_prli_sent--;
2314     spin_unlock_irq(&ndlp->lock);
2315 
2316     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2317         "PRLI cmpl:       status:x%x/x%x did:x%x",
2318         ulp_status, ulp_word4,
2319         ndlp->nlp_DID);
2320 
2321     /* PRLI completes to NPort <nlp_DID> */
2322     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2323              "0103 PRLI completes to NPort x%06x "
2324              "Data: x%x x%x x%x x%x\n",
2325              ndlp->nlp_DID, ulp_status, ulp_word4,
2326              vport->num_disc_nodes, ndlp->fc4_prli_sent);
2327 
2328     /* Check to see if link went down during discovery */
2329     if (lpfc_els_chk_latt(vport))
2330         goto out;
2331 
2332     if (ulp_status) {
2333         /* Check for retry */
2334         if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2335             /* ELS command is being retried */
2336             goto out;
2337         }
2338 
2339         /* If we don't send GFT_ID to Fabric, a PRLI error
2340          * could be expected.
2341          */
2342         if ((vport->fc_flag & FC_FABRIC) ||
2343             (vport->cfg_enable_fc4_type != LPFC_ENABLE_BOTH)) {
2344             mode = KERN_ERR;
2345             loglevel =  LOG_TRACE_EVENT;
2346         } else {
2347             mode = KERN_INFO;
2348             loglevel =  LOG_ELS;
2349         }
2350 
2351         /* PRLI failed */
2352         lpfc_printf_vlog(vport, mode, loglevel,
2353                  "2754 PRLI failure DID:%06X Status:x%x/x%x, "
2354                  "data: x%x\n",
2355                  ndlp->nlp_DID, ulp_status,
2356                  ulp_word4, ndlp->fc4_prli_sent);
2357 
2358         /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2359         if (!lpfc_error_lost_link(ulp_status, ulp_word4))
2360             lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2361                         NLP_EVT_CMPL_PRLI);
2362 
2363         /*
2364          * For P2P topology, retain the node so that PLOGI can be
2365          * attempted on it again.
2366          */
2367         if (vport->fc_flag & FC_PT2PT)
2368             goto out;
2369 
2370         /* As long as this node is not registered with the SCSI
2371          * or NVMe transport and no other PRLIs are outstanding,
2372          * it is no longer an active node.  Otherwise devloss
2373          * handles the final cleanup.
2374          */
2375         spin_lock_irq(&ndlp->lock);
2376         if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
2377             !ndlp->fc4_prli_sent) {
2378             ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2379             if (!(ndlp->nlp_flag & NLP_IN_DEV_LOSS))
2380                 release_node = true;
2381         }
2382         spin_unlock_irq(&ndlp->lock);
2383 
2384         if (release_node)
2385             lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2386                         NLP_EVT_DEVICE_RM);
2387     } else {
2388         /* Good status, call state machine.  However, if another
2389          * PRLI is outstanding, don't call the state machine
2390          * because final disposition to Mapped or Unmapped is
2391          * completed there.
2392          */
2393         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2394                     NLP_EVT_CMPL_PRLI);
2395     }
2396 
2397 out:
2398     lpfc_els_free_iocb(phba, cmdiocb);
2399     lpfc_nlp_put(ndlp);
2400     return;
2401 }
2402 
2403 /**
2404  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
2405  * @vport: pointer to a host virtual N_Port data structure.
2406  * @ndlp: pointer to a node-list data structure.
2407  * @retry: number of retries to the command IOCB.
2408  *
2409  * This routine issues a Process Login (PRLI) ELS command for the
2410  * @vport. The PRLI service parameters are set up in the payload of the
2411  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
2412  * is put to the IOCB completion callback func field before invoking the
2413  * routine lpfc_sli_issue_iocb() to send out PRLI command.
2414  *
2415  * Note that the ndlp reference count will be incremented by 1 for holding the
2416  * ndlp and the reference to ndlp will be stored into the ndlp field of
2417  * the IOCB for the completion callback function to the PRLI ELS command.
2418  *
2419  * Return code
2420  *   0 - successfully issued prli iocb command for @vport
2421  *   1 - failed to issue prli iocb command for @vport
2422  **/
2423 int
2424 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2425             uint8_t retry)
2426 {
2427     int rc = 0;
2428     struct lpfc_hba *phba = vport->phba;
2429     PRLI *npr;
2430     struct lpfc_nvme_prli *npr_nvme;
2431     struct lpfc_iocbq *elsiocb;
2432     uint8_t *pcmd;
2433     uint16_t cmdsize;
2434     u32 local_nlp_type, elscmd;
2435 
2436     /*
2437      * If we are in RSCN mode, the FC4 types supported from a
2438      * previous GFT_ID command may not be accurate. So, if we
2439      * are a NVME Initiator, always look for the possibility of
2440      * the remote NPort beng a NVME Target.
2441      */
2442     if (phba->sli_rev == LPFC_SLI_REV4 &&
2443         vport->fc_flag & FC_RSCN_MODE &&
2444         vport->nvmei_support)
2445         ndlp->nlp_fc4_type |= NLP_FC4_NVME;
2446     local_nlp_type = ndlp->nlp_fc4_type;
2447 
2448     /* This routine will issue 1 or 2 PRLIs, so zero all the ndlp
2449      * fields here before any of them can complete.
2450      */
2451     ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2452     ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
2453     ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
2454     ndlp->nlp_flag &= ~(NLP_FIRSTBURST | NLP_NPR_2B_DISC);
2455     ndlp->nvme_fb_size = 0;
2456 
2457  send_next_prli:
2458     if (local_nlp_type & NLP_FC4_FCP) {
2459         /* Payload is 4 + 16 = 20 x14 bytes. */
2460         cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2461         elscmd = ELS_CMD_PRLI;
2462     } else if (local_nlp_type & NLP_FC4_NVME) {
2463         /* Payload is 4 + 20 = 24 x18 bytes. */
2464         cmdsize = (sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli));
2465         elscmd = ELS_CMD_NVMEPRLI;
2466     } else {
2467         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2468                  "3083 Unknown FC_TYPE x%x ndlp x%06x\n",
2469                  ndlp->nlp_fc4_type, ndlp->nlp_DID);
2470         return 1;
2471     }
2472 
2473     /* SLI3 ports don't support NVME.  If this rport is a strict NVME
2474      * FC4 type, implicitly LOGO.
2475      */
2476     if (phba->sli_rev == LPFC_SLI_REV3 &&
2477         ndlp->nlp_fc4_type == NLP_FC4_NVME) {
2478         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2479                  "3088 Rport fc4 type 0x%x not supported by SLI3 adapter\n",
2480                  ndlp->nlp_type);
2481         lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
2482         return 1;
2483     }
2484 
2485     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2486                      ndlp->nlp_DID, elscmd);
2487     if (!elsiocb)
2488         return 1;
2489 
2490     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
2491 
2492     /* For PRLI request, remainder of payload is service parameters */
2493     memset(pcmd, 0, cmdsize);
2494 
2495     if (local_nlp_type & NLP_FC4_FCP) {
2496         /* Remainder of payload is FCP PRLI parameter page.
2497          * Note: this data structure is defined as
2498          * BE/LE in the structure definition so no
2499          * byte swap call is made.
2500          */
2501         *((uint32_t *)(pcmd)) = ELS_CMD_PRLI;
2502         pcmd += sizeof(uint32_t);
2503         npr = (PRLI *)pcmd;
2504 
2505         /*
2506          * If our firmware version is 3.20 or later,
2507          * set the following bits for FC-TAPE support.
2508          */
2509         if (phba->vpd.rev.feaLevelHigh >= 0x02) {
2510             npr->ConfmComplAllowed = 1;
2511             npr->Retry = 1;
2512             npr->TaskRetryIdReq = 1;
2513         }
2514         npr->estabImagePair = 1;
2515         npr->readXferRdyDis = 1;
2516         if (vport->cfg_first_burst_size)
2517             npr->writeXferRdyDis = 1;
2518 
2519         /* For FCP support */
2520         npr->prliType = PRLI_FCP_TYPE;
2521         npr->initiatorFunc = 1;
2522         elsiocb->cmd_flag |= LPFC_PRLI_FCP_REQ;
2523 
2524         /* Remove FCP type - processed. */
2525         local_nlp_type &= ~NLP_FC4_FCP;
2526     } else if (local_nlp_type & NLP_FC4_NVME) {
2527         /* Remainder of payload is NVME PRLI parameter page.
2528          * This data structure is the newer definition that
2529          * uses bf macros so a byte swap is required.
2530          */
2531         *((uint32_t *)(pcmd)) = ELS_CMD_NVMEPRLI;
2532         pcmd += sizeof(uint32_t);
2533         npr_nvme = (struct lpfc_nvme_prli *)pcmd;
2534         bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
2535         bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
2536         if (phba->nsler) {
2537             bf_set(prli_nsler, npr_nvme, 1);
2538             bf_set(prli_conf, npr_nvme, 1);
2539         }
2540 
2541         /* Only initiators request first burst. */
2542         if ((phba->cfg_nvme_enable_fb) &&
2543             !phba->nvmet_support)
2544             bf_set(prli_fba, npr_nvme, 1);
2545 
2546         if (phba->nvmet_support) {
2547             bf_set(prli_tgt, npr_nvme, 1);
2548             bf_set(prli_disc, npr_nvme, 1);
2549         } else {
2550             bf_set(prli_init, npr_nvme, 1);
2551             bf_set(prli_conf, npr_nvme, 1);
2552         }
2553 
2554         npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
2555         npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
2556         elsiocb->cmd_flag |= LPFC_PRLI_NVME_REQ;
2557 
2558         /* Remove NVME type - processed. */
2559         local_nlp_type &= ~NLP_FC4_NVME;
2560     }
2561 
2562     phba->fc_stat.elsXmitPRLI++;
2563     elsiocb->cmd_cmpl = lpfc_cmpl_els_prli;
2564 
2565     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2566                   "Issue PRLI:  did:x%x refcnt %d",
2567                   ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2568     elsiocb->ndlp = lpfc_nlp_get(ndlp);
2569     if (!elsiocb->ndlp) {
2570         lpfc_els_free_iocb(phba, elsiocb);
2571         return 1;
2572     }
2573 
2574     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2575     if (rc == IOCB_ERROR) {
2576         lpfc_els_free_iocb(phba, elsiocb);
2577         lpfc_nlp_put(ndlp);
2578         return 1;
2579     }
2580 
2581     /* The vport counters are used for lpfc_scan_finished, but
2582      * the ndlp is used to track outstanding PRLIs for different
2583      * FC4 types.
2584      */
2585     spin_lock_irq(&ndlp->lock);
2586     ndlp->nlp_flag |= NLP_PRLI_SND;
2587     vport->fc_prli_sent++;
2588     ndlp->fc4_prli_sent++;
2589     spin_unlock_irq(&ndlp->lock);
2590 
2591     /* The driver supports 2 FC4 types.  Make sure
2592      * a PRLI is issued for all types before exiting.
2593      */
2594     if (phba->sli_rev == LPFC_SLI_REV4 &&
2595         local_nlp_type & (NLP_FC4_FCP | NLP_FC4_NVME))
2596         goto send_next_prli;
2597     else
2598         return 0;
2599 }
2600 
2601 /**
2602  * lpfc_rscn_disc - Perform rscn discovery for a vport
2603  * @vport: pointer to a host virtual N_Port data structure.
2604  *
2605  * This routine performs Registration State Change Notification (RSCN)
2606  * discovery for a @vport. If the @vport's node port recovery count is not
2607  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
2608  * the nodes that need recovery. If none of the PLOGI were needed through
2609  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
2610  * invoked to check and handle possible more RSCN came in during the period
2611  * of processing the current ones.
2612  **/
2613 static void
2614 lpfc_rscn_disc(struct lpfc_vport *vport)
2615 {
2616     lpfc_can_disctmo(vport);
2617 
2618     /* RSCN discovery */
2619     /* go thru NPR nodes and issue ELS PLOGIs */
2620     if (vport->fc_npr_cnt)
2621         if (lpfc_els_disc_plogi(vport))
2622             return;
2623 
2624     lpfc_end_rscn(vport);
2625 }
2626 
2627 /**
2628  * lpfc_adisc_done - Complete the adisc phase of discovery
2629  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2630  *
2631  * This function is called when the final ADISC is completed during discovery.
2632  * This function handles clearing link attention or issuing reg_vpi depending
2633  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2634  * discovery.
2635  * This function is called with no locks held.
2636  **/
2637 static void
2638 lpfc_adisc_done(struct lpfc_vport *vport)
2639 {
2640     struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
2641     struct lpfc_hba   *phba = vport->phba;
2642 
2643     /*
2644      * For NPIV, cmpl_reg_vpi will set port_state to READY,
2645      * and continue discovery.
2646      */
2647     if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2648         !(vport->fc_flag & FC_RSCN_MODE) &&
2649         (phba->sli_rev < LPFC_SLI_REV4)) {
2650 
2651         /*
2652          * If link is down, clear_la and reg_vpi will be done after
2653          * flogi following a link up event
2654          */
2655         if (!lpfc_is_link_up(phba))
2656             return;
2657 
2658         /* The ADISCs are complete.  Doesn't matter if they
2659          * succeeded or failed because the ADISC completion
2660          * routine guarantees to call the state machine and
2661          * the RPI is either unregistered (failed ADISC response)
2662          * or the RPI is still valid and the node is marked
2663          * mapped for a target.  The exchanges should be in the
2664          * correct state. This code is specific to SLI3.
2665          */
2666         lpfc_issue_clear_la(phba, vport);
2667         lpfc_issue_reg_vpi(phba, vport);
2668         return;
2669     }
2670     /*
2671     * For SLI2, we need to set port_state to READY
2672     * and continue discovery.
2673     */
2674     if (vport->port_state < LPFC_VPORT_READY) {
2675         /* If we get here, there is nothing to ADISC */
2676         lpfc_issue_clear_la(phba, vport);
2677         if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2678             vport->num_disc_nodes = 0;
2679             /* go thru NPR list, issue ELS PLOGIs */
2680             if (vport->fc_npr_cnt)
2681                 lpfc_els_disc_plogi(vport);
2682             if (!vport->num_disc_nodes) {
2683                 spin_lock_irq(shost->host_lock);
2684                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2685                 spin_unlock_irq(shost->host_lock);
2686                 lpfc_can_disctmo(vport);
2687                 lpfc_end_rscn(vport);
2688             }
2689         }
2690         vport->port_state = LPFC_VPORT_READY;
2691     } else
2692         lpfc_rscn_disc(vport);
2693 }
2694 
2695 /**
2696  * lpfc_more_adisc - Issue more adisc as needed
2697  * @vport: pointer to a host virtual N_Port data structure.
2698  *
2699  * This routine determines whether there are more ndlps on a @vport
2700  * node list need to have Address Discover (ADISC) issued. If so, it will
2701  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2702  * remaining nodes which need to have ADISC sent.
2703  **/
2704 void
2705 lpfc_more_adisc(struct lpfc_vport *vport)
2706 {
2707     if (vport->num_disc_nodes)
2708         vport->num_disc_nodes--;
2709     /* Continue discovery with <num_disc_nodes> ADISCs to go */
2710     lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2711              "0210 Continue discovery with %d ADISCs to go "
2712              "Data: x%x x%x x%x\n",
2713              vport->num_disc_nodes, vport->fc_adisc_cnt,
2714              vport->fc_flag, vport->port_state);
2715     /* Check to see if there are more ADISCs to be sent */
2716     if (vport->fc_flag & FC_NLP_MORE) {
2717         lpfc_set_disctmo(vport);
2718         /* go thru NPR nodes and issue any remaining ELS ADISCs */
2719         lpfc_els_disc_adisc(vport);
2720     }
2721     if (!vport->num_disc_nodes)
2722         lpfc_adisc_done(vport);
2723     return;
2724 }
2725 
2726 /**
2727  * lpfc_cmpl_els_adisc - Completion callback function for adisc
2728  * @phba: pointer to lpfc hba data structure.
2729  * @cmdiocb: pointer to lpfc command iocb data structure.
2730  * @rspiocb: pointer to lpfc response iocb data structure.
2731  *
2732  * This routine is the completion function for issuing the Address Discover
2733  * (ADISC) command. It first checks to see whether link went down during
2734  * the discovery process. If so, the node will be marked as node port
2735  * recovery for issuing discover IOCB by the link attention handler and
2736  * exit. Otherwise, the response status is checked. If error was reported
2737  * in the response status, the ADISC command shall be retried by invoking
2738  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2739  * the response status, the state machine is invoked to set transition
2740  * with respect to NLP_EVT_CMPL_ADISC event.
2741  **/
2742 static void
2743 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2744             struct lpfc_iocbq *rspiocb)
2745 {
2746     struct lpfc_vport *vport = cmdiocb->vport;
2747     IOCB_t *irsp;
2748     struct lpfc_nodelist *ndlp;
2749     int  disc;
2750     u32 ulp_status, ulp_word4, tmo;
2751     bool release_node = false;
2752 
2753     /* we pass cmdiocb to state machine which needs rspiocb as well */
2754     cmdiocb->rsp_iocb = rspiocb;
2755 
2756     ndlp = cmdiocb->ndlp;
2757 
2758     ulp_status = get_job_ulpstatus(phba, rspiocb);
2759     ulp_word4 = get_job_word4(phba, rspiocb);
2760 
2761     if (phba->sli_rev == LPFC_SLI_REV4) {
2762         tmo = get_wqe_tmo(cmdiocb);
2763     } else {
2764         irsp = &rspiocb->iocb;
2765         tmo = irsp->ulpTimeout;
2766     }
2767 
2768     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2769         "ADISC cmpl:      status:x%x/x%x did:x%x",
2770         ulp_status, ulp_word4,
2771         ndlp->nlp_DID);
2772 
2773     /* Since ndlp can be freed in the disc state machine, note if this node
2774      * is being used during discovery.
2775      */
2776     spin_lock_irq(&ndlp->lock);
2777     disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2778     ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
2779     spin_unlock_irq(&ndlp->lock);
2780     /* ADISC completes to NPort <nlp_DID> */
2781     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2782              "0104 ADISC completes to NPort x%x "
2783              "Data: x%x x%x x%x x%x x%x\n",
2784              ndlp->nlp_DID, ulp_status, ulp_word4,
2785              tmo, disc, vport->num_disc_nodes);
2786     /* Check to see if link went down during discovery */
2787     if (lpfc_els_chk_latt(vport)) {
2788         spin_lock_irq(&ndlp->lock);
2789         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2790         spin_unlock_irq(&ndlp->lock);
2791         goto out;
2792     }
2793 
2794     if (ulp_status) {
2795         /* Check for retry */
2796         if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2797             /* ELS command is being retried */
2798             if (disc) {
2799                 spin_lock_irq(&ndlp->lock);
2800                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2801                 spin_unlock_irq(&ndlp->lock);
2802                 lpfc_set_disctmo(vport);
2803             }
2804             goto out;
2805         }
2806         /* ADISC failed */
2807         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2808                  "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2809                  ndlp->nlp_DID, ulp_status,
2810                  ulp_word4);
2811         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2812                     NLP_EVT_CMPL_ADISC);
2813 
2814         /* As long as this node is not registered with the SCSI or NVMe
2815          * transport, it is no longer an active node. Otherwise
2816          * devloss handles the final cleanup.
2817          */
2818         spin_lock_irq(&ndlp->lock);
2819         if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
2820             ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2821             if (!(ndlp->nlp_flag & NLP_IN_DEV_LOSS))
2822                 release_node = true;
2823         }
2824         spin_unlock_irq(&ndlp->lock);
2825 
2826         if (release_node)
2827             lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2828                         NLP_EVT_DEVICE_RM);
2829     } else
2830         /* Good status, call state machine */
2831         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2832                     NLP_EVT_CMPL_ADISC);
2833 
2834     /* Check to see if there are more ADISCs to be sent */
2835     if (disc && vport->num_disc_nodes)
2836         lpfc_more_adisc(vport);
2837 out:
2838     lpfc_els_free_iocb(phba, cmdiocb);
2839     lpfc_nlp_put(ndlp);
2840     return;
2841 }
2842 
2843 /**
2844  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2845  * @vport: pointer to a virtual N_Port data structure.
2846  * @ndlp: pointer to a node-list data structure.
2847  * @retry: number of retries to the command IOCB.
2848  *
2849  * This routine issues an Address Discover (ADISC) for an @ndlp on a
2850  * @vport. It prepares the payload of the ADISC ELS command, updates the
2851  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2852  * to issue the ADISC ELS command.
2853  *
2854  * Note that the ndlp reference count will be incremented by 1 for holding the
2855  * ndlp and the reference to ndlp will be stored into the ndlp field of
2856  * the IOCB for the completion callback function to the ADISC ELS command.
2857  *
2858  * Return code
2859  *   0 - successfully issued adisc
2860  *   1 - failed to issue adisc
2861  **/
2862 int
2863 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2864              uint8_t retry)
2865 {
2866     int rc = 0;
2867     struct lpfc_hba  *phba = vport->phba;
2868     ADISC *ap;
2869     struct lpfc_iocbq *elsiocb;
2870     uint8_t *pcmd;
2871     uint16_t cmdsize;
2872 
2873     cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2874     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2875                      ndlp->nlp_DID, ELS_CMD_ADISC);
2876     if (!elsiocb)
2877         return 1;
2878 
2879     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
2880 
2881     /* For ADISC request, remainder of payload is service parameters */
2882     *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2883     pcmd += sizeof(uint32_t);
2884 
2885     /* Fill in ADISC payload */
2886     ap = (ADISC *) pcmd;
2887     ap->hardAL_PA = phba->fc_pref_ALPA;
2888     memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2889     memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2890     ap->DID = be32_to_cpu(vport->fc_myDID);
2891 
2892     phba->fc_stat.elsXmitADISC++;
2893     elsiocb->cmd_cmpl = lpfc_cmpl_els_adisc;
2894     spin_lock_irq(&ndlp->lock);
2895     ndlp->nlp_flag |= NLP_ADISC_SND;
2896     spin_unlock_irq(&ndlp->lock);
2897     elsiocb->ndlp = lpfc_nlp_get(ndlp);
2898     if (!elsiocb->ndlp) {
2899         lpfc_els_free_iocb(phba, elsiocb);
2900         goto err;
2901     }
2902 
2903     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2904                   "Issue ADISC:   did:x%x refcnt %d",
2905                   ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2906 
2907     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2908     if (rc == IOCB_ERROR) {
2909         lpfc_els_free_iocb(phba, elsiocb);
2910         lpfc_nlp_put(ndlp);
2911         goto err;
2912     }
2913 
2914     return 0;
2915 
2916 err:
2917     spin_lock_irq(&ndlp->lock);
2918     ndlp->nlp_flag &= ~NLP_ADISC_SND;
2919     spin_unlock_irq(&ndlp->lock);
2920     return 1;
2921 }
2922 
2923 /**
2924  * lpfc_cmpl_els_logo - Completion callback function for logo
2925  * @phba: pointer to lpfc hba data structure.
2926  * @cmdiocb: pointer to lpfc command iocb data structure.
2927  * @rspiocb: pointer to lpfc response iocb data structure.
2928  *
2929  * This routine is the completion function for issuing the ELS Logout (LOGO)
2930  * command. If no error status was reported from the LOGO response, the
2931  * state machine of the associated ndlp shall be invoked for transition with
2932  * respect to NLP_EVT_CMPL_LOGO event.
2933  **/
2934 static void
2935 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2936            struct lpfc_iocbq *rspiocb)
2937 {
2938     struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
2939     struct lpfc_vport *vport = ndlp->vport;
2940     IOCB_t *irsp;
2941     unsigned long flags;
2942     uint32_t skip_recovery = 0;
2943     int wake_up_waiter = 0;
2944     u32 ulp_status;
2945     u32 ulp_word4;
2946     u32 tmo;
2947 
2948     /* we pass cmdiocb to state machine which needs rspiocb as well */
2949     cmdiocb->rsp_iocb = rspiocb;
2950 
2951     ulp_status = get_job_ulpstatus(phba, rspiocb);
2952     ulp_word4 = get_job_word4(phba, rspiocb);
2953 
2954     if (phba->sli_rev == LPFC_SLI_REV4) {
2955         tmo = get_wqe_tmo(cmdiocb);
2956     } else {
2957         irsp = &rspiocb->iocb;
2958         tmo = irsp->ulpTimeout;
2959     }
2960 
2961     spin_lock_irq(&ndlp->lock);
2962     ndlp->nlp_flag &= ~NLP_LOGO_SND;
2963     if (ndlp->save_flags & NLP_WAIT_FOR_LOGO) {
2964         wake_up_waiter = 1;
2965         ndlp->save_flags &= ~NLP_WAIT_FOR_LOGO;
2966     }
2967     spin_unlock_irq(&ndlp->lock);
2968 
2969     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2970         "LOGO cmpl:       status:x%x/x%x did:x%x",
2971         ulp_status, ulp_word4,
2972         ndlp->nlp_DID);
2973 
2974     /* LOGO completes to NPort <nlp_DID> */
2975     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2976              "0105 LOGO completes to NPort x%x "
2977              "refcnt %d nflags x%x Data: x%x x%x x%x x%x\n",
2978              ndlp->nlp_DID, kref_read(&ndlp->kref), ndlp->nlp_flag,
2979              ulp_status, ulp_word4,
2980              tmo, vport->num_disc_nodes);
2981 
2982     if (lpfc_els_chk_latt(vport)) {
2983         skip_recovery = 1;
2984         goto out;
2985     }
2986 
2987     /* The LOGO will not be retried on failure.  A LOGO was
2988      * issued to the remote rport and a ACC or RJT or no Answer are
2989      * all acceptable.  Note the failure and move forward with
2990      * discovery.  The PLOGI will retry.
2991      */
2992     if (ulp_status) {
2993         /* LOGO failed */
2994         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2995                  "2756 LOGO failure, No Retry DID:%06X "
2996                  "Status:x%x/x%x\n",
2997                  ndlp->nlp_DID, ulp_status,
2998                  ulp_word4);
2999 
3000         if (lpfc_error_lost_link(ulp_status, ulp_word4)) {
3001             skip_recovery = 1;
3002             goto out;
3003         }
3004     }
3005 
3006     /* Call state machine. This will unregister the rpi if needed. */
3007     lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
3008 
3009     /* The driver sets this flag for an NPIV instance that doesn't want to
3010      * log into the remote port.
3011      */
3012     if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
3013         spin_lock_irq(&ndlp->lock);
3014         if (phba->sli_rev == LPFC_SLI_REV4)
3015             ndlp->nlp_flag |= NLP_RELEASE_RPI;
3016         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
3017         spin_unlock_irq(&ndlp->lock);
3018         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
3019                     NLP_EVT_DEVICE_RM);
3020         goto out_rsrc_free;
3021     }
3022 
3023 out:
3024     /* At this point, the LOGO processing is complete. NOTE: For a
3025      * pt2pt topology, we are assuming the NPortID will only change
3026      * on link up processing. For a LOGO / PLOGI initiated by the
3027      * Initiator, we are assuming the NPortID is not going to change.
3028      */
3029 
3030     if (wake_up_waiter && ndlp->logo_waitq)
3031         wake_up(ndlp->logo_waitq);
3032     /*
3033      * If the node is a target, the handling attempts to recover the port.
3034      * For any other port type, the rpi is unregistered as an implicit
3035      * LOGO.
3036      */
3037     if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) &&
3038         skip_recovery == 0) {
3039         lpfc_cancel_retry_delay_tmo(vport, ndlp);
3040         spin_lock_irqsave(&ndlp->lock, flags);
3041         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3042         spin_unlock_irqrestore(&ndlp->lock, flags);
3043 
3044         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3045                  "3187 LOGO completes to NPort x%x: Start "
3046                  "Recovery Data: x%x x%x x%x x%x\n",
3047                  ndlp->nlp_DID, ulp_status,
3048                  ulp_word4, tmo,
3049                  vport->num_disc_nodes);
3050 
3051         lpfc_els_free_iocb(phba, cmdiocb);
3052         lpfc_nlp_put(ndlp);
3053 
3054         lpfc_disc_start(vport);
3055         return;
3056     }
3057 
3058     /* Cleanup path for failed REG_RPI handling. If REG_RPI fails, the
3059      * driver sends a LOGO to the rport to cleanup.  For fabric and
3060      * initiator ports cleanup the node as long as it the node is not
3061      * register with the transport.
3062      */
3063     if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
3064         spin_lock_irq(&ndlp->lock);
3065         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
3066         spin_unlock_irq(&ndlp->lock);
3067         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
3068                     NLP_EVT_DEVICE_RM);
3069     }
3070 out_rsrc_free:
3071     /* Driver is done with the I/O. */
3072     lpfc_els_free_iocb(phba, cmdiocb);
3073     lpfc_nlp_put(ndlp);
3074 }
3075 
3076 /**
3077  * lpfc_issue_els_logo - Issue a logo to an node on a vport
3078  * @vport: pointer to a virtual N_Port data structure.
3079  * @ndlp: pointer to a node-list data structure.
3080  * @retry: number of retries to the command IOCB.
3081  *
3082  * This routine constructs and issues an ELS Logout (LOGO) iocb command
3083  * to a remote node, referred by an @ndlp on a @vport. It constructs the
3084  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
3085  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
3086  *
3087  * Note that the ndlp reference count will be incremented by 1 for holding the
3088  * ndlp and the reference to ndlp will be stored into the ndlp field of
3089  * the IOCB for the completion callback function to the LOGO ELS command.
3090  *
3091  * Callers of this routine are expected to unregister the RPI first
3092  *
3093  * Return code
3094  *   0 - successfully issued logo
3095  *   1 - failed to issue logo
3096  **/
3097 int
3098 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3099             uint8_t retry)
3100 {
3101     struct lpfc_hba  *phba = vport->phba;
3102     struct lpfc_iocbq *elsiocb;
3103     uint8_t *pcmd;
3104     uint16_t cmdsize;
3105     int rc;
3106 
3107     spin_lock_irq(&ndlp->lock);
3108     if (ndlp->nlp_flag & NLP_LOGO_SND) {
3109         spin_unlock_irq(&ndlp->lock);
3110         return 0;
3111     }
3112     spin_unlock_irq(&ndlp->lock);
3113 
3114     cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
3115     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3116                      ndlp->nlp_DID, ELS_CMD_LOGO);
3117     if (!elsiocb)
3118         return 1;
3119 
3120     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
3121     *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
3122     pcmd += sizeof(uint32_t);
3123 
3124     /* Fill in LOGO payload */
3125     *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
3126     pcmd += sizeof(uint32_t);
3127     memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
3128 
3129     phba->fc_stat.elsXmitLOGO++;
3130     elsiocb->cmd_cmpl = lpfc_cmpl_els_logo;
3131     spin_lock_irq(&ndlp->lock);
3132     ndlp->nlp_flag |= NLP_LOGO_SND;
3133     ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
3134     spin_unlock_irq(&ndlp->lock);
3135     elsiocb->ndlp = lpfc_nlp_get(ndlp);
3136     if (!elsiocb->ndlp) {
3137         lpfc_els_free_iocb(phba, elsiocb);
3138         goto err;
3139     }
3140 
3141     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3142                   "Issue LOGO:      did:x%x refcnt %d",
3143                   ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3144 
3145     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3146     if (rc == IOCB_ERROR) {
3147         lpfc_els_free_iocb(phba, elsiocb);
3148         lpfc_nlp_put(ndlp);
3149         goto err;
3150     }
3151 
3152     spin_lock_irq(&ndlp->lock);
3153     ndlp->nlp_prev_state = ndlp->nlp_state;
3154     spin_unlock_irq(&ndlp->lock);
3155     lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3156     return 0;
3157 
3158 err:
3159     spin_lock_irq(&ndlp->lock);
3160     ndlp->nlp_flag &= ~NLP_LOGO_SND;
3161     spin_unlock_irq(&ndlp->lock);
3162     return 1;
3163 }
3164 
3165 /**
3166  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
3167  * @phba: pointer to lpfc hba data structure.
3168  * @cmdiocb: pointer to lpfc command iocb data structure.
3169  * @rspiocb: pointer to lpfc response iocb data structure.
3170  *
3171  * This routine is a generic completion callback function for ELS commands.
3172  * Specifically, it is the callback function which does not need to perform
3173  * any command specific operations. It is currently used by the ELS command
3174  * issuing routines for RSCN, lpfc_issue_els_rscn, and the ELS Fibre Channel
3175  * Address Resolution Protocol Response (FARPR) routine, lpfc_issue_els_farpr().
3176  * Other than certain debug loggings, this callback function simply invokes the
3177  * lpfc_els_chk_latt() routine to check whether link went down during the
3178  * discovery process.
3179  **/
3180 static void
3181 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3182           struct lpfc_iocbq *rspiocb)
3183 {
3184     struct lpfc_vport *vport = cmdiocb->vport;
3185     struct lpfc_nodelist *free_ndlp;
3186     IOCB_t *irsp;
3187     u32 ulp_status, ulp_word4, tmo, did, iotag;
3188 
3189     ulp_status = get_job_ulpstatus(phba, rspiocb);
3190     ulp_word4 = get_job_word4(phba, rspiocb);
3191     did = get_job_els_rsp64_did(phba, cmdiocb);
3192 
3193     if (phba->sli_rev == LPFC_SLI_REV4) {
3194         tmo = get_wqe_tmo(cmdiocb);
3195         iotag = get_wqe_reqtag(cmdiocb);
3196     } else {
3197         irsp = &rspiocb->iocb;
3198         tmo = irsp->ulpTimeout;
3199         iotag = irsp->ulpIoTag;
3200     }
3201 
3202     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3203                   "ELS cmd cmpl:    status:x%x/x%x did:x%x",
3204                   ulp_status, ulp_word4, did);
3205 
3206     /* ELS cmd tag <ulpIoTag> completes */
3207     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3208              "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
3209              iotag, ulp_status, ulp_word4, tmo);
3210 
3211     /* Check to see if link went down during discovery */
3212     lpfc_els_chk_latt(vport);
3213 
3214     free_ndlp = cmdiocb->ndlp;
3215 
3216     lpfc_els_free_iocb(phba, cmdiocb);
3217     lpfc_nlp_put(free_ndlp);
3218 }
3219 
3220 /**
3221  * lpfc_reg_fab_ctrl_node - RPI register the fabric controller node.
3222  * @vport: pointer to lpfc_vport data structure.
3223  * @fc_ndlp: pointer to the fabric controller (0xfffffd) node.
3224  *
3225  * This routine registers the rpi assigned to the fabric controller
3226  * NPort_ID (0xfffffd) with the port and moves the node to UNMAPPED
3227  * state triggering a registration with the SCSI transport.
3228  *
3229  * This routine is single out because the fabric controller node
3230  * does not receive a PLOGI.  This routine is consumed by the
3231  * SCR and RDF ELS commands.  Callers are expected to qualify
3232  * with SLI4 first.
3233  **/
3234 static int
3235 lpfc_reg_fab_ctrl_node(struct lpfc_vport *vport, struct lpfc_nodelist *fc_ndlp)
3236 {
3237     int rc = 0;
3238     struct lpfc_hba *phba = vport->phba;
3239     struct lpfc_nodelist *ns_ndlp;
3240     LPFC_MBOXQ_t *mbox;
3241 
3242     if (fc_ndlp->nlp_flag & NLP_RPI_REGISTERED)
3243         return rc;
3244 
3245     ns_ndlp = lpfc_findnode_did(vport, NameServer_DID);
3246     if (!ns_ndlp)
3247         return -ENODEV;
3248 
3249     lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3250              "0935 %s: Reg FC RPI x%x on FC DID x%x NSSte: x%x\n",
3251              __func__, fc_ndlp->nlp_rpi, fc_ndlp->nlp_DID,
3252              ns_ndlp->nlp_state);
3253     if (ns_ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
3254         return -ENODEV;
3255 
3256     mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3257     if (!mbox) {
3258         lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3259                  "0936 %s: no memory for reg_login "
3260                  "Data: x%x x%x x%x x%x\n", __func__,
3261                  fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
3262                  fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
3263         return -ENOMEM;
3264     }
3265     rc = lpfc_reg_rpi(phba, vport->vpi, fc_ndlp->nlp_DID,
3266               (u8 *)&vport->fc_sparam, mbox, fc_ndlp->nlp_rpi);
3267     if (rc) {
3268         rc = -EACCES;
3269         goto out;
3270     }
3271 
3272     fc_ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
3273     mbox->mbox_cmpl = lpfc_mbx_cmpl_fc_reg_login;
3274     mbox->ctx_ndlp = lpfc_nlp_get(fc_ndlp);
3275     if (!mbox->ctx_ndlp) {
3276         rc = -ENOMEM;
3277         goto out;
3278     }
3279 
3280     mbox->vport = vport;
3281     rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3282     if (rc == MBX_NOT_FINISHED) {
3283         rc = -ENODEV;
3284         lpfc_nlp_put(fc_ndlp);
3285         goto out;
3286     }
3287     /* Success path. Exit. */
3288     lpfc_nlp_set_state(vport, fc_ndlp,
3289                NLP_STE_REG_LOGIN_ISSUE);
3290     return 0;
3291 
3292  out:
3293     lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
3294     lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3295              "0938 %s: failed to format reg_login "
3296              "Data: x%x x%x x%x x%x\n", __func__,
3297              fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
3298              fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
3299     return rc;
3300 }
3301 
3302 /**
3303  * lpfc_cmpl_els_disc_cmd - Completion callback function for Discovery ELS cmd
3304  * @phba: pointer to lpfc hba data structure.
3305  * @cmdiocb: pointer to lpfc command iocb data structure.
3306  * @rspiocb: pointer to lpfc response iocb data structure.
3307  *
3308  * This routine is a generic completion callback function for Discovery ELS cmd.
3309  * Currently used by the ELS command issuing routines for the ELS State Change
3310  * Request (SCR), lpfc_issue_els_scr() and the ELS RDF, lpfc_issue_els_rdf().
3311  * These commands will be retried once only for ELS timeout errors.
3312  **/
3313 static void
3314 lpfc_cmpl_els_disc_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3315                struct lpfc_iocbq *rspiocb)
3316 {
3317     struct lpfc_vport *vport = cmdiocb->vport;
3318     IOCB_t *irsp;
3319     struct lpfc_els_rdf_rsp *prdf;
3320     struct lpfc_dmabuf *pcmd, *prsp;
3321     u32 *pdata;
3322     u32 cmd;
3323     struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
3324     u32 ulp_status, ulp_word4, tmo, did, iotag;
3325 
3326     ulp_status = get_job_ulpstatus(phba, rspiocb);
3327     ulp_word4 = get_job_word4(phba, rspiocb);
3328     did = get_job_els_rsp64_did(phba, cmdiocb);
3329 
3330     if (phba->sli_rev == LPFC_SLI_REV4) {
3331         tmo = get_wqe_tmo(cmdiocb);
3332         iotag = get_wqe_reqtag(cmdiocb);
3333     } else {
3334         irsp = &rspiocb->iocb;
3335         tmo = irsp->ulpTimeout;
3336         iotag = irsp->ulpIoTag;
3337     }
3338 
3339     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3340         "ELS cmd cmpl:    status:x%x/x%x did:x%x",
3341         ulp_status, ulp_word4, did);
3342 
3343     /* ELS cmd tag <ulpIoTag> completes */
3344     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3345              "0217 ELS cmd tag x%x completes Data: x%x x%x x%x x%x\n",
3346              iotag, ulp_status, ulp_word4, tmo, cmdiocb->retry);
3347 
3348     pcmd = cmdiocb->cmd_dmabuf;
3349     if (!pcmd)
3350         goto out;
3351 
3352     pdata = (u32 *)pcmd->virt;
3353     if (!pdata)
3354         goto out;
3355     cmd = *pdata;
3356 
3357     /* Only 1 retry for ELS Timeout only */
3358     if (ulp_status == IOSTAT_LOCAL_REJECT &&
3359         ((ulp_word4 & IOERR_PARAM_MASK) ==
3360         IOERR_SEQUENCE_TIMEOUT)) {
3361         cmdiocb->retry++;
3362         if (cmdiocb->retry <= 1) {
3363             switch (cmd) {
3364             case ELS_CMD_SCR:
3365                 lpfc_issue_els_scr(vport, cmdiocb->retry);
3366                 break;
3367             case ELS_CMD_EDC:
3368                 lpfc_issue_els_edc(vport, cmdiocb->retry);
3369                 break;
3370             case ELS_CMD_RDF:
3371                 lpfc_issue_els_rdf(vport, cmdiocb->retry);
3372                 break;
3373             }
3374             goto out;
3375         }
3376         phba->fc_stat.elsRetryExceeded++;
3377     }
3378     if (cmd == ELS_CMD_EDC) {
3379         /* must be called before checking uplStatus and returning */
3380         lpfc_cmpl_els_edc(phba, cmdiocb, rspiocb);
3381         return;
3382     }
3383     if (ulp_status) {
3384         /* ELS discovery cmd completes with error */
3385         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS | LOG_CGN_MGMT,
3386                  "4203 ELS cmd x%x error: x%x x%X\n", cmd,
3387                  ulp_status, ulp_word4);
3388         goto out;
3389     }
3390 
3391     /* The RDF response doesn't have any impact on the running driver
3392      * but the notification descriptors are dumped here for support.
3393      */
3394     if (cmd == ELS_CMD_RDF) {
3395         int i;
3396 
3397         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
3398         if (!prsp)
3399             goto out;
3400 
3401         prdf = (struct lpfc_els_rdf_rsp *)prsp->virt;
3402         if (!prdf)
3403             goto out;
3404 
3405         for (i = 0; i < ELS_RDF_REG_TAG_CNT &&
3406                 i < be32_to_cpu(prdf->reg_d1.reg_desc.count); i++)
3407             lpfc_printf_vlog(vport, KERN_INFO,
3408                      LOG_ELS | LOG_CGN_MGMT,
3409                      "4677 Fabric RDF Notification Grant "
3410                      "Data: 0x%08x Reg: %x %x\n",
3411                      be32_to_cpu(
3412                          prdf->reg_d1.desc_tags[i]),
3413                      phba->cgn_reg_signal,
3414                      phba->cgn_reg_fpin);
3415     }
3416 
3417 out:
3418     /* Check to see if link went down during discovery */
3419     lpfc_els_chk_latt(vport);
3420     lpfc_els_free_iocb(phba, cmdiocb);
3421     lpfc_nlp_put(ndlp);
3422     return;
3423 }
3424 
3425 /**
3426  * lpfc_issue_els_scr - Issue a scr to an node on a vport
3427  * @vport: pointer to a host virtual N_Port data structure.
3428  * @retry: retry counter for the command IOCB.
3429  *
3430  * This routine issues a State Change Request (SCR) to a fabric node
3431  * on a @vport. The remote node is Fabric Controller (0xfffffd). It
3432  * first search the @vport node list to find the matching ndlp. If no such
3433  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
3434  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
3435  * routine is invoked to send the SCR IOCB.
3436  *
3437  * Note that the ndlp reference count will be incremented by 1 for holding the
3438  * ndlp and the reference to ndlp will be stored into the ndlp field of
3439  * the IOCB for the completion callback function to the SCR ELS command.
3440  *
3441  * Return code
3442  *   0 - Successfully issued scr command
3443  *   1 - Failed to issue scr command
3444  **/
3445 int
3446 lpfc_issue_els_scr(struct lpfc_vport *vport, uint8_t retry)
3447 {
3448     int rc = 0;
3449     struct lpfc_hba  *phba = vport->phba;
3450     struct lpfc_iocbq *elsiocb;
3451     uint8_t *pcmd;
3452     uint16_t cmdsize;
3453     struct lpfc_nodelist *ndlp;
3454 
3455     cmdsize = (sizeof(uint32_t) + sizeof(SCR));
3456 
3457     ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3458     if (!ndlp) {
3459         ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3460         if (!ndlp)
3461             return 1;
3462         lpfc_enqueue_node(vport, ndlp);
3463     }
3464 
3465     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3466                      ndlp->nlp_DID, ELS_CMD_SCR);
3467     if (!elsiocb)
3468         return 1;
3469 
3470     if (phba->sli_rev == LPFC_SLI_REV4) {
3471         rc = lpfc_reg_fab_ctrl_node(vport, ndlp);
3472         if (rc) {
3473             lpfc_els_free_iocb(phba, elsiocb);
3474             lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3475                      "0937 %s: Failed to reg fc node, rc %d\n",
3476                      __func__, rc);
3477             return 1;
3478         }
3479     }
3480     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
3481 
3482     *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
3483     pcmd += sizeof(uint32_t);
3484 
3485     /* For SCR, remainder of payload is SCR parameter page */
3486     memset(pcmd, 0, sizeof(SCR));
3487     ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
3488 
3489     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3490         "Issue SCR:       did:x%x",
3491         ndlp->nlp_DID, 0, 0);
3492 
3493     phba->fc_stat.elsXmitSCR++;
3494     elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
3495     elsiocb->ndlp = lpfc_nlp_get(ndlp);
3496     if (!elsiocb->ndlp) {
3497         lpfc_els_free_iocb(phba, elsiocb);
3498         return 1;
3499     }
3500 
3501     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3502                   "Issue SCR:     did:x%x refcnt %d",
3503                   ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3504 
3505     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3506     if (rc == IOCB_ERROR) {
3507         lpfc_els_free_iocb(phba, elsiocb);
3508         lpfc_nlp_put(ndlp);
3509         return 1;
3510     }
3511 
3512     return 0;
3513 }
3514 
3515 /**
3516  * lpfc_issue_els_rscn - Issue an RSCN to the Fabric Controller (Fabric)
3517  *   or the other nport (pt2pt).
3518  * @vport: pointer to a host virtual N_Port data structure.
3519  * @retry: number of retries to the command IOCB.
3520  *
3521  * This routine issues a RSCN to the Fabric Controller (DID 0xFFFFFD)
3522  *  when connected to a fabric, or to the remote port when connected
3523  *  in point-to-point mode. When sent to the Fabric Controller, it will
3524  *  replay the RSCN to registered recipients.
3525  *
3526  * Note that the ndlp reference count will be incremented by 1 for holding the
3527  * ndlp and the reference to ndlp will be stored into the ndlp field of
3528  * the IOCB for the completion callback function to the RSCN ELS command.
3529  *
3530  * Return code
3531  *   0 - Successfully issued RSCN command
3532  *   1 - Failed to issue RSCN command
3533  **/
3534 int
3535 lpfc_issue_els_rscn(struct lpfc_vport *vport, uint8_t retry)
3536 {
3537     int rc = 0;
3538     struct lpfc_hba *phba = vport->phba;
3539     struct lpfc_iocbq *elsiocb;
3540     struct lpfc_nodelist *ndlp;
3541     struct {
3542         struct fc_els_rscn rscn;
3543         struct fc_els_rscn_page portid;
3544     } *event;
3545     uint32_t nportid;
3546     uint16_t cmdsize = sizeof(*event);
3547 
3548     /* Not supported for private loop */
3549     if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
3550         !(vport->fc_flag & FC_PUBLIC_LOOP))
3551         return 1;
3552 
3553     if (vport->fc_flag & FC_PT2PT) {
3554         /* find any mapped nport - that would be the other nport */
3555         ndlp = lpfc_findnode_mapped(vport);
3556         if (!ndlp)
3557             return 1;
3558     } else {
3559         nportid = FC_FID_FCTRL;
3560         /* find the fabric controller node */
3561         ndlp = lpfc_findnode_did(vport, nportid);
3562         if (!ndlp) {
3563             /* if one didn't exist, make one */
3564             ndlp = lpfc_nlp_init(vport, nportid);
3565             if (!ndlp)
3566                 return 1;
3567             lpfc_enqueue_node(vport, ndlp);
3568         }
3569     }
3570 
3571     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3572                      ndlp->nlp_DID, ELS_CMD_RSCN_XMT);
3573 
3574     if (!elsiocb)
3575         return 1;
3576 
3577     event = elsiocb->cmd_dmabuf->virt;
3578 
3579     event->rscn.rscn_cmd = ELS_RSCN;
3580     event->rscn.rscn_page_len = sizeof(struct fc_els_rscn_page);
3581     event->rscn.rscn_plen = cpu_to_be16(cmdsize);
3582 
3583     nportid = vport->fc_myDID;
3584     /* appears that page flags must be 0 for fabric to broadcast RSCN */
3585     event->portid.rscn_page_flags = 0;
3586     event->portid.rscn_fid[0] = (nportid & 0x00FF0000) >> 16;
3587     event->portid.rscn_fid[1] = (nportid & 0x0000FF00) >> 8;
3588     event->portid.rscn_fid[2] = nportid & 0x000000FF;
3589 
3590     phba->fc_stat.elsXmitRSCN++;
3591     elsiocb->cmd_cmpl = lpfc_cmpl_els_cmd;
3592     elsiocb->ndlp = lpfc_nlp_get(ndlp);
3593     if (!elsiocb->ndlp) {
3594         lpfc_els_free_iocb(phba, elsiocb);
3595         return 1;
3596     }
3597 
3598     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3599                   "Issue RSCN:       did:x%x",
3600                   ndlp->nlp_DID, 0, 0);
3601 
3602     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3603     if (rc == IOCB_ERROR) {
3604         lpfc_els_free_iocb(phba, elsiocb);
3605         lpfc_nlp_put(ndlp);
3606         return 1;
3607     }
3608 
3609     return 0;
3610 }
3611 
3612 /**
3613  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
3614  * @vport: pointer to a host virtual N_Port data structure.
3615  * @nportid: N_Port identifier to the remote node.
3616  * @retry: number of retries to the command IOCB.
3617  *
3618  * This routine issues a Fibre Channel Address Resolution Response
3619  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
3620  * is passed into the function. It first search the @vport node list to find
3621  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
3622  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
3623  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
3624  *
3625  * Note that the ndlp reference count will be incremented by 1 for holding the
3626  * ndlp and the reference to ndlp will be stored into the ndlp field of
3627  * the IOCB for the completion callback function to the FARPR ELS command.
3628  *
3629  * Return code
3630  *   0 - Successfully issued farpr command
3631  *   1 - Failed to issue farpr command
3632  **/
3633 static int
3634 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
3635 {
3636     int rc = 0;
3637     struct lpfc_hba  *phba = vport->phba;
3638     struct lpfc_iocbq *elsiocb;
3639     FARP *fp;
3640     uint8_t *pcmd;
3641     uint32_t *lp;
3642     uint16_t cmdsize;
3643     struct lpfc_nodelist *ondlp;
3644     struct lpfc_nodelist *ndlp;
3645 
3646     cmdsize = (sizeof(uint32_t) + sizeof(FARP));
3647 
3648     ndlp = lpfc_findnode_did(vport, nportid);
3649     if (!ndlp) {
3650         ndlp = lpfc_nlp_init(vport, nportid);
3651         if (!ndlp)
3652             return 1;
3653         lpfc_enqueue_node(vport, ndlp);
3654     }
3655 
3656     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3657                      ndlp->nlp_DID, ELS_CMD_FARPR);
3658     if (!elsiocb)
3659         return 1;
3660 
3661     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
3662 
3663     *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
3664     pcmd += sizeof(uint32_t);
3665 
3666     /* Fill in FARPR payload */
3667     fp = (FARP *) (pcmd);
3668     memset(fp, 0, sizeof(FARP));
3669     lp = (uint32_t *) pcmd;
3670     *lp++ = be32_to_cpu(nportid);
3671     *lp++ = be32_to_cpu(vport->fc_myDID);
3672     fp->Rflags = 0;
3673     fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
3674 
3675     memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
3676     memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3677     ondlp = lpfc_findnode_did(vport, nportid);
3678     if (ondlp) {
3679         memcpy(&fp->OportName, &ondlp->nlp_portname,
3680                sizeof(struct lpfc_name));
3681         memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
3682                sizeof(struct lpfc_name));
3683     }
3684 
3685     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3686         "Issue FARPR:     did:x%x",
3687         ndlp->nlp_DID, 0, 0);
3688 
3689     phba->fc_stat.elsXmitFARPR++;
3690     elsiocb->cmd_cmpl = lpfc_cmpl_els_cmd;
3691     elsiocb->ndlp = lpfc_nlp_get(ndlp);
3692     if (!elsiocb->ndlp) {
3693         lpfc_els_free_iocb(phba, elsiocb);
3694         return 1;
3695     }
3696 
3697     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3698     if (rc == IOCB_ERROR) {
3699         /* The additional lpfc_nlp_put will cause the following
3700          * lpfc_els_free_iocb routine to trigger the release of
3701          * the node.
3702          */
3703         lpfc_els_free_iocb(phba, elsiocb);
3704         lpfc_nlp_put(ndlp);
3705         return 1;
3706     }
3707     /* This will cause the callback-function lpfc_cmpl_els_cmd to
3708      * trigger the release of the node.
3709      */
3710     /* Don't release reference count as RDF is likely outstanding */
3711     return 0;
3712 }
3713 
3714 /**
3715  * lpfc_issue_els_rdf - Register for diagnostic functions from the fabric.
3716  * @vport: pointer to a host virtual N_Port data structure.
3717  * @retry: retry counter for the command IOCB.
3718  *
3719  * This routine issues an ELS RDF to the Fabric Controller to register
3720  * for diagnostic functions.
3721  *
3722  * Note that the ndlp reference count will be incremented by 1 for holding the
3723  * ndlp and the reference to ndlp will be stored into the ndlp field of
3724  * the IOCB for the completion callback function to the RDF ELS command.
3725  *
3726  * Return code
3727  *   0 - Successfully issued rdf command
3728  *   1 - Failed to issue rdf command
3729  **/
3730 int
3731 lpfc_issue_els_rdf(struct lpfc_vport *vport, uint8_t retry)
3732 {
3733     struct lpfc_hba *phba = vport->phba;
3734     struct lpfc_iocbq *elsiocb;
3735     struct lpfc_els_rdf_req *prdf;
3736     struct lpfc_nodelist *ndlp;
3737     uint16_t cmdsize;
3738     int rc;
3739 
3740     cmdsize = sizeof(*prdf);
3741 
3742     ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3743     if (!ndlp) {
3744         ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3745         if (!ndlp)
3746             return -ENODEV;
3747         lpfc_enqueue_node(vport, ndlp);
3748     }
3749 
3750     /* RDF ELS is not required on an NPIV VN_Port. */
3751     if (vport->port_type == LPFC_NPIV_PORT)
3752         return -EACCES;
3753 
3754     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3755                      ndlp->nlp_DID, ELS_CMD_RDF);
3756     if (!elsiocb)
3757         return -ENOMEM;
3758 
3759     /* Configure the payload for the supported FPIN events. */
3760     prdf = (struct lpfc_els_rdf_req *)elsiocb->cmd_dmabuf->virt;
3761     memset(prdf, 0, cmdsize);
3762     prdf->rdf.fpin_cmd = ELS_RDF;
3763     prdf->rdf.desc_len = cpu_to_be32(sizeof(struct lpfc_els_rdf_req) -
3764                      sizeof(struct fc_els_rdf));
3765     prdf->reg_d1.reg_desc.desc_tag = cpu_to_be32(ELS_DTAG_FPIN_REGISTER);
3766     prdf->reg_d1.reg_desc.desc_len = cpu_to_be32(
3767                 FC_TLV_DESC_LENGTH_FROM_SZ(prdf->reg_d1));
3768     prdf->reg_d1.reg_desc.count = cpu_to_be32(ELS_RDF_REG_TAG_CNT);
3769     prdf->reg_d1.desc_tags[0] = cpu_to_be32(ELS_DTAG_LNK_INTEGRITY);
3770     prdf->reg_d1.desc_tags[1] = cpu_to_be32(ELS_DTAG_DELIVERY);
3771     prdf->reg_d1.desc_tags[2] = cpu_to_be32(ELS_DTAG_PEER_CONGEST);
3772     prdf->reg_d1.desc_tags[3] = cpu_to_be32(ELS_DTAG_CONGESTION);
3773 
3774     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3775              "6444 Xmit RDF to remote NPORT x%x Reg: %x %x\n",
3776              ndlp->nlp_DID, phba->cgn_reg_signal,
3777              phba->cgn_reg_fpin);
3778 
3779     phba->cgn_fpin_frequency = LPFC_FPIN_INIT_FREQ;
3780     elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
3781     elsiocb->ndlp = lpfc_nlp_get(ndlp);
3782     if (!elsiocb->ndlp) {
3783         lpfc_els_free_iocb(phba, elsiocb);
3784         return -EIO;
3785     }
3786 
3787     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3788                   "Issue RDF:     did:x%x refcnt %d",
3789                   ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3790 
3791     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3792     if (rc == IOCB_ERROR) {
3793         lpfc_els_free_iocb(phba, elsiocb);
3794         lpfc_nlp_put(ndlp);
3795         return -EIO;
3796     }
3797     return 0;
3798 }
3799 
3800  /**
3801   * lpfc_els_rcv_rdf - Receive RDF ELS request from the fabric.
3802   * @vport: pointer to a host virtual N_Port data structure.
3803   * @cmdiocb: pointer to lpfc command iocb data structure.
3804   * @ndlp: pointer to a node-list data structure.
3805   *
3806   * A received RDF implies a possible change to fabric supported diagnostic
3807   * functions.  This routine sends LS_ACC and then has the Nx_Port issue a new
3808   * RDF request to reregister for supported diagnostic functions.
3809   *
3810   * Return code
3811   *   0 - Success
3812   *   -EIO - Failed to process received RDF
3813   **/
3814 static int
3815 lpfc_els_rcv_rdf(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3816          struct lpfc_nodelist *ndlp)
3817 {
3818     /* Send LS_ACC */
3819     if (lpfc_els_rsp_acc(vport, ELS_CMD_RDF, cmdiocb, ndlp, NULL)) {
3820         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3821                  "1623 Failed to RDF_ACC from x%x for x%x\n",
3822                  ndlp->nlp_DID, vport->fc_myDID);
3823         return -EIO;
3824     }
3825 
3826     /* Issue new RDF for reregistering */
3827     if (lpfc_issue_els_rdf(vport, 0)) {
3828         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3829                  "2623 Failed to re register RDF for x%x\n",
3830                  vport->fc_myDID);
3831         return -EIO;
3832     }
3833 
3834     return 0;
3835 }
3836 
3837 /**
3838  * lpfc_least_capable_settings - helper function for EDC rsp processing
3839  * @phba: pointer to lpfc hba data structure.
3840  * @pcgd: pointer to congestion detection descriptor in EDC rsp.
3841  *
3842  * This helper routine determines the least capable setting for
3843  * congestion signals, signal freq, including scale, from the
3844  * congestion detection descriptor in the EDC rsp.  The routine
3845  * sets @phba values in preparation for a set_featues mailbox.
3846  **/
3847 static void
3848 lpfc_least_capable_settings(struct lpfc_hba *phba,
3849                 struct fc_diag_cg_sig_desc *pcgd)
3850 {
3851     u32 rsp_sig_cap = 0, drv_sig_cap = 0;
3852     u32 rsp_sig_freq_cyc = 0, rsp_sig_freq_scale = 0;
3853 
3854     /* Get rsp signal and frequency capabilities.  */
3855     rsp_sig_cap = be32_to_cpu(pcgd->xmt_signal_capability);
3856     rsp_sig_freq_cyc = be16_to_cpu(pcgd->xmt_signal_frequency.count);
3857     rsp_sig_freq_scale = be16_to_cpu(pcgd->xmt_signal_frequency.units);
3858 
3859     /* If the Fport does not support signals. Set FPIN only */
3860     if (rsp_sig_cap == EDC_CG_SIG_NOTSUPPORTED)
3861         goto out_no_support;
3862 
3863     /* Apply the xmt scale to the xmt cycle to get the correct frequency.
3864      * Adapter default is 100 millisSeconds.  Convert all xmt cycle values
3865      * to milliSeconds.
3866      */
3867     switch (rsp_sig_freq_scale) {
3868     case EDC_CG_SIGFREQ_SEC:
3869         rsp_sig_freq_cyc *= MSEC_PER_SEC;
3870         break;
3871     case EDC_CG_SIGFREQ_MSEC:
3872         rsp_sig_freq_cyc = 1;
3873         break;
3874     default:
3875         goto out_no_support;
3876     }
3877 
3878     /* Convenient shorthand. */
3879     drv_sig_cap = phba->cgn_reg_signal;
3880 
3881     /* Choose the least capable frequency. */
3882     if (rsp_sig_freq_cyc > phba->cgn_sig_freq)
3883         phba->cgn_sig_freq = rsp_sig_freq_cyc;
3884 
3885     /* Should be some common signals support. Settle on least capable
3886      * signal and adjust FPIN values. Initialize defaults to ease the
3887      * decision.
3888      */
3889     phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
3890     phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
3891     if (rsp_sig_cap == EDC_CG_SIG_WARN_ONLY &&
3892         (drv_sig_cap == EDC_CG_SIG_WARN_ONLY ||
3893          drv_sig_cap == EDC_CG_SIG_WARN_ALARM)) {
3894         phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
3895         phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
3896     }
3897     if (rsp_sig_cap == EDC_CG_SIG_WARN_ALARM) {
3898         if (drv_sig_cap == EDC_CG_SIG_WARN_ALARM) {
3899             phba->cgn_reg_signal = EDC_CG_SIG_WARN_ALARM;
3900             phba->cgn_reg_fpin = LPFC_CGN_FPIN_NONE;
3901         }
3902         if (drv_sig_cap == EDC_CG_SIG_WARN_ONLY) {
3903             phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
3904             phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
3905         }
3906     }
3907 
3908     /* We are NOT recording signal frequency in congestion info buffer */
3909     return;
3910 
3911 out_no_support:
3912     phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
3913     phba->cgn_sig_freq = 0;
3914     phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
3915 }
3916 
3917 DECLARE_ENUM2STR_LOOKUP(lpfc_get_tlv_dtag_nm, fc_ls_tlv_dtag,
3918             FC_LS_TLV_DTAG_INIT);
3919 
3920 /**
3921  * lpfc_cmpl_els_edc - Completion callback function for EDC
3922  * @phba: pointer to lpfc hba data structure.
3923  * @cmdiocb: pointer to lpfc command iocb data structure.
3924  * @rspiocb: pointer to lpfc response iocb data structure.
3925  *
3926  * This routine is the completion callback function for issuing the Exchange
3927  * Diagnostic Capabilities (EDC) command. The driver issues an EDC to
3928  * notify the FPort of its Congestion and Link Fault capabilities.  This
3929  * routine parses the FPort's response and decides on the least common
3930  * values applicable to both FPort and NPort for Warnings and Alarms that
3931  * are communicated via hardware signals.
3932  **/
3933 static void
3934 lpfc_cmpl_els_edc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3935           struct lpfc_iocbq *rspiocb)
3936 {
3937     IOCB_t *irsp_iocb;
3938     struct fc_els_edc_resp *edc_rsp;
3939     struct fc_tlv_desc *tlv;
3940     struct fc_diag_cg_sig_desc *pcgd;
3941     struct fc_diag_lnkflt_desc *plnkflt;
3942     struct lpfc_dmabuf *pcmd, *prsp;
3943     const char *dtag_nm;
3944     u32 *pdata, dtag;
3945     int desc_cnt = 0, bytes_remain;
3946     bool rcv_cap_desc = false;
3947     struct lpfc_nodelist *ndlp;
3948     u32 ulp_status, ulp_word4, tmo, did, iotag;
3949 
3950     ndlp = cmdiocb->ndlp;
3951 
3952     ulp_status = get_job_ulpstatus(phba, rspiocb);
3953     ulp_word4 = get_job_word4(phba, rspiocb);
3954     did = get_job_els_rsp64_did(phba, rspiocb);
3955 
3956     if (phba->sli_rev == LPFC_SLI_REV4) {
3957         tmo = get_wqe_tmo(rspiocb);
3958         iotag = get_wqe_reqtag(rspiocb);
3959     } else {
3960         irsp_iocb = &rspiocb->iocb;
3961         tmo = irsp_iocb->ulpTimeout;
3962         iotag = irsp_iocb->ulpIoTag;
3963     }
3964 
3965     lpfc_debugfs_disc_trc(phba->pport, LPFC_DISC_TRC_ELS_CMD,
3966                   "EDC cmpl:    status:x%x/x%x did:x%x",
3967                   ulp_status, ulp_word4, did);
3968 
3969     /* ELS cmd tag <ulpIoTag> completes */
3970     lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3971             "4201 EDC cmd tag x%x completes Data: x%x x%x x%x\n",
3972             iotag, ulp_status, ulp_word4, tmo);
3973 
3974     pcmd = cmdiocb->cmd_dmabuf;
3975     if (!pcmd)
3976         goto out;
3977 
3978     pdata = (u32 *)pcmd->virt;
3979     if (!pdata)
3980         goto out;
3981 
3982     /* Need to clear signal values, send features MB and RDF with FPIN. */
3983     if (ulp_status)
3984         goto out;
3985 
3986     prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
3987     if (!prsp)
3988         goto out;
3989 
3990     edc_rsp = prsp->virt;
3991     if (!edc_rsp)
3992         goto out;
3993 
3994     /* ELS cmd tag <ulpIoTag> completes */
3995     lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3996             "4676 Fabric EDC Rsp: "
3997             "0x%02x, 0x%08x\n",
3998             edc_rsp->acc_hdr.la_cmd,
3999             be32_to_cpu(edc_rsp->desc_list_len));
4000 
4001     /*
4002      * Payload length in bytes is the response descriptor list
4003      * length minus the 12 bytes of Link Service Request
4004      * Information descriptor in the reply.
4005      */
4006     bytes_remain = be32_to_cpu(edc_rsp->desc_list_len) -
4007                    sizeof(struct fc_els_lsri_desc);
4008     if (bytes_remain <= 0)
4009         goto out;
4010 
4011     tlv = edc_rsp->desc;
4012 
4013     /*
4014      * cycle through EDC diagnostic descriptors to find the
4015      * congestion signaling capability descriptor
4016      */
4017     while (bytes_remain) {
4018         if (bytes_remain < FC_TLV_DESC_HDR_SZ) {
4019             lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
4020                     "6461 Truncated TLV hdr on "
4021                     "Diagnostic descriptor[%d]\n",
4022                     desc_cnt);
4023             goto out;
4024         }
4025 
4026         dtag = be32_to_cpu(tlv->desc_tag);
4027         switch (dtag) {
4028         case ELS_DTAG_LNK_FAULT_CAP:
4029             if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
4030                 FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
4031                     sizeof(struct fc_diag_lnkflt_desc)) {
4032                 lpfc_printf_log(
4033                     phba, KERN_WARNING, LOG_CGN_MGMT,
4034                     "6462 Truncated Link Fault Diagnostic "
4035                     "descriptor[%d]: %d vs 0x%zx 0x%zx\n",
4036                     desc_cnt, bytes_remain,
4037                     FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
4038                     sizeof(struct fc_diag_cg_sig_desc));
4039                 goto out;
4040             }
4041             plnkflt = (struct fc_diag_lnkflt_desc *)tlv;
4042             lpfc_printf_log(
4043                 phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
4044                 "4617 Link Fault Desc Data: 0x%08x 0x%08x "
4045                 "0x%08x 0x%08x 0x%08x\n",
4046                 be32_to_cpu(plnkflt->desc_tag),
4047                 be32_to_cpu(plnkflt->desc_len),
4048                 be32_to_cpu(
4049                     plnkflt->degrade_activate_threshold),
4050                 be32_to_cpu(
4051                     plnkflt->degrade_deactivate_threshold),
4052                 be32_to_cpu(plnkflt->fec_degrade_interval));
4053             break;
4054         case ELS_DTAG_CG_SIGNAL_CAP:
4055             if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
4056                 FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
4057                     sizeof(struct fc_diag_cg_sig_desc)) {
4058                 lpfc_printf_log(
4059                     phba, KERN_WARNING, LOG_CGN_MGMT,
4060                     "6463 Truncated Cgn Signal Diagnostic "
4061                     "descriptor[%d]: %d vs 0x%zx 0x%zx\n",
4062                     desc_cnt, bytes_remain,
4063                     FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
4064                     sizeof(struct fc_diag_cg_sig_desc));
4065                 goto out;
4066             }
4067 
4068             pcgd = (struct fc_diag_cg_sig_desc *)tlv;
4069             lpfc_printf_log(
4070                 phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
4071                 "4616 CGN Desc Data: 0x%08x 0x%08x "
4072                 "0x%08x 0x%04x 0x%04x 0x%08x 0x%04x 0x%04x\n",
4073                 be32_to_cpu(pcgd->desc_tag),
4074                 be32_to_cpu(pcgd->desc_len),
4075                 be32_to_cpu(pcgd->xmt_signal_capability),
4076                 be16_to_cpu(pcgd->xmt_signal_frequency.count),
4077                 be16_to_cpu(pcgd->xmt_signal_frequency.units),
4078                 be32_to_cpu(pcgd->rcv_signal_capability),
4079                 be16_to_cpu(pcgd->rcv_signal_frequency.count),
4080                 be16_to_cpu(pcgd->rcv_signal_frequency.units));
4081 
4082             /* Compare driver and Fport capabilities and choose
4083              * least common.
4084              */
4085             lpfc_least_capable_settings(phba, pcgd);
4086             rcv_cap_desc = true;
4087             break;
4088         default:
4089             dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
4090             lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
4091                     "4919 unknown Diagnostic "
4092                     "Descriptor[%d]: tag x%x (%s)\n",
4093                     desc_cnt, dtag, dtag_nm);
4094         }
4095 
4096         bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
4097         tlv = fc_tlv_next_desc(tlv);
4098         desc_cnt++;
4099     }
4100 
4101 out:
4102     if (!rcv_cap_desc) {
4103         phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
4104         phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
4105         phba->cgn_sig_freq = 0;
4106         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_CGN_MGMT,
4107                 "4202 EDC rsp error - sending RDF "
4108                 "for FPIN only.\n");
4109     }
4110 
4111     lpfc_config_cgn_signal(phba);
4112 
4113     /* Check to see if link went down during discovery */
4114     lpfc_els_chk_latt(phba->pport);
4115     lpfc_debugfs_disc_trc(phba->pport, LPFC_DISC_TRC_ELS_CMD,
4116                   "EDC Cmpl:     did:x%x refcnt %d",
4117                   ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
4118     lpfc_els_free_iocb(phba, cmdiocb);
4119     lpfc_nlp_put(ndlp);
4120 }
4121 
4122 static void
4123 lpfc_format_edc_cgn_desc(struct lpfc_hba *phba, struct fc_diag_cg_sig_desc *cgd)
4124 {
4125     /* We are assuming cgd was zero'ed before calling this routine */
4126 
4127     /* Configure the congestion detection capability */
4128     cgd->desc_tag = cpu_to_be32(ELS_DTAG_CG_SIGNAL_CAP);
4129 
4130     /* Descriptor len doesn't include the tag or len fields. */
4131     cgd->desc_len = cpu_to_be32(
4132         FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_diag_cg_sig_desc));
4133 
4134     /* xmt_signal_capability already set to EDC_CG_SIG_NOTSUPPORTED.
4135      * xmt_signal_frequency.count already set to 0.
4136      * xmt_signal_frequency.units already set to 0.
4137      */
4138 
4139     if (phba->cmf_active_mode == LPFC_CFG_OFF) {
4140         /* rcv_signal_capability already set to EDC_CG_SIG_NOTSUPPORTED.
4141          * rcv_signal_frequency.count already set to 0.
4142          * rcv_signal_frequency.units already set to 0.
4143          */
4144         phba->cgn_sig_freq = 0;
4145         return;
4146     }
4147     switch (phba->cgn_reg_signal) {
4148     case EDC_CG_SIG_WARN_ONLY:
4149         cgd->rcv_signal_capability = cpu_to_be32(EDC_CG_SIG_WARN_ONLY);
4150         break;
4151     case EDC_CG_SIG_WARN_ALARM:
4152         cgd->rcv_signal_capability = cpu_to_be32(EDC_CG_SIG_WARN_ALARM);
4153         break;
4154     default:
4155         /* rcv_signal_capability left 0 thus no support */
4156         break;
4157     }
4158 
4159     /* We start negotiation with lpfc_fabric_cgn_frequency, after
4160      * the completion we settle on the higher frequency.
4161      */
4162     cgd->rcv_signal_frequency.count =
4163         cpu_to_be16(lpfc_fabric_cgn_frequency);
4164     cgd->rcv_signal_frequency.units =
4165         cpu_to_be16(EDC_CG_SIGFREQ_MSEC);
4166 }
4167 
4168  /**
4169   * lpfc_issue_els_edc - Exchange Diagnostic Capabilities with the fabric.
4170   * @vport: pointer to a host virtual N_Port data structure.
4171   * @retry: retry counter for the command iocb.
4172   *
4173   * This routine issues an ELS EDC to the F-Port Controller to communicate
4174   * this N_Port's support of hardware signals in its Congestion
4175   * Capabilities Descriptor.
4176   *
4177   * Note: This routine does not check if one or more signals are
4178   * set in the cgn_reg_signal parameter.  The caller makes the
4179   * decision to enforce cgn_reg_signal as nonzero or zero depending
4180   * on the conditions.  During Fabric requests, the driver
4181   * requires cgn_reg_signals to be nonzero.  But a dynamic request
4182   * to set the congestion mode to OFF from Monitor or Manage
4183   * would correctly issue an EDC with no signals enabled to
4184   * turn off switch functionality and then update the FW.
4185   *
4186   * Return code
4187   *   0 - Successfully issued edc command
4188   *   1 - Failed to issue edc command
4189   **/
4190 int
4191 lpfc_issue_els_edc(struct lpfc_vport *vport, uint8_t retry)
4192 {
4193     struct lpfc_hba  *phba = vport->phba;
4194     struct lpfc_iocbq *elsiocb;
4195     struct lpfc_els_edc_req *edc_req;
4196     struct fc_diag_cg_sig_desc *cgn_desc;
4197     u16 cmdsize;
4198     struct lpfc_nodelist *ndlp;
4199     u8 *pcmd = NULL;
4200     u32 edc_req_size, cgn_desc_size;
4201     int rc;
4202 
4203     if (vport->port_type == LPFC_NPIV_PORT)
4204         return -EACCES;
4205 
4206     ndlp = lpfc_findnode_did(vport, Fabric_DID);
4207     if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
4208         return -ENODEV;
4209 
4210     /* If HBA doesn't support signals, drop into RDF */
4211     if (!phba->cgn_init_reg_signal)
4212         goto try_rdf;
4213 
4214     edc_req_size = sizeof(struct fc_els_edc);
4215     cgn_desc_size = sizeof(struct fc_diag_cg_sig_desc);
4216     cmdsize = edc_req_size + cgn_desc_size;
4217     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
4218                      ndlp->nlp_DID, ELS_CMD_EDC);
4219     if (!elsiocb)
4220         goto try_rdf;
4221 
4222     /* Configure the payload for the supported Diagnostics capabilities. */
4223     pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
4224     memset(pcmd, 0, cmdsize);
4225     edc_req = (struct lpfc_els_edc_req *)pcmd;
4226     edc_req->edc.desc_len = cpu_to_be32(cgn_desc_size);
4227     edc_req->edc.edc_cmd = ELS_EDC;
4228 
4229     cgn_desc = &edc_req->cgn_desc;
4230 
4231     lpfc_format_edc_cgn_desc(phba, cgn_desc);
4232 
4233     phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
4234 
4235     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
4236              "4623 Xmit EDC to remote "
4237              "NPORT x%x reg_sig x%x reg_fpin:x%x\n",
4238              ndlp->nlp_DID, phba->cgn_reg_signal,
4239              phba->cgn_reg_fpin);
4240 
4241     elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
4242     elsiocb->ndlp = lpfc_nlp_get(ndlp);
4243     if (!elsiocb->ndlp) {
4244         lpfc_els_free_iocb(phba, elsiocb);
4245         return -EIO;
4246     }
4247 
4248     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4249                   "Issue EDC:     did:x%x refcnt %d",
4250                   ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
4251     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4252     if (rc == IOCB_ERROR) {
4253         /* The additional lpfc_nlp_put will cause the following
4254          * lpfc_els_free_iocb routine to trigger the rlease of
4255          * the node.
4256          */
4257         lpfc_els_free_iocb(phba, elsiocb);
4258         lpfc_nlp_put(ndlp);
4259         goto try_rdf;
4260     }
4261     return 0;
4262 try_rdf:
4263     phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
4264     phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
4265     rc = lpfc_issue_els_rdf(vport, 0);
4266     return rc;
4267 }
4268 
4269 /**
4270  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
4271  * @vport: pointer to a host virtual N_Port data structure.
4272  * @nlp: pointer to a node-list data structure.
4273  *
4274  * This routine cancels the timer with a delayed IOCB-command retry for
4275  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
4276  * removes the ELS retry event if it presents. In addition, if the
4277  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
4278  * commands are sent for the @vport's nodes that require issuing discovery
4279  * ADISC.
4280  **/
4281 void
4282 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
4283 {
4284     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4285     struct lpfc_work_evt *evtp;
4286 
4287     if (!(nlp->nlp_flag & NLP_DELAY_TMO))
4288         return;
4289     spin_lock_irq(&nlp->lock);
4290     nlp->nlp_flag &= ~NLP_DELAY_TMO;
4291     spin_unlock_irq(&nlp->lock);
4292     del_timer_sync(&nlp->nlp_delayfunc);
4293     nlp->nlp_last_elscmd = 0;
4294     if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
4295         list_del_init(&nlp->els_retry_evt.evt_listp);
4296         /* Decrement nlp reference count held for the delayed retry */
4297         evtp = &nlp->els_retry_evt;
4298         lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
4299     }
4300     if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
4301         spin_lock_irq(&nlp->lock);
4302         nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
4303         spin_unlock_irq(&nlp->lock);
4304         if (vport->num_disc_nodes) {
4305             if (vport->port_state < LPFC_VPORT_READY) {
4306                 /* Check if there are more ADISCs to be sent */
4307                 lpfc_more_adisc(vport);
4308             } else {
4309                 /* Check if there are more PLOGIs to be sent */
4310                 lpfc_more_plogi(vport);
4311                 if (vport->num_disc_nodes == 0) {
4312                     spin_lock_irq(shost->host_lock);
4313                     vport->fc_flag &= ~FC_NDISC_ACTIVE;
4314                     spin_unlock_irq(shost->host_lock);
4315                     lpfc_can_disctmo(vport);
4316                     lpfc_end_rscn(vport);
4317                 }
4318             }
4319         }
4320     }
4321     return;
4322 }
4323 
4324 /**
4325  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
4326  * @t: pointer to the timer function associated data (ndlp).
4327  *
4328  * This routine is invoked by the ndlp delayed-function timer to check
4329  * whether there is any pending ELS retry event(s) with the node. If not, it
4330  * simply returns. Otherwise, if there is at least one ELS delayed event, it
4331  * adds the delayed events to the HBA work list and invokes the
4332  * lpfc_worker_wake_up() routine to wake up worker thread to process the
4333  * event. Note that lpfc_nlp_get() is called before posting the event to
4334  * the work list to hold reference count of ndlp so that it guarantees the
4335  * reference to ndlp will still be available when the worker thread gets
4336  * to the event associated with the ndlp.
4337  **/
4338 void
4339 lpfc_els_retry_delay(struct timer_list *t)
4340 {
4341     struct lpfc_nodelist *ndlp = from_timer(ndlp, t, nlp_delayfunc);
4342     struct lpfc_vport *vport = ndlp->vport;
4343     struct lpfc_hba   *phba = vport->phba;
4344     unsigned long flags;
4345     struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
4346 
4347     spin_lock_irqsave(&phba->hbalock, flags);
4348     if (!list_empty(&evtp->evt_listp)) {
4349         spin_unlock_irqrestore(&phba->hbalock, flags);
4350         return;
4351     }
4352 
4353     /* We need to hold the node by incrementing the reference
4354      * count until the queued work is done
4355      */
4356     evtp->evt_arg1  = lpfc_nlp_get(ndlp);
4357     if (evtp->evt_arg1) {
4358         evtp->evt = LPFC_EVT_ELS_RETRY;
4359         list_add_tail(&evtp->evt_listp, &phba->work_list);
4360         lpfc_worker_wake_up(phba);
4361     }
4362     spin_unlock_irqrestore(&phba->hbalock, flags);
4363     return;
4364 }
4365 
4366 /**
4367  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
4368  * @ndlp: pointer to a node-list data structure.
4369  *
4370  * This routine is the worker-thread handler for processing the @ndlp delayed
4371  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
4372  * the last ELS command from the associated ndlp and invokes the proper ELS
4373  * function according to the delayed ELS command to retry the command.
4374  **/
4375 void
4376 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
4377 {
4378     struct lpfc_vport *vport = ndlp->vport;
4379     uint32_t cmd, retry;
4380 
4381     spin_lock_irq(&ndlp->lock);
4382     cmd = ndlp->nlp_last_elscmd;
4383     ndlp->nlp_last_elscmd = 0;
4384 
4385     if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
4386         spin_unlock_irq(&ndlp->lock);
4387         return;
4388     }
4389 
4390     ndlp->nlp_flag &= ~NLP_DELAY_TMO;
4391     spin_unlock_irq(&ndlp->lock);
4392     /*
4393      * If a discovery event readded nlp_delayfunc after timer
4394      * firing and before processing the timer, cancel the
4395      * nlp_delayfunc.
4396      */
4397     del_timer_sync(&ndlp->nlp_delayfunc);
4398     retry = ndlp->nlp_retry;
4399     ndlp->nlp_retry = 0;
4400 
4401     switch (cmd) {
4402     case ELS_CMD_FLOGI:
4403         lpfc_issue_els_flogi(vport, ndlp, retry);
4404         break;
4405     case ELS_CMD_PLOGI:
4406         if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
4407             ndlp->nlp_prev_state = ndlp->nlp_state;
4408             lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4409         }
4410         break;
4411     case ELS_CMD_ADISC:
4412         if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
4413             ndlp->nlp_prev_state = ndlp->nlp_state;
4414             lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4415         }
4416         break;
4417     case ELS_CMD_PRLI:
4418     case ELS_CMD_NVMEPRLI:
4419         if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
4420             ndlp->nlp_prev_state = ndlp->nlp_state;
4421             lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
4422         }
4423         break;
4424     case ELS_CMD_LOGO:
4425         if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
4426             ndlp->nlp_prev_state = ndlp->nlp_state;
4427             lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
4428         }
4429         break;
4430     case ELS_CMD_FDISC:
4431         if (!(vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI))
4432             lpfc_issue_els_fdisc(vport, ndlp, retry);
4433         break;
4434     }
4435     return;
4436 }
4437 
4438 /**
4439  * lpfc_link_reset - Issue link reset
4440  * @vport: pointer to a virtual N_Port data structure.
4441  *
4442  * This routine performs link reset by sending INIT_LINK mailbox command.
4443  * For SLI-3 adapter, link attention interrupt is enabled before issuing
4444  * INIT_LINK mailbox command.
4445  *
4446  * Return code
4447  *   0 - Link reset initiated successfully
4448  *   1 - Failed to initiate link reset
4449  **/
4450 int
4451 lpfc_link_reset(struct lpfc_vport *vport)
4452 {
4453     struct lpfc_hba *phba = vport->phba;
4454     LPFC_MBOXQ_t *mbox;
4455     uint32_t control;
4456     int rc;
4457 
4458     lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4459              "2851 Attempt link reset\n");
4460     mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4461     if (!mbox) {
4462         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4463                 "2852 Failed to allocate mbox memory");
4464         return 1;
4465     }
4466 
4467     /* Enable Link attention interrupts */
4468     if (phba->sli_rev <= LPFC_SLI_REV3) {
4469         spin_lock_irq(&phba->hbalock);
4470         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4471         control = readl(phba->HCregaddr);
4472         control |= HC_LAINT_ENA;
4473         writel(control, phba->HCregaddr);
4474         readl(phba->HCregaddr); /* flush */
4475         spin_unlock_irq(&phba->hbalock);
4476     }
4477 
4478     lpfc_init_link(phba, mbox, phba->cfg_topology,
4479                phba->cfg_link_speed);
4480     mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4481     mbox->vport = vport;
4482     rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4483     if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
4484         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4485                 "2853 Failed to issue INIT_LINK "
4486                 "mbox command, rc:x%x\n", rc);
4487         mempool_free(mbox, phba->mbox_mem_pool);
4488         return 1;
4489     }
4490 
4491     return 0;
4492 }
4493 
4494 /**
4495  * lpfc_els_retry - Make retry decision on an els command iocb
4496  * @phba: pointer to lpfc hba data structure.
4497  * @cmdiocb: pointer to lpfc command iocb data structure.
4498  * @rspiocb: pointer to lpfc response iocb data structure.
4499  *
4500  * This routine makes a retry decision on an ELS command IOCB, which has
4501  * failed. The following ELS IOCBs use this function for retrying the command
4502  * when previously issued command responsed with error status: FLOGI, PLOGI,
4503  * PRLI, ADISC and FDISC. Based on the ELS command type and the
4504  * returned error status, it makes the decision whether a retry shall be
4505  * issued for the command, and whether a retry shall be made immediately or
4506  * delayed. In the former case, the corresponding ELS command issuing-function
4507  * is called to retry the command. In the later case, the ELS command shall
4508  * be posted to the ndlp delayed event and delayed function timer set to the
4509  * ndlp for the delayed command issusing.
4510  *
4511  * Return code
4512  *   0 - No retry of els command is made
4513  *   1 - Immediate or delayed retry of els command is made
4514  **/
4515 static int
4516 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4517            struct lpfc_iocbq *rspiocb)
4518 {
4519     struct lpfc_vport *vport = cmdiocb->vport;
4520     union lpfc_wqe128 *irsp = &rspiocb->wqe;
4521     struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
4522     struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
4523     uint32_t *elscmd;
4524     struct ls_rjt stat;
4525     int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
4526     int logerr = 0;
4527     uint32_t cmd = 0;
4528     uint32_t did;
4529     int link_reset = 0, rc;
4530     u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
4531     u32 ulp_word4 = get_job_word4(phba, rspiocb);
4532 
4533 
4534     /* Note: cmd_dmabuf may be 0 for internal driver abort
4535      * of delays ELS command.
4536      */
4537 
4538     if (pcmd && pcmd->virt) {
4539         elscmd = (uint32_t *) (pcmd->virt);
4540         cmd = *elscmd++;
4541     }
4542 
4543     if (ndlp)
4544         did = ndlp->nlp_DID;
4545     else {
4546         /* We should only hit this case for retrying PLOGI */
4547         did = get_job_els_rsp64_did(phba, rspiocb);
4548         ndlp = lpfc_findnode_did(vport, did);
4549         if (!ndlp && (cmd != ELS_CMD_PLOGI))
4550             return 0;
4551     }
4552 
4553     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4554         "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
4555         *(((uint32_t *)irsp) + 7), ulp_word4, did);
4556 
4557     switch (ulp_status) {
4558     case IOSTAT_FCP_RSP_ERROR:
4559         break;
4560     case IOSTAT_REMOTE_STOP:
4561         if (phba->sli_rev == LPFC_SLI_REV4) {
4562             /* This IO was aborted by the target, we don't
4563              * know the rxid and because we did not send the
4564              * ABTS we cannot generate and RRQ.
4565              */
4566             lpfc_set_rrq_active(phba, ndlp,
4567                      cmdiocb->sli4_lxritag, 0, 0);
4568         }
4569         break;
4570     case IOSTAT_LOCAL_REJECT:
4571         switch ((ulp_word4 & IOERR_PARAM_MASK)) {
4572         case IOERR_LOOP_OPEN_FAILURE:
4573             if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
4574                 delay = 1000;
4575             retry = 1;
4576             break;
4577 
4578         case IOERR_ILLEGAL_COMMAND:
4579             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4580                      "0124 Retry illegal cmd x%x "
4581                      "retry:x%x delay:x%x\n",
4582                      cmd, cmdiocb->retry, delay);
4583             retry = 1;
4584             /* All command's retry policy */
4585             maxretry = 8;
4586             if (cmdiocb->retry > 2)
4587                 delay = 1000;
4588             break;
4589 
4590         case IOERR_NO_RESOURCES:
4591             logerr = 1; /* HBA out of resources */
4592             retry = 1;
4593             if (cmdiocb->retry > 100)
4594                 delay = 100;
4595             maxretry = 250;
4596             break;
4597 
4598         case IOERR_ILLEGAL_FRAME:
4599             delay = 100;
4600             retry = 1;
4601             break;
4602 
4603         case IOERR_INVALID_RPI:
4604             if (cmd == ELS_CMD_PLOGI &&
4605                 did == NameServer_DID) {
4606                 /* Continue forever if plogi to */
4607                 /* the nameserver fails */
4608                 maxretry = 0;
4609                 delay = 100;
4610             }
4611             retry = 1;
4612             break;
4613 
4614         case IOERR_SEQUENCE_TIMEOUT:
4615             if (cmd == ELS_CMD_PLOGI &&
4616                 did == NameServer_DID &&
4617                 (cmdiocb->retry + 1) == maxretry) {
4618                 /* Reset the Link */
4619                 link_reset = 1;
4620                 break;
4621             }
4622             retry = 1;
4623             delay = 100;
4624             break;
4625         case IOERR_SLI_ABORTED:
4626             /* Retry ELS PLOGI command?
4627              * Possibly the rport just wasn't ready.
4628              */
4629             if (cmd == ELS_CMD_PLOGI) {
4630                 /* No retry if state change */
4631                 if (ndlp &&
4632                     ndlp->nlp_state != NLP_STE_PLOGI_ISSUE)
4633                     goto out_retry;
4634                 retry = 1;
4635                 maxretry = 2;
4636             }
4637             break;
4638         }
4639         break;
4640 
4641     case IOSTAT_NPORT_RJT:
4642     case IOSTAT_FABRIC_RJT:
4643         if (ulp_word4 & RJT_UNAVAIL_TEMP) {
4644             retry = 1;
4645             break;
4646         }
4647         break;
4648 
4649     case IOSTAT_NPORT_BSY:
4650     case IOSTAT_FABRIC_BSY:
4651         logerr = 1; /* Fabric / Remote NPort out of resources */
4652         retry = 1;
4653         break;
4654 
4655     case IOSTAT_LS_RJT:
4656         stat.un.ls_rjt_error_be = cpu_to_be32(ulp_word4);
4657         /* Added for Vendor specifc support
4658          * Just keep retrying for these Rsn / Exp codes
4659          */
4660         if ((vport->fc_flag & FC_PT2PT) &&
4661             cmd == ELS_CMD_NVMEPRLI) {
4662             switch (stat.un.b.lsRjtRsnCode) {
4663             case LSRJT_UNABLE_TPC:
4664             case LSRJT_INVALID_CMD:
4665             case LSRJT_LOGICAL_ERR:
4666             case LSRJT_CMD_UNSUPPORTED:
4667                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
4668                          "0168 NVME PRLI LS_RJT "
4669                          "reason %x port doesn't "
4670                          "support NVME, disabling NVME\n",
4671                          stat.un.b.lsRjtRsnCode);
4672                 retry = 0;
4673                 vport->fc_flag |= FC_PT2PT_NO_NVME;
4674                 goto out_retry;
4675             }
4676         }
4677         switch (stat.un.b.lsRjtRsnCode) {
4678         case LSRJT_UNABLE_TPC:
4679             /* The driver has a VALID PLOGI but the rport has
4680              * rejected the PRLI - can't do it now.  Delay
4681              * for 1 second and try again.
4682              *
4683              * However, if explanation is REQ_UNSUPPORTED there's
4684              * no point to retry PRLI.
4685              */
4686             if ((cmd == ELS_CMD_PRLI || cmd == ELS_CMD_NVMEPRLI) &&
4687                 stat.un.b.lsRjtRsnCodeExp !=
4688                 LSEXP_REQ_UNSUPPORTED) {
4689                 delay = 1000;
4690                 maxretry = lpfc_max_els_tries + 1;
4691                 retry = 1;
4692                 break;
4693             }
4694 
4695             /* Legacy bug fix code for targets with PLOGI delays. */
4696             if (stat.un.b.lsRjtRsnCodeExp ==
4697                 LSEXP_CMD_IN_PROGRESS) {
4698                 if (cmd == ELS_CMD_PLOGI) {
4699                     delay = 1000;
4700                     maxretry = 48;
4701                 }
4702                 retry = 1;
4703                 break;
4704             }
4705             if (stat.un.b.lsRjtRsnCodeExp ==
4706                 LSEXP_CANT_GIVE_DATA) {
4707                 if (cmd == ELS_CMD_PLOGI) {
4708                     delay = 1000;
4709                     maxretry = 48;
4710                 }
4711                 retry = 1;
4712                 break;
4713             }
4714             if (cmd == ELS_CMD_PLOGI) {
4715                 delay = 1000;
4716                 maxretry = lpfc_max_els_tries + 1;
4717                 retry = 1;
4718                 break;
4719             }
4720             if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4721               (cmd == ELS_CMD_FDISC) &&
4722               (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
4723                 lpfc_printf_vlog(vport, KERN_ERR,
4724                          LOG_TRACE_EVENT,
4725                          "0125 FDISC Failed (x%x). "
4726                          "Fabric out of resources\n",
4727                          stat.un.lsRjtError);
4728                 lpfc_vport_set_state(vport,
4729                              FC_VPORT_NO_FABRIC_RSCS);
4730             }
4731             break;
4732 
4733         case LSRJT_LOGICAL_BSY:
4734             if ((cmd == ELS_CMD_PLOGI) ||
4735                 (cmd == ELS_CMD_PRLI) ||
4736                 (cmd == ELS_CMD_NVMEPRLI)) {
4737                 delay = 1000;
4738                 maxretry = 48;
4739             } else if (cmd == ELS_CMD_FDISC) {
4740                 /* FDISC retry policy */
4741                 maxretry = 48;
4742                 if (cmdiocb->retry >= 32)
4743                     delay = 1000;
4744             }
4745             retry = 1;
4746             break;
4747 
4748         case LSRJT_LOGICAL_ERR:
4749             /* There are some cases where switches return this
4750              * error when they are not ready and should be returning
4751              * Logical Busy. We should delay every time.
4752              */
4753             if (cmd == ELS_CMD_FDISC &&
4754                 stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
4755                 maxretry = 3;
4756                 delay = 1000;
4757                 retry = 1;
4758             } else if (cmd == ELS_CMD_FLOGI &&
4759                    stat.un.b.lsRjtRsnCodeExp ==
4760                         LSEXP_NOTHING_MORE) {
4761                 vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
4762                 retry = 1;
4763                 lpfc_printf_vlog(vport, KERN_ERR,
4764                          LOG_TRACE_EVENT,
4765                          "0820 FLOGI Failed (x%x). "
4766                          "BBCredit Not Supported\n",
4767                          stat.un.lsRjtError);
4768             }
4769             break;
4770 
4771         case LSRJT_PROTOCOL_ERR:
4772             if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4773               (cmd == ELS_CMD_FDISC) &&
4774               ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
4775               (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
4776               ) {
4777                 lpfc_printf_vlog(vport, KERN_ERR,
4778                          LOG_TRACE_EVENT,
4779                          "0122 FDISC Failed (x%x). "
4780                          "Fabric Detected Bad WWN\n",
4781                          stat.un.lsRjtError);
4782                 lpfc_vport_set_state(vport,
4783                              FC_VPORT_FABRIC_REJ_WWN);
4784             }
4785             break;
4786         case LSRJT_VENDOR_UNIQUE:
4787             if ((stat.un.b.vendorUnique == 0x45) &&
4788                 (cmd == ELS_CMD_FLOGI)) {
4789                 goto out_retry;
4790             }
4791             break;
4792         case LSRJT_CMD_UNSUPPORTED:
4793             /* lpfc nvmet returns this type of LS_RJT when it
4794              * receives an FCP PRLI because lpfc nvmet only
4795              * support NVME.  ELS request is terminated for FCP4
4796              * on this rport.
4797              */
4798             if (stat.un.b.lsRjtRsnCodeExp ==
4799                 LSEXP_REQ_UNSUPPORTED) {
4800                 if (cmd == ELS_CMD_PRLI) {
4801                     spin_lock_irq(&ndlp->lock);
4802                     ndlp->nlp_flag |= NLP_FCP_PRLI_RJT;
4803                     spin_unlock_irq(&ndlp->lock);
4804                     retry = 0;
4805                     goto out_retry;
4806                 }
4807             }
4808             break;
4809         }
4810         break;
4811 
4812     case IOSTAT_INTERMED_RSP:
4813     case IOSTAT_BA_RJT:
4814         break;
4815 
4816     default:
4817         break;
4818     }
4819 
4820     if (link_reset) {
4821         rc = lpfc_link_reset(vport);
4822         if (rc) {
4823             /* Do not give up. Retry PLOGI one more time and attempt
4824              * link reset if PLOGI fails again.
4825              */
4826             retry = 1;
4827             delay = 100;
4828             goto out_retry;
4829         }
4830         return 1;
4831     }
4832 
4833     if (did == FDMI_DID)
4834         retry = 1;
4835 
4836     if ((cmd == ELS_CMD_FLOGI) &&
4837         (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
4838         !lpfc_error_lost_link(ulp_status, ulp_word4)) {
4839         /* FLOGI retry policy */
4840         retry = 1;
4841         /* retry FLOGI forever */
4842         if (phba->link_flag != LS_LOOPBACK_MODE)
4843             maxretry = 0;
4844         else
4845             maxretry = 2;
4846 
4847         if (cmdiocb->retry >= 100)
4848             delay = 5000;
4849         else if (cmdiocb->retry >= 32)
4850             delay = 1000;
4851     } else if ((cmd == ELS_CMD_FDISC) &&
4852         !lpfc_error_lost_link(ulp_status, ulp_word4)) {
4853         /* retry FDISCs every second up to devloss */
4854         retry = 1;
4855         maxretry = vport->cfg_devloss_tmo;
4856         delay = 1000;
4857     }
4858 
4859     cmdiocb->retry++;
4860     if (maxretry && (cmdiocb->retry >= maxretry)) {
4861         phba->fc_stat.elsRetryExceeded++;
4862         retry = 0;
4863     }
4864 
4865     if ((vport->load_flag & FC_UNLOADING) != 0)
4866         retry = 0;
4867 
4868 out_retry:
4869     if (retry) {
4870         if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
4871             /* Stop retrying PLOGI and FDISC if in FCF discovery */
4872             if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
4873                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4874                          "2849 Stop retry ELS command "
4875                          "x%x to remote NPORT x%x, "
4876                          "Data: x%x x%x\n", cmd, did,
4877                          cmdiocb->retry, delay);
4878                 return 0;
4879             }
4880         }
4881 
4882         /* Retry ELS command <elsCmd> to remote NPORT <did> */
4883         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4884                  "0107 Retry ELS command x%x to remote "
4885                  "NPORT x%x Data: x%x x%x\n",
4886                  cmd, did, cmdiocb->retry, delay);
4887 
4888         if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
4889             ((ulp_status != IOSTAT_LOCAL_REJECT) ||
4890             ((ulp_word4 & IOERR_PARAM_MASK) !=
4891             IOERR_NO_RESOURCES))) {
4892             /* Don't reset timer for no resources */
4893 
4894             /* If discovery / RSCN timer is running, reset it */
4895             if (timer_pending(&vport->fc_disctmo) ||
4896                 (vport->fc_flag & FC_RSCN_MODE))
4897                 lpfc_set_disctmo(vport);
4898         }
4899 
4900         phba->fc_stat.elsXmitRetry++;
4901         if (ndlp && delay) {
4902             phba->fc_stat.elsDelayRetry++;
4903             ndlp->nlp_retry = cmdiocb->retry;
4904 
4905             /* delay is specified in milliseconds */
4906             mod_timer(&ndlp->nlp_delayfunc,
4907                 jiffies + msecs_to_jiffies(delay));
4908             spin_lock_irq(&ndlp->lock);
4909             ndlp->nlp_flag |= NLP_DELAY_TMO;
4910             spin_unlock_irq(&ndlp->lock);
4911 
4912             ndlp->nlp_prev_state = ndlp->nlp_state;
4913             if ((cmd == ELS_CMD_PRLI) ||
4914                 (cmd == ELS_CMD_NVMEPRLI))
4915                 lpfc_nlp_set_state(vport, ndlp,
4916                     NLP_STE_PRLI_ISSUE);
4917             else if (cmd != ELS_CMD_ADISC)
4918                 lpfc_nlp_set_state(vport, ndlp,
4919                     NLP_STE_NPR_NODE);
4920             ndlp->nlp_last_elscmd = cmd;
4921 
4922             return 1;
4923         }
4924         switch (cmd) {
4925         case ELS_CMD_FLOGI:
4926             lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
4927             return 1;
4928         case ELS_CMD_FDISC:
4929             lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
4930             return 1;
4931         case ELS_CMD_PLOGI:
4932             if (ndlp) {
4933                 ndlp->nlp_prev_state = ndlp->nlp_state;
4934                 lpfc_nlp_set_state(vport, ndlp,
4935                            NLP_STE_PLOGI_ISSUE);
4936             }
4937             lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
4938             return 1;
4939         case ELS_CMD_ADISC:
4940             ndlp->nlp_prev_state = ndlp->nlp_state;
4941             lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4942             lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
4943             return 1;
4944         case ELS_CMD_PRLI:
4945         case ELS_CMD_NVMEPRLI:
4946             ndlp->nlp_prev_state = ndlp->nlp_state;
4947             lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
4948             lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
4949             return 1;
4950         case ELS_CMD_LOGO:
4951             ndlp->nlp_prev_state = ndlp->nlp_state;
4952             lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
4953             lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
4954             return 1;
4955         }
4956     }
4957     /* No retry ELS command <elsCmd> to remote NPORT <did> */
4958     if (logerr) {
4959         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4960              "0137 No retry ELS command x%x to remote "
4961              "NPORT x%x: Out of Resources: Error:x%x/%x\n",
4962              cmd, did, ulp_status,
4963              ulp_word4);
4964     }
4965     else {
4966         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4967              "0108 No retry ELS command x%x to remote "
4968              "NPORT x%x Retried:%d Error:x%x/%x\n",
4969              cmd, did, cmdiocb->retry, ulp_status,
4970              ulp_word4);
4971     }
4972     return 0;
4973 }
4974 
4975 /**
4976  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
4977  * @phba: pointer to lpfc hba data structure.
4978  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
4979  *
4980  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
4981  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
4982  * checks to see whether there is a lpfc DMA buffer associated with the
4983  * response of the command IOCB. If so, it will be released before releasing
4984  * the lpfc DMA buffer associated with the IOCB itself.
4985  *
4986  * Return code
4987  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
4988  **/
4989 static int
4990 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
4991 {
4992     struct lpfc_dmabuf *buf_ptr;
4993 
4994     /* Free the response before processing the command. */
4995     if (!list_empty(&buf_ptr1->list)) {
4996         list_remove_head(&buf_ptr1->list, buf_ptr,
4997                  struct lpfc_dmabuf,
4998                  list);
4999         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
5000         kfree(buf_ptr);
5001     }
5002     lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
5003     kfree(buf_ptr1);
5004     return 0;
5005 }
5006 
5007 /**
5008  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
5009  * @phba: pointer to lpfc hba data structure.
5010  * @buf_ptr: pointer to the lpfc dma buffer data structure.
5011  *
5012  * This routine releases the lpfc Direct Memory Access (DMA) buffer
5013  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
5014  * pool.
5015  *
5016  * Return code
5017  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
5018  **/
5019 static int
5020 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
5021 {
5022     lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
5023     kfree(buf_ptr);
5024     return 0;
5025 }
5026 
5027 /**
5028  * lpfc_els_free_iocb - Free a command iocb and its associated resources
5029  * @phba: pointer to lpfc hba data structure.
5030  * @elsiocb: pointer to lpfc els command iocb data structure.
5031  *
5032  * This routine frees a command IOCB and its associated resources. The
5033  * command IOCB data structure contains the reference to various associated
5034  * resources, these fields must be set to NULL if the associated reference
5035  * not present:
5036  *   cmd_dmabuf - reference to cmd.
5037  *   cmd_dmabuf->next - reference to rsp
5038  *   rsp_dmabuf - unused
5039  *   bpl_dmabuf - reference to bpl
5040  *
5041  * It first properly decrements the reference count held on ndlp for the
5042  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
5043  * set, it invokes the lpfc_els_free_data() routine to release the Direct
5044  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
5045  * adds the DMA buffer the @phba data structure for the delayed release.
5046  * If reference to the Buffer Pointer List (BPL) is present, the
5047  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
5048  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
5049  * invoked to release the IOCB data structure back to @phba IOCBQ list.
5050  *
5051  * Return code
5052  *   0 - Success (currently, always return 0)
5053  **/
5054 int
5055 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
5056 {
5057     struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
5058 
5059     /* The I/O iocb is complete.  Clear the node and first dmbuf */
5060     elsiocb->ndlp = NULL;
5061 
5062     /* cmd_dmabuf = cmd,  cmd_dmabuf->next = rsp, bpl_dmabuf = bpl */
5063     if (elsiocb->cmd_dmabuf) {
5064         if (elsiocb->cmd_flag & LPFC_DELAY_MEM_FREE) {
5065             /* Firmware could still be in progress of DMAing
5066              * payload, so don't free data buffer till after
5067              * a hbeat.
5068              */
5069             elsiocb->cmd_flag &= ~LPFC_DELAY_MEM_FREE;
5070             buf_ptr = elsiocb->cmd_dmabuf;
5071             elsiocb->cmd_dmabuf = NULL;
5072             if (buf_ptr) {
5073                 buf_ptr1 = NULL;
5074                 spin_lock_irq(&phba->hbalock);
5075                 if (!list_empty(&buf_ptr->list)) {
5076                     list_remove_head(&buf_ptr->list,
5077                         buf_ptr1, struct lpfc_dmabuf,
5078                         list);
5079                     INIT_LIST_HEAD(&buf_ptr1->list);
5080                     list_add_tail(&buf_ptr1->list,
5081                         &phba->elsbuf);
5082                     phba->elsbuf_cnt++;
5083                 }
5084                 INIT_LIST_HEAD(&buf_ptr->list);
5085                 list_add_tail(&buf_ptr->list, &phba->elsbuf);
5086                 phba->elsbuf_cnt++;
5087                 spin_unlock_irq(&phba->hbalock);
5088             }
5089         } else {
5090             buf_ptr1 = elsiocb->cmd_dmabuf;
5091             lpfc_els_free_data(phba, buf_ptr1);
5092             elsiocb->cmd_dmabuf = NULL;
5093         }
5094     }
5095 
5096     if (elsiocb->bpl_dmabuf) {
5097         buf_ptr = elsiocb->bpl_dmabuf;
5098         lpfc_els_free_bpl(phba, buf_ptr);
5099         elsiocb->bpl_dmabuf = NULL;
5100     }
5101     lpfc_sli_release_iocbq(phba, elsiocb);
5102     return 0;
5103 }
5104 
5105 /**
5106  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
5107  * @phba: pointer to lpfc hba data structure.
5108  * @cmdiocb: pointer to lpfc command iocb data structure.
5109  * @rspiocb: pointer to lpfc response iocb data structure.
5110  *
5111  * This routine is the completion callback function to the Logout (LOGO)
5112  * Accept (ACC) Response ELS command. This routine is invoked to indicate
5113  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
5114  * release the ndlp if it has the last reference remaining (reference count
5115  * is 1). If succeeded (meaning ndlp released), it sets the iocb ndlp
5116  * field to NULL to inform the following lpfc_els_free_iocb() routine no
5117  * ndlp reference count needs to be decremented. Otherwise, the ndlp
5118  * reference use-count shall be decremented by the lpfc_els_free_iocb()
5119  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
5120  * IOCB data structure.
5121  **/
5122 static void
5123 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5124                struct lpfc_iocbq *rspiocb)
5125 {
5126     struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
5127     struct lpfc_vport *vport = cmdiocb->vport;
5128     u32 ulp_status, ulp_word4;
5129 
5130     ulp_status = get_job_ulpstatus(phba, rspiocb);
5131     ulp_word4 = get_job_word4(phba, rspiocb);
5132 
5133     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5134         "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
5135         ulp_status, ulp_word4, ndlp->nlp_DID);
5136     /* ACC to LOGO completes to NPort <nlp_DID> */
5137     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5138              "0109 ACC to LOGO completes to NPort x%x refcnt %d "
5139              "Data: x%x x%x x%x\n",
5140              ndlp->nlp_DID, kref_read(&ndlp->kref), ndlp->nlp_flag,
5141              ndlp->nlp_state, ndlp->nlp_rpi);
5142 
5143     /* This clause allows the LOGO ACC to complete and free resources
5144      * for the Fabric Domain Controller.  It does deliberately skip
5145      * the unreg_rpi and release rpi because some fabrics send RDP
5146      * requests after logging out from the initiator.
5147      */
5148     if (ndlp->nlp_type & NLP_FABRIC &&
5149         ((ndlp->nlp_DID & WELL_KNOWN_DID_MASK) != WELL_KNOWN_DID_MASK))
5150         goto out;
5151 
5152     if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
5153         /* If PLOGI is being retried, PLOGI completion will cleanup the
5154          * node. The NLP_NPR_2B_DISC flag needs to be retained to make
5155          * progress on nodes discovered from last RSCN.
5156          */
5157         if ((ndlp->nlp_flag & NLP_DELAY_TMO) &&
5158             (ndlp->nlp_last_elscmd == ELS_CMD_PLOGI))
5159             goto out;
5160 
5161         /* NPort Recovery mode or node is just allocated */
5162         if (!lpfc_nlp_not_used(ndlp)) {
5163             /* A LOGO is completing and the node is in NPR state.
5164              * Just unregister the RPI because the node is still
5165              * required.
5166              */
5167             lpfc_unreg_rpi(vport, ndlp);
5168         } else {
5169             /* Indicate the node has already released, should
5170              * not reference to it from within lpfc_els_free_iocb.
5171              */
5172             cmdiocb->ndlp = NULL;
5173         }
5174     }
5175  out:
5176     /*
5177      * The driver received a LOGO from the rport and has ACK'd it.
5178      * At this point, the driver is done so release the IOCB
5179      */
5180     lpfc_els_free_iocb(phba, cmdiocb);
5181     lpfc_nlp_put(ndlp);
5182 }
5183 
5184 /**
5185  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
5186  * @phba: pointer to lpfc hba data structure.
5187  * @pmb: pointer to the driver internal queue element for mailbox command.
5188  *
5189  * This routine is the completion callback function for unregister default
5190  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
5191  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
5192  * decrements the ndlp reference count held for this completion callback
5193  * function. After that, it invokes the lpfc_nlp_not_used() to check
5194  * whether there is only one reference left on the ndlp. If so, it will
5195  * perform one more decrement and trigger the release of the ndlp.
5196  **/
5197 void
5198 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5199 {
5200     struct lpfc_nodelist *ndlp = pmb->ctx_ndlp;
5201     u32 mbx_flag = pmb->mbox_flag;
5202     u32 mbx_cmd = pmb->u.mb.mbxCommand;
5203 
5204     if (ndlp) {
5205         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
5206                  "0006 rpi x%x DID:%x flg:%x %d x%px "
5207                  "mbx_cmd x%x mbx_flag x%x x%px\n",
5208                  ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5209                  kref_read(&ndlp->kref), ndlp, mbx_cmd,
5210                  mbx_flag, pmb);
5211 
5212         /* This ends the default/temporary RPI cleanup logic for this
5213          * ndlp and the node and rpi needs to be released. Free the rpi
5214          * first on an UNREG_LOGIN and then release the final
5215          * references.
5216          */
5217         spin_lock_irq(&ndlp->lock);
5218         ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
5219         if (mbx_cmd == MBX_UNREG_LOGIN)
5220             ndlp->nlp_flag &= ~NLP_UNREG_INP;
5221         spin_unlock_irq(&ndlp->lock);
5222         lpfc_nlp_put(ndlp);
5223         lpfc_drop_node(ndlp->vport, ndlp);
5224     }
5225 
5226     lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
5227 }
5228 
5229 /**
5230  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
5231  * @phba: pointer to lpfc hba data structure.
5232  * @cmdiocb: pointer to lpfc command iocb data structure.
5233  * @rspiocb: pointer to lpfc response iocb data structure.
5234  *
5235  * This routine is the completion callback function for ELS Response IOCB
5236  * command. In normal case, this callback function just properly sets the
5237  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
5238  * field in the command IOCB is not NULL, the referred mailbox command will
5239  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
5240  * the IOCB.
5241  **/
5242 static void
5243 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5244           struct lpfc_iocbq *rspiocb)
5245 {
5246     struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
5247     struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
5248     struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
5249     IOCB_t  *irsp;
5250     LPFC_MBOXQ_t *mbox = NULL;
5251     u32 ulp_status, ulp_word4, tmo, did, iotag;
5252 
5253     if (!vport) {
5254         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5255                 "3177 ELS response failed\n");
5256         goto out;
5257     }
5258     if (cmdiocb->context_un.mbox)
5259         mbox = cmdiocb->context_un.mbox;
5260 
5261     ulp_status = get_job_ulpstatus(phba, rspiocb);
5262     ulp_word4 = get_job_word4(phba, rspiocb);
5263     did = get_job_els_rsp64_did(phba, cmdiocb);
5264 
5265     if (phba->sli_rev == LPFC_SLI_REV4) {
5266         tmo = get_wqe_tmo(cmdiocb);
5267         iotag = get_wqe_reqtag(cmdiocb);
5268     } else {
5269         irsp = &rspiocb->iocb;
5270         tmo = irsp->ulpTimeout;
5271         iotag = irsp->ulpIoTag;
5272     }
5273 
5274     /* Check to see if link went down during discovery */
5275     if (!ndlp || lpfc_els_chk_latt(vport)) {
5276         if (mbox)
5277             lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
5278         goto out;
5279     }
5280 
5281     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5282         "ELS rsp cmpl:    status:x%x/x%x did:x%x",
5283         ulp_status, ulp_word4, did);
5284     /* ELS response tag <ulpIoTag> completes */
5285     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5286              "0110 ELS response tag x%x completes "
5287              "Data: x%x x%x x%x x%x x%x x%x x%x x%x %p %p\n",
5288              iotag, ulp_status, ulp_word4, tmo,
5289              ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5290              ndlp->nlp_rpi, kref_read(&ndlp->kref), mbox, ndlp);
5291     if (mbox) {
5292         if (ulp_status == 0
5293             && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
5294             if (!lpfc_unreg_rpi(vport, ndlp) &&
5295                 (!(vport->fc_flag & FC_PT2PT))) {
5296                 if (ndlp->nlp_state ==  NLP_STE_PLOGI_ISSUE ||
5297                     ndlp->nlp_state ==
5298                      NLP_STE_REG_LOGIN_ISSUE) {
5299                     lpfc_printf_vlog(vport, KERN_INFO,
5300                              LOG_DISCOVERY,
5301                              "0314 PLOGI recov "
5302                              "DID x%x "
5303                              "Data: x%x x%x x%x\n",
5304                              ndlp->nlp_DID,
5305                              ndlp->nlp_state,
5306                              ndlp->nlp_rpi,
5307                              ndlp->nlp_flag);
5308                     goto out_free_mbox;
5309                 }
5310             }
5311 
5312             /* Increment reference count to ndlp to hold the
5313              * reference to ndlp for the callback function.
5314              */
5315             mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
5316             if (!mbox->ctx_ndlp)
5317                 goto out_free_mbox;
5318 
5319             mbox->vport = vport;
5320             if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
5321                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
5322                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
5323             }
5324             else {
5325                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
5326                 ndlp->nlp_prev_state = ndlp->nlp_state;
5327                 lpfc_nlp_set_state(vport, ndlp,
5328                        NLP_STE_REG_LOGIN_ISSUE);
5329             }
5330 
5331             ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
5332             if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5333                 != MBX_NOT_FINISHED)
5334                 goto out;
5335 
5336             /* Decrement the ndlp reference count we
5337              * set for this failed mailbox command.
5338              */
5339             lpfc_nlp_put(ndlp);
5340             ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
5341 
5342             /* ELS rsp: Cannot issue reg_login for <NPortid> */
5343             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5344                 "0138 ELS rsp: Cannot issue reg_login for x%x "
5345                 "Data: x%x x%x x%x\n",
5346                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5347                 ndlp->nlp_rpi);
5348         }
5349 out_free_mbox:
5350         lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
5351     }
5352 out:
5353     if (ndlp && shost) {
5354         spin_lock_irq(&ndlp->lock);
5355         if (mbox)
5356             ndlp->nlp_flag &= ~NLP_ACC_REGLOGIN;
5357         ndlp->nlp_flag &= ~NLP_RM_DFLT_RPI;
5358         spin_unlock_irq(&ndlp->lock);
5359     }
5360 
5361     /* An SLI4 NPIV instance wants to drop the node at this point under
5362      * these conditions and release the RPI.
5363      */
5364     if (phba->sli_rev == LPFC_SLI_REV4 &&
5365         (vport && vport->port_type == LPFC_NPIV_PORT) &&
5366         !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD) &&
5367         ndlp->nlp_flag & NLP_RELEASE_RPI) {
5368         if (ndlp->nlp_state !=  NLP_STE_PLOGI_ISSUE &&
5369             ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
5370             lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
5371             spin_lock_irq(&ndlp->lock);
5372             ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
5373             ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
5374             spin_unlock_irq(&ndlp->lock);
5375             lpfc_drop_node(vport, ndlp);
5376         }
5377     }
5378 
5379     /* Release the originating I/O reference. */
5380     lpfc_els_free_iocb(phba, cmdiocb);
5381     lpfc_nlp_put(ndlp);
5382     return;
5383 }
5384 
5385 /**
5386  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
5387  * @vport: pointer to a host virtual N_Port data structure.
5388  * @flag: the els command code to be accepted.
5389  * @oldiocb: pointer to the original lpfc command iocb data structure.
5390  * @ndlp: pointer to a node-list data structure.
5391  * @mbox: pointer to the driver internal queue element for mailbox command.
5392  *
5393  * This routine prepares and issues an Accept (ACC) response IOCB
5394  * command. It uses the @flag to properly set up the IOCB field for the
5395  * specific ACC response command to be issued and invokes the
5396  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
5397  * @mbox pointer is passed in, it will be put into the context_un.mbox
5398  * field of the IOCB for the completion callback function to issue the
5399  * mailbox command to the HBA later when callback is invoked.
5400  *
5401  * Note that the ndlp reference count will be incremented by 1 for holding the
5402  * ndlp and the reference to ndlp will be stored into the ndlp field of
5403  * the IOCB for the completion callback function to the corresponding
5404  * response ELS IOCB command.
5405  *
5406  * Return code
5407  *   0 - Successfully issued acc response
5408  *   1 - Failed to issue acc response
5409  **/
5410 int
5411 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
5412          struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
5413          LPFC_MBOXQ_t *mbox)
5414 {
5415     struct lpfc_hba  *phba = vport->phba;
5416     IOCB_t *icmd;
5417     IOCB_t *oldcmd;
5418     union lpfc_wqe128 *wqe;
5419     union lpfc_wqe128 *oldwqe = &oldiocb->wqe;
5420     struct lpfc_iocbq *elsiocb;
5421     uint8_t *pcmd;
5422     struct serv_parm *sp;
5423     uint16_t cmdsize;
5424     int rc;
5425     ELS_PKT *els_pkt_ptr;
5426     struct fc_els_rdf_resp *rdf_resp;
5427 
5428     switch (flag) {
5429     case ELS_CMD_ACC:
5430         cmdsize = sizeof(uint32_t);
5431         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5432                          ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5433         if (!elsiocb) {
5434             spin_lock_irq(&ndlp->lock);
5435             ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5436             spin_unlock_irq(&ndlp->lock);
5437             return 1;
5438         }
5439 
5440         if (phba->sli_rev == LPFC_SLI_REV4) {
5441             wqe = &elsiocb->wqe;
5442             /* XRI / rx_id */
5443             bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5444                    bf_get(wqe_ctxt_tag,
5445                       &oldwqe->xmit_els_rsp.wqe_com));
5446 
5447             /* oxid */
5448             bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5449                    bf_get(wqe_rcvoxid,
5450                       &oldwqe->xmit_els_rsp.wqe_com));
5451         } else {
5452             icmd = &elsiocb->iocb;
5453             oldcmd = &oldiocb->iocb;
5454             icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5455             icmd->unsli3.rcvsli3.ox_id =
5456                 oldcmd->unsli3.rcvsli3.ox_id;
5457         }
5458 
5459         pcmd = elsiocb->cmd_dmabuf->virt;
5460         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5461         pcmd += sizeof(uint32_t);
5462 
5463         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5464             "Issue ACC:       did:x%x flg:x%x",
5465             ndlp->nlp_DID, ndlp->nlp_flag, 0);
5466         break;
5467     case ELS_CMD_FLOGI:
5468     case ELS_CMD_PLOGI:
5469         cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
5470         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5471                          ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5472         if (!elsiocb)
5473             return 1;
5474 
5475         if (phba->sli_rev == LPFC_SLI_REV4) {
5476             wqe = &elsiocb->wqe;
5477             /* XRI / rx_id */
5478             bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5479                    bf_get(wqe_ctxt_tag,
5480                       &oldwqe->xmit_els_rsp.wqe_com));
5481 
5482             /* oxid */
5483             bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5484                    bf_get(wqe_rcvoxid,
5485                       &oldwqe->xmit_els_rsp.wqe_com));
5486         } else {
5487             icmd = &elsiocb->iocb;
5488             oldcmd = &oldiocb->iocb;
5489             icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5490             icmd->unsli3.rcvsli3.ox_id =
5491                 oldcmd->unsli3.rcvsli3.ox_id;
5492         }
5493 
5494         pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
5495 
5496         if (mbox)
5497             elsiocb->context_un.mbox = mbox;
5498 
5499         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5500         pcmd += sizeof(uint32_t);
5501         sp = (struct serv_parm *)pcmd;
5502 
5503         if (flag == ELS_CMD_FLOGI) {
5504             /* Copy the received service parameters back */
5505             memcpy(sp, &phba->fc_fabparam,
5506                    sizeof(struct serv_parm));
5507 
5508             /* Clear the F_Port bit */
5509             sp->cmn.fPort = 0;
5510 
5511             /* Mark all class service parameters as invalid */
5512             sp->cls1.classValid = 0;
5513             sp->cls2.classValid = 0;
5514             sp->cls3.classValid = 0;
5515             sp->cls4.classValid = 0;
5516 
5517             /* Copy our worldwide names */
5518             memcpy(&sp->portName, &vport->fc_sparam.portName,
5519                    sizeof(struct lpfc_name));
5520             memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
5521                    sizeof(struct lpfc_name));
5522         } else {
5523             memcpy(pcmd, &vport->fc_sparam,
5524                    sizeof(struct serv_parm));
5525 
5526             sp->cmn.valid_vendor_ver_level = 0;
5527             memset(sp->un.vendorVersion, 0,
5528                    sizeof(sp->un.vendorVersion));
5529             sp->cmn.bbRcvSizeMsb &= 0xF;
5530 
5531             /* If our firmware supports this feature, convey that
5532              * info to the target using the vendor specific field.
5533              */
5534             if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
5535                 sp->cmn.valid_vendor_ver_level = 1;
5536                 sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
5537                 sp->un.vv.flags =
5538                     cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
5539             }
5540         }
5541 
5542         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5543             "Issue ACC FLOGI/PLOGI: did:x%x flg:x%x",
5544             ndlp->nlp_DID, ndlp->nlp_flag, 0);
5545         break;
5546     case ELS_CMD_PRLO:
5547         cmdsize = sizeof(uint32_t) + sizeof(PRLO);
5548         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5549                          ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
5550         if (!elsiocb)
5551             return 1;
5552 
5553         if (phba->sli_rev == LPFC_SLI_REV4) {
5554             wqe = &elsiocb->wqe;
5555             /* XRI / rx_id */
5556             bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5557                    bf_get(wqe_ctxt_tag,
5558                       &oldwqe->xmit_els_rsp.wqe_com));
5559 
5560             /* oxid */
5561             bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5562                    bf_get(wqe_rcvoxid,
5563                       &oldwqe->xmit_els_rsp.wqe_com));
5564         } else {
5565             icmd = &elsiocb->iocb;
5566             oldcmd = &oldiocb->iocb;
5567             icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5568             icmd->unsli3.rcvsli3.ox_id =
5569                 oldcmd->unsli3.rcvsli3.ox_id;
5570         }
5571 
5572         pcmd = (u8 *) elsiocb->cmd_dmabuf->virt;
5573 
5574         memcpy(pcmd, oldiocb->cmd_dmabuf->virt,
5575                sizeof(uint32_t) + sizeof(PRLO));
5576         *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
5577         els_pkt_ptr = (ELS_PKT *) pcmd;
5578         els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
5579 
5580         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5581             "Issue ACC PRLO:  did:x%x flg:x%x",
5582             ndlp->nlp_DID, ndlp->nlp_flag, 0);
5583         break;
5584     case ELS_CMD_RDF:
5585         cmdsize = sizeof(*rdf_resp);
5586         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5587                          ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5588         if (!elsiocb)
5589             return 1;
5590 
5591         if (phba->sli_rev == LPFC_SLI_REV4) {
5592             wqe = &elsiocb->wqe;
5593             /* XRI / rx_id */
5594             bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5595                    bf_get(wqe_ctxt_tag,
5596                       &oldwqe->xmit_els_rsp.wqe_com));
5597 
5598             /* oxid */
5599             bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5600                    bf_get(wqe_rcvoxid,
5601                       &oldwqe->xmit_els_rsp.wqe_com));
5602         } else {
5603             icmd = &elsiocb->iocb;
5604             oldcmd = &oldiocb->iocb;
5605             icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5606             icmd->unsli3.rcvsli3.ox_id =
5607                 oldcmd->unsli3.rcvsli3.ox_id;
5608         }
5609 
5610         pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
5611         rdf_resp = (struct fc_els_rdf_resp *)pcmd;
5612         memset(rdf_resp, 0, sizeof(*rdf_resp));
5613         rdf_resp->acc_hdr.la_cmd = ELS_LS_ACC;
5614 
5615         /* FC-LS-5 specifies desc_list_len shall be set to 12 */
5616         rdf_resp->desc_list_len = cpu_to_be32(12);
5617 
5618         /* FC-LS-5 specifies LS REQ Information descriptor */
5619         rdf_resp->lsri.desc_tag = cpu_to_be32(1);
5620         rdf_resp->lsri.desc_len = cpu_to_be32(sizeof(u32));
5621         rdf_resp->lsri.rqst_w0.cmd = ELS_RDF;
5622         break;
5623     default:
5624         return 1;
5625     }
5626     if (ndlp->nlp_flag & NLP_LOGO_ACC) {
5627         spin_lock_irq(&ndlp->lock);
5628         if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED ||
5629             ndlp->nlp_flag & NLP_REG_LOGIN_SEND))
5630             ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5631         spin_unlock_irq(&ndlp->lock);
5632         elsiocb->cmd_cmpl = lpfc_cmpl_els_logo_acc;
5633     } else {
5634         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
5635     }
5636 
5637     phba->fc_stat.elsXmitACC++;
5638     elsiocb->ndlp = lpfc_nlp_get(ndlp);
5639     if (!elsiocb->ndlp) {
5640         lpfc_els_free_iocb(phba, elsiocb);
5641         return 1;
5642     }
5643 
5644     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5645     if (rc == IOCB_ERROR) {
5646         lpfc_els_free_iocb(phba, elsiocb);
5647         lpfc_nlp_put(ndlp);
5648         return 1;
5649     }
5650 
5651     /* Xmit ELS ACC response tag <ulpIoTag> */
5652     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5653              "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
5654              "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
5655              "RPI: x%x, fc_flag x%x refcnt %d\n",
5656              rc, elsiocb->iotag, elsiocb->sli4_xritag,
5657              ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5658              ndlp->nlp_rpi, vport->fc_flag, kref_read(&ndlp->kref));
5659     return 0;
5660 }
5661 
5662 /**
5663  * lpfc_els_rsp_reject - Prepare and issue a rjt response iocb command
5664  * @vport: pointer to a virtual N_Port data structure.
5665  * @rejectError: reject response to issue
5666  * @oldiocb: pointer to the original lpfc command iocb data structure.
5667  * @ndlp: pointer to a node-list data structure.
5668  * @mbox: pointer to the driver internal queue element for mailbox command.
5669  *
5670  * This routine prepares and issue an Reject (RJT) response IOCB
5671  * command. If a @mbox pointer is passed in, it will be put into the
5672  * context_un.mbox field of the IOCB for the completion callback function
5673  * to issue to the HBA later.
5674  *
5675  * Note that the ndlp reference count will be incremented by 1 for holding the
5676  * ndlp and the reference to ndlp will be stored into the ndlp field of
5677  * the IOCB for the completion callback function to the reject response
5678  * ELS IOCB command.
5679  *
5680  * Return code
5681  *   0 - Successfully issued reject response
5682  *   1 - Failed to issue reject response
5683  **/
5684 int
5685 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
5686             struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
5687             LPFC_MBOXQ_t *mbox)
5688 {
5689     int rc;
5690     struct lpfc_hba  *phba = vport->phba;
5691     IOCB_t *icmd;
5692     IOCB_t *oldcmd;
5693     union lpfc_wqe128 *wqe;
5694     struct lpfc_iocbq *elsiocb;
5695     uint8_t *pcmd;
5696     uint16_t cmdsize;
5697 
5698     cmdsize = 2 * sizeof(uint32_t);
5699     elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5700                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
5701     if (!elsiocb)
5702         return 1;
5703 
5704     if (phba->sli_rev == LPFC_SLI_REV4) {
5705         wqe = &elsiocb->wqe;
5706         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
5707                get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
5708         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5709                get_job_rcvoxid(phba, oldiocb));
5710     } else {
5711         icmd = &elsiocb->iocb;
5712         oldcmd = &oldiocb->iocb;
5713         icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5714         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5715     }
5716 
5717     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
5718 
5719     *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
5720     pcmd += sizeof(uint32_t);
5721     *((uint32_t *) (pcmd)) = rejectError;
5722 
5723     if (mbox)
5724         elsiocb->context_un.mbox = mbox;
5725 
5726     /* Xmit ELS RJT <err> response tag <ulpIoTag> */
5727     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5728              "0129 Xmit ELS RJT x%x response tag x%x "
5729              "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
5730              "rpi x%x\n",
5731              rejectError, elsiocb->iotag,
5732              get_job_ulpcontext(phba, elsiocb), ndlp->nlp_DID,
5733              ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
5734     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5735         "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
5736         ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
5737 
5738     phba->fc_stat.elsXmitLSRJT++;
5739     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
5740     elsiocb->ndlp = lpfc_nlp_get(ndlp);
5741     if (!elsiocb->ndlp) {
5742         lpfc_els_free_iocb(phba, elsiocb);
5743         return 1;
5744     }
5745 
5746     /* The NPIV instance is rejecting this unsolicited ELS. Make sure the
5747      * node's assigned RPI gets released provided this node is not already
5748      * registered with the transport.
5749      */
5750     if (phba->sli_rev == LPFC_SLI_REV4 &&
5751         vport->port_type == LPFC_NPIV_PORT &&
5752         !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD)) {
5753         spin_lock_irq(&ndlp->lock);
5754         ndlp->nlp_flag |= NLP_RELEASE_RPI;
5755         spin_unlock_irq(&ndlp->lock);
5756     }
5757 
5758     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5759     if (rc == IOCB_ERROR) {
5760         lpfc_els_free_iocb(phba, elsiocb);
5761         lpfc_nlp_put(ndlp);
5762         return 1;
5763     }
5764 
5765     return 0;
5766 }
5767 
5768  /**
5769   * lpfc_issue_els_edc_rsp - Exchange Diagnostic Capabilities with the fabric.
5770   * @vport: pointer to a host virtual N_Port data structure.
5771   * @cmdiocb: pointer to the original lpfc command iocb data structure.
5772   * @ndlp: NPort to where rsp is directed
5773   *
5774   * This routine issues an EDC ACC RSP to the F-Port Controller to communicate
5775   * this N_Port's support of hardware signals in its Congestion
5776   * Capabilities Descriptor.
5777   *
5778   * Return code
5779   *   0 - Successfully issued edc rsp command
5780   *   1 - Failed to issue edc rsp command
5781   **/
5782 static int
5783 lpfc_issue_els_edc_rsp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5784                struct lpfc_nodelist *ndlp)
5785 {
5786     struct lpfc_hba  *phba = vport->phba;
5787     struct lpfc_els_edc_rsp *edc_rsp;
5788     struct lpfc_iocbq *elsiocb;
5789     IOCB_t *icmd, *cmd;
5790     union lpfc_wqe128 *wqe;
5791     uint8_t *pcmd;
5792     int cmdsize, rc;
5793 
5794     cmdsize = sizeof(struct lpfc_els_edc_rsp);
5795     elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, cmdiocb->retry,
5796                      ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5797     if (!elsiocb)
5798         return 1;
5799 
5800     if (phba->sli_rev == LPFC_SLI_REV4) {
5801         wqe = &elsiocb->wqe;
5802         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
5803                get_job_ulpcontext(phba, cmdiocb)); /* Xri / rx_id */
5804         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5805                get_job_rcvoxid(phba, cmdiocb));
5806     } else {
5807         icmd = &elsiocb->iocb;
5808         cmd = &cmdiocb->iocb;
5809         icmd->ulpContext = cmd->ulpContext; /* Xri / rx_id */
5810         icmd->unsli3.rcvsli3.ox_id = cmd->unsli3.rcvsli3.ox_id;
5811     }
5812 
5813     pcmd = elsiocb->cmd_dmabuf->virt;
5814     memset(pcmd, 0, cmdsize);
5815 
5816     edc_rsp = (struct lpfc_els_edc_rsp *)pcmd;
5817     edc_rsp->edc_rsp.acc_hdr.la_cmd = ELS_LS_ACC;
5818     edc_rsp->edc_rsp.desc_list_len = cpu_to_be32(
5819         FC_TLV_DESC_LENGTH_FROM_SZ(struct lpfc_els_edc_rsp));
5820     edc_rsp->edc_rsp.lsri.desc_tag = cpu_to_be32(ELS_DTAG_LS_REQ_INFO);
5821     edc_rsp->edc_rsp.lsri.desc_len = cpu_to_be32(
5822         FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_els_lsri_desc));
5823     edc_rsp->edc_rsp.lsri.rqst_w0.cmd = ELS_EDC;
5824     lpfc_format_edc_cgn_desc(phba, &edc_rsp->cgn_desc);
5825 
5826     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5827                   "Issue EDC ACC:      did:x%x flg:x%x refcnt %d",
5828                   ndlp->nlp_DID, ndlp->nlp_flag,
5829                   kref_read(&ndlp->kref));
5830     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
5831 
5832     phba->fc_stat.elsXmitACC++;
5833     elsiocb->ndlp = lpfc_nlp_get(ndlp);
5834     if (!elsiocb->ndlp) {
5835         lpfc_els_free_iocb(phba, elsiocb);
5836         return 1;
5837     }
5838 
5839     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5840     if (rc == IOCB_ERROR) {
5841         lpfc_els_free_iocb(phba, elsiocb);
5842         lpfc_nlp_put(ndlp);
5843         return 1;
5844     }
5845 
5846     /* Xmit ELS ACC response tag <ulpIoTag> */
5847     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5848              "0152 Xmit EDC ACC response Status: x%x, IoTag: x%x, "
5849              "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
5850              "RPI: x%x, fc_flag x%x\n",
5851              rc, elsiocb->iotag, elsiocb->sli4_xritag,
5852              ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5853              ndlp->nlp_rpi, vport->fc_flag);
5854 
5855     return 0;
5856 }
5857 
5858 /**
5859  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
5860  * @vport: pointer to a virtual N_Port data structure.
5861  * @oldiocb: pointer to the original lpfc command iocb data structure.
5862  * @ndlp: pointer to a node-list data structure.
5863  *
5864  * This routine prepares and issues an Accept (ACC) response to Address
5865  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
5866  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
5867  *
5868  * Note that the ndlp reference count will be incremented by 1 for holding the
5869  * ndlp and the reference to ndlp will be stored into the ndlp field of
5870  * the IOCB for the completion callback function to the ADISC Accept response
5871  * ELS IOCB command.
5872  *
5873  * Return code
5874  *   0 - Successfully issued acc adisc response
5875  *   1 - Failed to issue adisc acc response
5876  **/
5877 int
5878 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
5879                struct lpfc_nodelist *ndlp)
5880 {
5881     struct lpfc_hba  *phba = vport->phba;
5882     ADISC *ap;
5883     IOCB_t *icmd, *oldcmd;
5884     union lpfc_wqe128 *wqe;
5885     struct lpfc_iocbq *elsiocb;
5886     uint8_t *pcmd;
5887     uint16_t cmdsize;
5888     int rc;
5889     u32 ulp_context;
5890 
5891     cmdsize = sizeof(uint32_t) + sizeof(ADISC);
5892     elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5893                      ndlp->nlp_DID, ELS_CMD_ACC);
5894     if (!elsiocb)
5895         return 1;
5896 
5897     if (phba->sli_rev == LPFC_SLI_REV4) {
5898         wqe = &elsiocb->wqe;
5899         /* XRI / rx_id */
5900         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
5901                get_job_ulpcontext(phba, oldiocb));
5902         ulp_context = get_job_ulpcontext(phba, elsiocb);
5903         /* oxid */
5904         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5905                get_job_rcvoxid(phba, oldiocb));
5906     } else {
5907         icmd = &elsiocb->iocb;
5908         oldcmd = &oldiocb->iocb;
5909         icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5910         ulp_context = elsiocb->iocb.ulpContext;
5911         icmd->unsli3.rcvsli3.ox_id =
5912             oldcmd->unsli3.rcvsli3.ox_id;
5913     }
5914 
5915     /* Xmit ADISC ACC response tag <ulpIoTag> */
5916     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5917              "0130 Xmit ADISC ACC response iotag x%x xri: "
5918              "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
5919              elsiocb->iotag, ulp_context,
5920              ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5921              ndlp->nlp_rpi);
5922     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
5923 
5924     *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5925     pcmd += sizeof(uint32_t);
5926 
5927     ap = (ADISC *) (pcmd);
5928     ap->hardAL_PA = phba->fc_pref_ALPA;
5929     memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
5930     memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
5931     ap->DID = be32_to_cpu(vport->fc_myDID);
5932 
5933     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5934               "Issue ACC ADISC: did:x%x flg:x%x refcnt %d",
5935               ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
5936 
5937     phba->fc_stat.elsXmitACC++;
5938     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
5939     elsiocb->ndlp = lpfc_nlp_get(ndlp);
5940     if (!elsiocb->ndlp) {
5941         lpfc_els_free_iocb(phba, elsiocb);
5942         return 1;
5943     }
5944 
5945     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5946     if (rc == IOCB_ERROR) {
5947         lpfc_els_free_iocb(phba, elsiocb);
5948         lpfc_nlp_put(ndlp);
5949         return 1;
5950     }
5951 
5952     return 0;
5953 }
5954 
5955 /**
5956  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
5957  * @vport: pointer to a virtual N_Port data structure.
5958  * @oldiocb: pointer to the original lpfc command iocb data structure.
5959  * @ndlp: pointer to a node-list data structure.
5960  *
5961  * This routine prepares and issues an Accept (ACC) response to Process
5962  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
5963  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
5964  *
5965  * Note that the ndlp reference count will be incremented by 1 for holding the
5966  * ndlp and the reference to ndlp will be stored into the ndlp field of
5967  * the IOCB for the completion callback function to the PRLI Accept response
5968  * ELS IOCB command.
5969  *
5970  * Return code
5971  *   0 - Successfully issued acc prli response
5972  *   1 - Failed to issue acc prli response
5973  **/
5974 int
5975 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
5976               struct lpfc_nodelist *ndlp)
5977 {
5978     struct lpfc_hba  *phba = vport->phba;
5979     PRLI *npr;
5980     struct lpfc_nvme_prli *npr_nvme;
5981     lpfc_vpd_t *vpd;
5982     IOCB_t *icmd;
5983     IOCB_t *oldcmd;
5984     union lpfc_wqe128 *wqe;
5985     struct lpfc_iocbq *elsiocb;
5986     uint8_t *pcmd;
5987     uint16_t cmdsize;
5988     uint32_t prli_fc4_req, *req_payload;
5989     struct lpfc_dmabuf *req_buf;
5990     int rc;
5991     u32 elsrspcmd, ulp_context;
5992 
5993     /* Need the incoming PRLI payload to determine if the ACC is for an
5994      * FC4 or NVME PRLI type.  The PRLI type is at word 1.
5995      */
5996     req_buf = oldiocb->cmd_dmabuf;
5997     req_payload = (((uint32_t *)req_buf->virt) + 1);
5998 
5999     /* PRLI type payload is at byte 3 for FCP or NVME. */
6000     prli_fc4_req = be32_to_cpu(*req_payload);
6001     prli_fc4_req = (prli_fc4_req >> 24) & 0xff;
6002     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6003              "6127 PRLI_ACC:  Req Type x%x, Word1 x%08x\n",
6004              prli_fc4_req, *((uint32_t *)req_payload));
6005 
6006     if (prli_fc4_req == PRLI_FCP_TYPE) {
6007         cmdsize = sizeof(uint32_t) + sizeof(PRLI);
6008         elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
6009     } else if (prli_fc4_req & PRLI_NVME_TYPE) {
6010         cmdsize = sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli);
6011         elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_NVMEPRLI & ~ELS_RSP_MASK));
6012     } else {
6013         return 1;
6014     }
6015 
6016     elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6017                      ndlp->nlp_DID, elsrspcmd);
6018     if (!elsiocb)
6019         return 1;
6020 
6021     if (phba->sli_rev == LPFC_SLI_REV4) {
6022         wqe = &elsiocb->wqe;
6023         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
6024                get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
6025         ulp_context = get_job_ulpcontext(phba, elsiocb);
6026         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6027                get_job_rcvoxid(phba, oldiocb));
6028     } else {
6029         icmd = &elsiocb->iocb;
6030         oldcmd = &oldiocb->iocb;
6031         icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
6032         ulp_context = elsiocb->iocb.ulpContext;
6033         icmd->unsli3.rcvsli3.ox_id =
6034             oldcmd->unsli3.rcvsli3.ox_id;
6035     }
6036 
6037     /* Xmit PRLI ACC response tag <ulpIoTag> */
6038     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6039              "0131 Xmit PRLI ACC response tag x%x xri x%x, "
6040              "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
6041              elsiocb->iotag, ulp_context,
6042              ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6043              ndlp->nlp_rpi);
6044     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
6045     memset(pcmd, 0, cmdsize);
6046 
6047     *((uint32_t *)(pcmd)) = elsrspcmd;
6048     pcmd += sizeof(uint32_t);
6049 
6050     /* For PRLI, remainder of payload is PRLI parameter page */
6051     vpd = &phba->vpd;
6052 
6053     if (prli_fc4_req == PRLI_FCP_TYPE) {
6054         /*
6055          * If the remote port is a target and our firmware version
6056          * is 3.20 or later, set the following bits for FC-TAPE
6057          * support.
6058          */
6059         npr = (PRLI *) pcmd;
6060         if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
6061             (vpd->rev.feaLevelHigh >= 0x02)) {
6062             npr->ConfmComplAllowed = 1;
6063             npr->Retry = 1;
6064             npr->TaskRetryIdReq = 1;
6065         }
6066         npr->acceptRspCode = PRLI_REQ_EXECUTED;
6067         npr->estabImagePair = 1;
6068         npr->readXferRdyDis = 1;
6069         npr->ConfmComplAllowed = 1;
6070         npr->prliType = PRLI_FCP_TYPE;
6071         npr->initiatorFunc = 1;
6072     } else if (prli_fc4_req & PRLI_NVME_TYPE) {
6073         /* Respond with an NVME PRLI Type */
6074         npr_nvme = (struct lpfc_nvme_prli *) pcmd;
6075         bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
6076         bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
6077         bf_set(prli_acc_rsp_code, npr_nvme, PRLI_REQ_EXECUTED);
6078         if (phba->nvmet_support) {
6079             bf_set(prli_tgt, npr_nvme, 1);
6080             bf_set(prli_disc, npr_nvme, 1);
6081             if (phba->cfg_nvme_enable_fb) {
6082                 bf_set(prli_fba, npr_nvme, 1);
6083 
6084                 /* TBD.  Target mode needs to post buffers
6085                  * that support the configured first burst
6086                  * byte size.
6087                  */
6088                 bf_set(prli_fb_sz, npr_nvme,
6089                        phba->cfg_nvmet_fb_size);
6090             }
6091         } else {
6092             bf_set(prli_init, npr_nvme, 1);
6093         }
6094 
6095         lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
6096                  "6015 NVME issue PRLI ACC word1 x%08x "
6097                  "word4 x%08x word5 x%08x flag x%x, "
6098                  "fcp_info x%x nlp_type x%x\n",
6099                  npr_nvme->word1, npr_nvme->word4,
6100                  npr_nvme->word5, ndlp->nlp_flag,
6101                  ndlp->nlp_fcp_info, ndlp->nlp_type);
6102         npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
6103         npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
6104         npr_nvme->word5 = cpu_to_be32(npr_nvme->word5);
6105     } else
6106         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6107                  "6128 Unknown FC_TYPE x%x x%x ndlp x%06x\n",
6108                  prli_fc4_req, ndlp->nlp_fc4_type,
6109                  ndlp->nlp_DID);
6110 
6111     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6112               "Issue ACC PRLI:  did:x%x flg:x%x",
6113               ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6114 
6115     phba->fc_stat.elsXmitACC++;
6116     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
6117     elsiocb->ndlp =  lpfc_nlp_get(ndlp);
6118     if (!elsiocb->ndlp) {
6119         lpfc_els_free_iocb(phba, elsiocb);
6120         return 1;
6121     }
6122 
6123     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6124     if (rc == IOCB_ERROR) {
6125         lpfc_els_free_iocb(phba, elsiocb);
6126         lpfc_nlp_put(ndlp);
6127         return 1;
6128     }
6129 
6130     return 0;
6131 }
6132 
6133 /**
6134  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
6135  * @vport: pointer to a virtual N_Port data structure.
6136  * @format: rnid command format.
6137  * @oldiocb: pointer to the original lpfc command iocb data structure.
6138  * @ndlp: pointer to a node-list data structure.
6139  *
6140  * This routine issues a Request Node Identification Data (RNID) Accept
6141  * (ACC) response. It constructs the RNID ACC response command according to
6142  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
6143  * issue the response.
6144  *
6145  * Note that the ndlp reference count will be incremented by 1 for holding the
6146  * ndlp and the reference to ndlp will be stored into the ndlp field of
6147  * the IOCB for the completion callback function.
6148  *
6149  * Return code
6150  *   0 - Successfully issued acc rnid response
6151  *   1 - Failed to issue acc rnid response
6152  **/
6153 static int
6154 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
6155               struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
6156 {
6157     struct lpfc_hba  *phba = vport->phba;
6158     RNID *rn;
6159     IOCB_t *icmd, *oldcmd;
6160     union lpfc_wqe128 *wqe;
6161     struct lpfc_iocbq *elsiocb;
6162     uint8_t *pcmd;
6163     uint16_t cmdsize;
6164     int rc;
6165     u32 ulp_context;
6166 
6167     cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
6168                     + (2 * sizeof(struct lpfc_name));
6169     if (format)
6170         cmdsize += sizeof(RNID_TOP_DISC);
6171 
6172     elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6173                      ndlp->nlp_DID, ELS_CMD_ACC);
6174     if (!elsiocb)
6175         return 1;
6176 
6177     if (phba->sli_rev == LPFC_SLI_REV4) {
6178         wqe = &elsiocb->wqe;
6179         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
6180                get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
6181         ulp_context = get_job_ulpcontext(phba, elsiocb);
6182         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6183                get_job_rcvoxid(phba, oldiocb));
6184     } else {
6185         icmd = &elsiocb->iocb;
6186         oldcmd = &oldiocb->iocb;
6187         icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
6188         ulp_context = elsiocb->iocb.ulpContext;
6189         icmd->unsli3.rcvsli3.ox_id =
6190             oldcmd->unsli3.rcvsli3.ox_id;
6191     }
6192 
6193     /* Xmit RNID ACC response tag <ulpIoTag> */
6194     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6195              "0132 Xmit RNID ACC response tag x%x xri x%x\n",
6196              elsiocb->iotag, ulp_context);
6197     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
6198     *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6199     pcmd += sizeof(uint32_t);
6200 
6201     memset(pcmd, 0, sizeof(RNID));
6202     rn = (RNID *) (pcmd);
6203     rn->Format = format;
6204     rn->CommonLen = (2 * sizeof(struct lpfc_name));
6205     memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
6206     memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
6207     switch (format) {
6208     case 0:
6209         rn->SpecificLen = 0;
6210         break;
6211     case RNID_TOPOLOGY_DISC:
6212         rn->SpecificLen = sizeof(RNID_TOP_DISC);
6213         memcpy(&rn->un.topologyDisc.portName,
6214                &vport->fc_portname, sizeof(struct lpfc_name));
6215         rn->un.topologyDisc.unitType = RNID_HBA;
6216         rn->un.topologyDisc.physPort = 0;
6217         rn->un.topologyDisc.attachedNodes = 0;
6218         break;
6219     default:
6220         rn->CommonLen = 0;
6221         rn->SpecificLen = 0;
6222         break;
6223     }
6224 
6225     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6226               "Issue ACC RNID:  did:x%x flg:x%x refcnt %d",
6227               ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6228 
6229     phba->fc_stat.elsXmitACC++;
6230     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
6231     elsiocb->ndlp = lpfc_nlp_get(ndlp);
6232     if (!elsiocb->ndlp) {
6233         lpfc_els_free_iocb(phba, elsiocb);
6234         return 1;
6235     }
6236 
6237     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6238     if (rc == IOCB_ERROR) {
6239         lpfc_els_free_iocb(phba, elsiocb);
6240         lpfc_nlp_put(ndlp);
6241         return 1;
6242     }
6243 
6244     return 0;
6245 }
6246 
6247 /**
6248  * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
6249  * @vport: pointer to a virtual N_Port data structure.
6250  * @iocb: pointer to the lpfc command iocb data structure.
6251  * @ndlp: pointer to a node-list data structure.
6252  *
6253  * Return
6254  **/
6255 static void
6256 lpfc_els_clear_rrq(struct lpfc_vport *vport,
6257            struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
6258 {
6259     struct lpfc_hba  *phba = vport->phba;
6260     uint8_t *pcmd;
6261     struct RRQ *rrq;
6262     uint16_t rxid;
6263     uint16_t xri;
6264     struct lpfc_node_rrq *prrq;
6265 
6266 
6267     pcmd = (uint8_t *)iocb->cmd_dmabuf->virt;
6268     pcmd += sizeof(uint32_t);
6269     rrq = (struct RRQ *)pcmd;
6270     rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
6271     rxid = bf_get(rrq_rxid, rrq);
6272 
6273     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6274             "2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
6275             " x%x x%x\n",
6276             be32_to_cpu(bf_get(rrq_did, rrq)),
6277             bf_get(rrq_oxid, rrq),
6278             rxid,
6279             get_wqe_reqtag(iocb),
6280             get_job_ulpcontext(phba, iocb));
6281 
6282     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6283         "Clear RRQ:  did:x%x flg:x%x exchg:x%.08x",
6284         ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
6285     if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
6286         xri = bf_get(rrq_oxid, rrq);
6287     else
6288         xri = rxid;
6289     prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
6290     if (prrq)
6291         lpfc_clr_rrq_active(phba, xri, prrq);
6292     return;
6293 }
6294 
6295 /**
6296  * lpfc_els_rsp_echo_acc - Issue echo acc response
6297  * @vport: pointer to a virtual N_Port data structure.
6298  * @data: pointer to echo data to return in the accept.
6299  * @oldiocb: pointer to the original lpfc command iocb data structure.
6300  * @ndlp: pointer to a node-list data structure.
6301  *
6302  * Return code
6303  *   0 - Successfully issued acc echo response
6304  *   1 - Failed to issue acc echo response
6305  **/
6306 static int
6307 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
6308               struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
6309 {
6310     struct lpfc_hba  *phba = vport->phba;
6311     IOCB_t *icmd, *oldcmd;
6312     union lpfc_wqe128 *wqe;
6313     struct lpfc_iocbq *elsiocb;
6314     uint8_t *pcmd;
6315     uint16_t cmdsize;
6316     int rc;
6317     u32 ulp_context;
6318 
6319     if (phba->sli_rev == LPFC_SLI_REV4)
6320         cmdsize = oldiocb->wcqe_cmpl.total_data_placed;
6321     else
6322         cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
6323 
6324     /* The accumulated length can exceed the BPL_SIZE.  For
6325      * now, use this as the limit
6326      */
6327     if (cmdsize > LPFC_BPL_SIZE)
6328         cmdsize = LPFC_BPL_SIZE;
6329     elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6330                      ndlp->nlp_DID, ELS_CMD_ACC);
6331     if (!elsiocb)
6332         return 1;
6333 
6334     if (phba->sli_rev == LPFC_SLI_REV4) {
6335         wqe = &elsiocb->wqe;
6336         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
6337                get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
6338         ulp_context = get_job_ulpcontext(phba, elsiocb);
6339         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6340                get_job_rcvoxid(phba, oldiocb));
6341     } else {
6342         icmd = &elsiocb->iocb;
6343         oldcmd = &oldiocb->iocb;
6344         icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
6345         ulp_context = elsiocb->iocb.ulpContext;
6346         icmd->unsli3.rcvsli3.ox_id =
6347             oldcmd->unsli3.rcvsli3.ox_id;
6348     }
6349 
6350     /* Xmit ECHO ACC response tag <ulpIoTag> */
6351     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6352              "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
6353              elsiocb->iotag, ulp_context);
6354     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
6355     *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6356     pcmd += sizeof(uint32_t);
6357     memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
6358 
6359     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6360               "Issue ACC ECHO:  did:x%x flg:x%x refcnt %d",
6361               ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6362 
6363     phba->fc_stat.elsXmitACC++;
6364     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
6365     elsiocb->ndlp =  lpfc_nlp_get(ndlp);
6366     if (!elsiocb->ndlp) {
6367         lpfc_els_free_iocb(phba, elsiocb);
6368         return 1;
6369     }
6370 
6371     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6372     if (rc == IOCB_ERROR) {
6373         lpfc_els_free_iocb(phba, elsiocb);
6374         lpfc_nlp_put(ndlp);
6375         return 1;
6376     }
6377 
6378     return 0;
6379 }
6380 
6381 /**
6382  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
6383  * @vport: pointer to a host virtual N_Port data structure.
6384  *
6385  * This routine issues Address Discover (ADISC) ELS commands to those
6386  * N_Ports which are in node port recovery state and ADISC has not been issued
6387  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
6388  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
6389  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
6390  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
6391  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
6392  * IOCBs quit for later pick up. On the other hand, after walking through
6393  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
6394  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
6395  * no more ADISC need to be sent.
6396  *
6397  * Return code
6398  *    The number of N_Ports with adisc issued.
6399  **/
6400 int
6401 lpfc_els_disc_adisc(struct lpfc_vport *vport)
6402 {
6403     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6404     struct lpfc_nodelist *ndlp, *next_ndlp;
6405     int sentadisc = 0;
6406 
6407     /* go thru NPR nodes and issue any remaining ELS ADISCs */
6408     list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
6409 
6410         if (ndlp->nlp_state != NLP_STE_NPR_NODE ||
6411             !(ndlp->nlp_flag & NLP_NPR_ADISC))
6412             continue;
6413 
6414         spin_lock_irq(&ndlp->lock);
6415         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
6416         spin_unlock_irq(&ndlp->lock);
6417 
6418         if (!(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
6419             /* This node was marked for ADISC but was not picked
6420              * for discovery. This is possible if the node was
6421              * missing in gidft response.
6422              *
6423              * At time of marking node for ADISC, we skipped unreg
6424              * from backend
6425              */
6426             lpfc_nlp_unreg_node(vport, ndlp);
6427             lpfc_unreg_rpi(vport, ndlp);
6428             continue;
6429         }
6430 
6431         ndlp->nlp_prev_state = ndlp->nlp_state;
6432         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
6433         lpfc_issue_els_adisc(vport, ndlp, 0);
6434         sentadisc++;
6435         vport->num_disc_nodes++;
6436         if (vport->num_disc_nodes >=
6437                 vport->cfg_discovery_threads) {
6438             spin_lock_irq(shost->host_lock);
6439             vport->fc_flag |= FC_NLP_MORE;
6440             spin_unlock_irq(shost->host_lock);
6441             break;
6442         }
6443 
6444     }
6445     if (sentadisc == 0) {
6446         spin_lock_irq(shost->host_lock);
6447         vport->fc_flag &= ~FC_NLP_MORE;
6448         spin_unlock_irq(shost->host_lock);
6449     }
6450     return sentadisc;
6451 }
6452 
6453 /**
6454  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
6455  * @vport: pointer to a host virtual N_Port data structure.
6456  *
6457  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
6458  * which are in node port recovery state, with a @vport. Each time an ELS
6459  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
6460  * the per @vport number of discover count (num_disc_nodes) shall be
6461  * incremented. If the num_disc_nodes reaches a pre-configured threshold
6462  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
6463  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
6464  * later pick up. On the other hand, after walking through all the ndlps with
6465  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
6466  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
6467  * PLOGI need to be sent.
6468  *
6469  * Return code
6470  *   The number of N_Ports with plogi issued.
6471  **/
6472 int
6473 lpfc_els_disc_plogi(struct lpfc_vport *vport)
6474 {
6475     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6476     struct lpfc_nodelist *ndlp, *next_ndlp;
6477     int sentplogi = 0;
6478 
6479     /* go thru NPR nodes and issue any remaining ELS PLOGIs */
6480     list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
6481         if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
6482                 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
6483                 (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
6484                 (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
6485             ndlp->nlp_prev_state = ndlp->nlp_state;
6486             lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6487             lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
6488             sentplogi++;
6489             vport->num_disc_nodes++;
6490             if (vport->num_disc_nodes >=
6491                     vport->cfg_discovery_threads) {
6492                 spin_lock_irq(shost->host_lock);
6493                 vport->fc_flag |= FC_NLP_MORE;
6494                 spin_unlock_irq(shost->host_lock);
6495                 break;
6496             }
6497         }
6498     }
6499 
6500     lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6501              "6452 Discover PLOGI %d flag x%x\n",
6502              sentplogi, vport->fc_flag);
6503 
6504     if (sentplogi) {
6505         lpfc_set_disctmo(vport);
6506     }
6507     else {
6508         spin_lock_irq(shost->host_lock);
6509         vport->fc_flag &= ~FC_NLP_MORE;
6510         spin_unlock_irq(shost->host_lock);
6511     }
6512     return sentplogi;
6513 }
6514 
6515 static uint32_t
6516 lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
6517         uint32_t word0)
6518 {
6519 
6520     desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
6521     desc->payload.els_req = word0;
6522     desc->length = cpu_to_be32(sizeof(desc->payload));
6523 
6524     return sizeof(struct fc_rdp_link_service_desc);
6525 }
6526 
6527 static uint32_t
6528 lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
6529         uint8_t *page_a0, uint8_t *page_a2)
6530 {
6531     uint16_t wavelength;
6532     uint16_t temperature;
6533     uint16_t rx_power;
6534     uint16_t tx_bias;
6535     uint16_t tx_power;
6536     uint16_t vcc;
6537     uint16_t flag = 0;
6538     struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
6539     struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
6540 
6541     desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
6542 
6543     trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
6544             &page_a0[SSF_TRANSCEIVER_CODE_B4];
6545     trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
6546             &page_a0[SSF_TRANSCEIVER_CODE_B5];
6547 
6548     if ((trasn_code_byte4->fc_sw_laser) ||
6549         (trasn_code_byte5->fc_sw_laser_sl) ||
6550         (trasn_code_byte5->fc_sw_laser_sn)) {  /* check if its short WL */
6551         flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
6552     } else if (trasn_code_byte4->fc_lw_laser) {
6553         wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
6554             page_a0[SSF_WAVELENGTH_B0];
6555         if (wavelength == SFP_WAVELENGTH_LC1310)
6556             flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
6557         if (wavelength == SFP_WAVELENGTH_LL1550)
6558             flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
6559     }
6560     /* check if its SFP+ */
6561     flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
6562             SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
6563                     << SFP_FLAG_CT_SHIFT;
6564 
6565     /* check if its OPTICAL */
6566     flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
6567             SFP_FLAG_IS_OPTICAL_PORT : 0)
6568                     << SFP_FLAG_IS_OPTICAL_SHIFT;
6569 
6570     temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
6571         page_a2[SFF_TEMPERATURE_B0]);
6572     vcc = (page_a2[SFF_VCC_B1] << 8 |
6573         page_a2[SFF_VCC_B0]);
6574     tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
6575         page_a2[SFF_TXPOWER_B0]);
6576     tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
6577         page_a2[SFF_TX_BIAS_CURRENT_B0]);
6578     rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
6579         page_a2[SFF_RXPOWER_B0]);
6580     desc->sfp_info.temperature = cpu_to_be16(temperature);
6581     desc->sfp_info.rx_power = cpu_to_be16(rx_power);
6582     desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
6583     desc->sfp_info.tx_power = cpu_to_be16(tx_power);
6584     desc->sfp_info.vcc = cpu_to_be16(vcc);
6585 
6586     desc->sfp_info.flags = cpu_to_be16(flag);
6587     desc->length = cpu_to_be32(sizeof(desc->sfp_info));
6588 
6589     return sizeof(struct fc_rdp_sfp_desc);
6590 }
6591 
6592 static uint32_t
6593 lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
6594         READ_LNK_VAR *stat)
6595 {
6596     uint32_t type;
6597 
6598     desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
6599 
6600     type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
6601 
6602     desc->info.port_type = cpu_to_be32(type);
6603 
6604     desc->info.link_status.link_failure_cnt =
6605         cpu_to_be32(stat->linkFailureCnt);
6606     desc->info.link_status.loss_of_synch_cnt =
6607         cpu_to_be32(stat->lossSyncCnt);
6608     desc->info.link_status.loss_of_signal_cnt =
6609         cpu_to_be32(stat->lossSignalCnt);
6610     desc->info.link_status.primitive_seq_proto_err =
6611         cpu_to_be32(stat->primSeqErrCnt);
6612     desc->info.link_status.invalid_trans_word =
6613         cpu_to_be32(stat->invalidXmitWord);
6614     desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
6615 
6616     desc->length = cpu_to_be32(sizeof(desc->info));
6617 
6618     return sizeof(struct fc_rdp_link_error_status_desc);
6619 }
6620 
6621 static uint32_t
6622 lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc *desc, READ_LNK_VAR *stat,
6623               struct lpfc_vport *vport)
6624 {
6625     uint32_t bbCredit;
6626 
6627     desc->tag = cpu_to_be32(RDP_BBC_DESC_TAG);
6628 
6629     bbCredit = vport->fc_sparam.cmn.bbCreditLsb |
6630             (vport->fc_sparam.cmn.bbCreditMsb << 8);
6631     desc->bbc_info.port_bbc = cpu_to_be32(bbCredit);
6632     if (vport->phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
6633         bbCredit = vport->phba->fc_fabparam.cmn.bbCreditLsb |
6634             (vport->phba->fc_fabparam.cmn.bbCreditMsb << 8);
6635         desc->bbc_info.attached_port_bbc = cpu_to_be32(bbCredit);
6636     } else {
6637         desc->bbc_info.attached_port_bbc = 0;
6638     }
6639 
6640     desc->bbc_info.rtt = 0;
6641     desc->length = cpu_to_be32(sizeof(desc->bbc_info));
6642 
6643     return sizeof(struct fc_rdp_bbc_desc);
6644 }
6645 
6646 static uint32_t
6647 lpfc_rdp_res_oed_temp_desc(struct lpfc_hba *phba,
6648                struct fc_rdp_oed_sfp_desc *desc, uint8_t *page_a2)
6649 {
6650     uint32_t flags = 0;
6651 
6652     desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6653 
6654     desc->oed_info.hi_alarm = page_a2[SSF_TEMP_HIGH_ALARM];
6655     desc->oed_info.lo_alarm = page_a2[SSF_TEMP_LOW_ALARM];
6656     desc->oed_info.hi_warning = page_a2[SSF_TEMP_HIGH_WARNING];
6657     desc->oed_info.lo_warning = page_a2[SSF_TEMP_LOW_WARNING];
6658 
6659     if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
6660         flags |= RDP_OET_HIGH_ALARM;
6661     if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
6662         flags |= RDP_OET_LOW_ALARM;
6663     if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
6664         flags |= RDP_OET_HIGH_WARNING;
6665     if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
6666         flags |= RDP_OET_LOW_WARNING;
6667 
6668     flags |= ((0xf & RDP_OED_TEMPERATURE) << RDP_OED_TYPE_SHIFT);
6669     desc->oed_info.function_flags = cpu_to_be32(flags);
6670     desc->length = cpu_to_be32(sizeof(desc->oed_info));
6671     return sizeof(struct fc_rdp_oed_sfp_desc);
6672 }
6673 
6674 static uint32_t
6675 lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba *phba,
6676                   struct fc_rdp_oed_sfp_desc *desc,
6677                   uint8_t *page_a2)
6678 {
6679     uint32_t flags = 0;
6680 
6681     desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6682 
6683     desc->oed_info.hi_alarm = page_a2[SSF_VOLTAGE_HIGH_ALARM];
6684     desc->oed_info.lo_alarm = page_a2[SSF_VOLTAGE_LOW_ALARM];
6685     desc->oed_info.hi_warning = page_a2[SSF_VOLTAGE_HIGH_WARNING];
6686     desc->oed_info.lo_warning = page_a2[SSF_VOLTAGE_LOW_WARNING];
6687 
6688     if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
6689         flags |= RDP_OET_HIGH_ALARM;
6690     if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_VOLTAGE)
6691         flags |= RDP_OET_LOW_ALARM;
6692     if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
6693         flags |= RDP_OET_HIGH_WARNING;
6694     if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_VOLTAGE)
6695         flags |= RDP_OET_LOW_WARNING;
6696 
6697     flags |= ((0xf & RDP_OED_VOLTAGE) << RDP_OED_TYPE_SHIFT);
6698     desc->oed_info.function_flags = cpu_to_be32(flags);
6699     desc->length = cpu_to_be32(sizeof(desc->oed_info));
6700     return sizeof(struct fc_rdp_oed_sfp_desc);
6701 }
6702 
6703 static uint32_t
6704 lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba *phba,
6705                  struct fc_rdp_oed_sfp_desc *desc,
6706                  uint8_t *page_a2)
6707 {
6708     uint32_t flags = 0;
6709 
6710     desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6711 
6712     desc->oed_info.hi_alarm = page_a2[SSF_BIAS_HIGH_ALARM];
6713     desc->oed_info.lo_alarm = page_a2[SSF_BIAS_LOW_ALARM];
6714     desc->oed_info.hi_warning = page_a2[SSF_BIAS_HIGH_WARNING];
6715     desc->oed_info.lo_warning = page_a2[SSF_BIAS_LOW_WARNING];
6716 
6717     if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXBIAS)
6718         flags |= RDP_OET_HIGH_ALARM;
6719     if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXBIAS)
6720         flags |= RDP_OET_LOW_ALARM;
6721     if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXBIAS)
6722         flags |= RDP_OET_HIGH_WARNING;
6723     if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXBIAS)
6724         flags |= RDP_OET_LOW_WARNING;
6725 
6726     flags |= ((0xf & RDP_OED_TXBIAS) << RDP_OED_TYPE_SHIFT);
6727     desc->oed_info.function_flags = cpu_to_be32(flags);
6728     desc->length = cpu_to_be32(sizeof(desc->oed_info));
6729     return sizeof(struct fc_rdp_oed_sfp_desc);
6730 }
6731 
6732 static uint32_t
6733 lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba *phba,
6734                   struct fc_rdp_oed_sfp_desc *desc,
6735                   uint8_t *page_a2)
6736 {
6737     uint32_t flags = 0;
6738 
6739     desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6740 
6741     desc->oed_info.hi_alarm = page_a2[SSF_TXPOWER_HIGH_ALARM];
6742     desc->oed_info.lo_alarm = page_a2[SSF_TXPOWER_LOW_ALARM];
6743     desc->oed_info.hi_warning = page_a2[SSF_TXPOWER_HIGH_WARNING];
6744     desc->oed_info.lo_warning = page_a2[SSF_TXPOWER_LOW_WARNING];
6745 
6746     if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXPOWER)
6747         flags |= RDP_OET_HIGH_ALARM;
6748     if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXPOWER)
6749         flags |= RDP_OET_LOW_ALARM;
6750     if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXPOWER)
6751         flags |= RDP_OET_HIGH_WARNING;
6752     if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXPOWER)
6753         flags |= RDP_OET_LOW_WARNING;
6754 
6755     flags |= ((0xf & RDP_OED_TXPOWER) << RDP_OED_TYPE_SHIFT);
6756     desc->oed_info.function_flags = cpu_to_be32(flags);
6757     desc->length = cpu_to_be32(sizeof(desc->oed_info));
6758     return sizeof(struct fc_rdp_oed_sfp_desc);
6759 }
6760 
6761 
6762 static uint32_t
6763 lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba *phba,
6764                   struct fc_rdp_oed_sfp_desc *desc,
6765                   uint8_t *page_a2)
6766 {
6767     uint32_t flags = 0;
6768 
6769     desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6770 
6771     desc->oed_info.hi_alarm = page_a2[SSF_RXPOWER_HIGH_ALARM];
6772     desc->oed_info.lo_alarm = page_a2[SSF_RXPOWER_LOW_ALARM];
6773     desc->oed_info.hi_warning = page_a2[SSF_RXPOWER_HIGH_WARNING];
6774     desc->oed_info.lo_warning = page_a2[SSF_RXPOWER_LOW_WARNING];
6775 
6776     if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_RXPOWER)
6777         flags |= RDP_OET_HIGH_ALARM;
6778     if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_RXPOWER)
6779         flags |= RDP_OET_LOW_ALARM;
6780     if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_RXPOWER)
6781         flags |= RDP_OET_HIGH_WARNING;
6782     if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_RXPOWER)
6783         flags |= RDP_OET_LOW_WARNING;
6784 
6785     flags |= ((0xf & RDP_OED_RXPOWER) << RDP_OED_TYPE_SHIFT);
6786     desc->oed_info.function_flags = cpu_to_be32(flags);
6787     desc->length = cpu_to_be32(sizeof(desc->oed_info));
6788     return sizeof(struct fc_rdp_oed_sfp_desc);
6789 }
6790 
6791 static uint32_t
6792 lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc *desc,
6793               uint8_t *page_a0, struct lpfc_vport *vport)
6794 {
6795     desc->tag = cpu_to_be32(RDP_OPD_DESC_TAG);
6796     memcpy(desc->opd_info.vendor_name, &page_a0[SSF_VENDOR_NAME], 16);
6797     memcpy(desc->opd_info.model_number, &page_a0[SSF_VENDOR_PN], 16);
6798     memcpy(desc->opd_info.serial_number, &page_a0[SSF_VENDOR_SN], 16);
6799     memcpy(desc->opd_info.revision, &page_a0[SSF_VENDOR_REV], 4);
6800     memcpy(desc->opd_info.date, &page_a0[SSF_DATE_CODE], 8);
6801     desc->length = cpu_to_be32(sizeof(desc->opd_info));
6802     return sizeof(struct fc_rdp_opd_sfp_desc);
6803 }
6804 
6805 static uint32_t
6806 lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc *desc, READ_LNK_VAR *stat)
6807 {
6808     if (bf_get(lpfc_read_link_stat_gec2, stat) == 0)
6809         return 0;
6810     desc->tag = cpu_to_be32(RDP_FEC_DESC_TAG);
6811 
6812     desc->info.CorrectedBlocks =
6813         cpu_to_be32(stat->fecCorrBlkCount);
6814     desc->info.UncorrectableBlocks =
6815         cpu_to_be32(stat->fecUncorrBlkCount);
6816 
6817     desc->length = cpu_to_be32(sizeof(desc->info));
6818 
6819     return sizeof(struct fc_fec_rdp_desc);
6820 }
6821 
6822 static uint32_t
6823 lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
6824 {
6825     uint16_t rdp_cap = 0;
6826     uint16_t rdp_speed;
6827 
6828     desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
6829 
6830     switch (phba->fc_linkspeed) {
6831     case LPFC_LINK_SPEED_1GHZ:
6832         rdp_speed = RDP_PS_1GB;
6833         break;
6834     case LPFC_LINK_SPEED_2GHZ:
6835         rdp_speed = RDP_PS_2GB;
6836         break;
6837     case LPFC_LINK_SPEED_4GHZ:
6838         rdp_speed = RDP_PS_4GB;
6839         break;
6840     case LPFC_LINK_SPEED_8GHZ:
6841         rdp_speed = RDP_PS_8GB;
6842         break;
6843     case LPFC_LINK_SPEED_10GHZ:
6844         rdp_speed = RDP_PS_10GB;
6845         break;
6846     case LPFC_LINK_SPEED_16GHZ:
6847         rdp_speed = RDP_PS_16GB;
6848         break;
6849     case LPFC_LINK_SPEED_32GHZ:
6850         rdp_speed = RDP_PS_32GB;
6851         break;
6852     case LPFC_LINK_SPEED_64GHZ:
6853         rdp_speed = RDP_PS_64GB;
6854         break;
6855     case LPFC_LINK_SPEED_128GHZ:
6856         rdp_speed = RDP_PS_128GB;
6857         break;
6858     case LPFC_LINK_SPEED_256GHZ:
6859         rdp_speed = RDP_PS_256GB;
6860         break;
6861     default:
6862         rdp_speed = RDP_PS_UNKNOWN;
6863         break;
6864     }
6865 
6866     desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
6867 
6868     if (phba->lmt & LMT_256Gb)
6869         rdp_cap |= RDP_PS_256GB;
6870     if (phba->lmt & LMT_128Gb)
6871         rdp_cap |= RDP_PS_128GB;
6872     if (phba->lmt & LMT_64Gb)
6873         rdp_cap |= RDP_PS_64GB;
6874     if (phba->lmt & LMT_32Gb)
6875         rdp_cap |= RDP_PS_32GB;
6876     if (phba->lmt & LMT_16Gb)
6877         rdp_cap |= RDP_PS_16GB;
6878     if (phba->lmt & LMT_10Gb)
6879         rdp_cap |= RDP_PS_10GB;
6880     if (phba->lmt & LMT_8Gb)
6881         rdp_cap |= RDP_PS_8GB;
6882     if (phba->lmt & LMT_4Gb)
6883         rdp_cap |= RDP_PS_4GB;
6884     if (phba->lmt & LMT_2Gb)
6885         rdp_cap |= RDP_PS_2GB;
6886     if (phba->lmt & LMT_1Gb)
6887         rdp_cap |= RDP_PS_1GB;
6888 
6889     if (rdp_cap == 0)
6890         rdp_cap = RDP_CAP_UNKNOWN;
6891     if (phba->cfg_link_speed != LPFC_USER_LINK_SPEED_AUTO)
6892         rdp_cap |= RDP_CAP_USER_CONFIGURED;
6893 
6894     desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
6895     desc->length = cpu_to_be32(sizeof(desc->info));
6896     return sizeof(struct fc_rdp_port_speed_desc);
6897 }
6898 
6899 static uint32_t
6900 lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
6901         struct lpfc_vport *vport)
6902 {
6903 
6904     desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
6905 
6906     memcpy(desc->port_names.wwnn, &vport->fc_nodename,
6907             sizeof(desc->port_names.wwnn));
6908 
6909     memcpy(desc->port_names.wwpn, &vport->fc_portname,
6910             sizeof(desc->port_names.wwpn));
6911 
6912     desc->length = cpu_to_be32(sizeof(desc->port_names));
6913     return sizeof(struct fc_rdp_port_name_desc);
6914 }
6915 
6916 static uint32_t
6917 lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
6918         struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
6919 {
6920 
6921     desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
6922     if (vport->fc_flag & FC_FABRIC) {
6923         memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
6924                sizeof(desc->port_names.wwnn));
6925 
6926         memcpy(desc->port_names.wwpn, &vport->fabric_portname,
6927                sizeof(desc->port_names.wwpn));
6928     } else {  /* Point to Point */
6929         memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
6930                sizeof(desc->port_names.wwnn));
6931 
6932         memcpy(desc->port_names.wwpn, &ndlp->nlp_portname,
6933                sizeof(desc->port_names.wwpn));
6934     }
6935 
6936     desc->length = cpu_to_be32(sizeof(desc->port_names));
6937     return sizeof(struct fc_rdp_port_name_desc);
6938 }
6939 
6940 static void
6941 lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
6942         int status)
6943 {
6944     struct lpfc_nodelist *ndlp = rdp_context->ndlp;
6945     struct lpfc_vport *vport = ndlp->vport;
6946     struct lpfc_iocbq *elsiocb;
6947     struct ulp_bde64 *bpl;
6948     IOCB_t *icmd;
6949     union lpfc_wqe128 *wqe;
6950     uint8_t *pcmd;
6951     struct ls_rjt *stat;
6952     struct fc_rdp_res_frame *rdp_res;
6953     uint32_t cmdsize, len;
6954     uint16_t *flag_ptr;
6955     int rc;
6956     u32 ulp_context;
6957 
6958     if (status != SUCCESS)
6959         goto error;
6960 
6961     /* This will change once we know the true size of the RDP payload */
6962     cmdsize = sizeof(struct fc_rdp_res_frame);
6963 
6964     elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
6965                 lpfc_max_els_tries, rdp_context->ndlp,
6966                 rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
6967     if (!elsiocb)
6968         goto free_rdp_context;
6969 
6970     ulp_context = get_job_ulpcontext(phba, elsiocb);
6971     if (phba->sli_rev == LPFC_SLI_REV4) {
6972         wqe = &elsiocb->wqe;
6973         /* ox-id of the frame */
6974         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6975                rdp_context->ox_id);
6976         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6977                rdp_context->rx_id);
6978     } else {
6979         icmd = &elsiocb->iocb;
6980         icmd->ulpContext = rdp_context->rx_id;
6981         icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
6982     }
6983 
6984     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6985             "2171 Xmit RDP response tag x%x xri x%x, "
6986             "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x",
6987             elsiocb->iotag, ulp_context,
6988             ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6989             ndlp->nlp_rpi);
6990     rdp_res = (struct fc_rdp_res_frame *)elsiocb->cmd_dmabuf->virt;
6991     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
6992     memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
6993     *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6994 
6995     /* Update Alarm and Warning */
6996     flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_ALARM_FLAGS);
6997     phba->sfp_alarm |= *flag_ptr;
6998     flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_WARNING_FLAGS);
6999     phba->sfp_warning |= *flag_ptr;
7000 
7001     /* For RDP payload */
7002     len = 8;
7003     len += lpfc_rdp_res_link_service((struct fc_rdp_link_service_desc *)
7004                      (len + pcmd), ELS_CMD_RDP);
7005 
7006     len += lpfc_rdp_res_sfp_desc((struct fc_rdp_sfp_desc *)(len + pcmd),
7007             rdp_context->page_a0, rdp_context->page_a2);
7008     len += lpfc_rdp_res_speed((struct fc_rdp_port_speed_desc *)(len + pcmd),
7009                   phba);
7010     len += lpfc_rdp_res_link_error((struct fc_rdp_link_error_status_desc *)
7011                        (len + pcmd), &rdp_context->link_stat);
7012     len += lpfc_rdp_res_diag_port_names((struct fc_rdp_port_name_desc *)
7013                          (len + pcmd), vport);
7014     len += lpfc_rdp_res_attach_port_names((struct fc_rdp_port_name_desc *)
7015                     (len + pcmd), vport, ndlp);
7016     len += lpfc_rdp_res_fec_desc((struct fc_fec_rdp_desc *)(len + pcmd),
7017             &rdp_context->link_stat);
7018     len += lpfc_rdp_res_bbc_desc((struct fc_rdp_bbc_desc *)(len + pcmd),
7019                      &rdp_context->link_stat, vport);
7020     len += lpfc_rdp_res_oed_temp_desc(phba,
7021                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7022                 rdp_context->page_a2);
7023     len += lpfc_rdp_res_oed_voltage_desc(phba,
7024                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7025                 rdp_context->page_a2);
7026     len += lpfc_rdp_res_oed_txbias_desc(phba,
7027                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7028                 rdp_context->page_a2);
7029     len += lpfc_rdp_res_oed_txpower_desc(phba,
7030                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7031                 rdp_context->page_a2);
7032     len += lpfc_rdp_res_oed_rxpower_desc(phba,
7033                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7034                 rdp_context->page_a2);
7035     len += lpfc_rdp_res_opd_desc((struct fc_rdp_opd_sfp_desc *)(len + pcmd),
7036                      rdp_context->page_a0, vport);
7037 
7038     rdp_res->length = cpu_to_be32(len - 8);
7039     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7040 
7041     /* Now that we know the true size of the payload, update the BPL */
7042     bpl = (struct ulp_bde64 *)elsiocb->bpl_dmabuf->virt;
7043     bpl->tus.f.bdeSize = len;
7044     bpl->tus.f.bdeFlags = 0;
7045     bpl->tus.w = le32_to_cpu(bpl->tus.w);
7046 
7047     phba->fc_stat.elsXmitACC++;
7048     elsiocb->ndlp = lpfc_nlp_get(ndlp);
7049     if (!elsiocb->ndlp) {
7050         lpfc_els_free_iocb(phba, elsiocb);
7051         goto free_rdp_context;
7052     }
7053 
7054     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7055     if (rc == IOCB_ERROR) {
7056         lpfc_els_free_iocb(phba, elsiocb);
7057         lpfc_nlp_put(ndlp);
7058     }
7059 
7060     goto free_rdp_context;
7061 
7062 error:
7063     cmdsize = 2 * sizeof(uint32_t);
7064     elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
7065             ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
7066     if (!elsiocb)
7067         goto free_rdp_context;
7068 
7069     if (phba->sli_rev == LPFC_SLI_REV4) {
7070         wqe = &elsiocb->wqe;
7071         /* ox-id of the frame */
7072         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7073                rdp_context->ox_id);
7074         bf_set(wqe_ctxt_tag,
7075                &wqe->xmit_els_rsp.wqe_com,
7076                rdp_context->rx_id);
7077     } else {
7078         icmd = &elsiocb->iocb;
7079         icmd->ulpContext = rdp_context->rx_id;
7080         icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
7081     }
7082 
7083     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
7084 
7085     *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
7086     stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
7087     stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7088 
7089     phba->fc_stat.elsXmitLSRJT++;
7090     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7091     elsiocb->ndlp = lpfc_nlp_get(ndlp);
7092     if (!elsiocb->ndlp) {
7093         lpfc_els_free_iocb(phba, elsiocb);
7094         goto free_rdp_context;
7095     }
7096 
7097     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7098     if (rc == IOCB_ERROR) {
7099         lpfc_els_free_iocb(phba, elsiocb);
7100         lpfc_nlp_put(ndlp);
7101     }
7102 
7103 free_rdp_context:
7104     /* This reference put is for the original unsolicited RDP. If the
7105      * prep failed, there is no reference to remove.
7106      */
7107     lpfc_nlp_put(ndlp);
7108     kfree(rdp_context);
7109 }
7110 
7111 static int
7112 lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
7113 {
7114     LPFC_MBOXQ_t *mbox = NULL;
7115     int rc;
7116 
7117     mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7118     if (!mbox) {
7119         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
7120                 "7105 failed to allocate mailbox memory");
7121         return 1;
7122     }
7123 
7124     if (lpfc_sli4_dump_page_a0(phba, mbox))
7125         goto rdp_fail;
7126     mbox->vport = rdp_context->ndlp->vport;
7127     mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
7128     mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
7129     rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7130     if (rc == MBX_NOT_FINISHED) {
7131         lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
7132         return 1;
7133     }
7134 
7135     return 0;
7136 
7137 rdp_fail:
7138     mempool_free(mbox, phba->mbox_mem_pool);
7139     return 1;
7140 }
7141 
7142 /*
7143  * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
7144  * @vport: pointer to a host virtual N_Port data structure.
7145  * @cmdiocb: pointer to lpfc command iocb data structure.
7146  * @ndlp: pointer to a node-list data structure.
7147  *
7148  * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
7149  * IOCB. First, the payload of the unsolicited RDP is checked.
7150  * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
7151  * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
7152  * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
7153  * gather all data and send RDP response.
7154  *
7155  * Return code
7156  *   0 - Sent the acc response
7157  *   1 - Sent the reject response.
7158  */
7159 static int
7160 lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7161         struct lpfc_nodelist *ndlp)
7162 {
7163     struct lpfc_hba *phba = vport->phba;
7164     struct lpfc_dmabuf *pcmd;
7165     uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
7166     struct fc_rdp_req_frame *rdp_req;
7167     struct lpfc_rdp_context *rdp_context;
7168     union lpfc_wqe128 *cmd = NULL;
7169     struct ls_rjt stat;
7170 
7171     if (phba->sli_rev < LPFC_SLI_REV4 ||
7172         bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
7173                         LPFC_SLI_INTF_IF_TYPE_2) {
7174         rjt_err = LSRJT_UNABLE_TPC;
7175         rjt_expl = LSEXP_REQ_UNSUPPORTED;
7176         goto error;
7177     }
7178 
7179     if (phba->sli_rev < LPFC_SLI_REV4 || (phba->hba_flag & HBA_FCOE_MODE)) {
7180         rjt_err = LSRJT_UNABLE_TPC;
7181         rjt_expl = LSEXP_REQ_UNSUPPORTED;
7182         goto error;
7183     }
7184 
7185     pcmd = cmdiocb->cmd_dmabuf;
7186     rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
7187 
7188     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7189              "2422 ELS RDP Request "
7190              "dec len %d tag x%x port_id %d len %d\n",
7191              be32_to_cpu(rdp_req->rdp_des_length),
7192              be32_to_cpu(rdp_req->nport_id_desc.tag),
7193              be32_to_cpu(rdp_req->nport_id_desc.nport_id),
7194              be32_to_cpu(rdp_req->nport_id_desc.length));
7195 
7196     if (sizeof(struct fc_rdp_nport_desc) !=
7197             be32_to_cpu(rdp_req->rdp_des_length))
7198         goto rjt_logerr;
7199     if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
7200         goto rjt_logerr;
7201     if (RDP_NPORT_ID_SIZE !=
7202             be32_to_cpu(rdp_req->nport_id_desc.length))
7203         goto rjt_logerr;
7204     rdp_context = kzalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
7205     if (!rdp_context) {
7206         rjt_err = LSRJT_UNABLE_TPC;
7207         goto error;
7208     }
7209 
7210     cmd = &cmdiocb->wqe;
7211     rdp_context->ndlp = lpfc_nlp_get(ndlp);
7212     if (!rdp_context->ndlp) {
7213         kfree(rdp_context);
7214         rjt_err = LSRJT_UNABLE_TPC;
7215         goto error;
7216     }
7217     rdp_context->ox_id = bf_get(wqe_rcvoxid,
7218                     &cmd->xmit_els_rsp.wqe_com);
7219     rdp_context->rx_id = bf_get(wqe_ctxt_tag,
7220                     &cmd->xmit_els_rsp.wqe_com);
7221     rdp_context->cmpl = lpfc_els_rdp_cmpl;
7222     if (lpfc_get_rdp_info(phba, rdp_context)) {
7223         lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
7224                  "2423 Unable to send mailbox");
7225         kfree(rdp_context);
7226         rjt_err = LSRJT_UNABLE_TPC;
7227         lpfc_nlp_put(ndlp);
7228         goto error;
7229     }
7230 
7231     return 0;
7232 
7233 rjt_logerr:
7234     rjt_err = LSRJT_LOGICAL_ERR;
7235 
7236 error:
7237     memset(&stat, 0, sizeof(stat));
7238     stat.un.b.lsRjtRsnCode = rjt_err;
7239     stat.un.b.lsRjtRsnCodeExp = rjt_expl;
7240     lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7241     return 1;
7242 }
7243 
7244 
7245 static void
7246 lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7247 {
7248     MAILBOX_t *mb;
7249     IOCB_t *icmd;
7250     union lpfc_wqe128 *wqe;
7251     uint8_t *pcmd;
7252     struct lpfc_iocbq *elsiocb;
7253     struct lpfc_nodelist *ndlp;
7254     struct ls_rjt *stat;
7255     union lpfc_sli4_cfg_shdr *shdr;
7256     struct lpfc_lcb_context *lcb_context;
7257     struct fc_lcb_res_frame *lcb_res;
7258     uint32_t cmdsize, shdr_status, shdr_add_status;
7259     int rc;
7260 
7261     mb = &pmb->u.mb;
7262     lcb_context = (struct lpfc_lcb_context *)pmb->ctx_ndlp;
7263     ndlp = lcb_context->ndlp;
7264     pmb->ctx_ndlp = NULL;
7265     pmb->ctx_buf = NULL;
7266 
7267     shdr = (union lpfc_sli4_cfg_shdr *)
7268             &pmb->u.mqe.un.beacon_config.header.cfg_shdr;
7269     shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7270     shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7271 
7272     lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
7273                 "0194 SET_BEACON_CONFIG mailbox "
7274                 "completed with status x%x add_status x%x,"
7275                 " mbx status x%x\n",
7276                 shdr_status, shdr_add_status, mb->mbxStatus);
7277 
7278     if ((mb->mbxStatus != MBX_SUCCESS) || shdr_status ||
7279         (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE) ||
7280         (shdr_add_status == ADD_STATUS_INVALID_REQUEST)) {
7281         mempool_free(pmb, phba->mbox_mem_pool);
7282         goto error;
7283     }
7284 
7285     mempool_free(pmb, phba->mbox_mem_pool);
7286     cmdsize = sizeof(struct fc_lcb_res_frame);
7287     elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7288             lpfc_max_els_tries, ndlp,
7289             ndlp->nlp_DID, ELS_CMD_ACC);
7290 
7291     /* Decrement the ndlp reference count from previous mbox command */
7292     lpfc_nlp_put(ndlp);
7293 
7294     if (!elsiocb)
7295         goto free_lcb_context;
7296 
7297     lcb_res = (struct fc_lcb_res_frame *)elsiocb->cmd_dmabuf->virt;
7298 
7299     memset(lcb_res, 0, sizeof(struct fc_lcb_res_frame));
7300 
7301     if (phba->sli_rev == LPFC_SLI_REV4) {
7302         wqe = &elsiocb->wqe;
7303         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, lcb_context->rx_id);
7304         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7305                lcb_context->ox_id);
7306     } else {
7307         icmd = &elsiocb->iocb;
7308         icmd->ulpContext = lcb_context->rx_id;
7309         icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
7310     }
7311 
7312     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
7313     *((uint32_t *)(pcmd)) = ELS_CMD_ACC;
7314     lcb_res->lcb_sub_command = lcb_context->sub_command;
7315     lcb_res->lcb_type = lcb_context->type;
7316     lcb_res->capability = lcb_context->capability;
7317     lcb_res->lcb_frequency = lcb_context->frequency;
7318     lcb_res->lcb_duration = lcb_context->duration;
7319     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7320     phba->fc_stat.elsXmitACC++;
7321 
7322     elsiocb->ndlp = lpfc_nlp_get(ndlp);
7323     if (!elsiocb->ndlp) {
7324         lpfc_els_free_iocb(phba, elsiocb);
7325         goto out;
7326     }
7327 
7328     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7329     if (rc == IOCB_ERROR) {
7330         lpfc_els_free_iocb(phba, elsiocb);
7331         lpfc_nlp_put(ndlp);
7332     }
7333  out:
7334     kfree(lcb_context);
7335     return;
7336 
7337 error:
7338     cmdsize = sizeof(struct fc_lcb_res_frame);
7339     elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7340                      lpfc_max_els_tries, ndlp,
7341                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
7342     lpfc_nlp_put(ndlp);
7343     if (!elsiocb)
7344         goto free_lcb_context;
7345 
7346     if (phba->sli_rev == LPFC_SLI_REV4) {
7347         wqe = &elsiocb->wqe;
7348         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, lcb_context->rx_id);
7349         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7350                lcb_context->ox_id);
7351     } else {
7352         icmd = &elsiocb->iocb;
7353         icmd->ulpContext = lcb_context->rx_id;
7354         icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
7355     }
7356 
7357     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
7358 
7359     *((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
7360     stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
7361     stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7362 
7363     if (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE)
7364         stat->un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
7365 
7366     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7367     phba->fc_stat.elsXmitLSRJT++;
7368     elsiocb->ndlp = lpfc_nlp_get(ndlp);
7369     if (!elsiocb->ndlp) {
7370         lpfc_els_free_iocb(phba, elsiocb);
7371         goto free_lcb_context;
7372     }
7373 
7374     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7375     if (rc == IOCB_ERROR) {
7376         lpfc_els_free_iocb(phba, elsiocb);
7377         lpfc_nlp_put(ndlp);
7378     }
7379 free_lcb_context:
7380     kfree(lcb_context);
7381 }
7382 
7383 static int
7384 lpfc_sli4_set_beacon(struct lpfc_vport *vport,
7385              struct lpfc_lcb_context *lcb_context,
7386              uint32_t beacon_state)
7387 {
7388     struct lpfc_hba *phba = vport->phba;
7389     union lpfc_sli4_cfg_shdr *cfg_shdr;
7390     LPFC_MBOXQ_t *mbox = NULL;
7391     uint32_t len;
7392     int rc;
7393 
7394     mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7395     if (!mbox)
7396         return 1;
7397 
7398     cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
7399     len = sizeof(struct lpfc_mbx_set_beacon_config) -
7400         sizeof(struct lpfc_sli4_cfg_mhdr);
7401     lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7402              LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
7403              LPFC_SLI4_MBX_EMBED);
7404     mbox->ctx_ndlp = (void *)lcb_context;
7405     mbox->vport = phba->pport;
7406     mbox->mbox_cmpl = lpfc_els_lcb_rsp;
7407     bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
7408            phba->sli4_hba.physical_port);
7409     bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
7410            beacon_state);
7411     mbox->u.mqe.un.beacon_config.word5 = 0;     /* Reserved */
7412 
7413     /*
7414      *  Check bv1s bit before issuing the mailbox
7415      *  if bv1s == 1, LCB V1 supported
7416      *  else, LCB V0 supported
7417      */
7418 
7419     if (phba->sli4_hba.pc_sli4_params.bv1s) {
7420         /* COMMON_SET_BEACON_CONFIG_V1 */
7421         cfg_shdr->request.word9 = BEACON_VERSION_V1;
7422         lcb_context->capability |= LCB_CAPABILITY_DURATION;
7423         bf_set(lpfc_mbx_set_beacon_port_type,
7424                &mbox->u.mqe.un.beacon_config, 0);
7425         bf_set(lpfc_mbx_set_beacon_duration_v1,
7426                &mbox->u.mqe.un.beacon_config,
7427                be16_to_cpu(lcb_context->duration));
7428     } else {
7429         /* COMMON_SET_BEACON_CONFIG_V0 */
7430         if (be16_to_cpu(lcb_context->duration) != 0) {
7431             mempool_free(mbox, phba->mbox_mem_pool);
7432             return 1;
7433         }
7434         cfg_shdr->request.word9 = BEACON_VERSION_V0;
7435         lcb_context->capability &=  ~(LCB_CAPABILITY_DURATION);
7436         bf_set(lpfc_mbx_set_beacon_state,
7437                &mbox->u.mqe.un.beacon_config, beacon_state);
7438         bf_set(lpfc_mbx_set_beacon_port_type,
7439                &mbox->u.mqe.un.beacon_config, 1);
7440         bf_set(lpfc_mbx_set_beacon_duration,
7441                &mbox->u.mqe.un.beacon_config,
7442                be16_to_cpu(lcb_context->duration));
7443     }
7444 
7445     rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7446     if (rc == MBX_NOT_FINISHED) {
7447         mempool_free(mbox, phba->mbox_mem_pool);
7448         return 1;
7449     }
7450 
7451     return 0;
7452 }
7453 
7454 
7455 /**
7456  * lpfc_els_rcv_lcb - Process an unsolicited LCB
7457  * @vport: pointer to a host virtual N_Port data structure.
7458  * @cmdiocb: pointer to lpfc command iocb data structure.
7459  * @ndlp: pointer to a node-list data structure.
7460  *
7461  * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
7462  * First, the payload of the unsolicited LCB is checked.
7463  * Then based on Subcommand beacon will either turn on or off.
7464  *
7465  * Return code
7466  * 0 - Sent the acc response
7467  * 1 - Sent the reject response.
7468  **/
7469 static int
7470 lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7471          struct lpfc_nodelist *ndlp)
7472 {
7473     struct lpfc_hba *phba = vport->phba;
7474     struct lpfc_dmabuf *pcmd;
7475     uint8_t *lp;
7476     struct fc_lcb_request_frame *beacon;
7477     struct lpfc_lcb_context *lcb_context;
7478     u8 state, rjt_err = 0;
7479     struct ls_rjt stat;
7480 
7481     pcmd = cmdiocb->cmd_dmabuf;
7482     lp = (uint8_t *)pcmd->virt;
7483     beacon = (struct fc_lcb_request_frame *)pcmd->virt;
7484 
7485     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7486             "0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
7487             "type x%x frequency %x duration x%x\n",
7488             lp[0], lp[1], lp[2],
7489             beacon->lcb_command,
7490             beacon->lcb_sub_command,
7491             beacon->lcb_type,
7492             beacon->lcb_frequency,
7493             be16_to_cpu(beacon->lcb_duration));
7494 
7495     if (beacon->lcb_sub_command != LPFC_LCB_ON &&
7496         beacon->lcb_sub_command != LPFC_LCB_OFF) {
7497         rjt_err = LSRJT_CMD_UNSUPPORTED;
7498         goto rjt;
7499     }
7500 
7501     if (phba->sli_rev < LPFC_SLI_REV4  ||
7502         phba->hba_flag & HBA_FCOE_MODE ||
7503         (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
7504         LPFC_SLI_INTF_IF_TYPE_2)) {
7505         rjt_err = LSRJT_CMD_UNSUPPORTED;
7506         goto rjt;
7507     }
7508 
7509     lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
7510     if (!lcb_context) {
7511         rjt_err = LSRJT_UNABLE_TPC;
7512         goto rjt;
7513     }
7514 
7515     state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
7516     lcb_context->sub_command = beacon->lcb_sub_command;
7517     lcb_context->capability = 0;
7518     lcb_context->type = beacon->lcb_type;
7519     lcb_context->frequency = beacon->lcb_frequency;
7520     lcb_context->duration = beacon->lcb_duration;
7521     lcb_context->ox_id = get_job_rcvoxid(phba, cmdiocb);
7522     lcb_context->rx_id = get_job_ulpcontext(phba, cmdiocb);
7523     lcb_context->ndlp = lpfc_nlp_get(ndlp);
7524     if (!lcb_context->ndlp) {
7525         rjt_err = LSRJT_UNABLE_TPC;
7526         goto rjt_free;
7527     }
7528 
7529     if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
7530         lpfc_printf_vlog(ndlp->vport, KERN_ERR, LOG_TRACE_EVENT,
7531                  "0193 failed to send mail box");
7532         lpfc_nlp_put(ndlp);
7533         rjt_err = LSRJT_UNABLE_TPC;
7534         goto rjt_free;
7535     }
7536     return 0;
7537 
7538 rjt_free:
7539     kfree(lcb_context);
7540 rjt:
7541     memset(&stat, 0, sizeof(stat));
7542     stat.un.b.lsRjtRsnCode = rjt_err;
7543     lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7544     return 1;
7545 }
7546 
7547 
7548 /**
7549  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
7550  * @vport: pointer to a host virtual N_Port data structure.
7551  *
7552  * This routine cleans up any Registration State Change Notification
7553  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
7554  * @vport together with the host_lock is used to prevent multiple thread
7555  * trying to access the RSCN array on a same @vport at the same time.
7556  **/
7557 void
7558 lpfc_els_flush_rscn(struct lpfc_vport *vport)
7559 {
7560     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7561     struct lpfc_hba  *phba = vport->phba;
7562     int i;
7563 
7564     spin_lock_irq(shost->host_lock);
7565     if (vport->fc_rscn_flush) {
7566         /* Another thread is walking fc_rscn_id_list on this vport */
7567         spin_unlock_irq(shost->host_lock);
7568         return;
7569     }
7570     /* Indicate we are walking lpfc_els_flush_rscn on this vport */
7571     vport->fc_rscn_flush = 1;
7572     spin_unlock_irq(shost->host_lock);
7573 
7574     for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
7575         lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
7576         vport->fc_rscn_id_list[i] = NULL;
7577     }
7578     spin_lock_irq(shost->host_lock);
7579     vport->fc_rscn_id_cnt = 0;
7580     vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
7581     spin_unlock_irq(shost->host_lock);
7582     lpfc_can_disctmo(vport);
7583     /* Indicate we are done walking this fc_rscn_id_list */
7584     vport->fc_rscn_flush = 0;
7585 }
7586 
7587 /**
7588  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
7589  * @vport: pointer to a host virtual N_Port data structure.
7590  * @did: remote destination port identifier.
7591  *
7592  * This routine checks whether there is any pending Registration State
7593  * Configuration Notification (RSCN) to a @did on @vport.
7594  *
7595  * Return code
7596  *   None zero - The @did matched with a pending rscn
7597  *   0 - not able to match @did with a pending rscn
7598  **/
7599 int
7600 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
7601 {
7602     D_ID ns_did;
7603     D_ID rscn_did;
7604     uint32_t *lp;
7605     uint32_t payload_len, i;
7606     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7607 
7608     ns_did.un.word = did;
7609 
7610     /* Never match fabric nodes for RSCNs */
7611     if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
7612         return 0;
7613 
7614     /* If we are doing a FULL RSCN rediscovery, match everything */
7615     if (vport->fc_flag & FC_RSCN_DISCOVERY)
7616         return did;
7617 
7618     spin_lock_irq(shost->host_lock);
7619     if (vport->fc_rscn_flush) {
7620         /* Another thread is walking fc_rscn_id_list on this vport */
7621         spin_unlock_irq(shost->host_lock);
7622         return 0;
7623     }
7624     /* Indicate we are walking fc_rscn_id_list on this vport */
7625     vport->fc_rscn_flush = 1;
7626     spin_unlock_irq(shost->host_lock);
7627     for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
7628         lp = vport->fc_rscn_id_list[i]->virt;
7629         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
7630         payload_len -= sizeof(uint32_t);    /* take off word 0 */
7631         while (payload_len) {
7632             rscn_did.un.word = be32_to_cpu(*lp++);
7633             payload_len -= sizeof(uint32_t);
7634             switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
7635             case RSCN_ADDRESS_FORMAT_PORT:
7636                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
7637                     && (ns_did.un.b.area == rscn_did.un.b.area)
7638                     && (ns_did.un.b.id == rscn_did.un.b.id))
7639                     goto return_did_out;
7640                 break;
7641             case RSCN_ADDRESS_FORMAT_AREA:
7642                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
7643                     && (ns_did.un.b.area == rscn_did.un.b.area))
7644                     goto return_did_out;
7645                 break;
7646             case RSCN_ADDRESS_FORMAT_DOMAIN:
7647                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
7648                     goto return_did_out;
7649                 break;
7650             case RSCN_ADDRESS_FORMAT_FABRIC:
7651                 goto return_did_out;
7652             }
7653         }
7654     }
7655     /* Indicate we are done with walking fc_rscn_id_list on this vport */
7656     vport->fc_rscn_flush = 0;
7657     return 0;
7658 return_did_out:
7659     /* Indicate we are done with walking fc_rscn_id_list on this vport */
7660     vport->fc_rscn_flush = 0;
7661     return did;
7662 }
7663 
7664 /**
7665  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
7666  * @vport: pointer to a host virtual N_Port data structure.
7667  *
7668  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
7669  * state machine for a @vport's nodes that are with pending RSCN (Registration
7670  * State Change Notification).
7671  *
7672  * Return code
7673  *   0 - Successful (currently alway return 0)
7674  **/
7675 static int
7676 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
7677 {
7678     struct lpfc_nodelist *ndlp = NULL, *n;
7679 
7680     /* Move all affected nodes by pending RSCNs to NPR state. */
7681     list_for_each_entry_safe(ndlp, n, &vport->fc_nodes, nlp_listp) {
7682         if ((ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
7683             !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
7684             continue;
7685 
7686         /* NVME Target mode does not do RSCN Recovery. */
7687         if (vport->phba->nvmet_support)
7688             continue;
7689 
7690         /* If we are in the process of doing discovery on this
7691          * NPort, let it continue on its own.
7692          */
7693         switch (ndlp->nlp_state) {
7694         case  NLP_STE_PLOGI_ISSUE:
7695         case  NLP_STE_ADISC_ISSUE:
7696         case  NLP_STE_REG_LOGIN_ISSUE:
7697         case  NLP_STE_PRLI_ISSUE:
7698         case  NLP_STE_LOGO_ISSUE:
7699             continue;
7700         }
7701 
7702         lpfc_disc_state_machine(vport, ndlp, NULL,
7703                     NLP_EVT_DEVICE_RECOVERY);
7704         lpfc_cancel_retry_delay_tmo(vport, ndlp);
7705     }
7706     return 0;
7707 }
7708 
7709 /**
7710  * lpfc_send_rscn_event - Send an RSCN event to management application
7711  * @vport: pointer to a host virtual N_Port data structure.
7712  * @cmdiocb: pointer to lpfc command iocb data structure.
7713  *
7714  * lpfc_send_rscn_event sends an RSCN netlink event to management
7715  * applications.
7716  */
7717 static void
7718 lpfc_send_rscn_event(struct lpfc_vport *vport,
7719         struct lpfc_iocbq *cmdiocb)
7720 {
7721     struct lpfc_dmabuf *pcmd;
7722     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7723     uint32_t *payload_ptr;
7724     uint32_t payload_len;
7725     struct lpfc_rscn_event_header *rscn_event_data;
7726 
7727     pcmd = cmdiocb->cmd_dmabuf;
7728     payload_ptr = (uint32_t *) pcmd->virt;
7729     payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
7730 
7731     rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
7732         payload_len, GFP_KERNEL);
7733     if (!rscn_event_data) {
7734         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
7735             "0147 Failed to allocate memory for RSCN event\n");
7736         return;
7737     }
7738     rscn_event_data->event_type = FC_REG_RSCN_EVENT;
7739     rscn_event_data->payload_length = payload_len;
7740     memcpy(rscn_event_data->rscn_payload, payload_ptr,
7741         payload_len);
7742 
7743     fc_host_post_vendor_event(shost,
7744         fc_get_event_number(),
7745         sizeof(struct lpfc_rscn_event_header) + payload_len,
7746         (char *)rscn_event_data,
7747         LPFC_NL_VENDOR_ID);
7748 
7749     kfree(rscn_event_data);
7750 }
7751 
7752 /**
7753  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
7754  * @vport: pointer to a host virtual N_Port data structure.
7755  * @cmdiocb: pointer to lpfc command iocb data structure.
7756  * @ndlp: pointer to a node-list data structure.
7757  *
7758  * This routine processes an unsolicited RSCN (Registration State Change
7759  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
7760  * to invoke fc_host_post_event() routine to the FC transport layer. If the
7761  * discover state machine is about to begin discovery, it just accepts the
7762  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
7763  * contains N_Port IDs for other vports on this HBA, it just accepts the
7764  * RSCN and ignore processing it. If the state machine is in the recovery
7765  * state, the fc_rscn_id_list of this @vport is walked and the
7766  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
7767  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
7768  * routine is invoked to handle the RSCN event.
7769  *
7770  * Return code
7771  *   0 - Just sent the acc response
7772  *   1 - Sent the acc response and waited for name server completion
7773  **/
7774 static int
7775 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7776           struct lpfc_nodelist *ndlp)
7777 {
7778     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7779     struct lpfc_hba  *phba = vport->phba;
7780     struct lpfc_dmabuf *pcmd;
7781     uint32_t *lp, *datap;
7782     uint32_t payload_len, length, nportid, *cmd;
7783     int rscn_cnt;
7784     int rscn_id = 0, hba_id = 0;
7785     int i, tmo;
7786 
7787     pcmd = cmdiocb->cmd_dmabuf;
7788     lp = (uint32_t *) pcmd->virt;
7789 
7790     payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
7791     payload_len -= sizeof(uint32_t);    /* take off word 0 */
7792     /* RSCN received */
7793     lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7794              "0214 RSCN received Data: x%x x%x x%x x%x\n",
7795              vport->fc_flag, payload_len, *lp,
7796              vport->fc_rscn_id_cnt);
7797 
7798     /* Send an RSCN event to the management application */
7799     lpfc_send_rscn_event(vport, cmdiocb);
7800 
7801     for (i = 0; i < payload_len/sizeof(uint32_t); i++)
7802         fc_host_post_event(shost, fc_get_event_number(),
7803             FCH_EVT_RSCN, lp[i]);
7804 
7805     /* Check if RSCN is coming from a direct-connected remote NPort */
7806     if (vport->fc_flag & FC_PT2PT) {
7807         /* If so, just ACC it, no other action needed for now */
7808         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7809                  "2024 pt2pt RSCN %08x Data: x%x x%x\n",
7810                  *lp, vport->fc_flag, payload_len);
7811         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7812 
7813         /* Check to see if we need to NVME rescan this target
7814          * remoteport.
7815          */
7816         if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
7817             ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
7818             lpfc_nvme_rescan_port(vport, ndlp);
7819         return 0;
7820     }
7821 
7822     /* If we are about to begin discovery, just ACC the RSCN.
7823      * Discovery processing will satisfy it.
7824      */
7825     if (vport->port_state <= LPFC_NS_QRY) {
7826         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7827             "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
7828             ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
7829 
7830         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7831         return 0;
7832     }
7833 
7834     /* If this RSCN just contains NPortIDs for other vports on this HBA,
7835      * just ACC and ignore it.
7836      */
7837     if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
7838         !(vport->cfg_peer_port_login)) {
7839         i = payload_len;
7840         datap = lp;
7841         while (i > 0) {
7842             nportid = *datap++;
7843             nportid = ((be32_to_cpu(nportid)) & Mask_DID);
7844             i -= sizeof(uint32_t);
7845             rscn_id++;
7846             if (lpfc_find_vport_by_did(phba, nportid))
7847                 hba_id++;
7848         }
7849         if (rscn_id == hba_id) {
7850             /* ALL NPortIDs in RSCN are on HBA */
7851             lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7852                      "0219 Ignore RSCN "
7853                      "Data: x%x x%x x%x x%x\n",
7854                      vport->fc_flag, payload_len,
7855                      *lp, vport->fc_rscn_id_cnt);
7856             lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7857                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
7858                 ndlp->nlp_DID, vport->port_state,
7859                 ndlp->nlp_flag);
7860 
7861             lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
7862                 ndlp, NULL);
7863             /* Restart disctmo if its already running */
7864             if (vport->fc_flag & FC_DISC_TMO) {
7865                 tmo = ((phba->fc_ratov * 3) + 3);
7866                 mod_timer(&vport->fc_disctmo,
7867                       jiffies +
7868                       msecs_to_jiffies(1000 * tmo));
7869             }
7870             return 0;
7871         }
7872     }
7873 
7874     spin_lock_irq(shost->host_lock);
7875     if (vport->fc_rscn_flush) {
7876         /* Another thread is walking fc_rscn_id_list on this vport */
7877         vport->fc_flag |= FC_RSCN_DISCOVERY;
7878         spin_unlock_irq(shost->host_lock);
7879         /* Send back ACC */
7880         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7881         return 0;
7882     }
7883     /* Indicate we are walking fc_rscn_id_list on this vport */
7884     vport->fc_rscn_flush = 1;
7885     spin_unlock_irq(shost->host_lock);
7886     /* Get the array count after successfully have the token */
7887     rscn_cnt = vport->fc_rscn_id_cnt;
7888     /* If we are already processing an RSCN, save the received
7889      * RSCN payload buffer, cmdiocb->cmd_dmabuf to process later.
7890      */
7891     if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
7892         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7893             "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
7894             ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
7895 
7896         spin_lock_irq(shost->host_lock);
7897         vport->fc_flag |= FC_RSCN_DEFERRED;
7898 
7899         /* Restart disctmo if its already running */
7900         if (vport->fc_flag & FC_DISC_TMO) {
7901             tmo = ((phba->fc_ratov * 3) + 3);
7902             mod_timer(&vport->fc_disctmo,
7903                   jiffies + msecs_to_jiffies(1000 * tmo));
7904         }
7905         if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
7906             !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
7907             vport->fc_flag |= FC_RSCN_MODE;
7908             spin_unlock_irq(shost->host_lock);
7909             if (rscn_cnt) {
7910                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
7911                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
7912             }
7913             if ((rscn_cnt) &&
7914                 (payload_len + length <= LPFC_BPL_SIZE)) {
7915                 *cmd &= ELS_CMD_MASK;
7916                 *cmd |= cpu_to_be32(payload_len + length);
7917                 memcpy(((uint8_t *)cmd) + length, lp,
7918                        payload_len);
7919             } else {
7920                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
7921                 vport->fc_rscn_id_cnt++;
7922                 /* If we zero, cmdiocb->cmd_dmabuf, the calling
7923                  * routine will not try to free it.
7924                  */
7925                 cmdiocb->cmd_dmabuf = NULL;
7926             }
7927             /* Deferred RSCN */
7928             lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7929                      "0235 Deferred RSCN "
7930                      "Data: x%x x%x x%x\n",
7931                      vport->fc_rscn_id_cnt, vport->fc_flag,
7932                      vport->port_state);
7933         } else {
7934             vport->fc_flag |= FC_RSCN_DISCOVERY;
7935             spin_unlock_irq(shost->host_lock);
7936             /* ReDiscovery RSCN */
7937             lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7938                      "0234 ReDiscovery RSCN "
7939                      "Data: x%x x%x x%x\n",
7940                      vport->fc_rscn_id_cnt, vport->fc_flag,
7941                      vport->port_state);
7942         }
7943         /* Indicate we are done walking fc_rscn_id_list on this vport */
7944         vport->fc_rscn_flush = 0;
7945         /* Send back ACC */
7946         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7947         /* send RECOVERY event for ALL nodes that match RSCN payload */
7948         lpfc_rscn_recovery_check(vport);
7949         return 0;
7950     }
7951     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7952         "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
7953         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
7954 
7955     spin_lock_irq(shost->host_lock);
7956     vport->fc_flag |= FC_RSCN_MODE;
7957     spin_unlock_irq(shost->host_lock);
7958     vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
7959     /* Indicate we are done walking fc_rscn_id_list on this vport */
7960     vport->fc_rscn_flush = 0;
7961     /*
7962      * If we zero, cmdiocb->cmd_dmabuf, the calling routine will
7963      * not try to free it.
7964      */
7965     cmdiocb->cmd_dmabuf = NULL;
7966     lpfc_set_disctmo(vport);
7967     /* Send back ACC */
7968     lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7969     /* send RECOVERY event for ALL nodes that match RSCN payload */
7970     lpfc_rscn_recovery_check(vport);
7971     return lpfc_els_handle_rscn(vport);
7972 }
7973 
7974 /**
7975  * lpfc_els_handle_rscn - Handle rscn for a vport
7976  * @vport: pointer to a host virtual N_Port data structure.
7977  *
7978  * This routine handles the Registration State Configuration Notification
7979  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
7980  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
7981  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
7982  * NameServer shall be issued. If CT command to the NameServer fails to be
7983  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
7984  * RSCN activities with the @vport.
7985  *
7986  * Return code
7987  *   0 - Cleaned up rscn on the @vport
7988  *   1 - Wait for plogi to name server before proceed
7989  **/
7990 int
7991 lpfc_els_handle_rscn(struct lpfc_vport *vport)
7992 {
7993     struct lpfc_nodelist *ndlp;
7994     struct lpfc_hba  *phba = vport->phba;
7995 
7996     /* Ignore RSCN if the port is being torn down. */
7997     if (vport->load_flag & FC_UNLOADING) {
7998         lpfc_els_flush_rscn(vport);
7999         return 0;
8000     }
8001 
8002     /* Start timer for RSCN processing */
8003     lpfc_set_disctmo(vport);
8004 
8005     /* RSCN processed */
8006     lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
8007              "0215 RSCN processed Data: x%x x%x x%x x%x x%x x%x\n",
8008              vport->fc_flag, 0, vport->fc_rscn_id_cnt,
8009              vport->port_state, vport->num_disc_nodes,
8010              vport->gidft_inp);
8011 
8012     /* To process RSCN, first compare RSCN data with NameServer */
8013     vport->fc_ns_retry = 0;
8014     vport->num_disc_nodes = 0;
8015 
8016     ndlp = lpfc_findnode_did(vport, NameServer_DID);
8017     if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
8018         /* Good ndlp, issue CT Request to NameServer.  Need to
8019          * know how many gidfts were issued.  If none, then just
8020          * flush the RSCN.  Otherwise, the outstanding requests
8021          * need to complete.
8022          */
8023         if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT) {
8024             if (lpfc_issue_gidft(vport) > 0)
8025                 return 1;
8026         } else if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_PT) {
8027             if (lpfc_issue_gidpt(vport) > 0)
8028                 return 1;
8029         } else {
8030             return 1;
8031         }
8032     } else {
8033         /* Nameserver login in question.  Revalidate. */
8034         if (ndlp) {
8035             ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
8036             lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8037         } else {
8038             ndlp = lpfc_nlp_init(vport, NameServer_DID);
8039             if (!ndlp) {
8040                 lpfc_els_flush_rscn(vport);
8041                 return 0;
8042             }
8043             ndlp->nlp_prev_state = ndlp->nlp_state;
8044             lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8045         }
8046         ndlp->nlp_type |= NLP_FABRIC;
8047         lpfc_issue_els_plogi(vport, NameServer_DID, 0);
8048         /* Wait for NameServer login cmpl before we can
8049          * continue
8050          */
8051         return 1;
8052     }
8053 
8054     lpfc_els_flush_rscn(vport);
8055     return 0;
8056 }
8057 
8058 /**
8059  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
8060  * @vport: pointer to a host virtual N_Port data structure.
8061  * @cmdiocb: pointer to lpfc command iocb data structure.
8062  * @ndlp: pointer to a node-list data structure.
8063  *
8064  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
8065  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
8066  * point topology. As an unsolicited FLOGI should not be received in a loop
8067  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
8068  * lpfc_check_sparm() routine is invoked to check the parameters in the
8069  * unsolicited FLOGI. If parameters validation failed, the routine
8070  * lpfc_els_rsp_reject() shall be called with reject reason code set to
8071  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
8072  * FLOGI shall be compared with the Port WWN of the @vport to determine who
8073  * will initiate PLOGI. The higher lexicographical value party shall has
8074  * higher priority (as the winning port) and will initiate PLOGI and
8075  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
8076  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
8077  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
8078  *
8079  * Return code
8080  *   0 - Successfully processed the unsolicited flogi
8081  *   1 - Failed to process the unsolicited flogi
8082  **/
8083 static int
8084 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8085            struct lpfc_nodelist *ndlp)
8086 {
8087     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8088     struct lpfc_hba  *phba = vport->phba;
8089     struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
8090     uint32_t *lp = (uint32_t *) pcmd->virt;
8091     union lpfc_wqe128 *wqe = &cmdiocb->wqe;
8092     struct serv_parm *sp;
8093     LPFC_MBOXQ_t *mbox;
8094     uint32_t cmd, did;
8095     int rc;
8096     uint32_t fc_flag = 0;
8097     uint32_t port_state = 0;
8098 
8099     /* Clear external loopback plug detected flag */
8100     phba->link_flag &= ~LS_EXTERNAL_LOOPBACK;
8101 
8102     cmd = *lp++;
8103     sp = (struct serv_parm *) lp;
8104 
8105     /* FLOGI received */
8106 
8107     lpfc_set_disctmo(vport);
8108 
8109     if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8110         /* We should never receive a FLOGI in loop mode, ignore it */
8111         did =  bf_get(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest);
8112 
8113         /* An FLOGI ELS command <elsCmd> was received from DID <did> in
8114            Loop Mode */
8115         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
8116                  "0113 An FLOGI ELS command x%x was "
8117                  "received from DID x%x in Loop Mode\n",
8118                  cmd, did);
8119         return 1;
8120     }
8121 
8122     (void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
8123 
8124     /*
8125      * If our portname is greater than the remote portname,
8126      * then we initiate Nport login.
8127      */
8128 
8129     rc = memcmp(&vport->fc_portname, &sp->portName,
8130             sizeof(struct lpfc_name));
8131 
8132     if (!rc) {
8133         if (phba->sli_rev < LPFC_SLI_REV4) {
8134             mbox = mempool_alloc(phba->mbox_mem_pool,
8135                          GFP_KERNEL);
8136             if (!mbox)
8137                 return 1;
8138             lpfc_linkdown(phba);
8139             lpfc_init_link(phba, mbox,
8140                        phba->cfg_topology,
8141                        phba->cfg_link_speed);
8142             mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
8143             mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8144             mbox->vport = vport;
8145             rc = lpfc_sli_issue_mbox(phba, mbox,
8146                          MBX_NOWAIT);
8147             lpfc_set_loopback_flag(phba);
8148             if (rc == MBX_NOT_FINISHED)
8149                 mempool_free(mbox, phba->mbox_mem_pool);
8150             return 1;
8151         }
8152 
8153         /* External loopback plug insertion detected */
8154         phba->link_flag |= LS_EXTERNAL_LOOPBACK;
8155 
8156         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_LIBDFC,
8157                  "1119 External Loopback plug detected\n");
8158 
8159         /* abort the flogi coming back to ourselves
8160          * due to external loopback on the port.
8161          */
8162         lpfc_els_abort_flogi(phba);
8163         return 0;
8164 
8165     } else if (rc > 0) {    /* greater than */
8166         spin_lock_irq(shost->host_lock);
8167         vport->fc_flag |= FC_PT2PT_PLOGI;
8168         spin_unlock_irq(shost->host_lock);
8169 
8170         /* If we have the high WWPN we can assign our own
8171          * myDID; otherwise, we have to WAIT for a PLOGI
8172          * from the remote NPort to find out what it
8173          * will be.
8174          */
8175         vport->fc_myDID = PT2PT_LocalID;
8176     } else {
8177         vport->fc_myDID = PT2PT_RemoteID;
8178     }
8179 
8180     /*
8181      * The vport state should go to LPFC_FLOGI only
8182      * AFTER we issue a FLOGI, not receive one.
8183      */
8184     spin_lock_irq(shost->host_lock);
8185     fc_flag = vport->fc_flag;
8186     port_state = vport->port_state;
8187     vport->fc_flag |= FC_PT2PT;
8188     vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
8189 
8190     /* Acking an unsol FLOGI.  Count 1 for link bounce
8191      * work-around.
8192      */
8193     vport->rcv_flogi_cnt++;
8194     spin_unlock_irq(shost->host_lock);
8195     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8196              "3311 Rcv Flogi PS x%x new PS x%x "
8197              "fc_flag x%x new fc_flag x%x\n",
8198              port_state, vport->port_state,
8199              fc_flag, vport->fc_flag);
8200 
8201     /*
8202      * We temporarily set fc_myDID to make it look like we are
8203      * a Fabric. This is done just so we end up with the right
8204      * did / sid on the FLOGI ACC rsp.
8205      */
8206     did = vport->fc_myDID;
8207     vport->fc_myDID = Fabric_DID;
8208 
8209     memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
8210 
8211     /* Defer ACC response until AFTER we issue a FLOGI */
8212     if (!(phba->hba_flag & HBA_FLOGI_ISSUED)) {
8213         phba->defer_flogi_acc_rx_id = bf_get(wqe_ctxt_tag,
8214                              &wqe->xmit_els_rsp.wqe_com);
8215         phba->defer_flogi_acc_ox_id = bf_get(wqe_rcvoxid,
8216                              &wqe->xmit_els_rsp.wqe_com);
8217 
8218         vport->fc_myDID = did;
8219 
8220         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8221                  "3344 Deferring FLOGI ACC: rx_id: x%x,"
8222                  " ox_id: x%x, hba_flag x%x\n",
8223                  phba->defer_flogi_acc_rx_id,
8224                  phba->defer_flogi_acc_ox_id, phba->hba_flag);
8225 
8226         phba->defer_flogi_acc_flag = true;
8227 
8228         return 0;
8229     }
8230 
8231     /* Send back ACC */
8232     lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
8233 
8234     /* Now lets put fc_myDID back to what its supposed to be */
8235     vport->fc_myDID = did;
8236 
8237     return 0;
8238 }
8239 
8240 /**
8241  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
8242  * @vport: pointer to a host virtual N_Port data structure.
8243  * @cmdiocb: pointer to lpfc command iocb data structure.
8244  * @ndlp: pointer to a node-list data structure.
8245  *
8246  * This routine processes Request Node Identification Data (RNID) IOCB
8247  * received as an ELS unsolicited event. Only when the RNID specified format
8248  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
8249  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
8250  * Accept (ACC) the RNID ELS command. All the other RNID formats are
8251  * rejected by invoking the lpfc_els_rsp_reject() routine.
8252  *
8253  * Return code
8254  *   0 - Successfully processed rnid iocb (currently always return 0)
8255  **/
8256 static int
8257 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8258           struct lpfc_nodelist *ndlp)
8259 {
8260     struct lpfc_dmabuf *pcmd;
8261     uint32_t *lp;
8262     RNID *rn;
8263     struct ls_rjt stat;
8264 
8265     pcmd = cmdiocb->cmd_dmabuf;
8266     lp = (uint32_t *) pcmd->virt;
8267 
8268     lp++;
8269     rn = (RNID *) lp;
8270 
8271     /* RNID received */
8272 
8273     switch (rn->Format) {
8274     case 0:
8275     case RNID_TOPOLOGY_DISC:
8276         /* Send back ACC */
8277         lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
8278         break;
8279     default:
8280         /* Reject this request because format not supported */
8281         stat.un.b.lsRjtRsvd0 = 0;
8282         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8283         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8284         stat.un.b.vendorUnique = 0;
8285         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
8286             NULL);
8287     }
8288     return 0;
8289 }
8290 
8291 /**
8292  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
8293  * @vport: pointer to a host virtual N_Port data structure.
8294  * @cmdiocb: pointer to lpfc command iocb data structure.
8295  * @ndlp: pointer to a node-list data structure.
8296  *
8297  * Return code
8298  *   0 - Successfully processed echo iocb (currently always return 0)
8299  **/
8300 static int
8301 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8302           struct lpfc_nodelist *ndlp)
8303 {
8304     uint8_t *pcmd;
8305 
8306     pcmd = (uint8_t *)cmdiocb->cmd_dmabuf->virt;
8307 
8308     /* skip over first word of echo command to find echo data */
8309     pcmd += sizeof(uint32_t);
8310 
8311     lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
8312     return 0;
8313 }
8314 
8315 /**
8316  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
8317  * @vport: pointer to a host virtual N_Port data structure.
8318  * @cmdiocb: pointer to lpfc command iocb data structure.
8319  * @ndlp: pointer to a node-list data structure.
8320  *
8321  * This routine processes a Link Incident Report Registration(LIRR) IOCB
8322  * received as an ELS unsolicited event. Currently, this function just invokes
8323  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
8324  *
8325  * Return code
8326  *   0 - Successfully processed lirr iocb (currently always return 0)
8327  **/
8328 static int
8329 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8330           struct lpfc_nodelist *ndlp)
8331 {
8332     struct ls_rjt stat;
8333 
8334     /* For now, unconditionally reject this command */
8335     stat.un.b.lsRjtRsvd0 = 0;
8336     stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8337     stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8338     stat.un.b.vendorUnique = 0;
8339     lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8340     return 0;
8341 }
8342 
8343 /**
8344  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
8345  * @vport: pointer to a host virtual N_Port data structure.
8346  * @cmdiocb: pointer to lpfc command iocb data structure.
8347  * @ndlp: pointer to a node-list data structure.
8348  *
8349  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
8350  * received as an ELS unsolicited event. A request to RRQ shall only
8351  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
8352  * Nx_Port N_Port_ID of the target Exchange is the same as the
8353  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
8354  * not accepted, an LS_RJT with reason code "Unable to perform
8355  * command request" and reason code explanation "Invalid Originator
8356  * S_ID" shall be returned. For now, we just unconditionally accept
8357  * RRQ from the target.
8358  **/
8359 static void
8360 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8361          struct lpfc_nodelist *ndlp)
8362 {
8363     lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8364     if (vport->phba->sli_rev == LPFC_SLI_REV4)
8365         lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
8366 }
8367 
8368 /**
8369  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
8370  * @phba: pointer to lpfc hba data structure.
8371  * @pmb: pointer to the driver internal queue element for mailbox command.
8372  *
8373  * This routine is the completion callback function for the MBX_READ_LNK_STAT
8374  * mailbox command. This callback function is to actually send the Accept
8375  * (ACC) response to a Read Link Status (RLS) unsolicited IOCB event. It
8376  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
8377  * mailbox command, constructs the RLS response with the link statistics
8378  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
8379  * response to the RLS.
8380  *
8381  * Note that the ndlp reference count will be incremented by 1 for holding the
8382  * ndlp and the reference to ndlp will be stored into the ndlp field of
8383  * the IOCB for the completion callback function to the RLS Accept Response
8384  * ELS IOCB command.
8385  *
8386  **/
8387 static void
8388 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
8389 {
8390     int rc = 0;
8391     MAILBOX_t *mb;
8392     IOCB_t *icmd;
8393     union lpfc_wqe128 *wqe;
8394     struct RLS_RSP *rls_rsp;
8395     uint8_t *pcmd;
8396     struct lpfc_iocbq *elsiocb;
8397     struct lpfc_nodelist *ndlp;
8398     uint16_t oxid;
8399     uint16_t rxid;
8400     uint32_t cmdsize;
8401     u32 ulp_context;
8402 
8403     mb = &pmb->u.mb;
8404 
8405     ndlp = pmb->ctx_ndlp;
8406     rxid = (uint16_t)((unsigned long)(pmb->ctx_buf) & 0xffff);
8407     oxid = (uint16_t)(((unsigned long)(pmb->ctx_buf) >> 16) & 0xffff);
8408     pmb->ctx_buf = NULL;
8409     pmb->ctx_ndlp = NULL;
8410 
8411     if (mb->mbxStatus) {
8412         mempool_free(pmb, phba->mbox_mem_pool);
8413         return;
8414     }
8415 
8416     cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
8417     elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
8418                      lpfc_max_els_tries, ndlp,
8419                      ndlp->nlp_DID, ELS_CMD_ACC);
8420 
8421     /* Decrement the ndlp reference count from previous mbox command */
8422     lpfc_nlp_put(ndlp);
8423 
8424     if (!elsiocb) {
8425         mempool_free(pmb, phba->mbox_mem_pool);
8426         return;
8427     }
8428 
8429     ulp_context = get_job_ulpcontext(phba, elsiocb);
8430     if (phba->sli_rev == LPFC_SLI_REV4) {
8431         wqe = &elsiocb->wqe;
8432         /* Xri / rx_id */
8433         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, rxid);
8434         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com, oxid);
8435     } else {
8436         icmd = &elsiocb->iocb;
8437         icmd->ulpContext = rxid;
8438         icmd->unsli3.rcvsli3.ox_id = oxid;
8439     }
8440 
8441     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
8442     *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8443     pcmd += sizeof(uint32_t); /* Skip past command */
8444     rls_rsp = (struct RLS_RSP *)pcmd;
8445 
8446     rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
8447     rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
8448     rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
8449     rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
8450     rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
8451     rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
8452     mempool_free(pmb, phba->mbox_mem_pool);
8453     /* Xmit ELS RLS ACC response tag <ulpIoTag> */
8454     lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
8455              "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
8456              "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
8457              elsiocb->iotag, ulp_context,
8458              ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8459              ndlp->nlp_rpi);
8460     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
8461     phba->fc_stat.elsXmitACC++;
8462     elsiocb->ndlp = lpfc_nlp_get(ndlp);
8463     if (!elsiocb->ndlp) {
8464         lpfc_els_free_iocb(phba, elsiocb);
8465         return;
8466     }
8467 
8468     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8469     if (rc == IOCB_ERROR) {
8470         lpfc_els_free_iocb(phba, elsiocb);
8471         lpfc_nlp_put(ndlp);
8472     }
8473     return;
8474 }
8475 
8476 /**
8477  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
8478  * @vport: pointer to a host virtual N_Port data structure.
8479  * @cmdiocb: pointer to lpfc command iocb data structure.
8480  * @ndlp: pointer to a node-list data structure.
8481  *
8482  * This routine processes Read Link Status (RLS) IOCB received as an
8483  * ELS unsolicited event. It first checks the remote port state. If the
8484  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
8485  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
8486  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
8487  * for reading the HBA link statistics. It is for the callback function,
8488  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
8489  * to actually sending out RPL Accept (ACC) response.
8490  *
8491  * Return codes
8492  *   0 - Successfully processed rls iocb (currently always return 0)
8493  **/
8494 static int
8495 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8496          struct lpfc_nodelist *ndlp)
8497 {
8498     struct lpfc_hba *phba = vport->phba;
8499     LPFC_MBOXQ_t *mbox;
8500     struct ls_rjt stat;
8501     u32 ctx = get_job_ulpcontext(phba, cmdiocb);
8502     u32 ox_id = get_job_rcvoxid(phba, cmdiocb);
8503 
8504     if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8505         (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
8506         /* reject the unsolicited RLS request and done with it */
8507         goto reject_out;
8508 
8509     mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
8510     if (mbox) {
8511         lpfc_read_lnk_stat(phba, mbox);
8512         mbox->ctx_buf = (void *)((unsigned long)
8513                      (ox_id << 16 | ctx));
8514         mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
8515         if (!mbox->ctx_ndlp)
8516             goto node_err;
8517         mbox->vport = vport;
8518         mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
8519         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
8520             != MBX_NOT_FINISHED)
8521             /* Mbox completion will send ELS Response */
8522             return 0;
8523         /* Decrement reference count used for the failed mbox
8524          * command.
8525          */
8526         lpfc_nlp_put(ndlp);
8527 node_err:
8528         mempool_free(mbox, phba->mbox_mem_pool);
8529     }
8530 reject_out:
8531     /* issue rejection response */
8532     stat.un.b.lsRjtRsvd0 = 0;
8533     stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8534     stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8535     stat.un.b.vendorUnique = 0;
8536     lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8537     return 0;
8538 }
8539 
8540 /**
8541  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
8542  * @vport: pointer to a host virtual N_Port data structure.
8543  * @cmdiocb: pointer to lpfc command iocb data structure.
8544  * @ndlp: pointer to a node-list data structure.
8545  *
8546  * This routine processes Read Timout Value (RTV) IOCB received as an
8547  * ELS unsolicited event. It first checks the remote port state. If the
8548  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
8549  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
8550  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
8551  * Value (RTV) unsolicited IOCB event.
8552  *
8553  * Note that the ndlp reference count will be incremented by 1 for holding the
8554  * ndlp and the reference to ndlp will be stored into the ndlp field of
8555  * the IOCB for the completion callback function to the RTV Accept Response
8556  * ELS IOCB command.
8557  *
8558  * Return codes
8559  *   0 - Successfully processed rtv iocb (currently always return 0)
8560  **/
8561 static int
8562 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8563          struct lpfc_nodelist *ndlp)
8564 {
8565     int rc = 0;
8566     IOCB_t *icmd;
8567     union lpfc_wqe128 *wqe;
8568     struct lpfc_hba *phba = vport->phba;
8569     struct ls_rjt stat;
8570     struct RTV_RSP *rtv_rsp;
8571     uint8_t *pcmd;
8572     struct lpfc_iocbq *elsiocb;
8573     uint32_t cmdsize;
8574     u32 ulp_context;
8575 
8576     if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8577         (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
8578         /* reject the unsolicited RTV request and done with it */
8579         goto reject_out;
8580 
8581     cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
8582     elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
8583                      lpfc_max_els_tries, ndlp,
8584                      ndlp->nlp_DID, ELS_CMD_ACC);
8585 
8586     if (!elsiocb)
8587         return 1;
8588 
8589     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
8590     *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8591     pcmd += sizeof(uint32_t); /* Skip past command */
8592 
8593     ulp_context = get_job_ulpcontext(phba, elsiocb);
8594     /* use the command's xri in the response */
8595     if (phba->sli_rev == LPFC_SLI_REV4) {
8596         wqe = &elsiocb->wqe;
8597         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
8598                get_job_ulpcontext(phba, cmdiocb));
8599         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8600                get_job_rcvoxid(phba, cmdiocb));
8601     } else {
8602         icmd = &elsiocb->iocb;
8603         icmd->ulpContext = get_job_ulpcontext(phba, cmdiocb);
8604         icmd->unsli3.rcvsli3.ox_id = get_job_rcvoxid(phba, cmdiocb);
8605     }
8606 
8607     rtv_rsp = (struct RTV_RSP *)pcmd;
8608 
8609     /* populate RTV payload */
8610     rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
8611     rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
8612     bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
8613     bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
8614     rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
8615 
8616     /* Xmit ELS RLS ACC response tag <ulpIoTag> */
8617     lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
8618              "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
8619              "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
8620              "Data: x%x x%x x%x\n",
8621              elsiocb->iotag, ulp_context,
8622              ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8623              ndlp->nlp_rpi,
8624             rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
8625     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
8626     phba->fc_stat.elsXmitACC++;
8627     elsiocb->ndlp = lpfc_nlp_get(ndlp);
8628     if (!elsiocb->ndlp) {
8629         lpfc_els_free_iocb(phba, elsiocb);
8630         return 0;
8631     }
8632 
8633     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8634     if (rc == IOCB_ERROR) {
8635         lpfc_els_free_iocb(phba, elsiocb);
8636         lpfc_nlp_put(ndlp);
8637     }
8638     return 0;
8639 
8640 reject_out:
8641     /* issue rejection response */
8642     stat.un.b.lsRjtRsvd0 = 0;
8643     stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8644     stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8645     stat.un.b.vendorUnique = 0;
8646     lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8647     return 0;
8648 }
8649 
8650 /* lpfc_issue_els_rrq - Process an unsolicited rrq iocb
8651  * @vport: pointer to a host virtual N_Port data structure.
8652  * @ndlp: pointer to a node-list data structure.
8653  * @did: DID of the target.
8654  * @rrq: Pointer to the rrq struct.
8655  *
8656  * Build a ELS RRQ command and send it to the target. If the issue_iocb is
8657  * Successful the the completion handler will clear the RRQ.
8658  *
8659  * Return codes
8660  *   0 - Successfully sent rrq els iocb.
8661  *   1 - Failed to send rrq els iocb.
8662  **/
8663 static int
8664 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
8665             uint32_t did, struct lpfc_node_rrq *rrq)
8666 {
8667     struct lpfc_hba  *phba = vport->phba;
8668     struct RRQ *els_rrq;
8669     struct lpfc_iocbq *elsiocb;
8670     uint8_t *pcmd;
8671     uint16_t cmdsize;
8672     int ret;
8673 
8674     if (!ndlp)
8675         return 1;
8676 
8677     /* If ndlp is not NULL, we will bump the reference count on it */
8678     cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
8679     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
8680                      ELS_CMD_RRQ);
8681     if (!elsiocb)
8682         return 1;
8683 
8684     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
8685 
8686     /* For RRQ request, remainder of payload is Exchange IDs */
8687     *((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
8688     pcmd += sizeof(uint32_t);
8689     els_rrq = (struct RRQ *) pcmd;
8690 
8691     bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
8692     bf_set(rrq_rxid, els_rrq, rrq->rxid);
8693     bf_set(rrq_did, els_rrq, vport->fc_myDID);
8694     els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
8695     els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
8696 
8697 
8698     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8699         "Issue RRQ:     did:x%x",
8700         did, rrq->xritag, rrq->rxid);
8701     elsiocb->context_un.rrq = rrq;
8702     elsiocb->cmd_cmpl = lpfc_cmpl_els_rrq;
8703 
8704     elsiocb->ndlp = lpfc_nlp_get(ndlp);
8705     if (!elsiocb->ndlp)
8706         goto io_err;
8707 
8708     ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8709     if (ret == IOCB_ERROR) {
8710         lpfc_nlp_put(ndlp);
8711         goto io_err;
8712     }
8713     return 0;
8714 
8715  io_err:
8716     lpfc_els_free_iocb(phba, elsiocb);
8717     return 1;
8718 }
8719 
8720 /**
8721  * lpfc_send_rrq - Sends ELS RRQ if needed.
8722  * @phba: pointer to lpfc hba data structure.
8723  * @rrq: pointer to the active rrq.
8724  *
8725  * This routine will call the lpfc_issue_els_rrq if the rrq is
8726  * still active for the xri. If this function returns a failure then
8727  * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
8728  *
8729  * Returns 0 Success.
8730  *         1 Failure.
8731  **/
8732 int
8733 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
8734 {
8735     struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
8736                                rrq->nlp_DID);
8737     if (!ndlp)
8738         return 1;
8739 
8740     if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
8741         return lpfc_issue_els_rrq(rrq->vport, ndlp,
8742                      rrq->nlp_DID, rrq);
8743     else
8744         return 1;
8745 }
8746 
8747 /**
8748  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
8749  * @vport: pointer to a host virtual N_Port data structure.
8750  * @cmdsize: size of the ELS command.
8751  * @oldiocb: pointer to the original lpfc command iocb data structure.
8752  * @ndlp: pointer to a node-list data structure.
8753  *
8754  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
8755  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
8756  *
8757  * Note that the ndlp reference count will be incremented by 1 for holding the
8758  * ndlp and the reference to ndlp will be stored into the ndlp field of
8759  * the IOCB for the completion callback function to the RPL Accept Response
8760  * ELS command.
8761  *
8762  * Return code
8763  *   0 - Successfully issued ACC RPL ELS command
8764  *   1 - Failed to issue ACC RPL ELS command
8765  **/
8766 static int
8767 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
8768              struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
8769 {
8770     int rc = 0;
8771     struct lpfc_hba *phba = vport->phba;
8772     IOCB_t *icmd;
8773     union lpfc_wqe128 *wqe;
8774     RPL_RSP rpl_rsp;
8775     struct lpfc_iocbq *elsiocb;
8776     uint8_t *pcmd;
8777     u32 ulp_context;
8778 
8779     elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
8780                      ndlp->nlp_DID, ELS_CMD_ACC);
8781 
8782     if (!elsiocb)
8783         return 1;
8784 
8785     ulp_context = get_job_ulpcontext(phba, elsiocb);
8786     if (phba->sli_rev == LPFC_SLI_REV4) {
8787         wqe = &elsiocb->wqe;
8788         /* Xri / rx_id */
8789         bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
8790                get_job_ulpcontext(phba, oldiocb));
8791         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8792                get_job_rcvoxid(phba, oldiocb));
8793     } else {
8794         icmd = &elsiocb->iocb;
8795         icmd->ulpContext = get_job_ulpcontext(phba, oldiocb);
8796         icmd->unsli3.rcvsli3.ox_id = get_job_rcvoxid(phba, oldiocb);
8797     }
8798 
8799     pcmd = elsiocb->cmd_dmabuf->virt;
8800     *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8801     pcmd += sizeof(uint16_t);
8802     *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
8803     pcmd += sizeof(uint16_t);
8804 
8805     /* Setup the RPL ACC payload */
8806     rpl_rsp.listLen = be32_to_cpu(1);
8807     rpl_rsp.index = 0;
8808     rpl_rsp.port_num_blk.portNum = 0;
8809     rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
8810     memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
8811         sizeof(struct lpfc_name));
8812     memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
8813     /* Xmit ELS RPL ACC response tag <ulpIoTag> */
8814     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8815              "0120 Xmit ELS RPL ACC response tag x%x "
8816              "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
8817              "rpi x%x\n",
8818              elsiocb->iotag, ulp_context,
8819              ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8820              ndlp->nlp_rpi);
8821     elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
8822     phba->fc_stat.elsXmitACC++;
8823     elsiocb->ndlp = lpfc_nlp_get(ndlp);
8824     if (!elsiocb->ndlp) {
8825         lpfc_els_free_iocb(phba, elsiocb);
8826         return 1;
8827     }
8828 
8829     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8830     if (rc == IOCB_ERROR) {
8831         lpfc_els_free_iocb(phba, elsiocb);
8832         lpfc_nlp_put(ndlp);
8833         return 1;
8834     }
8835 
8836     return 0;
8837 }
8838 
8839 /**
8840  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
8841  * @vport: pointer to a host virtual N_Port data structure.
8842  * @cmdiocb: pointer to lpfc command iocb data structure.
8843  * @ndlp: pointer to a node-list data structure.
8844  *
8845  * This routine processes Read Port List (RPL) IOCB received as an ELS
8846  * unsolicited event. It first checks the remote port state. If the remote
8847  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
8848  * invokes the lpfc_els_rsp_reject() routine to send reject response.
8849  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
8850  * to accept the RPL.
8851  *
8852  * Return code
8853  *   0 - Successfully processed rpl iocb (currently always return 0)
8854  **/
8855 static int
8856 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8857          struct lpfc_nodelist *ndlp)
8858 {
8859     struct lpfc_dmabuf *pcmd;
8860     uint32_t *lp;
8861     uint32_t maxsize;
8862     uint16_t cmdsize;
8863     RPL *rpl;
8864     struct ls_rjt stat;
8865 
8866     if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8867         (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
8868         /* issue rejection response */
8869         stat.un.b.lsRjtRsvd0 = 0;
8870         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8871         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8872         stat.un.b.vendorUnique = 0;
8873         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
8874             NULL);
8875         /* rejected the unsolicited RPL request and done with it */
8876         return 0;
8877     }
8878 
8879     pcmd = cmdiocb->cmd_dmabuf;
8880     lp = (uint32_t *) pcmd->virt;
8881     rpl = (RPL *) (lp + 1);
8882     maxsize = be32_to_cpu(rpl->maxsize);
8883 
8884     /* We support only one port */
8885     if ((rpl->index == 0) &&
8886         ((maxsize == 0) ||
8887          ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
8888         cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
8889     } else {
8890         cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
8891     }
8892     lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
8893 
8894     return 0;
8895 }
8896 
8897 /**
8898  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
8899  * @vport: pointer to a virtual N_Port data structure.
8900  * @cmdiocb: pointer to lpfc command iocb data structure.
8901  * @ndlp: pointer to a node-list data structure.
8902  *
8903  * This routine processes Fibre Channel Address Resolution Protocol
8904  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
8905  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
8906  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
8907  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
8908  * remote PortName is compared against the FC PortName stored in the @vport
8909  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
8910  * compared against the FC NodeName stored in the @vport data structure.
8911  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
8912  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
8913  * invoked to send out FARP Response to the remote node. Before sending the
8914  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
8915  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
8916  * routine is invoked to log into the remote port first.
8917  *
8918  * Return code
8919  *   0 - Either the FARP Match Mode not supported or successfully processed
8920  **/
8921 static int
8922 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8923           struct lpfc_nodelist *ndlp)
8924 {
8925     struct lpfc_dmabuf *pcmd;
8926     uint32_t *lp;
8927     FARP *fp;
8928     uint32_t cnt, did;
8929 
8930     did = get_job_els_rsp64_did(vport->phba, cmdiocb);
8931     pcmd = cmdiocb->cmd_dmabuf;
8932     lp = (uint32_t *) pcmd->virt;
8933 
8934     lp++;
8935     fp = (FARP *) lp;
8936     /* FARP-REQ received from DID <did> */
8937     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8938              "0601 FARP-REQ received from DID x%x\n", did);
8939     /* We will only support match on WWPN or WWNN */
8940     if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
8941         return 0;
8942     }
8943 
8944     cnt = 0;
8945     /* If this FARP command is searching for my portname */
8946     if (fp->Mflags & FARP_MATCH_PORT) {
8947         if (memcmp(&fp->RportName, &vport->fc_portname,
8948                sizeof(struct lpfc_name)) == 0)
8949             cnt = 1;
8950     }
8951 
8952     /* If this FARP command is searching for my nodename */
8953     if (fp->Mflags & FARP_MATCH_NODE) {
8954         if (memcmp(&fp->RnodeName, &vport->fc_nodename,
8955                sizeof(struct lpfc_name)) == 0)
8956             cnt = 1;
8957     }
8958 
8959     if (cnt) {
8960         if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
8961            (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
8962             /* Log back into the node before sending the FARP. */
8963             if (fp->Rflags & FARP_REQUEST_PLOGI) {
8964                 ndlp->nlp_prev_state = ndlp->nlp_state;
8965                 lpfc_nlp_set_state(vport, ndlp,
8966                            NLP_STE_PLOGI_ISSUE);
8967                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
8968             }
8969 
8970             /* Send a FARP response to that node */
8971             if (fp->Rflags & FARP_REQUEST_FARPR)
8972                 lpfc_issue_els_farpr(vport, did, 0);
8973         }
8974     }
8975     return 0;
8976 }
8977 
8978 /**
8979  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
8980  * @vport: pointer to a host virtual N_Port data structure.
8981  * @cmdiocb: pointer to lpfc command iocb data structure.
8982  * @ndlp: pointer to a node-list data structure.
8983  *
8984  * This routine processes Fibre Channel Address Resolution Protocol
8985  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
8986  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
8987  * the FARP response request.
8988  *
8989  * Return code
8990  *   0 - Successfully processed FARPR IOCB (currently always return 0)
8991  **/
8992 static int
8993 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8994            struct lpfc_nodelist  *ndlp)
8995 {
8996     struct lpfc_dmabuf *pcmd;
8997     uint32_t *lp;
8998     uint32_t did;
8999 
9000     did = get_job_els_rsp64_did(vport->phba, cmdiocb);
9001     pcmd = cmdiocb->cmd_dmabuf;
9002     lp = (uint32_t *)pcmd->virt;
9003 
9004     lp++;
9005     /* FARP-RSP received from DID <did> */
9006     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9007              "0600 FARP-RSP received from DID x%x\n", did);
9008     /* ACCEPT the Farp resp request */
9009     lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
9010 
9011     return 0;
9012 }
9013 
9014 /**
9015  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
9016  * @vport: pointer to a host virtual N_Port data structure.
9017  * @cmdiocb: pointer to lpfc command iocb data structure.
9018  * @fan_ndlp: pointer to a node-list data structure.
9019  *
9020  * This routine processes a Fabric Address Notification (FAN) IOCB
9021  * command received as an ELS unsolicited event. The FAN ELS command will
9022  * only be processed on a physical port (i.e., the @vport represents the
9023  * physical port). The fabric NodeName and PortName from the FAN IOCB are
9024  * compared against those in the phba data structure. If any of those is
9025  * different, the lpfc_initial_flogi() routine is invoked to initialize
9026  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
9027  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
9028  * is invoked to register login to the fabric.
9029  *
9030  * Return code
9031  *   0 - Successfully processed fan iocb (currently always return 0).
9032  **/
9033 static int
9034 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9035          struct lpfc_nodelist *fan_ndlp)
9036 {
9037     struct lpfc_hba *phba = vport->phba;
9038     uint32_t *lp;
9039     FAN *fp;
9040 
9041     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
9042     lp = (uint32_t *)cmdiocb->cmd_dmabuf->virt;
9043     fp = (FAN *) ++lp;
9044     /* FAN received; Fan does not have a reply sequence */
9045     if ((vport == phba->pport) &&
9046         (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
9047         if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
9048                 sizeof(struct lpfc_name))) ||
9049             (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
9050                 sizeof(struct lpfc_name)))) {
9051             /* This port has switched fabrics. FLOGI is required */
9052             lpfc_issue_init_vfi(vport);
9053         } else {
9054             /* FAN verified - skip FLOGI */
9055             vport->fc_myDID = vport->fc_prevDID;
9056             if (phba->sli_rev < LPFC_SLI_REV4)
9057                 lpfc_issue_fabric_reglogin(vport);
9058             else {
9059                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9060                     "3138 Need register VFI: (x%x/%x)\n",
9061                     vport->fc_prevDID, vport->fc_myDID);
9062                 lpfc_issue_reg_vfi(vport);
9063             }
9064         }
9065     }
9066     return 0;
9067 }
9068 
9069 /**
9070  * lpfc_els_rcv_edc - Process an unsolicited EDC iocb
9071  * @vport: pointer to a host virtual N_Port data structure.
9072  * @cmdiocb: pointer to lpfc command iocb data structure.
9073  * @ndlp: pointer to a node-list data structure.
9074  *
9075  * Return code
9076  *   0 - Successfully processed echo iocb (currently always return 0)
9077  **/
9078 static int
9079 lpfc_els_rcv_edc(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9080          struct lpfc_nodelist *ndlp)
9081 {
9082     struct lpfc_hba  *phba = vport->phba;
9083     struct fc_els_edc *edc_req;
9084     struct fc_tlv_desc *tlv;
9085     uint8_t *payload;
9086     uint32_t *ptr, dtag;
9087     const char *dtag_nm;
9088     int desc_cnt = 0, bytes_remain;
9089     bool rcv_cap_desc = false;
9090 
9091     payload = cmdiocb->cmd_dmabuf->virt;
9092 
9093     edc_req = (struct fc_els_edc *)payload;
9094     bytes_remain = be32_to_cpu(edc_req->desc_len);
9095 
9096     ptr = (uint32_t *)payload;
9097     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
9098              "3319 Rcv EDC payload len %d: x%x x%x x%x\n",
9099              bytes_remain, be32_to_cpu(*ptr),
9100              be32_to_cpu(*(ptr + 1)), be32_to_cpu(*(ptr + 2)));
9101 
9102     /* No signal support unless there is a congestion descriptor */
9103     phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
9104     phba->cgn_sig_freq = 0;
9105     phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
9106 
9107     if (bytes_remain <= 0)
9108         goto out;
9109 
9110     tlv = edc_req->desc;
9111 
9112     /*
9113      * cycle through EDC diagnostic descriptors to find the
9114      * congestion signaling capability descriptor
9115      */
9116     while (bytes_remain && !rcv_cap_desc) {
9117         if (bytes_remain < FC_TLV_DESC_HDR_SZ) {
9118             lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
9119                     "6464 Truncated TLV hdr on "
9120                     "Diagnostic descriptor[%d]\n",
9121                     desc_cnt);
9122             goto out;
9123         }
9124 
9125         dtag = be32_to_cpu(tlv->desc_tag);
9126         switch (dtag) {
9127         case ELS_DTAG_LNK_FAULT_CAP:
9128             if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
9129                 FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
9130                 sizeof(struct fc_diag_lnkflt_desc)) {
9131                 lpfc_printf_log(
9132                     phba, KERN_WARNING, LOG_CGN_MGMT,
9133                     "6465 Truncated Link Fault Diagnostic "
9134                     "descriptor[%d]: %d vs 0x%zx 0x%zx\n",
9135                     desc_cnt, bytes_remain,
9136                     FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
9137                     sizeof(struct fc_diag_cg_sig_desc));
9138                 goto out;
9139             }
9140             /* No action for Link Fault descriptor for now */
9141             break;
9142         case ELS_DTAG_CG_SIGNAL_CAP:
9143             if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
9144                 FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
9145                 sizeof(struct fc_diag_cg_sig_desc)) {
9146                 lpfc_printf_log(
9147                     phba, KERN_WARNING, LOG_CGN_MGMT,
9148                     "6466 Truncated cgn signal Diagnostic "
9149                     "descriptor[%d]: %d vs 0x%zx 0x%zx\n",
9150                     desc_cnt, bytes_remain,
9151                     FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
9152                     sizeof(struct fc_diag_cg_sig_desc));
9153                 goto out;
9154             }
9155 
9156             phba->cgn_reg_fpin = phba->cgn_init_reg_fpin;
9157             phba->cgn_reg_signal = phba->cgn_init_reg_signal;
9158 
9159             /* We start negotiation with lpfc_fabric_cgn_frequency.
9160              * When we process the EDC, we will settle on the
9161              * higher frequency.
9162              */
9163             phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
9164 
9165             lpfc_least_capable_settings(
9166                 phba, (struct fc_diag_cg_sig_desc *)tlv);
9167             rcv_cap_desc = true;
9168             break;
9169         default:
9170             dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
9171             lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
9172                     "6467 unknown Diagnostic "
9173                     "Descriptor[%d]: tag x%x (%s)\n",
9174                     desc_cnt, dtag, dtag_nm);
9175         }
9176         bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
9177         tlv = fc_tlv_next_desc(tlv);
9178         desc_cnt++;
9179     }
9180 out:
9181     /* Need to send back an ACC */
9182     lpfc_issue_els_edc_rsp(vport, cmdiocb, ndlp);
9183 
9184     lpfc_config_cgn_signal(phba);
9185     return 0;
9186 }
9187 
9188 /**
9189  * lpfc_els_timeout - Handler funciton to the els timer
9190  * @t: timer context used to obtain the vport.
9191  *
9192  * This routine is invoked by the ELS timer after timeout. It posts the ELS
9193  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
9194  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
9195  * up the worker thread. It is for the worker thread to invoke the routine
9196  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
9197  **/
9198 void
9199 lpfc_els_timeout(struct timer_list *t)
9200 {
9201     struct lpfc_vport *vport = from_timer(vport, t, els_tmofunc);
9202     struct lpfc_hba   *phba = vport->phba;
9203     uint32_t tmo_posted;
9204     unsigned long iflag;
9205 
9206     spin_lock_irqsave(&vport->work_port_lock, iflag);
9207     tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
9208     if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
9209         vport->work_port_events |= WORKER_ELS_TMO;
9210     spin_unlock_irqrestore(&vport->work_port_lock, iflag);
9211 
9212     if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
9213         lpfc_worker_wake_up(phba);
9214     return;
9215 }
9216 
9217 
9218 /**
9219  * lpfc_els_timeout_handler - Process an els timeout event
9220  * @vport: pointer to a virtual N_Port data structure.
9221  *
9222  * This routine is the actual handler function that processes an ELS timeout
9223  * event. It walks the ELS ring to get and abort all the IOCBs (except the
9224  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
9225  * invoking the lpfc_sli_issue_abort_iotag() routine.
9226  **/
9227 void
9228 lpfc_els_timeout_handler(struct lpfc_vport *vport)
9229 {
9230     struct lpfc_hba  *phba = vport->phba;
9231     struct lpfc_sli_ring *pring;
9232     struct lpfc_iocbq *tmp_iocb, *piocb;
9233     IOCB_t *cmd = NULL;
9234     struct lpfc_dmabuf *pcmd;
9235     uint32_t els_command = 0;
9236     uint32_t timeout;
9237     uint32_t remote_ID = 0xffffffff;
9238     LIST_HEAD(abort_list);
9239     u32 ulp_command = 0, ulp_context = 0, did = 0, iotag = 0;
9240 
9241 
9242     timeout = (uint32_t)(phba->fc_ratov << 1);
9243 
9244     pring = lpfc_phba_elsring(phba);
9245     if (unlikely(!pring))
9246         return;
9247 
9248     if (phba->pport->load_flag & FC_UNLOADING)
9249         return;
9250 
9251     spin_lock_irq(&phba->hbalock);
9252     if (phba->sli_rev == LPFC_SLI_REV4)
9253         spin_lock(&pring->ring_lock);
9254 
9255     list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
9256         ulp_command = get_job_cmnd(phba, piocb);
9257         ulp_context = get_job_ulpcontext(phba, piocb);
9258         did = get_job_els_rsp64_did(phba, piocb);
9259 
9260         if (phba->sli_rev == LPFC_SLI_REV4) {
9261             iotag = get_wqe_reqtag(piocb);
9262         } else {
9263             cmd = &piocb->iocb;
9264             iotag = cmd->ulpIoTag;
9265         }
9266 
9267         if ((piocb->cmd_flag & LPFC_IO_LIBDFC) != 0 ||
9268             ulp_command == CMD_ABORT_XRI_CX ||
9269             ulp_command == CMD_ABORT_XRI_CN ||
9270             ulp_command == CMD_CLOSE_XRI_CN)
9271             continue;
9272 
9273         if (piocb->vport != vport)
9274             continue;
9275 
9276         pcmd = piocb->cmd_dmabuf;
9277         if (pcmd)
9278             els_command = *(uint32_t *) (pcmd->virt);
9279 
9280         if (els_command == ELS_CMD_FARP ||
9281             els_command == ELS_CMD_FARPR ||
9282             els_command == ELS_CMD_FDISC)
9283             continue;
9284 
9285         if (piocb->drvrTimeout > 0) {
9286             if (piocb->drvrTimeout >= timeout)
9287                 piocb->drvrTimeout -= timeout;
9288             else
9289                 piocb->drvrTimeout = 0;
9290             continue;
9291         }
9292 
9293         remote_ID = 0xffffffff;
9294         if (ulp_command != CMD_GEN_REQUEST64_CR) {
9295             remote_ID = did;
9296         } else {
9297             struct lpfc_nodelist *ndlp;
9298             ndlp = __lpfc_findnode_rpi(vport, ulp_context);
9299             if (ndlp)
9300                 remote_ID = ndlp->nlp_DID;
9301         }
9302         list_add_tail(&piocb->dlist, &abort_list);
9303     }
9304     if (phba->sli_rev == LPFC_SLI_REV4)
9305         spin_unlock(&pring->ring_lock);
9306     spin_unlock_irq(&phba->hbalock);
9307 
9308     list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
9309         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9310              "0127 ELS timeout Data: x%x x%x x%x "
9311              "x%x\n", els_command,
9312              remote_ID, ulp_command, iotag);
9313 
9314         spin_lock_irq(&phba->hbalock);
9315         list_del_init(&piocb->dlist);
9316         lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
9317         spin_unlock_irq(&phba->hbalock);
9318     }
9319 
9320     /* Make sure HBA is alive */
9321     lpfc_issue_hb_tmo(phba);
9322 
9323     if (!list_empty(&pring->txcmplq))
9324         if (!(phba->pport->load_flag & FC_UNLOADING))
9325             mod_timer(&vport->els_tmofunc,
9326                   jiffies + msecs_to_jiffies(1000 * timeout));
9327 }
9328 
9329 /**
9330  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
9331  * @vport: pointer to a host virtual N_Port data structure.
9332  *
9333  * This routine is used to clean up all the outstanding ELS commands on a
9334  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
9335  * routine. After that, it walks the ELS transmit queue to remove all the
9336  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
9337  * the IOCBs with a non-NULL completion callback function, the callback
9338  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
9339  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
9340  * callback function, the IOCB will simply be released. Finally, it walks
9341  * the ELS transmit completion queue to issue an abort IOCB to any transmit
9342  * completion queue IOCB that is associated with the @vport and is not
9343  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
9344  * part of the discovery state machine) out to HBA by invoking the
9345  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
9346  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
9347  * the IOCBs are aborted when this function returns.
9348  **/
9349 void
9350 lpfc_els_flush_cmd(struct lpfc_vport *vport)
9351 {
9352     LIST_HEAD(abort_list);
9353     struct lpfc_hba  *phba = vport->phba;
9354     struct lpfc_sli_ring *pring;
9355     struct lpfc_iocbq *tmp_iocb, *piocb;
9356     u32 ulp_command;
9357     unsigned long iflags = 0;
9358 
9359     lpfc_fabric_abort_vport(vport);
9360 
9361     /*
9362      * For SLI3, only the hbalock is required.  But SLI4 needs to coordinate
9363      * with the ring insert operation.  Because lpfc_sli_issue_abort_iotag
9364      * ultimately grabs the ring_lock, the driver must splice the list into
9365      * a working list and release the locks before calling the abort.
9366      */
9367     spin_lock_irqsave(&phba->hbalock, iflags);
9368     pring = lpfc_phba_elsring(phba);
9369 
9370     /* Bail out if we've no ELS wq, like in PCI error recovery case. */
9371     if (unlikely(!pring)) {
9372         spin_unlock_irqrestore(&phba->hbalock, iflags);
9373         return;
9374     }
9375 
9376     if (phba->sli_rev == LPFC_SLI_REV4)
9377         spin_lock(&pring->ring_lock);
9378 
9379     /* First we need to issue aborts to outstanding cmds on txcmpl */
9380     list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
9381         if (piocb->cmd_flag & LPFC_IO_LIBDFC)
9382             continue;
9383 
9384         if (piocb->vport != vport)
9385             continue;
9386 
9387         if (piocb->cmd_flag & LPFC_DRIVER_ABORTED)
9388             continue;
9389 
9390         /* On the ELS ring we can have ELS_REQUESTs or
9391          * GEN_REQUESTs waiting for a response.
9392          */
9393         ulp_command = get_job_cmnd(phba, piocb);
9394         if (ulp_command == CMD_ELS_REQUEST64_CR) {
9395             list_add_tail(&piocb->dlist, &abort_list);
9396 
9397             /* If the link is down when flushing ELS commands
9398              * the firmware will not complete them till after
9399              * the link comes back up. This may confuse
9400              * discovery for the new link up, so we need to
9401              * change the compl routine to just clean up the iocb
9402              * and avoid any retry logic.
9403              */
9404             if (phba->link_state == LPFC_LINK_DOWN)
9405                 piocb->cmd_cmpl = lpfc_cmpl_els_link_down;
9406         }
9407         if (ulp_command == CMD_GEN_REQUEST64_CR)
9408             list_add_tail(&piocb->dlist, &abort_list);
9409     }
9410 
9411     if (phba->sli_rev == LPFC_SLI_REV4)
9412         spin_unlock(&pring->ring_lock);
9413     spin_unlock_irqrestore(&phba->hbalock, iflags);
9414 
9415     /* Abort each txcmpl iocb on aborted list and remove the dlist links. */
9416     list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
9417         spin_lock_irqsave(&phba->hbalock, iflags);
9418         list_del_init(&piocb->dlist);
9419         lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
9420         spin_unlock_irqrestore(&phba->hbalock, iflags);
9421     }
9422     /* Make sure HBA is alive */
9423     lpfc_issue_hb_tmo(phba);
9424 
9425     if (!list_empty(&abort_list))
9426         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9427                  "3387 abort list for txq not empty\n");
9428     INIT_LIST_HEAD(&abort_list);
9429 
9430     spin_lock_irqsave(&phba->hbalock, iflags);
9431     if (phba->sli_rev == LPFC_SLI_REV4)
9432         spin_lock(&pring->ring_lock);
9433 
9434     /* No need to abort the txq list,
9435      * just queue them up for lpfc_sli_cancel_iocbs
9436      */
9437     list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
9438         ulp_command = get_job_cmnd(phba, piocb);
9439 
9440         if (piocb->cmd_flag & LPFC_IO_LIBDFC)
9441             continue;
9442 
9443         /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
9444         if (ulp_command == CMD_QUE_RING_BUF_CN ||
9445             ulp_command == CMD_QUE_RING_BUF64_CN ||
9446             ulp_command == CMD_CLOSE_XRI_CN ||
9447             ulp_command == CMD_ABORT_XRI_CN ||
9448             ulp_command == CMD_ABORT_XRI_CX)
9449             continue;
9450 
9451         if (piocb->vport != vport)
9452             continue;
9453 
9454         list_del_init(&piocb->list);
9455         list_add_tail(&piocb->list, &abort_list);
9456     }
9457 
9458     /* The same holds true for any FLOGI/FDISC on the fabric_iocb_list */
9459     if (vport == phba->pport) {
9460         list_for_each_entry_safe(piocb, tmp_iocb,
9461                      &phba->fabric_iocb_list, list) {
9462             list_del_init(&piocb->list);
9463             list_add_tail(&piocb->list, &abort_list);
9464         }
9465     }
9466 
9467     if (phba->sli_rev == LPFC_SLI_REV4)
9468         spin_unlock(&pring->ring_lock);
9469     spin_unlock_irqrestore(&phba->hbalock, iflags);
9470 
9471     /* Cancel all the IOCBs from the completions list */
9472     lpfc_sli_cancel_iocbs(phba, &abort_list,
9473                   IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
9474 
9475     return;
9476 }
9477 
9478 /**
9479  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
9480  * @phba: pointer to lpfc hba data structure.
9481  *
9482  * This routine is used to clean up all the outstanding ELS commands on a
9483  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
9484  * routine. After that, it walks the ELS transmit queue to remove all the
9485  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
9486  * the IOCBs with the completion callback function associated, the callback
9487  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
9488  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
9489  * callback function associated, the IOCB will simply be released. Finally,
9490  * it walks the ELS transmit completion queue to issue an abort IOCB to any
9491  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
9492  * management plane IOCBs that are not part of the discovery state machine)
9493  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
9494  **/
9495 void
9496 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
9497 {
9498     struct lpfc_vport *vport;
9499 
9500     spin_lock_irq(&phba->port_list_lock);
9501     list_for_each_entry(vport, &phba->port_list, listentry)
9502         lpfc_els_flush_cmd(vport);
9503     spin_unlock_irq(&phba->port_list_lock);
9504 
9505     return;
9506 }
9507 
9508 /**
9509  * lpfc_send_els_failure_event - Posts an ELS command failure event
9510  * @phba: Pointer to hba context object.
9511  * @cmdiocbp: Pointer to command iocb which reported error.
9512  * @rspiocbp: Pointer to response iocb which reported error.
9513  *
9514  * This function sends an event when there is an ELS command
9515  * failure.
9516  **/
9517 void
9518 lpfc_send_els_failure_event(struct lpfc_hba *phba,
9519             struct lpfc_iocbq *cmdiocbp,
9520             struct lpfc_iocbq *rspiocbp)
9521 {
9522     struct lpfc_vport *vport = cmdiocbp->vport;
9523     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9524     struct lpfc_lsrjt_event lsrjt_event;
9525     struct lpfc_fabric_event_header fabric_event;
9526     struct ls_rjt stat;
9527     struct lpfc_nodelist *ndlp;
9528     uint32_t *pcmd;
9529     u32 ulp_status, ulp_word4;
9530 
9531     ndlp = cmdiocbp->ndlp;
9532     if (!ndlp)
9533         return;
9534 
9535     ulp_status = get_job_ulpstatus(phba, rspiocbp);
9536     ulp_word4 = get_job_word4(phba, rspiocbp);
9537 
9538     if (ulp_status == IOSTAT_LS_RJT) {
9539         lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
9540         lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
9541         memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
9542             sizeof(struct lpfc_name));
9543         memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
9544             sizeof(struct lpfc_name));
9545         pcmd = (uint32_t *)cmdiocbp->cmd_dmabuf->virt;
9546         lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
9547         stat.un.ls_rjt_error_be = cpu_to_be32(ulp_word4);
9548         lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
9549         lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
9550         fc_host_post_vendor_event(shost,
9551             fc_get_event_number(),
9552             sizeof(lsrjt_event),
9553             (char *)&lsrjt_event,
9554             LPFC_NL_VENDOR_ID);
9555         return;
9556     }
9557     if (ulp_status == IOSTAT_NPORT_BSY ||
9558         ulp_status == IOSTAT_FABRIC_BSY) {
9559         fabric_event.event_type = FC_REG_FABRIC_EVENT;
9560         if (ulp_status == IOSTAT_NPORT_BSY)
9561             fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
9562         else
9563             fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
9564         memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
9565             sizeof(struct lpfc_name));
9566         memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
9567             sizeof(struct lpfc_name));
9568         fc_host_post_vendor_event(shost,
9569             fc_get_event_number(),
9570             sizeof(fabric_event),
9571             (char *)&fabric_event,
9572             LPFC_NL_VENDOR_ID);
9573         return;
9574     }
9575 
9576 }
9577 
9578 /**
9579  * lpfc_send_els_event - Posts unsolicited els event
9580  * @vport: Pointer to vport object.
9581  * @ndlp: Pointer FC node object.
9582  * @payload: ELS command code type.
9583  *
9584  * This function posts an event when there is an incoming
9585  * unsolicited ELS command.
9586  **/
9587 static void
9588 lpfc_send_els_event(struct lpfc_vport *vport,
9589             struct lpfc_nodelist *ndlp,
9590             uint32_t *payload)
9591 {
9592     struct lpfc_els_event_header *els_data = NULL;
9593     struct lpfc_logo_event *logo_data = NULL;
9594     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9595 
9596     if (*payload == ELS_CMD_LOGO) {
9597         logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
9598         if (!logo_data) {
9599             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9600                 "0148 Failed to allocate memory "
9601                 "for LOGO event\n");
9602             return;
9603         }
9604         els_data = &logo_data->header;
9605     } else {
9606         els_data = kmalloc(sizeof(struct lpfc_els_event_header),
9607             GFP_KERNEL);
9608         if (!els_data) {
9609             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9610                 "0149 Failed to allocate memory "
9611                 "for ELS event\n");
9612             return;
9613         }
9614     }
9615     els_data->event_type = FC_REG_ELS_EVENT;
9616     switch (*payload) {
9617     case ELS_CMD_PLOGI:
9618         els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
9619         break;
9620     case ELS_CMD_PRLO:
9621         els_data->subcategory = LPFC_EVENT_PRLO_RCV;
9622         break;
9623     case ELS_CMD_ADISC:
9624         els_data->subcategory = LPFC_EVENT_ADISC_RCV;
9625         break;
9626     case ELS_CMD_LOGO:
9627         els_data->subcategory = LPFC_EVENT_LOGO_RCV;
9628         /* Copy the WWPN in the LOGO payload */
9629         memcpy(logo_data->logo_wwpn, &payload[2],
9630             sizeof(struct lpfc_name));
9631         break;
9632     default:
9633         kfree(els_data);
9634         return;
9635     }
9636     memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
9637     memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
9638     if (*payload == ELS_CMD_LOGO) {
9639         fc_host_post_vendor_event(shost,
9640             fc_get_event_number(),
9641             sizeof(struct lpfc_logo_event),
9642             (char *)logo_data,
9643             LPFC_NL_VENDOR_ID);
9644         kfree(logo_data);
9645     } else {
9646         fc_host_post_vendor_event(shost,
9647             fc_get_event_number(),
9648             sizeof(struct lpfc_els_event_header),
9649             (char *)els_data,
9650             LPFC_NL_VENDOR_ID);
9651         kfree(els_data);
9652     }
9653 
9654     return;
9655 }
9656 
9657 
9658 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_li_event_nm, fc_fpin_li_event_types,
9659             FC_FPIN_LI_EVT_TYPES_INIT);
9660 
9661 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_deli_event_nm, fc_fpin_deli_event_types,
9662             FC_FPIN_DELI_EVT_TYPES_INIT);
9663 
9664 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_event_nm, fc_fpin_congn_event_types,
9665             FC_FPIN_CONGN_EVT_TYPES_INIT);
9666 
9667 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_severity_nm,
9668             fc_fpin_congn_severity_types,
9669             FC_FPIN_CONGN_SEVERITY_INIT);
9670 
9671 
9672 /**
9673  * lpfc_display_fpin_wwpn - Display WWPNs accessible by the attached port
9674  * @phba: Pointer to phba object.
9675  * @wwnlist: Pointer to list of WWPNs in FPIN payload
9676  * @cnt: count of WWPNs in FPIN payload
9677  *
9678  * This routine is called by LI and PC descriptors.
9679  * Limit the number of WWPNs displayed to 6 log messages, 6 per log message
9680  */
9681 static void
9682 lpfc_display_fpin_wwpn(struct lpfc_hba *phba, __be64 *wwnlist, u32 cnt)
9683 {
9684     char buf[LPFC_FPIN_WWPN_LINE_SZ];
9685     __be64 wwn;
9686     u64 wwpn;
9687     int i, len;
9688     int line = 0;
9689     int wcnt = 0;
9690     bool endit = false;
9691 
9692     len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ, "Accessible WWPNs:");
9693     for (i = 0; i < cnt; i++) {
9694         /* Are we on the last WWPN */
9695         if (i == (cnt - 1))
9696             endit = true;
9697 
9698         /* Extract the next WWPN from the payload */
9699         wwn = *wwnlist++;
9700         wwpn = be64_to_cpu(wwn);
9701         len += scnprintf(buf + len, LPFC_FPIN_WWPN_LINE_SZ - len,
9702                  " %016llx", wwpn);
9703 
9704         /* Log a message if we are on the last WWPN
9705          * or if we hit the max allowed per message.
9706          */
9707         wcnt++;
9708         if (wcnt == LPFC_FPIN_WWPN_LINE_CNT || endit) {
9709             buf[len] = 0;
9710             lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9711                     "4686 %s\n", buf);
9712 
9713             /* Check if we reached the last WWPN */
9714             if (endit)
9715                 return;
9716 
9717             /* Limit the number of log message displayed per FPIN */
9718             line++;
9719             if (line == LPFC_FPIN_WWPN_NUM_LINE) {
9720                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9721                         "4687 %d WWPNs Truncated\n",
9722                         cnt - i - 1);
9723                 return;
9724             }
9725 
9726             /* Start over with next log message */
9727             wcnt = 0;
9728             len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ,
9729                     "Additional WWPNs:");
9730         }
9731     }
9732 }
9733 
9734 /**
9735  * lpfc_els_rcv_fpin_li - Process an FPIN Link Integrity Event.
9736  * @phba: Pointer to phba object.
9737  * @tlv:  Pointer to the Link Integrity Notification Descriptor.
9738  *
9739  * This function processes a Link Integrity FPIN event by logging a message.
9740  **/
9741 static void
9742 lpfc_els_rcv_fpin_li(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9743 {
9744     struct fc_fn_li_desc *li = (struct fc_fn_li_desc *)tlv;
9745     const char *li_evt_str;
9746     u32 li_evt, cnt;
9747 
9748     li_evt = be16_to_cpu(li->event_type);
9749     li_evt_str = lpfc_get_fpin_li_event_nm(li_evt);
9750     cnt = be32_to_cpu(li->pname_count);
9751 
9752     lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9753             "4680 FPIN Link Integrity %s (x%x) "
9754             "Detecting PN x%016llx Attached PN x%016llx "
9755             "Duration %d mSecs Count %d Port Cnt %d\n",
9756             li_evt_str, li_evt,
9757             be64_to_cpu(li->detecting_wwpn),
9758             be64_to_cpu(li->attached_wwpn),
9759             be32_to_cpu(li->event_threshold),
9760             be32_to_cpu(li->event_count), cnt);
9761 
9762     lpfc_display_fpin_wwpn(phba, (__be64 *)&li->pname_list, cnt);
9763 }
9764 
9765 /**
9766  * lpfc_els_rcv_fpin_del - Process an FPIN Delivery Event.
9767  * @phba: Pointer to hba object.
9768  * @tlv:  Pointer to the Delivery Notification Descriptor TLV
9769  *
9770  * This function processes a Delivery FPIN event by logging a message.
9771  **/
9772 static void
9773 lpfc_els_rcv_fpin_del(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9774 {
9775     struct fc_fn_deli_desc *del = (struct fc_fn_deli_desc *)tlv;
9776     const char *del_rsn_str;
9777     u32 del_rsn;
9778     __be32 *frame;
9779 
9780     del_rsn = be16_to_cpu(del->deli_reason_code);
9781     del_rsn_str = lpfc_get_fpin_deli_event_nm(del_rsn);
9782 
9783     /* Skip over desc_tag/desc_len header to payload */
9784     frame = (__be32 *)(del + 1);
9785 
9786     lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9787             "4681 FPIN Delivery %s (x%x) "
9788             "Detecting PN x%016llx Attached PN x%016llx "
9789             "DiscHdr0  x%08x "
9790             "DiscHdr1 x%08x DiscHdr2 x%08x DiscHdr3 x%08x "
9791             "DiscHdr4 x%08x DiscHdr5 x%08x\n",
9792             del_rsn_str, del_rsn,
9793             be64_to_cpu(del->detecting_wwpn),
9794             be64_to_cpu(del->attached_wwpn),
9795             be32_to_cpu(frame[0]),
9796             be32_to_cpu(frame[1]),
9797             be32_to_cpu(frame[2]),
9798             be32_to_cpu(frame[3]),
9799             be32_to_cpu(frame[4]),
9800             be32_to_cpu(frame[5]));
9801 }
9802 
9803 /**
9804  * lpfc_els_rcv_fpin_peer_cgn - Process a FPIN Peer Congestion Event.
9805  * @phba: Pointer to hba object.
9806  * @tlv:  Pointer to the Peer Congestion Notification Descriptor TLV
9807  *
9808  * This function processes a Peer Congestion FPIN event by logging a message.
9809  **/
9810 static void
9811 lpfc_els_rcv_fpin_peer_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9812 {
9813     struct fc_fn_peer_congn_desc *pc = (struct fc_fn_peer_congn_desc *)tlv;
9814     const char *pc_evt_str;
9815     u32 pc_evt, cnt;
9816 
9817     pc_evt = be16_to_cpu(pc->event_type);
9818     pc_evt_str = lpfc_get_fpin_congn_event_nm(pc_evt);
9819     cnt = be32_to_cpu(pc->pname_count);
9820 
9821     lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_ELS,
9822             "4684 FPIN Peer Congestion %s (x%x) "
9823             "Duration %d mSecs "
9824             "Detecting PN x%016llx Attached PN x%016llx "
9825             "Impacted Port Cnt %d\n",
9826             pc_evt_str, pc_evt,
9827             be32_to_cpu(pc->event_period),
9828             be64_to_cpu(pc->detecting_wwpn),
9829             be64_to_cpu(pc->attached_wwpn),
9830             cnt);
9831 
9832     lpfc_display_fpin_wwpn(phba, (__be64 *)&pc->pname_list, cnt);
9833 }
9834 
9835 /**
9836  * lpfc_els_rcv_fpin_cgn - Process an FPIN Congestion notification
9837  * @phba: Pointer to hba object.
9838  * @tlv:  Pointer to the Congestion Notification Descriptor TLV
9839  *
9840  * This function processes an FPIN Congestion Notifiction.  The notification
9841  * could be an Alarm or Warning.  This routine feeds that data into driver's
9842  * running congestion algorithm. It also processes the FPIN by
9843  * logging a message. It returns 1 to indicate deliver this message
9844  * to the upper layer or 0 to indicate don't deliver it.
9845  **/
9846 static int
9847 lpfc_els_rcv_fpin_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9848 {
9849     struct lpfc_cgn_info *cp;
9850     struct fc_fn_congn_desc *cgn = (struct fc_fn_congn_desc *)tlv;
9851     const char *cgn_evt_str;
9852     u32 cgn_evt;
9853     const char *cgn_sev_str;
9854     u32 cgn_sev;
9855     uint16_t value;
9856     u32 crc;
9857     bool nm_log = false;
9858     int rc = 1;
9859 
9860     cgn_evt = be16_to_cpu(cgn->event_type);
9861     cgn_evt_str = lpfc_get_fpin_congn_event_nm(cgn_evt);
9862     cgn_sev = cgn->severity;
9863     cgn_sev_str = lpfc_get_fpin_congn_severity_nm(cgn_sev);
9864 
9865     /* The driver only takes action on a Credit Stall or Oversubscription
9866      * event type to engage the IO algorithm.  The driver prints an
9867      * unmaskable message only for Lost Credit and Credit Stall.
9868      * TODO: Still need to have definition of host action on clear,
9869      *       lost credit and device specific event types.
9870      */
9871     switch (cgn_evt) {
9872     case FPIN_CONGN_LOST_CREDIT:
9873         nm_log = true;
9874         break;
9875     case FPIN_CONGN_CREDIT_STALL:
9876         nm_log = true;
9877         fallthrough;
9878     case FPIN_CONGN_OVERSUBSCRIPTION:
9879         if (cgn_evt == FPIN_CONGN_OVERSUBSCRIPTION)
9880             nm_log = false;
9881         switch (cgn_sev) {
9882         case FPIN_CONGN_SEVERITY_ERROR:
9883             /* Take action here for an Alarm event */
9884             if (phba->cmf_active_mode != LPFC_CFG_OFF) {
9885                 if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_ALARM) {
9886                     /* Track of alarm cnt for SYNC_WQE */
9887                     atomic_inc(&phba->cgn_sync_alarm_cnt);
9888                 }
9889                 /* Track alarm cnt for cgn_info regardless
9890                  * of whether CMF is configured for Signals
9891                  * or FPINs.
9892                  */
9893                 atomic_inc(&phba->cgn_fabric_alarm_cnt);
9894                 goto cleanup;
9895             }
9896             break;
9897         case FPIN_CONGN_SEVERITY_WARNING:
9898             /* Take action here for a Warning event */
9899             if (phba->cmf_active_mode != LPFC_CFG_OFF) {
9900                 if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_WARN) {
9901                     /* Track of warning cnt for SYNC_WQE */
9902                     atomic_inc(&phba->cgn_sync_warn_cnt);
9903                 }
9904                 /* Track warning cnt and freq for cgn_info
9905                  * regardless of whether CMF is configured for
9906                  * Signals or FPINs.
9907                  */
9908                 atomic_inc(&phba->cgn_fabric_warn_cnt);
9909 cleanup:
9910                 /* Save frequency in ms */
9911                 phba->cgn_fpin_frequency =
9912                     be32_to_cpu(cgn->event_period);
9913                 value = phba->cgn_fpin_frequency;
9914                 if (phba->cgn_i) {
9915                     cp = (struct lpfc_cgn_info *)
9916                         phba->cgn_i->virt;
9917                     cp->cgn_alarm_freq =
9918                         cpu_to_le16(value);
9919                     cp->cgn_warn_freq =
9920                         cpu_to_le16(value);
9921                     crc = lpfc_cgn_calc_crc32
9922                         (cp,
9923                         LPFC_CGN_INFO_SZ,
9924                         LPFC_CGN_CRC32_SEED);
9925                     cp->cgn_info_crc = cpu_to_le32(crc);
9926                 }
9927 
9928                 /* Don't deliver to upper layer since
9929                  * driver took action on this tlv.
9930                  */
9931                 rc = 0;
9932             }
9933             break;
9934         }
9935         break;
9936     }
9937 
9938     /* Change the log level to unmaskable for the following event types. */
9939     lpfc_printf_log(phba, (nm_log ? KERN_WARNING : KERN_INFO),
9940             LOG_CGN_MGMT | LOG_ELS,
9941             "4683 FPIN CONGESTION %s type %s (x%x) Event "
9942             "Duration %d mSecs\n",
9943             cgn_sev_str, cgn_evt_str, cgn_evt,
9944             be32_to_cpu(cgn->event_period));
9945     return rc;
9946 }
9947 
9948 void
9949 lpfc_els_rcv_fpin(struct lpfc_vport *vport, void *p, u32 fpin_length)
9950 {
9951     struct lpfc_hba *phba = vport->phba;
9952     struct fc_els_fpin *fpin = (struct fc_els_fpin *)p;
9953     struct fc_tlv_desc *tlv, *first_tlv, *current_tlv;
9954     const char *dtag_nm;
9955     int desc_cnt = 0, bytes_remain, cnt;
9956     u32 dtag, deliver = 0;
9957     int len;
9958 
9959     /* FPINs handled only if we are in the right discovery state */
9960     if (vport->port_state < LPFC_DISC_AUTH)
9961         return;
9962 
9963     /* make sure there is the full fpin header */
9964     if (fpin_length < sizeof(struct fc_els_fpin))
9965         return;
9966 
9967     /* Sanity check descriptor length. The desc_len value does not
9968      * include space for the ELS command and the desc_len fields.
9969      */
9970     len = be32_to_cpu(fpin->desc_len);
9971     if (fpin_length < len + sizeof(struct fc_els_fpin)) {
9972         lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
9973                 "4671 Bad ELS FPIN length %d: %d\n",
9974                 len, fpin_length);
9975         return;
9976     }
9977 
9978     tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
9979     first_tlv = tlv;
9980     bytes_remain = fpin_length - offsetof(struct fc_els_fpin, fpin_desc);
9981     bytes_remain = min_t(u32, bytes_remain, be32_to_cpu(fpin->desc_len));
9982 
9983     /* process each descriptor separately */
9984     while (bytes_remain >= FC_TLV_DESC_HDR_SZ &&
9985            bytes_remain >= FC_TLV_DESC_SZ_FROM_LENGTH(tlv)) {
9986         dtag = be32_to_cpu(tlv->desc_tag);
9987         switch (dtag) {
9988         case ELS_DTAG_LNK_INTEGRITY:
9989             lpfc_els_rcv_fpin_li(phba, tlv);
9990             deliver = 1;
9991             break;
9992         case ELS_DTAG_DELIVERY:
9993             lpfc_els_rcv_fpin_del(phba, tlv);
9994             deliver = 1;
9995             break;
9996         case ELS_DTAG_PEER_CONGEST:
9997             lpfc_els_rcv_fpin_peer_cgn(phba, tlv);
9998             deliver = 1;
9999             break;
10000         case ELS_DTAG_CONGESTION:
10001             deliver = lpfc_els_rcv_fpin_cgn(phba, tlv);
10002             break;
10003         default:
10004             dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
10005             lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
10006                     "4678 unknown FPIN descriptor[%d]: "
10007                     "tag x%x (%s)\n",
10008                     desc_cnt, dtag, dtag_nm);
10009 
10010             /* If descriptor is bad, drop the rest of the data */
10011             return;
10012         }
10013         lpfc_cgn_update_stat(phba, dtag);
10014         cnt = be32_to_cpu(tlv->desc_len);
10015 
10016         /* Sanity check descriptor length. The desc_len value does not
10017          * include space for the desc_tag and the desc_len fields.
10018          */
10019         len -= (cnt + sizeof(struct fc_tlv_desc));
10020         if (len < 0) {
10021             dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
10022             lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
10023                     "4672 Bad FPIN descriptor TLV length "
10024                     "%d: %d %d %s\n",
10025                     cnt, len, fpin_length, dtag_nm);
10026             return;
10027         }
10028 
10029         current_tlv = tlv;
10030         bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
10031         tlv = fc_tlv_next_desc(tlv);
10032 
10033         /* Format payload such that the FPIN delivered to the
10034          * upper layer is a single descriptor FPIN.
10035          */
10036         if (desc_cnt)
10037             memcpy(first_tlv, current_tlv,
10038                    (cnt + sizeof(struct fc_els_fpin)));
10039 
10040         /* Adjust the length so that it only reflects a
10041          * single descriptor FPIN.
10042          */
10043         fpin_length = cnt + sizeof(struct fc_els_fpin);
10044         fpin->desc_len = cpu_to_be32(fpin_length);
10045         fpin_length += sizeof(struct fc_els_fpin); /* the entire FPIN */
10046 
10047         /* Send every descriptor individually to the upper layer */
10048         if (deliver)
10049             fc_host_fpin_rcv(lpfc_shost_from_vport(vport),
10050                      fpin_length, (char *)fpin);
10051         desc_cnt++;
10052     }
10053 }
10054 
10055 /**
10056  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
10057  * @phba: pointer to lpfc hba data structure.
10058  * @pring: pointer to a SLI ring.
10059  * @vport: pointer to a host virtual N_Port data structure.
10060  * @elsiocb: pointer to lpfc els command iocb data structure.
10061  *
10062  * This routine is used for processing the IOCB associated with a unsolicited
10063  * event. It first determines whether there is an existing ndlp that matches
10064  * the DID from the unsolicited IOCB. If not, it will create a new one with
10065  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
10066  * IOCB is then used to invoke the proper routine and to set up proper state
10067  * of the discovery state machine.
10068  **/
10069 static void
10070 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10071               struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
10072 {
10073     struct lpfc_nodelist *ndlp;
10074     struct ls_rjt stat;
10075     u32 *payload, payload_len;
10076     u32 cmd = 0, did = 0, newnode, status = 0;
10077     uint8_t rjt_exp, rjt_err = 0, init_link = 0;
10078     struct lpfc_wcqe_complete *wcqe_cmpl = NULL;
10079     LPFC_MBOXQ_t *mbox;
10080 
10081     if (!vport || !elsiocb->cmd_dmabuf)
10082         goto dropit;
10083 
10084     newnode = 0;
10085     wcqe_cmpl = &elsiocb->wcqe_cmpl;
10086     payload = elsiocb->cmd_dmabuf->virt;
10087     if (phba->sli_rev == LPFC_SLI_REV4)
10088         payload_len = wcqe_cmpl->total_data_placed;
10089     else
10090         payload_len = elsiocb->iocb.unsli3.rcvsli3.acc_len;
10091     status = get_job_ulpstatus(phba, elsiocb);
10092     cmd = *payload;
10093     if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
10094         lpfc_sli3_post_buffer(phba, pring, 1);
10095 
10096     did = get_job_els_rsp64_did(phba, elsiocb);
10097     if (status) {
10098         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10099             "RCV Unsol ELS:  status:x%x/x%x did:x%x",
10100             status, get_job_word4(phba, elsiocb), did);
10101         goto dropit;
10102     }
10103 
10104     /* Check to see if link went down during discovery */
10105     if (lpfc_els_chk_latt(vport))
10106         goto dropit;
10107 
10108     /* Ignore traffic received during vport shutdown. */
10109     if (vport->load_flag & FC_UNLOADING)
10110         goto dropit;
10111 
10112     /* If NPort discovery is delayed drop incoming ELS */
10113     if ((vport->fc_flag & FC_DISC_DELAYED) &&
10114             (cmd != ELS_CMD_PLOGI))
10115         goto dropit;
10116 
10117     ndlp = lpfc_findnode_did(vport, did);
10118     if (!ndlp) {
10119         /* Cannot find existing Fabric ndlp, so allocate a new one */
10120         ndlp = lpfc_nlp_init(vport, did);
10121         if (!ndlp)
10122             goto dropit;
10123         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
10124         newnode = 1;
10125         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
10126             ndlp->nlp_type |= NLP_FABRIC;
10127     } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
10128         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
10129         newnode = 1;
10130     }
10131 
10132     phba->fc_stat.elsRcvFrame++;
10133 
10134     /*
10135      * Do not process any unsolicited ELS commands
10136      * if the ndlp is in DEV_LOSS
10137      */
10138     spin_lock_irq(&ndlp->lock);
10139     if (ndlp->nlp_flag & NLP_IN_DEV_LOSS) {
10140         spin_unlock_irq(&ndlp->lock);
10141         if (newnode)
10142             lpfc_nlp_put(ndlp);
10143         goto dropit;
10144     }
10145     spin_unlock_irq(&ndlp->lock);
10146 
10147     elsiocb->ndlp = lpfc_nlp_get(ndlp);
10148     if (!elsiocb->ndlp)
10149         goto dropit;
10150     elsiocb->vport = vport;
10151 
10152     if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
10153         cmd &= ELS_CMD_MASK;
10154     }
10155     /* ELS command <elsCmd> received from NPORT <did> */
10156     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10157              "0112 ELS command x%x received from NPORT x%x "
10158              "refcnt %d Data: x%x x%x x%x x%x\n",
10159              cmd, did, kref_read(&ndlp->kref), vport->port_state,
10160              vport->fc_flag, vport->fc_myDID, vport->fc_prevDID);
10161 
10162     /* reject till our FLOGI completes or PLOGI assigned DID via PT2PT */
10163     if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
10164         (cmd != ELS_CMD_FLOGI) &&
10165         !((cmd == ELS_CMD_PLOGI) && (vport->fc_flag & FC_PT2PT))) {
10166         rjt_err = LSRJT_LOGICAL_BSY;
10167         rjt_exp = LSEXP_NOTHING_MORE;
10168         goto lsrjt;
10169     }
10170 
10171     switch (cmd) {
10172     case ELS_CMD_PLOGI:
10173         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10174             "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
10175             did, vport->port_state, ndlp->nlp_flag);
10176 
10177         phba->fc_stat.elsRcvPLOGI++;
10178         ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
10179         if (phba->sli_rev == LPFC_SLI_REV4 &&
10180             (phba->pport->fc_flag & FC_PT2PT)) {
10181             vport->fc_prevDID = vport->fc_myDID;
10182             /* Our DID needs to be updated before registering
10183              * the vfi. This is done in lpfc_rcv_plogi but
10184              * that is called after the reg_vfi.
10185              */
10186             vport->fc_myDID =
10187                 bf_get(els_rsp64_sid,
10188                        &elsiocb->wqe.xmit_els_rsp);
10189             lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10190                      "3312 Remote port assigned DID x%x "
10191                      "%x\n", vport->fc_myDID,
10192                      vport->fc_prevDID);
10193         }
10194 
10195         lpfc_send_els_event(vport, ndlp, payload);
10196 
10197         /* If Nport discovery is delayed, reject PLOGIs */
10198         if (vport->fc_flag & FC_DISC_DELAYED) {
10199             rjt_err = LSRJT_UNABLE_TPC;
10200             rjt_exp = LSEXP_NOTHING_MORE;
10201             break;
10202         }
10203 
10204         if (vport->port_state < LPFC_DISC_AUTH) {
10205             if (!(phba->pport->fc_flag & FC_PT2PT) ||
10206                 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
10207                 rjt_err = LSRJT_UNABLE_TPC;
10208                 rjt_exp = LSEXP_NOTHING_MORE;
10209                 break;
10210             }
10211         }
10212 
10213         spin_lock_irq(&ndlp->lock);
10214         ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
10215         spin_unlock_irq(&ndlp->lock);
10216 
10217         lpfc_disc_state_machine(vport, ndlp, elsiocb,
10218                     NLP_EVT_RCV_PLOGI);
10219 
10220         break;
10221     case ELS_CMD_FLOGI:
10222         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10223             "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
10224             did, vport->port_state, ndlp->nlp_flag);
10225 
10226         phba->fc_stat.elsRcvFLOGI++;
10227 
10228         /* If the driver believes fabric discovery is done and is ready,
10229          * bounce the link.  There is some descrepancy.
10230          */
10231         if (vport->port_state >= LPFC_LOCAL_CFG_LINK &&
10232             vport->fc_flag & FC_PT2PT &&
10233             vport->rcv_flogi_cnt >= 1) {
10234             rjt_err = LSRJT_LOGICAL_BSY;
10235             rjt_exp = LSEXP_NOTHING_MORE;
10236             init_link++;
10237             goto lsrjt;
10238         }
10239 
10240         lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
10241         /* retain node if our response is deferred */
10242         if (phba->defer_flogi_acc_flag)
10243             break;
10244         if (newnode)
10245             lpfc_disc_state_machine(vport, ndlp, NULL,
10246                     NLP_EVT_DEVICE_RM);
10247         break;
10248     case ELS_CMD_LOGO:
10249         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10250             "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
10251             did, vport->port_state, ndlp->nlp_flag);
10252 
10253         phba->fc_stat.elsRcvLOGO++;
10254         lpfc_send_els_event(vport, ndlp, payload);
10255         if (vport->port_state < LPFC_DISC_AUTH) {
10256             rjt_err = LSRJT_UNABLE_TPC;
10257             rjt_exp = LSEXP_NOTHING_MORE;
10258             break;
10259         }
10260         lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
10261         if (newnode)
10262             lpfc_disc_state_machine(vport, ndlp, NULL,
10263                     NLP_EVT_DEVICE_RM);
10264         break;
10265     case ELS_CMD_PRLO:
10266         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10267             "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
10268             did, vport->port_state, ndlp->nlp_flag);
10269 
10270         phba->fc_stat.elsRcvPRLO++;
10271         lpfc_send_els_event(vport, ndlp, payload);
10272         if (vport->port_state < LPFC_DISC_AUTH) {
10273             rjt_err = LSRJT_UNABLE_TPC;
10274             rjt_exp = LSEXP_NOTHING_MORE;
10275             break;
10276         }
10277         lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
10278         break;
10279     case ELS_CMD_LCB:
10280         phba->fc_stat.elsRcvLCB++;
10281         lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
10282         break;
10283     case ELS_CMD_RDP:
10284         phba->fc_stat.elsRcvRDP++;
10285         lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
10286         break;
10287     case ELS_CMD_RSCN:
10288         phba->fc_stat.elsRcvRSCN++;
10289         lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
10290         if (newnode)
10291             lpfc_disc_state_machine(vport, ndlp, NULL,
10292                     NLP_EVT_DEVICE_RM);
10293         break;
10294     case ELS_CMD_ADISC:
10295         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10296             "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
10297             did, vport->port_state, ndlp->nlp_flag);
10298 
10299         lpfc_send_els_event(vport, ndlp, payload);
10300         phba->fc_stat.elsRcvADISC++;
10301         if (vport->port_state < LPFC_DISC_AUTH) {
10302             rjt_err = LSRJT_UNABLE_TPC;
10303             rjt_exp = LSEXP_NOTHING_MORE;
10304             break;
10305         }
10306         lpfc_disc_state_machine(vport, ndlp, elsiocb,
10307                     NLP_EVT_RCV_ADISC);
10308         break;
10309     case ELS_CMD_PDISC:
10310         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10311             "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
10312             did, vport->port_state, ndlp->nlp_flag);
10313 
10314         phba->fc_stat.elsRcvPDISC++;
10315         if (vport->port_state < LPFC_DISC_AUTH) {
10316             rjt_err = LSRJT_UNABLE_TPC;
10317             rjt_exp = LSEXP_NOTHING_MORE;
10318             break;
10319         }
10320         lpfc_disc_state_machine(vport, ndlp, elsiocb,
10321                     NLP_EVT_RCV_PDISC);
10322         break;
10323     case ELS_CMD_FARPR:
10324         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10325             "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
10326             did, vport->port_state, ndlp->nlp_flag);
10327 
10328         phba->fc_stat.elsRcvFARPR++;
10329         lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
10330         break;
10331     case ELS_CMD_FARP:
10332         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10333             "RCV FARP:        did:x%x/ste:x%x flg:x%x",
10334             did, vport->port_state, ndlp->nlp_flag);
10335 
10336         phba->fc_stat.elsRcvFARP++;
10337         lpfc_els_rcv_farp(vport, elsiocb, ndlp);
10338         break;
10339     case ELS_CMD_FAN:
10340         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10341             "RCV FAN:         did:x%x/ste:x%x flg:x%x",
10342             did, vport->port_state, ndlp->nlp_flag);
10343 
10344         phba->fc_stat.elsRcvFAN++;
10345         lpfc_els_rcv_fan(vport, elsiocb, ndlp);
10346         break;
10347     case ELS_CMD_PRLI:
10348     case ELS_CMD_NVMEPRLI:
10349         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10350             "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
10351             did, vport->port_state, ndlp->nlp_flag);
10352 
10353         phba->fc_stat.elsRcvPRLI++;
10354         if ((vport->port_state < LPFC_DISC_AUTH) &&
10355             (vport->fc_flag & FC_FABRIC)) {
10356             rjt_err = LSRJT_UNABLE_TPC;
10357             rjt_exp = LSEXP_NOTHING_MORE;
10358             break;
10359         }
10360         lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
10361         break;
10362     case ELS_CMD_LIRR:
10363         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10364             "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
10365             did, vport->port_state, ndlp->nlp_flag);
10366 
10367         phba->fc_stat.elsRcvLIRR++;
10368         lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
10369         if (newnode)
10370             lpfc_disc_state_machine(vport, ndlp, NULL,
10371                     NLP_EVT_DEVICE_RM);
10372         break;
10373     case ELS_CMD_RLS:
10374         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10375             "RCV RLS:         did:x%x/ste:x%x flg:x%x",
10376             did, vport->port_state, ndlp->nlp_flag);
10377 
10378         phba->fc_stat.elsRcvRLS++;
10379         lpfc_els_rcv_rls(vport, elsiocb, ndlp);
10380         if (newnode)
10381             lpfc_disc_state_machine(vport, ndlp, NULL,
10382                     NLP_EVT_DEVICE_RM);
10383         break;
10384     case ELS_CMD_RPL:
10385         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10386             "RCV RPL:         did:x%x/ste:x%x flg:x%x",
10387             did, vport->port_state, ndlp->nlp_flag);
10388 
10389         phba->fc_stat.elsRcvRPL++;
10390         lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
10391         if (newnode)
10392             lpfc_disc_state_machine(vport, ndlp, NULL,
10393                     NLP_EVT_DEVICE_RM);
10394         break;
10395     case ELS_CMD_RNID:
10396         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10397             "RCV RNID:        did:x%x/ste:x%x flg:x%x",
10398             did, vport->port_state, ndlp->nlp_flag);
10399 
10400         phba->fc_stat.elsRcvRNID++;
10401         lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
10402         if (newnode)
10403             lpfc_disc_state_machine(vport, ndlp, NULL,
10404                     NLP_EVT_DEVICE_RM);
10405         break;
10406     case ELS_CMD_RTV:
10407         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10408             "RCV RTV:        did:x%x/ste:x%x flg:x%x",
10409             did, vport->port_state, ndlp->nlp_flag);
10410         phba->fc_stat.elsRcvRTV++;
10411         lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
10412         if (newnode)
10413             lpfc_disc_state_machine(vport, ndlp, NULL,
10414                     NLP_EVT_DEVICE_RM);
10415         break;
10416     case ELS_CMD_RRQ:
10417         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10418             "RCV RRQ:         did:x%x/ste:x%x flg:x%x",
10419             did, vport->port_state, ndlp->nlp_flag);
10420 
10421         phba->fc_stat.elsRcvRRQ++;
10422         lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
10423         if (newnode)
10424             lpfc_disc_state_machine(vport, ndlp, NULL,
10425                     NLP_EVT_DEVICE_RM);
10426         break;
10427     case ELS_CMD_ECHO:
10428         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10429             "RCV ECHO:        did:x%x/ste:x%x flg:x%x",
10430             did, vport->port_state, ndlp->nlp_flag);
10431 
10432         phba->fc_stat.elsRcvECHO++;
10433         lpfc_els_rcv_echo(vport, elsiocb, ndlp);
10434         if (newnode)
10435             lpfc_disc_state_machine(vport, ndlp, NULL,
10436                     NLP_EVT_DEVICE_RM);
10437         break;
10438     case ELS_CMD_REC:
10439         /* receive this due to exchange closed */
10440         rjt_err = LSRJT_UNABLE_TPC;
10441         rjt_exp = LSEXP_INVALID_OX_RX;
10442         break;
10443     case ELS_CMD_FPIN:
10444         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10445                       "RCV FPIN:       did:x%x/ste:x%x flg:x%x",
10446                       did, vport->port_state, ndlp->nlp_flag);
10447 
10448         lpfc_els_rcv_fpin(vport, (struct fc_els_fpin *)payload,
10449                   payload_len);
10450 
10451         /* There are no replies, so no rjt codes */
10452         break;
10453     case ELS_CMD_EDC:
10454         lpfc_els_rcv_edc(vport, elsiocb, ndlp);
10455         break;
10456     case ELS_CMD_RDF:
10457         phba->fc_stat.elsRcvRDF++;
10458         /* Accept RDF only from fabric controller */
10459         if (did != Fabric_Cntl_DID) {
10460             lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
10461                      "1115 Received RDF from invalid DID "
10462                      "x%x\n", did);
10463             rjt_err = LSRJT_PROTOCOL_ERR;
10464             rjt_exp = LSEXP_NOTHING_MORE;
10465             goto lsrjt;
10466         }
10467 
10468         lpfc_els_rcv_rdf(vport, elsiocb, ndlp);
10469         break;
10470     default:
10471         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10472             "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
10473             cmd, did, vport->port_state);
10474 
10475         /* Unsupported ELS command, reject */
10476         rjt_err = LSRJT_CMD_UNSUPPORTED;
10477         rjt_exp = LSEXP_NOTHING_MORE;
10478 
10479         /* Unknown ELS command <elsCmd> received from NPORT <did> */
10480         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10481                  "0115 Unknown ELS command x%x "
10482                  "received from NPORT x%x\n", cmd, did);
10483         if (newnode)
10484             lpfc_disc_state_machine(vport, ndlp, NULL,
10485                     NLP_EVT_DEVICE_RM);
10486         break;
10487     }
10488 
10489 lsrjt:
10490     /* check if need to LS_RJT received ELS cmd */
10491     if (rjt_err) {
10492         memset(&stat, 0, sizeof(stat));
10493         stat.un.b.lsRjtRsnCode = rjt_err;
10494         stat.un.b.lsRjtRsnCodeExp = rjt_exp;
10495         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
10496                     NULL);
10497         /* Remove the reference from above for new nodes. */
10498         if (newnode)
10499             lpfc_disc_state_machine(vport, ndlp, NULL,
10500                     NLP_EVT_DEVICE_RM);
10501     }
10502 
10503     /* Release the reference on this elsiocb, not the ndlp. */
10504     lpfc_nlp_put(elsiocb->ndlp);
10505     elsiocb->ndlp = NULL;
10506 
10507     /* Special case.  Driver received an unsolicited command that
10508      * unsupportable given the driver's current state.  Reset the
10509      * link and start over.
10510      */
10511     if (init_link) {
10512         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10513         if (!mbox)
10514             return;
10515         lpfc_linkdown(phba);
10516         lpfc_init_link(phba, mbox,
10517                    phba->cfg_topology,
10518                    phba->cfg_link_speed);
10519         mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
10520         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10521         mbox->vport = vport;
10522         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
10523             MBX_NOT_FINISHED)
10524             mempool_free(mbox, phba->mbox_mem_pool);
10525     }
10526 
10527     return;
10528 
10529 dropit:
10530     if (vport && !(vport->load_flag & FC_UNLOADING))
10531         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10532             "0111 Dropping received ELS cmd "
10533             "Data: x%x x%x x%x x%x\n",
10534             cmd, status, get_job_word4(phba, elsiocb), did);
10535 
10536     phba->fc_stat.elsRcvDrop++;
10537 }
10538 
10539 /**
10540  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
10541  * @phba: pointer to lpfc hba data structure.
10542  * @pring: pointer to a SLI ring.
10543  * @elsiocb: pointer to lpfc els iocb data structure.
10544  *
10545  * This routine is used to process an unsolicited event received from a SLI
10546  * (Service Level Interface) ring. The actual processing of the data buffer
10547  * associated with the unsolicited event is done by invoking the routine
10548  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
10549  * SLI ring on which the unsolicited event was received.
10550  **/
10551 void
10552 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10553              struct lpfc_iocbq *elsiocb)
10554 {
10555     struct lpfc_vport *vport = elsiocb->vport;
10556     u32 ulp_command, status, parameter, bde_count = 0;
10557     IOCB_t *icmd;
10558     struct lpfc_wcqe_complete *wcqe_cmpl = NULL;
10559     struct lpfc_dmabuf *bdeBuf1 = elsiocb->cmd_dmabuf;
10560     struct lpfc_dmabuf *bdeBuf2 = elsiocb->bpl_dmabuf;
10561     dma_addr_t paddr;
10562 
10563     elsiocb->cmd_dmabuf = NULL;
10564     elsiocb->rsp_dmabuf = NULL;
10565     elsiocb->bpl_dmabuf = NULL;
10566 
10567     wcqe_cmpl = &elsiocb->wcqe_cmpl;
10568     ulp_command = get_job_cmnd(phba, elsiocb);
10569     status = get_job_ulpstatus(phba, elsiocb);
10570     parameter = get_job_word4(phba, elsiocb);
10571     if (phba->sli_rev == LPFC_SLI_REV4)
10572         bde_count = wcqe_cmpl->word3;
10573     else
10574         bde_count = elsiocb->iocb.ulpBdeCount;
10575 
10576     if (status == IOSTAT_NEED_BUFFER) {
10577         lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
10578     } else if (status == IOSTAT_LOCAL_REJECT &&
10579            (parameter & IOERR_PARAM_MASK) ==
10580            IOERR_RCV_BUFFER_WAITING) {
10581         phba->fc_stat.NoRcvBuf++;
10582         /* Not enough posted buffers; Try posting more buffers */
10583         if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
10584             lpfc_sli3_post_buffer(phba, pring, 0);
10585         return;
10586     }
10587 
10588     if (phba->sli_rev == LPFC_SLI_REV3) {
10589         icmd = &elsiocb->iocb;
10590         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
10591             (ulp_command == CMD_IOCB_RCV_ELS64_CX ||
10592              ulp_command == CMD_IOCB_RCV_SEQ64_CX)) {
10593             if (icmd->unsli3.rcvsli3.vpi == 0xffff)
10594                 vport = phba->pport;
10595             else
10596                 vport = lpfc_find_vport_by_vpid(phba,
10597                         icmd->unsli3.rcvsli3.vpi);
10598         }
10599     }
10600 
10601     /* If there are no BDEs associated
10602      * with this IOCB, there is nothing to do.
10603      */
10604     if (bde_count == 0)
10605         return;
10606 
10607     /* Account for SLI2 or SLI3 and later unsolicited buffering */
10608     if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
10609         elsiocb->cmd_dmabuf = bdeBuf1;
10610         if (bde_count == 2)
10611             elsiocb->bpl_dmabuf = bdeBuf2;
10612     } else {
10613         icmd = &elsiocb->iocb;
10614         paddr = getPaddr(icmd->un.cont64[0].addrHigh,
10615                  icmd->un.cont64[0].addrLow);
10616         elsiocb->cmd_dmabuf = lpfc_sli_ringpostbuf_get(phba, pring,
10617                                    paddr);
10618         if (bde_count == 2) {
10619             paddr = getPaddr(icmd->un.cont64[1].addrHigh,
10620                      icmd->un.cont64[1].addrLow);
10621             elsiocb->bpl_dmabuf = lpfc_sli_ringpostbuf_get(phba,
10622                                     pring,
10623                                     paddr);
10624         }
10625     }
10626 
10627     lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
10628     /*
10629      * The different unsolicited event handlers would tell us
10630      * if they are done with "mp" by setting cmd_dmabuf to NULL.
10631      */
10632     if (elsiocb->cmd_dmabuf) {
10633         lpfc_in_buf_free(phba, elsiocb->cmd_dmabuf);
10634         elsiocb->cmd_dmabuf = NULL;
10635     }
10636 
10637     if (elsiocb->bpl_dmabuf) {
10638         lpfc_in_buf_free(phba, elsiocb->bpl_dmabuf);
10639         elsiocb->bpl_dmabuf = NULL;
10640     }
10641 
10642 }
10643 
10644 static void
10645 lpfc_start_fdmi(struct lpfc_vport *vport)
10646 {
10647     struct lpfc_nodelist *ndlp;
10648 
10649     /* If this is the first time, allocate an ndlp and initialize
10650      * it. Otherwise, make sure the node is enabled and then do the
10651      * login.
10652      */
10653     ndlp = lpfc_findnode_did(vport, FDMI_DID);
10654     if (!ndlp) {
10655         ndlp = lpfc_nlp_init(vport, FDMI_DID);
10656         if (ndlp) {
10657             ndlp->nlp_type |= NLP_FABRIC;
10658         } else {
10659             return;
10660         }
10661     }
10662 
10663     lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
10664     lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
10665 }
10666 
10667 /**
10668  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
10669  * @phba: pointer to lpfc hba data structure.
10670  * @vport: pointer to a virtual N_Port data structure.
10671  *
10672  * This routine issues a Port Login (PLOGI) to the Name Server with
10673  * State Change Request (SCR) for a @vport. This routine will create an
10674  * ndlp for the Name Server associated to the @vport if such node does
10675  * not already exist. The PLOGI to Name Server is issued by invoking the
10676  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
10677  * (FDMI) is configured to the @vport, a FDMI node will be created and
10678  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
10679  **/
10680 void
10681 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
10682 {
10683     struct lpfc_nodelist *ndlp;
10684     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10685 
10686     /*
10687      * If lpfc_delay_discovery parameter is set and the clean address
10688      * bit is cleared and fc fabric parameters chenged, delay FC NPort
10689      * discovery.
10690      */
10691     spin_lock_irq(shost->host_lock);
10692     if (vport->fc_flag & FC_DISC_DELAYED) {
10693         spin_unlock_irq(shost->host_lock);
10694         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10695                  "3334 Delay fc port discovery for %d secs\n",
10696                  phba->fc_ratov);
10697         mod_timer(&vport->delayed_disc_tmo,
10698             jiffies + msecs_to_jiffies(1000 * phba->fc_ratov));
10699         return;
10700     }
10701     spin_unlock_irq(shost->host_lock);
10702 
10703     ndlp = lpfc_findnode_did(vport, NameServer_DID);
10704     if (!ndlp) {
10705         ndlp = lpfc_nlp_init(vport, NameServer_DID);
10706         if (!ndlp) {
10707             if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
10708                 lpfc_disc_start(vport);
10709                 return;
10710             }
10711             lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10712             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10713                      "0251 NameServer login: no memory\n");
10714             return;
10715         }
10716     }
10717 
10718     ndlp->nlp_type |= NLP_FABRIC;
10719 
10720     lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
10721 
10722     if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
10723         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10724         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10725                  "0252 Cannot issue NameServer login\n");
10726         return;
10727     }
10728 
10729     if ((phba->cfg_enable_SmartSAN ||
10730          (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) &&
10731          (vport->load_flag & FC_ALLOW_FDMI))
10732         lpfc_start_fdmi(vport);
10733 }
10734 
10735 /**
10736  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
10737  * @phba: pointer to lpfc hba data structure.
10738  * @pmb: pointer to the driver internal queue element for mailbox command.
10739  *
10740  * This routine is the completion callback function to register new vport
10741  * mailbox command. If the new vport mailbox command completes successfully,
10742  * the fabric registration login shall be performed on physical port (the
10743  * new vport created is actually a physical port, with VPI 0) or the port
10744  * login to Name Server for State Change Request (SCR) will be performed
10745  * on virtual port (real virtual port, with VPI greater than 0).
10746  **/
10747 static void
10748 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
10749 {
10750     struct lpfc_vport *vport = pmb->vport;
10751     struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
10752     struct lpfc_nodelist *ndlp = pmb->ctx_ndlp;
10753     MAILBOX_t *mb = &pmb->u.mb;
10754     int rc;
10755 
10756     spin_lock_irq(shost->host_lock);
10757     vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
10758     spin_unlock_irq(shost->host_lock);
10759 
10760     if (mb->mbxStatus) {
10761         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10762                 "0915 Register VPI failed : Status: x%x"
10763                 " upd bit: x%x \n", mb->mbxStatus,
10764                  mb->un.varRegVpi.upd);
10765         if (phba->sli_rev == LPFC_SLI_REV4 &&
10766             mb->un.varRegVpi.upd)
10767             goto mbox_err_exit ;
10768 
10769         switch (mb->mbxStatus) {
10770         case 0x11:  /* unsupported feature */
10771         case 0x9603:    /* max_vpi exceeded */
10772         case 0x9602:    /* Link event since CLEAR_LA */
10773             /* giving up on vport registration */
10774             lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10775             spin_lock_irq(shost->host_lock);
10776             vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
10777             spin_unlock_irq(shost->host_lock);
10778             lpfc_can_disctmo(vport);
10779             break;
10780         /* If reg_vpi fail with invalid VPI status, re-init VPI */
10781         case 0x20:
10782             spin_lock_irq(shost->host_lock);
10783             vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
10784             spin_unlock_irq(shost->host_lock);
10785             lpfc_init_vpi(phba, pmb, vport->vpi);
10786             pmb->vport = vport;
10787             pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
10788             rc = lpfc_sli_issue_mbox(phba, pmb,
10789                 MBX_NOWAIT);
10790             if (rc == MBX_NOT_FINISHED) {
10791                 lpfc_printf_vlog(vport, KERN_ERR,
10792                          LOG_TRACE_EVENT,
10793                     "2732 Failed to issue INIT_VPI"
10794                     " mailbox command\n");
10795             } else {
10796                 lpfc_nlp_put(ndlp);
10797                 return;
10798             }
10799             fallthrough;
10800         default:
10801             /* Try to recover from this error */
10802             if (phba->sli_rev == LPFC_SLI_REV4)
10803                 lpfc_sli4_unreg_all_rpis(vport);
10804             lpfc_mbx_unreg_vpi(vport);
10805             spin_lock_irq(shost->host_lock);
10806             vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
10807             spin_unlock_irq(shost->host_lock);
10808             if (mb->mbxStatus == MBX_NOT_FINISHED)
10809                 break;
10810             if ((vport->port_type == LPFC_PHYSICAL_PORT) &&
10811                 !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG)) {
10812                 if (phba->sli_rev == LPFC_SLI_REV4)
10813                     lpfc_issue_init_vfi(vport);
10814                 else
10815                     lpfc_initial_flogi(vport);
10816             } else {
10817                 lpfc_initial_fdisc(vport);
10818             }
10819             break;
10820         }
10821     } else {
10822         spin_lock_irq(shost->host_lock);
10823         vport->vpi_state |= LPFC_VPI_REGISTERED;
10824         spin_unlock_irq(shost->host_lock);
10825         if (vport == phba->pport) {
10826             if (phba->sli_rev < LPFC_SLI_REV4)
10827                 lpfc_issue_fabric_reglogin(vport);
10828             else {
10829                 /*
10830                  * If the physical port is instantiated using
10831                  * FDISC, do not start vport discovery.
10832                  */
10833                 if (vport->port_state != LPFC_FDISC)
10834                     lpfc_start_fdiscs(phba);
10835                 lpfc_do_scr_ns_plogi(phba, vport);
10836             }
10837         } else {
10838             lpfc_do_scr_ns_plogi(phba, vport);
10839         }
10840     }
10841 mbox_err_exit:
10842     /* Now, we decrement the ndlp reference count held for this
10843      * callback function
10844      */
10845     lpfc_nlp_put(ndlp);
10846 
10847     mempool_free(pmb, phba->mbox_mem_pool);
10848     return;
10849 }
10850 
10851 /**
10852  * lpfc_register_new_vport - Register a new vport with a HBA
10853  * @phba: pointer to lpfc hba data structure.
10854  * @vport: pointer to a host virtual N_Port data structure.
10855  * @ndlp: pointer to a node-list data structure.
10856  *
10857  * This routine registers the @vport as a new virtual port with a HBA.
10858  * It is done through a registering vpi mailbox command.
10859  **/
10860 void
10861 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
10862             struct lpfc_nodelist *ndlp)
10863 {
10864     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10865     LPFC_MBOXQ_t *mbox;
10866 
10867     mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10868     if (mbox) {
10869         lpfc_reg_vpi(vport, mbox);
10870         mbox->vport = vport;
10871         mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
10872         if (!mbox->ctx_ndlp) {
10873             mempool_free(mbox, phba->mbox_mem_pool);
10874             goto mbox_err_exit;
10875         }
10876 
10877         mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
10878         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
10879             == MBX_NOT_FINISHED) {
10880             /* mailbox command not success, decrement ndlp
10881              * reference count for this command
10882              */
10883             lpfc_nlp_put(ndlp);
10884             mempool_free(mbox, phba->mbox_mem_pool);
10885 
10886             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10887                 "0253 Register VPI: Can't send mbox\n");
10888             goto mbox_err_exit;
10889         }
10890     } else {
10891         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10892                  "0254 Register VPI: no memory\n");
10893         goto mbox_err_exit;
10894     }
10895     return;
10896 
10897 mbox_err_exit:
10898     lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10899     spin_lock_irq(shost->host_lock);
10900     vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
10901     spin_unlock_irq(shost->host_lock);
10902     return;
10903 }
10904 
10905 /**
10906  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
10907  * @phba: pointer to lpfc hba data structure.
10908  *
10909  * This routine cancels the retry delay timers to all the vports.
10910  **/
10911 void
10912 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
10913 {
10914     struct lpfc_vport **vports;
10915     struct lpfc_nodelist *ndlp;
10916     uint32_t link_state;
10917     int i;
10918 
10919     /* Treat this failure as linkdown for all vports */
10920     link_state = phba->link_state;
10921     lpfc_linkdown(phba);
10922     phba->link_state = link_state;
10923 
10924     vports = lpfc_create_vport_work_array(phba);
10925 
10926     if (vports) {
10927         for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10928             ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
10929             if (ndlp)
10930                 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
10931             lpfc_els_flush_cmd(vports[i]);
10932         }
10933         lpfc_destroy_vport_work_array(phba, vports);
10934     }
10935 }
10936 
10937 /**
10938  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
10939  * @phba: pointer to lpfc hba data structure.
10940  *
10941  * This routine abort all pending discovery commands and
10942  * start a timer to retry FLOGI for the physical port
10943  * discovery.
10944  **/
10945 void
10946 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
10947 {
10948     struct lpfc_nodelist *ndlp;
10949 
10950     /* Cancel the all vports retry delay retry timers */
10951     lpfc_cancel_all_vport_retry_delay_timer(phba);
10952 
10953     /* If fabric require FLOGI, then re-instantiate physical login */
10954     ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
10955     if (!ndlp)
10956         return;
10957 
10958     mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
10959     spin_lock_irq(&ndlp->lock);
10960     ndlp->nlp_flag |= NLP_DELAY_TMO;
10961     spin_unlock_irq(&ndlp->lock);
10962     ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
10963     phba->pport->port_state = LPFC_FLOGI;
10964     return;
10965 }
10966 
10967 /**
10968  * lpfc_fabric_login_reqd - Check if FLOGI required.
10969  * @phba: pointer to lpfc hba data structure.
10970  * @cmdiocb: pointer to FDISC command iocb.
10971  * @rspiocb: pointer to FDISC response iocb.
10972  *
10973  * This routine checks if a FLOGI is reguired for FDISC
10974  * to succeed.
10975  **/
10976 static int
10977 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
10978         struct lpfc_iocbq *cmdiocb,
10979         struct lpfc_iocbq *rspiocb)
10980 {
10981     u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
10982     u32 ulp_word4 = get_job_word4(phba, rspiocb);
10983 
10984     if (ulp_status != IOSTAT_FABRIC_RJT ||
10985         ulp_word4 != RJT_LOGIN_REQUIRED)
10986         return 0;
10987     else
10988         return 1;
10989 }
10990 
10991 /**
10992  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
10993  * @phba: pointer to lpfc hba data structure.
10994  * @cmdiocb: pointer to lpfc command iocb data structure.
10995  * @rspiocb: pointer to lpfc response iocb data structure.
10996  *
10997  * This routine is the completion callback function to a Fabric Discover
10998  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
10999  * single threaded, each FDISC completion callback function will reset
11000  * the discovery timer for all vports such that the timers will not get
11001  * unnecessary timeout. The function checks the FDISC IOCB status. If error
11002  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
11003  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
11004  * assigned to the vport has been changed with the completion of the FDISC
11005  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
11006  * are unregistered from the HBA, and then the lpfc_register_new_vport()
11007  * routine is invoked to register new vport with the HBA. Otherwise, the
11008  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
11009  * Server for State Change Request (SCR).
11010  **/
11011 static void
11012 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11013             struct lpfc_iocbq *rspiocb)
11014 {
11015     struct lpfc_vport *vport = cmdiocb->vport;
11016     struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
11017     struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
11018     struct lpfc_nodelist *np;
11019     struct lpfc_nodelist *next_np;
11020     struct lpfc_iocbq *piocb;
11021     struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf, *prsp;
11022     struct serv_parm *sp;
11023     uint8_t fabric_param_changed;
11024     u32 ulp_status, ulp_word4;
11025 
11026     ulp_status = get_job_ulpstatus(phba, rspiocb);
11027     ulp_word4 = get_job_word4(phba, rspiocb);
11028 
11029     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
11030              "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
11031              ulp_status, ulp_word4,
11032              vport->fc_prevDID);
11033     /* Since all FDISCs are being single threaded, we
11034      * must reset the discovery timer for ALL vports
11035      * waiting to send FDISC when one completes.
11036      */
11037     list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
11038         lpfc_set_disctmo(piocb->vport);
11039     }
11040 
11041     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11042         "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
11043         ulp_status, ulp_word4, vport->fc_prevDID);
11044 
11045     if (ulp_status) {
11046 
11047         if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
11048             lpfc_retry_pport_discovery(phba);
11049             goto out;
11050         }
11051 
11052         /* Check for retry */
11053         if (lpfc_els_retry(phba, cmdiocb, rspiocb))
11054             goto out;
11055         /* FDISC failed */
11056         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11057                  "0126 FDISC failed. (x%x/x%x)\n",
11058                  ulp_status, ulp_word4);
11059         goto fdisc_failed;
11060     }
11061 
11062     lpfc_check_nlp_post_devloss(vport, ndlp);
11063 
11064     spin_lock_irq(shost->host_lock);
11065     vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
11066     vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
11067     vport->fc_flag |= FC_FABRIC;
11068     if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
11069         vport->fc_flag |=  FC_PUBLIC_LOOP;
11070     spin_unlock_irq(shost->host_lock);
11071 
11072     vport->fc_myDID = ulp_word4 & Mask_DID;
11073     lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
11074     prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
11075     if (!prsp)
11076         goto out;
11077     sp = prsp->virt + sizeof(uint32_t);
11078     fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
11079     memcpy(&vport->fabric_portname, &sp->portName,
11080         sizeof(struct lpfc_name));
11081     memcpy(&vport->fabric_nodename, &sp->nodeName,
11082         sizeof(struct lpfc_name));
11083     if (fabric_param_changed &&
11084         !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
11085         /* If our NportID changed, we need to ensure all
11086          * remaining NPORTs get unreg_login'ed so we can
11087          * issue unreg_vpi.
11088          */
11089         list_for_each_entry_safe(np, next_np,
11090             &vport->fc_nodes, nlp_listp) {
11091             if ((np->nlp_state != NLP_STE_NPR_NODE) ||
11092                 !(np->nlp_flag & NLP_NPR_ADISC))
11093                 continue;
11094             spin_lock_irq(&ndlp->lock);
11095             np->nlp_flag &= ~NLP_NPR_ADISC;
11096             spin_unlock_irq(&ndlp->lock);
11097             lpfc_unreg_rpi(vport, np);
11098         }
11099         lpfc_cleanup_pending_mbox(vport);
11100 
11101         if (phba->sli_rev == LPFC_SLI_REV4)
11102             lpfc_sli4_unreg_all_rpis(vport);
11103 
11104         lpfc_mbx_unreg_vpi(vport);
11105         spin_lock_irq(shost->host_lock);
11106         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
11107         if (phba->sli_rev == LPFC_SLI_REV4)
11108             vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
11109         else
11110             vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
11111         spin_unlock_irq(shost->host_lock);
11112     } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
11113         !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
11114         /*
11115          * Driver needs to re-reg VPI in order for f/w
11116          * to update the MAC address.
11117          */
11118         lpfc_register_new_vport(phba, vport, ndlp);
11119         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
11120         goto out;
11121     }
11122 
11123     if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
11124         lpfc_issue_init_vpi(vport);
11125     else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
11126         lpfc_register_new_vport(phba, vport, ndlp);
11127     else
11128         lpfc_do_scr_ns_plogi(phba, vport);
11129 
11130     /* The FDISC completed successfully. Move the fabric ndlp to
11131      * UNMAPPED state and register with the transport.
11132      */
11133     lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
11134     goto out;
11135 
11136 fdisc_failed:
11137     if (vport->fc_vport &&
11138         (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS))
11139         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11140     /* Cancel discovery timer */
11141     lpfc_can_disctmo(vport);
11142 out:
11143     lpfc_els_free_iocb(phba, cmdiocb);
11144     lpfc_nlp_put(ndlp);
11145 }
11146 
11147 /**
11148  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
11149  * @vport: pointer to a virtual N_Port data structure.
11150  * @ndlp: pointer to a node-list data structure.
11151  * @retry: number of retries to the command IOCB.
11152  *
11153  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
11154  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
11155  * routine to issue the IOCB, which makes sure only one outstanding fabric
11156  * IOCB will be sent off HBA at any given time.
11157  *
11158  * Note that the ndlp reference count will be incremented by 1 for holding the
11159  * ndlp and the reference to ndlp will be stored into the ndlp field of
11160  * the IOCB for the completion callback function to the FDISC ELS command.
11161  *
11162  * Return code
11163  *   0 - Successfully issued fdisc iocb command
11164  *   1 - Failed to issue fdisc iocb command
11165  **/
11166 static int
11167 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
11168              uint8_t retry)
11169 {
11170     struct lpfc_hba *phba = vport->phba;
11171     IOCB_t *icmd;
11172     union lpfc_wqe128 *wqe = NULL;
11173     struct lpfc_iocbq *elsiocb;
11174     struct serv_parm *sp;
11175     uint8_t *pcmd;
11176     uint16_t cmdsize;
11177     int did = ndlp->nlp_DID;
11178     int rc;
11179 
11180     vport->port_state = LPFC_FDISC;
11181     vport->fc_myDID = 0;
11182     cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
11183     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
11184                      ELS_CMD_FDISC);
11185     if (!elsiocb) {
11186         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11187         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11188                  "0255 Issue FDISC: no IOCB\n");
11189         return 1;
11190     }
11191 
11192     if (phba->sli_rev == LPFC_SLI_REV4) {
11193         wqe = &elsiocb->wqe;
11194         bf_set(els_req64_sid, &wqe->els_req, 0);
11195         bf_set(els_req64_sp, &wqe->els_req, 1);
11196     } else {
11197         icmd = &elsiocb->iocb;
11198         icmd->un.elsreq64.myID = 0;
11199         icmd->un.elsreq64.fl = 1;
11200         icmd->ulpCt_h = 1;
11201         icmd->ulpCt_l = 0;
11202     }
11203 
11204     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
11205     *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
11206     pcmd += sizeof(uint32_t); /* CSP Word 1 */
11207     memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
11208     sp = (struct serv_parm *) pcmd;
11209     /* Setup CSPs accordingly for Fabric */
11210     sp->cmn.e_d_tov = 0;
11211     sp->cmn.w2.r_a_tov = 0;
11212     sp->cmn.virtual_fabric_support = 0;
11213     sp->cls1.classValid = 0;
11214     sp->cls2.seqDelivery = 1;
11215     sp->cls3.seqDelivery = 1;
11216 
11217     pcmd += sizeof(uint32_t); /* CSP Word 2 */
11218     pcmd += sizeof(uint32_t); /* CSP Word 3 */
11219     pcmd += sizeof(uint32_t); /* CSP Word 4 */
11220     pcmd += sizeof(uint32_t); /* Port Name */
11221     memcpy(pcmd, &vport->fc_portname, 8);
11222     pcmd += sizeof(uint32_t); /* Node Name */
11223     pcmd += sizeof(uint32_t); /* Node Name */
11224     memcpy(pcmd, &vport->fc_nodename, 8);
11225     sp->cmn.valid_vendor_ver_level = 0;
11226     memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
11227     lpfc_set_disctmo(vport);
11228 
11229     phba->fc_stat.elsXmitFDISC++;
11230     elsiocb->cmd_cmpl = lpfc_cmpl_els_fdisc;
11231 
11232     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11233         "Issue FDISC:     did:x%x",
11234         did, 0, 0);
11235 
11236     elsiocb->ndlp = lpfc_nlp_get(ndlp);
11237     if (!elsiocb->ndlp)
11238         goto err_out;
11239 
11240     rc = lpfc_issue_fabric_iocb(phba, elsiocb);
11241     if (rc == IOCB_ERROR) {
11242         lpfc_nlp_put(ndlp);
11243         goto err_out;
11244     }
11245 
11246     lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
11247     return 0;
11248 
11249  err_out:
11250     lpfc_els_free_iocb(phba, elsiocb);
11251     lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11252     lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11253              "0256 Issue FDISC: Cannot send IOCB\n");
11254     return 1;
11255 }
11256 
11257 /**
11258  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
11259  * @phba: pointer to lpfc hba data structure.
11260  * @cmdiocb: pointer to lpfc command iocb data structure.
11261  * @rspiocb: pointer to lpfc response iocb data structure.
11262  *
11263  * This routine is the completion callback function to the issuing of a LOGO
11264  * ELS command off a vport. It frees the command IOCB and then decrement the
11265  * reference count held on ndlp for this completion function, indicating that
11266  * the reference to the ndlp is no long needed. Note that the
11267  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
11268  * callback function and an additional explicit ndlp reference decrementation
11269  * will trigger the actual release of the ndlp.
11270  **/
11271 static void
11272 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11273             struct lpfc_iocbq *rspiocb)
11274 {
11275     struct lpfc_vport *vport = cmdiocb->vport;
11276     IOCB_t *irsp;
11277     struct lpfc_nodelist *ndlp;
11278     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
11279     u32 ulp_status, ulp_word4, did, tmo;
11280 
11281     ndlp = cmdiocb->ndlp;
11282 
11283     ulp_status = get_job_ulpstatus(phba, rspiocb);
11284     ulp_word4 = get_job_word4(phba, rspiocb);
11285 
11286     if (phba->sli_rev == LPFC_SLI_REV4) {
11287         did = get_job_els_rsp64_did(phba, cmdiocb);
11288         tmo = get_wqe_tmo(cmdiocb);
11289     } else {
11290         irsp = &rspiocb->iocb;
11291         did = get_job_els_rsp64_did(phba, rspiocb);
11292         tmo = irsp->ulpTimeout;
11293     }
11294 
11295     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11296         "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
11297         ulp_status, ulp_word4, did);
11298 
11299     /* NPIV LOGO completes to NPort <nlp_DID> */
11300     lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
11301              "2928 NPIV LOGO completes to NPort x%x "
11302              "Data: x%x x%x x%x x%x x%x x%x x%x\n",
11303              ndlp->nlp_DID, ulp_status, ulp_word4,
11304              tmo, vport->num_disc_nodes,
11305              kref_read(&ndlp->kref), ndlp->nlp_flag,
11306              ndlp->fc4_xpt_flags);
11307 
11308     if (ulp_status == IOSTAT_SUCCESS) {
11309         spin_lock_irq(shost->host_lock);
11310         vport->fc_flag &= ~FC_NDISC_ACTIVE;
11311         vport->fc_flag &= ~FC_FABRIC;
11312         spin_unlock_irq(shost->host_lock);
11313         lpfc_can_disctmo(vport);
11314     }
11315 
11316     if (ndlp->save_flags & NLP_WAIT_FOR_LOGO) {
11317         /* Wake up lpfc_vport_delete if waiting...*/
11318         if (ndlp->logo_waitq)
11319             wake_up(ndlp->logo_waitq);
11320         spin_lock_irq(&ndlp->lock);
11321         ndlp->nlp_flag &= ~(NLP_ISSUE_LOGO | NLP_LOGO_SND);
11322         ndlp->save_flags &= ~NLP_WAIT_FOR_LOGO;
11323         spin_unlock_irq(&ndlp->lock);
11324     }
11325 
11326     /* Safe to release resources now. */
11327     lpfc_els_free_iocb(phba, cmdiocb);
11328     lpfc_nlp_put(ndlp);
11329 }
11330 
11331 /**
11332  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
11333  * @vport: pointer to a virtual N_Port data structure.
11334  * @ndlp: pointer to a node-list data structure.
11335  *
11336  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
11337  *
11338  * Note that the ndlp reference count will be incremented by 1 for holding the
11339  * ndlp and the reference to ndlp will be stored into the ndlp field of
11340  * the IOCB for the completion callback function to the LOGO ELS command.
11341  *
11342  * Return codes
11343  *   0 - Successfully issued logo off the @vport
11344  *   1 - Failed to issue logo off the @vport
11345  **/
11346 int
11347 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
11348 {
11349     int rc = 0;
11350     struct lpfc_hba  *phba = vport->phba;
11351     struct lpfc_iocbq *elsiocb;
11352     uint8_t *pcmd;
11353     uint16_t cmdsize;
11354 
11355     cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
11356     elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
11357                      ELS_CMD_LOGO);
11358     if (!elsiocb)
11359         return 1;
11360 
11361     pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
11362     *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
11363     pcmd += sizeof(uint32_t);
11364 
11365     /* Fill in LOGO payload */
11366     *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
11367     pcmd += sizeof(uint32_t);
11368     memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
11369 
11370     lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11371         "Issue LOGO npiv  did:x%x flg:x%x",
11372         ndlp->nlp_DID, ndlp->nlp_flag, 0);
11373 
11374     elsiocb->cmd_cmpl = lpfc_cmpl_els_npiv_logo;
11375     spin_lock_irq(&ndlp->lock);
11376     ndlp->nlp_flag |= NLP_LOGO_SND;
11377     spin_unlock_irq(&ndlp->lock);
11378     elsiocb->ndlp = lpfc_nlp_get(ndlp);
11379     if (!elsiocb->ndlp) {
11380         lpfc_els_free_iocb(phba, elsiocb);
11381         goto err;
11382     }
11383 
11384     rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
11385     if (rc == IOCB_ERROR) {
11386         lpfc_els_free_iocb(phba, elsiocb);
11387         lpfc_nlp_put(ndlp);
11388         goto err;
11389     }
11390     return 0;
11391 
11392 err:
11393     spin_lock_irq(&ndlp->lock);
11394     ndlp->nlp_flag &= ~NLP_LOGO_SND;
11395     spin_unlock_irq(&ndlp->lock);
11396     return 1;
11397 }
11398 
11399 /**
11400  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
11401  * @t: timer context used to obtain the lpfc hba.
11402  *
11403  * This routine is invoked by the fabric iocb block timer after
11404  * timeout. It posts the fabric iocb block timeout event by setting the
11405  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
11406  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
11407  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
11408  * posted event WORKER_FABRIC_BLOCK_TMO.
11409  **/
11410 void
11411 lpfc_fabric_block_timeout(struct timer_list *t)
11412 {
11413     struct lpfc_hba  *phba = from_timer(phba, t, fabric_block_timer);
11414     unsigned long iflags;
11415     uint32_t tmo_posted;
11416 
11417     spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11418     tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
11419     if (!tmo_posted)
11420         phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
11421     spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11422 
11423     if (!tmo_posted)
11424         lpfc_worker_wake_up(phba);
11425     return;
11426 }
11427 
11428 /**
11429  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
11430  * @phba: pointer to lpfc hba data structure.
11431  *
11432  * This routine issues one fabric iocb from the driver internal list to
11433  * the HBA. It first checks whether it's ready to issue one fabric iocb to
11434  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
11435  * remove one pending fabric iocb from the driver internal list and invokes
11436  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
11437  **/
11438 static void
11439 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
11440 {
11441     struct lpfc_iocbq *iocb;
11442     unsigned long iflags;
11443     int ret;
11444 
11445 repeat:
11446     iocb = NULL;
11447     spin_lock_irqsave(&phba->hbalock, iflags);
11448     /* Post any pending iocb to the SLI layer */
11449     if (atomic_read(&phba->fabric_iocb_count) == 0) {
11450         list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
11451                  list);
11452         if (iocb)
11453             /* Increment fabric iocb count to hold the position */
11454             atomic_inc(&phba->fabric_iocb_count);
11455     }
11456     spin_unlock_irqrestore(&phba->hbalock, iflags);
11457     if (iocb) {
11458         iocb->fabric_cmd_cmpl = iocb->cmd_cmpl;
11459         iocb->cmd_cmpl = lpfc_cmpl_fabric_iocb;
11460         iocb->cmd_flag |= LPFC_IO_FABRIC;
11461 
11462         lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11463                       "Fabric sched1:   ste:x%x",
11464                       iocb->vport->port_state, 0, 0);
11465 
11466         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11467 
11468         if (ret == IOCB_ERROR) {
11469             iocb->cmd_cmpl = iocb->fabric_cmd_cmpl;
11470             iocb->fabric_cmd_cmpl = NULL;
11471             iocb->cmd_flag &= ~LPFC_IO_FABRIC;
11472             set_job_ulpstatus(iocb, IOSTAT_LOCAL_REJECT);
11473             iocb->wcqe_cmpl.parameter = IOERR_SLI_ABORTED;
11474             iocb->cmd_cmpl(phba, iocb, iocb);
11475 
11476             atomic_dec(&phba->fabric_iocb_count);
11477             goto repeat;
11478         }
11479     }
11480 }
11481 
11482 /**
11483  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
11484  * @phba: pointer to lpfc hba data structure.
11485  *
11486  * This routine unblocks the  issuing fabric iocb command. The function
11487  * will clear the fabric iocb block bit and then invoke the routine
11488  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
11489  * from the driver internal fabric iocb list.
11490  **/
11491 void
11492 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
11493 {
11494     clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11495 
11496     lpfc_resume_fabric_iocbs(phba);
11497     return;
11498 }
11499 
11500 /**
11501  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
11502  * @phba: pointer to lpfc hba data structure.
11503  *
11504  * This routine blocks the issuing fabric iocb for a specified amount of
11505  * time (currently 100 ms). This is done by set the fabric iocb block bit
11506  * and set up a timeout timer for 100ms. When the block bit is set, no more
11507  * fabric iocb will be issued out of the HBA.
11508  **/
11509 static void
11510 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
11511 {
11512     int blocked;
11513 
11514     blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11515     /* Start a timer to unblock fabric iocbs after 100ms */
11516     if (!blocked)
11517         mod_timer(&phba->fabric_block_timer,
11518               jiffies + msecs_to_jiffies(100));
11519 
11520     return;
11521 }
11522 
11523 /**
11524  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
11525  * @phba: pointer to lpfc hba data structure.
11526  * @cmdiocb: pointer to lpfc command iocb data structure.
11527  * @rspiocb: pointer to lpfc response iocb data structure.
11528  *
11529  * This routine is the callback function that is put to the fabric iocb's
11530  * callback function pointer (iocb->cmd_cmpl). The original iocb's callback
11531  * function pointer has been stored in iocb->fabric_cmd_cmpl. This callback
11532  * function first restores and invokes the original iocb's callback function
11533  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
11534  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
11535  **/
11536 static void
11537 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11538               struct lpfc_iocbq *rspiocb)
11539 {
11540     struct ls_rjt stat;
11541     u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
11542     u32 ulp_word4 = get_job_word4(phba, rspiocb);
11543 
11544     WARN_ON((cmdiocb->cmd_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC);
11545 
11546     switch (ulp_status) {
11547         case IOSTAT_NPORT_RJT:
11548         case IOSTAT_FABRIC_RJT:
11549             if (ulp_word4 & RJT_UNAVAIL_TEMP)
11550                 lpfc_block_fabric_iocbs(phba);
11551             break;
11552 
11553         case IOSTAT_NPORT_BSY:
11554         case IOSTAT_FABRIC_BSY:
11555             lpfc_block_fabric_iocbs(phba);
11556             break;
11557 
11558         case IOSTAT_LS_RJT:
11559             stat.un.ls_rjt_error_be =
11560                 cpu_to_be32(ulp_word4);
11561             if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
11562                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
11563                 lpfc_block_fabric_iocbs(phba);
11564             break;
11565     }
11566 
11567     BUG_ON(atomic_read(&phba->fabric_iocb_count) == 0);
11568 
11569     cmdiocb->cmd_cmpl = cmdiocb->fabric_cmd_cmpl;
11570     cmdiocb->fabric_cmd_cmpl = NULL;
11571     cmdiocb->cmd_flag &= ~LPFC_IO_FABRIC;
11572     cmdiocb->cmd_cmpl(phba, cmdiocb, rspiocb);
11573 
11574     atomic_dec(&phba->fabric_iocb_count);
11575     if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
11576         /* Post any pending iocbs to HBA */
11577         lpfc_resume_fabric_iocbs(phba);
11578     }
11579 }
11580 
11581 /**
11582  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
11583  * @phba: pointer to lpfc hba data structure.
11584  * @iocb: pointer to lpfc command iocb data structure.
11585  *
11586  * This routine is used as the top-level API for issuing a fabric iocb command
11587  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
11588  * function makes sure that only one fabric bound iocb will be outstanding at
11589  * any given time. As such, this function will first check to see whether there
11590  * is already an outstanding fabric iocb on the wire. If so, it will put the
11591  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
11592  * issued later. Otherwise, it will issue the iocb on the wire and update the
11593  * fabric iocb count it indicate that there is one fabric iocb on the wire.
11594  *
11595  * Note, this implementation has a potential sending out fabric IOCBs out of
11596  * order. The problem is caused by the construction of the "ready" boolen does
11597  * not include the condition that the internal fabric IOCB list is empty. As
11598  * such, it is possible a fabric IOCB issued by this routine might be "jump"
11599  * ahead of the fabric IOCBs in the internal list.
11600  *
11601  * Return code
11602  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
11603  *   IOCB_ERROR - failed to issue fabric iocb
11604  **/
11605 static int
11606 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
11607 {
11608     unsigned long iflags;
11609     int ready;
11610     int ret;
11611 
11612     BUG_ON(atomic_read(&phba->fabric_iocb_count) > 1);
11613 
11614     spin_lock_irqsave(&phba->hbalock, iflags);
11615     ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
11616         !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11617 
11618     if (ready)
11619         /* Increment fabric iocb count to hold the position */
11620         atomic_inc(&phba->fabric_iocb_count);
11621     spin_unlock_irqrestore(&phba->hbalock, iflags);
11622     if (ready) {
11623         iocb->fabric_cmd_cmpl = iocb->cmd_cmpl;
11624         iocb->cmd_cmpl = lpfc_cmpl_fabric_iocb;
11625         iocb->cmd_flag |= LPFC_IO_FABRIC;
11626 
11627         lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11628                       "Fabric sched2:   ste:x%x",
11629                       iocb->vport->port_state, 0, 0);
11630 
11631         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11632 
11633         if (ret == IOCB_ERROR) {
11634             iocb->cmd_cmpl = iocb->fabric_cmd_cmpl;
11635             iocb->fabric_cmd_cmpl = NULL;
11636             iocb->cmd_flag &= ~LPFC_IO_FABRIC;
11637             atomic_dec(&phba->fabric_iocb_count);
11638         }
11639     } else {
11640         spin_lock_irqsave(&phba->hbalock, iflags);
11641         list_add_tail(&iocb->list, &phba->fabric_iocb_list);
11642         spin_unlock_irqrestore(&phba->hbalock, iflags);
11643         ret = IOCB_SUCCESS;
11644     }
11645     return ret;
11646 }
11647 
11648 /**
11649  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
11650  * @vport: pointer to a virtual N_Port data structure.
11651  *
11652  * This routine aborts all the IOCBs associated with a @vport from the
11653  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
11654  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
11655  * list, removes each IOCB associated with the @vport off the list, set the
11656  * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
11657  * associated with the IOCB.
11658  **/
11659 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
11660 {
11661     LIST_HEAD(completions);
11662     struct lpfc_hba  *phba = vport->phba;
11663     struct lpfc_iocbq *tmp_iocb, *piocb;
11664 
11665     spin_lock_irq(&phba->hbalock);
11666     list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
11667                  list) {
11668 
11669         if (piocb->vport != vport)
11670             continue;
11671 
11672         list_move_tail(&piocb->list, &completions);
11673     }
11674     spin_unlock_irq(&phba->hbalock);
11675 
11676     /* Cancel all the IOCBs from the completions list */
11677     lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11678                   IOERR_SLI_ABORTED);
11679 }
11680 
11681 /**
11682  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
11683  * @ndlp: pointer to a node-list data structure.
11684  *
11685  * This routine aborts all the IOCBs associated with an @ndlp from the
11686  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
11687  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
11688  * list, removes each IOCB associated with the @ndlp off the list, set the
11689  * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
11690  * associated with the IOCB.
11691  **/
11692 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
11693 {
11694     LIST_HEAD(completions);
11695     struct lpfc_hba  *phba = ndlp->phba;
11696     struct lpfc_iocbq *tmp_iocb, *piocb;
11697     struct lpfc_sli_ring *pring;
11698 
11699     pring = lpfc_phba_elsring(phba);
11700 
11701     if (unlikely(!pring))
11702         return;
11703 
11704     spin_lock_irq(&phba->hbalock);
11705     list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
11706                  list) {
11707         if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
11708 
11709             list_move_tail(&piocb->list, &completions);
11710         }
11711     }
11712     spin_unlock_irq(&phba->hbalock);
11713 
11714     /* Cancel all the IOCBs from the completions list */
11715     lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11716                   IOERR_SLI_ABORTED);
11717 }
11718 
11719 /**
11720  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
11721  * @phba: pointer to lpfc hba data structure.
11722  *
11723  * This routine aborts all the IOCBs currently on the driver internal
11724  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
11725  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
11726  * list, removes IOCBs off the list, set the status field to
11727  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
11728  * the IOCB.
11729  **/
11730 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
11731 {
11732     LIST_HEAD(completions);
11733 
11734     spin_lock_irq(&phba->hbalock);
11735     list_splice_init(&phba->fabric_iocb_list, &completions);
11736     spin_unlock_irq(&phba->hbalock);
11737 
11738     /* Cancel all the IOCBs from the completions list */
11739     lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11740                   IOERR_SLI_ABORTED);
11741 }
11742 
11743 /**
11744  * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
11745  * @vport: pointer to lpfc vport data structure.
11746  *
11747  * This routine is invoked by the vport cleanup for deletions and the cleanup
11748  * for an ndlp on removal.
11749  **/
11750 void
11751 lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
11752 {
11753     struct lpfc_hba *phba = vport->phba;
11754     struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
11755     struct lpfc_nodelist *ndlp = NULL;
11756     unsigned long iflag = 0;
11757 
11758     spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
11759     list_for_each_entry_safe(sglq_entry, sglq_next,
11760             &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
11761         if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport) {
11762             lpfc_nlp_put(sglq_entry->ndlp);
11763             ndlp = sglq_entry->ndlp;
11764             sglq_entry->ndlp = NULL;
11765 
11766             /* If the xri on the abts_els_sgl list is for the Fport
11767              * node and the vport is unloading, the xri aborted wcqe
11768              * likely isn't coming back.  Just release the sgl.
11769              */
11770             if ((vport->load_flag & FC_UNLOADING) &&
11771                 ndlp->nlp_DID == Fabric_DID) {
11772                 list_del(&sglq_entry->list);
11773                 sglq_entry->state = SGL_FREED;
11774                 list_add_tail(&sglq_entry->list,
11775                     &phba->sli4_hba.lpfc_els_sgl_list);
11776             }
11777         }
11778     }
11779     spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
11780     return;
11781 }
11782 
11783 /**
11784  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
11785  * @phba: pointer to lpfc hba data structure.
11786  * @axri: pointer to the els xri abort wcqe structure.
11787  *
11788  * This routine is invoked by the worker thread to process a SLI4 slow-path
11789  * ELS aborted xri.
11790  **/
11791 void
11792 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
11793               struct sli4_wcqe_xri_aborted *axri)
11794 {
11795     uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
11796     uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
11797     uint16_t lxri = 0;
11798 
11799     struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
11800     unsigned long iflag = 0;
11801     struct lpfc_nodelist *ndlp;
11802     struct lpfc_sli_ring *pring;
11803 
11804     pring = lpfc_phba_elsring(phba);
11805 
11806     spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
11807     list_for_each_entry_safe(sglq_entry, sglq_next,
11808             &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
11809         if (sglq_entry->sli4_xritag == xri) {
11810             list_del(&sglq_entry->list);
11811             ndlp = sglq_entry->ndlp;
11812             sglq_entry->ndlp = NULL;
11813             list_add_tail(&sglq_entry->list,
11814                 &phba->sli4_hba.lpfc_els_sgl_list);
11815             sglq_entry->state = SGL_FREED;
11816             spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock,
11817                            iflag);
11818 
11819             if (ndlp) {
11820                 lpfc_set_rrq_active(phba, ndlp,
11821                     sglq_entry->sli4_lxritag,
11822                     rxid, 1);
11823                 lpfc_nlp_put(ndlp);
11824             }
11825 
11826             /* Check if TXQ queue needs to be serviced */
11827             if (pring && !list_empty(&pring->txq))
11828                 lpfc_worker_wake_up(phba);
11829             return;
11830         }
11831     }
11832     spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
11833     lxri = lpfc_sli4_xri_inrange(phba, xri);
11834     if (lxri == NO_XRI)
11835         return;
11836 
11837     spin_lock_irqsave(&phba->hbalock, iflag);
11838     sglq_entry = __lpfc_get_active_sglq(phba, lxri);
11839     if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
11840         spin_unlock_irqrestore(&phba->hbalock, iflag);
11841         return;
11842     }
11843     sglq_entry->state = SGL_XRI_ABORTED;
11844     spin_unlock_irqrestore(&phba->hbalock, iflag);
11845     return;
11846 }
11847 
11848 /* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
11849  * @vport: pointer to virtual port object.
11850  * @ndlp: nodelist pointer for the impacted node.
11851  *
11852  * The driver calls this routine in response to an SLI4 XRI ABORT CQE
11853  * or an SLI3 ASYNC_STATUS_CN event from the port.  For either event,
11854  * the driver is required to send a LOGO to the remote node before it
11855  * attempts to recover its login to the remote node.
11856  */
11857 void
11858 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
11859                struct lpfc_nodelist *ndlp)
11860 {
11861     struct Scsi_Host *shost;
11862     struct lpfc_hba *phba;
11863     unsigned long flags = 0;
11864 
11865     shost = lpfc_shost_from_vport(vport);
11866     phba = vport->phba;
11867     if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
11868         lpfc_printf_log(phba, KERN_INFO,
11869                 LOG_SLI, "3093 No rport recovery needed. "
11870                 "rport in state 0x%x\n", ndlp->nlp_state);
11871         return;
11872     }
11873     lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11874             "3094 Start rport recovery on shost id 0x%x "
11875             "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
11876             "flags 0x%x\n",
11877             shost->host_no, ndlp->nlp_DID,
11878             vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
11879             ndlp->nlp_flag);
11880     /*
11881      * The rport is not responding.  Remove the FCP-2 flag to prevent
11882      * an ADISC in the follow-up recovery code.
11883      */
11884     spin_lock_irqsave(&ndlp->lock, flags);
11885     ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
11886     ndlp->nlp_flag |= NLP_ISSUE_LOGO;
11887     spin_unlock_irqrestore(&ndlp->lock, flags);
11888     lpfc_unreg_rpi(vport, ndlp);
11889 }
11890 
11891 static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport)
11892 {
11893     bitmap_zero(vport->vmid_priority_range, LPFC_VMID_MAX_PRIORITY_RANGE);
11894 }
11895 
11896 static void
11897 lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max)
11898 {
11899     u32 i;
11900 
11901     if ((min > max) || (max > LPFC_VMID_MAX_PRIORITY_RANGE))
11902         return;
11903 
11904     for (i = min; i <= max; i++)
11905         set_bit(i, vport->vmid_priority_range);
11906 }
11907 
11908 static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid)
11909 {
11910     set_bit(ctcl_vmid, vport->vmid_priority_range);
11911 }
11912 
11913 u32 lpfc_vmid_get_cs_ctl(struct lpfc_vport *vport)
11914 {
11915     u32 i;
11916 
11917     i = find_first_bit(vport->vmid_priority_range,
11918                LPFC_VMID_MAX_PRIORITY_RANGE);
11919 
11920     if (i == LPFC_VMID_MAX_PRIORITY_RANGE)
11921         return 0;
11922 
11923     clear_bit(i, vport->vmid_priority_range);
11924     return i;
11925 }
11926 
11927 #define MAX_PRIORITY_DESC   255
11928 
11929 static void
11930 lpfc_cmpl_els_qfpa(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11931            struct lpfc_iocbq *rspiocb)
11932 {
11933     struct lpfc_vport *vport = cmdiocb->vport;
11934     struct priority_range_desc *desc;
11935     struct lpfc_dmabuf *prsp = NULL;
11936     struct lpfc_vmid_priority_range *vmid_range = NULL;
11937     u32 *data;
11938     struct lpfc_dmabuf *dmabuf = cmdiocb->cmd_dmabuf;
11939     u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
11940     u32 ulp_word4 = get_job_word4(phba, rspiocb);
11941     u8 *pcmd, max_desc;
11942     u32 len, i;
11943     struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
11944 
11945     prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
11946     if (!prsp)
11947         goto out;
11948 
11949     pcmd = prsp->virt;
11950     data = (u32 *)pcmd;
11951     if (data[0] == ELS_CMD_LS_RJT) {
11952         lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
11953                  "3277 QFPA LS_RJT x%x  x%x\n",
11954                  data[0], data[1]);
11955         goto out;
11956     }
11957     if (ulp_status) {
11958         lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
11959                  "6529 QFPA failed with status x%x  x%x\n",
11960                  ulp_status, ulp_word4);
11961         goto out;
11962     }
11963 
11964     if (!vport->qfpa_res) {
11965         max_desc = FCELSSIZE / sizeof(*vport->qfpa_res);
11966         vport->qfpa_res = kcalloc(max_desc, sizeof(*vport->qfpa_res),
11967                       GFP_KERNEL);
11968         if (!vport->qfpa_res)
11969             goto out;
11970     }
11971 
11972     len = *((u32 *)(pcmd + 4));
11973     len = be32_to_cpu(len);
11974     memcpy(vport->qfpa_res, pcmd, len + 8);
11975     len = len / LPFC_PRIORITY_RANGE_DESC_SIZE;
11976 
11977     desc = (struct priority_range_desc *)(pcmd + 8);
11978     vmid_range = vport->vmid_priority.vmid_range;
11979     if (!vmid_range) {
11980         vmid_range = kcalloc(MAX_PRIORITY_DESC, sizeof(*vmid_range),
11981                      GFP_KERNEL);
11982         if (!vmid_range) {
11983             kfree(vport->qfpa_res);
11984             goto out;
11985         }
11986         vport->vmid_priority.vmid_range = vmid_range;
11987     }
11988     vport->vmid_priority.num_descriptors = len;
11989 
11990     for (i = 0; i < len; i++, vmid_range++, desc++) {
11991         lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
11992                  "6539 vmid values low=%d, high=%d, qos=%d, "
11993                  "local ve id=%d\n", desc->lo_range,
11994                  desc->hi_range, desc->qos_priority,
11995                  desc->local_ve_id);
11996 
11997         vmid_range->low = desc->lo_range << 1;
11998         if (desc->local_ve_id == QFPA_ODD_ONLY)
11999             vmid_range->low++;
12000         if (desc->qos_priority)
12001             vport->vmid_flag |= LPFC_VMID_QOS_ENABLED;
12002         vmid_range->qos = desc->qos_priority;
12003 
12004         vmid_range->high = desc->hi_range << 1;
12005         if ((desc->local_ve_id == QFPA_ODD_ONLY) ||
12006             (desc->local_ve_id == QFPA_EVEN_ODD))
12007             vmid_range->high++;
12008     }
12009     lpfc_init_cs_ctl_bitmap(vport);
12010     for (i = 0; i < vport->vmid_priority.num_descriptors; i++) {
12011         lpfc_vmid_set_cs_ctl_range(vport,
12012                 vport->vmid_priority.vmid_range[i].low,
12013                 vport->vmid_priority.vmid_range[i].high);
12014     }
12015 
12016     vport->vmid_flag |= LPFC_VMID_QFPA_CMPL;
12017  out:
12018     lpfc_els_free_iocb(phba, cmdiocb);
12019     lpfc_nlp_put(ndlp);
12020 }
12021 
12022 int lpfc_issue_els_qfpa(struct lpfc_vport *vport)
12023 {
12024     struct lpfc_hba *phba = vport->phba;
12025     struct lpfc_nodelist *ndlp;
12026     struct lpfc_iocbq *elsiocb;
12027     u8 *pcmd;
12028     int ret;
12029 
12030     ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
12031     if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12032         return -ENXIO;
12033 
12034     elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_QFPA_SIZE, 2, ndlp,
12035                      ndlp->nlp_DID, ELS_CMD_QFPA);
12036     if (!elsiocb)
12037         return -ENOMEM;
12038 
12039     pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
12040 
12041     *((u32 *)(pcmd)) = ELS_CMD_QFPA;
12042     pcmd += 4;
12043 
12044     elsiocb->cmd_cmpl = lpfc_cmpl_els_qfpa;
12045 
12046     elsiocb->ndlp = lpfc_nlp_get(ndlp);
12047     if (!elsiocb->ndlp) {
12048         lpfc_els_free_iocb(vport->phba, elsiocb);
12049         return -ENXIO;
12050     }
12051 
12052     ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 2);
12053     if (ret != IOCB_SUCCESS) {
12054         lpfc_els_free_iocb(phba, elsiocb);
12055         lpfc_nlp_put(ndlp);
12056         return -EIO;
12057     }
12058     vport->vmid_flag &= ~LPFC_VMID_QOS_ENABLED;
12059     return 0;
12060 }
12061 
12062 int
12063 lpfc_vmid_uvem(struct lpfc_vport *vport,
12064            struct lpfc_vmid *vmid, bool instantiated)
12065 {
12066     struct lpfc_vem_id_desc *vem_id_desc;
12067     struct lpfc_nodelist *ndlp;
12068     struct lpfc_iocbq *elsiocb;
12069     struct instantiated_ve_desc *inst_desc;
12070     struct lpfc_vmid_context *vmid_context;
12071     u8 *pcmd;
12072     u32 *len;
12073     int ret = 0;
12074 
12075     ndlp = lpfc_findnode_did(vport, Fabric_DID);
12076     if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12077         return -ENXIO;
12078 
12079     vmid_context = kmalloc(sizeof(*vmid_context), GFP_KERNEL);
12080     if (!vmid_context)
12081         return -ENOMEM;
12082     elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_UVEM_SIZE, 2,
12083                      ndlp, Fabric_DID, ELS_CMD_UVEM);
12084     if (!elsiocb)
12085         goto out;
12086 
12087     lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
12088              "3427 Host vmid %s %d\n",
12089              vmid->host_vmid, instantiated);
12090     vmid_context->vmp = vmid;
12091     vmid_context->nlp = ndlp;
12092     vmid_context->instantiated = instantiated;
12093     elsiocb->vmid_tag.vmid_context = vmid_context;
12094     pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
12095 
12096     if (uuid_is_null((uuid_t *)vport->lpfc_vmid_host_uuid))
12097         memcpy(vport->lpfc_vmid_host_uuid, vmid->host_vmid,
12098                LPFC_COMPRESS_VMID_SIZE);
12099 
12100     *((u32 *)(pcmd)) = ELS_CMD_UVEM;
12101     len = (u32 *)(pcmd + 4);
12102     *len = cpu_to_be32(LPFC_UVEM_SIZE - 8);
12103 
12104     vem_id_desc = (struct lpfc_vem_id_desc *)(pcmd + 8);
12105     vem_id_desc->tag = be32_to_cpu(VEM_ID_DESC_TAG);
12106     vem_id_desc->length = be32_to_cpu(LPFC_UVEM_VEM_ID_DESC_SIZE);
12107     memcpy(vem_id_desc->vem_id, vport->lpfc_vmid_host_uuid,
12108            LPFC_COMPRESS_VMID_SIZE);
12109 
12110     inst_desc = (struct instantiated_ve_desc *)(pcmd + 32);
12111     inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
12112     inst_desc->length = be32_to_cpu(LPFC_UVEM_VE_MAP_DESC_SIZE);
12113     memcpy(inst_desc->global_vem_id, vmid->host_vmid,
12114            LPFC_COMPRESS_VMID_SIZE);
12115 
12116     bf_set(lpfc_instantiated_nport_id, inst_desc, vport->fc_myDID);
12117     bf_set(lpfc_instantiated_local_id, inst_desc,
12118            vmid->un.cs_ctl_vmid);
12119     if (instantiated) {
12120         inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
12121     } else {
12122         inst_desc->tag = be32_to_cpu(DEINSTANTIATED_VE_DESC_TAG);
12123         lpfc_vmid_put_cs_ctl(vport, vmid->un.cs_ctl_vmid);
12124     }
12125     inst_desc->word6 = cpu_to_be32(inst_desc->word6);
12126 
12127     elsiocb->cmd_cmpl = lpfc_cmpl_els_uvem;
12128 
12129     elsiocb->ndlp = lpfc_nlp_get(ndlp);
12130     if (!elsiocb->ndlp) {
12131         lpfc_els_free_iocb(vport->phba, elsiocb);
12132         goto out;
12133     }
12134 
12135     ret = lpfc_sli_issue_iocb(vport->phba, LPFC_ELS_RING, elsiocb, 0);
12136     if (ret != IOCB_SUCCESS) {
12137         lpfc_els_free_iocb(vport->phba, elsiocb);
12138         lpfc_nlp_put(ndlp);
12139         goto out;
12140     }
12141 
12142     return 0;
12143  out:
12144     kfree(vmid_context);
12145     return -EIO;
12146 }
12147 
12148 static void
12149 lpfc_cmpl_els_uvem(struct lpfc_hba *phba, struct lpfc_iocbq *icmdiocb,
12150            struct lpfc_iocbq *rspiocb)
12151 {
12152     struct lpfc_vport *vport = icmdiocb->vport;
12153     struct lpfc_dmabuf *prsp = NULL;
12154     struct lpfc_vmid_context *vmid_context =
12155         icmdiocb->vmid_tag.vmid_context;
12156     struct lpfc_nodelist *ndlp = icmdiocb->ndlp;
12157     u8 *pcmd;
12158     u32 *data;
12159     u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12160     u32 ulp_word4 = get_job_word4(phba, rspiocb);
12161     struct lpfc_dmabuf *dmabuf = icmdiocb->cmd_dmabuf;
12162     struct lpfc_vmid *vmid;
12163 
12164     vmid = vmid_context->vmp;
12165     if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12166         ndlp = NULL;
12167 
12168     prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
12169     if (!prsp)
12170         goto out;
12171     pcmd = prsp->virt;
12172     data = (u32 *)pcmd;
12173     if (data[0] == ELS_CMD_LS_RJT) {
12174         lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
12175                  "4532 UVEM LS_RJT %x %x\n", data[0], data[1]);
12176         goto out;
12177     }
12178     if (ulp_status) {
12179         lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
12180                  "4533 UVEM error status %x: %x\n",
12181                  ulp_status, ulp_word4);
12182         goto out;
12183     }
12184     spin_lock(&phba->hbalock);
12185     /* Set IN USE flag */
12186     vport->vmid_flag |= LPFC_VMID_IN_USE;
12187     phba->pport->vmid_flag |= LPFC_VMID_IN_USE;
12188     spin_unlock(&phba->hbalock);
12189 
12190     if (vmid_context->instantiated) {
12191         write_lock(&vport->vmid_lock);
12192         vmid->flag |= LPFC_VMID_REGISTERED;
12193         vmid->flag &= ~LPFC_VMID_REQ_REGISTER;
12194         write_unlock(&vport->vmid_lock);
12195     }
12196 
12197  out:
12198     kfree(vmid_context);
12199     lpfc_els_free_iocb(phba, icmdiocb);
12200     lpfc_nlp_put(ndlp);
12201 }