]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - drivers/scsi/lpfc/lpfc_hbadisc.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mv-sheeva.git] / drivers / scsi / lpfc / lpfc_hbadisc.c
index 2445e399fd60fd3e76bfbcd3a644519e11206893..e1466eec56b703ce1d8fda03d85d29b2be1d31cd 100644 (file)
@@ -20,6 +20,7 @@
  *******************************************************************/
 
 #include <linux/blkdev.h>
+#include <linux/slab.h>
 #include <linux/pci.h>
 #include <linux/kthread.h>
 #include <linux/interrupt.h>
@@ -525,6 +526,8 @@ lpfc_work_done(struct lpfc_hba *phba)
                        spin_unlock_irq(&phba->hbalock);
                        lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
                }
+               if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
+                       lpfc_sli4_fcf_redisc_event_proc(phba);
        }
 
        vports = lpfc_create_vport_work_array(phba);
@@ -706,6 +709,8 @@ lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
 void
 lpfc_port_link_failure(struct lpfc_vport *vport)
 {
+       lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
+
        /* Cleanup any outstanding received buffers */
        lpfc_cleanup_rcv_buffers(vport);
 
@@ -752,12 +757,14 @@ lpfc_linkdown(struct lpfc_hba *phba)
        lpfc_scsi_dev_block(phba);
 
        spin_lock_irq(&phba->hbalock);
-       phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_DISCOVERED);
+       phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
+       spin_unlock_irq(&phba->hbalock);
        if (phba->link_state > LPFC_LINK_DOWN) {
                phba->link_state = LPFC_LINK_DOWN;
+               spin_lock_irq(shost->host_lock);
                phba->pport->fc_flag &= ~FC_LBIT;
+               spin_unlock_irq(shost->host_lock);
        }
-       spin_unlock_irq(&phba->hbalock);
        vports = lpfc_create_vport_work_array(phba);
        if (vports != NULL)
                for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
@@ -1023,7 +1030,7 @@ lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
                return;
        }
        spin_lock_irqsave(&phba->hbalock, flags);
-       phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
+       phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
        phba->hba_flag &= ~FCF_DISC_INPROGRESS;
        spin_unlock_irqrestore(&phba->hbalock, flags);
        if (vport->port_state != LPFC_FLOGI)
@@ -1045,25 +1052,23 @@ lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
 static uint32_t
 lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
 {
-       if ((fab_name[0] ==
-               bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record)) &&
-           (fab_name[1] ==
-               bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record)) &&
-           (fab_name[2] ==
-               bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record)) &&
-           (fab_name[3] ==
-               bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record)) &&
-           (fab_name[4] ==
-               bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record)) &&
-           (fab_name[5] ==
-               bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record)) &&
-           (fab_name[6] ==
-               bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record)) &&
-           (fab_name[7] ==
-               bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record)))
-               return 1;
-       else
+       if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
+               return 0;
+       if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
                return 0;
+       if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
+               return 0;
+       if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
+               return 0;
+       if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
+               return 0;
+       if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
+               return 0;
+       if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
+               return 0;
+       if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
+               return 0;
+       return 1;
 }
 
 /**
@@ -1078,30 +1083,28 @@ lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
 static uint32_t
 lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
 {
-       if ((sw_name[0] ==
-               bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record)) &&
-           (sw_name[1] ==
-               bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record)) &&
-           (sw_name[2] ==
-               bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record)) &&
-           (sw_name[3] ==
-               bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record)) &&
-           (sw_name[4] ==
-               bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record)) &&
-           (sw_name[5] ==
-               bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record)) &&
-           (sw_name[6] ==
-               bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record)) &&
-           (sw_name[7] ==
-               bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record)))
-               return 1;
-       else
+       if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
+               return 0;
+       if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
+               return 0;
+       if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
+               return 0;
+       if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
+               return 0;
+       if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
+               return 0;
+       if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
                return 0;
+       if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
+               return 0;
+       if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
+               return 0;
+       return 1;
 }
 
 /**
  * lpfc_mac_addr_match - Check if the fcf mac address match.
- * @phba: pointer to lpfc hba data structure.
+ * @mac_addr: pointer to mac address.
  * @new_fcf_record: pointer to fcf record.
  *
  * This routine compare the fcf record's mac address with HBA's
@@ -1109,84 +1112,114 @@ lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
  * returns 1 else return 0.
  **/
 static uint32_t
-lpfc_mac_addr_match(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
+lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
 {
-       if ((phba->fcf.mac_addr[0] ==
-               bf_get(lpfc_fcf_record_mac_0, new_fcf_record)) &&
-           (phba->fcf.mac_addr[1] ==
-               bf_get(lpfc_fcf_record_mac_1, new_fcf_record)) &&
-           (phba->fcf.mac_addr[2] ==
-               bf_get(lpfc_fcf_record_mac_2, new_fcf_record)) &&
-           (phba->fcf.mac_addr[3] ==
-               bf_get(lpfc_fcf_record_mac_3, new_fcf_record)) &&
-           (phba->fcf.mac_addr[4] ==
-               bf_get(lpfc_fcf_record_mac_4, new_fcf_record)) &&
-           (phba->fcf.mac_addr[5] ==
-               bf_get(lpfc_fcf_record_mac_5, new_fcf_record)))
-               return 1;
-       else
+       if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
+               return 0;
+       if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
                return 0;
+       if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
+               return 0;
+       if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
+               return 0;
+       if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
+               return 0;
+       if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
+               return 0;
+       return 1;
+}
+
+static bool
+lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
+{
+       return (curr_vlan_id == new_vlan_id);
 }
 
 /**
  * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
- * @phba: pointer to lpfc hba data structure.
+ * @fcf: pointer to driver fcf record.
  * @new_fcf_record: pointer to fcf record.
  *
  * This routine copies the FCF information from the FCF
  * record to lpfc_hba data structure.
  **/
 static void
-lpfc_copy_fcf_record(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
+lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
+                    struct fcf_record *new_fcf_record)
 {
-       phba->fcf.fabric_name[0] =
+       /* Fabric name */
+       fcf_rec->fabric_name[0] =
                bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
-       phba->fcf.fabric_name[1] =
+       fcf_rec->fabric_name[1] =
                bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
-       phba->fcf.fabric_name[2] =
+       fcf_rec->fabric_name[2] =
                bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
-       phba->fcf.fabric_name[3] =
+       fcf_rec->fabric_name[3] =
                bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
-       phba->fcf.fabric_name[4] =
+       fcf_rec->fabric_name[4] =
                bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
-       phba->fcf.fabric_name[5] =
+       fcf_rec->fabric_name[5] =
                bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
-       phba->fcf.fabric_name[6] =
+       fcf_rec->fabric_name[6] =
                bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
-       phba->fcf.fabric_name[7] =
+       fcf_rec->fabric_name[7] =
                bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
-       phba->fcf.mac_addr[0] =
-               bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
-       phba->fcf.mac_addr[1] =
-               bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
-       phba->fcf.mac_addr[2] =
-               bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
-       phba->fcf.mac_addr[3] =
-               bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
-       phba->fcf.mac_addr[4] =
-               bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
-       phba->fcf.mac_addr[5] =
-               bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
-       phba->fcf.fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
-       phba->fcf.priority = new_fcf_record->fip_priority;
-       phba->fcf.switch_name[0] =
+       /* Mac address */
+       fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
+       fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
+       fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
+       fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
+       fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
+       fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
+       /* FCF record index */
+       fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
+       /* FCF record priority */
+       fcf_rec->priority = new_fcf_record->fip_priority;
+       /* Switch name */
+       fcf_rec->switch_name[0] =
                bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
-       phba->fcf.switch_name[1] =
+       fcf_rec->switch_name[1] =
                bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
-       phba->fcf.switch_name[2] =
+       fcf_rec->switch_name[2] =
                bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
-       phba->fcf.switch_name[3] =
+       fcf_rec->switch_name[3] =
                bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
-       phba->fcf.switch_name[4] =
+       fcf_rec->switch_name[4] =
                bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
-       phba->fcf.switch_name[5] =
+       fcf_rec->switch_name[5] =
                bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
-       phba->fcf.switch_name[6] =
+       fcf_rec->switch_name[6] =
                bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
-       phba->fcf.switch_name[7] =
+       fcf_rec->switch_name[7] =
                bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
 }
 
+/**
+ * lpfc_update_fcf_record - Update driver fcf record
+ * @phba: pointer to lpfc hba data structure.
+ * @fcf_rec: pointer to driver fcf record.
+ * @new_fcf_record: pointer to hba fcf record.
+ * @addr_mode: address mode to be set to the driver fcf record.
+ * @vlan_id: vlan tag to be set to the driver fcf record.
+ * @flag: flag bits to be set to the driver fcf record.
+ *
+ * This routine updates the driver FCF record from the new HBA FCF record
+ * together with the address mode, vlan_id, and other informations. This
+ * routine is called with the host lock held.
+ **/
+static void
+__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
+                      struct fcf_record *new_fcf_record, uint32_t addr_mode,
+                      uint16_t vlan_id, uint32_t flag)
+{
+       /* Copy the fields from the HBA's FCF record */
+       lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
+       /* Update other fields of driver FCF record */
+       fcf_rec->addr_mode = addr_mode;
+       fcf_rec->vlan_id = vlan_id;
+       fcf_rec->flag |= (flag | RECORD_VALID);
+}
+
 /**
  * lpfc_register_fcf - Register the FCF with hba.
  * @phba: pointer to lpfc hba data structure.
@@ -1212,7 +1245,7 @@ lpfc_register_fcf(struct lpfc_hba *phba)
 
        /* The FCF is already registered, start discovery */
        if (phba->fcf.fcf_flag & FCF_REGISTERED) {
-               phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
+               phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
                phba->hba_flag &= ~FCF_DISC_INPROGRESS;
                spin_unlock_irqrestore(&phba->hbalock, flags);
                if (phba->pport->port_state != LPFC_FLOGI)
@@ -1250,6 +1283,7 @@ lpfc_register_fcf(struct lpfc_hba *phba)
  * @new_fcf_record: pointer to fcf record.
  * @boot_flag: Indicates if this record used by boot bios.
  * @addr_mode: The address mode to be used by this FCF
+ * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
  *
  * This routine compare the fcf record with connect list obtained from the
  * config region to decide if this FCF can be used for SAN discovery. It returns
@@ -1323,7 +1357,8 @@ lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
                return 1;
        }
 
-       list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list, list) {
+       list_for_each_entry(conn_entry,
+                           &phba->fcf_conn_rec_list, list) {
                if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
                        continue;
 
@@ -1447,8 +1482,6 @@ lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
 int
 lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
 {
-       LPFC_MBOXQ_t *mbox;
-       int rc;
        /*
         * If the Link is up and no FCoE events while in the
         * FCF discovery, no need to restart FCF discovery.
@@ -1457,84 +1490,70 @@ lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
                (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
                return 0;
 
+       lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
+                       "2768 Pending link or FCF event during current "
+                       "handling of the previous event: link_state:x%x, "
+                       "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
+                       phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
+                       phba->fcoe_eventtag);
+
        spin_lock_irq(&phba->hbalock);
        phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
        spin_unlock_irq(&phba->hbalock);
 
-       if (phba->link_state >= LPFC_LINK_UP)
-               lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
-       else {
+       if (phba->link_state >= LPFC_LINK_UP) {
+               lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
+                               "2780 Restart FCF table scan due to "
+                               "pending FCF event:evt_tag_at_scan:x%x, "
+                               "evt_tag_current:x%x\n",
+                               phba->fcoe_eventtag_at_fcf_scan,
+                               phba->fcoe_eventtag);
+               lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
+       } else {
                /*
                 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
                 * flag
                 */
                spin_lock_irq(&phba->hbalock);
                phba->hba_flag &= ~FCF_DISC_INPROGRESS;
+               phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
                spin_unlock_irq(&phba->hbalock);
        }
 
+       /* Unregister the currently registered FCF if required */
        if (unreg_fcf) {
                spin_lock_irq(&phba->hbalock);
                phba->fcf.fcf_flag &= ~FCF_REGISTERED;
                spin_unlock_irq(&phba->hbalock);
-               mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
-               if (!mbox) {
-                       lpfc_printf_log(phba, KERN_ERR,
-                               LOG_DISCOVERY|LOG_MBOX,
-                               "2610 UNREG_FCFI mbox allocation failed\n");
-                       return 1;
-               }
-               lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
-               mbox->vport = phba->pport;
-               mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
-               rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
-               if (rc == MBX_NOT_FINISHED) {
-                       lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
-                               "2611 UNREG_FCFI issue mbox failed\n");
-                       mempool_free(mbox, phba->mbox_mem_pool);
-               }
+               lpfc_sli4_unregister_fcf(phba);
        }
-
        return 1;
 }
 
 /**
- * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
+ * lpfc_sli4_fcf_rec_mbox_parse - parse non-embedded fcf record mailbox command
  * @phba: pointer to lpfc hba data structure.
  * @mboxq: pointer to mailbox object.
+ * @next_fcf_index: pointer to holder of next fcf index.
  *
- * This function iterate through all the fcf records available in
- * HBA and choose the optimal FCF record for discovery. After finding
- * the FCF for discovery it register the FCF record and kick start
- * discovery.
- * If FCF_IN_USE flag is set in currently used FCF, the routine try to
- * use a FCF record which match fabric name and mac address of the
- * currently used FCF record.
- * If the driver support only one FCF, it will try to use the FCF record
- * used by BOOT_BIOS.
+ * This routine parses the non-embedded fcf mailbox command by performing the
+ * necessarily error checking, non-embedded read FCF record mailbox command
+ * SGE parsing, and endianness swapping.
+ *
+ * Returns the pointer to the new FCF record in the non-embedded mailbox
+ * command DMA memory if successfully, other NULL.
  */
-void
-lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
+static struct fcf_record *
+lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
+                            uint16_t *next_fcf_index)
 {
        void *virt_addr;
        dma_addr_t phys_addr;
-       uint8_t *bytep;
        struct lpfc_mbx_sge sge;
        struct lpfc_mbx_read_fcf_tbl *read_fcf;
        uint32_t shdr_status, shdr_add_status;
        union lpfc_sli4_cfg_shdr *shdr;
        struct fcf_record *new_fcf_record;
-       int rc;
-       uint32_t boot_flag, addr_mode;
-       uint32_t next_fcf_index;
-       unsigned long flags;
-       uint16_t vlan_id;
-
-       /* If there is pending FCoE event restart FCF table scan */
-       if (lpfc_check_pending_fcoe_event(phba, 0)) {
-               lpfc_sli4_mbox_cmd_free(phba, mboxq);
-               return;
-       }
 
        /* Get the first SGE entry from the non-embedded DMA memory. This
         * routine only uses a single SGE.
@@ -1545,140 +1564,359 @@ lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
                                "2524 Failed to get the non-embedded SGE "
                                "virtual address\n");
-               goto out;
+               return NULL;
        }
        virt_addr = mboxq->sge_array->addr[0];
 
        shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
-       shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
-                                &shdr->response);
-       /*
-        * The FCF Record was read and there is no reason for the driver
-        * to maintain the FCF record data or memory. Instead, just need
-        * to book keeping the FCFIs can be used.
-        */
+       shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
        if (shdr_status || shdr_add_status) {
-               if (shdr_status == STATUS_FCF_TABLE_EMPTY) {
-                       lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
+               if (shdr_status == STATUS_FCF_TABLE_EMPTY)
+                       lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
                                        "2726 READ_FCF_RECORD Indicates empty "
                                        "FCF table.\n");
-               } else {
-                       lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
+               else
+                       lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
                                        "2521 READ_FCF_RECORD mailbox failed "
-                                       "with status x%x add_status x%x, mbx\n",
-                                       shdr_status, shdr_add_status);
-               }
-               goto out;
+                                       "with status x%x add_status x%x, "
+                                       "mbx\n", shdr_status, shdr_add_status);
+               return NULL;
        }
-       /* Interpreting the returned information of FCF records */
+
+       /* Interpreting the returned information of the FCF record */
        read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
        lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
                              sizeof(struct lpfc_mbx_read_fcf_tbl));
-       next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
-
+       *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
        new_fcf_record = (struct fcf_record *)(virt_addr +
                          sizeof(struct lpfc_mbx_read_fcf_tbl));
        lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
                              sizeof(struct fcf_record));
-       bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
 
-       rc = lpfc_match_fcf_conn_list(phba, new_fcf_record,
-                                     &boot_flag, &addr_mode,
-                                       &vlan_id);
+       return new_fcf_record;
+}
+
+/**
+ * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
+ * @phba: pointer to lpfc hba data structure.
+ * @fcf_record: pointer to the fcf record.
+ * @vlan_id: the lowest vlan identifier associated to this fcf record.
+ * @next_fcf_index: the index to the next fcf record in hba's fcf table.
+ *
+ * This routine logs the detailed FCF record if the LOG_FIP loggin is
+ * enabled.
+ **/
+static void
+lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
+                             struct fcf_record *fcf_record,
+                             uint16_t vlan_id,
+                             uint16_t next_fcf_index)
+{
+       lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
+                       "2764 READ_FCF_RECORD:\n"
+                       "\tFCF_Index     : x%x\n"
+                       "\tFCF_Avail     : x%x\n"
+                       "\tFCF_Valid     : x%x\n"
+                       "\tFIP_Priority  : x%x\n"
+                       "\tMAC_Provider  : x%x\n"
+                       "\tLowest VLANID : x%x\n"
+                       "\tFCF_MAC Addr  : x%x:%x:%x:%x:%x:%x\n"
+                       "\tFabric_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
+                       "\tSwitch_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
+                       "\tNext_FCF_Index: x%x\n",
+                       bf_get(lpfc_fcf_record_fcf_index, fcf_record),
+                       bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
+                       bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
+                       fcf_record->fip_priority,
+                       bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
+                       vlan_id,
+                       bf_get(lpfc_fcf_record_mac_0, fcf_record),
+                       bf_get(lpfc_fcf_record_mac_1, fcf_record),
+                       bf_get(lpfc_fcf_record_mac_2, fcf_record),
+                       bf_get(lpfc_fcf_record_mac_3, fcf_record),
+                       bf_get(lpfc_fcf_record_mac_4, fcf_record),
+                       bf_get(lpfc_fcf_record_mac_5, fcf_record),
+                       bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
+                       bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
+                       bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
+                       bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
+                       bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
+                       bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
+                       bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
+                       bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
+                       bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
+                       bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
+                       bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
+                       bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
+                       bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
+                       bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
+                       bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
+                       bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
+                       next_fcf_index);
+}
+
+/**
+ * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
+ * @phba: pointer to lpfc hba data structure.
+ * @mboxq: pointer to mailbox object.
+ *
+ * This function iterates through all the fcf records available in
+ * HBA and chooses the optimal FCF record for discovery. After finding
+ * the FCF for discovery it registers the FCF record and kicks start
+ * discovery.
+ * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
+ * use an FCF record which matches fabric name and mac address of the
+ * currently used FCF record.
+ * If the driver supports only one FCF, it will try to use the FCF record
+ * used by BOOT_BIOS.
+ */
+void
+lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
+{
+       struct fcf_record *new_fcf_record;
+       uint32_t boot_flag, addr_mode;
+       uint16_t fcf_index, next_fcf_index;
+       struct lpfc_fcf_rec *fcf_rec = NULL;
+       uint16_t vlan_id;
+       int rc;
+
+       /* If there is pending FCoE event restart FCF table scan */
+       if (lpfc_check_pending_fcoe_event(phba, 0)) {
+               lpfc_sli4_mbox_cmd_free(phba, mboxq);
+               return;
+       }
+
+       /* Parse the FCF record from the non-embedded mailbox command */
+       new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
+                                                     &next_fcf_index);
+       if (!new_fcf_record) {
+               lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
+                               "2765 Mailbox command READ_FCF_RECORD "
+                               "failed to retrieve a FCF record.\n");
+               /* Let next new FCF event trigger fast failover */
+               spin_lock_irq(&phba->hbalock);
+               phba->hba_flag &= ~FCF_DISC_INPROGRESS;
+               spin_unlock_irq(&phba->hbalock);
+               lpfc_sli4_mbox_cmd_free(phba, mboxq);
+               return;
+       }
+
+       /* Check the FCF record against the connection list */
+       rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
+                                     &addr_mode, &vlan_id);
+
+       /* Log the FCF record information if turned on */
+       lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
+                                     next_fcf_index);
+
        /*
         * If the fcf record does not match with connect list entries
-        * read the next entry.
+        * read the next entry; otherwise, this is an eligible FCF
+        * record for round robin FCF failover.
         */
-       if (!rc)
+       if (!rc) {
+               lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
+                               "2781 FCF record fcf_index:x%x failed FCF "
+                               "connection list check, fcf_avail:x%x, "
+                               "fcf_valid:x%x\n",
+                               bf_get(lpfc_fcf_record_fcf_index,
+                                      new_fcf_record),
+                               bf_get(lpfc_fcf_record_fcf_avail,
+                                      new_fcf_record),
+                               bf_get(lpfc_fcf_record_fcf_valid,
+                                      new_fcf_record));
                goto read_next_fcf;
+       } else {
+               fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
+               rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
+               if (rc)
+                       goto read_next_fcf;
+       }
+
        /*
         * If this is not the first FCF discovery of the HBA, use last
-        * FCF record for the discovery.
+        * FCF record for the discovery. The condition that a rescan
+        * matches the in-use FCF record: fabric name, switch name, mac
+        * address, and vlan_id.
         */
-       spin_lock_irqsave(&phba->hbalock, flags);
+       spin_lock_irq(&phba->hbalock);
        if (phba->fcf.fcf_flag & FCF_IN_USE) {
-               if (lpfc_fab_name_match(phba->fcf.fabric_name,
+               if (lpfc_fab_name_match(phba->fcf.current_rec.fabric_name,
                                        new_fcf_record) &&
-                   lpfc_sw_name_match(phba->fcf.switch_name,
+                   lpfc_sw_name_match(phba->fcf.current_rec.switch_name,
                                        new_fcf_record) &&
-                   lpfc_mac_addr_match(phba, new_fcf_record)) {
+                   lpfc_mac_addr_match(phba->fcf.current_rec.mac_addr,
+                                       new_fcf_record) &&
+                   lpfc_vlan_id_match(phba->fcf.current_rec.vlan_id,
+                                       vlan_id)) {
                        phba->fcf.fcf_flag |= FCF_AVAILABLE;
-                       spin_unlock_irqrestore(&phba->hbalock, flags);
+                       if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
+                               /* Stop FCF redisc wait timer if pending */
+                               __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
+                       else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
+                               /* If in fast failover, mark it's completed */
+                               phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV |
+                                                       FCF_DISCOVERY);
+                       spin_unlock_irq(&phba->hbalock);
                        goto out;
                }
-               spin_unlock_irqrestore(&phba->hbalock, flags);
-               goto read_next_fcf;
+               /*
+                * Read next FCF record from HBA searching for the matching
+                * with in-use record only if not during the fast failover
+                * period. In case of fast failover period, it shall try to
+                * determine whether the FCF record just read should be the
+                * next candidate.
+                */
+               if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
+                       spin_unlock_irq(&phba->hbalock);
+                       goto read_next_fcf;
+               }
        }
+       /*
+        * Update on failover FCF record only if it's in FCF fast-failover
+        * period; otherwise, update on current FCF record.
+        */
+       if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
+               fcf_rec = &phba->fcf.failover_rec;
+       else
+               fcf_rec = &phba->fcf.current_rec;
+
        if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
                /*
-                * If the current FCF record does not have boot flag
-                * set and new fcf record has boot flag set, use the
-                * new fcf record.
+                * If the driver FCF record does not have boot flag
+                * set and new hba fcf record has boot flag set, use
+                * the new hba fcf record.
                 */
-               if (boot_flag && !(phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
-                       /* Use this FCF record */
-                       lpfc_copy_fcf_record(phba, new_fcf_record);
-                       phba->fcf.addr_mode = addr_mode;
-                       phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
-                       if (vlan_id != 0xFFFF) {
-                               phba->fcf.fcf_flag |= FCF_VALID_VLAN;
-                               phba->fcf.vlan_id = vlan_id;
-                       }
-                       spin_unlock_irqrestore(&phba->hbalock, flags);
+               if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
+                       /* Choose this FCF record */
+                       __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
+                                       addr_mode, vlan_id, BOOT_ENABLE);
+                       spin_unlock_irq(&phba->hbalock);
                        goto read_next_fcf;
                }
                /*
-                * If the current FCF record has boot flag set and the
-                * new FCF record does not have boot flag, read the next
-                * FCF record.
+                * If the driver FCF record has boot flag set and the
+                * new hba FCF record does not have boot flag, read
+                * the next FCF record.
                 */
-               if (!boot_flag && (phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
-                       spin_unlock_irqrestore(&phba->hbalock, flags);
+               if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
+                       spin_unlock_irq(&phba->hbalock);
                        goto read_next_fcf;
                }
                /*
-                * If there is a record with lower priority value for
-                * the current FCF, use that record.
+                * If the new hba FCF record has lower priority value
+                * than the driver FCF record, use the new record.
                 */
-               if (lpfc_fab_name_match(phba->fcf.fabric_name,
-                                       new_fcf_record) &&
-                   (new_fcf_record->fip_priority < phba->fcf.priority)) {
-                       /* Use this FCF record */
-                       lpfc_copy_fcf_record(phba, new_fcf_record);
-                       phba->fcf.addr_mode = addr_mode;
-                       if (vlan_id != 0xFFFF) {
-                               phba->fcf.fcf_flag |= FCF_VALID_VLAN;
-                               phba->fcf.vlan_id = vlan_id;
-                       }
-                       spin_unlock_irqrestore(&phba->hbalock, flags);
-                       goto read_next_fcf;
+               if (new_fcf_record->fip_priority < fcf_rec->priority) {
+                       /* Choose this FCF record */
+                       __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
+                                       addr_mode, vlan_id, 0);
                }
-               spin_unlock_irqrestore(&phba->hbalock, flags);
+               spin_unlock_irq(&phba->hbalock);
                goto read_next_fcf;
        }
        /*
-        * This is the first available FCF record, use this
-        * record.
+        * This is the first suitable FCF record, choose this record for
+        * initial best-fit FCF.
         */
-       lpfc_copy_fcf_record(phba, new_fcf_record);
-       phba->fcf.addr_mode = addr_mode;
-       if (boot_flag)
-               phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
-       phba->fcf.fcf_flag |= FCF_AVAILABLE;
-       if (vlan_id != 0xFFFF) {
-               phba->fcf.fcf_flag |= FCF_VALID_VLAN;
-               phba->fcf.vlan_id = vlan_id;
+       if (fcf_rec) {
+               __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
+                                        addr_mode, vlan_id, (boot_flag ?
+                                        BOOT_ENABLE : 0));
+               phba->fcf.fcf_flag |= FCF_AVAILABLE;
        }
-       spin_unlock_irqrestore(&phba->hbalock, flags);
+       spin_unlock_irq(&phba->hbalock);
        goto read_next_fcf;
 
 read_next_fcf:
        lpfc_sli4_mbox_cmd_free(phba, mboxq);
-       if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0)
-               lpfc_register_fcf(phba);
-       else
-               lpfc_sli4_read_fcf_record(phba, next_fcf_index);
+       if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
+               if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
+                       /*
+                        * Case of FCF fast failover scan
+                        */
+
+                       /*
+                        * It has not found any suitable FCF record, cancel
+                        * FCF scan inprogress, and do nothing
+                        */
+                       if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
+                               lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
+                                              "2782 No suitable FCF record "
+                                              "found during this round of "
+                                              "post FCF rediscovery scan: "
+                                              "fcf_evt_tag:x%x, fcf_index: "
+                                              "x%x\n",
+                                              phba->fcoe_eventtag_at_fcf_scan,
+                                              bf_get(lpfc_fcf_record_fcf_index,
+                                                     new_fcf_record));
+                               /*
+                                * Let next new FCF event trigger fast
+                                * failover
+                                */
+                               spin_lock_irq(&phba->hbalock);
+                               phba->hba_flag &= ~FCF_DISC_INPROGRESS;
+                               spin_unlock_irq(&phba->hbalock);
+                               return;
+                       }
+                       /*
+                        * It has found a suitable FCF record that is not
+                        * the same as in-use FCF record, unregister the
+                        * in-use FCF record, replace the in-use FCF record
+                        * with the new FCF record, mark FCF fast failover
+                        * completed, and then start register the new FCF
+                        * record.
+                        */
+
+                       /* Unregister the current in-use FCF record */
+                       lpfc_unregister_fcf(phba);
+
+                       /* Replace in-use record with the new record */
+                       memcpy(&phba->fcf.current_rec,
+                              &phba->fcf.failover_rec,
+                              sizeof(struct lpfc_fcf_rec));
+                       /* mark the FCF fast failover completed */
+                       spin_lock_irq(&phba->hbalock);
+                       phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
+                       spin_unlock_irq(&phba->hbalock);
+                       /*
+                        * Set up the initial registered FCF index for FLOGI
+                        * round robin FCF failover.
+                        */
+                       phba->fcf.fcf_rr_init_indx =
+                                       phba->fcf.failover_rec.fcf_indx;
+                       /* Register to the new FCF record */
+                       lpfc_register_fcf(phba);
+               } else {
+                       /*
+                        * In case of transaction period to fast FCF failover,
+                        * do nothing when search to the end of the FCF table.
+                        */
+                       if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
+                           (phba->fcf.fcf_flag & FCF_REDISC_PEND))
+                               return;
+                       /*
+                        * Otherwise, initial scan or post linkdown rescan,
+                        * register with the best FCF record found so far
+                        * through the FCF scanning process.
+                        */
+
+                       /* mark the initial FCF discovery completed */
+                       spin_lock_irq(&phba->hbalock);
+                       phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
+                       spin_unlock_irq(&phba->hbalock);
+                       /*
+                        * Set up the initial registered FCF index for FLOGI
+                        * round robin FCF failover
+                        */
+                       phba->fcf.fcf_rr_init_indx =
+                                       phba->fcf.current_rec.fcf_indx;
+                       /* Register to the new FCF record */
+                       lpfc_register_fcf(phba);
+               }
+       } else
+               lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
        return;
 
 out:
@@ -1688,6 +1926,141 @@ out:
        return;
 }
 
+/**
+ * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf round robin read_fcf mbox cmpl hdler
+ * @phba: pointer to lpfc hba data structure.
+ * @mboxq: pointer to mailbox object.
+ *
+ * This is the callback function for FLOGI failure round robin FCF failover
+ * read FCF record mailbox command from the eligible FCF record bmask for
+ * performing the failover. If the FCF read back is not valid/available, it
+ * fails through to retrying FLOGI to the currently registered FCF again.
+ * Otherwise, if the FCF read back is valid and available, it will set the
+ * newly read FCF record to the failover FCF record, unregister currently
+ * registered FCF record, copy the failover FCF record to the current
+ * FCF record, and then register the current FCF record before proceeding
+ * to trying FLOGI on the new failover FCF.
+ */
+void
+lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
+{
+       struct fcf_record *new_fcf_record;
+       uint32_t boot_flag, addr_mode;
+       uint16_t next_fcf_index;
+       uint16_t current_fcf_index;
+       uint16_t vlan_id;
+
+       /* If link state is not up, stop the round robin failover process */
+       if (phba->link_state < LPFC_LINK_UP) {
+               spin_lock_irq(&phba->hbalock);
+               phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
+               spin_unlock_irq(&phba->hbalock);
+               lpfc_sli4_mbox_cmd_free(phba, mboxq);
+               return;
+       }
+
+       /* Parse the FCF record from the non-embedded mailbox command */
+       new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
+                                                     &next_fcf_index);
+       if (!new_fcf_record) {
+               lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
+                               "2766 Mailbox command READ_FCF_RECORD "
+                               "failed to retrieve a FCF record.\n");
+               goto out;
+       }
+
+       /* Get the needed parameters from FCF record */
+       lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
+                                &addr_mode, &vlan_id);
+
+       /* Log the FCF record information if turned on */
+       lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
+                                     next_fcf_index);
+
+       /* Upload new FCF record to the failover FCF record */
+       spin_lock_irq(&phba->hbalock);
+       __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
+                                new_fcf_record, addr_mode, vlan_id,
+                                (boot_flag ? BOOT_ENABLE : 0));
+       spin_unlock_irq(&phba->hbalock);
+
+       current_fcf_index = phba->fcf.current_rec.fcf_indx;
+
+       /* Unregister the current in-use FCF record */
+       lpfc_unregister_fcf(phba);
+
+       /* Replace in-use record with the new record */
+       memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
+              sizeof(struct lpfc_fcf_rec));
+
+       lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
+                       "2783 FLOGI round robin FCF failover from FCF "
+                       "(index:x%x) to FCF (index:x%x).\n",
+                       current_fcf_index,
+                       bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
+
+out:
+       lpfc_sli4_mbox_cmd_free(phba, mboxq);
+       lpfc_register_fcf(phba);
+}
+
+/**
+ * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
+ * @phba: pointer to lpfc hba data structure.
+ * @mboxq: pointer to mailbox object.
+ *
+ * This is the callback function of read FCF record mailbox command for
+ * updating the eligible FCF bmask for FLOGI failure round robin FCF
+ * failover when a new FCF event happened. If the FCF read back is
+ * valid/available and it passes the connection list check, it updates
+ * the bmask for the eligible FCF record for round robin failover.
+ */
+void
+lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
+{
+       struct fcf_record *new_fcf_record;
+       uint32_t boot_flag, addr_mode;
+       uint16_t fcf_index, next_fcf_index;
+       uint16_t vlan_id;
+       int rc;
+
+       /* If link state is not up, no need to proceed */
+       if (phba->link_state < LPFC_LINK_UP)
+               goto out;
+
+       /* If FCF discovery period is over, no need to proceed */
+       if (phba->fcf.fcf_flag & FCF_DISCOVERY)
+               goto out;
+
+       /* Parse the FCF record from the non-embedded mailbox command */
+       new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
+                                                     &next_fcf_index);
+       if (!new_fcf_record) {
+               lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
+                               "2767 Mailbox command READ_FCF_RECORD "
+                               "failed to retrieve a FCF record.\n");
+               goto out;
+       }
+
+       /* Check the connection list for eligibility */
+       rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
+                                     &addr_mode, &vlan_id);
+
+       /* Log the FCF record information if turned on */
+       lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
+                                     next_fcf_index);
+
+       if (!rc)
+               goto out;
+
+       /* Update the eligible FCF record index bmask */
+       fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
+       rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
+
+out:
+       lpfc_sli4_mbox_cmd_free(phba, mboxq);
+}
+
 /**
  * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
  * @phba: pointer to lpfc hba data structure.
@@ -1695,10 +2068,13 @@ out:
  *
  * This function handles completion of init vpi mailbox command.
  */
-static void
+void
 lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
 {
        struct lpfc_vport *vport = mboxq->vport;
+       struct lpfc_nodelist *ndlp;
+       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
+
        if (mboxq->u.mb.mbxStatus) {
                lpfc_printf_vlog(vport, KERN_ERR,
                                LOG_MBOX,
@@ -1708,9 +2084,23 @@ lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
                lpfc_vport_set_state(vport, FC_VPORT_FAILED);
                return;
        }
-       spin_lock_irq(&phba->hbalock);
+       spin_lock_irq(shost->host_lock);
        vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
-       spin_unlock_irq(&phba->hbalock);
+       spin_unlock_irq(shost->host_lock);
+
+       /* If this port is physical port or FDISC is done, do reg_vpi */
+       if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
+                       ndlp = lpfc_findnode_did(vport, Fabric_DID);
+                       if (!ndlp)
+                               lpfc_printf_vlog(vport, KERN_ERR,
+                                       LOG_DISCOVERY,
+                                       "2731 Cannot find fabric "
+                                       "controller node\n");
+                       else
+                               lpfc_register_new_vport(phba, vport, ndlp);
+                       mempool_free(mboxq, phba->mbox_mem_pool);
+                       return;
+       }
 
        if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
                lpfc_initial_fdisc(vport);
@@ -1719,9 +2109,41 @@ lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
                lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
                                 "2606 No NPIV Fabric support\n");
        }
+       mempool_free(mboxq, phba->mbox_mem_pool);
        return;
 }
 
+/**
+ * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
+ * @vport: pointer to lpfc_vport data structure.
+ *
+ * This function issue a init_vpi mailbox command to initialize
+ * VPI for the vport.
+ */
+void
+lpfc_issue_init_vpi(struct lpfc_vport *vport)
+{
+       LPFC_MBOXQ_t *mboxq;
+       int rc;
+
+       mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
+       if (!mboxq) {
+               lpfc_printf_vlog(vport, KERN_ERR,
+                       LOG_MBOX, "2607 Failed to allocate "
+                       "init_vpi mailbox\n");
+               return;
+       }
+       lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
+       mboxq->vport = vport;
+       mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
+       rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
+       if (rc == MBX_NOT_FINISHED) {
+               lpfc_printf_vlog(vport, KERN_ERR,
+                       LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
+               mempool_free(mboxq, vport->phba->mbox_mem_pool);
+       }
+}
+
 /**
  * lpfc_start_fdiscs - send fdiscs for each vports on this port.
  * @phba: pointer to lpfc hba data structure.
@@ -1734,8 +2156,6 @@ lpfc_start_fdiscs(struct lpfc_hba *phba)
 {
        struct lpfc_vport **vports;
        int i;
-       LPFC_MBOXQ_t *mboxq;
-       int rc;
 
        vports = lpfc_create_vport_work_array(phba);
        if (vports != NULL) {
@@ -1754,26 +2174,7 @@ lpfc_start_fdiscs(struct lpfc_hba *phba)
                                continue;
                        }
                        if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
-                               mboxq = mempool_alloc(phba->mbox_mem_pool,
-                                       GFP_KERNEL);
-                               if (!mboxq) {
-                                       lpfc_printf_vlog(vports[i], KERN_ERR,
-                                       LOG_MBOX, "2607 Failed to allocate "
-                                       "init_vpi mailbox\n");
-                                       continue;
-                               }
-                               lpfc_init_vpi(phba, mboxq, vports[i]->vpi);
-                               mboxq->vport = vports[i];
-                               mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
-                               rc = lpfc_sli_issue_mbox(phba, mboxq,
-                                       MBX_NOWAIT);
-                               if (rc == MBX_NOT_FINISHED) {
-                                       lpfc_printf_vlog(vports[i], KERN_ERR,
-                                       LOG_MBOX, "2608 Failed to issue "
-                                       "init_vpi mailbox\n");
-                                       mempool_free(mboxq,
-                                               phba->mbox_mem_pool);
-                               }
+                               lpfc_issue_init_vpi(vports[i]);
                                continue;
                        }
                        if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
@@ -1796,6 +2197,7 @@ lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
 {
        struct lpfc_dmabuf *dmabuf = mboxq->context1;
        struct lpfc_vport *vport = mboxq->vport;
+       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 
        if (mboxq->u.mb.mbxStatus) {
                lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
@@ -1813,7 +2215,11 @@ lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
                goto fail_free_mem;
        }
        /* The VPI is implicitly registered when the VFI is registered */
+       spin_lock_irq(shost->host_lock);
        vport->vpi_state |= LPFC_VPI_REGISTERED;
+       vport->fc_flag |= FC_VFI_REGISTERED;
+       vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
+       spin_unlock_irq(shost->host_lock);
 
        if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
                lpfc_start_fdiscs(phba);
@@ -1887,8 +2293,6 @@ lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
        int rc;
        struct fcf_record *fcf_record;
 
-       sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
-
        spin_lock_irq(&phba->hbalock);
        switch (la->UlnkSpeed) {
        case LA_1GHZ_LINK:
@@ -1980,18 +2384,24 @@ lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
        spin_unlock_irq(&phba->hbalock);
 
        lpfc_linkup(phba);
-       if (sparam_mbox) {
-               lpfc_read_sparam(phba, sparam_mbox, 0);
-               sparam_mbox->vport = vport;
-               sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
-               rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
-               if (rc == MBX_NOT_FINISHED) {
-                       mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
-                       lpfc_mbuf_free(phba, mp->virt, mp->phys);
-                       kfree(mp);
-                       mempool_free(sparam_mbox, phba->mbox_mem_pool);
-                       goto out;
-               }
+       sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
+       if (!sparam_mbox)
+               goto out;
+
+       rc = lpfc_read_sparam(phba, sparam_mbox, 0);
+       if (rc) {
+               mempool_free(sparam_mbox, phba->mbox_mem_pool);
+               goto out;
+       }
+       sparam_mbox->vport = vport;
+       sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
+       rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
+       if (rc == MBX_NOT_FINISHED) {
+               mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
+               lpfc_mbuf_free(phba, mp->virt, mp->phys);
+               kfree(mp);
+               mempool_free(sparam_mbox, phba->mbox_mem_pool);
+               goto out;
        }
 
        if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
@@ -2049,11 +2459,20 @@ lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
                        spin_unlock_irq(&phba->hbalock);
                        return;
                }
+               /* This is the initial FCF discovery scan */
+               phba->fcf.fcf_flag |= FCF_INIT_DISC;
                spin_unlock_irq(&phba->hbalock);
-               rc = lpfc_sli4_read_fcf_record(phba,
-                                       LPFC_FCOE_FCF_GET_FIRST);
-               if (rc)
+               lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
+                               "2778 Start FCF table scan at linkup\n");
+
+               rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
+                                                    LPFC_FCOE_FCF_GET_FIRST);
+               if (rc) {
+                       spin_lock_irq(&phba->hbalock);
+                       phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
+                       spin_unlock_irq(&phba->hbalock);
                        goto out;
+               }
        }
 
        return;
@@ -2139,10 +2558,12 @@ lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
        }
 
        phba->fc_eventTag = la->eventTag;
+       spin_lock_irq(&phba->hbalock);
        if (la->mm)
                phba->sli.sli_flag |= LPFC_MENLO_MAINT;
        else
                phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
+       spin_unlock_irq(&phba->hbalock);
 
        phba->link_events++;
        if (la->attType == AT_LINK_UP && (!la->mm)) {
@@ -2271,10 +2692,10 @@ lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
                                 mb->mbxStatus);
                break;
        }
-       spin_lock_irq(&phba->hbalock);
+       spin_lock_irq(shost->host_lock);
        vport->vpi_state &= ~LPFC_VPI_REGISTERED;
        vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
-       spin_unlock_irq(&phba->hbalock);
+       spin_unlock_irq(shost->host_lock);
        vport->unreg_vpi_cmpl = VPORT_OK;
        mempool_free(pmb, phba->mbox_mem_pool);
        /*
@@ -2332,7 +2753,10 @@ lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
                goto out;
        }
 
+       spin_lock_irq(shost->host_lock);
        vport->vpi_state |= LPFC_VPI_REGISTERED;
+       vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
+       spin_unlock_irq(shost->host_lock);
        vport->num_disc_nodes = 0;
        /* go thru NPR list and issue ELS PLOGIs */
        if (vport->fc_npr_cnt)
@@ -3218,6 +3642,38 @@ lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
        return 0;
 }
 
+/**
+ * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
+ * @phba: pointer to lpfc hba data structure.
+ *
+ * This routine is invoked to unregister all the currently registered RPIs
+ * to the HBA.
+ **/
+void
+lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
+{
+       struct lpfc_vport **vports;
+       struct lpfc_nodelist *ndlp;
+       struct Scsi_Host *shost;
+       int i;
+
+       vports = lpfc_create_vport_work_array(phba);
+       for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
+               shost = lpfc_shost_from_vport(vports[i]);
+               spin_lock_irq(shost->host_lock);
+               list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
+                       if (ndlp->nlp_flag & NLP_RPI_VALID) {
+                               /* The mempool_alloc might sleep */
+                               spin_unlock_irq(shost->host_lock);
+                               lpfc_unreg_rpi(vports[i], ndlp);
+                               spin_lock_irq(shost->host_lock);
+                       }
+               }
+               spin_unlock_irq(shost->host_lock);
+       }
+       lpfc_destroy_vport_work_array(phba, vports);
+}
+
 void
 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
 {
@@ -4448,63 +4904,56 @@ lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
 }
 
 /**
- * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
+ * lpfc_unregister_fcf_prep - Unregister fcf record preparation
  * @phba: Pointer to hba context object.
  *
- * This function check if there are any connected remote port for the FCF and
- * if all the devices are disconnected, this function unregister FCFI.
- * This function also tries to use another FCF for discovery.
+ * This function prepare the HBA for unregistering the currently registered
+ * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
+ * VFIs.
  */
-void
-lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
+int
+lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
 {
        LPFC_MBOXQ_t *mbox;
-       int rc;
        struct lpfc_vport **vports;
-       int i;
-
-       spin_lock_irq(&phba->hbalock);
-       /*
-        * If HBA is not running in FIP mode or
-        * If HBA does not support FCoE or
-        * If FCF is not registered.
-        * do nothing.
-        */
-       if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
-               !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
-               (!(phba->hba_flag & HBA_FIP_SUPPORT))) {
-               spin_unlock_irq(&phba->hbalock);
-               return;
-       }
-       spin_unlock_irq(&phba->hbalock);
+       struct lpfc_nodelist *ndlp;
+       struct Scsi_Host *shost;
+       int i, rc;
 
+       /* Unregister RPIs */
        if (lpfc_fcf_inuse(phba))
-               return;
+               lpfc_unreg_hba_rpis(phba);
 
        /* At this point, all discovery is aborted */
        phba->pport->port_state = LPFC_VPORT_UNKNOWN;
 
        /* Unregister VPIs */
        vports = lpfc_create_vport_work_array(phba);
-       if (vports &&
-               (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
+       if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
                for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
+                       /* Stop FLOGI/FDISC retries */
+                       ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
+                       if (ndlp)
+                               lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
                        lpfc_mbx_unreg_vpi(vports[i]);
-                       spin_lock_irq(&phba->hbalock);
+                       shost = lpfc_shost_from_vport(vports[i]);
+                       spin_lock_irq(shost->host_lock);
                        vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
                        vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
-                       spin_unlock_irq(&phba->hbalock);
+                       spin_unlock_irq(shost->host_lock);
                }
        lpfc_destroy_vport_work_array(phba, vports);
 
+       /* Cleanup any outstanding ELS commands */
+       lpfc_els_flush_all_cmd(phba);
+
        /* Unregister VFI */
        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
        if (!mbox) {
                lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
-                       "2556 UNREG_VFI mbox allocation failed"
-                       "HBA state x%x\n",
-                       phba->pport->port_state);
-               return;
+                               "2556 UNREG_VFI mbox allocation failed"
+                               "HBA state x%x\n", phba->pport->port_state);
+               return -ENOMEM;
        }
 
        lpfc_unreg_vfi(mbox, phba->pport);
@@ -4514,58 +4963,172 @@ lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
        if (rc == MBX_NOT_FINISHED) {
                lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
-                       "2557 UNREG_VFI issue mbox failed rc x%x "
-                       "HBA state x%x\n",
-                       rc, phba->pport->port_state);
+                               "2557 UNREG_VFI issue mbox failed rc x%x "
+                               "HBA state x%x\n",
+                               rc, phba->pport->port_state);
                mempool_free(mbox, phba->mbox_mem_pool);
-               return;
+               return -EIO;
        }
 
-       /* Unregister FCF */
+       shost = lpfc_shost_from_vport(phba->pport);
+       spin_lock_irq(shost->host_lock);
+       phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
+       spin_unlock_irq(shost->host_lock);
+
+       return 0;
+}
+
+/**
+ * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
+ * @phba: Pointer to hba context object.
+ *
+ * This function issues synchronous unregister FCF mailbox command to HBA to
+ * unregister the currently registered FCF record. The driver does not reset
+ * the driver FCF usage state flags.
+ *
+ * Return 0 if successfully issued, none-zero otherwise.
+ */
+int
+lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
+{
+       LPFC_MBOXQ_t *mbox;
+       int rc;
+
        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
        if (!mbox) {
                lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
-                       "2551 UNREG_FCFI mbox allocation failed"
-                       "HBA state x%x\n",
-                       phba->pport->port_state);
-               return;
+                               "2551 UNREG_FCFI mbox allocation failed"
+                               "HBA state x%x\n", phba->pport->port_state);
+               return -ENOMEM;
        }
-
        lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
        mbox->vport = phba->pport;
        mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 
        if (rc == MBX_NOT_FINISHED) {
-               lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
-                       "2552 UNREG_FCFI issue mbox failed rc x%x "
-                       "HBA state x%x\n",
-                       rc, phba->pport->port_state);
-               mempool_free(mbox, phba->mbox_mem_pool);
+               lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
+                               "2552 Unregister FCFI command failed rc x%x "
+                               "HBA state x%x\n",
+                               rc, phba->pport->port_state);
+               return -EINVAL;
+       }
+       return 0;
+}
+
+/**
+ * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
+ * @phba: Pointer to hba context object.
+ *
+ * This function unregisters the currently reigstered FCF. This function
+ * also tries to find another FCF for discovery by rescan the HBA FCF table.
+ */
+void
+lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
+{
+       int rc;
+
+       /* Preparation for unregistering fcf */
+       rc = lpfc_unregister_fcf_prep(phba);
+       if (rc) {
+               lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
+                               "2748 Failed to prepare for unregistering "
+                               "HBA's FCF record: rc=%d\n", rc);
                return;
        }
 
-       spin_lock_irq(&phba->hbalock);
-       phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_REGISTERED |
-               FCF_DISCOVERED | FCF_BOOT_ENABLE | FCF_IN_USE |
-               FCF_VALID_VLAN);
-       spin_unlock_irq(&phba->hbalock);
+       /* Now, unregister FCF record and reset HBA FCF state */
+       rc = lpfc_sli4_unregister_fcf(phba);
+       if (rc)
+               return;
+       /* Reset HBA FCF states after successful unregister FCF */
+       phba->fcf.fcf_flag = 0;
+       phba->fcf.current_rec.flag = 0;
 
        /*
         * If driver is not unloading, check if there is any other
         * FCF record that can be used for discovery.
         */
        if ((phba->pport->load_flag & FC_UNLOADING) ||
-               (phba->link_state < LPFC_LINK_UP))
+           (phba->link_state < LPFC_LINK_UP))
                return;
 
-       rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
+       /* This is considered as the initial FCF discovery scan */
+       spin_lock_irq(&phba->hbalock);
+       phba->fcf.fcf_flag |= FCF_INIT_DISC;
+       spin_unlock_irq(&phba->hbalock);
+       rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
 
-       if (rc)
+       if (rc) {
+               spin_lock_irq(&phba->hbalock);
+               phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
+               spin_unlock_irq(&phba->hbalock);
                lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
-                       "2553 lpfc_unregister_unused_fcf failed to read FCF"
-                       " record HBA state x%x\n",
-                       phba->pport->port_state);
+                               "2553 lpfc_unregister_unused_fcf failed "
+                               "to read FCF record HBA state x%x\n",
+                               phba->pport->port_state);
+       }
+}
+
+/**
+ * lpfc_unregister_fcf - Unregister the currently registered fcf record
+ * @phba: Pointer to hba context object.
+ *
+ * This function just unregisters the currently reigstered FCF. It does not
+ * try to find another FCF for discovery.
+ */
+void
+lpfc_unregister_fcf(struct lpfc_hba *phba)
+{
+       int rc;
+
+       /* Preparation for unregistering fcf */
+       rc = lpfc_unregister_fcf_prep(phba);
+       if (rc) {
+               lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
+                               "2749 Failed to prepare for unregistering "
+                               "HBA's FCF record: rc=%d\n", rc);
+               return;
+       }
+
+       /* Now, unregister FCF record and reset HBA FCF state */
+       rc = lpfc_sli4_unregister_fcf(phba);
+       if (rc)
+               return;
+       /* Set proper HBA FCF states after successful unregister FCF */
+       spin_lock_irq(&phba->hbalock);
+       phba->fcf.fcf_flag &= ~FCF_REGISTERED;
+       spin_unlock_irq(&phba->hbalock);
+}
+
+/**
+ * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
+ * @phba: Pointer to hba context object.
+ *
+ * This function check if there are any connected remote port for the FCF and
+ * if all the devices are disconnected, this function unregister FCFI.
+ * This function also tries to use another FCF for discovery.
+ */
+void
+lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
+{
+       /*
+        * If HBA is not running in FIP mode or if HBA does not support
+        * FCoE or if FCF is not registered, do nothing.
+        */
+       spin_lock_irq(&phba->hbalock);
+       if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
+           !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
+           !(phba->hba_flag & HBA_FIP_SUPPORT)) {
+               spin_unlock_irq(&phba->hbalock);
+               return;
+       }
+       spin_unlock_irq(&phba->hbalock);
+
+       if (lpfc_fcf_inuse(phba))
+               return;
+
+       lpfc_unregister_fcf_rescan(phba);
 }
 
 /**