Back to home page

OSCL-LXR

 
 

    


0001 /*******************************************************************
0002  * This file is part of the Emulex Linux Device Driver for         *
0003  * Fibre Channel Host Bus Adapters.                                *
0004  * Copyright (C) 2017-2022 Broadcom. All Rights Reserved. The term *
0005  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
0006  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
0007  * EMULEX and SLI are trademarks of Emulex.                        *
0008  * www.broadcom.com                                                *
0009  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
0010  *                                                                 *
0011  * This program is free software; you can redistribute it and/or   *
0012  * modify it under the terms of version 2 of the GNU General       *
0013  * Public License as published by the Free Software Foundation.    *
0014  * This program is distributed in the hope that it will be useful. *
0015  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
0016  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
0017  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
0018  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
0019  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
0020  * more details, a copy of which can be found in the file COPYING  *
0021  * included with this package.                                     *
0022  *******************************************************************/
0023 
0024 #include <linux/blkdev.h>
0025 #include <linux/delay.h>
0026 #include <linux/dma-mapping.h>
0027 #include <linux/idr.h>
0028 #include <linux/interrupt.h>
0029 #include <linux/kthread.h>
0030 #include <linux/pci.h>
0031 #include <linux/slab.h>
0032 #include <linux/spinlock.h>
0033 #include <linux/sched/signal.h>
0034 
0035 #include <scsi/scsi.h>
0036 #include <scsi/scsi_device.h>
0037 #include <scsi/scsi_host.h>
0038 #include <scsi/scsi_transport_fc.h>
0039 
0040 #include "lpfc_hw4.h"
0041 #include "lpfc_hw.h"
0042 #include "lpfc_sli.h"
0043 #include "lpfc_sli4.h"
0044 #include "lpfc_nl.h"
0045 #include "lpfc_disc.h"
0046 #include "lpfc_scsi.h"
0047 #include "lpfc.h"
0048 #include "lpfc_logmsg.h"
0049 #include "lpfc_crtn.h"
0050 #include "lpfc_version.h"
0051 #include "lpfc_vport.h"
0052 
0053 inline void lpfc_vport_set_state(struct lpfc_vport *vport,
0054                  enum fc_vport_state new_state)
0055 {
0056     struct fc_vport *fc_vport = vport->fc_vport;
0057 
0058     if (fc_vport) {
0059         /*
0060          * When the transport defines fc_vport_set state we will replace
0061          * this code with the following line
0062          */
0063         /* fc_vport_set_state(fc_vport, new_state); */
0064         if (new_state != FC_VPORT_INITIALIZING)
0065             fc_vport->vport_last_state = fc_vport->vport_state;
0066         fc_vport->vport_state = new_state;
0067     }
0068 
0069     /* for all the error states we will set the invternal state to FAILED */
0070     switch (new_state) {
0071     case FC_VPORT_NO_FABRIC_SUPP:
0072     case FC_VPORT_NO_FABRIC_RSCS:
0073     case FC_VPORT_FABRIC_LOGOUT:
0074     case FC_VPORT_FABRIC_REJ_WWN:
0075     case FC_VPORT_FAILED:
0076         vport->port_state = LPFC_VPORT_FAILED;
0077         break;
0078     case FC_VPORT_LINKDOWN:
0079         vport->port_state = LPFC_VPORT_UNKNOWN;
0080         break;
0081     default:
0082         /* do nothing */
0083         break;
0084     }
0085 }
0086 
0087 int
0088 lpfc_alloc_vpi(struct lpfc_hba *phba)
0089 {
0090     unsigned long vpi;
0091 
0092     spin_lock_irq(&phba->hbalock);
0093     /* Start at bit 1 because vpi zero is reserved for the physical port */
0094     vpi = find_next_zero_bit(phba->vpi_bmask, (phba->max_vpi + 1), 1);
0095     if (vpi > phba->max_vpi)
0096         vpi = 0;
0097     else
0098         set_bit(vpi, phba->vpi_bmask);
0099     if (phba->sli_rev == LPFC_SLI_REV4)
0100         phba->sli4_hba.max_cfg_param.vpi_used++;
0101     spin_unlock_irq(&phba->hbalock);
0102     return vpi;
0103 }
0104 
0105 static void
0106 lpfc_free_vpi(struct lpfc_hba *phba, int vpi)
0107 {
0108     if (vpi == 0)
0109         return;
0110     spin_lock_irq(&phba->hbalock);
0111     clear_bit(vpi, phba->vpi_bmask);
0112     if (phba->sli_rev == LPFC_SLI_REV4)
0113         phba->sli4_hba.max_cfg_param.vpi_used--;
0114     spin_unlock_irq(&phba->hbalock);
0115 }
0116 
0117 static int
0118 lpfc_vport_sparm(struct lpfc_hba *phba, struct lpfc_vport *vport)
0119 {
0120     LPFC_MBOXQ_t *pmb;
0121     MAILBOX_t *mb;
0122     struct lpfc_dmabuf *mp;
0123     int  rc;
0124 
0125     pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
0126     if (!pmb) {
0127         return -ENOMEM;
0128     }
0129     mb = &pmb->u.mb;
0130 
0131     rc = lpfc_read_sparam(phba, pmb, vport->vpi);
0132     if (rc) {
0133         mempool_free(pmb, phba->mbox_mem_pool);
0134         return -ENOMEM;
0135     }
0136 
0137     /*
0138      * Wait for the read_sparams mailbox to complete.  Driver needs
0139      * this per vport to start the FDISC.  If the mailbox fails,
0140      * just cleanup and return an error unless the failure is a
0141      * mailbox timeout.  For MBX_TIMEOUT, allow the default
0142      * mbox completion handler to take care of the cleanup.  This
0143      * is safe as the mailbox command isn't one that triggers
0144      * another mailbox.
0145      */
0146     pmb->vport = vport;
0147     rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
0148     if (rc != MBX_SUCCESS) {
0149         if (signal_pending(current)) {
0150             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0151                      "1830 Signal aborted mbxCmd x%x\n",
0152                      mb->mbxCommand);
0153             if (rc != MBX_TIMEOUT)
0154                 lpfc_mbox_rsrc_cleanup(phba, pmb,
0155                                MBOX_THD_UNLOCKED);
0156             return -EINTR;
0157         } else {
0158             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0159                      "1818 VPort failed init, mbxCmd x%x "
0160                      "READ_SPARM mbxStatus x%x, rc = x%x\n",
0161                      mb->mbxCommand, mb->mbxStatus, rc);
0162             if (rc != MBX_TIMEOUT)
0163                 lpfc_mbox_rsrc_cleanup(phba, pmb,
0164                                MBOX_THD_UNLOCKED);
0165             return -EIO;
0166         }
0167     }
0168 
0169     mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
0170     memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
0171     memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
0172            sizeof (struct lpfc_name));
0173     memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
0174            sizeof (struct lpfc_name));
0175     lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
0176     return 0;
0177 }
0178 
0179 static int
0180 lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn,
0181               const char *name_type)
0182 {
0183                 /* ensure that IEEE format 1 addresses
0184                  * contain zeros in bits 59-48
0185                  */
0186     if (!((wwn->u.wwn[0] >> 4) == 1 &&
0187           ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0)))
0188         return 1;
0189 
0190     lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
0191             "1822 Invalid %s: %02x:%02x:%02x:%02x:"
0192             "%02x:%02x:%02x:%02x\n",
0193             name_type,
0194             wwn->u.wwn[0], wwn->u.wwn[1],
0195             wwn->u.wwn[2], wwn->u.wwn[3],
0196             wwn->u.wwn[4], wwn->u.wwn[5],
0197             wwn->u.wwn[6], wwn->u.wwn[7]);
0198     return 0;
0199 }
0200 
0201 static int
0202 lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
0203 {
0204     struct lpfc_vport *vport;
0205     unsigned long flags;
0206 
0207     spin_lock_irqsave(&phba->port_list_lock, flags);
0208     list_for_each_entry(vport, &phba->port_list, listentry) {
0209         if (vport == new_vport)
0210             continue;
0211         /* If they match, return not unique */
0212         if (memcmp(&vport->fc_sparam.portName,
0213                &new_vport->fc_sparam.portName,
0214                sizeof(struct lpfc_name)) == 0) {
0215             spin_unlock_irqrestore(&phba->port_list_lock, flags);
0216             return 0;
0217         }
0218     }
0219     spin_unlock_irqrestore(&phba->port_list_lock, flags);
0220     return 1;
0221 }
0222 
0223 /**
0224  * lpfc_discovery_wait - Wait for driver discovery to quiesce
0225  * @vport: The virtual port for which this call is being executed.
0226  *
0227  * This driver calls this routine specifically from lpfc_vport_delete
0228  * to enforce a synchronous execution of vport
0229  * delete relative to discovery activities.  The
0230  * lpfc_vport_delete routine should not return until it
0231  * can reasonably guarantee that discovery has quiesced.
0232  * Post FDISC LOGO, the driver must wait until its SAN teardown is
0233  * complete and all resources recovered before allowing
0234  * cleanup.
0235  *
0236  * This routine does not require any locks held.
0237  **/
0238 static void lpfc_discovery_wait(struct lpfc_vport *vport)
0239 {
0240     struct lpfc_hba *phba = vport->phba;
0241     uint32_t wait_flags = 0;
0242     unsigned long wait_time_max;
0243     unsigned long start_time;
0244 
0245     wait_flags = FC_RSCN_MODE | FC_RSCN_DISCOVERY | FC_NLP_MORE |
0246              FC_RSCN_DEFERRED | FC_NDISC_ACTIVE | FC_DISC_TMO;
0247 
0248     /*
0249      * The time constraint on this loop is a balance between the
0250      * fabric RA_TOV value and dev_loss tmo.  The driver's
0251      * devloss_tmo is 10 giving this loop a 3x multiplier minimally.
0252      */
0253     wait_time_max = msecs_to_jiffies(((phba->fc_ratov * 3) + 3) * 1000);
0254     wait_time_max += jiffies;
0255     start_time = jiffies;
0256     while (time_before(jiffies, wait_time_max)) {
0257         if ((vport->num_disc_nodes > 0)    ||
0258             (vport->fc_flag & wait_flags)  ||
0259             ((vport->port_state > LPFC_VPORT_FAILED) &&
0260              (vport->port_state < LPFC_VPORT_READY))) {
0261             lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
0262                     "1833 Vport discovery quiesce Wait:"
0263                     " state x%x fc_flags x%x"
0264                     " num_nodes x%x, waiting 1000 msecs"
0265                     " total wait msecs x%x\n",
0266                     vport->port_state, vport->fc_flag,
0267                     vport->num_disc_nodes,
0268                     jiffies_to_msecs(jiffies - start_time));
0269             msleep(1000);
0270         } else {
0271             /* Base case.  Wait variants satisfied.  Break out */
0272             lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
0273                      "1834 Vport discovery quiesced:"
0274                      " state x%x fc_flags x%x"
0275                      " wait msecs x%x\n",
0276                      vport->port_state, vport->fc_flag,
0277                      jiffies_to_msecs(jiffies
0278                         - start_time));
0279             break;
0280         }
0281     }
0282 
0283     if (time_after(jiffies, wait_time_max))
0284         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0285                  "1835 Vport discovery quiesce failed:"
0286                  " state x%x fc_flags x%x wait msecs x%x\n",
0287                  vport->port_state, vport->fc_flag,
0288                  jiffies_to_msecs(jiffies - start_time));
0289 }
0290 
0291 int
0292 lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
0293 {
0294     struct lpfc_nodelist *ndlp;
0295     struct Scsi_Host *shost = fc_vport->shost;
0296     struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata;
0297     struct lpfc_hba   *phba = pport->phba;
0298     struct lpfc_vport *vport = NULL;
0299     int instance;
0300     int vpi;
0301     int rc = VPORT_ERROR;
0302     int status;
0303 
0304     if ((phba->sli_rev < 3) || !(phba->cfg_enable_npiv)) {
0305         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
0306                 "1808 Create VPORT failed: "
0307                 "NPIV is not enabled: SLImode:%d\n",
0308                 phba->sli_rev);
0309         rc = VPORT_INVAL;
0310         goto error_out;
0311     }
0312 
0313     /* NPIV is not supported if HBA has NVME Target enabled */
0314     if (phba->nvmet_support) {
0315         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
0316                 "3189 Create VPORT failed: "
0317                 "NPIV is not supported on NVME Target\n");
0318         rc = VPORT_INVAL;
0319         goto error_out;
0320     }
0321 
0322     vpi = lpfc_alloc_vpi(phba);
0323     if (vpi == 0) {
0324         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
0325                 "1809 Create VPORT failed: "
0326                 "Max VPORTs (%d) exceeded\n",
0327                 phba->max_vpi);
0328         rc = VPORT_NORESOURCES;
0329         goto error_out;
0330     }
0331 
0332     /* Assign an unused board number */
0333     if ((instance = lpfc_get_instance()) < 0) {
0334         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
0335                 "1810 Create VPORT failed: Cannot get "
0336                 "instance number\n");
0337         lpfc_free_vpi(phba, vpi);
0338         rc = VPORT_NORESOURCES;
0339         goto error_out;
0340     }
0341 
0342     vport = lpfc_create_port(phba, instance, &fc_vport->dev);
0343     if (!vport) {
0344         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
0345                 "1811 Create VPORT failed: vpi x%x\n", vpi);
0346         lpfc_free_vpi(phba, vpi);
0347         rc = VPORT_NORESOURCES;
0348         goto error_out;
0349     }
0350 
0351     vport->vpi = vpi;
0352     lpfc_debugfs_initialize(vport);
0353 
0354     if ((status = lpfc_vport_sparm(phba, vport))) {
0355         if (status == -EINTR) {
0356             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0357                      "1831 Create VPORT Interrupted.\n");
0358             rc = VPORT_ERROR;
0359         } else {
0360             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0361                      "1813 Create VPORT failed. "
0362                      "Cannot get sparam\n");
0363             rc = VPORT_NORESOURCES;
0364         }
0365         lpfc_free_vpi(phba, vpi);
0366         destroy_port(vport);
0367         goto error_out;
0368     }
0369 
0370     u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
0371     u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
0372 
0373     memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
0374     memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
0375 
0376     if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
0377         !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
0378         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0379                  "1821 Create VPORT failed. "
0380                  "Invalid WWN format\n");
0381         lpfc_free_vpi(phba, vpi);
0382         destroy_port(vport);
0383         rc = VPORT_INVAL;
0384         goto error_out;
0385     }
0386 
0387     if (!lpfc_unique_wwpn(phba, vport)) {
0388         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0389                  "1823 Create VPORT failed. "
0390                  "Duplicate WWN on HBA\n");
0391         lpfc_free_vpi(phba, vpi);
0392         destroy_port(vport);
0393         rc = VPORT_INVAL;
0394         goto error_out;
0395     }
0396 
0397     /* Create binary sysfs attribute for vport */
0398     lpfc_alloc_sysfs_attr(vport);
0399 
0400     /* Set the DFT_LUN_Q_DEPTH accordingly */
0401     vport->cfg_lun_queue_depth  = phba->pport->cfg_lun_queue_depth;
0402 
0403     /* Only the physical port can support NVME for now */
0404     vport->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
0405 
0406     *(struct lpfc_vport **)fc_vport->dd_data = vport;
0407     vport->fc_vport = fc_vport;
0408 
0409     /* At this point we are fully registered with SCSI Layer.  */
0410     vport->load_flag |= FC_ALLOW_FDMI;
0411     if (phba->cfg_enable_SmartSAN ||
0412         (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
0413         /* Setup appropriate attribute masks */
0414         vport->fdmi_hba_mask = phba->pport->fdmi_hba_mask;
0415         vport->fdmi_port_mask = phba->pport->fdmi_port_mask;
0416     }
0417 
0418     /*
0419      * In SLI4, the vpi must be activated before it can be used
0420      * by the port.
0421      */
0422     if ((phba->sli_rev == LPFC_SLI_REV4) &&
0423         (pport->fc_flag & FC_VFI_REGISTERED)) {
0424         rc = lpfc_sli4_init_vpi(vport);
0425         if (rc) {
0426             lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
0427                     "1838 Failed to INIT_VPI on vpi %d "
0428                     "status %d\n", vpi, rc);
0429             rc = VPORT_NORESOURCES;
0430             lpfc_free_vpi(phba, vpi);
0431             goto error_out;
0432         }
0433     } else if (phba->sli_rev == LPFC_SLI_REV4) {
0434         /*
0435          * Driver cannot INIT_VPI now. Set the flags to
0436          * init_vpi when reg_vfi complete.
0437          */
0438         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
0439         lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
0440         rc = VPORT_OK;
0441         goto out;
0442     }
0443 
0444     if ((phba->link_state < LPFC_LINK_UP) ||
0445         (pport->port_state < LPFC_FABRIC_CFG_LINK) ||
0446         (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
0447         lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
0448         rc = VPORT_OK;
0449         goto out;
0450     }
0451 
0452     if (disable) {
0453         lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
0454         rc = VPORT_OK;
0455         goto out;
0456     }
0457 
0458     /* Use the Physical nodes Fabric NDLP to determine if the link is
0459      * up and ready to FDISC.
0460      */
0461     ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
0462     if (ndlp &&
0463         ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
0464         if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
0465             lpfc_set_disctmo(vport);
0466             lpfc_initial_fdisc(vport);
0467         } else {
0468             lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
0469             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0470                      "0262 No NPIV Fabric support\n");
0471         }
0472     } else {
0473         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
0474     }
0475     rc = VPORT_OK;
0476 
0477 out:
0478     lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
0479              "1825 Vport Created.\n");
0480     lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
0481 error_out:
0482     return rc;
0483 }
0484 
0485 static int
0486 lpfc_send_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
0487 {
0488     int rc;
0489     struct lpfc_hba *phba = vport->phba;
0490 
0491     DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
0492 
0493     spin_lock_irq(&ndlp->lock);
0494     if (!(ndlp->save_flags & NLP_WAIT_FOR_LOGO) &&
0495         !ndlp->logo_waitq) {
0496         ndlp->logo_waitq = &waitq;
0497         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
0498         ndlp->nlp_flag |= NLP_ISSUE_LOGO;
0499         ndlp->save_flags |= NLP_WAIT_FOR_LOGO;
0500     }
0501     spin_unlock_irq(&ndlp->lock);
0502     rc = lpfc_issue_els_npiv_logo(vport, ndlp);
0503     if (!rc) {
0504         wait_event_timeout(waitq,
0505                    (!(ndlp->save_flags & NLP_WAIT_FOR_LOGO)),
0506                    msecs_to_jiffies(phba->fc_ratov * 2000));
0507 
0508         if (!(ndlp->save_flags & NLP_WAIT_FOR_LOGO))
0509             goto logo_cmpl;
0510         /* LOGO wait failed.  Correct status. */
0511         rc = -EINTR;
0512     } else {
0513         rc = -EIO;
0514     }
0515 
0516     /* Error - clean up node flags. */
0517     spin_lock_irq(&ndlp->lock);
0518     ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
0519     ndlp->save_flags &= ~NLP_WAIT_FOR_LOGO;
0520     spin_unlock_irq(&ndlp->lock);
0521 
0522  logo_cmpl:
0523     lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
0524              "1824 Issue LOGO completes with status %d\n",
0525              rc);
0526     spin_lock_irq(&ndlp->lock);
0527     ndlp->logo_waitq = NULL;
0528     spin_unlock_irq(&ndlp->lock);
0529     return rc;
0530 }
0531 
0532 static int
0533 disable_vport(struct fc_vport *fc_vport)
0534 {
0535     struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
0536     struct lpfc_hba   *phba = vport->phba;
0537     struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
0538     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
0539 
0540     /* Can't disable during an outstanding delete. */
0541     if (vport->load_flag & FC_UNLOADING)
0542         return 0;
0543 
0544     ndlp = lpfc_findnode_did(vport, Fabric_DID);
0545     if (ndlp && phba->link_state >= LPFC_LINK_UP)
0546         (void)lpfc_send_npiv_logo(vport, ndlp);
0547 
0548     lpfc_sli_host_down(vport);
0549 
0550     /* Mark all nodes for discovery so we can remove them by
0551      * calling lpfc_cleanup_rpis(vport, 1)
0552      */
0553     list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
0554         if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
0555             continue;
0556         lpfc_disc_state_machine(vport, ndlp, NULL,
0557                     NLP_EVT_DEVICE_RECOVERY);
0558     }
0559     lpfc_cleanup_rpis(vport, 1);
0560 
0561     lpfc_stop_vport_timers(vport);
0562     lpfc_unreg_all_rpis(vport);
0563     lpfc_unreg_default_rpis(vport);
0564     /*
0565      * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
0566      * scsi_host_put() to release the vport.
0567      */
0568     lpfc_mbx_unreg_vpi(vport);
0569     if (phba->sli_rev == LPFC_SLI_REV4) {
0570         spin_lock_irq(shost->host_lock);
0571         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
0572         spin_unlock_irq(shost->host_lock);
0573     }
0574 
0575     lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
0576     lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
0577              "1826 Vport Disabled.\n");
0578     return VPORT_OK;
0579 }
0580 
0581 static int
0582 enable_vport(struct fc_vport *fc_vport)
0583 {
0584     struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
0585     struct lpfc_hba   *phba = vport->phba;
0586     struct lpfc_nodelist *ndlp = NULL;
0587     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
0588 
0589     if ((phba->link_state < LPFC_LINK_UP) ||
0590         (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
0591         lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
0592         return VPORT_OK;
0593     }
0594 
0595     spin_lock_irq(shost->host_lock);
0596     vport->load_flag |= FC_LOADING;
0597     if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
0598         spin_unlock_irq(shost->host_lock);
0599         lpfc_issue_init_vpi(vport);
0600         goto out;
0601     }
0602 
0603     vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
0604     spin_unlock_irq(shost->host_lock);
0605 
0606     /* Use the Physical nodes Fabric NDLP to determine if the link is
0607      * up and ready to FDISC.
0608      */
0609     ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
0610     if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
0611         if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
0612             lpfc_set_disctmo(vport);
0613             lpfc_initial_fdisc(vport);
0614         } else {
0615             lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
0616             lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0617                      "0264 No NPIV Fabric support\n");
0618         }
0619     } else {
0620         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
0621     }
0622 
0623 out:
0624     lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
0625              "1827 Vport Enabled.\n");
0626     return VPORT_OK;
0627 }
0628 
0629 int
0630 lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
0631 {
0632     if (disable)
0633         return disable_vport(fc_vport);
0634     else
0635         return enable_vport(fc_vport);
0636 }
0637 
0638 int
0639 lpfc_vport_delete(struct fc_vport *fc_vport)
0640 {
0641     struct lpfc_nodelist *ndlp = NULL;
0642     struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
0643     struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
0644     struct lpfc_hba  *phba = vport->phba;
0645     int rc;
0646 
0647     if (vport->port_type == LPFC_PHYSICAL_PORT) {
0648         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0649                  "1812 vport_delete failed: Cannot delete "
0650                  "physical host\n");
0651         return VPORT_ERROR;
0652     }
0653 
0654     /* If the vport is a static vport fail the deletion. */
0655     if ((vport->vport_flag & STATIC_VPORT) &&
0656         !(phba->pport->load_flag & FC_UNLOADING)) {
0657         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
0658                  "1837 vport_delete failed: Cannot delete "
0659                  "static vport.\n");
0660         return VPORT_ERROR;
0661     }
0662 
0663     spin_lock_irq(&phba->hbalock);
0664     vport->load_flag |= FC_UNLOADING;
0665     spin_unlock_irq(&phba->hbalock);
0666 
0667     /*
0668      * If we are not unloading the driver then prevent the vport_delete
0669      * from happening until after this vport's discovery is finished.
0670      */
0671     if (!(phba->pport->load_flag & FC_UNLOADING)) {
0672         int check_count = 0;
0673         while (check_count < ((phba->fc_ratov * 3) + 3) &&
0674                vport->port_state > LPFC_VPORT_FAILED &&
0675                vport->port_state < LPFC_VPORT_READY) {
0676             check_count++;
0677             msleep(1000);
0678         }
0679         if (vport->port_state > LPFC_VPORT_FAILED &&
0680             vport->port_state < LPFC_VPORT_READY)
0681             return -EAGAIN;
0682     }
0683 
0684     /*
0685      * Take early refcount for outstanding I/O requests we schedule during
0686      * delete processing for unreg_vpi.  Always keep this before
0687      * scsi_remove_host() as we can no longer obtain a reference through
0688      * scsi_host_get() after scsi_host_remove as shost is set to SHOST_DEL.
0689      */
0690     if (!scsi_host_get(shost))
0691         return VPORT_INVAL;
0692 
0693     lpfc_free_sysfs_attr(vport);
0694     lpfc_debugfs_terminate(vport);
0695 
0696     /* Remove FC host to break driver binding. */
0697     fc_remove_host(shost);
0698     scsi_remove_host(shost);
0699 
0700     /* Send the DA_ID and Fabric LOGO to cleanup Nameserver entries. */
0701     ndlp = lpfc_findnode_did(vport, Fabric_DID);
0702     if (!ndlp)
0703         goto skip_logo;
0704 
0705     if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
0706         phba->link_state >= LPFC_LINK_UP &&
0707         phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
0708         if (vport->cfg_enable_da_id) {
0709             /* Send DA_ID and wait for a completion. */
0710             rc = lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0);
0711             if (rc) {
0712                 lpfc_printf_log(vport->phba, KERN_WARNING,
0713                         LOG_VPORT,
0714                         "1829 CT command failed to "
0715                         "delete objects on fabric, "
0716                         "rc %d\n", rc);
0717             }
0718         }
0719 
0720         /*
0721          * If the vpi is not registered, then a valid FDISC doesn't
0722          * exist and there is no need for a ELS LOGO.  Just cleanup
0723          * the ndlp.
0724          */
0725         if (!(vport->vpi_state & LPFC_VPI_REGISTERED))
0726             goto skip_logo;
0727 
0728         /* Issue a Fabric LOGO to cleanup fabric resources. */
0729         ndlp = lpfc_findnode_did(vport, Fabric_DID);
0730         if (!ndlp)
0731             goto skip_logo;
0732 
0733         rc = lpfc_send_npiv_logo(vport, ndlp);
0734         if (rc)
0735             goto skip_logo;
0736     }
0737 
0738     if (!(phba->pport->load_flag & FC_UNLOADING))
0739         lpfc_discovery_wait(vport);
0740 
0741 skip_logo:
0742 
0743     lpfc_cleanup(vport);
0744 
0745     /* Remove scsi host now.  The nodes are cleaned up. */
0746     lpfc_sli_host_down(vport);
0747     lpfc_stop_vport_timers(vport);
0748 
0749     if (!(phba->pport->load_flag & FC_UNLOADING)) {
0750         lpfc_unreg_all_rpis(vport);
0751         lpfc_unreg_default_rpis(vport);
0752         /*
0753          * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
0754          * does the scsi_host_put() to release the vport.
0755          */
0756         if (!(vport->vpi_state & LPFC_VPI_REGISTERED) ||
0757                 lpfc_mbx_unreg_vpi(vport))
0758             scsi_host_put(shost);
0759     } else {
0760         scsi_host_put(shost);
0761     }
0762 
0763     lpfc_free_vpi(phba, vport->vpi);
0764     vport->work_port_events = 0;
0765     spin_lock_irq(&phba->port_list_lock);
0766     list_del_init(&vport->listentry);
0767     spin_unlock_irq(&phba->port_list_lock);
0768     lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
0769              "1828 Vport Deleted.\n");
0770     scsi_host_put(shost);
0771     return VPORT_OK;
0772 }
0773 
0774 struct lpfc_vport **
0775 lpfc_create_vport_work_array(struct lpfc_hba *phba)
0776 {
0777     struct lpfc_vport *port_iterator;
0778     struct lpfc_vport **vports;
0779     int index = 0;
0780     vports = kcalloc(phba->max_vports + 1, sizeof(struct lpfc_vport *),
0781              GFP_KERNEL);
0782     if (vports == NULL)
0783         return NULL;
0784     spin_lock_irq(&phba->port_list_lock);
0785     list_for_each_entry(port_iterator, &phba->port_list, listentry) {
0786         if (port_iterator->load_flag & FC_UNLOADING)
0787             continue;
0788         if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
0789             lpfc_printf_vlog(port_iterator, KERN_ERR,
0790                      LOG_TRACE_EVENT,
0791                      "1801 Create vport work array FAILED: "
0792                      "cannot do scsi_host_get\n");
0793             continue;
0794         }
0795         vports[index++] = port_iterator;
0796     }
0797     spin_unlock_irq(&phba->port_list_lock);
0798     return vports;
0799 }
0800 
0801 void
0802 lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
0803 {
0804     int i;
0805     if (vports == NULL)
0806         return;
0807     for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
0808         scsi_host_put(lpfc_shost_from_vport(vports[i]));
0809     kfree(vports);
0810 }
0811 
0812 
0813 /**
0814  * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
0815  * @vport: Pointer to vport object.
0816  *
0817  * This function resets the statistical data for the vport. This function
0818  * is called with the host_lock held
0819  **/
0820 void
0821 lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
0822 {
0823     struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
0824 
0825     list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
0826         if (ndlp->lat_data)
0827             memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
0828                 sizeof(struct lpfc_scsicmd_bkt));
0829     }
0830 }
0831 
0832 
0833 /**
0834  * lpfc_alloc_bucket - Allocate data buffer required for statistical data
0835  * @vport: Pointer to vport object.
0836  *
0837  * This function allocates data buffer required for all the FC
0838  * nodes of the vport to collect statistical data.
0839  **/
0840 void
0841 lpfc_alloc_bucket(struct lpfc_vport *vport)
0842 {
0843     struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
0844 
0845     list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
0846 
0847         kfree(ndlp->lat_data);
0848         ndlp->lat_data = NULL;
0849 
0850         if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
0851             ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
0852                      sizeof(struct lpfc_scsicmd_bkt),
0853                      GFP_ATOMIC);
0854 
0855             if (!ndlp->lat_data)
0856                 lpfc_printf_vlog(vport, KERN_ERR,
0857                     LOG_TRACE_EVENT,
0858                     "0287 lpfc_alloc_bucket failed to "
0859                     "allocate statistical data buffer DID "
0860                     "0x%x\n", ndlp->nlp_DID);
0861         }
0862     }
0863 }
0864 
0865 /**
0866  * lpfc_free_bucket - Free data buffer required for statistical data
0867  * @vport: Pointer to vport object.
0868  *
0869  * Th function frees statistical data buffer of all the FC
0870  * nodes of the vport.
0871  **/
0872 void
0873 lpfc_free_bucket(struct lpfc_vport *vport)
0874 {
0875     struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
0876 
0877     list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
0878 
0879         kfree(ndlp->lat_data);
0880         ndlp->lat_data = NULL;
0881     }
0882 }