[SCSI] lpfc 8.3.20: Updates to FC discovery commands
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_nportdisc.c
index 50a2476..d85a742 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-2007 Emulex.  All rights reserved.           *
+ * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
  * EMULEX and SLI are trademarks of Emulex.                        *
  * www.emulex.com                                                  *
  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
@@ -21,6 +21,7 @@
 
 #include <linux/blkdev.h>
 #include <linux/pci.h>
+#include <linux/slab.h>
 #include <linux/interrupt.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_host.h>
 #include <scsi/scsi_transport_fc.h>
 
+#include "lpfc_hw4.h"
 #include "lpfc_hw.h"
 #include "lpfc_sli.h"
+#include "lpfc_sli4.h"
+#include "lpfc_nl.h"
 #include "lpfc_disc.h"
 #include "lpfc_scsi.h"
 #include "lpfc.h"
 #include "lpfc_logmsg.h"
 #include "lpfc_crtn.h"
 #include "lpfc_vport.h"
+#include "lpfc_debugfs.h"
 
 
 /* Called to verify a rcv'ed ADISC was intended for us. */
@@ -58,7 +63,7 @@ lpfc_check_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 
 int
 lpfc_check_sparm(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
-                struct serv_parm * sp, uint32_t class)
+                struct serv_parm *sp, uint32_t class, int flogi)
 {
        volatile struct serv_parm *hsp = &vport->fc_sparam;
        uint16_t hsp_value, ssp_value = 0;
@@ -71,49 +76,56 @@ lpfc_check_sparm(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
         * correcting the byte values.
         */
        if (sp->cls1.classValid) {
-               hsp_value = (hsp->cls1.rcvDataSizeMsb << 8) |
-                               hsp->cls1.rcvDataSizeLsb;
-               ssp_value = (sp->cls1.rcvDataSizeMsb << 8) |
-                               sp->cls1.rcvDataSizeLsb;
-               if (!ssp_value)
-                       goto bad_service_param;
-               if (ssp_value > hsp_value) {
-                       sp->cls1.rcvDataSizeLsb = hsp->cls1.rcvDataSizeLsb;
-                       sp->cls1.rcvDataSizeMsb = hsp->cls1.rcvDataSizeMsb;
+               if (!flogi) {
+                       hsp_value = ((hsp->cls1.rcvDataSizeMsb << 8) |
+                                    hsp->cls1.rcvDataSizeLsb);
+                       ssp_value = ((sp->cls1.rcvDataSizeMsb << 8) |
+                                    sp->cls1.rcvDataSizeLsb);
+                       if (!ssp_value)
+                               goto bad_service_param;
+                       if (ssp_value > hsp_value) {
+                               sp->cls1.rcvDataSizeLsb =
+                                       hsp->cls1.rcvDataSizeLsb;
+                               sp->cls1.rcvDataSizeMsb =
+                                       hsp->cls1.rcvDataSizeMsb;
+                       }
                }
-       } else if (class == CLASS1) {
+       } else if (class == CLASS1)
                goto bad_service_param;
-       }
-
        if (sp->cls2.classValid) {
-               hsp_value = (hsp->cls2.rcvDataSizeMsb << 8) |
-                               hsp->cls2.rcvDataSizeLsb;
-               ssp_value = (sp->cls2.rcvDataSizeMsb << 8) |
-                               sp->cls2.rcvDataSizeLsb;
-               if (!ssp_value)
-                       goto bad_service_param;
-               if (ssp_value > hsp_value) {
-                       sp->cls2.rcvDataSizeLsb = hsp->cls2.rcvDataSizeLsb;
-                       sp->cls2.rcvDataSizeMsb = hsp->cls2.rcvDataSizeMsb;
+               if (!flogi) {
+                       hsp_value = ((hsp->cls2.rcvDataSizeMsb << 8) |
+                                    hsp->cls2.rcvDataSizeLsb);
+                       ssp_value = ((sp->cls2.rcvDataSizeMsb << 8) |
+                                    sp->cls2.rcvDataSizeLsb);
+                       if (!ssp_value)
+                               goto bad_service_param;
+                       if (ssp_value > hsp_value) {
+                               sp->cls2.rcvDataSizeLsb =
+                                       hsp->cls2.rcvDataSizeLsb;
+                               sp->cls2.rcvDataSizeMsb =
+                                       hsp->cls2.rcvDataSizeMsb;
+                       }
                }
-       } else if (class == CLASS2) {
+       } else if (class == CLASS2)
                goto bad_service_param;
-       }
-
        if (sp->cls3.classValid) {
-               hsp_value = (hsp->cls3.rcvDataSizeMsb << 8) |
-                               hsp->cls3.rcvDataSizeLsb;
-               ssp_value = (sp->cls3.rcvDataSizeMsb << 8) |
-                               sp->cls3.rcvDataSizeLsb;
-               if (!ssp_value)
-                       goto bad_service_param;
-               if (ssp_value > hsp_value) {
-                       sp->cls3.rcvDataSizeLsb = hsp->cls3.rcvDataSizeLsb;
-                       sp->cls3.rcvDataSizeMsb = hsp->cls3.rcvDataSizeMsb;
+               if (!flogi) {
+                       hsp_value = ((hsp->cls3.rcvDataSizeMsb << 8) |
+                                    hsp->cls3.rcvDataSizeLsb);
+                       ssp_value = ((sp->cls3.rcvDataSizeMsb << 8) |
+                                    sp->cls3.rcvDataSizeLsb);
+                       if (!ssp_value)
+                               goto bad_service_param;
+                       if (ssp_value > hsp_value) {
+                               sp->cls3.rcvDataSizeLsb =
+                                       hsp->cls3.rcvDataSizeLsb;
+                               sp->cls3.rcvDataSizeMsb =
+                                       hsp->cls3.rcvDataSizeMsb;
+                       }
                }
-       } else if (class == CLASS3) {
+       } else if (class == CLASS3)
                goto bad_service_param;
-       }
 
        /*
         * Preserve the upper four bits of the MSB from the PLOGI response.
@@ -132,15 +144,15 @@ lpfc_check_sparm(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        memcpy(&ndlp->nlp_portname, &sp->portName, sizeof (struct lpfc_name));
        return 1;
 bad_service_param:
-       lpfc_printf_log(vport->phba, KERN_ERR, LOG_DISCOVERY,
-                       "%d (%d):0207 Device %x "
-                       "(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x) sent "
-                       "invalid service parameters.  Ignoring device.\n",
-                       vport->phba->brd_no, ndlp->vport->vpi, ndlp->nlp_DID,
-                       sp->nodeName.u.wwn[0], sp->nodeName.u.wwn[1],
-                       sp->nodeName.u.wwn[2], sp->nodeName.u.wwn[3],
-                       sp->nodeName.u.wwn[4], sp->nodeName.u.wwn[5],
-                       sp->nodeName.u.wwn[6], sp->nodeName.u.wwn[7]);
+       lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
+                        "0207 Device %x "
+                        "(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x) sent "
+                        "invalid service parameters.  Ignoring device.\n",
+                        ndlp->nlp_DID,
+                        sp->nodeName.u.wwn[0], sp->nodeName.u.wwn[1],
+                        sp->nodeName.u.wwn[2], sp->nodeName.u.wwn[3],
+                        sp->nodeName.u.wwn[4], sp->nodeName.u.wwn[5],
+                        sp->nodeName.u.wwn[6], sp->nodeName.u.wwn[7]);
        return 0;
 }
 
@@ -178,6 +190,7 @@ lpfc_check_elscmpl_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 }
 
 
+
 /*
  * Free resources / clean up outstanding I/Os
  * associated with a LPFC_NODELIST entry. This
@@ -187,60 +200,57 @@ int
 lpfc_els_abort(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
 {
        LIST_HEAD(completions);
+       LIST_HEAD(txcmplq_completions);
+       LIST_HEAD(abort_list);
        struct lpfc_sli  *psli = &phba->sli;
        struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
        struct lpfc_iocbq *iocb, *next_iocb;
-       IOCB_t *cmd;
 
        /* Abort outstanding I/O on NPort <nlp_DID> */
-       lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d (%d):0205 Abort outstanding I/O on NPort x%x "
-                       "Data: x%x x%x x%x\n",
-                       phba->brd_no, ndlp->vport->vpi, ndlp->nlp_DID,
-                       ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
+       lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_DISCOVERY,
+                        "2819 Abort outstanding I/O on NPort x%x "
+                        "Data: x%x x%x x%x\n",
+                        ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
+                        ndlp->nlp_rpi);
 
        lpfc_fabric_abort_nport(ndlp);
 
        /* First check the txq */
        spin_lock_irq(&phba->hbalock);
        list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
-               /* Check to see if iocb matches the nport we are looking
-                  for */
+               /* Check to see if iocb matches the nport we are looking for */
                if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp)) {
-                       /* It matches, so deque and call compl with an
-                          error */
+                       /* It matches, so deque and call compl with anp error */
                        list_move_tail(&iocb->list, &completions);
                        pring->txq_cnt--;
                }
        }
 
        /* Next check the txcmplq */
-       list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
-               /* Check to see if iocb matches the nport we are looking
-                  for */
-               if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp)) {
-                       lpfc_sli_issue_abort_iotag(phba, pring, iocb);
-               }
+       list_splice_init(&pring->txcmplq, &txcmplq_completions);
+       spin_unlock_irq(&phba->hbalock);
+
+       list_for_each_entry_safe(iocb, next_iocb, &txcmplq_completions, list) {
+               /* Check to see if iocb matches the nport we are looking for */
+               if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
+                       list_add_tail(&iocb->dlist, &abort_list);
        }
+       spin_lock_irq(&phba->hbalock);
+       list_splice(&txcmplq_completions, &pring->txcmplq);
        spin_unlock_irq(&phba->hbalock);
 
-       while (!list_empty(&completions)) {
-               iocb = list_get_first(&completions, struct lpfc_iocbq, list);
-               cmd = &iocb->iocb;
-               list_del_init(&iocb->list);
-
-               if (!iocb->iocb_cmpl)
-                       lpfc_sli_release_iocbq(phba, iocb);
-               else {
-                       cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
-                       cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
-                       (iocb->iocb_cmpl) (phba, iocb, iocb);
-               }
+       list_for_each_entry_safe(iocb, next_iocb, &abort_list, dlist) {
+                       spin_lock_irq(&phba->hbalock);
+                       list_del_init(&iocb->dlist);
+                       lpfc_sli_issue_abort_iotag(phba, pring, iocb);
+                       spin_unlock_irq(&phba->hbalock);
        }
 
-       /* If we are delaying issuing an ELS command, cancel it */
-       if (ndlp->nlp_flag & NLP_DELAY_TMO)
-               lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
+       /* Cancel all the IOCBs from the completions list */
+       lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
+                             IOERR_SLI_ABORTED);
+
+       lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
        return 0;
 }
 
@@ -259,7 +269,7 @@ lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        int rc;
 
        memset(&stat, 0, sizeof (struct ls_rjt));
-       if (vport->port_state <= LPFC_FLOGI) {
+       if (vport->port_state <= LPFC_FDISC) {
                /* Before responding to PLOGI, check for pt2pt mode.
                 * If we are pt2pt, with an outstanding FLOGI, abort
                 * the FLOGI and resend it first.
@@ -282,30 +292,48 @@ lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                        stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
                        stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
                        lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
-                                           ndlp);
+                                           ndlp, NULL);
                        return 0;
                }
        }
        pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
        lp = (uint32_t *) pcmd->virt;
        sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
-       if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3) == 0)) {
+       if (wwn_to_u64(sp->portName.u.wwn) == 0) {
+               lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
+                                "0140 PLOGI Reject: invalid nname\n");
+               stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
+               stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_PNAME;
+               lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
+                       NULL);
+               return 0;
+       }
+       if (wwn_to_u64(sp->nodeName.u.wwn) == 0) {
+               lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
+                                "0141 PLOGI Reject: invalid pname\n");
+               stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
+               stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_NNAME;
+               lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
+                       NULL);
+               return 0;
+       }
+       if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0) == 0)) {
                /* Reject this request because invalid parameters */
                stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
                stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
-               lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp);
+               lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
+                       NULL);
                return 0;
        }
        icmd = &cmdiocb->iocb;
 
        /* PLOGI chkparm OK */
-       lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d (%d):0114 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
-                       phba->brd_no, vport->vpi,
-                       ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
-                       ndlp->nlp_rpi);
+       lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
+                        "0114 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
+                        ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
+                        ndlp->nlp_rpi);
 
-       if (phba->cfg_fcp_class == 2 && sp->cls2.classValid)
+       if (vport->cfg_fcp_class == 2 && sp->cls2.classValid)
                ndlp->nlp_fcp_info |= CLASS2;
        else
                ndlp->nlp_fcp_info |= CLASS3;
@@ -331,7 +359,7 @@ lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        case  NLP_STE_PRLI_ISSUE:
        case  NLP_STE_UNMAPPED_NODE:
        case  NLP_STE_MAPPED_NODE:
-               lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL, 0);
+               lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
                return 1;
        }
 
@@ -345,8 +373,7 @@ lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                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));
+               rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
                if (rc == MBX_NOT_FINISHED) {
                        mempool_free(mbox, phba->mbox_mem_pool);
                        goto out;
@@ -358,8 +385,8 @@ lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        if (!mbox)
                goto out;
 
-       rc = lpfc_reg_login(phba, vport->vpi, icmd->un.rcvels.remoteID,
-                           (uint8_t *) sp, mbox, 0);
+       rc = lpfc_reg_rpi(phba, vport->vpi, icmd->un.rcvels.remoteID,
+                           (uint8_t *) sp, mbox, ndlp->nlp_rpi);
        if (rc) {
                mempool_free(mbox, phba->mbox_mem_pool);
                goto out;
@@ -392,13 +419,29 @@ lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                lpfc_els_abort(phba, ndlp);
        }
 
-       lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, mbox, 0);
-       return 1;
+       if ((vport->port_type == LPFC_NPIV_PORT &&
+            vport->cfg_restrict_login)) {
 
+               /* In order to preserve RPIs, we want to cleanup
+                * the default RPI the firmware created to rcv
+                * this ELS request. The only way to do this is
+                * to register, then unregister the RPI.
+                */
+               spin_lock_irq(shost->host_lock);
+               ndlp->nlp_flag |= NLP_RM_DFLT_RPI;
+               spin_unlock_irq(shost->host_lock);
+               stat.un.b.lsRjtRsnCode = LSRJT_INVALID_CMD;
+               stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
+               lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
+                       ndlp, mbox);
+               return 1;
+       }
+       lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, mbox);
+       return 1;
 out:
        stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
        stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
-       lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp);
+       lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
        return 0;
 }
 
@@ -436,7 +479,7 @@ lpfc_rcv_padisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                        lpfc_els_rsp_adisc_acc(vport, cmdiocb, ndlp);
                } else {
                        lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp,
-                                        NULL, 0);
+                                        NULL);
                }
                return 1;
        }
@@ -445,7 +488,7 @@ lpfc_rcv_padisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
        stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
        stat.un.b.vendorUnique = 0;
-       lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp);
+       lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
 
        /* 1 sec timeout */
        mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
@@ -464,6 +507,9 @@ lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
              struct lpfc_iocbq *cmdiocb, uint32_t els_cmd)
 {
        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
+       struct lpfc_hba    *phba = vport->phba;
+       struct lpfc_vport **vports;
+       int i, active_vlink_present = 0 ;
 
        /* Put ndlp in NPR state with 1 sec timeout for plogi, ACC logo */
        /* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
@@ -473,12 +519,51 @@ lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        ndlp->nlp_flag |= NLP_LOGO_ACC;
        spin_unlock_irq(shost->host_lock);
        if (els_cmd == ELS_CMD_PRLO)
-               lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL, 0);
+               lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
        else
-               lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
+               lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
+       if (ndlp->nlp_DID == Fabric_DID) {
+               if (vport->port_state <= LPFC_FDISC)
+                       goto out;
+               lpfc_linkdown_port(vport);
+               spin_lock_irq(shost->host_lock);
+               vport->fc_flag |= FC_VPORT_LOGO_RCVD;
+               spin_unlock_irq(shost->host_lock);
+               vports = lpfc_create_vport_work_array(phba);
+               if (vports) {
+                       for (i = 0; i <= phba->max_vports && vports[i] != NULL;
+                                       i++) {
+                               if ((!(vports[i]->fc_flag &
+                                       FC_VPORT_LOGO_RCVD)) &&
+                                       (vports[i]->port_state > LPFC_FDISC)) {
+                                       active_vlink_present = 1;
+                                       break;
+                               }
+                       }
+                       lpfc_destroy_vport_work_array(phba, vports);
+               }
 
-       if (!(ndlp->nlp_type & NLP_FABRIC) ||
-           (ndlp->nlp_state == NLP_STE_ADISC_ISSUE)) {
+               if (active_vlink_present) {
+                       /*
+                        * If there are other active VLinks present,
+                        * re-instantiate the Vlink using FDISC.
+                        */
+                       mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
+                       spin_lock_irq(shost->host_lock);
+                       ndlp->nlp_flag |= NLP_DELAY_TMO;
+                       spin_unlock_irq(shost->host_lock);
+                       ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
+                       vport->port_state = LPFC_FDISC;
+               } else {
+                       spin_lock_irq(shost->host_lock);
+                       phba->pport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG;
+                       spin_unlock_irq(shost->host_lock);
+                       lpfc_retry_pport_discovery(phba);
+               }
+       } else if ((!(ndlp->nlp_type & NLP_FABRIC) &&
+               ((ndlp->nlp_type & NLP_FCP_TARGET) ||
+               !(ndlp->nlp_type & NLP_FCP_INITIATOR))) ||
+               (ndlp->nlp_state == NLP_STE_ADISC_ISSUE)) {
                /* Only try to re-login if this is NOT a Fabric Node */
                mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
                spin_lock_irq(shost->host_lock);
@@ -486,12 +571,10 @@ lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                spin_unlock_irq(shost->host_lock);
 
                ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
-               ndlp->nlp_prev_state = ndlp->nlp_state;
-               lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
-       } else {
-               ndlp->nlp_prev_state = ndlp->nlp_state;
-               lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
        }
+out:
+       ndlp->nlp_prev_state = ndlp->nlp_state;
+       lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
 
        spin_lock_irq(shost->host_lock);
        ndlp->nlp_flag &= ~NLP_NPR_ADISC;
@@ -535,6 +618,11 @@ lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                        roles |= FC_RPORT_ROLE_FCP_INITIATOR;
                if (ndlp->nlp_type & NLP_FCP_TARGET)
                        roles |= FC_RPORT_ROLE_FCP_TARGET;
+
+               lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
+                       "rport rolechg:   role:x%x did:x%x flg:x%x",
+                       roles, ndlp->nlp_DID, ndlp->nlp_flag);
+
                fc_remote_port_rolechg(rport, roles);
        }
 }
@@ -543,31 +631,100 @@ static uint32_t
 lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
 {
        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
-       struct lpfc_hba  *phba = vport->phba;
 
-       /* Check config parameter use-adisc or FCP-2 */
-       if ((phba->cfg_use_adisc && (vport->fc_flag & FC_RSCN_MODE)) ||
-           ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
-               spin_lock_irq(shost->host_lock);
-               ndlp->nlp_flag |= NLP_NPR_ADISC;
-               spin_unlock_irq(shost->host_lock);
-               return 1;
+       if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
+               ndlp->nlp_flag &= ~NLP_NPR_ADISC;
+               return 0;
+       }
+
+       if (!(vport->fc_flag & FC_PT2PT)) {
+               /* Check config parameter use-adisc or FCP-2 */
+               if ((vport->cfg_use_adisc && (vport->fc_flag & FC_RSCN_MODE)) ||
+                   ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) &&
+                    (ndlp->nlp_type & NLP_FCP_TARGET))) {
+                       spin_lock_irq(shost->host_lock);
+                       ndlp->nlp_flag |= NLP_NPR_ADISC;
+                       spin_unlock_irq(shost->host_lock);
+                       return 1;
+               }
        }
        ndlp->nlp_flag &= ~NLP_NPR_ADISC;
        lpfc_unreg_rpi(vport, ndlp);
        return 0;
 }
+/**
+ * lpfc_release_rpi - Release a RPI by issueing unreg_login mailbox cmd.
+ * @phba : Pointer to lpfc_hba structure.
+ * @vport: Pointer to lpfc_vport structure.
+ * @rpi  : rpi to be release.
+ *
+ * This function will send a unreg_login mailbox command to the firmware
+ * to release a rpi.
+ **/
+void
+lpfc_release_rpi(struct lpfc_hba *phba,
+               struct lpfc_vport *vport,
+               uint16_t rpi)
+{
+       LPFC_MBOXQ_t *pmb;
+       int rc;
+
+       pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
+                       GFP_KERNEL);
+       if (!pmb)
+               lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
+                       "2796 mailbox memory allocation failed \n");
+       else {
+               lpfc_unreg_login(phba, vport->vpi, rpi, pmb);
+               pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
+               rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
+               if (rc == MBX_NOT_FINISHED)
+                       mempool_free(pmb, phba->mbox_mem_pool);
+       }
+}
 
 static uint32_t
 lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                  void *arg, uint32_t evt)
 {
-       lpfc_printf_log(vport->phba, KERN_ERR, LOG_DISCOVERY,
-                       "%d (%d):0253 Illegal State Transition: node x%x "
-                       "event x%x, state x%x Data: x%x x%x\n",
-                       vport->phba->brd_no, vport->vpi,
-                       ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
-                       ndlp->nlp_flag);
+       struct lpfc_hba *phba;
+       LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
+       MAILBOX_t *mb;
+       uint16_t rpi;
+
+       phba = vport->phba;
+       /* Release the RPI if reglogin completing */
+       if (!(phba->pport->load_flag & FC_UNLOADING) &&
+               (evt == NLP_EVT_CMPL_REG_LOGIN) &&
+               (!pmb->u.mb.mbxStatus)) {
+               mb = &pmb->u.mb;
+               rpi = pmb->u.mb.un.varWords[0];
+               lpfc_release_rpi(phba, vport, rpi);
+       }
+       lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
+                        "0271 Illegal State Transition: node x%x "
+                        "event x%x, state x%x Data: x%x x%x\n",
+                        ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
+                        ndlp->nlp_flag);
+       return ndlp->nlp_state;
+}
+
+static uint32_t
+lpfc_cmpl_plogi_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
+                 void *arg, uint32_t evt)
+{
+       /* This transition is only legal if we previously
+        * rcv'ed a PLOGI. Since we don't want 2 discovery threads
+        * working on the same NPortID, do nothing for this thread
+        * to stop it.
+        */
+       if (!(ndlp->nlp_flag & NLP_RCV_PLOGI)) {
+               lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
+                        "0272 Illegal State Transition: node x%x "
+                        "event x%x, state x%x Data: x%x x%x\n",
+                        ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
+                        ndlp->nlp_flag);
+       }
        return ndlp->nlp_state;
 }
 
@@ -582,11 +739,8 @@ lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        cmdiocb = (struct lpfc_iocbq *) arg;
 
        if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
-               ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
-               lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
                return ndlp->nlp_state;
        }
-       lpfc_drop_node(vport, ndlp);
        return NLP_STE_FREED_NODE;
 }
 
@@ -595,7 +749,6 @@ lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                         void *arg, uint32_t evt)
 {
        lpfc_issue_els_logo(vport, ndlp, 0);
-       lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
        return ndlp->nlp_state;
 }
 
@@ -609,8 +762,7 @@ lpfc_rcv_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        spin_lock_irq(shost->host_lock);
        ndlp->nlp_flag |= NLP_LOGO_ACC;
        spin_unlock_irq(shost->host_lock);
-       lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
-       lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
+       lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
 
        return ndlp->nlp_state;
 }
@@ -619,7 +771,6 @@ static uint32_t
 lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                           void *arg, uint32_t evt)
 {
-       lpfc_drop_node(vport, ndlp);
        return NLP_STE_FREED_NODE;
 }
 
@@ -627,7 +778,6 @@ static uint32_t
 lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                           void *arg, uint32_t evt)
 {
-       lpfc_drop_node(vport, ndlp);
        return NLP_STE_FREED_NODE;
 }
 
@@ -635,6 +785,7 @@ static uint32_t
 lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                           void *arg, uint32_t evt)
 {
+       struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
        struct lpfc_hba   *phba = vport->phba;
        struct lpfc_iocbq *cmdiocb = arg;
        struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
@@ -657,9 +808,25 @@ lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                   ours */
                stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
                stat.un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
-               lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp);
+               lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
+                       NULL);
        } else {
-               lpfc_rcv_plogi(vport, ndlp, cmdiocb);
+               if (lpfc_rcv_plogi(vport, ndlp, cmdiocb) &&
+                   (ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
+                   (vport->num_disc_nodes)) {
+                       spin_lock_irq(shost->host_lock);
+                       ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
+                       spin_unlock_irq(shost->host_lock);
+                       /* Check if there are more PLOGIs to be sent */
+                       lpfc_more_plogi(vport);
+                       if (vport->num_disc_nodes == 0) {
+                               spin_lock_irq(shost->host_lock);
+                               vport->fc_flag &= ~FC_NDISC_ACTIVE;
+                               spin_unlock_irq(shost->host_lock);
+                               lpfc_can_disctmo(vport);
+                               lpfc_end_rscn(vport);
+                       }
+               }
        } /* If our portname was less */
 
        return ndlp->nlp_state;
@@ -675,7 +842,7 @@ lpfc_rcv_prli_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        memset(&stat, 0, sizeof (struct ls_rjt));
        stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
        stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
-       lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp);
+       lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
        return ndlp->nlp_state;
 }
 
@@ -704,7 +871,7 @@ lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        lpfc_els_abort(phba, ndlp);
 
        if (evt == NLP_EVT_RCV_LOGO) {
-               lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
+               lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
        } else {
                lpfc_issue_els_logo(vport, ndlp, 0);
        }
@@ -728,6 +895,7 @@ lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
                            uint32_t evt)
 {
        struct lpfc_hba    *phba = vport->phba;
+       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
        struct lpfc_iocbq  *cmdiocb, *rspiocb;
        struct lpfc_dmabuf *pcmd, *prsp, *mp;
        uint32_t *lp;
@@ -754,18 +922,23 @@ lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
 
        lp = (uint32_t *) prsp->virt;
        sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
-       if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3))
-               goto out;
 
+       /* Some switches have FDMI servers returning 0 for WWN */
+       if ((ndlp->nlp_DID != FDMI_DID) &&
+               (wwn_to_u64(sp->portName.u.wwn) == 0 ||
+               wwn_to_u64(sp->nodeName.u.wwn) == 0)) {
+               lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
+                                "0142 PLOGI RSP: Invalid WWN.\n");
+               goto out;
+       }
+       if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0))
+               goto out;
        /* PLOGI chkparm OK */
-       lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
-                       "%d (%d):0121 PLOGI chkparm OK "
-                       "Data: x%x x%x x%x x%x\n",
-                       phba->brd_no, vport->vpi,
-                       ndlp->nlp_DID, ndlp->nlp_state,
-                       ndlp->nlp_flag, ndlp->nlp_rpi);
-
-       if (phba->cfg_fcp_class == 2 && (sp->cls2.classValid))
+       lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
+                        "0121 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
+                        ndlp->nlp_DID, ndlp->nlp_state,
+                        ndlp->nlp_flag, ndlp->nlp_rpi);
+       if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
                ndlp->nlp_fcp_info |= CLASS2;
        else
                ndlp->nlp_fcp_info |= CLASS3;
@@ -784,10 +957,9 @@ lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
 
        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
        if (!mbox) {
-               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
-                       "%d (%d):0133 PLOGI: no memory for reg_login "
+               lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
+                       "0133 PLOGI: no memory for reg_login "
                        "Data: x%x x%x x%x x%x\n",
-                       phba->brd_no, vport->vpi,
                        ndlp->nlp_DID, ndlp->nlp_state,
                        ndlp->nlp_flag, ndlp->nlp_rpi);
                goto out;
@@ -795,8 +967,8 @@ lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
 
        lpfc_unreg_rpi(vport, ndlp);
 
-       if (lpfc_reg_login(phba, vport->vpi, irsp->un.elsreq64.remoteID,
-                          (uint8_t *) sp, mbox, 0) == 0) {
+       if (lpfc_reg_rpi(phba, vport->vpi, irsp->un.elsreq64.remoteID,
+                        (uint8_t *) sp, mbox, ndlp->nlp_rpi) == 0) {
                switch (ndlp->nlp_DID) {
                case NameServer_DID:
                        mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
@@ -805,56 +977,84 @@ lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
                        mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
                        break;
                default:
+                       ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
                        mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
                }
                mbox->context2 = lpfc_nlp_get(ndlp);
                mbox->vport = vport;
-               if (lpfc_sli_issue_mbox(phba, mbox,
-                                       (MBX_NOWAIT | MBX_STOP_IOCB))
+               if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
                    != MBX_NOT_FINISHED) {
                        lpfc_nlp_set_state(vport, ndlp,
                                           NLP_STE_REG_LOGIN_ISSUE);
                        return ndlp->nlp_state;
                }
+               if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
+                       ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
+               /* decrement node reference count to the failed mbox
+                * command
+                */
                lpfc_nlp_put(ndlp);
                mp = (struct lpfc_dmabuf *) mbox->context1;
                lpfc_mbuf_free(phba, mp->virt, mp->phys);
                kfree(mp);
                mempool_free(mbox, phba->mbox_mem_pool);
 
-               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
-                       "%d (%d):0134 PLOGI: cannot issue reg_login "
-                       "Data: x%x x%x x%x x%x\n",
-                       phba->brd_no, vport->vpi,
-                       ndlp->nlp_DID, ndlp->nlp_state,
-                       ndlp->nlp_flag, ndlp->nlp_rpi);
+               lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
+                                "0134 PLOGI: cannot issue reg_login "
+                                "Data: x%x x%x x%x x%x\n",
+                                ndlp->nlp_DID, ndlp->nlp_state,
+                                ndlp->nlp_flag, ndlp->nlp_rpi);
        } else {
                mempool_free(mbox, phba->mbox_mem_pool);
 
-               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
-                       "%d (%d):0135 PLOGI: cannot format reg_login "
-                       "Data: x%x x%x x%x x%x\n",
-                       phba->brd_no, vport->vpi,
-                       ndlp->nlp_DID, ndlp->nlp_state,
-                       ndlp->nlp_flag, ndlp->nlp_rpi);
+               lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
+                                "0135 PLOGI: cannot format reg_login "
+                                "Data: x%x x%x x%x x%x\n",
+                                ndlp->nlp_DID, ndlp->nlp_state,
+                                ndlp->nlp_flag, ndlp->nlp_rpi);
        }
 
 
 out:
        if (ndlp->nlp_DID == NameServer_DID) {
                lpfc_vport_set_state(vport, FC_VPORT_FAILED);
-               lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
-                       "%d (%d):0261 Cannot Register NameServer login\n",
-                       phba->brd_no, vport->vpi);
+               lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
+                                "0261 Cannot Register NameServer login\n");
        }
 
-       /* Free this node since the driver cannot login or has the wrong
-          sparm */
-       lpfc_drop_node(vport, ndlp);
+       spin_lock_irq(shost->host_lock);
+       ndlp->nlp_flag |= NLP_DEFER_RM;
+       spin_unlock_irq(shost->host_lock);
        return NLP_STE_FREED_NODE;
 }
 
 static uint32_t
+lpfc_cmpl_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
+                          void *arg, uint32_t evt)
+{
+       return ndlp->nlp_state;
+}
+
+static uint32_t
+lpfc_cmpl_reglogin_plogi_issue(struct lpfc_vport *vport,
+       struct lpfc_nodelist *ndlp, void *arg, uint32_t evt)
+{
+       struct lpfc_hba *phba;
+       LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
+       MAILBOX_t *mb = &pmb->u.mb;
+       uint16_t rpi;
+
+       phba = vport->phba;
+       /* Release the RPI */
+       if (!(phba->pport->load_flag & FC_UNLOADING) &&
+               !mb->mbxStatus) {
+               rpi = pmb->u.mb.un.varWords[0];
+               lpfc_release_rpi(phba, vport, rpi);
+       }
+       return ndlp->nlp_state;
+}
+
+static uint32_t
 lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                           void *arg, uint32_t evt)
 {
@@ -905,6 +1105,7 @@ static uint32_t
 lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                           void *arg, uint32_t evt)
 {
+       struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
        struct lpfc_hba   *phba = vport->phba;
        struct lpfc_iocbq *cmdiocb;
 
@@ -913,9 +1114,16 @@ lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 
        cmdiocb = (struct lpfc_iocbq *) arg;
 
-       if (lpfc_rcv_plogi(vport, ndlp, cmdiocb))
+       if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
+               if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
+                       spin_lock_irq(shost->host_lock);
+                       ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
+                       spin_unlock_irq(shost->host_lock);
+                       if (vport->num_disc_nodes)
+                               lpfc_more_adisc(vport);
+               }
                return ndlp->nlp_state;
-
+       }
        ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
        lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
        lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
@@ -985,6 +1193,7 @@ lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
        struct lpfc_iocbq *cmdiocb, *rspiocb;
        IOCB_t *irsp;
        ADISC *ap;
+       int rc;
 
        cmdiocb = (struct lpfc_iocbq *) arg;
        rspiocb = cmdiocb->context_un.rsp_iocb;
@@ -1010,6 +1219,15 @@ lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
                return ndlp->nlp_state;
        }
 
+       if (phba->sli_rev == LPFC_SLI_REV4) {
+               rc = lpfc_sli4_resume_rpi(ndlp);
+               if (rc) {
+                       /* Stay in state and retry. */
+                       ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
+                       return ndlp->nlp_state;
+               }
+       }
+
        if (ndlp->nlp_type & NLP_FCP_TARGET) {
                ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
                lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
@@ -1017,6 +1235,7 @@ lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
                ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
                lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
        }
+
        return ndlp->nlp_state;
 }
 
@@ -1107,7 +1326,7 @@ lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
 
        /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
        if ((mb = phba->sli.mbox_active)) {
-               if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
+               if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
                   (ndlp == (struct lpfc_nodelist *) mb->context2)) {
                        lpfc_nlp_put(ndlp);
                        mb->context2 = NULL;
@@ -1117,15 +1336,16 @@ lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
 
        spin_lock_irq(&phba->hbalock);
        list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
-               if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
+               if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
                   (ndlp == (struct lpfc_nodelist *) mb->context2)) {
                        mp = (struct lpfc_dmabuf *) (mb->context1);
                        if (mp) {
-                               lpfc_mbuf_free(phba, mp->virt, mp->phys);
+                               __lpfc_mbuf_free(phba, mp->virt, mp->phys);
                                kfree(mp);
                        }
                        lpfc_nlp_put(ndlp);
                        list_del(&mb->list);
+                       phba->sli.mboxq_cnt--;
                        mempool_free(mb, phba->mbox_mem_pool);
                }
        }
@@ -1156,7 +1376,7 @@ lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
        struct lpfc_iocbq *cmdiocb;
 
        cmdiocb = (struct lpfc_iocbq *) arg;
-       lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL, 0);
+       lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
        return ndlp->nlp_state;
 }
 
@@ -1167,26 +1387,22 @@ lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
                                  uint32_t evt)
 {
        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
-       struct lpfc_hba  *phba = vport->phba;
        LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
-       MAILBOX_t *mb = &pmb->mb;
+       MAILBOX_t *mb = &pmb->u.mb;
        uint32_t did  = mb->un.varWords[1];
 
        if (mb->mbxStatus) {
                /* RegLogin failed */
-               lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
-                               "%d (%d):0246 RegLogin failed Data: x%x x%x "
-                               "x%x\n",
-                               phba->brd_no, vport->vpi,
+               lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
+                               "0246 RegLogin failed Data: x%x x%x x%x\n",
                                did, mb->mbxStatus, vport->port_state);
-
                /*
                 * If RegLogin failed due to lack of HBA resources do not
                 * retry discovery.
                 */
                if (mb->mbxStatus == MBXERR_RPI_FULL) {
-                       ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
-                       lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
+                       ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
+                       lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
                        return ndlp->nlp_state;
                }
 
@@ -1204,6 +1420,7 @@ lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
        }
 
        ndlp->nlp_rpi = mb->un.varWords[0];
+       ndlp->nlp_flag |= NLP_RPI_REGISTERED;
 
        /* Only if we are not a fabric nport do we issue PRLI */
        if (!(ndlp->nlp_type & NLP_FABRIC)) {
@@ -1253,6 +1470,7 @@ lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
        ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
        lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
        spin_lock_irq(shost->host_lock);
+       ndlp->nlp_flag |= NLP_IGNR_REG_CMPL;
        ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
        spin_unlock_irq(shost->host_lock);
        lpfc_disc_set_adisc(vport, ndlp);
@@ -1315,7 +1533,7 @@ lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 {
        struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
 
-       lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL, 0);
+       lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
        return ndlp->nlp_state;
 }
 
@@ -1335,6 +1553,10 @@ lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 
        irsp = &rspiocb->iocb;
        if (irsp->ulpStatus) {
+               if ((vport->port_type == LPFC_NPIV_PORT) &&
+                   vport->cfg_restrict_login) {
+                       goto out;
+               }
                ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
                lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
                return ndlp->nlp_state;
@@ -1354,14 +1576,15 @@ lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        }
        if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
            (vport->port_type == LPFC_NPIV_PORT) &&
-            phba->cfg_vport_restrict_login) {
+            vport->cfg_restrict_login) {
+out:
                spin_lock_irq(shost->host_lock);
                ndlp->nlp_flag |= NLP_TARGET_REMOVE;
                spin_unlock_irq(shost->host_lock);
                lpfc_issue_els_logo(vport, ndlp, 0);
 
                ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
-               lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
+               lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
                return ndlp->nlp_state;
        }
 
@@ -1502,7 +1725,7 @@ lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 {
        struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
 
-       lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL, 0);
+       lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
        return ndlp->nlp_state;
 }
 
@@ -1573,8 +1796,8 @@ lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
 
        /* flush the target */
-       lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
-                           ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
+       lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
+                           ndlp->nlp_sid, 0, LPFC_CTX_TGT);
 
        /* Treat like rcv logo */
        lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
@@ -1606,24 +1829,21 @@ lpfc_rcv_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        struct lpfc_iocbq *cmdiocb  = (struct lpfc_iocbq *) arg;
 
        /* Ignore PLOGI if we have an outstanding LOGO */
-       if (ndlp->nlp_flag & NLP_LOGO_SND) {
+       if (ndlp->nlp_flag & (NLP_LOGO_SND | NLP_LOGO_ACC))
                return ndlp->nlp_state;
-       }
-
        if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
+               lpfc_cancel_retry_delay_tmo(vport, ndlp);
                spin_lock_irq(shost->host_lock);
-               ndlp->nlp_flag &= ~NLP_NPR_ADISC;
+               ndlp->nlp_flag &= ~(NLP_NPR_ADISC | NLP_NPR_2B_DISC);
                spin_unlock_irq(shost->host_lock);
-               return ndlp->nlp_state;
-       }
-
-       /* send PLOGI immediately, move to PLOGI issue state */
-       if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
-               ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
-               lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
-               lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
+       } else if (!(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
+               /* send PLOGI immediately, move to PLOGI issue state */
+               if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
+                       ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
+                       lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
+                       lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
+               }
        }
-
        return ndlp->nlp_state;
 }
 
@@ -1638,7 +1858,7 @@ lpfc_rcv_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        memset(&stat, 0, sizeof (struct ls_rjt));
        stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
        stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
-       lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp);
+       lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
 
        if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
                if (ndlp->nlp_flag & NLP_NPR_ADISC) {
@@ -1674,7 +1894,6 @@ lpfc_rcv_padisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
 
        lpfc_rcv_padisc(vport, ndlp, cmdiocb);
-
        /*
         * Do not start discovery if discovery is about to start
         * or discovery in progress for this node. Starting discovery
@@ -1707,7 +1926,7 @@ lpfc_rcv_prlo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        ndlp->nlp_flag |= NLP_LOGO_ACC;
        spin_unlock_irq(shost->host_lock);
 
-       lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
+       lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
 
        if ((ndlp->nlp_flag & NLP_DELAY_TMO) == 0) {
                mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
@@ -1736,7 +1955,7 @@ lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
 
        irsp = &rspiocb->iocb;
        if (irsp->ulpStatus) {
-               lpfc_drop_node(vport, ndlp);
+               ndlp->nlp_flag |= NLP_DEFER_RM;
                return NLP_STE_FREED_NODE;
        }
        return ndlp->nlp_state;
@@ -1764,8 +1983,13 @@ static uint32_t
 lpfc_cmpl_logo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                        void *arg, uint32_t evt)
 {
+       struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
+       if (ndlp->nlp_DID == Fabric_DID) {
+               spin_lock_irq(shost->host_lock);
+               vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
+               spin_unlock_irq(shost->host_lock);
+       }
        lpfc_unreg_rpi(vport, ndlp);
-       /* This routine does nothing, just return the current state */
        return ndlp->nlp_state;
 }
 
@@ -1793,11 +2017,12 @@ lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
                            void *arg, uint32_t evt)
 {
        LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
-       MAILBOX_t    *mb = &pmb->mb;
+       MAILBOX_t    *mb = &pmb->u.mb;
 
-       if (!mb->mbxStatus)
+       if (!mb->mbxStatus) {
                ndlp->nlp_rpi = mb->un.varWords[0];
-       else {
+               ndlp->nlp_flag |= NLP_RPI_REGISTERED;
+       } else {
                if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
                        lpfc_drop_node(vport, ndlp);
                        return NLP_STE_FREED_NODE;
@@ -1834,12 +2059,10 @@ lpfc_device_recov_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
        if (vport->fc_flag & FC_RSCN_DEFERRED)
                return ndlp->nlp_state;
 
+       lpfc_cancel_retry_delay_tmo(vport, ndlp);
        spin_lock_irq(shost->host_lock);
        ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
        spin_unlock_irq(shost->host_lock);
-       if (ndlp->nlp_flag & NLP_DELAY_TMO) {
-               lpfc_cancel_retry_delay_tmo(vport, ndlp);
-       }
        return ndlp->nlp_state;
 }
 
@@ -1925,9 +2148,9 @@ static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
        lpfc_rcv_els_plogi_issue,       /* RCV_PRLO        */
        lpfc_cmpl_plogi_plogi_issue,    /* CMPL_PLOGI      */
        lpfc_disc_illegal,              /* CMPL_PRLI       */
-       lpfc_disc_illegal,              /* CMPL_LOGO       */
+       lpfc_cmpl_logo_plogi_issue,     /* CMPL_LOGO       */
        lpfc_disc_illegal,              /* CMPL_ADISC      */
-       lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
+       lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
        lpfc_device_rm_plogi_issue,     /* DEVICE_RM       */
        lpfc_device_recov_plogi_issue,  /* DEVICE_RECOVERY */
 
@@ -1951,7 +2174,7 @@ static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
        lpfc_rcv_padisc_reglogin_issue, /* RCV_ADISC       */
        lpfc_rcv_padisc_reglogin_issue, /* RCV_PDISC       */
        lpfc_rcv_prlo_reglogin_issue,   /* RCV_PRLO        */
-       lpfc_disc_illegal,              /* CMPL_PLOGI      */
+       lpfc_cmpl_plogi_illegal,        /* CMPL_PLOGI      */
        lpfc_disc_illegal,              /* CMPL_PRLI       */
        lpfc_disc_illegal,              /* CMPL_LOGO       */
        lpfc_disc_illegal,              /* CMPL_ADISC      */
@@ -1965,7 +2188,7 @@ static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
        lpfc_rcv_padisc_prli_issue,     /* RCV_ADISC       */
        lpfc_rcv_padisc_prli_issue,     /* RCV_PDISC       */
        lpfc_rcv_prlo_prli_issue,       /* RCV_PRLO        */
-       lpfc_disc_illegal,              /* CMPL_PLOGI      */
+       lpfc_cmpl_plogi_illegal,        /* CMPL_PLOGI      */
        lpfc_cmpl_prli_prli_issue,      /* CMPL_PRLI       */
        lpfc_disc_illegal,              /* CMPL_LOGO       */
        lpfc_disc_illegal,              /* CMPL_ADISC      */
@@ -2020,32 +2243,48 @@ int
 lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
                        void *arg, uint32_t evt)
 {
-       struct lpfc_hba  *phba = vport->phba;
        uint32_t cur_state, rc;
        uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
                         uint32_t);
+       uint32_t got_ndlp = 0;
+
+       if (lpfc_nlp_get(ndlp))
+               got_ndlp = 1;
 
-       lpfc_nlp_get(ndlp);
        cur_state = ndlp->nlp_state;
 
        /* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
-       lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d (%d):0211 DSM in event x%x on NPort x%x in "
-                       "state %d Data: x%x\n",
-                       phba->brd_no, vport->vpi,
-                       evt, ndlp->nlp_DID, cur_state, ndlp->nlp_flag);
+       lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
+                        "0211 DSM in event x%x on NPort x%x in "
+                        "state %d Data: x%x\n",
+                        evt, ndlp->nlp_DID, cur_state, ndlp->nlp_flag);
+
+       lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
+                "DSM in:          evt:%d ste:%d did:x%x",
+               evt, cur_state, ndlp->nlp_DID);
 
        func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
        rc = (func) (vport, ndlp, arg, evt);
 
        /* DSM out state <rc> on NPort <nlp_DID> */
-       lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
-                       "%d (%d):0212 DSM out state %d on NPort x%x "
-                       "Data: x%x\n",
-                       phba->brd_no, vport->vpi,
+       if (got_ndlp) {
+               lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
+                        "0212 DSM out state %d on NPort x%x Data: x%x\n",
+                        rc, ndlp->nlp_DID, ndlp->nlp_flag);
+
+               lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
+                       "DSM out:         ste:%d did:x%x flg:x%x",
                        rc, ndlp->nlp_DID, ndlp->nlp_flag);
+               /* Decrement the ndlp reference count held for this function */
+               lpfc_nlp_put(ndlp);
+       } else {
+               lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
+                       "0213 DSM out state %d on NPort free\n", rc);
 
-       lpfc_nlp_put(ndlp);
+               lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
+                       "DSM out:         ste:%d did:x%x flg:x%x",
+                       rc, 0, 0);
+       }
 
        return rc;
 }