root/drivers/scsi/lpfc/lpfc_els.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. lpfc_els_chk_latt
  2. lpfc_prep_els_iocb
  3. lpfc_issue_fabric_reglogin
  4. lpfc_issue_reg_vfi
  5. lpfc_issue_unreg_vfi
  6. lpfc_check_clean_addr_bit
  7. lpfc_cmpl_els_flogi_fabric
  8. lpfc_cmpl_els_flogi_nport
  9. lpfc_cmpl_els_flogi
  10. lpfc_cmpl_els_link_down
  11. lpfc_issue_els_flogi
  12. lpfc_els_abort_flogi
  13. lpfc_initial_flogi
  14. lpfc_initial_fdisc
  15. lpfc_more_plogi
  16. lpfc_plogi_confirm_nport
  17. lpfc_end_rscn
  18. lpfc_cmpl_els_rrq
  19. lpfc_cmpl_els_plogi
  20. lpfc_issue_els_plogi
  21. lpfc_cmpl_els_prli
  22. lpfc_issue_els_prli
  23. lpfc_rscn_disc
  24. lpfc_adisc_done
  25. lpfc_more_adisc
  26. lpfc_cmpl_els_adisc
  27. lpfc_issue_els_adisc
  28. lpfc_cmpl_els_logo
  29. lpfc_issue_els_logo
  30. lpfc_cmpl_els_cmd
  31. lpfc_issue_els_scr
  32. lpfc_issue_els_rscn
  33. lpfc_issue_els_farpr
  34. lpfc_cancel_retry_delay_tmo
  35. lpfc_els_retry_delay
  36. lpfc_els_retry_delay_handler
  37. lpfc_link_reset
  38. lpfc_els_retry
  39. lpfc_els_free_data
  40. lpfc_els_free_bpl
  41. lpfc_els_free_iocb
  42. lpfc_cmpl_els_logo_acc
  43. lpfc_mbx_cmpl_dflt_rpi
  44. lpfc_cmpl_els_rsp
  45. lpfc_els_rsp_acc
  46. lpfc_els_rsp_reject
  47. lpfc_els_rsp_adisc_acc
  48. lpfc_els_rsp_prli_acc
  49. lpfc_els_rsp_rnid_acc
  50. lpfc_els_clear_rrq
  51. lpfc_els_rsp_echo_acc
  52. lpfc_els_disc_adisc
  53. lpfc_els_disc_plogi
  54. lpfc_rdp_res_link_service
  55. lpfc_rdp_res_sfp_desc
  56. lpfc_rdp_res_link_error
  57. lpfc_rdp_res_bbc_desc
  58. lpfc_rdp_res_oed_temp_desc
  59. lpfc_rdp_res_oed_voltage_desc
  60. lpfc_rdp_res_oed_txbias_desc
  61. lpfc_rdp_res_oed_txpower_desc
  62. lpfc_rdp_res_oed_rxpower_desc
  63. lpfc_rdp_res_opd_desc
  64. lpfc_rdp_res_fec_desc
  65. lpfc_rdp_res_speed
  66. lpfc_rdp_res_diag_port_names
  67. lpfc_rdp_res_attach_port_names
  68. lpfc_els_rdp_cmpl
  69. lpfc_get_rdp_info
  70. lpfc_els_rcv_rdp
  71. lpfc_els_lcb_rsp
  72. lpfc_sli4_set_beacon
  73. lpfc_els_rcv_lcb
  74. lpfc_els_flush_rscn
  75. lpfc_rscn_payload_check
  76. lpfc_rscn_recovery_check
  77. lpfc_send_rscn_event
  78. lpfc_els_rcv_rscn
  79. lpfc_els_handle_rscn
  80. lpfc_els_rcv_flogi
  81. lpfc_els_rcv_rnid
  82. lpfc_els_rcv_echo
  83. lpfc_els_rcv_lirr
  84. lpfc_els_rcv_rrq
  85. lpfc_els_rsp_rls_acc
  86. lpfc_els_rsp_rps_acc
  87. lpfc_els_rcv_rls
  88. lpfc_els_rcv_rtv
  89. lpfc_els_rcv_rps
  90. lpfc_issue_els_rrq
  91. lpfc_send_rrq
  92. lpfc_els_rsp_rpl_acc
  93. lpfc_els_rcv_rpl
  94. lpfc_els_rcv_farp
  95. lpfc_els_rcv_farpr
  96. lpfc_els_rcv_fan
  97. lpfc_els_timeout
  98. lpfc_els_timeout_handler
  99. lpfc_els_flush_cmd
  100. lpfc_els_flush_all_cmd
  101. lpfc_send_els_failure_event
  102. lpfc_send_els_event
  103. lpfc_els_unsol_buffer
  104. lpfc_els_unsol_event
  105. lpfc_start_fdmi
  106. lpfc_do_scr_ns_plogi
  107. lpfc_cmpl_reg_new_vport
  108. lpfc_register_new_vport
  109. lpfc_cancel_all_vport_retry_delay_timer
  110. lpfc_retry_pport_discovery
  111. lpfc_fabric_login_reqd
  112. lpfc_cmpl_els_fdisc
  113. lpfc_issue_els_fdisc
  114. lpfc_cmpl_els_npiv_logo
  115. lpfc_issue_els_npiv_logo
  116. lpfc_fabric_block_timeout
  117. lpfc_resume_fabric_iocbs
  118. lpfc_unblock_fabric_iocbs
  119. lpfc_block_fabric_iocbs
  120. lpfc_cmpl_fabric_iocb
  121. lpfc_issue_fabric_iocb
  122. lpfc_fabric_abort_vport
  123. lpfc_fabric_abort_nport
  124. lpfc_fabric_abort_hba
  125. lpfc_sli4_vport_delete_els_xri_aborted
  126. lpfc_sli4_els_xri_aborted
  127. lpfc_sli_abts_recover_port

   1 /*******************************************************************
   2  * This file is part of the Emulex Linux Device Driver for         *
   3  * Fibre Channel Host Bus Adapters.                                *
   4  * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
   5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
   6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
   7  * EMULEX and SLI are trademarks of Emulex.                        *
   8  * www.broadcom.com                                                *
   9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
  10  *                                                                 *
  11  * This program is free software; you can redistribute it and/or   *
  12  * modify it under the terms of version 2 of the GNU General       *
  13  * Public License as published by the Free Software Foundation.    *
  14  * This program is distributed in the hope that it will be useful. *
  15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
  16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
  17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
  18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
  20  * more details, a copy of which can be found in the file COPYING  *
  21  * included with this package.                                     *
  22  *******************************************************************/
  23 /* See Fibre Channel protocol T11 FC-LS for details */
  24 #include <linux/blkdev.h>
  25 #include <linux/pci.h>
  26 #include <linux/slab.h>
  27 #include <linux/interrupt.h>
  28 
  29 #include <scsi/scsi.h>
  30 #include <scsi/scsi_device.h>
  31 #include <scsi/scsi_host.h>
  32 #include <scsi/scsi_transport_fc.h>
  33 #include <uapi/scsi/fc/fc_fs.h>
  34 #include <uapi/scsi/fc/fc_els.h>
  35 
  36 #include "lpfc_hw4.h"
  37 #include "lpfc_hw.h"
  38 #include "lpfc_sli.h"
  39 #include "lpfc_sli4.h"
  40 #include "lpfc_nl.h"
  41 #include "lpfc_disc.h"
  42 #include "lpfc_scsi.h"
  43 #include "lpfc.h"
  44 #include "lpfc_logmsg.h"
  45 #include "lpfc_crtn.h"
  46 #include "lpfc_vport.h"
  47 #include "lpfc_debugfs.h"
  48 
  49 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
  50                           struct lpfc_iocbq *);
  51 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
  52                         struct lpfc_iocbq *);
  53 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
  54 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
  55                                 struct lpfc_nodelist *ndlp, uint8_t retry);
  56 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
  57                                   struct lpfc_iocbq *iocb);
  58 
  59 static int lpfc_max_els_tries = 3;
  60 
  61 /**
  62  * lpfc_els_chk_latt - Check host link attention event for a vport
  63  * @vport: pointer to a host virtual N_Port data structure.
  64  *
  65  * This routine checks whether there is an outstanding host link
  66  * attention event during the discovery process with the @vport. It is done
  67  * by reading the HBA's Host Attention (HA) register. If there is any host
  68  * link attention events during this @vport's discovery process, the @vport
  69  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
  70  * be issued if the link state is not already in host link cleared state,
  71  * and a return code shall indicate whether the host link attention event
  72  * had happened.
  73  *
  74  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
  75  * state in LPFC_VPORT_READY, the request for checking host link attention
  76  * event will be ignored and a return code shall indicate no host link
  77  * attention event had happened.
  78  *
  79  * Return codes
  80  *   0 - no host link attention event happened
  81  *   1 - host link attention event happened
  82  **/
  83 int
  84 lpfc_els_chk_latt(struct lpfc_vport *vport)
  85 {
  86         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  87         struct lpfc_hba  *phba = vport->phba;
  88         uint32_t ha_copy;
  89 
  90         if (vport->port_state >= LPFC_VPORT_READY ||
  91             phba->link_state == LPFC_LINK_DOWN ||
  92             phba->sli_rev > LPFC_SLI_REV3)
  93                 return 0;
  94 
  95         /* Read the HBA Host Attention Register */
  96         if (lpfc_readl(phba->HAregaddr, &ha_copy))
  97                 return 1;
  98 
  99         if (!(ha_copy & HA_LATT))
 100                 return 0;
 101 
 102         /* Pending Link Event during Discovery */
 103         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
 104                          "0237 Pending Link Event during "
 105                          "Discovery: State x%x\n",
 106                          phba->pport->port_state);
 107 
 108         /* CLEAR_LA should re-enable link attention events and
 109          * we should then immediately take a LATT event. The
 110          * LATT processing should call lpfc_linkdown() which
 111          * will cleanup any left over in-progress discovery
 112          * events.
 113          */
 114         spin_lock_irq(shost->host_lock);
 115         vport->fc_flag |= FC_ABORT_DISCOVERY;
 116         spin_unlock_irq(shost->host_lock);
 117 
 118         if (phba->link_state != LPFC_CLEAR_LA)
 119                 lpfc_issue_clear_la(phba, vport);
 120 
 121         return 1;
 122 }
 123 
 124 /**
 125  * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
 126  * @vport: pointer to a host virtual N_Port data structure.
 127  * @expectRsp: flag indicating whether response is expected.
 128  * @cmdSize: size of the ELS command.
 129  * @retry: number of retries to the command IOCB when it fails.
 130  * @ndlp: pointer to a node-list data structure.
 131  * @did: destination identifier.
 132  * @elscmd: the ELS command code.
 133  *
 134  * This routine is used for allocating a lpfc-IOCB data structure from
 135  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
 136  * passed into the routine for discovery state machine to issue an Extended
 137  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
 138  * and preparation routine that is used by all the discovery state machine
 139  * routines and the ELS command-specific fields will be later set up by
 140  * the individual discovery machine routines after calling this routine
 141  * allocating and preparing a generic IOCB data structure. It fills in the
 142  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
 143  * payload and response payload (if expected). The reference count on the
 144  * ndlp is incremented by 1 and the reference to the ndlp is put into
 145  * context1 of the IOCB data structure for this IOCB to hold the ndlp
 146  * reference for the command's callback function to access later.
 147  *
 148  * Return code
 149  *   Pointer to the newly allocated/prepared els iocb data structure
 150  *   NULL - when els iocb data structure allocation/preparation failed
 151  **/
 152 struct lpfc_iocbq *
 153 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
 154                    uint16_t cmdSize, uint8_t retry,
 155                    struct lpfc_nodelist *ndlp, uint32_t did,
 156                    uint32_t elscmd)
 157 {
 158         struct lpfc_hba  *phba = vport->phba;
 159         struct lpfc_iocbq *elsiocb;
 160         struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
 161         struct ulp_bde64 *bpl;
 162         IOCB_t *icmd;
 163 
 164 
 165         if (!lpfc_is_link_up(phba))
 166                 return NULL;
 167 
 168         /* Allocate buffer for  command iocb */
 169         elsiocb = lpfc_sli_get_iocbq(phba);
 170 
 171         if (elsiocb == NULL)
 172                 return NULL;
 173 
 174         /*
 175          * If this command is for fabric controller and HBA running
 176          * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
 177          */
 178         if ((did == Fabric_DID) &&
 179                 (phba->hba_flag & HBA_FIP_SUPPORT) &&
 180                 ((elscmd == ELS_CMD_FLOGI) ||
 181                  (elscmd == ELS_CMD_FDISC) ||
 182                  (elscmd == ELS_CMD_LOGO)))
 183                 switch (elscmd) {
 184                 case ELS_CMD_FLOGI:
 185                 elsiocb->iocb_flag |=
 186                         ((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
 187                                         & LPFC_FIP_ELS_ID_MASK);
 188                 break;
 189                 case ELS_CMD_FDISC:
 190                 elsiocb->iocb_flag |=
 191                         ((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
 192                                         & LPFC_FIP_ELS_ID_MASK);
 193                 break;
 194                 case ELS_CMD_LOGO:
 195                 elsiocb->iocb_flag |=
 196                         ((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
 197                                         & LPFC_FIP_ELS_ID_MASK);
 198                 break;
 199                 }
 200         else
 201                 elsiocb->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
 202 
 203         icmd = &elsiocb->iocb;
 204 
 205         /* fill in BDEs for command */
 206         /* Allocate buffer for command payload */
 207         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
 208         if (pcmd)
 209                 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
 210         if (!pcmd || !pcmd->virt)
 211                 goto els_iocb_free_pcmb_exit;
 212 
 213         INIT_LIST_HEAD(&pcmd->list);
 214 
 215         /* Allocate buffer for response payload */
 216         if (expectRsp) {
 217                 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
 218                 if (prsp)
 219                         prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
 220                                                      &prsp->phys);
 221                 if (!prsp || !prsp->virt)
 222                         goto els_iocb_free_prsp_exit;
 223                 INIT_LIST_HEAD(&prsp->list);
 224         } else
 225                 prsp = NULL;
 226 
 227         /* Allocate buffer for Buffer ptr list */
 228         pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
 229         if (pbuflist)
 230                 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
 231                                                  &pbuflist->phys);
 232         if (!pbuflist || !pbuflist->virt)
 233                 goto els_iocb_free_pbuf_exit;
 234 
 235         INIT_LIST_HEAD(&pbuflist->list);
 236 
 237         if (expectRsp) {
 238                 icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
 239                 icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
 240                 icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
 241                 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
 242 
 243                 icmd->un.elsreq64.remoteID = did;               /* DID */
 244                 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
 245                 if (elscmd == ELS_CMD_FLOGI)
 246                         icmd->ulpTimeout = FF_DEF_RATOV * 2;
 247                 else if (elscmd == ELS_CMD_LOGO)
 248                         icmd->ulpTimeout = phba->fc_ratov;
 249                 else
 250                         icmd->ulpTimeout = phba->fc_ratov * 2;
 251         } else {
 252                 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
 253                 icmd->un.xseq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
 254                 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
 255                 icmd->un.xseq64.bdl.bdeSize = sizeof(struct ulp_bde64);
 256                 icmd->un.xseq64.xmit_els_remoteID = did;        /* DID */
 257                 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
 258         }
 259         icmd->ulpBdeCount = 1;
 260         icmd->ulpLe = 1;
 261         icmd->ulpClass = CLASS3;
 262 
 263         /*
 264          * If we have NPIV enabled, we want to send ELS traffic by VPI.
 265          * For SLI4, since the driver controls VPIs we also want to include
 266          * all ELS pt2pt protocol traffic as well.
 267          */
 268         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
 269                 ((phba->sli_rev == LPFC_SLI_REV4) &&
 270                     (vport->fc_flag & FC_PT2PT))) {
 271 
 272                 if (expectRsp) {
 273                         icmd->un.elsreq64.myID = vport->fc_myDID;
 274 
 275                         /* For ELS_REQUEST64_CR, use the VPI by default */
 276                         icmd->ulpContext = phba->vpi_ids[vport->vpi];
 277                 }
 278 
 279                 icmd->ulpCt_h = 0;
 280                 /* The CT field must be 0=INVALID_RPI for the ECHO cmd */
 281                 if (elscmd == ELS_CMD_ECHO)
 282                         icmd->ulpCt_l = 0; /* context = invalid RPI */
 283                 else
 284                         icmd->ulpCt_l = 1; /* context = VPI */
 285         }
 286 
 287         bpl = (struct ulp_bde64 *) pbuflist->virt;
 288         bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
 289         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
 290         bpl->tus.f.bdeSize = cmdSize;
 291         bpl->tus.f.bdeFlags = 0;
 292         bpl->tus.w = le32_to_cpu(bpl->tus.w);
 293 
 294         if (expectRsp) {
 295                 bpl++;
 296                 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
 297                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
 298                 bpl->tus.f.bdeSize = FCELSSIZE;
 299                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
 300                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
 301         }
 302 
 303         /* prevent preparing iocb with NULL ndlp reference */
 304         elsiocb->context1 = lpfc_nlp_get(ndlp);
 305         if (!elsiocb->context1)
 306                 goto els_iocb_free_pbuf_exit;
 307         elsiocb->context2 = pcmd;
 308         elsiocb->context3 = pbuflist;
 309         elsiocb->retry = retry;
 310         elsiocb->vport = vport;
 311         elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
 312 
 313         if (prsp) {
 314                 list_add(&prsp->list, &pcmd->list);
 315         }
 316         if (expectRsp) {
 317                 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
 318                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 319                                  "0116 Xmit ELS command x%x to remote "
 320                                  "NPORT x%x I/O tag: x%x, port state:x%x "
 321                                  "rpi x%x fc_flag:x%x\n",
 322                                  elscmd, did, elsiocb->iotag,
 323                                  vport->port_state, ndlp->nlp_rpi,
 324                                  vport->fc_flag);
 325         } else {
 326                 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
 327                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 328                                  "0117 Xmit ELS response x%x to remote "
 329                                  "NPORT x%x I/O tag: x%x, size: x%x "
 330                                  "port_state x%x  rpi x%x fc_flag x%x\n",
 331                                  elscmd, ndlp->nlp_DID, elsiocb->iotag,
 332                                  cmdSize, vport->port_state,
 333                                  ndlp->nlp_rpi, vport->fc_flag);
 334         }
 335         return elsiocb;
 336 
 337 els_iocb_free_pbuf_exit:
 338         if (expectRsp)
 339                 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
 340         kfree(pbuflist);
 341 
 342 els_iocb_free_prsp_exit:
 343         lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
 344         kfree(prsp);
 345 
 346 els_iocb_free_pcmb_exit:
 347         kfree(pcmd);
 348         lpfc_sli_release_iocbq(phba, elsiocb);
 349         return NULL;
 350 }
 351 
 352 /**
 353  * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
 354  * @vport: pointer to a host virtual N_Port data structure.
 355  *
 356  * This routine issues a fabric registration login for a @vport. An
 357  * active ndlp node with Fabric_DID must already exist for this @vport.
 358  * The routine invokes two mailbox commands to carry out fabric registration
 359  * login through the HBA firmware: the first mailbox command requests the
 360  * HBA to perform link configuration for the @vport; and the second mailbox
 361  * command requests the HBA to perform the actual fabric registration login
 362  * with the @vport.
 363  *
 364  * Return code
 365  *   0 - successfully issued fabric registration login for @vport
 366  *   -ENXIO -- failed to issue fabric registration login for @vport
 367  **/
 368 int
 369 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
 370 {
 371         struct lpfc_hba  *phba = vport->phba;
 372         LPFC_MBOXQ_t *mbox;
 373         struct lpfc_dmabuf *mp;
 374         struct lpfc_nodelist *ndlp;
 375         struct serv_parm *sp;
 376         int rc;
 377         int err = 0;
 378 
 379         sp = &phba->fc_fabparam;
 380         ndlp = lpfc_findnode_did(vport, Fabric_DID);
 381         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
 382                 err = 1;
 383                 goto fail;
 384         }
 385 
 386         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 387         if (!mbox) {
 388                 err = 2;
 389                 goto fail;
 390         }
 391 
 392         vport->port_state = LPFC_FABRIC_CFG_LINK;
 393         lpfc_config_link(phba, mbox);
 394         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
 395         mbox->vport = vport;
 396 
 397         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 398         if (rc == MBX_NOT_FINISHED) {
 399                 err = 3;
 400                 goto fail_free_mbox;
 401         }
 402 
 403         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 404         if (!mbox) {
 405                 err = 4;
 406                 goto fail;
 407         }
 408         rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
 409                           ndlp->nlp_rpi);
 410         if (rc) {
 411                 err = 5;
 412                 goto fail_free_mbox;
 413         }
 414 
 415         mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
 416         mbox->vport = vport;
 417         /* increment the reference count on ndlp to hold reference
 418          * for the callback routine.
 419          */
 420         mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
 421 
 422         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 423         if (rc == MBX_NOT_FINISHED) {
 424                 err = 6;
 425                 goto fail_issue_reg_login;
 426         }
 427 
 428         return 0;
 429 
 430 fail_issue_reg_login:
 431         /* decrement the reference count on ndlp just incremented
 432          * for the failed mbox command.
 433          */
 434         lpfc_nlp_put(ndlp);
 435         mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
 436         lpfc_mbuf_free(phba, mp->virt, mp->phys);
 437         kfree(mp);
 438 fail_free_mbox:
 439         mempool_free(mbox, phba->mbox_mem_pool);
 440 
 441 fail:
 442         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
 443         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
 444                 "0249 Cannot issue Register Fabric login: Err %d\n", err);
 445         return -ENXIO;
 446 }
 447 
 448 /**
 449  * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
 450  * @vport: pointer to a host virtual N_Port data structure.
 451  *
 452  * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
 453  * the @vport. This mailbox command is necessary for SLI4 port only.
 454  *
 455  * Return code
 456  *   0 - successfully issued REG_VFI for @vport
 457  *   A failure code otherwise.
 458  **/
 459 int
 460 lpfc_issue_reg_vfi(struct lpfc_vport *vport)
 461 {
 462         struct lpfc_hba  *phba = vport->phba;
 463         LPFC_MBOXQ_t *mboxq = NULL;
 464         struct lpfc_nodelist *ndlp;
 465         struct lpfc_dmabuf *dmabuf = NULL;
 466         int rc = 0;
 467 
 468         /* move forward in case of SLI4 FC port loopback test and pt2pt mode */
 469         if ((phba->sli_rev == LPFC_SLI_REV4) &&
 470             !(phba->link_flag & LS_LOOPBACK_MODE) &&
 471             !(vport->fc_flag & FC_PT2PT)) {
 472                 ndlp = lpfc_findnode_did(vport, Fabric_DID);
 473                 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
 474                         rc = -ENODEV;
 475                         goto fail;
 476                 }
 477         }
 478 
 479         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 480         if (!mboxq) {
 481                 rc = -ENOMEM;
 482                 goto fail;
 483         }
 484 
 485         /* Supply CSP's only if we are fabric connect or pt-to-pt connect */
 486         if ((vport->fc_flag & FC_FABRIC) || (vport->fc_flag & FC_PT2PT)) {
 487                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
 488                 if (!dmabuf) {
 489                         rc = -ENOMEM;
 490                         goto fail;
 491                 }
 492                 dmabuf->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &dmabuf->phys);
 493                 if (!dmabuf->virt) {
 494                         rc = -ENOMEM;
 495                         goto fail;
 496                 }
 497                 memcpy(dmabuf->virt, &phba->fc_fabparam,
 498                        sizeof(struct serv_parm));
 499         }
 500 
 501         vport->port_state = LPFC_FABRIC_CFG_LINK;
 502         if (dmabuf)
 503                 lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
 504         else
 505                 lpfc_reg_vfi(mboxq, vport, 0);
 506 
 507         mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
 508         mboxq->vport = vport;
 509         mboxq->ctx_buf = dmabuf;
 510         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
 511         if (rc == MBX_NOT_FINISHED) {
 512                 rc = -ENXIO;
 513                 goto fail;
 514         }
 515         return 0;
 516 
 517 fail:
 518         if (mboxq)
 519                 mempool_free(mboxq, phba->mbox_mem_pool);
 520         if (dmabuf) {
 521                 if (dmabuf->virt)
 522                         lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
 523                 kfree(dmabuf);
 524         }
 525 
 526         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
 527         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
 528                 "0289 Issue Register VFI failed: Err %d\n", rc);
 529         return rc;
 530 }
 531 
 532 /**
 533  * lpfc_issue_unreg_vfi - Unregister VFI for this vport's fabric login
 534  * @vport: pointer to a host virtual N_Port data structure.
 535  *
 536  * This routine issues a UNREG_VFI mailbox with the vfi, vpi, fcfi triplet for
 537  * the @vport. This mailbox command is necessary for SLI4 port only.
 538  *
 539  * Return code
 540  *   0 - successfully issued REG_VFI for @vport
 541  *   A failure code otherwise.
 542  **/
 543 int
 544 lpfc_issue_unreg_vfi(struct lpfc_vport *vport)
 545 {
 546         struct lpfc_hba *phba = vport->phba;
 547         struct Scsi_Host *shost;
 548         LPFC_MBOXQ_t *mboxq;
 549         int rc;
 550 
 551         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 552         if (!mboxq) {
 553                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
 554                                 "2556 UNREG_VFI mbox allocation failed"
 555                                 "HBA state x%x\n", phba->pport->port_state);
 556                 return -ENOMEM;
 557         }
 558 
 559         lpfc_unreg_vfi(mboxq, vport);
 560         mboxq->vport = vport;
 561         mboxq->mbox_cmpl = lpfc_unregister_vfi_cmpl;
 562 
 563         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
 564         if (rc == MBX_NOT_FINISHED) {
 565                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
 566                                 "2557 UNREG_VFI issue mbox failed rc x%x "
 567                                 "HBA state x%x\n",
 568                                 rc, phba->pport->port_state);
 569                 mempool_free(mboxq, phba->mbox_mem_pool);
 570                 return -EIO;
 571         }
 572 
 573         shost = lpfc_shost_from_vport(vport);
 574         spin_lock_irq(shost->host_lock);
 575         vport->fc_flag &= ~FC_VFI_REGISTERED;
 576         spin_unlock_irq(shost->host_lock);
 577         return 0;
 578 }
 579 
 580 /**
 581  * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
 582  * @vport: pointer to a host virtual N_Port data structure.
 583  * @sp: pointer to service parameter data structure.
 584  *
 585  * This routine is called from FLOGI/FDISC completion handler functions.
 586  * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
 587  * node nodename is changed in the completion service parameter else return
 588  * 0. This function also set flag in the vport data structure to delay
 589  * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
 590  * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
 591  * node nodename is changed in the completion service parameter.
 592  *
 593  * Return code
 594  *   0 - FCID and Fabric Nodename and Fabric portname is not changed.
 595  *   1 - FCID or Fabric Nodename or Fabric portname is changed.
 596  *
 597  **/
 598 static uint8_t
 599 lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
 600                 struct serv_parm *sp)
 601 {
 602         struct lpfc_hba *phba = vport->phba;
 603         uint8_t fabric_param_changed = 0;
 604         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 605 
 606         if ((vport->fc_prevDID != vport->fc_myDID) ||
 607                 memcmp(&vport->fabric_portname, &sp->portName,
 608                         sizeof(struct lpfc_name)) ||
 609                 memcmp(&vport->fabric_nodename, &sp->nodeName,
 610                         sizeof(struct lpfc_name)) ||
 611                 (vport->vport_flag & FAWWPN_PARAM_CHG)) {
 612                 fabric_param_changed = 1;
 613                 vport->vport_flag &= ~FAWWPN_PARAM_CHG;
 614         }
 615         /*
 616          * Word 1 Bit 31 in common service parameter is overloaded.
 617          * Word 1 Bit 31 in FLOGI request is multiple NPort request
 618          * Word 1 Bit 31 in FLOGI response is clean address bit
 619          *
 620          * If fabric parameter is changed and clean address bit is
 621          * cleared delay nport discovery if
 622          * - vport->fc_prevDID != 0 (not initial discovery) OR
 623          * - lpfc_delay_discovery module parameter is set.
 624          */
 625         if (fabric_param_changed && !sp->cmn.clean_address_bit &&
 626             (vport->fc_prevDID || phba->cfg_delay_discovery)) {
 627                 spin_lock_irq(shost->host_lock);
 628                 vport->fc_flag |= FC_DISC_DELAYED;
 629                 spin_unlock_irq(shost->host_lock);
 630         }
 631 
 632         return fabric_param_changed;
 633 }
 634 
 635 
 636 /**
 637  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
 638  * @vport: pointer to a host virtual N_Port data structure.
 639  * @ndlp: pointer to a node-list data structure.
 640  * @sp: pointer to service parameter data structure.
 641  * @irsp: pointer to the IOCB within the lpfc response IOCB.
 642  *
 643  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
 644  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
 645  * port in a fabric topology. It properly sets up the parameters to the @ndlp
 646  * from the IOCB response. It also check the newly assigned N_Port ID to the
 647  * @vport against the previously assigned N_Port ID. If it is different from
 648  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
 649  * is invoked on all the remaining nodes with the @vport to unregister the
 650  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
 651  * is invoked to register login to the fabric.
 652  *
 653  * Return code
 654  *   0 - Success (currently, always return 0)
 655  **/
 656 static int
 657 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 658                            struct serv_parm *sp, IOCB_t *irsp)
 659 {
 660         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 661         struct lpfc_hba  *phba = vport->phba;
 662         struct lpfc_nodelist *np;
 663         struct lpfc_nodelist *next_np;
 664         uint8_t fabric_param_changed;
 665 
 666         spin_lock_irq(shost->host_lock);
 667         vport->fc_flag |= FC_FABRIC;
 668         spin_unlock_irq(shost->host_lock);
 669 
 670         phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
 671         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
 672                 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
 673 
 674         phba->fc_edtovResol = sp->cmn.edtovResolution;
 675         phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
 676 
 677         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
 678                 spin_lock_irq(shost->host_lock);
 679                 vport->fc_flag |= FC_PUBLIC_LOOP;
 680                 spin_unlock_irq(shost->host_lock);
 681         }
 682 
 683         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
 684         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
 685         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
 686         ndlp->nlp_class_sup = 0;
 687         if (sp->cls1.classValid)
 688                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
 689         if (sp->cls2.classValid)
 690                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
 691         if (sp->cls3.classValid)
 692                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
 693         if (sp->cls4.classValid)
 694                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
 695         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
 696                                 sp->cmn.bbRcvSizeLsb;
 697 
 698         fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
 699         if (fabric_param_changed) {
 700                 /* Reset FDMI attribute masks based on config parameter */
 701                 if (phba->cfg_enable_SmartSAN ||
 702                     (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
 703                         /* Setup appropriate attribute masks */
 704                         vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
 705                         if (phba->cfg_enable_SmartSAN)
 706                                 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
 707                         else
 708                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
 709                 } else {
 710                         vport->fdmi_hba_mask = 0;
 711                         vport->fdmi_port_mask = 0;
 712                 }
 713 
 714         }
 715         memcpy(&vport->fabric_portname, &sp->portName,
 716                         sizeof(struct lpfc_name));
 717         memcpy(&vport->fabric_nodename, &sp->nodeName,
 718                         sizeof(struct lpfc_name));
 719         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
 720 
 721         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
 722                 if (sp->cmn.response_multiple_NPort) {
 723                         lpfc_printf_vlog(vport, KERN_WARNING,
 724                                          LOG_ELS | LOG_VPORT,
 725                                          "1816 FLOGI NPIV supported, "
 726                                          "response data 0x%x\n",
 727                                          sp->cmn.response_multiple_NPort);
 728                         spin_lock_irq(&phba->hbalock);
 729                         phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
 730                         spin_unlock_irq(&phba->hbalock);
 731                 } else {
 732                         /* Because we asked f/w for NPIV it still expects us
 733                         to call reg_vnpid atleast for the physcial host */
 734                         lpfc_printf_vlog(vport, KERN_WARNING,
 735                                          LOG_ELS | LOG_VPORT,
 736                                          "1817 Fabric does not support NPIV "
 737                                          "- configuring single port mode.\n");
 738                         spin_lock_irq(&phba->hbalock);
 739                         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
 740                         spin_unlock_irq(&phba->hbalock);
 741                 }
 742         }
 743 
 744         /*
 745          * For FC we need to do some special processing because of the SLI
 746          * Port's default settings of the Common Service Parameters.
 747          */
 748         if ((phba->sli_rev == LPFC_SLI_REV4) &&
 749             (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)) {
 750                 /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
 751                 if (fabric_param_changed)
 752                         lpfc_unregister_fcf_prep(phba);
 753 
 754                 /* This should just update the VFI CSPs*/
 755                 if (vport->fc_flag & FC_VFI_REGISTERED)
 756                         lpfc_issue_reg_vfi(vport);
 757         }
 758 
 759         if (fabric_param_changed &&
 760                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
 761 
 762                 /* If our NportID changed, we need to ensure all
 763                  * remaining NPORTs get unreg_login'ed.
 764                  */
 765                 list_for_each_entry_safe(np, next_np,
 766                                         &vport->fc_nodes, nlp_listp) {
 767                         if (!NLP_CHK_NODE_ACT(np))
 768                                 continue;
 769                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
 770                                    !(np->nlp_flag & NLP_NPR_ADISC))
 771                                 continue;
 772                         spin_lock_irq(shost->host_lock);
 773                         np->nlp_flag &= ~NLP_NPR_ADISC;
 774                         spin_unlock_irq(shost->host_lock);
 775                         lpfc_unreg_rpi(vport, np);
 776                 }
 777                 lpfc_cleanup_pending_mbox(vport);
 778 
 779                 if (phba->sli_rev == LPFC_SLI_REV4) {
 780                         lpfc_sli4_unreg_all_rpis(vport);
 781                         lpfc_mbx_unreg_vpi(vport);
 782                         spin_lock_irq(shost->host_lock);
 783                         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
 784                         spin_unlock_irq(shost->host_lock);
 785                 }
 786 
 787                 /*
 788                  * For SLI3 and SLI4, the VPI needs to be reregistered in
 789                  * response to this fabric parameter change event.
 790                  */
 791                 spin_lock_irq(shost->host_lock);
 792                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
 793                 spin_unlock_irq(shost->host_lock);
 794         } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
 795                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
 796                         /*
 797                          * Driver needs to re-reg VPI in order for f/w
 798                          * to update the MAC address.
 799                          */
 800                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
 801                         lpfc_register_new_vport(phba, vport, ndlp);
 802                         return 0;
 803         }
 804 
 805         if (phba->sli_rev < LPFC_SLI_REV4) {
 806                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
 807                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
 808                     vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
 809                         lpfc_register_new_vport(phba, vport, ndlp);
 810                 else
 811                         lpfc_issue_fabric_reglogin(vport);
 812         } else {
 813                 ndlp->nlp_type |= NLP_FABRIC;
 814                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
 815                 if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
 816                         (vport->vpi_state & LPFC_VPI_REGISTERED)) {
 817                         lpfc_start_fdiscs(phba);
 818                         lpfc_do_scr_ns_plogi(phba, vport);
 819                 } else if (vport->fc_flag & FC_VFI_REGISTERED)
 820                         lpfc_issue_init_vpi(vport);
 821                 else {
 822                         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 823                                         "3135 Need register VFI: (x%x/%x)\n",
 824                                         vport->fc_prevDID, vport->fc_myDID);
 825                         lpfc_issue_reg_vfi(vport);
 826                 }
 827         }
 828         return 0;
 829 }
 830 
 831 /**
 832  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
 833  * @vport: pointer to a host virtual N_Port data structure.
 834  * @ndlp: pointer to a node-list data structure.
 835  * @sp: pointer to service parameter data structure.
 836  *
 837  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
 838  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
 839  * in a point-to-point topology. First, the @vport's N_Port Name is compared
 840  * with the received N_Port Name: if the @vport's N_Port Name is greater than
 841  * the received N_Port Name lexicographically, this node shall assign local
 842  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
 843  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
 844  * this node shall just wait for the remote node to issue PLOGI and assign
 845  * N_Port IDs.
 846  *
 847  * Return code
 848  *   0 - Success
 849  *   -ENXIO - Fail
 850  **/
 851 static int
 852 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 853                           struct serv_parm *sp)
 854 {
 855         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 856         struct lpfc_hba  *phba = vport->phba;
 857         LPFC_MBOXQ_t *mbox;
 858         int rc;
 859 
 860         spin_lock_irq(shost->host_lock);
 861         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
 862         vport->fc_flag |= FC_PT2PT;
 863         spin_unlock_irq(shost->host_lock);
 864 
 865         /* If we are pt2pt with another NPort, force NPIV off! */
 866         phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
 867 
 868         /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
 869         if ((phba->sli_rev == LPFC_SLI_REV4) && phba->fc_topology_changed) {
 870                 lpfc_unregister_fcf_prep(phba);
 871 
 872                 spin_lock_irq(shost->host_lock);
 873                 vport->fc_flag &= ~FC_VFI_REGISTERED;
 874                 spin_unlock_irq(shost->host_lock);
 875                 phba->fc_topology_changed = 0;
 876         }
 877 
 878         rc = memcmp(&vport->fc_portname, &sp->portName,
 879                     sizeof(vport->fc_portname));
 880 
 881         if (rc >= 0) {
 882                 /* This side will initiate the PLOGI */
 883                 spin_lock_irq(shost->host_lock);
 884                 vport->fc_flag |= FC_PT2PT_PLOGI;
 885                 spin_unlock_irq(shost->host_lock);
 886 
 887                 /*
 888                  * N_Port ID cannot be 0, set our Id to LocalID
 889                  * the other side will be RemoteID.
 890                  */
 891 
 892                 /* not equal */
 893                 if (rc)
 894                         vport->fc_myDID = PT2PT_LocalID;
 895 
 896                 /* Decrement ndlp reference count indicating that ndlp can be
 897                  * safely released when other references to it are done.
 898                  */
 899                 lpfc_nlp_put(ndlp);
 900 
 901                 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
 902                 if (!ndlp) {
 903                         /*
 904                          * Cannot find existing Fabric ndlp, so allocate a
 905                          * new one
 906                          */
 907                         ndlp = lpfc_nlp_init(vport, PT2PT_RemoteID);
 908                         if (!ndlp)
 909                                 goto fail;
 910                 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
 911                         ndlp = lpfc_enable_node(vport, ndlp,
 912                                                 NLP_STE_UNUSED_NODE);
 913                         if(!ndlp)
 914                                 goto fail;
 915                 }
 916 
 917                 memcpy(&ndlp->nlp_portname, &sp->portName,
 918                        sizeof(struct lpfc_name));
 919                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
 920                        sizeof(struct lpfc_name));
 921                 /* Set state will put ndlp onto node list if not already done */
 922                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
 923                 spin_lock_irq(shost->host_lock);
 924                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
 925                 spin_unlock_irq(shost->host_lock);
 926 
 927                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 928                 if (!mbox)
 929                         goto fail;
 930 
 931                 lpfc_config_link(phba, mbox);
 932 
 933                 mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
 934                 mbox->vport = vport;
 935                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 936                 if (rc == MBX_NOT_FINISHED) {
 937                         mempool_free(mbox, phba->mbox_mem_pool);
 938                         goto fail;
 939                 }
 940         } else {
 941                 /* This side will wait for the PLOGI, decrement ndlp reference
 942                  * count indicating that ndlp can be released when other
 943                  * references to it are done.
 944                  */
 945                 lpfc_nlp_put(ndlp);
 946 
 947                 /* Start discovery - this should just do CLEAR_LA */
 948                 lpfc_disc_start(vport);
 949         }
 950 
 951         return 0;
 952 fail:
 953         return -ENXIO;
 954 }
 955 
 956 /**
 957  * lpfc_cmpl_els_flogi - Completion callback function for flogi
 958  * @phba: pointer to lpfc hba data structure.
 959  * @cmdiocb: pointer to lpfc command iocb data structure.
 960  * @rspiocb: pointer to lpfc response iocb data structure.
 961  *
 962  * This routine is the top-level completion callback function for issuing
 963  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
 964  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
 965  * retry has been made (either immediately or delayed with lpfc_els_retry()
 966  * returning 1), the command IOCB will be released and function returned.
 967  * If the retry attempt has been given up (possibly reach the maximum
 968  * number of retries), one additional decrement of ndlp reference shall be
 969  * invoked before going out after releasing the command IOCB. This will
 970  * actually release the remote node (Note, lpfc_els_free_iocb() will also
 971  * invoke one decrement of ndlp reference count). If no error reported in
 972  * the IOCB status, the command Port ID field is used to determine whether
 973  * this is a point-to-point topology or a fabric topology: if the Port ID
 974  * field is assigned, it is a fabric topology; otherwise, it is a
 975  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
 976  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
 977  * specific topology completion conditions.
 978  **/
 979 static void
 980 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 981                     struct lpfc_iocbq *rspiocb)
 982 {
 983         struct lpfc_vport *vport = cmdiocb->vport;
 984         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
 985         IOCB_t *irsp = &rspiocb->iocb;
 986         struct lpfc_nodelist *ndlp = cmdiocb->context1;
 987         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
 988         struct serv_parm *sp;
 989         uint16_t fcf_index;
 990         int rc;
 991 
 992         /* Check to see if link went down during discovery */
 993         if (lpfc_els_chk_latt(vport)) {
 994                 /* One additional decrement on node reference count to
 995                  * trigger the release of the node
 996                  */
 997                 lpfc_nlp_put(ndlp);
 998                 goto out;
 999         }
1000 
1001         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1002                 "FLOGI cmpl:      status:x%x/x%x state:x%x",
1003                 irsp->ulpStatus, irsp->un.ulpWord[4],
1004                 vport->port_state);
1005 
1006         if (irsp->ulpStatus) {
1007                 /*
1008                  * In case of FIP mode, perform roundrobin FCF failover
1009                  * due to new FCF discovery
1010                  */
1011                 if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
1012                     (phba->fcf.fcf_flag & FCF_DISCOVERY)) {
1013                         if (phba->link_state < LPFC_LINK_UP)
1014                                 goto stop_rr_fcf_flogi;
1015                         if ((phba->fcoe_cvl_eventtag_attn ==
1016                              phba->fcoe_cvl_eventtag) &&
1017                             (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1018                             ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1019                             IOERR_SLI_ABORTED))
1020                                 goto stop_rr_fcf_flogi;
1021                         else
1022                                 phba->fcoe_cvl_eventtag_attn =
1023                                         phba->fcoe_cvl_eventtag;
1024                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1025                                         "2611 FLOGI failed on FCF (x%x), "
1026                                         "status:x%x/x%x, tmo:x%x, perform "
1027                                         "roundrobin FCF failover\n",
1028                                         phba->fcf.current_rec.fcf_indx,
1029                                         irsp->ulpStatus, irsp->un.ulpWord[4],
1030                                         irsp->ulpTimeout);
1031                         lpfc_sli4_set_fcf_flogi_fail(phba,
1032                                         phba->fcf.current_rec.fcf_indx);
1033                         fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
1034                         rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
1035                         if (rc)
1036                                 goto out;
1037                 }
1038 
1039 stop_rr_fcf_flogi:
1040                 /* FLOGI failure */
1041                 if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1042                       ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1043                                         IOERR_LOOP_OPEN_FAILURE)))
1044                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1045                                         "2858 FLOGI failure Status:x%x/x%x "
1046                                         "TMO:x%x Data x%x x%x\n",
1047                                         irsp->ulpStatus, irsp->un.ulpWord[4],
1048                                         irsp->ulpTimeout, phba->hba_flag,
1049                                         phba->fcf.fcf_flag);
1050 
1051                 /* Check for retry */
1052                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1053                         goto out;
1054 
1055                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
1056                                  "0150 FLOGI failure Status:x%x/x%x "
1057                                  "xri x%x TMO:x%x\n",
1058                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1059                                  cmdiocb->sli4_xritag, irsp->ulpTimeout);
1060 
1061                 /* If this is not a loop open failure, bail out */
1062                 if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1063                       ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1064                                         IOERR_LOOP_OPEN_FAILURE)))
1065                         goto flogifail;
1066 
1067                 /* FLOGI failed, so there is no fabric */
1068                 spin_lock_irq(shost->host_lock);
1069                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1070                 spin_unlock_irq(shost->host_lock);
1071 
1072                 /* If private loop, then allow max outstanding els to be
1073                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
1074                  * alpa map would take too long otherwise.
1075                  */
1076                 if (phba->alpa_map[0] == 0)
1077                         vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
1078                 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1079                     (!(vport->fc_flag & FC_VFI_REGISTERED) ||
1080                      (vport->fc_prevDID != vport->fc_myDID) ||
1081                         phba->fc_topology_changed)) {
1082                         if (vport->fc_flag & FC_VFI_REGISTERED) {
1083                                 if (phba->fc_topology_changed) {
1084                                         lpfc_unregister_fcf_prep(phba);
1085                                         spin_lock_irq(shost->host_lock);
1086                                         vport->fc_flag &= ~FC_VFI_REGISTERED;
1087                                         spin_unlock_irq(shost->host_lock);
1088                                         phba->fc_topology_changed = 0;
1089                                 } else {
1090                                         lpfc_sli4_unreg_all_rpis(vport);
1091                                 }
1092                         }
1093 
1094                         /* Do not register VFI if the driver aborted FLOGI */
1095                         if (!lpfc_error_lost_link(irsp))
1096                                 lpfc_issue_reg_vfi(vport);
1097                         lpfc_nlp_put(ndlp);
1098                         goto out;
1099                 }
1100                 goto flogifail;
1101         }
1102         spin_lock_irq(shost->host_lock);
1103         vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
1104         vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
1105         spin_unlock_irq(shost->host_lock);
1106 
1107         /*
1108          * The FLogI succeeded.  Sync the data for the CPU before
1109          * accessing it.
1110          */
1111         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1112         if (!prsp)
1113                 goto out;
1114         sp = prsp->virt + sizeof(uint32_t);
1115 
1116         /* FLOGI completes successfully */
1117         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1118                          "0101 FLOGI completes successfully, I/O tag:x%x, "
1119                          "xri x%x Data: x%x x%x x%x x%x x%x %x\n",
1120                          cmdiocb->iotag, cmdiocb->sli4_xritag,
1121                          irsp->un.ulpWord[4], sp->cmn.e_d_tov,
1122                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
1123                          vport->port_state, vport->fc_flag);
1124 
1125         if (vport->port_state == LPFC_FLOGI) {
1126                 /*
1127                  * If Common Service Parameters indicate Nport
1128                  * we are point to point, if Fport we are Fabric.
1129                  */
1130                 if (sp->cmn.fPort)
1131                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
1132                 else if (!(phba->hba_flag & HBA_FCOE_MODE))
1133                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
1134                 else {
1135                         lpfc_printf_vlog(vport, KERN_ERR,
1136                                 LOG_FIP | LOG_ELS,
1137                                 "2831 FLOGI response with cleared Fabric "
1138                                 "bit fcf_index 0x%x "
1139                                 "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
1140                                 "Fabric Name "
1141                                 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
1142                                 phba->fcf.current_rec.fcf_indx,
1143                                 phba->fcf.current_rec.switch_name[0],
1144                                 phba->fcf.current_rec.switch_name[1],
1145                                 phba->fcf.current_rec.switch_name[2],
1146                                 phba->fcf.current_rec.switch_name[3],
1147                                 phba->fcf.current_rec.switch_name[4],
1148                                 phba->fcf.current_rec.switch_name[5],
1149                                 phba->fcf.current_rec.switch_name[6],
1150                                 phba->fcf.current_rec.switch_name[7],
1151                                 phba->fcf.current_rec.fabric_name[0],
1152                                 phba->fcf.current_rec.fabric_name[1],
1153                                 phba->fcf.current_rec.fabric_name[2],
1154                                 phba->fcf.current_rec.fabric_name[3],
1155                                 phba->fcf.current_rec.fabric_name[4],
1156                                 phba->fcf.current_rec.fabric_name[5],
1157                                 phba->fcf.current_rec.fabric_name[6],
1158                                 phba->fcf.current_rec.fabric_name[7]);
1159                         lpfc_nlp_put(ndlp);
1160                         spin_lock_irq(&phba->hbalock);
1161                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1162                         phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1163                         spin_unlock_irq(&phba->hbalock);
1164                         phba->fcf.fcf_redisc_attempted = 0; /* reset */
1165                         goto out;
1166                 }
1167                 if (!rc) {
1168                         /* Mark the FCF discovery process done */
1169                         if (phba->hba_flag & HBA_FIP_SUPPORT)
1170                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
1171                                                 LOG_ELS,
1172                                                 "2769 FLOGI to FCF (x%x) "
1173                                                 "completed successfully\n",
1174                                                 phba->fcf.current_rec.fcf_indx);
1175                         spin_lock_irq(&phba->hbalock);
1176                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1177                         phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1178                         spin_unlock_irq(&phba->hbalock);
1179                         phba->fcf.fcf_redisc_attempted = 0; /* reset */
1180                         goto out;
1181                 }
1182         }
1183 
1184 flogifail:
1185         spin_lock_irq(&phba->hbalock);
1186         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1187         spin_unlock_irq(&phba->hbalock);
1188 
1189         lpfc_nlp_put(ndlp);
1190 
1191         if (!lpfc_error_lost_link(irsp)) {
1192                 /* FLOGI failed, so just use loop map to make discovery list */
1193                 lpfc_disc_list_loopmap(vport);
1194 
1195                 /* Start discovery */
1196                 lpfc_disc_start(vport);
1197         } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
1198                         (((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1199                          IOERR_SLI_ABORTED) &&
1200                         ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1201                          IOERR_SLI_DOWN))) &&
1202                         (phba->link_state != LPFC_CLEAR_LA)) {
1203                 /* If FLOGI failed enable link interrupt. */
1204                 lpfc_issue_clear_la(phba, vport);
1205         }
1206 out:
1207         lpfc_els_free_iocb(phba, cmdiocb);
1208 }
1209 
1210 /**
1211  * lpfc_cmpl_els_link_down - Completion callback function for ELS command
1212  *                           aborted during a link down
1213  * @phba: pointer to lpfc hba data structure.
1214  * @cmdiocb: pointer to lpfc command iocb data structure.
1215  * @rspiocb: pointer to lpfc response iocb data structure.
1216  *
1217  */
1218 static void
1219 lpfc_cmpl_els_link_down(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1220                         struct lpfc_iocbq *rspiocb)
1221 {
1222         IOCB_t *irsp;
1223         uint32_t *pcmd;
1224         uint32_t cmd;
1225 
1226         pcmd = (uint32_t *)(((struct lpfc_dmabuf *)cmdiocb->context2)->virt);
1227         cmd = *pcmd;
1228         irsp = &rspiocb->iocb;
1229 
1230         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1231                         "6445 ELS completes after LINK_DOWN: "
1232                         " Status %x/%x cmd x%x flg x%x\n",
1233                         irsp->ulpStatus, irsp->un.ulpWord[4], cmd,
1234                         cmdiocb->iocb_flag);
1235 
1236         if (cmdiocb->iocb_flag & LPFC_IO_FABRIC) {
1237                 cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
1238                 atomic_dec(&phba->fabric_iocb_count);
1239         }
1240         lpfc_els_free_iocb(phba, cmdiocb);
1241 }
1242 
1243 /**
1244  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
1245  * @vport: pointer to a host virtual N_Port data structure.
1246  * @ndlp: pointer to a node-list data structure.
1247  * @retry: number of retries to the command IOCB.
1248  *
1249  * This routine issues a Fabric Login (FLOGI) Request ELS command
1250  * for a @vport. The initiator service parameters are put into the payload
1251  * of the FLOGI Request IOCB and the top-level callback function pointer
1252  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1253  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1254  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1255  *
1256  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1257  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1258  * will be stored into the context1 field of the IOCB for the completion
1259  * callback function to the FLOGI ELS command.
1260  *
1261  * Return code
1262  *   0 - successfully issued flogi iocb for @vport
1263  *   1 - failed to issue flogi iocb for @vport
1264  **/
1265 static int
1266 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1267                      uint8_t retry)
1268 {
1269         struct lpfc_hba  *phba = vport->phba;
1270         struct serv_parm *sp;
1271         IOCB_t *icmd;
1272         struct lpfc_iocbq *elsiocb;
1273         struct lpfc_iocbq defer_flogi_acc;
1274         uint8_t *pcmd;
1275         uint16_t cmdsize;
1276         uint32_t tmo, did;
1277         int rc;
1278 
1279         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1280         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1281                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
1282 
1283         if (!elsiocb)
1284                 return 1;
1285 
1286         icmd = &elsiocb->iocb;
1287         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1288 
1289         /* For FLOGI request, remainder of payload is service parameters */
1290         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1291         pcmd += sizeof(uint32_t);
1292         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1293         sp = (struct serv_parm *) pcmd;
1294 
1295         /* Setup CSPs accordingly for Fabric */
1296         sp->cmn.e_d_tov = 0;
1297         sp->cmn.w2.r_a_tov = 0;
1298         sp->cmn.virtual_fabric_support = 0;
1299         sp->cls1.classValid = 0;
1300         if (sp->cmn.fcphLow < FC_PH3)
1301                 sp->cmn.fcphLow = FC_PH3;
1302         if (sp->cmn.fcphHigh < FC_PH3)
1303                 sp->cmn.fcphHigh = FC_PH3;
1304 
1305         if  (phba->sli_rev == LPFC_SLI_REV4) {
1306                 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1307                     LPFC_SLI_INTF_IF_TYPE_0) {
1308                         elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1309                         elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1310                         /* FLOGI needs to be 3 for WQE FCFI */
1311                         /* Set the fcfi to the fcfi we registered with */
1312                         elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1313                 }
1314                 /* Can't do SLI4 class2 without support sequence coalescing */
1315                 sp->cls2.classValid = 0;
1316                 sp->cls2.seqDelivery = 0;
1317         } else {
1318                 /* Historical, setting sequential-delivery bit for SLI3 */
1319                 sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
1320                 sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
1321                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1322                         sp->cmn.request_multiple_Nport = 1;
1323                         /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1324                         icmd->ulpCt_h = 1;
1325                         icmd->ulpCt_l = 0;
1326                 } else
1327                         sp->cmn.request_multiple_Nport = 0;
1328         }
1329 
1330         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
1331                 icmd->un.elsreq64.myID = 0;
1332                 icmd->un.elsreq64.fl = 1;
1333         }
1334 
1335         tmo = phba->fc_ratov;
1336         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1337         lpfc_set_disctmo(vport);
1338         phba->fc_ratov = tmo;
1339 
1340         phba->fc_stat.elsXmitFLOGI++;
1341         elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
1342 
1343         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1344                 "Issue FLOGI:     opt:x%x",
1345                 phba->sli3_options, 0, 0);
1346 
1347         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1348 
1349         phba->hba_flag |= HBA_FLOGI_ISSUED;
1350 
1351         /* Check for a deferred FLOGI ACC condition */
1352         if (phba->defer_flogi_acc_flag) {
1353                 did = vport->fc_myDID;
1354                 vport->fc_myDID = Fabric_DID;
1355 
1356                 memset(&defer_flogi_acc, 0, sizeof(struct lpfc_iocbq));
1357 
1358                 defer_flogi_acc.iocb.ulpContext = phba->defer_flogi_acc_rx_id;
1359                 defer_flogi_acc.iocb.unsli3.rcvsli3.ox_id =
1360                                                 phba->defer_flogi_acc_ox_id;
1361 
1362                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1363                                  "3354 Xmit deferred FLOGI ACC: rx_id: x%x,"
1364                                  " ox_id: x%x, hba_flag x%x\n",
1365                                  phba->defer_flogi_acc_rx_id,
1366                                  phba->defer_flogi_acc_ox_id, phba->hba_flag);
1367 
1368                 /* Send deferred FLOGI ACC */
1369                 lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, &defer_flogi_acc,
1370                                  ndlp, NULL);
1371 
1372                 phba->defer_flogi_acc_flag = false;
1373 
1374                 vport->fc_myDID = did;
1375         }
1376 
1377         if (rc == IOCB_ERROR) {
1378                 lpfc_els_free_iocb(phba, elsiocb);
1379                 return 1;
1380         }
1381         return 0;
1382 }
1383 
1384 /**
1385  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1386  * @phba: pointer to lpfc hba data structure.
1387  *
1388  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1389  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1390  * list and issues an abort IOCB commond on each outstanding IOCB that
1391  * contains a active Fabric_DID ndlp. Note that this function is to issue
1392  * the abort IOCB command on all the outstanding IOCBs, thus when this
1393  * function returns, it does not guarantee all the IOCBs are actually aborted.
1394  *
1395  * Return code
1396  *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1397  **/
1398 int
1399 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1400 {
1401         struct lpfc_sli_ring *pring;
1402         struct lpfc_iocbq *iocb, *next_iocb;
1403         struct lpfc_nodelist *ndlp;
1404         IOCB_t *icmd;
1405 
1406         /* Abort outstanding I/O on NPort <nlp_DID> */
1407         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1408                         "0201 Abort outstanding I/O on NPort x%x\n",
1409                         Fabric_DID);
1410 
1411         pring = lpfc_phba_elsring(phba);
1412         if (unlikely(!pring))
1413                 return -EIO;
1414 
1415         /*
1416          * Check the txcmplq for an iocb that matches the nport the driver is
1417          * searching for.
1418          */
1419         spin_lock_irq(&phba->hbalock);
1420         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1421                 icmd = &iocb->iocb;
1422                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
1423                         ndlp = (struct lpfc_nodelist *)(iocb->context1);
1424                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1425                             (ndlp->nlp_DID == Fabric_DID))
1426                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1427                 }
1428         }
1429         spin_unlock_irq(&phba->hbalock);
1430 
1431         return 0;
1432 }
1433 
1434 /**
1435  * lpfc_initial_flogi - Issue an initial fabric login for a vport
1436  * @vport: pointer to a host virtual N_Port data structure.
1437  *
1438  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1439  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1440  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1441  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1442  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1443  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1444  * @vport.
1445  *
1446  * Return code
1447  *   0 - failed to issue initial flogi for @vport
1448  *   1 - successfully issued initial flogi for @vport
1449  **/
1450 int
1451 lpfc_initial_flogi(struct lpfc_vport *vport)
1452 {
1453         struct lpfc_nodelist *ndlp;
1454 
1455         vport->port_state = LPFC_FLOGI;
1456         lpfc_set_disctmo(vport);
1457 
1458         /* First look for the Fabric ndlp */
1459         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1460         if (!ndlp) {
1461                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1462                 ndlp = lpfc_nlp_init(vport, Fabric_DID);
1463                 if (!ndlp)
1464                         return 0;
1465                 /* Set the node type */
1466                 ndlp->nlp_type |= NLP_FABRIC;
1467                 /* Put ndlp onto node list */
1468                 lpfc_enqueue_node(vport, ndlp);
1469         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1470                 /* re-setup ndlp without removing from node list */
1471                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1472                 if (!ndlp)
1473                         return 0;
1474         }
1475 
1476         if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1477                 /* This decrement of reference count to node shall kick off
1478                  * the release of the node.
1479                  */
1480                 lpfc_nlp_put(ndlp);
1481                 return 0;
1482         }
1483         return 1;
1484 }
1485 
1486 /**
1487  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1488  * @vport: pointer to a host virtual N_Port data structure.
1489  *
1490  * This routine issues an initial Fabric Discover (FDISC) 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_fdisc() routine
1495  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1496  * @vport.
1497  *
1498  * Return code
1499  *   0 - failed to issue initial fdisc for @vport
1500  *   1 - successfully issued initial fdisc for @vport
1501  **/
1502 int
1503 lpfc_initial_fdisc(struct lpfc_vport *vport)
1504 {
1505         struct lpfc_nodelist *ndlp;
1506 
1507         /* First look for the Fabric ndlp */
1508         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1509         if (!ndlp) {
1510                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1511                 ndlp = lpfc_nlp_init(vport, Fabric_DID);
1512                 if (!ndlp)
1513                         return 0;
1514                 /* Put ndlp onto node list */
1515                 lpfc_enqueue_node(vport, ndlp);
1516         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1517                 /* re-setup ndlp without removing from node list */
1518                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1519                 if (!ndlp)
1520                         return 0;
1521         }
1522 
1523         if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1524                 /* decrement node reference count to trigger the release of
1525                  * the node.
1526                  */
1527                 lpfc_nlp_put(ndlp);
1528                 return 0;
1529         }
1530         return 1;
1531 }
1532 
1533 /**
1534  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1535  * @vport: pointer to a host virtual N_Port data structure.
1536  *
1537  * This routine checks whether there are more remaining Port Logins
1538  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1539  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1540  * to issue ELS PLOGIs up to the configured discover threads with the
1541  * @vport (@vport->cfg_discovery_threads). The function also decrement
1542  * the @vport's num_disc_node by 1 if it is not already 0.
1543  **/
1544 void
1545 lpfc_more_plogi(struct lpfc_vport *vport)
1546 {
1547         if (vport->num_disc_nodes)
1548                 vport->num_disc_nodes--;
1549 
1550         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1551         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1552                          "0232 Continue discovery with %d PLOGIs to go "
1553                          "Data: x%x x%x x%x\n",
1554                          vport->num_disc_nodes, vport->fc_plogi_cnt,
1555                          vport->fc_flag, vport->port_state);
1556         /* Check to see if there are more PLOGIs to be sent */
1557         if (vport->fc_flag & FC_NLP_MORE)
1558                 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1559                 lpfc_els_disc_plogi(vport);
1560 
1561         return;
1562 }
1563 
1564 /**
1565  * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
1566  * @phba: pointer to lpfc hba data structure.
1567  * @prsp: pointer to response IOCB payload.
1568  * @ndlp: pointer to a node-list data structure.
1569  *
1570  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1571  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1572  * The following cases are considered N_Port confirmed:
1573  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1574  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1575  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1576  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1577  * 1) if there is a node on vport list other than the @ndlp with the same
1578  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1579  * on that node to release the RPI associated with the node; 2) if there is
1580  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1581  * into, a new node shall be allocated (or activated). In either case, the
1582  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1583  * be released and the new_ndlp shall be put on to the vport node list and
1584  * its pointer returned as the confirmed node.
1585  *
1586  * Note that before the @ndlp got "released", the keepDID from not-matching
1587  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1588  * of the @ndlp. This is because the release of @ndlp is actually to put it
1589  * into an inactive state on the vport node list and the vport node list
1590  * management algorithm does not allow two node with a same DID.
1591  *
1592  * Return code
1593  *   pointer to the PLOGI N_Port @ndlp
1594  **/
1595 static struct lpfc_nodelist *
1596 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1597                          struct lpfc_nodelist *ndlp)
1598 {
1599         struct lpfc_vport *vport = ndlp->vport;
1600         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1601         struct lpfc_nodelist *new_ndlp;
1602         struct lpfc_rport_data *rdata;
1603         struct fc_rport *rport;
1604         struct serv_parm *sp;
1605         uint8_t  name[sizeof(struct lpfc_name)];
1606         uint32_t rc, keepDID = 0, keep_nlp_flag = 0;
1607         uint32_t keep_new_nlp_flag = 0;
1608         uint16_t keep_nlp_state;
1609         u32 keep_nlp_fc4_type = 0;
1610         struct lpfc_nvme_rport *keep_nrport = NULL;
1611         int  put_node;
1612         int  put_rport;
1613         unsigned long *active_rrqs_xri_bitmap = NULL;
1614 
1615         /* Fabric nodes can have the same WWPN so we don't bother searching
1616          * by WWPN.  Just return the ndlp that was given to us.
1617          */
1618         if (ndlp->nlp_type & NLP_FABRIC)
1619                 return ndlp;
1620 
1621         sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1622         memset(name, 0, sizeof(struct lpfc_name));
1623 
1624         /* Now we find out if the NPort we are logging into, matches the WWPN
1625          * we have for that ndlp. If not, we have some work to do.
1626          */
1627         new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1628 
1629         /* return immediately if the WWPN matches ndlp */
1630         if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1631                 return ndlp;
1632 
1633         if (phba->sli_rev == LPFC_SLI_REV4) {
1634                 active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
1635                                                        GFP_KERNEL);
1636                 if (active_rrqs_xri_bitmap)
1637                         memset(active_rrqs_xri_bitmap, 0,
1638                                phba->cfg_rrq_xri_bitmap_sz);
1639         }
1640 
1641         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1642                          "3178 PLOGI confirm: ndlp x%x x%x x%x: "
1643                          "new_ndlp x%x x%x x%x\n",
1644                          ndlp->nlp_DID, ndlp->nlp_flag,  ndlp->nlp_fc4_type,
1645                          (new_ndlp ? new_ndlp->nlp_DID : 0),
1646                          (new_ndlp ? new_ndlp->nlp_flag : 0),
1647                          (new_ndlp ? new_ndlp->nlp_fc4_type : 0));
1648 
1649         if (!new_ndlp) {
1650                 rc = memcmp(&ndlp->nlp_portname, name,
1651                             sizeof(struct lpfc_name));
1652                 if (!rc) {
1653                         if (active_rrqs_xri_bitmap)
1654                                 mempool_free(active_rrqs_xri_bitmap,
1655                                              phba->active_rrq_pool);
1656                         return ndlp;
1657                 }
1658                 new_ndlp = lpfc_nlp_init(vport, ndlp->nlp_DID);
1659                 if (!new_ndlp) {
1660                         if (active_rrqs_xri_bitmap)
1661                                 mempool_free(active_rrqs_xri_bitmap,
1662                                              phba->active_rrq_pool);
1663                         return ndlp;
1664                 }
1665         } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1666                 rc = memcmp(&ndlp->nlp_portname, name,
1667                             sizeof(struct lpfc_name));
1668                 if (!rc) {
1669                         if (active_rrqs_xri_bitmap)
1670                                 mempool_free(active_rrqs_xri_bitmap,
1671                                              phba->active_rrq_pool);
1672                         return ndlp;
1673                 }
1674                 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1675                                                 NLP_STE_UNUSED_NODE);
1676                 if (!new_ndlp) {
1677                         if (active_rrqs_xri_bitmap)
1678                                 mempool_free(active_rrqs_xri_bitmap,
1679                                              phba->active_rrq_pool);
1680                         return ndlp;
1681                 }
1682                 keepDID = new_ndlp->nlp_DID;
1683                 if ((phba->sli_rev == LPFC_SLI_REV4) && active_rrqs_xri_bitmap)
1684                         memcpy(active_rrqs_xri_bitmap,
1685                                new_ndlp->active_rrqs_xri_bitmap,
1686                                phba->cfg_rrq_xri_bitmap_sz);
1687         } else {
1688                 keepDID = new_ndlp->nlp_DID;
1689                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1690                     active_rrqs_xri_bitmap)
1691                         memcpy(active_rrqs_xri_bitmap,
1692                                new_ndlp->active_rrqs_xri_bitmap,
1693                                phba->cfg_rrq_xri_bitmap_sz);
1694         }
1695 
1696         /* At this point in this routine, we know new_ndlp will be
1697          * returned. however, any previous GID_FTs that were done
1698          * would have updated nlp_fc4_type in ndlp, so we must ensure
1699          * new_ndlp has the right value.
1700          */
1701         if (vport->fc_flag & FC_FABRIC) {
1702                 keep_nlp_fc4_type = new_ndlp->nlp_fc4_type;
1703                 new_ndlp->nlp_fc4_type = ndlp->nlp_fc4_type;
1704         }
1705 
1706         lpfc_unreg_rpi(vport, new_ndlp);
1707         new_ndlp->nlp_DID = ndlp->nlp_DID;
1708         new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1709         if (phba->sli_rev == LPFC_SLI_REV4)
1710                 memcpy(new_ndlp->active_rrqs_xri_bitmap,
1711                        ndlp->active_rrqs_xri_bitmap,
1712                        phba->cfg_rrq_xri_bitmap_sz);
1713 
1714         spin_lock_irq(shost->host_lock);
1715         keep_new_nlp_flag = new_ndlp->nlp_flag;
1716         keep_nlp_flag = ndlp->nlp_flag;
1717         new_ndlp->nlp_flag = ndlp->nlp_flag;
1718 
1719         /* if new_ndlp had NLP_UNREG_INP set, keep it */
1720         if (keep_new_nlp_flag & NLP_UNREG_INP)
1721                 new_ndlp->nlp_flag |= NLP_UNREG_INP;
1722         else
1723                 new_ndlp->nlp_flag &= ~NLP_UNREG_INP;
1724 
1725         /* if new_ndlp had NLP_RPI_REGISTERED set, keep it */
1726         if (keep_new_nlp_flag & NLP_RPI_REGISTERED)
1727                 new_ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1728         else
1729                 new_ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1730 
1731         ndlp->nlp_flag = keep_new_nlp_flag;
1732 
1733         /* if ndlp had NLP_UNREG_INP set, keep it */
1734         if (keep_nlp_flag & NLP_UNREG_INP)
1735                 ndlp->nlp_flag |= NLP_UNREG_INP;
1736         else
1737                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
1738 
1739         /* if ndlp had NLP_RPI_REGISTERED set, keep it */
1740         if (keep_nlp_flag & NLP_RPI_REGISTERED)
1741                 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1742         else
1743                 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1744 
1745         spin_unlock_irq(shost->host_lock);
1746 
1747         /* Set nlp_states accordingly */
1748         keep_nlp_state = new_ndlp->nlp_state;
1749         lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1750 
1751         /* interchange the nvme remoteport structs */
1752         keep_nrport = new_ndlp->nrport;
1753         new_ndlp->nrport = ndlp->nrport;
1754 
1755         /* Move this back to NPR state */
1756         if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1757                 /* The new_ndlp is replacing ndlp totally, so we need
1758                  * to put ndlp on UNUSED list and try to free it.
1759                  */
1760                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1761                          "3179 PLOGI confirm NEW: %x %x\n",
1762                          new_ndlp->nlp_DID, keepDID);
1763 
1764                 /* Fix up the rport accordingly */
1765                 rport =  ndlp->rport;
1766                 if (rport) {
1767                         rdata = rport->dd_data;
1768                         if (rdata->pnode == ndlp) {
1769                                 /* break the link before dropping the ref */
1770                                 ndlp->rport = NULL;
1771                                 lpfc_nlp_put(ndlp);
1772                                 rdata->pnode = lpfc_nlp_get(new_ndlp);
1773                                 new_ndlp->rport = rport;
1774                         }
1775                         new_ndlp->nlp_type = ndlp->nlp_type;
1776                 }
1777 
1778                 /* Fix up the nvme rport */
1779                 if (ndlp->nrport) {
1780                         ndlp->nrport = NULL;
1781                         lpfc_nlp_put(ndlp);
1782                 }
1783 
1784                 /* We shall actually free the ndlp with both nlp_DID and
1785                  * nlp_portname fields equals 0 to avoid any ndlp on the
1786                  * nodelist never to be used.
1787                  */
1788                 if (ndlp->nlp_DID == 0) {
1789                         spin_lock_irq(&phba->ndlp_lock);
1790                         NLP_SET_FREE_REQ(ndlp);
1791                         spin_unlock_irq(&phba->ndlp_lock);
1792                 }
1793 
1794                 /* Two ndlps cannot have the same did on the nodelist.
1795                  * Note: for this case, ndlp has a NULL WWPN so setting
1796                  * the nlp_fc4_type isn't required.
1797                  */
1798                 ndlp->nlp_DID = keepDID;
1799                 lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1800                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1801                     active_rrqs_xri_bitmap)
1802                         memcpy(ndlp->active_rrqs_xri_bitmap,
1803                                active_rrqs_xri_bitmap,
1804                                phba->cfg_rrq_xri_bitmap_sz);
1805 
1806                 if (!NLP_CHK_NODE_ACT(ndlp))
1807                         lpfc_drop_node(vport, ndlp);
1808         }
1809         else {
1810                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1811                          "3180 PLOGI confirm SWAP: %x %x\n",
1812                          new_ndlp->nlp_DID, keepDID);
1813 
1814                 lpfc_unreg_rpi(vport, ndlp);
1815 
1816                 /* Two ndlps cannot have the same did and the fc4
1817                  * type must be transferred because the ndlp is in
1818                  * flight.
1819                  */
1820                 ndlp->nlp_DID = keepDID;
1821                 ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1822 
1823                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1824                     active_rrqs_xri_bitmap)
1825                         memcpy(ndlp->active_rrqs_xri_bitmap,
1826                                active_rrqs_xri_bitmap,
1827                                phba->cfg_rrq_xri_bitmap_sz);
1828 
1829                 /* Since we are switching over to the new_ndlp,
1830                  * reset the old ndlp state
1831                  */
1832                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
1833                     (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
1834                         keep_nlp_state = NLP_STE_NPR_NODE;
1835                 lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1836 
1837                 /* Previous ndlp no longer active with nvme host transport.
1838                  * Remove reference from earlier registration unless the
1839                  * nvme host took care of it.
1840                  */
1841                 if (ndlp->nrport)
1842                         lpfc_nlp_put(ndlp);
1843                 ndlp->nrport = keep_nrport;
1844 
1845                 /* Fix up the rport accordingly */
1846                 rport = ndlp->rport;
1847                 if (rport) {
1848                         rdata = rport->dd_data;
1849                         put_node = rdata->pnode != NULL;
1850                         put_rport = ndlp->rport != NULL;
1851                         rdata->pnode = NULL;
1852                         ndlp->rport = NULL;
1853                         if (put_node)
1854                                 lpfc_nlp_put(ndlp);
1855                         if (put_rport)
1856                                 put_device(&rport->dev);
1857                 }
1858         }
1859         if (phba->sli_rev == LPFC_SLI_REV4 &&
1860             active_rrqs_xri_bitmap)
1861                 mempool_free(active_rrqs_xri_bitmap,
1862                              phba->active_rrq_pool);
1863 
1864         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1865                          "3173 PLOGI confirm exit: new_ndlp x%x x%x x%x\n",
1866                          new_ndlp->nlp_DID, new_ndlp->nlp_flag,
1867                          new_ndlp->nlp_fc4_type);
1868 
1869         return new_ndlp;
1870 }
1871 
1872 /**
1873  * lpfc_end_rscn - Check and handle more rscn for a vport
1874  * @vport: pointer to a host virtual N_Port data structure.
1875  *
1876  * This routine checks whether more Registration State Change
1877  * Notifications (RSCNs) came in while the discovery state machine was in
1878  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1879  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1880  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1881  * handling the RSCNs.
1882  **/
1883 void
1884 lpfc_end_rscn(struct lpfc_vport *vport)
1885 {
1886         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1887 
1888         if (vport->fc_flag & FC_RSCN_MODE) {
1889                 /*
1890                  * Check to see if more RSCNs came in while we were
1891                  * processing this one.
1892                  */
1893                 if (vport->fc_rscn_id_cnt ||
1894                     (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1895                         lpfc_els_handle_rscn(vport);
1896                 else {
1897                         spin_lock_irq(shost->host_lock);
1898                         vport->fc_flag &= ~FC_RSCN_MODE;
1899                         spin_unlock_irq(shost->host_lock);
1900                 }
1901         }
1902 }
1903 
1904 /**
1905  * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1906  * @phba: pointer to lpfc hba data structure.
1907  * @cmdiocb: pointer to lpfc command iocb data structure.
1908  * @rspiocb: pointer to lpfc response iocb data structure.
1909  *
1910  * This routine will call the clear rrq function to free the rrq and
1911  * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1912  * exist then the clear_rrq is still called because the rrq needs to
1913  * be freed.
1914  **/
1915 
1916 static void
1917 lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1918                     struct lpfc_iocbq *rspiocb)
1919 {
1920         struct lpfc_vport *vport = cmdiocb->vport;
1921         IOCB_t *irsp;
1922         struct lpfc_nodelist *ndlp;
1923         struct lpfc_node_rrq *rrq;
1924 
1925         /* we pass cmdiocb to state machine which needs rspiocb as well */
1926         rrq = cmdiocb->context_un.rrq;
1927         cmdiocb->context_un.rsp_iocb = rspiocb;
1928 
1929         irsp = &rspiocb->iocb;
1930         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1931                 "RRQ cmpl:      status:x%x/x%x did:x%x",
1932                 irsp->ulpStatus, irsp->un.ulpWord[4],
1933                 irsp->un.elsreq64.remoteID);
1934 
1935         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1936         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || ndlp != rrq->ndlp) {
1937                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1938                                  "2882 RRQ completes to NPort x%x "
1939                                  "with no ndlp. Data: x%x x%x x%x\n",
1940                                  irsp->un.elsreq64.remoteID,
1941                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1942                                  irsp->ulpIoTag);
1943                 goto out;
1944         }
1945 
1946         /* rrq completes to NPort <nlp_DID> */
1947         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1948                          "2880 RRQ completes to NPort x%x "
1949                          "Data: x%x x%x x%x x%x x%x\n",
1950                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1951                          irsp->ulpTimeout, rrq->xritag, rrq->rxid);
1952 
1953         if (irsp->ulpStatus) {
1954                 /* Check for retry */
1955                 /* RRQ failed Don't print the vport to vport rjts */
1956                 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1957                         (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1958                         ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1959                         (phba)->pport->cfg_log_verbose & LOG_ELS)
1960                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1961                                  "2881 RRQ failure DID:%06X Status:x%x/x%x\n",
1962                                  ndlp->nlp_DID, irsp->ulpStatus,
1963                                  irsp->un.ulpWord[4]);
1964         }
1965 out:
1966         if (rrq)
1967                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1968         lpfc_els_free_iocb(phba, cmdiocb);
1969         return;
1970 }
1971 /**
1972  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1973  * @phba: pointer to lpfc hba data structure.
1974  * @cmdiocb: pointer to lpfc command iocb data structure.
1975  * @rspiocb: pointer to lpfc response iocb data structure.
1976  *
1977  * This routine is the completion callback function for issuing the Port
1978  * Login (PLOGI) command. For PLOGI completion, there must be an active
1979  * ndlp on the vport node list that matches the remote node ID from the
1980  * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
1981  * ignored and command IOCB released. The PLOGI response IOCB status is
1982  * checked for error conditons. If there is error status reported, PLOGI
1983  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1984  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1985  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1986  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1987  * there are additional N_Port nodes with the vport that need to perform
1988  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1989  * PLOGIs.
1990  **/
1991 static void
1992 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1993                     struct lpfc_iocbq *rspiocb)
1994 {
1995         struct lpfc_vport *vport = cmdiocb->vport;
1996         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1997         IOCB_t *irsp;
1998         struct lpfc_nodelist *ndlp;
1999         struct lpfc_dmabuf *prsp;
2000         int disc;
2001 
2002         /* we pass cmdiocb to state machine which needs rspiocb as well */
2003         cmdiocb->context_un.rsp_iocb = rspiocb;
2004 
2005         irsp = &rspiocb->iocb;
2006         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2007                 "PLOGI cmpl:      status:x%x/x%x did:x%x",
2008                 irsp->ulpStatus, irsp->un.ulpWord[4],
2009                 irsp->un.elsreq64.remoteID);
2010 
2011         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
2012         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2013                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2014                                  "0136 PLOGI completes to NPort x%x "
2015                                  "with no ndlp. Data: x%x x%x x%x\n",
2016                                  irsp->un.elsreq64.remoteID,
2017                                  irsp->ulpStatus, irsp->un.ulpWord[4],
2018                                  irsp->ulpIoTag);
2019                 goto out;
2020         }
2021 
2022         /* Since ndlp can be freed in the disc state machine, note if this node
2023          * is being used during discovery.
2024          */
2025         spin_lock_irq(shost->host_lock);
2026         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2027         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2028         spin_unlock_irq(shost->host_lock);
2029 
2030         /* PLOGI completes to NPort <nlp_DID> */
2031         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2032                          "0102 PLOGI completes to NPort x%06x "
2033                          "Data: x%x x%x x%x x%x x%x\n",
2034                          ndlp->nlp_DID, ndlp->nlp_fc4_type,
2035                          irsp->ulpStatus, irsp->un.ulpWord[4],
2036                          disc, vport->num_disc_nodes);
2037 
2038         /* Check to see if link went down during discovery */
2039         if (lpfc_els_chk_latt(vport)) {
2040                 spin_lock_irq(shost->host_lock);
2041                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2042                 spin_unlock_irq(shost->host_lock);
2043                 goto out;
2044         }
2045 
2046         if (irsp->ulpStatus) {
2047                 /* Check for retry */
2048                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2049                         /* ELS command is being retried */
2050                         if (disc) {
2051                                 spin_lock_irq(shost->host_lock);
2052                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2053                                 spin_unlock_irq(shost->host_lock);
2054                         }
2055                         goto out;
2056                 }
2057                 /* PLOGI failed Don't print the vport to vport rjts */
2058                 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
2059                         (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
2060                         ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
2061                         (phba)->pport->cfg_log_verbose & LOG_ELS)
2062                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2063                                  "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
2064                                  ndlp->nlp_DID, irsp->ulpStatus,
2065                                  irsp->un.ulpWord[4]);
2066                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2067                 if (!lpfc_error_lost_link(irsp))
2068                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2069                                                 NLP_EVT_CMPL_PLOGI);
2070         } else {
2071                 /* Good status, call state machine */
2072                 prsp = list_entry(((struct lpfc_dmabuf *)
2073                                    cmdiocb->context2)->list.next,
2074                                   struct lpfc_dmabuf, list);
2075                 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
2076                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2077                                              NLP_EVT_CMPL_PLOGI);
2078         }
2079 
2080         if (disc && vport->num_disc_nodes) {
2081                 /* Check to see if there are more PLOGIs to be sent */
2082                 lpfc_more_plogi(vport);
2083 
2084                 if (vport->num_disc_nodes == 0) {
2085                         spin_lock_irq(shost->host_lock);
2086                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
2087                         spin_unlock_irq(shost->host_lock);
2088 
2089                         lpfc_can_disctmo(vport);
2090                         lpfc_end_rscn(vport);
2091                 }
2092         }
2093 
2094 out:
2095         lpfc_els_free_iocb(phba, cmdiocb);
2096         return;
2097 }
2098 
2099 /**
2100  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
2101  * @vport: pointer to a host virtual N_Port data structure.
2102  * @did: destination port identifier.
2103  * @retry: number of retries to the command IOCB.
2104  *
2105  * This routine issues a Port Login (PLOGI) command to a remote N_Port
2106  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
2107  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
2108  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
2109  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
2110  *
2111  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2112  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2113  * will be stored into the context1 field of the IOCB for the completion
2114  * callback function to the PLOGI ELS command.
2115  *
2116  * Return code
2117  *   0 - Successfully issued a plogi for @vport
2118  *   1 - failed to issue a plogi for @vport
2119  **/
2120 int
2121 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
2122 {
2123         struct lpfc_hba  *phba = vport->phba;
2124         struct Scsi_Host *shost;
2125         struct serv_parm *sp;
2126         struct lpfc_nodelist *ndlp;
2127         struct lpfc_iocbq *elsiocb;
2128         uint8_t *pcmd;
2129         uint16_t cmdsize;
2130         int ret;
2131 
2132         ndlp = lpfc_findnode_did(vport, did);
2133 
2134         if (ndlp) {
2135                 /* Defer the processing of the issue PLOGI until after the
2136                  * outstanding UNREG_RPI mbox command completes, unless we
2137                  * are going offline. This logic does not apply for Fabric DIDs
2138                  */
2139                 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2140                     ((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
2141                     !(vport->fc_flag & FC_OFFLINE_MODE)) {
2142                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2143                                          "4110 Issue PLOGI x%x deferred "
2144                                          "on NPort x%x rpi x%x Data: x%px\n",
2145                                          ndlp->nlp_defer_did, ndlp->nlp_DID,
2146                                          ndlp->nlp_rpi, ndlp);
2147 
2148                         /* We can only defer 1st PLOGI */
2149                         if (ndlp->nlp_defer_did == NLP_EVT_NOTHING_PENDING)
2150                                 ndlp->nlp_defer_did = did;
2151                         return 0;
2152                 }
2153                 if (!NLP_CHK_NODE_ACT(ndlp))
2154                         ndlp = NULL;
2155         }
2156 
2157         /* If ndlp is not NULL, we will bump the reference count on it */
2158         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
2159         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
2160                                      ELS_CMD_PLOGI);
2161         if (!elsiocb)
2162                 return 1;
2163 
2164         shost = lpfc_shost_from_vport(vport);
2165         spin_lock_irq(shost->host_lock);
2166         ndlp->nlp_flag &= ~NLP_FCP_PRLI_RJT;
2167         spin_unlock_irq(shost->host_lock);
2168 
2169         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2170 
2171         /* For PLOGI request, remainder of payload is service parameters */
2172         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
2173         pcmd += sizeof(uint32_t);
2174         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2175         sp = (struct serv_parm *) pcmd;
2176 
2177         /*
2178          * If we are a N-port connected to a Fabric, fix-up paramm's so logins
2179          * to device on remote loops work.
2180          */
2181         if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
2182                 sp->cmn.altBbCredit = 1;
2183 
2184         if (sp->cmn.fcphLow < FC_PH_4_3)
2185                 sp->cmn.fcphLow = FC_PH_4_3;
2186 
2187         if (sp->cmn.fcphHigh < FC_PH3)
2188                 sp->cmn.fcphHigh = FC_PH3;
2189 
2190         sp->cmn.valid_vendor_ver_level = 0;
2191         memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
2192         sp->cmn.bbRcvSizeMsb &= 0xF;
2193 
2194         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2195                 "Issue PLOGI:     did:x%x",
2196                 did, 0, 0);
2197 
2198         /* If our firmware supports this feature, convey that
2199          * information to the target using the vendor specific field.
2200          */
2201         if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
2202                 sp->cmn.valid_vendor_ver_level = 1;
2203                 sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
2204                 sp->un.vv.flags = cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
2205         }
2206 
2207         phba->fc_stat.elsXmitPLOGI++;
2208         elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
2209         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2210 
2211         if (ret == IOCB_ERROR) {
2212                 lpfc_els_free_iocb(phba, elsiocb);
2213                 return 1;
2214         }
2215         return 0;
2216 }
2217 
2218 /**
2219  * lpfc_cmpl_els_prli - Completion callback function for prli
2220  * @phba: pointer to lpfc hba data structure.
2221  * @cmdiocb: pointer to lpfc command iocb data structure.
2222  * @rspiocb: pointer to lpfc response iocb data structure.
2223  *
2224  * This routine is the completion callback function for a Process Login
2225  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2226  * status. If there is error status reported, PRLI retry shall be attempted
2227  * by invoking the lpfc_els_retry() routine. Otherwise, the state
2228  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2229  * ndlp to mark the PRLI completion.
2230  **/
2231 static void
2232 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2233                    struct lpfc_iocbq *rspiocb)
2234 {
2235         struct lpfc_vport *vport = cmdiocb->vport;
2236         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2237         IOCB_t *irsp;
2238         struct lpfc_nodelist *ndlp;
2239         char *mode;
2240 
2241         /* we pass cmdiocb to state machine which needs rspiocb as well */
2242         cmdiocb->context_un.rsp_iocb = rspiocb;
2243 
2244         irsp = &(rspiocb->iocb);
2245         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2246         spin_lock_irq(shost->host_lock);
2247         ndlp->nlp_flag &= ~NLP_PRLI_SND;
2248 
2249         /* Driver supports multiple FC4 types.  Counters matter. */
2250         vport->fc_prli_sent--;
2251         ndlp->fc4_prli_sent--;
2252         spin_unlock_irq(shost->host_lock);
2253 
2254         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2255                 "PRLI cmpl:       status:x%x/x%x did:x%x",
2256                 irsp->ulpStatus, irsp->un.ulpWord[4],
2257                 ndlp->nlp_DID);
2258 
2259         /* PRLI completes to NPort <nlp_DID> */
2260         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2261                          "0103 PRLI completes to NPort x%06x "
2262                          "Data: x%x x%x x%x x%x\n",
2263                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2264                          vport->num_disc_nodes, ndlp->fc4_prli_sent);
2265 
2266         /* Check to see if link went down during discovery */
2267         if (lpfc_els_chk_latt(vport))
2268                 goto out;
2269 
2270         if (irsp->ulpStatus) {
2271                 /* Check for retry */
2272                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2273                         /* ELS command is being retried */
2274                         goto out;
2275                 }
2276 
2277                 /* If we don't send GFT_ID to Fabric, a PRLI error
2278                  * could be expected.
2279                  */
2280                 if ((vport->fc_flag & FC_FABRIC) ||
2281                     (vport->cfg_enable_fc4_type != LPFC_ENABLE_BOTH))
2282                         mode = KERN_ERR;
2283                 else
2284                         mode = KERN_INFO;
2285 
2286                 /* PRLI failed */
2287                 lpfc_printf_vlog(vport, mode, LOG_ELS,
2288                                  "2754 PRLI failure DID:%06X Status:x%x/x%x, "
2289                                  "data: x%x\n",
2290                                  ndlp->nlp_DID, irsp->ulpStatus,
2291                                  irsp->un.ulpWord[4], ndlp->fc4_prli_sent);
2292 
2293                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2294                 if (lpfc_error_lost_link(irsp))
2295                         goto out;
2296                 else
2297                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2298                                                 NLP_EVT_CMPL_PRLI);
2299         } else {
2300                 /* Good status, call state machine.  However, if another
2301                  * PRLI is outstanding, don't call the state machine
2302                  * because final disposition to Mapped or Unmapped is
2303                  * completed there.
2304                  */
2305                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2306                                         NLP_EVT_CMPL_PRLI);
2307         }
2308 
2309 out:
2310         lpfc_els_free_iocb(phba, cmdiocb);
2311         return;
2312 }
2313 
2314 /**
2315  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
2316  * @vport: pointer to a host virtual N_Port data structure.
2317  * @ndlp: pointer to a node-list data structure.
2318  * @retry: number of retries to the command IOCB.
2319  *
2320  * This routine issues a Process Login (PRLI) ELS command for the
2321  * @vport. The PRLI service parameters are set up in the payload of the
2322  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
2323  * is put to the IOCB completion callback func field before invoking the
2324  * routine lpfc_sli_issue_iocb() to send out PRLI command.
2325  *
2326  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2327  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2328  * will be stored into the context1 field of the IOCB for the completion
2329  * callback function to the PRLI ELS command.
2330  *
2331  * Return code
2332  *   0 - successfully issued prli iocb command for @vport
2333  *   1 - failed to issue prli iocb command for @vport
2334  **/
2335 int
2336 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2337                     uint8_t retry)
2338 {
2339         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2340         struct lpfc_hba *phba = vport->phba;
2341         PRLI *npr;
2342         struct lpfc_nvme_prli *npr_nvme;
2343         struct lpfc_iocbq *elsiocb;
2344         uint8_t *pcmd;
2345         uint16_t cmdsize;
2346         u32 local_nlp_type, elscmd;
2347 
2348         /*
2349          * If we are in RSCN mode, the FC4 types supported from a
2350          * previous GFT_ID command may not be accurate. So, if we
2351          * are a NVME Initiator, always look for the possibility of
2352          * the remote NPort beng a NVME Target.
2353          */
2354         if (phba->sli_rev == LPFC_SLI_REV4 &&
2355             vport->fc_flag & FC_RSCN_MODE &&
2356             vport->nvmei_support)
2357                 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
2358         local_nlp_type = ndlp->nlp_fc4_type;
2359 
2360         /* This routine will issue 1 or 2 PRLIs, so zero all the ndlp
2361          * fields here before any of them can complete.
2362          */
2363         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2364         ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
2365         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
2366         ndlp->nlp_flag &= ~(NLP_FIRSTBURST | NLP_NPR_2B_DISC);
2367         ndlp->nvme_fb_size = 0;
2368 
2369  send_next_prli:
2370         if (local_nlp_type & NLP_FC4_FCP) {
2371                 /* Payload is 4 + 16 = 20 x14 bytes. */
2372                 cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2373                 elscmd = ELS_CMD_PRLI;
2374         } else if (local_nlp_type & NLP_FC4_NVME) {
2375                 /* Payload is 4 + 20 = 24 x18 bytes. */
2376                 cmdsize = (sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli));
2377                 elscmd = ELS_CMD_NVMEPRLI;
2378         } else {
2379                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2380                                  "3083 Unknown FC_TYPE x%x ndlp x%06x\n",
2381                                  ndlp->nlp_fc4_type, ndlp->nlp_DID);
2382                 return 1;
2383         }
2384 
2385         /* SLI3 ports don't support NVME.  If this rport is a strict NVME
2386          * FC4 type, implicitly LOGO.
2387          */
2388         if (phba->sli_rev == LPFC_SLI_REV3 &&
2389             ndlp->nlp_fc4_type == NLP_FC4_NVME) {
2390                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2391                                  "3088 Rport fc4 type 0x%x not supported by SLI3 adapter\n",
2392                                  ndlp->nlp_type);
2393                 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
2394                 return 1;
2395         }
2396 
2397         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2398                                      ndlp->nlp_DID, elscmd);
2399         if (!elsiocb)
2400                 return 1;
2401 
2402         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2403 
2404         /* For PRLI request, remainder of payload is service parameters */
2405         memset(pcmd, 0, cmdsize);
2406 
2407         if (local_nlp_type & NLP_FC4_FCP) {
2408                 /* Remainder of payload is FCP PRLI parameter page.
2409                  * Note: this data structure is defined as
2410                  * BE/LE in the structure definition so no
2411                  * byte swap call is made.
2412                  */
2413                 *((uint32_t *)(pcmd)) = ELS_CMD_PRLI;
2414                 pcmd += sizeof(uint32_t);
2415                 npr = (PRLI *)pcmd;
2416 
2417                 /*
2418                  * If our firmware version is 3.20 or later,
2419                  * set the following bits for FC-TAPE support.
2420                  */
2421                 if (phba->vpd.rev.feaLevelHigh >= 0x02) {
2422                         npr->ConfmComplAllowed = 1;
2423                         npr->Retry = 1;
2424                         npr->TaskRetryIdReq = 1;
2425                 }
2426                 npr->estabImagePair = 1;
2427                 npr->readXferRdyDis = 1;
2428                 if (vport->cfg_first_burst_size)
2429                         npr->writeXferRdyDis = 1;
2430 
2431                 /* For FCP support */
2432                 npr->prliType = PRLI_FCP_TYPE;
2433                 npr->initiatorFunc = 1;
2434                 elsiocb->iocb_flag |= LPFC_PRLI_FCP_REQ;
2435 
2436                 /* Remove FCP type - processed. */
2437                 local_nlp_type &= ~NLP_FC4_FCP;
2438         } else if (local_nlp_type & NLP_FC4_NVME) {
2439                 /* Remainder of payload is NVME PRLI parameter page.
2440                  * This data structure is the newer definition that
2441                  * uses bf macros so a byte swap is required.
2442                  */
2443                 *((uint32_t *)(pcmd)) = ELS_CMD_NVMEPRLI;
2444                 pcmd += sizeof(uint32_t);
2445                 npr_nvme = (struct lpfc_nvme_prli *)pcmd;
2446                 bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
2447                 bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
2448                 if (phba->nsler) {
2449                         bf_set(prli_nsler, npr_nvme, 1);
2450                         bf_set(prli_conf, npr_nvme, 1);
2451                 }
2452 
2453                 /* Only initiators request first burst. */
2454                 if ((phba->cfg_nvme_enable_fb) &&
2455                     !phba->nvmet_support)
2456                         bf_set(prli_fba, npr_nvme, 1);
2457 
2458                 if (phba->nvmet_support) {
2459                         bf_set(prli_tgt, npr_nvme, 1);
2460                         bf_set(prli_disc, npr_nvme, 1);
2461                 } else {
2462                         bf_set(prli_init, npr_nvme, 1);
2463                         bf_set(prli_conf, npr_nvme, 1);
2464                 }
2465 
2466                 npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
2467                 npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
2468                 elsiocb->iocb_flag |= LPFC_PRLI_NVME_REQ;
2469 
2470                 /* Remove NVME type - processed. */
2471                 local_nlp_type &= ~NLP_FC4_NVME;
2472         }
2473 
2474         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2475                 "Issue PRLI:      did:x%x",
2476                 ndlp->nlp_DID, 0, 0);
2477 
2478         phba->fc_stat.elsXmitPRLI++;
2479         elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
2480         spin_lock_irq(shost->host_lock);
2481         ndlp->nlp_flag |= NLP_PRLI_SND;
2482 
2483         /* The vport counters are used for lpfc_scan_finished, but
2484          * the ndlp is used to track outstanding PRLIs for different
2485          * FC4 types.
2486          */
2487         vport->fc_prli_sent++;
2488         ndlp->fc4_prli_sent++;
2489         spin_unlock_irq(shost->host_lock);
2490         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2491             IOCB_ERROR) {
2492                 spin_lock_irq(shost->host_lock);
2493                 ndlp->nlp_flag &= ~NLP_PRLI_SND;
2494                 spin_unlock_irq(shost->host_lock);
2495                 lpfc_els_free_iocb(phba, elsiocb);
2496                 return 1;
2497         }
2498 
2499 
2500         /* The driver supports 2 FC4 types.  Make sure
2501          * a PRLI is issued for all types before exiting.
2502          */
2503         if (phba->sli_rev == LPFC_SLI_REV4 &&
2504             local_nlp_type & (NLP_FC4_FCP | NLP_FC4_NVME))
2505                 goto send_next_prli;
2506 
2507         return 0;
2508 }
2509 
2510 /**
2511  * lpfc_rscn_disc - Perform rscn discovery for a vport
2512  * @vport: pointer to a host virtual N_Port data structure.
2513  *
2514  * This routine performs Registration State Change Notification (RSCN)
2515  * discovery for a @vport. If the @vport's node port recovery count is not
2516  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
2517  * the nodes that need recovery. If none of the PLOGI were needed through
2518  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
2519  * invoked to check and handle possible more RSCN came in during the period
2520  * of processing the current ones.
2521  **/
2522 static void
2523 lpfc_rscn_disc(struct lpfc_vport *vport)
2524 {
2525         lpfc_can_disctmo(vport);
2526 
2527         /* RSCN discovery */
2528         /* go thru NPR nodes and issue ELS PLOGIs */
2529         if (vport->fc_npr_cnt)
2530                 if (lpfc_els_disc_plogi(vport))
2531                         return;
2532 
2533         lpfc_end_rscn(vport);
2534 }
2535 
2536 /**
2537  * lpfc_adisc_done - Complete the adisc phase of discovery
2538  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2539  *
2540  * This function is called when the final ADISC is completed during discovery.
2541  * This function handles clearing link attention or issuing reg_vpi depending
2542  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2543  * discovery.
2544  * This function is called with no locks held.
2545  **/
2546 static void
2547 lpfc_adisc_done(struct lpfc_vport *vport)
2548 {
2549         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
2550         struct lpfc_hba   *phba = vport->phba;
2551 
2552         /*
2553          * For NPIV, cmpl_reg_vpi will set port_state to READY,
2554          * and continue discovery.
2555          */
2556         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2557             !(vport->fc_flag & FC_RSCN_MODE) &&
2558             (phba->sli_rev < LPFC_SLI_REV4)) {
2559                 /* The ADISCs are complete.  Doesn't matter if they
2560                  * succeeded or failed because the ADISC completion
2561                  * routine guarantees to call the state machine and
2562                  * the RPI is either unregistered (failed ADISC response)
2563                  * or the RPI is still valid and the node is marked
2564                  * mapped for a target.  The exchanges should be in the
2565                  * correct state. This code is specific to SLI3.
2566                  */
2567                 lpfc_issue_clear_la(phba, vport);
2568                 lpfc_issue_reg_vpi(phba, vport);
2569                 return;
2570         }
2571         /*
2572         * For SLI2, we need to set port_state to READY
2573         * and continue discovery.
2574         */
2575         if (vport->port_state < LPFC_VPORT_READY) {
2576                 /* If we get here, there is nothing to ADISC */
2577                 lpfc_issue_clear_la(phba, vport);
2578                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2579                         vport->num_disc_nodes = 0;
2580                         /* go thru NPR list, issue ELS PLOGIs */
2581                         if (vport->fc_npr_cnt)
2582                                 lpfc_els_disc_plogi(vport);
2583                         if (!vport->num_disc_nodes) {
2584                                 spin_lock_irq(shost->host_lock);
2585                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2586                                 spin_unlock_irq(shost->host_lock);
2587                                 lpfc_can_disctmo(vport);
2588                                 lpfc_end_rscn(vport);
2589                         }
2590                 }
2591                 vport->port_state = LPFC_VPORT_READY;
2592         } else
2593                 lpfc_rscn_disc(vport);
2594 }
2595 
2596 /**
2597  * lpfc_more_adisc - Issue more adisc as needed
2598  * @vport: pointer to a host virtual N_Port data structure.
2599  *
2600  * This routine determines whether there are more ndlps on a @vport
2601  * node list need to have Address Discover (ADISC) issued. If so, it will
2602  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2603  * remaining nodes which need to have ADISC sent.
2604  **/
2605 void
2606 lpfc_more_adisc(struct lpfc_vport *vport)
2607 {
2608         if (vport->num_disc_nodes)
2609                 vport->num_disc_nodes--;
2610         /* Continue discovery with <num_disc_nodes> ADISCs to go */
2611         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2612                          "0210 Continue discovery with %d ADISCs to go "
2613                          "Data: x%x x%x x%x\n",
2614                          vport->num_disc_nodes, vport->fc_adisc_cnt,
2615                          vport->fc_flag, vport->port_state);
2616         /* Check to see if there are more ADISCs to be sent */
2617         if (vport->fc_flag & FC_NLP_MORE) {
2618                 lpfc_set_disctmo(vport);
2619                 /* go thru NPR nodes and issue any remaining ELS ADISCs */
2620                 lpfc_els_disc_adisc(vport);
2621         }
2622         if (!vport->num_disc_nodes)
2623                 lpfc_adisc_done(vport);
2624         return;
2625 }
2626 
2627 /**
2628  * lpfc_cmpl_els_adisc - Completion callback function for adisc
2629  * @phba: pointer to lpfc hba data structure.
2630  * @cmdiocb: pointer to lpfc command iocb data structure.
2631  * @rspiocb: pointer to lpfc response iocb data structure.
2632  *
2633  * This routine is the completion function for issuing the Address Discover
2634  * (ADISC) command. It first checks to see whether link went down during
2635  * the discovery process. If so, the node will be marked as node port
2636  * recovery for issuing discover IOCB by the link attention handler and
2637  * exit. Otherwise, the response status is checked. If error was reported
2638  * in the response status, the ADISC command shall be retried by invoking
2639  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2640  * the response status, the state machine is invoked to set transition
2641  * with respect to NLP_EVT_CMPL_ADISC event.
2642  **/
2643 static void
2644 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2645                     struct lpfc_iocbq *rspiocb)
2646 {
2647         struct lpfc_vport *vport = cmdiocb->vport;
2648         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2649         IOCB_t *irsp;
2650         struct lpfc_nodelist *ndlp;
2651         int  disc;
2652 
2653         /* we pass cmdiocb to state machine which needs rspiocb as well */
2654         cmdiocb->context_un.rsp_iocb = rspiocb;
2655 
2656         irsp = &(rspiocb->iocb);
2657         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2658 
2659         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2660                 "ADISC cmpl:      status:x%x/x%x did:x%x",
2661                 irsp->ulpStatus, irsp->un.ulpWord[4],
2662                 ndlp->nlp_DID);
2663 
2664         /* Since ndlp can be freed in the disc state machine, note if this node
2665          * is being used during discovery.
2666          */
2667         spin_lock_irq(shost->host_lock);
2668         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2669         ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
2670         spin_unlock_irq(shost->host_lock);
2671         /* ADISC completes to NPort <nlp_DID> */
2672         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2673                          "0104 ADISC completes to NPort x%x "
2674                          "Data: x%x x%x x%x x%x x%x\n",
2675                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2676                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
2677         /* Check to see if link went down during discovery */
2678         if (lpfc_els_chk_latt(vport)) {
2679                 spin_lock_irq(shost->host_lock);
2680                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2681                 spin_unlock_irq(shost->host_lock);
2682                 goto out;
2683         }
2684 
2685         if (irsp->ulpStatus) {
2686                 /* Check for retry */
2687                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2688                         /* ELS command is being retried */
2689                         if (disc) {
2690                                 spin_lock_irq(shost->host_lock);
2691                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2692                                 spin_unlock_irq(shost->host_lock);
2693                                 lpfc_set_disctmo(vport);
2694                         }
2695                         goto out;
2696                 }
2697                 /* ADISC failed */
2698                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2699                                  "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2700                                  ndlp->nlp_DID, irsp->ulpStatus,
2701                                  irsp->un.ulpWord[4]);
2702                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2703                 if (!lpfc_error_lost_link(irsp))
2704                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2705                                                 NLP_EVT_CMPL_ADISC);
2706         } else
2707                 /* Good status, call state machine */
2708                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2709                                         NLP_EVT_CMPL_ADISC);
2710 
2711         /* Check to see if there are more ADISCs to be sent */
2712         if (disc && vport->num_disc_nodes)
2713                 lpfc_more_adisc(vport);
2714 out:
2715         lpfc_els_free_iocb(phba, cmdiocb);
2716         return;
2717 }
2718 
2719 /**
2720  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2721  * @vport: pointer to a virtual N_Port data structure.
2722  * @ndlp: pointer to a node-list data structure.
2723  * @retry: number of retries to the command IOCB.
2724  *
2725  * This routine issues an Address Discover (ADISC) for an @ndlp on a
2726  * @vport. It prepares the payload of the ADISC ELS command, updates the
2727  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2728  * to issue the ADISC ELS command.
2729  *
2730  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2731  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2732  * will be stored into the context1 field of the IOCB for the completion
2733  * callback function to the ADISC ELS command.
2734  *
2735  * Return code
2736  *   0 - successfully issued adisc
2737  *   1 - failed to issue adisc
2738  **/
2739 int
2740 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2741                      uint8_t retry)
2742 {
2743         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2744         struct lpfc_hba  *phba = vport->phba;
2745         ADISC *ap;
2746         struct lpfc_iocbq *elsiocb;
2747         uint8_t *pcmd;
2748         uint16_t cmdsize;
2749 
2750         cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2751         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2752                                      ndlp->nlp_DID, ELS_CMD_ADISC);
2753         if (!elsiocb)
2754                 return 1;
2755 
2756         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2757 
2758         /* For ADISC request, remainder of payload is service parameters */
2759         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2760         pcmd += sizeof(uint32_t);
2761 
2762         /* Fill in ADISC payload */
2763         ap = (ADISC *) pcmd;
2764         ap->hardAL_PA = phba->fc_pref_ALPA;
2765         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2766         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2767         ap->DID = be32_to_cpu(vport->fc_myDID);
2768 
2769         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2770                 "Issue ADISC:     did:x%x",
2771                 ndlp->nlp_DID, 0, 0);
2772 
2773         phba->fc_stat.elsXmitADISC++;
2774         elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2775         spin_lock_irq(shost->host_lock);
2776         ndlp->nlp_flag |= NLP_ADISC_SND;
2777         spin_unlock_irq(shost->host_lock);
2778         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2779             IOCB_ERROR) {
2780                 spin_lock_irq(shost->host_lock);
2781                 ndlp->nlp_flag &= ~NLP_ADISC_SND;
2782                 spin_unlock_irq(shost->host_lock);
2783                 lpfc_els_free_iocb(phba, elsiocb);
2784                 return 1;
2785         }
2786         return 0;
2787 }
2788 
2789 /**
2790  * lpfc_cmpl_els_logo - Completion callback function for logo
2791  * @phba: pointer to lpfc hba data structure.
2792  * @cmdiocb: pointer to lpfc command iocb data structure.
2793  * @rspiocb: pointer to lpfc response iocb data structure.
2794  *
2795  * This routine is the completion function for issuing the ELS Logout (LOGO)
2796  * command. If no error status was reported from the LOGO response, the
2797  * state machine of the associated ndlp shall be invoked for transition with
2798  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
2799  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
2800  **/
2801 static void
2802 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2803                    struct lpfc_iocbq *rspiocb)
2804 {
2805         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2806         struct lpfc_vport *vport = ndlp->vport;
2807         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2808         IOCB_t *irsp;
2809         struct lpfcMboxq *mbox;
2810         unsigned long flags;
2811         uint32_t skip_recovery = 0;
2812 
2813         /* we pass cmdiocb to state machine which needs rspiocb as well */
2814         cmdiocb->context_un.rsp_iocb = rspiocb;
2815 
2816         irsp = &(rspiocb->iocb);
2817         spin_lock_irq(shost->host_lock);
2818         ndlp->nlp_flag &= ~NLP_LOGO_SND;
2819         spin_unlock_irq(shost->host_lock);
2820 
2821         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2822                 "LOGO cmpl:       status:x%x/x%x did:x%x",
2823                 irsp->ulpStatus, irsp->un.ulpWord[4],
2824                 ndlp->nlp_DID);
2825 
2826         /* LOGO completes to NPort <nlp_DID> */
2827         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2828                          "0105 LOGO completes to NPort x%x "
2829                          "Data: x%x x%x x%x x%x\n",
2830                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2831                          irsp->ulpTimeout, vport->num_disc_nodes);
2832 
2833         if (lpfc_els_chk_latt(vport)) {
2834                 skip_recovery = 1;
2835                 goto out;
2836         }
2837 
2838         /* Check to see if link went down during discovery */
2839         if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2840                 /* NLP_EVT_DEVICE_RM should unregister the RPI
2841                  * which should abort all outstanding IOs.
2842                  */
2843                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2844                                         NLP_EVT_DEVICE_RM);
2845                 skip_recovery = 1;
2846                 goto out;
2847         }
2848 
2849         /* The LOGO will not be retried on failure.  A LOGO was
2850          * issued to the remote rport and a ACC or RJT or no Answer are
2851          * all acceptable.  Note the failure and move forward with
2852          * discovery.  The PLOGI will retry.
2853          */
2854         if (irsp->ulpStatus) {
2855                 /* LOGO failed */
2856                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2857                                  "2756 LOGO failure, No Retry DID:%06X Status:x%x/x%x\n",
2858                                  ndlp->nlp_DID, irsp->ulpStatus,
2859                                  irsp->un.ulpWord[4]);
2860                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2861                 if (lpfc_error_lost_link(irsp)) {
2862                         skip_recovery = 1;
2863                         goto out;
2864                 }
2865         }
2866 
2867         /* Call state machine. This will unregister the rpi if needed. */
2868         lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
2869 
2870 out:
2871         lpfc_els_free_iocb(phba, cmdiocb);
2872         /* If we are in pt2pt mode, we could rcv new S_ID on PLOGI */
2873         if ((vport->fc_flag & FC_PT2PT) &&
2874                 !(vport->fc_flag & FC_PT2PT_PLOGI)) {
2875                 phba->pport->fc_myDID = 0;
2876 
2877                 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
2878                     (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
2879                         if (phba->nvmet_support)
2880                                 lpfc_nvmet_update_targetport(phba);
2881                         else
2882                                 lpfc_nvme_update_localport(phba->pport);
2883                 }
2884 
2885                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2886                 if (mbox) {
2887                         lpfc_config_link(phba, mbox);
2888                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2889                         mbox->vport = vport;
2890                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
2891                                 MBX_NOT_FINISHED) {
2892                                 mempool_free(mbox, phba->mbox_mem_pool);
2893                                 skip_recovery = 1;
2894                         }
2895                 }
2896         }
2897 
2898         /*
2899          * If the node is a target, the handling attempts to recover the port.
2900          * For any other port type, the rpi is unregistered as an implicit
2901          * LOGO.
2902          */
2903         if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) &&
2904             skip_recovery == 0) {
2905                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2906                 spin_lock_irqsave(shost->host_lock, flags);
2907                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2908                 spin_unlock_irqrestore(shost->host_lock, flags);
2909 
2910                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2911                                  "3187 LOGO completes to NPort x%x: Start "
2912                                  "Recovery Data: x%x x%x x%x x%x\n",
2913                                  ndlp->nlp_DID, irsp->ulpStatus,
2914                                  irsp->un.ulpWord[4], irsp->ulpTimeout,
2915                                  vport->num_disc_nodes);
2916                 lpfc_disc_start(vport);
2917         }
2918         return;
2919 }
2920 
2921 /**
2922  * lpfc_issue_els_logo - Issue a logo to an node on a vport
2923  * @vport: pointer to a virtual N_Port data structure.
2924  * @ndlp: pointer to a node-list data structure.
2925  * @retry: number of retries to the command IOCB.
2926  *
2927  * This routine constructs and issues an ELS Logout (LOGO) iocb command
2928  * to a remote node, referred by an @ndlp on a @vport. It constructs the
2929  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2930  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2931  *
2932  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2933  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2934  * will be stored into the context1 field of the IOCB for the completion
2935  * callback function to the LOGO ELS command.
2936  *
2937  * Callers of this routine are expected to unregister the RPI first
2938  *
2939  * Return code
2940  *   0 - successfully issued logo
2941  *   1 - failed to issue logo
2942  **/
2943 int
2944 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2945                     uint8_t retry)
2946 {
2947         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2948         struct lpfc_hba  *phba = vport->phba;
2949         struct lpfc_iocbq *elsiocb;
2950         uint8_t *pcmd;
2951         uint16_t cmdsize;
2952         int rc;
2953 
2954         spin_lock_irq(shost->host_lock);
2955         if (ndlp->nlp_flag & NLP_LOGO_SND) {
2956                 spin_unlock_irq(shost->host_lock);
2957                 return 0;
2958         }
2959         spin_unlock_irq(shost->host_lock);
2960 
2961         cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2962         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2963                                      ndlp->nlp_DID, ELS_CMD_LOGO);
2964         if (!elsiocb)
2965                 return 1;
2966 
2967         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2968         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
2969         pcmd += sizeof(uint32_t);
2970 
2971         /* Fill in LOGO payload */
2972         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
2973         pcmd += sizeof(uint32_t);
2974         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
2975 
2976         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2977                 "Issue LOGO:      did:x%x",
2978                 ndlp->nlp_DID, 0, 0);
2979 
2980         phba->fc_stat.elsXmitLOGO++;
2981         elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2982         spin_lock_irq(shost->host_lock);
2983         ndlp->nlp_flag |= NLP_LOGO_SND;
2984         ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
2985         spin_unlock_irq(shost->host_lock);
2986         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2987         if (rc == IOCB_ERROR) {
2988                 spin_lock_irq(shost->host_lock);
2989                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2990                 spin_unlock_irq(shost->host_lock);
2991                 lpfc_els_free_iocb(phba, elsiocb);
2992                 return 1;
2993         }
2994 
2995         spin_lock_irq(shost->host_lock);
2996         ndlp->nlp_prev_state = ndlp->nlp_state;
2997         spin_unlock_irq(shost->host_lock);
2998         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
2999         return 0;
3000 }
3001 
3002 /**
3003  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
3004  * @phba: pointer to lpfc hba data structure.
3005  * @cmdiocb: pointer to lpfc command iocb data structure.
3006  * @rspiocb: pointer to lpfc response iocb data structure.
3007  *
3008  * This routine is a generic completion callback function for ELS commands.
3009  * Specifically, it is the callback function which does not need to perform
3010  * any command specific operations. It is currently used by the ELS command
3011  * issuing routines for the ELS State Change  Request (SCR),
3012  * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
3013  * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
3014  * certain debug loggings, this callback function simply invokes the
3015  * lpfc_els_chk_latt() routine to check whether link went down during the
3016  * discovery process.
3017  **/
3018 static void
3019 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3020                   struct lpfc_iocbq *rspiocb)
3021 {
3022         struct lpfc_vport *vport = cmdiocb->vport;
3023         IOCB_t *irsp;
3024 
3025         irsp = &rspiocb->iocb;
3026 
3027         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3028                 "ELS cmd cmpl:    status:x%x/x%x did:x%x",
3029                 irsp->ulpStatus, irsp->un.ulpWord[4],
3030                 irsp->un.elsreq64.remoteID);
3031         /* ELS cmd tag <ulpIoTag> completes */
3032         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3033                          "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
3034                          irsp->ulpIoTag, irsp->ulpStatus,
3035                          irsp->un.ulpWord[4], irsp->ulpTimeout);
3036         /* Check to see if link went down during discovery */
3037         lpfc_els_chk_latt(vport);
3038         lpfc_els_free_iocb(phba, cmdiocb);
3039         return;
3040 }
3041 
3042 /**
3043  * lpfc_issue_els_scr - Issue a scr to an node on a vport
3044  * @vport: pointer to a host virtual N_Port data structure.
3045  * @nportid: N_Port identifier to the remote node.
3046  * @retry: number of retries to the command IOCB.
3047  *
3048  * This routine issues a State Change Request (SCR) to a fabric node
3049  * on a @vport. The remote node @nportid is passed into the function. It
3050  * first search the @vport node list to find the matching ndlp. If no such
3051  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
3052  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
3053  * routine is invoked to send the SCR IOCB.
3054  *
3055  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3056  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3057  * will be stored into the context1 field of the IOCB for the completion
3058  * callback function to the SCR ELS command.
3059  *
3060  * Return code
3061  *   0 - Successfully issued scr command
3062  *   1 - Failed to issue scr command
3063  **/
3064 int
3065 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
3066 {
3067         struct lpfc_hba  *phba = vport->phba;
3068         struct lpfc_iocbq *elsiocb;
3069         uint8_t *pcmd;
3070         uint16_t cmdsize;
3071         struct lpfc_nodelist *ndlp;
3072 
3073         cmdsize = (sizeof(uint32_t) + sizeof(SCR));
3074 
3075         ndlp = lpfc_findnode_did(vport, nportid);
3076         if (!ndlp) {
3077                 ndlp = lpfc_nlp_init(vport, nportid);
3078                 if (!ndlp)
3079                         return 1;
3080                 lpfc_enqueue_node(vport, ndlp);
3081         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3082                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3083                 if (!ndlp)
3084                         return 1;
3085         }
3086 
3087         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3088                                      ndlp->nlp_DID, ELS_CMD_SCR);
3089 
3090         if (!elsiocb) {
3091                 /* This will trigger the release of the node just
3092                  * allocated
3093                  */
3094                 lpfc_nlp_put(ndlp);
3095                 return 1;
3096         }
3097 
3098         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3099 
3100         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
3101         pcmd += sizeof(uint32_t);
3102 
3103         /* For SCR, remainder of payload is SCR parameter page */
3104         memset(pcmd, 0, sizeof(SCR));
3105         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
3106 
3107         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3108                 "Issue SCR:       did:x%x",
3109                 ndlp->nlp_DID, 0, 0);
3110 
3111         phba->fc_stat.elsXmitSCR++;
3112         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3113         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3114             IOCB_ERROR) {
3115                 /* The additional lpfc_nlp_put will cause the following
3116                  * lpfc_els_free_iocb routine to trigger the rlease of
3117                  * the node.
3118                  */
3119                 lpfc_nlp_put(ndlp);
3120                 lpfc_els_free_iocb(phba, elsiocb);
3121                 return 1;
3122         }
3123         /* This will cause the callback-function lpfc_cmpl_els_cmd to
3124          * trigger the release of node.
3125          */
3126         if (!(vport->fc_flag & FC_PT2PT))
3127                 lpfc_nlp_put(ndlp);
3128         return 0;
3129 }
3130 
3131 /**
3132  * lpfc_issue_els_rscn - Issue an RSCN to the Fabric Controller (Fabric)
3133  *   or the other nport (pt2pt).
3134  * @vport: pointer to a host virtual N_Port data structure.
3135  * @retry: number of retries to the command IOCB.
3136  *
3137  * This routine issues a RSCN to the Fabric Controller (DID 0xFFFFFD)
3138  *  when connected to a fabric, or to the remote port when connected
3139  *  in point-to-point mode. When sent to the Fabric Controller, it will
3140  *  replay the RSCN to registered recipients.
3141  *
3142  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3143  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3144  * will be stored into the context1 field of the IOCB for the completion
3145  * callback function to the RSCN ELS command.
3146  *
3147  * Return code
3148  *   0 - Successfully issued RSCN command
3149  *   1 - Failed to issue RSCN command
3150  **/
3151 int
3152 lpfc_issue_els_rscn(struct lpfc_vport *vport, uint8_t retry)
3153 {
3154         struct lpfc_hba *phba = vport->phba;
3155         struct lpfc_iocbq *elsiocb;
3156         struct lpfc_nodelist *ndlp;
3157         struct {
3158                 struct fc_els_rscn rscn;
3159                 struct fc_els_rscn_page portid;
3160         } *event;
3161         uint32_t nportid;
3162         uint16_t cmdsize = sizeof(*event);
3163 
3164         /* Not supported for private loop */
3165         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
3166             !(vport->fc_flag & FC_PUBLIC_LOOP))
3167                 return 1;
3168 
3169         if (vport->fc_flag & FC_PT2PT) {
3170                 /* find any mapped nport - that would be the other nport */
3171                 ndlp = lpfc_findnode_mapped(vport);
3172                 if (!ndlp)
3173                         return 1;
3174         } else {
3175                 nportid = FC_FID_FCTRL;
3176                 /* find the fabric controller node */
3177                 ndlp = lpfc_findnode_did(vport, nportid);
3178                 if (!ndlp) {
3179                         /* if one didn't exist, make one */
3180                         ndlp = lpfc_nlp_init(vport, nportid);
3181                         if (!ndlp)
3182                                 return 1;
3183                         lpfc_enqueue_node(vport, ndlp);
3184                 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3185                         ndlp = lpfc_enable_node(vport, ndlp,
3186                                                 NLP_STE_UNUSED_NODE);
3187                         if (!ndlp)
3188                                 return 1;
3189                 }
3190         }
3191 
3192         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3193                                      ndlp->nlp_DID, ELS_CMD_RSCN_XMT);
3194 
3195         if (!elsiocb) {
3196                 /* This will trigger the release of the node just
3197                  * allocated
3198                  */
3199                 lpfc_nlp_put(ndlp);
3200                 return 1;
3201         }
3202 
3203         event = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
3204 
3205         event->rscn.rscn_cmd = ELS_RSCN;
3206         event->rscn.rscn_page_len = sizeof(struct fc_els_rscn_page);
3207         event->rscn.rscn_plen = cpu_to_be16(cmdsize);
3208 
3209         nportid = vport->fc_myDID;
3210         /* appears that page flags must be 0 for fabric to broadcast RSCN */
3211         event->portid.rscn_page_flags = 0;
3212         event->portid.rscn_fid[0] = (nportid & 0x00FF0000) >> 16;
3213         event->portid.rscn_fid[1] = (nportid & 0x0000FF00) >> 8;
3214         event->portid.rscn_fid[2] = nportid & 0x000000FF;
3215 
3216         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3217                               "Issue RSCN:       did:x%x",
3218                               ndlp->nlp_DID, 0, 0);
3219 
3220         phba->fc_stat.elsXmitRSCN++;
3221         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3222         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3223             IOCB_ERROR) {
3224                 /* The additional lpfc_nlp_put will cause the following
3225                  * lpfc_els_free_iocb routine to trigger the rlease of
3226                  * the node.
3227                  */
3228                 lpfc_nlp_put(ndlp);
3229                 lpfc_els_free_iocb(phba, elsiocb);
3230                 return 1;
3231         }
3232         /* This will cause the callback-function lpfc_cmpl_els_cmd to
3233          * trigger the release of node.
3234          */
3235         if (!(vport->fc_flag & FC_PT2PT))
3236                 lpfc_nlp_put(ndlp);
3237 
3238         return 0;
3239 }
3240 
3241 /**
3242  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
3243  * @vport: pointer to a host virtual N_Port data structure.
3244  * @nportid: N_Port identifier to the remote node.
3245  * @retry: number of retries to the command IOCB.
3246  *
3247  * This routine issues a Fibre Channel Address Resolution Response
3248  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
3249  * is passed into the function. It first search the @vport node list to find
3250  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
3251  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
3252  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
3253  *
3254  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3255  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3256  * will be stored into the context1 field of the IOCB for the completion
3257  * callback function to the PARPR ELS command.
3258  *
3259  * Return code
3260  *   0 - Successfully issued farpr command
3261  *   1 - Failed to issue farpr command
3262  **/
3263 static int
3264 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
3265 {
3266         struct lpfc_hba  *phba = vport->phba;
3267         struct lpfc_iocbq *elsiocb;
3268         FARP *fp;
3269         uint8_t *pcmd;
3270         uint32_t *lp;
3271         uint16_t cmdsize;
3272         struct lpfc_nodelist *ondlp;
3273         struct lpfc_nodelist *ndlp;
3274 
3275         cmdsize = (sizeof(uint32_t) + sizeof(FARP));
3276 
3277         ndlp = lpfc_findnode_did(vport, nportid);
3278         if (!ndlp) {
3279                 ndlp = lpfc_nlp_init(vport, nportid);
3280                 if (!ndlp)
3281                         return 1;
3282                 lpfc_enqueue_node(vport, ndlp);
3283         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3284                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3285                 if (!ndlp)
3286                         return 1;
3287         }
3288 
3289         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3290                                      ndlp->nlp_DID, ELS_CMD_RNID);
3291         if (!elsiocb) {
3292                 /* This will trigger the release of the node just
3293                  * allocated
3294                  */
3295                 lpfc_nlp_put(ndlp);
3296                 return 1;
3297         }
3298 
3299         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3300 
3301         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
3302         pcmd += sizeof(uint32_t);
3303 
3304         /* Fill in FARPR payload */
3305         fp = (FARP *) (pcmd);
3306         memset(fp, 0, sizeof(FARP));
3307         lp = (uint32_t *) pcmd;
3308         *lp++ = be32_to_cpu(nportid);
3309         *lp++ = be32_to_cpu(vport->fc_myDID);
3310         fp->Rflags = 0;
3311         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
3312 
3313         memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
3314         memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3315         ondlp = lpfc_findnode_did(vport, nportid);
3316         if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
3317                 memcpy(&fp->OportName, &ondlp->nlp_portname,
3318                        sizeof(struct lpfc_name));
3319                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
3320                        sizeof(struct lpfc_name));
3321         }
3322 
3323         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3324                 "Issue FARPR:     did:x%x",
3325                 ndlp->nlp_DID, 0, 0);
3326 
3327         phba->fc_stat.elsXmitFARPR++;
3328         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3329         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3330             IOCB_ERROR) {
3331                 /* The additional lpfc_nlp_put will cause the following
3332                  * lpfc_els_free_iocb routine to trigger the release of
3333                  * the node.
3334                  */
3335                 lpfc_nlp_put(ndlp);
3336                 lpfc_els_free_iocb(phba, elsiocb);
3337                 return 1;
3338         }
3339         /* This will cause the callback-function lpfc_cmpl_els_cmd to
3340          * trigger the release of the node.
3341          */
3342         lpfc_nlp_put(ndlp);
3343         return 0;
3344 }
3345 
3346 /**
3347  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
3348  * @vport: pointer to a host virtual N_Port data structure.
3349  * @nlp: pointer to a node-list data structure.
3350  *
3351  * This routine cancels the timer with a delayed IOCB-command retry for
3352  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
3353  * removes the ELS retry event if it presents. In addition, if the
3354  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
3355  * commands are sent for the @vport's nodes that require issuing discovery
3356  * ADISC.
3357  **/
3358 void
3359 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
3360 {
3361         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3362         struct lpfc_work_evt *evtp;
3363 
3364         if (!(nlp->nlp_flag & NLP_DELAY_TMO))
3365                 return;
3366         spin_lock_irq(shost->host_lock);
3367         nlp->nlp_flag &= ~NLP_DELAY_TMO;
3368         spin_unlock_irq(shost->host_lock);
3369         del_timer_sync(&nlp->nlp_delayfunc);
3370         nlp->nlp_last_elscmd = 0;
3371         if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
3372                 list_del_init(&nlp->els_retry_evt.evt_listp);
3373                 /* Decrement nlp reference count held for the delayed retry */
3374                 evtp = &nlp->els_retry_evt;
3375                 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
3376         }
3377         if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
3378                 spin_lock_irq(shost->host_lock);
3379                 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
3380                 spin_unlock_irq(shost->host_lock);
3381                 if (vport->num_disc_nodes) {
3382                         if (vport->port_state < LPFC_VPORT_READY) {
3383                                 /* Check if there are more ADISCs to be sent */
3384                                 lpfc_more_adisc(vport);
3385                         } else {
3386                                 /* Check if there are more PLOGIs to be sent */
3387                                 lpfc_more_plogi(vport);
3388                                 if (vport->num_disc_nodes == 0) {
3389                                         spin_lock_irq(shost->host_lock);
3390                                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
3391                                         spin_unlock_irq(shost->host_lock);
3392                                         lpfc_can_disctmo(vport);
3393                                         lpfc_end_rscn(vport);
3394                                 }
3395                         }
3396                 }
3397         }
3398         return;
3399 }
3400 
3401 /**
3402  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
3403  * @ptr: holder for the pointer to the timer function associated data (ndlp).
3404  *
3405  * This routine is invoked by the ndlp delayed-function timer to check
3406  * whether there is any pending ELS retry event(s) with the node. If not, it
3407  * simply returns. Otherwise, if there is at least one ELS delayed event, it
3408  * adds the delayed events to the HBA work list and invokes the
3409  * lpfc_worker_wake_up() routine to wake up worker thread to process the
3410  * event. Note that lpfc_nlp_get() is called before posting the event to
3411  * the work list to hold reference count of ndlp so that it guarantees the
3412  * reference to ndlp will still be available when the worker thread gets
3413  * to the event associated with the ndlp.
3414  **/
3415 void
3416 lpfc_els_retry_delay(struct timer_list *t)
3417 {
3418         struct lpfc_nodelist *ndlp = from_timer(ndlp, t, nlp_delayfunc);
3419         struct lpfc_vport *vport = ndlp->vport;
3420         struct lpfc_hba   *phba = vport->phba;
3421         unsigned long flags;
3422         struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
3423 
3424         spin_lock_irqsave(&phba->hbalock, flags);
3425         if (!list_empty(&evtp->evt_listp)) {
3426                 spin_unlock_irqrestore(&phba->hbalock, flags);
3427                 return;
3428         }
3429 
3430         /* We need to hold the node by incrementing the reference
3431          * count until the queued work is done
3432          */
3433         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
3434         if (evtp->evt_arg1) {
3435                 evtp->evt = LPFC_EVT_ELS_RETRY;
3436                 list_add_tail(&evtp->evt_listp, &phba->work_list);
3437                 lpfc_worker_wake_up(phba);
3438         }
3439         spin_unlock_irqrestore(&phba->hbalock, flags);
3440         return;
3441 }
3442 
3443 /**
3444  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
3445  * @ndlp: pointer to a node-list data structure.
3446  *
3447  * This routine is the worker-thread handler for processing the @ndlp delayed
3448  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
3449  * the last ELS command from the associated ndlp and invokes the proper ELS
3450  * function according to the delayed ELS command to retry the command.
3451  **/
3452 void
3453 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
3454 {
3455         struct lpfc_vport *vport = ndlp->vport;
3456         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3457         uint32_t cmd, retry;
3458 
3459         spin_lock_irq(shost->host_lock);
3460         cmd = ndlp->nlp_last_elscmd;
3461         ndlp->nlp_last_elscmd = 0;
3462 
3463         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
3464                 spin_unlock_irq(shost->host_lock);
3465                 return;
3466         }
3467 
3468         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
3469         spin_unlock_irq(shost->host_lock);
3470         /*
3471          * If a discovery event readded nlp_delayfunc after timer
3472          * firing and before processing the timer, cancel the
3473          * nlp_delayfunc.
3474          */
3475         del_timer_sync(&ndlp->nlp_delayfunc);
3476         retry = ndlp->nlp_retry;
3477         ndlp->nlp_retry = 0;
3478 
3479         switch (cmd) {
3480         case ELS_CMD_FLOGI:
3481                 lpfc_issue_els_flogi(vport, ndlp, retry);
3482                 break;
3483         case ELS_CMD_PLOGI:
3484                 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
3485                         ndlp->nlp_prev_state = ndlp->nlp_state;
3486                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3487                 }
3488                 break;
3489         case ELS_CMD_ADISC:
3490                 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
3491                         ndlp->nlp_prev_state = ndlp->nlp_state;
3492                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3493                 }
3494                 break;
3495         case ELS_CMD_PRLI:
3496         case ELS_CMD_NVMEPRLI:
3497                 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
3498                         ndlp->nlp_prev_state = ndlp->nlp_state;
3499                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3500                 }
3501                 break;
3502         case ELS_CMD_LOGO:
3503                 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
3504                         ndlp->nlp_prev_state = ndlp->nlp_state;
3505                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3506                 }
3507                 break;
3508         case ELS_CMD_FDISC:
3509                 if (!(vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI))
3510                         lpfc_issue_els_fdisc(vport, ndlp, retry);
3511                 break;
3512         }
3513         return;
3514 }
3515 
3516 /**
3517  * lpfc_link_reset - Issue link reset
3518  * @vport: pointer to a virtual N_Port data structure.
3519  *
3520  * This routine performs link reset by sending INIT_LINK mailbox command.
3521  * For SLI-3 adapter, link attention interrupt is enabled before issuing
3522  * INIT_LINK mailbox command.
3523  *
3524  * Return code
3525  *   0 - Link reset initiated successfully
3526  *   1 - Failed to initiate link reset
3527  **/
3528 int
3529 lpfc_link_reset(struct lpfc_vport *vport)
3530 {
3531         struct lpfc_hba *phba = vport->phba;
3532         LPFC_MBOXQ_t *mbox;
3533         uint32_t control;
3534         int rc;
3535 
3536         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3537                          "2851 Attempt link reset\n");
3538         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3539         if (!mbox) {
3540                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
3541                                 "2852 Failed to allocate mbox memory");
3542                 return 1;
3543         }
3544 
3545         /* Enable Link attention interrupts */
3546         if (phba->sli_rev <= LPFC_SLI_REV3) {
3547                 spin_lock_irq(&phba->hbalock);
3548                 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3549                 control = readl(phba->HCregaddr);
3550                 control |= HC_LAINT_ENA;
3551                 writel(control, phba->HCregaddr);
3552                 readl(phba->HCregaddr); /* flush */
3553                 spin_unlock_irq(&phba->hbalock);
3554         }
3555 
3556         lpfc_init_link(phba, mbox, phba->cfg_topology,
3557                        phba->cfg_link_speed);
3558         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3559         mbox->vport = vport;
3560         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3561         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
3562                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
3563                                 "2853 Failed to issue INIT_LINK "
3564                                 "mbox command, rc:x%x\n", rc);
3565                 mempool_free(mbox, phba->mbox_mem_pool);
3566                 return 1;
3567         }
3568 
3569         return 0;
3570 }
3571 
3572 /**
3573  * lpfc_els_retry - Make retry decision on an els command iocb
3574  * @phba: pointer to lpfc hba data structure.
3575  * @cmdiocb: pointer to lpfc command iocb data structure.
3576  * @rspiocb: pointer to lpfc response iocb data structure.
3577  *
3578  * This routine makes a retry decision on an ELS command IOCB, which has
3579  * failed. The following ELS IOCBs use this function for retrying the command
3580  * when previously issued command responsed with error status: FLOGI, PLOGI,
3581  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
3582  * returned error status, it makes the decision whether a retry shall be
3583  * issued for the command, and whether a retry shall be made immediately or
3584  * delayed. In the former case, the corresponding ELS command issuing-function
3585  * is called to retry the command. In the later case, the ELS command shall
3586  * be posted to the ndlp delayed event and delayed function timer set to the
3587  * ndlp for the delayed command issusing.
3588  *
3589  * Return code
3590  *   0 - No retry of els command is made
3591  *   1 - Immediate or delayed retry of els command is made
3592  **/
3593 static int
3594 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3595                struct lpfc_iocbq *rspiocb)
3596 {
3597         struct lpfc_vport *vport = cmdiocb->vport;
3598         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3599         IOCB_t *irsp = &rspiocb->iocb;
3600         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3601         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3602         uint32_t *elscmd;
3603         struct ls_rjt stat;
3604         int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
3605         int logerr = 0;
3606         uint32_t cmd = 0;
3607         uint32_t did;
3608         int link_reset = 0, rc;
3609 
3610 
3611         /* Note: context2 may be 0 for internal driver abort
3612          * of delays ELS command.
3613          */
3614 
3615         if (pcmd && pcmd->virt) {
3616                 elscmd = (uint32_t *) (pcmd->virt);
3617                 cmd = *elscmd++;
3618         }
3619 
3620         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
3621                 did = ndlp->nlp_DID;
3622         else {
3623                 /* We should only hit this case for retrying PLOGI */
3624                 did = irsp->un.elsreq64.remoteID;
3625                 ndlp = lpfc_findnode_did(vport, did);
3626                 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
3627                     && (cmd != ELS_CMD_PLOGI))
3628                         return 1;
3629         }
3630 
3631         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3632                 "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
3633                 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
3634 
3635         switch (irsp->ulpStatus) {
3636         case IOSTAT_FCP_RSP_ERROR:
3637                 break;
3638         case IOSTAT_REMOTE_STOP:
3639                 if (phba->sli_rev == LPFC_SLI_REV4) {
3640                         /* This IO was aborted by the target, we don't
3641                          * know the rxid and because we did not send the
3642                          * ABTS we cannot generate and RRQ.
3643                          */
3644                         lpfc_set_rrq_active(phba, ndlp,
3645                                          cmdiocb->sli4_lxritag, 0, 0);
3646                 }
3647                 break;
3648         case IOSTAT_LOCAL_REJECT:
3649                 switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
3650                 case IOERR_LOOP_OPEN_FAILURE:
3651                         if (cmd == ELS_CMD_FLOGI) {
3652                                 if (PCI_DEVICE_ID_HORNET ==
3653                                         phba->pcidev->device) {
3654                                         phba->fc_topology = LPFC_TOPOLOGY_LOOP;
3655                                         phba->pport->fc_myDID = 0;
3656                                         phba->alpa_map[0] = 0;
3657                                         phba->alpa_map[1] = 0;
3658                                 }
3659                         }
3660                         if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
3661                                 delay = 1000;
3662                         retry = 1;
3663                         break;
3664 
3665                 case IOERR_ILLEGAL_COMMAND:
3666                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3667                                          "0124 Retry illegal cmd x%x "
3668                                          "retry:x%x delay:x%x\n",
3669                                          cmd, cmdiocb->retry, delay);
3670                         retry = 1;
3671                         /* All command's retry policy */
3672                         maxretry = 8;
3673                         if (cmdiocb->retry > 2)
3674                                 delay = 1000;
3675                         break;
3676 
3677                 case IOERR_NO_RESOURCES:
3678                         logerr = 1; /* HBA out of resources */
3679                         retry = 1;
3680                         if (cmdiocb->retry > 100)
3681                                 delay = 100;
3682                         maxretry = 250;
3683                         break;
3684 
3685                 case IOERR_ILLEGAL_FRAME:
3686                         delay = 100;
3687                         retry = 1;
3688                         break;
3689 
3690                 case IOERR_INVALID_RPI:
3691                         if (cmd == ELS_CMD_PLOGI &&
3692                             did == NameServer_DID) {
3693                                 /* Continue forever if plogi to */
3694                                 /* the nameserver fails */
3695                                 maxretry = 0;
3696                                 delay = 100;
3697                         }
3698                         retry = 1;
3699                         break;
3700 
3701                 case IOERR_SEQUENCE_TIMEOUT:
3702                         if (cmd == ELS_CMD_PLOGI &&
3703                             did == NameServer_DID &&
3704                             (cmdiocb->retry + 1) == maxretry) {
3705                                 /* Reset the Link */
3706                                 link_reset = 1;
3707                                 break;
3708                         }
3709                         retry = 1;
3710                         delay = 100;
3711                         break;
3712                 }
3713                 break;
3714 
3715         case IOSTAT_NPORT_RJT:
3716         case IOSTAT_FABRIC_RJT:
3717                 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
3718                         retry = 1;
3719                         break;
3720                 }
3721                 break;
3722 
3723         case IOSTAT_NPORT_BSY:
3724         case IOSTAT_FABRIC_BSY:
3725                 logerr = 1; /* Fabric / Remote NPort out of resources */
3726                 retry = 1;
3727                 break;
3728 
3729         case IOSTAT_LS_RJT:
3730                 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
3731                 /* Added for Vendor specifc support
3732                  * Just keep retrying for these Rsn / Exp codes
3733                  */
3734                 switch (stat.un.b.lsRjtRsnCode) {
3735                 case LSRJT_UNABLE_TPC:
3736                         /* The driver has a VALID PLOGI but the rport has
3737                          * rejected the PRLI - can't do it now.  Delay
3738                          * for 1 second and try again - don't care about
3739                          * the explanation.
3740                          */
3741                         if (cmd == ELS_CMD_PRLI || cmd == ELS_CMD_NVMEPRLI) {
3742                                 delay = 1000;
3743                                 maxretry = lpfc_max_els_tries + 1;
3744                                 retry = 1;
3745                                 break;
3746                         }
3747 
3748                         /* Legacy bug fix code for targets with PLOGI delays. */
3749                         if (stat.un.b.lsRjtRsnCodeExp ==
3750                             LSEXP_CMD_IN_PROGRESS) {
3751                                 if (cmd == ELS_CMD_PLOGI) {
3752                                         delay = 1000;
3753                                         maxretry = 48;
3754                                 }
3755                                 retry = 1;
3756                                 break;
3757                         }
3758                         if (stat.un.b.lsRjtRsnCodeExp ==
3759                             LSEXP_CANT_GIVE_DATA) {
3760                                 if (cmd == ELS_CMD_PLOGI) {
3761                                         delay = 1000;
3762                                         maxretry = 48;
3763                                 }
3764                                 retry = 1;
3765                                 break;
3766                         }
3767                         if (cmd == ELS_CMD_PLOGI) {
3768                                 delay = 1000;
3769                                 maxretry = lpfc_max_els_tries + 1;
3770                                 retry = 1;
3771                                 break;
3772                         }
3773                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3774                           (cmd == ELS_CMD_FDISC) &&
3775                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
3776                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3777                                                  "0125 FDISC Failed (x%x). "
3778                                                  "Fabric out of resources\n",
3779                                                  stat.un.lsRjtError);
3780                                 lpfc_vport_set_state(vport,
3781                                                      FC_VPORT_NO_FABRIC_RSCS);
3782                         }
3783                         break;
3784 
3785                 case LSRJT_LOGICAL_BSY:
3786                         if ((cmd == ELS_CMD_PLOGI) ||
3787                             (cmd == ELS_CMD_PRLI) ||
3788                             (cmd == ELS_CMD_NVMEPRLI)) {
3789                                 delay = 1000;
3790                                 maxretry = 48;
3791                         } else if (cmd == ELS_CMD_FDISC) {
3792                                 /* FDISC retry policy */
3793                                 maxretry = 48;
3794                                 if (cmdiocb->retry >= 32)
3795                                         delay = 1000;
3796                         }
3797                         retry = 1;
3798                         break;
3799 
3800                 case LSRJT_LOGICAL_ERR:
3801                         /* There are some cases where switches return this
3802                          * error when they are not ready and should be returning
3803                          * Logical Busy. We should delay every time.
3804                          */
3805                         if (cmd == ELS_CMD_FDISC &&
3806                             stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
3807                                 maxretry = 3;
3808                                 delay = 1000;
3809                                 retry = 1;
3810                         } else if (cmd == ELS_CMD_FLOGI &&
3811                                    stat.un.b.lsRjtRsnCodeExp ==
3812                                                 LSEXP_NOTHING_MORE) {
3813                                 vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
3814                                 retry = 1;
3815                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3816                                                  "0820 FLOGI Failed (x%x). "
3817                                                  "BBCredit Not Supported\n",
3818                                                  stat.un.lsRjtError);
3819                         }
3820                         break;
3821 
3822                 case LSRJT_PROTOCOL_ERR:
3823                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3824                           (cmd == ELS_CMD_FDISC) &&
3825                           ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
3826                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
3827                           ) {
3828                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3829                                                  "0122 FDISC Failed (x%x). "
3830                                                  "Fabric Detected Bad WWN\n",
3831                                                  stat.un.lsRjtError);
3832                                 lpfc_vport_set_state(vport,
3833                                                      FC_VPORT_FABRIC_REJ_WWN);
3834                         }
3835                         break;
3836                 case LSRJT_VENDOR_UNIQUE:
3837                         if ((stat.un.b.vendorUnique == 0x45) &&
3838                             (cmd == ELS_CMD_FLOGI)) {
3839                                 goto out_retry;
3840                         }
3841                         break;
3842                 case LSRJT_CMD_UNSUPPORTED:
3843                         /* lpfc nvmet returns this type of LS_RJT when it
3844                          * receives an FCP PRLI because lpfc nvmet only
3845                          * support NVME.  ELS request is terminated for FCP4
3846                          * on this rport.
3847                          */
3848                         if (stat.un.b.lsRjtRsnCodeExp ==
3849                             LSEXP_REQ_UNSUPPORTED && cmd == ELS_CMD_PRLI) {
3850                                 spin_lock_irq(shost->host_lock);
3851                                 ndlp->nlp_flag |= NLP_FCP_PRLI_RJT;
3852                                 spin_unlock_irq(shost->host_lock);
3853                                 retry = 0;
3854                                 goto out_retry;
3855                         }
3856                         break;
3857                 }
3858                 break;
3859 
3860         case IOSTAT_INTERMED_RSP:
3861         case IOSTAT_BA_RJT:
3862                 break;
3863 
3864         default:
3865                 break;
3866         }
3867 
3868         if (link_reset) {
3869                 rc = lpfc_link_reset(vport);
3870                 if (rc) {
3871                         /* Do not give up. Retry PLOGI one more time and attempt
3872                          * link reset if PLOGI fails again.
3873                          */
3874                         retry = 1;
3875                         delay = 100;
3876                         goto out_retry;
3877                 }
3878                 return 1;
3879         }
3880 
3881         if (did == FDMI_DID)
3882                 retry = 1;
3883 
3884         if ((cmd == ELS_CMD_FLOGI) &&
3885             (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
3886             !lpfc_error_lost_link(irsp)) {
3887                 /* FLOGI retry policy */
3888                 retry = 1;
3889                 /* retry FLOGI forever */
3890                 if (phba->link_flag != LS_LOOPBACK_MODE)
3891                         maxretry = 0;
3892                 else
3893                         maxretry = 2;
3894 
3895                 if (cmdiocb->retry >= 100)
3896                         delay = 5000;
3897                 else if (cmdiocb->retry >= 32)
3898                         delay = 1000;
3899         } else if ((cmd == ELS_CMD_FDISC) && !lpfc_error_lost_link(irsp)) {
3900                 /* retry FDISCs every second up to devloss */
3901                 retry = 1;
3902                 maxretry = vport->cfg_devloss_tmo;
3903                 delay = 1000;
3904         }
3905 
3906         cmdiocb->retry++;
3907         if (maxretry && (cmdiocb->retry >= maxretry)) {
3908                 phba->fc_stat.elsRetryExceeded++;
3909                 retry = 0;
3910         }
3911 
3912         if ((vport->load_flag & FC_UNLOADING) != 0)
3913                 retry = 0;
3914 
3915 out_retry:
3916         if (retry) {
3917                 if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
3918                         /* Stop retrying PLOGI and FDISC if in FCF discovery */
3919                         if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3920                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3921                                                  "2849 Stop retry ELS command "
3922                                                  "x%x to remote NPORT x%x, "
3923                                                  "Data: x%x x%x\n", cmd, did,
3924                                                  cmdiocb->retry, delay);
3925                                 return 0;
3926                         }
3927                 }
3928 
3929                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
3930                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3931                                  "0107 Retry ELS command x%x to remote "
3932                                  "NPORT x%x Data: x%x x%x\n",
3933                                  cmd, did, cmdiocb->retry, delay);
3934 
3935                 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
3936                         ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
3937                         ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
3938                         IOERR_NO_RESOURCES))) {
3939                         /* Don't reset timer for no resources */
3940 
3941                         /* If discovery / RSCN timer is running, reset it */
3942                         if (timer_pending(&vport->fc_disctmo) ||
3943                             (vport->fc_flag & FC_RSCN_MODE))
3944                                 lpfc_set_disctmo(vport);
3945                 }
3946 
3947                 phba->fc_stat.elsXmitRetry++;
3948                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
3949                         phba->fc_stat.elsDelayRetry++;
3950                         ndlp->nlp_retry = cmdiocb->retry;
3951 
3952                         /* delay is specified in milliseconds */
3953                         mod_timer(&ndlp->nlp_delayfunc,
3954                                 jiffies + msecs_to_jiffies(delay));
3955                         spin_lock_irq(shost->host_lock);
3956                         ndlp->nlp_flag |= NLP_DELAY_TMO;
3957                         spin_unlock_irq(shost->host_lock);
3958 
3959                         ndlp->nlp_prev_state = ndlp->nlp_state;
3960                         if ((cmd == ELS_CMD_PRLI) ||
3961                             (cmd == ELS_CMD_NVMEPRLI))
3962                                 lpfc_nlp_set_state(vport, ndlp,
3963                                         NLP_STE_PRLI_ISSUE);
3964                         else
3965                                 lpfc_nlp_set_state(vport, ndlp,
3966                                         NLP_STE_NPR_NODE);
3967                         ndlp->nlp_last_elscmd = cmd;
3968 
3969                         return 1;
3970                 }
3971                 switch (cmd) {
3972                 case ELS_CMD_FLOGI:
3973                         lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
3974                         return 1;
3975                 case ELS_CMD_FDISC:
3976                         lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
3977                         return 1;
3978                 case ELS_CMD_PLOGI:
3979                         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3980                                 ndlp->nlp_prev_state = ndlp->nlp_state;
3981                                 lpfc_nlp_set_state(vport, ndlp,
3982                                                    NLP_STE_PLOGI_ISSUE);
3983                         }
3984                         lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
3985                         return 1;
3986                 case ELS_CMD_ADISC:
3987                         ndlp->nlp_prev_state = ndlp->nlp_state;
3988                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3989                         lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
3990                         return 1;
3991                 case ELS_CMD_PRLI:
3992                 case ELS_CMD_NVMEPRLI:
3993                         ndlp->nlp_prev_state = ndlp->nlp_state;
3994                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3995                         lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
3996                         return 1;
3997                 case ELS_CMD_LOGO:
3998                         ndlp->nlp_prev_state = ndlp->nlp_state;
3999                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
4000                         lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
4001                         return 1;
4002                 }
4003         }
4004         /* No retry ELS command <elsCmd> to remote NPORT <did> */
4005         if (logerr) {
4006                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4007                          "0137 No retry ELS command x%x to remote "
4008                          "NPORT x%x: Out of Resources: Error:x%x/%x\n",
4009                          cmd, did, irsp->ulpStatus,
4010                          irsp->un.ulpWord[4]);
4011         }
4012         else {
4013                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4014                          "0108 No retry ELS command x%x to remote "
4015                          "NPORT x%x Retried:%d Error:x%x/%x\n",
4016                          cmd, did, cmdiocb->retry, irsp->ulpStatus,
4017                          irsp->un.ulpWord[4]);
4018         }
4019         return 0;
4020 }
4021 
4022 /**
4023  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
4024  * @phba: pointer to lpfc hba data structure.
4025  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
4026  *
4027  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
4028  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
4029  * checks to see whether there is a lpfc DMA buffer associated with the
4030  * response of the command IOCB. If so, it will be released before releasing
4031  * the lpfc DMA buffer associated with the IOCB itself.
4032  *
4033  * Return code
4034  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
4035  **/
4036 static int
4037 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
4038 {
4039         struct lpfc_dmabuf *buf_ptr;
4040 
4041         /* Free the response before processing the command. */
4042         if (!list_empty(&buf_ptr1->list)) {
4043                 list_remove_head(&buf_ptr1->list, buf_ptr,
4044                                  struct lpfc_dmabuf,
4045                                  list);
4046                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4047                 kfree(buf_ptr);
4048         }
4049         lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
4050         kfree(buf_ptr1);
4051         return 0;
4052 }
4053 
4054 /**
4055  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
4056  * @phba: pointer to lpfc hba data structure.
4057  * @buf_ptr: pointer to the lpfc dma buffer data structure.
4058  *
4059  * This routine releases the lpfc Direct Memory Access (DMA) buffer
4060  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
4061  * pool.
4062  *
4063  * Return code
4064  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
4065  **/
4066 static int
4067 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
4068 {
4069         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4070         kfree(buf_ptr);
4071         return 0;
4072 }
4073 
4074 /**
4075  * lpfc_els_free_iocb - Free a command iocb and its associated resources
4076  * @phba: pointer to lpfc hba data structure.
4077  * @elsiocb: pointer to lpfc els command iocb data structure.
4078  *
4079  * This routine frees a command IOCB and its associated resources. The
4080  * command IOCB data structure contains the reference to various associated
4081  * resources, these fields must be set to NULL if the associated reference
4082  * not present:
4083  *   context1 - reference to ndlp
4084  *   context2 - reference to cmd
4085  *   context2->next - reference to rsp
4086  *   context3 - reference to bpl
4087  *
4088  * It first properly decrements the reference count held on ndlp for the
4089  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
4090  * set, it invokes the lpfc_els_free_data() routine to release the Direct
4091  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
4092  * adds the DMA buffer the @phba data structure for the delayed release.
4093  * If reference to the Buffer Pointer List (BPL) is present, the
4094  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
4095  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
4096  * invoked to release the IOCB data structure back to @phba IOCBQ list.
4097  *
4098  * Return code
4099  *   0 - Success (currently, always return 0)
4100  **/
4101 int
4102 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
4103 {
4104         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
4105         struct lpfc_nodelist *ndlp;
4106 
4107         ndlp = (struct lpfc_nodelist *)elsiocb->context1;
4108         if (ndlp) {
4109                 if (ndlp->nlp_flag & NLP_DEFER_RM) {
4110                         lpfc_nlp_put(ndlp);
4111 
4112                         /* If the ndlp is not being used by another discovery
4113                          * thread, free it.
4114                          */
4115                         if (!lpfc_nlp_not_used(ndlp)) {
4116                                 /* If ndlp is being used by another discovery
4117                                  * thread, just clear NLP_DEFER_RM
4118                                  */
4119                                 ndlp->nlp_flag &= ~NLP_DEFER_RM;
4120                         }
4121                 }
4122                 else
4123                         lpfc_nlp_put(ndlp);
4124                 elsiocb->context1 = NULL;
4125         }
4126         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
4127         if (elsiocb->context2) {
4128                 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
4129                         /* Firmware could still be in progress of DMAing
4130                          * payload, so don't free data buffer till after
4131                          * a hbeat.
4132                          */
4133                         elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
4134                         buf_ptr = elsiocb->context2;
4135                         elsiocb->context2 = NULL;
4136                         if (buf_ptr) {
4137                                 buf_ptr1 = NULL;
4138                                 spin_lock_irq(&phba->hbalock);
4139                                 if (!list_empty(&buf_ptr->list)) {
4140                                         list_remove_head(&buf_ptr->list,
4141                                                 buf_ptr1, struct lpfc_dmabuf,
4142                                                 list);
4143                                         INIT_LIST_HEAD(&buf_ptr1->list);
4144                                         list_add_tail(&buf_ptr1->list,
4145                                                 &phba->elsbuf);
4146                                         phba->elsbuf_cnt++;
4147                                 }
4148                                 INIT_LIST_HEAD(&buf_ptr->list);
4149                                 list_add_tail(&buf_ptr->list, &phba->elsbuf);
4150                                 phba->elsbuf_cnt++;
4151                                 spin_unlock_irq(&phba->hbalock);
4152                         }
4153                 } else {
4154                         buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
4155                         lpfc_els_free_data(phba, buf_ptr1);
4156                         elsiocb->context2 = NULL;
4157                 }
4158         }
4159 
4160         if (elsiocb->context3) {
4161                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
4162                 lpfc_els_free_bpl(phba, buf_ptr);
4163                 elsiocb->context3 = NULL;
4164         }
4165         lpfc_sli_release_iocbq(phba, elsiocb);
4166         return 0;
4167 }
4168 
4169 /**
4170  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
4171  * @phba: pointer to lpfc hba data structure.
4172  * @cmdiocb: pointer to lpfc command iocb data structure.
4173  * @rspiocb: pointer to lpfc response iocb data structure.
4174  *
4175  * This routine is the completion callback function to the Logout (LOGO)
4176  * Accept (ACC) Response ELS command. This routine is invoked to indicate
4177  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
4178  * release the ndlp if it has the last reference remaining (reference count
4179  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
4180  * field to NULL to inform the following lpfc_els_free_iocb() routine no
4181  * ndlp reference count needs to be decremented. Otherwise, the ndlp
4182  * reference use-count shall be decremented by the lpfc_els_free_iocb()
4183  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
4184  * IOCB data structure.
4185  **/
4186 static void
4187 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4188                        struct lpfc_iocbq *rspiocb)
4189 {
4190         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4191         struct lpfc_vport *vport = cmdiocb->vport;
4192         IOCB_t *irsp;
4193 
4194         irsp = &rspiocb->iocb;
4195         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4196                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
4197                 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
4198         /* ACC to LOGO completes to NPort <nlp_DID> */
4199         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4200                          "0109 ACC to LOGO completes to NPort x%x "
4201                          "Data: x%x x%x x%x\n",
4202                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4203                          ndlp->nlp_rpi);
4204 
4205         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
4206                 /* NPort Recovery mode or node is just allocated */
4207                 if (!lpfc_nlp_not_used(ndlp)) {
4208                         /* If the ndlp is being used by another discovery
4209                          * thread, just unregister the RPI.
4210                          */
4211                         lpfc_unreg_rpi(vport, ndlp);
4212                 } else {
4213                         /* Indicate the node has already released, should
4214                          * not reference to it from within lpfc_els_free_iocb.
4215                          */
4216                         cmdiocb->context1 = NULL;
4217                 }
4218         }
4219 
4220         /*
4221          * The driver received a LOGO from the rport and has ACK'd it.
4222          * At this point, the driver is done so release the IOCB
4223          */
4224         lpfc_els_free_iocb(phba, cmdiocb);
4225 }
4226 
4227 /**
4228  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
4229  * @phba: pointer to lpfc hba data structure.
4230  * @pmb: pointer to the driver internal queue element for mailbox command.
4231  *
4232  * This routine is the completion callback function for unregister default
4233  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
4234  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
4235  * decrements the ndlp reference count held for this completion callback
4236  * function. After that, it invokes the lpfc_nlp_not_used() to check
4237  * whether there is only one reference left on the ndlp. If so, it will
4238  * perform one more decrement and trigger the release of the ndlp.
4239  **/
4240 void
4241 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4242 {
4243         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
4244         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4245 
4246         pmb->ctx_buf = NULL;
4247         pmb->ctx_ndlp = NULL;
4248 
4249         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4250         kfree(mp);
4251         mempool_free(pmb, phba->mbox_mem_pool);
4252         if (ndlp) {
4253                 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4254                                  "0006 rpi%x DID:%x flg:%x %d map:%x x%px\n",
4255                                  ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4256                                  kref_read(&ndlp->kref),
4257                                  ndlp->nlp_usg_map, ndlp);
4258                 if (NLP_CHK_NODE_ACT(ndlp)) {
4259                         lpfc_nlp_put(ndlp);
4260                         /* This is the end of the default RPI cleanup logic for
4261                          * this ndlp. If no other discovery threads are using
4262                          * this ndlp, free all resources associated with it.
4263                          */
4264                         lpfc_nlp_not_used(ndlp);
4265                 } else {
4266                         lpfc_drop_node(ndlp->vport, ndlp);
4267                 }
4268         }
4269 
4270         return;
4271 }
4272 
4273 /**
4274  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
4275  * @phba: pointer to lpfc hba data structure.
4276  * @cmdiocb: pointer to lpfc command iocb data structure.
4277  * @rspiocb: pointer to lpfc response iocb data structure.
4278  *
4279  * This routine is the completion callback function for ELS Response IOCB
4280  * command. In normal case, this callback function just properly sets the
4281  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
4282  * field in the command IOCB is not NULL, the referred mailbox command will
4283  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
4284  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
4285  * link down event occurred during the discovery, the lpfc_nlp_not_used()
4286  * routine shall be invoked trying to release the ndlp if no other threads
4287  * are currently referring it.
4288  **/
4289 static void
4290 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4291                   struct lpfc_iocbq *rspiocb)
4292 {
4293         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4294         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
4295         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
4296         IOCB_t  *irsp;
4297         uint8_t *pcmd;
4298         LPFC_MBOXQ_t *mbox = NULL;
4299         struct lpfc_dmabuf *mp = NULL;
4300         uint32_t ls_rjt = 0;
4301 
4302         irsp = &rspiocb->iocb;
4303 
4304         if (cmdiocb->context_un.mbox)
4305                 mbox = cmdiocb->context_un.mbox;
4306 
4307         /* First determine if this is a LS_RJT cmpl. Note, this callback
4308          * function can have cmdiocb->contest1 (ndlp) field set to NULL.
4309          */
4310         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
4311         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
4312             (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
4313                 /* A LS_RJT associated with Default RPI cleanup has its own
4314                  * separate code path.
4315                  */
4316                 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
4317                         ls_rjt = 1;
4318         }
4319 
4320         /* Check to see if link went down during discovery */
4321         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
4322                 if (mbox) {
4323                         mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
4324                         if (mp) {
4325                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4326                                 kfree(mp);
4327                         }
4328                         mempool_free(mbox, phba->mbox_mem_pool);
4329                 }
4330                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
4331                     (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
4332                         if (lpfc_nlp_not_used(ndlp)) {
4333                                 ndlp = NULL;
4334                                 /* Indicate the node has already released,
4335                                  * should not reference to it from within
4336                                  * the routine lpfc_els_free_iocb.
4337                                  */
4338                                 cmdiocb->context1 = NULL;
4339                         }
4340                 goto out;
4341         }
4342 
4343         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4344                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
4345                 irsp->ulpStatus, irsp->un.ulpWord[4],
4346                 cmdiocb->iocb.un.elsreq64.remoteID);
4347         /* ELS response tag <ulpIoTag> completes */
4348         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4349                          "0110 ELS response tag x%x completes "
4350                          "Data: x%x x%x x%x x%x x%x x%x x%x\n",
4351                          cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
4352                          rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
4353                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4354                          ndlp->nlp_rpi);
4355         if (mbox) {
4356                 if ((rspiocb->iocb.ulpStatus == 0)
4357                     && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
4358                         if (!lpfc_unreg_rpi(vport, ndlp) &&
4359                             (!(vport->fc_flag & FC_PT2PT)) &&
4360                             (ndlp->nlp_state ==  NLP_STE_PLOGI_ISSUE ||
4361                              ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE)) {
4362                                 lpfc_printf_vlog(vport, KERN_INFO,
4363                                         LOG_DISCOVERY,
4364                                         "0314 PLOGI recov DID x%x "
4365                                         "Data: x%x x%x x%x\n",
4366                                         ndlp->nlp_DID, ndlp->nlp_state,
4367                                         ndlp->nlp_rpi, ndlp->nlp_flag);
4368                                 mp = mbox->ctx_buf;
4369                                 if (mp) {
4370                                         lpfc_mbuf_free(phba, mp->virt,
4371                                                        mp->phys);
4372                                         kfree(mp);
4373                                 }
4374                                 mempool_free(mbox, phba->mbox_mem_pool);
4375                                 goto out;
4376                         }
4377 
4378                         /* Increment reference count to ndlp to hold the
4379                          * reference to ndlp for the callback function.
4380                          */
4381                         mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
4382                         mbox->vport = vport;
4383                         if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
4384                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4385                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4386                         }
4387                         else {
4388                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
4389                                 ndlp->nlp_prev_state = ndlp->nlp_state;
4390                                 lpfc_nlp_set_state(vport, ndlp,
4391                                            NLP_STE_REG_LOGIN_ISSUE);
4392                         }
4393 
4394                         ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
4395                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4396                             != MBX_NOT_FINISHED)
4397                                 goto out;
4398 
4399                         /* Decrement the ndlp reference count we
4400                          * set for this failed mailbox command.
4401                          */
4402                         lpfc_nlp_put(ndlp);
4403                         ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
4404 
4405                         /* ELS rsp: Cannot issue reg_login for <NPortid> */
4406                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4407                                 "0138 ELS rsp: Cannot issue reg_login for x%x "
4408                                 "Data: x%x x%x x%x\n",
4409                                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4410                                 ndlp->nlp_rpi);
4411 
4412                         if (lpfc_nlp_not_used(ndlp)) {
4413                                 ndlp = NULL;
4414                                 /* Indicate node has already been released,
4415                                  * should not reference to it from within
4416                                  * the routine lpfc_els_free_iocb.
4417                                  */
4418                                 cmdiocb->context1 = NULL;
4419                         }
4420                 } else {
4421                         /* Do not drop node for lpfc_els_abort'ed ELS cmds */
4422                         if (!lpfc_error_lost_link(irsp) &&
4423                             ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
4424                                 if (lpfc_nlp_not_used(ndlp)) {
4425                                         ndlp = NULL;
4426                                         /* Indicate node has already been
4427                                          * released, should not reference
4428                                          * to it from within the routine
4429                                          * lpfc_els_free_iocb.
4430                                          */
4431                                         cmdiocb->context1 = NULL;
4432                                 }
4433                         }
4434                 }
4435                 mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
4436                 if (mp) {
4437                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4438                         kfree(mp);
4439                 }
4440                 mempool_free(mbox, phba->mbox_mem_pool);
4441         }
4442 out:
4443         if (ndlp && NLP_CHK_NODE_ACT(ndlp) && shost) {
4444                 spin_lock_irq(shost->host_lock);
4445                 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
4446                 spin_unlock_irq(shost->host_lock);
4447 
4448                 /* If the node is not being used by another discovery thread,
4449                  * and we are sending a reject, we are done with it.
4450                  * Release driver reference count here and free associated
4451                  * resources.
4452                  */
4453                 if (ls_rjt)
4454                         if (lpfc_nlp_not_used(ndlp))
4455                                 /* Indicate node has already been released,
4456                                  * should not reference to it from within
4457                                  * the routine lpfc_els_free_iocb.
4458                                  */
4459                                 cmdiocb->context1 = NULL;
4460 
4461         }
4462 
4463         lpfc_els_free_iocb(phba, cmdiocb);
4464         return;
4465 }
4466 
4467 /**
4468  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
4469  * @vport: pointer to a host virtual N_Port data structure.
4470  * @flag: the els command code to be accepted.
4471  * @oldiocb: pointer to the original lpfc command iocb data structure.
4472  * @ndlp: pointer to a node-list data structure.
4473  * @mbox: pointer to the driver internal queue element for mailbox command.
4474  *
4475  * This routine prepares and issues an Accept (ACC) response IOCB
4476  * command. It uses the @flag to properly set up the IOCB field for the
4477  * specific ACC response command to be issued and invokes the
4478  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
4479  * @mbox pointer is passed in, it will be put into the context_un.mbox
4480  * field of the IOCB for the completion callback function to issue the
4481  * mailbox command to the HBA later when callback is invoked.
4482  *
4483  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4484  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4485  * will be stored into the context1 field of the IOCB for the completion
4486  * callback function to the corresponding response ELS IOCB command.
4487  *
4488  * Return code
4489  *   0 - Successfully issued acc response
4490  *   1 - Failed to issue acc response
4491  **/
4492 int
4493 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
4494                  struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
4495                  LPFC_MBOXQ_t *mbox)
4496 {
4497         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4498         struct lpfc_hba  *phba = vport->phba;
4499         IOCB_t *icmd;
4500         IOCB_t *oldcmd;
4501         struct lpfc_iocbq *elsiocb;
4502         uint8_t *pcmd;
4503         struct serv_parm *sp;
4504         uint16_t cmdsize;
4505         int rc;
4506         ELS_PKT *els_pkt_ptr;
4507 
4508         oldcmd = &oldiocb->iocb;
4509 
4510         switch (flag) {
4511         case ELS_CMD_ACC:
4512                 cmdsize = sizeof(uint32_t);
4513                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4514                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
4515                 if (!elsiocb) {
4516                         spin_lock_irq(shost->host_lock);
4517                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4518                         spin_unlock_irq(shost->host_lock);
4519                         return 1;
4520                 }
4521 
4522                 icmd = &elsiocb->iocb;
4523                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4524                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4525                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4526                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4527                 pcmd += sizeof(uint32_t);
4528 
4529                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4530                         "Issue ACC:       did:x%x flg:x%x",
4531                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
4532                 break;
4533         case ELS_CMD_FLOGI:
4534         case ELS_CMD_PLOGI:
4535                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
4536                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4537                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
4538                 if (!elsiocb)
4539                         return 1;
4540 
4541                 icmd = &elsiocb->iocb;
4542                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4543                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4544                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4545 
4546                 if (mbox)
4547                         elsiocb->context_un.mbox = mbox;
4548 
4549                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4550                 pcmd += sizeof(uint32_t);
4551                 sp = (struct serv_parm *)pcmd;
4552 
4553                 if (flag == ELS_CMD_FLOGI) {
4554                         /* Copy the received service parameters back */
4555                         memcpy(sp, &phba->fc_fabparam,
4556                                sizeof(struct serv_parm));
4557 
4558                         /* Clear the F_Port bit */
4559                         sp->cmn.fPort = 0;
4560 
4561                         /* Mark all class service parameters as invalid */
4562                         sp->cls1.classValid = 0;
4563                         sp->cls2.classValid = 0;
4564                         sp->cls3.classValid = 0;
4565                         sp->cls4.classValid = 0;
4566 
4567                         /* Copy our worldwide names */
4568                         memcpy(&sp->portName, &vport->fc_sparam.portName,
4569                                sizeof(struct lpfc_name));
4570                         memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
4571                                sizeof(struct lpfc_name));
4572                 } else {
4573                         memcpy(pcmd, &vport->fc_sparam,
4574                                sizeof(struct serv_parm));
4575 
4576                         sp->cmn.valid_vendor_ver_level = 0;
4577                         memset(sp->un.vendorVersion, 0,
4578                                sizeof(sp->un.vendorVersion));
4579                         sp->cmn.bbRcvSizeMsb &= 0xF;
4580 
4581                         /* If our firmware supports this feature, convey that
4582                          * info to the target using the vendor specific field.
4583                          */
4584                         if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
4585                                 sp->cmn.valid_vendor_ver_level = 1;
4586                                 sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
4587                                 sp->un.vv.flags =
4588                                         cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
4589                         }
4590                 }
4591 
4592                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4593                         "Issue ACC FLOGI/PLOGI: did:x%x flg:x%x",
4594                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
4595                 break;
4596         case ELS_CMD_PRLO:
4597                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
4598                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4599                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
4600                 if (!elsiocb)
4601                         return 1;
4602 
4603                 icmd = &elsiocb->iocb;
4604                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4605                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4606                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4607 
4608                 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
4609                        sizeof(uint32_t) + sizeof(PRLO));
4610                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
4611                 els_pkt_ptr = (ELS_PKT *) pcmd;
4612                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
4613 
4614                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4615                         "Issue ACC PRLO:  did:x%x flg:x%x",
4616                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
4617                 break;
4618         default:
4619                 return 1;
4620         }
4621         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
4622                 spin_lock_irq(shost->host_lock);
4623                 if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4624                         ndlp->nlp_flag & NLP_REG_LOGIN_SEND))
4625                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4626                 spin_unlock_irq(shost->host_lock);
4627                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
4628         } else {
4629                 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4630         }
4631 
4632         phba->fc_stat.elsXmitACC++;
4633         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4634         if (rc == IOCB_ERROR) {
4635                 lpfc_els_free_iocb(phba, elsiocb);
4636                 return 1;
4637         }
4638         return 0;
4639 }
4640 
4641 /**
4642  * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
4643  * @vport: pointer to a virtual N_Port data structure.
4644  * @rejectError:
4645  * @oldiocb: pointer to the original lpfc command iocb data structure.
4646  * @ndlp: pointer to a node-list data structure.
4647  * @mbox: pointer to the driver internal queue element for mailbox command.
4648  *
4649  * This routine prepares and issue an Reject (RJT) response IOCB
4650  * command. If a @mbox pointer is passed in, it will be put into the
4651  * context_un.mbox field of the IOCB for the completion callback function
4652  * to issue to the HBA later.
4653  *
4654  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4655  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4656  * will be stored into the context1 field of the IOCB for the completion
4657  * callback function to the reject response ELS IOCB command.
4658  *
4659  * Return code
4660  *   0 - Successfully issued reject response
4661  *   1 - Failed to issue reject response
4662  **/
4663 int
4664 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
4665                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
4666                     LPFC_MBOXQ_t *mbox)
4667 {
4668         struct lpfc_hba  *phba = vport->phba;
4669         IOCB_t *icmd;
4670         IOCB_t *oldcmd;
4671         struct lpfc_iocbq *elsiocb;
4672         uint8_t *pcmd;
4673         uint16_t cmdsize;
4674         int rc;
4675 
4676         cmdsize = 2 * sizeof(uint32_t);
4677         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4678                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
4679         if (!elsiocb)
4680                 return 1;
4681 
4682         icmd = &elsiocb->iocb;
4683         oldcmd = &oldiocb->iocb;
4684         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4685         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4686         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4687 
4688         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
4689         pcmd += sizeof(uint32_t);
4690         *((uint32_t *) (pcmd)) = rejectError;
4691 
4692         if (mbox)
4693                 elsiocb->context_un.mbox = mbox;
4694 
4695         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
4696         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4697                          "0129 Xmit ELS RJT x%x response tag x%x "
4698                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4699                          "rpi x%x\n",
4700                          rejectError, elsiocb->iotag,
4701                          elsiocb->iocb.ulpContext, ndlp->nlp_DID,
4702                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
4703         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4704                 "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
4705                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
4706 
4707         phba->fc_stat.elsXmitLSRJT++;
4708         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4709         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4710 
4711         if (rc == IOCB_ERROR) {
4712                 lpfc_els_free_iocb(phba, elsiocb);
4713                 return 1;
4714         }
4715         return 0;
4716 }
4717 
4718 /**
4719  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
4720  * @vport: pointer to a virtual N_Port data structure.
4721  * @oldiocb: pointer to the original lpfc command iocb data structure.
4722  * @ndlp: pointer to a node-list data structure.
4723  *
4724  * This routine prepares and issues an Accept (ACC) response to Address
4725  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
4726  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4727  *
4728  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4729  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4730  * will be stored into the context1 field of the IOCB for the completion
4731  * callback function to the ADISC Accept response ELS IOCB command.
4732  *
4733  * Return code
4734  *   0 - Successfully issued acc adisc response
4735  *   1 - Failed to issue adisc acc response
4736  **/
4737 int
4738 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
4739                        struct lpfc_nodelist *ndlp)
4740 {
4741         struct lpfc_hba  *phba = vport->phba;
4742         ADISC *ap;
4743         IOCB_t *icmd, *oldcmd;
4744         struct lpfc_iocbq *elsiocb;
4745         uint8_t *pcmd;
4746         uint16_t cmdsize;
4747         int rc;
4748 
4749         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
4750         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4751                                      ndlp->nlp_DID, ELS_CMD_ACC);
4752         if (!elsiocb)
4753                 return 1;
4754 
4755         icmd = &elsiocb->iocb;
4756         oldcmd = &oldiocb->iocb;
4757         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4758         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4759 
4760         /* Xmit ADISC ACC response tag <ulpIoTag> */
4761         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4762                          "0130 Xmit ADISC ACC response iotag x%x xri: "
4763                          "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
4764                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4765                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4766                          ndlp->nlp_rpi);
4767         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4768 
4769         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4770         pcmd += sizeof(uint32_t);
4771 
4772         ap = (ADISC *) (pcmd);
4773         ap->hardAL_PA = phba->fc_pref_ALPA;
4774         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4775         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
4776         ap->DID = be32_to_cpu(vport->fc_myDID);
4777 
4778         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4779                 "Issue ACC ADISC: did:x%x flg:x%x",
4780                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4781 
4782         phba->fc_stat.elsXmitACC++;
4783         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4784         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4785         if (rc == IOCB_ERROR) {
4786                 lpfc_els_free_iocb(phba, elsiocb);
4787                 return 1;
4788         }
4789 
4790         /* Xmit ELS ACC response tag <ulpIoTag> */
4791         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4792                          "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
4793                          "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
4794                          "RPI: x%x, fc_flag x%x\n",
4795                          rc, elsiocb->iotag, elsiocb->sli4_xritag,
4796                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4797                          ndlp->nlp_rpi, vport->fc_flag);
4798         return 0;
4799 }
4800 
4801 /**
4802  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
4803  * @vport: pointer to a virtual N_Port data structure.
4804  * @oldiocb: pointer to the original lpfc command iocb data structure.
4805  * @ndlp: pointer to a node-list data structure.
4806  *
4807  * This routine prepares and issues an Accept (ACC) response to Process
4808  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
4809  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4810  *
4811  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4812  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4813  * will be stored into the context1 field of the IOCB for the completion
4814  * callback function to the PRLI Accept response ELS IOCB command.
4815  *
4816  * Return code
4817  *   0 - Successfully issued acc prli response
4818  *   1 - Failed to issue acc prli response
4819  **/
4820 int
4821 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
4822                       struct lpfc_nodelist *ndlp)
4823 {
4824         struct lpfc_hba  *phba = vport->phba;
4825         PRLI *npr;
4826         struct lpfc_nvme_prli *npr_nvme;
4827         lpfc_vpd_t *vpd;
4828         IOCB_t *icmd;
4829         IOCB_t *oldcmd;
4830         struct lpfc_iocbq *elsiocb;
4831         uint8_t *pcmd;
4832         uint16_t cmdsize;
4833         uint32_t prli_fc4_req, *req_payload;
4834         struct lpfc_dmabuf *req_buf;
4835         int rc;
4836         u32 elsrspcmd;
4837 
4838         /* Need the incoming PRLI payload to determine if the ACC is for an
4839          * FC4 or NVME PRLI type.  The PRLI type is at word 1.
4840          */
4841         req_buf = (struct lpfc_dmabuf *)oldiocb->context2;
4842         req_payload = (((uint32_t *)req_buf->virt) + 1);
4843 
4844         /* PRLI type payload is at byte 3 for FCP or NVME. */
4845         prli_fc4_req = be32_to_cpu(*req_payload);
4846         prli_fc4_req = (prli_fc4_req >> 24) & 0xff;
4847         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4848                          "6127 PRLI_ACC:  Req Type x%x, Word1 x%08x\n",
4849                          prli_fc4_req, *((uint32_t *)req_payload));
4850 
4851         if (prli_fc4_req == PRLI_FCP_TYPE) {
4852                 cmdsize = sizeof(uint32_t) + sizeof(PRLI);
4853                 elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
4854         } else if (prli_fc4_req & PRLI_NVME_TYPE) {
4855                 cmdsize = sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli);
4856                 elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_NVMEPRLI & ~ELS_RSP_MASK));
4857         } else {
4858                 return 1;
4859         }
4860 
4861         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4862                 ndlp->nlp_DID, elsrspcmd);
4863         if (!elsiocb)
4864                 return 1;
4865 
4866         icmd = &elsiocb->iocb;
4867         oldcmd = &oldiocb->iocb;
4868         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4869         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4870 
4871         /* Xmit PRLI ACC response tag <ulpIoTag> */
4872         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4873                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
4874                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4875                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4876                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4877                          ndlp->nlp_rpi);
4878         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4879         memset(pcmd, 0, cmdsize);
4880 
4881         *((uint32_t *)(pcmd)) = elsrspcmd;
4882         pcmd += sizeof(uint32_t);
4883 
4884         /* For PRLI, remainder of payload is PRLI parameter page */
4885         vpd = &phba->vpd;
4886 
4887         if (prli_fc4_req == PRLI_FCP_TYPE) {
4888                 /*
4889                  * If the remote port is a target and our firmware version
4890                  * is 3.20 or later, set the following bits for FC-TAPE
4891                  * support.
4892                  */
4893                 npr = (PRLI *) pcmd;
4894                 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
4895                     (vpd->rev.feaLevelHigh >= 0x02)) {
4896                         npr->ConfmComplAllowed = 1;
4897                         npr->Retry = 1;
4898                         npr->TaskRetryIdReq = 1;
4899                 }
4900                 npr->acceptRspCode = PRLI_REQ_EXECUTED;
4901                 npr->estabImagePair = 1;
4902                 npr->readXferRdyDis = 1;
4903                 npr->ConfmComplAllowed = 1;
4904                 npr->prliType = PRLI_FCP_TYPE;
4905                 npr->initiatorFunc = 1;
4906         } else if (prli_fc4_req & PRLI_NVME_TYPE) {
4907                 /* Respond with an NVME PRLI Type */
4908                 npr_nvme = (struct lpfc_nvme_prli *) pcmd;
4909                 bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
4910                 bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
4911                 bf_set(prli_acc_rsp_code, npr_nvme, PRLI_REQ_EXECUTED);
4912                 if (phba->nvmet_support) {
4913                         bf_set(prli_tgt, npr_nvme, 1);
4914                         bf_set(prli_disc, npr_nvme, 1);
4915                         if (phba->cfg_nvme_enable_fb) {
4916                                 bf_set(prli_fba, npr_nvme, 1);
4917 
4918                                 /* TBD.  Target mode needs to post buffers
4919                                  * that support the configured first burst
4920                                  * byte size.
4921                                  */
4922                                 bf_set(prli_fb_sz, npr_nvme,
4923                                        phba->cfg_nvmet_fb_size);
4924                         }
4925                 } else {
4926                         bf_set(prli_init, npr_nvme, 1);
4927                 }
4928 
4929                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
4930                                  "6015 NVME issue PRLI ACC word1 x%08x "
4931                                  "word4 x%08x word5 x%08x flag x%x, "
4932                                  "fcp_info x%x nlp_type x%x\n",
4933                                  npr_nvme->word1, npr_nvme->word4,
4934                                  npr_nvme->word5, ndlp->nlp_flag,
4935                                  ndlp->nlp_fcp_info, ndlp->nlp_type);
4936                 npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
4937                 npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
4938                 npr_nvme->word5 = cpu_to_be32(npr_nvme->word5);
4939         } else
4940                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4941                                  "6128 Unknown FC_TYPE x%x x%x ndlp x%06x\n",
4942                                  prli_fc4_req, ndlp->nlp_fc4_type,
4943                                  ndlp->nlp_DID);
4944 
4945         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4946                 "Issue ACC PRLI:  did:x%x flg:x%x",
4947                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4948 
4949         phba->fc_stat.elsXmitACC++;
4950         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4951 
4952         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4953         if (rc == IOCB_ERROR) {
4954                 lpfc_els_free_iocb(phba, elsiocb);
4955                 return 1;
4956         }
4957         return 0;
4958 }
4959 
4960 /**
4961  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
4962  * @vport: pointer to a virtual N_Port data structure.
4963  * @format: rnid command format.
4964  * @oldiocb: pointer to the original lpfc command iocb data structure.
4965  * @ndlp: pointer to a node-list data structure.
4966  *
4967  * This routine issues a Request Node Identification Data (RNID) Accept
4968  * (ACC) response. It constructs the RNID ACC response command according to
4969  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
4970  * issue the response. Note that this command does not need to hold the ndlp
4971  * reference count for the callback. So, the ndlp reference count taken by
4972  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
4973  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
4974  * there is no ndlp reference available.
4975  *
4976  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4977  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4978  * will be stored into the context1 field of the IOCB for the completion
4979  * callback function. However, for the RNID Accept Response ELS command,
4980  * this is undone later by this routine after the IOCB is allocated.
4981  *
4982  * Return code
4983  *   0 - Successfully issued acc rnid response
4984  *   1 - Failed to issue acc rnid response
4985  **/
4986 static int
4987 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
4988                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4989 {
4990         struct lpfc_hba  *phba = vport->phba;
4991         RNID *rn;
4992         IOCB_t *icmd, *oldcmd;
4993         struct lpfc_iocbq *elsiocb;
4994         uint8_t *pcmd;
4995         uint16_t cmdsize;
4996         int rc;
4997 
4998         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
4999                                         + (2 * sizeof(struct lpfc_name));
5000         if (format)
5001                 cmdsize += sizeof(RNID_TOP_DISC);
5002 
5003         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5004                                      ndlp->nlp_DID, ELS_CMD_ACC);
5005         if (!elsiocb)
5006                 return 1;
5007 
5008         icmd = &elsiocb->iocb;
5009         oldcmd = &oldiocb->iocb;
5010         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
5011         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5012 
5013         /* Xmit RNID ACC response tag <ulpIoTag> */
5014         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5015                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
5016                          elsiocb->iotag, elsiocb->iocb.ulpContext);
5017         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5018         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5019         pcmd += sizeof(uint32_t);
5020 
5021         memset(pcmd, 0, sizeof(RNID));
5022         rn = (RNID *) (pcmd);
5023         rn->Format = format;
5024         rn->CommonLen = (2 * sizeof(struct lpfc_name));
5025         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
5026         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
5027         switch (format) {
5028         case 0:
5029                 rn->SpecificLen = 0;
5030                 break;
5031         case RNID_TOPOLOGY_DISC:
5032                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
5033                 memcpy(&rn->un.topologyDisc.portName,
5034                        &vport->fc_portname, sizeof(struct lpfc_name));
5035                 rn->un.topologyDisc.unitType = RNID_HBA;
5036                 rn->un.topologyDisc.physPort = 0;
5037                 rn->un.topologyDisc.attachedNodes = 0;
5038                 break;
5039         default:
5040                 rn->CommonLen = 0;
5041                 rn->SpecificLen = 0;
5042                 break;
5043         }
5044 
5045         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5046                 "Issue ACC RNID:  did:x%x flg:x%x",
5047                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
5048 
5049         phba->fc_stat.elsXmitACC++;
5050         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5051 
5052         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5053         if (rc == IOCB_ERROR) {
5054                 lpfc_els_free_iocb(phba, elsiocb);
5055                 return 1;
5056         }
5057         return 0;
5058 }
5059 
5060 /**
5061  * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
5062  * @vport: pointer to a virtual N_Port data structure.
5063  * @iocb: pointer to the lpfc command iocb data structure.
5064  * @ndlp: pointer to a node-list data structure.
5065  *
5066  * Return
5067  **/
5068 static void
5069 lpfc_els_clear_rrq(struct lpfc_vport *vport,
5070                    struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
5071 {
5072         struct lpfc_hba  *phba = vport->phba;
5073         uint8_t *pcmd;
5074         struct RRQ *rrq;
5075         uint16_t rxid;
5076         uint16_t xri;
5077         struct lpfc_node_rrq *prrq;
5078 
5079 
5080         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) iocb->context2)->virt);
5081         pcmd += sizeof(uint32_t);
5082         rrq = (struct RRQ *)pcmd;
5083         rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
5084         rxid = bf_get(rrq_rxid, rrq);
5085 
5086         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5087                         "2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
5088                         " x%x x%x\n",
5089                         be32_to_cpu(bf_get(rrq_did, rrq)),
5090                         bf_get(rrq_oxid, rrq),
5091                         rxid,
5092                         iocb->iotag, iocb->iocb.ulpContext);
5093 
5094         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5095                 "Clear RRQ:  did:x%x flg:x%x exchg:x%.08x",
5096                 ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
5097         if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
5098                 xri = bf_get(rrq_oxid, rrq);
5099         else
5100                 xri = rxid;
5101         prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
5102         if (prrq)
5103                 lpfc_clr_rrq_active(phba, xri, prrq);
5104         return;
5105 }
5106 
5107 /**
5108  * lpfc_els_rsp_echo_acc - Issue echo acc response
5109  * @vport: pointer to a virtual N_Port data structure.
5110  * @data: pointer to echo data to return in the accept.
5111  * @oldiocb: pointer to the original lpfc command iocb data structure.
5112  * @ndlp: pointer to a node-list data structure.
5113  *
5114  * Return code
5115  *   0 - Successfully issued acc echo response
5116  *   1 - Failed to issue acc echo response
5117  **/
5118 static int
5119 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
5120                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
5121 {
5122         struct lpfc_hba  *phba = vport->phba;
5123         struct lpfc_iocbq *elsiocb;
5124         uint8_t *pcmd;
5125         uint16_t cmdsize;
5126         int rc;
5127 
5128         cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
5129 
5130         /* The accumulated length can exceed the BPL_SIZE.  For
5131          * now, use this as the limit
5132          */
5133         if (cmdsize > LPFC_BPL_SIZE)
5134                 cmdsize = LPFC_BPL_SIZE;
5135         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5136                                      ndlp->nlp_DID, ELS_CMD_ACC);
5137         if (!elsiocb)
5138                 return 1;
5139 
5140         elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext;  /* Xri / rx_id */
5141         elsiocb->iocb.unsli3.rcvsli3.ox_id = oldiocb->iocb.unsli3.rcvsli3.ox_id;
5142 
5143         /* Xmit ECHO ACC response tag <ulpIoTag> */
5144         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5145                          "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
5146                          elsiocb->iotag, elsiocb->iocb.ulpContext);
5147         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5148         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5149         pcmd += sizeof(uint32_t);
5150         memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
5151 
5152         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5153                 "Issue ACC ECHO:  did:x%x flg:x%x",
5154                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
5155 
5156         phba->fc_stat.elsXmitACC++;
5157         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5158 
5159         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5160         if (rc == IOCB_ERROR) {
5161                 lpfc_els_free_iocb(phba, elsiocb);
5162                 return 1;
5163         }
5164         return 0;
5165 }
5166 
5167 /**
5168  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
5169  * @vport: pointer to a host virtual N_Port data structure.
5170  *
5171  * This routine issues Address Discover (ADISC) ELS commands to those
5172  * N_Ports which are in node port recovery state and ADISC has not been issued
5173  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
5174  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
5175  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
5176  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
5177  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
5178  * IOCBs quit for later pick up. On the other hand, after walking through
5179  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
5180  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
5181  * no more ADISC need to be sent.
5182  *
5183  * Return code
5184  *    The number of N_Ports with adisc issued.
5185  **/
5186 int
5187 lpfc_els_disc_adisc(struct lpfc_vport *vport)
5188 {
5189         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5190         struct lpfc_nodelist *ndlp, *next_ndlp;
5191         int sentadisc = 0;
5192 
5193         /* go thru NPR nodes and issue any remaining ELS ADISCs */
5194         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
5195                 if (!NLP_CHK_NODE_ACT(ndlp))
5196                         continue;
5197                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
5198                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
5199                     (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
5200                         spin_lock_irq(shost->host_lock);
5201                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
5202                         spin_unlock_irq(shost->host_lock);
5203                         ndlp->nlp_prev_state = ndlp->nlp_state;
5204                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
5205                         lpfc_issue_els_adisc(vport, ndlp, 0);
5206                         sentadisc++;
5207                         vport->num_disc_nodes++;
5208                         if (vport->num_disc_nodes >=
5209                             vport->cfg_discovery_threads) {
5210                                 spin_lock_irq(shost->host_lock);
5211                                 vport->fc_flag |= FC_NLP_MORE;
5212                                 spin_unlock_irq(shost->host_lock);
5213                                 break;
5214                         }
5215                 }
5216         }
5217         if (sentadisc == 0) {
5218                 spin_lock_irq(shost->host_lock);
5219                 vport->fc_flag &= ~FC_NLP_MORE;
5220                 spin_unlock_irq(shost->host_lock);
5221         }
5222         return sentadisc;
5223 }
5224 
5225 /**
5226  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
5227  * @vport: pointer to a host virtual N_Port data structure.
5228  *
5229  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
5230  * which are in node port recovery state, with a @vport. Each time an ELS
5231  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
5232  * the per @vport number of discover count (num_disc_nodes) shall be
5233  * incremented. If the num_disc_nodes reaches a pre-configured threshold
5234  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
5235  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
5236  * later pick up. On the other hand, after walking through all the ndlps with
5237  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
5238  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
5239  * PLOGI need to be sent.
5240  *
5241  * Return code
5242  *   The number of N_Ports with plogi issued.
5243  **/
5244 int
5245 lpfc_els_disc_plogi(struct lpfc_vport *vport)
5246 {
5247         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5248         struct lpfc_nodelist *ndlp, *next_ndlp;
5249         int sentplogi = 0;
5250 
5251         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
5252         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
5253                 if (!NLP_CHK_NODE_ACT(ndlp))
5254                         continue;
5255                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
5256                                 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
5257                                 (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
5258                                 (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
5259                         ndlp->nlp_prev_state = ndlp->nlp_state;
5260                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5261                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5262                         sentplogi++;
5263                         vport->num_disc_nodes++;
5264                         if (vport->num_disc_nodes >=
5265                                         vport->cfg_discovery_threads) {
5266                                 spin_lock_irq(shost->host_lock);
5267                                 vport->fc_flag |= FC_NLP_MORE;
5268                                 spin_unlock_irq(shost->host_lock);
5269                                 break;
5270                         }
5271                 }
5272         }
5273         if (sentplogi) {
5274                 lpfc_set_disctmo(vport);
5275         }
5276         else {
5277                 spin_lock_irq(shost->host_lock);
5278                 vport->fc_flag &= ~FC_NLP_MORE;
5279                 spin_unlock_irq(shost->host_lock);
5280         }
5281         return sentplogi;
5282 }
5283 
5284 static uint32_t
5285 lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
5286                 uint32_t word0)
5287 {
5288 
5289         desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
5290         desc->payload.els_req = word0;
5291         desc->length = cpu_to_be32(sizeof(desc->payload));
5292 
5293         return sizeof(struct fc_rdp_link_service_desc);
5294 }
5295 
5296 static uint32_t
5297 lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
5298                 uint8_t *page_a0, uint8_t *page_a2)
5299 {
5300         uint16_t wavelength;
5301         uint16_t temperature;
5302         uint16_t rx_power;
5303         uint16_t tx_bias;
5304         uint16_t tx_power;
5305         uint16_t vcc;
5306         uint16_t flag = 0;
5307         struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
5308         struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
5309 
5310         desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
5311 
5312         trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
5313                         &page_a0[SSF_TRANSCEIVER_CODE_B4];
5314         trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
5315                         &page_a0[SSF_TRANSCEIVER_CODE_B5];
5316 
5317         if ((trasn_code_byte4->fc_sw_laser) ||
5318             (trasn_code_byte5->fc_sw_laser_sl) ||
5319             (trasn_code_byte5->fc_sw_laser_sn)) {  /* check if its short WL */
5320                 flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
5321         } else if (trasn_code_byte4->fc_lw_laser) {
5322                 wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
5323                         page_a0[SSF_WAVELENGTH_B0];
5324                 if (wavelength == SFP_WAVELENGTH_LC1310)
5325                         flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
5326                 if (wavelength == SFP_WAVELENGTH_LL1550)
5327                         flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
5328         }
5329         /* check if its SFP+ */
5330         flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
5331                         SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
5332                                         << SFP_FLAG_CT_SHIFT;
5333 
5334         /* check if its OPTICAL */
5335         flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
5336                         SFP_FLAG_IS_OPTICAL_PORT : 0)
5337                                         << SFP_FLAG_IS_OPTICAL_SHIFT;
5338 
5339         temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
5340                 page_a2[SFF_TEMPERATURE_B0]);
5341         vcc = (page_a2[SFF_VCC_B1] << 8 |
5342                 page_a2[SFF_VCC_B0]);
5343         tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
5344                 page_a2[SFF_TXPOWER_B0]);
5345         tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
5346                 page_a2[SFF_TX_BIAS_CURRENT_B0]);
5347         rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
5348                 page_a2[SFF_RXPOWER_B0]);
5349         desc->sfp_info.temperature = cpu_to_be16(temperature);
5350         desc->sfp_info.rx_power = cpu_to_be16(rx_power);
5351         desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
5352         desc->sfp_info.tx_power = cpu_to_be16(tx_power);
5353         desc->sfp_info.vcc = cpu_to_be16(vcc);
5354 
5355         desc->sfp_info.flags = cpu_to_be16(flag);
5356         desc->length = cpu_to_be32(sizeof(desc->sfp_info));
5357 
5358         return sizeof(struct fc_rdp_sfp_desc);
5359 }
5360 
5361 static uint32_t
5362 lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
5363                 READ_LNK_VAR *stat)
5364 {
5365         uint32_t type;
5366 
5367         desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
5368 
5369         type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
5370 
5371         desc->info.port_type = cpu_to_be32(type);
5372 
5373         desc->info.link_status.link_failure_cnt =
5374                 cpu_to_be32(stat->linkFailureCnt);
5375         desc->info.link_status.loss_of_synch_cnt =
5376                 cpu_to_be32(stat->lossSyncCnt);
5377         desc->info.link_status.loss_of_signal_cnt =
5378                 cpu_to_be32(stat->lossSignalCnt);
5379         desc->info.link_status.primitive_seq_proto_err =
5380                 cpu_to_be32(stat->primSeqErrCnt);
5381         desc->info.link_status.invalid_trans_word =
5382                 cpu_to_be32(stat->invalidXmitWord);
5383         desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
5384 
5385         desc->length = cpu_to_be32(sizeof(desc->info));
5386 
5387         return sizeof(struct fc_rdp_link_error_status_desc);
5388 }
5389 
5390 static uint32_t
5391 lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc *desc, READ_LNK_VAR *stat,
5392                       struct lpfc_vport *vport)
5393 {
5394         uint32_t bbCredit;
5395 
5396         desc->tag = cpu_to_be32(RDP_BBC_DESC_TAG);
5397 
5398         bbCredit = vport->fc_sparam.cmn.bbCreditLsb |
5399                         (vport->fc_sparam.cmn.bbCreditMsb << 8);
5400         desc->bbc_info.port_bbc = cpu_to_be32(bbCredit);
5401         if (vport->phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
5402                 bbCredit = vport->phba->fc_fabparam.cmn.bbCreditLsb |
5403                         (vport->phba->fc_fabparam.cmn.bbCreditMsb << 8);
5404                 desc->bbc_info.attached_port_bbc = cpu_to_be32(bbCredit);
5405         } else {
5406                 desc->bbc_info.attached_port_bbc = 0;
5407         }
5408 
5409         desc->bbc_info.rtt = 0;
5410         desc->length = cpu_to_be32(sizeof(desc->bbc_info));
5411 
5412         return sizeof(struct fc_rdp_bbc_desc);
5413 }
5414 
5415 static uint32_t
5416 lpfc_rdp_res_oed_temp_desc(struct lpfc_hba *phba,
5417                            struct fc_rdp_oed_sfp_desc *desc, uint8_t *page_a2)
5418 {
5419         uint32_t flags = 0;
5420 
5421         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5422 
5423         desc->oed_info.hi_alarm = page_a2[SSF_TEMP_HIGH_ALARM];
5424         desc->oed_info.lo_alarm = page_a2[SSF_TEMP_LOW_ALARM];
5425         desc->oed_info.hi_warning = page_a2[SSF_TEMP_HIGH_WARNING];
5426         desc->oed_info.lo_warning = page_a2[SSF_TEMP_LOW_WARNING];
5427 
5428         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
5429                 flags |= RDP_OET_HIGH_ALARM;
5430         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
5431                 flags |= RDP_OET_LOW_ALARM;
5432         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
5433                 flags |= RDP_OET_HIGH_WARNING;
5434         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
5435                 flags |= RDP_OET_LOW_WARNING;
5436 
5437         flags |= ((0xf & RDP_OED_TEMPERATURE) << RDP_OED_TYPE_SHIFT);
5438         desc->oed_info.function_flags = cpu_to_be32(flags);
5439         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5440         return sizeof(struct fc_rdp_oed_sfp_desc);
5441 }
5442 
5443 static uint32_t
5444 lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba *phba,
5445                               struct fc_rdp_oed_sfp_desc *desc,
5446                               uint8_t *page_a2)
5447 {
5448         uint32_t flags = 0;
5449 
5450         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5451 
5452         desc->oed_info.hi_alarm = page_a2[SSF_VOLTAGE_HIGH_ALARM];
5453         desc->oed_info.lo_alarm = page_a2[SSF_VOLTAGE_LOW_ALARM];
5454         desc->oed_info.hi_warning = page_a2[SSF_VOLTAGE_HIGH_WARNING];
5455         desc->oed_info.lo_warning = page_a2[SSF_VOLTAGE_LOW_WARNING];
5456 
5457         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
5458                 flags |= RDP_OET_HIGH_ALARM;
5459         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_VOLTAGE)
5460                 flags |= RDP_OET_LOW_ALARM;
5461         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
5462                 flags |= RDP_OET_HIGH_WARNING;
5463         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_VOLTAGE)
5464                 flags |= RDP_OET_LOW_WARNING;
5465 
5466         flags |= ((0xf & RDP_OED_VOLTAGE) << RDP_OED_TYPE_SHIFT);
5467         desc->oed_info.function_flags = cpu_to_be32(flags);
5468         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5469         return sizeof(struct fc_rdp_oed_sfp_desc);
5470 }
5471 
5472 static uint32_t
5473 lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba *phba,
5474                              struct fc_rdp_oed_sfp_desc *desc,
5475                              uint8_t *page_a2)
5476 {
5477         uint32_t flags = 0;
5478 
5479         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5480 
5481         desc->oed_info.hi_alarm = page_a2[SSF_BIAS_HIGH_ALARM];
5482         desc->oed_info.lo_alarm = page_a2[SSF_BIAS_LOW_ALARM];
5483         desc->oed_info.hi_warning = page_a2[SSF_BIAS_HIGH_WARNING];
5484         desc->oed_info.lo_warning = page_a2[SSF_BIAS_LOW_WARNING];
5485 
5486         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXBIAS)
5487                 flags |= RDP_OET_HIGH_ALARM;
5488         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXBIAS)
5489                 flags |= RDP_OET_LOW_ALARM;
5490         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXBIAS)
5491                 flags |= RDP_OET_HIGH_WARNING;
5492         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXBIAS)
5493                 flags |= RDP_OET_LOW_WARNING;
5494 
5495         flags |= ((0xf & RDP_OED_TXBIAS) << RDP_OED_TYPE_SHIFT);
5496         desc->oed_info.function_flags = cpu_to_be32(flags);
5497         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5498         return sizeof(struct fc_rdp_oed_sfp_desc);
5499 }
5500 
5501 static uint32_t
5502 lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba *phba,
5503                               struct fc_rdp_oed_sfp_desc *desc,
5504                               uint8_t *page_a2)
5505 {
5506         uint32_t flags = 0;
5507 
5508         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5509 
5510         desc->oed_info.hi_alarm = page_a2[SSF_TXPOWER_HIGH_ALARM];
5511         desc->oed_info.lo_alarm = page_a2[SSF_TXPOWER_LOW_ALARM];
5512         desc->oed_info.hi_warning = page_a2[SSF_TXPOWER_HIGH_WARNING];
5513         desc->oed_info.lo_warning = page_a2[SSF_TXPOWER_LOW_WARNING];
5514 
5515         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXPOWER)
5516                 flags |= RDP_OET_HIGH_ALARM;
5517         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXPOWER)
5518                 flags |= RDP_OET_LOW_ALARM;
5519         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXPOWER)
5520                 flags |= RDP_OET_HIGH_WARNING;
5521         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXPOWER)
5522                 flags |= RDP_OET_LOW_WARNING;
5523 
5524         flags |= ((0xf & RDP_OED_TXPOWER) << RDP_OED_TYPE_SHIFT);
5525         desc->oed_info.function_flags = cpu_to_be32(flags);
5526         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5527         return sizeof(struct fc_rdp_oed_sfp_desc);
5528 }
5529 
5530 
5531 static uint32_t
5532 lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba *phba,
5533                               struct fc_rdp_oed_sfp_desc *desc,
5534                               uint8_t *page_a2)
5535 {
5536         uint32_t flags = 0;
5537 
5538         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5539 
5540         desc->oed_info.hi_alarm = page_a2[SSF_RXPOWER_HIGH_ALARM];
5541         desc->oed_info.lo_alarm = page_a2[SSF_RXPOWER_LOW_ALARM];
5542         desc->oed_info.hi_warning = page_a2[SSF_RXPOWER_HIGH_WARNING];
5543         desc->oed_info.lo_warning = page_a2[SSF_RXPOWER_LOW_WARNING];
5544 
5545         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_RXPOWER)
5546                 flags |= RDP_OET_HIGH_ALARM;
5547         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_RXPOWER)
5548                 flags |= RDP_OET_LOW_ALARM;
5549         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_RXPOWER)
5550                 flags |= RDP_OET_HIGH_WARNING;
5551         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_RXPOWER)
5552                 flags |= RDP_OET_LOW_WARNING;
5553 
5554         flags |= ((0xf & RDP_OED_RXPOWER) << RDP_OED_TYPE_SHIFT);
5555         desc->oed_info.function_flags = cpu_to_be32(flags);
5556         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5557         return sizeof(struct fc_rdp_oed_sfp_desc);
5558 }
5559 
5560 static uint32_t
5561 lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc *desc,
5562                       uint8_t *page_a0, struct lpfc_vport *vport)
5563 {
5564         desc->tag = cpu_to_be32(RDP_OPD_DESC_TAG);
5565         memcpy(desc->opd_info.vendor_name, &page_a0[SSF_VENDOR_NAME], 16);
5566         memcpy(desc->opd_info.model_number, &page_a0[SSF_VENDOR_PN], 16);
5567         memcpy(desc->opd_info.serial_number, &page_a0[SSF_VENDOR_SN], 16);
5568         memcpy(desc->opd_info.revision, &page_a0[SSF_VENDOR_REV], 4);
5569         memcpy(desc->opd_info.date, &page_a0[SSF_DATE_CODE], 8);
5570         desc->length = cpu_to_be32(sizeof(desc->opd_info));
5571         return sizeof(struct fc_rdp_opd_sfp_desc);
5572 }
5573 
5574 static uint32_t
5575 lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc *desc, READ_LNK_VAR *stat)
5576 {
5577         if (bf_get(lpfc_read_link_stat_gec2, stat) == 0)
5578                 return 0;
5579         desc->tag = cpu_to_be32(RDP_FEC_DESC_TAG);
5580 
5581         desc->info.CorrectedBlocks =
5582                 cpu_to_be32(stat->fecCorrBlkCount);
5583         desc->info.UncorrectableBlocks =
5584                 cpu_to_be32(stat->fecUncorrBlkCount);
5585 
5586         desc->length = cpu_to_be32(sizeof(desc->info));
5587 
5588         return sizeof(struct fc_fec_rdp_desc);
5589 }
5590 
5591 static uint32_t
5592 lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
5593 {
5594         uint16_t rdp_cap = 0;
5595         uint16_t rdp_speed;
5596 
5597         desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
5598 
5599         switch (phba->fc_linkspeed) {
5600         case LPFC_LINK_SPEED_1GHZ:
5601                 rdp_speed = RDP_PS_1GB;
5602                 break;
5603         case LPFC_LINK_SPEED_2GHZ:
5604                 rdp_speed = RDP_PS_2GB;
5605                 break;
5606         case LPFC_LINK_SPEED_4GHZ:
5607                 rdp_speed = RDP_PS_4GB;
5608                 break;
5609         case LPFC_LINK_SPEED_8GHZ:
5610                 rdp_speed = RDP_PS_8GB;
5611                 break;
5612         case LPFC_LINK_SPEED_10GHZ:
5613                 rdp_speed = RDP_PS_10GB;
5614                 break;
5615         case LPFC_LINK_SPEED_16GHZ:
5616                 rdp_speed = RDP_PS_16GB;
5617                 break;
5618         case LPFC_LINK_SPEED_32GHZ:
5619                 rdp_speed = RDP_PS_32GB;
5620                 break;
5621         case LPFC_LINK_SPEED_64GHZ:
5622                 rdp_speed = RDP_PS_64GB;
5623                 break;
5624         default:
5625                 rdp_speed = RDP_PS_UNKNOWN;
5626                 break;
5627         }
5628 
5629         desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
5630 
5631         if (phba->lmt & LMT_128Gb)
5632                 rdp_cap |= RDP_PS_128GB;
5633         if (phba->lmt & LMT_64Gb)
5634                 rdp_cap |= RDP_PS_64GB;
5635         if (phba->lmt & LMT_32Gb)
5636                 rdp_cap |= RDP_PS_32GB;
5637         if (phba->lmt & LMT_16Gb)
5638                 rdp_cap |= RDP_PS_16GB;
5639         if (phba->lmt & LMT_10Gb)
5640                 rdp_cap |= RDP_PS_10GB;
5641         if (phba->lmt & LMT_8Gb)
5642                 rdp_cap |= RDP_PS_8GB;
5643         if (phba->lmt & LMT_4Gb)
5644                 rdp_cap |= RDP_PS_4GB;
5645         if (phba->lmt & LMT_2Gb)
5646                 rdp_cap |= RDP_PS_2GB;
5647         if (phba->lmt & LMT_1Gb)
5648                 rdp_cap |= RDP_PS_1GB;
5649 
5650         if (rdp_cap == 0)
5651                 rdp_cap = RDP_CAP_UNKNOWN;
5652         if (phba->cfg_link_speed != LPFC_USER_LINK_SPEED_AUTO)
5653                 rdp_cap |= RDP_CAP_USER_CONFIGURED;
5654 
5655         desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
5656         desc->length = cpu_to_be32(sizeof(desc->info));
5657         return sizeof(struct fc_rdp_port_speed_desc);
5658 }
5659 
5660 static uint32_t
5661 lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
5662                 struct lpfc_vport *vport)
5663 {
5664 
5665         desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
5666 
5667         memcpy(desc->port_names.wwnn, &vport->fc_nodename,
5668                         sizeof(desc->port_names.wwnn));
5669 
5670         memcpy(desc->port_names.wwpn, &vport->fc_portname,
5671                         sizeof(desc->port_names.wwpn));
5672 
5673         desc->length = cpu_to_be32(sizeof(desc->port_names));
5674         return sizeof(struct fc_rdp_port_name_desc);
5675 }
5676 
5677 static uint32_t
5678 lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
5679                 struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5680 {
5681 
5682         desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
5683         if (vport->fc_flag & FC_FABRIC) {
5684                 memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
5685                        sizeof(desc->port_names.wwnn));
5686 
5687                 memcpy(desc->port_names.wwpn, &vport->fabric_portname,
5688                        sizeof(desc->port_names.wwpn));
5689         } else {  /* Point to Point */
5690                 memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
5691                        sizeof(desc->port_names.wwnn));
5692 
5693                 memcpy(desc->port_names.wwpn, &ndlp->nlp_portname,
5694                        sizeof(desc->port_names.wwpn));
5695         }
5696 
5697         desc->length = cpu_to_be32(sizeof(desc->port_names));
5698         return sizeof(struct fc_rdp_port_name_desc);
5699 }
5700 
5701 static void
5702 lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
5703                 int status)
5704 {
5705         struct lpfc_nodelist *ndlp = rdp_context->ndlp;
5706         struct lpfc_vport *vport = ndlp->vport;
5707         struct lpfc_iocbq *elsiocb;
5708         struct ulp_bde64 *bpl;
5709         IOCB_t *icmd;
5710         uint8_t *pcmd;
5711         struct ls_rjt *stat;
5712         struct fc_rdp_res_frame *rdp_res;
5713         uint32_t cmdsize, len;
5714         uint16_t *flag_ptr;
5715         int rc;
5716 
5717         if (status != SUCCESS)
5718                 goto error;
5719 
5720         /* This will change once we know the true size of the RDP payload */
5721         cmdsize = sizeof(struct fc_rdp_res_frame);
5722 
5723         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
5724                         lpfc_max_els_tries, rdp_context->ndlp,
5725                         rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
5726         lpfc_nlp_put(ndlp);
5727         if (!elsiocb)
5728                 goto free_rdp_context;
5729 
5730         icmd = &elsiocb->iocb;
5731         icmd->ulpContext = rdp_context->rx_id;
5732         icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
5733 
5734         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5735                         "2171 Xmit RDP response tag x%x xri x%x, "
5736                         "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x",
5737                         elsiocb->iotag, elsiocb->iocb.ulpContext,
5738                         ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5739                         ndlp->nlp_rpi);
5740         rdp_res = (struct fc_rdp_res_frame *)
5741                 (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5742         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5743         memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
5744         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5745 
5746         /* Update Alarm and Warning */
5747         flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_ALARM_FLAGS);
5748         phba->sfp_alarm |= *flag_ptr;
5749         flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_WARNING_FLAGS);
5750         phba->sfp_warning |= *flag_ptr;
5751 
5752         /* For RDP payload */
5753         len = 8;
5754         len += lpfc_rdp_res_link_service((struct fc_rdp_link_service_desc *)
5755                                          (len + pcmd), ELS_CMD_RDP);
5756 
5757         len += lpfc_rdp_res_sfp_desc((struct fc_rdp_sfp_desc *)(len + pcmd),
5758                         rdp_context->page_a0, rdp_context->page_a2);
5759         len += lpfc_rdp_res_speed((struct fc_rdp_port_speed_desc *)(len + pcmd),
5760                                   phba);
5761         len += lpfc_rdp_res_link_error((struct fc_rdp_link_error_status_desc *)
5762                                        (len + pcmd), &rdp_context->link_stat);
5763         len += lpfc_rdp_res_diag_port_names((struct fc_rdp_port_name_desc *)
5764                                              (len + pcmd), vport);
5765         len += lpfc_rdp_res_attach_port_names((struct fc_rdp_port_name_desc *)
5766                                         (len + pcmd), vport, ndlp);
5767         len += lpfc_rdp_res_fec_desc((struct fc_fec_rdp_desc *)(len + pcmd),
5768                         &rdp_context->link_stat);
5769         len += lpfc_rdp_res_bbc_desc((struct fc_rdp_bbc_desc *)(len + pcmd),
5770                                      &rdp_context->link_stat, vport);
5771         len += lpfc_rdp_res_oed_temp_desc(phba,
5772                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5773                                 rdp_context->page_a2);
5774         len += lpfc_rdp_res_oed_voltage_desc(phba,
5775                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5776                                 rdp_context->page_a2);
5777         len += lpfc_rdp_res_oed_txbias_desc(phba,
5778                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5779                                 rdp_context->page_a2);
5780         len += lpfc_rdp_res_oed_txpower_desc(phba,
5781                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5782                                 rdp_context->page_a2);
5783         len += lpfc_rdp_res_oed_rxpower_desc(phba,
5784                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5785                                 rdp_context->page_a2);
5786         len += lpfc_rdp_res_opd_desc((struct fc_rdp_opd_sfp_desc *)(len + pcmd),
5787                                      rdp_context->page_a0, vport);
5788 
5789         rdp_res->length = cpu_to_be32(len - 8);
5790         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5791 
5792         /* Now that we know the true size of the payload, update the BPL */
5793         bpl = (struct ulp_bde64 *)
5794                 (((struct lpfc_dmabuf *)(elsiocb->context3))->virt);
5795         bpl->tus.f.bdeSize = len;
5796         bpl->tus.f.bdeFlags = 0;
5797         bpl->tus.w = le32_to_cpu(bpl->tus.w);
5798 
5799         phba->fc_stat.elsXmitACC++;
5800         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5801         if (rc == IOCB_ERROR)
5802                 lpfc_els_free_iocb(phba, elsiocb);
5803 
5804         kfree(rdp_context);
5805 
5806         return;
5807 error:
5808         cmdsize = 2 * sizeof(uint32_t);
5809         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
5810                         ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
5811         lpfc_nlp_put(ndlp);
5812         if (!elsiocb)
5813                 goto free_rdp_context;
5814 
5815         icmd = &elsiocb->iocb;
5816         icmd->ulpContext = rdp_context->rx_id;
5817         icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
5818         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5819 
5820         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
5821         stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
5822         stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5823 
5824         phba->fc_stat.elsXmitLSRJT++;
5825         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5826         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5827 
5828         if (rc == IOCB_ERROR)
5829                 lpfc_els_free_iocb(phba, elsiocb);
5830 free_rdp_context:
5831         kfree(rdp_context);
5832 }
5833 
5834 static int
5835 lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
5836 {
5837         LPFC_MBOXQ_t *mbox = NULL;
5838         int rc;
5839 
5840         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5841         if (!mbox) {
5842                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
5843                                 "7105 failed to allocate mailbox memory");
5844                 return 1;
5845         }
5846 
5847         if (lpfc_sli4_dump_page_a0(phba, mbox))
5848                 goto prep_mbox_fail;
5849         mbox->vport = rdp_context->ndlp->vport;
5850         mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
5851         mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
5852         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5853         if (rc == MBX_NOT_FINISHED)
5854                 goto issue_mbox_fail;
5855 
5856         return 0;
5857 
5858 prep_mbox_fail:
5859 issue_mbox_fail:
5860         mempool_free(mbox, phba->mbox_mem_pool);
5861         return 1;
5862 }
5863 
5864 /*
5865  * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
5866  * @vport: pointer to a host virtual N_Port data structure.
5867  * @cmdiocb: pointer to lpfc command iocb data structure.
5868  * @ndlp: pointer to a node-list data structure.
5869  *
5870  * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
5871  * IOCB. First, the payload of the unsolicited RDP is checked.
5872  * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
5873  * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
5874  * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
5875  * gather all data and send RDP response.
5876  *
5877  * Return code
5878  *   0 - Sent the acc response
5879  *   1 - Sent the reject response.
5880  */
5881 static int
5882 lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5883                 struct lpfc_nodelist *ndlp)
5884 {
5885         struct lpfc_hba *phba = vport->phba;
5886         struct lpfc_dmabuf *pcmd;
5887         uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
5888         struct fc_rdp_req_frame *rdp_req;
5889         struct lpfc_rdp_context *rdp_context;
5890         IOCB_t *cmd = NULL;
5891         struct ls_rjt stat;
5892 
5893         if (phba->sli_rev < LPFC_SLI_REV4 ||
5894             bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
5895                                                 LPFC_SLI_INTF_IF_TYPE_2) {
5896                 rjt_err = LSRJT_UNABLE_TPC;
5897                 rjt_expl = LSEXP_REQ_UNSUPPORTED;
5898                 goto error;
5899         }
5900 
5901         if (phba->sli_rev < LPFC_SLI_REV4 || (phba->hba_flag & HBA_FCOE_MODE)) {
5902                 rjt_err = LSRJT_UNABLE_TPC;
5903                 rjt_expl = LSEXP_REQ_UNSUPPORTED;
5904                 goto error;
5905         }
5906 
5907         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5908         rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
5909 
5910         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5911                          "2422 ELS RDP Request "
5912                          "dec len %d tag x%x port_id %d len %d\n",
5913                          be32_to_cpu(rdp_req->rdp_des_length),
5914                          be32_to_cpu(rdp_req->nport_id_desc.tag),
5915                          be32_to_cpu(rdp_req->nport_id_desc.nport_id),
5916                          be32_to_cpu(rdp_req->nport_id_desc.length));
5917 
5918         if (sizeof(struct fc_rdp_nport_desc) !=
5919                         be32_to_cpu(rdp_req->rdp_des_length))
5920                 goto rjt_logerr;
5921         if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
5922                 goto rjt_logerr;
5923         if (RDP_NPORT_ID_SIZE !=
5924                         be32_to_cpu(rdp_req->nport_id_desc.length))
5925                 goto rjt_logerr;
5926         rdp_context = kzalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
5927         if (!rdp_context) {
5928                 rjt_err = LSRJT_UNABLE_TPC;
5929                 goto error;
5930         }
5931 
5932         cmd = &cmdiocb->iocb;
5933         rdp_context->ndlp = lpfc_nlp_get(ndlp);
5934         rdp_context->ox_id = cmd->unsli3.rcvsli3.ox_id;
5935         rdp_context->rx_id = cmd->ulpContext;
5936         rdp_context->cmpl = lpfc_els_rdp_cmpl;
5937         if (lpfc_get_rdp_info(phba, rdp_context)) {
5938                 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
5939                                  "2423 Unable to send mailbox");
5940                 kfree(rdp_context);
5941                 rjt_err = LSRJT_UNABLE_TPC;
5942                 lpfc_nlp_put(ndlp);
5943                 goto error;
5944         }
5945 
5946         return 0;
5947 
5948 rjt_logerr:
5949         rjt_err = LSRJT_LOGICAL_ERR;
5950 
5951 error:
5952         memset(&stat, 0, sizeof(stat));
5953         stat.un.b.lsRjtRsnCode = rjt_err;
5954         stat.un.b.lsRjtRsnCodeExp = rjt_expl;
5955         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5956         return 1;
5957 }
5958 
5959 
5960 static void
5961 lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5962 {
5963         MAILBOX_t *mb;
5964         IOCB_t *icmd;
5965         uint8_t *pcmd;
5966         struct lpfc_iocbq *elsiocb;
5967         struct lpfc_nodelist *ndlp;
5968         struct ls_rjt *stat;
5969         union lpfc_sli4_cfg_shdr *shdr;
5970         struct lpfc_lcb_context *lcb_context;
5971         struct fc_lcb_res_frame *lcb_res;
5972         uint32_t cmdsize, shdr_status, shdr_add_status;
5973         int rc;
5974 
5975         mb = &pmb->u.mb;
5976         lcb_context = (struct lpfc_lcb_context *)pmb->ctx_ndlp;
5977         ndlp = lcb_context->ndlp;
5978         pmb->ctx_ndlp = NULL;
5979         pmb->ctx_buf = NULL;
5980 
5981         shdr = (union lpfc_sli4_cfg_shdr *)
5982                         &pmb->u.mqe.un.beacon_config.header.cfg_shdr;
5983         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5984         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5985 
5986         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
5987                                 "0194 SET_BEACON_CONFIG mailbox "
5988                                 "completed with status x%x add_status x%x,"
5989                                 " mbx status x%x\n",
5990                                 shdr_status, shdr_add_status, mb->mbxStatus);
5991 
5992         if ((mb->mbxStatus != MBX_SUCCESS) || shdr_status ||
5993             (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE) ||
5994             (shdr_add_status == ADD_STATUS_INVALID_REQUEST)) {
5995                 mempool_free(pmb, phba->mbox_mem_pool);
5996                 goto error;
5997         }
5998 
5999         mempool_free(pmb, phba->mbox_mem_pool);
6000         cmdsize = sizeof(struct fc_lcb_res_frame);
6001         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6002                         lpfc_max_els_tries, ndlp,
6003                         ndlp->nlp_DID, ELS_CMD_ACC);
6004 
6005         /* Decrement the ndlp reference count from previous mbox command */
6006         lpfc_nlp_put(ndlp);
6007 
6008         if (!elsiocb)
6009                 goto free_lcb_context;
6010 
6011         lcb_res = (struct fc_lcb_res_frame *)
6012                 (((struct lpfc_dmabuf *)elsiocb->context2)->virt);
6013 
6014         memset(lcb_res, 0, sizeof(struct fc_lcb_res_frame));
6015         icmd = &elsiocb->iocb;
6016         icmd->ulpContext = lcb_context->rx_id;
6017         icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
6018 
6019         pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
6020         *((uint32_t *)(pcmd)) = ELS_CMD_ACC;
6021         lcb_res->lcb_sub_command = lcb_context->sub_command;
6022         lcb_res->lcb_type = lcb_context->type;
6023         lcb_res->capability = lcb_context->capability;
6024         lcb_res->lcb_frequency = lcb_context->frequency;
6025         lcb_res->lcb_duration = lcb_context->duration;
6026         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6027         phba->fc_stat.elsXmitACC++;
6028         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6029         if (rc == IOCB_ERROR)
6030                 lpfc_els_free_iocb(phba, elsiocb);
6031 
6032         kfree(lcb_context);
6033         return;
6034 
6035 error:
6036         cmdsize = sizeof(struct fc_lcb_res_frame);
6037         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6038                         lpfc_max_els_tries, ndlp,
6039                         ndlp->nlp_DID, ELS_CMD_LS_RJT);
6040         lpfc_nlp_put(ndlp);
6041         if (!elsiocb)
6042                 goto free_lcb_context;
6043 
6044         icmd = &elsiocb->iocb;
6045         icmd->ulpContext = lcb_context->rx_id;
6046         icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
6047         pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
6048 
6049         *((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
6050         stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
6051         stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6052 
6053         if (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE)
6054                 stat->un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
6055 
6056         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6057         phba->fc_stat.elsXmitLSRJT++;
6058         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6059         if (rc == IOCB_ERROR)
6060                 lpfc_els_free_iocb(phba, elsiocb);
6061 free_lcb_context:
6062         kfree(lcb_context);
6063 }
6064 
6065 static int
6066 lpfc_sli4_set_beacon(struct lpfc_vport *vport,
6067                      struct lpfc_lcb_context *lcb_context,
6068                      uint32_t beacon_state)
6069 {
6070         struct lpfc_hba *phba = vport->phba;
6071         union lpfc_sli4_cfg_shdr *cfg_shdr;
6072         LPFC_MBOXQ_t *mbox = NULL;
6073         uint32_t len;
6074         int rc;
6075 
6076         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6077         if (!mbox)
6078                 return 1;
6079 
6080         cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
6081         len = sizeof(struct lpfc_mbx_set_beacon_config) -
6082                 sizeof(struct lpfc_sli4_cfg_mhdr);
6083         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6084                          LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
6085                          LPFC_SLI4_MBX_EMBED);
6086         mbox->ctx_ndlp = (void *)lcb_context;
6087         mbox->vport = phba->pport;
6088         mbox->mbox_cmpl = lpfc_els_lcb_rsp;
6089         bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
6090                phba->sli4_hba.physical_port);
6091         bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
6092                beacon_state);
6093         mbox->u.mqe.un.beacon_config.word5 = 0;         /* Reserved */
6094 
6095         /*
6096          *      Check bv1s bit before issuing the mailbox
6097          *      if bv1s == 1, LCB V1 supported
6098          *      else, LCB V0 supported
6099          */
6100 
6101         if (phba->sli4_hba.pc_sli4_params.bv1s) {
6102                 /* COMMON_SET_BEACON_CONFIG_V1 */
6103                 cfg_shdr->request.word9 = BEACON_VERSION_V1;
6104                 lcb_context->capability |= LCB_CAPABILITY_DURATION;
6105                 bf_set(lpfc_mbx_set_beacon_port_type,
6106                        &mbox->u.mqe.un.beacon_config, 0);
6107                 bf_set(lpfc_mbx_set_beacon_duration_v1,
6108                        &mbox->u.mqe.un.beacon_config,
6109                        be16_to_cpu(lcb_context->duration));
6110         } else {
6111                 /* COMMON_SET_BEACON_CONFIG_V0 */
6112                 if (be16_to_cpu(lcb_context->duration) != 0) {
6113                         mempool_free(mbox, phba->mbox_mem_pool);
6114                         return 1;
6115                 }
6116                 cfg_shdr->request.word9 = BEACON_VERSION_V0;
6117                 lcb_context->capability &=  ~(LCB_CAPABILITY_DURATION);
6118                 bf_set(lpfc_mbx_set_beacon_state,
6119                        &mbox->u.mqe.un.beacon_config, beacon_state);
6120                 bf_set(lpfc_mbx_set_beacon_port_type,
6121                        &mbox->u.mqe.un.beacon_config, 1);
6122                 bf_set(lpfc_mbx_set_beacon_duration,
6123                        &mbox->u.mqe.un.beacon_config,
6124                        be16_to_cpu(lcb_context->duration));
6125         }
6126 
6127         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6128         if (rc == MBX_NOT_FINISHED) {
6129                 mempool_free(mbox, phba->mbox_mem_pool);
6130                 return 1;
6131         }
6132 
6133         return 0;
6134 }
6135 
6136 
6137 /**
6138  * lpfc_els_rcv_lcb - Process an unsolicited LCB
6139  * @vport: pointer to a host virtual N_Port data structure.
6140  * @cmdiocb: pointer to lpfc command iocb data structure.
6141  * @ndlp: pointer to a node-list data structure.
6142  *
6143  * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
6144  * First, the payload of the unsolicited LCB is checked.
6145  * Then based on Subcommand beacon will either turn on or off.
6146  *
6147  * Return code
6148  * 0 - Sent the acc response
6149  * 1 - Sent the reject response.
6150  **/
6151 static int
6152 lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6153                  struct lpfc_nodelist *ndlp)
6154 {
6155         struct lpfc_hba *phba = vport->phba;
6156         struct lpfc_dmabuf *pcmd;
6157         uint8_t *lp;
6158         struct fc_lcb_request_frame *beacon;
6159         struct lpfc_lcb_context *lcb_context;
6160         uint8_t state, rjt_err;
6161         struct ls_rjt stat;
6162 
6163         pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
6164         lp = (uint8_t *)pcmd->virt;
6165         beacon = (struct fc_lcb_request_frame *)pcmd->virt;
6166 
6167         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6168                         "0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
6169                         "type x%x frequency %x duration x%x\n",
6170                         lp[0], lp[1], lp[2],
6171                         beacon->lcb_command,
6172                         beacon->lcb_sub_command,
6173                         beacon->lcb_type,
6174                         beacon->lcb_frequency,
6175                         be16_to_cpu(beacon->lcb_duration));
6176 
6177         if (beacon->lcb_sub_command != LPFC_LCB_ON &&
6178             beacon->lcb_sub_command != LPFC_LCB_OFF) {
6179                 rjt_err = LSRJT_CMD_UNSUPPORTED;
6180                 goto rjt;
6181         }
6182 
6183         if (phba->sli_rev < LPFC_SLI_REV4  ||
6184             phba->hba_flag & HBA_FCOE_MODE ||
6185             (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
6186             LPFC_SLI_INTF_IF_TYPE_2)) {
6187                 rjt_err = LSRJT_CMD_UNSUPPORTED;
6188                 goto rjt;
6189         }
6190 
6191         lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
6192         if (!lcb_context) {
6193                 rjt_err = LSRJT_UNABLE_TPC;
6194                 goto rjt;
6195         }
6196 
6197         state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
6198         lcb_context->sub_command = beacon->lcb_sub_command;
6199         lcb_context->capability = 0;
6200         lcb_context->type = beacon->lcb_type;
6201         lcb_context->frequency = beacon->lcb_frequency;
6202         lcb_context->duration = beacon->lcb_duration;
6203         lcb_context->ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
6204         lcb_context->rx_id = cmdiocb->iocb.ulpContext;
6205         lcb_context->ndlp = lpfc_nlp_get(ndlp);
6206         if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
6207                 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
6208                                  LOG_ELS, "0193 failed to send mail box");
6209                 kfree(lcb_context);
6210                 lpfc_nlp_put(ndlp);
6211                 rjt_err = LSRJT_UNABLE_TPC;
6212                 goto rjt;
6213         }
6214         return 0;
6215 rjt:
6216         memset(&stat, 0, sizeof(stat));
6217         stat.un.b.lsRjtRsnCode = rjt_err;
6218         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6219         return 1;
6220 }
6221 
6222 
6223 /**
6224  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
6225  * @vport: pointer to a host virtual N_Port data structure.
6226  *
6227  * This routine cleans up any Registration State Change Notification
6228  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
6229  * @vport together with the host_lock is used to prevent multiple thread
6230  * trying to access the RSCN array on a same @vport at the same time.
6231  **/
6232 void
6233 lpfc_els_flush_rscn(struct lpfc_vport *vport)
6234 {
6235         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6236         struct lpfc_hba  *phba = vport->phba;
6237         int i;
6238 
6239         spin_lock_irq(shost->host_lock);
6240         if (vport->fc_rscn_flush) {
6241                 /* Another thread is walking fc_rscn_id_list on this vport */
6242                 spin_unlock_irq(shost->host_lock);
6243                 return;
6244         }
6245         /* Indicate we are walking lpfc_els_flush_rscn on this vport */
6246         vport->fc_rscn_flush = 1;
6247         spin_unlock_irq(shost->host_lock);
6248 
6249         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
6250                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
6251                 vport->fc_rscn_id_list[i] = NULL;
6252         }
6253         spin_lock_irq(shost->host_lock);
6254         vport->fc_rscn_id_cnt = 0;
6255         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
6256         spin_unlock_irq(shost->host_lock);
6257         lpfc_can_disctmo(vport);
6258         /* Indicate we are done walking this fc_rscn_id_list */
6259         vport->fc_rscn_flush = 0;
6260 }
6261 
6262 /**
6263  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
6264  * @vport: pointer to a host virtual N_Port data structure.
6265  * @did: remote destination port identifier.
6266  *
6267  * This routine checks whether there is any pending Registration State
6268  * Configuration Notification (RSCN) to a @did on @vport.
6269  *
6270  * Return code
6271  *   None zero - The @did matched with a pending rscn
6272  *   0 - not able to match @did with a pending rscn
6273  **/
6274 int
6275 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
6276 {
6277         D_ID ns_did;
6278         D_ID rscn_did;
6279         uint32_t *lp;
6280         uint32_t payload_len, i;
6281         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6282 
6283         ns_did.un.word = did;
6284 
6285         /* Never match fabric nodes for RSCNs */
6286         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
6287                 return 0;
6288 
6289         /* If we are doing a FULL RSCN rediscovery, match everything */
6290         if (vport->fc_flag & FC_RSCN_DISCOVERY)
6291                 return did;
6292 
6293         spin_lock_irq(shost->host_lock);
6294         if (vport->fc_rscn_flush) {
6295                 /* Another thread is walking fc_rscn_id_list on this vport */
6296                 spin_unlock_irq(shost->host_lock);
6297                 return 0;
6298         }
6299         /* Indicate we are walking fc_rscn_id_list on this vport */
6300         vport->fc_rscn_flush = 1;
6301         spin_unlock_irq(shost->host_lock);
6302         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
6303                 lp = vport->fc_rscn_id_list[i]->virt;
6304                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
6305                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
6306                 while (payload_len) {
6307                         rscn_did.un.word = be32_to_cpu(*lp++);
6308                         payload_len -= sizeof(uint32_t);
6309                         switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
6310                         case RSCN_ADDRESS_FORMAT_PORT:
6311                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
6312                                     && (ns_did.un.b.area == rscn_did.un.b.area)
6313                                     && (ns_did.un.b.id == rscn_did.un.b.id))
6314                                         goto return_did_out;
6315                                 break;
6316                         case RSCN_ADDRESS_FORMAT_AREA:
6317                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
6318                                     && (ns_did.un.b.area == rscn_did.un.b.area))
6319                                         goto return_did_out;
6320                                 break;
6321                         case RSCN_ADDRESS_FORMAT_DOMAIN:
6322                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
6323                                         goto return_did_out;
6324                                 break;
6325                         case RSCN_ADDRESS_FORMAT_FABRIC:
6326                                 goto return_did_out;
6327                         }
6328                 }
6329         }
6330         /* Indicate we are done with walking fc_rscn_id_list on this vport */
6331         vport->fc_rscn_flush = 0;
6332         return 0;
6333 return_did_out:
6334         /* Indicate we are done with walking fc_rscn_id_list on this vport */
6335         vport->fc_rscn_flush = 0;
6336         return did;
6337 }
6338 
6339 /**
6340  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
6341  * @vport: pointer to a host virtual N_Port data structure.
6342  *
6343  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
6344  * state machine for a @vport's nodes that are with pending RSCN (Registration
6345  * State Change Notification).
6346  *
6347  * Return code
6348  *   0 - Successful (currently alway return 0)
6349  **/
6350 static int
6351 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
6352 {
6353         struct lpfc_nodelist *ndlp = NULL;
6354 
6355         /* Move all affected nodes by pending RSCNs to NPR state. */
6356         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
6357                 if (!NLP_CHK_NODE_ACT(ndlp) ||
6358                     (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
6359                     !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
6360                         continue;
6361 
6362                 /* NVME Target mode does not do RSCN Recovery. */
6363                 if (vport->phba->nvmet_support)
6364                         continue;
6365 
6366                 /* If we are in the process of doing discovery on this
6367                  * NPort, let it continue on its own.
6368                  */
6369                 switch (ndlp->nlp_state) {
6370                 case  NLP_STE_PLOGI_ISSUE:
6371                 case  NLP_STE_ADISC_ISSUE:
6372                 case  NLP_STE_REG_LOGIN_ISSUE:
6373                 case  NLP_STE_PRLI_ISSUE:
6374                 case  NLP_STE_LOGO_ISSUE:
6375                         continue;
6376                 }
6377 
6378                 /* Check to see if we need to NVME rescan this target
6379                  * remoteport.
6380                  */
6381                 if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
6382                     ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
6383                         lpfc_nvme_rescan_port(vport, ndlp);
6384 
6385                 lpfc_disc_state_machine(vport, ndlp, NULL,
6386                                         NLP_EVT_DEVICE_RECOVERY);
6387                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
6388         }
6389         return 0;
6390 }
6391 
6392 /**
6393  * lpfc_send_rscn_event - Send an RSCN event to management application
6394  * @vport: pointer to a host virtual N_Port data structure.
6395  * @cmdiocb: pointer to lpfc command iocb data structure.
6396  *
6397  * lpfc_send_rscn_event sends an RSCN netlink event to management
6398  * applications.
6399  */
6400 static void
6401 lpfc_send_rscn_event(struct lpfc_vport *vport,
6402                 struct lpfc_iocbq *cmdiocb)
6403 {
6404         struct lpfc_dmabuf *pcmd;
6405         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6406         uint32_t *payload_ptr;
6407         uint32_t payload_len;
6408         struct lpfc_rscn_event_header *rscn_event_data;
6409 
6410         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6411         payload_ptr = (uint32_t *) pcmd->virt;
6412         payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
6413 
6414         rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
6415                 payload_len, GFP_KERNEL);
6416         if (!rscn_event_data) {
6417                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6418                         "0147 Failed to allocate memory for RSCN event\n");
6419                 return;
6420         }
6421         rscn_event_data->event_type = FC_REG_RSCN_EVENT;
6422         rscn_event_data->payload_length = payload_len;
6423         memcpy(rscn_event_data->rscn_payload, payload_ptr,
6424                 payload_len);
6425 
6426         fc_host_post_vendor_event(shost,
6427                 fc_get_event_number(),
6428                 sizeof(struct lpfc_rscn_event_header) + payload_len,
6429                 (char *)rscn_event_data,
6430                 LPFC_NL_VENDOR_ID);
6431 
6432         kfree(rscn_event_data);
6433 }
6434 
6435 /**
6436  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
6437  * @vport: pointer to a host virtual N_Port data structure.
6438  * @cmdiocb: pointer to lpfc command iocb data structure.
6439  * @ndlp: pointer to a node-list data structure.
6440  *
6441  * This routine processes an unsolicited RSCN (Registration State Change
6442  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
6443  * to invoke fc_host_post_event() routine to the FC transport layer. If the
6444  * discover state machine is about to begin discovery, it just accepts the
6445  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
6446  * contains N_Port IDs for other vports on this HBA, it just accepts the
6447  * RSCN and ignore processing it. If the state machine is in the recovery
6448  * state, the fc_rscn_id_list of this @vport is walked and the
6449  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
6450  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
6451  * routine is invoked to handle the RSCN event.
6452  *
6453  * Return code
6454  *   0 - Just sent the acc response
6455  *   1 - Sent the acc response and waited for name server completion
6456  **/
6457 static int
6458 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6459                   struct lpfc_nodelist *ndlp)
6460 {
6461         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6462         struct lpfc_hba  *phba = vport->phba;
6463         struct lpfc_dmabuf *pcmd;
6464         uint32_t *lp, *datap;
6465         uint32_t payload_len, length, nportid, *cmd;
6466         int rscn_cnt;
6467         int rscn_id = 0, hba_id = 0;
6468         int i, tmo;
6469 
6470         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6471         lp = (uint32_t *) pcmd->virt;
6472 
6473         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
6474         payload_len -= sizeof(uint32_t);        /* take off word 0 */
6475         /* RSCN received */
6476         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6477                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
6478                          vport->fc_flag, payload_len, *lp,
6479                          vport->fc_rscn_id_cnt);
6480 
6481         /* Send an RSCN event to the management application */
6482         lpfc_send_rscn_event(vport, cmdiocb);
6483 
6484         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
6485                 fc_host_post_event(shost, fc_get_event_number(),
6486                         FCH_EVT_RSCN, lp[i]);
6487 
6488         /* Check if RSCN is coming from a direct-connected remote NPort */
6489         if (vport->fc_flag & FC_PT2PT) {
6490                 /* If so, just ACC it, no other action needed for now */
6491                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6492                                  "2024 pt2pt RSCN %08x Data: x%x x%x\n",
6493                                  *lp, vport->fc_flag, payload_len);
6494                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6495 
6496                 /* Check to see if we need to NVME rescan this target
6497                  * remoteport.
6498                  */
6499                 if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
6500                     ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
6501                         lpfc_nvme_rescan_port(vport, ndlp);
6502                 return 0;
6503         }
6504 
6505         /* If we are about to begin discovery, just ACC the RSCN.
6506          * Discovery processing will satisfy it.
6507          */
6508         if (vport->port_state <= LPFC_NS_QRY) {
6509                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6510                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
6511                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6512 
6513                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6514                 return 0;
6515         }
6516 
6517         /* If this RSCN just contains NPortIDs for other vports on this HBA,
6518          * just ACC and ignore it.
6519          */
6520         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
6521                 !(vport->cfg_peer_port_login)) {
6522                 i = payload_len;
6523                 datap = lp;
6524                 while (i > 0) {
6525                         nportid = *datap++;
6526                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
6527                         i -= sizeof(uint32_t);
6528                         rscn_id++;
6529                         if (lpfc_find_vport_by_did(phba, nportid))
6530                                 hba_id++;
6531                 }
6532                 if (rscn_id == hba_id) {
6533                         /* ALL NPortIDs in RSCN are on HBA */
6534                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6535                                          "0219 Ignore RSCN "
6536                                          "Data: x%x x%x x%x x%x\n",
6537                                          vport->fc_flag, payload_len,
6538                                          *lp, vport->fc_rscn_id_cnt);
6539                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6540                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
6541                                 ndlp->nlp_DID, vport->port_state,
6542                                 ndlp->nlp_flag);
6543 
6544                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
6545                                 ndlp, NULL);
6546                         return 0;
6547                 }
6548         }
6549 
6550         spin_lock_irq(shost->host_lock);
6551         if (vport->fc_rscn_flush) {
6552                 /* Another thread is walking fc_rscn_id_list on this vport */
6553                 vport->fc_flag |= FC_RSCN_DISCOVERY;
6554                 spin_unlock_irq(shost->host_lock);
6555                 /* Send back ACC */
6556                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6557                 return 0;
6558         }
6559         /* Indicate we are walking fc_rscn_id_list on this vport */
6560         vport->fc_rscn_flush = 1;
6561         spin_unlock_irq(shost->host_lock);
6562         /* Get the array count after successfully have the token */
6563         rscn_cnt = vport->fc_rscn_id_cnt;
6564         /* If we are already processing an RSCN, save the received
6565          * RSCN payload buffer, cmdiocb->context2 to process later.
6566          */
6567         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
6568                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6569                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
6570                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6571 
6572                 spin_lock_irq(shost->host_lock);
6573                 vport->fc_flag |= FC_RSCN_DEFERRED;
6574 
6575                 /* Restart disctmo if its already running */
6576                 if (vport->fc_flag & FC_DISC_TMO) {
6577                         tmo = ((phba->fc_ratov * 3) + 3);
6578                         mod_timer(&vport->fc_disctmo,
6579                                   jiffies + msecs_to_jiffies(1000 * tmo));
6580                 }
6581                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
6582                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
6583                         vport->fc_flag |= FC_RSCN_MODE;
6584                         spin_unlock_irq(shost->host_lock);
6585                         if (rscn_cnt) {
6586                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
6587                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
6588                         }
6589                         if ((rscn_cnt) &&
6590                             (payload_len + length <= LPFC_BPL_SIZE)) {
6591                                 *cmd &= ELS_CMD_MASK;
6592                                 *cmd |= cpu_to_be32(payload_len + length);
6593                                 memcpy(((uint8_t *)cmd) + length, lp,
6594                                        payload_len);
6595                         } else {
6596                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
6597                                 vport->fc_rscn_id_cnt++;
6598                                 /* If we zero, cmdiocb->context2, the calling
6599                                  * routine will not try to free it.
6600                                  */
6601                                 cmdiocb->context2 = NULL;
6602                         }
6603                         /* Deferred RSCN */
6604                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6605                                          "0235 Deferred RSCN "
6606                                          "Data: x%x x%x x%x\n",
6607                                          vport->fc_rscn_id_cnt, vport->fc_flag,
6608                                          vport->port_state);
6609                 } else {
6610                         vport->fc_flag |= FC_RSCN_DISCOVERY;
6611                         spin_unlock_irq(shost->host_lock);
6612                         /* ReDiscovery RSCN */
6613                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6614                                          "0234 ReDiscovery RSCN "
6615                                          "Data: x%x x%x x%x\n",
6616                                          vport->fc_rscn_id_cnt, vport->fc_flag,
6617                                          vport->port_state);
6618                 }
6619                 /* Indicate we are done walking fc_rscn_id_list on this vport */
6620                 vport->fc_rscn_flush = 0;
6621                 /* Send back ACC */
6622                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6623                 /* send RECOVERY event for ALL nodes that match RSCN payload */
6624                 lpfc_rscn_recovery_check(vport);
6625                 return 0;
6626         }
6627         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6628                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
6629                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6630 
6631         spin_lock_irq(shost->host_lock);
6632         vport->fc_flag |= FC_RSCN_MODE;
6633         spin_unlock_irq(shost->host_lock);
6634         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
6635         /* Indicate we are done walking fc_rscn_id_list on this vport */
6636         vport->fc_rscn_flush = 0;
6637         /*
6638          * If we zero, cmdiocb->context2, the calling routine will
6639          * not try to free it.
6640          */
6641         cmdiocb->context2 = NULL;
6642         lpfc_set_disctmo(vport);
6643         /* Send back ACC */
6644         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6645         /* send RECOVERY event for ALL nodes that match RSCN payload */
6646         lpfc_rscn_recovery_check(vport);
6647         return lpfc_els_handle_rscn(vport);
6648 }
6649 
6650 /**
6651  * lpfc_els_handle_rscn - Handle rscn for a vport
6652  * @vport: pointer to a host virtual N_Port data structure.
6653  *
6654  * This routine handles the Registration State Configuration Notification
6655  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
6656  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
6657  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
6658  * NameServer shall be issued. If CT command to the NameServer fails to be
6659  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
6660  * RSCN activities with the @vport.
6661  *
6662  * Return code
6663  *   0 - Cleaned up rscn on the @vport
6664  *   1 - Wait for plogi to name server before proceed
6665  **/
6666 int
6667 lpfc_els_handle_rscn(struct lpfc_vport *vport)
6668 {
6669         struct lpfc_nodelist *ndlp;
6670         struct lpfc_hba  *phba = vport->phba;
6671 
6672         /* Ignore RSCN if the port is being torn down. */
6673         if (vport->load_flag & FC_UNLOADING) {
6674                 lpfc_els_flush_rscn(vport);
6675                 return 0;
6676         }
6677 
6678         /* Start timer for RSCN processing */
6679         lpfc_set_disctmo(vport);
6680 
6681         /* RSCN processed */
6682         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6683                          "0215 RSCN processed Data: x%x x%x x%x x%x\n",
6684                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
6685                          vport->port_state);
6686 
6687         /* To process RSCN, first compare RSCN data with NameServer */
6688         vport->fc_ns_retry = 0;
6689         vport->num_disc_nodes = 0;
6690 
6691         ndlp = lpfc_findnode_did(vport, NameServer_DID);
6692         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
6693             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
6694                 /* Good ndlp, issue CT Request to NameServer.  Need to
6695                  * know how many gidfts were issued.  If none, then just
6696                  * flush the RSCN.  Otherwise, the outstanding requests
6697                  * need to complete.
6698                  */
6699                 if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT) {
6700                         if (lpfc_issue_gidft(vport) > 0)
6701                                 return 1;
6702                 } else if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_PT) {
6703                         if (lpfc_issue_gidpt(vport) > 0)
6704                                 return 1;
6705                 } else {
6706                         return 1;
6707                 }
6708         } else {
6709                 /* Nameserver login in question.  Revalidate. */
6710                 if (ndlp) {
6711                         ndlp = lpfc_enable_node(vport, ndlp,
6712                                                 NLP_STE_PLOGI_ISSUE);
6713                         if (!ndlp) {
6714                                 lpfc_els_flush_rscn(vport);
6715                                 return 0;
6716                         }
6717                         ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
6718                 } else {
6719                         ndlp = lpfc_nlp_init(vport, NameServer_DID);
6720                         if (!ndlp) {
6721                                 lpfc_els_flush_rscn(vport);
6722                                 return 0;
6723                         }
6724                         ndlp->nlp_prev_state = ndlp->nlp_state;
6725                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6726                 }
6727                 ndlp->nlp_type |= NLP_FABRIC;
6728                 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
6729                 /* Wait for NameServer login cmpl before we can
6730                  * continue
6731                  */
6732                 return 1;
6733         }
6734 
6735         lpfc_els_flush_rscn(vport);
6736         return 0;
6737 }
6738 
6739 /**
6740  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
6741  * @vport: pointer to a host virtual N_Port data structure.
6742  * @cmdiocb: pointer to lpfc command iocb data structure.
6743  * @ndlp: pointer to a node-list data structure.
6744  *
6745  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
6746  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
6747  * point topology. As an unsolicited FLOGI should not be received in a loop
6748  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
6749  * lpfc_check_sparm() routine is invoked to check the parameters in the
6750  * unsolicited FLOGI. If parameters validation failed, the routine
6751  * lpfc_els_rsp_reject() shall be called with reject reason code set to
6752  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
6753  * FLOGI shall be compared with the Port WWN of the @vport to determine who
6754  * will initiate PLOGI. The higher lexicographical value party shall has
6755  * higher priority (as the winning port) and will initiate PLOGI and
6756  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
6757  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
6758  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
6759  *
6760  * Return code
6761  *   0 - Successfully processed the unsolicited flogi
6762  *   1 - Failed to process the unsolicited flogi
6763  **/
6764 static int
6765 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6766                    struct lpfc_nodelist *ndlp)
6767 {
6768         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6769         struct lpfc_hba  *phba = vport->phba;
6770         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6771         uint32_t *lp = (uint32_t *) pcmd->virt;
6772         IOCB_t *icmd = &cmdiocb->iocb;
6773         struct serv_parm *sp;
6774         LPFC_MBOXQ_t *mbox;
6775         uint32_t cmd, did;
6776         int rc;
6777         uint32_t fc_flag = 0;
6778         uint32_t port_state = 0;
6779 
6780         cmd = *lp++;
6781         sp = (struct serv_parm *) lp;
6782 
6783         /* FLOGI received */
6784 
6785         lpfc_set_disctmo(vport);
6786 
6787         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
6788                 /* We should never receive a FLOGI in loop mode, ignore it */
6789                 did = icmd->un.elsreq64.remoteID;
6790 
6791                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
6792                    Loop Mode */
6793                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6794                                  "0113 An FLOGI ELS command x%x was "
6795                                  "received from DID x%x in Loop Mode\n",
6796                                  cmd, did);
6797                 return 1;
6798         }
6799 
6800         (void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
6801 
6802         /*
6803          * If our portname is greater than the remote portname,
6804          * then we initiate Nport login.
6805          */
6806 
6807         rc = memcmp(&vport->fc_portname, &sp->portName,
6808                     sizeof(struct lpfc_name));
6809 
6810         if (!rc) {
6811                 if (phba->sli_rev < LPFC_SLI_REV4) {
6812                         mbox = mempool_alloc(phba->mbox_mem_pool,
6813                                              GFP_KERNEL);
6814                         if (!mbox)
6815                                 return 1;
6816                         lpfc_linkdown(phba);
6817                         lpfc_init_link(phba, mbox,
6818                                        phba->cfg_topology,
6819                                        phba->cfg_link_speed);
6820                         mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
6821                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6822                         mbox->vport = vport;
6823                         rc = lpfc_sli_issue_mbox(phba, mbox,
6824                                                  MBX_NOWAIT);
6825                         lpfc_set_loopback_flag(phba);
6826                         if (rc == MBX_NOT_FINISHED)
6827                                 mempool_free(mbox, phba->mbox_mem_pool);
6828                         return 1;
6829                 }
6830 
6831                 /* abort the flogi coming back to ourselves
6832                  * due to external loopback on the port.
6833                  */
6834                 lpfc_els_abort_flogi(phba);
6835                 return 0;
6836 
6837         } else if (rc > 0) {    /* greater than */
6838                 spin_lock_irq(shost->host_lock);
6839                 vport->fc_flag |= FC_PT2PT_PLOGI;
6840                 spin_unlock_irq(shost->host_lock);
6841 
6842                 /* If we have the high WWPN we can assign our own
6843                  * myDID; otherwise, we have to WAIT for a PLOGI
6844                  * from the remote NPort to find out what it
6845                  * will be.
6846                  */
6847                 vport->fc_myDID = PT2PT_LocalID;
6848         } else {
6849                 vport->fc_myDID = PT2PT_RemoteID;
6850         }
6851 
6852         /*
6853          * The vport state should go to LPFC_FLOGI only
6854          * AFTER we issue a FLOGI, not receive one.
6855          */
6856         spin_lock_irq(shost->host_lock);
6857         fc_flag = vport->fc_flag;
6858         port_state = vport->port_state;
6859         vport->fc_flag |= FC_PT2PT;
6860         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
6861 
6862         /* Acking an unsol FLOGI.  Count 1 for link bounce
6863          * work-around.
6864          */
6865         vport->rcv_flogi_cnt++;
6866         spin_unlock_irq(shost->host_lock);
6867         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6868                          "3311 Rcv Flogi PS x%x new PS x%x "
6869                          "fc_flag x%x new fc_flag x%x\n",
6870                          port_state, vport->port_state,
6871                          fc_flag, vport->fc_flag);
6872 
6873         /*
6874          * We temporarily set fc_myDID to make it look like we are
6875          * a Fabric. This is done just so we end up with the right
6876          * did / sid on the FLOGI ACC rsp.
6877          */
6878         did = vport->fc_myDID;
6879         vport->fc_myDID = Fabric_DID;
6880 
6881         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
6882 
6883         /* Defer ACC response until AFTER we issue a FLOGI */
6884         if (!(phba->hba_flag & HBA_FLOGI_ISSUED)) {
6885                 phba->defer_flogi_acc_rx_id = cmdiocb->iocb.ulpContext;
6886                 phba->defer_flogi_acc_ox_id =
6887                                         cmdiocb->iocb.unsli3.rcvsli3.ox_id;
6888 
6889                 vport->fc_myDID = did;
6890 
6891                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6892                                  "3344 Deferring FLOGI ACC: rx_id: x%x,"
6893                                  " ox_id: x%x, hba_flag x%x\n",
6894                                  phba->defer_flogi_acc_rx_id,
6895                                  phba->defer_flogi_acc_ox_id, phba->hba_flag);
6896 
6897                 phba->defer_flogi_acc_flag = true;
6898 
6899                 return 0;
6900         }
6901 
6902         /* Send back ACC */
6903         lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
6904 
6905         /* Now lets put fc_myDID back to what its supposed to be */
6906         vport->fc_myDID = did;
6907 
6908         return 0;
6909 }
6910 
6911 /**
6912  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
6913  * @vport: pointer to a host virtual N_Port data structure.
6914  * @cmdiocb: pointer to lpfc command iocb data structure.
6915  * @ndlp: pointer to a node-list data structure.
6916  *
6917  * This routine processes Request Node Identification Data (RNID) IOCB
6918  * received as an ELS unsolicited event. Only when the RNID specified format
6919  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
6920  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
6921  * Accept (ACC) the RNID ELS command. All the other RNID formats are
6922  * rejected by invoking the lpfc_els_rsp_reject() routine.
6923  *
6924  * Return code
6925  *   0 - Successfully processed rnid iocb (currently always return 0)
6926  **/
6927 static int
6928 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6929                   struct lpfc_nodelist *ndlp)
6930 {
6931         struct lpfc_dmabuf *pcmd;
6932         uint32_t *lp;
6933         RNID *rn;
6934         struct ls_rjt stat;
6935 
6936         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6937         lp = (uint32_t *) pcmd->virt;
6938 
6939         lp++;
6940         rn = (RNID *) lp;
6941 
6942         /* RNID received */
6943 
6944         switch (rn->Format) {
6945         case 0:
6946         case RNID_TOPOLOGY_DISC:
6947                 /* Send back ACC */
6948                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
6949                 break;
6950         default:
6951                 /* Reject this request because format not supported */
6952                 stat.un.b.lsRjtRsvd0 = 0;
6953                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6954                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6955                 stat.un.b.vendorUnique = 0;
6956                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
6957                         NULL);
6958         }
6959         return 0;
6960 }
6961 
6962 /**
6963  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
6964  * @vport: pointer to a host virtual N_Port data structure.
6965  * @cmdiocb: pointer to lpfc command iocb data structure.
6966  * @ndlp: pointer to a node-list data structure.
6967  *
6968  * Return code
6969  *   0 - Successfully processed echo iocb (currently always return 0)
6970  **/
6971 static int
6972 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6973                   struct lpfc_nodelist *ndlp)
6974 {
6975         uint8_t *pcmd;
6976 
6977         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
6978 
6979         /* skip over first word of echo command to find echo data */
6980         pcmd += sizeof(uint32_t);
6981 
6982         lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
6983         return 0;
6984 }
6985 
6986 /**
6987  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
6988  * @vport: pointer to a host virtual N_Port data structure.
6989  * @cmdiocb: pointer to lpfc command iocb data structure.
6990  * @ndlp: pointer to a node-list data structure.
6991  *
6992  * This routine processes a Link Incident Report Registration(LIRR) IOCB
6993  * received as an ELS unsolicited event. Currently, this function just invokes
6994  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
6995  *
6996  * Return code
6997  *   0 - Successfully processed lirr iocb (currently always return 0)
6998  **/
6999 static int
7000 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7001                   struct lpfc_nodelist *ndlp)
7002 {
7003         struct ls_rjt stat;
7004 
7005         /* For now, unconditionally reject this command */
7006         stat.un.b.lsRjtRsvd0 = 0;
7007         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7008         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7009         stat.un.b.vendorUnique = 0;
7010         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7011         return 0;
7012 }
7013 
7014 /**
7015  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
7016  * @vport: pointer to a host virtual N_Port data structure.
7017  * @cmdiocb: pointer to lpfc command iocb data structure.
7018  * @ndlp: pointer to a node-list data structure.
7019  *
7020  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
7021  * received as an ELS unsolicited event. A request to RRQ shall only
7022  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
7023  * Nx_Port N_Port_ID of the target Exchange is the same as the
7024  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
7025  * not accepted, an LS_RJT with reason code "Unable to perform
7026  * command request" and reason code explanation "Invalid Originator
7027  * S_ID" shall be returned. For now, we just unconditionally accept
7028  * RRQ from the target.
7029  **/
7030 static void
7031 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7032                  struct lpfc_nodelist *ndlp)
7033 {
7034         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7035         if (vport->phba->sli_rev == LPFC_SLI_REV4)
7036                 lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
7037 }
7038 
7039 /**
7040  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
7041  * @phba: pointer to lpfc hba data structure.
7042  * @pmb: pointer to the driver internal queue element for mailbox command.
7043  *
7044  * This routine is the completion callback function for the MBX_READ_LNK_STAT
7045  * mailbox command. This callback function is to actually send the Accept
7046  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
7047  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
7048  * mailbox command, constructs the RPS response with the link statistics
7049  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
7050  * response to the RPS.
7051  *
7052  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7053  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7054  * will be stored into the context1 field of the IOCB for the completion
7055  * callback function to the RPS Accept Response ELS IOCB command.
7056  *
7057  **/
7058 static void
7059 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7060 {
7061         MAILBOX_t *mb;
7062         IOCB_t *icmd;
7063         struct RLS_RSP *rls_rsp;
7064         uint8_t *pcmd;
7065         struct lpfc_iocbq *elsiocb;
7066         struct lpfc_nodelist *ndlp;
7067         uint16_t oxid;
7068         uint16_t rxid;
7069         uint32_t cmdsize;
7070 
7071         mb = &pmb->u.mb;
7072 
7073         ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
7074         rxid = (uint16_t)((unsigned long)(pmb->ctx_buf) & 0xffff);
7075         oxid = (uint16_t)(((unsigned long)(pmb->ctx_buf) >> 16) & 0xffff);
7076         pmb->ctx_buf = NULL;
7077         pmb->ctx_ndlp = NULL;
7078 
7079         if (mb->mbxStatus) {
7080                 mempool_free(pmb, phba->mbox_mem_pool);
7081                 return;
7082         }
7083 
7084         cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
7085         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7086                                      lpfc_max_els_tries, ndlp,
7087                                      ndlp->nlp_DID, ELS_CMD_ACC);
7088 
7089         /* Decrement the ndlp reference count from previous mbox command */
7090         lpfc_nlp_put(ndlp);
7091 
7092         if (!elsiocb) {
7093                 mempool_free(pmb, phba->mbox_mem_pool);
7094                 return;
7095         }
7096 
7097         icmd = &elsiocb->iocb;
7098         icmd->ulpContext = rxid;
7099         icmd->unsli3.rcvsli3.ox_id = oxid;
7100 
7101         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7102         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7103         pcmd += sizeof(uint32_t); /* Skip past command */
7104         rls_rsp = (struct RLS_RSP *)pcmd;
7105 
7106         rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
7107         rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
7108         rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
7109         rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
7110         rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
7111         rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
7112         mempool_free(pmb, phba->mbox_mem_pool);
7113         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
7114         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
7115                          "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
7116                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
7117                          elsiocb->iotag, elsiocb->iocb.ulpContext,
7118                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7119                          ndlp->nlp_rpi);
7120         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7121         phba->fc_stat.elsXmitACC++;
7122         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7123                 lpfc_els_free_iocb(phba, elsiocb);
7124 }
7125 
7126 /**
7127  * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
7128  * @phba: pointer to lpfc hba data structure.
7129  * @pmb: pointer to the driver internal queue element for mailbox command.
7130  *
7131  * This routine is the completion callback function for the MBX_READ_LNK_STAT
7132  * mailbox command. This callback function is to actually send the Accept
7133  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
7134  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
7135  * mailbox command, constructs the RPS response with the link statistics
7136  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
7137  * response to the RPS.
7138  *
7139  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7140  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7141  * will be stored into the context1 field of the IOCB for the completion
7142  * callback function to the RPS Accept Response ELS IOCB command.
7143  *
7144  **/
7145 static void
7146 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7147 {
7148         MAILBOX_t *mb;
7149         IOCB_t *icmd;
7150         RPS_RSP *rps_rsp;
7151         uint8_t *pcmd;
7152         struct lpfc_iocbq *elsiocb;
7153         struct lpfc_nodelist *ndlp;
7154         uint16_t status;
7155         uint16_t oxid;
7156         uint16_t rxid;
7157         uint32_t cmdsize;
7158 
7159         mb = &pmb->u.mb;
7160 
7161         ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
7162         rxid = (uint16_t)((unsigned long)(pmb->ctx_buf) & 0xffff);
7163         oxid = (uint16_t)(((unsigned long)(pmb->ctx_buf) >> 16) & 0xffff);
7164         pmb->ctx_ndlp = NULL;
7165         pmb->ctx_buf = NULL;
7166 
7167         if (mb->mbxStatus) {
7168                 mempool_free(pmb, phba->mbox_mem_pool);
7169                 return;
7170         }
7171 
7172         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
7173         mempool_free(pmb, phba->mbox_mem_pool);
7174         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7175                                      lpfc_max_els_tries, ndlp,
7176                                      ndlp->nlp_DID, ELS_CMD_ACC);
7177 
7178         /* Decrement the ndlp reference count from previous mbox command */
7179         lpfc_nlp_put(ndlp);
7180 
7181         if (!elsiocb)
7182                 return;
7183 
7184         icmd = &elsiocb->iocb;
7185         icmd->ulpContext = rxid;
7186         icmd->unsli3.rcvsli3.ox_id = oxid;
7187 
7188         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7189         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7190         pcmd += sizeof(uint32_t); /* Skip past command */
7191         rps_rsp = (RPS_RSP *)pcmd;
7192 
7193         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
7194                 status = 0x10;
7195         else
7196                 status = 0x8;
7197         if (phba->pport->fc_flag & FC_FABRIC)
7198                 status |= 0x4;
7199 
7200         rps_rsp->rsvd1 = 0;
7201         rps_rsp->portStatus = cpu_to_be16(status);
7202         rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
7203         rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
7204         rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
7205         rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
7206         rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
7207         rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
7208         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
7209         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
7210                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
7211                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
7212                          elsiocb->iotag, elsiocb->iocb.ulpContext,
7213                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7214                          ndlp->nlp_rpi);
7215         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7216         phba->fc_stat.elsXmitACC++;
7217         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7218                 lpfc_els_free_iocb(phba, elsiocb);
7219         return;
7220 }
7221 
7222 /**
7223  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
7224  * @vport: pointer to a host virtual N_Port data structure.
7225  * @cmdiocb: pointer to lpfc command iocb data structure.
7226  * @ndlp: pointer to a node-list data structure.
7227  *
7228  * This routine processes Read Port Status (RPL) IOCB received as an
7229  * ELS unsolicited event. It first checks the remote port state. If the
7230  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
7231  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
7232  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
7233  * for reading the HBA link statistics. It is for the callback function,
7234  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
7235  * to actually sending out RPL Accept (ACC) response.
7236  *
7237  * Return codes
7238  *   0 - Successfully processed rls iocb (currently always return 0)
7239  **/
7240 static int
7241 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7242                  struct lpfc_nodelist *ndlp)
7243 {
7244         struct lpfc_hba *phba = vport->phba;
7245         LPFC_MBOXQ_t *mbox;
7246         struct ls_rjt stat;
7247 
7248         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7249             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7250                 /* reject the unsolicited RPS request and done with it */
7251                 goto reject_out;
7252 
7253         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
7254         if (mbox) {
7255                 lpfc_read_lnk_stat(phba, mbox);
7256                 mbox->ctx_buf = (void *)((unsigned long)
7257                         ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
7258                         cmdiocb->iocb.ulpContext)); /* rx_id */
7259                 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
7260                 mbox->vport = vport;
7261                 mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
7262                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
7263                         != MBX_NOT_FINISHED)
7264                         /* Mbox completion will send ELS Response */
7265                         return 0;
7266                 /* Decrement reference count used for the failed mbox
7267                  * command.
7268                  */
7269                 lpfc_nlp_put(ndlp);
7270                 mempool_free(mbox, phba->mbox_mem_pool);
7271         }
7272 reject_out:
7273         /* issue rejection response */
7274         stat.un.b.lsRjtRsvd0 = 0;
7275         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7276         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7277         stat.un.b.vendorUnique = 0;
7278         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7279         return 0;
7280 }
7281 
7282 /**
7283  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
7284  * @vport: pointer to a host virtual N_Port data structure.
7285  * @cmdiocb: pointer to lpfc command iocb data structure.
7286  * @ndlp: pointer to a node-list data structure.
7287  *
7288  * This routine processes Read Timout Value (RTV) IOCB received as an
7289  * ELS unsolicited event. It first checks the remote port state. If the
7290  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
7291  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
7292  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
7293  * Value (RTV) unsolicited IOCB event.
7294  *
7295  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7296  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7297  * will be stored into the context1 field of the IOCB for the completion
7298  * callback function to the RPS Accept Response ELS IOCB command.
7299  *
7300  * Return codes
7301  *   0 - Successfully processed rtv iocb (currently always return 0)
7302  **/
7303 static int
7304 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7305                  struct lpfc_nodelist *ndlp)
7306 {
7307         struct lpfc_hba *phba = vport->phba;
7308         struct ls_rjt stat;
7309         struct RTV_RSP *rtv_rsp;
7310         uint8_t *pcmd;
7311         struct lpfc_iocbq *elsiocb;
7312         uint32_t cmdsize;
7313 
7314 
7315         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7316             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7317                 /* reject the unsolicited RPS request and done with it */
7318                 goto reject_out;
7319 
7320         cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
7321         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7322                                      lpfc_max_els_tries, ndlp,
7323                                      ndlp->nlp_DID, ELS_CMD_ACC);
7324 
7325         if (!elsiocb)
7326                 return 1;
7327 
7328         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7329         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7330         pcmd += sizeof(uint32_t); /* Skip past command */
7331 
7332         /* use the command's xri in the response */
7333         elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext;  /* Xri / rx_id */
7334         elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
7335 
7336         rtv_rsp = (struct RTV_RSP *)pcmd;
7337 
7338         /* populate RTV payload */
7339         rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
7340         rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
7341         bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
7342         bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
7343         rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
7344 
7345         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
7346         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
7347                          "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
7348                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
7349                          "Data: x%x x%x x%x\n",
7350                          elsiocb->iotag, elsiocb->iocb.ulpContext,
7351                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7352                          ndlp->nlp_rpi,
7353                         rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
7354         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7355         phba->fc_stat.elsXmitACC++;
7356         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7357                 lpfc_els_free_iocb(phba, elsiocb);
7358         return 0;
7359 
7360 reject_out:
7361         /* issue rejection response */
7362         stat.un.b.lsRjtRsvd0 = 0;
7363         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7364         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7365         stat.un.b.vendorUnique = 0;
7366         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7367         return 0;
7368 }
7369 
7370 /* lpfc_els_rcv_rps - Process an unsolicited rps iocb
7371  * @vport: pointer to a host virtual N_Port data structure.
7372  * @cmdiocb: pointer to lpfc command iocb data structure.
7373  * @ndlp: pointer to a node-list data structure.
7374  *
7375  * This routine processes Read Port Status (RPS) IOCB received as an
7376  * ELS unsolicited event. It first checks the remote port state. If the
7377  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
7378  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
7379  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
7380  * for reading the HBA link statistics. It is for the callback function,
7381  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
7382  * to actually sending out RPS Accept (ACC) response.
7383  *
7384  * Return codes
7385  *   0 - Successfully processed rps iocb (currently always return 0)
7386  **/
7387 static int
7388 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7389                  struct lpfc_nodelist *ndlp)
7390 {
7391         struct lpfc_hba *phba = vport->phba;
7392         uint32_t *lp;
7393         uint8_t flag;
7394         LPFC_MBOXQ_t *mbox;
7395         struct lpfc_dmabuf *pcmd;
7396         RPS *rps;
7397         struct ls_rjt stat;
7398 
7399         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7400             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7401                 /* reject the unsolicited RPS request and done with it */
7402                 goto reject_out;
7403 
7404         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7405         lp = (uint32_t *) pcmd->virt;
7406         flag = (be32_to_cpu(*lp++) & 0xf);
7407         rps = (RPS *) lp;
7408 
7409         if ((flag == 0) ||
7410             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
7411             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
7412                                     sizeof(struct lpfc_name)) == 0))) {
7413 
7414                 printk("Fix me....\n");
7415                 dump_stack();
7416                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
7417                 if (mbox) {
7418                         lpfc_read_lnk_stat(phba, mbox);
7419                         mbox->ctx_buf = (void *)((unsigned long)
7420                                 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
7421                                 cmdiocb->iocb.ulpContext)); /* rx_id */
7422                         mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
7423                         mbox->vport = vport;
7424                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
7425                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
7426                                 != MBX_NOT_FINISHED)
7427                                 /* Mbox completion will send ELS Response */
7428                                 return 0;
7429                         /* Decrement reference count used for the failed mbox
7430                          * command.
7431                          */
7432                         lpfc_nlp_put(ndlp);
7433                         mempool_free(mbox, phba->mbox_mem_pool);
7434                 }
7435         }
7436 
7437 reject_out:
7438         /* issue rejection response */
7439         stat.un.b.lsRjtRsvd0 = 0;
7440         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7441         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7442         stat.un.b.vendorUnique = 0;
7443         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7444         return 0;
7445 }
7446 
7447 /* lpfc_issue_els_rrq - Process an unsolicited rps iocb
7448  * @vport: pointer to a host virtual N_Port data structure.
7449  * @ndlp: pointer to a node-list data structure.
7450  * @did: DID of the target.
7451  * @rrq: Pointer to the rrq struct.
7452  *
7453  * Build a ELS RRQ command and send it to the target. If the issue_iocb is
7454  * Successful the the completion handler will clear the RRQ.
7455  *
7456  * Return codes
7457  *   0 - Successfully sent rrq els iocb.
7458  *   1 - Failed to send rrq els iocb.
7459  **/
7460 static int
7461 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
7462                         uint32_t did, struct lpfc_node_rrq *rrq)
7463 {
7464         struct lpfc_hba  *phba = vport->phba;
7465         struct RRQ *els_rrq;
7466         struct lpfc_iocbq *elsiocb;
7467         uint8_t *pcmd;
7468         uint16_t cmdsize;
7469         int ret;
7470 
7471 
7472         if (ndlp != rrq->ndlp)
7473                 ndlp = rrq->ndlp;
7474         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
7475                 return 1;
7476 
7477         /* If ndlp is not NULL, we will bump the reference count on it */
7478         cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
7479         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
7480                                      ELS_CMD_RRQ);
7481         if (!elsiocb)
7482                 return 1;
7483 
7484         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7485 
7486         /* For RRQ request, remainder of payload is Exchange IDs */
7487         *((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
7488         pcmd += sizeof(uint32_t);
7489         els_rrq = (struct RRQ *) pcmd;
7490 
7491         bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
7492         bf_set(rrq_rxid, els_rrq, rrq->rxid);
7493         bf_set(rrq_did, els_rrq, vport->fc_myDID);
7494         els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
7495         els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
7496 
7497 
7498         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7499                 "Issue RRQ:     did:x%x",
7500                 did, rrq->xritag, rrq->rxid);
7501         elsiocb->context_un.rrq = rrq;
7502         elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
7503         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7504 
7505         if (ret == IOCB_ERROR) {
7506                 lpfc_els_free_iocb(phba, elsiocb);
7507                 return 1;
7508         }
7509         return 0;
7510 }
7511 
7512 /**
7513  * lpfc_send_rrq - Sends ELS RRQ if needed.
7514  * @phba: pointer to lpfc hba data structure.
7515  * @rrq: pointer to the active rrq.
7516  *
7517  * This routine will call the lpfc_issue_els_rrq if the rrq is
7518  * still active for the xri. If this function returns a failure then
7519  * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
7520  *
7521  * Returns 0 Success.
7522  *         1 Failure.
7523  **/
7524 int
7525 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
7526 {
7527         struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
7528                                                        rrq->nlp_DID);
7529         if (!ndlp)
7530                 return 1;
7531 
7532         if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
7533                 return lpfc_issue_els_rrq(rrq->vport, ndlp,
7534                                          rrq->nlp_DID, rrq);
7535         else
7536                 return 1;
7537 }
7538 
7539 /**
7540  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
7541  * @vport: pointer to a host virtual N_Port data structure.
7542  * @cmdsize: size of the ELS command.
7543  * @oldiocb: pointer to the original lpfc command iocb data structure.
7544  * @ndlp: pointer to a node-list data structure.
7545  *
7546  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
7547  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
7548  *
7549  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7550  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7551  * will be stored into the context1 field of the IOCB for the completion
7552  * callback function to the RPL Accept Response ELS command.
7553  *
7554  * Return code
7555  *   0 - Successfully issued ACC RPL ELS command
7556  *   1 - Failed to issue ACC RPL ELS command
7557  **/
7558 static int
7559 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
7560                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
7561 {
7562         struct lpfc_hba *phba = vport->phba;
7563         IOCB_t *icmd, *oldcmd;
7564         RPL_RSP rpl_rsp;
7565         struct lpfc_iocbq *elsiocb;
7566         uint8_t *pcmd;
7567 
7568         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
7569                                      ndlp->nlp_DID, ELS_CMD_ACC);
7570 
7571         if (!elsiocb)
7572                 return 1;
7573 
7574         icmd = &elsiocb->iocb;
7575         oldcmd = &oldiocb->iocb;
7576         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
7577         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
7578 
7579         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7580         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7581         pcmd += sizeof(uint16_t);
7582         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
7583         pcmd += sizeof(uint16_t);
7584 
7585         /* Setup the RPL ACC payload */
7586         rpl_rsp.listLen = be32_to_cpu(1);
7587         rpl_rsp.index = 0;
7588         rpl_rsp.port_num_blk.portNum = 0;
7589         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
7590         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
7591             sizeof(struct lpfc_name));
7592         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
7593         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
7594         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7595                          "0120 Xmit ELS RPL ACC response tag x%x "
7596                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
7597                          "rpi x%x\n",
7598                          elsiocb->iotag, elsiocb->iocb.ulpContext,
7599                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7600                          ndlp->nlp_rpi);
7601         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7602         phba->fc_stat.elsXmitACC++;
7603         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
7604             IOCB_ERROR) {
7605                 lpfc_els_free_iocb(phba, elsiocb);
7606                 return 1;
7607         }
7608         return 0;
7609 }
7610 
7611 /**
7612  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
7613  * @vport: pointer to a host virtual N_Port data structure.
7614  * @cmdiocb: pointer to lpfc command iocb data structure.
7615  * @ndlp: pointer to a node-list data structure.
7616  *
7617  * This routine processes Read Port List (RPL) IOCB received as an ELS
7618  * unsolicited event. It first checks the remote port state. If the remote
7619  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
7620  * invokes the lpfc_els_rsp_reject() routine to send reject response.
7621  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
7622  * to accept the RPL.
7623  *
7624  * Return code
7625  *   0 - Successfully processed rpl iocb (currently always return 0)
7626  **/
7627 static int
7628 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7629                  struct lpfc_nodelist *ndlp)
7630 {
7631         struct lpfc_dmabuf *pcmd;
7632         uint32_t *lp;
7633         uint32_t maxsize;
7634         uint16_t cmdsize;
7635         RPL *rpl;
7636         struct ls_rjt stat;
7637 
7638         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7639             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
7640                 /* issue rejection response */
7641                 stat.un.b.lsRjtRsvd0 = 0;
7642                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7643                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7644                 stat.un.b.vendorUnique = 0;
7645                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
7646                         NULL);
7647                 /* rejected the unsolicited RPL request and done with it */
7648                 return 0;
7649         }
7650 
7651         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7652         lp = (uint32_t *) pcmd->virt;
7653         rpl = (RPL *) (lp + 1);
7654         maxsize = be32_to_cpu(rpl->maxsize);
7655 
7656         /* We support only one port */
7657         if ((rpl->index == 0) &&
7658             ((maxsize == 0) ||
7659              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
7660                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
7661         } else {
7662                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
7663         }
7664         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
7665 
7666         return 0;
7667 }
7668 
7669 /**
7670  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
7671  * @vport: pointer to a virtual N_Port data structure.
7672  * @cmdiocb: pointer to lpfc command iocb data structure.
7673  * @ndlp: pointer to a node-list data structure.
7674  *
7675  * This routine processes Fibre Channel Address Resolution Protocol
7676  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
7677  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
7678  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
7679  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
7680  * remote PortName is compared against the FC PortName stored in the @vport
7681  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
7682  * compared against the FC NodeName stored in the @vport data structure.
7683  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
7684  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
7685  * invoked to send out FARP Response to the remote node. Before sending the
7686  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
7687  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
7688  * routine is invoked to log into the remote port first.
7689  *
7690  * Return code
7691  *   0 - Either the FARP Match Mode not supported or successfully processed
7692  **/
7693 static int
7694 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7695                   struct lpfc_nodelist *ndlp)
7696 {
7697         struct lpfc_dmabuf *pcmd;
7698         uint32_t *lp;
7699         IOCB_t *icmd;
7700         FARP *fp;
7701         uint32_t cnt, did;
7702 
7703         icmd = &cmdiocb->iocb;
7704         did = icmd->un.elsreq64.remoteID;
7705         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7706         lp = (uint32_t *) pcmd->virt;
7707 
7708         lp++;
7709         fp = (FARP *) lp;
7710         /* FARP-REQ received from DID <did> */
7711         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7712                          "0601 FARP-REQ received from DID x%x\n", did);
7713         /* We will only support match on WWPN or WWNN */
7714         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
7715                 return 0;
7716         }
7717 
7718         cnt = 0;
7719         /* If this FARP command is searching for my portname */
7720         if (fp->Mflags & FARP_MATCH_PORT) {
7721                 if (memcmp(&fp->RportName, &vport->fc_portname,
7722                            sizeof(struct lpfc_name)) == 0)
7723                         cnt = 1;
7724         }
7725 
7726         /* If this FARP command is searching for my nodename */
7727         if (fp->Mflags & FARP_MATCH_NODE) {
7728                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
7729                            sizeof(struct lpfc_name)) == 0)
7730                         cnt = 1;
7731         }
7732 
7733         if (cnt) {
7734                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
7735                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
7736                         /* Log back into the node before sending the FARP. */
7737                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
7738                                 ndlp->nlp_prev_state = ndlp->nlp_state;
7739                                 lpfc_nlp_set_state(vport, ndlp,
7740                                                    NLP_STE_PLOGI_ISSUE);
7741                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
7742                         }
7743 
7744                         /* Send a FARP response to that node */
7745                         if (fp->Rflags & FARP_REQUEST_FARPR)
7746                                 lpfc_issue_els_farpr(vport, did, 0);
7747                 }
7748         }
7749         return 0;
7750 }
7751 
7752 /**
7753  * lpfc_els_rcv_farpr - Process an unsolicited farp response 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 Fibre Channel Address Resolution Protocol
7759  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
7760  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
7761  * the FARP response request.
7762  *
7763  * Return code
7764  *   0 - Successfully processed FARPR IOCB (currently always return 0)
7765  **/
7766 static int
7767 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7768                    struct lpfc_nodelist  *ndlp)
7769 {
7770         struct lpfc_dmabuf *pcmd;
7771         uint32_t *lp;
7772         IOCB_t *icmd;
7773         uint32_t did;
7774 
7775         icmd = &cmdiocb->iocb;
7776         did = icmd->un.elsreq64.remoteID;
7777         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7778         lp = (uint32_t *) pcmd->virt;
7779 
7780         lp++;
7781         /* FARP-RSP received from DID <did> */
7782         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7783                          "0600 FARP-RSP received from DID x%x\n", did);
7784         /* ACCEPT the Farp resp request */
7785         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7786 
7787         return 0;
7788 }
7789 
7790 /**
7791  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
7792  * @vport: pointer to a host virtual N_Port data structure.
7793  * @cmdiocb: pointer to lpfc command iocb data structure.
7794  * @fan_ndlp: pointer to a node-list data structure.
7795  *
7796  * This routine processes a Fabric Address Notification (FAN) IOCB
7797  * command received as an ELS unsolicited event. The FAN ELS command will
7798  * only be processed on a physical port (i.e., the @vport represents the
7799  * physical port). The fabric NodeName and PortName from the FAN IOCB are
7800  * compared against those in the phba data structure. If any of those is
7801  * different, the lpfc_initial_flogi() routine is invoked to initialize
7802  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
7803  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
7804  * is invoked to register login to the fabric.
7805  *
7806  * Return code
7807  *   0 - Successfully processed fan iocb (currently always return 0).
7808  **/
7809 static int
7810 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7811                  struct lpfc_nodelist *fan_ndlp)
7812 {
7813         struct lpfc_hba *phba = vport->phba;
7814         uint32_t *lp;
7815         FAN *fp;
7816 
7817         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
7818         lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
7819         fp = (FAN *) ++lp;
7820         /* FAN received; Fan does not have a reply sequence */
7821         if ((vport == phba->pport) &&
7822             (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
7823                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
7824                             sizeof(struct lpfc_name))) ||
7825                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
7826                             sizeof(struct lpfc_name)))) {
7827                         /* This port has switched fabrics. FLOGI is required */
7828                         lpfc_issue_init_vfi(vport);
7829                 } else {
7830                         /* FAN verified - skip FLOGI */
7831                         vport->fc_myDID = vport->fc_prevDID;
7832                         if (phba->sli_rev < LPFC_SLI_REV4)
7833                                 lpfc_issue_fabric_reglogin(vport);
7834                         else {
7835                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7836                                         "3138 Need register VFI: (x%x/%x)\n",
7837                                         vport->fc_prevDID, vport->fc_myDID);
7838                                 lpfc_issue_reg_vfi(vport);
7839                         }
7840                 }
7841         }
7842         return 0;
7843 }
7844 
7845 /**
7846  * lpfc_els_timeout - Handler funciton to the els timer
7847  * @ptr: holder for the timer function associated data.
7848  *
7849  * This routine is invoked by the ELS timer after timeout. It posts the ELS
7850  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
7851  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
7852  * up the worker thread. It is for the worker thread to invoke the routine
7853  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
7854  **/
7855 void
7856 lpfc_els_timeout(struct timer_list *t)
7857 {
7858         struct lpfc_vport *vport = from_timer(vport, t, els_tmofunc);
7859         struct lpfc_hba   *phba = vport->phba;
7860         uint32_t tmo_posted;
7861         unsigned long iflag;
7862 
7863         spin_lock_irqsave(&vport->work_port_lock, iflag);
7864         tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
7865         if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
7866                 vport->work_port_events |= WORKER_ELS_TMO;
7867         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
7868 
7869         if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
7870                 lpfc_worker_wake_up(phba);
7871         return;
7872 }
7873 
7874 
7875 /**
7876  * lpfc_els_timeout_handler - Process an els timeout event
7877  * @vport: pointer to a virtual N_Port data structure.
7878  *
7879  * This routine is the actual handler function that processes an ELS timeout
7880  * event. It walks the ELS ring to get and abort all the IOCBs (except the
7881  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
7882  * invoking the lpfc_sli_issue_abort_iotag() routine.
7883  **/
7884 void
7885 lpfc_els_timeout_handler(struct lpfc_vport *vport)
7886 {
7887         struct lpfc_hba  *phba = vport->phba;
7888         struct lpfc_sli_ring *pring;
7889         struct lpfc_iocbq *tmp_iocb, *piocb;
7890         IOCB_t *cmd = NULL;
7891         struct lpfc_dmabuf *pcmd;
7892         uint32_t els_command = 0;
7893         uint32_t timeout;
7894         uint32_t remote_ID = 0xffffffff;
7895         LIST_HEAD(abort_list);
7896 
7897 
7898         timeout = (uint32_t)(phba->fc_ratov << 1);
7899 
7900         pring = lpfc_phba_elsring(phba);
7901         if (unlikely(!pring))
7902                 return;
7903 
7904         if ((phba->pport->load_flag & FC_UNLOADING))
7905                 return;
7906         spin_lock_irq(&phba->hbalock);
7907         if (phba->sli_rev == LPFC_SLI_REV4)
7908                 spin_lock(&pring->ring_lock);
7909 
7910         if ((phba->pport->load_flag & FC_UNLOADING)) {
7911                 if (phba->sli_rev == LPFC_SLI_REV4)
7912                         spin_unlock(&pring->ring_lock);
7913                 spin_unlock_irq(&phba->hbalock);
7914                 return;
7915         }
7916 
7917         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
7918                 cmd = &piocb->iocb;
7919 
7920                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
7921                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
7922                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
7923                         continue;
7924 
7925                 if (piocb->vport != vport)
7926                         continue;
7927 
7928                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
7929                 if (pcmd)
7930                         els_command = *(uint32_t *) (pcmd->virt);
7931 
7932                 if (els_command == ELS_CMD_FARP ||
7933                     els_command == ELS_CMD_FARPR ||
7934                     els_command == ELS_CMD_FDISC)
7935                         continue;
7936 
7937                 if (piocb->drvrTimeout > 0) {
7938                         if (piocb->drvrTimeout >= timeout)
7939                                 piocb->drvrTimeout -= timeout;
7940                         else
7941                                 piocb->drvrTimeout = 0;
7942                         continue;
7943                 }
7944 
7945                 remote_ID = 0xffffffff;
7946                 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
7947                         remote_ID = cmd->un.elsreq64.remoteID;
7948                 else {
7949                         struct lpfc_nodelist *ndlp;
7950                         ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
7951                         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
7952                                 remote_ID = ndlp->nlp_DID;
7953                 }
7954                 list_add_tail(&piocb->dlist, &abort_list);
7955         }
7956         if (phba->sli_rev == LPFC_SLI_REV4)
7957                 spin_unlock(&pring->ring_lock);
7958         spin_unlock_irq(&phba->hbalock);
7959 
7960         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
7961                 cmd = &piocb->iocb;
7962                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7963                          "0127 ELS timeout Data: x%x x%x x%x "
7964                          "x%x\n", els_command,
7965                          remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
7966                 spin_lock_irq(&phba->hbalock);
7967                 list_del_init(&piocb->dlist);
7968                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
7969                 spin_unlock_irq(&phba->hbalock);
7970         }
7971 
7972         if (!list_empty(&pring->txcmplq))
7973                 if (!(phba->pport->load_flag & FC_UNLOADING))
7974                         mod_timer(&vport->els_tmofunc,
7975                                   jiffies + msecs_to_jiffies(1000 * timeout));
7976 }
7977 
7978 /**
7979  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
7980  * @vport: pointer to a host virtual N_Port data structure.
7981  *
7982  * This routine is used to clean up all the outstanding ELS commands on a
7983  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
7984  * routine. After that, it walks the ELS transmit queue to remove all the
7985  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
7986  * the IOCBs with a non-NULL completion callback function, the callback
7987  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
7988  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
7989  * callback function, the IOCB will simply be released. Finally, it walks
7990  * the ELS transmit completion queue to issue an abort IOCB to any transmit
7991  * completion queue IOCB that is associated with the @vport and is not
7992  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
7993  * part of the discovery state machine) out to HBA by invoking the
7994  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
7995  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
7996  * the IOCBs are aborted when this function returns.
7997  **/
7998 void
7999 lpfc_els_flush_cmd(struct lpfc_vport *vport)
8000 {
8001         LIST_HEAD(abort_list);
8002         struct lpfc_hba  *phba = vport->phba;
8003         struct lpfc_sli_ring *pring;
8004         struct lpfc_iocbq *tmp_iocb, *piocb;
8005         IOCB_t *cmd = NULL;
8006         unsigned long iflags = 0;
8007 
8008         lpfc_fabric_abort_vport(vport);
8009 
8010         /*
8011          * For SLI3, only the hbalock is required.  But SLI4 needs to coordinate
8012          * with the ring insert operation.  Because lpfc_sli_issue_abort_iotag
8013          * ultimately grabs the ring_lock, the driver must splice the list into
8014          * a working list and release the locks before calling the abort.
8015          */
8016         spin_lock_irqsave(&phba->hbalock, iflags);
8017         pring = lpfc_phba_elsring(phba);
8018 
8019         /* Bail out if we've no ELS wq, like in PCI error recovery case. */
8020         if (unlikely(!pring)) {
8021                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8022                 return;
8023         }
8024 
8025         if (phba->sli_rev == LPFC_SLI_REV4)
8026                 spin_lock(&pring->ring_lock);
8027 
8028         /* First we need to issue aborts to outstanding cmds on txcmpl */
8029         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
8030                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
8031                         continue;
8032 
8033                 if (piocb->vport != vport)
8034                         continue;
8035 
8036                 if (piocb->iocb_flag & LPFC_DRIVER_ABORTED)
8037                         continue;
8038 
8039                 /* On the ELS ring we can have ELS_REQUESTs or
8040                  * GEN_REQUESTs waiting for a response.
8041                  */
8042                 cmd = &piocb->iocb;
8043                 if (cmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
8044                         list_add_tail(&piocb->dlist, &abort_list);
8045 
8046                         /* If the link is down when flushing ELS commands
8047                          * the firmware will not complete them till after
8048                          * the link comes back up. This may confuse
8049                          * discovery for the new link up, so we need to
8050                          * change the compl routine to just clean up the iocb
8051                          * and avoid any retry logic.
8052                          */
8053                         if (phba->link_state == LPFC_LINK_DOWN)
8054                                 piocb->iocb_cmpl = lpfc_cmpl_els_link_down;
8055                 }
8056                 if (cmd->ulpCommand == CMD_GEN_REQUEST64_CR)
8057                         list_add_tail(&piocb->dlist, &abort_list);
8058         }
8059 
8060         if (phba->sli_rev == LPFC_SLI_REV4)
8061                 spin_unlock(&pring->ring_lock);
8062         spin_unlock_irqrestore(&phba->hbalock, iflags);
8063 
8064         /* Abort each txcmpl iocb on aborted list and remove the dlist links. */
8065         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
8066                 spin_lock_irqsave(&phba->hbalock, iflags);
8067                 list_del_init(&piocb->dlist);
8068                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
8069                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8070         }
8071         if (!list_empty(&abort_list))
8072                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8073                                  "3387 abort list for txq not empty\n");
8074         INIT_LIST_HEAD(&abort_list);
8075 
8076         spin_lock_irqsave(&phba->hbalock, iflags);
8077         if (phba->sli_rev == LPFC_SLI_REV4)
8078                 spin_lock(&pring->ring_lock);
8079 
8080         /* No need to abort the txq list,
8081          * just queue them up for lpfc_sli_cancel_iocbs
8082          */
8083         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
8084                 cmd = &piocb->iocb;
8085 
8086                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
8087                         continue;
8088                 }
8089 
8090                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
8091                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
8092                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
8093                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8094                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
8095                         continue;
8096 
8097                 if (piocb->vport != vport)
8098                         continue;
8099 
8100                 list_del_init(&piocb->list);
8101                 list_add_tail(&piocb->list, &abort_list);
8102         }
8103 
8104         /* The same holds true for any FLOGI/FDISC on the fabric_iocb_list */
8105         if (vport == phba->pport) {
8106                 list_for_each_entry_safe(piocb, tmp_iocb,
8107                                          &phba->fabric_iocb_list, list) {
8108                         cmd = &piocb->iocb;
8109                         list_del_init(&piocb->list);
8110                         list_add_tail(&piocb->list, &abort_list);
8111                 }
8112         }
8113 
8114         if (phba->sli_rev == LPFC_SLI_REV4)
8115                 spin_unlock(&pring->ring_lock);
8116         spin_unlock_irqrestore(&phba->hbalock, iflags);
8117 
8118         /* Cancel all the IOCBs from the completions list */
8119         lpfc_sli_cancel_iocbs(phba, &abort_list,
8120                               IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
8121 
8122         return;
8123 }
8124 
8125 /**
8126  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
8127  * @phba: pointer to lpfc hba data structure.
8128  *
8129  * This routine is used to clean up all the outstanding ELS commands on a
8130  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
8131  * routine. After that, it walks the ELS transmit queue to remove all the
8132  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
8133  * the IOCBs with the completion callback function associated, the callback
8134  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
8135  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
8136  * callback function associated, the IOCB will simply be released. Finally,
8137  * it walks the ELS transmit completion queue to issue an abort IOCB to any
8138  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
8139  * management plane IOCBs that are not part of the discovery state machine)
8140  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
8141  **/
8142 void
8143 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
8144 {
8145         struct lpfc_vport *vport;
8146 
8147         spin_lock_irq(&phba->port_list_lock);
8148         list_for_each_entry(vport, &phba->port_list, listentry)
8149                 lpfc_els_flush_cmd(vport);
8150         spin_unlock_irq(&phba->port_list_lock);
8151 
8152         return;
8153 }
8154 
8155 /**
8156  * lpfc_send_els_failure_event - Posts an ELS command failure event
8157  * @phba: Pointer to hba context object.
8158  * @cmdiocbp: Pointer to command iocb which reported error.
8159  * @rspiocbp: Pointer to response iocb which reported error.
8160  *
8161  * This function sends an event when there is an ELS command
8162  * failure.
8163  **/
8164 void
8165 lpfc_send_els_failure_event(struct lpfc_hba *phba,
8166                         struct lpfc_iocbq *cmdiocbp,
8167                         struct lpfc_iocbq *rspiocbp)
8168 {
8169         struct lpfc_vport *vport = cmdiocbp->vport;
8170         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8171         struct lpfc_lsrjt_event lsrjt_event;
8172         struct lpfc_fabric_event_header fabric_event;
8173         struct ls_rjt stat;
8174         struct lpfc_nodelist *ndlp;
8175         uint32_t *pcmd;
8176 
8177         ndlp = cmdiocbp->context1;
8178         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8179                 return;
8180 
8181         if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
8182                 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
8183                 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
8184                 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
8185                         sizeof(struct lpfc_name));
8186                 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
8187                         sizeof(struct lpfc_name));
8188                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8189                         cmdiocbp->context2)->virt);
8190                 lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
8191                 stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
8192                 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
8193                 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
8194                 fc_host_post_vendor_event(shost,
8195                         fc_get_event_number(),
8196                         sizeof(lsrjt_event),
8197                         (char *)&lsrjt_event,
8198                         LPFC_NL_VENDOR_ID);
8199                 return;
8200         }
8201         if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
8202                 (rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
8203                 fabric_event.event_type = FC_REG_FABRIC_EVENT;
8204                 if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
8205                         fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
8206                 else
8207                         fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
8208                 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
8209                         sizeof(struct lpfc_name));
8210                 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
8211                         sizeof(struct lpfc_name));
8212                 fc_host_post_vendor_event(shost,
8213                         fc_get_event_number(),
8214                         sizeof(fabric_event),
8215                         (char *)&fabric_event,
8216                         LPFC_NL_VENDOR_ID);
8217                 return;
8218         }
8219 
8220 }
8221 
8222 /**
8223  * lpfc_send_els_event - Posts unsolicited els event
8224  * @vport: Pointer to vport object.
8225  * @ndlp: Pointer FC node object.
8226  * @cmd: ELS command code.
8227  *
8228  * This function posts an event when there is an incoming
8229  * unsolicited ELS command.
8230  **/
8231 static void
8232 lpfc_send_els_event(struct lpfc_vport *vport,
8233                     struct lpfc_nodelist *ndlp,
8234                     uint32_t *payload)
8235 {
8236         struct lpfc_els_event_header *els_data = NULL;
8237         struct lpfc_logo_event *logo_data = NULL;
8238         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8239 
8240         if (*payload == ELS_CMD_LOGO) {
8241                 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
8242                 if (!logo_data) {
8243                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8244                                 "0148 Failed to allocate memory "
8245                                 "for LOGO event\n");
8246                         return;
8247                 }
8248                 els_data = &logo_data->header;
8249         } else {
8250                 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
8251                         GFP_KERNEL);
8252                 if (!els_data) {
8253                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8254                                 "0149 Failed to allocate memory "
8255                                 "for ELS event\n");
8256                         return;
8257                 }
8258         }
8259         els_data->event_type = FC_REG_ELS_EVENT;
8260         switch (*payload) {
8261         case ELS_CMD_PLOGI:
8262                 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
8263                 break;
8264         case ELS_CMD_PRLO:
8265                 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
8266                 break;
8267         case ELS_CMD_ADISC:
8268                 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
8269                 break;
8270         case ELS_CMD_LOGO:
8271                 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
8272                 /* Copy the WWPN in the LOGO payload */
8273                 memcpy(logo_data->logo_wwpn, &payload[2],
8274                         sizeof(struct lpfc_name));
8275                 break;
8276         default:
8277                 kfree(els_data);
8278                 return;
8279         }
8280         memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
8281         memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
8282         if (*payload == ELS_CMD_LOGO) {
8283                 fc_host_post_vendor_event(shost,
8284                         fc_get_event_number(),
8285                         sizeof(struct lpfc_logo_event),
8286                         (char *)logo_data,
8287                         LPFC_NL_VENDOR_ID);
8288                 kfree(logo_data);
8289         } else {
8290                 fc_host_post_vendor_event(shost,
8291                         fc_get_event_number(),
8292                         sizeof(struct lpfc_els_event_header),
8293                         (char *)els_data,
8294                         LPFC_NL_VENDOR_ID);
8295                 kfree(els_data);
8296         }
8297 
8298         return;
8299 }
8300 
8301 
8302 /**
8303  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
8304  * @phba: pointer to lpfc hba data structure.
8305  * @pring: pointer to a SLI ring.
8306  * @vport: pointer to a host virtual N_Port data structure.
8307  * @elsiocb: pointer to lpfc els command iocb data structure.
8308  *
8309  * This routine is used for processing the IOCB associated with a unsolicited
8310  * event. It first determines whether there is an existing ndlp that matches
8311  * the DID from the unsolicited IOCB. If not, it will create a new one with
8312  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
8313  * IOCB is then used to invoke the proper routine and to set up proper state
8314  * of the discovery state machine.
8315  **/
8316 static void
8317 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8318                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
8319 {
8320         struct Scsi_Host  *shost;
8321         struct lpfc_nodelist *ndlp;
8322         struct ls_rjt stat;
8323         uint32_t *payload;
8324         uint32_t cmd, did, newnode;
8325         uint8_t rjt_exp, rjt_err = 0, init_link = 0;
8326         IOCB_t *icmd = &elsiocb->iocb;
8327         LPFC_MBOXQ_t *mbox;
8328 
8329         if (!vport || !(elsiocb->context2))
8330                 goto dropit;
8331 
8332         newnode = 0;
8333         payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
8334         cmd = *payload;
8335         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
8336                 lpfc_post_buffer(phba, pring, 1);
8337 
8338         did = icmd->un.rcvels.remoteID;
8339         if (icmd->ulpStatus) {
8340                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8341                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
8342                         icmd->ulpStatus, icmd->un.ulpWord[4], did);
8343                 goto dropit;
8344         }
8345 
8346         /* Check to see if link went down during discovery */
8347         if (lpfc_els_chk_latt(vport))
8348                 goto dropit;
8349 
8350         /* Ignore traffic received during vport shutdown. */
8351         if (vport->load_flag & FC_UNLOADING)
8352                 goto dropit;
8353 
8354         /* If NPort discovery is delayed drop incoming ELS */
8355         if ((vport->fc_flag & FC_DISC_DELAYED) &&
8356                         (cmd != ELS_CMD_PLOGI))
8357                 goto dropit;
8358 
8359         ndlp = lpfc_findnode_did(vport, did);
8360         if (!ndlp) {
8361                 /* Cannot find existing Fabric ndlp, so allocate a new one */
8362                 ndlp = lpfc_nlp_init(vport, did);
8363                 if (!ndlp)
8364                         goto dropit;
8365                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
8366                 newnode = 1;
8367                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
8368                         ndlp->nlp_type |= NLP_FABRIC;
8369         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
8370                 ndlp = lpfc_enable_node(vport, ndlp,
8371                                         NLP_STE_UNUSED_NODE);
8372                 if (!ndlp)
8373                         goto dropit;
8374                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
8375                 newnode = 1;
8376                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
8377                         ndlp->nlp_type |= NLP_FABRIC;
8378         } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
8379                 /* This is similar to the new node path */
8380                 ndlp = lpfc_nlp_get(ndlp);
8381                 if (!ndlp)
8382                         goto dropit;
8383                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
8384                 newnode = 1;
8385         }
8386 
8387         phba->fc_stat.elsRcvFrame++;
8388 
8389         /*
8390          * Do not process any unsolicited ELS commands
8391          * if the ndlp is in DEV_LOSS
8392          */
8393         shost = lpfc_shost_from_vport(vport);
8394         spin_lock_irq(shost->host_lock);
8395         if (ndlp->nlp_flag & NLP_IN_DEV_LOSS) {
8396                 spin_unlock_irq(shost->host_lock);
8397                 goto dropit;
8398         }
8399         spin_unlock_irq(shost->host_lock);
8400 
8401         elsiocb->context1 = lpfc_nlp_get(ndlp);
8402         elsiocb->vport = vport;
8403 
8404         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
8405                 cmd &= ELS_CMD_MASK;
8406         }
8407         /* ELS command <elsCmd> received from NPORT <did> */
8408         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8409                          "0112 ELS command x%x received from NPORT x%x "
8410                          "Data: x%x x%x x%x x%x\n",
8411                         cmd, did, vport->port_state, vport->fc_flag,
8412                         vport->fc_myDID, vport->fc_prevDID);
8413 
8414         /* reject till our FLOGI completes or PLOGI assigned DID via PT2PT */
8415         if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
8416             (cmd != ELS_CMD_FLOGI) &&
8417             !((cmd == ELS_CMD_PLOGI) && (vport->fc_flag & FC_PT2PT))) {
8418                 rjt_err = LSRJT_LOGICAL_BSY;
8419                 rjt_exp = LSEXP_NOTHING_MORE;
8420                 goto lsrjt;
8421         }
8422 
8423         switch (cmd) {
8424         case ELS_CMD_PLOGI:
8425                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8426                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
8427                         did, vport->port_state, ndlp->nlp_flag);
8428 
8429                 phba->fc_stat.elsRcvPLOGI++;
8430                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
8431                 if (phba->sli_rev == LPFC_SLI_REV4 &&
8432                     (phba->pport->fc_flag & FC_PT2PT)) {
8433                         vport->fc_prevDID = vport->fc_myDID;
8434                         /* Our DID needs to be updated before registering
8435                          * the vfi. This is done in lpfc_rcv_plogi but
8436                          * that is called after the reg_vfi.
8437                          */
8438                         vport->fc_myDID = elsiocb->iocb.un.rcvels.parmRo;
8439                         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8440                                          "3312 Remote port assigned DID x%x "
8441                                          "%x\n", vport->fc_myDID,
8442                                          vport->fc_prevDID);
8443                 }
8444 
8445                 lpfc_send_els_event(vport, ndlp, payload);
8446 
8447                 /* If Nport discovery is delayed, reject PLOGIs */
8448                 if (vport->fc_flag & FC_DISC_DELAYED) {
8449                         rjt_err = LSRJT_UNABLE_TPC;
8450                         rjt_exp = LSEXP_NOTHING_MORE;
8451                         break;
8452                 }
8453 
8454                 if (vport->port_state < LPFC_DISC_AUTH) {
8455                         if (!(phba->pport->fc_flag & FC_PT2PT) ||
8456                                 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
8457                                 rjt_err = LSRJT_UNABLE_TPC;
8458                                 rjt_exp = LSEXP_NOTHING_MORE;
8459                                 break;
8460                         }
8461                 }
8462 
8463                 spin_lock_irq(shost->host_lock);
8464                 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
8465                 spin_unlock_irq(shost->host_lock);
8466 
8467                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8468                                         NLP_EVT_RCV_PLOGI);
8469 
8470                 break;
8471         case ELS_CMD_FLOGI:
8472                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8473                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
8474                         did, vport->port_state, ndlp->nlp_flag);
8475 
8476                 phba->fc_stat.elsRcvFLOGI++;
8477 
8478                 /* If the driver believes fabric discovery is done and is ready,
8479                  * bounce the link.  There is some descrepancy.
8480                  */
8481                 if (vport->port_state >= LPFC_LOCAL_CFG_LINK &&
8482                     vport->fc_flag & FC_PT2PT &&
8483                     vport->rcv_flogi_cnt >= 1) {
8484                         rjt_err = LSRJT_LOGICAL_BSY;
8485                         rjt_exp = LSEXP_NOTHING_MORE;
8486                         init_link++;
8487                         goto lsrjt;
8488                 }
8489 
8490                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
8491                 if (newnode)
8492                         lpfc_nlp_put(ndlp);
8493                 break;
8494         case ELS_CMD_LOGO:
8495                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8496                         "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
8497                         did, vport->port_state, ndlp->nlp_flag);
8498 
8499                 phba->fc_stat.elsRcvLOGO++;
8500                 lpfc_send_els_event(vport, ndlp, payload);
8501                 if (vport->port_state < LPFC_DISC_AUTH) {
8502                         rjt_err = LSRJT_UNABLE_TPC;
8503                         rjt_exp = LSEXP_NOTHING_MORE;
8504                         break;
8505                 }
8506                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
8507                 break;
8508         case ELS_CMD_PRLO:
8509                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8510                         "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
8511                         did, vport->port_state, ndlp->nlp_flag);
8512 
8513                 phba->fc_stat.elsRcvPRLO++;
8514                 lpfc_send_els_event(vport, ndlp, payload);
8515                 if (vport->port_state < LPFC_DISC_AUTH) {
8516                         rjt_err = LSRJT_UNABLE_TPC;
8517                         rjt_exp = LSEXP_NOTHING_MORE;
8518                         break;
8519                 }
8520                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
8521                 break;
8522         case ELS_CMD_LCB:
8523                 phba->fc_stat.elsRcvLCB++;
8524                 lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
8525                 break;
8526         case ELS_CMD_RDP:
8527                 phba->fc_stat.elsRcvRDP++;
8528                 lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
8529                 break;
8530         case ELS_CMD_RSCN:
8531                 phba->fc_stat.elsRcvRSCN++;
8532                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
8533                 if (newnode)
8534                         lpfc_nlp_put(ndlp);
8535                 break;
8536         case ELS_CMD_ADISC:
8537                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8538                         "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
8539                         did, vport->port_state, ndlp->nlp_flag);
8540 
8541                 lpfc_send_els_event(vport, ndlp, payload);
8542                 phba->fc_stat.elsRcvADISC++;
8543                 if (vport->port_state < LPFC_DISC_AUTH) {
8544                         rjt_err = LSRJT_UNABLE_TPC;
8545                         rjt_exp = LSEXP_NOTHING_MORE;
8546                         break;
8547                 }
8548                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8549                                         NLP_EVT_RCV_ADISC);
8550                 break;
8551         case ELS_CMD_PDISC:
8552                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8553                         "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
8554                         did, vport->port_state, ndlp->nlp_flag);
8555 
8556                 phba->fc_stat.elsRcvPDISC++;
8557                 if (vport->port_state < LPFC_DISC_AUTH) {
8558                         rjt_err = LSRJT_UNABLE_TPC;
8559                         rjt_exp = LSEXP_NOTHING_MORE;
8560                         break;
8561                 }
8562                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8563                                         NLP_EVT_RCV_PDISC);
8564                 break;
8565         case ELS_CMD_FARPR:
8566                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8567                         "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
8568                         did, vport->port_state, ndlp->nlp_flag);
8569 
8570                 phba->fc_stat.elsRcvFARPR++;
8571                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
8572                 break;
8573         case ELS_CMD_FARP:
8574                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8575                         "RCV FARP:        did:x%x/ste:x%x flg:x%x",
8576                         did, vport->port_state, ndlp->nlp_flag);
8577 
8578                 phba->fc_stat.elsRcvFARP++;
8579                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
8580                 break;
8581         case ELS_CMD_FAN:
8582                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8583                         "RCV FAN:         did:x%x/ste:x%x flg:x%x",
8584                         did, vport->port_state, ndlp->nlp_flag);
8585 
8586                 phba->fc_stat.elsRcvFAN++;
8587                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
8588                 break;
8589         case ELS_CMD_PRLI:
8590         case ELS_CMD_NVMEPRLI:
8591                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8592                         "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
8593                         did, vport->port_state, ndlp->nlp_flag);
8594 
8595                 phba->fc_stat.elsRcvPRLI++;
8596                 if ((vport->port_state < LPFC_DISC_AUTH) &&
8597                     (vport->fc_flag & FC_FABRIC)) {
8598                         rjt_err = LSRJT_UNABLE_TPC;
8599                         rjt_exp = LSEXP_NOTHING_MORE;
8600                         break;
8601                 }
8602                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
8603                 break;
8604         case ELS_CMD_LIRR:
8605                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8606                         "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
8607                         did, vport->port_state, ndlp->nlp_flag);
8608 
8609                 phba->fc_stat.elsRcvLIRR++;
8610                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
8611                 if (newnode)
8612                         lpfc_nlp_put(ndlp);
8613                 break;
8614         case ELS_CMD_RLS:
8615                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8616                         "RCV RLS:         did:x%x/ste:x%x flg:x%x",
8617                         did, vport->port_state, ndlp->nlp_flag);
8618 
8619                 phba->fc_stat.elsRcvRLS++;
8620                 lpfc_els_rcv_rls(vport, elsiocb, ndlp);
8621                 if (newnode)
8622                         lpfc_nlp_put(ndlp);
8623                 break;
8624         case ELS_CMD_RPS:
8625                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8626                         "RCV RPS:         did:x%x/ste:x%x flg:x%x",
8627                         did, vport->port_state, ndlp->nlp_flag);
8628 
8629                 phba->fc_stat.elsRcvRPS++;
8630                 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
8631                 if (newnode)
8632                         lpfc_nlp_put(ndlp);
8633                 break;
8634         case ELS_CMD_RPL:
8635                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8636                         "RCV RPL:         did:x%x/ste:x%x flg:x%x",
8637                         did, vport->port_state, ndlp->nlp_flag);
8638 
8639                 phba->fc_stat.elsRcvRPL++;
8640                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
8641                 if (newnode)
8642                         lpfc_nlp_put(ndlp);
8643                 break;
8644         case ELS_CMD_RNID:
8645                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8646                         "RCV RNID:        did:x%x/ste:x%x flg:x%x",
8647                         did, vport->port_state, ndlp->nlp_flag);
8648 
8649                 phba->fc_stat.elsRcvRNID++;
8650                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
8651                 if (newnode)
8652                         lpfc_nlp_put(ndlp);
8653                 break;
8654         case ELS_CMD_RTV:
8655                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8656                         "RCV RTV:        did:x%x/ste:x%x flg:x%x",
8657                         did, vport->port_state, ndlp->nlp_flag);
8658                 phba->fc_stat.elsRcvRTV++;
8659                 lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
8660                 if (newnode)
8661                         lpfc_nlp_put(ndlp);
8662                 break;
8663         case ELS_CMD_RRQ:
8664                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8665                         "RCV RRQ:         did:x%x/ste:x%x flg:x%x",
8666                         did, vport->port_state, ndlp->nlp_flag);
8667 
8668                 phba->fc_stat.elsRcvRRQ++;
8669                 lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
8670                 if (newnode)
8671                         lpfc_nlp_put(ndlp);
8672                 break;
8673         case ELS_CMD_ECHO:
8674                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8675                         "RCV ECHO:        did:x%x/ste:x%x flg:x%x",
8676                         did, vport->port_state, ndlp->nlp_flag);
8677 
8678                 phba->fc_stat.elsRcvECHO++;
8679                 lpfc_els_rcv_echo(vport, elsiocb, ndlp);
8680                 if (newnode)
8681                         lpfc_nlp_put(ndlp);
8682                 break;
8683         case ELS_CMD_REC:
8684                 /* receive this due to exchange closed */
8685                 rjt_err = LSRJT_UNABLE_TPC;
8686                 rjt_exp = LSEXP_INVALID_OX_RX;
8687                 break;
8688         case ELS_CMD_FPIN:
8689                 /*
8690                  * Received FPIN from fabric - pass it to the
8691                  * transport FPIN handler.
8692                  */
8693                 fc_host_fpin_rcv(shost, elsiocb->iocb.unsli3.rcvsli3.acc_len,
8694                                 (char *)payload);
8695                 break;
8696         default:
8697                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8698                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
8699                         cmd, did, vport->port_state);
8700 
8701                 /* Unsupported ELS command, reject */
8702                 rjt_err = LSRJT_CMD_UNSUPPORTED;
8703                 rjt_exp = LSEXP_NOTHING_MORE;
8704 
8705                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
8706                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8707                                  "0115 Unknown ELS command x%x "
8708                                  "received from NPORT x%x\n", cmd, did);
8709                 if (newnode)
8710                         lpfc_nlp_put(ndlp);
8711                 break;
8712         }
8713 
8714 lsrjt:
8715         /* check if need to LS_RJT received ELS cmd */
8716         if (rjt_err) {
8717                 memset(&stat, 0, sizeof(stat));
8718                 stat.un.b.lsRjtRsnCode = rjt_err;
8719                 stat.un.b.lsRjtRsnCodeExp = rjt_exp;
8720                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
8721                         NULL);
8722         }
8723 
8724         lpfc_nlp_put(elsiocb->context1);
8725         elsiocb->context1 = NULL;
8726 
8727         /* Special case.  Driver received an unsolicited command that
8728          * unsupportable given the driver's current state.  Reset the
8729          * link and start over.
8730          */
8731         if (init_link) {
8732                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8733                 if (!mbox)
8734                         return;
8735                 lpfc_linkdown(phba);
8736                 lpfc_init_link(phba, mbox,
8737                                phba->cfg_topology,
8738                                phba->cfg_link_speed);
8739                 mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
8740                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8741                 mbox->vport = vport;
8742                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
8743                     MBX_NOT_FINISHED)
8744                         mempool_free(mbox, phba->mbox_mem_pool);
8745         }
8746 
8747         return;
8748 
8749 dropit:
8750         if (vport && !(vport->load_flag & FC_UNLOADING))
8751                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8752                         "0111 Dropping received ELS cmd "
8753                         "Data: x%x x%x x%x\n",
8754                         icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
8755         phba->fc_stat.elsRcvDrop++;
8756 }
8757 
8758 /**
8759  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
8760  * @phba: pointer to lpfc hba data structure.
8761  * @pring: pointer to a SLI ring.
8762  * @elsiocb: pointer to lpfc els iocb data structure.
8763  *
8764  * This routine is used to process an unsolicited event received from a SLI
8765  * (Service Level Interface) ring. The actual processing of the data buffer
8766  * associated with the unsolicited event is done by invoking the routine
8767  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
8768  * SLI ring on which the unsolicited event was received.
8769  **/
8770 void
8771 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8772                      struct lpfc_iocbq *elsiocb)
8773 {
8774         struct lpfc_vport *vport = phba->pport;
8775         IOCB_t *icmd = &elsiocb->iocb;
8776         dma_addr_t paddr;
8777         struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
8778         struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
8779 
8780         elsiocb->context1 = NULL;
8781         elsiocb->context2 = NULL;
8782         elsiocb->context3 = NULL;
8783 
8784         if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
8785                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
8786         } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
8787                    (icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
8788                    IOERR_RCV_BUFFER_WAITING) {
8789                 phba->fc_stat.NoRcvBuf++;
8790                 /* Not enough posted buffers; Try posting more buffers */
8791                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
8792                         lpfc_post_buffer(phba, pring, 0);
8793                 return;
8794         }
8795 
8796         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
8797             (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
8798              icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
8799                 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
8800                         vport = phba->pport;
8801                 else
8802                         vport = lpfc_find_vport_by_vpid(phba,
8803                                                 icmd->unsli3.rcvsli3.vpi);
8804         }
8805 
8806         /* If there are no BDEs associated
8807          * with this IOCB, there is nothing to do.
8808          */
8809         if (icmd->ulpBdeCount == 0)
8810                 return;
8811 
8812         /* type of ELS cmd is first 32bit word
8813          * in packet
8814          */
8815         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
8816                 elsiocb->context2 = bdeBuf1;
8817         } else {
8818                 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
8819                                  icmd->un.cont64[0].addrLow);
8820                 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
8821                                                              paddr);
8822         }
8823 
8824         lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
8825         /*
8826          * The different unsolicited event handlers would tell us
8827          * if they are done with "mp" by setting context2 to NULL.
8828          */
8829         if (elsiocb->context2) {
8830                 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
8831                 elsiocb->context2 = NULL;
8832         }
8833 
8834         /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
8835         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
8836             icmd->ulpBdeCount == 2) {
8837                 elsiocb->context2 = bdeBuf2;
8838                 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
8839                 /* free mp if we are done with it */
8840                 if (elsiocb->context2) {
8841                         lpfc_in_buf_free(phba, elsiocb->context2);
8842                         elsiocb->context2 = NULL;
8843                 }
8844         }
8845 }
8846 
8847 static void
8848 lpfc_start_fdmi(struct lpfc_vport *vport)
8849 {
8850         struct lpfc_nodelist *ndlp;
8851 
8852         /* If this is the first time, allocate an ndlp and initialize
8853          * it. Otherwise, make sure the node is enabled and then do the
8854          * login.
8855          */
8856         ndlp = lpfc_findnode_did(vport, FDMI_DID);
8857         if (!ndlp) {
8858                 ndlp = lpfc_nlp_init(vport, FDMI_DID);
8859                 if (ndlp) {
8860                         ndlp->nlp_type |= NLP_FABRIC;
8861                 } else {
8862                         return;
8863                 }
8864         }
8865         if (!NLP_CHK_NODE_ACT(ndlp))
8866                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
8867 
8868         if (ndlp) {
8869                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8870                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
8871         }
8872 }
8873 
8874 /**
8875  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
8876  * @phba: pointer to lpfc hba data structure.
8877  * @vport: pointer to a virtual N_Port data structure.
8878  *
8879  * This routine issues a Port Login (PLOGI) to the Name Server with
8880  * State Change Request (SCR) for a @vport. This routine will create an
8881  * ndlp for the Name Server associated to the @vport if such node does
8882  * not already exist. The PLOGI to Name Server is issued by invoking the
8883  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
8884  * (FDMI) is configured to the @vport, a FDMI node will be created and
8885  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
8886  **/
8887 void
8888 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
8889 {
8890         struct lpfc_nodelist *ndlp;
8891         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8892 
8893         /*
8894          * If lpfc_delay_discovery parameter is set and the clean address
8895          * bit is cleared and fc fabric parameters chenged, delay FC NPort
8896          * discovery.
8897          */
8898         spin_lock_irq(shost->host_lock);
8899         if (vport->fc_flag & FC_DISC_DELAYED) {
8900                 spin_unlock_irq(shost->host_lock);
8901                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
8902                                 "3334 Delay fc port discovery for %d seconds\n",
8903                                 phba->fc_ratov);
8904                 mod_timer(&vport->delayed_disc_tmo,
8905                         jiffies + msecs_to_jiffies(1000 * phba->fc_ratov));
8906                 return;
8907         }
8908         spin_unlock_irq(shost->host_lock);
8909 
8910         ndlp = lpfc_findnode_did(vport, NameServer_DID);
8911         if (!ndlp) {
8912                 ndlp = lpfc_nlp_init(vport, NameServer_DID);
8913                 if (!ndlp) {
8914                         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8915                                 lpfc_disc_start(vport);
8916                                 return;
8917                         }
8918                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8919                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8920                                          "0251 NameServer login: no memory\n");
8921                         return;
8922                 }
8923         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
8924                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
8925                 if (!ndlp) {
8926                         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8927                                 lpfc_disc_start(vport);
8928                                 return;
8929                         }
8930                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8931                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8932                                         "0348 NameServer login: node freed\n");
8933                         return;
8934                 }
8935         }
8936         ndlp->nlp_type |= NLP_FABRIC;
8937 
8938         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8939 
8940         if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
8941                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8942                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8943                                  "0252 Cannot issue NameServer login\n");
8944                 return;
8945         }
8946 
8947         if ((phba->cfg_enable_SmartSAN ||
8948              (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) &&
8949              (vport->load_flag & FC_ALLOW_FDMI))
8950                 lpfc_start_fdmi(vport);
8951 }
8952 
8953 /**
8954  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
8955  * @phba: pointer to lpfc hba data structure.
8956  * @pmb: pointer to the driver internal queue element for mailbox command.
8957  *
8958  * This routine is the completion callback function to register new vport
8959  * mailbox command. If the new vport mailbox command completes successfully,
8960  * the fabric registration login shall be performed on physical port (the
8961  * new vport created is actually a physical port, with VPI 0) or the port
8962  * login to Name Server for State Change Request (SCR) will be performed
8963  * on virtual port (real virtual port, with VPI greater than 0).
8964  **/
8965 static void
8966 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
8967 {
8968         struct lpfc_vport *vport = pmb->vport;
8969         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
8970         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
8971         MAILBOX_t *mb = &pmb->u.mb;
8972         int rc;
8973 
8974         spin_lock_irq(shost->host_lock);
8975         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
8976         spin_unlock_irq(shost->host_lock);
8977 
8978         if (mb->mbxStatus) {
8979                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
8980                                 "0915 Register VPI failed : Status: x%x"
8981                                 " upd bit: x%x \n", mb->mbxStatus,
8982                                  mb->un.varRegVpi.upd);
8983                 if (phba->sli_rev == LPFC_SLI_REV4 &&
8984                         mb->un.varRegVpi.upd)
8985                         goto mbox_err_exit ;
8986 
8987                 switch (mb->mbxStatus) {
8988                 case 0x11:      /* unsupported feature */
8989                 case 0x9603:    /* max_vpi exceeded */
8990                 case 0x9602:    /* Link event since CLEAR_LA */
8991                         /* giving up on vport registration */
8992                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8993                         spin_lock_irq(shost->host_lock);
8994                         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
8995                         spin_unlock_irq(shost->host_lock);
8996                         lpfc_can_disctmo(vport);
8997                         break;
8998                 /* If reg_vpi fail with invalid VPI status, re-init VPI */
8999                 case 0x20:
9000                         spin_lock_irq(shost->host_lock);
9001                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
9002                         spin_unlock_irq(shost->host_lock);
9003                         lpfc_init_vpi(phba, pmb, vport->vpi);
9004                         pmb->vport = vport;
9005                         pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
9006                         rc = lpfc_sli_issue_mbox(phba, pmb,
9007                                 MBX_NOWAIT);
9008                         if (rc == MBX_NOT_FINISHED) {
9009                                 lpfc_printf_vlog(vport,
9010                                         KERN_ERR, LOG_MBOX,
9011                                         "2732 Failed to issue INIT_VPI"
9012                                         " mailbox command\n");
9013                         } else {
9014                                 lpfc_nlp_put(ndlp);
9015                                 return;
9016                         }
9017                         /* fall through */
9018                 default:
9019                         /* Try to recover from this error */
9020                         if (phba->sli_rev == LPFC_SLI_REV4)
9021                                 lpfc_sli4_unreg_all_rpis(vport);
9022                         lpfc_mbx_unreg_vpi(vport);
9023                         spin_lock_irq(shost->host_lock);
9024                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
9025                         spin_unlock_irq(shost->host_lock);
9026                         if (mb->mbxStatus == MBX_NOT_FINISHED)
9027                                 break;
9028                         if ((vport->port_type == LPFC_PHYSICAL_PORT) &&
9029                             !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG)) {
9030                                 if (phba->sli_rev == LPFC_SLI_REV4)
9031                                         lpfc_issue_init_vfi(vport);
9032                                 else
9033                                         lpfc_initial_flogi(vport);
9034                         } else {
9035                                 lpfc_initial_fdisc(vport);
9036                         }
9037                         break;
9038                 }
9039         } else {
9040                 spin_lock_irq(shost->host_lock);
9041                 vport->vpi_state |= LPFC_VPI_REGISTERED;
9042                 spin_unlock_irq(shost->host_lock);
9043                 if (vport == phba->pport) {
9044                         if (phba->sli_rev < LPFC_SLI_REV4)
9045                                 lpfc_issue_fabric_reglogin(vport);
9046                         else {
9047                                 /*
9048                                  * If the physical port is instantiated using
9049                                  * FDISC, do not start vport discovery.
9050                                  */
9051                                 if (vport->port_state != LPFC_FDISC)
9052                                         lpfc_start_fdiscs(phba);
9053                                 lpfc_do_scr_ns_plogi(phba, vport);
9054                         }
9055                 } else
9056                         lpfc_do_scr_ns_plogi(phba, vport);
9057         }
9058 mbox_err_exit:
9059         /* Now, we decrement the ndlp reference count held for this
9060          * callback function
9061          */
9062         lpfc_nlp_put(ndlp);
9063 
9064         mempool_free(pmb, phba->mbox_mem_pool);
9065         return;
9066 }
9067 
9068 /**
9069  * lpfc_register_new_vport - Register a new vport with a HBA
9070  * @phba: pointer to lpfc hba data structure.
9071  * @vport: pointer to a host virtual N_Port data structure.
9072  * @ndlp: pointer to a node-list data structure.
9073  *
9074  * This routine registers the @vport as a new virtual port with a HBA.
9075  * It is done through a registering vpi mailbox command.
9076  **/
9077 void
9078 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
9079                         struct lpfc_nodelist *ndlp)
9080 {
9081         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9082         LPFC_MBOXQ_t *mbox;
9083 
9084         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9085         if (mbox) {
9086                 lpfc_reg_vpi(vport, mbox);
9087                 mbox->vport = vport;
9088                 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
9089                 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
9090                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
9091                     == MBX_NOT_FINISHED) {
9092                         /* mailbox command not success, decrement ndlp
9093                          * reference count for this command
9094                          */
9095                         lpfc_nlp_put(ndlp);
9096                         mempool_free(mbox, phba->mbox_mem_pool);
9097 
9098                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
9099                                 "0253 Register VPI: Can't send mbox\n");
9100                         goto mbox_err_exit;
9101                 }
9102         } else {
9103                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
9104                                  "0254 Register VPI: no memory\n");
9105                 goto mbox_err_exit;
9106         }
9107         return;
9108 
9109 mbox_err_exit:
9110         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9111         spin_lock_irq(shost->host_lock);
9112         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
9113         spin_unlock_irq(shost->host_lock);
9114         return;
9115 }
9116 
9117 /**
9118  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
9119  * @phba: pointer to lpfc hba data structure.
9120  *
9121  * This routine cancels the retry delay timers to all the vports.
9122  **/
9123 void
9124 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
9125 {
9126         struct lpfc_vport **vports;
9127         struct lpfc_nodelist *ndlp;
9128         uint32_t link_state;
9129         int i;
9130 
9131         /* Treat this failure as linkdown for all vports */
9132         link_state = phba->link_state;
9133         lpfc_linkdown(phba);
9134         phba->link_state = link_state;
9135 
9136         vports = lpfc_create_vport_work_array(phba);
9137 
9138         if (vports) {
9139                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9140                         ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
9141                         if (ndlp)
9142                                 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
9143                         lpfc_els_flush_cmd(vports[i]);
9144                 }
9145                 lpfc_destroy_vport_work_array(phba, vports);
9146         }
9147 }
9148 
9149 /**
9150  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
9151  * @phba: pointer to lpfc hba data structure.
9152  *
9153  * This routine abort all pending discovery commands and
9154  * start a timer to retry FLOGI for the physical port
9155  * discovery.
9156  **/
9157 void
9158 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
9159 {
9160         struct lpfc_nodelist *ndlp;
9161         struct Scsi_Host  *shost;
9162 
9163         /* Cancel the all vports retry delay retry timers */
9164         lpfc_cancel_all_vport_retry_delay_timer(phba);
9165 
9166         /* If fabric require FLOGI, then re-instantiate physical login */
9167         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
9168         if (!ndlp)
9169                 return;
9170 
9171         shost = lpfc_shost_from_vport(phba->pport);
9172         mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
9173         spin_lock_irq(shost->host_lock);
9174         ndlp->nlp_flag |= NLP_DELAY_TMO;
9175         spin_unlock_irq(shost->host_lock);
9176         ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
9177         phba->pport->port_state = LPFC_FLOGI;
9178         return;
9179 }
9180 
9181 /**
9182  * lpfc_fabric_login_reqd - Check if FLOGI required.
9183  * @phba: pointer to lpfc hba data structure.
9184  * @cmdiocb: pointer to FDISC command iocb.
9185  * @rspiocb: pointer to FDISC response iocb.
9186  *
9187  * This routine checks if a FLOGI is reguired for FDISC
9188  * to succeed.
9189  **/
9190 static int
9191 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
9192                 struct lpfc_iocbq *cmdiocb,
9193                 struct lpfc_iocbq *rspiocb)
9194 {
9195 
9196         if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
9197                 (rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
9198                 return 0;
9199         else
9200                 return 1;
9201 }
9202 
9203 /**
9204  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
9205  * @phba: pointer to lpfc hba data structure.
9206  * @cmdiocb: pointer to lpfc command iocb data structure.
9207  * @rspiocb: pointer to lpfc response iocb data structure.
9208  *
9209  * This routine is the completion callback function to a Fabric Discover
9210  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
9211  * single threaded, each FDISC completion callback function will reset
9212  * the discovery timer for all vports such that the timers will not get
9213  * unnecessary timeout. The function checks the FDISC IOCB status. If error
9214  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
9215  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
9216  * assigned to the vport has been changed with the completion of the FDISC
9217  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
9218  * are unregistered from the HBA, and then the lpfc_register_new_vport()
9219  * routine is invoked to register new vport with the HBA. Otherwise, the
9220  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
9221  * Server for State Change Request (SCR).
9222  **/
9223 static void
9224 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9225                     struct lpfc_iocbq *rspiocb)
9226 {
9227         struct lpfc_vport *vport = cmdiocb->vport;
9228         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
9229         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
9230         struct lpfc_nodelist *np;
9231         struct lpfc_nodelist *next_np;
9232         IOCB_t *irsp = &rspiocb->iocb;
9233         struct lpfc_iocbq *piocb;
9234         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
9235         struct serv_parm *sp;
9236         uint8_t fabric_param_changed;
9237 
9238         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9239                          "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
9240                          irsp->ulpStatus, irsp->un.ulpWord[4],
9241                          vport->fc_prevDID);
9242         /* Since all FDISCs are being single threaded, we
9243          * must reset the discovery timer for ALL vports
9244          * waiting to send FDISC when one completes.
9245          */
9246         list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
9247                 lpfc_set_disctmo(piocb->vport);
9248         }
9249 
9250         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9251                 "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
9252                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
9253 
9254         if (irsp->ulpStatus) {
9255 
9256                 if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
9257                         lpfc_retry_pport_discovery(phba);
9258                         goto out;
9259                 }
9260 
9261                 /* Check for retry */
9262                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
9263                         goto out;
9264                 /* FDISC failed */
9265                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
9266                                  "0126 FDISC failed. (x%x/x%x)\n",
9267                                  irsp->ulpStatus, irsp->un.ulpWord[4]);
9268                 goto fdisc_failed;
9269         }
9270         spin_lock_irq(shost->host_lock);
9271         vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
9272         vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
9273         vport->fc_flag |= FC_FABRIC;
9274         if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
9275                 vport->fc_flag |=  FC_PUBLIC_LOOP;
9276         spin_unlock_irq(shost->host_lock);
9277 
9278         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
9279         lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
9280         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
9281         if (!prsp)
9282                 goto out;
9283         sp = prsp->virt + sizeof(uint32_t);
9284         fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
9285         memcpy(&vport->fabric_portname, &sp->portName,
9286                 sizeof(struct lpfc_name));
9287         memcpy(&vport->fabric_nodename, &sp->nodeName,
9288                 sizeof(struct lpfc_name));
9289         if (fabric_param_changed &&
9290                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
9291                 /* If our NportID changed, we need to ensure all
9292                  * remaining NPORTs get unreg_login'ed so we can
9293                  * issue unreg_vpi.
9294                  */
9295                 list_for_each_entry_safe(np, next_np,
9296                         &vport->fc_nodes, nlp_listp) {
9297                         if (!NLP_CHK_NODE_ACT(ndlp) ||
9298                             (np->nlp_state != NLP_STE_NPR_NODE) ||
9299                             !(np->nlp_flag & NLP_NPR_ADISC))
9300                                 continue;
9301                         spin_lock_irq(shost->host_lock);
9302                         np->nlp_flag &= ~NLP_NPR_ADISC;
9303                         spin_unlock_irq(shost->host_lock);
9304                         lpfc_unreg_rpi(vport, np);
9305                 }
9306                 lpfc_cleanup_pending_mbox(vport);
9307 
9308                 if (phba->sli_rev == LPFC_SLI_REV4)
9309                         lpfc_sli4_unreg_all_rpis(vport);
9310 
9311                 lpfc_mbx_unreg_vpi(vport);
9312                 spin_lock_irq(shost->host_lock);
9313                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
9314                 if (phba->sli_rev == LPFC_SLI_REV4)
9315                         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
9316                 else
9317                         vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
9318                 spin_unlock_irq(shost->host_lock);
9319         } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
9320                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
9321                 /*
9322                  * Driver needs to re-reg VPI in order for f/w
9323                  * to update the MAC address.
9324                  */
9325                 lpfc_register_new_vport(phba, vport, ndlp);
9326                 goto out;
9327         }
9328 
9329         if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
9330                 lpfc_issue_init_vpi(vport);
9331         else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
9332                 lpfc_register_new_vport(phba, vport, ndlp);
9333         else
9334                 lpfc_do_scr_ns_plogi(phba, vport);
9335         goto out;
9336 fdisc_failed:
9337         if (vport->fc_vport &&
9338             (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS))
9339                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9340         /* Cancel discovery timer */
9341         lpfc_can_disctmo(vport);
9342         lpfc_nlp_put(ndlp);
9343 out:
9344         lpfc_els_free_iocb(phba, cmdiocb);
9345 }
9346 
9347 /**
9348  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
9349  * @vport: pointer to a virtual N_Port data structure.
9350  * @ndlp: pointer to a node-list data structure.
9351  * @retry: number of retries to the command IOCB.
9352  *
9353  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
9354  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
9355  * routine to issue the IOCB, which makes sure only one outstanding fabric
9356  * IOCB will be sent off HBA at any given time.
9357  *
9358  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
9359  * will be incremented by 1 for holding the ndlp and the reference to ndlp
9360  * will be stored into the context1 field of the IOCB for the completion
9361  * callback function to the FDISC ELS command.
9362  *
9363  * Return code
9364  *   0 - Successfully issued fdisc iocb command
9365  *   1 - Failed to issue fdisc iocb command
9366  **/
9367 static int
9368 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
9369                      uint8_t retry)
9370 {
9371         struct lpfc_hba *phba = vport->phba;
9372         IOCB_t *icmd;
9373         struct lpfc_iocbq *elsiocb;
9374         struct serv_parm *sp;
9375         uint8_t *pcmd;
9376         uint16_t cmdsize;
9377         int did = ndlp->nlp_DID;
9378         int rc;
9379 
9380         vport->port_state = LPFC_FDISC;
9381         vport->fc_myDID = 0;
9382         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
9383         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
9384                                      ELS_CMD_FDISC);
9385         if (!elsiocb) {
9386                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9387                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
9388                                  "0255 Issue FDISC: no IOCB\n");
9389                 return 1;
9390         }
9391 
9392         icmd = &elsiocb->iocb;
9393         icmd->un.elsreq64.myID = 0;
9394         icmd->un.elsreq64.fl = 1;
9395 
9396         /*
9397          * SLI3 ports require a different context type value than SLI4.
9398          * Catch SLI3 ports here and override the prep.
9399          */
9400         if (phba->sli_rev == LPFC_SLI_REV3) {
9401                 icmd->ulpCt_h = 1;
9402                 icmd->ulpCt_l = 0;
9403         }
9404 
9405         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
9406         *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
9407         pcmd += sizeof(uint32_t); /* CSP Word 1 */
9408         memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
9409         sp = (struct serv_parm *) pcmd;
9410         /* Setup CSPs accordingly for Fabric */
9411         sp->cmn.e_d_tov = 0;
9412         sp->cmn.w2.r_a_tov = 0;
9413         sp->cmn.virtual_fabric_support = 0;
9414         sp->cls1.classValid = 0;
9415         sp->cls2.seqDelivery = 1;
9416         sp->cls3.seqDelivery = 1;
9417 
9418         pcmd += sizeof(uint32_t); /* CSP Word 2 */
9419         pcmd += sizeof(uint32_t); /* CSP Word 3 */
9420         pcmd += sizeof(uint32_t); /* CSP Word 4 */
9421         pcmd += sizeof(uint32_t); /* Port Name */
9422         memcpy(pcmd, &vport->fc_portname, 8);
9423         pcmd += sizeof(uint32_t); /* Node Name */
9424         pcmd += sizeof(uint32_t); /* Node Name */
9425         memcpy(pcmd, &vport->fc_nodename, 8);
9426         sp->cmn.valid_vendor_ver_level = 0;
9427         memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
9428         lpfc_set_disctmo(vport);
9429 
9430         phba->fc_stat.elsXmitFDISC++;
9431         elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
9432 
9433         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9434                 "Issue FDISC:     did:x%x",
9435                 did, 0, 0);
9436 
9437         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
9438         if (rc == IOCB_ERROR) {
9439                 lpfc_els_free_iocb(phba, elsiocb);
9440                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9441                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
9442                                  "0256 Issue FDISC: Cannot send IOCB\n");
9443                 return 1;
9444         }
9445         lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
9446         return 0;
9447 }
9448 
9449 /**
9450  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
9451  * @phba: pointer to lpfc hba data structure.
9452  * @cmdiocb: pointer to lpfc command iocb data structure.
9453  * @rspiocb: pointer to lpfc response iocb data structure.
9454  *
9455  * This routine is the completion callback function to the issuing of a LOGO
9456  * ELS command off a vport. It frees the command IOCB and then decrement the
9457  * reference count held on ndlp for this completion function, indicating that
9458  * the reference to the ndlp is no long needed. Note that the
9459  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
9460  * callback function and an additional explicit ndlp reference decrementation
9461  * will trigger the actual release of the ndlp.
9462  **/
9463 static void
9464 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9465                         struct lpfc_iocbq *rspiocb)
9466 {
9467         struct lpfc_vport *vport = cmdiocb->vport;
9468         IOCB_t *irsp;
9469         struct lpfc_nodelist *ndlp;
9470         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9471 
9472         ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
9473         irsp = &rspiocb->iocb;
9474         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9475                 "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
9476                 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
9477 
9478         lpfc_els_free_iocb(phba, cmdiocb);
9479         vport->unreg_vpi_cmpl = VPORT_ERROR;
9480 
9481         /* Trigger the release of the ndlp after logo */
9482         lpfc_nlp_put(ndlp);
9483 
9484         /* NPIV LOGO completes to NPort <nlp_DID> */
9485         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9486                          "2928 NPIV LOGO completes to NPort x%x "
9487                          "Data: x%x x%x x%x x%x\n",
9488                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
9489                          irsp->ulpTimeout, vport->num_disc_nodes);
9490 
9491         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
9492                 spin_lock_irq(shost->host_lock);
9493                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
9494                 vport->fc_flag &= ~FC_FABRIC;
9495                 spin_unlock_irq(shost->host_lock);
9496                 lpfc_can_disctmo(vport);
9497         }
9498 }
9499 
9500 /**
9501  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
9502  * @vport: pointer to a virtual N_Port data structure.
9503  * @ndlp: pointer to a node-list data structure.
9504  *
9505  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
9506  *
9507  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
9508  * will be incremented by 1 for holding the ndlp and the reference to ndlp
9509  * will be stored into the context1 field of the IOCB for the completion
9510  * callback function to the LOGO ELS command.
9511  *
9512  * Return codes
9513  *   0 - Successfully issued logo off the @vport
9514  *   1 - Failed to issue logo off the @vport
9515  **/
9516 int
9517 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
9518 {
9519         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9520         struct lpfc_hba  *phba = vport->phba;
9521         struct lpfc_iocbq *elsiocb;
9522         uint8_t *pcmd;
9523         uint16_t cmdsize;
9524 
9525         cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
9526         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
9527                                      ELS_CMD_LOGO);
9528         if (!elsiocb)
9529                 return 1;
9530 
9531         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
9532         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
9533         pcmd += sizeof(uint32_t);
9534 
9535         /* Fill in LOGO payload */
9536         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
9537         pcmd += sizeof(uint32_t);
9538         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
9539 
9540         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9541                 "Issue LOGO npiv  did:x%x flg:x%x",
9542                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
9543 
9544         elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
9545         spin_lock_irq(shost->host_lock);
9546         ndlp->nlp_flag |= NLP_LOGO_SND;
9547         spin_unlock_irq(shost->host_lock);
9548         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
9549             IOCB_ERROR) {
9550                 spin_lock_irq(shost->host_lock);
9551                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
9552                 spin_unlock_irq(shost->host_lock);
9553                 lpfc_els_free_iocb(phba, elsiocb);
9554                 return 1;
9555         }
9556         return 0;
9557 }
9558 
9559 /**
9560  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
9561  * @ptr: holder for the timer function associated data.
9562  *
9563  * This routine is invoked by the fabric iocb block timer after
9564  * timeout. It posts the fabric iocb block timeout event by setting the
9565  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
9566  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
9567  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
9568  * posted event WORKER_FABRIC_BLOCK_TMO.
9569  **/
9570 void
9571 lpfc_fabric_block_timeout(struct timer_list *t)
9572 {
9573         struct lpfc_hba  *phba = from_timer(phba, t, fabric_block_timer);
9574         unsigned long iflags;
9575         uint32_t tmo_posted;
9576 
9577         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
9578         tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
9579         if (!tmo_posted)
9580                 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
9581         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
9582 
9583         if (!tmo_posted)
9584                 lpfc_worker_wake_up(phba);
9585         return;
9586 }
9587 
9588 /**
9589  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
9590  * @phba: pointer to lpfc hba data structure.
9591  *
9592  * This routine issues one fabric iocb from the driver internal list to
9593  * the HBA. It first checks whether it's ready to issue one fabric iocb to
9594  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
9595  * remove one pending fabric iocb from the driver internal list and invokes
9596  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
9597  **/
9598 static void
9599 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
9600 {
9601         struct lpfc_iocbq *iocb;
9602         unsigned long iflags;
9603         int ret;
9604         IOCB_t *cmd;
9605 
9606 repeat:
9607         iocb = NULL;
9608         spin_lock_irqsave(&phba->hbalock, iflags);
9609         /* Post any pending iocb to the SLI layer */
9610         if (atomic_read(&phba->fabric_iocb_count) == 0) {
9611                 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
9612                                  list);
9613                 if (iocb)
9614                         /* Increment fabric iocb count to hold the position */
9615                         atomic_inc(&phba->fabric_iocb_count);
9616         }
9617         spin_unlock_irqrestore(&phba->hbalock, iflags);
9618         if (iocb) {
9619                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
9620                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
9621                 iocb->iocb_flag |= LPFC_IO_FABRIC;
9622 
9623                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
9624                         "Fabric sched1:   ste:x%x",
9625                         iocb->vport->port_state, 0, 0);
9626 
9627                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
9628 
9629                 if (ret == IOCB_ERROR) {
9630                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
9631                         iocb->fabric_iocb_cmpl = NULL;
9632                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
9633                         cmd = &iocb->iocb;
9634                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
9635                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
9636                         iocb->iocb_cmpl(phba, iocb, iocb);
9637 
9638                         atomic_dec(&phba->fabric_iocb_count);
9639                         goto repeat;
9640                 }
9641         }
9642 
9643         return;
9644 }
9645 
9646 /**
9647  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
9648  * @phba: pointer to lpfc hba data structure.
9649  *
9650  * This routine unblocks the  issuing fabric iocb command. The function
9651  * will clear the fabric iocb block bit and then invoke the routine
9652  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
9653  * from the driver internal fabric iocb list.
9654  **/
9655 void
9656 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
9657 {
9658         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
9659 
9660         lpfc_resume_fabric_iocbs(phba);
9661         return;
9662 }
9663 
9664 /**
9665  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
9666  * @phba: pointer to lpfc hba data structure.
9667  *
9668  * This routine blocks the issuing fabric iocb for a specified amount of
9669  * time (currently 100 ms). This is done by set the fabric iocb block bit
9670  * and set up a timeout timer for 100ms. When the block bit is set, no more
9671  * fabric iocb will be issued out of the HBA.
9672  **/
9673 static void
9674 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
9675 {
9676         int blocked;
9677 
9678         blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
9679         /* Start a timer to unblock fabric iocbs after 100ms */
9680         if (!blocked)
9681                 mod_timer(&phba->fabric_block_timer,
9682                           jiffies + msecs_to_jiffies(100));
9683 
9684         return;
9685 }
9686 
9687 /**
9688  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
9689  * @phba: pointer to lpfc hba data structure.
9690  * @cmdiocb: pointer to lpfc command iocb data structure.
9691  * @rspiocb: pointer to lpfc response iocb data structure.
9692  *
9693  * This routine is the callback function that is put to the fabric iocb's
9694  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
9695  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
9696  * function first restores and invokes the original iocb's callback function
9697  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
9698  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
9699  **/
9700 static void
9701 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9702         struct lpfc_iocbq *rspiocb)
9703 {
9704         struct ls_rjt stat;
9705 
9706         BUG_ON((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC);
9707 
9708         switch (rspiocb->iocb.ulpStatus) {
9709                 case IOSTAT_NPORT_RJT:
9710                 case IOSTAT_FABRIC_RJT:
9711                         if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
9712                                 lpfc_block_fabric_iocbs(phba);
9713                         }
9714                         break;
9715 
9716                 case IOSTAT_NPORT_BSY:
9717                 case IOSTAT_FABRIC_BSY:
9718                         lpfc_block_fabric_iocbs(phba);
9719                         break;
9720 
9721                 case IOSTAT_LS_RJT:
9722                         stat.un.lsRjtError =
9723                                 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
9724                         if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
9725                                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
9726                                 lpfc_block_fabric_iocbs(phba);
9727                         break;
9728         }
9729 
9730         BUG_ON(atomic_read(&phba->fabric_iocb_count) == 0);
9731 
9732         cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
9733         cmdiocb->fabric_iocb_cmpl = NULL;
9734         cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
9735         cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
9736 
9737         atomic_dec(&phba->fabric_iocb_count);
9738         if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
9739                 /* Post any pending iocbs to HBA */
9740                 lpfc_resume_fabric_iocbs(phba);
9741         }
9742 }
9743 
9744 /**
9745  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
9746  * @phba: pointer to lpfc hba data structure.
9747  * @iocb: pointer to lpfc command iocb data structure.
9748  *
9749  * This routine is used as the top-level API for issuing a fabric iocb command
9750  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
9751  * function makes sure that only one fabric bound iocb will be outstanding at
9752  * any given time. As such, this function will first check to see whether there
9753  * is already an outstanding fabric iocb on the wire. If so, it will put the
9754  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
9755  * issued later. Otherwise, it will issue the iocb on the wire and update the
9756  * fabric iocb count it indicate that there is one fabric iocb on the wire.
9757  *
9758  * Note, this implementation has a potential sending out fabric IOCBs out of
9759  * order. The problem is caused by the construction of the "ready" boolen does
9760  * not include the condition that the internal fabric IOCB list is empty. As
9761  * such, it is possible a fabric IOCB issued by this routine might be "jump"
9762  * ahead of the fabric IOCBs in the internal list.
9763  *
9764  * Return code
9765  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
9766  *   IOCB_ERROR - failed to issue fabric iocb
9767  **/
9768 static int
9769 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
9770 {
9771         unsigned long iflags;
9772         int ready;
9773         int ret;
9774 
9775         BUG_ON(atomic_read(&phba->fabric_iocb_count) > 1);
9776 
9777         spin_lock_irqsave(&phba->hbalock, iflags);
9778         ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
9779                 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
9780 
9781         if (ready)
9782                 /* Increment fabric iocb count to hold the position */
9783                 atomic_inc(&phba->fabric_iocb_count);
9784         spin_unlock_irqrestore(&phba->hbalock, iflags);
9785         if (ready) {
9786                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
9787                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
9788                 iocb->iocb_flag |= LPFC_IO_FABRIC;
9789 
9790                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
9791                         "Fabric sched2:   ste:x%x",
9792                         iocb->vport->port_state, 0, 0);
9793 
9794                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
9795 
9796                 if (ret == IOCB_ERROR) {
9797                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
9798                         iocb->fabric_iocb_cmpl = NULL;
9799                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
9800                         atomic_dec(&phba->fabric_iocb_count);
9801                 }
9802         } else {
9803                 spin_lock_irqsave(&phba->hbalock, iflags);
9804                 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
9805                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9806                 ret = IOCB_SUCCESS;
9807         }
9808         return ret;
9809 }
9810 
9811 /**
9812  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
9813  * @vport: pointer to a virtual N_Port data structure.
9814  *
9815  * This routine aborts all the IOCBs associated with a @vport from the
9816  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
9817  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
9818  * list, removes each IOCB associated with the @vport off the list, set the
9819  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
9820  * associated with the IOCB.
9821  **/
9822 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
9823 {
9824         LIST_HEAD(completions);
9825         struct lpfc_hba  *phba = vport->phba;
9826         struct lpfc_iocbq *tmp_iocb, *piocb;
9827 
9828         spin_lock_irq(&phba->hbalock);
9829         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
9830                                  list) {
9831 
9832                 if (piocb->vport != vport)
9833                         continue;
9834 
9835                 list_move_tail(&piocb->list, &completions);
9836         }
9837         spin_unlock_irq(&phba->hbalock);
9838 
9839         /* Cancel all the IOCBs from the completions list */
9840         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9841                               IOERR_SLI_ABORTED);
9842 }
9843 
9844 /**
9845  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
9846  * @ndlp: pointer to a node-list data structure.
9847  *
9848  * This routine aborts all the IOCBs associated with an @ndlp from the
9849  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
9850  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
9851  * list, removes each IOCB associated with the @ndlp off the list, set the
9852  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
9853  * associated with the IOCB.
9854  **/
9855 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
9856 {
9857         LIST_HEAD(completions);
9858         struct lpfc_hba  *phba = ndlp->phba;
9859         struct lpfc_iocbq *tmp_iocb, *piocb;
9860         struct lpfc_sli_ring *pring;
9861 
9862         pring = lpfc_phba_elsring(phba);
9863 
9864         if (unlikely(!pring))
9865                 return;
9866 
9867         spin_lock_irq(&phba->hbalock);
9868         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
9869                                  list) {
9870                 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
9871 
9872                         list_move_tail(&piocb->list, &completions);
9873                 }
9874         }
9875         spin_unlock_irq(&phba->hbalock);
9876 
9877         /* Cancel all the IOCBs from the completions list */
9878         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9879                               IOERR_SLI_ABORTED);
9880 }
9881 
9882 /**
9883  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
9884  * @phba: pointer to lpfc hba data structure.
9885  *
9886  * This routine aborts all the IOCBs currently on the driver internal
9887  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
9888  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
9889  * list, removes IOCBs off the list, set the status feild to
9890  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
9891  * the IOCB.
9892  **/
9893 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
9894 {
9895         LIST_HEAD(completions);
9896 
9897         spin_lock_irq(&phba->hbalock);
9898         list_splice_init(&phba->fabric_iocb_list, &completions);
9899         spin_unlock_irq(&phba->hbalock);
9900 
9901         /* Cancel all the IOCBs from the completions list */
9902         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9903                               IOERR_SLI_ABORTED);
9904 }
9905 
9906 /**
9907  * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
9908  * @vport: pointer to lpfc vport data structure.
9909  *
9910  * This routine is invoked by the vport cleanup for deletions and the cleanup
9911  * for an ndlp on removal.
9912  **/
9913 void
9914 lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
9915 {
9916         struct lpfc_hba *phba = vport->phba;
9917         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
9918         unsigned long iflag = 0;
9919 
9920         spin_lock_irqsave(&phba->hbalock, iflag);
9921         spin_lock(&phba->sli4_hba.sgl_list_lock);
9922         list_for_each_entry_safe(sglq_entry, sglq_next,
9923                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
9924                 if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport)
9925                         sglq_entry->ndlp = NULL;
9926         }
9927         spin_unlock(&phba->sli4_hba.sgl_list_lock);
9928         spin_unlock_irqrestore(&phba->hbalock, iflag);
9929         return;
9930 }
9931 
9932 /**
9933  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
9934  * @phba: pointer to lpfc hba data structure.
9935  * @axri: pointer to the els xri abort wcqe structure.
9936  *
9937  * This routine is invoked by the worker thread to process a SLI4 slow-path
9938  * ELS aborted xri.
9939  **/
9940 void
9941 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
9942                           struct sli4_wcqe_xri_aborted *axri)
9943 {
9944         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
9945         uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
9946         uint16_t lxri = 0;
9947 
9948         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
9949         unsigned long iflag = 0;
9950         struct lpfc_nodelist *ndlp;
9951         struct lpfc_sli_ring *pring;
9952 
9953         pring = lpfc_phba_elsring(phba);
9954 
9955         spin_lock_irqsave(&phba->hbalock, iflag);
9956         spin_lock(&phba->sli4_hba.sgl_list_lock);
9957         list_for_each_entry_safe(sglq_entry, sglq_next,
9958                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
9959                 if (sglq_entry->sli4_xritag == xri) {
9960                         list_del(&sglq_entry->list);
9961                         ndlp = sglq_entry->ndlp;
9962                         sglq_entry->ndlp = NULL;
9963                         list_add_tail(&sglq_entry->list,
9964                                 &phba->sli4_hba.lpfc_els_sgl_list);
9965                         sglq_entry->state = SGL_FREED;
9966                         spin_unlock(&phba->sli4_hba.sgl_list_lock);
9967                         spin_unlock_irqrestore(&phba->hbalock, iflag);
9968                         lpfc_set_rrq_active(phba, ndlp,
9969                                 sglq_entry->sli4_lxritag,
9970                                 rxid, 1);
9971 
9972                         /* Check if TXQ queue needs to be serviced */
9973                         if (pring && !list_empty(&pring->txq))
9974                                 lpfc_worker_wake_up(phba);
9975                         return;
9976                 }
9977         }
9978         spin_unlock(&phba->sli4_hba.sgl_list_lock);
9979         lxri = lpfc_sli4_xri_inrange(phba, xri);
9980         if (lxri == NO_XRI) {
9981                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9982                 return;
9983         }
9984         spin_lock(&phba->sli4_hba.sgl_list_lock);
9985         sglq_entry = __lpfc_get_active_sglq(phba, lxri);
9986         if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
9987                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
9988                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9989                 return;
9990         }
9991         sglq_entry->state = SGL_XRI_ABORTED;
9992         spin_unlock(&phba->sli4_hba.sgl_list_lock);
9993         spin_unlock_irqrestore(&phba->hbalock, iflag);
9994         return;
9995 }
9996 
9997 /* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
9998  * @vport: pointer to virtual port object.
9999  * @ndlp: nodelist pointer for the impacted node.
10000  *
10001  * The driver calls this routine in response to an SLI4 XRI ABORT CQE
10002  * or an SLI3 ASYNC_STATUS_CN event from the port.  For either event,
10003  * the driver is required to send a LOGO to the remote node before it
10004  * attempts to recover its login to the remote node.
10005  */
10006 void
10007 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
10008                            struct lpfc_nodelist *ndlp)
10009 {
10010         struct Scsi_Host *shost;
10011         struct lpfc_hba *phba;
10012         unsigned long flags = 0;
10013 
10014         shost = lpfc_shost_from_vport(vport);
10015         phba = vport->phba;
10016         if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
10017                 lpfc_printf_log(phba, KERN_INFO,
10018                                 LOG_SLI, "3093 No rport recovery needed. "
10019                                 "rport in state 0x%x\n", ndlp->nlp_state);
10020                 return;
10021         }
10022         lpfc_printf_log(phba, KERN_ERR,
10023                         LOG_ELS | LOG_FCP_ERROR | LOG_NVME_IOERR,
10024                         "3094 Start rport recovery on shost id 0x%x "
10025                         "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
10026                         "flags 0x%x\n",
10027                         shost->host_no, ndlp->nlp_DID,
10028                         vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
10029                         ndlp->nlp_flag);
10030         /*
10031          * The rport is not responding.  Remove the FCP-2 flag to prevent
10032          * an ADISC in the follow-up recovery code.
10033          */
10034         spin_lock_irqsave(shost->host_lock, flags);
10035         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
10036         ndlp->nlp_flag |= NLP_ISSUE_LOGO;
10037         spin_unlock_irqrestore(shost->host_lock, flags);
10038         lpfc_unreg_rpi(vport, ndlp);
10039 }
10040 

/* [<][>][^][v][top][bottom][index][help] */