root/drivers/scsi/lpfc/lpfc_hbadisc.c

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

DEFINITIONS

This source file includes following definitions.
  1. lpfc_terminate_rport_io
  2. lpfc_dev_loss_tmo_callbk
  3. lpfc_dev_loss_tmo_handler
  4. lpfc_sli4_post_dev_loss_tmo_handler
  5. lpfc_alloc_fast_evt
  6. lpfc_free_fast_evt
  7. lpfc_send_fastpath_evt
  8. lpfc_work_list_done
  9. lpfc_work_done
  10. lpfc_do_work
  11. lpfc_workq_post_event
  12. lpfc_cleanup_rpis
  13. lpfc_port_link_failure
  14. lpfc_linkdown_port
  15. lpfc_linkdown
  16. lpfc_linkup_cleanup_nodes
  17. lpfc_linkup_port
  18. lpfc_linkup
  19. lpfc_mbx_cmpl_clear_la
  20. lpfc_mbx_cmpl_local_config_link
  21. lpfc_sli4_clear_fcf_rr_bmask
  22. lpfc_mbx_cmpl_reg_fcfi
  23. lpfc_fab_name_match
  24. lpfc_sw_name_match
  25. lpfc_mac_addr_match
  26. lpfc_vlan_id_match
  27. __lpfc_update_fcf_record_pri
  28. lpfc_copy_fcf_record
  29. __lpfc_update_fcf_record
  30. lpfc_register_fcf
  31. lpfc_match_fcf_conn_list
  32. lpfc_check_pending_fcoe_event
  33. lpfc_sli4_new_fcf_random_select
  34. lpfc_sli4_fcf_rec_mbox_parse
  35. lpfc_sli4_log_fcf_record_info
  36. lpfc_sli4_fcf_record_match
  37. lpfc_sli4_fcf_rr_next_proc
  38. lpfc_sli4_fcf_pri_list_del
  39. lpfc_sli4_set_fcf_flogi_fail
  40. lpfc_sli4_fcf_pri_list_add
  41. lpfc_mbx_cmpl_fcf_scan_read_fcf_rec
  42. lpfc_mbx_cmpl_fcf_rr_read_fcf_rec
  43. lpfc_mbx_cmpl_read_fcf_rec
  44. lpfc_init_vfi_cmpl
  45. lpfc_issue_init_vfi
  46. lpfc_init_vpi_cmpl
  47. lpfc_issue_init_vpi
  48. lpfc_start_fdiscs
  49. lpfc_mbx_cmpl_reg_vfi
  50. lpfc_mbx_cmpl_read_sparam
  51. lpfc_mbx_process_link_up
  52. lpfc_enable_la
  53. lpfc_mbx_issue_link_down
  54. lpfc_mbx_cmpl_read_topology
  55. lpfc_mbx_cmpl_reg_login
  56. lpfc_mbx_cmpl_unreg_vpi
  57. lpfc_mbx_unreg_vpi
  58. lpfc_mbx_cmpl_reg_vpi
  59. lpfc_create_static_vport
  60. lpfc_mbx_cmpl_fabric_reg_login
  61. lpfc_issue_gidft
  62. lpfc_issue_gidpt
  63. lpfc_mbx_cmpl_ns_reg_login
  64. lpfc_register_remote_port
  65. lpfc_unregister_remote_port
  66. lpfc_nlp_counters
  67. lpfc_nlp_state_cleanup
  68. lpfc_nlp_state_name
  69. lpfc_nlp_set_state
  70. lpfc_enqueue_node
  71. lpfc_dequeue_node
  72. lpfc_disable_node
  73. lpfc_initialize_node
  74. lpfc_enable_node
  75. lpfc_drop_node
  76. lpfc_set_disctmo
  77. lpfc_can_disctmo
  78. lpfc_check_sli_ndlp
  79. __lpfc_dequeue_nport_iocbs
  80. lpfc_sli3_dequeue_nport_iocbs
  81. lpfc_sli4_dequeue_nport_iocbs
  82. lpfc_no_rpi
  83. lpfc_nlp_logo_unreg
  84. lpfc_set_unreg_login_mbx_cmpl
  85. lpfc_unreg_rpi
  86. lpfc_unreg_hba_rpis
  87. lpfc_unreg_all_rpis
  88. lpfc_unreg_default_rpis
  89. lpfc_cleanup_node
  90. lpfc_nlp_remove
  91. lpfc_matchdid
  92. __lpfc_findnode_did
  93. lpfc_findnode_did
  94. lpfc_findnode_mapped
  95. lpfc_setup_disc_node
  96. lpfc_disc_list_loopmap
  97. lpfc_issue_clear_la
  98. lpfc_issue_reg_vpi
  99. lpfc_disc_start
  100. lpfc_free_tx
  101. lpfc_disc_flush_list
  102. lpfc_cleanup_discovery_resources
  103. lpfc_disc_timeout
  104. lpfc_disc_timeout_handler
  105. lpfc_mbx_cmpl_fdmi_reg_login
  106. lpfc_filter_by_rpi
  107. lpfc_filter_by_wwpn
  108. __lpfc_find_node
  109. __lpfc_findnode_rpi
  110. lpfc_findnode_wwpn
  111. lpfc_findnode_rpi
  112. lpfc_find_vport_by_vpid
  113. lpfc_nlp_init
  114. lpfc_nlp_release
  115. lpfc_nlp_get
  116. lpfc_nlp_put
  117. lpfc_nlp_not_used
  118. lpfc_fcf_inuse
  119. lpfc_unregister_vfi_cmpl
  120. lpfc_unregister_fcfi_cmpl
  121. lpfc_unregister_fcf_prep
  122. lpfc_sli4_unregister_fcf
  123. lpfc_unregister_fcf_rescan
  124. lpfc_unregister_fcf
  125. lpfc_unregister_unused_fcf
  126. lpfc_read_fcf_conn_tbl
  127. lpfc_read_fcoe_param
  128. lpfc_get_rec_conf23
  129. lpfc_parse_fcoe_conf

   1 /*******************************************************************
   2  * This file is part of the Emulex Linux Device Driver for         *
   3  * Fibre Channel Host Bus Adapters.                                *
   4  * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
   5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
   6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
   7  * EMULEX and SLI are trademarks of Emulex.                        *
   8  * www.broadcom.com                                                *
   9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
  10  *                                                                 *
  11  * This program is free software; you can redistribute it and/or   *
  12  * modify it under the terms of version 2 of the GNU General       *
  13  * Public License as published by the Free Software Foundation.    *
  14  * This program is distributed in the hope that it will be useful. *
  15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
  16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
  17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
  18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
  20  * more details, a copy of which can be found in the file COPYING  *
  21  * included with this package.                                     *
  22  *******************************************************************/
  23 
  24 #include <linux/blkdev.h>
  25 #include <linux/delay.h>
  26 #include <linux/slab.h>
  27 #include <linux/pci.h>
  28 #include <linux/kthread.h>
  29 #include <linux/interrupt.h>
  30 #include <linux/lockdep.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 <linux/nvme-fc-driver.h>
  40 
  41 #include "lpfc_hw4.h"
  42 #include "lpfc_hw.h"
  43 #include "lpfc_nl.h"
  44 #include "lpfc_disc.h"
  45 #include "lpfc_sli.h"
  46 #include "lpfc_sli4.h"
  47 #include "lpfc.h"
  48 #include "lpfc_scsi.h"
  49 #include "lpfc_nvme.h"
  50 #include "lpfc_logmsg.h"
  51 #include "lpfc_crtn.h"
  52 #include "lpfc_vport.h"
  53 #include "lpfc_debugfs.h"
  54 
  55 /* AlpaArray for assignment of scsid for scan-down and bind_method */
  56 static uint8_t lpfcAlpaArray[] = {
  57         0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
  58         0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
  59         0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
  60         0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
  61         0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
  62         0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
  63         0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
  64         0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
  65         0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
  66         0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
  67         0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
  68         0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
  69         0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
  70 };
  71 
  72 static void lpfc_disc_timeout_handler(struct lpfc_vport *);
  73 static void lpfc_disc_flush_list(struct lpfc_vport *vport);
  74 static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
  75 static int lpfc_fcf_inuse(struct lpfc_hba *);
  76 
  77 void
  78 lpfc_terminate_rport_io(struct fc_rport *rport)
  79 {
  80         struct lpfc_rport_data *rdata;
  81         struct lpfc_nodelist * ndlp;
  82         struct lpfc_hba *phba;
  83 
  84         rdata = rport->dd_data;
  85         ndlp = rdata->pnode;
  86 
  87         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  88                 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
  89                         printk(KERN_ERR "Cannot find remote node"
  90                         " to terminate I/O Data x%x\n",
  91                         rport->port_id);
  92                 return;
  93         }
  94 
  95         phba  = ndlp->phba;
  96 
  97         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
  98                 "rport terminate: sid:x%x did:x%x flg:x%x",
  99                 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
 100 
 101         if (ndlp->nlp_sid != NLP_NO_SID) {
 102                 lpfc_sli_abort_iocb(ndlp->vport,
 103                         &phba->sli.sli3_ring[LPFC_FCP_RING],
 104                         ndlp->nlp_sid, 0, LPFC_CTX_TGT);
 105         }
 106 }
 107 
 108 /*
 109  * This function will be called when dev_loss_tmo fire.
 110  */
 111 void
 112 lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
 113 {
 114         struct lpfc_rport_data *rdata;
 115         struct lpfc_nodelist * ndlp;
 116         struct lpfc_vport *vport;
 117         struct Scsi_Host *shost;
 118         struct lpfc_hba   *phba;
 119         struct lpfc_work_evt *evtp;
 120         int  put_node;
 121         int  put_rport;
 122         unsigned long iflags;
 123 
 124         rdata = rport->dd_data;
 125         ndlp = rdata->pnode;
 126         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
 127                 return;
 128 
 129         vport = ndlp->vport;
 130         phba  = vport->phba;
 131 
 132         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
 133                 "rport devlosscb: sid:x%x did:x%x flg:x%x",
 134                 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
 135 
 136         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
 137                          "3181 dev_loss_callbk x%06x, rport x%px flg x%x\n",
 138                          ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
 139 
 140         /* Don't defer this if we are in the process of deleting the vport
 141          * or unloading the driver. The unload will cleanup the node
 142          * appropriately we just need to cleanup the ndlp rport info here.
 143          */
 144         if (vport->load_flag & FC_UNLOADING) {
 145                 put_node = rdata->pnode != NULL;
 146                 put_rport = ndlp->rport != NULL;
 147                 rdata->pnode = NULL;
 148                 ndlp->rport = NULL;
 149                 if (put_node)
 150                         lpfc_nlp_put(ndlp);
 151                 if (put_rport)
 152                         put_device(&rport->dev);
 153                 return;
 154         }
 155 
 156         if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
 157                 return;
 158 
 159         if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn))
 160                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
 161                                 "6789 rport name %llx != node port name %llx",
 162                                 rport->port_name,
 163                                 wwn_to_u64(ndlp->nlp_portname.u.wwn));
 164 
 165         evtp = &ndlp->dev_loss_evt;
 166 
 167         if (!list_empty(&evtp->evt_listp)) {
 168                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
 169                                 "6790 rport name %llx dev_loss_evt pending",
 170                                 rport->port_name);
 171                 return;
 172         }
 173 
 174         shost = lpfc_shost_from_vport(vport);
 175         spin_lock_irqsave(shost->host_lock, iflags);
 176         ndlp->nlp_flag |= NLP_IN_DEV_LOSS;
 177         spin_unlock_irqrestore(shost->host_lock, iflags);
 178 
 179         /* We need to hold the node by incrementing the reference
 180          * count until this queued work is done
 181          */
 182         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
 183 
 184         spin_lock_irqsave(&phba->hbalock, iflags);
 185         if (evtp->evt_arg1) {
 186                 evtp->evt = LPFC_EVT_DEV_LOSS;
 187                 list_add_tail(&evtp->evt_listp, &phba->work_list);
 188                 lpfc_worker_wake_up(phba);
 189         }
 190         spin_unlock_irqrestore(&phba->hbalock, iflags);
 191 
 192         return;
 193 }
 194 
 195 /**
 196  * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
 197  * @ndlp: Pointer to remote node object.
 198  *
 199  * This function is called from the worker thread when devloss timeout timer
 200  * expires. For SLI4 host, this routine shall return 1 when at lease one
 201  * remote node, including this @ndlp, is still in use of FCF; otherwise, this
 202  * routine shall return 0 when there is no remote node is still in use of FCF
 203  * when devloss timeout happened to this @ndlp.
 204  **/
 205 static int
 206 lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
 207 {
 208         struct lpfc_rport_data *rdata;
 209         struct fc_rport   *rport;
 210         struct lpfc_vport *vport;
 211         struct lpfc_hba   *phba;
 212         struct Scsi_Host  *shost;
 213         uint8_t *name;
 214         int  put_node;
 215         int warn_on = 0;
 216         int fcf_inuse = 0;
 217         unsigned long iflags;
 218 
 219         rport = ndlp->rport;
 220         vport = ndlp->vport;
 221         shost = lpfc_shost_from_vport(vport);
 222 
 223         spin_lock_irqsave(shost->host_lock, iflags);
 224         ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
 225         spin_unlock_irqrestore(shost->host_lock, iflags);
 226 
 227         if (!rport)
 228                 return fcf_inuse;
 229 
 230         name = (uint8_t *) &ndlp->nlp_portname;
 231         phba  = vport->phba;
 232 
 233         if (phba->sli_rev == LPFC_SLI_REV4)
 234                 fcf_inuse = lpfc_fcf_inuse(phba);
 235 
 236         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
 237                 "rport devlosstmo:did:x%x type:x%x id:x%x",
 238                 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
 239 
 240         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
 241                          "3182 dev_loss_tmo_handler x%06x, rport x%px flg x%x\n",
 242                          ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
 243 
 244         /*
 245          * lpfc_nlp_remove if reached with dangling rport drops the
 246          * reference. To make sure that does not happen clear rport
 247          * pointer in ndlp before lpfc_nlp_put.
 248          */
 249         rdata = rport->dd_data;
 250 
 251         /* Don't defer this if we are in the process of deleting the vport
 252          * or unloading the driver. The unload will cleanup the node
 253          * appropriately we just need to cleanup the ndlp rport info here.
 254          */
 255         if (vport->load_flag & FC_UNLOADING) {
 256                 if (ndlp->nlp_sid != NLP_NO_SID) {
 257                         /* flush the target */
 258                         lpfc_sli_abort_iocb(vport,
 259                                             &phba->sli.sli3_ring[LPFC_FCP_RING],
 260                                             ndlp->nlp_sid, 0, LPFC_CTX_TGT);
 261                 }
 262                 put_node = rdata->pnode != NULL;
 263                 rdata->pnode = NULL;
 264                 ndlp->rport = NULL;
 265                 if (put_node)
 266                         lpfc_nlp_put(ndlp);
 267                 put_device(&rport->dev);
 268 
 269                 return fcf_inuse;
 270         }
 271 
 272         if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
 273                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 274                                  "0284 Devloss timeout Ignored on "
 275                                  "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
 276                                  "NPort x%x\n",
 277                                  *name, *(name+1), *(name+2), *(name+3),
 278                                  *(name+4), *(name+5), *(name+6), *(name+7),
 279                                  ndlp->nlp_DID);
 280                 return fcf_inuse;
 281         }
 282 
 283         put_node = rdata->pnode != NULL;
 284         rdata->pnode = NULL;
 285         ndlp->rport = NULL;
 286         if (put_node)
 287                 lpfc_nlp_put(ndlp);
 288         put_device(&rport->dev);
 289 
 290         if (ndlp->nlp_type & NLP_FABRIC)
 291                 return fcf_inuse;
 292 
 293         if (ndlp->nlp_sid != NLP_NO_SID) {
 294                 warn_on = 1;
 295                 lpfc_sli_abort_iocb(vport, &phba->sli.sli3_ring[LPFC_FCP_RING],
 296                                     ndlp->nlp_sid, 0, LPFC_CTX_TGT);
 297         }
 298 
 299         if (warn_on) {
 300                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
 301                                  "0203 Devloss timeout on "
 302                                  "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
 303                                  "NPort x%06x Data: x%x x%x x%x\n",
 304                                  *name, *(name+1), *(name+2), *(name+3),
 305                                  *(name+4), *(name+5), *(name+6), *(name+7),
 306                                  ndlp->nlp_DID, ndlp->nlp_flag,
 307                                  ndlp->nlp_state, ndlp->nlp_rpi);
 308         } else {
 309                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 310                                  "0204 Devloss timeout on "
 311                                  "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
 312                                  "NPort x%06x Data: x%x x%x x%x\n",
 313                                  *name, *(name+1), *(name+2), *(name+3),
 314                                  *(name+4), *(name+5), *(name+6), *(name+7),
 315                                  ndlp->nlp_DID, ndlp->nlp_flag,
 316                                  ndlp->nlp_state, ndlp->nlp_rpi);
 317         }
 318 
 319         if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
 320             !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
 321             (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
 322             (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
 323             (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
 324                 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
 325 
 326         return fcf_inuse;
 327 }
 328 
 329 /**
 330  * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
 331  * @phba: Pointer to hba context object.
 332  * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
 333  * @nlp_did: remote node identifer with devloss timeout.
 334  *
 335  * This function is called from the worker thread after invoking devloss
 336  * timeout handler and releasing the reference count for the ndlp with
 337  * which the devloss timeout was handled for SLI4 host. For the devloss
 338  * timeout of the last remote node which had been in use of FCF, when this
 339  * routine is invoked, it shall be guaranteed that none of the remote are
 340  * in-use of FCF. When devloss timeout to the last remote using the FCF,
 341  * if the FIP engine is neither in FCF table scan process nor roundrobin
 342  * failover process, the in-use FCF shall be unregistered. If the FIP
 343  * engine is in FCF discovery process, the devloss timeout state shall
 344  * be set for either the FCF table scan process or roundrobin failover
 345  * process to unregister the in-use FCF.
 346  **/
 347 static void
 348 lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
 349                                     uint32_t nlp_did)
 350 {
 351         /* If devloss timeout happened to a remote node when FCF had no
 352          * longer been in-use, do nothing.
 353          */
 354         if (!fcf_inuse)
 355                 return;
 356 
 357         if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
 358                 spin_lock_irq(&phba->hbalock);
 359                 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
 360                         if (phba->hba_flag & HBA_DEVLOSS_TMO) {
 361                                 spin_unlock_irq(&phba->hbalock);
 362                                 return;
 363                         }
 364                         phba->hba_flag |= HBA_DEVLOSS_TMO;
 365                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
 366                                         "2847 Last remote node (x%x) using "
 367                                         "FCF devloss tmo\n", nlp_did);
 368                 }
 369                 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
 370                         spin_unlock_irq(&phba->hbalock);
 371                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
 372                                         "2868 Devloss tmo to FCF rediscovery "
 373                                         "in progress\n");
 374                         return;
 375                 }
 376                 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
 377                         spin_unlock_irq(&phba->hbalock);
 378                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
 379                                         "2869 Devloss tmo to idle FIP engine, "
 380                                         "unreg in-use FCF and rescan.\n");
 381                         /* Unregister in-use FCF and rescan */
 382                         lpfc_unregister_fcf_rescan(phba);
 383                         return;
 384                 }
 385                 spin_unlock_irq(&phba->hbalock);
 386                 if (phba->hba_flag & FCF_TS_INPROG)
 387                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
 388                                         "2870 FCF table scan in progress\n");
 389                 if (phba->hba_flag & FCF_RR_INPROG)
 390                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
 391                                         "2871 FLOGI roundrobin FCF failover "
 392                                         "in progress\n");
 393         }
 394         lpfc_unregister_unused_fcf(phba);
 395 }
 396 
 397 /**
 398  * lpfc_alloc_fast_evt - Allocates data structure for posting event
 399  * @phba: Pointer to hba context object.
 400  *
 401  * This function is called from the functions which need to post
 402  * events from interrupt context. This function allocates data
 403  * structure required for posting event. It also keeps track of
 404  * number of events pending and prevent event storm when there are
 405  * too many events.
 406  **/
 407 struct lpfc_fast_path_event *
 408 lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
 409         struct lpfc_fast_path_event *ret;
 410 
 411         /* If there are lot of fast event do not exhaust memory due to this */
 412         if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
 413                 return NULL;
 414 
 415         ret = kzalloc(sizeof(struct lpfc_fast_path_event),
 416                         GFP_ATOMIC);
 417         if (ret) {
 418                 atomic_inc(&phba->fast_event_count);
 419                 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
 420                 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
 421         }
 422         return ret;
 423 }
 424 
 425 /**
 426  * lpfc_free_fast_evt - Frees event data structure
 427  * @phba: Pointer to hba context object.
 428  * @evt:  Event object which need to be freed.
 429  *
 430  * This function frees the data structure required for posting
 431  * events.
 432  **/
 433 void
 434 lpfc_free_fast_evt(struct lpfc_hba *phba,
 435                 struct lpfc_fast_path_event *evt) {
 436 
 437         atomic_dec(&phba->fast_event_count);
 438         kfree(evt);
 439 }
 440 
 441 /**
 442  * lpfc_send_fastpath_evt - Posts events generated from fast path
 443  * @phba: Pointer to hba context object.
 444  * @evtp: Event data structure.
 445  *
 446  * This function is called from worker thread, when the interrupt
 447  * context need to post an event. This function posts the event
 448  * to fc transport netlink interface.
 449  **/
 450 static void
 451 lpfc_send_fastpath_evt(struct lpfc_hba *phba,
 452                 struct lpfc_work_evt *evtp)
 453 {
 454         unsigned long evt_category, evt_sub_category;
 455         struct lpfc_fast_path_event *fast_evt_data;
 456         char *evt_data;
 457         uint32_t evt_data_size;
 458         struct Scsi_Host *shost;
 459 
 460         fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
 461                 work_evt);
 462 
 463         evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
 464         evt_sub_category = (unsigned long) fast_evt_data->un.
 465                         fabric_evt.subcategory;
 466         shost = lpfc_shost_from_vport(fast_evt_data->vport);
 467         if (evt_category == FC_REG_FABRIC_EVENT) {
 468                 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
 469                         evt_data = (char *) &fast_evt_data->un.read_check_error;
 470                         evt_data_size = sizeof(fast_evt_data->un.
 471                                 read_check_error);
 472                 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
 473                         (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
 474                         evt_data = (char *) &fast_evt_data->un.fabric_evt;
 475                         evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
 476                 } else {
 477                         lpfc_free_fast_evt(phba, fast_evt_data);
 478                         return;
 479                 }
 480         } else if (evt_category == FC_REG_SCSI_EVENT) {
 481                 switch (evt_sub_category) {
 482                 case LPFC_EVENT_QFULL:
 483                 case LPFC_EVENT_DEVBSY:
 484                         evt_data = (char *) &fast_evt_data->un.scsi_evt;
 485                         evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
 486                         break;
 487                 case LPFC_EVENT_CHECK_COND:
 488                         evt_data = (char *) &fast_evt_data->un.check_cond_evt;
 489                         evt_data_size =  sizeof(fast_evt_data->un.
 490                                 check_cond_evt);
 491                         break;
 492                 case LPFC_EVENT_VARQUEDEPTH:
 493                         evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
 494                         evt_data_size = sizeof(fast_evt_data->un.
 495                                 queue_depth_evt);
 496                         break;
 497                 default:
 498                         lpfc_free_fast_evt(phba, fast_evt_data);
 499                         return;
 500                 }
 501         } else {
 502                 lpfc_free_fast_evt(phba, fast_evt_data);
 503                 return;
 504         }
 505 
 506         if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
 507                 fc_host_post_vendor_event(shost,
 508                         fc_get_event_number(),
 509                         evt_data_size,
 510                         evt_data,
 511                         LPFC_NL_VENDOR_ID);
 512 
 513         lpfc_free_fast_evt(phba, fast_evt_data);
 514         return;
 515 }
 516 
 517 static void
 518 lpfc_work_list_done(struct lpfc_hba *phba)
 519 {
 520         struct lpfc_work_evt  *evtp = NULL;
 521         struct lpfc_nodelist  *ndlp;
 522         int free_evt;
 523         int fcf_inuse;
 524         uint32_t nlp_did;
 525 
 526         spin_lock_irq(&phba->hbalock);
 527         while (!list_empty(&phba->work_list)) {
 528                 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
 529                                  evt_listp);
 530                 spin_unlock_irq(&phba->hbalock);
 531                 free_evt = 1;
 532                 switch (evtp->evt) {
 533                 case LPFC_EVT_ELS_RETRY:
 534                         ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
 535                         lpfc_els_retry_delay_handler(ndlp);
 536                         free_evt = 0; /* evt is part of ndlp */
 537                         /* decrement the node reference count held
 538                          * for this queued work
 539                          */
 540                         lpfc_nlp_put(ndlp);
 541                         break;
 542                 case LPFC_EVT_DEV_LOSS:
 543                         ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
 544                         fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
 545                         free_evt = 0;
 546                         /* decrement the node reference count held for
 547                          * this queued work
 548                          */
 549                         nlp_did = ndlp->nlp_DID;
 550                         lpfc_nlp_put(ndlp);
 551                         if (phba->sli_rev == LPFC_SLI_REV4)
 552                                 lpfc_sli4_post_dev_loss_tmo_handler(phba,
 553                                                                     fcf_inuse,
 554                                                                     nlp_did);
 555                         break;
 556                 case LPFC_EVT_ONLINE:
 557                         if (phba->link_state < LPFC_LINK_DOWN)
 558                                 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
 559                         else
 560                                 *(int *) (evtp->evt_arg1) = 0;
 561                         complete((struct completion *)(evtp->evt_arg2));
 562                         break;
 563                 case LPFC_EVT_OFFLINE_PREP:
 564                         if (phba->link_state >= LPFC_LINK_DOWN)
 565                                 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
 566                         *(int *)(evtp->evt_arg1) = 0;
 567                         complete((struct completion *)(evtp->evt_arg2));
 568                         break;
 569                 case LPFC_EVT_OFFLINE:
 570                         lpfc_offline(phba);
 571                         lpfc_sli_brdrestart(phba);
 572                         *(int *)(evtp->evt_arg1) =
 573                                 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
 574                         lpfc_unblock_mgmt_io(phba);
 575                         complete((struct completion *)(evtp->evt_arg2));
 576                         break;
 577                 case LPFC_EVT_WARM_START:
 578                         lpfc_offline(phba);
 579                         lpfc_reset_barrier(phba);
 580                         lpfc_sli_brdreset(phba);
 581                         lpfc_hba_down_post(phba);
 582                         *(int *)(evtp->evt_arg1) =
 583                                 lpfc_sli_brdready(phba, HS_MBRDY);
 584                         lpfc_unblock_mgmt_io(phba);
 585                         complete((struct completion *)(evtp->evt_arg2));
 586                         break;
 587                 case LPFC_EVT_KILL:
 588                         lpfc_offline(phba);
 589                         *(int *)(evtp->evt_arg1)
 590                                 = (phba->pport->stopped)
 591                                         ? 0 : lpfc_sli_brdkill(phba);
 592                         lpfc_unblock_mgmt_io(phba);
 593                         complete((struct completion *)(evtp->evt_arg2));
 594                         break;
 595                 case LPFC_EVT_FASTPATH_MGMT_EVT:
 596                         lpfc_send_fastpath_evt(phba, evtp);
 597                         free_evt = 0;
 598                         break;
 599                 case LPFC_EVT_RESET_HBA:
 600                         if (!(phba->pport->load_flag & FC_UNLOADING))
 601                                 lpfc_reset_hba(phba);
 602                         break;
 603                 }
 604                 if (free_evt)
 605                         kfree(evtp);
 606                 spin_lock_irq(&phba->hbalock);
 607         }
 608         spin_unlock_irq(&phba->hbalock);
 609 
 610 }
 611 
 612 static void
 613 lpfc_work_done(struct lpfc_hba *phba)
 614 {
 615         struct lpfc_sli_ring *pring;
 616         uint32_t ha_copy, status, control, work_port_events;
 617         struct lpfc_vport **vports;
 618         struct lpfc_vport *vport;
 619         int i;
 620 
 621         spin_lock_irq(&phba->hbalock);
 622         ha_copy = phba->work_ha;
 623         phba->work_ha = 0;
 624         spin_unlock_irq(&phba->hbalock);
 625 
 626         /* First, try to post the next mailbox command to SLI4 device */
 627         if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
 628                 lpfc_sli4_post_async_mbox(phba);
 629 
 630         if (ha_copy & HA_ERATT)
 631                 /* Handle the error attention event */
 632                 lpfc_handle_eratt(phba);
 633 
 634         if (ha_copy & HA_MBATT)
 635                 lpfc_sli_handle_mb_event(phba);
 636 
 637         if (ha_copy & HA_LATT)
 638                 lpfc_handle_latt(phba);
 639 
 640         /* Process SLI4 events */
 641         if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
 642                 if (phba->hba_flag & HBA_RRQ_ACTIVE)
 643                         lpfc_handle_rrq_active(phba);
 644                 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
 645                         lpfc_sli4_els_xri_abort_event_proc(phba);
 646                 if (phba->hba_flag & ASYNC_EVENT)
 647                         lpfc_sli4_async_event_proc(phba);
 648                 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
 649                         spin_lock_irq(&phba->hbalock);
 650                         phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
 651                         spin_unlock_irq(&phba->hbalock);
 652                         lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
 653                 }
 654                 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
 655                         lpfc_sli4_fcf_redisc_event_proc(phba);
 656         }
 657 
 658         vports = lpfc_create_vport_work_array(phba);
 659         if (vports != NULL)
 660                 for (i = 0; i <= phba->max_vports; i++) {
 661                         /*
 662                          * We could have no vports in array if unloading, so if
 663                          * this happens then just use the pport
 664                          */
 665                         if (vports[i] == NULL && i == 0)
 666                                 vport = phba->pport;
 667                         else
 668                                 vport = vports[i];
 669                         if (vport == NULL)
 670                                 break;
 671                         spin_lock_irq(&vport->work_port_lock);
 672                         work_port_events = vport->work_port_events;
 673                         vport->work_port_events &= ~work_port_events;
 674                         spin_unlock_irq(&vport->work_port_lock);
 675                         if (work_port_events & WORKER_DISC_TMO)
 676                                 lpfc_disc_timeout_handler(vport);
 677                         if (work_port_events & WORKER_ELS_TMO)
 678                                 lpfc_els_timeout_handler(vport);
 679                         if (work_port_events & WORKER_HB_TMO)
 680                                 lpfc_hb_timeout_handler(phba);
 681                         if (work_port_events & WORKER_MBOX_TMO)
 682                                 lpfc_mbox_timeout_handler(phba);
 683                         if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
 684                                 lpfc_unblock_fabric_iocbs(phba);
 685                         if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
 686                                 lpfc_ramp_down_queue_handler(phba);
 687                         if (work_port_events & WORKER_DELAYED_DISC_TMO)
 688                                 lpfc_delayed_disc_timeout_handler(vport);
 689                 }
 690         lpfc_destroy_vport_work_array(phba, vports);
 691 
 692         pring = lpfc_phba_elsring(phba);
 693         status = (ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
 694         status >>= (4*LPFC_ELS_RING);
 695         if (pring && (status & HA_RXMASK ||
 696                       pring->flag & LPFC_DEFERRED_RING_EVENT ||
 697                       phba->hba_flag & HBA_SP_QUEUE_EVT)) {
 698                 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
 699                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
 700                         /* Preserve legacy behavior. */
 701                         if (!(phba->hba_flag & HBA_SP_QUEUE_EVT))
 702                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
 703                 } else {
 704                         /* Driver could have abort request completed in queue
 705                          * when link goes down.  Allow for this transition.
 706                          */
 707                         if (phba->link_state >= LPFC_LINK_DOWN ||
 708                             phba->link_flag & LS_MDS_LOOPBACK) {
 709                                 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
 710                                 lpfc_sli_handle_slow_ring_event(phba, pring,
 711                                                                 (status &
 712                                                                 HA_RXMASK));
 713                         }
 714                 }
 715                 if (phba->sli_rev == LPFC_SLI_REV4)
 716                         lpfc_drain_txq(phba);
 717                 /*
 718                  * Turn on Ring interrupts
 719                  */
 720                 if (phba->sli_rev <= LPFC_SLI_REV3) {
 721                         spin_lock_irq(&phba->hbalock);
 722                         control = readl(phba->HCregaddr);
 723                         if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
 724                                 lpfc_debugfs_slow_ring_trc(phba,
 725                                         "WRK Enable ring: cntl:x%x hacopy:x%x",
 726                                         control, ha_copy, 0);
 727 
 728                                 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
 729                                 writel(control, phba->HCregaddr);
 730                                 readl(phba->HCregaddr); /* flush */
 731                         } else {
 732                                 lpfc_debugfs_slow_ring_trc(phba,
 733                                         "WRK Ring ok:     cntl:x%x hacopy:x%x",
 734                                         control, ha_copy, 0);
 735                         }
 736                         spin_unlock_irq(&phba->hbalock);
 737                 }
 738         }
 739         lpfc_work_list_done(phba);
 740 }
 741 
 742 int
 743 lpfc_do_work(void *p)
 744 {
 745         struct lpfc_hba *phba = p;
 746         int rc;
 747 
 748         set_user_nice(current, MIN_NICE);
 749         current->flags |= PF_NOFREEZE;
 750         phba->data_flags = 0;
 751 
 752         while (!kthread_should_stop()) {
 753                 /* wait and check worker queue activities */
 754                 rc = wait_event_interruptible(phba->work_waitq,
 755                                         (test_and_clear_bit(LPFC_DATA_READY,
 756                                                             &phba->data_flags)
 757                                          || kthread_should_stop()));
 758                 /* Signal wakeup shall terminate the worker thread */
 759                 if (rc) {
 760                         lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
 761                                         "0433 Wakeup on signal: rc=x%x\n", rc);
 762                         break;
 763                 }
 764 
 765                 /* Attend pending lpfc data processing */
 766                 lpfc_work_done(phba);
 767         }
 768         phba->worker_thread = NULL;
 769         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
 770                         "0432 Worker thread stopped.\n");
 771         return 0;
 772 }
 773 
 774 /*
 775  * This is only called to handle FC worker events. Since this a rare
 776  * occurrence, we allocate a struct lpfc_work_evt structure here instead of
 777  * embedding it in the IOCB.
 778  */
 779 int
 780 lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
 781                       uint32_t evt)
 782 {
 783         struct lpfc_work_evt  *evtp;
 784         unsigned long flags;
 785 
 786         /*
 787          * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
 788          * be queued to worker thread for processing
 789          */
 790         evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
 791         if (!evtp)
 792                 return 0;
 793 
 794         evtp->evt_arg1  = arg1;
 795         evtp->evt_arg2  = arg2;
 796         evtp->evt       = evt;
 797 
 798         spin_lock_irqsave(&phba->hbalock, flags);
 799         list_add_tail(&evtp->evt_listp, &phba->work_list);
 800         spin_unlock_irqrestore(&phba->hbalock, flags);
 801 
 802         lpfc_worker_wake_up(phba);
 803 
 804         return 1;
 805 }
 806 
 807 void
 808 lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
 809 {
 810         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 811         struct lpfc_hba  *phba = vport->phba;
 812         struct lpfc_nodelist *ndlp, *next_ndlp;
 813 
 814         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
 815                 if (!NLP_CHK_NODE_ACT(ndlp))
 816                         continue;
 817                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
 818                         continue;
 819                 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
 820                         ((vport->port_type == LPFC_NPIV_PORT) &&
 821                         (ndlp->nlp_DID == NameServer_DID)))
 822                         lpfc_unreg_rpi(vport, ndlp);
 823 
 824                 /* Leave Fabric nodes alone on link down */
 825                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
 826                     (!remove && ndlp->nlp_type & NLP_FABRIC))
 827                         continue;
 828                 lpfc_disc_state_machine(vport, ndlp, NULL,
 829                                         remove
 830                                         ? NLP_EVT_DEVICE_RM
 831                                         : NLP_EVT_DEVICE_RECOVERY);
 832         }
 833         if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
 834                 if (phba->sli_rev == LPFC_SLI_REV4)
 835                         lpfc_sli4_unreg_all_rpis(vport);
 836                 lpfc_mbx_unreg_vpi(vport);
 837                 spin_lock_irq(shost->host_lock);
 838                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
 839                 spin_unlock_irq(shost->host_lock);
 840         }
 841 }
 842 
 843 void
 844 lpfc_port_link_failure(struct lpfc_vport *vport)
 845 {
 846         lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
 847 
 848         /* Cleanup any outstanding received buffers */
 849         lpfc_cleanup_rcv_buffers(vport);
 850 
 851         /* Cleanup any outstanding RSCN activity */
 852         lpfc_els_flush_rscn(vport);
 853 
 854         /* Cleanup any outstanding ELS commands */
 855         lpfc_els_flush_cmd(vport);
 856 
 857         lpfc_cleanup_rpis(vport, 0);
 858 
 859         /* Turn off discovery timer if its running */
 860         lpfc_can_disctmo(vport);
 861 }
 862 
 863 void
 864 lpfc_linkdown_port(struct lpfc_vport *vport)
 865 {
 866         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
 867 
 868         if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
 869                 fc_host_post_event(shost, fc_get_event_number(),
 870                                    FCH_EVT_LINKDOWN, 0);
 871 
 872         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
 873                 "Link Down:       state:x%x rtry:x%x flg:x%x",
 874                 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
 875 
 876         lpfc_port_link_failure(vport);
 877 
 878         /* Stop delayed Nport discovery */
 879         spin_lock_irq(shost->host_lock);
 880         vport->fc_flag &= ~FC_DISC_DELAYED;
 881         spin_unlock_irq(shost->host_lock);
 882         del_timer_sync(&vport->delayed_disc_tmo);
 883 }
 884 
 885 int
 886 lpfc_linkdown(struct lpfc_hba *phba)
 887 {
 888         struct lpfc_vport *vport = phba->pport;
 889         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
 890         struct lpfc_vport **vports;
 891         LPFC_MBOXQ_t          *mb;
 892         int i;
 893 
 894         if (phba->link_state == LPFC_LINK_DOWN)
 895                 return 0;
 896 
 897         /* Block all SCSI stack I/Os */
 898         lpfc_scsi_dev_block(phba);
 899 
 900         phba->defer_flogi_acc_flag = false;
 901 
 902         spin_lock_irq(&phba->hbalock);
 903         phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
 904         spin_unlock_irq(&phba->hbalock);
 905         if (phba->link_state > LPFC_LINK_DOWN) {
 906                 phba->link_state = LPFC_LINK_DOWN;
 907                 if (phba->sli4_hba.conf_trunk) {
 908                         phba->trunk_link.link0.state = 0;
 909                         phba->trunk_link.link1.state = 0;
 910                         phba->trunk_link.link2.state = 0;
 911                         phba->trunk_link.link3.state = 0;
 912                         phba->sli4_hba.link_state.logical_speed =
 913                                                 LPFC_LINK_SPEED_UNKNOWN;
 914                 }
 915                 spin_lock_irq(shost->host_lock);
 916                 phba->pport->fc_flag &= ~FC_LBIT;
 917                 spin_unlock_irq(shost->host_lock);
 918         }
 919         vports = lpfc_create_vport_work_array(phba);
 920         if (vports != NULL) {
 921                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
 922                         /* Issue a LINK DOWN event to all nodes */
 923                         lpfc_linkdown_port(vports[i]);
 924 
 925                         vports[i]->fc_myDID = 0;
 926 
 927                         if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
 928                             (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
 929                                 if (phba->nvmet_support)
 930                                         lpfc_nvmet_update_targetport(phba);
 931                                 else
 932                                         lpfc_nvme_update_localport(vports[i]);
 933                         }
 934                 }
 935         }
 936         lpfc_destroy_vport_work_array(phba, vports);
 937 
 938         /* Clean up any SLI3 firmware default rpi's */
 939         if (phba->sli_rev > LPFC_SLI_REV3)
 940                 goto skip_unreg_did;
 941 
 942         mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 943         if (mb) {
 944                 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
 945                 mb->vport = vport;
 946                 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
 947                 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
 948                     == MBX_NOT_FINISHED) {
 949                         mempool_free(mb, phba->mbox_mem_pool);
 950                 }
 951         }
 952 
 953  skip_unreg_did:
 954         /* Setup myDID for link up if we are in pt2pt mode */
 955         if (phba->pport->fc_flag & FC_PT2PT) {
 956                 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 957                 if (mb) {
 958                         lpfc_config_link(phba, mb);
 959                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
 960                         mb->vport = vport;
 961                         if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
 962                             == MBX_NOT_FINISHED) {
 963                                 mempool_free(mb, phba->mbox_mem_pool);
 964                         }
 965                 }
 966                 spin_lock_irq(shost->host_lock);
 967                 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
 968                 phba->pport->rcv_flogi_cnt = 0;
 969                 spin_unlock_irq(shost->host_lock);
 970         }
 971         return 0;
 972 }
 973 
 974 static void
 975 lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
 976 {
 977         struct lpfc_nodelist *ndlp;
 978 
 979         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
 980                 ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
 981                 if (!NLP_CHK_NODE_ACT(ndlp))
 982                         continue;
 983                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
 984                         continue;
 985                 if (ndlp->nlp_type & NLP_FABRIC) {
 986                         /* On Linkup its safe to clean up the ndlp
 987                          * from Fabric connections.
 988                          */
 989                         if (ndlp->nlp_DID != Fabric_DID)
 990                                 lpfc_unreg_rpi(vport, ndlp);
 991                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
 992                 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
 993                         /* Fail outstanding IO now since device is
 994                          * marked for PLOGI.
 995                          */
 996                         lpfc_unreg_rpi(vport, ndlp);
 997                 }
 998         }
 999 }
1000 
1001 static void
1002 lpfc_linkup_port(struct lpfc_vport *vport)
1003 {
1004         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1005         struct lpfc_hba  *phba = vport->phba;
1006 
1007         if ((vport->load_flag & FC_UNLOADING) != 0)
1008                 return;
1009 
1010         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1011                 "Link Up:         top:x%x speed:x%x flg:x%x",
1012                 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
1013 
1014         /* If NPIV is not enabled, only bring the physical port up */
1015         if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1016                 (vport != phba->pport))
1017                 return;
1018 
1019         if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1020                 fc_host_post_event(shost, fc_get_event_number(),
1021                                    FCH_EVT_LINKUP, 0);
1022 
1023         spin_lock_irq(shost->host_lock);
1024         vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
1025                             FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1026         vport->fc_flag |= FC_NDISC_ACTIVE;
1027         vport->fc_ns_retry = 0;
1028         spin_unlock_irq(shost->host_lock);
1029 
1030         if (vport->fc_flag & FC_LBIT)
1031                 lpfc_linkup_cleanup_nodes(vport);
1032 
1033 }
1034 
1035 static int
1036 lpfc_linkup(struct lpfc_hba *phba)
1037 {
1038         struct lpfc_vport **vports;
1039         int i;
1040         struct Scsi_Host  *shost = lpfc_shost_from_vport(phba->pport);
1041 
1042         phba->link_state = LPFC_LINK_UP;
1043 
1044         /* Unblock fabric iocbs if they are blocked */
1045         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1046         del_timer_sync(&phba->fabric_block_timer);
1047 
1048         vports = lpfc_create_vport_work_array(phba);
1049         if (vports != NULL)
1050                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1051                         lpfc_linkup_port(vports[i]);
1052         lpfc_destroy_vport_work_array(phba, vports);
1053 
1054         /* Clear the pport flogi counter in case the link down was
1055          * absorbed without an ACQE. No lock here - in worker thread
1056          * and discovery is synchronized.
1057          */
1058         spin_lock_irq(shost->host_lock);
1059         phba->pport->rcv_flogi_cnt = 0;
1060         spin_unlock_irq(shost->host_lock);
1061 
1062         /* reinitialize initial FLOGI flag */
1063         phba->hba_flag &= ~(HBA_FLOGI_ISSUED);
1064         phba->defer_flogi_acc_flag = false;
1065 
1066         return 0;
1067 }
1068 
1069 /*
1070  * This routine handles processing a CLEAR_LA mailbox
1071  * command upon completion. It is setup in the LPFC_MBOXQ
1072  * as the completion routine when the command is
1073  * handed off to the SLI layer. SLI3 only.
1074  */
1075 static void
1076 lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1077 {
1078         struct lpfc_vport *vport = pmb->vport;
1079         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1080         struct lpfc_sli   *psli = &phba->sli;
1081         MAILBOX_t *mb = &pmb->u.mb;
1082         uint32_t control;
1083 
1084         /* Since we don't do discovery right now, turn these off here */
1085         psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1086         psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1087 
1088         /* Check for error */
1089         if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
1090                 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
1091                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1092                                  "0320 CLEAR_LA mbxStatus error x%x hba "
1093                                  "state x%x\n",
1094                                  mb->mbxStatus, vport->port_state);
1095                 phba->link_state = LPFC_HBA_ERROR;
1096                 goto out;
1097         }
1098 
1099         if (vport->port_type == LPFC_PHYSICAL_PORT)
1100                 phba->link_state = LPFC_HBA_READY;
1101 
1102         spin_lock_irq(&phba->hbalock);
1103         psli->sli_flag |= LPFC_PROCESS_LA;
1104         control = readl(phba->HCregaddr);
1105         control |= HC_LAINT_ENA;
1106         writel(control, phba->HCregaddr);
1107         readl(phba->HCregaddr); /* flush */
1108         spin_unlock_irq(&phba->hbalock);
1109         mempool_free(pmb, phba->mbox_mem_pool);
1110         return;
1111 
1112 out:
1113         /* Device Discovery completes */
1114         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1115                          "0225 Device Discovery completes\n");
1116         mempool_free(pmb, phba->mbox_mem_pool);
1117 
1118         spin_lock_irq(shost->host_lock);
1119         vport->fc_flag &= ~FC_ABORT_DISCOVERY;
1120         spin_unlock_irq(shost->host_lock);
1121 
1122         lpfc_can_disctmo(vport);
1123 
1124         /* turn on Link Attention interrupts */
1125 
1126         spin_lock_irq(&phba->hbalock);
1127         psli->sli_flag |= LPFC_PROCESS_LA;
1128         control = readl(phba->HCregaddr);
1129         control |= HC_LAINT_ENA;
1130         writel(control, phba->HCregaddr);
1131         readl(phba->HCregaddr); /* flush */
1132         spin_unlock_irq(&phba->hbalock);
1133 
1134         return;
1135 }
1136 
1137 
1138 void
1139 lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1140 {
1141         struct lpfc_vport *vport = pmb->vport;
1142 
1143         if (pmb->u.mb.mbxStatus)
1144                 goto out;
1145 
1146         mempool_free(pmb, phba->mbox_mem_pool);
1147 
1148         /* don't perform discovery for SLI4 loopback diagnostic test */
1149         if ((phba->sli_rev == LPFC_SLI_REV4) &&
1150             !(phba->hba_flag & HBA_FCOE_MODE) &&
1151             (phba->link_flag & LS_LOOPBACK_MODE))
1152                 return;
1153 
1154         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
1155             vport->fc_flag & FC_PUBLIC_LOOP &&
1156             !(vport->fc_flag & FC_LBIT)) {
1157                         /* Need to wait for FAN - use discovery timer
1158                          * for timeout.  port_state is identically
1159                          * LPFC_LOCAL_CFG_LINK while waiting for FAN
1160                          */
1161                         lpfc_set_disctmo(vport);
1162                         return;
1163         }
1164 
1165         /* Start discovery by sending a FLOGI. port_state is identically
1166          * LPFC_FLOGI while waiting for FLOGI cmpl. Check if sending
1167          * the FLOGI is being deferred till after MBX_READ_SPARAM completes.
1168          */
1169         if (vport->port_state != LPFC_FLOGI) {
1170                 if (!(phba->hba_flag & HBA_DEFER_FLOGI))
1171                         lpfc_initial_flogi(vport);
1172         } else {
1173                 if (vport->fc_flag & FC_PT2PT)
1174                         lpfc_disc_start(vport);
1175         }
1176         return;
1177 
1178 out:
1179         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1180                          "0306 CONFIG_LINK mbxStatus error x%x "
1181                          "HBA state x%x\n",
1182                          pmb->u.mb.mbxStatus, vport->port_state);
1183         mempool_free(pmb, phba->mbox_mem_pool);
1184 
1185         lpfc_linkdown(phba);
1186 
1187         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1188                          "0200 CONFIG_LINK bad hba state x%x\n",
1189                          vport->port_state);
1190 
1191         lpfc_issue_clear_la(phba, vport);
1192         return;
1193 }
1194 
1195 /**
1196  * lpfc_sli4_clear_fcf_rr_bmask
1197  * @phba pointer to the struct lpfc_hba for this port.
1198  * This fucnction resets the round robin bit mask and clears the
1199  * fcf priority list. The list deletions are done while holding the
1200  * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1201  * from the lpfc_fcf_pri record.
1202  **/
1203 void
1204 lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1205 {
1206         struct lpfc_fcf_pri *fcf_pri;
1207         struct lpfc_fcf_pri *next_fcf_pri;
1208         memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1209         spin_lock_irq(&phba->hbalock);
1210         list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1211                                 &phba->fcf.fcf_pri_list, list) {
1212                 list_del_init(&fcf_pri->list);
1213                 fcf_pri->fcf_rec.flag = 0;
1214         }
1215         spin_unlock_irq(&phba->hbalock);
1216 }
1217 static void
1218 lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1219 {
1220         struct lpfc_vport *vport = mboxq->vport;
1221 
1222         if (mboxq->u.mb.mbxStatus) {
1223                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1224                          "2017 REG_FCFI mbxStatus error x%x "
1225                          "HBA state x%x\n",
1226                          mboxq->u.mb.mbxStatus, vport->port_state);
1227                 goto fail_out;
1228         }
1229 
1230         /* Start FCoE discovery by sending a FLOGI. */
1231         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1232         /* Set the FCFI registered flag */
1233         spin_lock_irq(&phba->hbalock);
1234         phba->fcf.fcf_flag |= FCF_REGISTERED;
1235         spin_unlock_irq(&phba->hbalock);
1236 
1237         /* If there is a pending FCoE event, restart FCF table scan. */
1238         if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1239                 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
1240                 goto fail_out;
1241 
1242         /* Mark successful completion of FCF table scan */
1243         spin_lock_irq(&phba->hbalock);
1244         phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1245         phba->hba_flag &= ~FCF_TS_INPROG;
1246         if (vport->port_state != LPFC_FLOGI) {
1247                 phba->hba_flag |= FCF_RR_INPROG;
1248                 spin_unlock_irq(&phba->hbalock);
1249                 lpfc_issue_init_vfi(vport);
1250                 goto out;
1251         }
1252         spin_unlock_irq(&phba->hbalock);
1253         goto out;
1254 
1255 fail_out:
1256         spin_lock_irq(&phba->hbalock);
1257         phba->hba_flag &= ~FCF_RR_INPROG;
1258         spin_unlock_irq(&phba->hbalock);
1259 out:
1260         mempool_free(mboxq, phba->mbox_mem_pool);
1261 }
1262 
1263 /**
1264  * lpfc_fab_name_match - Check if the fcf fabric name match.
1265  * @fab_name: pointer to fabric name.
1266  * @new_fcf_record: pointer to fcf record.
1267  *
1268  * This routine compare the fcf record's fabric name with provided
1269  * fabric name. If the fabric name are identical this function
1270  * returns 1 else return 0.
1271  **/
1272 static uint32_t
1273 lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1274 {
1275         if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
1276                 return 0;
1277         if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1278                 return 0;
1279         if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1280                 return 0;
1281         if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1282                 return 0;
1283         if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1284                 return 0;
1285         if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1286                 return 0;
1287         if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1288                 return 0;
1289         if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1290                 return 0;
1291         return 1;
1292 }
1293 
1294 /**
1295  * lpfc_sw_name_match - Check if the fcf switch name match.
1296  * @fab_name: pointer to fabric name.
1297  * @new_fcf_record: pointer to fcf record.
1298  *
1299  * This routine compare the fcf record's switch name with provided
1300  * switch name. If the switch name are identical this function
1301  * returns 1 else return 0.
1302  **/
1303 static uint32_t
1304 lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1305 {
1306         if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
1307                 return 0;
1308         if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1309                 return 0;
1310         if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1311                 return 0;
1312         if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1313                 return 0;
1314         if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1315                 return 0;
1316         if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1317                 return 0;
1318         if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1319                 return 0;
1320         if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1321                 return 0;
1322         return 1;
1323 }
1324 
1325 /**
1326  * lpfc_mac_addr_match - Check if the fcf mac address match.
1327  * @mac_addr: pointer to mac address.
1328  * @new_fcf_record: pointer to fcf record.
1329  *
1330  * This routine compare the fcf record's mac address with HBA's
1331  * FCF mac address. If the mac addresses are identical this function
1332  * returns 1 else return 0.
1333  **/
1334 static uint32_t
1335 lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
1336 {
1337         if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
1338                 return 0;
1339         if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1340                 return 0;
1341         if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1342                 return 0;
1343         if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1344                 return 0;
1345         if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1346                 return 0;
1347         if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1348                 return 0;
1349         return 1;
1350 }
1351 
1352 static bool
1353 lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1354 {
1355         return (curr_vlan_id == new_vlan_id);
1356 }
1357 
1358 /**
1359  * lpfc_update_fcf_record - Update driver fcf record
1360  * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1361  * @phba: pointer to lpfc hba data structure.
1362  * @fcf_index: Index for the lpfc_fcf_record.
1363  * @new_fcf_record: pointer to hba fcf record.
1364  *
1365  * This routine updates the driver FCF priority record from the new HBA FCF
1366  * record. This routine is called with the host lock held.
1367  **/
1368 static void
1369 __lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1370                                  struct fcf_record *new_fcf_record
1371                                  )
1372 {
1373         struct lpfc_fcf_pri *fcf_pri;
1374 
1375         lockdep_assert_held(&phba->hbalock);
1376 
1377         fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1378         fcf_pri->fcf_rec.fcf_index = fcf_index;
1379         /* FCF record priority */
1380         fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1381 
1382 }
1383 
1384 /**
1385  * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1386  * @fcf: pointer to driver fcf record.
1387  * @new_fcf_record: pointer to fcf record.
1388  *
1389  * This routine copies the FCF information from the FCF
1390  * record to lpfc_hba data structure.
1391  **/
1392 static void
1393 lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1394                      struct fcf_record *new_fcf_record)
1395 {
1396         /* Fabric name */
1397         fcf_rec->fabric_name[0] =
1398                 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1399         fcf_rec->fabric_name[1] =
1400                 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1401         fcf_rec->fabric_name[2] =
1402                 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1403         fcf_rec->fabric_name[3] =
1404                 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1405         fcf_rec->fabric_name[4] =
1406                 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1407         fcf_rec->fabric_name[5] =
1408                 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1409         fcf_rec->fabric_name[6] =
1410                 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1411         fcf_rec->fabric_name[7] =
1412                 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1413         /* Mac address */
1414         fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1415         fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1416         fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1417         fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1418         fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1419         fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1420         /* FCF record index */
1421         fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1422         /* FCF record priority */
1423         fcf_rec->priority = new_fcf_record->fip_priority;
1424         /* Switch name */
1425         fcf_rec->switch_name[0] =
1426                 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1427         fcf_rec->switch_name[1] =
1428                 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1429         fcf_rec->switch_name[2] =
1430                 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1431         fcf_rec->switch_name[3] =
1432                 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1433         fcf_rec->switch_name[4] =
1434                 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1435         fcf_rec->switch_name[5] =
1436                 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1437         fcf_rec->switch_name[6] =
1438                 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1439         fcf_rec->switch_name[7] =
1440                 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
1441 }
1442 
1443 /**
1444  * lpfc_update_fcf_record - Update driver fcf record
1445  * @phba: pointer to lpfc hba data structure.
1446  * @fcf_rec: pointer to driver fcf record.
1447  * @new_fcf_record: pointer to hba fcf record.
1448  * @addr_mode: address mode to be set to the driver fcf record.
1449  * @vlan_id: vlan tag to be set to the driver fcf record.
1450  * @flag: flag bits to be set to the driver fcf record.
1451  *
1452  * This routine updates the driver FCF record from the new HBA FCF record
1453  * together with the address mode, vlan_id, and other informations. This
1454  * routine is called with the host lock held.
1455  **/
1456 static void
1457 __lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1458                        struct fcf_record *new_fcf_record, uint32_t addr_mode,
1459                        uint16_t vlan_id, uint32_t flag)
1460 {
1461         lockdep_assert_held(&phba->hbalock);
1462 
1463         /* Copy the fields from the HBA's FCF record */
1464         lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1465         /* Update other fields of driver FCF record */
1466         fcf_rec->addr_mode = addr_mode;
1467         fcf_rec->vlan_id = vlan_id;
1468         fcf_rec->flag |= (flag | RECORD_VALID);
1469         __lpfc_update_fcf_record_pri(phba,
1470                 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1471                                  new_fcf_record);
1472 }
1473 
1474 /**
1475  * lpfc_register_fcf - Register the FCF with hba.
1476  * @phba: pointer to lpfc hba data structure.
1477  *
1478  * This routine issues a register fcfi mailbox command to register
1479  * the fcf with HBA.
1480  **/
1481 static void
1482 lpfc_register_fcf(struct lpfc_hba *phba)
1483 {
1484         LPFC_MBOXQ_t *fcf_mbxq;
1485         int rc;
1486 
1487         spin_lock_irq(&phba->hbalock);
1488         /* If the FCF is not available do nothing. */
1489         if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
1490                 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1491                 spin_unlock_irq(&phba->hbalock);
1492                 return;
1493         }
1494 
1495         /* The FCF is already registered, start discovery */
1496         if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1497                 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1498                 phba->hba_flag &= ~FCF_TS_INPROG;
1499                 if (phba->pport->port_state != LPFC_FLOGI &&
1500                     phba->pport->fc_flag & FC_FABRIC) {
1501                         phba->hba_flag |= FCF_RR_INPROG;
1502                         spin_unlock_irq(&phba->hbalock);
1503                         lpfc_initial_flogi(phba->pport);
1504                         return;
1505                 }
1506                 spin_unlock_irq(&phba->hbalock);
1507                 return;
1508         }
1509         spin_unlock_irq(&phba->hbalock);
1510 
1511         fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1512         if (!fcf_mbxq) {
1513                 spin_lock_irq(&phba->hbalock);
1514                 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1515                 spin_unlock_irq(&phba->hbalock);
1516                 return;
1517         }
1518 
1519         lpfc_reg_fcfi(phba, fcf_mbxq);
1520         fcf_mbxq->vport = phba->pport;
1521         fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1522         rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
1523         if (rc == MBX_NOT_FINISHED) {
1524                 spin_lock_irq(&phba->hbalock);
1525                 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1526                 spin_unlock_irq(&phba->hbalock);
1527                 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
1528         }
1529 
1530         return;
1531 }
1532 
1533 /**
1534  * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1535  * @phba: pointer to lpfc hba data structure.
1536  * @new_fcf_record: pointer to fcf record.
1537  * @boot_flag: Indicates if this record used by boot bios.
1538  * @addr_mode: The address mode to be used by this FCF
1539  * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
1540  *
1541  * This routine compare the fcf record with connect list obtained from the
1542  * config region to decide if this FCF can be used for SAN discovery. It returns
1543  * 1 if this record can be used for SAN discovery else return zero. If this FCF
1544  * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1545  * is used by boot bios and addr_mode will indicate the addressing mode to be
1546  * used for this FCF when the function returns.
1547  * If the FCF record need to be used with a particular vlan id, the vlan is
1548  * set in the vlan_id on return of the function. If not VLAN tagging need to
1549  * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
1550  **/
1551 static int
1552 lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1553                         struct fcf_record *new_fcf_record,
1554                         uint32_t *boot_flag, uint32_t *addr_mode,
1555                         uint16_t *vlan_id)
1556 {
1557         struct lpfc_fcf_conn_entry *conn_entry;
1558         int i, j, fcf_vlan_id = 0;
1559 
1560         /* Find the lowest VLAN id in the FCF record */
1561         for (i = 0; i < 512; i++) {
1562                 if (new_fcf_record->vlan_bitmap[i]) {
1563                         fcf_vlan_id = i * 8;
1564                         j = 0;
1565                         while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1566                                 j++;
1567                                 fcf_vlan_id++;
1568                         }
1569                         break;
1570                 }
1571         }
1572 
1573         /* FCF not valid/available or solicitation in progress */
1574         if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1575             !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1576             bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
1577                 return 0;
1578 
1579         if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
1580                 *boot_flag = 0;
1581                 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1582                                 new_fcf_record);
1583                 if (phba->valid_vlan)
1584                         *vlan_id = phba->vlan_id;
1585                 else
1586                         *vlan_id = LPFC_FCOE_NULL_VID;
1587                 return 1;
1588         }
1589 
1590         /*
1591          * If there are no FCF connection table entry, driver connect to all
1592          * FCFs.
1593          */
1594         if (list_empty(&phba->fcf_conn_rec_list)) {
1595                 *boot_flag = 0;
1596                 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1597                         new_fcf_record);
1598 
1599                 /*
1600                  * When there are no FCF connect entries, use driver's default
1601                  * addressing mode - FPMA.
1602                  */
1603                 if (*addr_mode & LPFC_FCF_FPMA)
1604                         *addr_mode = LPFC_FCF_FPMA;
1605 
1606                 /* If FCF record report a vlan id use that vlan id */
1607                 if (fcf_vlan_id)
1608                         *vlan_id = fcf_vlan_id;
1609                 else
1610                         *vlan_id = LPFC_FCOE_NULL_VID;
1611                 return 1;
1612         }
1613 
1614         list_for_each_entry(conn_entry,
1615                             &phba->fcf_conn_rec_list, list) {
1616                 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1617                         continue;
1618 
1619                 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1620                         !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
1621                                              new_fcf_record))
1622                         continue;
1623                 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1624                         !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1625                                             new_fcf_record))
1626                         continue;
1627                 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1628                         /*
1629                          * If the vlan bit map does not have the bit set for the
1630                          * vlan id to be used, then it is not a match.
1631                          */
1632                         if (!(new_fcf_record->vlan_bitmap
1633                                 [conn_entry->conn_rec.vlan_tag / 8] &
1634                                 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1635                                 continue;
1636                 }
1637 
1638                 /*
1639                  * If connection record does not support any addressing mode,
1640                  * skip the FCF record.
1641                  */
1642                 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1643                         & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1644                         continue;
1645 
1646                 /*
1647                  * Check if the connection record specifies a required
1648                  * addressing mode.
1649                  */
1650                 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1651                         !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1652 
1653                         /*
1654                          * If SPMA required but FCF not support this continue.
1655                          */
1656                         if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1657                                 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1658                                         new_fcf_record) & LPFC_FCF_SPMA))
1659                                 continue;
1660 
1661                         /*
1662                          * If FPMA required but FCF not support this continue.
1663                          */
1664                         if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1665                                 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1666                                 new_fcf_record) & LPFC_FCF_FPMA))
1667                                 continue;
1668                 }
1669 
1670                 /*
1671                  * This fcf record matches filtering criteria.
1672                  */
1673                 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1674                         *boot_flag = 1;
1675                 else
1676                         *boot_flag = 0;
1677 
1678                 /*
1679                  * If user did not specify any addressing mode, or if the
1680                  * preferred addressing mode specified by user is not supported
1681                  * by FCF, allow fabric to pick the addressing mode.
1682                  */
1683                 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1684                                 new_fcf_record);
1685                 /*
1686                  * If the user specified a required address mode, assign that
1687                  * address mode
1688                  */
1689                 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1690                         (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1691                         *addr_mode = (conn_entry->conn_rec.flags &
1692                                 FCFCNCT_AM_SPMA) ?
1693                                 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1694                 /*
1695                  * If the user specified a preferred address mode, use the
1696                  * addr mode only if FCF support the addr_mode.
1697                  */
1698                 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1699                         (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1700                         (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1701                         (*addr_mode & LPFC_FCF_SPMA))
1702                                 *addr_mode = LPFC_FCF_SPMA;
1703                 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1704                         (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1705                         !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1706                         (*addr_mode & LPFC_FCF_FPMA))
1707                                 *addr_mode = LPFC_FCF_FPMA;
1708 
1709                 /* If matching connect list has a vlan id, use it */
1710                 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1711                         *vlan_id = conn_entry->conn_rec.vlan_tag;
1712                 /*
1713                  * If no vlan id is specified in connect list, use the vlan id
1714                  * in the FCF record
1715                  */
1716                 else if (fcf_vlan_id)
1717                         *vlan_id = fcf_vlan_id;
1718                 else
1719                         *vlan_id = LPFC_FCOE_NULL_VID;
1720 
1721                 return 1;
1722         }
1723 
1724         return 0;
1725 }
1726 
1727 /**
1728  * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1729  * @phba: pointer to lpfc hba data structure.
1730  * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1731  *
1732  * This function check if there is any fcoe event pending while driver
1733  * scan FCF entries. If there is any pending event, it will restart the
1734  * FCF saning and return 1 else return 0.
1735  */
1736 int
1737 lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1738 {
1739         /*
1740          * If the Link is up and no FCoE events while in the
1741          * FCF discovery, no need to restart FCF discovery.
1742          */
1743         if ((phba->link_state  >= LPFC_LINK_UP) &&
1744             (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1745                 return 0;
1746 
1747         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1748                         "2768 Pending link or FCF event during current "
1749                         "handling of the previous event: link_state:x%x, "
1750                         "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1751                         phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1752                         phba->fcoe_eventtag);
1753 
1754         spin_lock_irq(&phba->hbalock);
1755         phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1756         spin_unlock_irq(&phba->hbalock);
1757 
1758         if (phba->link_state >= LPFC_LINK_UP) {
1759                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1760                                 "2780 Restart FCF table scan due to "
1761                                 "pending FCF event:evt_tag_at_scan:x%x, "
1762                                 "evt_tag_current:x%x\n",
1763                                 phba->fcoe_eventtag_at_fcf_scan,
1764                                 phba->fcoe_eventtag);
1765                 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1766         } else {
1767                 /*
1768                  * Do not continue FCF discovery and clear FCF_TS_INPROG
1769                  * flag
1770                  */
1771                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1772                                 "2833 Stop FCF discovery process due to link "
1773                                 "state change (x%x)\n", phba->link_state);
1774                 spin_lock_irq(&phba->hbalock);
1775                 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1776                 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
1777                 spin_unlock_irq(&phba->hbalock);
1778         }
1779 
1780         /* Unregister the currently registered FCF if required */
1781         if (unreg_fcf) {
1782                 spin_lock_irq(&phba->hbalock);
1783                 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1784                 spin_unlock_irq(&phba->hbalock);
1785                 lpfc_sli4_unregister_fcf(phba);
1786         }
1787         return 1;
1788 }
1789 
1790 /**
1791  * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1792  * @phba: pointer to lpfc hba data structure.
1793  * @fcf_cnt: number of eligible fcf record seen so far.
1794  *
1795  * This function makes an running random selection decision on FCF record to
1796  * use through a sequence of @fcf_cnt eligible FCF records with equal
1797  * probability. To perform integer manunipulation of random numbers with
1798  * size unit32_t, the lower 16 bits of the 32-bit random number returned
1799  * from prandom_u32() are taken as the random random number generated.
1800  *
1801  * Returns true when outcome is for the newly read FCF record should be
1802  * chosen; otherwise, return false when outcome is for keeping the previously
1803  * chosen FCF record.
1804  **/
1805 static bool
1806 lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1807 {
1808         uint32_t rand_num;
1809 
1810         /* Get 16-bit uniform random number */
1811         rand_num = 0xFFFF & prandom_u32();
1812 
1813         /* Decision with probability 1/fcf_cnt */
1814         if ((fcf_cnt * rand_num) < 0xFFFF)
1815                 return true;
1816         else
1817                 return false;
1818 }
1819 
1820 /**
1821  * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
1822  * @phba: pointer to lpfc hba data structure.
1823  * @mboxq: pointer to mailbox object.
1824  * @next_fcf_index: pointer to holder of next fcf index.
1825  *
1826  * This routine parses the non-embedded fcf mailbox command by performing the
1827  * necessarily error checking, non-embedded read FCF record mailbox command
1828  * SGE parsing, and endianness swapping.
1829  *
1830  * Returns the pointer to the new FCF record in the non-embedded mailbox
1831  * command DMA memory if successfully, other NULL.
1832  */
1833 static struct fcf_record *
1834 lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1835                              uint16_t *next_fcf_index)
1836 {
1837         void *virt_addr;
1838         struct lpfc_mbx_sge sge;
1839         struct lpfc_mbx_read_fcf_tbl *read_fcf;
1840         uint32_t shdr_status, shdr_add_status, if_type;
1841         union lpfc_sli4_cfg_shdr *shdr;
1842         struct fcf_record *new_fcf_record;
1843 
1844         /* Get the first SGE entry from the non-embedded DMA memory. This
1845          * routine only uses a single SGE.
1846          */
1847         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1848         if (unlikely(!mboxq->sge_array)) {
1849                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1850                                 "2524 Failed to get the non-embedded SGE "
1851                                 "virtual address\n");
1852                 return NULL;
1853         }
1854         virt_addr = mboxq->sge_array->addr[0];
1855 
1856         shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1857         lpfc_sli_pcimem_bcopy(shdr, shdr,
1858                               sizeof(union lpfc_sli4_cfg_shdr));
1859         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1860         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1861         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
1862         if (shdr_status || shdr_add_status) {
1863                 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1864                                         if_type == LPFC_SLI_INTF_IF_TYPE_2)
1865                         lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
1866                                         "2726 READ_FCF_RECORD Indicates empty "
1867                                         "FCF table.\n");
1868                 else
1869                         lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
1870                                         "2521 READ_FCF_RECORD mailbox failed "
1871                                         "with status x%x add_status x%x, "
1872                                         "mbx\n", shdr_status, shdr_add_status);
1873                 return NULL;
1874         }
1875 
1876         /* Interpreting the returned information of the FCF record */
1877         read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1878         lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1879                               sizeof(struct lpfc_mbx_read_fcf_tbl));
1880         *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1881         new_fcf_record = (struct fcf_record *)(virt_addr +
1882                           sizeof(struct lpfc_mbx_read_fcf_tbl));
1883         lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1884                                 offsetof(struct fcf_record, vlan_bitmap));
1885         new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1886         new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
1887 
1888         return new_fcf_record;
1889 }
1890 
1891 /**
1892  * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1893  * @phba: pointer to lpfc hba data structure.
1894  * @fcf_record: pointer to the fcf record.
1895  * @vlan_id: the lowest vlan identifier associated to this fcf record.
1896  * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1897  *
1898  * This routine logs the detailed FCF record if the LOG_FIP loggin is
1899  * enabled.
1900  **/
1901 static void
1902 lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1903                               struct fcf_record *fcf_record,
1904                               uint16_t vlan_id,
1905                               uint16_t next_fcf_index)
1906 {
1907         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1908                         "2764 READ_FCF_RECORD:\n"
1909                         "\tFCF_Index     : x%x\n"
1910                         "\tFCF_Avail     : x%x\n"
1911                         "\tFCF_Valid     : x%x\n"
1912                         "\tFCF_SOL       : x%x\n"
1913                         "\tFIP_Priority  : x%x\n"
1914                         "\tMAC_Provider  : x%x\n"
1915                         "\tLowest VLANID : x%x\n"
1916                         "\tFCF_MAC Addr  : x%x:%x:%x:%x:%x:%x\n"
1917                         "\tFabric_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1918                         "\tSwitch_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1919                         "\tNext_FCF_Index: x%x\n",
1920                         bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1921                         bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1922                         bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1923                         bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
1924                         fcf_record->fip_priority,
1925                         bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1926                         vlan_id,
1927                         bf_get(lpfc_fcf_record_mac_0, fcf_record),
1928                         bf_get(lpfc_fcf_record_mac_1, fcf_record),
1929                         bf_get(lpfc_fcf_record_mac_2, fcf_record),
1930                         bf_get(lpfc_fcf_record_mac_3, fcf_record),
1931                         bf_get(lpfc_fcf_record_mac_4, fcf_record),
1932                         bf_get(lpfc_fcf_record_mac_5, fcf_record),
1933                         bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1934                         bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1935                         bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1936                         bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1937                         bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1938                         bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1939                         bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1940                         bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1941                         bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1942                         bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1943                         bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1944                         bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1945                         bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1946                         bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1947                         bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1948                         bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1949                         next_fcf_index);
1950 }
1951 
1952 /**
1953  lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1954  * @phba: pointer to lpfc hba data structure.
1955  * @fcf_rec: pointer to an existing FCF record.
1956  * @new_fcf_record: pointer to a new FCF record.
1957  * @new_vlan_id: vlan id from the new FCF record.
1958  *
1959  * This function performs matching test of a new FCF record against an existing
1960  * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1961  * will not be used as part of the FCF record matching criteria.
1962  *
1963  * Returns true if all the fields matching, otherwise returns false.
1964  */
1965 static bool
1966 lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1967                            struct lpfc_fcf_rec *fcf_rec,
1968                            struct fcf_record *new_fcf_record,
1969                            uint16_t new_vlan_id)
1970 {
1971         if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1972                 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1973                         return false;
1974         if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1975                 return false;
1976         if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1977                 return false;
1978         if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1979                 return false;
1980         if (fcf_rec->priority != new_fcf_record->fip_priority)
1981                 return false;
1982         return true;
1983 }
1984 
1985 /**
1986  * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1987  * @vport: Pointer to vport object.
1988  * @fcf_index: index to next fcf.
1989  *
1990  * This function processing the roundrobin fcf failover to next fcf index.
1991  * When this function is invoked, there will be a current fcf registered
1992  * for flogi.
1993  * Return: 0 for continue retrying flogi on currently registered fcf;
1994  *         1 for stop flogi on currently registered fcf;
1995  */
1996 int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1997 {
1998         struct lpfc_hba *phba = vport->phba;
1999         int rc;
2000 
2001         if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
2002                 spin_lock_irq(&phba->hbalock);
2003                 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2004                         spin_unlock_irq(&phba->hbalock);
2005                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2006                                         "2872 Devloss tmo with no eligible "
2007                                         "FCF, unregister in-use FCF (x%x) "
2008                                         "and rescan FCF table\n",
2009                                         phba->fcf.current_rec.fcf_indx);
2010                         lpfc_unregister_fcf_rescan(phba);
2011                         goto stop_flogi_current_fcf;
2012                 }
2013                 /* Mark the end to FLOGI roundrobin failover */
2014                 phba->hba_flag &= ~FCF_RR_INPROG;
2015                 /* Allow action to new fcf asynchronous event */
2016                 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2017                 spin_unlock_irq(&phba->hbalock);
2018                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2019                                 "2865 No FCF available, stop roundrobin FCF "
2020                                 "failover and change port state:x%x/x%x\n",
2021                                 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
2022                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2023 
2024                 if (!phba->fcf.fcf_redisc_attempted) {
2025                         lpfc_unregister_fcf(phba);
2026 
2027                         rc = lpfc_sli4_redisc_fcf_table(phba);
2028                         if (!rc) {
2029                                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2030                                                 "3195 Rediscover FCF table\n");
2031                                 phba->fcf.fcf_redisc_attempted = 1;
2032                                 lpfc_sli4_clear_fcf_rr_bmask(phba);
2033                         } else {
2034                                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2035                                                 "3196 Rediscover FCF table "
2036                                                 "failed. Status:x%x\n", rc);
2037                         }
2038                 } else {
2039                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2040                                         "3197 Already rediscover FCF table "
2041                                         "attempted. No more retry\n");
2042                 }
2043                 goto stop_flogi_current_fcf;
2044         } else {
2045                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
2046                                 "2794 Try FLOGI roundrobin FCF failover to "
2047                                 "(x%x)\n", fcf_index);
2048                 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
2049                 if (rc)
2050                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
2051                                         "2761 FLOGI roundrobin FCF failover "
2052                                         "failed (rc:x%x) to read FCF (x%x)\n",
2053                                         rc, phba->fcf.current_rec.fcf_indx);
2054                 else
2055                         goto stop_flogi_current_fcf;
2056         }
2057         return 0;
2058 
2059 stop_flogi_current_fcf:
2060         lpfc_can_disctmo(vport);
2061         return 1;
2062 }
2063 
2064 /**
2065  * lpfc_sli4_fcf_pri_list_del
2066  * @phba: pointer to lpfc hba data structure.
2067  * @fcf_index the index of the fcf record to delete
2068  * This routine checks the on list flag of the fcf_index to be deleted.
2069  * If it is one the list then it is removed from the list, and the flag
2070  * is cleared. This routine grab the hbalock before removing the fcf
2071  * record from the list.
2072  **/
2073 static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2074                         uint16_t fcf_index)
2075 {
2076         struct lpfc_fcf_pri *new_fcf_pri;
2077 
2078         new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2079         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2080                 "3058 deleting idx x%x pri x%x flg x%x\n",
2081                 fcf_index, new_fcf_pri->fcf_rec.priority,
2082                  new_fcf_pri->fcf_rec.flag);
2083         spin_lock_irq(&phba->hbalock);
2084         if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2085                 if (phba->fcf.current_rec.priority ==
2086                                 new_fcf_pri->fcf_rec.priority)
2087                         phba->fcf.eligible_fcf_cnt--;
2088                 list_del_init(&new_fcf_pri->list);
2089                 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2090         }
2091         spin_unlock_irq(&phba->hbalock);
2092 }
2093 
2094 /**
2095  * lpfc_sli4_set_fcf_flogi_fail
2096  * @phba: pointer to lpfc hba data structure.
2097  * @fcf_index the index of the fcf record to update
2098  * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2099  * flag so the the round robin slection for the particular priority level
2100  * will try a different fcf record that does not have this bit set.
2101  * If the fcf record is re-read for any reason this flag is cleared brfore
2102  * adding it to the priority list.
2103  **/
2104 void
2105 lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2106 {
2107         struct lpfc_fcf_pri *new_fcf_pri;
2108         new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2109         spin_lock_irq(&phba->hbalock);
2110         new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2111         spin_unlock_irq(&phba->hbalock);
2112 }
2113 
2114 /**
2115  * lpfc_sli4_fcf_pri_list_add
2116  * @phba: pointer to lpfc hba data structure.
2117  * @fcf_index the index of the fcf record to add
2118  * This routine checks the priority of the fcf_index to be added.
2119  * If it is a lower priority than the current head of the fcf_pri list
2120  * then it is added to the list in the right order.
2121  * If it is the same priority as the current head of the list then it
2122  * is added to the head of the list and its bit in the rr_bmask is set.
2123  * If the fcf_index to be added is of a higher priority than the current
2124  * head of the list then the rr_bmask is cleared, its bit is set in the
2125  * rr_bmask and it is added to the head of the list.
2126  * returns:
2127  * 0=success 1=failure
2128  **/
2129 static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2130         uint16_t fcf_index,
2131         struct fcf_record *new_fcf_record)
2132 {
2133         uint16_t current_fcf_pri;
2134         uint16_t last_index;
2135         struct lpfc_fcf_pri *fcf_pri;
2136         struct lpfc_fcf_pri *next_fcf_pri;
2137         struct lpfc_fcf_pri *new_fcf_pri;
2138         int ret;
2139 
2140         new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2141         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2142                 "3059 adding idx x%x pri x%x flg x%x\n",
2143                 fcf_index, new_fcf_record->fip_priority,
2144                  new_fcf_pri->fcf_rec.flag);
2145         spin_lock_irq(&phba->hbalock);
2146         if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2147                 list_del_init(&new_fcf_pri->list);
2148         new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2149         new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2150         if (list_empty(&phba->fcf.fcf_pri_list)) {
2151                 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2152                 ret = lpfc_sli4_fcf_rr_index_set(phba,
2153                                 new_fcf_pri->fcf_rec.fcf_index);
2154                 goto out;
2155         }
2156 
2157         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2158                                 LPFC_SLI4_FCF_TBL_INDX_MAX);
2159         if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2160                 ret = 0; /* Empty rr list */
2161                 goto out;
2162         }
2163         current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2164         if (new_fcf_pri->fcf_rec.priority <=  current_fcf_pri) {
2165                 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2166                 if (new_fcf_pri->fcf_rec.priority <  current_fcf_pri) {
2167                         memset(phba->fcf.fcf_rr_bmask, 0,
2168                                 sizeof(*phba->fcf.fcf_rr_bmask));
2169                         /* fcfs_at_this_priority_level = 1; */
2170                         phba->fcf.eligible_fcf_cnt = 1;
2171                 } else
2172                         /* fcfs_at_this_priority_level++; */
2173                         phba->fcf.eligible_fcf_cnt++;
2174                 ret = lpfc_sli4_fcf_rr_index_set(phba,
2175                                 new_fcf_pri->fcf_rec.fcf_index);
2176                 goto out;
2177         }
2178 
2179         list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2180                                 &phba->fcf.fcf_pri_list, list) {
2181                 if (new_fcf_pri->fcf_rec.priority <=
2182                                 fcf_pri->fcf_rec.priority) {
2183                         if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2184                                 list_add(&new_fcf_pri->list,
2185                                                 &phba->fcf.fcf_pri_list);
2186                         else
2187                                 list_add(&new_fcf_pri->list,
2188                                          &((struct lpfc_fcf_pri *)
2189                                         fcf_pri->list.prev)->list);
2190                         ret = 0;
2191                         goto out;
2192                 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2193                         || new_fcf_pri->fcf_rec.priority <
2194                                 next_fcf_pri->fcf_rec.priority) {
2195                         list_add(&new_fcf_pri->list, &fcf_pri->list);
2196                         ret = 0;
2197                         goto out;
2198                 }
2199                 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2200                         continue;
2201 
2202         }
2203         ret = 1;
2204 out:
2205         /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2206         new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2207         spin_unlock_irq(&phba->hbalock);
2208         return ret;
2209 }
2210 
2211 /**
2212  * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2213  * @phba: pointer to lpfc hba data structure.
2214  * @mboxq: pointer to mailbox object.
2215  *
2216  * This function iterates through all the fcf records available in
2217  * HBA and chooses the optimal FCF record for discovery. After finding
2218  * the FCF for discovery it registers the FCF record and kicks start
2219  * discovery.
2220  * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2221  * use an FCF record which matches fabric name and mac address of the
2222  * currently used FCF record.
2223  * If the driver supports only one FCF, it will try to use the FCF record
2224  * used by BOOT_BIOS.
2225  */
2226 void
2227 lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2228 {
2229         struct fcf_record *new_fcf_record;
2230         uint32_t boot_flag, addr_mode;
2231         uint16_t fcf_index, next_fcf_index;
2232         struct lpfc_fcf_rec *fcf_rec = NULL;
2233         uint16_t vlan_id = LPFC_FCOE_NULL_VID;
2234         bool select_new_fcf;
2235         int rc;
2236 
2237         /* If there is pending FCoE event restart FCF table scan */
2238         if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
2239                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2240                 return;
2241         }
2242 
2243         /* Parse the FCF record from the non-embedded mailbox command */
2244         new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2245                                                       &next_fcf_index);
2246         if (!new_fcf_record) {
2247                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2248                                 "2765 Mailbox command READ_FCF_RECORD "
2249                                 "failed to retrieve a FCF record.\n");
2250                 /* Let next new FCF event trigger fast failover */
2251                 spin_lock_irq(&phba->hbalock);
2252                 phba->hba_flag &= ~FCF_TS_INPROG;
2253                 spin_unlock_irq(&phba->hbalock);
2254                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2255                 return;
2256         }
2257 
2258         /* Check the FCF record against the connection list */
2259         rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2260                                       &addr_mode, &vlan_id);
2261 
2262         /* Log the FCF record information if turned on */
2263         lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2264                                       next_fcf_index);
2265 
2266         /*
2267          * If the fcf record does not match with connect list entries
2268          * read the next entry; otherwise, this is an eligible FCF
2269          * record for roundrobin FCF failover.
2270          */
2271         if (!rc) {
2272                 lpfc_sli4_fcf_pri_list_del(phba,
2273                                         bf_get(lpfc_fcf_record_fcf_index,
2274                                                new_fcf_record));
2275                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2276                                 "2781 FCF (x%x) failed connection "
2277                                 "list check: (x%x/x%x/%x)\n",
2278                                 bf_get(lpfc_fcf_record_fcf_index,
2279                                        new_fcf_record),
2280                                 bf_get(lpfc_fcf_record_fcf_avail,
2281                                        new_fcf_record),
2282                                 bf_get(lpfc_fcf_record_fcf_valid,
2283                                        new_fcf_record),
2284                                 bf_get(lpfc_fcf_record_fcf_sol,
2285                                        new_fcf_record));
2286                 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2287                     lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2288                     new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
2289                         if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2290                             phba->fcf.current_rec.fcf_indx) {
2291                                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2292                                         "2862 FCF (x%x) matches property "
2293                                         "of in-use FCF (x%x)\n",
2294                                         bf_get(lpfc_fcf_record_fcf_index,
2295                                                new_fcf_record),
2296                                         phba->fcf.current_rec.fcf_indx);
2297                                 goto read_next_fcf;
2298                         }
2299                         /*
2300                          * In case the current in-use FCF record becomes
2301                          * invalid/unavailable during FCF discovery that
2302                          * was not triggered by fast FCF failover process,
2303                          * treat it as fast FCF failover.
2304                          */
2305                         if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2306                             !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2307                                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2308                                                 "2835 Invalid in-use FCF "
2309                                                 "(x%x), enter FCF failover "
2310                                                 "table scan.\n",
2311                                                 phba->fcf.current_rec.fcf_indx);
2312                                 spin_lock_irq(&phba->hbalock);
2313                                 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2314                                 spin_unlock_irq(&phba->hbalock);
2315                                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2316                                 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2317                                                 LPFC_FCOE_FCF_GET_FIRST);
2318                                 return;
2319                         }
2320                 }
2321                 goto read_next_fcf;
2322         } else {
2323                 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2324                 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2325                                                         new_fcf_record);
2326                 if (rc)
2327                         goto read_next_fcf;
2328         }
2329 
2330         /*
2331          * If this is not the first FCF discovery of the HBA, use last
2332          * FCF record for the discovery. The condition that a rescan
2333          * matches the in-use FCF record: fabric name, switch name, mac
2334          * address, and vlan_id.
2335          */
2336         spin_lock_irq(&phba->hbalock);
2337         if (phba->fcf.fcf_flag & FCF_IN_USE) {
2338                 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2339                         lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2340                     new_fcf_record, vlan_id)) {
2341                         if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2342                             phba->fcf.current_rec.fcf_indx) {
2343                                 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2344                                 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2345                                         /* Stop FCF redisc wait timer */
2346                                         __lpfc_sli4_stop_fcf_redisc_wait_timer(
2347                                                                         phba);
2348                                 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2349                                         /* Fast failover, mark completed */
2350                                         phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2351                                 spin_unlock_irq(&phba->hbalock);
2352                                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2353                                                 "2836 New FCF matches in-use "
2354                                                 "FCF (x%x), port_state:x%x, "
2355                                                 "fc_flag:x%x\n",
2356                                                 phba->fcf.current_rec.fcf_indx,
2357                                                 phba->pport->port_state,
2358                                                 phba->pport->fc_flag);
2359                                 goto out;
2360                         } else
2361                                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2362                                         "2863 New FCF (x%x) matches "
2363                                         "property of in-use FCF (x%x)\n",
2364                                         bf_get(lpfc_fcf_record_fcf_index,
2365                                                new_fcf_record),
2366                                         phba->fcf.current_rec.fcf_indx);
2367                 }
2368                 /*
2369                  * Read next FCF record from HBA searching for the matching
2370                  * with in-use record only if not during the fast failover
2371                  * period. In case of fast failover period, it shall try to
2372                  * determine whether the FCF record just read should be the
2373                  * next candidate.
2374                  */
2375                 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2376                         spin_unlock_irq(&phba->hbalock);
2377                         goto read_next_fcf;
2378                 }
2379         }
2380         /*
2381          * Update on failover FCF record only if it's in FCF fast-failover
2382          * period; otherwise, update on current FCF record.
2383          */
2384         if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2385                 fcf_rec = &phba->fcf.failover_rec;
2386         else
2387                 fcf_rec = &phba->fcf.current_rec;
2388 
2389         if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2390                 /*
2391                  * If the driver FCF record does not have boot flag
2392                  * set and new hba fcf record has boot flag set, use
2393                  * the new hba fcf record.
2394                  */
2395                 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2396                         /* Choose this FCF record */
2397                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2398                                         "2837 Update current FCF record "
2399                                         "(x%x) with new FCF record (x%x)\n",
2400                                         fcf_rec->fcf_indx,
2401                                         bf_get(lpfc_fcf_record_fcf_index,
2402                                         new_fcf_record));
2403                         __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2404                                         addr_mode, vlan_id, BOOT_ENABLE);
2405                         spin_unlock_irq(&phba->hbalock);
2406                         goto read_next_fcf;
2407                 }
2408                 /*
2409                  * If the driver FCF record has boot flag set and the
2410                  * new hba FCF record does not have boot flag, read
2411                  * the next FCF record.
2412                  */
2413                 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
2414                         spin_unlock_irq(&phba->hbalock);
2415                         goto read_next_fcf;
2416                 }
2417                 /*
2418                  * If the new hba FCF record has lower priority value
2419                  * than the driver FCF record, use the new record.
2420                  */
2421                 if (new_fcf_record->fip_priority < fcf_rec->priority) {
2422                         /* Choose the new FCF record with lower priority */
2423                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2424                                         "2838 Update current FCF record "
2425                                         "(x%x) with new FCF record (x%x)\n",
2426                                         fcf_rec->fcf_indx,
2427                                         bf_get(lpfc_fcf_record_fcf_index,
2428                                                new_fcf_record));
2429                         __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2430                                         addr_mode, vlan_id, 0);
2431                         /* Reset running random FCF selection count */
2432                         phba->fcf.eligible_fcf_cnt = 1;
2433                 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2434                         /* Update running random FCF selection count */
2435                         phba->fcf.eligible_fcf_cnt++;
2436                         select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2437                                                 phba->fcf.eligible_fcf_cnt);
2438                         if (select_new_fcf) {
2439                                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2440                                         "2839 Update current FCF record "
2441                                         "(x%x) with new FCF record (x%x)\n",
2442                                         fcf_rec->fcf_indx,
2443                                         bf_get(lpfc_fcf_record_fcf_index,
2444                                                new_fcf_record));
2445                                 /* Choose the new FCF by random selection */
2446                                 __lpfc_update_fcf_record(phba, fcf_rec,
2447                                                          new_fcf_record,
2448                                                          addr_mode, vlan_id, 0);
2449                         }
2450                 }
2451                 spin_unlock_irq(&phba->hbalock);
2452                 goto read_next_fcf;
2453         }
2454         /*
2455          * This is the first suitable FCF record, choose this record for
2456          * initial best-fit FCF.
2457          */
2458         if (fcf_rec) {
2459                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2460                                 "2840 Update initial FCF candidate "
2461                                 "with FCF (x%x)\n",
2462                                 bf_get(lpfc_fcf_record_fcf_index,
2463                                        new_fcf_record));
2464                 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2465                                          addr_mode, vlan_id, (boot_flag ?
2466                                          BOOT_ENABLE : 0));
2467                 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2468                 /* Setup initial running random FCF selection count */
2469                 phba->fcf.eligible_fcf_cnt = 1;
2470         }
2471         spin_unlock_irq(&phba->hbalock);
2472         goto read_next_fcf;
2473 
2474 read_next_fcf:
2475         lpfc_sli4_mbox_cmd_free(phba, mboxq);
2476         if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2477                 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2478                         /*
2479                          * Case of FCF fast failover scan
2480                          */
2481 
2482                         /*
2483                          * It has not found any suitable FCF record, cancel
2484                          * FCF scan inprogress, and do nothing
2485                          */
2486                         if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
2487                                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2488                                                "2782 No suitable FCF found: "
2489                                                "(x%x/x%x)\n",
2490                                                phba->fcoe_eventtag_at_fcf_scan,
2491                                                bf_get(lpfc_fcf_record_fcf_index,
2492                                                       new_fcf_record));
2493                                 spin_lock_irq(&phba->hbalock);
2494                                 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2495                                         phba->hba_flag &= ~FCF_TS_INPROG;
2496                                         spin_unlock_irq(&phba->hbalock);
2497                                         /* Unregister in-use FCF and rescan */
2498                                         lpfc_printf_log(phba, KERN_INFO,
2499                                                         LOG_FIP,
2500                                                         "2864 On devloss tmo "
2501                                                         "unreg in-use FCF and "
2502                                                         "rescan FCF table\n");
2503                                         lpfc_unregister_fcf_rescan(phba);
2504                                         return;
2505                                 }
2506                                 /*
2507                                  * Let next new FCF event trigger fast failover
2508                                  */
2509                                 phba->hba_flag &= ~FCF_TS_INPROG;
2510                                 spin_unlock_irq(&phba->hbalock);
2511                                 return;
2512                         }
2513                         /*
2514                          * It has found a suitable FCF record that is not
2515                          * the same as in-use FCF record, unregister the
2516                          * in-use FCF record, replace the in-use FCF record
2517                          * with the new FCF record, mark FCF fast failover
2518                          * completed, and then start register the new FCF
2519                          * record.
2520                          */
2521 
2522                         /* Unregister the current in-use FCF record */
2523                         lpfc_unregister_fcf(phba);
2524 
2525                         /* Replace in-use record with the new record */
2526                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2527                                         "2842 Replace in-use FCF (x%x) "
2528                                         "with failover FCF (x%x)\n",
2529                                         phba->fcf.current_rec.fcf_indx,
2530                                         phba->fcf.failover_rec.fcf_indx);
2531                         memcpy(&phba->fcf.current_rec,
2532                                &phba->fcf.failover_rec,
2533                                sizeof(struct lpfc_fcf_rec));
2534                         /*
2535                          * Mark the fast FCF failover rediscovery completed
2536                          * and the start of the first round of the roundrobin
2537                          * FCF failover.
2538                          */
2539                         spin_lock_irq(&phba->hbalock);
2540                         phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2541                         spin_unlock_irq(&phba->hbalock);
2542                         /* Register to the new FCF record */
2543                         lpfc_register_fcf(phba);
2544                 } else {
2545                         /*
2546                          * In case of transaction period to fast FCF failover,
2547                          * do nothing when search to the end of the FCF table.
2548                          */
2549                         if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2550                             (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2551                                 return;
2552 
2553                         if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2554                                 phba->fcf.fcf_flag & FCF_IN_USE) {
2555                                 /*
2556                                  * In case the current in-use FCF record no
2557                                  * longer existed during FCF discovery that
2558                                  * was not triggered by fast FCF failover
2559                                  * process, treat it as fast FCF failover.
2560                                  */
2561                                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2562                                                 "2841 In-use FCF record (x%x) "
2563                                                 "not reported, entering fast "
2564                                                 "FCF failover mode scanning.\n",
2565                                                 phba->fcf.current_rec.fcf_indx);
2566                                 spin_lock_irq(&phba->hbalock);
2567                                 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2568                                 spin_unlock_irq(&phba->hbalock);
2569                                 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2570                                                 LPFC_FCOE_FCF_GET_FIRST);
2571                                 return;
2572                         }
2573                         /* Register to the new FCF record */
2574                         lpfc_register_fcf(phba);
2575                 }
2576         } else
2577                 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
2578         return;
2579 
2580 out:
2581         lpfc_sli4_mbox_cmd_free(phba, mboxq);
2582         lpfc_register_fcf(phba);
2583 
2584         return;
2585 }
2586 
2587 /**
2588  * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
2589  * @phba: pointer to lpfc hba data structure.
2590  * @mboxq: pointer to mailbox object.
2591  *
2592  * This is the callback function for FLOGI failure roundrobin FCF failover
2593  * read FCF record mailbox command from the eligible FCF record bmask for
2594  * performing the failover. If the FCF read back is not valid/available, it
2595  * fails through to retrying FLOGI to the currently registered FCF again.
2596  * Otherwise, if the FCF read back is valid and available, it will set the
2597  * newly read FCF record to the failover FCF record, unregister currently
2598  * registered FCF record, copy the failover FCF record to the current
2599  * FCF record, and then register the current FCF record before proceeding
2600  * to trying FLOGI on the new failover FCF.
2601  */
2602 void
2603 lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2604 {
2605         struct fcf_record *new_fcf_record;
2606         uint32_t boot_flag, addr_mode;
2607         uint16_t next_fcf_index, fcf_index;
2608         uint16_t current_fcf_index;
2609         uint16_t vlan_id;
2610         int rc;
2611 
2612         /* If link state is not up, stop the roundrobin failover process */
2613         if (phba->link_state < LPFC_LINK_UP) {
2614                 spin_lock_irq(&phba->hbalock);
2615                 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
2616                 phba->hba_flag &= ~FCF_RR_INPROG;
2617                 spin_unlock_irq(&phba->hbalock);
2618                 goto out;
2619         }
2620 
2621         /* Parse the FCF record from the non-embedded mailbox command */
2622         new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2623                                                       &next_fcf_index);
2624         if (!new_fcf_record) {
2625                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2626                                 "2766 Mailbox command READ_FCF_RECORD "
2627                                 "failed to retrieve a FCF record. "
2628                                 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2629                                 phba->fcf.fcf_flag);
2630                 lpfc_unregister_fcf_rescan(phba);
2631                 goto out;
2632         }
2633 
2634         /* Get the needed parameters from FCF record */
2635         rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2636                                       &addr_mode, &vlan_id);
2637 
2638         /* Log the FCF record information if turned on */
2639         lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2640                                       next_fcf_index);
2641 
2642         fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2643         if (!rc) {
2644                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2645                                 "2848 Remove ineligible FCF (x%x) from "
2646                                 "from roundrobin bmask\n", fcf_index);
2647                 /* Clear roundrobin bmask bit for ineligible FCF */
2648                 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2649                 /* Perform next round of roundrobin FCF failover */
2650                 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2651                 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2652                 if (rc)
2653                         goto out;
2654                 goto error_out;
2655         }
2656 
2657         if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2658                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2659                                 "2760 Perform FLOGI roundrobin FCF failover: "
2660                                 "FCF (x%x) back to FCF (x%x)\n",
2661                                 phba->fcf.current_rec.fcf_indx, fcf_index);
2662                 /* Wait 500 ms before retrying FLOGI to current FCF */
2663                 msleep(500);
2664                 lpfc_issue_init_vfi(phba->pport);
2665                 goto out;
2666         }
2667 
2668         /* Upload new FCF record to the failover FCF record */
2669         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2670                         "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2671                         phba->fcf.failover_rec.fcf_indx, fcf_index);
2672         spin_lock_irq(&phba->hbalock);
2673         __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2674                                  new_fcf_record, addr_mode, vlan_id,
2675                                  (boot_flag ? BOOT_ENABLE : 0));
2676         spin_unlock_irq(&phba->hbalock);
2677 
2678         current_fcf_index = phba->fcf.current_rec.fcf_indx;
2679 
2680         /* Unregister the current in-use FCF record */
2681         lpfc_unregister_fcf(phba);
2682 
2683         /* Replace in-use record with the new record */
2684         memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2685                sizeof(struct lpfc_fcf_rec));
2686 
2687         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2688                         "2783 Perform FLOGI roundrobin FCF failover: FCF "
2689                         "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
2690 
2691 error_out:
2692         lpfc_register_fcf(phba);
2693 out:
2694         lpfc_sli4_mbox_cmd_free(phba, mboxq);
2695 }
2696 
2697 /**
2698  * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2699  * @phba: pointer to lpfc hba data structure.
2700  * @mboxq: pointer to mailbox object.
2701  *
2702  * This is the callback function of read FCF record mailbox command for
2703  * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
2704  * failover when a new FCF event happened. If the FCF read back is
2705  * valid/available and it passes the connection list check, it updates
2706  * the bmask for the eligible FCF record for roundrobin failover.
2707  */
2708 void
2709 lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2710 {
2711         struct fcf_record *new_fcf_record;
2712         uint32_t boot_flag, addr_mode;
2713         uint16_t fcf_index, next_fcf_index;
2714         uint16_t vlan_id;
2715         int rc;
2716 
2717         /* If link state is not up, no need to proceed */
2718         if (phba->link_state < LPFC_LINK_UP)
2719                 goto out;
2720 
2721         /* If FCF discovery period is over, no need to proceed */
2722         if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
2723                 goto out;
2724 
2725         /* Parse the FCF record from the non-embedded mailbox command */
2726         new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2727                                                       &next_fcf_index);
2728         if (!new_fcf_record) {
2729                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2730                                 "2767 Mailbox command READ_FCF_RECORD "
2731                                 "failed to retrieve a FCF record.\n");
2732                 goto out;
2733         }
2734 
2735         /* Check the connection list for eligibility */
2736         rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2737                                       &addr_mode, &vlan_id);
2738 
2739         /* Log the FCF record information if turned on */
2740         lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2741                                       next_fcf_index);
2742 
2743         if (!rc)
2744                 goto out;
2745 
2746         /* Update the eligible FCF record index bmask */
2747         fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2748 
2749         rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
2750 
2751 out:
2752         lpfc_sli4_mbox_cmd_free(phba, mboxq);
2753 }
2754 
2755 /**
2756  * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2757  * @phba: pointer to lpfc hba data structure.
2758  * @mboxq: pointer to mailbox data structure.
2759  *
2760  * This function handles completion of init vfi mailbox command.
2761  */
2762 static void
2763 lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2764 {
2765         struct lpfc_vport *vport = mboxq->vport;
2766 
2767         /*
2768          * VFI not supported on interface type 0, just do the flogi
2769          * Also continue if the VFI is in use - just use the same one.
2770          */
2771         if (mboxq->u.mb.mbxStatus &&
2772             (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2773                         LPFC_SLI_INTF_IF_TYPE_0) &&
2774             mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
2775                 lpfc_printf_vlog(vport, KERN_ERR,
2776                                 LOG_MBOX,
2777                                 "2891 Init VFI mailbox failed 0x%x\n",
2778                                 mboxq->u.mb.mbxStatus);
2779                 mempool_free(mboxq, phba->mbox_mem_pool);
2780                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2781                 return;
2782         }
2783 
2784         lpfc_initial_flogi(vport);
2785         mempool_free(mboxq, phba->mbox_mem_pool);
2786         return;
2787 }
2788 
2789 /**
2790  * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2791  * @vport: pointer to lpfc_vport data structure.
2792  *
2793  * This function issue a init_vfi mailbox command to initialize the VFI and
2794  * VPI for the physical port.
2795  */
2796 void
2797 lpfc_issue_init_vfi(struct lpfc_vport *vport)
2798 {
2799         LPFC_MBOXQ_t *mboxq;
2800         int rc;
2801         struct lpfc_hba *phba = vport->phba;
2802 
2803         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2804         if (!mboxq) {
2805                 lpfc_printf_vlog(vport, KERN_ERR,
2806                         LOG_MBOX, "2892 Failed to allocate "
2807                         "init_vfi mailbox\n");
2808                 return;
2809         }
2810         lpfc_init_vfi(mboxq, vport);
2811         mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2812         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2813         if (rc == MBX_NOT_FINISHED) {
2814                 lpfc_printf_vlog(vport, KERN_ERR,
2815                         LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2816                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2817         }
2818 }
2819 
2820 /**
2821  * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2822  * @phba: pointer to lpfc hba data structure.
2823  * @mboxq: pointer to mailbox data structure.
2824  *
2825  * This function handles completion of init vpi mailbox command.
2826  */
2827 void
2828 lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2829 {
2830         struct lpfc_vport *vport = mboxq->vport;
2831         struct lpfc_nodelist *ndlp;
2832         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2833 
2834         if (mboxq->u.mb.mbxStatus) {
2835                 lpfc_printf_vlog(vport, KERN_ERR,
2836                                 LOG_MBOX,
2837                                 "2609 Init VPI mailbox failed 0x%x\n",
2838                                 mboxq->u.mb.mbxStatus);
2839                 mempool_free(mboxq, phba->mbox_mem_pool);
2840                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2841                 return;
2842         }
2843         spin_lock_irq(shost->host_lock);
2844         vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
2845         spin_unlock_irq(shost->host_lock);
2846 
2847         /* If this port is physical port or FDISC is done, do reg_vpi */
2848         if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2849                         ndlp = lpfc_findnode_did(vport, Fabric_DID);
2850                         if (!ndlp)
2851                                 lpfc_printf_vlog(vport, KERN_ERR,
2852                                         LOG_DISCOVERY,
2853                                         "2731 Cannot find fabric "
2854                                         "controller node\n");
2855                         else
2856                                 lpfc_register_new_vport(phba, vport, ndlp);
2857                         mempool_free(mboxq, phba->mbox_mem_pool);
2858                         return;
2859         }
2860 
2861         if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2862                 lpfc_initial_fdisc(vport);
2863         else {
2864                 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
2865                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2866                                  "2606 No NPIV Fabric support\n");
2867         }
2868         mempool_free(mboxq, phba->mbox_mem_pool);
2869         return;
2870 }
2871 
2872 /**
2873  * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2874  * @vport: pointer to lpfc_vport data structure.
2875  *
2876  * This function issue a init_vpi mailbox command to initialize
2877  * VPI for the vport.
2878  */
2879 void
2880 lpfc_issue_init_vpi(struct lpfc_vport *vport)
2881 {
2882         LPFC_MBOXQ_t *mboxq;
2883         int rc, vpi;
2884 
2885         if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2886                 vpi = lpfc_alloc_vpi(vport->phba);
2887                 if (!vpi) {
2888                         lpfc_printf_vlog(vport, KERN_ERR,
2889                                          LOG_MBOX,
2890                                          "3303 Failed to obtain vport vpi\n");
2891                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2892                         return;
2893                 }
2894                 vport->vpi = vpi;
2895         }
2896 
2897         mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2898         if (!mboxq) {
2899                 lpfc_printf_vlog(vport, KERN_ERR,
2900                         LOG_MBOX, "2607 Failed to allocate "
2901                         "init_vpi mailbox\n");
2902                 return;
2903         }
2904         lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2905         mboxq->vport = vport;
2906         mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2907         rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2908         if (rc == MBX_NOT_FINISHED) {
2909                 lpfc_printf_vlog(vport, KERN_ERR,
2910                         LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2911                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2912         }
2913 }
2914 
2915 /**
2916  * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2917  * @phba: pointer to lpfc hba data structure.
2918  *
2919  * This function loops through the list of vports on the @phba and issues an
2920  * FDISC if possible.
2921  */
2922 void
2923 lpfc_start_fdiscs(struct lpfc_hba *phba)
2924 {
2925         struct lpfc_vport **vports;
2926         int i;
2927 
2928         vports = lpfc_create_vport_work_array(phba);
2929         if (vports != NULL) {
2930                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2931                         if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2932                                 continue;
2933                         /* There are no vpi for this vport */
2934                         if (vports[i]->vpi > phba->max_vpi) {
2935                                 lpfc_vport_set_state(vports[i],
2936                                                      FC_VPORT_FAILED);
2937                                 continue;
2938                         }
2939                         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
2940                                 lpfc_vport_set_state(vports[i],
2941                                                      FC_VPORT_LINKDOWN);
2942                                 continue;
2943                         }
2944                         if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
2945                                 lpfc_issue_init_vpi(vports[i]);
2946                                 continue;
2947                         }
2948                         if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2949                                 lpfc_initial_fdisc(vports[i]);
2950                         else {
2951                                 lpfc_vport_set_state(vports[i],
2952                                                      FC_VPORT_NO_FABRIC_SUPP);
2953                                 lpfc_printf_vlog(vports[i], KERN_ERR,
2954                                                  LOG_ELS,
2955                                                  "0259 No NPIV "
2956                                                  "Fabric support\n");
2957                         }
2958                 }
2959         }
2960         lpfc_destroy_vport_work_array(phba, vports);
2961 }
2962 
2963 void
2964 lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2965 {
2966         struct lpfc_dmabuf *dmabuf = mboxq->ctx_buf;
2967         struct lpfc_vport *vport = mboxq->vport;
2968         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2969 
2970         /*
2971          * VFI not supported for interface type 0, so ignore any mailbox
2972          * error (except VFI in use) and continue with the discovery.
2973          */
2974         if (mboxq->u.mb.mbxStatus &&
2975             (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2976                         LPFC_SLI_INTF_IF_TYPE_0) &&
2977             mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
2978                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2979                          "2018 REG_VFI mbxStatus error x%x "
2980                          "HBA state x%x\n",
2981                          mboxq->u.mb.mbxStatus, vport->port_state);
2982                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
2983                         /* FLOGI failed, use loop map to make discovery list */
2984                         lpfc_disc_list_loopmap(vport);
2985                         /* Start discovery */
2986                         lpfc_disc_start(vport);
2987                         goto out_free_mem;
2988                 }
2989                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2990                 goto out_free_mem;
2991         }
2992 
2993         /* If the VFI is already registered, there is nothing else to do
2994          * Unless this was a VFI update and we are in PT2PT mode, then
2995          * we should drop through to set the port state to ready.
2996          */
2997         if (vport->fc_flag & FC_VFI_REGISTERED)
2998                 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2999                       vport->fc_flag & FC_PT2PT))
3000                         goto out_free_mem;
3001 
3002         /* The VPI is implicitly registered when the VFI is registered */
3003         spin_lock_irq(shost->host_lock);
3004         vport->vpi_state |= LPFC_VPI_REGISTERED;
3005         vport->fc_flag |= FC_VFI_REGISTERED;
3006         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
3007         vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
3008         spin_unlock_irq(shost->host_lock);
3009 
3010         /* In case SLI4 FC loopback test, we are ready */
3011         if ((phba->sli_rev == LPFC_SLI_REV4) &&
3012             (phba->link_flag & LS_LOOPBACK_MODE)) {
3013                 phba->link_state = LPFC_HBA_READY;
3014                 goto out_free_mem;
3015         }
3016 
3017         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3018                          "3313 cmpl reg vfi  port_state:%x fc_flag:%x myDid:%x "
3019                          "alpacnt:%d LinkState:%x topology:%x\n",
3020                          vport->port_state, vport->fc_flag, vport->fc_myDID,
3021                          vport->phba->alpa_map[0],
3022                          phba->link_state, phba->fc_topology);
3023 
3024         if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
3025                 /*
3026                  * For private loop or for NPort pt2pt,
3027                  * just start discovery and we are done.
3028                  */
3029                 if ((vport->fc_flag & FC_PT2PT) ||
3030                     ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
3031                     !(vport->fc_flag & FC_PUBLIC_LOOP))) {
3032 
3033                         /* Use loop map to make discovery list */
3034                         lpfc_disc_list_loopmap(vport);
3035                         /* Start discovery */
3036                         if (vport->fc_flag & FC_PT2PT)
3037                                 vport->port_state = LPFC_VPORT_READY;
3038                         else
3039                                 lpfc_disc_start(vport);
3040                 } else {
3041                         lpfc_start_fdiscs(phba);
3042                         lpfc_do_scr_ns_plogi(phba, vport);
3043                 }
3044         }
3045 
3046 out_free_mem:
3047         mempool_free(mboxq, phba->mbox_mem_pool);
3048         if (dmabuf) {
3049                 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
3050                 kfree(dmabuf);
3051         }
3052         return;
3053 }
3054 
3055 static void
3056 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3057 {
3058         MAILBOX_t *mb = &pmb->u.mb;
3059         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3060         struct lpfc_vport  *vport = pmb->vport;
3061         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3062         struct serv_parm *sp = &vport->fc_sparam;
3063         uint32_t ed_tov;
3064 
3065         /* Check for error */
3066         if (mb->mbxStatus) {
3067                 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
3068                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3069                                  "0319 READ_SPARAM mbxStatus error x%x "
3070                                  "hba state x%x>\n",
3071                                  mb->mbxStatus, vport->port_state);
3072                 lpfc_linkdown(phba);
3073                 goto out;
3074         }
3075 
3076         memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
3077                sizeof (struct serv_parm));
3078 
3079         ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3080         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
3081                 ed_tov = (ed_tov + 999999) / 1000000;
3082 
3083         phba->fc_edtov = ed_tov;
3084         phba->fc_ratov = (2 * ed_tov) / 1000;
3085         if (phba->fc_ratov < FF_DEF_RATOV) {
3086                 /* RA_TOV should be atleast 10sec for initial flogi */
3087                 phba->fc_ratov = FF_DEF_RATOV;
3088         }
3089 
3090         lpfc_update_vport_wwn(vport);
3091         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
3092         if (vport->port_type == LPFC_PHYSICAL_PORT) {
3093                 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3094                 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3095         }
3096 
3097         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3098         kfree(mp);
3099         mempool_free(pmb, phba->mbox_mem_pool);
3100 
3101         /* Check if sending the FLOGI is being deferred to after we get
3102          * up to date CSPs from MBX_READ_SPARAM.
3103          */
3104         if (phba->hba_flag & HBA_DEFER_FLOGI) {
3105                 lpfc_initial_flogi(vport);
3106                 phba->hba_flag &= ~HBA_DEFER_FLOGI;
3107         }
3108         return;
3109 
3110 out:
3111         pmb->ctx_buf = NULL;
3112         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3113         kfree(mp);
3114         lpfc_issue_clear_la(phba, vport);
3115         mempool_free(pmb, phba->mbox_mem_pool);
3116         return;
3117 }
3118 
3119 static void
3120 lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
3121 {
3122         struct lpfc_vport *vport = phba->pport;
3123         LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
3124         struct Scsi_Host *shost;
3125         int i;
3126         struct lpfc_dmabuf *mp;
3127         int rc;
3128         struct fcf_record *fcf_record;
3129         uint32_t fc_flags = 0;
3130         unsigned long iflags;
3131 
3132         spin_lock_irqsave(&phba->hbalock, iflags);
3133         phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3134 
3135         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3136                 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3137                 case LPFC_LINK_SPEED_1GHZ:
3138                 case LPFC_LINK_SPEED_2GHZ:
3139                 case LPFC_LINK_SPEED_4GHZ:
3140                 case LPFC_LINK_SPEED_8GHZ:
3141                 case LPFC_LINK_SPEED_10GHZ:
3142                 case LPFC_LINK_SPEED_16GHZ:
3143                 case LPFC_LINK_SPEED_32GHZ:
3144                 case LPFC_LINK_SPEED_64GHZ:
3145                 case LPFC_LINK_SPEED_128GHZ:
3146                         break;
3147                 default:
3148                         phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3149                         break;
3150                 }
3151         }
3152 
3153         if (phba->fc_topology &&
3154             phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3155                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3156                                 "3314 Toplogy changed was 0x%x is 0x%x\n",
3157                                 phba->fc_topology,
3158                                 bf_get(lpfc_mbx_read_top_topology, la));
3159                 phba->fc_topology_changed = 1;
3160         }
3161 
3162         phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
3163         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
3164 
3165         shost = lpfc_shost_from_vport(vport);
3166         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3167                 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
3168 
3169                 /* if npiv is enabled and this adapter supports npiv log
3170                  * a message that npiv is not supported in this topology
3171                  */
3172                 if (phba->cfg_enable_npiv && phba->max_vpi)
3173                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3174                                 "1309 Link Up Event npiv not supported in loop "
3175                                 "topology\n");
3176                                 /* Get Loop Map information */
3177                 if (bf_get(lpfc_mbx_read_top_il, la))
3178                         fc_flags |= FC_LBIT;
3179 
3180                 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3181                 i = la->lilpBde64.tus.f.bdeSize;
3182 
3183                 if (i == 0) {
3184                         phba->alpa_map[0] = 0;
3185                 } else {
3186                         if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
3187                                 int numalpa, j, k;
3188                                 union {
3189                                         uint8_t pamap[16];
3190                                         struct {
3191                                                 uint32_t wd1;
3192                                                 uint32_t wd2;
3193                                                 uint32_t wd3;
3194                                                 uint32_t wd4;
3195                                         } pa;
3196                                 } un;
3197                                 numalpa = phba->alpa_map[0];
3198                                 j = 0;
3199                                 while (j < numalpa) {
3200                                         memset(un.pamap, 0, 16);
3201                                         for (k = 1; j < numalpa; k++) {
3202                                                 un.pamap[k - 1] =
3203                                                         phba->alpa_map[j + 1];
3204                                                 j++;
3205                                                 if (k == 16)
3206                                                         break;
3207                                         }
3208                                         /* Link Up Event ALPA map */
3209                                         lpfc_printf_log(phba,
3210                                                         KERN_WARNING,
3211                                                         LOG_LINK_EVENT,
3212                                                         "1304 Link Up Event "
3213                                                         "ALPA map Data: x%x "
3214                                                         "x%x x%x x%x\n",
3215                                                         un.pa.wd1, un.pa.wd2,
3216                                                         un.pa.wd3, un.pa.wd4);
3217                                 }
3218                         }
3219                 }
3220         } else {
3221                 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
3222                         if (phba->max_vpi && phba->cfg_enable_npiv &&
3223                            (phba->sli_rev >= LPFC_SLI_REV3))
3224                                 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3225                 }
3226                 vport->fc_myDID = phba->fc_pref_DID;
3227                 fc_flags |= FC_LBIT;
3228         }
3229         spin_unlock_irqrestore(&phba->hbalock, iflags);
3230 
3231         if (fc_flags) {
3232                 spin_lock_irqsave(shost->host_lock, iflags);
3233                 vport->fc_flag |= fc_flags;
3234                 spin_unlock_irqrestore(shost->host_lock, iflags);
3235         }
3236 
3237         lpfc_linkup(phba);
3238         sparam_mbox = NULL;
3239 
3240         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3241                 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3242                 if (!cfglink_mbox)
3243                         goto out;
3244                 vport->port_state = LPFC_LOCAL_CFG_LINK;
3245                 lpfc_config_link(phba, cfglink_mbox);
3246                 cfglink_mbox->vport = vport;
3247                 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
3248                 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
3249                 if (rc == MBX_NOT_FINISHED) {
3250                         mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3251                         goto out;
3252                 }
3253         }
3254 
3255         sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3256         if (!sparam_mbox)
3257                 goto out;
3258 
3259         rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3260         if (rc) {
3261                 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3262                 goto out;
3263         }
3264         sparam_mbox->vport = vport;
3265         sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3266         rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3267         if (rc == MBX_NOT_FINISHED) {
3268                 mp = (struct lpfc_dmabuf *)sparam_mbox->ctx_buf;
3269                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3270                 kfree(mp);
3271                 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3272                 goto out;
3273         }
3274 
3275         if (phba->hba_flag & HBA_FCOE_MODE) {
3276                 vport->port_state = LPFC_VPORT_UNKNOWN;
3277                 /*
3278                  * Add the driver's default FCF record at FCF index 0 now. This
3279                  * is phase 1 implementation that support FCF index 0 and driver
3280                  * defaults.
3281                  */
3282                 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
3283                         fcf_record = kzalloc(sizeof(struct fcf_record),
3284                                         GFP_KERNEL);
3285                         if (unlikely(!fcf_record)) {
3286                                 lpfc_printf_log(phba, KERN_ERR,
3287                                         LOG_MBOX | LOG_SLI,
3288                                         "2554 Could not allocate memory for "
3289                                         "fcf record\n");
3290                                 rc = -ENODEV;
3291                                 goto out;
3292                         }
3293 
3294                         lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3295                                                 LPFC_FCOE_FCF_DEF_INDEX);
3296                         rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3297                         if (unlikely(rc)) {
3298                                 lpfc_printf_log(phba, KERN_ERR,
3299                                         LOG_MBOX | LOG_SLI,
3300                                         "2013 Could not manually add FCF "
3301                                         "record 0, status %d\n", rc);
3302                                 rc = -ENODEV;
3303                                 kfree(fcf_record);
3304                                 goto out;
3305                         }
3306                         kfree(fcf_record);
3307                 }
3308                 /*
3309                  * The driver is expected to do FIP/FCF. Call the port
3310                  * and get the FCF Table.
3311                  */
3312                 spin_lock_irqsave(&phba->hbalock, iflags);
3313                 if (phba->hba_flag & FCF_TS_INPROG) {
3314                         spin_unlock_irqrestore(&phba->hbalock, iflags);
3315                         return;
3316                 }
3317                 /* This is the initial FCF discovery scan */
3318                 phba->fcf.fcf_flag |= FCF_INIT_DISC;
3319                 spin_unlock_irqrestore(&phba->hbalock, iflags);
3320                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3321                                 "2778 Start FCF table scan at linkup\n");
3322                 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3323                                                      LPFC_FCOE_FCF_GET_FIRST);
3324                 if (rc) {
3325                         spin_lock_irqsave(&phba->hbalock, iflags);
3326                         phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3327                         spin_unlock_irqrestore(&phba->hbalock, iflags);
3328                         goto out;
3329                 }
3330                 /* Reset FCF roundrobin bmask for new discovery */
3331                 lpfc_sli4_clear_fcf_rr_bmask(phba);
3332         } else {
3333                 if (phba->bbcredit_support && phba->cfg_enable_bbcr &&
3334                     !(phba->link_flag & LS_LOOPBACK_MODE))
3335                         phba->hba_flag |= HBA_DEFER_FLOGI;
3336         }
3337 
3338         /* Prepare for LINK up registrations */
3339         memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
3340         scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
3341                   init_utsname()->nodename);
3342         return;
3343 out:
3344         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3345         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3346                          "0263 Discovery Mailbox error: state: 0x%x : x%px x%px\n",
3347                          vport->port_state, sparam_mbox, cfglink_mbox);
3348         lpfc_issue_clear_la(phba, vport);
3349         return;
3350 }
3351 
3352 static void
3353 lpfc_enable_la(struct lpfc_hba *phba)
3354 {
3355         uint32_t control;
3356         struct lpfc_sli *psli = &phba->sli;
3357         spin_lock_irq(&phba->hbalock);
3358         psli->sli_flag |= LPFC_PROCESS_LA;
3359         if (phba->sli_rev <= LPFC_SLI_REV3) {
3360                 control = readl(phba->HCregaddr);
3361                 control |= HC_LAINT_ENA;
3362                 writel(control, phba->HCregaddr);
3363                 readl(phba->HCregaddr); /* flush */
3364         }
3365         spin_unlock_irq(&phba->hbalock);
3366 }
3367 
3368 static void
3369 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3370 {
3371         lpfc_linkdown(phba);
3372         lpfc_enable_la(phba);
3373         lpfc_unregister_unused_fcf(phba);
3374         /* turn on Link Attention interrupts - no CLEAR_LA needed */
3375 }
3376 
3377 
3378 /*
3379  * This routine handles processing a READ_TOPOLOGY mailbox
3380  * command upon completion. It is setup in the LPFC_MBOXQ
3381  * as the completion routine when the command is
3382  * handed off to the SLI layer. SLI4 only.
3383  */
3384 void
3385 lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3386 {
3387         struct lpfc_vport *vport = pmb->vport;
3388         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3389         struct lpfc_mbx_read_top *la;
3390         struct lpfc_sli_ring *pring;
3391         MAILBOX_t *mb = &pmb->u.mb;
3392         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3393         uint8_t attn_type;
3394         unsigned long iflags;
3395 
3396         /* Unblock ELS traffic */
3397         pring = lpfc_phba_elsring(phba);
3398         if (pring)
3399                 pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3400 
3401         /* Check for error */
3402         if (mb->mbxStatus) {
3403                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3404                                 "1307 READ_LA mbox error x%x state x%x\n",
3405                                 mb->mbxStatus, vport->port_state);
3406                 lpfc_mbx_issue_link_down(phba);
3407                 phba->link_state = LPFC_HBA_ERROR;
3408                 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
3409         }
3410 
3411         la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3412         attn_type = bf_get(lpfc_mbx_read_top_att_type, la);
3413 
3414         memcpy(&phba->alpa_map[0], mp->virt, 128);
3415 
3416         spin_lock_irqsave(shost->host_lock, iflags);
3417         if (bf_get(lpfc_mbx_read_top_pb, la))
3418                 vport->fc_flag |= FC_BYPASSED_MODE;
3419         else
3420                 vport->fc_flag &= ~FC_BYPASSED_MODE;
3421         spin_unlock_irqrestore(shost->host_lock, iflags);
3422 
3423         if (phba->fc_eventTag <= la->eventTag) {
3424                 phba->fc_stat.LinkMultiEvent++;
3425                 if (attn_type == LPFC_ATT_LINK_UP)
3426                         if (phba->fc_eventTag != 0)
3427                                 lpfc_linkdown(phba);
3428         }
3429 
3430         phba->fc_eventTag = la->eventTag;
3431         if (phba->sli_rev < LPFC_SLI_REV4) {
3432                 spin_lock_irqsave(&phba->hbalock, iflags);
3433                 if (bf_get(lpfc_mbx_read_top_mm, la))
3434                         phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3435                 else
3436                         phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3437                 spin_unlock_irqrestore(&phba->hbalock, iflags);
3438         }
3439 
3440         phba->link_events++;
3441         if ((attn_type == LPFC_ATT_LINK_UP) &&
3442             !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
3443                 phba->fc_stat.LinkUp++;
3444                 if (phba->link_flag & LS_LOOPBACK_MODE) {
3445                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3446                                         "1306 Link Up Event in loop back mode "
3447                                         "x%x received Data: x%x x%x x%x x%x\n",
3448                                         la->eventTag, phba->fc_eventTag,
3449                                         bf_get(lpfc_mbx_read_top_alpa_granted,
3450                                                la),
3451                                         bf_get(lpfc_mbx_read_top_link_spd, la),
3452                                         phba->alpa_map[0]);
3453                 } else {
3454                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3455                                         "1303 Link Up Event x%x received "
3456                                         "Data: x%x x%x x%x x%x x%x x%x %d\n",
3457                                         la->eventTag, phba->fc_eventTag,
3458                                         bf_get(lpfc_mbx_read_top_alpa_granted,
3459                                                la),
3460                                         bf_get(lpfc_mbx_read_top_link_spd, la),
3461                                         phba->alpa_map[0],
3462                                         bf_get(lpfc_mbx_read_top_mm, la),
3463                                         bf_get(lpfc_mbx_read_top_fa, la),
3464                                         phba->wait_4_mlo_maint_flg);
3465                 }
3466                 lpfc_mbx_process_link_up(phba, la);
3467         } else if (attn_type == LPFC_ATT_LINK_DOWN ||
3468                    attn_type == LPFC_ATT_UNEXP_WWPN) {
3469                 phba->fc_stat.LinkDown++;
3470                 if (phba->link_flag & LS_LOOPBACK_MODE)
3471                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3472                                 "1308 Link Down Event in loop back mode "
3473                                 "x%x received "
3474                                 "Data: x%x x%x x%x\n",
3475                                 la->eventTag, phba->fc_eventTag,
3476                                 phba->pport->port_state, vport->fc_flag);
3477                 else if (attn_type == LPFC_ATT_UNEXP_WWPN)
3478                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3479                                 "1313 Link Down UNEXP WWPN Event x%x received "
3480                                 "Data: x%x x%x x%x x%x x%x\n",
3481                                 la->eventTag, phba->fc_eventTag,
3482                                 phba->pport->port_state, vport->fc_flag,
3483                                 bf_get(lpfc_mbx_read_top_mm, la),
3484                                 bf_get(lpfc_mbx_read_top_fa, la));
3485                 else
3486                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3487                                 "1305 Link Down Event x%x received "
3488                                 "Data: x%x x%x x%x x%x x%x\n",
3489                                 la->eventTag, phba->fc_eventTag,
3490                                 phba->pport->port_state, vport->fc_flag,
3491                                 bf_get(lpfc_mbx_read_top_mm, la),
3492                                 bf_get(lpfc_mbx_read_top_fa, la));
3493                 lpfc_mbx_issue_link_down(phba);
3494         }
3495         if (phba->sli.sli_flag & LPFC_MENLO_MAINT &&
3496             attn_type == LPFC_ATT_LINK_UP) {
3497                 if (phba->link_state != LPFC_LINK_DOWN) {
3498                         phba->fc_stat.LinkDown++;
3499                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3500                                 "1312 Link Down Event x%x received "
3501                                 "Data: x%x x%x x%x\n",
3502                                 la->eventTag, phba->fc_eventTag,
3503                                 phba->pport->port_state, vport->fc_flag);
3504                         lpfc_mbx_issue_link_down(phba);
3505                 } else
3506                         lpfc_enable_la(phba);
3507 
3508                 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3509                                 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3510                                 "Data: x%x x%x x%x\n",
3511                                 la->eventTag, phba->fc_eventTag,
3512                                 phba->pport->port_state, vport->fc_flag);
3513                 /*
3514                  * The cmnd that triggered this will be waiting for this
3515                  * signal.
3516                  */
3517                 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3518                 if (phba->wait_4_mlo_maint_flg) {
3519                         phba->wait_4_mlo_maint_flg = 0;
3520                         wake_up_interruptible(&phba->wait_4_mlo_m_q);
3521                 }
3522         }
3523 
3524         if ((phba->sli_rev < LPFC_SLI_REV4) &&
3525             bf_get(lpfc_mbx_read_top_fa, la)) {
3526                 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
3527                         lpfc_issue_clear_la(phba, vport);
3528                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3529                                 "1311 fa %d\n",
3530                                 bf_get(lpfc_mbx_read_top_fa, la));
3531         }
3532 
3533 lpfc_mbx_cmpl_read_topology_free_mbuf:
3534         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3535         kfree(mp);
3536         mempool_free(pmb, phba->mbox_mem_pool);
3537         return;
3538 }
3539 
3540 /*
3541  * This routine handles processing a REG_LOGIN mailbox
3542  * command upon completion. It is setup in the LPFC_MBOXQ
3543  * as the completion routine when the command is
3544  * handed off to the SLI layer.
3545  */
3546 void
3547 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3548 {
3549         struct lpfc_vport  *vport = pmb->vport;
3550         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3551         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3552         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3553 
3554         pmb->ctx_buf = NULL;
3555         pmb->ctx_ndlp = NULL;
3556 
3557         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3558                          "0002 rpi:%x DID:%x flg:%x %d map:%x x%px\n",
3559                          ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3560                          kref_read(&ndlp->kref),
3561                          ndlp->nlp_usg_map, ndlp);
3562         if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3563                 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3564 
3565         if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3566             ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
3567                 /* We rcvd a rscn after issuing this
3568                  * mbox reg login, we may have cycled
3569                  * back through the state and be
3570                  * back at reg login state so this
3571                  * mbox needs to be ignored becase
3572                  * there is another reg login in
3573                  * process.
3574                  */
3575                 spin_lock_irq(shost->host_lock);
3576                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3577                 spin_unlock_irq(shost->host_lock);
3578 
3579                 /*
3580                  * We cannot leave the RPI registered because
3581                  * if we go thru discovery again for this ndlp
3582                  * a subsequent REG_RPI will fail.
3583                  */
3584                 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3585                 lpfc_unreg_rpi(vport, ndlp);
3586         }
3587 
3588         /* Call state machine */
3589         lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
3590 
3591         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3592         kfree(mp);
3593         mempool_free(pmb, phba->mbox_mem_pool);
3594         /* decrement the node reference count held for this callback
3595          * function.
3596          */
3597         lpfc_nlp_put(ndlp);
3598 
3599         return;
3600 }
3601 
3602 static void
3603 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3604 {
3605         MAILBOX_t *mb = &pmb->u.mb;
3606         struct lpfc_vport *vport = pmb->vport;
3607         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3608 
3609         switch (mb->mbxStatus) {
3610         case 0x0011:
3611         case 0x0020:
3612                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3613                                  "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3614                                  mb->mbxStatus);
3615                 break;
3616         /* If VPI is busy, reset the HBA */
3617         case 0x9700:
3618                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3619                         "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3620                         vport->vpi, mb->mbxStatus);
3621                 if (!(phba->pport->load_flag & FC_UNLOADING))
3622                         lpfc_workq_post_event(phba, NULL, NULL,
3623                                 LPFC_EVT_RESET_HBA);
3624         }
3625         spin_lock_irq(shost->host_lock);
3626         vport->vpi_state &= ~LPFC_VPI_REGISTERED;
3627         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3628         spin_unlock_irq(shost->host_lock);
3629         vport->unreg_vpi_cmpl = VPORT_OK;
3630         mempool_free(pmb, phba->mbox_mem_pool);
3631         lpfc_cleanup_vports_rrqs(vport, NULL);
3632         /*
3633          * This shost reference might have been taken at the beginning of
3634          * lpfc_vport_delete()
3635          */
3636         if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
3637                 scsi_host_put(shost);
3638 }
3639 
3640 int
3641 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3642 {
3643         struct lpfc_hba  *phba = vport->phba;
3644         LPFC_MBOXQ_t *mbox;
3645         int rc;
3646 
3647         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3648         if (!mbox)
3649                 return 1;
3650 
3651         lpfc_unreg_vpi(phba, vport->vpi, mbox);
3652         mbox->vport = vport;
3653         mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
3654         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3655         if (rc == MBX_NOT_FINISHED) {
3656                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3657                                  "1800 Could not issue unreg_vpi\n");
3658                 mempool_free(mbox, phba->mbox_mem_pool);
3659                 vport->unreg_vpi_cmpl = VPORT_ERROR;
3660                 return rc;
3661         }
3662         return 0;
3663 }
3664 
3665 static void
3666 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3667 {
3668         struct lpfc_vport *vport = pmb->vport;
3669         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3670         MAILBOX_t *mb = &pmb->u.mb;
3671 
3672         switch (mb->mbxStatus) {
3673         case 0x0011:
3674         case 0x9601:
3675         case 0x9602:
3676                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3677                                  "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3678                                  mb->mbxStatus);
3679                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3680                 spin_lock_irq(shost->host_lock);
3681                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3682                 spin_unlock_irq(shost->host_lock);
3683                 vport->fc_myDID = 0;
3684 
3685                 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3686                     (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3687                         if (phba->nvmet_support)
3688                                 lpfc_nvmet_update_targetport(phba);
3689                         else
3690                                 lpfc_nvme_update_localport(vport);
3691                 }
3692                 goto out;
3693         }
3694 
3695         spin_lock_irq(shost->host_lock);
3696         vport->vpi_state |= LPFC_VPI_REGISTERED;
3697         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
3698         spin_unlock_irq(shost->host_lock);
3699         vport->num_disc_nodes = 0;
3700         /* go thru NPR list and issue ELS PLOGIs */
3701         if (vport->fc_npr_cnt)
3702                 lpfc_els_disc_plogi(vport);
3703 
3704         if (!vport->num_disc_nodes) {
3705                 spin_lock_irq(shost->host_lock);
3706                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3707                 spin_unlock_irq(shost->host_lock);
3708                 lpfc_can_disctmo(vport);
3709         }
3710         vport->port_state = LPFC_VPORT_READY;
3711 
3712 out:
3713         mempool_free(pmb, phba->mbox_mem_pool);
3714         return;
3715 }
3716 
3717 /**
3718  * lpfc_create_static_vport - Read HBA config region to create static vports.
3719  * @phba: pointer to lpfc hba data structure.
3720  *
3721  * This routine issue a DUMP mailbox command for config region 22 to get
3722  * the list of static vports to be created. The function create vports
3723  * based on the information returned from the HBA.
3724  **/
3725 void
3726 lpfc_create_static_vport(struct lpfc_hba *phba)
3727 {
3728         LPFC_MBOXQ_t *pmb = NULL;
3729         MAILBOX_t *mb;
3730         struct static_vport_info *vport_info;
3731         int mbx_wait_rc = 0, i;
3732         struct fc_vport_identifiers vport_id;
3733         struct fc_vport *new_fc_vport;
3734         struct Scsi_Host *shost;
3735         struct lpfc_vport *vport;
3736         uint16_t offset = 0;
3737         uint8_t *vport_buff;
3738         struct lpfc_dmabuf *mp;
3739         uint32_t byte_count = 0;
3740 
3741         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3742         if (!pmb) {
3743                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3744                                 "0542 lpfc_create_static_vport failed to"
3745                                 " allocate mailbox memory\n");
3746                 return;
3747         }
3748         memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
3749         mb = &pmb->u.mb;
3750 
3751         vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3752         if (!vport_info) {
3753                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3754                                 "0543 lpfc_create_static_vport failed to"
3755                                 " allocate vport_info\n");
3756                 mempool_free(pmb, phba->mbox_mem_pool);
3757                 return;
3758         }
3759 
3760         vport_buff = (uint8_t *) vport_info;
3761         do {
3762                 /* free dma buffer from previous round */
3763                 if (pmb->ctx_buf) {
3764                         mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3765                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3766                         kfree(mp);
3767                 }
3768                 if (lpfc_dump_static_vport(phba, pmb, offset))
3769                         goto out;
3770 
3771                 pmb->vport = phba->pport;
3772                 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3773                                                         LPFC_MBOX_TMO);
3774 
3775                 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
3776                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3777                                 "0544 lpfc_create_static_vport failed to"
3778                                 " issue dump mailbox command ret 0x%x "
3779                                 "status 0x%x\n",
3780                                 mbx_wait_rc, mb->mbxStatus);
3781                         goto out;
3782                 }
3783 
3784                 if (phba->sli_rev == LPFC_SLI_REV4) {
3785                         byte_count = pmb->u.mqe.un.mb_words[5];
3786                         mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3787                         if (byte_count > sizeof(struct static_vport_info) -
3788                                         offset)
3789                                 byte_count = sizeof(struct static_vport_info)
3790                                         - offset;
3791                         memcpy(vport_buff + offset, mp->virt, byte_count);
3792                         offset += byte_count;
3793                 } else {
3794                         if (mb->un.varDmp.word_cnt >
3795                                 sizeof(struct static_vport_info) - offset)
3796                                 mb->un.varDmp.word_cnt =
3797                                         sizeof(struct static_vport_info)
3798                                                 - offset;
3799                         byte_count = mb->un.varDmp.word_cnt;
3800                         lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3801                                 vport_buff + offset,
3802                                 byte_count);
3803 
3804                         offset += byte_count;
3805                 }
3806 
3807         } while (byte_count &&
3808                 offset < sizeof(struct static_vport_info));
3809 
3810 
3811         if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3812                 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3813                         != VPORT_INFO_REV)) {
3814                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3815                         "0545 lpfc_create_static_vport bad"
3816                         " information header 0x%x 0x%x\n",
3817                         le32_to_cpu(vport_info->signature),
3818                         le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3819 
3820                 goto out;
3821         }
3822 
3823         shost = lpfc_shost_from_vport(phba->pport);
3824 
3825         for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3826                 memset(&vport_id, 0, sizeof(vport_id));
3827                 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3828                 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3829                 if (!vport_id.port_name || !vport_id.node_name)
3830                         continue;
3831 
3832                 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3833                 vport_id.vport_type = FC_PORTTYPE_NPIV;
3834                 vport_id.disable = false;
3835                 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3836 
3837                 if (!new_fc_vport) {
3838                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3839                                 "0546 lpfc_create_static_vport failed to"
3840                                 " create vport\n");
3841                         continue;
3842                 }
3843 
3844                 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3845                 vport->vport_flag |= STATIC_VPORT;
3846         }
3847 
3848 out:
3849         kfree(vport_info);
3850         if (mbx_wait_rc != MBX_TIMEOUT) {
3851                 if (pmb->ctx_buf) {
3852                         mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3853                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3854                         kfree(mp);
3855                 }
3856                 mempool_free(pmb, phba->mbox_mem_pool);
3857         }
3858 
3859         return;
3860 }
3861 
3862 /*
3863  * This routine handles processing a Fabric REG_LOGIN mailbox
3864  * command upon completion. It is setup in the LPFC_MBOXQ
3865  * as the completion routine when the command is
3866  * handed off to the SLI layer.
3867  */
3868 void
3869 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3870 {
3871         struct lpfc_vport *vport = pmb->vport;
3872         MAILBOX_t *mb = &pmb->u.mb;
3873         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3874         struct lpfc_nodelist *ndlp;
3875         struct Scsi_Host *shost;
3876 
3877         ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3878         pmb->ctx_ndlp = NULL;
3879         pmb->ctx_buf = NULL;
3880 
3881         if (mb->mbxStatus) {
3882                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3883                                  "0258 Register Fabric login error: 0x%x\n",
3884                                  mb->mbxStatus);
3885                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3886                 kfree(mp);
3887                 mempool_free(pmb, phba->mbox_mem_pool);
3888 
3889                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3890                         /* FLOGI failed, use loop map to make discovery list */
3891                         lpfc_disc_list_loopmap(vport);
3892 
3893                         /* Start discovery */
3894                         lpfc_disc_start(vport);
3895                         /* Decrement the reference count to ndlp after the
3896                          * reference to the ndlp are done.
3897                          */
3898                         lpfc_nlp_put(ndlp);
3899                         return;
3900                 }
3901 
3902                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3903                 /* Decrement the reference count to ndlp after the reference
3904                  * to the ndlp are done.
3905                  */
3906                 lpfc_nlp_put(ndlp);
3907                 return;
3908         }
3909 
3910         if (phba->sli_rev < LPFC_SLI_REV4)
3911                 ndlp->nlp_rpi = mb->un.varWords[0];
3912         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3913         ndlp->nlp_type |= NLP_FABRIC;
3914         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
3915 
3916         if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
3917                 /* when physical port receive logo donot start
3918                  * vport discovery */
3919                 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3920                         lpfc_start_fdiscs(phba);
3921                 else {
3922                         shost = lpfc_shost_from_vport(vport);
3923                         spin_lock_irq(shost->host_lock);
3924                         vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
3925                         spin_unlock_irq(shost->host_lock);
3926                 }
3927                 lpfc_do_scr_ns_plogi(phba, vport);
3928         }
3929 
3930         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3931         kfree(mp);
3932         mempool_free(pmb, phba->mbox_mem_pool);
3933 
3934         /* Drop the reference count from the mbox at the end after
3935          * all the current reference to the ndlp have been done.
3936          */
3937         lpfc_nlp_put(ndlp);
3938         return;
3939 }
3940 
3941  /*
3942   * This routine will issue a GID_FT for each FC4 Type supported
3943   * by the driver. ALL GID_FTs must complete before discovery is started.
3944   */
3945 int
3946 lpfc_issue_gidft(struct lpfc_vport *vport)
3947 {
3948         /* Good status, issue CT Request to NameServer */
3949         if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3950             (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
3951                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
3952                         /* Cannot issue NameServer FCP Query, so finish up
3953                          * discovery
3954                          */
3955                         lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3956                                          "0604 %s FC TYPE %x %s\n",
3957                                          "Failed to issue GID_FT to ",
3958                                          FC_TYPE_FCP,
3959                                          "Finishing discovery.");
3960                         return 0;
3961                 }
3962                 vport->gidft_inp++;
3963         }
3964 
3965         if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3966             (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3967                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
3968                         /* Cannot issue NameServer NVME Query, so finish up
3969                          * discovery
3970                          */
3971                         lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3972                                          "0605 %s FC_TYPE %x %s %d\n",
3973                                          "Failed to issue GID_FT to ",
3974                                          FC_TYPE_NVME,
3975                                          "Finishing discovery: gidftinp ",
3976                                          vport->gidft_inp);
3977                         if (vport->gidft_inp == 0)
3978                                 return 0;
3979                 } else
3980                         vport->gidft_inp++;
3981         }
3982         return vport->gidft_inp;
3983 }
3984 
3985 /**
3986  * lpfc_issue_gidpt - issue a GID_PT for all N_Ports
3987  * @vport: The virtual port for which this call is being executed.
3988  *
3989  * This routine will issue a GID_PT to get a list of all N_Ports
3990  *
3991  * Return value :
3992  *   0 - Failure to issue a GID_PT
3993  *   1 - GID_PT issued
3994  **/
3995 int
3996 lpfc_issue_gidpt(struct lpfc_vport *vport)
3997 {
3998         /* Good status, issue CT Request to NameServer */
3999         if (lpfc_ns_cmd(vport, SLI_CTNS_GID_PT, 0, GID_PT_N_PORT)) {
4000                 /* Cannot issue NameServer FCP Query, so finish up
4001                  * discovery
4002                  */
4003                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
4004                                  "0606 %s Port TYPE %x %s\n",
4005                                  "Failed to issue GID_PT to ",
4006                                  GID_PT_N_PORT,
4007                                  "Finishing discovery.");
4008                 return 0;
4009         }
4010         vport->gidft_inp++;
4011         return 1;
4012 }
4013 
4014 /*
4015  * This routine handles processing a NameServer REG_LOGIN mailbox
4016  * command upon completion. It is setup in the LPFC_MBOXQ
4017  * as the completion routine when the command is
4018  * handed off to the SLI layer.
4019  */
4020 void
4021 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4022 {
4023         MAILBOX_t *mb = &pmb->u.mb;
4024         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
4025         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4026         struct lpfc_vport *vport = pmb->vport;
4027 
4028         pmb->ctx_buf = NULL;
4029         pmb->ctx_ndlp = NULL;
4030         vport->gidft_inp = 0;
4031 
4032         if (mb->mbxStatus) {
4033                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4034                                  "0260 Register NameServer error: 0x%x\n",
4035                                  mb->mbxStatus);
4036 
4037 out:
4038                 /* decrement the node reference count held for this
4039                  * callback function.
4040                  */
4041                 lpfc_nlp_put(ndlp);
4042                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4043                 kfree(mp);
4044                 mempool_free(pmb, phba->mbox_mem_pool);
4045 
4046                 /* If no other thread is using the ndlp, free it */
4047                 lpfc_nlp_not_used(ndlp);
4048 
4049                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4050                         /*
4051                          * RegLogin failed, use loop map to make discovery
4052                          * list
4053                          */
4054                         lpfc_disc_list_loopmap(vport);
4055 
4056                         /* Start discovery */
4057                         lpfc_disc_start(vport);
4058                         return;
4059                 }
4060                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4061                 return;
4062         }
4063 
4064         if (phba->sli_rev < LPFC_SLI_REV4)
4065                 ndlp->nlp_rpi = mb->un.varWords[0];
4066         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4067         ndlp->nlp_type |= NLP_FABRIC;
4068         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4069         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4070                          "0003 rpi:%x DID:%x flg:%x %d map%x x%px\n",
4071                          ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4072                          kref_read(&ndlp->kref),
4073                          ndlp->nlp_usg_map, ndlp);
4074 
4075         if (vport->port_state < LPFC_VPORT_READY) {
4076                 /* Link up discovery requires Fabric registration. */
4077                 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
4078                 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
4079                 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
4080                 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
4081 
4082                 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4083                     (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
4084                         lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
4085 
4086                 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4087                     (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
4088                         lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
4089                                     FC_TYPE_NVME);
4090 
4091                 /* Issue SCR just before NameServer GID_FT Query */
4092                 lpfc_issue_els_scr(vport, SCR_DID, 0);
4093         }
4094 
4095         vport->fc_ns_retry = 0;
4096         if (lpfc_issue_gidft(vport) == 0)
4097                 goto out;
4098 
4099         /*
4100          * At this point in time we may need to wait for multiple
4101          * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
4102          *
4103          * decrement the node reference count held for this
4104          * callback function.
4105          */
4106         lpfc_nlp_put(ndlp);
4107         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4108         kfree(mp);
4109         mempool_free(pmb, phba->mbox_mem_pool);
4110 
4111         return;
4112 }
4113 
4114 static void
4115 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4116 {
4117         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4118         struct fc_rport  *rport;
4119         struct lpfc_rport_data *rdata;
4120         struct fc_rport_identifiers rport_ids;
4121         struct lpfc_hba  *phba = vport->phba;
4122 
4123         if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4124                 return;
4125 
4126         /* Remote port has reappeared. Re-register w/ FC transport */
4127         rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4128         rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
4129         rport_ids.port_id = ndlp->nlp_DID;
4130         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
4131 
4132         /*
4133          * We leave our node pointer in rport->dd_data when we unregister a
4134          * FCP target port.  But fc_remote_port_add zeros the space to which
4135          * rport->dd_data points.  So, if we're reusing a previously
4136          * registered port, drop the reference that we took the last time we
4137          * registered the port.
4138          */
4139         rport = ndlp->rport;
4140         if (rport) {
4141                 rdata = rport->dd_data;
4142                 /* break the link before dropping the ref */
4143                 ndlp->rport = NULL;
4144                 if (rdata) {
4145                         if (rdata->pnode == ndlp)
4146                                 lpfc_nlp_put(ndlp);
4147                         rdata->pnode = NULL;
4148                 }
4149                 /* drop reference for earlier registeration */
4150                 put_device(&rport->dev);
4151         }
4152 
4153         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4154                 "rport add:       did:x%x flg:x%x type x%x",
4155                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4156 
4157         /* Don't add the remote port if unloading. */
4158         if (vport->load_flag & FC_UNLOADING)
4159                 return;
4160 
4161         ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
4162         if (!rport || !get_device(&rport->dev)) {
4163                 dev_printk(KERN_WARNING, &phba->pcidev->dev,
4164                            "Warning: fc_remote_port_add failed\n");
4165                 return;
4166         }
4167 
4168         /* initialize static port data */
4169         rport->maxframe_size = ndlp->nlp_maxframe;
4170         rport->supported_classes = ndlp->nlp_class_sup;
4171         rdata = rport->dd_data;
4172         rdata->pnode = lpfc_nlp_get(ndlp);
4173 
4174         if (ndlp->nlp_type & NLP_FCP_TARGET)
4175                 rport_ids.roles |= FC_PORT_ROLE_FCP_TARGET;
4176         if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4177                 rport_ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
4178         if (ndlp->nlp_type & NLP_NVME_INITIATOR)
4179                 rport_ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
4180         if (ndlp->nlp_type & NLP_NVME_TARGET)
4181                 rport_ids.roles |= FC_PORT_ROLE_NVME_TARGET;
4182         if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
4183                 rport_ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
4184 
4185         if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
4186                 fc_remote_port_rolechg(rport, rport_ids.roles);
4187 
4188         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4189                          "3183 rport register x%06x, rport x%px role x%x\n",
4190                          ndlp->nlp_DID, rport, rport_ids.roles);
4191 
4192         if ((rport->scsi_target_id != -1) &&
4193             (rport->scsi_target_id < LPFC_MAX_TARGET)) {
4194                 ndlp->nlp_sid = rport->scsi_target_id;
4195         }
4196         return;
4197 }
4198 
4199 static void
4200 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
4201 {
4202         struct fc_rport *rport = ndlp->rport;
4203         struct lpfc_vport *vport = ndlp->vport;
4204 
4205         if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4206                 return;
4207 
4208         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4209                 "rport delete:    did:x%x flg:x%x type x%x",
4210                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4211 
4212         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4213                          "3184 rport unregister x%06x, rport x%px\n",
4214                          ndlp->nlp_DID, rport);
4215 
4216         fc_remote_port_delete(rport);
4217 
4218         return;
4219 }
4220 
4221 static void
4222 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
4223 {
4224         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4225         unsigned long iflags;
4226 
4227         spin_lock_irqsave(shost->host_lock, iflags);
4228         switch (state) {
4229         case NLP_STE_UNUSED_NODE:
4230                 vport->fc_unused_cnt += count;
4231                 break;
4232         case NLP_STE_PLOGI_ISSUE:
4233                 vport->fc_plogi_cnt += count;
4234                 break;
4235         case NLP_STE_ADISC_ISSUE:
4236                 vport->fc_adisc_cnt += count;
4237                 break;
4238         case NLP_STE_REG_LOGIN_ISSUE:
4239                 vport->fc_reglogin_cnt += count;
4240                 break;
4241         case NLP_STE_PRLI_ISSUE:
4242                 vport->fc_prli_cnt += count;
4243                 break;
4244         case NLP_STE_UNMAPPED_NODE:
4245                 vport->fc_unmap_cnt += count;
4246                 break;
4247         case NLP_STE_MAPPED_NODE:
4248                 vport->fc_map_cnt += count;
4249                 break;
4250         case NLP_STE_NPR_NODE:
4251                 if (vport->fc_npr_cnt == 0 && count == -1)
4252                         vport->fc_npr_cnt = 0;
4253                 else
4254                         vport->fc_npr_cnt += count;
4255                 break;
4256         }
4257         spin_unlock_irqrestore(shost->host_lock, iflags);
4258 }
4259 
4260 static void
4261 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4262                        int old_state, int new_state)
4263 {
4264         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4265 
4266         if (new_state == NLP_STE_UNMAPPED_NODE) {
4267                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4268                 ndlp->nlp_type |= NLP_FC_NODE;
4269         }
4270         if (new_state == NLP_STE_MAPPED_NODE)
4271                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4272         if (new_state == NLP_STE_NPR_NODE)
4273                 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
4274 
4275         /* FCP and NVME Transport interface */
4276         if ((old_state == NLP_STE_MAPPED_NODE ||
4277              old_state == NLP_STE_UNMAPPED_NODE)) {
4278                 if (ndlp->rport) {
4279                         vport->phba->nport_event_cnt++;
4280                         lpfc_unregister_remote_port(ndlp);
4281                 }
4282 
4283                 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4284                         vport->phba->nport_event_cnt++;
4285                         if (vport->phba->nvmet_support == 0) {
4286                                 /* Start devloss if target. */
4287                                 if (ndlp->nlp_type & NLP_NVME_TARGET)
4288                                         lpfc_nvme_unregister_port(vport, ndlp);
4289                         } else {
4290                                 /* NVMET has no upcall. */
4291                                 lpfc_nlp_put(ndlp);
4292                         }
4293                 }
4294         }
4295 
4296         /* FCP and NVME Transport interfaces */
4297 
4298         if (new_state ==  NLP_STE_MAPPED_NODE ||
4299             new_state == NLP_STE_UNMAPPED_NODE) {
4300                 if (ndlp->nlp_fc4_type ||
4301                     ndlp->nlp_DID == Fabric_DID ||
4302                     ndlp->nlp_DID == NameServer_DID ||
4303                     ndlp->nlp_DID == FDMI_DID) {
4304                         vport->phba->nport_event_cnt++;
4305                         /*
4306                          * Tell the fc transport about the port, if we haven't
4307                          * already. If we have, and it's a scsi entity, be
4308                          */
4309                         lpfc_register_remote_port(vport, ndlp);
4310                 }
4311                 /* Notify the NVME transport of this new rport. */
4312                 if (vport->phba->sli_rev >= LPFC_SLI_REV4 &&
4313                     ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4314                         if (vport->phba->nvmet_support == 0) {
4315                                 /* Register this rport with the transport.
4316                                  * Only NVME Target Rports are registered with
4317                                  * the transport.
4318                                  */
4319                                 if (ndlp->nlp_type & NLP_NVME_TARGET) {
4320                                         vport->phba->nport_event_cnt++;
4321                                         lpfc_nvme_register_port(vport, ndlp);
4322                                 }
4323                         } else {
4324                                 /* Just take an NDLP ref count since the
4325                                  * target does not register rports.
4326                                  */
4327                                 lpfc_nlp_get(ndlp);
4328                         }
4329                 }
4330         }
4331 
4332         if ((new_state ==  NLP_STE_MAPPED_NODE) &&
4333                 (vport->stat_data_enabled)) {
4334                 /*
4335                  * A new target is discovered, if there is no buffer for
4336                  * statistical data collection allocate buffer.
4337                  */
4338                 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4339                                          sizeof(struct lpfc_scsicmd_bkt),
4340                                          GFP_KERNEL);
4341 
4342                 if (!ndlp->lat_data)
4343                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4344                                 "0286 lpfc_nlp_state_cleanup failed to "
4345                                 "allocate statistical data buffer DID "
4346                                 "0x%x\n", ndlp->nlp_DID);
4347         }
4348         /*
4349          * If the node just added to Mapped list was an FCP target,
4350          * but the remote port registration failed or assigned a target
4351          * id outside the presentable range - move the node to the
4352          * Unmapped List.
4353          */
4354         if ((new_state == NLP_STE_MAPPED_NODE) &&
4355             (ndlp->nlp_type & NLP_FCP_TARGET) &&
4356             (!ndlp->rport ||
4357              ndlp->rport->scsi_target_id == -1 ||
4358              ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
4359                 spin_lock_irq(shost->host_lock);
4360                 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
4361                 spin_unlock_irq(shost->host_lock);
4362                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4363         }
4364 }
4365 
4366 static char *
4367 lpfc_nlp_state_name(char *buffer, size_t size, int state)
4368 {
4369         static char *states[] = {
4370                 [NLP_STE_UNUSED_NODE] = "UNUSED",
4371                 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4372                 [NLP_STE_ADISC_ISSUE] = "ADISC",
4373                 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4374                 [NLP_STE_PRLI_ISSUE] = "PRLI",
4375                 [NLP_STE_LOGO_ISSUE] = "LOGO",
4376                 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4377                 [NLP_STE_MAPPED_NODE] = "MAPPED",
4378                 [NLP_STE_NPR_NODE] = "NPR",
4379         };
4380 
4381         if (state < NLP_STE_MAX_STATE && states[state])
4382                 strlcpy(buffer, states[state], size);
4383         else
4384                 snprintf(buffer, size, "unknown (%d)", state);
4385         return buffer;
4386 }
4387 
4388 void
4389 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4390                    int state)
4391 {
4392         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4393         int  old_state = ndlp->nlp_state;
4394         char name1[16], name2[16];
4395 
4396         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4397                          "0904 NPort state transition x%06x, %s -> %s\n",
4398                          ndlp->nlp_DID,
4399                          lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4400                          lpfc_nlp_state_name(name2, sizeof(name2), state));
4401 
4402         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4403                 "node statechg    did:x%x old:%d ste:%d",
4404                 ndlp->nlp_DID, old_state, state);
4405 
4406         if (old_state == NLP_STE_NPR_NODE &&
4407             state != NLP_STE_NPR_NODE)
4408                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
4409         if (old_state == NLP_STE_UNMAPPED_NODE) {
4410                 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4411                 ndlp->nlp_type &= ~NLP_FC_NODE;
4412         }
4413 
4414         if (list_empty(&ndlp->nlp_listp)) {
4415                 spin_lock_irq(shost->host_lock);
4416                 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4417                 spin_unlock_irq(shost->host_lock);
4418         } else if (old_state)
4419                 lpfc_nlp_counters(vport, old_state, -1);
4420 
4421         ndlp->nlp_state = state;
4422         lpfc_nlp_counters(vport, state, 1);
4423         lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
4424 }
4425 
4426 void
4427 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4428 {
4429         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4430 
4431         if (list_empty(&ndlp->nlp_listp)) {
4432                 spin_lock_irq(shost->host_lock);
4433                 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4434                 spin_unlock_irq(shost->host_lock);
4435         }
4436 }
4437 
4438 void
4439 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4440 {
4441         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4442 
4443         lpfc_cancel_retry_delay_tmo(vport, ndlp);
4444         if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4445                 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4446         spin_lock_irq(shost->host_lock);
4447         list_del_init(&ndlp->nlp_listp);
4448         spin_unlock_irq(shost->host_lock);
4449         lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4450                                 NLP_STE_UNUSED_NODE);
4451 }
4452 
4453 static void
4454 lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4455 {
4456         lpfc_cancel_retry_delay_tmo(vport, ndlp);
4457         if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4458                 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4459         lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4460                                 NLP_STE_UNUSED_NODE);
4461 }
4462 /**
4463  * lpfc_initialize_node - Initialize all fields of node object
4464  * @vport: Pointer to Virtual Port object.
4465  * @ndlp: Pointer to FC node object.
4466  * @did: FC_ID of the node.
4467  *
4468  * This function is always called when node object need to be initialized.
4469  * It initializes all the fields of the node object. Although the reference
4470  * to phba from @ndlp can be obtained indirectly through it's reference to
4471  * @vport, a direct reference to phba is taken here by @ndlp. This is due
4472  * to the life-span of the @ndlp might go beyond the existence of @vport as
4473  * the final release of ndlp is determined by its reference count. And, the
4474  * operation on @ndlp needs the reference to phba.
4475  **/
4476 static inline void
4477 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4478         uint32_t did)
4479 {
4480         INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4481         INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4482         timer_setup(&ndlp->nlp_delayfunc, lpfc_els_retry_delay, 0);
4483         ndlp->nlp_DID = did;
4484         ndlp->vport = vport;
4485         ndlp->phba = vport->phba;
4486         ndlp->nlp_sid = NLP_NO_SID;
4487         ndlp->nlp_fc4_type = NLP_FC4_NONE;
4488         kref_init(&ndlp->kref);
4489         NLP_INT_NODE_ACT(ndlp);
4490         atomic_set(&ndlp->cmd_pending, 0);
4491         ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
4492         ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4493 }
4494 
4495 struct lpfc_nodelist *
4496 lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4497                  int state)
4498 {
4499         struct lpfc_hba *phba = vport->phba;
4500         uint32_t did, flag;
4501         unsigned long flags;
4502         unsigned long *active_rrqs_xri_bitmap = NULL;
4503         int rpi = LPFC_RPI_ALLOC_ERROR;
4504         uint32_t defer_did = 0;
4505 
4506         if (!ndlp)
4507                 return NULL;
4508 
4509         if (phba->sli_rev == LPFC_SLI_REV4) {
4510                 if (ndlp->nlp_rpi == LPFC_RPI_ALLOC_ERROR)
4511                         rpi = lpfc_sli4_alloc_rpi(vport->phba);
4512                 else
4513                         rpi = ndlp->nlp_rpi;
4514 
4515                 if (rpi == LPFC_RPI_ALLOC_ERROR) {
4516                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4517                                          "0359 %s: ndlp:x%px "
4518                                          "usgmap:x%x refcnt:%d FAILED RPI "
4519                                          " ALLOC\n",
4520                                          __func__,
4521                                          (void *)ndlp, ndlp->nlp_usg_map,
4522                                          kref_read(&ndlp->kref));
4523                         return NULL;
4524                 }
4525         }
4526 
4527         spin_lock_irqsave(&phba->ndlp_lock, flags);
4528         /* The ndlp should not be in memory free mode */
4529         if (NLP_CHK_FREE_REQ(ndlp)) {
4530                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4531                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4532                                 "0277 %s: ndlp:x%px "
4533                                 "usgmap:x%x refcnt:%d\n",
4534                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
4535                                 kref_read(&ndlp->kref));
4536                 goto free_rpi;
4537         }
4538         /* The ndlp should not already be in active mode */
4539         if (NLP_CHK_NODE_ACT(ndlp)) {
4540                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4541                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4542                                 "0278 %s: ndlp:x%px "
4543                                 "usgmap:x%x refcnt:%d\n",
4544                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
4545                                 kref_read(&ndlp->kref));
4546                 goto free_rpi;
4547         }
4548 
4549         /* First preserve the orginal DID, xri_bitmap and some flags */
4550         did = ndlp->nlp_DID;
4551         flag = (ndlp->nlp_flag & NLP_UNREG_INP);
4552         if (flag & NLP_UNREG_INP)
4553                 defer_did = ndlp->nlp_defer_did;
4554         if (phba->sli_rev == LPFC_SLI_REV4)
4555                 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
4556 
4557         /* Zero ndlp except of ndlp linked list pointer */
4558         memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4559                 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
4560 
4561         /* Next reinitialize and restore saved objects */
4562         lpfc_initialize_node(vport, ndlp, did);
4563         ndlp->nlp_flag |= flag;
4564         if (flag & NLP_UNREG_INP)
4565                 ndlp->nlp_defer_did = defer_did;
4566         if (phba->sli_rev == LPFC_SLI_REV4)
4567                 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4568 
4569         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4570         if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4571                 ndlp->nlp_rpi = rpi;
4572                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4573                                  "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4574                                  "map:%x x%px\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4575                                  ndlp->nlp_flag,
4576                                  kref_read(&ndlp->kref),
4577                                  ndlp->nlp_usg_map, ndlp);
4578         }
4579 
4580 
4581         if (state != NLP_STE_UNUSED_NODE)
4582                 lpfc_nlp_set_state(vport, ndlp, state);
4583         else
4584                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4585                                  "0013 rpi:%x DID:%x flg:%x refcnt:%d "
4586                                  "map:%x x%px STATE=UNUSED\n",
4587                                  ndlp->nlp_rpi, ndlp->nlp_DID,
4588                                  ndlp->nlp_flag,
4589                                  kref_read(&ndlp->kref),
4590                                  ndlp->nlp_usg_map, ndlp);
4591 
4592         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4593                 "node enable:       did:x%x",
4594                 ndlp->nlp_DID, 0, 0);
4595         return ndlp;
4596 
4597 free_rpi:
4598         if (phba->sli_rev == LPFC_SLI_REV4)
4599                 lpfc_sli4_free_rpi(vport->phba, rpi);
4600         return NULL;
4601 }
4602 
4603 void
4604 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4605 {
4606         /*
4607          * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
4608          * be used if we wish to issue the "last" lpfc_nlp_put() to remove
4609          * the ndlp from the vport. The ndlp marked as UNUSED on the list
4610          * until ALL other outstanding threads have completed. We check
4611          * that the ndlp not already in the UNUSED state before we proceed.
4612          */
4613         if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4614                 return;
4615         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
4616         if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4617                 lpfc_cleanup_vports_rrqs(vport, ndlp);
4618                 lpfc_unreg_rpi(vport, ndlp);
4619         }
4620 
4621         lpfc_nlp_put(ndlp);
4622         return;
4623 }
4624 
4625 /*
4626  * Start / ReStart rescue timer for Discovery / RSCN handling
4627  */
4628 void
4629 lpfc_set_disctmo(struct lpfc_vport *vport)
4630 {
4631         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4632         struct lpfc_hba  *phba = vport->phba;
4633         uint32_t tmo;
4634 
4635         if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
4636                 /* For FAN, timeout should be greater than edtov */
4637                 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4638         } else {
4639                 /* Normal discovery timeout should be > than ELS/CT timeout
4640                  * FC spec states we need 3 * ratov for CT requests
4641                  */
4642                 tmo = ((phba->fc_ratov * 3) + 3);
4643         }
4644 
4645 
4646         if (!timer_pending(&vport->fc_disctmo)) {
4647                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4648                         "set disc timer:  tmo:x%x state:x%x flg:x%x",
4649                         tmo, vport->port_state, vport->fc_flag);
4650         }
4651 
4652         mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
4653         spin_lock_irq(shost->host_lock);
4654         vport->fc_flag |= FC_DISC_TMO;
4655         spin_unlock_irq(shost->host_lock);
4656 
4657         /* Start Discovery Timer state <hba_state> */
4658         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4659                          "0247 Start Discovery Timer state x%x "
4660                          "Data: x%x x%lx x%x x%x\n",
4661                          vport->port_state, tmo,
4662                          (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4663                          vport->fc_adisc_cnt);
4664 
4665         return;
4666 }
4667 
4668 /*
4669  * Cancel rescue timer for Discovery / RSCN handling
4670  */
4671 int
4672 lpfc_can_disctmo(struct lpfc_vport *vport)
4673 {
4674         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4675         unsigned long iflags;
4676 
4677         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4678                 "can disc timer:  state:x%x rtry:x%x flg:x%x",
4679                 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4680 
4681         /* Turn off discovery timer if its running */
4682         if (vport->fc_flag & FC_DISC_TMO) {
4683                 spin_lock_irqsave(shost->host_lock, iflags);
4684                 vport->fc_flag &= ~FC_DISC_TMO;
4685                 spin_unlock_irqrestore(shost->host_lock, iflags);
4686                 del_timer_sync(&vport->fc_disctmo);
4687                 spin_lock_irqsave(&vport->work_port_lock, iflags);
4688                 vport->work_port_events &= ~WORKER_DISC_TMO;
4689                 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
4690         }
4691 
4692         /* Cancel Discovery Timer state <hba_state> */
4693         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4694                          "0248 Cancel Discovery Timer state x%x "
4695                          "Data: x%x x%x x%x\n",
4696                          vport->port_state, vport->fc_flag,
4697                          vport->fc_plogi_cnt, vport->fc_adisc_cnt);
4698         return 0;
4699 }
4700 
4701 /*
4702  * Check specified ring for outstanding IOCB on the SLI queue
4703  * Return true if iocb matches the specified nport
4704  */
4705 int
4706 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4707                     struct lpfc_sli_ring *pring,
4708                     struct lpfc_iocbq *iocb,
4709                     struct lpfc_nodelist *ndlp)
4710 {
4711         IOCB_t *icmd = &iocb->iocb;
4712         struct lpfc_vport    *vport = ndlp->vport;
4713 
4714         if (iocb->vport != vport)
4715                 return 0;
4716 
4717         if (pring->ringno == LPFC_ELS_RING) {
4718                 switch (icmd->ulpCommand) {
4719                 case CMD_GEN_REQUEST64_CR:
4720                         if (iocb->context_un.ndlp == ndlp)
4721                                 return 1;
4722                         /* fall through */
4723                 case CMD_ELS_REQUEST64_CR:
4724                         if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4725                                 return 1;
4726                         /* fall through */
4727                 case CMD_XMIT_ELS_RSP64_CX:
4728                         if (iocb->context1 == (uint8_t *) ndlp)
4729                                 return 1;
4730                         /* fall through */
4731                 }
4732         } else if (pring->ringno == LPFC_FCP_RING) {
4733                 /* Skip match check if waiting to relogin to FCP target */
4734                 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
4735                     (ndlp->nlp_flag & NLP_DELAY_TMO)) {
4736                         return 0;
4737                 }
4738                 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
4739                         return 1;
4740                 }
4741         }
4742         return 0;
4743 }
4744 
4745 static void
4746 __lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4747                 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4748                 struct list_head *dequeue_list)
4749 {
4750         struct lpfc_iocbq *iocb, *next_iocb;
4751 
4752         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4753                 /* Check to see if iocb matches the nport */
4754                 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4755                         /* match, dequeue */
4756                         list_move_tail(&iocb->list, dequeue_list);
4757         }
4758 }
4759 
4760 static void
4761 lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4762                 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4763 {
4764         struct lpfc_sli *psli = &phba->sli;
4765         uint32_t i;
4766 
4767         spin_lock_irq(&phba->hbalock);
4768         for (i = 0; i < psli->num_rings; i++)
4769                 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4770                                                 dequeue_list);
4771         spin_unlock_irq(&phba->hbalock);
4772 }
4773 
4774 static void
4775 lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4776                 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4777 {
4778         struct lpfc_sli_ring *pring;
4779         struct lpfc_queue *qp = NULL;
4780 
4781         spin_lock_irq(&phba->hbalock);
4782         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4783                 pring = qp->pring;
4784                 if (!pring)
4785                         continue;
4786                 spin_lock(&pring->ring_lock);
4787                 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
4788                 spin_unlock(&pring->ring_lock);
4789         }
4790         spin_unlock_irq(&phba->hbalock);
4791 }
4792 
4793 /*
4794  * Free resources / clean up outstanding I/Os
4795  * associated with nlp_rpi in the LPFC_NODELIST entry.
4796  */
4797 static int
4798 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
4799 {
4800         LIST_HEAD(completions);
4801 
4802         lpfc_fabric_abort_nport(ndlp);
4803 
4804         /*
4805          * Everything that matches on txcmplq will be returned
4806          * by firmware with a no rpi error.
4807          */
4808         if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
4809                 if (phba->sli_rev != LPFC_SLI_REV4)
4810                         lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4811                 else
4812                         lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
4813         }
4814 
4815         /* Cancel all the IOCBs from the completions list */
4816         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4817                               IOERR_SLI_ABORTED);
4818 
4819         return 0;
4820 }
4821 
4822 /**
4823  * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4824  * @phba: Pointer to HBA context object.
4825  * @pmb: Pointer to mailbox object.
4826  *
4827  * This function will issue an ELS LOGO command after completing
4828  * the UNREG_RPI.
4829  **/
4830 static void
4831 lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4832 {
4833         struct lpfc_vport  *vport = pmb->vport;
4834         struct lpfc_nodelist *ndlp;
4835 
4836         ndlp = (struct lpfc_nodelist *)(pmb->ctx_ndlp);
4837         if (!ndlp)
4838                 return;
4839         lpfc_issue_els_logo(vport, ndlp, 0);
4840         mempool_free(pmb, phba->mbox_mem_pool);
4841 
4842         /* Check to see if there are any deferred events to process */
4843         if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
4844             (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
4845                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4846                                  "1434 UNREG cmpl deferred logo x%x "
4847                                  "on NPort x%x Data: x%x x%px\n",
4848                                  ndlp->nlp_rpi, ndlp->nlp_DID,
4849                                  ndlp->nlp_defer_did, ndlp);
4850 
4851                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
4852                 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4853                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4854         } else {
4855                 if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
4856                         lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
4857                         ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
4858                 }
4859                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
4860         }
4861 }
4862 
4863 /*
4864  * Sets the mailbox completion handler to be used for the
4865  * unreg_rpi command. The handler varies based on the state of
4866  * the port and what will be happening to the rpi next.
4867  */
4868 static void
4869 lpfc_set_unreg_login_mbx_cmpl(struct lpfc_hba *phba, struct lpfc_vport *vport,
4870         struct lpfc_nodelist *ndlp, LPFC_MBOXQ_t *mbox)
4871 {
4872         unsigned long iflags;
4873 
4874         if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4875                 mbox->ctx_ndlp = ndlp;
4876                 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4877 
4878         } else if (phba->sli_rev == LPFC_SLI_REV4 &&
4879                    (!(vport->load_flag & FC_UNLOADING)) &&
4880                     (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
4881                                       LPFC_SLI_INTF_IF_TYPE_2) &&
4882                     (kref_read(&ndlp->kref) > 0)) {
4883                 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
4884                 mbox->mbox_cmpl = lpfc_sli4_unreg_rpi_cmpl_clr;
4885         } else {
4886                 if (vport->load_flag & FC_UNLOADING) {
4887                         if (phba->sli_rev == LPFC_SLI_REV4) {
4888                                 spin_lock_irqsave(&vport->phba->ndlp_lock,
4889                                                   iflags);
4890                                 ndlp->nlp_flag |= NLP_RELEASE_RPI;
4891                                 spin_unlock_irqrestore(&vport->phba->ndlp_lock,
4892                                                        iflags);
4893                         }
4894                         lpfc_nlp_get(ndlp);
4895                 }
4896                 mbox->ctx_ndlp = ndlp;
4897                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4898         }
4899 }
4900 
4901 /*
4902  * Free rpi associated with LPFC_NODELIST entry.
4903  * This routine is called from lpfc_freenode(), when we are removing
4904  * a LPFC_NODELIST entry. It is also called if the driver initiates a
4905  * LOGO that completes successfully, and we are waiting to PLOGI back
4906  * to the remote NPort. In addition, it is called after we receive
4907  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4908  * we are waiting to PLOGI back to the remote NPort.
4909  */
4910 int
4911 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4912 {
4913         struct lpfc_hba *phba = vport->phba;
4914         LPFC_MBOXQ_t    *mbox;
4915         int rc, acc_plogi = 1;
4916         uint16_t rpi;
4917 
4918         if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4919             ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4920                 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4921                         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4922                                          "3366 RPI x%x needs to be "
4923                                          "unregistered nlp_flag x%x "
4924                                          "did x%x\n",
4925                                          ndlp->nlp_rpi, ndlp->nlp_flag,
4926                                          ndlp->nlp_DID);
4927 
4928                 /* If there is already an UNREG in progress for this ndlp,
4929                  * no need to queue up another one.
4930                  */
4931                 if (ndlp->nlp_flag & NLP_UNREG_INP) {
4932                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4933                                          "1436 unreg_rpi SKIP UNREG x%x on "
4934                                          "NPort x%x deferred x%x  flg x%x "
4935                                          "Data: x%px\n",
4936                                          ndlp->nlp_rpi, ndlp->nlp_DID,
4937                                          ndlp->nlp_defer_did,
4938                                          ndlp->nlp_flag, ndlp);
4939                         goto out;
4940                 }
4941 
4942                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4943                 if (mbox) {
4944                         /* SLI4 ports require the physical rpi value. */
4945                         rpi = ndlp->nlp_rpi;
4946                         if (phba->sli_rev == LPFC_SLI_REV4)
4947                                 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4948 
4949                         lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
4950                         mbox->vport = vport;
4951                         lpfc_set_unreg_login_mbx_cmpl(phba, vport, ndlp, mbox);
4952                         if (mbox->mbox_cmpl == lpfc_sli4_unreg_rpi_cmpl_clr)
4953                                 /*
4954                                  * accept PLOGIs after unreg_rpi_cmpl
4955                                  */
4956                                 acc_plogi = 0;
4957                         if (((ndlp->nlp_DID & Fabric_DID_MASK) !=
4958                             Fabric_DID_MASK) &&
4959                             (!(vport->fc_flag & FC_OFFLINE_MODE)))
4960                                 ndlp->nlp_flag |= NLP_UNREG_INP;
4961 
4962                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4963                                          "1433 unreg_rpi UNREG x%x on "
4964                                          "NPort x%x deferred flg x%x "
4965                                          "Data:x%px\n",
4966                                          ndlp->nlp_rpi, ndlp->nlp_DID,
4967                                          ndlp->nlp_flag, ndlp);
4968 
4969                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4970                         if (rc == MBX_NOT_FINISHED) {
4971                                 mempool_free(mbox, phba->mbox_mem_pool);
4972                                 acc_plogi = 1;
4973                         }
4974                 }
4975                 lpfc_no_rpi(phba, ndlp);
4976 out:
4977                 if (phba->sli_rev != LPFC_SLI_REV4)
4978                         ndlp->nlp_rpi = 0;
4979                 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
4980                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
4981                 if (acc_plogi)
4982                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4983                 return 1;
4984         }
4985         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4986         return 0;
4987 }
4988 
4989 /**
4990  * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4991  * @phba: pointer to lpfc hba data structure.
4992  *
4993  * This routine is invoked to unregister all the currently registered RPIs
4994  * to the HBA.
4995  **/
4996 void
4997 lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4998 {
4999         struct lpfc_vport **vports;
5000         struct lpfc_nodelist *ndlp;
5001         struct Scsi_Host *shost;
5002         int i;
5003 
5004         vports = lpfc_create_vport_work_array(phba);
5005         if (!vports) {
5006                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5007                         "2884 Vport array allocation failed \n");
5008                 return;
5009         }
5010         for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5011                 shost = lpfc_shost_from_vport(vports[i]);
5012                 spin_lock_irq(shost->host_lock);
5013                 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5014                         if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5015                                 /* The mempool_alloc might sleep */
5016                                 spin_unlock_irq(shost->host_lock);
5017                                 lpfc_unreg_rpi(vports[i], ndlp);
5018                                 spin_lock_irq(shost->host_lock);
5019                         }
5020                 }
5021                 spin_unlock_irq(shost->host_lock);
5022         }
5023         lpfc_destroy_vport_work_array(phba, vports);
5024 }
5025 
5026 void
5027 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
5028 {
5029         struct lpfc_hba  *phba  = vport->phba;
5030         LPFC_MBOXQ_t     *mbox;
5031         int rc;
5032 
5033         if (phba->sli_rev == LPFC_SLI_REV4) {
5034                 lpfc_sli4_unreg_all_rpis(vport);
5035                 return;
5036         }
5037 
5038         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5039         if (mbox) {
5040                 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
5041                                  mbox);
5042                 mbox->vport = vport;
5043                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5044                 mbox->ctx_ndlp = NULL;
5045                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5046                 if (rc != MBX_TIMEOUT)
5047                         mempool_free(mbox, phba->mbox_mem_pool);
5048 
5049                 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5050                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
5051                                 "1836 Could not issue "
5052                                 "unreg_login(all_rpis) status %d\n", rc);
5053         }
5054 }
5055 
5056 void
5057 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
5058 {
5059         struct lpfc_hba  *phba  = vport->phba;
5060         LPFC_MBOXQ_t     *mbox;
5061         int rc;
5062 
5063         /* Unreg DID is an SLI3 operation. */
5064         if (phba->sli_rev > LPFC_SLI_REV3)
5065                 return;
5066 
5067         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5068         if (mbox) {
5069                 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
5070                                mbox);
5071                 mbox->vport = vport;
5072                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5073                 mbox->ctx_ndlp = NULL;
5074                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5075                 if (rc != MBX_TIMEOUT)
5076                         mempool_free(mbox, phba->mbox_mem_pool);
5077 
5078                 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5079                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
5080                                          "1815 Could not issue "
5081                                          "unreg_did (default rpis) status %d\n",
5082                                          rc);
5083         }
5084 }
5085 
5086 /*
5087  * Free resources associated with LPFC_NODELIST entry
5088  * so it can be freed.
5089  */
5090 static int
5091 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5092 {
5093         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5094         struct lpfc_hba  *phba = vport->phba;
5095         LPFC_MBOXQ_t *mb, *nextmb;
5096         struct lpfc_dmabuf *mp;
5097         unsigned long iflags;
5098 
5099         /* Cleanup node for NPort <nlp_DID> */
5100         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5101                          "0900 Cleanup node for NPort x%x "
5102                          "Data: x%x x%x x%x\n",
5103                          ndlp->nlp_DID, ndlp->nlp_flag,
5104                          ndlp->nlp_state, ndlp->nlp_rpi);
5105         if (NLP_CHK_FREE_REQ(ndlp)) {
5106                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5107                                 "0280 %s: ndlp:x%px "
5108                                 "usgmap:x%x refcnt:%d\n",
5109                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
5110                                 kref_read(&ndlp->kref));
5111                 lpfc_dequeue_node(vport, ndlp);
5112         } else {
5113                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5114                                 "0281 %s: ndlp:x%px "
5115                                 "usgmap:x%x refcnt:%d\n",
5116                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
5117                                 kref_read(&ndlp->kref));
5118                 lpfc_disable_node(vport, ndlp);
5119         }
5120 
5121 
5122         /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
5123 
5124         /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
5125         if ((mb = phba->sli.mbox_active)) {
5126                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5127                    !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5128                    (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5129                         mb->ctx_ndlp = NULL;
5130                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5131                 }
5132         }
5133 
5134         spin_lock_irq(&phba->hbalock);
5135         /* Cleanup REG_LOGIN completions which are not yet processed */
5136         list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
5137                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
5138                         (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
5139                         (ndlp != (struct lpfc_nodelist *)mb->ctx_ndlp))
5140                         continue;
5141 
5142                 mb->ctx_ndlp = NULL;
5143                 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5144         }
5145 
5146         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
5147                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5148                    !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5149                     (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5150                         mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
5151                         if (mp) {
5152                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
5153                                 kfree(mp);
5154                         }
5155                         list_del(&mb->list);
5156                         mempool_free(mb, phba->mbox_mem_pool);
5157                         /* We shall not invoke the lpfc_nlp_put to decrement
5158                          * the ndlp reference count as we are in the process
5159                          * of lpfc_nlp_release.
5160                          */
5161                 }
5162         }
5163         spin_unlock_irq(&phba->hbalock);
5164 
5165         lpfc_els_abort(phba, ndlp);
5166 
5167         spin_lock_irq(shost->host_lock);
5168         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
5169         spin_unlock_irq(shost->host_lock);
5170 
5171         ndlp->nlp_last_elscmd = 0;
5172         del_timer_sync(&ndlp->nlp_delayfunc);
5173 
5174         list_del_init(&ndlp->els_retry_evt.evt_listp);
5175         list_del_init(&ndlp->dev_loss_evt.evt_listp);
5176         lpfc_cleanup_vports_rrqs(vport, ndlp);
5177         if (phba->sli_rev == LPFC_SLI_REV4)
5178                 ndlp->nlp_flag |= NLP_RELEASE_RPI;
5179         if (!lpfc_unreg_rpi(vport, ndlp)) {
5180                 /* Clean up unregistered and non freed rpis */
5181                 if ((ndlp->nlp_flag & NLP_RELEASE_RPI) &&
5182                     !(ndlp->nlp_rpi == LPFC_RPI_ALLOC_ERROR)) {
5183                         lpfc_sli4_free_rpi(vport->phba,
5184                                            ndlp->nlp_rpi);
5185                         spin_lock_irqsave(&vport->phba->ndlp_lock,
5186                                           iflags);
5187                         ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
5188                         ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
5189                         spin_unlock_irqrestore(&vport->phba->ndlp_lock,
5190                                                iflags);
5191                 }
5192         }
5193         return 0;
5194 }
5195 
5196 /*
5197  * Check to see if we can free the nlp back to the freelist.
5198  * If we are in the middle of using the nlp in the discovery state
5199  * machine, defer the free till we reach the end of the state machine.
5200  */
5201 static void
5202 lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5203 {
5204         struct lpfc_hba  *phba = vport->phba;
5205         struct lpfc_rport_data *rdata;
5206         struct fc_rport *rport;
5207         LPFC_MBOXQ_t *mbox;
5208         int rc;
5209 
5210         lpfc_cancel_retry_delay_tmo(vport, ndlp);
5211         if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
5212             !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
5213             !(ndlp->nlp_flag & NLP_RPI_REGISTERED) &&
5214             phba->sli_rev != LPFC_SLI_REV4) {
5215                 /* For this case we need to cleanup the default rpi
5216                  * allocated by the firmware.
5217                  */
5218                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5219                                  "0005 rpi:%x DID:%x flg:%x %d map:%x x%px\n",
5220                                  ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5221                                  kref_read(&ndlp->kref),
5222                                  ndlp->nlp_usg_map, ndlp);
5223                 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
5224                         != NULL) {
5225                         rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
5226                             (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
5227                         if (rc) {
5228                                 mempool_free(mbox, phba->mbox_mem_pool);
5229                         }
5230                         else {
5231                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
5232                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
5233                                 mbox->vport = vport;
5234                                 mbox->ctx_ndlp = ndlp;
5235                                 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5236                                 if (rc == MBX_NOT_FINISHED) {
5237                                         mempool_free(mbox, phba->mbox_mem_pool);
5238                                 }
5239                         }
5240                 }
5241         }
5242         lpfc_cleanup_node(vport, ndlp);
5243 
5244         /*
5245          * ndlp->rport must be set to NULL before it reaches here
5246          * i.e. break rport/node link before doing lpfc_nlp_put for
5247          * registered rport and then drop the reference of rport.
5248          */
5249         if (ndlp->rport) {
5250                 /*
5251                  * extra lpfc_nlp_put dropped the reference of ndlp
5252                  * for registered rport so need to cleanup rport
5253                  */
5254                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5255                                 "0940 removed node x%px DID x%x "
5256                                 " rport not null x%px\n",
5257                                 ndlp, ndlp->nlp_DID, ndlp->rport);
5258                 rport = ndlp->rport;
5259                 rdata = rport->dd_data;
5260                 rdata->pnode = NULL;
5261                 ndlp->rport = NULL;
5262                 put_device(&rport->dev);
5263         }
5264 }
5265 
5266 static int
5267 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5268               uint32_t did)
5269 {
5270         D_ID mydid, ndlpdid, matchdid;
5271 
5272         if (did == Bcast_DID)
5273                 return 0;
5274 
5275         /* First check for Direct match */
5276         if (ndlp->nlp_DID == did)
5277                 return 1;
5278 
5279         /* Next check for area/domain identically equals 0 match */
5280         mydid.un.word = vport->fc_myDID;
5281         if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
5282                 return 0;
5283         }
5284 
5285         matchdid.un.word = did;
5286         ndlpdid.un.word = ndlp->nlp_DID;
5287         if (matchdid.un.b.id == ndlpdid.un.b.id) {
5288                 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5289                     (mydid.un.b.area == matchdid.un.b.area)) {
5290                         /* This code is supposed to match the ID
5291                          * for a private loop device that is
5292                          * connect to fl_port. But we need to
5293                          * check that the port did not just go
5294                          * from pt2pt to fabric or we could end
5295                          * up matching ndlp->nlp_DID 000001 to
5296                          * fabric DID 0x20101
5297                          */
5298                         if ((ndlpdid.un.b.domain == 0) &&
5299                             (ndlpdid.un.b.area == 0)) {
5300                                 if (ndlpdid.un.b.id &&
5301                                     vport->phba->fc_topology ==
5302                                     LPFC_TOPOLOGY_LOOP)
5303                                         return 1;
5304                         }
5305                         return 0;
5306                 }
5307 
5308                 matchdid.un.word = ndlp->nlp_DID;
5309                 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5310                     (mydid.un.b.area == ndlpdid.un.b.area)) {
5311                         if ((matchdid.un.b.domain == 0) &&
5312                             (matchdid.un.b.area == 0)) {
5313                                 if (matchdid.un.b.id)
5314                                         return 1;
5315                         }
5316                 }
5317         }
5318         return 0;
5319 }
5320 
5321 /* Search for a nodelist entry */
5322 static struct lpfc_nodelist *
5323 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5324 {
5325         struct lpfc_nodelist *ndlp;
5326         uint32_t data1;
5327 
5328         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5329                 if (lpfc_matchdid(vport, ndlp, did)) {
5330                         data1 = (((uint32_t)ndlp->nlp_state << 24) |
5331                                  ((uint32_t)ndlp->nlp_xri << 16) |
5332                                  ((uint32_t)ndlp->nlp_type << 8) |
5333                                  ((uint32_t)ndlp->nlp_usg_map & 0xff));
5334                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5335                                          "0929 FIND node DID "
5336                                          "Data: x%px x%x x%x x%x x%x x%px\n",
5337                                          ndlp, ndlp->nlp_DID,
5338                                          ndlp->nlp_flag, data1, ndlp->nlp_rpi,
5339                                          ndlp->active_rrqs_xri_bitmap);
5340                         return ndlp;
5341                 }
5342         }
5343 
5344         /* FIND node did <did> NOT FOUND */
5345         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5346                          "0932 FIND node did x%x NOT FOUND.\n", did);
5347         return NULL;
5348 }
5349 
5350 struct lpfc_nodelist *
5351 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5352 {
5353         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5354         struct lpfc_nodelist *ndlp;
5355         unsigned long iflags;
5356 
5357         spin_lock_irqsave(shost->host_lock, iflags);
5358         ndlp = __lpfc_findnode_did(vport, did);
5359         spin_unlock_irqrestore(shost->host_lock, iflags);
5360         return ndlp;
5361 }
5362 
5363 struct lpfc_nodelist *
5364 lpfc_findnode_mapped(struct lpfc_vport *vport)
5365 {
5366         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5367         struct lpfc_nodelist *ndlp;
5368         uint32_t data1;
5369         unsigned long iflags;
5370 
5371         spin_lock_irqsave(shost->host_lock, iflags);
5372 
5373         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5374                 if (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
5375                     ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
5376                         data1 = (((uint32_t)ndlp->nlp_state << 24) |
5377                                  ((uint32_t)ndlp->nlp_xri << 16) |
5378                                  ((uint32_t)ndlp->nlp_type << 8) |
5379                                  ((uint32_t)ndlp->nlp_rpi & 0xff));
5380                         spin_unlock_irqrestore(shost->host_lock, iflags);
5381                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5382                                          "2025 FIND node DID "
5383                                          "Data: x%px x%x x%x x%x x%px\n",
5384                                          ndlp, ndlp->nlp_DID,
5385                                          ndlp->nlp_flag, data1,
5386                                          ndlp->active_rrqs_xri_bitmap);
5387                         return ndlp;
5388                 }
5389         }
5390         spin_unlock_irqrestore(shost->host_lock, iflags);
5391 
5392         /* FIND node did <did> NOT FOUND */
5393         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5394                          "2026 FIND mapped did NOT FOUND.\n");
5395         return NULL;
5396 }
5397 
5398 struct lpfc_nodelist *
5399 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5400 {
5401         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5402         struct lpfc_nodelist *ndlp;
5403 
5404         ndlp = lpfc_findnode_did(vport, did);
5405         if (!ndlp) {
5406                 if (vport->phba->nvmet_support)
5407                         return NULL;
5408                 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5409                     lpfc_rscn_payload_check(vport, did) == 0)
5410                         return NULL;
5411                 ndlp = lpfc_nlp_init(vport, did);
5412                 if (!ndlp)
5413                         return NULL;
5414                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5415                 spin_lock_irq(shost->host_lock);
5416                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5417                 spin_unlock_irq(shost->host_lock);
5418                 return ndlp;
5419         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5420                 if (vport->phba->nvmet_support)
5421                         return NULL;
5422                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
5423                 if (!ndlp) {
5424                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
5425                                          "0014 Could not enable ndlp\n");
5426                         return NULL;
5427                 }
5428                 spin_lock_irq(shost->host_lock);
5429                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5430                 spin_unlock_irq(shost->host_lock);
5431                 return ndlp;
5432         }
5433 
5434         /* The NVME Target does not want to actively manage an rport.
5435          * The goal is to allow the target to reset its state and clear
5436          * pending IO in preparation for the initiator to recover.
5437          */
5438         if ((vport->fc_flag & FC_RSCN_MODE) &&
5439             !(vport->fc_flag & FC_NDISC_ACTIVE)) {
5440                 if (lpfc_rscn_payload_check(vport, did)) {
5441 
5442                         /* Since this node is marked for discovery,
5443                          * delay timeout is not needed.
5444                          */
5445                         lpfc_cancel_retry_delay_tmo(vport, ndlp);
5446 
5447                         /* NVME Target mode waits until rport is known to be
5448                          * impacted by the RSCN before it transitions.  No
5449                          * active management - just go to NPR provided the
5450                          * node had a valid login.
5451                          */
5452                         if (vport->phba->nvmet_support)
5453                                 return ndlp;
5454 
5455                         /* If we've already received a PLOGI from this NPort
5456                          * we don't need to try to discover it again.
5457                          */
5458                         if (ndlp->nlp_flag & NLP_RCV_PLOGI &&
5459                             !(ndlp->nlp_type &
5460                              (NLP_FCP_TARGET | NLP_NVME_TARGET)))
5461                                 return NULL;
5462 
5463                         ndlp->nlp_prev_state = ndlp->nlp_state;
5464                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5465 
5466                         spin_lock_irq(shost->host_lock);
5467                         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5468                         spin_unlock_irq(shost->host_lock);
5469                 } else
5470                         ndlp = NULL;
5471         } else {
5472                 /* If the initiator received a PLOGI from this NPort or if the
5473                  * initiator is already in the process of discovery on it,
5474                  * there's no need to try to discover it again.
5475                  */
5476                 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
5477                     ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5478                     (!vport->phba->nvmet_support &&
5479                      ndlp->nlp_flag & NLP_RCV_PLOGI))
5480                         return NULL;
5481 
5482                 if (vport->phba->nvmet_support)
5483                         return ndlp;
5484 
5485                 /* Moving to NPR state clears unsolicited flags and
5486                  * allows for rediscovery
5487                  */
5488                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5489 
5490                 spin_lock_irq(shost->host_lock);
5491                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5492                 spin_unlock_irq(shost->host_lock);
5493         }
5494         return ndlp;
5495 }
5496 
5497 /* Build a list of nodes to discover based on the loopmap */
5498 void
5499 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
5500 {
5501         struct lpfc_hba  *phba = vport->phba;
5502         int j;
5503         uint32_t alpa, index;
5504 
5505         if (!lpfc_is_link_up(phba))
5506                 return;
5507 
5508         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
5509                 return;
5510 
5511         /* Check for loop map present or not */
5512         if (phba->alpa_map[0]) {
5513                 for (j = 1; j <= phba->alpa_map[0]; j++) {
5514                         alpa = phba->alpa_map[j];
5515                         if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
5516                                 continue;
5517                         lpfc_setup_disc_node(vport, alpa);
5518                 }
5519         } else {
5520                 /* No alpamap, so try all alpa's */
5521                 for (j = 0; j < FC_MAXLOOP; j++) {
5522                         /* If cfg_scan_down is set, start from highest
5523                          * ALPA (0xef) to lowest (0x1).
5524                          */
5525                         if (vport->cfg_scan_down)
5526                                 index = j;
5527                         else
5528                                 index = FC_MAXLOOP - j - 1;
5529                         alpa = lpfcAlpaArray[index];
5530                         if ((vport->fc_myDID & 0xff) == alpa)
5531                                 continue;
5532                         lpfc_setup_disc_node(vport, alpa);
5533                 }
5534         }
5535         return;
5536 }
5537 
5538 /* SLI3 only */
5539 void
5540 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
5541 {
5542         LPFC_MBOXQ_t *mbox;
5543         struct lpfc_sli *psli = &phba->sli;
5544         struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5545         struct lpfc_sli_ring *fcp_ring   = &psli->sli3_ring[LPFC_FCP_RING];
5546         int  rc;
5547 
5548         /*
5549          * if it's not a physical port or if we already send
5550          * clear_la then don't send it.
5551          */
5552         if ((phba->link_state >= LPFC_CLEAR_LA) ||
5553             (vport->port_type != LPFC_PHYSICAL_PORT) ||
5554                 (phba->sli_rev == LPFC_SLI_REV4))
5555                 return;
5556 
5557                         /* Link up discovery */
5558         if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5559                 phba->link_state = LPFC_CLEAR_LA;
5560                 lpfc_clear_la(phba, mbox);
5561                 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5562                 mbox->vport = vport;
5563                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5564                 if (rc == MBX_NOT_FINISHED) {
5565                         mempool_free(mbox, phba->mbox_mem_pool);
5566                         lpfc_disc_flush_list(vport);
5567                         extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5568                         fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5569                         phba->link_state = LPFC_HBA_ERROR;
5570                 }
5571         }
5572 }
5573 
5574 /* Reg_vpi to tell firmware to resume normal operations */
5575 void
5576 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5577 {
5578         LPFC_MBOXQ_t *regvpimbox;
5579 
5580         regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5581         if (regvpimbox) {
5582                 lpfc_reg_vpi(vport, regvpimbox);
5583                 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5584                 regvpimbox->vport = vport;
5585                 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
5586                                         == MBX_NOT_FINISHED) {
5587                         mempool_free(regvpimbox, phba->mbox_mem_pool);
5588                 }
5589         }
5590 }
5591 
5592 /* Start Link up / RSCN discovery on NPR nodes */
5593 void
5594 lpfc_disc_start(struct lpfc_vport *vport)
5595 {
5596         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5597         struct lpfc_hba  *phba = vport->phba;
5598         uint32_t num_sent;
5599         uint32_t clear_la_pending;
5600 
5601         if (!lpfc_is_link_up(phba)) {
5602                 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5603                                  "3315 Link is not up %x\n",
5604                                  phba->link_state);
5605                 return;
5606         }
5607 
5608         if (phba->link_state == LPFC_CLEAR_LA)
5609                 clear_la_pending = 1;
5610         else
5611                 clear_la_pending = 0;
5612 
5613         if (vport->port_state < LPFC_VPORT_READY)
5614                 vport->port_state = LPFC_DISC_AUTH;
5615 
5616         lpfc_set_disctmo(vport);
5617 
5618         vport->fc_prevDID = vport->fc_myDID;
5619         vport->num_disc_nodes = 0;
5620 
5621         /* Start Discovery state <hba_state> */
5622         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5623                          "0202 Start Discovery hba state x%x "
5624                          "Data: x%x x%x x%x\n",
5625                          vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5626                          vport->fc_adisc_cnt);
5627 
5628         /* First do ADISCs - if any */
5629         num_sent = lpfc_els_disc_adisc(vport);
5630 
5631         if (num_sent)
5632                 return;
5633 
5634         /* Register the VPI for SLI3, NPIV only. */
5635         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5636             !(vport->fc_flag & FC_PT2PT) &&
5637             !(vport->fc_flag & FC_RSCN_MODE) &&
5638             (phba->sli_rev < LPFC_SLI_REV4)) {
5639                 lpfc_issue_clear_la(phba, vport);
5640                 lpfc_issue_reg_vpi(phba, vport);
5641                 return;
5642         }
5643 
5644         /*
5645          * For SLI2, we need to set port_state to READY and continue
5646          * discovery.
5647          */
5648         if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5649                 /* If we get here, there is nothing to ADISC */
5650                 lpfc_issue_clear_la(phba, vport);
5651 
5652                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
5653                         vport->num_disc_nodes = 0;
5654                         /* go thru NPR nodes and issue ELS PLOGIs */
5655                         if (vport->fc_npr_cnt)
5656                                 lpfc_els_disc_plogi(vport);
5657 
5658                         if (!vport->num_disc_nodes) {
5659                                 spin_lock_irq(shost->host_lock);
5660                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5661                                 spin_unlock_irq(shost->host_lock);
5662                                 lpfc_can_disctmo(vport);
5663                         }
5664                 }
5665                 vport->port_state = LPFC_VPORT_READY;
5666         } else {
5667                 /* Next do PLOGIs - if any */
5668                 num_sent = lpfc_els_disc_plogi(vport);
5669 
5670                 if (num_sent)
5671                         return;
5672 
5673                 if (vport->fc_flag & FC_RSCN_MODE) {
5674                         /* Check to see if more RSCNs came in while we
5675                          * were processing this one.
5676                          */
5677                         if ((vport->fc_rscn_id_cnt == 0) &&
5678                             (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5679                                 spin_lock_irq(shost->host_lock);
5680                                 vport->fc_flag &= ~FC_RSCN_MODE;
5681                                 spin_unlock_irq(shost->host_lock);
5682                                 lpfc_can_disctmo(vport);
5683                         } else
5684                                 lpfc_els_handle_rscn(vport);
5685                 }
5686         }
5687         return;
5688 }
5689 
5690 /*
5691  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5692  *  ring the match the sppecified nodelist.
5693  */
5694 static void
5695 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
5696 {
5697         LIST_HEAD(completions);
5698         IOCB_t     *icmd;
5699         struct lpfc_iocbq    *iocb, *next_iocb;
5700         struct lpfc_sli_ring *pring;
5701 
5702         pring = lpfc_phba_elsring(phba);
5703         if (unlikely(!pring))
5704                 return;
5705 
5706         /* Error matching iocb on txq or txcmplq
5707          * First check the txq.
5708          */
5709         spin_lock_irq(&phba->hbalock);
5710         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5711                 if (iocb->context1 != ndlp) {
5712                         continue;
5713                 }
5714                 icmd = &iocb->iocb;
5715                 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5716                     (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5717 
5718                         list_move_tail(&iocb->list, &completions);
5719                 }
5720         }
5721 
5722         /* Next check the txcmplq */
5723         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5724                 if (iocb->context1 != ndlp) {
5725                         continue;
5726                 }
5727                 icmd = &iocb->iocb;
5728                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5729                     icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
5730                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
5731                 }
5732         }
5733         spin_unlock_irq(&phba->hbalock);
5734 
5735         /* Cancel all the IOCBs from the completions list */
5736         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5737                               IOERR_SLI_ABORTED);
5738 }
5739 
5740 static void
5741 lpfc_disc_flush_list(struct lpfc_vport *vport)
5742 {
5743         struct lpfc_nodelist *ndlp, *next_ndlp;
5744         struct lpfc_hba *phba = vport->phba;
5745 
5746         if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5747                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
5748                                          nlp_listp) {
5749                         if (!NLP_CHK_NODE_ACT(ndlp))
5750                                 continue;
5751                         if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5752                             ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5753                                 lpfc_free_tx(phba, ndlp);
5754                         }
5755                 }
5756         }
5757 }
5758 
5759 void
5760 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5761 {
5762         lpfc_els_flush_rscn(vport);
5763         lpfc_els_flush_cmd(vport);
5764         lpfc_disc_flush_list(vport);
5765 }
5766 
5767 /*****************************************************************************/
5768 /*
5769  * NAME:     lpfc_disc_timeout
5770  *
5771  * FUNCTION: Fibre Channel driver discovery timeout routine.
5772  *
5773  * EXECUTION ENVIRONMENT: interrupt only
5774  *
5775  * CALLED FROM:
5776  *      Timer function
5777  *
5778  * RETURNS:
5779  *      none
5780  */
5781 /*****************************************************************************/
5782 void
5783 lpfc_disc_timeout(struct timer_list *t)
5784 {
5785         struct lpfc_vport *vport = from_timer(vport, t, fc_disctmo);
5786         struct lpfc_hba   *phba = vport->phba;
5787         uint32_t tmo_posted;
5788         unsigned long flags = 0;
5789 
5790         if (unlikely(!phba))
5791                 return;
5792 
5793         spin_lock_irqsave(&vport->work_port_lock, flags);
5794         tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5795         if (!tmo_posted)
5796                 vport->work_port_events |= WORKER_DISC_TMO;
5797         spin_unlock_irqrestore(&vport->work_port_lock, flags);
5798 
5799         if (!tmo_posted)
5800                 lpfc_worker_wake_up(phba);
5801         return;
5802 }
5803 
5804 static void
5805 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
5806 {
5807         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5808         struct lpfc_hba  *phba = vport->phba;
5809         struct lpfc_sli  *psli = &phba->sli;
5810         struct lpfc_nodelist *ndlp, *next_ndlp;
5811         LPFC_MBOXQ_t *initlinkmbox;
5812         int rc, clrlaerr = 0;
5813 
5814         if (!(vport->fc_flag & FC_DISC_TMO))
5815                 return;
5816 
5817         spin_lock_irq(shost->host_lock);
5818         vport->fc_flag &= ~FC_DISC_TMO;
5819         spin_unlock_irq(shost->host_lock);
5820 
5821         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5822                 "disc timeout:    state:x%x rtry:x%x flg:x%x",
5823                 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5824 
5825         switch (vport->port_state) {
5826 
5827         case LPFC_LOCAL_CFG_LINK:
5828                 /*
5829                  * port_state is identically  LPFC_LOCAL_CFG_LINK while
5830                  * waiting for FAN timeout
5831                  */
5832                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5833                                  "0221 FAN timeout\n");
5834 
5835                 /* Start discovery by sending FLOGI, clean up old rpis */
5836                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
5837                                          nlp_listp) {
5838                         if (!NLP_CHK_NODE_ACT(ndlp))
5839                                 continue;
5840                         if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5841                                 continue;
5842                         if (ndlp->nlp_type & NLP_FABRIC) {
5843                                 /* Clean up the ndlp on Fabric connections */
5844                                 lpfc_drop_node(vport, ndlp);
5845 
5846                         } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
5847                                 /* Fail outstanding IO now since device
5848                                  * is marked for PLOGI.
5849                                  */
5850                                 lpfc_unreg_rpi(vport, ndlp);
5851                         }
5852                 }
5853                 if (vport->port_state != LPFC_FLOGI) {
5854                         if (phba->sli_rev <= LPFC_SLI_REV3)
5855                                 lpfc_initial_flogi(vport);
5856                         else
5857                                 lpfc_issue_init_vfi(vport);
5858                         return;
5859                 }
5860                 break;
5861 
5862         case LPFC_FDISC:
5863         case LPFC_FLOGI:
5864         /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
5865                 /* Initial FLOGI timeout */
5866                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5867                                  "0222 Initial %s timeout\n",
5868                                  vport->vpi ? "FDISC" : "FLOGI");
5869 
5870                 /* Assume no Fabric and go on with discovery.
5871                  * Check for outstanding ELS FLOGI to abort.
5872                  */
5873 
5874                 /* FLOGI failed, so just use loop map to make discovery list */
5875                 lpfc_disc_list_loopmap(vport);
5876 
5877                 /* Start discovery */
5878                 lpfc_disc_start(vport);
5879                 break;
5880 
5881         case LPFC_FABRIC_CFG_LINK:
5882         /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5883            NameServer login */
5884                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5885                                  "0223 Timeout while waiting for "
5886                                  "NameServer login\n");
5887                 /* Next look for NameServer ndlp */
5888                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
5889                 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
5890                         lpfc_els_abort(phba, ndlp);
5891 
5892                 /* ReStart discovery */
5893                 goto restart_disc;
5894 
5895         case LPFC_NS_QRY:
5896         /* Check for wait for NameServer Rsp timeout */
5897                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5898                                  "0224 NameServer Query timeout "
5899                                  "Data: x%x x%x\n",
5900                                  vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
5901 
5902                 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5903                         /* Try it one more time */
5904                         vport->fc_ns_retry++;
5905                         vport->gidft_inp = 0;
5906                         rc = lpfc_issue_gidft(vport);
5907                         if (rc == 0)
5908                                 break;
5909                 }
5910                 vport->fc_ns_retry = 0;
5911 
5912 restart_disc:
5913                 /*
5914                  * Discovery is over.
5915                  * set port_state to PORT_READY if SLI2.
5916                  * cmpl_reg_vpi will set port_state to READY for SLI3.
5917                  */
5918                 if (phba->sli_rev < LPFC_SLI_REV4) {
5919                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5920                                 lpfc_issue_reg_vpi(phba, vport);
5921                         else  {
5922                                 lpfc_issue_clear_la(phba, vport);
5923                                 vport->port_state = LPFC_VPORT_READY;
5924                         }
5925                 }
5926 
5927                 /* Setup and issue mailbox INITIALIZE LINK command */
5928                 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5929                 if (!initlinkmbox) {
5930                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5931                                          "0206 Device Discovery "
5932                                          "completion error\n");
5933                         phba->link_state = LPFC_HBA_ERROR;
5934                         break;
5935                 }
5936 
5937                 lpfc_linkdown(phba);
5938                 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5939                                phba->cfg_link_speed);
5940                 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
5941                 initlinkmbox->vport = vport;
5942                 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5943                 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
5944                 lpfc_set_loopback_flag(phba);
5945                 if (rc == MBX_NOT_FINISHED)
5946                         mempool_free(initlinkmbox, phba->mbox_mem_pool);
5947 
5948                 break;
5949 
5950         case LPFC_DISC_AUTH:
5951         /* Node Authentication timeout */
5952                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5953                                  "0227 Node Authentication timeout\n");
5954                 lpfc_disc_flush_list(vport);
5955 
5956                 /*
5957                  * set port_state to PORT_READY if SLI2.
5958                  * cmpl_reg_vpi will set port_state to READY for SLI3.
5959                  */
5960                 if (phba->sli_rev < LPFC_SLI_REV4) {
5961                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5962                                 lpfc_issue_reg_vpi(phba, vport);
5963                         else  { /* NPIV Not enabled */
5964                                 lpfc_issue_clear_la(phba, vport);
5965                                 vport->port_state = LPFC_VPORT_READY;
5966                         }
5967                 }
5968                 break;
5969 
5970         case LPFC_VPORT_READY:
5971                 if (vport->fc_flag & FC_RSCN_MODE) {
5972                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5973                                          "0231 RSCN timeout Data: x%x "
5974                                          "x%x\n",
5975                                          vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
5976 
5977                         /* Cleanup any outstanding ELS commands */
5978                         lpfc_els_flush_cmd(vport);
5979 
5980                         lpfc_els_flush_rscn(vport);
5981                         lpfc_disc_flush_list(vport);
5982                 }
5983                 break;
5984 
5985         default:
5986                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5987                                  "0273 Unexpected discovery timeout, "
5988                                  "vport State x%x\n", vport->port_state);
5989                 break;
5990         }
5991 
5992         switch (phba->link_state) {
5993         case LPFC_CLEAR_LA:
5994                                 /* CLEAR LA timeout */
5995                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5996                                  "0228 CLEAR LA timeout\n");
5997                 clrlaerr = 1;
5998                 break;
5999 
6000         case LPFC_LINK_UP:
6001                 lpfc_issue_clear_la(phba, vport);
6002                 /* fall through */
6003         case LPFC_LINK_UNKNOWN:
6004         case LPFC_WARM_START:
6005         case LPFC_INIT_START:
6006         case LPFC_INIT_MBX_CMDS:
6007         case LPFC_LINK_DOWN:
6008         case LPFC_HBA_ERROR:
6009                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
6010                                  "0230 Unexpected timeout, hba link "
6011                                  "state x%x\n", phba->link_state);
6012                 clrlaerr = 1;
6013                 break;
6014 
6015         case LPFC_HBA_READY:
6016                 break;
6017         }
6018 
6019         if (clrlaerr) {
6020                 lpfc_disc_flush_list(vport);
6021                 if (phba->sli_rev != LPFC_SLI_REV4) {
6022                         psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
6023                                 ~LPFC_STOP_IOCB_EVENT;
6024                         psli->sli3_ring[LPFC_FCP_RING].flag &=
6025                                 ~LPFC_STOP_IOCB_EVENT;
6026                 }
6027                 vport->port_state = LPFC_VPORT_READY;
6028         }
6029         return;
6030 }
6031 
6032 /*
6033  * This routine handles processing a NameServer REG_LOGIN mailbox
6034  * command upon completion. It is setup in the LPFC_MBOXQ
6035  * as the completion routine when the command is
6036  * handed off to the SLI layer.
6037  */
6038 void
6039 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6040 {
6041         MAILBOX_t *mb = &pmb->u.mb;
6042         struct lpfc_dmabuf   *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
6043         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
6044         struct lpfc_vport    *vport = pmb->vport;
6045 
6046         pmb->ctx_buf = NULL;
6047         pmb->ctx_ndlp = NULL;
6048 
6049         if (phba->sli_rev < LPFC_SLI_REV4)
6050                 ndlp->nlp_rpi = mb->un.varWords[0];
6051         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
6052         ndlp->nlp_type |= NLP_FABRIC;
6053         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
6054         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
6055                          "0004 rpi:%x DID:%x flg:%x %d map:%x x%px\n",
6056                          ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
6057                          kref_read(&ndlp->kref),
6058                          ndlp->nlp_usg_map, ndlp);
6059         /*
6060          * Start issuing Fabric-Device Management Interface (FDMI) command to
6061          * 0xfffffa (FDMI well known port).
6062          * DHBA -> DPRT -> RHBA -> RPA  (physical port)
6063          * DPRT -> RPRT (vports)
6064          */
6065         if (vport->port_type == LPFC_PHYSICAL_PORT)
6066                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
6067         else
6068                 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
6069 
6070 
6071         /* decrement the node reference count held for this callback
6072          * function.
6073          */
6074         lpfc_nlp_put(ndlp);
6075         lpfc_mbuf_free(phba, mp->virt, mp->phys);
6076         kfree(mp);
6077         mempool_free(pmb, phba->mbox_mem_pool);
6078 
6079         return;
6080 }
6081 
6082 static int
6083 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
6084 {
6085         uint16_t *rpi = param;
6086 
6087         /* check for active node */
6088         if (!NLP_CHK_NODE_ACT(ndlp))
6089                 return 0;
6090 
6091         return ndlp->nlp_rpi == *rpi;
6092 }
6093 
6094 static int
6095 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
6096 {
6097         return memcmp(&ndlp->nlp_portname, param,
6098                       sizeof(ndlp->nlp_portname)) == 0;
6099 }
6100 
6101 static struct lpfc_nodelist *
6102 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
6103 {
6104         struct lpfc_nodelist *ndlp;
6105 
6106         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
6107                 if (filter(ndlp, param)) {
6108                         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6109                                          "3185 FIND node filter %ps DID "
6110                                          "ndlp x%px did x%x flg x%x st x%x "
6111                                          "xri x%x type x%x rpi x%x\n",
6112                                          filter, ndlp, ndlp->nlp_DID,
6113                                          ndlp->nlp_flag, ndlp->nlp_state,
6114                                          ndlp->nlp_xri, ndlp->nlp_type,
6115                                          ndlp->nlp_rpi);
6116                         return ndlp;
6117                 }
6118         }
6119         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6120                          "3186 FIND node filter %ps NOT FOUND.\n", filter);
6121         return NULL;
6122 }
6123 
6124 /*
6125  * This routine looks up the ndlp lists for the given RPI. If rpi found it
6126  * returns the node list element pointer else return NULL.
6127  */
6128 struct lpfc_nodelist *
6129 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6130 {
6131         return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
6132 }
6133 
6134 /*
6135  * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
6136  * returns the node element list pointer else return NULL.
6137  */
6138 struct lpfc_nodelist *
6139 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
6140 {
6141         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6142         struct lpfc_nodelist *ndlp;
6143 
6144         spin_lock_irq(shost->host_lock);
6145         ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
6146         spin_unlock_irq(shost->host_lock);
6147         return ndlp;
6148 }
6149 
6150 /*
6151  * This routine looks up the ndlp lists for the given RPI. If the rpi
6152  * is found, the routine returns the node element list pointer else
6153  * return NULL.
6154  */
6155 struct lpfc_nodelist *
6156 lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6157 {
6158         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6159         struct lpfc_nodelist *ndlp;
6160         unsigned long flags;
6161 
6162         spin_lock_irqsave(shost->host_lock, flags);
6163         ndlp = __lpfc_findnode_rpi(vport, rpi);
6164         spin_unlock_irqrestore(shost->host_lock, flags);
6165         return ndlp;
6166 }
6167 
6168 /**
6169  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6170  * @phba: pointer to lpfc hba data structure.
6171  * @vpi: the physical host virtual N_Port identifier.
6172  *
6173  * This routine finds a vport on a HBA (referred by @phba) through a
6174  * @vpi. The function walks the HBA's vport list and returns the address
6175  * of the vport with the matching @vpi.
6176  *
6177  * Return code
6178  *    NULL - No vport with the matching @vpi found
6179  *    Otherwise - Address to the vport with the matching @vpi.
6180  **/
6181 struct lpfc_vport *
6182 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6183 {
6184         struct lpfc_vport *vport;
6185         unsigned long flags;
6186         int i = 0;
6187 
6188         /* The physical ports are always vpi 0 - translate is unnecessary. */
6189         if (vpi > 0) {
6190                 /*
6191                  * Translate the physical vpi to the logical vpi.  The
6192                  * vport stores the logical vpi.
6193                  */
6194                 for (i = 0; i < phba->max_vpi; i++) {
6195                         if (vpi == phba->vpi_ids[i])
6196                                 break;
6197                 }
6198 
6199                 if (i >= phba->max_vpi) {
6200                         lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
6201                                          "2936 Could not find Vport mapped "
6202                                          "to vpi %d\n", vpi);
6203                         return NULL;
6204                 }
6205         }
6206 
6207         spin_lock_irqsave(&phba->port_list_lock, flags);
6208         list_for_each_entry(vport, &phba->port_list, listentry) {
6209                 if (vport->vpi == i) {
6210                         spin_unlock_irqrestore(&phba->port_list_lock, flags);
6211                         return vport;
6212                 }
6213         }
6214         spin_unlock_irqrestore(&phba->port_list_lock, flags);
6215         return NULL;
6216 }
6217 
6218 struct lpfc_nodelist *
6219 lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
6220 {
6221         struct lpfc_nodelist *ndlp;
6222         int rpi = LPFC_RPI_ALLOC_ERROR;
6223 
6224         if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6225                 rpi = lpfc_sli4_alloc_rpi(vport->phba);
6226                 if (rpi == LPFC_RPI_ALLOC_ERROR)
6227                         return NULL;
6228         }
6229 
6230         ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
6231         if (!ndlp) {
6232                 if (vport->phba->sli_rev == LPFC_SLI_REV4)
6233                         lpfc_sli4_free_rpi(vport->phba, rpi);
6234                 return NULL;
6235         }
6236 
6237         memset(ndlp, 0, sizeof (struct lpfc_nodelist));
6238 
6239         lpfc_initialize_node(vport, ndlp, did);
6240         INIT_LIST_HEAD(&ndlp->nlp_listp);
6241         if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6242                 ndlp->nlp_rpi = rpi;
6243                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6244                                  "0007 rpi:%x DID:%x flg:%x refcnt:%d "
6245                                  "map:%x x%px\n", ndlp->nlp_rpi, ndlp->nlp_DID,
6246                                  ndlp->nlp_flag,
6247                                  kref_read(&ndlp->kref),
6248                                  ndlp->nlp_usg_map, ndlp);
6249 
6250                 ndlp->active_rrqs_xri_bitmap =
6251                                 mempool_alloc(vport->phba->active_rrq_pool,
6252                                               GFP_KERNEL);
6253                 if (ndlp->active_rrqs_xri_bitmap)
6254                         memset(ndlp->active_rrqs_xri_bitmap, 0,
6255                                ndlp->phba->cfg_rrq_xri_bitmap_sz);
6256         }
6257 
6258 
6259 
6260         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
6261                 "node init:       did:x%x",
6262                 ndlp->nlp_DID, 0, 0);
6263 
6264         return ndlp;
6265 }
6266 
6267 /* This routine releases all resources associated with a specifc NPort's ndlp
6268  * and mempool_free's the nodelist.
6269  */
6270 static void
6271 lpfc_nlp_release(struct kref *kref)
6272 {
6273         struct lpfc_hba *phba;
6274         unsigned long flags;
6275         struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
6276                                                   kref);
6277 
6278         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6279                 "node release:    did:x%x flg:x%x type:x%x",
6280                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
6281 
6282         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
6283                         "0279 %s: ndlp:x%px did %x "
6284                         "usgmap:x%x refcnt:%d rpi:%x\n",
6285                         __func__,
6286                         (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
6287                         kref_read(&ndlp->kref), ndlp->nlp_rpi);
6288 
6289         /* remove ndlp from action. */
6290         lpfc_nlp_remove(ndlp->vport, ndlp);
6291 
6292         /* clear the ndlp active flag for all release cases */
6293         phba = ndlp->phba;
6294         spin_lock_irqsave(&phba->ndlp_lock, flags);
6295         NLP_CLR_NODE_ACT(ndlp);
6296         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6297 
6298         /* free ndlp memory for final ndlp release */
6299         if (NLP_CHK_FREE_REQ(ndlp)) {
6300                 kfree(ndlp->lat_data);
6301                 if (phba->sli_rev == LPFC_SLI_REV4)
6302                         mempool_free(ndlp->active_rrqs_xri_bitmap,
6303                                      ndlp->phba->active_rrq_pool);
6304                 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
6305         }
6306 }
6307 
6308 /* This routine bumps the reference count for a ndlp structure to ensure
6309  * that one discovery thread won't free a ndlp while another discovery thread
6310  * is using it.
6311  */
6312 struct lpfc_nodelist *
6313 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6314 {
6315         struct lpfc_hba *phba;
6316         unsigned long flags;
6317 
6318         if (ndlp) {
6319                 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6320                         "node get:        did:x%x flg:x%x refcnt:x%x",
6321                         ndlp->nlp_DID, ndlp->nlp_flag,
6322                         kref_read(&ndlp->kref));
6323                 /* The check of ndlp usage to prevent incrementing the
6324                  * ndlp reference count that is in the process of being
6325                  * released.
6326                  */
6327                 phba = ndlp->phba;
6328                 spin_lock_irqsave(&phba->ndlp_lock, flags);
6329                 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
6330                         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6331                         lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6332                                 "0276 %s: ndlp:x%px "
6333                                 "usgmap:x%x refcnt:%d\n",
6334                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
6335                                 kref_read(&ndlp->kref));
6336                         return NULL;
6337                 } else
6338                         kref_get(&ndlp->kref);
6339                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6340         }
6341         return ndlp;
6342 }
6343 
6344 /* This routine decrements the reference count for a ndlp structure. If the
6345  * count goes to 0, this indicates the the associated nodelist should be
6346  * freed. Returning 1 indicates the ndlp resource has been released; on the
6347  * other hand, returning 0 indicates the ndlp resource has not been released
6348  * yet.
6349  */
6350 int
6351 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6352 {
6353         struct lpfc_hba *phba;
6354         unsigned long flags;
6355 
6356         if (!ndlp)
6357                 return 1;
6358 
6359         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6360                         "node put:        did:x%x flg:x%x refcnt:x%x",
6361                         ndlp->nlp_DID, ndlp->nlp_flag,
6362                         kref_read(&ndlp->kref));
6363         phba = ndlp->phba;
6364         spin_lock_irqsave(&phba->ndlp_lock, flags);
6365         /* Check the ndlp memory free acknowledge flag to avoid the
6366          * possible race condition that kref_put got invoked again
6367          * after previous one has done ndlp memory free.
6368          */
6369         if (NLP_CHK_FREE_ACK(ndlp)) {
6370                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6371                 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6372                                 "0274 %s: ndlp:x%px "
6373                                 "usgmap:x%x refcnt:%d\n",
6374                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
6375                                 kref_read(&ndlp->kref));
6376                 return 1;
6377         }
6378         /* Check the ndlp inactivate log flag to avoid the possible
6379          * race condition that kref_put got invoked again after ndlp
6380          * is already in inactivating state.
6381          */
6382         if (NLP_CHK_IACT_REQ(ndlp)) {
6383                 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6384                 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6385                                 "0275 %s: ndlp:x%px "
6386                                 "usgmap:x%x refcnt:%d\n",
6387                                 __func__, (void *)ndlp, ndlp->nlp_usg_map,
6388                                 kref_read(&ndlp->kref));
6389                 return 1;
6390         }
6391         /* For last put, mark the ndlp usage flags to make sure no
6392          * other kref_get and kref_put on the same ndlp shall get
6393          * in between the process when the final kref_put has been
6394          * invoked on this ndlp.
6395          */
6396         if (kref_read(&ndlp->kref) == 1) {
6397                 /* Indicate ndlp is put to inactive state. */
6398                 NLP_SET_IACT_REQ(ndlp);
6399                 /* Acknowledge ndlp memory free has been seen. */
6400                 if (NLP_CHK_FREE_REQ(ndlp))
6401                         NLP_SET_FREE_ACK(ndlp);
6402         }
6403         spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6404         /* Note, the kref_put returns 1 when decrementing a reference
6405          * count that was 1, it invokes the release callback function,
6406          * but it still left the reference count as 1 (not actually
6407          * performs the last decrementation). Otherwise, it actually
6408          * decrements the reference count and returns 0.
6409          */
6410         return kref_put(&ndlp->kref, lpfc_nlp_release);
6411 }
6412 
6413 /* This routine free's the specified nodelist if it is not in use
6414  * by any other discovery thread. This routine returns 1 if the
6415  * ndlp has been freed. A return value of 0 indicates the ndlp is
6416  * not yet been released.
6417  */
6418 int
6419 lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6420 {
6421         lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6422                 "node not used:   did:x%x flg:x%x refcnt:x%x",
6423                 ndlp->nlp_DID, ndlp->nlp_flag,
6424                 kref_read(&ndlp->kref));
6425         if (kref_read(&ndlp->kref) == 1)
6426                 if (lpfc_nlp_put(ndlp))
6427                         return 1;
6428         return 0;
6429 }
6430 
6431 /**
6432  * lpfc_fcf_inuse - Check if FCF can be unregistered.
6433  * @phba: Pointer to hba context object.
6434  *
6435  * This function iterate through all FC nodes associated
6436  * will all vports to check if there is any node with
6437  * fc_rports associated with it. If there is an fc_rport
6438  * associated with the node, then the node is either in
6439  * discovered state or its devloss_timer is pending.
6440  */
6441 static int
6442 lpfc_fcf_inuse(struct lpfc_hba *phba)
6443 {
6444         struct lpfc_vport **vports;
6445         int i, ret = 0;
6446         struct lpfc_nodelist *ndlp;
6447         struct Scsi_Host  *shost;
6448 
6449         vports = lpfc_create_vport_work_array(phba);
6450 
6451         /* If driver cannot allocate memory, indicate fcf is in use */
6452         if (!vports)
6453                 return 1;
6454 
6455         for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6456                 shost = lpfc_shost_from_vport(vports[i]);
6457                 spin_lock_irq(shost->host_lock);
6458                 /*
6459                  * IF the CVL_RCVD bit is not set then we have sent the
6460                  * flogi.
6461                  * If dev_loss fires while we are waiting we do not want to
6462                  * unreg the fcf.
6463                  */
6464                 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6465                         spin_unlock_irq(shost->host_lock);
6466                         ret =  1;
6467                         goto out;
6468                 }
6469                 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6470                         if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6471                           (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6472                                 ret = 1;
6473                                 spin_unlock_irq(shost->host_lock);
6474                                 goto out;
6475                         } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6476                                 ret = 1;
6477                                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
6478                                                 "2624 RPI %x DID %x flag %x "
6479                                                 "still logged in\n",
6480                                                 ndlp->nlp_rpi, ndlp->nlp_DID,
6481                                                 ndlp->nlp_flag);
6482                         }
6483                 }
6484                 spin_unlock_irq(shost->host_lock);
6485         }
6486 out:
6487         lpfc_destroy_vport_work_array(phba, vports);
6488         return ret;
6489 }
6490 
6491 /**
6492  * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6493  * @phba: Pointer to hba context object.
6494  * @mboxq: Pointer to mailbox object.
6495  *
6496  * This function frees memory associated with the mailbox command.
6497  */
6498 void
6499 lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6500 {
6501         struct lpfc_vport *vport = mboxq->vport;
6502         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6503 
6504         if (mboxq->u.mb.mbxStatus) {
6505                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6506                         "2555 UNREG_VFI mbxStatus error x%x "
6507                         "HBA state x%x\n",
6508                         mboxq->u.mb.mbxStatus, vport->port_state);
6509         }
6510         spin_lock_irq(shost->host_lock);
6511         phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6512         spin_unlock_irq(shost->host_lock);
6513         mempool_free(mboxq, phba->mbox_mem_pool);
6514         return;
6515 }
6516 
6517 /**
6518  * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6519  * @phba: Pointer to hba context object.
6520  * @mboxq: Pointer to mailbox object.
6521  *
6522  * This function frees memory associated with the mailbox command.
6523  */
6524 static void
6525 lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6526 {
6527         struct lpfc_vport *vport = mboxq->vport;
6528 
6529         if (mboxq->u.mb.mbxStatus) {
6530                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6531                         "2550 UNREG_FCFI mbxStatus error x%x "
6532                         "HBA state x%x\n",
6533                         mboxq->u.mb.mbxStatus, vport->port_state);
6534         }
6535         mempool_free(mboxq, phba->mbox_mem_pool);
6536         return;
6537 }
6538 
6539 /**
6540  * lpfc_unregister_fcf_prep - Unregister fcf record preparation
6541  * @phba: Pointer to hba context object.
6542  *
6543  * This function prepare the HBA for unregistering the currently registered
6544  * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6545  * VFIs.
6546  */
6547 int
6548 lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
6549 {
6550         struct lpfc_vport **vports;
6551         struct lpfc_nodelist *ndlp;
6552         struct Scsi_Host *shost;
6553         int i = 0, rc;
6554 
6555         /* Unregister RPIs */
6556         if (lpfc_fcf_inuse(phba))
6557                 lpfc_unreg_hba_rpis(phba);
6558 
6559         /* At this point, all discovery is aborted */
6560         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
6561 
6562         /* Unregister VPIs */
6563         vports = lpfc_create_vport_work_array(phba);
6564         if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
6565                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6566                         /* Stop FLOGI/FDISC retries */
6567                         ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6568                         if (ndlp)
6569                                 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
6570                         lpfc_cleanup_pending_mbox(vports[i]);
6571                         if (phba->sli_rev == LPFC_SLI_REV4)
6572                                 lpfc_sli4_unreg_all_rpis(vports[i]);
6573                         lpfc_mbx_unreg_vpi(vports[i]);
6574                         shost = lpfc_shost_from_vport(vports[i]);
6575                         spin_lock_irq(shost->host_lock);
6576                         vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6577                         vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
6578                         spin_unlock_irq(shost->host_lock);
6579                 }
6580         lpfc_destroy_vport_work_array(phba, vports);
6581         if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6582                 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6583                 if (ndlp)
6584                         lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6585                 lpfc_cleanup_pending_mbox(phba->pport);
6586                 if (phba->sli_rev == LPFC_SLI_REV4)
6587                         lpfc_sli4_unreg_all_rpis(phba->pport);
6588                 lpfc_mbx_unreg_vpi(phba->pport);
6589                 shost = lpfc_shost_from_vport(phba->pport);
6590                 spin_lock_irq(shost->host_lock);
6591                 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6592                 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6593                 spin_unlock_irq(shost->host_lock);
6594         }
6595 
6596         /* Cleanup any outstanding ELS commands */
6597         lpfc_els_flush_all_cmd(phba);
6598 
6599         /* Unregister the physical port VFI */
6600         rc = lpfc_issue_unreg_vfi(phba->pport);
6601         return rc;
6602 }
6603 
6604 /**
6605  * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6606  * @phba: Pointer to hba context object.
6607  *
6608  * This function issues synchronous unregister FCF mailbox command to HBA to
6609  * unregister the currently registered FCF record. The driver does not reset
6610  * the driver FCF usage state flags.
6611  *
6612  * Return 0 if successfully issued, none-zero otherwise.
6613  */
6614 int
6615 lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6616 {
6617         LPFC_MBOXQ_t *mbox;
6618         int rc;
6619 
6620         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6621         if (!mbox) {
6622                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6623                                 "2551 UNREG_FCFI mbox allocation failed"
6624                                 "HBA state x%x\n", phba->pport->port_state);
6625                 return -ENOMEM;
6626         }
6627         lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6628         mbox->vport = phba->pport;
6629         mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6630         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6631 
6632         if (rc == MBX_NOT_FINISHED) {
6633                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6634                                 "2552 Unregister FCFI command failed rc x%x "
6635                                 "HBA state x%x\n",
6636                                 rc, phba->pport->port_state);
6637                 return -EINVAL;
6638         }
6639         return 0;
6640 }
6641 
6642 /**
6643  * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6644  * @phba: Pointer to hba context object.
6645  *
6646  * This function unregisters the currently reigstered FCF. This function
6647  * also tries to find another FCF for discovery by rescan the HBA FCF table.
6648  */
6649 void
6650 lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6651 {
6652         int rc;
6653 
6654         /* Preparation for unregistering fcf */
6655         rc = lpfc_unregister_fcf_prep(phba);
6656         if (rc) {
6657                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6658                                 "2748 Failed to prepare for unregistering "
6659                                 "HBA's FCF record: rc=%d\n", rc);
6660                 return;
6661         }
6662 
6663         /* Now, unregister FCF record and reset HBA FCF state */
6664         rc = lpfc_sli4_unregister_fcf(phba);
6665         if (rc)
6666                 return;
6667         /* Reset HBA FCF states after successful unregister FCF */
6668         phba->fcf.fcf_flag = 0;
6669         phba->fcf.current_rec.flag = 0;
6670 
6671         /*
6672          * If driver is not unloading, check if there is any other
6673          * FCF record that can be used for discovery.
6674          */
6675         if ((phba->pport->load_flag & FC_UNLOADING) ||
6676             (phba->link_state < LPFC_LINK_UP))
6677                 return;
6678 
6679         /* This is considered as the initial FCF discovery scan */
6680         spin_lock_irq(&phba->hbalock);
6681         phba->fcf.fcf_flag |= FCF_INIT_DISC;
6682         spin_unlock_irq(&phba->hbalock);
6683 
6684         /* Reset FCF roundrobin bmask for new discovery */
6685         lpfc_sli4_clear_fcf_rr_bmask(phba);
6686 
6687         rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
6688 
6689         if (rc) {
6690                 spin_lock_irq(&phba->hbalock);
6691                 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6692                 spin_unlock_irq(&phba->hbalock);
6693                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6694                                 "2553 lpfc_unregister_unused_fcf failed "
6695                                 "to read FCF record HBA state x%x\n",
6696                                 phba->pport->port_state);
6697         }
6698 }
6699 
6700 /**
6701  * lpfc_unregister_fcf - Unregister the currently registered fcf record
6702  * @phba: Pointer to hba context object.
6703  *
6704  * This function just unregisters the currently reigstered FCF. It does not
6705  * try to find another FCF for discovery.
6706  */
6707 void
6708 lpfc_unregister_fcf(struct lpfc_hba *phba)
6709 {
6710         int rc;
6711 
6712         /* Preparation for unregistering fcf */
6713         rc = lpfc_unregister_fcf_prep(phba);
6714         if (rc) {
6715                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6716                                 "2749 Failed to prepare for unregistering "
6717                                 "HBA's FCF record: rc=%d\n", rc);
6718                 return;
6719         }
6720 
6721         /* Now, unregister FCF record and reset HBA FCF state */
6722         rc = lpfc_sli4_unregister_fcf(phba);
6723         if (rc)
6724                 return;
6725         /* Set proper HBA FCF states after successful unregister FCF */
6726         spin_lock_irq(&phba->hbalock);
6727         phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6728         spin_unlock_irq(&phba->hbalock);
6729 }
6730 
6731 /**
6732  * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6733  * @phba: Pointer to hba context object.
6734  *
6735  * This function check if there are any connected remote port for the FCF and
6736  * if all the devices are disconnected, this function unregister FCFI.
6737  * This function also tries to use another FCF for discovery.
6738  */
6739 void
6740 lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6741 {
6742         /*
6743          * If HBA is not running in FIP mode, if HBA does not support
6744          * FCoE, if FCF discovery is ongoing, or if FCF has not been
6745          * registered, do nothing.
6746          */
6747         spin_lock_irq(&phba->hbalock);
6748         if (!(phba->hba_flag & HBA_FCOE_MODE) ||
6749             !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
6750             !(phba->hba_flag & HBA_FIP_SUPPORT) ||
6751             (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
6752             (phba->pport->port_state == LPFC_FLOGI)) {
6753                 spin_unlock_irq(&phba->hbalock);
6754                 return;
6755         }
6756         spin_unlock_irq(&phba->hbalock);
6757 
6758         if (lpfc_fcf_inuse(phba))
6759                 return;
6760 
6761         lpfc_unregister_fcf_rescan(phba);
6762 }
6763 
6764 /**
6765  * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6766  * @phba: Pointer to hba context object.
6767  * @buff: Buffer containing the FCF connection table as in the config
6768  *         region.
6769  * This function create driver data structure for the FCF connection
6770  * record table read from config region 23.
6771  */
6772 static void
6773 lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6774         uint8_t *buff)
6775 {
6776         struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6777         struct lpfc_fcf_conn_hdr *conn_hdr;
6778         struct lpfc_fcf_conn_rec *conn_rec;
6779         uint32_t record_count;
6780         int i;
6781 
6782         /* Free the current connect table */
6783         list_for_each_entry_safe(conn_entry, next_conn_entry,
6784                 &phba->fcf_conn_rec_list, list) {
6785                 list_del_init(&conn_entry->list);
6786                 kfree(conn_entry);
6787         }
6788 
6789         conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6790         record_count = conn_hdr->length * sizeof(uint32_t)/
6791                 sizeof(struct lpfc_fcf_conn_rec);
6792 
6793         conn_rec = (struct lpfc_fcf_conn_rec *)
6794                 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6795 
6796         for (i = 0; i < record_count; i++) {
6797                 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6798                         continue;
6799                 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6800                         GFP_KERNEL);
6801                 if (!conn_entry) {
6802                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6803                                 "2566 Failed to allocate connection"
6804                                 " table entry\n");
6805                         return;
6806                 }
6807 
6808                 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6809                         sizeof(struct lpfc_fcf_conn_rec));
6810                 list_add_tail(&conn_entry->list,
6811                         &phba->fcf_conn_rec_list);
6812         }
6813 
6814         if (!list_empty(&phba->fcf_conn_rec_list)) {
6815                 i = 0;
6816                 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6817                                     list) {
6818                         conn_rec = &conn_entry->conn_rec;
6819                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6820                                         "3345 FCF connection list rec[%02d]: "
6821                                         "flags:x%04x, vtag:x%04x, "
6822                                         "fabric_name:x%02x:%02x:%02x:%02x:"
6823                                         "%02x:%02x:%02x:%02x, "
6824                                         "switch_name:x%02x:%02x:%02x:%02x:"
6825                                         "%02x:%02x:%02x:%02x\n", i++,
6826                                         conn_rec->flags, conn_rec->vlan_tag,
6827                                         conn_rec->fabric_name[0],
6828                                         conn_rec->fabric_name[1],
6829                                         conn_rec->fabric_name[2],
6830                                         conn_rec->fabric_name[3],
6831                                         conn_rec->fabric_name[4],
6832                                         conn_rec->fabric_name[5],
6833                                         conn_rec->fabric_name[6],
6834                                         conn_rec->fabric_name[7],
6835                                         conn_rec->switch_name[0],
6836                                         conn_rec->switch_name[1],
6837                                         conn_rec->switch_name[2],
6838                                         conn_rec->switch_name[3],
6839                                         conn_rec->switch_name[4],
6840                                         conn_rec->switch_name[5],
6841                                         conn_rec->switch_name[6],
6842                                         conn_rec->switch_name[7]);
6843                 }
6844         }
6845 }
6846 
6847 /**
6848  * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6849  * @phba: Pointer to hba context object.
6850  * @buff: Buffer containing the FCoE parameter data structure.
6851  *
6852  *  This function update driver data structure with config
6853  *  parameters read from config region 23.
6854  */
6855 static void
6856 lpfc_read_fcoe_param(struct lpfc_hba *phba,
6857                         uint8_t *buff)
6858 {
6859         struct lpfc_fip_param_hdr *fcoe_param_hdr;
6860         struct lpfc_fcoe_params *fcoe_param;
6861 
6862         fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6863                 buff;
6864         fcoe_param = (struct lpfc_fcoe_params *)
6865                 (buff + sizeof(struct lpfc_fip_param_hdr));
6866 
6867         if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6868                 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6869                 return;
6870 
6871         if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6872                 phba->valid_vlan = 1;
6873                 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6874                         0xFFF;
6875         }
6876 
6877         phba->fc_map[0] = fcoe_param->fc_map[0];
6878         phba->fc_map[1] = fcoe_param->fc_map[1];
6879         phba->fc_map[2] = fcoe_param->fc_map[2];
6880         return;
6881 }
6882 
6883 /**
6884  * lpfc_get_rec_conf23 - Get a record type in config region data.
6885  * @buff: Buffer containing config region 23 data.
6886  * @size: Size of the data buffer.
6887  * @rec_type: Record type to be searched.
6888  *
6889  * This function searches config region data to find the beginning
6890  * of the record specified by record_type. If record found, this
6891  * function return pointer to the record else return NULL.
6892  */
6893 static uint8_t *
6894 lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6895 {
6896         uint32_t offset = 0, rec_length;
6897 
6898         if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6899                 (size < sizeof(uint32_t)))
6900                 return NULL;
6901 
6902         rec_length = buff[offset + 1];
6903 
6904         /*
6905          * One TLV record has one word header and number of data words
6906          * specified in the rec_length field of the record header.
6907          */
6908         while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6909                 <= size) {
6910                 if (buff[offset] == rec_type)
6911                         return &buff[offset];
6912 
6913                 if (buff[offset] == LPFC_REGION23_LAST_REC)
6914                         return NULL;
6915 
6916                 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6917                 rec_length = buff[offset + 1];
6918         }
6919         return NULL;
6920 }
6921 
6922 /**
6923  * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6924  * @phba: Pointer to lpfc_hba data structure.
6925  * @buff: Buffer containing config region 23 data.
6926  * @size: Size of the data buffer.
6927  *
6928  * This function parses the FCoE config parameters in config region 23 and
6929  * populate driver data structure with the parameters.
6930  */
6931 void
6932 lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6933                 uint8_t *buff,
6934                 uint32_t size)
6935 {
6936         uint32_t offset = 0;
6937         uint8_t *rec_ptr;
6938 
6939         /*
6940          * If data size is less than 2 words signature and version cannot be
6941          * verified.
6942          */
6943         if (size < 2*sizeof(uint32_t))
6944                 return;
6945 
6946         /* Check the region signature first */
6947         if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6948                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6949                         "2567 Config region 23 has bad signature\n");
6950                 return;
6951         }
6952 
6953         offset += 4;
6954 
6955         /* Check the data structure version */
6956         if (buff[offset] != LPFC_REGION23_VERSION) {
6957                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6958                         "2568 Config region 23 has bad version\n");
6959                 return;
6960         }
6961         offset += 4;
6962 
6963         /* Read FCoE param record */
6964         rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6965                         size - offset, FCOE_PARAM_TYPE);
6966         if (rec_ptr)
6967                 lpfc_read_fcoe_param(phba, rec_ptr);
6968 
6969         /* Read FCF connection table */
6970         rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6971                 size - offset, FCOE_CONN_TBL_TYPE);
6972         if (rec_ptr)
6973                 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6974 
6975 }

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