root/drivers/scsi/lpfc/lpfc_ct.c

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

DEFINITIONS

This source file includes following definitions.
  1. lpfc_ct_ignore_hbq_buffer
  2. lpfc_ct_unsol_buffer
  3. lpfc_ct_unsol_event
  4. lpfc_ct_handle_unsol_abort
  5. lpfc_free_ct_rsp
  6. lpfc_alloc_ct_rsp
  7. lpfc_ct_free_iocb
  8. lpfc_gen_req
  9. lpfc_ct_cmd
  10. lpfc_find_vport_by_did
  11. lpfc_prep_node_fc4type
  12. lpfc_ns_rsp_audit_did
  13. lpfc_ns_rsp
  14. lpfc_cmpl_ct_cmd_gid_ft
  15. lpfc_cmpl_ct_cmd_gid_pt
  16. lpfc_cmpl_ct_cmd_gff_id
  17. lpfc_cmpl_ct_cmd_gft_id
  18. lpfc_cmpl_ct
  19. lpfc_cmpl_ct_cmd_rft_id
  20. lpfc_cmpl_ct_cmd_rnn_id
  21. lpfc_cmpl_ct_cmd_rspn_id
  22. lpfc_cmpl_ct_cmd_rsnn_nn
  23. lpfc_cmpl_ct_cmd_da_id
  24. lpfc_cmpl_ct_cmd_rff_id
  25. lpfc_vport_symbolic_port_name
  26. lpfc_vport_symbolic_node_name
  27. lpfc_find_map_node
  28. lpfc_get_gidft_type
  29. lpfc_ns_cmd
  30. lpfc_cmpl_ct_disc_fdmi
  31. lpfc_fdmi_change_check
  32. lpfc_fdmi_hba_attr_wwnn
  33. lpfc_fdmi_hba_attr_manufacturer
  34. lpfc_fdmi_hba_attr_sn
  35. lpfc_fdmi_hba_attr_model
  36. lpfc_fdmi_hba_attr_description
  37. lpfc_fdmi_hba_attr_hdw_ver
  38. lpfc_fdmi_hba_attr_drvr_ver
  39. lpfc_fdmi_hba_attr_rom_ver
  40. lpfc_fdmi_hba_attr_fmw_ver
  41. lpfc_fdmi_hba_attr_os_ver
  42. lpfc_fdmi_hba_attr_ct_len
  43. lpfc_fdmi_hba_attr_symbolic_name
  44. lpfc_fdmi_hba_attr_vendor_info
  45. lpfc_fdmi_hba_attr_num_ports
  46. lpfc_fdmi_hba_attr_fabric_wwnn
  47. lpfc_fdmi_hba_attr_bios_ver
  48. lpfc_fdmi_hba_attr_bios_state
  49. lpfc_fdmi_hba_attr_vendor_id
  50. lpfc_fdmi_port_attr_fc4type
  51. lpfc_fdmi_port_attr_support_speed
  52. lpfc_fdmi_port_attr_speed
  53. lpfc_fdmi_port_attr_max_frame
  54. lpfc_fdmi_port_attr_os_devname
  55. lpfc_fdmi_port_attr_host_name
  56. lpfc_fdmi_port_attr_wwnn
  57. lpfc_fdmi_port_attr_wwpn
  58. lpfc_fdmi_port_attr_symbolic_name
  59. lpfc_fdmi_port_attr_port_type
  60. lpfc_fdmi_port_attr_class
  61. lpfc_fdmi_port_attr_fabric_wwpn
  62. lpfc_fdmi_port_attr_active_fc4type
  63. lpfc_fdmi_port_attr_port_state
  64. lpfc_fdmi_port_attr_num_disc
  65. lpfc_fdmi_port_attr_nportid
  66. lpfc_fdmi_smart_attr_service
  67. lpfc_fdmi_smart_attr_guid
  68. lpfc_fdmi_smart_attr_version
  69. lpfc_fdmi_smart_attr_model
  70. lpfc_fdmi_smart_attr_port_info
  71. lpfc_fdmi_smart_attr_qos
  72. lpfc_fdmi_smart_attr_security
  73. lpfc_fdmi_cmd
  74. lpfc_delayed_disc_tmo
  75. lpfc_delayed_disc_timeout_handler
  76. lpfc_decode_firmware_rev

   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  *                                                                 *
  10  * This program is free software; you can redistribute it and/or   *
  11  * modify it under the terms of version 2 of the GNU General       *
  12  * Public License as published by the Free Software Foundation.    *
  13  * This program is distributed in the hope that it will be useful. *
  14  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
  15  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
  16  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
  17  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  18  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
  19  * more details, a copy of which can be found in the file COPYING  *
  20  * included with this package.                                     *
  21  *******************************************************************/
  22 
  23 /*
  24  * Fibre Channel SCSI LAN Device Driver CT support: FC Generic Services FC-GS
  25  */
  26 
  27 #include <linux/blkdev.h>
  28 #include <linux/pci.h>
  29 #include <linux/interrupt.h>
  30 #include <linux/slab.h>
  31 #include <linux/utsname.h>
  32 
  33 #include <scsi/scsi.h>
  34 #include <scsi/scsi_device.h>
  35 #include <scsi/scsi_host.h>
  36 #include <scsi/scsi_transport_fc.h>
  37 #include <scsi/fc/fc_fs.h>
  38 
  39 #include "lpfc_hw4.h"
  40 #include "lpfc_hw.h"
  41 #include "lpfc_sli.h"
  42 #include "lpfc_sli4.h"
  43 #include "lpfc_nl.h"
  44 #include "lpfc_disc.h"
  45 #include "lpfc.h"
  46 #include "lpfc_scsi.h"
  47 #include "lpfc_nvme.h"
  48 #include "lpfc_logmsg.h"
  49 #include "lpfc_crtn.h"
  50 #include "lpfc_version.h"
  51 #include "lpfc_vport.h"
  52 #include "lpfc_debugfs.h"
  53 
  54 /* FDMI Port Speed definitions - FC-GS-7 */
  55 #define HBA_PORTSPEED_1GFC              0x00000001      /* 1G FC */
  56 #define HBA_PORTSPEED_2GFC              0x00000002      /* 2G FC */
  57 #define HBA_PORTSPEED_4GFC              0x00000008      /* 4G FC */
  58 #define HBA_PORTSPEED_10GFC             0x00000004      /* 10G FC */
  59 #define HBA_PORTSPEED_8GFC              0x00000010      /* 8G FC */
  60 #define HBA_PORTSPEED_16GFC             0x00000020      /* 16G FC */
  61 #define HBA_PORTSPEED_32GFC             0x00000040      /* 32G FC */
  62 #define HBA_PORTSPEED_20GFC             0x00000080      /* 20G FC */
  63 #define HBA_PORTSPEED_40GFC             0x00000100      /* 40G FC */
  64 #define HBA_PORTSPEED_128GFC            0x00000200      /* 128G FC */
  65 #define HBA_PORTSPEED_64GFC             0x00000400      /* 64G FC */
  66 #define HBA_PORTSPEED_256GFC            0x00000800      /* 256G FC */
  67 #define HBA_PORTSPEED_UNKNOWN           0x00008000      /* Unknown */
  68 #define HBA_PORTSPEED_10GE              0x00010000      /* 10G E */
  69 #define HBA_PORTSPEED_40GE              0x00020000      /* 40G E */
  70 #define HBA_PORTSPEED_100GE             0x00040000      /* 100G E */
  71 #define HBA_PORTSPEED_25GE              0x00080000      /* 25G E */
  72 #define HBA_PORTSPEED_50GE              0x00100000      /* 50G E */
  73 #define HBA_PORTSPEED_400GE             0x00200000      /* 400G E */
  74 
  75 #define FOURBYTES       4
  76 
  77 
  78 static char *lpfc_release_version = LPFC_DRIVER_VERSION;
  79 
  80 static void
  81 lpfc_ct_ignore_hbq_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  82                           struct lpfc_dmabuf *mp, uint32_t size)
  83 {
  84         if (!mp) {
  85                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  86                                 "0146 Ignoring unsolicited CT No HBQ "
  87                                 "status = x%x\n",
  88                                 piocbq->iocb.ulpStatus);
  89         }
  90         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  91                         "0145 Ignoring unsolicted CT HBQ Size:%d "
  92                         "status = x%x\n",
  93                         size, piocbq->iocb.ulpStatus);
  94 }
  95 
  96 static void
  97 lpfc_ct_unsol_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  98                      struct lpfc_dmabuf *mp, uint32_t size)
  99 {
 100         lpfc_ct_ignore_hbq_buffer(phba, piocbq, mp, size);
 101 }
 102 
 103 void
 104 lpfc_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 105                     struct lpfc_iocbq *piocbq)
 106 {
 107         struct lpfc_dmabuf *mp = NULL;
 108         IOCB_t *icmd = &piocbq->iocb;
 109         int i;
 110         struct lpfc_iocbq *iocbq;
 111         dma_addr_t paddr;
 112         uint32_t size;
 113         struct list_head head;
 114         struct lpfc_dmabuf *bdeBuf;
 115 
 116         if (lpfc_bsg_ct_unsol_event(phba, pring, piocbq) == 0)
 117                 return;
 118 
 119         if (unlikely(icmd->ulpStatus == IOSTAT_NEED_BUFFER)) {
 120                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
 121         } else if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
 122                    ((icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
 123                    IOERR_RCV_BUFFER_WAITING)) {
 124                 /* Not enough posted buffers; Try posting more buffers */
 125                 phba->fc_stat.NoRcvBuf++;
 126                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
 127                         lpfc_post_buffer(phba, pring, 2);
 128                 return;
 129         }
 130 
 131         /* If there are no BDEs associated with this IOCB,
 132          * there is nothing to do.
 133          */
 134         if (icmd->ulpBdeCount == 0)
 135                 return;
 136 
 137         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
 138                 INIT_LIST_HEAD(&head);
 139                 list_add_tail(&head, &piocbq->list);
 140                 list_for_each_entry(iocbq, &head, list) {
 141                         icmd = &iocbq->iocb;
 142                         if (icmd->ulpBdeCount == 0)
 143                                 continue;
 144                         bdeBuf = iocbq->context2;
 145                         iocbq->context2 = NULL;
 146                         size  = icmd->un.cont64[0].tus.f.bdeSize;
 147                         lpfc_ct_unsol_buffer(phba, piocbq, bdeBuf, size);
 148                         lpfc_in_buf_free(phba, bdeBuf);
 149                         if (icmd->ulpBdeCount == 2) {
 150                                 bdeBuf = iocbq->context3;
 151                                 iocbq->context3 = NULL;
 152                                 size  = icmd->unsli3.rcvsli3.bde2.tus.f.bdeSize;
 153                                 lpfc_ct_unsol_buffer(phba, piocbq, bdeBuf,
 154                                                      size);
 155                                 lpfc_in_buf_free(phba, bdeBuf);
 156                         }
 157                 }
 158                 list_del(&head);
 159         } else {
 160                 INIT_LIST_HEAD(&head);
 161                 list_add_tail(&head, &piocbq->list);
 162                 list_for_each_entry(iocbq, &head, list) {
 163                         icmd = &iocbq->iocb;
 164                         if (icmd->ulpBdeCount == 0)
 165                                 lpfc_ct_unsol_buffer(phba, iocbq, NULL, 0);
 166                         for (i = 0; i < icmd->ulpBdeCount; i++) {
 167                                 paddr = getPaddr(icmd->un.cont64[i].addrHigh,
 168                                                  icmd->un.cont64[i].addrLow);
 169                                 mp = lpfc_sli_ringpostbuf_get(phba, pring,
 170                                                               paddr);
 171                                 size = icmd->un.cont64[i].tus.f.bdeSize;
 172                                 lpfc_ct_unsol_buffer(phba, iocbq, mp, size);
 173                                 lpfc_in_buf_free(phba, mp);
 174                         }
 175                         lpfc_post_buffer(phba, pring, i);
 176                 }
 177                 list_del(&head);
 178         }
 179 }
 180 
 181 /**
 182  * lpfc_ct_handle_unsol_abort - ct upper level protocol abort handler
 183  * @phba: Pointer to HBA context object.
 184  * @dmabuf: pointer to a dmabuf that describes the FC sequence
 185  *
 186  * This function serves as the upper level protocol abort handler for CT
 187  * protocol.
 188  *
 189  * Return 1 if abort has been handled, 0 otherwise.
 190  **/
 191 int
 192 lpfc_ct_handle_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
 193 {
 194         int handled;
 195 
 196         /* CT upper level goes through BSG */
 197         handled = lpfc_bsg_ct_unsol_abort(phba, dmabuf);
 198 
 199         return handled;
 200 }
 201 
 202 static void
 203 lpfc_free_ct_rsp(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
 204 {
 205         struct lpfc_dmabuf *mlast, *next_mlast;
 206 
 207         list_for_each_entry_safe(mlast, next_mlast, &mlist->list, list) {
 208                 lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
 209                 list_del(&mlast->list);
 210                 kfree(mlast);
 211         }
 212         lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
 213         kfree(mlist);
 214         return;
 215 }
 216 
 217 static struct lpfc_dmabuf *
 218 lpfc_alloc_ct_rsp(struct lpfc_hba *phba, int cmdcode, struct ulp_bde64 *bpl,
 219                   uint32_t size, int *entries)
 220 {
 221         struct lpfc_dmabuf *mlist = NULL;
 222         struct lpfc_dmabuf *mp;
 223         int cnt, i = 0;
 224 
 225         /* We get chunks of FCELSSIZE */
 226         cnt = size > FCELSSIZE ? FCELSSIZE: size;
 227 
 228         while (size) {
 229                 /* Allocate buffer for rsp payload */
 230                 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
 231                 if (!mp) {
 232                         if (mlist)
 233                                 lpfc_free_ct_rsp(phba, mlist);
 234                         return NULL;
 235                 }
 236 
 237                 INIT_LIST_HEAD(&mp->list);
 238 
 239                 if (cmdcode == be16_to_cpu(SLI_CTNS_GID_FT) ||
 240                     cmdcode == be16_to_cpu(SLI_CTNS_GFF_ID))
 241                         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
 242                 else
 243                         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
 244 
 245                 if (!mp->virt) {
 246                         kfree(mp);
 247                         if (mlist)
 248                                 lpfc_free_ct_rsp(phba, mlist);
 249                         return NULL;
 250                 }
 251 
 252                 /* Queue it to a linked list */
 253                 if (!mlist)
 254                         mlist = mp;
 255                 else
 256                         list_add_tail(&mp->list, &mlist->list);
 257 
 258                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
 259                 /* build buffer ptr list for IOCB */
 260                 bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
 261                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
 262                 bpl->tus.f.bdeSize = (uint16_t) cnt;
 263                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
 264                 bpl++;
 265 
 266                 i++;
 267                 size -= cnt;
 268         }
 269 
 270         *entries = i;
 271         return mlist;
 272 }
 273 
 274 int
 275 lpfc_ct_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *ctiocb)
 276 {
 277         struct lpfc_dmabuf *buf_ptr;
 278 
 279         if (ctiocb->context_un.ndlp) {
 280                 lpfc_nlp_put(ctiocb->context_un.ndlp);
 281                 ctiocb->context_un.ndlp = NULL;
 282         }
 283         if (ctiocb->context1) {
 284                 buf_ptr = (struct lpfc_dmabuf *) ctiocb->context1;
 285                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
 286                 kfree(buf_ptr);
 287                 ctiocb->context1 = NULL;
 288         }
 289         if (ctiocb->context2) {
 290                 lpfc_free_ct_rsp(phba, (struct lpfc_dmabuf *) ctiocb->context2);
 291                 ctiocb->context2 = NULL;
 292         }
 293 
 294         if (ctiocb->context3) {
 295                 buf_ptr = (struct lpfc_dmabuf *) ctiocb->context3;
 296                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
 297                 kfree(buf_ptr);
 298                 ctiocb->context3 = NULL;
 299         }
 300         lpfc_sli_release_iocbq(phba, ctiocb);
 301         return 0;
 302 }
 303 
 304 /**
 305  * lpfc_gen_req - Build and issue a GEN_REQUEST command  to the SLI Layer
 306  * @vport: pointer to a host virtual N_Port data structure.
 307  * @bmp: Pointer to BPL for SLI command
 308  * @inp: Pointer to data buffer for response data.
 309  * @outp: Pointer to data buffer that hold the CT command.
 310  * @cmpl: completion routine to call when command completes
 311  * @ndlp: Destination NPort nodelist entry
 312  *
 313  * This function as the final part for issuing a CT command.
 314  */
 315 static int
 316 lpfc_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
 317              struct lpfc_dmabuf *inp, struct lpfc_dmabuf *outp,
 318              void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
 319                      struct lpfc_iocbq *),
 320              struct lpfc_nodelist *ndlp, uint32_t usr_flg, uint32_t num_entry,
 321              uint32_t tmo, uint8_t retry)
 322 {
 323         struct lpfc_hba  *phba = vport->phba;
 324         IOCB_t *icmd;
 325         struct lpfc_iocbq *geniocb;
 326         int rc;
 327 
 328         /* Allocate buffer for  command iocb */
 329         geniocb = lpfc_sli_get_iocbq(phba);
 330 
 331         if (geniocb == NULL)
 332                 return 1;
 333 
 334         icmd = &geniocb->iocb;
 335         icmd->un.genreq64.bdl.ulpIoTag32 = 0;
 336         icmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
 337         icmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
 338         icmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
 339         icmd->un.genreq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
 340 
 341         if (usr_flg)
 342                 geniocb->context3 = NULL;
 343         else
 344                 geniocb->context3 = (uint8_t *) bmp;
 345 
 346         /* Save for completion so we can release these resources */
 347         geniocb->context1 = (uint8_t *) inp;
 348         geniocb->context2 = (uint8_t *) outp;
 349         geniocb->context_un.ndlp = lpfc_nlp_get(ndlp);
 350 
 351         /* Fill in payload, bp points to frame payload */
 352         icmd->ulpCommand = CMD_GEN_REQUEST64_CR;
 353 
 354         /* Fill in rest of iocb */
 355         icmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
 356         icmd->un.genreq64.w5.hcsw.Dfctl = 0;
 357         icmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
 358         icmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
 359 
 360         if (!tmo) {
 361                  /* FC spec states we need 3 * ratov for CT requests */
 362                 tmo = (3 * phba->fc_ratov);
 363         }
 364         icmd->ulpTimeout = tmo;
 365         icmd->ulpBdeCount = 1;
 366         icmd->ulpLe = 1;
 367         icmd->ulpClass = CLASS3;
 368         icmd->ulpContext = ndlp->nlp_rpi;
 369         if (phba->sli_rev == LPFC_SLI_REV4)
 370                 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
 371 
 372         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
 373                 /* For GEN_REQUEST64_CR, use the RPI */
 374                 icmd->ulpCt_h = 0;
 375                 icmd->ulpCt_l = 0;
 376         }
 377 
 378         /* Issue GEN REQ IOCB for NPORT <did> */
 379         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 380                          "0119 Issue GEN REQ IOCB to NPORT x%x "
 381                          "Data: x%x x%x\n",
 382                          ndlp->nlp_DID, icmd->ulpIoTag,
 383                          vport->port_state);
 384         geniocb->iocb_cmpl = cmpl;
 385         geniocb->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
 386         geniocb->vport = vport;
 387         geniocb->retry = retry;
 388         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, geniocb, 0);
 389 
 390         if (rc == IOCB_ERROR) {
 391                 lpfc_sli_release_iocbq(phba, geniocb);
 392                 return 1;
 393         }
 394 
 395         return 0;
 396 }
 397 
 398 /**
 399  * lpfc_ct_cmd - Build and issue a CT command
 400  * @vport: pointer to a host virtual N_Port data structure.
 401  * @inmp: Pointer to data buffer for response data.
 402  * @bmp: Pointer to BPL for SLI command
 403  * @ndlp: Destination NPort nodelist entry
 404  * @cmpl: completion routine to call when command completes
 405  *
 406  * This function is called for issuing a CT command.
 407  */
 408 static int
 409 lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
 410             struct lpfc_dmabuf *bmp, struct lpfc_nodelist *ndlp,
 411             void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
 412                           struct lpfc_iocbq *),
 413             uint32_t rsp_size, uint8_t retry)
 414 {
 415         struct lpfc_hba  *phba = vport->phba;
 416         struct ulp_bde64 *bpl = (struct ulp_bde64 *) bmp->virt;
 417         struct lpfc_dmabuf *outmp;
 418         int cnt = 0, status;
 419         int cmdcode = ((struct lpfc_sli_ct_request *) inmp->virt)->
 420                 CommandResponse.bits.CmdRsp;
 421 
 422         bpl++;                  /* Skip past ct request */
 423 
 424         /* Put buffer(s) for ct rsp in bpl */
 425         outmp = lpfc_alloc_ct_rsp(phba, cmdcode, bpl, rsp_size, &cnt);
 426         if (!outmp)
 427                 return -ENOMEM;
 428         /*
 429          * Form the CT IOCB.  The total number of BDEs in this IOCB
 430          * is the single command plus response count from
 431          * lpfc_alloc_ct_rsp.
 432          */
 433         cnt += 1;
 434         status = lpfc_gen_req(vport, bmp, inmp, outmp, cmpl, ndlp, 0,
 435                               cnt, 0, retry);
 436         if (status) {
 437                 lpfc_free_ct_rsp(phba, outmp);
 438                 return -ENOMEM;
 439         }
 440         return 0;
 441 }
 442 
 443 struct lpfc_vport *
 444 lpfc_find_vport_by_did(struct lpfc_hba *phba, uint32_t did) {
 445         struct lpfc_vport *vport_curr;
 446         unsigned long flags;
 447 
 448         spin_lock_irqsave(&phba->port_list_lock, flags);
 449         list_for_each_entry(vport_curr, &phba->port_list, listentry) {
 450                 if ((vport_curr->fc_myDID) && (vport_curr->fc_myDID == did)) {
 451                         spin_unlock_irqrestore(&phba->port_list_lock, flags);
 452                         return vport_curr;
 453                 }
 454         }
 455         spin_unlock_irqrestore(&phba->port_list_lock, flags);
 456         return NULL;
 457 }
 458 
 459 static void
 460 lpfc_prep_node_fc4type(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
 461 {
 462         struct lpfc_nodelist *ndlp;
 463 
 464         if ((vport->port_type != LPFC_NPIV_PORT) ||
 465             !(vport->ct_flags & FC_CT_RFF_ID) || !vport->cfg_restrict_login) {
 466 
 467                 ndlp = lpfc_setup_disc_node(vport, Did);
 468 
 469                 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
 470                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
 471                                 "Parse GID_FTrsp: did:x%x flg:x%x x%x",
 472                                 Did, ndlp->nlp_flag, vport->fc_flag);
 473 
 474                         /* By default, the driver expects to support FCP FC4 */
 475                         if (fc4_type == FC_TYPE_FCP)
 476                                 ndlp->nlp_fc4_type |= NLP_FC4_FCP;
 477 
 478                         if (fc4_type == FC_TYPE_NVME)
 479                                 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
 480 
 481                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 482                                          "0238 Process x%06x NameServer Rsp "
 483                                          "Data: x%x x%x x%x x%x x%x\n", Did,
 484                                          ndlp->nlp_flag, ndlp->nlp_fc4_type,
 485                                          ndlp->nlp_state, vport->fc_flag,
 486                                          vport->fc_rscn_id_cnt);
 487 
 488                         /* if ndlp needs to be discovered and prior
 489                          * state of ndlp hit devloss, change state to
 490                          * allow rediscovery.
 491                          */
 492                         if (ndlp->nlp_flag & NLP_NPR_2B_DISC &&
 493                             ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
 494                                 lpfc_nlp_set_state(vport, ndlp,
 495                                                    NLP_STE_NPR_NODE);
 496                         }
 497                 } else {
 498                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
 499                                 "Skip1 GID_FTrsp: did:x%x flg:x%x cnt:%d",
 500                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
 501 
 502                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 503                                          "0239 Skip x%06x NameServer Rsp "
 504                                          "Data: x%x x%x %p\n",
 505                                          Did, vport->fc_flag,
 506                                          vport->fc_rscn_id_cnt, ndlp);
 507                 }
 508         } else {
 509                 if (!(vport->fc_flag & FC_RSCN_MODE) ||
 510                     lpfc_rscn_payload_check(vport, Did)) {
 511                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
 512                                 "Query GID_FTrsp: did:x%x flg:x%x cnt:%d",
 513                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
 514 
 515                         /*
 516                          * This NPortID was previously a FCP/NVMe target,
 517                          * Don't even bother to send GFF_ID.
 518                          */
 519                         ndlp = lpfc_findnode_did(vport, Did);
 520                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
 521                             (ndlp->nlp_type &
 522                             (NLP_FCP_TARGET | NLP_NVME_TARGET))) {
 523                                 if (fc4_type == FC_TYPE_FCP)
 524                                         ndlp->nlp_fc4_type |= NLP_FC4_FCP;
 525                                 if (fc4_type == FC_TYPE_NVME)
 526                                         ndlp->nlp_fc4_type |= NLP_FC4_NVME;
 527                                 lpfc_setup_disc_node(vport, Did);
 528                         } else if (lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
 529                                    0, Did) == 0)
 530                                 vport->num_disc_nodes++;
 531                         else
 532                                 lpfc_setup_disc_node(vport, Did);
 533                 } else {
 534                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
 535                                 "Skip2 GID_FTrsp: did:x%x flg:x%x cnt:%d",
 536                                 Did, vport->fc_flag, vport->fc_rscn_id_cnt);
 537 
 538                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 539                                          "0245 Skip x%06x NameServer Rsp "
 540                                          "Data: x%x x%x\n", Did,
 541                                          vport->fc_flag,
 542                                          vport->fc_rscn_id_cnt);
 543                 }
 544         }
 545 }
 546 
 547 static void
 548 lpfc_ns_rsp_audit_did(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
 549 {
 550         struct lpfc_hba *phba = vport->phba;
 551         struct lpfc_nodelist *ndlp = NULL;
 552         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 553         char *str;
 554 
 555         if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT)
 556                 str = "GID_FT";
 557         else
 558                 str = "GID_PT";
 559         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 560                          "6430 Process %s rsp for %08x type %x %s %s\n",
 561                          str, Did, fc4_type,
 562                          (fc4_type == FC_TYPE_FCP) ?  "FCP" : " ",
 563                          (fc4_type == FC_TYPE_NVME) ?  "NVME" : " ");
 564         /*
 565          * To conserve rpi's, filter out addresses for other
 566          * vports on the same physical HBAs.
 567          */
 568         if (Did != vport->fc_myDID &&
 569             (!lpfc_find_vport_by_did(phba, Did) ||
 570              vport->cfg_peer_port_login)) {
 571                 if (!phba->nvmet_support) {
 572                         /* FCPI/NVMEI path. Process Did */
 573                         lpfc_prep_node_fc4type(vport, Did, fc4_type);
 574                         return;
 575                 }
 576                 /* NVMET path.  NVMET only cares about NVMEI nodes. */
 577                 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
 578                         if (ndlp->nlp_type != NLP_NVME_INITIATOR ||
 579                             ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
 580                                 continue;
 581                         spin_lock_irq(shost->host_lock);
 582                         if (ndlp->nlp_DID == Did)
 583                                 ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
 584                         else
 585                                 ndlp->nlp_flag |= NLP_NVMET_RECOV;
 586                         spin_unlock_irq(shost->host_lock);
 587                 }
 588         }
 589 }
 590 
 591 static int
 592 lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint8_t fc4_type,
 593             uint32_t Size)
 594 {
 595         struct lpfc_sli_ct_request *Response =
 596                 (struct lpfc_sli_ct_request *) mp->virt;
 597         struct lpfc_dmabuf *mlast, *next_mp;
 598         uint32_t *ctptr = (uint32_t *) & Response->un.gid.PortType;
 599         uint32_t Did, CTentry;
 600         int Cnt;
 601         struct list_head head;
 602         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 603         struct lpfc_nodelist *ndlp = NULL;
 604 
 605         lpfc_set_disctmo(vport);
 606         vport->num_disc_nodes = 0;
 607         vport->fc_ns_retry = 0;
 608 
 609 
 610         list_add_tail(&head, &mp->list);
 611         list_for_each_entry_safe(mp, next_mp, &head, list) {
 612                 mlast = mp;
 613 
 614                 Cnt = Size  > FCELSSIZE ? FCELSSIZE : Size;
 615 
 616                 Size -= Cnt;
 617 
 618                 if (!ctptr) {
 619                         ctptr = (uint32_t *) mlast->virt;
 620                 } else
 621                         Cnt -= 16;      /* subtract length of CT header */
 622 
 623                 /* Loop through entire NameServer list of DIDs */
 624                 while (Cnt >= sizeof(uint32_t)) {
 625                         /* Get next DID from NameServer List */
 626                         CTentry = *ctptr++;
 627                         Did = ((be32_to_cpu(CTentry)) & Mask_DID);
 628                         lpfc_ns_rsp_audit_did(vport, Did, fc4_type);
 629                         if (CTentry & (cpu_to_be32(SLI_CT_LAST_ENTRY)))
 630                                 goto nsout1;
 631 
 632                         Cnt -= sizeof(uint32_t);
 633                 }
 634                 ctptr = NULL;
 635 
 636         }
 637 
 638         /* All GID_FT entries processed.  If the driver is running in
 639          * in target mode, put impacted nodes into recovery and drop
 640          * the RPI to flush outstanding IO.
 641          */
 642         if (vport->phba->nvmet_support) {
 643                 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
 644                         if (!(ndlp->nlp_flag & NLP_NVMET_RECOV))
 645                                 continue;
 646                         lpfc_disc_state_machine(vport, ndlp, NULL,
 647                                                 NLP_EVT_DEVICE_RECOVERY);
 648                         spin_lock_irq(shost->host_lock);
 649                         ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
 650                         spin_unlock_irq(shost->host_lock);
 651                 }
 652         }
 653 
 654 nsout1:
 655         list_del(&head);
 656         return 0;
 657 }
 658 
 659 static void
 660 lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 661                         struct lpfc_iocbq *rspiocb)
 662 {
 663         struct lpfc_vport *vport = cmdiocb->vport;
 664         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 665         IOCB_t *irsp;
 666         struct lpfc_dmabuf *outp;
 667         struct lpfc_dmabuf *inp;
 668         struct lpfc_sli_ct_request *CTrsp;
 669         struct lpfc_sli_ct_request *CTreq;
 670         struct lpfc_nodelist *ndlp;
 671         int rc, type;
 672 
 673         /* First save ndlp, before we overwrite it */
 674         ndlp = cmdiocb->context_un.ndlp;
 675 
 676         /* we pass cmdiocb to state machine which needs rspiocb as well */
 677         cmdiocb->context_un.rsp_iocb = rspiocb;
 678         inp = (struct lpfc_dmabuf *) cmdiocb->context1;
 679         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
 680         irsp = &rspiocb->iocb;
 681 
 682         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
 683                  "GID_FT cmpl:     status:x%x/x%x rtry:%d",
 684                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_ns_retry);
 685 
 686         /* Don't bother processing response if vport is being torn down. */
 687         if (vport->load_flag & FC_UNLOADING) {
 688                 if (vport->fc_flag & FC_RSCN_MODE)
 689                         lpfc_els_flush_rscn(vport);
 690                 goto out;
 691         }
 692 
 693         if (lpfc_els_chk_latt(vport)) {
 694                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 695                                  "0216 Link event during NS query\n");
 696                 if (vport->fc_flag & FC_RSCN_MODE)
 697                         lpfc_els_flush_rscn(vport);
 698                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
 699                 goto out;
 700         }
 701         if (lpfc_error_lost_link(irsp)) {
 702                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 703                                  "0226 NS query failed due to link event\n");
 704                 if (vport->fc_flag & FC_RSCN_MODE)
 705                         lpfc_els_flush_rscn(vport);
 706                 goto out;
 707         }
 708 
 709         spin_lock_irq(shost->host_lock);
 710         if (vport->fc_flag & FC_RSCN_DEFERRED) {
 711                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
 712                 spin_unlock_irq(shost->host_lock);
 713 
 714                 /* This is a GID_FT completing so the gidft_inp counter was
 715                  * incremented before the GID_FT was issued to the wire.
 716                  */
 717                 vport->gidft_inp--;
 718 
 719                 /*
 720                  * Skip processing the NS response
 721                  * Re-issue the NS cmd
 722                  */
 723                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 724                                  "0151 Process Deferred RSCN Data: x%x x%x\n",
 725                                  vport->fc_flag, vport->fc_rscn_id_cnt);
 726                 lpfc_els_handle_rscn(vport);
 727 
 728                 goto out;
 729         }
 730         spin_unlock_irq(shost->host_lock);
 731 
 732         if (irsp->ulpStatus) {
 733                 /* Check for retry */
 734                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
 735                         if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
 736                             (irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
 737                             IOERR_NO_RESOURCES)
 738                                 vport->fc_ns_retry++;
 739 
 740                         type = lpfc_get_gidft_type(vport, cmdiocb);
 741                         if (type == 0)
 742                                 goto out;
 743 
 744                         /* CT command is being retried */
 745                         vport->gidft_inp--;
 746                         rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
 747                                          vport->fc_ns_retry, type);
 748                         if (rc == 0)
 749                                 goto out;
 750                 }
 751                 if (vport->fc_flag & FC_RSCN_MODE)
 752                         lpfc_els_flush_rscn(vport);
 753                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
 754                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
 755                                  "0257 GID_FT Query error: 0x%x 0x%x\n",
 756                                  irsp->ulpStatus, vport->fc_ns_retry);
 757         } else {
 758                 /* Good status, continue checking */
 759                 CTreq = (struct lpfc_sli_ct_request *) inp->virt;
 760                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
 761                 if (CTrsp->CommandResponse.bits.CmdRsp ==
 762                     cpu_to_be16(SLI_CT_RESPONSE_FS_ACC)) {
 763                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 764                                          "0208 NameServer Rsp Data: x%x x%x "
 765                                          "sz x%x\n",
 766                                          vport->fc_flag,
 767                                          CTreq->un.gid.Fc4Type,
 768                                          irsp->un.genreq64.bdl.bdeSize);
 769 
 770                         lpfc_ns_rsp(vport,
 771                                     outp,
 772                                     CTreq->un.gid.Fc4Type,
 773                                     (uint32_t) (irsp->un.genreq64.bdl.bdeSize));
 774                 } else if (CTrsp->CommandResponse.bits.CmdRsp ==
 775                            be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
 776                         /* NameServer Rsp Error */
 777                         if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
 778                             && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
 779                                 lpfc_printf_vlog(vport, KERN_INFO,
 780                                         LOG_DISCOVERY,
 781                                         "0269 No NameServer Entries "
 782                                         "Data: x%x x%x x%x x%x\n",
 783                                         CTrsp->CommandResponse.bits.CmdRsp,
 784                                         (uint32_t) CTrsp->ReasonCode,
 785                                         (uint32_t) CTrsp->Explanation,
 786                                         vport->fc_flag);
 787 
 788                                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
 789                                 "GID_FT no entry  cmd:x%x rsn:x%x exp:x%x",
 790                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
 791                                 (uint32_t) CTrsp->ReasonCode,
 792                                 (uint32_t) CTrsp->Explanation);
 793                         } else {
 794                                 lpfc_printf_vlog(vport, KERN_INFO,
 795                                         LOG_DISCOVERY,
 796                                         "0240 NameServer Rsp Error "
 797                                         "Data: x%x x%x x%x x%x\n",
 798                                         CTrsp->CommandResponse.bits.CmdRsp,
 799                                         (uint32_t) CTrsp->ReasonCode,
 800                                         (uint32_t) CTrsp->Explanation,
 801                                         vport->fc_flag);
 802 
 803                                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
 804                                 "GID_FT rsp err1  cmd:x%x rsn:x%x exp:x%x",
 805                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
 806                                 (uint32_t) CTrsp->ReasonCode,
 807                                 (uint32_t) CTrsp->Explanation);
 808                         }
 809 
 810 
 811                 } else {
 812                         /* NameServer Rsp Error */
 813                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
 814                                         "0241 NameServer Rsp Error "
 815                                         "Data: x%x x%x x%x x%x\n",
 816                                         CTrsp->CommandResponse.bits.CmdRsp,
 817                                         (uint32_t) CTrsp->ReasonCode,
 818                                         (uint32_t) CTrsp->Explanation,
 819                                         vport->fc_flag);
 820 
 821                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
 822                                 "GID_FT rsp err2  cmd:x%x rsn:x%x exp:x%x",
 823                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
 824                                 (uint32_t) CTrsp->ReasonCode,
 825                                 (uint32_t) CTrsp->Explanation);
 826                 }
 827                 vport->gidft_inp--;
 828         }
 829 
 830         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 831                          "4216 GID_FT cmpl inp %d disc %d\n",
 832                          vport->gidft_inp, vport->num_disc_nodes);
 833 
 834         /* Link up / RSCN discovery */
 835         if ((vport->num_disc_nodes == 0) &&
 836             (vport->gidft_inp == 0)) {
 837                 /*
 838                  * The driver has cycled through all Nports in the RSCN payload.
 839                  * Complete the handling by cleaning up and marking the
 840                  * current driver state.
 841                  */
 842                 if (vport->port_state >= LPFC_DISC_AUTH) {
 843                         if (vport->fc_flag & FC_RSCN_MODE) {
 844                                 lpfc_els_flush_rscn(vport);
 845                                 spin_lock_irq(shost->host_lock);
 846                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
 847                                 spin_unlock_irq(shost->host_lock);
 848                         }
 849                         else
 850                                 lpfc_els_flush_rscn(vport);
 851                 }
 852 
 853                 lpfc_disc_start(vport);
 854         }
 855 out:
 856         cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
 857         lpfc_ct_free_iocb(phba, cmdiocb);
 858         return;
 859 }
 860 
 861 static void
 862 lpfc_cmpl_ct_cmd_gid_pt(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 863                         struct lpfc_iocbq *rspiocb)
 864 {
 865         struct lpfc_vport *vport = cmdiocb->vport;
 866         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 867         IOCB_t *irsp;
 868         struct lpfc_dmabuf *outp;
 869         struct lpfc_dmabuf *inp;
 870         struct lpfc_sli_ct_request *CTrsp;
 871         struct lpfc_sli_ct_request *CTreq;
 872         struct lpfc_nodelist *ndlp;
 873         int rc;
 874 
 875         /* First save ndlp, before we overwrite it */
 876         ndlp = cmdiocb->context_un.ndlp;
 877 
 878         /* we pass cmdiocb to state machine which needs rspiocb as well */
 879         cmdiocb->context_un.rsp_iocb = rspiocb;
 880         inp = (struct lpfc_dmabuf *)cmdiocb->context1;
 881         outp = (struct lpfc_dmabuf *)cmdiocb->context2;
 882         irsp = &rspiocb->iocb;
 883 
 884         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
 885                               "GID_PT cmpl:     status:x%x/x%x rtry:%d",
 886                               irsp->ulpStatus, irsp->un.ulpWord[4],
 887                               vport->fc_ns_retry);
 888 
 889         /* Don't bother processing response if vport is being torn down. */
 890         if (vport->load_flag & FC_UNLOADING) {
 891                 if (vport->fc_flag & FC_RSCN_MODE)
 892                         lpfc_els_flush_rscn(vport);
 893                 goto out;
 894         }
 895 
 896         if (lpfc_els_chk_latt(vport)) {
 897                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 898                                  "4108 Link event during NS query\n");
 899                 if (vport->fc_flag & FC_RSCN_MODE)
 900                         lpfc_els_flush_rscn(vport);
 901                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
 902                 goto out;
 903         }
 904         if (lpfc_error_lost_link(irsp)) {
 905                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 906                                  "4166 NS query failed due to link event\n");
 907                 if (vport->fc_flag & FC_RSCN_MODE)
 908                         lpfc_els_flush_rscn(vport);
 909                 goto out;
 910         }
 911 
 912         spin_lock_irq(shost->host_lock);
 913         if (vport->fc_flag & FC_RSCN_DEFERRED) {
 914                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
 915                 spin_unlock_irq(shost->host_lock);
 916 
 917                 /* This is a GID_PT completing so the gidft_inp counter was
 918                  * incremented before the GID_PT was issued to the wire.
 919                  */
 920                 vport->gidft_inp--;
 921 
 922                 /*
 923                  * Skip processing the NS response
 924                  * Re-issue the NS cmd
 925                  */
 926                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
 927                                  "4167 Process Deferred RSCN Data: x%x x%x\n",
 928                                  vport->fc_flag, vport->fc_rscn_id_cnt);
 929                 lpfc_els_handle_rscn(vport);
 930 
 931                 goto out;
 932         }
 933         spin_unlock_irq(shost->host_lock);
 934 
 935         if (irsp->ulpStatus) {
 936                 /* Check for retry */
 937                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
 938                         if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
 939                             (irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
 940                             IOERR_NO_RESOURCES)
 941                                 vport->fc_ns_retry++;
 942 
 943                         /* CT command is being retried */
 944                         vport->gidft_inp--;
 945                         rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_PT,
 946                                          vport->fc_ns_retry, GID_PT_N_PORT);
 947                         if (rc == 0)
 948                                 goto out;
 949                 }
 950                 if (vport->fc_flag & FC_RSCN_MODE)
 951                         lpfc_els_flush_rscn(vport);
 952                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
 953                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
 954                                  "4103 GID_FT Query error: 0x%x 0x%x\n",
 955                                  irsp->ulpStatus, vport->fc_ns_retry);
 956         } else {
 957                 /* Good status, continue checking */
 958                 CTreq = (struct lpfc_sli_ct_request *)inp->virt;
 959                 CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
 960                 if (CTrsp->CommandResponse.bits.CmdRsp ==
 961                     cpu_to_be16(SLI_CT_RESPONSE_FS_ACC)) {
 962                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 963                                          "4105 NameServer Rsp Data: x%x x%x\n",
 964                                          vport->fc_flag,
 965                                          CTreq->un.gid.Fc4Type);
 966 
 967                         lpfc_ns_rsp(vport,
 968                                     outp,
 969                                     CTreq->un.gid.Fc4Type,
 970                                     (uint32_t)(irsp->un.genreq64.bdl.bdeSize));
 971                 } else if (CTrsp->CommandResponse.bits.CmdRsp ==
 972                            be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
 973                         /* NameServer Rsp Error */
 974                         if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
 975                             && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
 976                                 lpfc_printf_vlog(
 977                                         vport, KERN_INFO, LOG_DISCOVERY,
 978                                         "4106 No NameServer Entries "
 979                                         "Data: x%x x%x x%x x%x\n",
 980                                         CTrsp->CommandResponse.bits.CmdRsp,
 981                                         (uint32_t)CTrsp->ReasonCode,
 982                                         (uint32_t)CTrsp->Explanation,
 983                                         vport->fc_flag);
 984 
 985                                 lpfc_debugfs_disc_trc(
 986                                 vport, LPFC_DISC_TRC_CT,
 987                                 "GID_PT no entry  cmd:x%x rsn:x%x exp:x%x",
 988                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
 989                                 (uint32_t)CTrsp->ReasonCode,
 990                                 (uint32_t)CTrsp->Explanation);
 991                         } else {
 992                                 lpfc_printf_vlog(
 993                                         vport, KERN_INFO, LOG_DISCOVERY,
 994                                         "4107 NameServer Rsp Error "
 995                                         "Data: x%x x%x x%x x%x\n",
 996                                         CTrsp->CommandResponse.bits.CmdRsp,
 997                                         (uint32_t)CTrsp->ReasonCode,
 998                                         (uint32_t)CTrsp->Explanation,
 999                                         vport->fc_flag);
1000 
1001                                 lpfc_debugfs_disc_trc(
1002                                 vport, LPFC_DISC_TRC_CT,
1003                                 "GID_PT rsp err1  cmd:x%x rsn:x%x exp:x%x",
1004                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1005                                 (uint32_t)CTrsp->ReasonCode,
1006                                 (uint32_t)CTrsp->Explanation);
1007                         }
1008                 } else {
1009                         /* NameServer Rsp Error */
1010                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1011                                          "4109 NameServer Rsp Error "
1012                                          "Data: x%x x%x x%x x%x\n",
1013                                          CTrsp->CommandResponse.bits.CmdRsp,
1014                                          (uint32_t)CTrsp->ReasonCode,
1015                                          (uint32_t)CTrsp->Explanation,
1016                                          vport->fc_flag);
1017 
1018                         lpfc_debugfs_disc_trc(
1019                                 vport, LPFC_DISC_TRC_CT,
1020                                 "GID_PT rsp err2  cmd:x%x rsn:x%x exp:x%x",
1021                                 (uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1022                                 (uint32_t)CTrsp->ReasonCode,
1023                                 (uint32_t)CTrsp->Explanation);
1024                 }
1025                 vport->gidft_inp--;
1026         }
1027         /* Link up / RSCN discovery */
1028         if ((vport->num_disc_nodes == 0) &&
1029             (vport->gidft_inp == 0)) {
1030                 /*
1031                  * The driver has cycled through all Nports in the RSCN payload.
1032                  * Complete the handling by cleaning up and marking the
1033                  * current driver state.
1034                  */
1035                 if (vport->port_state >= LPFC_DISC_AUTH) {
1036                         if (vport->fc_flag & FC_RSCN_MODE) {
1037                                 lpfc_els_flush_rscn(vport);
1038                                 spin_lock_irq(shost->host_lock);
1039                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1040                                 spin_unlock_irq(shost->host_lock);
1041                         } else {
1042                                 lpfc_els_flush_rscn(vport);
1043                         }
1044                 }
1045 
1046                 lpfc_disc_start(vport);
1047         }
1048 out:
1049         cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
1050         lpfc_ct_free_iocb(phba, cmdiocb);
1051 }
1052 
1053 static void
1054 lpfc_cmpl_ct_cmd_gff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1055                         struct lpfc_iocbq *rspiocb)
1056 {
1057         struct lpfc_vport *vport = cmdiocb->vport;
1058         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1059         IOCB_t *irsp = &rspiocb->iocb;
1060         struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *) cmdiocb->context1;
1061         struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1062         struct lpfc_sli_ct_request *CTrsp;
1063         int did, rc, retry;
1064         uint8_t fbits;
1065         struct lpfc_nodelist *ndlp;
1066 
1067         did = ((struct lpfc_sli_ct_request *) inp->virt)->un.gff.PortId;
1068         did = be32_to_cpu(did);
1069 
1070         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1071                 "GFF_ID cmpl:     status:x%x/x%x did:x%x",
1072                 irsp->ulpStatus, irsp->un.ulpWord[4], did);
1073 
1074         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1075                 /* Good status, continue checking */
1076                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1077                 fbits = CTrsp->un.gff_acc.fbits[FCP_TYPE_FEATURE_OFFSET];
1078 
1079                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1080                                  "6431 Process GFF_ID rsp for %08x "
1081                                  "fbits %02x %s %s\n",
1082                                  did, fbits,
1083                                  (fbits & FC4_FEATURE_INIT) ? "Initiator" : " ",
1084                                  (fbits & FC4_FEATURE_TARGET) ? "Target" : " ");
1085 
1086                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1087                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
1088                         if ((fbits & FC4_FEATURE_INIT) &&
1089                             !(fbits & FC4_FEATURE_TARGET)) {
1090                                 lpfc_printf_vlog(vport, KERN_INFO,
1091                                                  LOG_DISCOVERY,
1092                                                  "0270 Skip x%x GFF "
1093                                                  "NameServer Rsp Data: (init) "
1094                                                  "x%x x%x\n", did, fbits,
1095                                                  vport->fc_rscn_id_cnt);
1096                                 goto out;
1097                         }
1098                 }
1099         }
1100         else {
1101                 /* Check for retry */
1102                 if (cmdiocb->retry < LPFC_MAX_NS_RETRY) {
1103                         retry = 1;
1104                         if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1105                                 switch ((irsp->un.ulpWord[4] &
1106                                         IOERR_PARAM_MASK)) {
1107 
1108                                 case IOERR_NO_RESOURCES:
1109                                         /* We don't increment the retry
1110                                          * count for this case.
1111                                          */
1112                                         break;
1113                                 case IOERR_LINK_DOWN:
1114                                 case IOERR_SLI_ABORTED:
1115                                 case IOERR_SLI_DOWN:
1116                                         retry = 0;
1117                                         break;
1118                                 default:
1119                                         cmdiocb->retry++;
1120                                 }
1121                         }
1122                         else
1123                                 cmdiocb->retry++;
1124 
1125                         if (retry) {
1126                                 /* CT command is being retried */
1127                                 rc = lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
1128                                          cmdiocb->retry, did);
1129                                 if (rc == 0) {
1130                                         /* success */
1131                                         lpfc_ct_free_iocb(phba, cmdiocb);
1132                                         return;
1133                                 }
1134                         }
1135                 }
1136                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1137                                  "0267 NameServer GFF Rsp "
1138                                  "x%x Error (%d %d) Data: x%x x%x\n",
1139                                  did, irsp->ulpStatus, irsp->un.ulpWord[4],
1140                                  vport->fc_flag, vport->fc_rscn_id_cnt);
1141         }
1142 
1143         /* This is a target port, unregistered port, or the GFF_ID failed */
1144         ndlp = lpfc_setup_disc_node(vport, did);
1145         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
1146                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1147                                  "0242 Process x%x GFF "
1148                                  "NameServer Rsp Data: x%x x%x x%x\n",
1149                                  did, ndlp->nlp_flag, vport->fc_flag,
1150                                  vport->fc_rscn_id_cnt);
1151         } else {
1152                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1153                                  "0243 Skip x%x GFF "
1154                                  "NameServer Rsp Data: x%x x%x\n", did,
1155                                  vport->fc_flag, vport->fc_rscn_id_cnt);
1156         }
1157 out:
1158         /* Link up / RSCN discovery */
1159         if (vport->num_disc_nodes)
1160                 vport->num_disc_nodes--;
1161         if (vport->num_disc_nodes == 0) {
1162                 /*
1163                  * The driver has cycled through all Nports in the RSCN payload.
1164                  * Complete the handling by cleaning up and marking the
1165                  * current driver state.
1166                  */
1167                 if (vport->port_state >= LPFC_DISC_AUTH) {
1168                         if (vport->fc_flag & FC_RSCN_MODE) {
1169                                 lpfc_els_flush_rscn(vport);
1170                                 spin_lock_irq(shost->host_lock);
1171                                 vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1172                                 spin_unlock_irq(shost->host_lock);
1173                         }
1174                         else
1175                                 lpfc_els_flush_rscn(vport);
1176                 }
1177                 lpfc_disc_start(vport);
1178         }
1179         lpfc_ct_free_iocb(phba, cmdiocb);
1180         return;
1181 }
1182 
1183 static void
1184 lpfc_cmpl_ct_cmd_gft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1185                                 struct lpfc_iocbq *rspiocb)
1186 {
1187         struct lpfc_vport *vport = cmdiocb->vport;
1188         IOCB_t *irsp = &rspiocb->iocb;
1189         struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *)cmdiocb->context1;
1190         struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *)cmdiocb->context2;
1191         struct lpfc_sli_ct_request *CTrsp;
1192         int did;
1193         struct lpfc_nodelist *ndlp;
1194         uint32_t fc4_data_0, fc4_data_1;
1195 
1196         did = ((struct lpfc_sli_ct_request *)inp->virt)->un.gft.PortId;
1197         did = be32_to_cpu(did);
1198 
1199         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1200                               "GFT_ID cmpl: status:x%x/x%x did:x%x",
1201                               irsp->ulpStatus, irsp->un.ulpWord[4], did);
1202 
1203         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1204                 /* Good status, continue checking */
1205                 CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
1206                 fc4_data_0 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[0]);
1207                 fc4_data_1 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[1]);
1208 
1209                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1210                                  "6432 Process GFT_ID rsp for %08x "
1211                                  "Data %08x %08x %s %s\n",
1212                                  did, fc4_data_0, fc4_data_1,
1213                                  (fc4_data_0 & LPFC_FC4_TYPE_BITMASK) ?
1214                                   "FCP" : " ",
1215                                  (fc4_data_1 & LPFC_FC4_TYPE_BITMASK) ?
1216                                   "NVME" : " ");
1217 
1218                 ndlp = lpfc_findnode_did(vport, did);
1219                 if (ndlp) {
1220                         /* The bitmask value for FCP and NVME FCP types is
1221                          * the same because they are 32 bits distant from
1222                          * each other in word0 and word0.
1223                          */
1224                         if (fc4_data_0 & LPFC_FC4_TYPE_BITMASK)
1225                                 ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1226                         if (fc4_data_1 &  LPFC_FC4_TYPE_BITMASK)
1227                                 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1228                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1229                                          "3064 Setting ndlp x%px, DID x%06x "
1230                                          "with FC4 x%08x, Data: x%08x x%08x "
1231                                          "%d\n",
1232                                          ndlp, did, ndlp->nlp_fc4_type,
1233                                          FC_TYPE_FCP, FC_TYPE_NVME,
1234                                          ndlp->nlp_state);
1235 
1236                         if (ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE &&
1237                             ndlp->nlp_fc4_type) {
1238                                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1239 
1240                                 lpfc_nlp_set_state(vport, ndlp,
1241                                                    NLP_STE_PRLI_ISSUE);
1242                                 lpfc_issue_els_prli(vport, ndlp, 0);
1243                         } else if (!ndlp->nlp_fc4_type) {
1244                                 /* If fc4 type is still unknown, then LOGO */
1245                                 lpfc_printf_vlog(vport, KERN_INFO,
1246                                                  LOG_DISCOVERY,
1247                                                  "6443 Sending LOGO ndlp x%px,"
1248                                                  "DID x%06x with fc4_type: "
1249                                                  "x%08x, state: %d\n",
1250                                                  ndlp, did, ndlp->nlp_fc4_type,
1251                                                  ndlp->nlp_state);
1252                                 lpfc_issue_els_logo(vport, ndlp, 0);
1253                                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1254                                 lpfc_nlp_set_state(vport, ndlp,
1255                                                    NLP_STE_NPR_NODE);
1256                         }
1257                 }
1258         } else
1259                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1260                                  "3065 GFT_ID failed x%08x\n", irsp->ulpStatus);
1261 
1262         lpfc_ct_free_iocb(phba, cmdiocb);
1263 }
1264 
1265 static void
1266 lpfc_cmpl_ct(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1267              struct lpfc_iocbq *rspiocb)
1268 {
1269         struct lpfc_vport *vport = cmdiocb->vport;
1270         struct lpfc_dmabuf *inp;
1271         struct lpfc_dmabuf *outp;
1272         IOCB_t *irsp;
1273         struct lpfc_sli_ct_request *CTrsp;
1274         struct lpfc_nodelist *ndlp;
1275         int cmdcode, rc;
1276         uint8_t retry;
1277         uint32_t latt;
1278 
1279         /* First save ndlp, before we overwrite it */
1280         ndlp = cmdiocb->context_un.ndlp;
1281 
1282         /* we pass cmdiocb to state machine which needs rspiocb as well */
1283         cmdiocb->context_un.rsp_iocb = rspiocb;
1284 
1285         inp = (struct lpfc_dmabuf *) cmdiocb->context1;
1286         outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1287         irsp = &rspiocb->iocb;
1288 
1289         cmdcode = be16_to_cpu(((struct lpfc_sli_ct_request *) inp->virt)->
1290                                         CommandResponse.bits.CmdRsp);
1291         CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1292 
1293         latt = lpfc_els_chk_latt(vport);
1294 
1295         /* RFT request completes status <ulpStatus> CmdRsp <CmdRsp> */
1296         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1297                          "0209 CT Request completes, latt %d, "
1298                          "ulpStatus x%x CmdRsp x%x, Context x%x, Tag x%x\n",
1299                          latt, irsp->ulpStatus,
1300                          CTrsp->CommandResponse.bits.CmdRsp,
1301                          cmdiocb->iocb.ulpContext, cmdiocb->iocb.ulpIoTag);
1302 
1303         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1304                 "CT cmd cmpl:     status:x%x/x%x cmd:x%x",
1305                 irsp->ulpStatus, irsp->un.ulpWord[4], cmdcode);
1306 
1307         if (irsp->ulpStatus) {
1308                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1309                                  "0268 NS cmd x%x Error (x%x x%x)\n",
1310                                  cmdcode, irsp->ulpStatus, irsp->un.ulpWord[4]);
1311 
1312                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1313                         (((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1314                           IOERR_SLI_DOWN) ||
1315                          ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1316                           IOERR_SLI_ABORTED)))
1317                         goto out;
1318 
1319                 retry = cmdiocb->retry;
1320                 if (retry >= LPFC_MAX_NS_RETRY)
1321                         goto out;
1322 
1323                 retry++;
1324                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1325                                  "0250 Retrying NS cmd %x\n", cmdcode);
1326                 rc = lpfc_ns_cmd(vport, cmdcode, retry, 0);
1327                 if (rc == 0)
1328                         goto out;
1329         }
1330 
1331 out:
1332         cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
1333         lpfc_ct_free_iocb(phba, cmdiocb);
1334         return;
1335 }
1336 
1337 static void
1338 lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1339                         struct lpfc_iocbq *rspiocb)
1340 {
1341         IOCB_t *irsp = &rspiocb->iocb;
1342         struct lpfc_vport *vport = cmdiocb->vport;
1343 
1344         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1345                 struct lpfc_dmabuf *outp;
1346                 struct lpfc_sli_ct_request *CTrsp;
1347 
1348                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1349                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1350                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1351                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1352                         vport->ct_flags |= FC_CT_RFT_ID;
1353         }
1354         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1355         return;
1356 }
1357 
1358 static void
1359 lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1360                         struct lpfc_iocbq *rspiocb)
1361 {
1362         IOCB_t *irsp = &rspiocb->iocb;
1363         struct lpfc_vport *vport = cmdiocb->vport;
1364 
1365         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1366                 struct lpfc_dmabuf *outp;
1367                 struct lpfc_sli_ct_request *CTrsp;
1368 
1369                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1370                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1371                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1372                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1373                         vport->ct_flags |= FC_CT_RNN_ID;
1374         }
1375         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1376         return;
1377 }
1378 
1379 static void
1380 lpfc_cmpl_ct_cmd_rspn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1381                          struct lpfc_iocbq *rspiocb)
1382 {
1383         IOCB_t *irsp = &rspiocb->iocb;
1384         struct lpfc_vport *vport = cmdiocb->vport;
1385 
1386         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1387                 struct lpfc_dmabuf *outp;
1388                 struct lpfc_sli_ct_request *CTrsp;
1389 
1390                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1391                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1392                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1393                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1394                         vport->ct_flags |= FC_CT_RSPN_ID;
1395         }
1396         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1397         return;
1398 }
1399 
1400 static void
1401 lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1402                          struct lpfc_iocbq *rspiocb)
1403 {
1404         IOCB_t *irsp = &rspiocb->iocb;
1405         struct lpfc_vport *vport = cmdiocb->vport;
1406 
1407         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1408                 struct lpfc_dmabuf *outp;
1409                 struct lpfc_sli_ct_request *CTrsp;
1410 
1411                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1412                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1413                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1414                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1415                         vport->ct_flags |= FC_CT_RSNN_NN;
1416         }
1417         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1418         return;
1419 }
1420 
1421 static void
1422 lpfc_cmpl_ct_cmd_da_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1423  struct lpfc_iocbq *rspiocb)
1424 {
1425         struct lpfc_vport *vport = cmdiocb->vport;
1426 
1427         /* even if it fails we will act as though it succeeded. */
1428         vport->ct_flags = 0;
1429         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1430         return;
1431 }
1432 
1433 static void
1434 lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1435                         struct lpfc_iocbq *rspiocb)
1436 {
1437         IOCB_t *irsp = &rspiocb->iocb;
1438         struct lpfc_vport *vport = cmdiocb->vport;
1439 
1440         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1441                 struct lpfc_dmabuf *outp;
1442                 struct lpfc_sli_ct_request *CTrsp;
1443 
1444                 outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1445                 CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1446                 if (CTrsp->CommandResponse.bits.CmdRsp ==
1447                     be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1448                         vport->ct_flags |= FC_CT_RFF_ID;
1449         }
1450         lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1451         return;
1452 }
1453 
1454 /*
1455  * Although the symbolic port name is thought to be an integer
1456  * as of January 18, 2016, leave it as a string until more of
1457  * the record state becomes defined.
1458  */
1459 int
1460 lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol,
1461         size_t size)
1462 {
1463         int n;
1464 
1465         /*
1466          * Use the lpfc board number as the Symbolic Port
1467          * Name object.  NPIV is not in play so this integer
1468          * value is sufficient and unique per FC-ID.
1469          */
1470         n = scnprintf(symbol, size, "%d", vport->phba->brd_no);
1471         return n;
1472 }
1473 
1474 
1475 int
1476 lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol,
1477         size_t size)
1478 {
1479         char fwrev[FW_REV_STR_SIZE] = {0};
1480         char tmp[MAXHOSTNAMELEN] = {0};
1481 
1482         memset(symbol, 0, size);
1483 
1484         scnprintf(tmp, sizeof(tmp), "Emulex %s", vport->phba->ModelName);
1485         if (strlcat(symbol, tmp, size) >= size)
1486                 goto buffer_done;
1487 
1488         lpfc_decode_firmware_rev(vport->phba, fwrev, 0);
1489         scnprintf(tmp, sizeof(tmp), " FV%s", fwrev);
1490         if (strlcat(symbol, tmp, size) >= size)
1491                 goto buffer_done;
1492 
1493         scnprintf(tmp, sizeof(tmp), " DV%s", lpfc_release_version);
1494         if (strlcat(symbol, tmp, size) >= size)
1495                 goto buffer_done;
1496 
1497         scnprintf(tmp, sizeof(tmp), " HN:%s", vport->phba->os_host_name);
1498         if (strlcat(symbol, tmp, size) >= size)
1499                 goto buffer_done;
1500 
1501         /* Note :- OS name is "Linux" */
1502         scnprintf(tmp, sizeof(tmp), " OS:%s", init_utsname()->sysname);
1503         strlcat(symbol, tmp, size);
1504 
1505 buffer_done:
1506         return strnlen(symbol, size);
1507 
1508 }
1509 
1510 static uint32_t
1511 lpfc_find_map_node(struct lpfc_vport *vport)
1512 {
1513         struct lpfc_nodelist *ndlp, *next_ndlp;
1514         struct Scsi_Host  *shost;
1515         uint32_t cnt = 0;
1516 
1517         shost = lpfc_shost_from_vport(vport);
1518         spin_lock_irq(shost->host_lock);
1519         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1520                 if (ndlp->nlp_type & NLP_FABRIC)
1521                         continue;
1522                 if ((ndlp->nlp_state == NLP_STE_MAPPED_NODE) ||
1523                     (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE))
1524                         cnt++;
1525         }
1526         spin_unlock_irq(shost->host_lock);
1527         return cnt;
1528 }
1529 
1530 /*
1531  * This routine will return the FC4 Type associated with the CT
1532  * GID_FT command.
1533  */
1534 int
1535 lpfc_get_gidft_type(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb)
1536 {
1537         struct lpfc_sli_ct_request *CtReq;
1538         struct lpfc_dmabuf *mp;
1539         uint32_t type;
1540 
1541         mp = cmdiocb->context1;
1542         if (mp == NULL)
1543                 return 0;
1544         CtReq = (struct lpfc_sli_ct_request *)mp->virt;
1545         type = (uint32_t)CtReq->un.gid.Fc4Type;
1546         if ((type != SLI_CTPT_FCP) && (type != SLI_CTPT_NVME))
1547                 return 0;
1548         return type;
1549 }
1550 
1551 /*
1552  * lpfc_ns_cmd
1553  * Description:
1554  *    Issue Cmd to NameServer
1555  *       SLI_CTNS_GID_FT
1556  *       LI_CTNS_RFT_ID
1557  */
1558 int
1559 lpfc_ns_cmd(struct lpfc_vport *vport, int cmdcode,
1560             uint8_t retry, uint32_t context)
1561 {
1562         struct lpfc_nodelist * ndlp;
1563         struct lpfc_hba *phba = vport->phba;
1564         struct lpfc_dmabuf *mp, *bmp;
1565         struct lpfc_sli_ct_request *CtReq;
1566         struct ulp_bde64 *bpl;
1567         void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
1568                       struct lpfc_iocbq *) = NULL;
1569         uint32_t *ptr;
1570         uint32_t rsp_size = 1024;
1571         size_t   size;
1572         int rc = 0;
1573 
1574         ndlp = lpfc_findnode_did(vport, NameServer_DID);
1575         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)
1576             || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) {
1577                 rc=1;
1578                 goto ns_cmd_exit;
1579         }
1580 
1581         /* fill in BDEs for command */
1582         /* Allocate buffer for command payload */
1583         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1584         if (!mp) {
1585                 rc=2;
1586                 goto ns_cmd_exit;
1587         }
1588 
1589         INIT_LIST_HEAD(&mp->list);
1590         mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
1591         if (!mp->virt) {
1592                 rc=3;
1593                 goto ns_cmd_free_mp;
1594         }
1595 
1596         /* Allocate buffer for Buffer ptr list */
1597         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1598         if (!bmp) {
1599                 rc=4;
1600                 goto ns_cmd_free_mpvirt;
1601         }
1602 
1603         INIT_LIST_HEAD(&bmp->list);
1604         bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(bmp->phys));
1605         if (!bmp->virt) {
1606                 rc=5;
1607                 goto ns_cmd_free_bmp;
1608         }
1609 
1610         /* NameServer Req */
1611         lpfc_printf_vlog(vport, KERN_INFO ,LOG_DISCOVERY,
1612                          "0236 NameServer Req Data: x%x x%x x%x x%x\n",
1613                          cmdcode, vport->fc_flag, vport->fc_rscn_id_cnt,
1614                          context);
1615 
1616         bpl = (struct ulp_bde64 *) bmp->virt;
1617         memset(bpl, 0, sizeof(struct ulp_bde64));
1618         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
1619         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
1620         bpl->tus.f.bdeFlags = 0;
1621         if (cmdcode == SLI_CTNS_GID_FT)
1622                 bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1623         else if (cmdcode == SLI_CTNS_GID_PT)
1624                 bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1625         else if (cmdcode == SLI_CTNS_GFF_ID)
1626                 bpl->tus.f.bdeSize = GFF_REQUEST_SZ;
1627         else if (cmdcode == SLI_CTNS_GFT_ID)
1628                 bpl->tus.f.bdeSize = GFT_REQUEST_SZ;
1629         else if (cmdcode == SLI_CTNS_RFT_ID)
1630                 bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
1631         else if (cmdcode == SLI_CTNS_RNN_ID)
1632                 bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
1633         else if (cmdcode == SLI_CTNS_RSPN_ID)
1634                 bpl->tus.f.bdeSize = RSPN_REQUEST_SZ;
1635         else if (cmdcode == SLI_CTNS_RSNN_NN)
1636                 bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
1637         else if (cmdcode == SLI_CTNS_DA_ID)
1638                 bpl->tus.f.bdeSize = DA_ID_REQUEST_SZ;
1639         else if (cmdcode == SLI_CTNS_RFF_ID)
1640                 bpl->tus.f.bdeSize = RFF_REQUEST_SZ;
1641         else
1642                 bpl->tus.f.bdeSize = 0;
1643         bpl->tus.w = le32_to_cpu(bpl->tus.w);
1644 
1645         CtReq = (struct lpfc_sli_ct_request *) mp->virt;
1646         memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
1647         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
1648         CtReq->RevisionId.bits.InId = 0;
1649         CtReq->FsType = SLI_CT_DIRECTORY_SERVICE;
1650         CtReq->FsSubType = SLI_CT_DIRECTORY_NAME_SERVER;
1651         CtReq->CommandResponse.bits.Size = 0;
1652         switch (cmdcode) {
1653         case SLI_CTNS_GID_FT:
1654                 CtReq->CommandResponse.bits.CmdRsp =
1655                     cpu_to_be16(SLI_CTNS_GID_FT);
1656                 CtReq->un.gid.Fc4Type = context;
1657 
1658                 if (vport->port_state < LPFC_NS_QRY)
1659                         vport->port_state = LPFC_NS_QRY;
1660                 lpfc_set_disctmo(vport);
1661                 cmpl = lpfc_cmpl_ct_cmd_gid_ft;
1662                 rsp_size = FC_MAX_NS_RSP;
1663                 break;
1664 
1665         case SLI_CTNS_GID_PT:
1666                 CtReq->CommandResponse.bits.CmdRsp =
1667                     cpu_to_be16(SLI_CTNS_GID_PT);
1668                 CtReq->un.gid.PortType = context;
1669 
1670                 if (vport->port_state < LPFC_NS_QRY)
1671                         vport->port_state = LPFC_NS_QRY;
1672                 lpfc_set_disctmo(vport);
1673                 cmpl = lpfc_cmpl_ct_cmd_gid_pt;
1674                 rsp_size = FC_MAX_NS_RSP;
1675                 break;
1676 
1677         case SLI_CTNS_GFF_ID:
1678                 CtReq->CommandResponse.bits.CmdRsp =
1679                         cpu_to_be16(SLI_CTNS_GFF_ID);
1680                 CtReq->un.gff.PortId = cpu_to_be32(context);
1681                 cmpl = lpfc_cmpl_ct_cmd_gff_id;
1682                 break;
1683 
1684         case SLI_CTNS_GFT_ID:
1685                 CtReq->CommandResponse.bits.CmdRsp =
1686                         cpu_to_be16(SLI_CTNS_GFT_ID);
1687                 CtReq->un.gft.PortId = cpu_to_be32(context);
1688                 cmpl = lpfc_cmpl_ct_cmd_gft_id;
1689                 break;
1690 
1691         case SLI_CTNS_RFT_ID:
1692                 vport->ct_flags &= ~FC_CT_RFT_ID;
1693                 CtReq->CommandResponse.bits.CmdRsp =
1694                     cpu_to_be16(SLI_CTNS_RFT_ID);
1695                 CtReq->un.rft.PortId = cpu_to_be32(vport->fc_myDID);
1696 
1697                 /* Register FC4 FCP type if enabled.  */
1698                 if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
1699                     vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)
1700                         CtReq->un.rft.fcpReg = 1;
1701 
1702                 /* Register NVME type if enabled.  Defined LE and swapped.
1703                  * rsvd[0] is used as word1 because of the hard-coded
1704                  * word0 usage in the ct_request data structure.
1705                  */
1706                 if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
1707                     vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
1708                         CtReq->un.rft.rsvd[0] =
1709                                 cpu_to_be32(LPFC_FC4_TYPE_BITMASK);
1710 
1711                 ptr = (uint32_t *)CtReq;
1712                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1713                                  "6433 Issue RFT (%s %s): %08x %08x %08x %08x "
1714                                  "%08x %08x %08x %08x\n",
1715                                  CtReq->un.rft.fcpReg ? "FCP" : " ",
1716                                  CtReq->un.rft.rsvd[0] ? "NVME" : " ",
1717                                  *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
1718                                  *(ptr + 4), *(ptr + 5),
1719                                  *(ptr + 6), *(ptr + 7));
1720                 cmpl = lpfc_cmpl_ct_cmd_rft_id;
1721                 break;
1722 
1723         case SLI_CTNS_RNN_ID:
1724                 vport->ct_flags &= ~FC_CT_RNN_ID;
1725                 CtReq->CommandResponse.bits.CmdRsp =
1726                     cpu_to_be16(SLI_CTNS_RNN_ID);
1727                 CtReq->un.rnn.PortId = cpu_to_be32(vport->fc_myDID);
1728                 memcpy(CtReq->un.rnn.wwnn,  &vport->fc_nodename,
1729                        sizeof(struct lpfc_name));
1730                 cmpl = lpfc_cmpl_ct_cmd_rnn_id;
1731                 break;
1732 
1733         case SLI_CTNS_RSPN_ID:
1734                 vport->ct_flags &= ~FC_CT_RSPN_ID;
1735                 CtReq->CommandResponse.bits.CmdRsp =
1736                     cpu_to_be16(SLI_CTNS_RSPN_ID);
1737                 CtReq->un.rspn.PortId = cpu_to_be32(vport->fc_myDID);
1738                 size = sizeof(CtReq->un.rspn.symbname);
1739                 CtReq->un.rspn.len =
1740                         lpfc_vport_symbolic_port_name(vport,
1741                         CtReq->un.rspn.symbname, size);
1742                 cmpl = lpfc_cmpl_ct_cmd_rspn_id;
1743                 break;
1744         case SLI_CTNS_RSNN_NN:
1745                 vport->ct_flags &= ~FC_CT_RSNN_NN;
1746                 CtReq->CommandResponse.bits.CmdRsp =
1747                     cpu_to_be16(SLI_CTNS_RSNN_NN);
1748                 memcpy(CtReq->un.rsnn.wwnn, &vport->fc_nodename,
1749                        sizeof(struct lpfc_name));
1750                 size = sizeof(CtReq->un.rsnn.symbname);
1751                 CtReq->un.rsnn.len =
1752                         lpfc_vport_symbolic_node_name(vport,
1753                         CtReq->un.rsnn.symbname, size);
1754                 cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
1755                 break;
1756         case SLI_CTNS_DA_ID:
1757                 /* Implement DA_ID Nameserver request */
1758                 CtReq->CommandResponse.bits.CmdRsp =
1759                         cpu_to_be16(SLI_CTNS_DA_ID);
1760                 CtReq->un.da_id.port_id = cpu_to_be32(vport->fc_myDID);
1761                 cmpl = lpfc_cmpl_ct_cmd_da_id;
1762                 break;
1763         case SLI_CTNS_RFF_ID:
1764                 vport->ct_flags &= ~FC_CT_RFF_ID;
1765                 CtReq->CommandResponse.bits.CmdRsp =
1766                     cpu_to_be16(SLI_CTNS_RFF_ID);
1767                 CtReq->un.rff.PortId = cpu_to_be32(vport->fc_myDID);
1768                 CtReq->un.rff.fbits = FC4_FEATURE_INIT;
1769 
1770                 /* The driver always supports FC_TYPE_FCP.  However, the
1771                  * caller can specify NVME (type x28) as well.  But only
1772                  * these that FC4 type is supported.
1773                  */
1774                 if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1775                      (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
1776                     (context == FC_TYPE_NVME)) {
1777                         if ((vport == phba->pport) && phba->nvmet_support) {
1778                                 CtReq->un.rff.fbits = (FC4_FEATURE_TARGET |
1779                                         FC4_FEATURE_NVME_DISC);
1780                                 lpfc_nvmet_update_targetport(phba);
1781                         } else {
1782                                 lpfc_nvme_update_localport(vport);
1783                         }
1784                         CtReq->un.rff.type_code = context;
1785 
1786                 } else if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1787                             (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) &&
1788                            (context == FC_TYPE_FCP))
1789                         CtReq->un.rff.type_code = context;
1790 
1791                 else
1792                         goto ns_cmd_free_bmpvirt;
1793 
1794                 ptr = (uint32_t *)CtReq;
1795                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1796                                  "6434 Issue RFF (%s): %08x %08x %08x %08x "
1797                                  "%08x %08x %08x %08x\n",
1798                                  (context == FC_TYPE_NVME) ? "NVME" : "FCP",
1799                                  *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
1800                                  *(ptr + 4), *(ptr + 5),
1801                                  *(ptr + 6), *(ptr + 7));
1802                 cmpl = lpfc_cmpl_ct_cmd_rff_id;
1803                 break;
1804         }
1805         /* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
1806          * to hold ndlp reference for the corresponding callback function.
1807          */
1808         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry)) {
1809                 /* On success, The cmpl function will free the buffers */
1810                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1811                         "Issue CT cmd:    cmd:x%x did:x%x",
1812                         cmdcode, ndlp->nlp_DID, 0);
1813                 return 0;
1814         }
1815         rc=6;
1816 
1817         /* Decrement ndlp reference count to release ndlp reference held
1818          * for the failed command's callback function.
1819          */
1820         lpfc_nlp_put(ndlp);
1821 
1822 ns_cmd_free_bmpvirt:
1823         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1824 ns_cmd_free_bmp:
1825         kfree(bmp);
1826 ns_cmd_free_mpvirt:
1827         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1828 ns_cmd_free_mp:
1829         kfree(mp);
1830 ns_cmd_exit:
1831         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1832                          "0266 Issue NameServer Req x%x err %d Data: x%x x%x\n",
1833                          cmdcode, rc, vport->fc_flag, vport->fc_rscn_id_cnt);
1834         return 1;
1835 }
1836 
1837 /**
1838  * lpfc_cmpl_ct_disc_fdmi - Handle a discovery FDMI completion
1839  * @phba: Pointer to HBA context object.
1840  * @cmdiocb: Pointer to the command IOCBQ.
1841  * @rspiocb: Pointer to the response IOCBQ.
1842  *
1843  * This function to handle the completion of a driver initiated FDMI
1844  * CT command issued during discovery.
1845  */
1846 static void
1847 lpfc_cmpl_ct_disc_fdmi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1848                        struct lpfc_iocbq *rspiocb)
1849 {
1850         struct lpfc_vport *vport = cmdiocb->vport;
1851         struct lpfc_dmabuf *inp = cmdiocb->context1;
1852         struct lpfc_dmabuf *outp = cmdiocb->context2;
1853         struct lpfc_sli_ct_request *CTcmd = inp->virt;
1854         struct lpfc_sli_ct_request *CTrsp = outp->virt;
1855         uint16_t fdmi_cmd = CTcmd->CommandResponse.bits.CmdRsp;
1856         uint16_t fdmi_rsp = CTrsp->CommandResponse.bits.CmdRsp;
1857         IOCB_t *irsp = &rspiocb->iocb;
1858         struct lpfc_nodelist *ndlp;
1859         uint32_t latt, cmd, err;
1860 
1861         latt = lpfc_els_chk_latt(vport);
1862         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1863                 "FDMI cmpl:       status:x%x/x%x latt:%d",
1864                 irsp->ulpStatus, irsp->un.ulpWord[4], latt);
1865 
1866         if (latt || irsp->ulpStatus) {
1867 
1868                 /* Look for a retryable error */
1869                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1870                         switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
1871                         case IOERR_SLI_ABORTED:
1872                         case IOERR_SLI_DOWN:
1873                                 /* Driver aborted this IO.  No retry as error
1874                                  * is likely Offline->Online or some adapter
1875                                  * error.  Recovery will try again.
1876                                  */
1877                                 break;
1878                         case IOERR_ABORT_IN_PROGRESS:
1879                         case IOERR_SEQUENCE_TIMEOUT:
1880                         case IOERR_ILLEGAL_FRAME:
1881                         case IOERR_NO_RESOURCES:
1882                         case IOERR_ILLEGAL_COMMAND:
1883                                 cmdiocb->retry++;
1884                                 if (cmdiocb->retry >= LPFC_FDMI_MAX_RETRY)
1885                                         break;
1886 
1887                                 /* Retry the same FDMI command */
1888                                 err = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING,
1889                                                           cmdiocb, 0);
1890                                 if (err == IOCB_ERROR)
1891                                         break;
1892                                 return;
1893                         default:
1894                                 break;
1895                         }
1896                 }
1897 
1898                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1899                                  "0229 FDMI cmd %04x failed, latt = %d "
1900                                  "ulpStatus: x%x, rid x%x\n",
1901                                  be16_to_cpu(fdmi_cmd), latt, irsp->ulpStatus,
1902                                  irsp->un.ulpWord[4]);
1903         }
1904         lpfc_ct_free_iocb(phba, cmdiocb);
1905 
1906         ndlp = lpfc_findnode_did(vport, FDMI_DID);
1907         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
1908                 return;
1909 
1910         /* Check for a CT LS_RJT response */
1911         cmd =  be16_to_cpu(fdmi_cmd);
1912         if (fdmi_rsp == cpu_to_be16(SLI_CT_RESPONSE_FS_RJT)) {
1913                 /* FDMI rsp failed */
1914                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1915                                  "0220 FDMI cmd failed FS_RJT Data: x%x", cmd);
1916 
1917                 /* Should we fallback to FDMI-2 / FDMI-1 ? */
1918                 switch (cmd) {
1919                 case SLI_MGMT_RHBA:
1920                         if (vport->fdmi_hba_mask == LPFC_FDMI2_HBA_ATTR) {
1921                                 /* Fallback to FDMI-1 */
1922                                 vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
1923                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
1924                                 /* Start over */
1925                                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
1926                         }
1927                         return;
1928 
1929                 case SLI_MGMT_RPRT:
1930                         if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
1931                                 /* Fallback to FDMI-1 */
1932                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
1933                                 /* Start over */
1934                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
1935                         }
1936                         if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
1937                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
1938                                 /* Retry the same command */
1939                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
1940                         }
1941                         return;
1942 
1943                 case SLI_MGMT_RPA:
1944                         if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
1945                                 /* Fallback to FDMI-1 */
1946                                 vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
1947                                 vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
1948                                 /* Start over */
1949                                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
1950                         }
1951                         if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
1952                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
1953                                 /* Retry the same command */
1954                                 lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
1955                         }
1956                         return;
1957                 }
1958         }
1959 
1960         /*
1961          * On success, need to cycle thru FDMI registration for discovery
1962          * DHBA -> DPRT -> RHBA -> RPA  (physical port)
1963          * DPRT -> RPRT (vports)
1964          */
1965         switch (cmd) {
1966         case SLI_MGMT_RHBA:
1967                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA, 0);
1968                 break;
1969 
1970         case SLI_MGMT_DHBA:
1971                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
1972                 break;
1973 
1974         case SLI_MGMT_DPRT:
1975                 if (vport->port_type == LPFC_PHYSICAL_PORT)
1976                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RHBA, 0);
1977                 else
1978                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT, 0);
1979                 break;
1980         }
1981         return;
1982 }
1983 
1984 
1985 /**
1986  * lpfc_fdmi_change_check - Check for changed FDMI parameters
1987  * @vport: pointer to a host virtual N_Port data structure.
1988  *
1989  * Check how many mapped NPorts we are connected to
1990  * Check if our hostname changed
1991  * Called from hbeat timeout routine to check if any FDMI parameters
1992  * changed. If so, re-register those Attributes.
1993  */
1994 void
1995 lpfc_fdmi_change_check(struct lpfc_vport *vport)
1996 {
1997         struct lpfc_hba *phba = vport->phba;
1998         struct lpfc_nodelist *ndlp;
1999         uint16_t cnt;
2000 
2001         if (!lpfc_is_link_up(phba))
2002                 return;
2003 
2004         /* Must be connected to a Fabric */
2005         if (!(vport->fc_flag & FC_FABRIC))
2006                 return;
2007 
2008         ndlp = lpfc_findnode_did(vport, FDMI_DID);
2009         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2010                 return;
2011 
2012         /* Check if system hostname changed */
2013         if (strcmp(phba->os_host_name, init_utsname()->nodename)) {
2014                 memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
2015                 scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
2016                           init_utsname()->nodename);
2017                 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2018 
2019                 /* Since this effects multiple HBA and PORT attributes, we need
2020                  * de-register and go thru the whole FDMI registration cycle.
2021                  * DHBA -> DPRT -> RHBA -> RPA  (physical port)
2022                  * DPRT -> RPRT (vports)
2023                  */
2024                 if (vport->port_type == LPFC_PHYSICAL_PORT)
2025                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2026                 else
2027                         lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
2028 
2029                 /* Since this code path registers all the port attributes
2030                  * we can just return without further checking.
2031                  */
2032                 return;
2033         }
2034 
2035         if (!(vport->fdmi_port_mask & LPFC_FDMI_PORT_ATTR_num_disc))
2036                 return;
2037 
2038         /* Check if the number of mapped NPorts changed */
2039         cnt = lpfc_find_map_node(vport);
2040         if (cnt == vport->fdmi_num_disc)
2041                 return;
2042 
2043         if (vport->port_type == LPFC_PHYSICAL_PORT) {
2044                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA,
2045                               LPFC_FDMI_PORT_ATTR_num_disc);
2046         } else {
2047                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT,
2048                               LPFC_FDMI_PORT_ATTR_num_disc);
2049         }
2050 }
2051 
2052 /* Routines for all individual HBA attributes */
2053 static int
2054 lpfc_fdmi_hba_attr_wwnn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2055 {
2056         struct lpfc_fdmi_attr_entry *ae;
2057         uint32_t size;
2058 
2059         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2060         memset(ae, 0, sizeof(struct lpfc_name));
2061 
2062         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
2063                sizeof(struct lpfc_name));
2064         size = FOURBYTES + sizeof(struct lpfc_name);
2065         ad->AttrLen = cpu_to_be16(size);
2066         ad->AttrType = cpu_to_be16(RHBA_NODENAME);
2067         return size;
2068 }
2069 static int
2070 lpfc_fdmi_hba_attr_manufacturer(struct lpfc_vport *vport,
2071                                 struct lpfc_fdmi_attr_def *ad)
2072 {
2073         struct lpfc_fdmi_attr_entry *ae;
2074         uint32_t len, size;
2075 
2076         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2077         memset(ae, 0, 256);
2078 
2079         /* This string MUST be consistent with other FC platforms
2080          * supported by Broadcom.
2081          */
2082         strncpy(ae->un.AttrString,
2083                 "Emulex Corporation",
2084                        sizeof(ae->un.AttrString));
2085         len = strnlen(ae->un.AttrString,
2086                           sizeof(ae->un.AttrString));
2087         len += (len & 3) ? (4 - (len & 3)) : 4;
2088         size = FOURBYTES + len;
2089         ad->AttrLen = cpu_to_be16(size);
2090         ad->AttrType = cpu_to_be16(RHBA_MANUFACTURER);
2091         return size;
2092 }
2093 
2094 static int
2095 lpfc_fdmi_hba_attr_sn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2096 {
2097         struct lpfc_hba *phba = vport->phba;
2098         struct lpfc_fdmi_attr_entry *ae;
2099         uint32_t len, size;
2100 
2101         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2102         memset(ae, 0, 256);
2103 
2104         strncpy(ae->un.AttrString, phba->SerialNumber,
2105                 sizeof(ae->un.AttrString));
2106         len = strnlen(ae->un.AttrString,
2107                           sizeof(ae->un.AttrString));
2108         len += (len & 3) ? (4 - (len & 3)) : 4;
2109         size = FOURBYTES + len;
2110         ad->AttrLen = cpu_to_be16(size);
2111         ad->AttrType = cpu_to_be16(RHBA_SERIAL_NUMBER);
2112         return size;
2113 }
2114 
2115 static int
2116 lpfc_fdmi_hba_attr_model(struct lpfc_vport *vport,
2117                          struct lpfc_fdmi_attr_def *ad)
2118 {
2119         struct lpfc_hba *phba = vport->phba;
2120         struct lpfc_fdmi_attr_entry *ae;
2121         uint32_t len, size;
2122 
2123         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2124         memset(ae, 0, 256);
2125 
2126         strncpy(ae->un.AttrString, phba->ModelName,
2127                 sizeof(ae->un.AttrString));
2128         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2129         len += (len & 3) ? (4 - (len & 3)) : 4;
2130         size = FOURBYTES + len;
2131         ad->AttrLen = cpu_to_be16(size);
2132         ad->AttrType = cpu_to_be16(RHBA_MODEL);
2133         return size;
2134 }
2135 
2136 static int
2137 lpfc_fdmi_hba_attr_description(struct lpfc_vport *vport,
2138                                struct lpfc_fdmi_attr_def *ad)
2139 {
2140         struct lpfc_hba *phba = vport->phba;
2141         struct lpfc_fdmi_attr_entry *ae;
2142         uint32_t len, size;
2143 
2144         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2145         memset(ae, 0, 256);
2146 
2147         strncpy(ae->un.AttrString, phba->ModelDesc,
2148                 sizeof(ae->un.AttrString));
2149         len = strnlen(ae->un.AttrString,
2150                                   sizeof(ae->un.AttrString));
2151         len += (len & 3) ? (4 - (len & 3)) : 4;
2152         size = FOURBYTES + len;
2153         ad->AttrLen = cpu_to_be16(size);
2154         ad->AttrType = cpu_to_be16(RHBA_MODEL_DESCRIPTION);
2155         return size;
2156 }
2157 
2158 static int
2159 lpfc_fdmi_hba_attr_hdw_ver(struct lpfc_vport *vport,
2160                            struct lpfc_fdmi_attr_def *ad)
2161 {
2162         struct lpfc_hba *phba = vport->phba;
2163         lpfc_vpd_t *vp = &phba->vpd;
2164         struct lpfc_fdmi_attr_entry *ae;
2165         uint32_t i, j, incr, size;
2166 
2167         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2168         memset(ae, 0, 256);
2169 
2170         /* Convert JEDEC ID to ascii for hardware version */
2171         incr = vp->rev.biuRev;
2172         for (i = 0; i < 8; i++) {
2173                 j = (incr & 0xf);
2174                 if (j <= 9)
2175                         ae->un.AttrString[7 - i] =
2176                             (char)((uint8_t) 0x30 +
2177                                    (uint8_t) j);
2178                 else
2179                         ae->un.AttrString[7 - i] =
2180                             (char)((uint8_t) 0x61 +
2181                                    (uint8_t) (j - 10));
2182                 incr = (incr >> 4);
2183         }
2184         size = FOURBYTES + 8;
2185         ad->AttrLen = cpu_to_be16(size);
2186         ad->AttrType = cpu_to_be16(RHBA_HARDWARE_VERSION);
2187         return size;
2188 }
2189 
2190 static int
2191 lpfc_fdmi_hba_attr_drvr_ver(struct lpfc_vport *vport,
2192                             struct lpfc_fdmi_attr_def *ad)
2193 {
2194         struct lpfc_fdmi_attr_entry *ae;
2195         uint32_t len, size;
2196 
2197         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2198         memset(ae, 0, 256);
2199 
2200         strncpy(ae->un.AttrString, lpfc_release_version,
2201                 sizeof(ae->un.AttrString));
2202         len = strnlen(ae->un.AttrString,
2203                           sizeof(ae->un.AttrString));
2204         len += (len & 3) ? (4 - (len & 3)) : 4;
2205         size = FOURBYTES + len;
2206         ad->AttrLen = cpu_to_be16(size);
2207         ad->AttrType = cpu_to_be16(RHBA_DRIVER_VERSION);
2208         return size;
2209 }
2210 
2211 static int
2212 lpfc_fdmi_hba_attr_rom_ver(struct lpfc_vport *vport,
2213                            struct lpfc_fdmi_attr_def *ad)
2214 {
2215         struct lpfc_hba *phba = vport->phba;
2216         struct lpfc_fdmi_attr_entry *ae;
2217         uint32_t len, size;
2218 
2219         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2220         memset(ae, 0, 256);
2221 
2222         if (phba->sli_rev == LPFC_SLI_REV4)
2223                 lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2224         else
2225                 strncpy(ae->un.AttrString, phba->OptionROMVersion,
2226                         sizeof(ae->un.AttrString));
2227         len = strnlen(ae->un.AttrString,
2228                           sizeof(ae->un.AttrString));
2229         len += (len & 3) ? (4 - (len & 3)) : 4;
2230         size = FOURBYTES + len;
2231         ad->AttrLen = cpu_to_be16(size);
2232         ad->AttrType = cpu_to_be16(RHBA_OPTION_ROM_VERSION);
2233         return size;
2234 }
2235 
2236 static int
2237 lpfc_fdmi_hba_attr_fmw_ver(struct lpfc_vport *vport,
2238                            struct lpfc_fdmi_attr_def *ad)
2239 {
2240         struct lpfc_hba *phba = vport->phba;
2241         struct lpfc_fdmi_attr_entry *ae;
2242         uint32_t len, size;
2243 
2244         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2245         memset(ae, 0, 256);
2246 
2247         lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2248         len = strnlen(ae->un.AttrString,
2249                           sizeof(ae->un.AttrString));
2250         len += (len & 3) ? (4 - (len & 3)) : 4;
2251         size = FOURBYTES + len;
2252         ad->AttrLen = cpu_to_be16(size);
2253         ad->AttrType = cpu_to_be16(RHBA_FIRMWARE_VERSION);
2254         return size;
2255 }
2256 
2257 static int
2258 lpfc_fdmi_hba_attr_os_ver(struct lpfc_vport *vport,
2259                           struct lpfc_fdmi_attr_def *ad)
2260 {
2261         struct lpfc_fdmi_attr_entry *ae;
2262         uint32_t len, size;
2263 
2264         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2265         memset(ae, 0, 256);
2266 
2267         snprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s %s %s",
2268                  init_utsname()->sysname,
2269                  init_utsname()->release,
2270                  init_utsname()->version);
2271 
2272         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2273         len += (len & 3) ? (4 - (len & 3)) : 4;
2274         size = FOURBYTES + len;
2275         ad->AttrLen = cpu_to_be16(size);
2276         ad->AttrType = cpu_to_be16(RHBA_OS_NAME_VERSION);
2277         return size;
2278 }
2279 
2280 static int
2281 lpfc_fdmi_hba_attr_ct_len(struct lpfc_vport *vport,
2282                           struct lpfc_fdmi_attr_def *ad)
2283 {
2284         struct lpfc_fdmi_attr_entry *ae;
2285         uint32_t size;
2286 
2287         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2288 
2289         ae->un.AttrInt =  cpu_to_be32(LPFC_MAX_CT_SIZE);
2290         size = FOURBYTES + sizeof(uint32_t);
2291         ad->AttrLen = cpu_to_be16(size);
2292         ad->AttrType = cpu_to_be16(RHBA_MAX_CT_PAYLOAD_LEN);
2293         return size;
2294 }
2295 
2296 static int
2297 lpfc_fdmi_hba_attr_symbolic_name(struct lpfc_vport *vport,
2298                                  struct lpfc_fdmi_attr_def *ad)
2299 {
2300         struct lpfc_fdmi_attr_entry *ae;
2301         uint32_t len, size;
2302 
2303         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2304         memset(ae, 0, 256);
2305 
2306         len = lpfc_vport_symbolic_node_name(vport,
2307                                 ae->un.AttrString, 256);
2308         len += (len & 3) ? (4 - (len & 3)) : 4;
2309         size = FOURBYTES + len;
2310         ad->AttrLen = cpu_to_be16(size);
2311         ad->AttrType = cpu_to_be16(RHBA_SYM_NODENAME);
2312         return size;
2313 }
2314 
2315 static int
2316 lpfc_fdmi_hba_attr_vendor_info(struct lpfc_vport *vport,
2317                                struct lpfc_fdmi_attr_def *ad)
2318 {
2319         struct lpfc_fdmi_attr_entry *ae;
2320         uint32_t size;
2321 
2322         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2323 
2324         /* Nothing is defined for this currently */
2325         ae->un.AttrInt =  cpu_to_be32(0);
2326         size = FOURBYTES + sizeof(uint32_t);
2327         ad->AttrLen = cpu_to_be16(size);
2328         ad->AttrType = cpu_to_be16(RHBA_VENDOR_INFO);
2329         return size;
2330 }
2331 
2332 static int
2333 lpfc_fdmi_hba_attr_num_ports(struct lpfc_vport *vport,
2334                              struct lpfc_fdmi_attr_def *ad)
2335 {
2336         struct lpfc_fdmi_attr_entry *ae;
2337         uint32_t size;
2338 
2339         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2340 
2341         /* Each driver instance corresponds to a single port */
2342         ae->un.AttrInt =  cpu_to_be32(1);
2343         size = FOURBYTES + sizeof(uint32_t);
2344         ad->AttrLen = cpu_to_be16(size);
2345         ad->AttrType = cpu_to_be16(RHBA_NUM_PORTS);
2346         return size;
2347 }
2348 
2349 static int
2350 lpfc_fdmi_hba_attr_fabric_wwnn(struct lpfc_vport *vport,
2351                                struct lpfc_fdmi_attr_def *ad)
2352 {
2353         struct lpfc_fdmi_attr_entry *ae;
2354         uint32_t size;
2355 
2356         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2357         memset(ae, 0, sizeof(struct lpfc_name));
2358 
2359         memcpy(&ae->un.AttrWWN, &vport->fabric_nodename,
2360                sizeof(struct lpfc_name));
2361         size = FOURBYTES + sizeof(struct lpfc_name);
2362         ad->AttrLen = cpu_to_be16(size);
2363         ad->AttrType = cpu_to_be16(RHBA_FABRIC_WWNN);
2364         return size;
2365 }
2366 
2367 static int
2368 lpfc_fdmi_hba_attr_bios_ver(struct lpfc_vport *vport,
2369                             struct lpfc_fdmi_attr_def *ad)
2370 {
2371         struct lpfc_hba *phba = vport->phba;
2372         struct lpfc_fdmi_attr_entry *ae;
2373         uint32_t len, size;
2374 
2375         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2376         memset(ae, 0, 256);
2377 
2378         strlcat(ae->un.AttrString, phba->BIOSVersion,
2379                 sizeof(ae->un.AttrString));
2380         len = strnlen(ae->un.AttrString,
2381                           sizeof(ae->un.AttrString));
2382         len += (len & 3) ? (4 - (len & 3)) : 4;
2383         size = FOURBYTES + len;
2384         ad->AttrLen = cpu_to_be16(size);
2385         ad->AttrType = cpu_to_be16(RHBA_BIOS_VERSION);
2386         return size;
2387 }
2388 
2389 static int
2390 lpfc_fdmi_hba_attr_bios_state(struct lpfc_vport *vport,
2391                               struct lpfc_fdmi_attr_def *ad)
2392 {
2393         struct lpfc_fdmi_attr_entry *ae;
2394         uint32_t size;
2395 
2396         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2397 
2398         /* Driver doesn't have access to this information */
2399         ae->un.AttrInt =  cpu_to_be32(0);
2400         size = FOURBYTES + sizeof(uint32_t);
2401         ad->AttrLen = cpu_to_be16(size);
2402         ad->AttrType = cpu_to_be16(RHBA_BIOS_STATE);
2403         return size;
2404 }
2405 
2406 static int
2407 lpfc_fdmi_hba_attr_vendor_id(struct lpfc_vport *vport,
2408                              struct lpfc_fdmi_attr_def *ad)
2409 {
2410         struct lpfc_fdmi_attr_entry *ae;
2411         uint32_t len, size;
2412 
2413         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2414         memset(ae, 0, 256);
2415 
2416         strncpy(ae->un.AttrString, "EMULEX",
2417                 sizeof(ae->un.AttrString));
2418         len = strnlen(ae->un.AttrString,
2419                           sizeof(ae->un.AttrString));
2420         len += (len & 3) ? (4 - (len & 3)) : 4;
2421         size = FOURBYTES + len;
2422         ad->AttrLen = cpu_to_be16(size);
2423         ad->AttrType = cpu_to_be16(RHBA_VENDOR_ID);
2424         return size;
2425 }
2426 
2427 /* Routines for all individual PORT attributes */
2428 static int
2429 lpfc_fdmi_port_attr_fc4type(struct lpfc_vport *vport,
2430                             struct lpfc_fdmi_attr_def *ad)
2431 {
2432         struct lpfc_hba   *phba = vport->phba;
2433         struct lpfc_fdmi_attr_entry *ae;
2434         uint32_t size;
2435 
2436         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2437         memset(ae, 0, 32);
2438 
2439         ae->un.AttrTypes[3] = 0x02; /* Type 0x1 - ELS */
2440         ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
2441         ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
2442 
2443         /* Check to see if Firmware supports NVME and on physical port */
2444         if ((phba->sli_rev == LPFC_SLI_REV4) && (vport == phba->pport) &&
2445             phba->sli4_hba.pc_sli4_params.nvme)
2446                 ae->un.AttrTypes[6] = 0x01; /* Type 0x28 - NVME */
2447 
2448         size = FOURBYTES + 32;
2449         ad->AttrLen = cpu_to_be16(size);
2450         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_FC4_TYPES);
2451         return size;
2452 }
2453 
2454 static int
2455 lpfc_fdmi_port_attr_support_speed(struct lpfc_vport *vport,
2456                                   struct lpfc_fdmi_attr_def *ad)
2457 {
2458         struct lpfc_hba   *phba = vport->phba;
2459         struct lpfc_fdmi_attr_entry *ae;
2460         uint32_t size;
2461 
2462         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2463 
2464         ae->un.AttrInt = 0;
2465         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2466                 if (phba->lmt & LMT_128Gb)
2467                         ae->un.AttrInt |= HBA_PORTSPEED_128GFC;
2468                 if (phba->lmt & LMT_64Gb)
2469                         ae->un.AttrInt |= HBA_PORTSPEED_64GFC;
2470                 if (phba->lmt & LMT_32Gb)
2471                         ae->un.AttrInt |= HBA_PORTSPEED_32GFC;
2472                 if (phba->lmt & LMT_16Gb)
2473                         ae->un.AttrInt |= HBA_PORTSPEED_16GFC;
2474                 if (phba->lmt & LMT_10Gb)
2475                         ae->un.AttrInt |= HBA_PORTSPEED_10GFC;
2476                 if (phba->lmt & LMT_8Gb)
2477                         ae->un.AttrInt |= HBA_PORTSPEED_8GFC;
2478                 if (phba->lmt & LMT_4Gb)
2479                         ae->un.AttrInt |= HBA_PORTSPEED_4GFC;
2480                 if (phba->lmt & LMT_2Gb)
2481                         ae->un.AttrInt |= HBA_PORTSPEED_2GFC;
2482                 if (phba->lmt & LMT_1Gb)
2483                         ae->un.AttrInt |= HBA_PORTSPEED_1GFC;
2484         } else {
2485                 /* FCoE links support only one speed */
2486                 switch (phba->fc_linkspeed) {
2487                 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2488                         ae->un.AttrInt = HBA_PORTSPEED_10GE;
2489                         break;
2490                 case LPFC_ASYNC_LINK_SPEED_25GBPS:
2491                         ae->un.AttrInt = HBA_PORTSPEED_25GE;
2492                         break;
2493                 case LPFC_ASYNC_LINK_SPEED_40GBPS:
2494                         ae->un.AttrInt = HBA_PORTSPEED_40GE;
2495                         break;
2496                 case LPFC_ASYNC_LINK_SPEED_100GBPS:
2497                         ae->un.AttrInt = HBA_PORTSPEED_100GE;
2498                         break;
2499                 }
2500         }
2501         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2502         size = FOURBYTES + sizeof(uint32_t);
2503         ad->AttrLen = cpu_to_be16(size);
2504         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_SPEED);
2505         return size;
2506 }
2507 
2508 static int
2509 lpfc_fdmi_port_attr_speed(struct lpfc_vport *vport,
2510                           struct lpfc_fdmi_attr_def *ad)
2511 {
2512         struct lpfc_hba   *phba = vport->phba;
2513         struct lpfc_fdmi_attr_entry *ae;
2514         uint32_t size;
2515 
2516         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2517 
2518         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2519                 switch (phba->fc_linkspeed) {
2520                 case LPFC_LINK_SPEED_1GHZ:
2521                         ae->un.AttrInt = HBA_PORTSPEED_1GFC;
2522                         break;
2523                 case LPFC_LINK_SPEED_2GHZ:
2524                         ae->un.AttrInt = HBA_PORTSPEED_2GFC;
2525                         break;
2526                 case LPFC_LINK_SPEED_4GHZ:
2527                         ae->un.AttrInt = HBA_PORTSPEED_4GFC;
2528                         break;
2529                 case LPFC_LINK_SPEED_8GHZ:
2530                         ae->un.AttrInt = HBA_PORTSPEED_8GFC;
2531                         break;
2532                 case LPFC_LINK_SPEED_10GHZ:
2533                         ae->un.AttrInt = HBA_PORTSPEED_10GFC;
2534                         break;
2535                 case LPFC_LINK_SPEED_16GHZ:
2536                         ae->un.AttrInt = HBA_PORTSPEED_16GFC;
2537                         break;
2538                 case LPFC_LINK_SPEED_32GHZ:
2539                         ae->un.AttrInt = HBA_PORTSPEED_32GFC;
2540                         break;
2541                 case LPFC_LINK_SPEED_64GHZ:
2542                         ae->un.AttrInt = HBA_PORTSPEED_64GFC;
2543                         break;
2544                 case LPFC_LINK_SPEED_128GHZ:
2545                         ae->un.AttrInt = HBA_PORTSPEED_128GFC;
2546                         break;
2547                 default:
2548                         ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2549                         break;
2550                 }
2551         } else {
2552                 switch (phba->fc_linkspeed) {
2553                 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2554                         ae->un.AttrInt = HBA_PORTSPEED_10GE;
2555                         break;
2556                 case LPFC_ASYNC_LINK_SPEED_25GBPS:
2557                         ae->un.AttrInt = HBA_PORTSPEED_25GE;
2558                         break;
2559                 case LPFC_ASYNC_LINK_SPEED_40GBPS:
2560                         ae->un.AttrInt = HBA_PORTSPEED_40GE;
2561                         break;
2562                 case LPFC_ASYNC_LINK_SPEED_100GBPS:
2563                         ae->un.AttrInt = HBA_PORTSPEED_100GE;
2564                         break;
2565                 default:
2566                         ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2567                         break;
2568                 }
2569         }
2570 
2571         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2572         size = FOURBYTES + sizeof(uint32_t);
2573         ad->AttrLen = cpu_to_be16(size);
2574         ad->AttrType = cpu_to_be16(RPRT_PORT_SPEED);
2575         return size;
2576 }
2577 
2578 static int
2579 lpfc_fdmi_port_attr_max_frame(struct lpfc_vport *vport,
2580                               struct lpfc_fdmi_attr_def *ad)
2581 {
2582         struct serv_parm *hsp;
2583         struct lpfc_fdmi_attr_entry *ae;
2584         uint32_t size;
2585 
2586         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2587 
2588         hsp = (struct serv_parm *)&vport->fc_sparam;
2589         ae->un.AttrInt = (((uint32_t) hsp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
2590                           (uint32_t) hsp->cmn.bbRcvSizeLsb;
2591         ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2592         size = FOURBYTES + sizeof(uint32_t);
2593         ad->AttrLen = cpu_to_be16(size);
2594         ad->AttrType = cpu_to_be16(RPRT_MAX_FRAME_SIZE);
2595         return size;
2596 }
2597 
2598 static int
2599 lpfc_fdmi_port_attr_os_devname(struct lpfc_vport *vport,
2600                                struct lpfc_fdmi_attr_def *ad)
2601 {
2602         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2603         struct lpfc_fdmi_attr_entry *ae;
2604         uint32_t len, size;
2605 
2606         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2607         memset(ae, 0, 256);
2608 
2609         snprintf(ae->un.AttrString, sizeof(ae->un.AttrString),
2610                  "/sys/class/scsi_host/host%d", shost->host_no);
2611         len = strnlen((char *)ae->un.AttrString,
2612                           sizeof(ae->un.AttrString));
2613         len += (len & 3) ? (4 - (len & 3)) : 4;
2614         size = FOURBYTES + len;
2615         ad->AttrLen = cpu_to_be16(size);
2616         ad->AttrType = cpu_to_be16(RPRT_OS_DEVICE_NAME);
2617         return size;
2618 }
2619 
2620 static int
2621 lpfc_fdmi_port_attr_host_name(struct lpfc_vport *vport,
2622                               struct lpfc_fdmi_attr_def *ad)
2623 {
2624         struct lpfc_fdmi_attr_entry *ae;
2625         uint32_t len, size;
2626 
2627         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2628         memset(ae, 0, 256);
2629 
2630         scnprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s",
2631                   vport->phba->os_host_name);
2632 
2633         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2634         len += (len & 3) ? (4 - (len & 3)) : 4;
2635         size = FOURBYTES + len;
2636         ad->AttrLen = cpu_to_be16(size);
2637         ad->AttrType = cpu_to_be16(RPRT_HOST_NAME);
2638         return size;
2639 }
2640 
2641 static int
2642 lpfc_fdmi_port_attr_wwnn(struct lpfc_vport *vport,
2643                          struct lpfc_fdmi_attr_def *ad)
2644 {
2645         struct lpfc_fdmi_attr_entry *ae;
2646         uint32_t size;
2647 
2648         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2649         memset(ae, 0,  sizeof(struct lpfc_name));
2650 
2651         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
2652                sizeof(struct lpfc_name));
2653         size = FOURBYTES + sizeof(struct lpfc_name);
2654         ad->AttrLen = cpu_to_be16(size);
2655         ad->AttrType = cpu_to_be16(RPRT_NODENAME);
2656         return size;
2657 }
2658 
2659 static int
2660 lpfc_fdmi_port_attr_wwpn(struct lpfc_vport *vport,
2661                          struct lpfc_fdmi_attr_def *ad)
2662 {
2663         struct lpfc_fdmi_attr_entry *ae;
2664         uint32_t size;
2665 
2666         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2667         memset(ae, 0,  sizeof(struct lpfc_name));
2668 
2669         memcpy(&ae->un.AttrWWN, &vport->fc_sparam.portName,
2670                sizeof(struct lpfc_name));
2671         size = FOURBYTES + sizeof(struct lpfc_name);
2672         ad->AttrLen = cpu_to_be16(size);
2673         ad->AttrType = cpu_to_be16(RPRT_PORTNAME);
2674         return size;
2675 }
2676 
2677 static int
2678 lpfc_fdmi_port_attr_symbolic_name(struct lpfc_vport *vport,
2679                                   struct lpfc_fdmi_attr_def *ad)
2680 {
2681         struct lpfc_fdmi_attr_entry *ae;
2682         uint32_t len, size;
2683 
2684         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2685         memset(ae, 0, 256);
2686 
2687         len = lpfc_vport_symbolic_port_name(vport, ae->un.AttrString, 256);
2688         len += (len & 3) ? (4 - (len & 3)) : 4;
2689         size = FOURBYTES + len;
2690         ad->AttrLen = cpu_to_be16(size);
2691         ad->AttrType = cpu_to_be16(RPRT_SYM_PORTNAME);
2692         return size;
2693 }
2694 
2695 static int
2696 lpfc_fdmi_port_attr_port_type(struct lpfc_vport *vport,
2697                               struct lpfc_fdmi_attr_def *ad)
2698 {
2699         struct lpfc_hba *phba = vport->phba;
2700         struct lpfc_fdmi_attr_entry *ae;
2701         uint32_t size;
2702 
2703         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2704         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP)
2705                 ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NLPORT);
2706         else
2707                 ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NPORT);
2708         size = FOURBYTES + sizeof(uint32_t);
2709         ad->AttrLen = cpu_to_be16(size);
2710         ad->AttrType = cpu_to_be16(RPRT_PORT_TYPE);
2711         return size;
2712 }
2713 
2714 static int
2715 lpfc_fdmi_port_attr_class(struct lpfc_vport *vport,
2716                           struct lpfc_fdmi_attr_def *ad)
2717 {
2718         struct lpfc_fdmi_attr_entry *ae;
2719         uint32_t size;
2720 
2721         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2722         ae->un.AttrInt = cpu_to_be32(FC_COS_CLASS2 | FC_COS_CLASS3);
2723         size = FOURBYTES + sizeof(uint32_t);
2724         ad->AttrLen = cpu_to_be16(size);
2725         ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_CLASS);
2726         return size;
2727 }
2728 
2729 static int
2730 lpfc_fdmi_port_attr_fabric_wwpn(struct lpfc_vport *vport,
2731                                 struct lpfc_fdmi_attr_def *ad)
2732 {
2733         struct lpfc_fdmi_attr_entry *ae;
2734         uint32_t size;
2735 
2736         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2737         memset(ae, 0,  sizeof(struct lpfc_name));
2738 
2739         memcpy(&ae->un.AttrWWN, &vport->fabric_portname,
2740                sizeof(struct lpfc_name));
2741         size = FOURBYTES + sizeof(struct lpfc_name);
2742         ad->AttrLen = cpu_to_be16(size);
2743         ad->AttrType = cpu_to_be16(RPRT_FABRICNAME);
2744         return size;
2745 }
2746 
2747 static int
2748 lpfc_fdmi_port_attr_active_fc4type(struct lpfc_vport *vport,
2749                                    struct lpfc_fdmi_attr_def *ad)
2750 {
2751         struct lpfc_fdmi_attr_entry *ae;
2752         uint32_t size;
2753 
2754         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2755         memset(ae, 0, 32);
2756 
2757         ae->un.AttrTypes[3] = 0x02; /* Type 0x1 - ELS */
2758         ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
2759         ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
2760 
2761         /* Check to see if NVME is configured or not */
2762         if (vport->phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
2763                 ae->un.AttrTypes[6] = 0x1; /* Type 0x28 - NVME */
2764 
2765         size = FOURBYTES + 32;
2766         ad->AttrLen = cpu_to_be16(size);
2767         ad->AttrType = cpu_to_be16(RPRT_ACTIVE_FC4_TYPES);
2768         return size;
2769 }
2770 
2771 static int
2772 lpfc_fdmi_port_attr_port_state(struct lpfc_vport *vport,
2773                                struct lpfc_fdmi_attr_def *ad)
2774 {
2775         struct lpfc_fdmi_attr_entry *ae;
2776         uint32_t size;
2777 
2778         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2779         /* Link Up - operational */
2780         ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTSTATE_ONLINE);
2781         size = FOURBYTES + sizeof(uint32_t);
2782         ad->AttrLen = cpu_to_be16(size);
2783         ad->AttrType = cpu_to_be16(RPRT_PORT_STATE);
2784         return size;
2785 }
2786 
2787 static int
2788 lpfc_fdmi_port_attr_num_disc(struct lpfc_vport *vport,
2789                              struct lpfc_fdmi_attr_def *ad)
2790 {
2791         struct lpfc_fdmi_attr_entry *ae;
2792         uint32_t size;
2793 
2794         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2795         vport->fdmi_num_disc = lpfc_find_map_node(vport);
2796         ae->un.AttrInt = cpu_to_be32(vport->fdmi_num_disc);
2797         size = FOURBYTES + sizeof(uint32_t);
2798         ad->AttrLen = cpu_to_be16(size);
2799         ad->AttrType = cpu_to_be16(RPRT_DISC_PORT);
2800         return size;
2801 }
2802 
2803 static int
2804 lpfc_fdmi_port_attr_nportid(struct lpfc_vport *vport,
2805                             struct lpfc_fdmi_attr_def *ad)
2806 {
2807         struct lpfc_fdmi_attr_entry *ae;
2808         uint32_t size;
2809 
2810         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2811         ae->un.AttrInt =  cpu_to_be32(vport->fc_myDID);
2812         size = FOURBYTES + sizeof(uint32_t);
2813         ad->AttrLen = cpu_to_be16(size);
2814         ad->AttrType = cpu_to_be16(RPRT_PORT_ID);
2815         return size;
2816 }
2817 
2818 static int
2819 lpfc_fdmi_smart_attr_service(struct lpfc_vport *vport,
2820                              struct lpfc_fdmi_attr_def *ad)
2821 {
2822         struct lpfc_fdmi_attr_entry *ae;
2823         uint32_t len, size;
2824 
2825         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2826         memset(ae, 0, 256);
2827 
2828         strncpy(ae->un.AttrString, "Smart SAN Initiator",
2829                 sizeof(ae->un.AttrString));
2830         len = strnlen(ae->un.AttrString,
2831                           sizeof(ae->un.AttrString));
2832         len += (len & 3) ? (4 - (len & 3)) : 4;
2833         size = FOURBYTES + len;
2834         ad->AttrLen = cpu_to_be16(size);
2835         ad->AttrType = cpu_to_be16(RPRT_SMART_SERVICE);
2836         return size;
2837 }
2838 
2839 static int
2840 lpfc_fdmi_smart_attr_guid(struct lpfc_vport *vport,
2841                           struct lpfc_fdmi_attr_def *ad)
2842 {
2843         struct lpfc_fdmi_attr_entry *ae;
2844         uint32_t size;
2845 
2846         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2847         memset(ae, 0, 256);
2848 
2849         memcpy(&ae->un.AttrString, &vport->fc_sparam.nodeName,
2850                sizeof(struct lpfc_name));
2851         memcpy((((uint8_t *)&ae->un.AttrString) +
2852                 sizeof(struct lpfc_name)),
2853                 &vport->fc_sparam.portName, sizeof(struct lpfc_name));
2854         size = FOURBYTES + (2 * sizeof(struct lpfc_name));
2855         ad->AttrLen =  cpu_to_be16(size);
2856         ad->AttrType = cpu_to_be16(RPRT_SMART_GUID);
2857         return size;
2858 }
2859 
2860 static int
2861 lpfc_fdmi_smart_attr_version(struct lpfc_vport *vport,
2862                              struct lpfc_fdmi_attr_def *ad)
2863 {
2864         struct lpfc_fdmi_attr_entry *ae;
2865         uint32_t len, size;
2866 
2867         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2868         memset(ae, 0, 256);
2869 
2870         strncpy(ae->un.AttrString, "Smart SAN Version 2.0",
2871                 sizeof(ae->un.AttrString));
2872         len = strnlen(ae->un.AttrString,
2873                           sizeof(ae->un.AttrString));
2874         len += (len & 3) ? (4 - (len & 3)) : 4;
2875         size = FOURBYTES + len;
2876         ad->AttrLen =  cpu_to_be16(size);
2877         ad->AttrType = cpu_to_be16(RPRT_SMART_VERSION);
2878         return size;
2879 }
2880 
2881 static int
2882 lpfc_fdmi_smart_attr_model(struct lpfc_vport *vport,
2883                            struct lpfc_fdmi_attr_def *ad)
2884 {
2885         struct lpfc_hba *phba = vport->phba;
2886         struct lpfc_fdmi_attr_entry *ae;
2887         uint32_t len, size;
2888 
2889         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2890         memset(ae, 0, 256);
2891 
2892         strncpy(ae->un.AttrString, phba->ModelName,
2893                 sizeof(ae->un.AttrString));
2894         len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2895         len += (len & 3) ? (4 - (len & 3)) : 4;
2896         size = FOURBYTES + len;
2897         ad->AttrLen = cpu_to_be16(size);
2898         ad->AttrType = cpu_to_be16(RPRT_SMART_MODEL);
2899         return size;
2900 }
2901 
2902 static int
2903 lpfc_fdmi_smart_attr_port_info(struct lpfc_vport *vport,
2904                                struct lpfc_fdmi_attr_def *ad)
2905 {
2906         struct lpfc_fdmi_attr_entry *ae;
2907         uint32_t size;
2908 
2909         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2910 
2911         /* SRIOV (type 3) is not supported */
2912         if (vport->vpi)
2913                 ae->un.AttrInt =  cpu_to_be32(2);  /* NPIV */
2914         else
2915                 ae->un.AttrInt =  cpu_to_be32(1);  /* Physical */
2916         size = FOURBYTES + sizeof(uint32_t);
2917         ad->AttrLen = cpu_to_be16(size);
2918         ad->AttrType = cpu_to_be16(RPRT_SMART_PORT_INFO);
2919         return size;
2920 }
2921 
2922 static int
2923 lpfc_fdmi_smart_attr_qos(struct lpfc_vport *vport,
2924                          struct lpfc_fdmi_attr_def *ad)
2925 {
2926         struct lpfc_fdmi_attr_entry *ae;
2927         uint32_t size;
2928 
2929         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2930         ae->un.AttrInt =  cpu_to_be32(0);
2931         size = FOURBYTES + sizeof(uint32_t);
2932         ad->AttrLen = cpu_to_be16(size);
2933         ad->AttrType = cpu_to_be16(RPRT_SMART_QOS);
2934         return size;
2935 }
2936 
2937 static int
2938 lpfc_fdmi_smart_attr_security(struct lpfc_vport *vport,
2939                               struct lpfc_fdmi_attr_def *ad)
2940 {
2941         struct lpfc_fdmi_attr_entry *ae;
2942         uint32_t size;
2943 
2944         ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
2945         ae->un.AttrInt =  cpu_to_be32(1);
2946         size = FOURBYTES + sizeof(uint32_t);
2947         ad->AttrLen = cpu_to_be16(size);
2948         ad->AttrType = cpu_to_be16(RPRT_SMART_SECURITY);
2949         return size;
2950 }
2951 
2952 /* RHBA attribute jump table */
2953 int (*lpfc_fdmi_hba_action[])
2954         (struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
2955         /* Action routine                 Mask bit     Attribute type */
2956         lpfc_fdmi_hba_attr_wwnn,          /* bit0     RHBA_NODENAME           */
2957         lpfc_fdmi_hba_attr_manufacturer,  /* bit1     RHBA_MANUFACTURER       */
2958         lpfc_fdmi_hba_attr_sn,            /* bit2     RHBA_SERIAL_NUMBER      */
2959         lpfc_fdmi_hba_attr_model,         /* bit3     RHBA_MODEL              */
2960         lpfc_fdmi_hba_attr_description,   /* bit4     RHBA_MODEL_DESCRIPTION  */
2961         lpfc_fdmi_hba_attr_hdw_ver,       /* bit5     RHBA_HARDWARE_VERSION   */
2962         lpfc_fdmi_hba_attr_drvr_ver,      /* bit6     RHBA_DRIVER_VERSION     */
2963         lpfc_fdmi_hba_attr_rom_ver,       /* bit7     RHBA_OPTION_ROM_VERSION */
2964         lpfc_fdmi_hba_attr_fmw_ver,       /* bit8     RHBA_FIRMWARE_VERSION   */
2965         lpfc_fdmi_hba_attr_os_ver,        /* bit9     RHBA_OS_NAME_VERSION    */
2966         lpfc_fdmi_hba_attr_ct_len,        /* bit10    RHBA_MAX_CT_PAYLOAD_LEN */
2967         lpfc_fdmi_hba_attr_symbolic_name, /* bit11    RHBA_SYM_NODENAME       */
2968         lpfc_fdmi_hba_attr_vendor_info,   /* bit12    RHBA_VENDOR_INFO        */
2969         lpfc_fdmi_hba_attr_num_ports,     /* bit13    RHBA_NUM_PORTS          */
2970         lpfc_fdmi_hba_attr_fabric_wwnn,   /* bit14    RHBA_FABRIC_WWNN        */
2971         lpfc_fdmi_hba_attr_bios_ver,      /* bit15    RHBA_BIOS_VERSION       */
2972         lpfc_fdmi_hba_attr_bios_state,    /* bit16    RHBA_BIOS_STATE         */
2973         lpfc_fdmi_hba_attr_vendor_id,     /* bit17    RHBA_VENDOR_ID          */
2974 };
2975 
2976 /* RPA / RPRT attribute jump table */
2977 int (*lpfc_fdmi_port_action[])
2978         (struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
2979         /* Action routine                   Mask bit   Attribute type */
2980         lpfc_fdmi_port_attr_fc4type,        /* bit0   RPRT_SUPPORT_FC4_TYPES  */
2981         lpfc_fdmi_port_attr_support_speed,  /* bit1   RPRT_SUPPORTED_SPEED    */
2982         lpfc_fdmi_port_attr_speed,          /* bit2   RPRT_PORT_SPEED         */
2983         lpfc_fdmi_port_attr_max_frame,      /* bit3   RPRT_MAX_FRAME_SIZE     */
2984         lpfc_fdmi_port_attr_os_devname,     /* bit4   RPRT_OS_DEVICE_NAME     */
2985         lpfc_fdmi_port_attr_host_name,      /* bit5   RPRT_HOST_NAME          */
2986         lpfc_fdmi_port_attr_wwnn,           /* bit6   RPRT_NODENAME           */
2987         lpfc_fdmi_port_attr_wwpn,           /* bit7   RPRT_PORTNAME           */
2988         lpfc_fdmi_port_attr_symbolic_name,  /* bit8   RPRT_SYM_PORTNAME       */
2989         lpfc_fdmi_port_attr_port_type,      /* bit9   RPRT_PORT_TYPE          */
2990         lpfc_fdmi_port_attr_class,          /* bit10  RPRT_SUPPORTED_CLASS    */
2991         lpfc_fdmi_port_attr_fabric_wwpn,    /* bit11  RPRT_FABRICNAME         */
2992         lpfc_fdmi_port_attr_active_fc4type, /* bit12  RPRT_ACTIVE_FC4_TYPES   */
2993         lpfc_fdmi_port_attr_port_state,     /* bit13  RPRT_PORT_STATE         */
2994         lpfc_fdmi_port_attr_num_disc,       /* bit14  RPRT_DISC_PORT          */
2995         lpfc_fdmi_port_attr_nportid,        /* bit15  RPRT_PORT_ID            */
2996         lpfc_fdmi_smart_attr_service,       /* bit16  RPRT_SMART_SERVICE      */
2997         lpfc_fdmi_smart_attr_guid,          /* bit17  RPRT_SMART_GUID         */
2998         lpfc_fdmi_smart_attr_version,       /* bit18  RPRT_SMART_VERSION      */
2999         lpfc_fdmi_smart_attr_model,         /* bit19  RPRT_SMART_MODEL        */
3000         lpfc_fdmi_smart_attr_port_info,     /* bit20  RPRT_SMART_PORT_INFO    */
3001         lpfc_fdmi_smart_attr_qos,           /* bit21  RPRT_SMART_QOS          */
3002         lpfc_fdmi_smart_attr_security,      /* bit22  RPRT_SMART_SECURITY     */
3003 };
3004 
3005 /**
3006  * lpfc_fdmi_cmd - Build and send a FDMI cmd to the specified NPort
3007  * @vport: pointer to a host virtual N_Port data structure.
3008  * @ndlp: ndlp to send FDMI cmd to (if NULL use FDMI_DID)
3009  * cmdcode: FDMI command to send
3010  * mask: Mask of HBA or PORT Attributes to send
3011  *
3012  * Builds and sends a FDMI command using the CT subsystem.
3013  */
3014 int
3015 lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3016               int cmdcode, uint32_t new_mask)
3017 {
3018         struct lpfc_hba *phba = vport->phba;
3019         struct lpfc_dmabuf *mp, *bmp;
3020         struct lpfc_sli_ct_request *CtReq;
3021         struct ulp_bde64 *bpl;
3022         uint32_t bit_pos;
3023         uint32_t size;
3024         uint32_t rsp_size;
3025         uint32_t mask;
3026         struct lpfc_fdmi_reg_hba *rh;
3027         struct lpfc_fdmi_port_entry *pe;
3028         struct lpfc_fdmi_reg_portattr *pab = NULL;
3029         struct lpfc_fdmi_attr_block *ab = NULL;
3030         int  (*func)(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad);
3031         void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
3032                      struct lpfc_iocbq *);
3033 
3034         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
3035                 return 0;
3036 
3037         cmpl = lpfc_cmpl_ct_disc_fdmi; /* called from discovery */
3038 
3039         /* fill in BDEs for command */
3040         /* Allocate buffer for command payload */
3041         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3042         if (!mp)
3043                 goto fdmi_cmd_exit;
3044 
3045         mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
3046         if (!mp->virt)
3047                 goto fdmi_cmd_free_mp;
3048 
3049         /* Allocate buffer for Buffer ptr list */
3050         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3051         if (!bmp)
3052                 goto fdmi_cmd_free_mpvirt;
3053 
3054         bmp->virt = lpfc_mbuf_alloc(phba, 0, &(bmp->phys));
3055         if (!bmp->virt)
3056                 goto fdmi_cmd_free_bmp;
3057 
3058         INIT_LIST_HEAD(&mp->list);
3059         INIT_LIST_HEAD(&bmp->list);
3060 
3061         /* FDMI request */
3062         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3063                          "0218 FDMI Request Data: x%x x%x x%x\n",
3064                          vport->fc_flag, vport->port_state, cmdcode);
3065         CtReq = (struct lpfc_sli_ct_request *)mp->virt;
3066 
3067         /* First populate the CT_IU preamble */
3068         memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
3069         CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
3070         CtReq->RevisionId.bits.InId = 0;
3071 
3072         CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
3073         CtReq->FsSubType = SLI_CT_FDMI_Subtypes;
3074 
3075         CtReq->CommandResponse.bits.CmdRsp = cpu_to_be16(cmdcode);
3076         rsp_size = LPFC_BPL_SIZE;
3077         size = 0;
3078 
3079         /* Next fill in the specific FDMI cmd information */
3080         switch (cmdcode) {
3081         case SLI_MGMT_RHAT:
3082         case SLI_MGMT_RHBA:
3083                 rh = (struct lpfc_fdmi_reg_hba *)&CtReq->un.PortID;
3084                 /* HBA Identifier */
3085                 memcpy(&rh->hi.PortName, &phba->pport->fc_sparam.portName,
3086                        sizeof(struct lpfc_name));
3087 
3088                 if (cmdcode == SLI_MGMT_RHBA) {
3089                         /* Registered Port List */
3090                         /* One entry (port) per adapter */
3091                         rh->rpl.EntryCnt = cpu_to_be32(1);
3092                         memcpy(&rh->rpl.pe, &phba->pport->fc_sparam.portName,
3093                                sizeof(struct lpfc_name));
3094 
3095                         /* point to the HBA attribute block */
3096                         size = 2 * sizeof(struct lpfc_name) +
3097                                 FOURBYTES;
3098                 } else {
3099                         size = sizeof(struct lpfc_name);
3100                 }
3101                 ab = (struct lpfc_fdmi_attr_block *)((uint8_t *)rh + size);
3102                 ab->EntryCnt = 0;
3103                 size += FOURBYTES;
3104                 bit_pos = 0;
3105                 if (new_mask)
3106                         mask = new_mask;
3107                 else
3108                         mask = vport->fdmi_hba_mask;
3109 
3110                 /* Mask will dictate what attributes to build in the request */
3111                 while (mask) {
3112                         if (mask & 0x1) {
3113                                 func = lpfc_fdmi_hba_action[bit_pos];
3114                                 size += func(vport,
3115                                              (struct lpfc_fdmi_attr_def *)
3116                                              ((uint8_t *)rh + size));
3117                                 ab->EntryCnt++;
3118                                 if ((size + 256) >
3119                                     (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3120                                         goto hba_out;
3121                         }
3122                         mask = mask >> 1;
3123                         bit_pos++;
3124                 }
3125 hba_out:
3126                 ab->EntryCnt = cpu_to_be32(ab->EntryCnt);
3127                 /* Total size */
3128                 size = GID_REQUEST_SZ - 4 + size;
3129                 break;
3130 
3131         case SLI_MGMT_RPRT:
3132         case SLI_MGMT_RPA:
3133                 pab = (struct lpfc_fdmi_reg_portattr *)&CtReq->un.PortID;
3134                 if (cmdcode == SLI_MGMT_RPRT) {
3135                         rh = (struct lpfc_fdmi_reg_hba *)pab;
3136                         /* HBA Identifier */
3137                         memcpy(&rh->hi.PortName,
3138                                &phba->pport->fc_sparam.portName,
3139                                sizeof(struct lpfc_name));
3140                         pab = (struct lpfc_fdmi_reg_portattr *)
3141                                 ((uint8_t *)pab +  sizeof(struct lpfc_name));
3142                 }
3143 
3144                 memcpy((uint8_t *)&pab->PortName,
3145                        (uint8_t *)&vport->fc_sparam.portName,
3146                        sizeof(struct lpfc_name));
3147                 size += sizeof(struct lpfc_name) + FOURBYTES;
3148                 pab->ab.EntryCnt = 0;
3149                 bit_pos = 0;
3150                 if (new_mask)
3151                         mask = new_mask;
3152                 else
3153                         mask = vport->fdmi_port_mask;
3154 
3155                 /* Mask will dictate what attributes to build in the request */
3156                 while (mask) {
3157                         if (mask & 0x1) {
3158                                 func = lpfc_fdmi_port_action[bit_pos];
3159                                 size += func(vport,
3160                                              (struct lpfc_fdmi_attr_def *)
3161                                              ((uint8_t *)pab + size));
3162                                 pab->ab.EntryCnt++;
3163                                 if ((size + 256) >
3164                                     (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3165                                         goto port_out;
3166                         }
3167                         mask = mask >> 1;
3168                         bit_pos++;
3169                 }
3170 port_out:
3171                 pab->ab.EntryCnt = cpu_to_be32(pab->ab.EntryCnt);
3172                 /* Total size */
3173                 if (cmdcode == SLI_MGMT_RPRT)
3174                         size += sizeof(struct lpfc_name);
3175                 size = GID_REQUEST_SZ - 4 + size;
3176                 break;
3177 
3178         case SLI_MGMT_GHAT:
3179         case SLI_MGMT_GRPL:
3180                 rsp_size = FC_MAX_NS_RSP;
3181                 /* fall through */
3182         case SLI_MGMT_DHBA:
3183         case SLI_MGMT_DHAT:
3184                 pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3185                 memcpy((uint8_t *)&pe->PortName,
3186                        (uint8_t *)&vport->fc_sparam.portName,
3187                        sizeof(struct lpfc_name));
3188                 size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3189                 break;
3190 
3191         case SLI_MGMT_GPAT:
3192         case SLI_MGMT_GPAS:
3193                 rsp_size = FC_MAX_NS_RSP;
3194                 /* fall through */
3195         case SLI_MGMT_DPRT:
3196         case SLI_MGMT_DPA:
3197                 pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3198                 memcpy((uint8_t *)&pe->PortName,
3199                        (uint8_t *)&vport->fc_sparam.portName,
3200                        sizeof(struct lpfc_name));
3201                 size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3202                 break;
3203         case SLI_MGMT_GRHL:
3204                 size = GID_REQUEST_SZ - 4;
3205                 break;
3206         default:
3207                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3208                                  "0298 FDMI cmdcode x%x not supported\n",
3209                                  cmdcode);
3210                 goto fdmi_cmd_free_bmpvirt;
3211         }
3212         CtReq->CommandResponse.bits.Size = cpu_to_be16(rsp_size);
3213 
3214         bpl = (struct ulp_bde64 *)bmp->virt;
3215         bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
3216         bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
3217         bpl->tus.f.bdeFlags = 0;
3218         bpl->tus.f.bdeSize = size;
3219 
3220         /*
3221          * The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
3222          * to hold ndlp reference for the corresponding callback function.
3223          */
3224         if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, 0))
3225                 return 0;
3226 
3227         /*
3228          * Decrement ndlp reference count to release ndlp reference held
3229          * for the failed command's callback function.
3230          */
3231         lpfc_nlp_put(ndlp);
3232 
3233 fdmi_cmd_free_bmpvirt:
3234         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3235 fdmi_cmd_free_bmp:
3236         kfree(bmp);
3237 fdmi_cmd_free_mpvirt:
3238         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3239 fdmi_cmd_free_mp:
3240         kfree(mp);
3241 fdmi_cmd_exit:
3242         /* Issue FDMI request failed */
3243         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3244                          "0244 Issue FDMI request failed Data: x%x\n",
3245                          cmdcode);
3246         return 1;
3247 }
3248 
3249 /**
3250  * lpfc_delayed_disc_tmo - Timeout handler for delayed discovery timer.
3251  * @ptr - Context object of the timer.
3252  *
3253  * This function set the WORKER_DELAYED_DISC_TMO flag and wake up
3254  * the worker thread.
3255  **/
3256 void
3257 lpfc_delayed_disc_tmo(struct timer_list *t)
3258 {
3259         struct lpfc_vport *vport = from_timer(vport, t, delayed_disc_tmo);
3260         struct lpfc_hba   *phba = vport->phba;
3261         uint32_t tmo_posted;
3262         unsigned long iflag;
3263 
3264         spin_lock_irqsave(&vport->work_port_lock, iflag);
3265         tmo_posted = vport->work_port_events & WORKER_DELAYED_DISC_TMO;
3266         if (!tmo_posted)
3267                 vport->work_port_events |= WORKER_DELAYED_DISC_TMO;
3268         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3269 
3270         if (!tmo_posted)
3271                 lpfc_worker_wake_up(phba);
3272         return;
3273 }
3274 
3275 /**
3276  * lpfc_delayed_disc_timeout_handler - Function called by worker thread to
3277  *      handle delayed discovery.
3278  * @vport: pointer to a host virtual N_Port data structure.
3279  *
3280  * This function start nport discovery of the vport.
3281  **/
3282 void
3283 lpfc_delayed_disc_timeout_handler(struct lpfc_vport *vport)
3284 {
3285         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3286 
3287         spin_lock_irq(shost->host_lock);
3288         if (!(vport->fc_flag & FC_DISC_DELAYED)) {
3289                 spin_unlock_irq(shost->host_lock);
3290                 return;
3291         }
3292         vport->fc_flag &= ~FC_DISC_DELAYED;
3293         spin_unlock_irq(shost->host_lock);
3294 
3295         lpfc_do_scr_ns_plogi(vport->phba, vport);
3296 }
3297 
3298 void
3299 lpfc_decode_firmware_rev(struct lpfc_hba *phba, char *fwrevision, int flag)
3300 {
3301         struct lpfc_sli *psli = &phba->sli;
3302         lpfc_vpd_t *vp = &phba->vpd;
3303         uint32_t b1, b2, b3, b4, i, rev;
3304         char c;
3305         uint32_t *ptr, str[4];
3306         uint8_t *fwname;
3307 
3308         if (phba->sli_rev == LPFC_SLI_REV4)
3309                 snprintf(fwrevision, FW_REV_STR_SIZE, "%s", vp->rev.opFwName);
3310         else if (vp->rev.rBit) {
3311                 if (psli->sli_flag & LPFC_SLI_ACTIVE)
3312                         rev = vp->rev.sli2FwRev;
3313                 else
3314                         rev = vp->rev.sli1FwRev;
3315 
3316                 b1 = (rev & 0x0000f000) >> 12;
3317                 b2 = (rev & 0x00000f00) >> 8;
3318                 b3 = (rev & 0x000000c0) >> 6;
3319                 b4 = (rev & 0x00000030) >> 4;
3320 
3321                 switch (b4) {
3322                 case 0:
3323                         c = 'N';
3324                         break;
3325                 case 1:
3326                         c = 'A';
3327                         break;
3328                 case 2:
3329                         c = 'B';
3330                         break;
3331                 case 3:
3332                         c = 'X';
3333                         break;
3334                 default:
3335                         c = 0;
3336                         break;
3337                 }
3338                 b4 = (rev & 0x0000000f);
3339 
3340                 if (psli->sli_flag & LPFC_SLI_ACTIVE)
3341                         fwname = vp->rev.sli2FwName;
3342                 else
3343                         fwname = vp->rev.sli1FwName;
3344 
3345                 for (i = 0; i < 16; i++)
3346                         if (fwname[i] == 0x20)
3347                                 fwname[i] = 0;
3348 
3349                 ptr = (uint32_t*)fwname;
3350 
3351                 for (i = 0; i < 3; i++)
3352                         str[i] = be32_to_cpu(*ptr++);
3353 
3354                 if (c == 0) {
3355                         if (flag)
3356                                 sprintf(fwrevision, "%d.%d%d (%s)",
3357                                         b1, b2, b3, (char *)str);
3358                         else
3359                                 sprintf(fwrevision, "%d.%d%d", b1,
3360                                         b2, b3);
3361                 } else {
3362                         if (flag)
3363                                 sprintf(fwrevision, "%d.%d%d%c%d (%s)",
3364                                         b1, b2, b3, c,
3365                                         b4, (char *)str);
3366                         else
3367                                 sprintf(fwrevision, "%d.%d%d%c%d",
3368                                         b1, b2, b3, c, b4);
3369                 }
3370         } else {
3371                 rev = vp->rev.smFwRev;
3372 
3373                 b1 = (rev & 0xff000000) >> 24;
3374                 b2 = (rev & 0x00f00000) >> 20;
3375                 b3 = (rev & 0x000f0000) >> 16;
3376                 c  = (rev & 0x0000ff00) >> 8;
3377                 b4 = (rev & 0x000000ff);
3378 
3379                 sprintf(fwrevision, "%d.%d%d%c%d", b1, b2, b3, c, b4);
3380         }
3381         return;
3382 }

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