[SCSI] lpfc: NPIV: add NPIV support on top of SLI-3
James Smart [Mon, 18 Jun 2007 00:56:38 +0000 (19:56 -0500)]
NPIV support is added to the driver.  It utilizes the interfaces of
the fc transport for the creation and deletion of vports. Within the
driver, a new Scsi_Host is created for each NPIV instance, and is
paired with a new instance of a FC port.  This allows N FC Port
elements to share a single Adapter.

Signed-off-by: James Smart <James.Smart@emulex.com>
Signed-off-by: James Bottomley <James.Bottomley@SteelEye.com>

20 files changed:
drivers/scsi/lpfc/Makefile
drivers/scsi/lpfc/lpfc.h
drivers/scsi/lpfc/lpfc_attr.c
drivers/scsi/lpfc/lpfc_crtn.h
drivers/scsi/lpfc/lpfc_ct.c
drivers/scsi/lpfc/lpfc_disc.h
drivers/scsi/lpfc/lpfc_els.c
drivers/scsi/lpfc/lpfc_hbadisc.c
drivers/scsi/lpfc/lpfc_hw.h
drivers/scsi/lpfc/lpfc_init.c
drivers/scsi/lpfc/lpfc_logmsg.h
drivers/scsi/lpfc/lpfc_mbox.c
drivers/scsi/lpfc/lpfc_mem.c
drivers/scsi/lpfc/lpfc_nportdisc.c
drivers/scsi/lpfc/lpfc_scsi.c
drivers/scsi/lpfc/lpfc_sli.c
drivers/scsi/lpfc/lpfc_sli.h
drivers/scsi/lpfc/lpfc_version.h
drivers/scsi/lpfc/lpfc_vport.c [new file with mode: 0644]
drivers/scsi/lpfc/lpfc_vport.h [new file with mode: 0644]

index d1be465..d94c9e0 100644 (file)
@@ -1,7 +1,7 @@
 #/*******************************************************************
 # * This file is part of the Emulex Linux Device Driver for         *
 # * Fibre Channel Host Bus Adapters.                                *
-# * Copyright (C) 2004-2005 Emulex.  All rights reserved.           *
+# * Copyright (C) 2004-2006 Emulex.  All rights reserved.           *
 # * EMULEX and SLI are trademarks of Emulex.                        *
 # * www.emulex.com                                                  *
 # *                                                                 *
@@ -27,4 +27,5 @@ endif
 obj-$(CONFIG_SCSI_LPFC) := lpfc.o
 
 lpfc-objs := lpfc_mem.o lpfc_sli.o lpfc_ct.o lpfc_els.o lpfc_hbadisc.o \
-       lpfc_init.o lpfc_mbox.o lpfc_nportdisc.o lpfc_scsi.o lpfc_attr.o
+       lpfc_init.o lpfc_mbox.o lpfc_nportdisc.o lpfc_scsi.o lpfc_attr.o \
+       lpfc_vport.o
index 74f4d18..4b9019d 100644 (file)
@@ -34,6 +34,17 @@ struct lpfc_sli2_slim;
 #define LPFC_IOCB_LIST_CNT     2250    /* list of IOCBs for fast-path usage. */
 #define LPFC_Q_RAMP_UP_INTERVAL 120     /* lun q_depth ramp up interval */
 
+/*
+ * Following time intervals are used of adjusting SCSI device
+ * queue depths when there are driver resource error or Firmware
+ * resource error.
+ */
+#define QUEUE_RAMP_DOWN_INTERVAL       (1 * HZ)   /* 1 Second */
+#define QUEUE_RAMP_UP_INTERVAL         (300 * HZ) /* 5 minutes */
+
+/* Number of exchanges reserved for discovery to complete */
+#define LPFC_DISC_IOCB_BUFF_COUNT 20
+
 /* Define macros for 64 bit support */
 #define putPaddrLow(addr)    ((uint32_t) (0xffffffff & (u64)(addr)))
 #define putPaddrHigh(addr)   ((uint32_t) (0xffffffff & (((u64)(addr))>>32)))
@@ -97,6 +108,29 @@ typedef struct lpfc_vpd {
                uint32_t sli2FwRev;
                uint8_t sli2FwName[16];
        } rev;
+       struct {
+#ifdef __BIG_ENDIAN_BITFIELD
+               uint32_t rsvd2  :24;  /* Reserved                             */
+               uint32_t cmv    : 1;  /* Configure Max VPIs                   */
+               uint32_t ccrp   : 1;  /* Config Command Ring Polling          */
+               uint32_t csah   : 1;  /* Configure Synchronous Abort Handling */
+               uint32_t chbs   : 1;  /* Cofigure Host Backing store          */
+               uint32_t cinb   : 1;  /* Enable Interrupt Notification Block  */
+               uint32_t cerbm  : 1;  /* Configure Enhanced Receive Buf Mgmt  */
+               uint32_t cmx    : 1;  /* Configure Max XRIs                   */
+               uint32_t cmr    : 1;  /* Configure Max RPIs                   */
+#else  /*  __LITTLE_ENDIAN */
+               uint32_t cmr    : 1;  /* Configure Max RPIs                   */
+               uint32_t cmx    : 1;  /* Configure Max XRIs                   */
+               uint32_t cerbm  : 1;  /* Configure Enhanced Receive Buf Mgmt  */
+               uint32_t cinb   : 1;  /* Enable Interrupt Notification Block  */
+               uint32_t chbs   : 1;  /* Cofigure Host Backing store          */
+               uint32_t csah   : 1;  /* Configure Synchronous Abort Handling */
+               uint32_t ccrp   : 1;  /* Config Command Ring Polling          */
+               uint32_t cmv    : 1;  /* Configure Max VPIs                   */
+               uint32_t rsvd2  :24;  /* Reserved                             */
+#endif
+       } sli3Feat;
 } lpfc_vpd_t;
 
 struct lpfc_scsi_buf;
@@ -129,6 +163,7 @@ struct lpfc_stats {
        uint32_t elsRcvRPS;
        uint32_t elsRcvRPL;
        uint32_t elsXmitFLOGI;
+       uint32_t elsXmitFDISC;
        uint32_t elsXmitPLOGI;
        uint32_t elsXmitPRLI;
        uint32_t elsXmitADISC;
@@ -174,18 +209,21 @@ struct lpfc_sysfs_mbox {
 
 struct lpfc_hba;
 
+
 enum discovery_state {
-       LPFC_STATE_UNKNOWN   =  0,    /* HBA state is unknown */
-       LPFC_LOCAL_CFG_LINK  =  6,    /* local NPORT Id configured */
-       LPFC_FLOGI           =  7,    /* FLOGI sent to Fabric */
-       LPFC_FABRIC_CFG_LINK =  8,    /* Fabric assigned NPORT Id
-                                      * configured */
-       LPFC_NS_REG          =  9,    /* Register with NameServer */
-       LPFC_NS_QRY          =  10,   /* Query NameServer for NPort ID list */
-       LPFC_BUILD_DISC_LIST =  11,   /* Build ADISC and PLOGI lists for
-                                      * device authentication / discovery */
-       LPFC_DISC_AUTH       =  12,   /* Processing ADISC list */
-       LPFC_VPORT_READY     =  32,
+       LPFC_VPORT_UNKNOWN     =  0,    /* vport state is unknown */
+       LPFC_VPORT_FAILED      =  1,    /* vport has failed */
+       LPFC_LOCAL_CFG_LINK    =  6,    /* local NPORT Id configured */
+       LPFC_FLOGI             =  7,    /* FLOGI sent to Fabric */
+       LPFC_FDISC             =  8,    /* FDISC sent for vport */
+       LPFC_FABRIC_CFG_LINK   =  9,    /* Fabric assigned NPORT Id
+                                        * configured */
+       LPFC_NS_REG            =  10,   /* Register with NameServer */
+       LPFC_NS_QRY            =  11,   /* Query NameServer for NPort ID list */
+       LPFC_BUILD_DISC_LIST   =  12,   /* Build ADISC and PLOGI lists for
+                                        * device authentication / discovery */
+       LPFC_DISC_AUTH         =  13,   /* Processing ADISC list */
+       LPFC_VPORT_READY       =  32,
 };
 
 enum hba_state {
@@ -195,8 +233,9 @@ enum hba_state {
        LPFC_INIT_MBX_CMDS   =   3,   /* Initialize HBA with mbox commands */
        LPFC_LINK_DOWN       =   4,   /* HBA initialized, link is down */
        LPFC_LINK_UP         =   5,   /* Link is up  - issue READ_LA */
-       LPFC_CLEAR_LA        =  13,   /* authentication cmplt - issue
+       LPFC_CLEAR_LA        =   6,   /* authentication cmplt - issue
                                       * CLEAR_LA */
+       LPFC_HBA_READY       =  32,
        LPFC_HBA_ERROR       =  -1
 };
 
@@ -209,26 +248,30 @@ struct lpfc_vport {
 #define LPFC_FABRIC_PORT 3
        enum discovery_state port_state;
 
+       uint16_t vpi;
 
        uint32_t fc_flag;       /* FC flags */
 /* Several of these flags are HBA centric and should be moved to
  * phba->link_flag (e.g. FC_PTP, FC_PUBLIC_LOOP)
  */
-#define FC_PT2PT                0x1    /* pt2pt with no fabric */
-#define FC_PT2PT_PLOGI          0x2    /* pt2pt initiate PLOGI */
-#define FC_DISC_TMO             0x4    /* Discovery timer running */
-#define FC_PUBLIC_LOOP          0x8    /* Public loop */
-#define FC_LBIT                 0x10   /* LOGIN bit in loopinit set */
-#define FC_RSCN_MODE            0x20   /* RSCN cmd rcv'ed */
-#define FC_NLP_MORE             0x40   /* More node to process in node tbl */
-#define FC_OFFLINE_MODE         0x80   /* Interface is offline for diag */
-#define FC_FABRIC               0x100  /* We are fabric attached */
-#define FC_ESTABLISH_LINK       0x200  /* Reestablish Link */
-#define FC_RSCN_DISCOVERY       0x400  /* Authenticate all devices after RSCN*/
-#define FC_SCSI_SCAN_TMO        0x4000 /* scsi scan timer running */
-#define FC_ABORT_DISCOVERY      0x8000 /* we want to abort discovery */
-#define FC_NDISC_ACTIVE         0x10000        /* NPort discovery active */
-#define FC_BYPASSED_MODE        0x20000        /* NPort is in bypassed mode */
+#define FC_PT2PT                0x1     /* pt2pt with no fabric */
+#define FC_PT2PT_PLOGI          0x2     /* pt2pt initiate PLOGI */
+#define FC_DISC_TMO             0x4     /* Discovery timer running */
+#define FC_PUBLIC_LOOP          0x8     /* Public loop */
+#define FC_LBIT                 0x10    /* LOGIN bit in loopinit set */
+#define FC_RSCN_MODE            0x20    /* RSCN cmd rcv'ed */
+#define FC_NLP_MORE             0x40    /* More node to process in node tbl */
+#define FC_OFFLINE_MODE         0x80    /* Interface is offline for diag */
+#define FC_FABRIC               0x100   /* We are fabric attached */
+#define FC_ESTABLISH_LINK       0x200   /* Reestablish Link */
+#define FC_RSCN_DISCOVERY       0x400   /* Auth all devices after RSCN */
+#define FC_SCSI_SCAN_TMO        0x4000  /* scsi scan timer running */
+#define FC_ABORT_DISCOVERY      0x8000  /* we want to abort discovery */
+#define FC_NDISC_ACTIVE         0x10000         /* NPort discovery active */
+#define FC_BYPASSED_MODE        0x20000         /* NPort is in bypassed mode */
+#define FC_RFF_NOT_SUPPORTED    0x40000         /* RFF_ID was rejected by switch */
+#define FC_VPORT_NEEDS_REG_VPI 0x80000  /* Needs to have its vpi registered */
+#define FC_RSCN_DEFERRED       0x100000 /* A deferred RSCN being processed */
 
        struct list_head fc_nodes;
 
@@ -269,6 +312,9 @@ struct lpfc_vport {
 #define WORKER_ELS_TMO                 0x2     /* ELS timeout */
 #define WORKER_MBOX_TMO                0x4     /* MBOX timeout */
 #define WORKER_FDMI_TMO                0x8     /* FDMI timeout */
+#define WORKER_FABRIC_BLOCK_TMO        0x10    /* fabric block timout */
+#define WORKER_RAMP_DOWN_QUEUE    0x20 /* Decrease Q depth */
+#define WORKER_RAMP_UP_QUEUE      0x40 /* Increase Q depth */
 
        struct timer_list fc_fdmitmo;
        struct timer_list els_tmofunc;
@@ -278,10 +324,10 @@ struct lpfc_vport {
        uint8_t load_flag;
 #define FC_LOADING             0x1     /* HBA in process of loading drvr */
 #define FC_UNLOADING           0x2     /* HBA in process of unloading drvr */
-
+       char  *vname;                   /* Application assigned name */
+       struct fc_vport *fc_vport;
 };
 
-
 struct hbq_s {
        uint16_t entry_count;     /* Current number of HBQ slots */
        uint32_t next_hbqPutIdx;  /* Index to next HBQ slot to use */
@@ -289,33 +335,38 @@ struct hbq_s {
        uint32_t local_hbqGetIdx; /* Local copy of Get index from Port */
 };
 
-#define MAX_HBQS  16
+#define LPFC_MAX_HBQS  16
+/* this matches the possition in the lpfc_hbq_defs array */
+#define LPFC_ELS_HBQ   0
 
 struct lpfc_hba {
        struct lpfc_sli sli;
        uint32_t sli_rev;               /* SLI2 or SLI3 */
        uint32_t sli3_options;          /* Mask of enabled SLI3 options */
-#define LPFC_SLI3_ENABLED      0x01
-#define LPFC_SLI3_HBQ_ENABLED  0x02
-#define LPFC_SLI3_INB_ENABLED  0x04
+#define LPFC_SLI3_ENABLED       0x01
+#define LPFC_SLI3_HBQ_ENABLED   0x02
+#define LPFC_SLI3_NPIV_ENABLED  0x04
+#define LPFC_SLI3_VPORT_TEARDOWN 0x08
        uint32_t iocb_cmd_size;
        uint32_t iocb_rsp_size;
 
        enum hba_state link_state;
        uint32_t link_flag;     /* link state flags */
-#define LS_LOOPBACK_MODE        0x40000        /* NPort is in Loopback mode */
+#define LS_LOOPBACK_MODE      0x1      /* NPort is in Loopback mode */
                                        /* This flag is set while issuing */
                                        /* INIT_LINK mailbox command */
-#define LS_IGNORE_ERATT         0x80000        /* intr handler should ignore ERATT */
+#define LS_NPIV_FAB_SUPPORTED 0x2      /* Fabric supports NPIV */
+#define LS_IGNORE_ERATT       0x3      /* intr handler should ignore ERATT */
 
        struct lpfc_sli2_slim *slim2p;
        struct lpfc_dmabuf hbqslimp;
 
        dma_addr_t slim2p_mapping;
 
-
        uint16_t pci_cfg_value;
 
+       uint8_t work_found;
+#define LPFC_MAX_WORKER_ITERATION  4
 
        uint8_t fc_linkspeed;   /* Link speed after last READ_LA */
 
@@ -325,7 +376,7 @@ struct lpfc_hba {
        struct timer_list fc_estabtmo;  /* link establishment timer */
        /* These fields used to be binfo */
        uint32_t fc_pref_DID;   /* preferred D_ID */
-       uint8_t fc_pref_ALPA;   /* preferred AL_PA */
+       uint8_t  fc_pref_ALPA;  /* preferred AL_PA */
        uint32_t fc_edtov;      /* E_D_TOV timer value */
        uint32_t fc_arbtov;     /* ARB_TOV timer value */
        uint32_t fc_ratov;      /* R_A_TOV timer value */
@@ -355,6 +406,8 @@ struct lpfc_hba {
        uint32_t cfg_nodev_tmo;
        uint32_t cfg_devloss_tmo;
        uint32_t cfg_hba_queue_depth;
+       uint32_t cfg_peer_port_login;
+       uint32_t cfg_vport_restrict_login;
        uint32_t cfg_fcp_class;
        uint32_t cfg_use_adisc;
        uint32_t cfg_ack0;
@@ -391,11 +444,9 @@ struct lpfc_hba {
        wait_queue_head_t    *work_wait;
        struct task_struct   *worker_thread;
 
-       struct   hbq_dmabuf *hbq_buffer_pool;
-       uint32_t hbq_buffer_count;
-       uint32_t hbq_buff_count;        /* Current hbq buffers */
+       struct list_head hbq_buffer_list;
        uint32_t hbq_count;             /* Count of configured HBQs */
-       struct hbq_s hbqs[MAX_HBQS];    /* local copy of hbq indicies  */
+       struct hbq_s hbqs[LPFC_MAX_HBQS]; /* local copy of hbq indicies  */
 
        unsigned long pci_bar0_map;     /* Physical address for PCI BAR0 */
        unsigned long pci_bar2_map;     /* Physical address for PCI BAR2 */
@@ -413,7 +464,7 @@ struct lpfc_hba {
 
        struct lpfc_hgp __iomem *host_gp; /* Host side get/put pointers */
        uint32_t __iomem  *hbq_put;     /* Address in SLIM to HBQ put ptrs */
-       uint32_t __iomem  *hbq_get;     /* Address in SLIM to HBQ get ptrs */
+       uint32_t          *hbq_get;     /* Host mem address of HBQ get ptrs */
 
        int brd_no;                     /* FC board number */
 
@@ -464,6 +515,22 @@ struct lpfc_hba {
        struct fc_host_statistics link_stats;
        struct list_head port_list;
        struct lpfc_vport *pport; /* physical lpfc_vport pointer */
+       uint16_t max_vpi;       /* Maximum virtual nports */
+       uint16_t vpi_cnt;       /* Nport count */
+#define LPFC_MAX_VPI 100  /* Max number of VPorts supported */
+       unsigned long *vpi_bmask; /* vpi allocation table */
+
+       /* Data structure used by fabric iocb scheduler */
+       struct list_head fabric_iocb_list;
+       atomic_t fabric_iocb_count;
+       struct timer_list fabric_block_timer;
+       unsigned long bit_flags;
+#define        FABRIC_COMANDS_BLOCKED  0
+       atomic_t num_rsrc_err;
+       atomic_t num_cmd_success;
+       unsigned long last_rsrc_error_time;
+       unsigned long last_ramp_down_time;
+       unsigned long last_ramp_up_time;
 };
 
 static inline struct Scsi_Host *
@@ -485,10 +552,9 @@ static inline int
 lpfc_is_link_up(struct lpfc_hba *phba)
 {
        return  phba->link_state == LPFC_LINK_UP ||
-               phba->link_state == LPFC_CLEAR_LA;
+               phba->link_state == LPFC_CLEAR_LA ||
+               phba->link_state == LPFC_HBA_READY;
 }
 
-
-
 #define FC_REG_DUMP_EVENT      0x10    /* Register for Dump events */
 
index b8adff8..5cb7924 100644 (file)
@@ -39,6 +39,7 @@
 #include "lpfc_version.h"
 #include "lpfc_compat.h"
 #include "lpfc_crtn.h"
+#include "lpfc_vport.h"
 
 #define LPFC_DEF_DEVLOSS_TMO 30
 #define LPFC_MIN_DEVLOSS_TMO 1
@@ -139,7 +140,7 @@ lpfc_fwrev_show(struct class_device *cdev, char *buf)
        char fwrev[32];
 
        lpfc_decode_firmware_rev(phba, fwrev, 1);
-       return snprintf(buf, PAGE_SIZE, "%s\n",fwrev);
+       return snprintf(buf, PAGE_SIZE, "%s, sli-%d\n", fwrev, phba->sli_rev);
 }
 
 static ssize_t
@@ -178,10 +179,11 @@ lpfc_state_show(struct class_device *cdev, char *buf)
        case LPFC_INIT_MBX_CMDS:
        case LPFC_LINK_DOWN:
        case LPFC_HBA_ERROR:
-               len += snprintf(buf + len, PAGE_SIZE-len, "Link Down");
+               len += snprintf(buf + len, PAGE_SIZE-len, "Link Down\n");
                break;
        case LPFC_LINK_UP:
        case LPFC_CLEAR_LA:
+       case LPFC_HBA_READY:
                len += snprintf(buf + len, PAGE_SIZE-len, "Link Up - \n");
 
                switch (vport->port_state) {
@@ -190,8 +192,9 @@ lpfc_state_show(struct class_device *cdev, char *buf)
                        break;
                case LPFC_LOCAL_CFG_LINK:
                        len += snprintf(buf + len, PAGE_SIZE-len,
-                                       "configuring\n");
+                                       "Configuring Link\n");
                        break;
+               case LPFC_FDISC:
                case LPFC_FLOGI:
                case LPFC_FABRIC_CFG_LINK:
                case LPFC_NS_REG:
@@ -205,7 +208,11 @@ lpfc_state_show(struct class_device *cdev, char *buf)
                        len += snprintf(buf + len, PAGE_SIZE - len, "Ready\n");
                        break;
 
-               case LPFC_STATE_UNKNOWN:
+               case LPFC_VPORT_FAILED:
+                       len += snprintf(buf + len, PAGE_SIZE - len, "Failed\n");
+                       break;
+
+               case LPFC_VPORT_UNKNOWN:
                        len += snprintf(buf + len, PAGE_SIZE - len,
                                        "Unknown\n");
                        break;
@@ -433,6 +440,151 @@ lpfc_board_mode_store(struct class_device *cdev, const char *buf, size_t count)
 }
 
 static ssize_t
+lpfc_max_vpi_show(struct class_device *cdev, char *buf)
+{
+       struct Scsi_Host  *shost = class_to_shost(cdev);
+       struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
+       struct lpfc_hba   *phba = vport->phba;
+
+       return snprintf(buf, PAGE_SIZE, "%d\n", phba->max_vpi);
+}
+
+static ssize_t
+lpfc_used_vpi_show(struct class_device *cdev, char *buf)
+{
+       struct Scsi_Host  *shost = class_to_shost(cdev);
+       struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
+       struct lpfc_hba   *phba = vport->phba;
+
+       /* Don't count the physical port */
+       return snprintf(buf, PAGE_SIZE, "%d\n", phba->vpi_cnt-1);
+}
+
+int
+lpfc_get_hba_info(struct lpfc_hba *phba, uint32_t *mxri,
+       uint32_t *axri, uint32_t *mrpi, uint32_t *arpi)
+{
+       struct lpfc_sli   *psli = &phba->sli;
+       LPFC_MBOXQ_t *pmboxq;
+       MAILBOX_t *pmb;
+       int rc = 0;
+
+       /*
+        * prevent udev from issuing mailbox commands until the port is
+        * configured.
+        */
+       if (phba->link_state < LPFC_LINK_DOWN ||
+           !phba->mbox_mem_pool ||
+           (phba->sli.sli_flag & LPFC_SLI2_ACTIVE) == 0)
+               return 0;
+
+       if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
+               return 0;
+
+       pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
+       if (!pmboxq)
+               return 0;
+       memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
+
+       pmb = &pmboxq->mb;
+       pmb->mbxCommand = MBX_READ_CONFIG;
+       pmb->mbxOwner = OWN_HOST;
+       pmboxq->context1 = NULL;
+
+       if ((phba->pport->fc_flag & FC_OFFLINE_MODE) ||
+               (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
+               rc = MBX_NOT_FINISHED;
+       else
+               rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
+
+       if (rc != MBX_SUCCESS) {
+               if (rc == MBX_TIMEOUT)
+                       pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
+               else
+                       mempool_free(pmboxq, phba->mbox_mem_pool);
+               return 0;
+       }
+
+       if (mrpi)
+               *mrpi = pmb->un.varRdConfig.max_rpi;
+       if (arpi)
+               *arpi = pmb->un.varRdConfig.avail_rpi;
+       if (mxri)
+               *mxri = pmb->un.varRdConfig.max_xri;
+       if (axri)
+               *axri = pmb->un.varRdConfig.avail_xri;
+
+       mempool_free(pmboxq, phba->mbox_mem_pool);
+       return 1;
+}
+
+static ssize_t
+lpfc_max_rpi_show(struct class_device *cdev, char *buf)
+{
+       struct Scsi_Host  *shost = class_to_shost(cdev);
+       struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
+       struct lpfc_hba   *phba = vport->phba;
+       uint32_t cnt;
+
+       if (lpfc_get_hba_info(phba, NULL, NULL, &cnt, NULL))
+               return snprintf(buf, PAGE_SIZE, "%d\n", cnt);
+       return snprintf(buf, PAGE_SIZE, "Unknown\n");
+}
+
+static ssize_t
+lpfc_used_rpi_show(struct class_device *cdev, char *buf)
+{
+       struct Scsi_Host  *shost = class_to_shost(cdev);
+       struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
+       struct lpfc_hba   *phba = vport->phba;
+       uint32_t cnt, acnt;
+
+       if (lpfc_get_hba_info(phba, NULL, NULL, &cnt, &acnt))
+               return snprintf(buf, PAGE_SIZE, "%d\n", (cnt - acnt));
+       return snprintf(buf, PAGE_SIZE, "Unknown\n");
+}
+
+static ssize_t
+lpfc_max_xri_show(struct class_device *cdev, char *buf)
+{
+       struct Scsi_Host  *shost = class_to_shost(cdev);
+       struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
+       struct lpfc_hba   *phba = vport->phba;
+       uint32_t cnt;
+
+       if (lpfc_get_hba_info(phba, &cnt, NULL, NULL, NULL))
+               return snprintf(buf, PAGE_SIZE, "%d\n", cnt);
+       return snprintf(buf, PAGE_SIZE, "Unknown\n");
+}
+
+static ssize_t
+lpfc_used_xri_show(struct class_device *cdev, char *buf)
+{
+       struct Scsi_Host  *shost = class_to_shost(cdev);
+       struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
+       struct lpfc_hba   *phba = vport->phba;
+       uint32_t cnt, acnt;
+
+       if (lpfc_get_hba_info(phba, &cnt, &acnt, NULL, NULL))
+               return snprintf(buf, PAGE_SIZE, "%d\n", (cnt - acnt));
+       return snprintf(buf, PAGE_SIZE, "Unknown\n");
+}
+
+static ssize_t
+lpfc_npiv_info_show(struct class_device *cdev, char *buf)
+{
+       struct Scsi_Host  *shost = class_to_shost(cdev);
+       struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
+       struct lpfc_hba   *phba = vport->phba;
+
+       if (!(phba->max_vpi))
+               return snprintf(buf, PAGE_SIZE, "NPIV Not Supported\n");
+       if (vport->port_type == LPFC_PHYSICAL_PORT)
+               return snprintf(buf, PAGE_SIZE, "NPIV Physical\n");
+       return snprintf(buf, PAGE_SIZE, "NPIV Virtual (VPI %d)\n", vport->vpi);
+}
+
+static ssize_t
 lpfc_poll_show(struct class_device *cdev, char *buf)
 {
        struct Scsi_Host  *shost = class_to_shost(cdev);
@@ -640,6 +792,13 @@ static CLASS_DEVICE_ATTR(management_version, S_IRUGO, management_version_show,
 static CLASS_DEVICE_ATTR(board_mode, S_IRUGO | S_IWUSR,
                         lpfc_board_mode_show, lpfc_board_mode_store);
 static CLASS_DEVICE_ATTR(issue_reset, S_IWUSR, NULL, lpfc_issue_reset);
+static CLASS_DEVICE_ATTR(max_vpi, S_IRUGO, lpfc_max_vpi_show, NULL);
+static CLASS_DEVICE_ATTR(used_vpi, S_IRUGO, lpfc_used_vpi_show, NULL);
+static CLASS_DEVICE_ATTR(max_rpi, S_IRUGO, lpfc_max_rpi_show, NULL);
+static CLASS_DEVICE_ATTR(used_rpi, S_IRUGO, lpfc_used_rpi_show, NULL);
+static CLASS_DEVICE_ATTR(max_xri, S_IRUGO, lpfc_max_xri_show, NULL);
+static CLASS_DEVICE_ATTR(used_xri, S_IRUGO, lpfc_used_xri_show, NULL);
+static CLASS_DEVICE_ATTR(npiv_info, S_IRUGO, lpfc_npiv_info_show, NULL);
 
 
 static char *lpfc_soft_wwn_key = "C99G71SL8032A";
@@ -829,6 +988,17 @@ MODULE_PARM_DESC(lpfc_poll, "FCP ring polling mode control:"
 static CLASS_DEVICE_ATTR(lpfc_poll, S_IRUGO | S_IWUSR,
                         lpfc_poll_show, lpfc_poll_store);
 
+int  lpfc_sli_mode = 0;
+module_param(lpfc_sli_mode, int, 0);
+MODULE_PARM_DESC(lpfc_sli_mode, "SLI mode selector:"
+                " 0 - auto (SLI-3 if supported),"
+                " 2 - select SLI-2 even on SLI-3 capable HBAs,"
+                " 3 - select SLI-3");
+
+int  lpfc_npiv_enable = 0;
+module_param(lpfc_npiv_enable, int, 0);
+MODULE_PARM_DESC(lpfc_npiv_enable, "Enable NPIV functionality");
+
 /*
 # lpfc_nodev_tmo: If set, it will hold all I/O errors on devices that disappear
 # until the timer expires. Value range is [0,255]. Default value is 30.
@@ -985,6 +1155,33 @@ LPFC_ATTR_R(hba_queue_depth, 8192, 32, 8192,
            "Max number of FCP commands we can queue to a lpfc HBA");
 
 /*
+# peer_port_login:  This parameter allows/prevents logins
+# between peer ports hosted on the same physical port.
+# When this parameter is set 0 peer ports of same physical port
+# are not allowed to login to each other.
+# When this parameter is set 1 peer ports of same physical port
+# are allowed to login to each other.
+# Default value of this parameter is 0.
+*/
+LPFC_ATTR_R(peer_port_login, 0, 0, 1,
+           "Allow peer ports on the same physical port to login to each "
+           "other.");
+
+/*
+# vport_restrict_login:  This parameter allows/prevents logins
+# between Virtual Ports and remote initiators.
+# When this parameter is not set (0) Virtual Ports will accept PLOGIs from
+# other initiators and will attempt to PLOGI all remote ports.
+# When this parameter is set (1) Virtual Ports will reject PLOGIs from
+# remote ports and will not attempt to PLOGI to other initiators.
+# This parameter does not restrict to the physical port.
+# This parameter does not restrict logins to Fabric resident remote ports.
+# Default value of this parameter is 1.
+*/
+LPFC_ATTR_RW(vport_restrict_login, 1, 0, 1,
+           "Restrict virtual ports login to remote initiators.");
+
+/*
 # Some disk devices have a "select ID" or "select Target" capability.
 # From a protocol standpoint "select ID" usually means select the
 # Fibre channel "ALPA".  In the FC-AL Profile there is an "informative
@@ -1127,6 +1324,7 @@ LPFC_ATTR_RW(poll_tmo, 10, 1, 255,
 LPFC_ATTR_R(use_msi, 0, 0, 1, "Use Message Signaled Interrupts, if possible");
 
 
+
 struct class_device_attribute *lpfc_hba_attrs[] = {
        &class_device_attr_info,
        &class_device_attr_serialnum,
@@ -1143,6 +1341,8 @@ struct class_device_attribute *lpfc_hba_attrs[] = {
        &class_device_attr_lpfc_log_verbose,
        &class_device_attr_lpfc_lun_queue_depth,
        &class_device_attr_lpfc_hba_queue_depth,
+       &class_device_attr_lpfc_peer_port_login,
+       &class_device_attr_lpfc_vport_restrict_login,
        &class_device_attr_lpfc_nodev_tmo,
        &class_device_attr_lpfc_devloss_tmo,
        &class_device_attr_lpfc_fcp_class,
@@ -1161,6 +1361,13 @@ struct class_device_attribute *lpfc_hba_attrs[] = {
        &class_device_attr_nport_evt_cnt,
        &class_device_attr_management_version,
        &class_device_attr_board_mode,
+       &class_device_attr_max_vpi,
+       &class_device_attr_used_vpi,
+       &class_device_attr_max_rpi,
+       &class_device_attr_used_rpi,
+       &class_device_attr_max_xri,
+       &class_device_attr_used_xri,
+       &class_device_attr_npiv_info,
        &class_device_attr_issue_reset,
        &class_device_attr_lpfc_poll,
        &class_device_attr_lpfc_poll_tmo,
@@ -1299,7 +1506,7 @@ sysfs_mbox_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
        } else {
                if (phba->sysfs_mbox.state  != SMBOX_WRITING ||
                    phba->sysfs_mbox.offset != off           ||
-                   phba->sysfs_mbox.mbox   == NULL ) {
+                   phba->sysfs_mbox.mbox   == NULL) {
                        sysfs_mbox_idle(phba);
                        spin_unlock_irq(&phba->hbalock);
                        return -EAGAIN;
@@ -1406,6 +1613,8 @@ sysfs_mbox_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
                        return -EPERM;
                }
 
+               phba->sysfs_mbox.mbox->vport = vport;
+
                if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
                        sysfs_mbox_idle(phba);
                        spin_unlock_irq(&phba->hbalock);
@@ -1480,12 +1689,12 @@ lpfc_alloc_sysfs_attr(struct lpfc_vport *vport)
        int error;
 
        error = sysfs_create_bin_file(&shost->shost_classdev.kobj,
-                                                       &sysfs_ctlreg_attr);
+                                     &sysfs_ctlreg_attr);
        if (error)
                goto out;
 
        error = sysfs_create_bin_file(&shost->shost_classdev.kobj,
-                                                       &sysfs_mbox_attr);
+                                     &sysfs_mbox_attr);
        if (error)
                goto out_remove_ctlreg_attr;
 
@@ -1527,7 +1736,9 @@ lpfc_get_host_port_type(struct Scsi_Host *shost)
 
        spin_lock_irq(shost->host_lock);
 
-       if (lpfc_is_link_up(phba)) {
+       if (vport->port_type == LPFC_NPIV_PORT) {
+               fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
+       } else if (lpfc_is_link_up(phba)) {
                if (phba->fc_topology == TOPOLOGY_LOOP) {
                        if (vport->fc_flag & FC_PUBLIC_LOOP)
                                fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
@@ -1563,6 +1774,7 @@ lpfc_get_host_port_state(struct Scsi_Host *shost)
                        break;
                case LPFC_LINK_UP:
                case LPFC_CLEAR_LA:
+               case LPFC_HBA_READY:
                        /* Links up, beyond this port_type reports state */
                        fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
                        break;
@@ -1644,13 +1856,14 @@ lpfc_get_stats(struct Scsi_Host *shost)
        unsigned long seconds;
        int rc = 0;
 
-                               /* prevent udev from issuing mailbox commands
-                                * until the port is configured.
-                                */
+       /*
+        * prevent udev from issuing mailbox commands until the port is
+        * configured.
+        */
        if (phba->link_state < LPFC_LINK_DOWN ||
            !phba->mbox_mem_pool ||
            (phba->sli.sli_flag & LPFC_SLI2_ACTIVE) == 0)
-                       return NULL;
+               return NULL;
 
        if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
                return NULL;
@@ -1664,6 +1877,7 @@ lpfc_get_stats(struct Scsi_Host *shost)
        pmb->mbxCommand = MBX_READ_STATUS;
        pmb->mbxOwner = OWN_HOST;
        pmboxq->context1 = NULL;
+       pmboxq->vport = vport;
 
        if ((vport->fc_flag & FC_OFFLINE_MODE) ||
                (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
@@ -1690,6 +1904,7 @@ lpfc_get_stats(struct Scsi_Host *shost)
        pmb->mbxCommand = MBX_READ_LNK_STAT;
        pmb->mbxOwner = OWN_HOST;
        pmboxq->context1 = NULL;
+       pmboxq->vport = vport;
 
        if ((vport->fc_flag & FC_OFFLINE_MODE) ||
            (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
@@ -1701,7 +1916,7 @@ lpfc_get_stats(struct Scsi_Host *shost)
                if (rc == MBX_TIMEOUT)
                        pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
                else
-                       mempool_free( pmboxq, phba->mbox_mem_pool);
+                       mempool_free(pmboxq, phba->mbox_mem_pool);
                return NULL;
        }
 
@@ -1769,6 +1984,7 @@ lpfc_reset_stats(struct Scsi_Host *shost)
        pmb->mbxOwner = OWN_HOST;
        pmb->un.varWords[0] = 0x1; /* reset request */
        pmboxq->context1 = NULL;
+       pmboxq->vport = vport;
 
        if ((vport->fc_flag & FC_OFFLINE_MODE) ||
                (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
@@ -1788,6 +2004,7 @@ lpfc_reset_stats(struct Scsi_Host *shost)
        pmb->mbxCommand = MBX_READ_LNK_STAT;
        pmb->mbxOwner = OWN_HOST;
        pmboxq->context1 = NULL;
+       pmboxq->vport = vport;
 
        if ((vport->fc_flag & FC_OFFLINE_MODE) ||
            (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
@@ -1950,6 +2167,69 @@ struct fc_function_template lpfc_transport_functions = {
        .issue_fc_host_lip = lpfc_issue_lip,
        .dev_loss_tmo_callbk = lpfc_dev_loss_tmo_callbk,
        .terminate_rport_io = lpfc_terminate_rport_io,
+
+       .vport_create = lpfc_vport_create,
+       .vport_delete = lpfc_vport_delete,
+       .dd_fcvport_size = sizeof(struct lpfc_vport *),
+};
+
+struct fc_function_template lpfc_vport_transport_functions = {
+       /* fixed attributes the driver supports */
+       .show_host_node_name = 1,
+       .show_host_port_name = 1,
+       .show_host_supported_classes = 1,
+       .show_host_supported_fc4s = 1,
+       .show_host_supported_speeds = 1,
+       .show_host_maxframe_size = 1,
+
+       /* dynamic attributes the driver supports */
+       .get_host_port_id = lpfc_get_host_port_id,
+       .show_host_port_id = 1,
+
+       .get_host_port_type = lpfc_get_host_port_type,
+       .show_host_port_type = 1,
+
+       .get_host_port_state = lpfc_get_host_port_state,
+       .show_host_port_state = 1,
+
+       /* active_fc4s is shown but doesn't change (thus no get function) */
+       .show_host_active_fc4s = 1,
+
+       .get_host_speed = lpfc_get_host_speed,
+       .show_host_speed = 1,
+
+       .get_host_fabric_name = lpfc_get_host_fabric_name,
+       .show_host_fabric_name = 1,
+
+       /*
+        * The LPFC driver treats linkdown handling as target loss events
+        * so there are no sysfs handlers for link_down_tmo.
+        */
+
+       .get_fc_host_stats = lpfc_get_stats,
+       .reset_fc_host_stats = lpfc_reset_stats,
+
+       .dd_fcrport_size = sizeof(struct lpfc_rport_data),
+       .show_rport_maxframe_size = 1,
+       .show_rport_supported_classes = 1,
+
+       .set_rport_dev_loss_tmo = lpfc_set_rport_loss_tmo,
+       .show_rport_dev_loss_tmo = 1,
+
+       .get_starget_port_id  = lpfc_get_starget_port_id,
+       .show_starget_port_id = 1,
+
+       .get_starget_node_name = lpfc_get_starget_node_name,
+       .show_starget_node_name = 1,
+
+       .get_starget_port_name = lpfc_get_starget_port_name,
+       .show_starget_port_name = 1,
+
+       .issue_fc_host_lip = lpfc_issue_lip,
+       .dev_loss_tmo_callbk = lpfc_dev_loss_tmo_callbk,
+       .terminate_rport_io = lpfc_terminate_rport_io,
+
+       .vport_disable = lpfc_vport_disable,
 };
 
 void
@@ -1972,6 +2252,8 @@ lpfc_get_cfgparam(struct lpfc_hba *phba)
        lpfc_discovery_threads_init(phba, lpfc_discovery_threads);
        lpfc_max_luns_init(phba, lpfc_max_luns);
        lpfc_poll_tmo_init(phba, lpfc_poll_tmo);
+       lpfc_peer_port_login_init(phba, lpfc_peer_port_login);
+       lpfc_vport_restrict_login_init(phba, lpfc_vport_restrict_login);
        lpfc_use_msi_init(phba, lpfc_use_msi);
        lpfc_devloss_tmo_init(phba, lpfc_devloss_tmo);
        lpfc_nodev_tmo_init(phba, lpfc_nodev_tmo);
index 7769307..94e7881 100644 (file)
@@ -28,15 +28,18 @@ int lpfc_read_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb,
 void lpfc_clear_la(struct lpfc_hba *, LPFC_MBOXQ_t *);
 void lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport);
 void lpfc_config_link(struct lpfc_hba *, LPFC_MBOXQ_t *);
-int lpfc_read_sparam(struct lpfc_hba *, LPFC_MBOXQ_t *);
+int lpfc_read_sparam(struct lpfc_hba *, LPFC_MBOXQ_t *, int);
 void lpfc_read_config(struct lpfc_hba *, LPFC_MBOXQ_t *);
 void lpfc_read_lnk_stat(struct lpfc_hba *, LPFC_MBOXQ_t *);
-int lpfc_reg_login(struct lpfc_hba *, uint32_t, uint8_t *, LPFC_MBOXQ_t *,
-                  uint32_t);
-void lpfc_unreg_login(struct lpfc_hba *, uint32_t, LPFC_MBOXQ_t *);
-void lpfc_unreg_did(struct lpfc_hba *, uint32_t, LPFC_MBOXQ_t *);
+int lpfc_reg_login(struct lpfc_hba *, uint16_t, uint32_t, uint8_t *,
+                  LPFC_MBOXQ_t *, uint32_t);
+void lpfc_unreg_login(struct lpfc_hba *, uint16_t, uint32_t, LPFC_MBOXQ_t *);
+void lpfc_unreg_did(struct lpfc_hba *, uint16_t, uint32_t, LPFC_MBOXQ_t *);
+void lpfc_reg_vpi(struct lpfc_hba *, uint16_t, uint32_t, LPFC_MBOXQ_t *);
+void lpfc_unreg_vpi(struct lpfc_hba *, uint16_t, LPFC_MBOXQ_t *);
 void lpfc_init_link(struct lpfc_hba *, LPFC_MBOXQ_t *, uint32_t, uint32_t);
 
+void lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove);
 int lpfc_linkdown(struct lpfc_hba *);
 void lpfc_mbx_cmpl_read_la(struct lpfc_hba *, LPFC_MBOXQ_t *);
 
@@ -51,6 +54,10 @@ void lpfc_drop_node(struct lpfc_vport *, struct lpfc_nodelist *);
 void lpfc_set_disctmo(struct lpfc_vport *);
 int  lpfc_can_disctmo(struct lpfc_vport *);
 int  lpfc_unreg_rpi(struct lpfc_vport *, struct lpfc_nodelist *);
+void lpfc_unreg_all_rpis(struct lpfc_vport *);
+void lpfc_unreg_default_rpis(struct lpfc_vport *);
+void lpfc_issue_reg_vpi(struct lpfc_hba *, struct lpfc_vport *);
+
 int lpfc_check_sli_ndlp(struct lpfc_hba *, struct lpfc_sli_ring *,
                        struct lpfc_iocbq *, struct lpfc_nodelist *);
 void lpfc_nlp_init(struct lpfc_vport *, struct lpfc_nodelist *, uint32_t);
@@ -60,25 +67,33 @@ struct lpfc_nodelist *lpfc_setup_disc_node(struct lpfc_vport *, uint32_t);
 void lpfc_disc_list_loopmap(struct lpfc_vport *);
 void lpfc_disc_start(struct lpfc_vport *);
 void lpfc_disc_flush_list(struct lpfc_vport *);
+void lpfc_cleanup_discovery_resources(struct lpfc_vport *);
 void lpfc_disc_timeout(unsigned long);
 
 struct lpfc_nodelist *__lpfc_findnode_rpi(struct lpfc_vport *, uint16_t);
 struct lpfc_nodelist *lpfc_findnode_rpi(struct lpfc_vport *, uint16_t);
 
+void lpfc_worker_wake_up(struct lpfc_hba *);
 int lpfc_workq_post_event(struct lpfc_hba *, void *, void *, uint32_t);
 int lpfc_do_work(void *);
 int lpfc_disc_state_machine(struct lpfc_vport *, struct lpfc_nodelist *, void *,
                            uint32_t);
 
+void lpfc_register_new_vport(struct lpfc_hba *, struct lpfc_vport *,
+                       struct lpfc_nodelist *);
+void lpfc_do_scr_ns_plogi(struct lpfc_hba *, struct lpfc_vport *);
 int lpfc_check_sparm(struct lpfc_vport *, struct lpfc_nodelist *,
                     struct serv_parm *, uint32_t);
-int lpfc_els_abort(struct lpfc_hba *, struct lpfc_nodelist * ndlp);
+int lpfc_els_abort(struct lpfc_hba *, struct lpfc_nodelist *);
 int lpfc_els_abort_flogi(struct lpfc_hba *);
 int lpfc_initial_flogi(struct lpfc_vport *);
+int lpfc_initial_fdisc(struct lpfc_vport *);
+int lpfc_issue_els_fdisc(struct lpfc_vport *, struct lpfc_nodelist *, uint8_t);
 int lpfc_issue_els_plogi(struct lpfc_vport *, uint32_t, uint8_t);
 int lpfc_issue_els_prli(struct lpfc_vport *, struct lpfc_nodelist *, uint8_t);
 int lpfc_issue_els_adisc(struct lpfc_vport *, struct lpfc_nodelist *, uint8_t);
 int lpfc_issue_els_logo(struct lpfc_vport *, struct lpfc_nodelist *, uint8_t);
+int lpfc_issue_els_npiv_logo(struct lpfc_vport *, struct lpfc_nodelist *);
 int lpfc_issue_els_scr(struct lpfc_vport *, uint32_t, uint8_t);
 int lpfc_els_free_iocb(struct lpfc_hba *, struct lpfc_iocbq *);
 int lpfc_els_rsp_acc(struct lpfc_vport *, uint32_t, struct lpfc_iocbq *,
@@ -95,7 +110,7 @@ void lpfc_els_retry_delay_handler(struct lpfc_nodelist *);
 void lpfc_els_unsol_event(struct lpfc_hba *, struct lpfc_sli_ring *,
                          struct lpfc_iocbq *);
 int lpfc_els_handle_rscn(struct lpfc_vport *);
-int lpfc_els_flush_rscn(struct lpfc_vport *);
+void lpfc_els_flush_rscn(struct lpfc_vport *);
 int lpfc_rscn_payload_check(struct lpfc_vport *, uint32_t);
 void lpfc_els_flush_cmd(struct lpfc_vport *);
 int lpfc_els_disc_adisc(struct lpfc_vport *);
@@ -105,7 +120,7 @@ void lpfc_els_timeout_handler(struct lpfc_vport *);
 
 void lpfc_ct_unsol_event(struct lpfc_hba *, struct lpfc_sli_ring *,
                         struct lpfc_iocbq *);
-int lpfc_ns_cmd(struct lpfc_vport *, struct lpfc_nodelist *, int);
+int lpfc_ns_cmd(struct lpfc_vport *, int, uint8_t, uint32_t);
 int lpfc_fdmi_cmd(struct lpfc_vport *, struct lpfc_nodelist *, int);
 void lpfc_fdmi_tmo(unsigned long);
 void lpfc_fdmi_timeout_handler(struct lpfc_vport *vport);
@@ -136,6 +151,7 @@ void lpfc_config_port(struct lpfc_hba *, LPFC_MBOXQ_t *);
 void lpfc_kill_board(struct lpfc_hba *, LPFC_MBOXQ_t *);
 void lpfc_mbox_put(struct lpfc_hba *, LPFC_MBOXQ_t *);
 LPFC_MBOXQ_t *lpfc_mbox_get(struct lpfc_hba *);
+void lpfc_mbox_cmpl_put(struct lpfc_hba *, LPFC_MBOXQ_t *);
 int lpfc_mbox_tmo_val(struct lpfc_hba *, int);
 
 void lpfc_config_hbq(struct lpfc_hba *, struct lpfc_hbq_init *, uint32_t ,
@@ -144,6 +160,7 @@ struct lpfc_hbq_entry * lpfc_sli_next_hbq_slot(struct lpfc_hba *, uint32_t);
 
 int lpfc_mem_alloc(struct lpfc_hba *);
 void lpfc_mem_free(struct lpfc_hba *);
+void lpfc_stop_vport_timers(struct lpfc_vport *);
 
 void lpfc_poll_timeout(unsigned long ptr);
 void lpfc_poll_start_timer(struct lpfc_hba * phba);
@@ -176,11 +193,10 @@ int lpfc_sli_ringpostbuf_put(struct lpfc_hba *, struct lpfc_sli_ring *,
 struct lpfc_dmabuf *lpfc_sli_ringpostbuf_get(struct lpfc_hba *,
                                             struct lpfc_sli_ring *,
                                             dma_addr_t);
-int lpfc_sli_hbqbuf_fill_hbq(struct lpfc_hba *);
-void lpfc_sli_hbqbuf_free(struct lpfc_hba *, void *, dma_addr_t);
+int lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *, uint32_t);
+int lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *, uint32_t);
 void lpfc_sli_hbqbuf_free_all(struct lpfc_hba *);
 struct hbq_dmabuf *lpfc_sli_hbqbuf_find(struct lpfc_hba *, uint32_t);
-void lpfc_sli_free_hbq(struct lpfc_hba *, struct hbq_dmabuf *);
 int lpfc_sli_hbq_size(void);
 int lpfc_sli_issue_abort_iotag(struct lpfc_hba *, struct lpfc_sli_ring *,
                               struct lpfc_iocbq *);
@@ -192,12 +208,15 @@ int lpfc_sli_abort_iocb(struct lpfc_hba *, struct lpfc_sli_ring *, uint16_t,
 void lpfc_mbox_timeout(unsigned long);
 void lpfc_mbox_timeout_handler(struct lpfc_hba *);
 
+struct lpfc_nodelist *__lpfc_find_node(struct lpfc_vport *, node_filter,
+                                      void *);
+struct lpfc_nodelist *lpfc_find_node(struct lpfc_vport *, node_filter, void *);
 struct lpfc_nodelist *lpfc_findnode_did(struct lpfc_vport *, uint32_t);
 struct lpfc_nodelist *lpfc_findnode_wwpn(struct lpfc_vport *,
                                         struct lpfc_name *);
 
 int lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq,
-                        uint32_t timeout);
+                            uint32_t timeout);
 
 int lpfc_sli_issue_iocb_wait(struct lpfc_hba * phba,
                             struct lpfc_sli_ring * pring,
@@ -210,11 +229,13 @@ void lpfc_sli_abort_fcp_cmpl(struct lpfc_hba * phba,
 
 void *lpfc_hbq_alloc(struct lpfc_hba *, int, dma_addr_t *);
 void lpfc_hbq_free(struct lpfc_hba *, void *, dma_addr_t);
+void lpfc_sli_free_hbq(struct lpfc_hba *, struct hbq_dmabuf *);
 
 void *lpfc_mbuf_alloc(struct lpfc_hba *, int, dma_addr_t *);
 void __lpfc_mbuf_free(struct lpfc_hba *, void *, dma_addr_t);
 void lpfc_mbuf_free(struct lpfc_hba *, void *, dma_addr_t);
 
+void lpfc_in_buf_free(struct lpfc_hba *, struct lpfc_dmabuf *);
 /* Function prototypes. */
 const char* lpfc_info(struct Scsi_Host *);
 void lpfc_scan_start(struct Scsi_Host *);
@@ -226,14 +247,34 @@ void lpfc_free_sysfs_attr(struct lpfc_vport *);
 extern struct class_device_attribute *lpfc_hba_attrs[];
 extern struct scsi_host_template lpfc_template;
 extern struct fc_function_template lpfc_transport_functions;
+extern struct fc_function_template lpfc_vport_transport_functions;
 extern int lpfc_sli_mode;
+extern int lpfc_npiv_enable;
 
-void lpfc_get_hba_sym_node_name(struct lpfc_hba *phba, uint8_t *symbp);
+int  lpfc_vport_symbolic_node_name(struct lpfc_vport *, char *, size_t);
 void lpfc_terminate_rport_io(struct fc_rport *);
 void lpfc_dev_loss_tmo_callbk(struct fc_rport *rport);
 
-struct lpfc_vport *lpfc_create_port(struct lpfc_hba *, int);
-void lpfc_post_hba_setup_vport_init(struct lpfc_vport *);
+struct lpfc_vport *lpfc_create_port(struct lpfc_hba *, int, struct fc_vport *);
+int  lpfc_vport_disable(struct fc_vport *fc_vport, bool disable);
+void lpfc_mbx_unreg_vpi(struct lpfc_vport *);
 void destroy_port(struct lpfc_vport *);
+int lpfc_get_instance(void);
+void lpfc_host_attrib_init(struct Scsi_Host *);
+
+/* Interface exported by fabric iocb scheduler */
+int lpfc_issue_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *);
+void lpfc_fabric_abort_vport(struct lpfc_vport *);
+void lpfc_fabric_abort_nport(struct lpfc_nodelist *);
+void lpfc_fabric_abort_hba(struct lpfc_hba *);
+void lpfc_fabric_abort_flogi(struct lpfc_hba *);
+void lpfc_fabric_block_timeout(unsigned long);
+void lpfc_unblock_fabric_iocbs(struct lpfc_hba *);
+void lpfc_adjust_queue_depth(struct lpfc_hba *);
+void lpfc_ramp_down_queue_handler(struct lpfc_hba *);
+void lpfc_ramp_up_queue_handler(struct lpfc_hba *);
 
 #define ScsiResult(host_code, scsi_code) (((host_code) << 16) | scsi_code)
+#define HBA_EVENT_RSCN                   5
+#define HBA_EVENT_LINK_UP                2
+#define HBA_EVENT_LINK_DOWN              3
index e8ed5d7..5584f39 100644 (file)
@@ -40,6 +40,7 @@
 #include "lpfc_logmsg.h"
 #include "lpfc_crtn.h"
 #include "lpfc_version.h"
+#include "lpfc_vport.h"
 
 #define HBA_PORTSPEED_UNKNOWN               0  /* Unknown - transceiver
                                                 * incapable of reporting */
@@ -74,15 +75,13 @@ lpfc_ct_unsol_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
               __FUNCTION__, __LINE__,
               piocbq, mp, size,
               piocbq->iocb.ulpStatus);
+
 }
 
 static void
 lpfc_ct_ignore_hbq_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
-                         struct hbq_dmabuf *sp, uint32_t size)
+                         struct lpfc_dmabuf *mp, uint32_t size)
 {
-       struct lpfc_dmabuf *mp = NULL;
-
-       mp = sp ? &sp->dbuf : NULL;
        if (!mp) {
                printk(KERN_ERR "%s (%d): Unsolited CT, no "
                       "HBQ buffer, piocbq = %p, status = x%x\n",
@@ -102,21 +101,26 @@ void
 lpfc_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
                    struct lpfc_iocbq *piocbq)
 {
+
        struct lpfc_dmabuf *mp = NULL;
-       struct hbq_dmabuf  *sp = NULL;
        IOCB_t *icmd = &piocbq->iocb;
        int i;
        struct lpfc_iocbq *iocbq;
        dma_addr_t paddr;
        uint32_t size;
+       struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
+       struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
+
+       piocbq->context2 = NULL;
+       piocbq->context3 = NULL;
 
-       if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
-           ((icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING)) {
+       if (unlikely(icmd->ulpStatus == IOSTAT_NEED_BUFFER)) {
+               lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
+       } else if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
+               ((icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING)) {
                /* Not enough posted buffers; Try posting more buffers */
                phba->fc_stat.NoRcvBuf++;
-               if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
-                       lpfc_sli_hbqbuf_fill_hbq(phba);
-               else
+               if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
                        lpfc_post_buffer(phba, pring, 0, 1);
                return;
        }
@@ -139,23 +143,14 @@ lpfc_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
                        }
 
                        size  = icmd->un.cont64[0].tus.f.bdeSize;
-                       sp = lpfc_sli_hbqbuf_find(phba, icmd->un.ulpWord[3]);
-                       if (sp)
-                               phba->hbq_buff_count--;
-                       lpfc_ct_ignore_hbq_buffer(phba, iocbq, sp, size);
-                       lpfc_sli_free_hbq(phba, sp);
+                       lpfc_ct_ignore_hbq_buffer(phba, piocbq, bdeBuf1, size);
+                       lpfc_in_buf_free(phba, bdeBuf1);
                        if (icmd->ulpBdeCount == 2) {
-                               sp = lpfc_sli_hbqbuf_find(phba,
-                                                         icmd->un.ulpWord[15]);
-                               if (sp)
-                                       phba->hbq_buff_count--;
-                               lpfc_ct_ignore_hbq_buffer(phba, iocbq, sp,
+                               lpfc_ct_ignore_hbq_buffer(phba, piocbq, bdeBuf2,
                                                          size);
-                               lpfc_sli_free_hbq(phba, sp);
+                               lpfc_in_buf_free(phba, bdeBuf2);
                        }
-
                }
-               lpfc_sli_hbqbuf_fill_hbq(phba);
        } else {
                struct lpfc_iocbq  *next;
 
@@ -176,8 +171,7 @@ lpfc_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
                                                              paddr);
                                size = icmd->un.cont64[i].tus.f.bdeSize;
                                lpfc_ct_unsol_buffer(phba, piocbq, mp, size);
-                               lpfc_mbuf_free(phba, mp->virt, mp->phys);
-                               kfree(mp);
+                               lpfc_in_buf_free(phba, mp);
                        }
                        list_del(&iocbq->list);
                        lpfc_sli_release_iocbq(phba, iocbq);
@@ -222,7 +216,8 @@ lpfc_alloc_ct_rsp(struct lpfc_hba *phba, int cmdcode, struct ulp_bde64 *bpl,
 
                INIT_LIST_HEAD(&mp->list);
 
-               if (cmdcode == be16_to_cpu(SLI_CTNS_GID_FT))
+               if (cmdcode == be16_to_cpu(SLI_CTNS_GID_FT) ||
+                   cmdcode == be16_to_cpu(SLI_CTNS_GFF_ID))
                        mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
                else
                        mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
@@ -242,8 +237,8 @@ lpfc_alloc_ct_rsp(struct lpfc_hba *phba, int cmdcode, struct ulp_bde64 *bpl,
 
                bpl->tus.f.bdeFlags = BUFF_USE_RCV;
                /* build buffer ptr list for IOCB */
-               bpl->addrLow = le32_to_cpu( putPaddrLow(mp->phys) );
-               bpl->addrHigh = le32_to_cpu( putPaddrHigh(mp->phys) );
+               bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
+               bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
                bpl->tus.f.bdeSize = (uint16_t) cnt;
                bpl->tus.w = le32_to_cpu(bpl->tus.w);
                bpl++;
@@ -262,13 +257,14 @@ lpfc_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
             void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
                     struct lpfc_iocbq *),
             struct lpfc_nodelist *ndlp, uint32_t usr_flg, uint32_t num_entry,
-            uint32_t tmo)
+            uint32_t tmo, uint8_t retry)
 {
        struct lpfc_hba  *phba = vport->phba;
        struct lpfc_sli  *psli = &phba->sli;
        struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
        IOCB_t *icmd;
        struct lpfc_iocbq *geniocb;
+       int rc;
 
        /* Allocate buffer for  command iocb */
        geniocb = lpfc_sli_get_iocbq(phba);
@@ -311,15 +307,25 @@ lpfc_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
        icmd->ulpClass = CLASS3;
        icmd->ulpContext = ndlp->nlp_rpi;
 
+       if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
+               /* For GEN_REQUEST64_CR, use the RPI */
+               icmd->ulpCt_h = 0;
+               icmd->ulpCt_l = 0;
+       }
+
        /* Issue GEN REQ IOCB for NPORT <did> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0119 Issue GEN REQ IOCB for NPORT x%x "
-                       "Data: x%x x%x\n", phba->brd_no, icmd->un.ulpWord[5],
-                       icmd->ulpIoTag, vport->port_state);
+                       "%d (%d):0119 Issue GEN REQ IOCB to NPORT x%x "
+                       "Data: x%x x%x\n", phba->brd_no, vport->vpi,
+                       ndlp->nlp_DID, icmd->ulpIoTag,
+                       vport->port_state);
        geniocb->iocb_cmpl = cmpl;
        geniocb->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
        geniocb->vport = vport;
-       if (lpfc_sli_issue_iocb(phba, pring, geniocb, 0) == IOCB_ERROR) {
+       geniocb->retry = retry;
+       rc = lpfc_sli_issue_iocb(phba, pring, geniocb, 0);
+
+       if (rc == IOCB_ERROR) {
                lpfc_sli_release_iocbq(phba, geniocb);
                return 1;
        }
@@ -332,7 +338,7 @@ lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
            struct lpfc_dmabuf *bmp, struct lpfc_nodelist *ndlp,
            void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
                          struct lpfc_iocbq *),
-           uint32_t rsp_size)
+           uint32_t rsp_size, uint8_t retry)
 {
        struct lpfc_hba  *phba = vport->phba;
        struct ulp_bde64 *bpl = (struct ulp_bde64 *) bmp->virt;
@@ -349,7 +355,7 @@ lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
                return -ENOMEM;
 
        status = lpfc_gen_req(vport, bmp, inmp, outmp, cmpl, ndlp, 0,
-                             cnt+1, 0);
+                             cnt+1, 0, retry);
        if (status) {
                lpfc_free_ct_rsp(phba, outmp);
                return -ENOMEM;
@@ -357,10 +363,23 @@ lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
        return 0;
 }
 
+static struct lpfc_vport *
+lpfc_find_vport_by_did(struct lpfc_hba *phba, uint32_t did) {
+
+       struct lpfc_vport *vport_curr;
+
+       list_for_each_entry(vport_curr, &phba->port_list, listentry) {
+               if ((vport_curr->fc_myDID) &&
+                       (vport_curr->fc_myDID == did))
+                       return vport_curr;
+       }
+
+       return NULL;
+}
+
 static int
 lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint32_t Size)
 {
-       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
        struct lpfc_hba  *phba = vport->phba;
        struct lpfc_sli_ct_request *Response =
                (struct lpfc_sli_ct_request *) mp->virt;
@@ -372,6 +391,7 @@ lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint32_t Size)
        struct list_head head;
 
        lpfc_set_disctmo(vport);
+       vport->num_disc_nodes = 0;
 
 
        list_add_tail(&head, &mp->list);
@@ -392,25 +412,64 @@ lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint32_t Size)
                        /* Get next DID from NameServer List */
                        CTentry = *ctptr++;
                        Did = ((be32_to_cpu(CTentry)) & Mask_DID);
+
                        ndlp = NULL;
-                       /* Check for rscn processing or not */
-                       if (Did != vport->fc_myDID)
-                               ndlp = lpfc_setup_disc_node(vport, Did);
-                       if (ndlp) {
-                               lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                                               "%d:0238 Process x%x NameServer"
-                                               " Rsp Data: x%x x%x x%x\n",
-                                               phba->brd_no,
-                                               Did, ndlp->nlp_flag,
-                                               vport->fc_flag,
-                                               vport->fc_rscn_id_cnt);
-                       } else {
-                               lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                                               "%d:0239 Skip x%x NameServer "
-                                               "Rsp Data: x%x x%x x%x\n",
-                                               phba->brd_no,
-                                               Did, Size, vport->fc_flag,
-                                               vport->fc_rscn_id_cnt);
+
+                       /*
+                        * Check for rscn processing or not
+                        * To conserve rpi's, filter out addresses for other
+                        * vports on the same physical HBAs.
+                        */
+                       if ((Did != vport->fc_myDID) &&
+                           ((lpfc_find_vport_by_did(phba, Did) == NULL) ||
+                            phba->cfg_peer_port_login)) {
+                               if ((vport->port_type != LPFC_NPIV_PORT) ||
+                                   (vport->fc_flag & FC_RFF_NOT_SUPPORTED) ||
+                                   (!phba->cfg_vport_restrict_login)) {
+                                       ndlp = lpfc_setup_disc_node(vport, Did);
+                                       if (ndlp) {
+                                               lpfc_printf_log(phba, KERN_INFO,
+                                                       LOG_DISCOVERY,
+                                                       "%d (%d):0238 Process "
+                                                       "x%x NameServer Rsp"
+                                                       "Data: x%x x%x x%x\n",
+                                                       phba->brd_no,
+                                                       vport->vpi, Did,
+                                                       ndlp->nlp_flag,
+                                                       vport->fc_flag,
+                                                       vport->fc_rscn_id_cnt);
+                                       } else {
+                                               lpfc_printf_log(phba, KERN_INFO,
+                                                       LOG_DISCOVERY,
+                                                       "%d (%d):0239 Skip x%x "
+                                                       "NameServer Rsp Data: "
+                                                       "x%x x%x\n",
+                                                       phba->brd_no,
+                                                       vport->vpi, Did,
+                                                       vport->fc_flag,
+                                                       vport->fc_rscn_id_cnt);
+                                       }
+
+                               } else {
+                                       if (!(vport->fc_flag & FC_RSCN_MODE) ||
+                                       (lpfc_rscn_payload_check(vport, Did))) {
+                                               if (lpfc_ns_cmd(vport,
+                                                       SLI_CTNS_GFF_ID,
+                                                       0, Did) == 0)
+                                                       vport->num_disc_nodes++;
+                                       }
+                                       else {
+                                               lpfc_printf_log(phba, KERN_INFO,
+                                                       LOG_DISCOVERY,
+                                                       "%d (%d):0245 Skip x%x "
+                                                       "NameServer Rsp Data: "
+                                                       "x%x x%x\n",
+                                                       phba->brd_no,
+                                                       vport->vpi, Did,
+                                                       vport->fc_flag,
+                                                       vport->fc_rscn_id_cnt);
+                                       }
+                               }
                        }
                        if (CTentry & (be32_to_cpu(SLI_CT_LAST_ENTRY)))
                                goto nsout1;
@@ -422,34 +481,19 @@ lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint32_t Size)
 
 nsout1:
        list_del(&head);
-
-       /*
-        * The driver has cycled through all Nports in the RSCN payload.
-        * Complete the handling by cleaning up and marking the
-        * current driver state.
-        */
-       if (vport->port_state == LPFC_VPORT_READY) {
-               lpfc_els_flush_rscn(vport);
-               spin_lock_irq(shost->host_lock);
-               vport->fc_flag |= FC_RSCN_MODE; /* we are still in RSCN mode */
-               spin_unlock_irq(shost->host_lock);
-       }
        return 0;
 }
 
-
-
-
 static void
 lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                        struct lpfc_iocbq *rspiocb)
 {
        struct lpfc_vport *vport = cmdiocb->vport;
+       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
        IOCB_t *irsp;
        struct lpfc_dmabuf *bmp;
        struct lpfc_dmabuf *inp;
        struct lpfc_dmabuf *outp;
-       struct lpfc_nodelist *ndlp;
        struct lpfc_sli_ct_request *CTrsp;
        int rc;
 
@@ -460,33 +504,41 @@ lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
        outp = (struct lpfc_dmabuf *) cmdiocb->context2;
        bmp = (struct lpfc_dmabuf *) cmdiocb->context3;
 
+       /* Don't bother processing response if vport is being torn down. */
+       if (vport->load_flag & FC_UNLOADING)
+               goto out;
+
        irsp = &rspiocb->iocb;
        if (irsp->ulpStatus) {
                if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
                        ((irsp->un.ulpWord[4] == IOERR_SLI_DOWN) ||
                         (irsp->un.ulpWord[4] == IOERR_SLI_ABORTED)))
-                       goto out;
+                       goto err1;
 
                /* Check for retry */
                if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
                        vport->fc_ns_retry++;
                        /* CT command is being retried */
-                       ndlp = lpfc_findnode_did(vport, NameServer_DID);
-                       if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
-                               rc = lpfc_ns_cmd(vport, ndlp, SLI_CTNS_GID_FT);
-                               if (rc == 0)
-                                       goto out;
-                               }
-                       }
+                       rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
+                                        vport->fc_ns_retry, 0);
+                       if (rc == 0)
+                               goto out;
+               }
+err1:
+               lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                       "%d (%d):0257 GID_FT Query error: 0x%x 0x%x\n",
+                       phba->brd_no, vport->vpi, irsp->ulpStatus,
+                       vport->fc_ns_retry);
        } else {
                /* Good status, continue checking */
                CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
                if (CTrsp->CommandResponse.bits.CmdRsp ==
                    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
                        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                                       "%d:0208 NameServer Rsp "
+                                       "%d (%d):0208 NameServer Rsp "
                                        "Data: x%x\n",
-                                       phba->brd_no,
+                                       phba->brd_no, vport->vpi,
                                        vport->fc_flag);
                        lpfc_ns_rsp(vport, outp,
                                    (uint32_t) (irsp->un.genreq64.bdl.bdeSize));
@@ -494,21 +546,19 @@ lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                           be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
                        /* NameServer Rsp Error */
                        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                                       "%d:0240 NameServer Rsp Error "
+                                       "%d (%d):0240 NameServer Rsp Error "
                                        "Data: x%x x%x x%x x%x\n",
-                                       phba->brd_no,
+                                       phba->brd_no, vport->vpi,
                                        CTrsp->CommandResponse.bits.CmdRsp,
                                        (uint32_t) CTrsp->ReasonCode,
                                        (uint32_t) CTrsp->Explanation,
                                        vport->fc_flag);
                } else {
                        /* NameServer Rsp Error */
-                       lpfc_printf_log(phba,
-                                       KERN_INFO,
-                                       LOG_DISCOVERY,
-                                       "%d:0241 NameServer Rsp Error "
+                       lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
+                                       "%d (%d):0241 NameServer Rsp Error "
                                        "Data: x%x x%x x%x x%x\n",
-                                       phba->brd_no,
+                                       phba->brd_no, vport->vpi,
                                        CTrsp->CommandResponse.bits.CmdRsp,
                                        (uint32_t) CTrsp->ReasonCode,
                                        (uint32_t) CTrsp->Explanation,
@@ -516,8 +566,111 @@ lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                }
        }
        /* Link up / RSCN discovery */
-       lpfc_disc_start(vport);
+       if (vport->num_disc_nodes == 0) {
+               /*
+                * The driver has cycled through all Nports in the RSCN payload.
+                * Complete the handling by cleaning up and marking the
+                * current driver state.
+                */
+               if (vport->port_state >= LPFC_DISC_AUTH) {
+                       if (vport->fc_flag & FC_RSCN_MODE) {
+                               lpfc_els_flush_rscn(vport);
+                               spin_lock_irq(shost->host_lock);
+                               vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
+                               spin_unlock_irq(shost->host_lock);
+                       }
+                       else
+                               lpfc_els_flush_rscn(vport);
+               }
+
+               lpfc_disc_start(vport);
+       }
+out:
+       lpfc_free_ct_rsp(phba, outp);
+       lpfc_mbuf_free(phba, inp->virt, inp->phys);
+       lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
+       kfree(inp);
+       kfree(bmp);
+       lpfc_sli_release_iocbq(phba, cmdiocb);
+       return;
+}
+
+void
+lpfc_cmpl_ct_cmd_gff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
+                       struct lpfc_iocbq *rspiocb)
+{
+       struct lpfc_vport *vport = cmdiocb->vport;
+       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
+       IOCB_t *irsp = &rspiocb->iocb;
+       struct lpfc_dmabuf *bmp = (struct lpfc_dmabuf *) cmdiocb->context3;
+       struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *) cmdiocb->context1;
+       struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *) cmdiocb->context2;
+       struct lpfc_sli_ct_request *CTrsp;
+       int did;
+       uint8_t fbits;
+       struct lpfc_nodelist *ndlp;
+
+       did = ((struct lpfc_sli_ct_request *) inp->virt)->un.gff.PortId;
+       did = be32_to_cpu(did);
+
+       if (irsp->ulpStatus == IOSTAT_SUCCESS) {
+               /* Good status, continue checking */
+               CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
+               fbits = CTrsp->un.gff_acc.fbits[FCP_TYPE_FEATURE_OFFSET];
+
+               if (CTrsp->CommandResponse.bits.CmdRsp ==
+                   be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
+                       if ((fbits & FC4_FEATURE_INIT) &&
+                           !(fbits & FC4_FEATURE_TARGET)) {
+                               lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
+                                               "%d (%d):0245 Skip x%x GFF "
+                                               "NameServer Rsp Data: (init) "
+                                               "x%x x%x\n", phba->brd_no,
+                                               vport->vpi, did, fbits,
+                                               vport->fc_rscn_id_cnt);
+                               goto out;
+                       }
+               }
+       }
+       /* This is a target port, unregistered port, or the GFF_ID failed */
+       ndlp = lpfc_setup_disc_node(vport, did);
+       if (ndlp) {
+               lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
+                               "%d (%d):0242 Process x%x GFF "
+                               "NameServer Rsp Data: x%x x%x x%x\n",
+                               phba->brd_no, vport->vpi,
+                               did, ndlp->nlp_flag, vport->fc_flag,
+                               vport->fc_rscn_id_cnt);
+       } else {
+               lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
+                               "%d (%d):0243 Skip x%x GFF "
+                               "NameServer Rsp Data: x%x x%x\n",
+                               phba->brd_no, vport->vpi, did,
+                               vport->fc_flag, vport->fc_rscn_id_cnt);
+       }
 out:
+       /* Link up / RSCN discovery */
+       if (vport->num_disc_nodes)
+               vport->num_disc_nodes--;
+       if (vport->num_disc_nodes == 0) {
+               /*
+                * The driver has cycled through all Nports in the RSCN payload.
+                * Complete the handling by cleaning up and marking the
+                * current driver state.
+                */
+               if (vport->port_state >= LPFC_DISC_AUTH) {
+                       if (vport->fc_flag & FC_RSCN_MODE) {
+                               lpfc_els_flush_rscn(vport);
+                               spin_lock_irq(shost->host_lock);
+                               vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
+                               spin_unlock_irq(shost->host_lock);
+                       }
+                       else
+                               lpfc_els_flush_rscn(vport);
+               }
+               lpfc_disc_start(vport);
+       }
+
        lpfc_free_ct_rsp(phba, outp);
        lpfc_mbuf_free(phba, inp->virt, inp->phys);
        lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
@@ -527,15 +680,19 @@ out:
        return;
 }
 
+
 static void
 lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                        struct lpfc_iocbq *rspiocb)
 {
+       struct lpfc_vport *vport = cmdiocb->vport;
        struct lpfc_dmabuf *bmp;
        struct lpfc_dmabuf *inp;
        struct lpfc_dmabuf *outp;
        IOCB_t *irsp;
        struct lpfc_sli_ct_request *CTrsp;
+       int cmdcode, rc;
+       uint8_t retry;
 
        /* we pass cmdiocb to state machine which needs rspiocb as well */
        cmdiocb->context_un.rsp_iocb = rspiocb;
@@ -545,16 +702,40 @@ lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
        bmp = (struct lpfc_dmabuf *) cmdiocb->context3;
        irsp = &rspiocb->iocb;
 
+       cmdcode = be16_to_cpu(((struct lpfc_sli_ct_request *) inp->virt)->
+                                       CommandResponse.bits.CmdRsp);
        CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
 
-       /* RFT request completes status <ulpStatus> CmdRsp <CmdRsp> */
+       /* NS request completes status <ulpStatus> CmdRsp <CmdRsp> */
        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d:0209 RFT request completes ulpStatus x%x "
+                       "%d (%d):0209 NS request %x completes "
+                       "ulpStatus x%x / x%x "
                        "CmdRsp x%x, Context x%x, Tag x%x\n",
-                       phba->brd_no, irsp->ulpStatus,
+                       phba->brd_no, vport->vpi,
+                       cmdcode, irsp->ulpStatus, irsp->un.ulpWord[4],
                        CTrsp->CommandResponse.bits.CmdRsp,
                        cmdiocb->iocb.ulpContext, cmdiocb->iocb.ulpIoTag);
 
+       if (irsp->ulpStatus) {
+               if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
+                       ((irsp->un.ulpWord[4] == IOERR_SLI_DOWN) ||
+                        (irsp->un.ulpWord[4] == IOERR_SLI_ABORTED)))
+                       goto out;
+
+               retry = cmdiocb->retry;
+               if (retry >= LPFC_MAX_NS_RETRY)
+                       goto out;
+
+               retry++;
+               lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
+                               "%d (%d):0216 Retrying NS cmd %x\n",
+                               phba->brd_no, vport->vpi, cmdcode);
+               rc = lpfc_ns_cmd(vport, cmdcode, retry, 0);
+               if (rc == 0)
+                       goto out;
+       }
+
+out:
        lpfc_free_ct_rsp(phba, outp);
        lpfc_mbuf_free(phba, inp->virt, inp->phys);
        lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
@@ -573,6 +754,14 @@ lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 }
 
 static void
+lpfc_cmpl_ct_cmd_rspn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
+                        struct lpfc_iocbq *rspiocb)
+{
+       lpfc_cmpl_ct_cmd_rft_id(phba, cmdiocb, rspiocb);
+       return;
+}
+
+static void
 lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                         struct lpfc_iocbq *rspiocb)
 {
@@ -581,23 +770,54 @@ lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 }
 
 static void
-lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
-                        struct lpfc_iocbq * rspiocb)
+lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
+                       struct lpfc_iocbq *rspiocb)
 {
+       IOCB_t *irsp = &rspiocb->iocb;
+       struct lpfc_vport *vport = cmdiocb->vport;
+
+       if (irsp->ulpStatus != IOSTAT_SUCCESS)
+           vport->fc_flag |= FC_RFF_NOT_SUPPORTED;
+
        lpfc_cmpl_ct_cmd_rft_id(phba, cmdiocb, rspiocb);
        return;
 }
 
-void
-lpfc_get_hba_sym_node_name(struct lpfc_hba *phba, uint8_t *symbp)
+int
+lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol,
+       size_t size)
+{
+       int n;
+       uint8_t *wwn = vport->phba->wwpn;
+
+       n = snprintf(symbol, size,
+                    "Emulex PPN-%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
+                    wwn[0], wwn[1], wwn[2], wwn[3],
+                    wwn[4], wwn[5], wwn[6], wwn[7]);
+
+       if (vport->port_type == LPFC_PHYSICAL_PORT)
+               return n;
+
+       if (n < size)
+               n += snprintf(symbol + n, size - n, " VPort-%d", vport->vpi);
+
+       if (n < size && vport->vname)
+               n += snprintf(symbol + n, size - n, " VName-%s", vport->vname);
+       return n;
+}
+
+int
+lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol,
+       size_t size)
 {
        char fwrev[16];
+       int n;
 
-       lpfc_decode_firmware_rev(phba, fwrev, 0);
+       lpfc_decode_firmware_rev(vport->phba, fwrev, 0);
 
-       sprintf(symbp, "Emulex %s FV%s DV%s", phba->ModelName,
-               fwrev, lpfc_release_version);
-       return;
+       n = snprintf(symbol, size, "Emulex %s FV%s DV%s",
+               vport->phba->ModelName, fwrev, lpfc_release_version);
+       return n;
 }
 
 /*
@@ -608,8 +828,10 @@ lpfc_get_hba_sym_node_name(struct lpfc_hba *phba, uint8_t *symbp)
  *       LI_CTNS_RFT_ID
  */
 int
-lpfc_ns_cmd(struct lpfc_vport *vport, struct lpfc_nodelist * ndlp, int cmdcode)
+lpfc_ns_cmd(struct lpfc_vport *vport, int cmdcode,
+           uint8_t retry, uint32_t context)
 {
+       struct lpfc_nodelist * ndlp;
        struct lpfc_hba *phba = vport->phba;
        struct lpfc_dmabuf *mp, *bmp;
        struct lpfc_sli_ct_request *CtReq;
@@ -617,6 +839,11 @@ lpfc_ns_cmd(struct lpfc_vport *vport, struct lpfc_nodelist * ndlp, int cmdcode)
        void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
                      struct lpfc_iocbq *) = NULL;
        uint32_t rsp_size = 1024;
+       size_t   size;
+
+       ndlp = lpfc_findnode_did(vport, NameServer_DID);
+       if (ndlp == NULL || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
+               return 1;
 
        /* fill in BDEs for command */
        /* Allocate buffer for command payload */
@@ -640,24 +867,26 @@ lpfc_ns_cmd(struct lpfc_vport *vport, struct lpfc_nodelist * ndlp, int cmdcode)
                goto ns_cmd_free_bmp;
 
        /* NameServer Req */
-       lpfc_printf_log(phba,
-                       KERN_INFO,
-                       LOG_DISCOVERY,
-                       "%d:0236 NameServer Req Data: x%x x%x x%x\n",
-                       phba->brd_no, cmdcode, vport->fc_flag,
+       lpfc_printf_log(phba, KERN_INFO ,LOG_DISCOVERY,
+                       "%d (%d):0236 NameServer Req Data: x%x x%x x%x\n",
+                       phba->brd_no, vport->vpi, cmdcode, vport->fc_flag,
                        vport->fc_rscn_id_cnt);
 
        bpl = (struct ulp_bde64 *) bmp->virt;
        memset(bpl, 0, sizeof(struct ulp_bde64));
-       bpl->addrHigh = le32_to_cpu( putPaddrHigh(mp->phys) );
-       bpl->addrLow = le32_to_cpu( putPaddrLow(mp->phys) );
+       bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
+       bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
        bpl->tus.f.bdeFlags = 0;
        if (cmdcode == SLI_CTNS_GID_FT)
                bpl->tus.f.bdeSize = GID_REQUEST_SZ;
+       else if (cmdcode == SLI_CTNS_GFF_ID)
+               bpl->tus.f.bdeSize = GFF_REQUEST_SZ;
        else if (cmdcode == SLI_CTNS_RFT_ID)
                bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
        else if (cmdcode == SLI_CTNS_RNN_ID)
                bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
+       else if (cmdcode == SLI_CTNS_RSPN_ID)
+               bpl->tus.f.bdeSize = RSPN_REQUEST_SZ;
        else if (cmdcode == SLI_CTNS_RSNN_NN)
                bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
        else if (cmdcode == SLI_CTNS_RFF_ID)
@@ -678,13 +907,20 @@ lpfc_ns_cmd(struct lpfc_vport *vport, struct lpfc_nodelist * ndlp, int cmdcode)
                CtReq->CommandResponse.bits.CmdRsp =
                    be16_to_cpu(SLI_CTNS_GID_FT);
                CtReq->un.gid.Fc4Type = SLI_CTPT_FCP;
-               if (vport->port_state < LPFC_VPORT_READY)
+               if (vport->port_state < LPFC_NS_QRY)
                        vport->port_state = LPFC_NS_QRY;
                lpfc_set_disctmo(vport);
                cmpl = lpfc_cmpl_ct_cmd_gid_ft;
                rsp_size = FC_MAX_NS_RSP;
                break;
 
+       case SLI_CTNS_GFF_ID:
+               CtReq->CommandResponse.bits.CmdRsp =
+                       be16_to_cpu(SLI_CTNS_GFF_ID);
+               CtReq->un.gff.PortId = be32_to_cpu(context);
+               cmpl = lpfc_cmpl_ct_cmd_gff_id;
+               break;
+
        case SLI_CTNS_RFT_ID:
                CtReq->CommandResponse.bits.CmdRsp =
                    be16_to_cpu(SLI_CTNS_RFT_ID);
@@ -693,17 +929,6 @@ lpfc_ns_cmd(struct lpfc_vport *vport, struct lpfc_nodelist * ndlp, int cmdcode)
                cmpl = lpfc_cmpl_ct_cmd_rft_id;
                break;
 
-       case SLI_CTNS_RFF_ID:
-               CtReq->CommandResponse.bits.CmdRsp =
-                       be16_to_cpu(SLI_CTNS_RFF_ID);
-               CtReq->un.rff.PortId = be32_to_cpu(vport->fc_myDID);
-               CtReq->un.rff.feature_res = 0;
-               CtReq->un.rff.feature_tgt = 0;
-               CtReq->un.rff.type_code = FC_FCP_DATA;
-               CtReq->un.rff.feature_init = 1;
-               cmpl = lpfc_cmpl_ct_cmd_rff_id;
-               break;
-
        case SLI_CTNS_RNN_ID:
                CtReq->CommandResponse.bits.CmdRsp =
                    be16_to_cpu(SLI_CTNS_RNN_ID);
@@ -713,18 +938,39 @@ lpfc_ns_cmd(struct lpfc_vport *vport, struct lpfc_nodelist * ndlp, int cmdcode)
                cmpl = lpfc_cmpl_ct_cmd_rnn_id;
                break;
 
+       case SLI_CTNS_RSPN_ID:
+               CtReq->CommandResponse.bits.CmdRsp =
+                   be16_to_cpu(SLI_CTNS_RSPN_ID);
+               CtReq->un.rspn.PortId = be32_to_cpu(vport->fc_myDID);
+               size = sizeof(CtReq->un.rspn.symbname);
+               CtReq->un.rspn.len =
+                       lpfc_vport_symbolic_port_name(vport,
+                       CtReq->un.rspn.symbname, size);
+               cmpl = lpfc_cmpl_ct_cmd_rspn_id;
+               break;
        case SLI_CTNS_RSNN_NN:
                CtReq->CommandResponse.bits.CmdRsp =
                    be16_to_cpu(SLI_CTNS_RSNN_NN);
                memcpy(CtReq->un.rsnn.wwnn, &vport->fc_nodename,
                       sizeof (struct lpfc_name));
-               lpfc_get_hba_sym_node_name(phba, CtReq->un.rsnn.symbname);
-               CtReq->un.rsnn.len = strlen(CtReq->un.rsnn.symbname);
+               size = sizeof(CtReq->un.rsnn.symbname);
+               CtReq->un.rsnn.len =
+                       lpfc_vport_symbolic_node_name(vport,
+                       CtReq->un.rsnn.symbname, size);
                cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
                break;
+       case SLI_CTNS_RFF_ID:
+               vport->fc_flag &= ~FC_RFF_NOT_SUPPORTED;
+               CtReq->CommandResponse.bits.CmdRsp =
+                   be16_to_cpu(SLI_CTNS_RFF_ID);
+               CtReq->un.rff.PortId = be32_to_cpu(vport->fc_myDID);;
+               CtReq->un.rff.fbits = FC4_FEATURE_INIT;
+               CtReq->un.rff.type_code = FC_FCP_DATA;
+               cmpl = lpfc_cmpl_ct_cmd_rff_id;
+               break;
        }
 
-       if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size))
+       if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry))
                /* On success, The cmpl function will free the buffers */
                return 0;
 
@@ -757,8 +1003,9 @@ lpfc_cmpl_ct_cmd_fdmi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
        if (fdmi_rsp == be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
                /* FDMI rsp failed */
                lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                               "%d:0220 FDMI rsp failed Data: x%x\n",
-                               phba->brd_no, be16_to_cpu(fdmi_cmd));
+                               "%d (%d):0220 FDMI rsp failed Data: x%x\n",
+                               phba->brd_no, vport->vpi,
+                               be16_to_cpu(fdmi_cmd));
        }
 
        switch (be16_to_cpu(fdmi_cmd)) {
@@ -828,9 +1075,9 @@ lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp, int cmdcode)
 
        /* FDMI request */
        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d:0218 FDMI Request Data: x%x x%x x%x\n",
-                       phba->brd_no,
-                       vport->fc_flag, vport->port_state, cmdcode);
+                       "%d (%d):0218 FDMI Request Data: x%x x%x x%x\n",
+                       phba->brd_no, vport->vpi, vport->fc_flag,
+                       vport->port_state, cmdcode);
 
        CtReq = (struct lpfc_sli_ct_request *) mp->virt;
 
@@ -1134,15 +1381,15 @@ lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp, int cmdcode)
        }
 
        bpl = (struct ulp_bde64 *) bmp->virt;
-       bpl->addrHigh = le32_to_cpu( putPaddrHigh(mp->phys) );
-       bpl->addrLow = le32_to_cpu( putPaddrLow(mp->phys) );
+       bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
+       bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
        bpl->tus.f.bdeFlags = 0;
        bpl->tus.f.bdeSize = size;
        bpl->tus.w = le32_to_cpu(bpl->tus.w);
 
        cmpl = lpfc_cmpl_ct_cmd_fdmi;
 
-       if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, FC_MAX_NS_RSP))
+       if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, FC_MAX_NS_RSP, 0))
                return 0;
 
        lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
@@ -1155,8 +1402,8 @@ fdmi_cmd_free_mp:
 fdmi_cmd_exit:
        /* Issue FDMI request failed */
        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d:0244 Issue FDMI request failed Data: x%x\n",
-                       phba->brd_no, cmdcode);
+                       "%d (%d):0244 Issue FDMI request failed Data: x%x\n",
+                       phba->brd_no, vport->vpi, cmdcode);
        return 1;
 }
 
@@ -1170,10 +1417,15 @@ lpfc_fdmi_tmo(unsigned long ptr)
        spin_lock_irqsave(&vport->work_port_lock, iflag);
        if (!(vport->work_port_events & WORKER_FDMI_TMO)) {
                vport->work_port_events |= WORKER_FDMI_TMO;
+               spin_unlock_irqrestore(&vport->work_port_lock, iflag);
+
+               spin_lock_irqsave(&phba->hbalock, iflag);
                if (phba->work_wait)
-                       wake_up(phba->work_wait);
+                       lpfc_worker_wake_up(phba);
+               spin_unlock_irqrestore(&phba->hbalock, iflag);
        }
-       spin_unlock_irqrestore(&vport->work_port_lock, iflag);
+       else
+               spin_unlock_irqrestore(&vport->work_port_lock, iflag);
 }
 
 void
index 20bace5..f23fe1e 100644 (file)
@@ -36,13 +36,14 @@ enum lpfc_work_type {
        LPFC_EVT_WARM_START,
        LPFC_EVT_KILL,
        LPFC_EVT_ELS_RETRY,
+       LPFC_EVT_DEV_LOSS,
 };
 
 /* structure used to queue event to the discovery tasklet */
 struct lpfc_work_evt {
        struct list_head      evt_listp;
-       void                * evt_arg1;
-       void                * evt_arg2;
+       void                 *evt_arg1;
+       void                 *evt_arg2;
        enum lpfc_work_type   evt;
 };
 
@@ -73,10 +74,12 @@ struct lpfc_nodelist {
 #define NLP_FCP_2_DEVICE   0x10                        /* FCP-2 device */
 
        struct timer_list   nlp_delayfunc;      /* Used for delayed ELS cmds */
+       struct timer_list   nlp_initiator_tmr;  /* Used with dev_loss */
        struct fc_rport *rport;                 /* Corresponding FC transport
                                                   port structure */
        struct lpfc_vport *vport;
        struct lpfc_work_evt els_retry_evt;
+       struct lpfc_work_evt dev_loss_evt;
        unsigned long last_ramp_up_time;        /* jiffy of last ramp up */
        unsigned long last_q_full_time;         /* jiffy of last queue full */
        struct kref     kref;
@@ -99,6 +102,7 @@ struct lpfc_nodelist {
 #define NLP_NPR_ADISC      0x2000000   /* Issue ADISC when dq'ed from
                                           NPR list */
 #define NLP_NODEV_REMOVE   0x8000000   /* Defer removal till discovery ends */
+#define NLP_TARGET_REMOVE  0x10000000   /* Target remove in process */
 
 /* There are 4 different double linked lists nodelist entries can reside on.
  * The Port Login (PLOGI) list and Address Discovery (ADISC) list are used
index d48247b..f60c85d 100644 (file)
 #include "lpfc.h"
 #include "lpfc_logmsg.h"
 #include "lpfc_crtn.h"
+#include "lpfc_vport.h"
 
 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
                          struct lpfc_iocbq *);
+static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
+                       struct lpfc_iocbq *);
+
 static int lpfc_max_els_tries = 3;
 
 static int
@@ -58,10 +62,10 @@ lpfc_els_chk_latt(struct lpfc_vport *vport)
                return 0;
 
        /* Pending Link Event during Discovery */
-       lpfc_printf_log(phba, KERN_WARNING, LOG_DISCOVERY,
-                       "%d:0237 Pending Link Event during "
+       lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
+                       "%d (%d):0237 Pending Link Event during "
                        "Discovery: State x%x\n",
-                       phba->brd_no, phba->pport->port_state);
+                       phba->brd_no, vport->vpi,  phba->pport->port_state);
 
        /* CLEAR_LA should re-enable link attention events and
         * we should then imediately take a LATT event. The
@@ -73,12 +77,10 @@ lpfc_els_chk_latt(struct lpfc_vport *vport)
        vport->fc_flag |= FC_ABORT_DISCOVERY;
        spin_unlock_irq(shost->host_lock);
 
-       if (phba->link_state != LPFC_CLEAR_LA) {
+       if (phba->link_state != LPFC_CLEAR_LA)
                lpfc_issue_clear_la(phba, vport);
-       }
 
        return 1;
-
 }
 
 static struct lpfc_iocbq *
@@ -106,7 +108,7 @@ lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
 
        /* fill in BDEs for command */
        /* Allocate buffer for command payload */
-       if (((pcmd = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL)) == 0) ||
+       if (((pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL)) == 0) ||
            ((pcmd->virt = lpfc_mbuf_alloc(phba,
                                           MEM_PRI, &(pcmd->phys))) == 0)) {
                kfree(pcmd);
@@ -119,7 +121,7 @@ lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
 
        /* Allocate buffer for response payload */
        if (expectRsp) {
-               prsp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
+               prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
                if (prsp)
                        prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
                                                     &prsp->phys);
@@ -136,7 +138,7 @@ lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
        }
 
        /* Allocate buffer for Buffer ptr list */
-       pbuflist = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
+       pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
        if (pbuflist)
                pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
                                                 &pbuflist->phys);
@@ -157,18 +159,26 @@ lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
        icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BDL;
        icmd->un.elsreq64.remoteID = did;       /* DID */
        if (expectRsp) {
-               icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
+               icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
                icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
                icmd->ulpTimeout = phba->fc_ratov * 2;
        } else {
-               icmd->un.elsreq64.bdl.bdeSize = sizeof (struct ulp_bde64);
+               icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
                icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
        }
-
        icmd->ulpBdeCount = 1;
        icmd->ulpLe = 1;
        icmd->ulpClass = CLASS3;
 
+       if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
+               icmd->un.elsreq64.myID = vport->fc_myDID;
+
+               /* For ELS_REQUEST64_CR, use the VPI by default */
+               icmd->ulpContext = vport->vpi;
+               icmd->ulpCt_h = 0;
+               icmd->ulpCt_l = 1;
+       }
+
        bpl = (struct ulp_bde64 *) pbuflist->virt;
        bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
        bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
@@ -186,7 +196,8 @@ lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
        }
 
        /* Save for completion so we can release these resources */
-       elsiocb->context1 = lpfc_nlp_get(ndlp);
+       if (elscmd != ELS_CMD_LS_RJT)
+               elsiocb->context1 = lpfc_nlp_get(ndlp);
        elsiocb->context2 = pcmd;
        elsiocb->context3 = pbuflist;
        elsiocb->retry = retry;
@@ -200,16 +211,16 @@ lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
        if (expectRsp) {
                /* Xmit ELS command <elsCmd> to remote NPORT <did> */
                lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                               "%d:0116 Xmit ELS command x%x to remote "
+                               "%d (%d):0116 Xmit ELS command x%x to remote "
                                "NPORT x%x I/O tag: x%x, port state: x%x\n",
-                               phba->brd_no, elscmd, did,
+                               phba->brd_no, vport->vpi,  elscmd, did,
                                elsiocb->iotag, vport->port_state);
        } else {
                /* Xmit ELS response <elsCmd> to remote NPORT <did> */
                lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                               "%d:0117 Xmit ELS response x%x to remote "
+                               "%d (%d):0117 Xmit ELS response x%x to remote "
                                "NPORT x%x I/O tag: x%x, size: x%x\n",
-                               phba->brd_no, elscmd,
+                               phba->brd_no, vport->vpi, elscmd,
                                ndlp->nlp_DID, elsiocb->iotag, cmdSize);
        }
 
@@ -218,15 +229,76 @@ lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
 
 
 static int
-lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
-                          struct serv_parm *sp, IOCB_t *irsp)
+lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
 {
-       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
        struct lpfc_hba  *phba = vport->phba;
        LPFC_MBOXQ_t *mbox;
        struct lpfc_dmabuf *mp;
+       struct lpfc_nodelist *ndlp;
+       struct serv_parm *sp;
        int rc;
 
+       sp = &phba->fc_fabparam;
+       ndlp = lpfc_findnode_did(vport, Fabric_DID);
+       if (!ndlp)
+               goto fail;
+
+       mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
+       if (!mbox)
+               goto fail;
+
+       vport->port_state = LPFC_FABRIC_CFG_LINK;
+       lpfc_config_link(phba, mbox);
+       mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
+       mbox->vport = vport;
+
+       rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT | MBX_STOP_IOCB);
+       if (rc == MBX_NOT_FINISHED)
+               goto fail_free_mbox;
+
+       mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
+       if (!mbox)
+               goto fail;
+       rc = lpfc_reg_login(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
+                           0);
+       if (rc)
+               goto fail_free_mbox;
+
+       mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
+       mbox->vport = vport;
+       mbox->context2 = lpfc_nlp_get(ndlp);
+
+       rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT | MBX_STOP_IOCB);
+       if (rc == MBX_NOT_FINISHED)
+               goto fail_issue_reg_login;
+
+       return 0;
+
+fail_issue_reg_login:
+       lpfc_nlp_put(ndlp);
+       mp = (struct lpfc_dmabuf *) mbox->context1;
+       lpfc_mbuf_free(phba, mp->virt, mp->phys);
+       kfree(mp);
+fail_free_mbox:
+       mempool_free(mbox, phba->mbox_mem_pool);
+
+fail:
+       lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+       lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+               "%d (%d):0249 Cannot issue Register Fabric login\n",
+               phba->brd_no, vport->vpi);
+       return -ENXIO;
+}
+
+static int
+lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
+                          struct serv_parm *sp, IOCB_t *irsp)
+{
+       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
+       struct lpfc_hba  *phba = vport->phba;
+       struct lpfc_nodelist *np;
+       struct lpfc_nodelist *next_np;
+
        spin_lock_irq(shost->host_lock);
        vport->fc_flag |= FC_FABRIC;
        spin_unlock_irq(shost->host_lock);
@@ -251,7 +323,7 @@ lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 
        vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
        memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
-       memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
+       memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
        ndlp->nlp_class_sup = 0;
        if (sp->cls1.classValid)
                ndlp->nlp_class_sup |= FC_COS_CLASS1;
@@ -265,47 +337,59 @@ lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                                sp->cmn.bbRcvSizeLsb;
        memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
 
-       ndlp->nlp_sid = irsp->un.ulpWord[4] & Mask_DID;
-
-       mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
-       if (!mbox)
-               goto fail;
+       if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
+               if (sp->cmn.response_multiple_NPort) {
+                       lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_VPORT,
+                                       "%d:1816 FLOGI NPIV supported, "
+                                       "response data 0x%x\n",
+                                       phba->brd_no,
+                                       sp->cmn.response_multiple_NPort);
+                       phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
 
-       vport->port_state = LPFC_FABRIC_CFG_LINK;
-       lpfc_config_link(phba, mbox);
-       mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
-       mbox->vport = vport;
-
-       rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT | MBX_STOP_IOCB);
-       if (rc == MBX_NOT_FINISHED)
-               goto fail_free_mbox;
+               } else {
+                       /* Because we asked f/w for NPIV it still expects us
+                          to call reg_vnpid atleast for the physcial host */
+                       lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_VPORT,
+                                       "%d:1817 Fabric does not support NPIV "
+                                       "- configuring single port mode.\n",
+                                       phba->brd_no);
+                       phba->vpi_cnt = 1;
+                       phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
+               }
+       }
 
-       mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
-       if (!mbox)
-               goto fail;
-       rc = lpfc_reg_login(phba, Fabric_DID, (uint8_t *) sp, mbox, 0);
-       if (rc)
-               goto fail_free_mbox;
+       if ((vport->fc_prevDID != vport->fc_myDID) &&
+               !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
 
-       mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
-       mbox->vport = vport;
-       mbox->context2 = lpfc_nlp_get(ndlp);
+               /* If our NportID changed, we need to ensure all
+                * remaining NPORTs get unreg_login'ed.
+                */
+               list_for_each_entry_safe(np, next_np,
+                                       &vport->fc_nodes, nlp_listp) {
+                       if ((np->nlp_state != NLP_STE_NPR_NODE) ||
+                                  !(np->nlp_flag & NLP_NPR_ADISC))
+                               continue;
+                       spin_lock_irq(shost->host_lock);
+                       np->nlp_flag &= ~NLP_NPR_ADISC;
+                       spin_unlock_irq(shost->host_lock);
+                       lpfc_unreg_rpi(vport, np);
+               }
+               if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
+                       lpfc_mbx_unreg_vpi(vport);
+                       vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
+               }
+       }
 
-       rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT | MBX_STOP_IOCB);
-       if (rc == MBX_NOT_FINISHED)
-               goto fail_issue_reg_login;
+       ndlp->nlp_sid = irsp->un.ulpWord[4] & Mask_DID;
+       lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
 
+       if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
+           vport->fc_flag & FC_VPORT_NEEDS_REG_VPI) {
+               lpfc_register_new_vport(phba, vport, ndlp);
+               return 0;
+       }
+       lpfc_issue_fabric_reglogin(vport);
        return 0;
-
- fail_issue_reg_login:
-       lpfc_nlp_put(ndlp);
-       mp = (struct lpfc_dmabuf *) mbox->context1;
-       lpfc_mbuf_free(phba, mp->virt, mp->phys);
-       kfree(mp);
- fail_free_mbox:
-       mempool_free(mbox, phba->mbox_mem_pool);
- fail:
-       return -ENXIO;
 }
 
 /*
@@ -322,12 +406,13 @@ lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 
        spin_lock_irq(shost->host_lock);
        vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
+       phba->vpi_cnt = 1;
        spin_unlock_irq(shost->host_lock);
 
        phba->fc_edtov = FF_DEF_EDTOV;
        phba->fc_ratov = FF_DEF_RATOV;
        rc = memcmp(&vport->fc_portname, &sp->portName,
-                       sizeof(struct lpfc_name));
+                   sizeof(vport->fc_portname));
        if (rc >= 0) {
                /* This side will initiate the PLOGI */
                spin_lock_irq(shost->host_lock);
@@ -352,7 +437,7 @@ lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
                mbox->vport = vport;
                rc = lpfc_sli_issue_mbox(phba, mbox,
-                               MBX_NOWAIT | MBX_STOP_IOCB);
+                                        MBX_NOWAIT | MBX_STOP_IOCB);
                if (rc == MBX_NOT_FINISHED) {
                        mempool_free(mbox, phba->mbox_mem_pool);
                        goto fail;
@@ -392,7 +477,7 @@ lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        /* Start discovery - this should just do CLEAR_LA */
        lpfc_disc_start(vport);
        return 0;
- fail:
+fail:
        return -ENXIO;
 }
 
@@ -422,6 +507,7 @@ lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                /* FLOGI failed, so there is no fabric */
                spin_lock_irq(shost->host_lock);
                vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
+               phba->vpi_cnt = 1;
                spin_unlock_irq(shost->host_lock);
 
                /* If private loop, then allow max outstanding els to be
@@ -433,11 +519,10 @@ lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                }
 
                /* FLOGI failure */
-               lpfc_printf_log(phba,
-                               KERN_INFO,
-                               LOG_ELS,
-                               "%d:0100 FLOGI failure Data: x%x x%x x%x\n",
-                               phba->brd_no,
+               lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
+                               "%d (%d):0100 FLOGI failure Data: x%x x%x "
+                               "x%x\n",
+                               phba->brd_no, vport->vpi,
                                irsp->ulpStatus, irsp->un.ulpWord[4],
                                irsp->ulpTimeout);
                goto flogifail;
@@ -453,9 +538,9 @@ lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
        /* FLOGI completes successfully */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0101 FLOGI completes sucessfully "
+                       "%d (%d):0101 FLOGI completes sucessfully "
                        "Data: x%x x%x x%x x%x\n",
-                       phba->brd_no,
+                       phba->brd_no, vport->vpi,
                        irsp->un.ulpWord[4], sp->cmn.e_d_tov,
                        sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
 
@@ -475,6 +560,7 @@ lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
 flogifail:
        lpfc_nlp_put(ndlp);
+       phba->vpi_cnt = 1;
 
        if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
            (irsp->un.ulpWord[4] != IOERR_SLI_ABORTED &&
@@ -506,9 +592,10 @@ lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 
        pring = &phba->sli.ring[LPFC_ELS_RING];
 
-       cmdsize = (sizeof (uint32_t) + sizeof (struct serv_parm));
+       cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
                                     ndlp->nlp_DID, ELS_CMD_FLOGI);
+
        if (!elsiocb)
                return 1;
 
@@ -517,8 +604,8 @@ lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 
        /* For FLOGI request, remainder of payload is service parameters */
        *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
-       pcmd += sizeof (uint32_t);
-       memcpy(pcmd, &vport->fc_sparam, sizeof (struct serv_parm));
+       pcmd += sizeof(uint32_t);
+       memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
        sp = (struct serv_parm *) pcmd;
 
        /* Setup CSPs accordingly for Fabric */
@@ -532,6 +619,14 @@ lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        if (sp->cmn.fcphHigh < FC_PH3)
                sp->cmn.fcphHigh = FC_PH3;
 
+       if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
+               sp->cmn.request_multiple_Nport = 1;
+
+               /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
+               icmd->ulpCt_h = 1;
+               icmd->ulpCt_l = 0;
+       }
+
        tmo = phba->fc_ratov;
        phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
        lpfc_set_disctmo(vport);
@@ -539,7 +634,7 @@ lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 
        phba->fc_stat.elsXmitFLOGI++;
        elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
-       rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
+       rc = lpfc_issue_fabric_iocb(phba, elsiocb);
        if (rc == IOCB_ERROR) {
                lpfc_els_free_iocb(phba, elsiocb);
                return 1;
@@ -572,8 +667,9 @@ lpfc_els_abort_flogi(struct lpfc_hba *phba)
                if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
                    icmd->un.elsreq64.bdl.ulpIoTag32) {
                        ndlp = (struct lpfc_nodelist *)(iocb->context1);
-                       if (ndlp && (ndlp->nlp_DID == Fabric_DID))
+                       if (ndlp && (ndlp->nlp_DID == Fabric_DID)) {
                                lpfc_sli_issue_abort_iotag(phba, pring, iocb);
+                       }
                }
        }
        spin_unlock_irq(&phba->hbalock);
@@ -604,6 +700,28 @@ lpfc_initial_flogi(struct lpfc_vport *vport)
        return 1;
 }
 
+int
+lpfc_initial_fdisc(struct lpfc_vport *vport)
+{
+       struct lpfc_hba *phba = vport->phba;
+       struct lpfc_nodelist *ndlp;
+
+       /* First look for the Fabric ndlp */
+       ndlp = lpfc_findnode_did(vport, Fabric_DID);
+       if (!ndlp) {
+               /* Cannot find existing Fabric ndlp, so allocate a new one */
+               ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
+               if (!ndlp)
+                       return 0;
+               lpfc_nlp_init(vport, ndlp, Fabric_DID);
+       } else {
+               lpfc_dequeue_node(vport, ndlp);
+       }
+       if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
+               lpfc_nlp_put(ndlp);
+       }
+       return 1;
+}
 static void
 lpfc_more_plogi(struct lpfc_vport *vport)
 {
@@ -615,9 +733,9 @@ lpfc_more_plogi(struct lpfc_vport *vport)
 
        /* Continue discovery with <num_disc_nodes> PLOGIs to go */
        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d:0232 Continue discovery with %d PLOGIs to go "
+                       "%d (%d):0232 Continue discovery with %d PLOGIs to go "
                        "Data: x%x x%x x%x\n",
-                       phba->brd_no, vport->num_disc_nodes,
+                       phba->brd_no, vport->vpi, vport->num_disc_nodes,
                        vport->fc_plogi_cnt, vport->fc_flag, vport->port_state);
 
        /* Check to see if there are more PLOGIs to be sent */
@@ -629,14 +747,13 @@ lpfc_more_plogi(struct lpfc_vport *vport)
 }
 
 static struct lpfc_nodelist *
-lpfc_plogi_confirm_nport(struct lpfc_hba *phba, struct lpfc_dmabuf *prsp,
+lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
                         struct lpfc_nodelist *ndlp)
 {
        struct lpfc_vport    *vport = ndlp->vport;
        struct lpfc_nodelist *new_ndlp;
-       uint32_t *lp;
        struct serv_parm *sp;
-       uint8_t name[sizeof (struct lpfc_name)];
+       uint8_t  name[sizeof(struct lpfc_name)];
        uint32_t rc;
 
        /* Fabric nodes can have the same WWPN so we don't bother searching
@@ -645,8 +762,7 @@ lpfc_plogi_confirm_nport(struct lpfc_hba *phba, struct lpfc_dmabuf *prsp,
        if (ndlp->nlp_type & NLP_FABRIC)
                return ndlp;
 
-       lp = (uint32_t *) prsp->virt;
-       sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
+       sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
        memset(name, 0, sizeof(struct lpfc_name));
 
        /* Now we find out if the NPort we are logging into, matches the WWPN
@@ -701,8 +817,12 @@ lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
        irsp = &rspiocb->iocb;
        ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
-
        if (!ndlp) {
+               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                       "%d (%d):0136 PLOGI completes to NPort x%x "
+                       "with no ndlp. Data: x%x x%x x%x\n",
+                       phba->brd_no, vport->vpi, irsp->un.elsreq64.remoteID,
+                       irsp->ulpStatus, irsp->un.ulpWord[4], irsp->ulpIoTag);
                goto out;
        }
 
@@ -717,11 +837,11 @@ lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
        /* PLOGI completes to NPort <nlp_DID> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0102 PLOGI completes to NPort x%x "
+                       "%d (%d):0102 PLOGI completes to NPort x%x "
                        "Data: x%x x%x x%x x%x x%x\n",
-                       phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
-                       irsp->un.ulpWord[4], irsp->ulpTimeout, disc,
-                       vport->num_disc_nodes);
+                       phba->brd_no, vport->vpi, ndlp->nlp_DID,
+                       irsp->ulpStatus, irsp->un.ulpWord[4],
+                       irsp->ulpTimeout, disc, vport->num_disc_nodes);
 
        /* Check to see if link went down during discovery */
        if (lpfc_els_chk_latt(vport)) {
@@ -748,24 +868,33 @@ lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                }
 
                /* PLOGI failed */
+               if (ndlp->nlp_DID == NameServer_DID) {
+                       lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+                       lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                               "%d (%d):0250 Nameserver login error: "
+                               "0x%x / 0x%x\n",
+                               phba->brd_no, vport->vpi,
+                               irsp->ulpStatus, irsp->un.ulpWord[4]);
+               }
+
                /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
                if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
-                  ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
-                  (irsp->un.ulpWord[4] == IOERR_LINK_DOWN) ||
-                  (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
+                   ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
+                    (irsp->un.ulpWord[4] == IOERR_LINK_DOWN) ||
+                    (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
                        rc = NLP_STE_FREED_NODE;
                } else {
                        rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
-                                       NLP_EVT_CMPL_PLOGI);
+                                                    NLP_EVT_CMPL_PLOGI);
                }
        } else {
                /* Good status, call state machine */
                prsp = list_entry(((struct lpfc_dmabuf *)
-                       cmdiocb->context2)->list.next,
-                       struct lpfc_dmabuf, list);
-               ndlp = lpfc_plogi_confirm_nport(phba, prsp, ndlp);
+                                  cmdiocb->context2)->list.next,
+                                 struct lpfc_dmabuf, list);
+               ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
                rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
-                                       NLP_EVT_CMPL_PLOGI);
+                                            NLP_EVT_CMPL_PLOGI);
        }
 
        if (disc && vport->num_disc_nodes) {
@@ -811,11 +940,12 @@ lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
        struct lpfc_sli *psli;
        uint8_t *pcmd;
        uint16_t cmdsize;
+       int ret;
 
        psli = &phba->sli;
        pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
 
-       cmdsize = (sizeof (uint32_t) + sizeof (struct serv_parm));
+       cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, NULL, did,
                                     ELS_CMD_PLOGI);
        if (!elsiocb)
@@ -826,8 +956,8 @@ lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
 
        /* For PLOGI request, remainder of payload is service parameters */
        *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
-       pcmd += sizeof (uint32_t);
-       memcpy(pcmd, &vport->fc_sparam, sizeof (struct serv_parm));
+       pcmd += sizeof(uint32_t);
+       memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
        sp = (struct serv_parm *) pcmd;
 
        if (sp->cmn.fcphLow < FC_PH_4_3)
@@ -838,7 +968,9 @@ lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
 
        phba->fc_stat.elsXmitPLOGI++;
        elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
-       if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
+       ret = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
+
+       if (ret == IOCB_ERROR) {
                lpfc_els_free_iocb(phba, elsiocb);
                return 1;
        }
@@ -867,10 +999,10 @@ lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
        /* PRLI completes to NPort <nlp_DID> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0103 PRLI completes to NPort x%x "
+                       "%d (%d):0103 PRLI completes to NPort x%x "
                        "Data: x%x x%x x%x x%x\n",
-                       phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
-                       irsp->un.ulpWord[4], irsp->ulpTimeout,
+                       phba->brd_no, vport->vpi, ndlp->nlp_DID,
+                       irsp->ulpStatus, irsp->un.ulpWord[4], irsp->ulpTimeout,
                        vport->num_disc_nodes);
 
        vport->fc_prli_sent--;
@@ -887,18 +1019,18 @@ lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                /* PRLI failed */
                /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
                if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
-                  ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
-                  (irsp->un.ulpWord[4] == IOERR_LINK_DOWN) ||
-                  (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
+                   ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
+                   (irsp->un.ulpWord[4] == IOERR_LINK_DOWN) ||
+                    (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
                        goto out;
                } else {
                        lpfc_disc_state_machine(vport, ndlp, cmdiocb,
-                                       NLP_EVT_CMPL_PRLI);
+                                               NLP_EVT_CMPL_PRLI);
                }
        } else {
                /* Good status, call state machine */
                lpfc_disc_state_machine(vport, ndlp, cmdiocb,
-                                                       NLP_EVT_CMPL_PRLI);
+                                       NLP_EVT_CMPL_PRLI);
        }
 
 out:
@@ -923,7 +1055,7 @@ lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        psli = &phba->sli;
        pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
 
-       cmdsize = (sizeof (uint32_t) + sizeof (PRLI));
+       cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
                                     ndlp->nlp_DID, ELS_CMD_PRLI);
        if (!elsiocb)
@@ -933,9 +1065,9 @@ lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
 
        /* For PRLI request, remainder of payload is service parameters */
-       memset(pcmd, 0, (sizeof (PRLI) + sizeof (uint32_t)));
+       memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
        *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
-       pcmd += sizeof (uint32_t);
+       pcmd += sizeof(uint32_t);
 
        /* For PRLI, remainder of payload is PRLI parameter page */
        npr = (PRLI *) pcmd;
@@ -982,9 +1114,9 @@ lpfc_more_adisc(struct lpfc_vport *vport)
 
        /* Continue discovery with <num_disc_nodes> ADISCs to go */
        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d:0210 Continue discovery with %d ADISCs to go "
+                       "%d (%d):0210 Continue discovery with %d ADISCs to go "
                        "Data: x%x x%x x%x\n",
-                       phba->brd_no, vport->num_disc_nodes,
+                       phba->brd_no, vport->vpi, vport->num_disc_nodes,
                        vport->fc_adisc_cnt, vport->fc_flag, vport->port_state);
 
        /* Check to see if there are more ADISCs to be sent */
@@ -1048,11 +1180,11 @@ lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
        /* ADISC completes to NPort <nlp_DID> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0104 ADISC completes to NPort x%x "
+                       "%d (%d):0104 ADISC completes to NPort x%x "
                        "Data: x%x x%x x%x x%x x%x\n",
-                       phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
-                       irsp->un.ulpWord[4], irsp->ulpTimeout, disc,
-                       vport->num_disc_nodes);
+                       phba->brd_no, vport->vpi, ndlp->nlp_DID,
+                       irsp->ulpStatus, irsp->un.ulpWord[4], irsp->ulpTimeout,
+                       disc, vport->num_disc_nodes);
 
        /* Check to see if link went down during discovery */
        if (lpfc_els_chk_latt(vport)) {
@@ -1095,12 +1227,41 @@ lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
                /* Check to see if we are done with ADISC authentication */
                if (vport->num_disc_nodes == 0) {
-                       lpfc_can_disctmo(vport);
-                       /* If we get here, there is nothing left to wait for */
-                       if (vport->port_state < LPFC_VPORT_READY &&
-                           phba->link_state != LPFC_CLEAR_LA) {
+                       /* If we get here, there is nothing left to ADISC */
+                       /*
+                        * For NPIV, cmpl_reg_vpi will set port_state to READY,
+                        * and continue discovery.
+                        */
+                       if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
+                          !(vport->fc_flag & FC_RSCN_MODE)) {
+                               lpfc_issue_reg_vpi(phba, vport);
+                               goto out;
+                       }
+                       /*
+                        * For SLI2, we need to set port_state to READY
+                        * and continue discovery.
+                        */
+                       if (vport->port_state < LPFC_VPORT_READY) {
+                               /* If we get here, there is nothing to ADISC */
                                if (vport->port_type == LPFC_PHYSICAL_PORT)
                                        lpfc_issue_clear_la(phba, vport);
+
+                               if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
+                                       vport->num_disc_nodes = 0;
+                                       /* go thru NPR list, issue ELS PLOGIs */
+                                       if (vport->fc_npr_cnt)
+                                               lpfc_els_disc_plogi(vport);
+
+                                       if (!vport->num_disc_nodes) {
+                                               spin_lock_irq(shost->host_lock);
+                                               vport->fc_flag &=
+                                                       ~FC_NDISC_ACTIVE;
+                                               spin_unlock_irq(
+                                                       shost->host_lock);
+                                               lpfc_can_disctmo(vport);
+                                       }
+                               }
+                               vport->port_state = LPFC_VPORT_READY;
                        } else {
                                lpfc_rscn_disc(vport);
                        }
@@ -1125,7 +1286,7 @@ lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        uint8_t *pcmd;
        uint16_t cmdsize;
 
-       cmdsize = (sizeof (uint32_t) + sizeof (ADISC));
+       cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
                                     ndlp->nlp_DID, ELS_CMD_ADISC);
        if (!elsiocb)
@@ -1136,13 +1297,13 @@ lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 
        /* For ADISC request, remainder of payload is service parameters */
        *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
-       pcmd += sizeof (uint32_t);
+       pcmd += sizeof(uint32_t);
 
        /* Fill in ADISC payload */
        ap = (ADISC *) pcmd;
        ap->hardAL_PA = phba->fc_pref_ALPA;
-       memcpy(&ap->portName, &vport->fc_portname, sizeof (struct lpfc_name));
-       memcpy(&ap->nodeName, &vport->fc_nodename, sizeof (struct lpfc_name));
+       memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
+       memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
        ap->DID = be32_to_cpu(vport->fc_myDID);
 
        phba->fc_stat.elsXmitADISC++;
@@ -1181,16 +1342,25 @@ lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
        /* LOGO completes to NPort <nlp_DID> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0105 LOGO completes to NPort x%x "
+                       "%d (%d):0105 LOGO completes to NPort x%x "
                        "Data: x%x x%x x%x x%x\n",
-                       phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
-                       irsp->un.ulpWord[4], irsp->ulpTimeout,
+                       phba->brd_no, vport->vpi, ndlp->nlp_DID,
+                       irsp->ulpStatus, irsp->un.ulpWord[4], irsp->ulpTimeout,
                        vport->num_disc_nodes);
 
        /* Check to see if link went down during discovery */
        if (lpfc_els_chk_latt(vport))
                goto out;
 
+       if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
+               /* NLP_EVT_DEVICE_RM should unregister the RPI
+                * which should abort all outstanding IOs.
+                */
+               lpfc_disc_state_machine(vport, ndlp, cmdiocb,
+                                       NLP_EVT_DEVICE_RM);
+               goto out;
+       }
+
        if (irsp->ulpStatus) {
                /* Check for retry */
                if (lpfc_els_retry(phba, cmdiocb, rspiocb))
@@ -1199,20 +1369,20 @@ lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                /* LOGO failed */
                /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
                if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
-                  ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
-                  (irsp->un.ulpWord[4] == IOERR_LINK_DOWN) ||
-                  (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
+                   ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
+                    (irsp->un.ulpWord[4] == IOERR_LINK_DOWN) ||
+                    (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
                        goto out;
                } else {
                        lpfc_disc_state_machine(vport, ndlp, cmdiocb,
-                                       NLP_EVT_CMPL_LOGO);
+                                               NLP_EVT_CMPL_LOGO);
                }
        } else {
                /* Good status, call state machine.
                 * This will unregister the rpi if needed.
                 */
                lpfc_disc_state_machine(vport, ndlp, cmdiocb,
-                                                       NLP_EVT_CMPL_LOGO);
+                                       NLP_EVT_CMPL_LOGO);
        }
 
 out:
@@ -1232,11 +1402,12 @@ lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        struct lpfc_sli *psli;
        uint8_t *pcmd;
        uint16_t cmdsize;
+       int rc;
 
        psli = &phba->sli;
        pring = &psli->ring[LPFC_ELS_RING];
 
-       cmdsize = (2 * sizeof (uint32_t)) + sizeof (struct lpfc_name);
+       cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
        elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
                                     ndlp->nlp_DID, ELS_CMD_LOGO);
        if (!elsiocb)
@@ -1245,19 +1416,21 @@ lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        icmd = &elsiocb->iocb;
        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
        *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
-       pcmd += sizeof (uint32_t);
+       pcmd += sizeof(uint32_t);
 
        /* Fill in LOGO payload */
        *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
-       pcmd += sizeof (uint32_t);
-       memcpy(pcmd, &vport->fc_portname, sizeof (struct lpfc_name));
+       pcmd += sizeof(uint32_t);
+       memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
 
        phba->fc_stat.elsXmitLOGO++;
        elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
        spin_lock_irq(shost->host_lock);
        ndlp->nlp_flag |= NLP_LOGO_SND;
        spin_unlock_irq(shost->host_lock);
-       if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
+       rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
+
+       if (rc == IOCB_ERROR) {
                spin_lock_irq(shost->host_lock);
                ndlp->nlp_flag &= ~NLP_LOGO_SND;
                spin_unlock_irq(shost->host_lock);
@@ -1277,11 +1450,10 @@ lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
        irsp = &rspiocb->iocb;
 
        /* ELS cmd tag <ulpIoTag> completes */
-       lpfc_printf_log(phba,
-                       KERN_INFO,
-                       LOG_ELS,
-                       "%d:0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
-                       phba->brd_no,
+       lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
+                       "%d (%d):0106 ELS cmd tag x%x completes Data: x%x x%x "
+                       "x%x\n",
+                       phba->brd_no, vport->vpi,
                        irsp->ulpIoTag, irsp->ulpStatus,
                        irsp->un.ulpWord[4], irsp->ulpTimeout);
 
@@ -1305,7 +1477,7 @@ lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
 
        psli = &phba->sli;
        pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
-       cmdsize = (sizeof (uint32_t) + sizeof (SCR));
+       cmdsize = (sizeof(uint32_t) + sizeof(SCR));
        ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
        if (!ndlp)
                return 1;
@@ -1324,10 +1496,10 @@ lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
 
        *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
-       pcmd += sizeof (uint32_t);
+       pcmd += sizeof(uint32_t);
 
        /* For SCR, remainder of payload is SCR parameter page */
-       memset(pcmd, 0, sizeof (SCR));
+       memset(pcmd, 0, sizeof(SCR));
        ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
 
        phba->fc_stat.elsXmitSCR++;
@@ -1358,7 +1530,7 @@ lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
 
        psli = &phba->sli;
        pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
-       cmdsize = (sizeof (uint32_t) + sizeof (FARP));
+       cmdsize = (sizeof(uint32_t) + sizeof(FARP));
        ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
        if (!ndlp)
                return 1;
@@ -1376,25 +1548,25 @@ lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
 
        *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
-       pcmd += sizeof (uint32_t);
+       pcmd += sizeof(uint32_t);
 
        /* Fill in FARPR payload */
        fp = (FARP *) (pcmd);
-       memset(fp, 0, sizeof (FARP));
+       memset(fp, 0, sizeof(FARP));
        lp = (uint32_t *) pcmd;
        *lp++ = be32_to_cpu(nportid);
        *lp++ = be32_to_cpu(vport->fc_myDID);
        fp->Rflags = 0;
        fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
 
-       memcpy(&fp->RportName, &vport->fc_portname, sizeof (struct lpfc_name));
-       memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof (struct lpfc_name));
+       memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
+       memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
        ondlp = lpfc_findnode_did(vport, nportid);
        if (ondlp) {
                memcpy(&fp->OportName, &ondlp->nlp_portname,
-                      sizeof (struct lpfc_name));
+                      sizeof(struct lpfc_name));
                memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
-                      sizeof (struct lpfc_name));
+                      sizeof(struct lpfc_name));
        }
 
        phba->fc_stat.elsXmitFARPR++;
@@ -1470,18 +1642,17 @@ lpfc_els_retry_delay(unsigned long ptr)
 {
        struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
        struct lpfc_vport *vport = ndlp->vport;
-       struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
        struct lpfc_hba   *phba = vport->phba;
-       unsigned long iflag;
+       unsigned long flags;
        struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
 
        ndlp = (struct lpfc_nodelist *) ptr;
        phba = ndlp->vport->phba;
        evtp = &ndlp->els_retry_evt;
 
-       spin_lock_irqsave(shost->host_lock, iflag);
+       spin_lock_irqsave(&phba->hbalock, flags);
        if (!list_empty(&evtp->evt_listp)) {
-               spin_unlock_irqrestore(shost->host_lock, iflag);
+               spin_unlock_irqrestore(&phba->hbalock, flags);
                return;
        }
 
@@ -1489,9 +1660,9 @@ lpfc_els_retry_delay(unsigned long ptr)
        evtp->evt       = LPFC_EVT_ELS_RETRY;
        list_add_tail(&evtp->evt_listp, &phba->work_list);
        if (phba->work_wait)
-               wake_up(phba->work_wait);
+               lpfc_worker_wake_up(phba);
 
-       spin_unlock_irqrestore(shost->host_lock, iflag);
+       spin_unlock_irqrestore(&phba->hbalock, flags);
        return;
 }
 
@@ -1550,6 +1721,9 @@ lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
                        lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
                }
                break;
+       case ELS_CMD_FDISC:
+               lpfc_issue_els_fdisc(vport, ndlp, retry);
+               break;
        }
        return;
 }
@@ -1598,7 +1772,7 @@ lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                switch ((irsp->un.ulpWord[4] & 0xff)) {
                case IOERR_LOOP_OPEN_FAILURE:
                        if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
-                                       delay = 1;
+                               delay = 1000;
                        retry = 1;
                        break;
 
@@ -1606,9 +1780,21 @@ lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                        retry = 1;
                        break;
 
+               case IOERR_ILLEGAL_COMMAND:
+                       if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) &&
+                           (cmd == ELS_CMD_FDISC)) {
+                               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                               "%d (%d):0124 FDISC failed (3/6) retrying...\n",
+                                       phba->brd_no, vport->vpi);
+                               lpfc_mbx_unreg_vpi(vport);
+                               retry = 1;
+                               /* Always retry for this case */
+                               cmdiocb->retry = 0;
+                       }
+                       break;
+
                case IOERR_NO_RESOURCES:
-                       if (cmd == ELS_CMD_PLOGI)
-                               delay = 1;
+                       delay = 100;
                        retry = 1;
                        break;
 
@@ -1641,27 +1827,56 @@ lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                        if (stat.un.b.lsRjtRsnCodeExp ==
                            LSEXP_CMD_IN_PROGRESS) {
                                if (cmd == ELS_CMD_PLOGI) {
-                                       delay = 1;
+                                       delay = 1000;
                                        maxretry = 48;
                                }
                                retry = 1;
                                break;
                        }
                        if (cmd == ELS_CMD_PLOGI) {
-                               delay = 1;
+                               delay = 1000;
                                maxretry = lpfc_max_els_tries + 1;
                                retry = 1;
                                break;
                        }
+                       if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
+                         (cmd == ELS_CMD_FDISC) &&
+                         (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
+                               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                               "%d (%d):0125 FDISC Failed (x%x)."
+                               " Fabric out of resources\n",
+                               phba->brd_no, vport->vpi, stat.un.lsRjtError);
+                               lpfc_vport_set_state(vport,
+                                                    FC_VPORT_NO_FABRIC_RSCS);
+                       }
                        break;
 
                case LSRJT_LOGICAL_BSY:
                        if (cmd == ELS_CMD_PLOGI) {
-                               delay = 1;
+                               delay = 1000;
                                maxretry = 48;
+                       } else if (cmd == ELS_CMD_FDISC) {
+                               /* Always retry for this case */
+                               cmdiocb->retry = 0;
                        }
                        retry = 1;
                        break;
+
+               case LSRJT_LOGICAL_ERR:
+               case LSRJT_PROTOCOL_ERR:
+                       if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
+                         (cmd == ELS_CMD_FDISC) &&
+                         ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
+                         (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
+                         ) {
+                               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                               "%d (%d):0123 FDISC Failed (x%x)."
+                               " Fabric Detected Bad WWN\n",
+                               phba->brd_no, vport->vpi, stat.un.lsRjtError);
+                               lpfc_vport_set_state(vport,
+                                                    FC_VPORT_FABRIC_REJ_WWN);
+                       }
+                       break;
                }
                break;
 
@@ -1688,15 +1903,15 @@ lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
                /* Retry ELS command <elsCmd> to remote NPORT <did> */
                lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                               "%d:0107 Retry ELS command x%x to remote "
+                               "%d (%d):0107 Retry ELS command x%x to remote "
                                "NPORT x%x Data: x%x x%x\n",
-                               phba->brd_no,
+                               phba->brd_no, vport->vpi,
                                cmd, did, cmdiocb->retry, delay);
 
                if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) {
                        /* If discovery / RSCN timer is running, reset it */
                        if (timer_pending(&vport->fc_disctmo) ||
-                             (vport->fc_flag & FC_RSCN_MODE))
+                           (vport->fc_flag & FC_RSCN_MODE))
                                lpfc_set_disctmo(vport);
                }
 
@@ -1705,7 +1920,9 @@ lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                        phba->fc_stat.elsDelayRetry++;
                        ndlp->nlp_retry = cmdiocb->retry;
 
-                       mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
+                       /* delay is specified in milliseconds */
+                       mod_timer(&ndlp->nlp_delayfunc,
+                               jiffies + msecs_to_jiffies(delay));
                        spin_lock_irq(shost->host_lock);
                        ndlp->nlp_flag |= NLP_DELAY_TMO;
                        spin_unlock_irq(shost->host_lock);
@@ -1720,6 +1937,9 @@ lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
                case ELS_CMD_FLOGI:
                        lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
                        return 1;
+               case ELS_CMD_FDISC:
+                       lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
+                       return 1;
                case ELS_CMD_PLOGI:
                        if (ndlp) {
                                ndlp->nlp_prev_state = ndlp->nlp_state;
@@ -1748,9 +1968,9 @@ lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
        /* No retry ELS command <elsCmd> to remote NPORT <did> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0108 No retry ELS command x%x to remote NPORT x%x "
-                       "Data: x%x\n",
-                       phba->brd_no,
+                       "%d (%d):0108 No retry ELS command x%x to remote "
+                       "NPORT x%x Data: x%x\n",
+                       phba->brd_no, vport->vpi,
                        cmd, did, cmdiocb->retry);
 
        return 0;
@@ -1798,10 +2018,10 @@ lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
        /* ACC to LOGO completes to NPort <nlp_DID> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0109 ACC to LOGO completes to NPort x%x "
+                       "%d (%d):0109 ACC to LOGO completes to NPort x%x "
                        "Data: x%x x%x x%x\n",
-                       phba->brd_no, ndlp->nlp_DID, ndlp->nlp_flag,
-                       ndlp->nlp_state, ndlp->nlp_rpi);
+                       phba->brd_no, vport->vpi, ndlp->nlp_DID,
+                       ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
 
        switch (ndlp->nlp_state) {
        case NLP_STE_UNUSED_NODE:       /* node is just allocated */
@@ -1848,9 +2068,9 @@ lpfc_cmpl_els_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
        /* ELS response tag <ulpIoTag> completes */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0110 ELS response tag x%x completes "
+                       "%d (%d):0110 ELS response tag x%x completes "
                        "Data: x%x x%x x%x x%x x%x x%x x%x\n",
-                       phba->brd_no,
+                       phba->brd_no, vport->vpi,
                        cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
                        rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
                        ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
@@ -1926,7 +2146,7 @@ lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
 
        switch (flag) {
        case ELS_CMD_ACC:
-               cmdsize = sizeof (uint32_t);
+               cmdsize = sizeof(uint32_t);
                elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
                                             ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
                if (!elsiocb) {
@@ -1940,10 +2160,10 @@ lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
                icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
                pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
                *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
-               pcmd += sizeof (uint32_t);
+               pcmd += sizeof(uint32_t);
                break;
        case ELS_CMD_PLOGI:
-               cmdsize = (sizeof (struct serv_parm) + sizeof (uint32_t));
+               cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
                elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
                                             ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
                if (!elsiocb)
@@ -1957,11 +2177,11 @@ lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
                        elsiocb->context_un.mbox = mbox;
 
                *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
-               pcmd += sizeof (uint32_t);
-               memcpy(pcmd, &vport->fc_sparam, sizeof (struct serv_parm));
+               pcmd += sizeof(uint32_t);
+               memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
                break;
        case ELS_CMD_PRLO:
-               cmdsize = sizeof (uint32_t) + sizeof (PRLO);
+               cmdsize = sizeof(uint32_t) + sizeof(PRLO);
                elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
                                             ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
                if (!elsiocb)
@@ -1972,7 +2192,7 @@ lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
                pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
 
                memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
-                      sizeof (uint32_t) + sizeof (PRLO));
+                      sizeof(uint32_t) + sizeof(PRLO));
                *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
                els_pkt_ptr = (ELS_PKT *) pcmd;
                els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
@@ -1988,9 +2208,9 @@ lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
 
        /* Xmit ELS ACC response tag <ulpIoTag> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
+                       "%d (%d):0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
                        "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
-                       phba->brd_no, elsiocb->iotag,
+                       phba->brd_no, vport->vpi, elsiocb->iotag,
                        elsiocb->iocb.ulpContext, ndlp->nlp_DID,
                        ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
 
@@ -2029,7 +2249,7 @@ lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
        psli = &phba->sli;
        pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
 
-       cmdsize = 2 * sizeof (uint32_t);
+       cmdsize = 2 * sizeof(uint32_t);
        elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
                                     ndlp->nlp_DID, ELS_CMD_LS_RJT);
        if (!elsiocb)
@@ -2041,14 +2261,15 @@ lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
 
        *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
-       pcmd += sizeof (uint32_t);
+       pcmd += sizeof(uint32_t);
        *((uint32_t *) (pcmd)) = rejectError;
 
        /* Xmit ELS RJT <err> response tag <ulpIoTag> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0129 Xmit ELS RJT x%x response tag x%x xri x%x, "
-                       "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
-                       phba->brd_no, rejectError, elsiocb->iotag,
+                       "%d (%d):0129 Xmit ELS RJT x%x response tag x%x "
+                       "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
+                       "rpi x%x\n",
+                       phba->brd_no, vport->vpi, rejectError, elsiocb->iotag,
                        elsiocb->iocb.ulpContext, ndlp->nlp_DID,
                        ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
 
@@ -2076,7 +2297,7 @@ lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
        uint16_t cmdsize;
        int rc;
 
-       cmdsize = sizeof (uint32_t) + sizeof (ADISC);
+       cmdsize = sizeof(uint32_t) + sizeof(ADISC);
        elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
                                     ndlp->nlp_DID, ELS_CMD_ACC);
        if (!elsiocb)
@@ -2088,21 +2309,21 @@ lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
 
        /* Xmit ADISC ACC response tag <ulpIoTag> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0130 Xmit ADISC ACC response iotag x%x xri: "
+                       "%d (%d):0130 Xmit ADISC ACC response iotag x%x xri: "
                        "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
-                       phba->brd_no, elsiocb->iotag,
+                       phba->brd_no, vport->vpi, elsiocb->iotag,
                        elsiocb->iocb.ulpContext, ndlp->nlp_DID,
                        ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
 
        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
 
        *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
-       pcmd += sizeof (uint32_t);
+       pcmd += sizeof(uint32_t);
 
        ap = (ADISC *) (pcmd);
        ap->hardAL_PA = phba->fc_pref_ALPA;
-       memcpy(&ap->portName, &vport->fc_portname, sizeof (struct lpfc_name));
-       memcpy(&ap->nodeName, &vport->fc_nodename, sizeof (struct lpfc_name));
+       memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
+       memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
        ap->DID = be32_to_cpu(vport->fc_myDID);
 
        phba->fc_stat.elsXmitACC++;
@@ -2134,9 +2355,9 @@ lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
        psli = &phba->sli;
        pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
 
-       cmdsize = sizeof (uint32_t) + sizeof (PRLI);
+       cmdsize = sizeof(uint32_t) + sizeof(PRLI);
        elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
-            ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
+               ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
        if (!elsiocb)
                return 1;
 
@@ -2146,19 +2367,19 @@ lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
 
        /* Xmit PRLI ACC response tag <ulpIoTag> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0131 Xmit PRLI ACC response tag x%x xri x%x, "
+                       "%d (%d):0131 Xmit PRLI ACC response tag x%x xri x%x, "
                        "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
-                       phba->brd_no, elsiocb->iotag,
+                       phba->brd_no, vport->vpi, elsiocb->iotag,
                        elsiocb->iocb.ulpContext, ndlp->nlp_DID,
                        ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
 
        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
 
        *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
-       pcmd += sizeof (uint32_t);
+       pcmd += sizeof(uint32_t);
 
        /* For PRLI, remainder of payload is PRLI parameter page */
-       memset(pcmd, 0, sizeof (PRLI));
+       memset(pcmd, 0, sizeof(PRLI));
 
        npr = (PRLI *) pcmd;
        vpd = &phba->vpd;
@@ -2208,10 +2429,10 @@ lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
        psli = &phba->sli;
        pring = &psli->ring[LPFC_ELS_RING];
 
-       cmdsize = sizeof (uint32_t) + sizeof (uint32_t)
-               + (2 * sizeof (struct lpfc_name));
+       cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
+                                       + (2 * sizeof(struct lpfc_name));
        if (format)
-               cmdsize += sizeof (RNID_TOP_DISC);
+               cmdsize += sizeof(RNID_TOP_DISC);
 
        elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
                                     ndlp->nlp_DID, ELS_CMD_ACC);
@@ -2224,30 +2445,30 @@ lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
 
        /* Xmit RNID ACC response tag <ulpIoTag> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0132 Xmit RNID ACC response tag x%x "
+                       "%d (%d):0132 Xmit RNID ACC response tag x%x "
                        "xri x%x\n",
-                       phba->brd_no, elsiocb->iotag,
+                       phba->brd_no, vport->vpi, elsiocb->iotag,
                        elsiocb->iocb.ulpContext);
 
        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
 
        *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
-       pcmd += sizeof (uint32_t);
+       pcmd += sizeof(uint32_t);
 
-       memset(pcmd, 0, sizeof (RNID));
+       memset(pcmd, 0, sizeof(RNID));
        rn = (RNID *) (pcmd);
        rn->Format = format;
-       rn->CommonLen = (2 * sizeof (struct lpfc_name));
-       memcpy(&rn->portName, &vport->fc_portname, sizeof (struct lpfc_name));
-       memcpy(&rn->nodeName, &vport->fc_nodename, sizeof (struct lpfc_name));
+       rn->CommonLen = (2 * sizeof(struct lpfc_name));
+       memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
+       memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
        switch (format) {
        case 0:
                rn->SpecificLen = 0;
                break;
        case RNID_TOPOLOGY_DISC:
-               rn->SpecificLen = sizeof (RNID_TOP_DISC);
+               rn->SpecificLen = sizeof(RNID_TOP_DISC);
                memcpy(&rn->un.topologyDisc.portName,
-                      &vport->fc_portname, sizeof (struct lpfc_name));
+                      &vport->fc_portname, sizeof(struct lpfc_name));
                rn->un.topologyDisc.unitType = RNID_HBA;
                rn->un.topologyDisc.physPort = 0;
                rn->un.topologyDisc.attachedNodes = 0;
@@ -2344,22 +2565,15 @@ lpfc_els_disc_plogi(struct lpfc_vport *vport)
        return sentplogi;
 }
 
-int
+void
 lpfc_els_flush_rscn(struct lpfc_vport *vport)
 {
        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
        struct lpfc_hba  *phba = vport->phba;
-       struct lpfc_dmabuf *mp;
        int i;
 
        for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
-               mp = vport->fc_rscn_id_list[i];
-               if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
-                       lpfc_sli_hbqbuf_free(phba, mp->virt, mp->phys);
-               else {
-                       lpfc_mbuf_free(phba, mp->virt, mp->phys);
-                       kfree(mp);
-               }
+               lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
                vport->fc_rscn_id_list[i] = NULL;
        }
        spin_lock_irq(shost->host_lock);
@@ -2367,7 +2581,6 @@ lpfc_els_flush_rscn(struct lpfc_vport *vport)
        vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
        spin_unlock_irq(shost->host_lock);
        lpfc_can_disctmo(vport);
-       return 0;
 }
 
 int
@@ -2375,13 +2588,11 @@ lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
 {
        D_ID ns_did;
        D_ID rscn_did;
-       struct lpfc_dmabuf *mp;
        uint32_t *lp;
-       uint32_t payload_len, cmd, i, match;
+       uint32_t payload_len, i;
        struct lpfc_hba *phba = vport->phba;
 
        ns_did.un.word = did;
-       match = 0;
 
        /* Never match fabric nodes for RSCNs */
        if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
@@ -2392,45 +2603,40 @@ lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
                return did;
 
        for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
-               mp = vport->fc_rscn_id_list[i];
-               lp = (uint32_t *) mp->virt;
-               cmd = *lp++;
-               payload_len = be32_to_cpu(cmd) & 0xffff; /* payload length */
-               payload_len -= sizeof (uint32_t);       /* take off word 0 */
+               lp = vport->fc_rscn_id_list[i]->virt;
+               payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
+               payload_len -= sizeof(uint32_t);        /* take off word 0 */
                while (payload_len) {
-                       rscn_did.un.word = *lp++;
-                       rscn_did.un.word = be32_to_cpu(rscn_did.un.word);
-                       payload_len -= sizeof (uint32_t);
+                       rscn_did.un.word = be32_to_cpu(*lp++);
+                       payload_len -= sizeof(uint32_t);
                        switch (rscn_did.un.b.resv) {
                        case 0: /* Single N_Port ID effected */
                                if (ns_did.un.word == rscn_did.un.word)
-                                       match = did;
+                                       return did;
                                break;
                        case 1: /* Whole N_Port Area effected */
                                if ((ns_did.un.b.domain == rscn_did.un.b.domain)
                                    && (ns_did.un.b.area == rscn_did.un.b.area))
-                                               match = did;
+                                       return did;
                                break;
                        case 2: /* Whole N_Port Domain effected */
                                if (ns_did.un.b.domain == rscn_did.un.b.domain)
-                                               match = did;
-                               break;
-                       case 3: /* Whole Fabric effected */
-                               match = did;
+                                       return did;
                                break;
                        default:
                                /* Unknown Identifier in RSCN node */
                                lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
-                                               "%d:0217 Unknown Identifier in "
-                                               "RSCN payload Data: x%x\n",
-                                               phba->brd_no, rscn_did.un.word);
-                               break;
-                       }
-                       if (match)
-                               break;
+                                               "%d (%d):0217 Unknown "
+                                               "Identifier in RSCN payload "
+                                               "Data: x%x\n",
+                                               phba->brd_no, vport->vpi,
+                                               rscn_did.un.word);
+                       case 3: /* Whole Fabric effected */
+                               return did;
                        }
                }
-       return match;
+       }
+       return 0;
 }
 
 static int
@@ -2448,7 +2654,7 @@ lpfc_rscn_recovery_check(struct lpfc_vport *vport)
                        continue;
 
                lpfc_disc_state_machine(vport, ndlp, NULL,
-                                       NLP_EVT_DEVICE_RECOVERY);
+                                               NLP_EVT_DEVICE_RECOVERY);
 
                /*
                 * Make sure NLP_DELAY_TMO is NOT running after a device
@@ -2468,25 +2674,26 @@ lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
        struct lpfc_hba  *phba = vport->phba;
        struct lpfc_dmabuf *pcmd;
-       uint32_t *lp;
+       struct lpfc_vport *next_vport;
+       uint32_t *lp, *datap;
        IOCB_t *icmd;
-       uint32_t payload_len, cmd;
+       uint32_t payload_len, length, nportid, *cmd;
+       int rscn_cnt = vport->fc_rscn_id_cnt;
+       int rscn_id = 0, hba_id = 0;
        int i;
 
        icmd = &cmdiocb->iocb;
        pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
        lp = (uint32_t *) pcmd->virt;
 
-       cmd = *lp++;
-       payload_len = be32_to_cpu(cmd) & 0xffff;        /* payload length */
-       payload_len -= sizeof (uint32_t);       /* take off word 0 */
-       cmd &= ELS_CMD_MASK;
+       payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
+       payload_len -= sizeof(uint32_t);        /* take off word 0 */
 
        /* RSCN received */
        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d:0214 RSCN received Data: x%x x%x x%x x%x\n",
-                       phba->brd_no, vport->fc_flag, payload_len, *lp,
-                       vport->fc_rscn_id_cnt);
+                       "%d (%d):0214 RSCN received Data: x%x x%x x%x x%x\n",
+                       phba->brd_no, vport->vpi, vport->fc_flag, payload_len,
+                       *lp, rscn_cnt);
 
        for (i = 0; i < payload_len/sizeof(uint32_t); i++)
                fc_host_post_event(shost, fc_get_event_number(),
@@ -2497,32 +2704,77 @@ lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
         */
        if (vport->port_state <= LPFC_NS_QRY) {
                lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL,
-                                                               newnode);
+                                newnode);
                return 0;
        }
 
+       /* If this RSCN just contains NPortIDs for other vports on this HBA,
+        * just ACC and ignore it.
+        */
+       if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
+               !(phba->cfg_peer_port_login)) {
+               i = payload_len;
+               datap = lp;
+               while (i > 0) {
+                       nportid = *datap++;
+                       nportid = ((be32_to_cpu(nportid)) & Mask_DID);
+                       i -= sizeof(uint32_t);
+                       rscn_id++;
+                       list_for_each_entry(next_vport, &phba->port_list,
+                               listentry) {
+                               if (nportid == next_vport->fc_myDID) {
+                                       hba_id++;
+                                       break;
+                               }
+                       }
+               }
+               if (rscn_id == hba_id) {
+                       /* ALL NPortIDs in RSCN are on HBA */
+                       lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
+                         "%d (%d):0214 Ignore RSCN Data: x%x x%x x%x x%x\n",
+                         phba->brd_no, vport->vpi, vport->fc_flag, payload_len,
+                         *lp, rscn_cnt);
+                       lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
+                               ndlp, NULL, newnode);
+                       return 0;
+               }
+       }
+
        /* If we are already processing an RSCN, save the received
         * RSCN payload buffer, cmdiocb->context2 to process later.
         */
        if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
-               if ((vport->fc_rscn_id_cnt < FC_MAX_HOLD_RSCN) &&
+               vport->fc_flag |= FC_RSCN_DEFERRED;
+               if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
                    !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
                        spin_lock_irq(shost->host_lock);
                        vport->fc_flag |= FC_RSCN_MODE;
                        spin_unlock_irq(shost->host_lock);
-                       vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
-
-                       /* If we zero, cmdiocb->context2, the calling
-                        * routine will not try to free it.
-                        */
-                       cmdiocb->context2 = NULL;
+                       if (rscn_cnt) {
+                               cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
+                               length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
+                       }
+                       if ((rscn_cnt) &&
+                           (payload_len + length <= LPFC_BPL_SIZE)) {
+                               *cmd &= ELS_CMD_MASK;
+                               *cmd |= be32_to_cpu(payload_len + length);
+                               memcpy(((uint8_t *)cmd) + length, lp,
+                                      payload_len);
+                       } else {
+                               vport->fc_rscn_id_list[rscn_cnt] = pcmd;
+                               vport->fc_rscn_id_cnt++;
+                               /* If we zero, cmdiocb->context2, the calling
+                                * routine will not try to free it.
+                                */
+                               cmdiocb->context2 = NULL;
+                       }
 
                        /* Deferred RSCN */
                        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                                       "%d:0235 Deferred RSCN "
+                                       "%d (%d):0235 Deferred RSCN "
                                        "Data: x%x x%x x%x\n",
-                                       phba->brd_no, vport->fc_rscn_id_cnt,
-                                       vport->fc_flag,
+                                       phba->brd_no, vport->vpi,
+                                       vport->fc_rscn_id_cnt, vport->fc_flag,
                                        vport->port_state);
                } else {
                        spin_lock_irq(shost->host_lock);
@@ -2530,10 +2782,10 @@ lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
                        spin_unlock_irq(shost->host_lock);
                        /* ReDiscovery RSCN */
                        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                                       "%d:0234 ReDiscovery RSCN "
+                                       "%d (%d):0234 ReDiscovery RSCN "
                                        "Data: x%x x%x x%x\n",
-                                       phba->brd_no, vport->fc_rscn_id_cnt,
-                                       vport->fc_flag,
+                                       phba->brd_no, vport->vpi,
+                                       vport->fc_rscn_id_cnt, vport->fc_flag,
                                        vport->port_state);
                }
                /* Send back ACC */
@@ -2542,6 +2794,7 @@ lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 
                /* send RECOVERY event for ALL nodes that match RSCN payload */
                lpfc_rscn_recovery_check(vport);
+               vport->fc_flag &= ~FC_RSCN_DEFERRED;
                return 0;
        }
 
@@ -2572,13 +2825,19 @@ lpfc_els_handle_rscn(struct lpfc_vport *vport)
        struct lpfc_nodelist *ndlp;
        struct lpfc_hba *phba = vport->phba;
 
+       /* Ignore RSCN if the port is being torn down. */
+       if (vport->load_flag & FC_UNLOADING) {
+               lpfc_els_flush_rscn(vport);
+               return 0;
+       }
+
        /* Start timer for RSCN processing */
        lpfc_set_disctmo(vport);
 
        /* RSCN processed */
        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d:0215 RSCN processed Data: x%x x%x x%x x%x\n",
-                       phba->brd_no,
+                       "%d (%d):0215 RSCN processed Data: x%x x%x x%x x%x\n",
+                       phba->brd_no, vport->vpi,
                        vport->fc_flag, 0, vport->fc_rscn_id_cnt,
                        vport->port_state);
 
@@ -2587,7 +2846,7 @@ lpfc_els_handle_rscn(struct lpfc_vport *vport)
        ndlp = lpfc_findnode_did(vport, NameServer_DID);
        if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
                /* Good ndlp, issue CT Request to NameServer */
-               if (lpfc_ns_cmd(vport, ndlp, SLI_CTNS_GID_FT) == 0)
+               if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
                        /* Wait for NameServer query cmpl before we can
                           continue */
                        return 1;
@@ -2649,9 +2908,9 @@ lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
                /* An FLOGI ELS command <elsCmd> was received from DID <did> in
                   Loop Mode */
                lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
-                               "%d:0113 An FLOGI ELS command x%x was received "
-                               "from DID x%x in Loop Mode\n",
-                               phba->brd_no, cmd, did);
+                               "%d (%d):0113 An FLOGI ELS command x%x was "
+                               "received from DID x%x in Loop Mode\n",
+                               phba->brd_no, vport->vpi, cmd, did);
                return 1;
        }
 
@@ -2663,7 +2922,7 @@ lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
                 */
 
                rc = memcmp(&vport->fc_portname, &sp->portName,
-                           sizeof (struct lpfc_name));
+                           sizeof(struct lpfc_name));
 
                if (!rc) {
                        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
@@ -2802,7 +3061,7 @@ lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 
        pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
        *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
-       pcmd += sizeof (uint32_t); /* Skip past command */
+       pcmd += sizeof(uint32_t); /* Skip past command */
        rps_rsp = (RPS_RSP *)pcmd;
 
        if (phba->fc_topology != TOPOLOGY_LOOP)
@@ -2823,9 +3082,10 @@ lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 
        /* Xmit ELS RPS ACC response tag <ulpIoTag> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
-                       "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
-                       phba->brd_no, elsiocb->iotag,
+                       "%d (%d):0118 Xmit ELS RPS ACC response tag x%x "
+                       "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
+                       "rpi x%x\n",
+                       phba->brd_no, ndlp->vport->vpi, elsiocb->iotag,
                        elsiocb->iocb.ulpContext, ndlp->nlp_DID,
                        ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
 
@@ -2865,14 +3125,17 @@ lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
        if ((flag == 0) ||
            ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
            ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
-                          sizeof (struct lpfc_name)) == 0))) {
+                                   sizeof(struct lpfc_name)) == 0))) {
 
+               printk("Fix me....\n");
+               dump_stack();
                mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
                if (mbox) {
                        lpfc_read_lnk_stat(phba, mbox);
                        mbox->context1 =
-                           (void *)((unsigned long)cmdiocb->iocb.ulpContext);
+                           (void *)((unsigned long) cmdiocb->iocb.ulpContext);
                        mbox->context2 = lpfc_nlp_get(ndlp);
+                       mbox->vport = vport;
                        mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
                        if (lpfc_sli_issue_mbox (phba, mbox,
                            (MBX_NOWAIT | MBX_STOP_IOCB)) != MBX_NOT_FINISHED)
@@ -2915,7 +3178,7 @@ lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
 
        pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
        *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
-       pcmd += sizeof (uint16_t);
+       pcmd += sizeof(uint16_t);
        *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
        pcmd += sizeof(uint16_t);
 
@@ -2932,9 +3195,10 @@ lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
 
        /* Xmit ELS RPL ACC response tag <ulpIoTag> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0120 Xmit ELS RPL ACC response tag x%x xri x%x, "
-                       "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
-                       phba->brd_no, elsiocb->iotag,
+                       "%d (%d):0120 Xmit ELS RPL ACC response tag x%x "
+                       "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
+                       "rpi x%x\n",
+                       phba->brd_no, vport->vpi, elsiocb->iotag,
                        elsiocb->iocb.ulpContext, ndlp->nlp_DID,
                        ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
 
@@ -3008,8 +3272,8 @@ lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 
        /* FARP-REQ received from DID <did> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                        "%d:0601 FARP-REQ received from DID x%x\n",
-                        phba->brd_no, did);
+                       "%d (%d):0601 FARP-REQ received from DID x%x\n",
+                       phba->brd_no, vport->vpi, did);
 
        /* We will only support match on WWPN or WWNN */
        if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
@@ -3020,14 +3284,14 @@ lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
        /* If this FARP command is searching for my portname */
        if (fp->Mflags & FARP_MATCH_PORT) {
                if (memcmp(&fp->RportName, &vport->fc_portname,
-                          sizeof (struct lpfc_name)) == 0)
+                          sizeof(struct lpfc_name)) == 0)
                        cnt = 1;
        }
 
        /* If this FARP command is searching for my nodename */
        if (fp->Mflags & FARP_MATCH_NODE) {
                if (memcmp(&fp->RnodeName, &vport->fc_nodename,
-                          sizeof (struct lpfc_name)) == 0)
+                          sizeof(struct lpfc_name)) == 0)
                        cnt = 1;
        }
 
@@ -3068,8 +3332,8 @@ lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
        cmd = *lp++;
        /* FARP-RSP received from DID <did> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                        "%d:0600 FARP-RSP received from DID x%x\n",
-                        phba->brd_no, did);
+                       "%d (%d):0600 FARP-RSP received from DID x%x\n",
+                       phba->brd_no, vport->vpi, did);
        /* ACCEPT the Farp resp request */
        lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
 
@@ -3090,8 +3354,8 @@ lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 
        /* FAN received */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0265 FAN received\n",
-                       phba->brd_no);
+                       "%d (%d):0265 FAN received\n",
+                       phba->brd_no, vport->vpi);
 
        icmd = &cmdiocb->iocb;
        did = icmd->un.elsreq64.remoteID;
@@ -3099,7 +3363,7 @@ lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
        lp = (uint32_t *)pcmd->virt;
 
        cmd = *lp++;
-       fp = (FAN *)lp;
+       fp = (FAN *) lp;
 
        /* FAN received; Fan does not have a reply sequence */
 
@@ -3178,10 +3442,15 @@ lpfc_els_timeout(unsigned long ptr)
        spin_lock_irqsave(&vport->work_port_lock, iflag);
        if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
                vport->work_port_events |= WORKER_ELS_TMO;
+               spin_unlock_irqrestore(&vport->work_port_lock, iflag);
+
+               spin_lock_irqsave(&phba->hbalock, iflag);
                if (phba->work_wait)
-                       wake_up(phba->work_wait);
+                       lpfc_worker_wake_up(phba);
+               spin_unlock_irqrestore(&phba->hbalock, iflag);
        }
-       spin_unlock_irqrestore(&vport->work_port_lock, iflag);
+       else
+               spin_unlock_irqrestore(&vport->work_port_lock, iflag);
        return;
 }
 
@@ -3221,17 +3490,19 @@ lpfc_els_timeout_handler(struct lpfc_vport *vport)
                if (pcmd)
                        els_command = *(uint32_t *) (pcmd->virt);
 
-               if ((els_command == ELS_CMD_FARP)
-                   || (els_command == ELS_CMD_FARPR)) {
+               if (els_command == ELS_CMD_FARP ||
+                   els_command == ELS_CMD_FARPR ||
+                   els_command == ELS_CMD_FDISC)
+                       continue;
+
+               if (vport != piocb->vport)
                        continue;
-               }
 
                if (piocb->drvrTimeout > 0) {
-                       if (piocb->drvrTimeout >= timeout) {
+                       if (piocb->drvrTimeout >= timeout)
                                piocb->drvrTimeout -= timeout;
-                       } else {
+                       else
                                piocb->drvrTimeout = 0;
-                       }
                        continue;
                }
 
@@ -3245,11 +3516,10 @@ lpfc_els_timeout_handler(struct lpfc_vport *vport)
                                remote_ID = ndlp->nlp_DID;
                }
 
-               lpfc_printf_log(phba,
-                               KERN_ERR,
-                               LOG_ELS,
-                               "%d:0127 ELS timeout Data: x%x x%x x%x x%x\n",
-                               phba->brd_no, els_command,
+               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                               "%d (%d):0127 ELS timeout Data: x%x x%x x%x "
+                               "x%x\n",
+                               phba->brd_no, vport->vpi, els_command,
                                remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
 
                lpfc_sli_issue_abort_iotag(phba, pring, piocb);
@@ -3268,6 +3538,11 @@ lpfc_els_flush_cmd(struct lpfc_vport *vport)
        struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
        struct lpfc_iocbq *tmp_iocb, *piocb;
        IOCB_t *cmd = NULL;
+       struct lpfc_dmabuf *pcmd;
+       uint32_t *elscmd;
+       uint32_t els_command;
+
+       lpfc_fabric_abort_vport(vport);
 
        spin_lock_irq(&phba->hbalock);
        list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
@@ -3284,6 +3559,10 @@ lpfc_els_flush_cmd(struct lpfc_vport *vport)
                    cmd->ulpCommand == CMD_ABORT_XRI_CN)
                        continue;
 
+               pcmd = (struct lpfc_dmabuf *) piocb->context2;
+               elscmd = (uint32_t *) (pcmd->virt);
+               els_command = *elscmd;
+
                if (piocb->vport != vport)
                        continue;
 
@@ -3306,7 +3585,7 @@ lpfc_els_flush_cmd(struct lpfc_vport *vport)
        while (!list_empty(&completions)) {
                piocb = list_get_first(&completions, struct lpfc_iocbq, list);
                cmd = &piocb->iocb;
-               list_del(&piocb->list);
+               list_del_init(&piocb->list);
 
                if (!piocb->iocb_cmpl)
                        lpfc_sli_release_iocbq(phba, piocb);
@@ -3322,21 +3601,20 @@ lpfc_els_flush_cmd(struct lpfc_vport *vport)
 
 static void
 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
-                     struct lpfc_vport *vport, struct lpfc_dmabuf *mp,
-                     struct lpfc_iocbq *elsiocb)
+                     struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
 {
        struct lpfc_nodelist *ndlp;
        struct ls_rjt stat;
-       uint32_t *lp;
+       uint32_t *payload;
        uint32_t cmd, did, newnode, rjt_err = 0;
        IOCB_t *icmd = &elsiocb->iocb;
 
-       if (!vport || !mp)
+       if (vport == NULL || elsiocb->context2 == NULL)
                goto dropit;
 
        newnode = 0;
-       lp = (uint32_t *) mp->virt;
-       cmd = *lp++;
+       payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
+       cmd = *payload;
        if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
                lpfc_post_buffer(phba, pring, 1, 1);
 
@@ -3347,6 +3625,10 @@ lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
        if (lpfc_els_chk_latt(vport))
                goto dropit;
 
+       /* Ignore traffic recevied during vport shutdown. */
+       if (vport->load_flag & FC_UNLOADING)
+               goto dropit;
+
        did = icmd->un.rcvels.remoteID;
        ndlp = lpfc_findnode_did(vport, did);
        if (!ndlp) {
@@ -3367,7 +3649,6 @@ lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
        if (elsiocb->context1)
                lpfc_nlp_put(elsiocb->context1);
        elsiocb->context1 = lpfc_nlp_get(ndlp);
-       elsiocb->context2 = mp;
        elsiocb->vport = vport;
 
        if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
@@ -3375,18 +3656,20 @@ lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
        }
        /* ELS command <elsCmd> received from NPORT <did> */
        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d:0112 ELS command x%x received from NPORT x%x "
-                       "Data: x%x\n", phba->brd_no, cmd, did,
+                       "%d (%d):0112 ELS command x%x received from NPORT x%x "
+                       "Data: x%x\n", phba->brd_no, vport->vpi, cmd, did,
                        vport->port_state);
 
        switch (cmd) {
        case ELS_CMD_PLOGI:
                phba->fc_stat.elsRcvPLOGI++;
-               if (vport->port_state < LPFC_DISC_AUTH) {
-                       rjt_err = 1;
+               if ((vport->port_state < LPFC_DISC_AUTH) ||
+                   ((vport->port_type == LPFC_NPIV_PORT &&
+                     phba->cfg_vport_restrict_login))) {
+                       rjt_err = 2;
                        break;
                }
-               ndlp = lpfc_plogi_confirm_nport(phba, mp, ndlp);
+               ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
                lpfc_disc_state_machine(vport, ndlp, elsiocb,
                                        NLP_EVT_RCV_PLOGI);
                break;
@@ -3482,13 +3765,13 @@ lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
                break;
        default:
                /* Unsupported ELS command, reject */
-               rjt_err = 1;
+               rjt_err = 2;
 
                /* Unknown ELS command <elsCmd> received from NPORT <did> */
                lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
-                               "%d:0115 Unknown ELS command x%x "
+                               "%d (%d):0115 Unknown ELS command x%x "
                                "received from NPORT x%x\n",
-                               phba->brd_no, cmd, did);
+                               phba->brd_no, vport->vpi, cmd, did);
                if (newnode)
                        lpfc_drop_node(vport, ndlp);
                break;
@@ -3496,96 +3779,742 @@ lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
        /* check if need to LS_RJT received ELS cmd */
        if (rjt_err) {
-               stat.un.b.lsRjtRsvd0 = 0;
-               stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
+               memset(&stat, 0, sizeof(stat));
+               if (rjt_err == 1)
+                       stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
+               else
+                       stat.un.b.lsRjtRsnCode = LSRJT_INVALID_CMD;
                stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
-               stat.un.b.vendorUnique = 0;
                lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp);
+               if (newnode)
+                       lpfc_drop_node(vport, ndlp);
        }
 
        return;
 
 dropit:
        lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
-                       "%d:0111 Dropping received ELS cmd "
+                       "%d (%d):0111 Dropping received ELS cmd "
                        "Data: x%x x%x x%x\n",
-                       phba->brd_no,
+                       phba->brd_no, vport ? vport->vpi : 0xffff,
                        icmd->ulpStatus, icmd->un.ulpWord[4],
                        icmd->ulpTimeout);
        phba->fc_stat.elsRcvDrop++;
 }
 
+static struct lpfc_vport *
+lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
+{
+       struct lpfc_vport *vport;
+
+       list_for_each_entry(vport, &phba->port_list, listentry) {
+               if (vport->vpi == vpi)
+                       return vport;
+       }
+       return NULL;
+}
 
 void
 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
                     struct lpfc_iocbq *elsiocb)
 {
        struct lpfc_vport *vport = phba->pport;
-       struct lpfc_dmabuf *mp = NULL;
        IOCB_t *icmd = &elsiocb->iocb;
-       struct hbq_dmabuf *sp = NULL;
        dma_addr_t paddr;
+       struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
+       struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
 
-       if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
-           ((icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING)) {
+       elsiocb->context2 = NULL;
+       elsiocb->context3 = NULL;
+
+       if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
+               lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
+       } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
+           (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
                phba->fc_stat.NoRcvBuf++;
                /* Not enough posted buffers; Try posting more buffers */
-               if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
-                       lpfc_sli_hbqbuf_fill_hbq(phba);
-               else
+               if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
                        lpfc_post_buffer(phba, pring, 0, 1);
                return;
        }
 
-       /* If there are no BDEs associated with this IOCB,
-        * there is nothing to do.
-        */
+       if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
+           (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
+            icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
+               if (icmd->unsli3.rcvsli3.vpi == 0xffff)
+                       vport = phba->pport;
+               else {
+                       uint16_t vpi = icmd->unsli3.rcvsli3.vpi;
+                       vport = lpfc_find_vport_by_vpid(phba, vpi);
+               }
+       }
+                               /* If there are no BDEs associated
+                                * with this IOCB, there is nothing to do.
+                                */
        if (icmd->ulpBdeCount == 0)
                return;
 
-       /* type of ELS cmd is first 32bit word in packet */
+                               /* type of ELS cmd is first 32bit word
+                                * in packet
+                                */
        if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
-               paddr = getPaddr(icmd->un.cont64[0].addrHigh,
-                                icmd->un.cont64[0].addrLow);
-               sp = lpfc_sli_hbqbuf_find(phba, icmd->un.ulpWord[3]);
-               if (sp)
-                       phba->hbq_buff_count--;
-               mp = sp ? &sp->dbuf : NULL;
+               elsiocb->context2 = bdeBuf1;
        } else {
                paddr = getPaddr(icmd->un.cont64[0].addrHigh,
                                 icmd->un.cont64[0].addrLow);
-               mp = lpfc_sli_ringpostbuf_get(phba, pring, paddr);
+               elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
+                                                            paddr);
        }
 
-       lpfc_els_unsol_buffer(phba, pring, vport, mp, elsiocb);
-
+       lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
+       /*
+        * The different unsolicited event handlers would tell us
+        * if they are done with "mp" by setting context2 to NULL.
+        */
        lpfc_nlp_put(elsiocb->context1);
        elsiocb->context1 = NULL;
        if (elsiocb->context2) {
-               if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
-                       lpfc_sli_free_hbq(phba, sp);
-               else {
-                       lpfc_mbuf_free(phba, mp->virt, mp->phys);
-                       kfree(mp);
-               }
+               lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
+               elsiocb->context2 = NULL;
        }
 
        /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
-       if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) != 0 &&
+       if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
            icmd->ulpBdeCount == 2) {
-               sp = lpfc_sli_hbqbuf_find(phba, icmd->un.ulpWord[15]);
-               if (sp)
-                       phba->hbq_buff_count--;
-               mp = sp ? &sp->dbuf : NULL;
-               lpfc_els_unsol_buffer(phba, pring, vport, mp, elsiocb);
+               elsiocb->context2 = bdeBuf2;
+               lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
                /* free mp if we are done with it */
                if (elsiocb->context2) {
-                       if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
-                               lpfc_sli_free_hbq(phba, sp);
-                       else {
-                               lpfc_mbuf_free(phba, mp->virt, mp->phys);
-                               kfree(mp);
+                       lpfc_in_buf_free(phba, elsiocb->context2);
+                       elsiocb->context2 = NULL;
+               }
+       }
+}
+
+void
+lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
+{
+       struct lpfc_nodelist *ndlp, *ndlp_fdmi;
+
+       ndlp = lpfc_findnode_did(vport, NameServer_DID);
+       if (!ndlp) {
+               ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
+               if (!ndlp) {
+                       if (phba->fc_topology == TOPOLOGY_LOOP) {
+                               lpfc_disc_start(vport);
+                               return;
+                       }
+                       lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+                       lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                               "%d (%d):0251 NameServer login: no memory\n",
+                               phba->brd_no, vport->vpi);
+                       return;
+               }
+               lpfc_nlp_init(vport, ndlp, NameServer_DID);
+               ndlp->nlp_type |= NLP_FABRIC;
+       }
+
+       lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
+
+       if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
+               lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                       "%d (%d):0252 Cannot issue NameServer login\n",
+                       phba->brd_no, vport->vpi);
+               return;
+       }
+
+       if (phba->cfg_fdmi_on) {
+               ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
+                                         GFP_KERNEL);
+               if (ndlp_fdmi) {
+                       lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
+                       ndlp_fdmi->nlp_type |= NLP_FABRIC;
+                       ndlp_fdmi->nlp_state =
+                               NLP_STE_PLOGI_ISSUE;
+                       lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID,
+                                            0);
+               }
+       }
+       return;
+}
+
+static void
+lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
+{
+       struct lpfc_vport *vport = pmb->vport;
+       struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
+       struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
+       MAILBOX_t *mb = &pmb->mb;
+
+       vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
+       lpfc_nlp_put(ndlp);
+
+       if (mb->mbxStatus) {
+               lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
+                               "%d (%d):0915 Register VPI failed: 0x%x\n",
+                               phba->brd_no, vport->vpi, mb->mbxStatus);
+
+               switch (mb->mbxStatus) {
+               case 0x11:      /* unsupported feature */
+               case 0x9603:    /* max_vpi exceeded */
+                       /* giving up on vport registration */
+                       lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+                       spin_lock_irq(shost->host_lock);
+                       vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
+                       spin_unlock_irq(shost->host_lock);
+                       lpfc_can_disctmo(vport);
+                       break;
+               default:
+                       /* Try to recover from this error */
+                       lpfc_mbx_unreg_vpi(vport);
+                       vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
+                       lpfc_initial_fdisc(vport);
+                       break;
+               }
+
+       } else {
+               if (vport == phba->pport)
+                       lpfc_issue_fabric_reglogin(vport);
+               else
+                       lpfc_do_scr_ns_plogi(phba, vport);
+       }
+       mempool_free(pmb, phba->mbox_mem_pool);
+       return;
+}
+
+void
+lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
+                       struct lpfc_nodelist *ndlp)
+{
+       LPFC_MBOXQ_t *mbox;
+
+       mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
+       if (mbox) {
+               lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, mbox);
+               mbox->vport = vport;
+               mbox->context2 = lpfc_nlp_get(ndlp);
+               mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
+               if (lpfc_sli_issue_mbox(phba, mbox,
+                                       MBX_NOWAIT | MBX_STOP_IOCB)
+                   == MBX_NOT_FINISHED) {
+                       mempool_free(mbox, phba->mbox_mem_pool);
+                       vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
+
+                       lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+
+                       lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
+                               "%d (%d):0253 Register VPI: Cannot send mbox\n",
+                               phba->brd_no, vport->vpi);
+               }
+       } else {
+               lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+
+               lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
+                       "%d (%d):0254 Register VPI: no memory\n",
+                       phba->brd_no, vport->vpi);
+
+               vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
+               lpfc_nlp_put(ndlp);
+       }
+}
+
+static void
+lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
+                   struct lpfc_iocbq *rspiocb)
+{
+       struct lpfc_vport *vport = cmdiocb->vport;
+       struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
+       struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
+       struct lpfc_nodelist *np;
+       struct lpfc_nodelist *next_np;
+       IOCB_t *irsp = &rspiocb->iocb;
+       struct lpfc_iocbq *piocb;
+
+       lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
+               "%d (%d):0123 FDISC completes. x%x/x%x prevDID: x%x\n",
+               phba->brd_no, vport->vpi,
+               irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
+
+       /* Since all FDISCs are being single threaded, we
+        * must reset the discovery timer for ALL vports
+        * waiting to send FDISC when one completes.
+        */
+       list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
+               lpfc_set_disctmo(piocb->vport);
+       }
+
+       if (irsp->ulpStatus) {
+               /* Check for retry */
+               if (lpfc_els_retry(phba, cmdiocb, rspiocb))
+                       goto out;
+
+               /* FDISC failed */
+               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                       "%d (%d):0124 FDISC failed. (%d/%d)\n",
+                       phba->brd_no, vport->vpi,
+                       irsp->ulpStatus, irsp->un.ulpWord[4]);
+               if (vport->fc_vport->vport_state == FC_VPORT_INITIALIZING)
+                       lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+
+               lpfc_nlp_put(ndlp);
+               /* giving up on FDISC. Cancel discovery timer */
+               lpfc_can_disctmo(vport);
+       } else {
+               spin_lock_irq(shost->host_lock);
+               vport->fc_flag |= FC_FABRIC;
+               if (vport->phba->fc_topology == TOPOLOGY_LOOP)
+                       vport->fc_flag |=  FC_PUBLIC_LOOP;
+               spin_unlock_irq(shost->host_lock);
+
+               vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
+               lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
+               if ((vport->fc_prevDID != vport->fc_myDID) &&
+                       !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
+                       /* If our NportID changed, we need to ensure all
+                        * remaining NPORTs get unreg_login'ed so we can
+                        * issue unreg_vpi.
+                        */
+                       list_for_each_entry_safe(np, next_np,
+                               &vport->fc_nodes, nlp_listp) {
+                               if (np->nlp_state != NLP_STE_NPR_NODE
+                                  || !(np->nlp_flag & NLP_NPR_ADISC))
+                                       continue;
+                               spin_lock_irq(shost->host_lock);
+                               np->nlp_flag &= ~NLP_NPR_ADISC;
+                               spin_unlock_irq(shost->host_lock);
+                               lpfc_unreg_rpi(vport, np);
+                       }
+                       lpfc_mbx_unreg_vpi(vport);
+                       vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
+               }
+
+               if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
+                       lpfc_register_new_vport(phba, vport, ndlp);
+               else
+                       lpfc_do_scr_ns_plogi(phba, vport);
+
+               lpfc_nlp_put(ndlp); /* Free Fabric ndlp for vports */
+       }
+
+out:
+       lpfc_els_free_iocb(phba, cmdiocb);
+}
+
+int
+lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
+                    uint8_t retry)
+{
+       struct lpfc_hba *phba = vport->phba;
+       IOCB_t *icmd;
+       struct lpfc_iocbq *elsiocb;
+       struct serv_parm *sp;
+       uint8_t *pcmd;
+       uint16_t cmdsize;
+       int did = ndlp->nlp_DID;
+       int rc;
+       int new_ndlp = 0;
+
+       cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
+       elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
+                                    ELS_CMD_FDISC);
+       if (!elsiocb) {
+               if (new_ndlp)
+                       mempool_free(ndlp, phba->nlp_mem_pool);
+               lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+
+               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                       "%d (%d):0255 Issue FDISC: no IOCB\n",
+                       phba->brd_no, vport->vpi);
+               return 1;
+       }
+
+       icmd = &elsiocb->iocb;
+       icmd->un.elsreq64.myID = 0;
+       icmd->un.elsreq64.fl = 1;
+
+       /* For FDISC, Let FDISC rsp set the NPortID for this VPI */
+       icmd->ulpCt_h = 1;
+       icmd->ulpCt_l = 0;
+
+       pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
+       *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
+       pcmd += sizeof(uint32_t); /* CSP Word 1 */
+       memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
+       sp = (struct serv_parm *) pcmd;
+       /* Setup CSPs accordingly for Fabric */
+       sp->cmn.e_d_tov = 0;
+       sp->cmn.w2.r_a_tov = 0;
+       sp->cls1.classValid = 0;
+       sp->cls2.seqDelivery = 1;
+       sp->cls3.seqDelivery = 1;
+
+       pcmd += sizeof(uint32_t); /* CSP Word 2 */
+       pcmd += sizeof(uint32_t); /* CSP Word 3 */
+       pcmd += sizeof(uint32_t); /* CSP Word 4 */
+       pcmd += sizeof(uint32_t); /* Port Name */
+       memcpy(pcmd, &vport->fc_portname, 8);
+       pcmd += sizeof(uint32_t); /* Node Name */
+       pcmd += sizeof(uint32_t); /* Node Name */
+       memcpy(pcmd, &vport->fc_nodename, 8);
+
+       lpfc_set_disctmo(vport);
+
+       phba->fc_stat.elsXmitFDISC++;
+       elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
+
+       rc = lpfc_issue_fabric_iocb(phba, elsiocb);
+       if (rc == IOCB_ERROR) {
+               lpfc_els_free_iocb(phba, elsiocb);
+               if (new_ndlp)
+                       mempool_free(ndlp, phba->nlp_mem_pool);
+               lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+
+               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                       "%d (%d):0256 Issue FDISC: Cannot send IOCB\n",
+                       phba->brd_no, vport->vpi);
+
+               return 1;
+       }
+       lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
+       vport->port_state = LPFC_FDISC;
+       return 0;
+}
+
+static void
+lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
+                       struct lpfc_iocbq *rspiocb)
+{
+       struct lpfc_vport *vport = cmdiocb->vport;
+
+       lpfc_els_free_iocb(phba, cmdiocb);
+       vport->unreg_vpi_cmpl = VPORT_ERROR;
+}
+
+int
+lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
+{
+       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
+       struct lpfc_hba  *phba = vport->phba;
+       struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
+       IOCB_t *icmd;
+       struct lpfc_iocbq *elsiocb;
+       uint8_t *pcmd;
+       uint16_t cmdsize;
+
+       cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
+       elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
+                                    ELS_CMD_LOGO);
+       if (!elsiocb)
+               return 1;
+
+       icmd = &elsiocb->iocb;
+       pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
+       *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
+       pcmd += sizeof(uint32_t);
+
+       /* Fill in LOGO payload */
+       *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
+       pcmd += sizeof(uint32_t);
+       memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
+
+       elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
+       spin_lock_irq(shost->host_lock);
+       ndlp->nlp_flag |= NLP_LOGO_SND;
+       spin_unlock_irq(shost->host_lock);
+       if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
+               spin_lock_irq(shost->host_lock);
+               ndlp->nlp_flag &= ~NLP_LOGO_SND;
+               spin_unlock_irq(shost->host_lock);
+               lpfc_els_free_iocb(phba, elsiocb);
+               return 1;
+       }
+       return 0;
+}
+
+void
+lpfc_fabric_block_timeout(unsigned long ptr)
+{
+       struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
+       unsigned long iflags;
+       uint32_t tmo_posted;
+       spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
+       tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
+       if (!tmo_posted)
+               phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
+       spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
+
+       if (!tmo_posted) {
+               spin_lock_irqsave(&phba->hbalock, iflags);
+               if (phba->work_wait)
+                       lpfc_worker_wake_up(phba);
+               spin_unlock_irqrestore(&phba->hbalock, iflags);
+       }
+}
+
+static void
+lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
+{
+       struct lpfc_iocbq *iocb;
+       unsigned long iflags;
+       int ret;
+       struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
+       IOCB_t *cmd;
+
+repeat:
+       iocb = NULL;
+       spin_lock_irqsave(&phba->hbalock, iflags);
+                               /* Post any pending iocb to the SLI layer */
+       if (atomic_read(&phba->fabric_iocb_count) == 0) {
+               list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
+                                list);
+               if (iocb)
+                       atomic_inc(&phba->fabric_iocb_count);
+       }
+       spin_unlock_irqrestore(&phba->hbalock, iflags);
+       if (iocb) {
+               iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
+               iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
+               iocb->iocb_flag |= LPFC_IO_FABRIC;
+
+               ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
+
+               if (ret == IOCB_ERROR) {
+                       iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
+                       iocb->fabric_iocb_cmpl = NULL;
+                       iocb->iocb_flag &= ~LPFC_IO_FABRIC;
+                       cmd = &iocb->iocb;
+                       cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
+                       cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
+                       iocb->iocb_cmpl(phba, iocb, iocb);
+
+                       atomic_dec(&phba->fabric_iocb_count);
+                       goto repeat;
+               }
+       }
+
+       return;
+}
+
+void
+lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
+{
+       clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
+
+       lpfc_resume_fabric_iocbs(phba);
+       return;
+}
+
+static void
+lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
+{
+       int blocked;
+
+       blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
+                               /* Start a timer to unblock fabric
+                                * iocbs after 100ms
+                                */
+       if (!blocked)
+               mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
+
+       return;
+}
+
+static void
+lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
+       struct lpfc_iocbq *rspiocb)
+{
+       struct ls_rjt stat;
+
+       if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
+               BUG();
+
+       switch (rspiocb->iocb.ulpStatus) {
+               case IOSTAT_NPORT_RJT:
+               case IOSTAT_FABRIC_RJT:
+                       if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
+                               lpfc_block_fabric_iocbs(phba);
                        }
+                       break;
+
+               case IOSTAT_NPORT_BSY:
+               case IOSTAT_FABRIC_BSY:
+                       lpfc_block_fabric_iocbs(phba);
+                       break;
+
+               case IOSTAT_LS_RJT:
+                       stat.un.lsRjtError =
+                               be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
+                       if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
+                               (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
+                               lpfc_block_fabric_iocbs(phba);
+                       break;
+       }
+
+       if (atomic_read(&phba->fabric_iocb_count) == 0)
+               BUG();
+
+       cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
+       cmdiocb->fabric_iocb_cmpl = NULL;
+       cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
+       cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
+
+       atomic_dec(&phba->fabric_iocb_count);
+       if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
+                               /* Post any pending iocbs to HBA */
+                   lpfc_resume_fabric_iocbs(phba);
+       }
+}
+
+int
+lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
+{
+       unsigned long iflags;
+       struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
+       int ready;
+       int ret;
+
+       if (atomic_read(&phba->fabric_iocb_count) > 1)
+               BUG();
+
+       spin_lock_irqsave(&phba->hbalock, iflags);
+       ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
+               !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
+
+       spin_unlock_irqrestore(&phba->hbalock, iflags);
+       if (ready) {
+               iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
+               iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
+               iocb->iocb_flag |= LPFC_IO_FABRIC;
+
+               atomic_inc(&phba->fabric_iocb_count);
+               ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
+
+               if (ret == IOCB_ERROR) {
+                       iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
+                       iocb->fabric_iocb_cmpl = NULL;
+                       iocb->iocb_flag &= ~LPFC_IO_FABRIC;
+                       atomic_dec(&phba->fabric_iocb_count);
+               }
+       } else {
+               spin_lock_irqsave(&phba->hbalock, iflags);
+               list_add_tail(&iocb->list, &phba->fabric_iocb_list);
+               spin_unlock_irqrestore(&phba->hbalock, iflags);
+               ret = IOCB_SUCCESS;
+       }
+       return ret;
+}
+
+
+void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
+{
+       LIST_HEAD(completions);
+       struct lpfc_hba  *phba = vport->phba;
+       struct lpfc_iocbq *tmp_iocb, *piocb;
+       IOCB_t *cmd;
+
+       spin_lock_irq(&phba->hbalock);
+       list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
+                                list) {
+
+               if (piocb->vport != vport)
+                       continue;
+
+               list_move_tail(&piocb->list, &completions);
+       }
+       spin_unlock_irq(&phba->hbalock);
+
+       while (!list_empty(&completions)) {
+               piocb = list_get_first(&completions, struct lpfc_iocbq, list);
+               list_del_init(&piocb->list);
+
+               cmd = &piocb->iocb;
+               cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
+               cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
+               (piocb->iocb_cmpl) (phba, piocb, piocb);
+       }
+}
+
+void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
+{
+       LIST_HEAD(completions);
+       struct lpfc_hba  *phba = ndlp->vport->phba;
+       struct lpfc_iocbq *tmp_iocb, *piocb;
+       struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
+       IOCB_t *cmd;
+
+       spin_lock_irq(&phba->hbalock);
+       list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
+                                list) {
+               if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
+
+                       list_move_tail(&piocb->list, &completions);
                }
        }
+       spin_unlock_irq(&phba->hbalock);
+
+       while (!list_empty(&completions)) {
+               piocb = list_get_first(&completions, struct lpfc_iocbq, list);
+               list_del_init(&piocb->list);
+
+               cmd = &piocb->iocb;
+               cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
+               cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
+               (piocb->iocb_cmpl) (phba, piocb, piocb);
+       }
 }
+
+void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
+{
+       LIST_HEAD(completions);
+       struct lpfc_iocbq *piocb;
+       IOCB_t *cmd;
+
+       spin_lock_irq(&phba->hbalock);
+       list_splice_init(&phba->fabric_iocb_list, &completions);
+       spin_unlock_irq(&phba->hbalock);
+
+       while (!list_empty(&completions)) {
+               piocb = list_get_first(&completions, struct lpfc_iocbq, list);
+               list_del_init(&piocb->list);
+
+               cmd = &piocb->iocb;
+               cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
+               cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
+               (piocb->iocb_cmpl) (phba, piocb, piocb);
+       }
+}
+
+
+void lpfc_fabric_abort_flogi(struct lpfc_hba *phba)
+{
+       LIST_HEAD(completions);
+       struct lpfc_iocbq *tmp_iocb, *piocb;
+       IOCB_t *cmd;
+       struct lpfc_nodelist *ndlp;
+
+       spin_lock_irq(&phba->hbalock);
+       list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
+                                list) {
+
+               cmd = &piocb->iocb;
+               ndlp = (struct lpfc_nodelist *) piocb->context1;
+               if (cmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
+                   ndlp != NULL &&
+                   ndlp->nlp_DID == Fabric_DID)
+                       list_move_tail(&piocb->list, &completions);
+       }
+       spin_unlock_irq(&phba->hbalock);
+
+       while (!list_empty(&completions)) {
+               piocb = list_get_first(&completions, struct lpfc_iocbq, list);
+               list_del_init(&piocb->list);
+
+               cmd = &piocb->iocb;
+               cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
+               cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
+               (piocb->iocb_cmpl) (phba, piocb, piocb);
+       }
+}
+
+
index 20b2a49..94ee967 100644 (file)
@@ -36,6 +36,7 @@
 #include "lpfc.h"
 #include "lpfc_logmsg.h"
 #include "lpfc_crtn.h"
+#include "lpfc_vport.h"
 
 /* AlpaArray for assignment of scsid for scan-down and bind_method */
 static uint8_t lpfcAlpaArray[] = {
@@ -96,50 +97,68 @@ lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
        int warn_on = 0;
        struct lpfc_hba *phba;
        struct lpfc_vport *vport;
+       int  put_node;
+       int  put_rport;
 
        rdata = rport->dd_data;
        ndlp = rdata->pnode;
 
        if (!ndlp) {
-               if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
+               if (rport->scsi_target_id != -1) {
                        printk(KERN_ERR "Cannot find remote node"
-                       " for rport in dev_loss_tmo_callbk x%x\n",
-                       rport->port_id);
+                               " for rport in dev_loss_tmo_callbk x%x\n",
+                               rport->port_id);
+               }
                return;
        }
 
-       if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
+       if (ndlp->nlp_type & NLP_FABRIC) {
+               /* We will clean up these Nodes in linkup */
+               put_node = rdata->pnode != NULL;
+               put_rport = ndlp->rport != NULL;
+               rdata->pnode = NULL;
+               ndlp->rport = NULL;
+               if (put_node)
+                       lpfc_nlp_put(ndlp);
+               if (put_rport)
+                       put_device(&rport->dev);
                return;
+       }
 
        name = (uint8_t *)&ndlp->nlp_portname;
        vport = ndlp->vport;
        phba  = vport->phba;
 
+       if (!(vport->load_flag & FC_UNLOADING) &&
+           ndlp->nlp_state == NLP_STE_MAPPED_NODE)
+               return;
+
+
        if (ndlp->nlp_sid != NLP_NO_SID) {
                warn_on = 1;
                /* flush the target */
                lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
-                       ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
+                                   ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
        }
        if (vport->load_flag & FC_UNLOADING)
                warn_on = 0;
 
        if (warn_on) {
                lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
-                               "%d:0203 Devloss timeout on "
+                               "%d (%d):0203 Devloss timeout on "
                                "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
                                "NPort x%x Data: x%x x%x x%x\n",
-                               phba->brd_no,
+                               phba->brd_no, vport->vpi,
                                *name, *(name+1), *(name+2), *(name+3),
                                *(name+4), *(name+5), *(name+6), *(name+7),
                                ndlp->nlp_DID, ndlp->nlp_flag,
                                ndlp->nlp_state, ndlp->nlp_rpi);
        } else {
                lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                               "%d:0204 Devloss timeout on "
+                               "%d (%d):0204 Devloss timeout on "
                                "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
                                "NPort x%x Data: x%x x%x x%x\n",
-                               phba->brd_no,
+                               phba->brd_no, vport->vpi,
                                *name, *(name+1), *(name+2), *(name+3),
                                *(name+4), *(name+5), *(name+6), *(name+7),
                                ndlp->nlp_DID, ndlp->nlp_flag,
@@ -152,12 +171,23 @@ lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
            (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
                lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
        else {
+               put_node = rdata->pnode != NULL;
+               put_rport = ndlp->rport != NULL;
                rdata->pnode = NULL;
                ndlp->rport = NULL;
-               lpfc_nlp_put(ndlp);
-               put_device(&rport->dev);
+               if (put_node)
+                       lpfc_nlp_put(ndlp);
+               if (put_rport)
+                       put_device(&rport->dev);
        }
+       return;
+}
 
+
+void
+lpfc_worker_wake_up(struct lpfc_hba *phba)
+{
+       wake_up(phba->work_wait);
        return;
 }
 
@@ -166,6 +196,7 @@ lpfc_work_list_done(struct lpfc_hba *phba)
 {
        struct lpfc_work_evt  *evtp = NULL;
        struct lpfc_nodelist  *ndlp;
+       struct lpfc_vport     *vport;
        int free_evt;
 
        spin_lock_irq(&phba->hbalock);
@@ -175,10 +206,23 @@ lpfc_work_list_done(struct lpfc_hba *phba)
                spin_unlock_irq(&phba->hbalock);
                free_evt = 1;
                switch (evtp->evt) {
+               case LPFC_EVT_DEV_LOSS:
+                       free_evt = 0; /* evt is part of ndlp */
+                       ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
+                       vport = ndlp->vport;
+                       if (!vport)
+                               break;
+                       if (!(vport->load_flag & FC_UNLOADING) &&
+                           !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
+                           !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
+                               lpfc_disc_state_machine(vport, ndlp, NULL,
+                                       NLP_EVT_DEVICE_RM);
+                       }
+                       break;
                case LPFC_EVT_ELS_RETRY:
                        ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
                        lpfc_els_retry_delay_handler(ndlp);
-                       free_evt = 0;
+                       free_evt = 0; /* evt is part of ndlp */
                        break;
                case LPFC_EVT_ONLINE:
                        if (phba->link_state < LPFC_LINK_DOWN)
@@ -250,24 +294,43 @@ lpfc_work_done(struct lpfc_hba *phba)
        if (ha_copy & HA_LATT)
                lpfc_handle_latt(phba);
 
-       vport = phba->pport;
+       spin_lock_irq(&phba->hbalock);
+       list_for_each_entry(vport, &phba->port_list, listentry) {
+               struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
 
-       work_port_events = vport->work_port_events;
+               if (!scsi_host_get(shost)) {
+                       continue;
+               }
+               spin_unlock_irq(&phba->hbalock);
+               work_port_events = vport->work_port_events;
 
-       if (work_port_events & WORKER_DISC_TMO)
-               lpfc_disc_timeout_handler(vport);
+               if (work_port_events & WORKER_DISC_TMO)
+                       lpfc_disc_timeout_handler(vport);
 
-       if (work_port_events & WORKER_ELS_TMO)
-               lpfc_els_timeout_handler(vport);
+               if (work_port_events & WORKER_ELS_TMO)
+                       lpfc_els_timeout_handler(vport);
 
-       if (work_port_events & WORKER_MBOX_TMO)
-               lpfc_mbox_timeout_handler(phba);
+               if (work_port_events & WORKER_MBOX_TMO)
+                       lpfc_mbox_timeout_handler(phba);
 
-       if (work_port_events & WORKER_FDMI_TMO)
-               lpfc_fdmi_timeout_handler(vport);
+               if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
+                       lpfc_unblock_fabric_iocbs(phba);
 
-       spin_lock_irq(&phba->hbalock);
-       vport->work_port_events &= ~work_port_events;
+               if (work_port_events & WORKER_FDMI_TMO)
+                       lpfc_fdmi_timeout_handler(vport);
+
+               if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
+                       lpfc_ramp_down_queue_handler(phba);
+
+               if (work_port_events & WORKER_RAMP_UP_QUEUE)
+                       lpfc_ramp_up_queue_handler(phba);
+
+               spin_lock_irq(&vport->work_port_lock);
+               vport->work_port_events &= ~work_port_events;
+               spin_unlock_irq(&vport->work_port_lock);
+               scsi_host_put(shost);
+               spin_lock_irq(&phba->hbalock);
+       }
        spin_unlock_irq(&phba->hbalock);
 
        for (i = 0; i < phba->sli.num_rings; i++, ha_copy >>= 4) {
@@ -300,24 +363,41 @@ lpfc_work_done(struct lpfc_hba *phba)
 static int
 check_work_wait_done(struct lpfc_hba *phba)
 {
-       struct lpfc_vport *vport = phba->pport;
-       int rc = 0;
-
-       if (!vport)
-               return 0;
+       struct lpfc_vport *vport;
+       struct lpfc_sli_ring *pring;
+       int i, rc = 0;
 
        spin_lock_irq(&phba->hbalock);
+       list_for_each_entry(vport, &phba->port_list, listentry) {
+               if (vport->work_port_events) {
+                       rc = 1;
+                       goto exit;
+               }
+       }
 
-       if (phba->work_ha ||
-           vport->work_port_events ||
-           (!list_empty(&phba->work_list)) ||
-           kthread_should_stop())
+       if (phba->work_ha || (!list_empty(&phba->work_list)) ||
+           kthread_should_stop()) {
                rc = 1;
+               goto exit;
+       }
+       for (i = 0; i < phba->sli.num_rings; i++) {
+               pring = &phba->sli.ring[i];
+               if (pring->flag & LPFC_DEFERRED_RING_EVENT) {
+                       rc = 1;
+                       goto exit;
+               }
+       }
+exit:
+       if (rc)
+               phba->work_found++;
+       else
+               phba->work_found = 0;
 
        spin_unlock_irq(&phba->hbalock);
        return rc;
 }
 
+
 int
 lpfc_do_work(void *p)
 {
@@ -327,11 +407,13 @@ lpfc_do_work(void *p)
 
        set_user_nice(current, -20);
        phba->work_wait = &work_waitq;
+       phba->work_found = 0;
 
        while (1) {
 
                rc = wait_event_interruptible(work_waitq,
-                                               check_work_wait_done(phba));
+                                             check_work_wait_done(phba));
+
                BUG_ON(rc);
 
                if (kthread_should_stop())
@@ -339,6 +421,17 @@ lpfc_do_work(void *p)
 
                lpfc_work_done(phba);
 
+               /* If there is alot of slow ring work, like during link up
+                * check_work_wait_done() may cause this thread to not give
+                * up the CPU for very long periods of time. This may cause
+                * soft lockups or other problems. To avoid these situations
+                * give up the CPU here after LPFC_MAX_WORKER_ITERATION
+                * consecutive iterations.
+                */
+               if (phba->work_found >= LPFC_MAX_WORKER_ITERATION) {
+                       phba->work_found = 0;
+                       schedule();
+               }
        }
        phba->work_wait = NULL;
        return 0;
@@ -360,7 +453,7 @@ lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
         * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
         * be queued to worker thread for processing
         */
-       evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_KERNEL);
+       evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
        if (!evtp)
                return 0;
 
@@ -371,37 +464,94 @@ lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
        spin_lock_irqsave(&phba->hbalock, flags);
        list_add_tail(&evtp->evt_listp, &phba->work_list);
        if (phba->work_wait)
-               wake_up(phba->work_wait);
+               lpfc_worker_wake_up(phba);
        spin_unlock_irqrestore(&phba->hbalock, flags);
 
        return 1;
 }
 
+void
+lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
+{
+       struct lpfc_hba  *phba = vport->phba;
+       struct lpfc_nodelist *ndlp, *next_ndlp;
+       int  rc;
+
+       list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
+               if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
+                       continue;
+
+               if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN)
+                       lpfc_unreg_rpi(vport, ndlp);
+
+               /* Leave Fabric nodes alone on link down */
+               if (!remove && ndlp->nlp_type & NLP_FABRIC)
+                       continue;
+               rc = lpfc_disc_state_machine(vport, ndlp, NULL,
+                                            remove
+                                            ? NLP_EVT_DEVICE_RM
+                                            : NLP_EVT_DEVICE_RECOVERY);
+       }
+       if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
+               lpfc_mbx_unreg_vpi(vport);
+               vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
+       }
+}
+
+static void
+lpfc_linkdown_port(struct lpfc_vport *vport)
+{
+       struct lpfc_nodelist *ndlp, *next_ndlp;
+       struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
+
+       fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
+
+       /* Cleanup any outstanding RSCN activity */
+       lpfc_els_flush_rscn(vport);
+
+       /* Cleanup any outstanding ELS commands */
+       lpfc_els_flush_cmd(vport);
+
+       lpfc_cleanup_rpis(vport, 0);
+
+       /* free any ndlp's on unused list */
+       list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
+                               /* free any ndlp's in unused state */
+               if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
+                       lpfc_drop_node(vport, ndlp);
+
+       /* Turn off discovery timer if its running */
+       lpfc_can_disctmo(vport);
+}
+
 int
 lpfc_linkdown(struct lpfc_hba *phba)
 {
        struct lpfc_vport *vport = phba->pport;
        struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
-       struct lpfc_sli   *psli;
-       struct lpfc_nodelist  *ndlp, *next_ndlp;
+       struct lpfc_vport *port_iterator;
        LPFC_MBOXQ_t          *mb;
-       int                   rc;
 
-       psli = &phba->sli;
        if (phba->link_state == LPFC_LINK_DOWN) {
                return 0;
        }
        spin_lock_irq(&phba->hbalock);
-       if (phba->link_state > LPFC_LINK_DOWN)
+       if (phba->link_state > LPFC_LINK_DOWN) {
                phba->link_state = LPFC_LINK_DOWN;
+               phba->pport->fc_flag &= ~FC_LBIT;
+       }
        spin_unlock_irq(&phba->hbalock);
 
-       fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
+       list_for_each_entry(port_iterator, &phba->port_list, listentry) {
+
+                               /* Issue a LINK DOWN event to all nodes */
+               lpfc_linkdown_port(port_iterator);
+       }
 
        /* Clean up any firmware default rpi's */
        mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
        if (mb) {
-               lpfc_unreg_did(phba, 0xffffffff, mb);
+               lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
                mb->vport = vport;
                mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
                if (lpfc_sli_issue_mbox(phba, mb, (MBX_NOWAIT | MBX_STOP_IOCB))
@@ -410,31 +560,13 @@ lpfc_linkdown(struct lpfc_hba *phba)
                }
        }
 
-       /* Cleanup any outstanding RSCN activity */
-       lpfc_els_flush_rscn(vport);
-
-       /* Cleanup any outstanding ELS commands */
-       lpfc_els_flush_cmd(vport);
-
-       /*
-        * Issue a LINK DOWN event to all nodes.
-        */
-       list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
-                               /* free any ndlp's on unused state */
-               if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
-                       lpfc_drop_node(vport, ndlp);
-               else            /* otherwise, force node recovery. */
-                       rc = lpfc_disc_state_machine(vport, ndlp, NULL,
-                                                    NLP_EVT_DEVICE_RECOVERY);
-       }
-
        /* Setup myDID for link up if we are in pt2pt mode */
-       if (vport->fc_flag & FC_PT2PT) {
-               vport->fc_myDID = 0;
+       if (phba->pport->fc_flag & FC_PT2PT) {
+               phba->pport->fc_myDID = 0;
                mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
                if (mb) {
                        lpfc_config_link(phba, mb);
-                       mb->mbox_cmpl=lpfc_sli_def_mbox_cmpl;
+                       mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
                        mb->vport = vport;
                        if (lpfc_sli_issue_mbox(phba, mb,
                                                (MBX_NOWAIT | MBX_STOP_IOCB))
@@ -443,66 +575,88 @@ lpfc_linkdown(struct lpfc_hba *phba)
                        }
                }
                spin_lock_irq(shost->host_lock);
-               vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
+               phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
                spin_unlock_irq(shost->host_lock);
        }
 
-       spin_lock_irq(shost->host_lock);
-       vport->fc_flag &= ~FC_LBIT;
-       spin_unlock_irq(shost->host_lock);
+       return 0;
+}
 
-       /* Turn off discovery timer if its running */
-       lpfc_can_disctmo(vport);
+static void
+lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
+{
+       struct lpfc_nodelist *ndlp;
 
-       /* Must process IOCBs on all rings to handle ABORTed I/Os */
-       return 0;
+       list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
+               if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
+                       continue;
+
+               if (ndlp->nlp_type & NLP_FABRIC) {
+                               /* On Linkup its safe to clean up the ndlp
+                                * from Fabric connections.
+                                */
+                       if (ndlp->nlp_DID != Fabric_DID)
+                               lpfc_unreg_rpi(vport, ndlp);
+                       lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
+               } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
+                               /* Fail outstanding IO now since device is
+                                * marked for PLOGI.
+                                */
+                       lpfc_unreg_rpi(vport, ndlp);
+               }
+       }
 }
 
-static int
-lpfc_linkup(struct lpfc_hba *phba)
+static void
+lpfc_linkup_port(struct lpfc_vport *vport)
 {
-       struct lpfc_vport *vport = phba->pport;
-       struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
+       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
        struct lpfc_nodelist *ndlp, *next_ndlp;
+       struct lpfc_hba  *phba = vport->phba;
+
+       if ((vport->load_flag & FC_UNLOADING) != 0)
+               return;
+
+       /* If NPIV is not enabled, only bring the physical port up */
+       if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
+               (vport != phba->pport))
+               return;
 
        fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
 
        spin_lock_irq(shost->host_lock);
-       phba->link_state = LPFC_LINK_UP;
        vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
                            FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
        vport->fc_flag |= FC_NDISC_ACTIVE;
        vport->fc_ns_retry = 0;
        spin_unlock_irq(shost->host_lock);
 
+       if (vport->fc_flag & FC_LBIT)
+               lpfc_linkup_cleanup_nodes(vport);
 
-       if (vport->fc_flag & FC_LBIT) {
-               list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
-                       if (ndlp->nlp_state != NLP_STE_UNUSED_NODE) {
-                               if (ndlp->nlp_type & NLP_FABRIC) {
-                                       /*
-                                        * On Linkup its safe to clean up the
-                                        * ndlp from Fabric connections.
-                                        */
-                                       lpfc_nlp_set_state(vport, ndlp,
-                                                          NLP_STE_UNUSED_NODE);
-                               } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
-                                       /*
-                                        * Fail outstanding IO now since
-                                        * device is marked for PLOGI.
-                                        */
-                                       lpfc_unreg_rpi(vport, ndlp);
-                               }
-                       }
-               }
-       }
-
-       /* free any ndlp's in unused state */
+                               /* free any ndlp's in unused state */
        list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
-                                nlp_listp) {
+                                nlp_listp)
                if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
                        lpfc_drop_node(vport, ndlp);
+}
+
+static int
+lpfc_linkup(struct lpfc_hba *phba)
+{
+       struct lpfc_vport *vport;
+
+       phba->link_state = LPFC_LINK_UP;
+
+       /* Unblock fabric iocbs if they are blocked */
+       clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
+       del_timer_sync(&phba->fabric_block_timer);
+
+       list_for_each_entry(vport, &phba->port_list, listentry) {
+               lpfc_linkup_port(vport);
        }
+       if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
+               lpfc_issue_clear_la(phba, phba->pport);
 
        return 0;
 }
@@ -529,18 +683,28 @@ lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 
        /* Check for error */
        if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
-               /* CLEAR_LA mbox error <mbxStatus> state <port_state> */
+               /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
-                               "%d:0320 CLEAR_LA mbxStatus error x%x hba "
+                               "%d (%d):0320 CLEAR_LA mbxStatus error x%x hba "
                                "state x%x\n",
-                               phba->brd_no, mb->mbxStatus, vport->port_state);
+                               phba->brd_no, vport->vpi, mb->mbxStatus,
+                               vport->port_state);
 
                phba->link_state = LPFC_HBA_ERROR;
                goto out;
        }
 
-       if (vport->fc_flag & FC_ABORT_DISCOVERY)
-               goto out;
+       if (vport->port_type == LPFC_PHYSICAL_PORT)
+               phba->link_state = LPFC_HBA_READY;
+
+       spin_lock_irq(&phba->hbalock);
+       psli->sli_flag |= LPFC_PROCESS_LA;
+       control = readl(phba->HCregaddr);
+       control |= HC_LAINT_ENA;
+       writel(control, phba->HCregaddr);
+       readl(phba->HCregaddr); /* flush */
+       spin_unlock_irq(&phba->hbalock);
+       return;
 
        vport->num_disc_nodes = 0;
        /* go thru NPR nodes and issue ELS PLOGIs */
@@ -558,8 +722,8 @@ lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 out:
        /* Device Discovery completes */
        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                        "%d:0225 Device Discovery completes\n",
-                        phba->brd_no);
+                       "%d (%d):0225 Device Discovery completes\n",
+                       phba->brd_no, vport->vpi);
 
        mempool_free(pmb, phba->mbox_mem_pool);
 
@@ -589,8 +753,6 @@ static void
 lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 {
        struct lpfc_vport *vport = pmb->vport;
-       struct lpfc_sli *psli = &phba->sli;
-       int rc;
 
        if (pmb->mb.mbxStatus)
                goto out;
@@ -606,49 +768,40 @@ lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
                         */
                        lpfc_set_disctmo(vport);
                        return;
-               }
+       }
 
        /* Start discovery by sending a FLOGI. port_state is identically
         * LPFC_FLOGI while waiting for FLOGI cmpl
         */
-       vport->port_state = LPFC_FLOGI;
-       lpfc_set_disctmo(vport);
-       lpfc_initial_flogi(vport);
+       if (vport->port_state != LPFC_FLOGI) {
+               vport->port_state = LPFC_FLOGI;
+               lpfc_set_disctmo(vport);
+               lpfc_initial_flogi(vport);
+       }
        return;
 
 out:
        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
-                       "%d:0306 CONFIG_LINK mbxStatus error x%x "
+                       "%d (%d):0306 CONFIG_LINK mbxStatus error x%x "
                        "HBA state x%x\n",
-                       phba->brd_no, pmb->mb.mbxStatus, vport->port_state);
+                       phba->brd_no, vport->vpi, pmb->mb.mbxStatus,
+                       vport->port_state);
 
-       lpfc_linkdown(phba);
+       mempool_free(pmb, phba->mbox_mem_pool);
 
-       phba->link_state = LPFC_HBA_ERROR;
+       lpfc_linkdown(phba);
 
        lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
-                       "%d:0200 CONFIG_LINK bad hba state x%x\n",
-                       phba->brd_no, vport->port_state);
+                       "%d (%d):0200 CONFIG_LINK bad hba state x%x\n",
+                       phba->brd_no, vport->vpi, vport->port_state);
 
-       lpfc_clear_la(phba, pmb);
-       pmb->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
-       pmb->vport = vport;
-       rc = lpfc_sli_issue_mbox(phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
-       if (rc == MBX_NOT_FINISHED) {
-               mempool_free(pmb, phba->mbox_mem_pool);
-               lpfc_disc_flush_list(vport);
-               psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
-               psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
-               psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
-               vport->port_state = LPFC_VPORT_READY;
-       }
+       lpfc_issue_clear_la(phba, vport);
        return;
 }
 
 static void
 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 {
-       struct lpfc_sli *psli = &phba->sli;
        MAILBOX_t *mb = &pmb->mb;
        struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
        struct lpfc_vport  *vport = pmb->vport;
@@ -658,12 +811,12 @@ lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
        if (mb->mbxStatus) {
                /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
-                               "%d:0319 READ_SPARAM mbxStatus error x%x "
+                               "%d (%d):0319 READ_SPARAM mbxStatus error x%x "
                                "hba state x%x>\n",
-                               phba->brd_no, mb->mbxStatus, vport->port_state);
+                               phba->brd_no, vport->vpi, mb->mbxStatus,
+                               vport->port_state);
 
                lpfc_linkdown(phba);
-               phba->link_state = LPFC_HBA_ERROR;
                goto out;
        }
 
@@ -675,12 +828,15 @@ lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
        if (phba->cfg_soft_wwpn)
                u64_to_wwn(phba->cfg_soft_wwpn,
                           vport->fc_sparam.portName.u.wwn);
-       memcpy((uint8_t *) &vport->fc_nodename,
-              (uint8_t *) &vport->fc_sparam.nodeName,
-              sizeof (struct lpfc_name));
-       memcpy((uint8_t *) &vport->fc_portname,
-              (uint8_t *) &vport->fc_sparam.portName,
-              sizeof (struct lpfc_name));
+       memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
+              sizeof(vport->fc_nodename));
+       memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
+              sizeof(vport->fc_portname));
+       if (vport->port_type == LPFC_PHYSICAL_PORT) {
+               memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
+               memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
+       }
+
        lpfc_mbuf_free(phba, mp->virt, mp->phys);
        kfree(mp);
        mempool_free(pmb, phba->mbox_mem_pool);
@@ -690,35 +846,15 @@ out:
        pmb->context1 = NULL;
        lpfc_mbuf_free(phba, mp->virt, mp->phys);
        kfree(mp);
-       if (phba->link_state != LPFC_CLEAR_LA) {
-               struct lpfc_sli_ring *extra_ring =
-                       &psli->ring[psli->extra_ring];
-               struct lpfc_sli_ring *fcp_ring  = &psli->ring[psli->fcp_ring];
-               struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
-
-               lpfc_clear_la(phba, pmb);
-               pmb->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
-               pmb->vport = vport;
-               if (lpfc_sli_issue_mbox(phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB))
-                   == MBX_NOT_FINISHED) {
-                       mempool_free(pmb, phba->mbox_mem_pool);
-                       lpfc_disc_flush_list(vport);
-                       extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
-                       fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
-                       next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
-                       vport->port_state = LPFC_VPORT_READY;
-               }
-       } else {
-               mempool_free(pmb, phba->mbox_mem_pool);
-       }
+       lpfc_issue_clear_la(phba, vport);
+       mempool_free(pmb, phba->mbox_mem_pool);
        return;
 }
 
 static void
-lpfc_mbx_process_link_up(struct lpfc_vport *vport, READ_LA_VAR *la)
+lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
 {
-       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
-       struct lpfc_hba  *phba  = vport->phba;
+       struct lpfc_vport *vport = phba->pport;
        LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
        int i;
        struct lpfc_dmabuf *mp;
@@ -727,30 +863,32 @@ lpfc_mbx_process_link_up(struct lpfc_vport *vport, READ_LA_VAR *la)
        sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
        cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 
-       spin_lock_irq(shost->host_lock);
+       spin_lock_irq(&phba->hbalock);
        switch (la->UlnkSpeed) {
-               case LA_1GHZ_LINK:
-                       phba->fc_linkspeed = LA_1GHZ_LINK;
-                       break;
-               case LA_2GHZ_LINK:
-                       phba->fc_linkspeed = LA_2GHZ_LINK;
-                       break;
-               case LA_4GHZ_LINK:
-                       phba->fc_linkspeed = LA_4GHZ_LINK;
-                       break;
-               case LA_8GHZ_LINK:
-                       phba->fc_linkspeed = LA_8GHZ_LINK;
-                       break;
-               default:
-                       phba->fc_linkspeed = LA_UNKNW_LINK;
-                       break;
+       case LA_1GHZ_LINK:
+               phba->fc_linkspeed = LA_1GHZ_LINK;
+               break;
+       case LA_2GHZ_LINK:
+               phba->fc_linkspeed = LA_2GHZ_LINK;
+               break;
+       case LA_4GHZ_LINK:
+               phba->fc_linkspeed = LA_4GHZ_LINK;
+               break;
+       case LA_8GHZ_LINK:
+               phba->fc_linkspeed = LA_8GHZ_LINK;
+               break;
+       default:
+               phba->fc_linkspeed = LA_UNKNW_LINK;
+               break;
        }
 
        phba->fc_topology = la->topology;
+       phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
 
        if (phba->fc_topology == TOPOLOGY_LOOP) {
-       /* Get Loop Map information */
+               phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
 
+                               /* Get Loop Map information */
                if (la->il)
                        vport->fc_flag |= FC_LBIT;
 
@@ -784,30 +922,35 @@ lpfc_mbx_process_link_up(struct lpfc_vport *vport, READ_LA_VAR *la)
                                        }
                                        /* Link Up Event ALPA map */
                                        lpfc_printf_log(phba,
-                                               KERN_WARNING,
-                                               LOG_LINK_EVENT,
-                                               "%d:1304 Link Up Event "
-                                               "ALPA map Data: x%x "
-                                               "x%x x%x x%x\n",
-                                               phba->brd_no,
-                                               un.pa.wd1, un.pa.wd2,
-                                               un.pa.wd3, un.pa.wd4);
+                                                       KERN_WARNING,
+                                                       LOG_LINK_EVENT,
+                                                       "%d:1304 Link Up Event "
+                                                       "ALPA map Data: x%x "
+                                                       "x%x x%x x%x\n",
+                                                       phba->brd_no,
+                                                       un.pa.wd1, un.pa.wd2,
+                                                       un.pa.wd3, un.pa.wd4);
                                }
                        }
                }
        } else {
+               if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
+                       if (phba->max_vpi && lpfc_npiv_enable &&
+                          (phba->sli_rev == 3))
+                               phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
+               }
                vport->fc_myDID = phba->fc_pref_DID;
                vport->fc_flag |= FC_LBIT;
        }
-       spin_unlock_irq(shost->host_lock);
+       spin_unlock_irq(&phba->hbalock);
 
        lpfc_linkup(phba);
        if (sparam_mbox) {
-               lpfc_read_sparam(phba, 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 | MBX_STOP_IOCB));
+                                   (MBX_NOWAIT | MBX_STOP_IOCB));
                if (rc == MBX_NOT_FINISHED) {
                        mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
                        lpfc_mbuf_free(phba, mp->virt, mp->phys);
@@ -815,7 +958,7 @@ lpfc_mbx_process_link_up(struct lpfc_vport *vport, READ_LA_VAR *la)
                        mempool_free(sparam_mbox, phba->mbox_mem_pool);
                        if (cfglink_mbox)
                                mempool_free(cfglink_mbox, phba->mbox_mem_pool);
-                       return;
+                       goto out;
                }
        }
 
@@ -825,10 +968,20 @@ lpfc_mbx_process_link_up(struct lpfc_vport *vport, READ_LA_VAR *la)
                cfglink_mbox->vport = vport;
                cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
                rc = lpfc_sli_issue_mbox(phba, cfglink_mbox,
-                                               (MBX_NOWAIT | MBX_STOP_IOCB));
-               if (rc == MBX_NOT_FINISHED)
-                       mempool_free(cfglink_mbox, phba->mbox_mem_pool);
+                                   (MBX_NOWAIT | MBX_STOP_IOCB));
+               if (rc != MBX_NOT_FINISHED)
+                       return;
+               mempool_free(cfglink_mbox, phba->mbox_mem_pool);
        }
+out:
+       lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+       lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
+               "%d (%d):0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
+               phba->brd_no, vport->vpi,
+               vport->port_state, sparam_mbox, cfglink_mbox);
+
+       lpfc_issue_clear_la(phba, vport);
+       return;
 }
 
 static void
@@ -886,12 +1039,12 @@ lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
        spin_unlock_irq(shost->host_lock);
 
        if (((phba->fc_eventTag + 1) < la->eventTag) ||
-            (phba->fc_eventTag == la->eventTag)) {
+           (phba->fc_eventTag == la->eventTag)) {
                phba->fc_stat.LinkMultiEvent++;
                if (la->attType == AT_LINK_UP)
                        if (phba->fc_eventTag != 0)
                                lpfc_linkdown(phba);
-               }
+       }
 
        phba->fc_eventTag = la->eventTag;
 
@@ -912,7 +1065,7 @@ lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
                                la->granted_AL_PA, la->UlnkSpeed,
                                phba->alpa_map[0]);
                }
-               lpfc_mbx_process_link_up(vport, la);
+               lpfc_mbx_process_link_up(phba, la);
        } else {
                phba->fc_stat.LinkDown++;
                lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
@@ -940,7 +1093,7 @@ void
 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 {
        struct lpfc_vport  *vport = pmb->vport;
-       struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
+       struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
        struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
 
        pmb->context1 = NULL;
@@ -955,6 +1108,100 @@ lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
        return;
 }
 
+static void
+lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
+{
+       MAILBOX_t *mb = &pmb->mb;
+       struct lpfc_vport *vport = pmb->vport;
+       struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
+
+       switch (mb->mbxStatus) {
+       case 0x0011:
+       case 0x0020:
+       case 0x9700:
+               lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
+                               "%d (%d):0911 cmpl_unreg_vpi, "
+                               "mb status = 0x%x\n",
+                               phba->brd_no, vport->vpi, mb->mbxStatus);
+               break;
+       default:
+               phba->vpi_cnt--;
+       }
+       vport->unreg_vpi_cmpl = VPORT_OK;
+       mempool_free(pmb, phba->mbox_mem_pool);
+       /*
+        * This shost reference might have been taken at the beginning of
+        * lpfc_vport_delete()
+        */
+       if (vport->load_flag & FC_UNLOADING)
+               scsi_host_put(shost);
+}
+
+void
+lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
+{
+       struct lpfc_hba  *phba = vport->phba;
+       LPFC_MBOXQ_t *mbox;
+       int rc;
+
+       mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
+       if (!mbox)
+               return;
+
+       lpfc_unreg_vpi(phba, vport->vpi, mbox);
+       mbox->vport = vport;
+       mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
+       rc = lpfc_sli_issue_mbox(phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
+       if (rc == MBX_NOT_FINISHED) {
+               lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
+                               "%d (%d):1800 Could not issue unreg_vpi\n",
+                               phba->brd_no, vport->vpi);
+               mempool_free(mbox, phba->mbox_mem_pool);
+               vport->unreg_vpi_cmpl = VPORT_ERROR;
+       }
+}
+
+static void
+lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
+{
+       struct lpfc_vport *vport = pmb->vport;
+       struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
+       MAILBOX_t *mb = &pmb->mb;
+
+       switch (mb->mbxStatus) {
+       case 0x0011:
+       case 0x9601:
+       case 0x9602:
+               lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
+                               "%d (%d):0912 cmpl_reg_vpi, mb status = 0x%x\n",
+                               phba->brd_no, vport->vpi, mb->mbxStatus);
+               lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+               spin_lock_irq(shost->host_lock);
+               vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
+               spin_unlock_irq(shost->host_lock);
+               vport->fc_myDID = 0;
+               goto out;
+       }
+       phba->vpi_cnt++;
+
+       vport->num_disc_nodes = 0;
+       /* go thru NPR list and issue ELS PLOGIs */
+       if (vport->fc_npr_cnt)
+               lpfc_els_disc_plogi(vport);
+
+       if (!vport->num_disc_nodes) {
+               spin_lock_irq(shost->host_lock);
+               vport->fc_flag &= ~FC_NDISC_ACTIVE;
+               spin_unlock_irq(shost->host_lock);
+               lpfc_can_disctmo(vport);
+       }
+       vport->port_state = LPFC_VPORT_READY;
+
+out:
+       mempool_free(pmb, phba->mbox_mem_pool);
+       return;
+}
+
 /*
  * This routine handles processing a Fabric REG_LOGIN mailbox
  * command upon completion. It is setup in the LPFC_MBOXQ
@@ -964,10 +1211,11 @@ lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 void
 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 {
-       struct lpfc_vport  *vport = pmb->vport;
+       struct lpfc_vport *vport = pmb->vport;
+       struct lpfc_vport *next_vport;
        MAILBOX_t *mb = &pmb->mb;
        struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
-       struct lpfc_nodelist *ndlp, *ndlp_fdmi;
+       struct lpfc_nodelist *ndlp;
        ndlp = (struct lpfc_nodelist *) pmb->context2;
 
        pmb->context1 = NULL;
@@ -979,11 +1227,20 @@ lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
                mempool_free(pmb, phba->mbox_mem_pool);
                lpfc_nlp_put(ndlp);
 
-               /* FLOGI failed, so just use loop map to make discovery list */
-               lpfc_disc_list_loopmap(vport);
+               if (phba->fc_topology == TOPOLOGY_LOOP) {
+                       /* FLOGI failed, use loop map to make discovery list */
+                       lpfc_disc_list_loopmap(vport);
+
+                       /* Start discovery */
+                       lpfc_disc_start(vport);
+                       return;
+               }
+
+               lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+               lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
+                       "%d (%d):0258 Register Fabric login error: 0x%x\n",
+                       phba->brd_no, vport->vpi, mb->mbxStatus);
 
-               /* Start discovery */
-               lpfc_disc_start(vport);
                return;
        }
 
@@ -994,47 +1251,25 @@ lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
        lpfc_nlp_put(ndlp);     /* Drop the reference from the mbox */
 
        if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
-               /* This NPort has been assigned an NPort_ID by the fabric as a
-                * result of the completed fabric login.  Issue a State Change
-                * Registration (SCR) ELS request to the fabric controller
-                * (SCR_DID) so that this NPort gets RSCN events from the
-                * fabric.
-                */
-               lpfc_issue_els_scr(vport, SCR_DID, 0);
-
-               ndlp = lpfc_findnode_did(vport, NameServer_DID);
-               if (!ndlp) {
-                       /* Allocate a new node instance.  If the pool is empty,
-                        * start the discovery process and skip the Nameserver
-                        * login process.  This is attempted again later on.
-                        * Otherwise, issue a Port Login (PLOGI) to
-                        * the NameServer
-                        */
-                       ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
-                       if (!ndlp) {
-                               lpfc_disc_start(vport);
-                               lpfc_mbuf_free(phba, mp->virt, mp->phys);
-                               kfree(mp);
-                               mempool_free(pmb, phba->mbox_mem_pool);
-                               return;
-                       } else {
-                               lpfc_nlp_init(vport, ndlp, NameServer_DID);
-                               ndlp->nlp_type |= NLP_FABRIC;
-                       }
-               }
+               list_for_each_entry(next_vport, &phba->port_list, listentry) {
+                       if (next_vport->port_type == LPFC_PHYSICAL_PORT)
+                               continue;
 
-               lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
-               lpfc_issue_els_plogi(vport, NameServer_DID, 0);
-               if (phba->cfg_fdmi_on) {
-                       ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
-                                                 GFP_KERNEL);
-                       if (ndlp_fdmi) {
-                               lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
-                               ndlp_fdmi->nlp_type |= NLP_FABRIC;
-                               ndlp_fdmi->nlp_state = NLP_STE_PLOGI_ISSUE;
-                               lpfc_issue_els_plogi(vport, FDMI_DID, 0);
+                       if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
+                               lpfc_initial_fdisc(next_vport);
+                       else {
+                               if (phba->sli3_options &
+                                       LPFC_SLI3_NPIV_ENABLED) {
+                                       lpfc_vport_set_state(vport,
+                                               FC_VPORT_NO_FABRIC_SUPP);
+                                       lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                                               "%d (%d):0259 No NPIV Fabric "
+                                               "support\n",
+                                               phba->brd_no, vport->vpi);
+                               }
                        }
                }
+               lpfc_do_scr_ns_plogi(phba, vport);
        }
 
        lpfc_mbuf_free(phba, mp->virt, mp->phys);
@@ -1058,20 +1293,28 @@ lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
        struct lpfc_vport *vport = pmb->vport;
 
        if (mb->mbxStatus) {
+out:
                lpfc_nlp_put(ndlp);
                lpfc_mbuf_free(phba, mp->virt, mp->phys);
                kfree(mp);
                mempool_free(pmb, phba->mbox_mem_pool);
                lpfc_drop_node(vport, ndlp);
 
-               /*
-                * RegLogin failed, so just use loop map to make discovery
-                * list
-                */
-               lpfc_disc_list_loopmap(vport);
+               if (phba->fc_topology == TOPOLOGY_LOOP) {
+                       /*
+                        * RegLogin failed, use loop map to make discovery
+                        * list
+                        */
+                       lpfc_disc_list_loopmap(vport);
 
-               /* Start discovery */
-               lpfc_disc_start(vport);
+                       /* Start discovery */
+                       lpfc_disc_start(vport);
+                       return;
+               }
+               lpfc_vport_set_state(vport, FC_VPORT_FAILED);
+               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
+                       "%d (%d):0260 Register NameServer error: 0x%x\n",
+                       phba->brd_no, vport->vpi, mb->mbxStatus);
                return;
        }
 
@@ -1083,17 +1326,21 @@ lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 
        if (vport->port_state < LPFC_VPORT_READY) {
                /* Link up discovery requires Fabric registration. */
-               lpfc_ns_cmd(vport, ndlp, SLI_CTNS_RNN_ID);
-               lpfc_ns_cmd(vport, ndlp, SLI_CTNS_RSNN_NN);
-               lpfc_ns_cmd(vport, ndlp, SLI_CTNS_RFT_ID);
-               lpfc_ns_cmd(vport, ndlp, SLI_CTNS_RFF_ID);
+               lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
+               lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
+               lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
+               lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
+               lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
+
+               /* Issue SCR just before NameServer GID_FT Query */
+               lpfc_issue_els_scr(vport, SCR_DID, 0);
        }
 
        vport->fc_ns_retry = 0;
        /* Good status, issue CT Request to NameServer */
-       if (lpfc_ns_cmd(vport, ndlp, SLI_CTNS_GID_FT)) {
+       if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
                /* Cannot issue NameServer Query, so finish up discovery */
-               lpfc_disc_start(vport);
+               goto out;
        }
 
        lpfc_nlp_put(ndlp);
@@ -1127,7 +1374,7 @@ lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
         * registered the port.
         */
        if (ndlp->rport && ndlp->rport->dd_data &&
-           *(struct lpfc_rport_data **) ndlp->rport->dd_data) {
+           ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp) {
                lpfc_nlp_put(ndlp);
        }
        ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
@@ -1147,16 +1394,16 @@ lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
                rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
        if (ndlp->nlp_type & NLP_FCP_INITIATOR)
                rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
+       del_timer_sync(&ndlp->nlp_initiator_tmr);
 
 
        if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
                fc_remote_port_rolechg(rport, rport_ids.roles);
 
        if ((rport->scsi_target_id != -1) &&
-               (rport->scsi_target_id < LPFC_MAX_TARGET)) {
+           (rport->scsi_target_id < LPFC_MAX_TARGET)) {
                ndlp->nlp_sid = rport->scsi_target_id;
        }
-
        return;
 }
 
@@ -1164,14 +1411,6 @@ static void
 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
 {
        struct fc_rport *rport = ndlp->rport;
-       struct lpfc_rport_data *rdata = rport->dd_data;
-
-       if (rport->scsi_target_id == -1) {
-               ndlp->rport = NULL;
-               rdata->pnode = NULL;
-               lpfc_nlp_put(ndlp);
-               put_device(&rport->dev);
-       }
 
        fc_remote_port_delete(rport);
 
@@ -1377,9 +1616,9 @@ lpfc_set_disctmo(struct lpfc_vport *vport)
 
        /* Start Discovery Timer state <hba_state> */
        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d:0247 Start Discovery Timer state x%x "
+                       "%d (%d):0247 Start Discovery Timer state x%x "
                        "Data: x%x x%lx x%x x%x\n",
-                       phba->brd_no, vport->port_state, tmo,
+                       phba->brd_no, vport->vpi, vport->port_state, tmo,
                        (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
                        vport->fc_adisc_cnt);
 
@@ -1409,10 +1648,11 @@ lpfc_can_disctmo(struct lpfc_vport *vport)
 
        /* Cancel Discovery Timer state <hba_state> */
        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d:0248 Cancel Discovery Timer state x%x "
+                       "%d (%d):0248 Cancel Discovery Timer state x%x "
                        "Data: x%x x%x x%x\n",
-                       phba->brd_no, vport->port_state, vport->fc_flag,
-                       vport->fc_plogi_cnt, vport->fc_adisc_cnt);
+                       phba->brd_no, vport->vpi, vport->port_state,
+                       vport->fc_flag, vport->fc_plogi_cnt,
+                       vport->fc_adisc_cnt);
 
        return 0;
 }
@@ -1429,6 +1669,11 @@ lpfc_check_sli_ndlp(struct lpfc_hba *phba,
 {
        struct lpfc_sli *psli = &phba->sli;
        IOCB_t *icmd = &iocb->iocb;
+       struct lpfc_vport    *vport = ndlp->vport;
+
+       if (iocb->vport != vport)
+               return 0;
+
        if (pring->ringno == LPFC_ELS_RING) {
                switch (icmd->ulpCommand) {
                case CMD_GEN_REQUEST64_CR:
@@ -1446,7 +1691,7 @@ lpfc_check_sli_ndlp(struct lpfc_hba *phba,
        } else if (pring->ringno == psli->fcp_ring) {
                /* Skip match check if waiting to relogin to FCP target */
                if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
-                 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
+                   (ndlp->nlp_flag & NLP_DELAY_TMO)) {
                        return 0;
                }
                if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
@@ -1472,6 +1717,8 @@ lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
        IOCB_t *icmd;
        uint32_t rpi, i;
 
+       lpfc_fabric_abort_nport(ndlp);
+
        /*
         * Everything that matches on txcmplq will be returned
         * by firmware with a no rpi error.
@@ -1490,8 +1737,8 @@ lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
                                 * Check to see if iocb matches the nport we are
                                 * looking for
                                 */
-                               if ((lpfc_check_sli_ndlp
-                                    (phba, pring, iocb, ndlp))) {
+                               if ((lpfc_check_sli_ndlp(phba, pring, iocb,
+                                                        ndlp))) {
                                        /* It matches, so deque and call compl
                                           with an error */
                                        list_move_tail(&iocb->list,
@@ -1505,7 +1752,7 @@ lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
 
        while (!list_empty(&completions)) {
                iocb = list_get_first(&completions, struct lpfc_iocbq, list);
-               list_del(&iocb->list);
+               list_del_init(&iocb->list);
 
                if (!iocb->iocb_cmpl)
                        lpfc_sli_release_iocbq(phba, iocb);
@@ -1539,11 +1786,11 @@ lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
        if (ndlp->nlp_rpi) {
                mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
                if (mbox) {
-                       lpfc_unreg_login(phba, ndlp->nlp_rpi, mbox);
+                       lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
                        mbox->vport = vport;
-                       mbox->mbox_cmpl=lpfc_sli_def_mbox_cmpl;
-                       rc = lpfc_sli_issue_mbox
-                                   (phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
+                       mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
+                       rc = lpfc_sli_issue_mbox(phba, mbox,
+                                                (MBX_NOWAIT | MBX_STOP_IOCB));
                        if (rc == MBX_NOT_FINISHED)
                                mempool_free(mbox, phba->mbox_mem_pool);
                }
@@ -1554,6 +1801,50 @@ lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
        return 0;
 }
 
+void
+lpfc_unreg_all_rpis(struct lpfc_vport *vport)
+{
+       struct lpfc_hba  *phba  = vport->phba;
+       LPFC_MBOXQ_t     *mbox;
+       int rc;
+
+       mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
+       if (mbox) {
+               lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
+               mbox->vport = vport;
+               mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
+               rc = lpfc_sli_issue_mbox(phba, mbox,
+                                        (MBX_NOWAIT | MBX_STOP_IOCB));
+               if (rc == MBX_NOT_FINISHED) {
+                       mempool_free(mbox, phba->mbox_mem_pool);
+               }
+       }
+}
+
+void
+lpfc_unreg_default_rpis(struct lpfc_vport *vport)
+{
+       struct lpfc_hba  *phba  = vport->phba;
+       LPFC_MBOXQ_t     *mbox;
+       int rc;
+
+       mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
+       if (mbox) {
+               lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
+               mbox->vport = vport;
+               mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
+               rc = lpfc_sli_issue_mbox(phba, mbox,
+                                        (MBX_NOWAIT | MBX_STOP_IOCB));
+               if (rc == MBX_NOT_FINISHED) {
+                       lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
+                                       "%d (%d):1815 Could not issue "
+                                       "unreg_did (default rpis)\n",
+                                       phba->brd_no, vport->vpi);
+                       mempool_free(mbox, phba->mbox_mem_pool);
+               }
+       }
+}
+
 /*
  * Free resources associated with LPFC_NODELIST entry
  * so it can be freed.
@@ -1568,9 +1859,9 @@ lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
 
        /* Cleanup node for NPort <nlp_DID> */
        lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
-                       "%d:0900 Cleanup node for NPort x%x "
+                       "%d (%d):0900 Cleanup node for NPort x%x "
                        "Data: x%x x%x x%x\n",
-                       phba->brd_no, ndlp->nlp_DID, ndlp->nlp_flag,
+                       phba->brd_no, vport->vpi, ndlp->nlp_DID, ndlp->nlp_flag,
                        ndlp->nlp_state, ndlp->nlp_rpi);
 
        lpfc_dequeue_node(vport, ndlp);
@@ -1587,7 +1878,7 @@ lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
        spin_lock_irq(&phba->hbalock);
        list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
                if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
-                  (ndlp == (struct lpfc_nodelist *) mb->context2)) {
+                   (ndlp == (struct lpfc_nodelist *) mb->context2)) {
                        mp = (struct lpfc_dmabuf *) (mb->context1);
                        if (mp) {
                                __lpfc_mbuf_free(phba, mp->virt, mp->phys);
@@ -1607,9 +1898,12 @@ lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
 
        ndlp->nlp_last_elscmd = 0;
        del_timer_sync(&ndlp->nlp_delayfunc);
+       del_timer_sync(&ndlp->nlp_initiator_tmr);
 
        if (!list_empty(&ndlp->els_retry_evt.evt_listp))
                list_del_init(&ndlp->els_retry_evt.evt_listp);
+       if (!list_empty(&ndlp->dev_loss_evt.evt_listp))
+               list_del_init(&ndlp->dev_loss_evt.evt_listp);
 
        lpfc_unreg_rpi(vport, ndlp);
 
@@ -1633,12 +1927,11 @@ lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
        lpfc_cleanup_node(vport, ndlp);
 
        /*
-        * We should never get here with a non-NULL ndlp->rport.  But
-        * if we do, drop the reference to the rport.  That seems the
-        * intelligent thing to do.
+        * We can get here with a non-NULL ndlp->rport because when we
+        * unregister a rport we don't break the rport/node linkage.  So if we
+        * do, make sure we don't leaving any dangling pointers behind.
         */
-       if (ndlp->rport && !(vport->load_flag & FC_UNLOADING)) {
-               put_device(&ndlp->rport->dev);
+       if (ndlp->rport) {
                rdata = ndlp->rport->dd_data;
                rdata->pnode = NULL;
                ndlp->rport = NULL;
@@ -1709,9 +2002,9 @@ __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
                                 ((uint32_t) ndlp->nlp_type << 8) |
                                 ((uint32_t) ndlp->nlp_rpi & 0xff));
                        lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
-                                       "%d:0929 FIND node DID "
+                                       "%d (%d):0929 FIND node DID "
                                        " Data: x%p x%x x%x x%x\n",
-                                       phba->brd_no,
+                                       phba->brd_no, vport->vpi,
                                        ndlp, ndlp->nlp_DID,
                                        ndlp->nlp_flag, data1);
                        return ndlp;
@@ -1720,8 +2013,8 @@ __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
 
        /* FIND node did <did> NOT FOUND */
        lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
-                       "%d:0932 FIND node did x%x NOT FOUND.\n",
-                       phba->brd_no, did);
+                       "%d (%d):0932 FIND node did x%x NOT FOUND.\n",
+                       phba->brd_no, vport->vpi, did);
        return NULL;
 }
 
@@ -1835,6 +2128,14 @@ lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
        struct lpfc_sli_ring *next_ring  = &psli->ring[psli->next_ring];
        int  rc;
 
+       /*
+        * if it's not a physical port or if we already send
+        * clear_la then don't send it.
+        */
+       if ((phba->link_state >= LPFC_CLEAR_LA) ||
+           (vport->port_type != LPFC_PHYSICAL_PORT))
+               return;
+
                        /* Link up discovery */
        if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
                phba->link_state = LPFC_CLEAR_LA;
@@ -1849,7 +2150,26 @@ lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
                        extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
                        fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
                        next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
-                       vport->port_state = LPFC_VPORT_READY;
+                       phba->link_state = LPFC_HBA_ERROR;
+               }
+       }
+}
+
+/* Reg_vpi to tell firmware to resume normal operations */
+void
+lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
+{
+       LPFC_MBOXQ_t *regvpimbox;
+
+       regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
+       if (regvpimbox) {
+               lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
+               regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
+               regvpimbox->vport = vport;
+               if (lpfc_sli_issue_mbox(phba, regvpimbox,
+                                       (MBX_NOWAIT | MBX_STOP_IOCB))
+                                       == MBX_NOT_FINISHED) {
+                       mempool_free(regvpimbox, phba->mbox_mem_pool);
                }
        }
 }
@@ -1860,7 +2180,6 @@ lpfc_disc_start(struct lpfc_vport *vport)
 {
        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
        struct lpfc_hba  *phba = vport->phba;
-       struct lpfc_nodelist *ndlp, *next_ndlp;
        uint32_t num_sent;
        uint32_t clear_la_pending;
        int did_changed;
@@ -1888,21 +2207,11 @@ lpfc_disc_start(struct lpfc_vport *vport)
 
        /* Start Discovery state <hba_state> */
        lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d:0202 Start Discovery hba state x%x "
+                       "%d (%d):0202 Start Discovery hba state x%x "
                        "Data: x%x x%x x%x\n",
-                       phba->brd_no, vport->port_state, vport->fc_flag,
-                       vport->fc_plogi_cnt, vport->fc_adisc_cnt);
-
-       /* If our did changed, we MUST do PLOGI */
-       list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
-               if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
-                   (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
-                   did_changed) {
-                       spin_lock_irq(shost->host_lock);
-                       ndlp->nlp_flag &= ~NLP_NPR_ADISC;
-                       spin_unlock_irq(shost->host_lock);
-               }
-       }
+                       phba->brd_no, vport->vpi, vport->port_state,
+                       vport->fc_flag, vport->fc_plogi_cnt,
+                       vport->fc_adisc_cnt);
 
        /* First do ADISCs - if any */
        num_sent = lpfc_els_disc_adisc(vport);
@@ -1910,12 +2219,26 @@ lpfc_disc_start(struct lpfc_vport *vport)
        if (num_sent)
                return;
 
+       /*
+        * For SLI3, cmpl_reg_vpi will set port_state to READY, and
+        * continue discovery.
+        */
+       if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
+           !(vport->fc_flag & FC_RSCN_MODE)) {
+               lpfc_issue_reg_vpi(phba, vport);
+               return;
+       }
+
+       /*
+        * For SLI2, we need to set port_state to READY and continue
+        * discovery.
+        */
        if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
-               if (vport->port_type == LPFC_PHYSICAL_PORT) {
                /* If we get here, there is nothing to ADISC */
+               if (vport->port_type == LPFC_PHYSICAL_PORT)
                        lpfc_issue_clear_la(phba, vport);
-               } else if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
 
+               if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
                        vport->num_disc_nodes = 0;
                        /* go thru NPR nodes and issue ELS PLOGIs */
                        if (vport->fc_npr_cnt)
@@ -1925,9 +2248,10 @@ lpfc_disc_start(struct lpfc_vport *vport)
                                spin_lock_irq(shost->host_lock);
                                vport->fc_flag &= ~FC_NDISC_ACTIVE;
                                spin_unlock_irq(shost->host_lock);
+                               lpfc_can_disctmo(vport);
                        }
-                       vport->port_state = LPFC_VPORT_READY;
                }
+               vport->port_state = LPFC_VPORT_READY;
        } else {
                /* Next do PLOGIs - if any */
                num_sent = lpfc_els_disc_plogi(vport);
@@ -1944,6 +2268,7 @@ lpfc_disc_start(struct lpfc_vport *vport)
                                spin_lock_irq(shost->host_lock);
                                vport->fc_flag &= ~FC_RSCN_MODE;
            &n