xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_vport.c (revision 7b7fd0ac7dc1ffcaf24d9bca0f051b0168e43cd4)
192d7f7b0SJames Smart /*******************************************************************
292d7f7b0SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
392d7f7b0SJames Smart  * Fibre Channel Host Bus Adapters.                                *
4191b5a38SJustin Tee  * Copyright (C) 2017-2023 Broadcom. All Rights Reserved. The term *
54ae2ebdeSJames Smart  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
650611577SJames Smart  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
792d7f7b0SJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
8d080abe0SJames Smart  * www.broadcom.com                                                *
992d7f7b0SJames Smart  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
1092d7f7b0SJames Smart  *                                                                 *
1192d7f7b0SJames Smart  * This program is free software; you can redistribute it and/or   *
1292d7f7b0SJames Smart  * modify it under the terms of version 2 of the GNU General       *
1392d7f7b0SJames Smart  * Public License as published by the Free Software Foundation.    *
1492d7f7b0SJames Smart  * This program is distributed in the hope that it will be useful. *
1592d7f7b0SJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
1692d7f7b0SJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
1792d7f7b0SJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
1892d7f7b0SJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
1992d7f7b0SJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
2092d7f7b0SJames Smart  * more details, a copy of which can be found in the file COPYING  *
2192d7f7b0SJames Smart  * included with this package.                                     *
2292d7f7b0SJames Smart  *******************************************************************/
2392d7f7b0SJames Smart 
2492d7f7b0SJames Smart #include <linux/blkdev.h>
2592d7f7b0SJames Smart #include <linux/delay.h>
2692d7f7b0SJames Smart #include <linux/dma-mapping.h>
2792d7f7b0SJames Smart #include <linux/idr.h>
2892d7f7b0SJames Smart #include <linux/interrupt.h>
2992d7f7b0SJames Smart #include <linux/kthread.h>
3092d7f7b0SJames Smart #include <linux/pci.h>
315a0e3ad6STejun Heo #include <linux/slab.h>
3292d7f7b0SJames Smart #include <linux/spinlock.h>
33174cd4b1SIngo Molnar #include <linux/sched/signal.h>
3492d7f7b0SJames Smart 
3592d7f7b0SJames Smart #include <scsi/scsi.h>
3692d7f7b0SJames Smart #include <scsi/scsi_device.h>
3792d7f7b0SJames Smart #include <scsi/scsi_host.h>
3892d7f7b0SJames Smart #include <scsi/scsi_transport_fc.h>
392ea259eeSJames Smart 
40da0436e9SJames Smart #include "lpfc_hw4.h"
4192d7f7b0SJames Smart #include "lpfc_hw.h"
4292d7f7b0SJames Smart #include "lpfc_sli.h"
43da0436e9SJames Smart #include "lpfc_sli4.h"
44ea2151b4SJames Smart #include "lpfc_nl.h"
4592d7f7b0SJames Smart #include "lpfc_disc.h"
4692d7f7b0SJames Smart #include "lpfc_scsi.h"
4792d7f7b0SJames Smart #include "lpfc.h"
4892d7f7b0SJames Smart #include "lpfc_logmsg.h"
4992d7f7b0SJames Smart #include "lpfc_crtn.h"
5092d7f7b0SJames Smart #include "lpfc_version.h"
5192d7f7b0SJames Smart #include "lpfc_vport.h"
5292d7f7b0SJames Smart 
lpfc_vport_set_state(struct lpfc_vport * vport,enum fc_vport_state new_state)5392d7f7b0SJames Smart inline void lpfc_vport_set_state(struct lpfc_vport *vport,
5492d7f7b0SJames Smart 				 enum fc_vport_state new_state)
5592d7f7b0SJames Smart {
5692d7f7b0SJames Smart 	struct fc_vport *fc_vport = vport->fc_vport;
5792d7f7b0SJames Smart 
5892d7f7b0SJames Smart 	if (fc_vport) {
5992d7f7b0SJames Smart 		/*
6092d7f7b0SJames Smart 		 * When the transport defines fc_vport_set state we will replace
6192d7f7b0SJames Smart 		 * this code with the following line
6292d7f7b0SJames Smart 		 */
6392d7f7b0SJames Smart 		/* fc_vport_set_state(fc_vport, new_state); */
6492d7f7b0SJames Smart 		if (new_state != FC_VPORT_INITIALIZING)
6592d7f7b0SJames Smart 			fc_vport->vport_last_state = fc_vport->vport_state;
6692d7f7b0SJames Smart 		fc_vport->vport_state = new_state;
6792d7f7b0SJames Smart 	}
6892d7f7b0SJames Smart 
6992d7f7b0SJames Smart 	/* for all the error states we will set the invternal state to FAILED */
7092d7f7b0SJames Smart 	switch (new_state) {
7192d7f7b0SJames Smart 	case FC_VPORT_NO_FABRIC_SUPP:
7292d7f7b0SJames Smart 	case FC_VPORT_NO_FABRIC_RSCS:
7392d7f7b0SJames Smart 	case FC_VPORT_FABRIC_LOGOUT:
7492d7f7b0SJames Smart 	case FC_VPORT_FABRIC_REJ_WWN:
7592d7f7b0SJames Smart 	case FC_VPORT_FAILED:
7692d7f7b0SJames Smart 		vport->port_state = LPFC_VPORT_FAILED;
7792d7f7b0SJames Smart 		break;
7892d7f7b0SJames Smart 	case FC_VPORT_LINKDOWN:
7992d7f7b0SJames Smart 		vport->port_state = LPFC_VPORT_UNKNOWN;
8092d7f7b0SJames Smart 		break;
8192d7f7b0SJames Smart 	default:
8292d7f7b0SJames Smart 		/* do nothing */
8392d7f7b0SJames Smart 		break;
8492d7f7b0SJames Smart 	}
8592d7f7b0SJames Smart }
8692d7f7b0SJames Smart 
8716a3a208SJames Smart int
lpfc_alloc_vpi(struct lpfc_hba * phba)8892d7f7b0SJames Smart lpfc_alloc_vpi(struct lpfc_hba *phba)
8992d7f7b0SJames Smart {
906d368e53SJames Smart 	unsigned long vpi;
9192d7f7b0SJames Smart 
9292d7f7b0SJames Smart 	spin_lock_irq(&phba->hbalock);
93858c9f6cSJames Smart 	/* Start at bit 1 because vpi zero is reserved for the physical port */
94858c9f6cSJames Smart 	vpi = find_next_zero_bit(phba->vpi_bmask, (phba->max_vpi + 1), 1);
9592d7f7b0SJames Smart 	if (vpi > phba->max_vpi)
9692d7f7b0SJames Smart 		vpi = 0;
9792d7f7b0SJames Smart 	else
9892d7f7b0SJames Smart 		set_bit(vpi, phba->vpi_bmask);
99da0436e9SJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4)
100da0436e9SJames Smart 		phba->sli4_hba.max_cfg_param.vpi_used++;
10192d7f7b0SJames Smart 	spin_unlock_irq(&phba->hbalock);
10292d7f7b0SJames Smart 	return vpi;
10392d7f7b0SJames Smart }
10492d7f7b0SJames Smart 
10592d7f7b0SJames Smart static void
lpfc_free_vpi(struct lpfc_hba * phba,int vpi)10692d7f7b0SJames Smart lpfc_free_vpi(struct lpfc_hba *phba, int vpi)
10792d7f7b0SJames Smart {
108da0436e9SJames Smart 	if (vpi == 0)
109da0436e9SJames Smart 		return;
11092d7f7b0SJames Smart 	spin_lock_irq(&phba->hbalock);
11192d7f7b0SJames Smart 	clear_bit(vpi, phba->vpi_bmask);
112da0436e9SJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4)
113da0436e9SJames Smart 		phba->sli4_hba.max_cfg_param.vpi_used--;
11492d7f7b0SJames Smart 	spin_unlock_irq(&phba->hbalock);
11592d7f7b0SJames Smart }
11692d7f7b0SJames Smart 
11792d7f7b0SJames Smart static int
lpfc_vport_sparm(struct lpfc_hba * phba,struct lpfc_vport * vport)11892d7f7b0SJames Smart lpfc_vport_sparm(struct lpfc_hba *phba, struct lpfc_vport *vport)
11992d7f7b0SJames Smart {
12092d7f7b0SJames Smart 	LPFC_MBOXQ_t *pmb;
12192d7f7b0SJames Smart 	MAILBOX_t *mb;
12292d7f7b0SJames Smart 	struct lpfc_dmabuf *mp;
12392d7f7b0SJames Smart 	int  rc;
12492d7f7b0SJames Smart 
12592d7f7b0SJames Smart 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12692d7f7b0SJames Smart 	if (!pmb) {
12792d7f7b0SJames Smart 		return -ENOMEM;
12892d7f7b0SJames Smart 	}
129f4b4c68fSJames Smart 	mb = &pmb->u.mb;
13092d7f7b0SJames Smart 
1319f1177a3SJames Smart 	rc = lpfc_read_sparam(phba, pmb, vport->vpi);
1329f1177a3SJames Smart 	if (rc) {
1339f1177a3SJames Smart 		mempool_free(pmb, phba->mbox_mem_pool);
1349f1177a3SJames Smart 		return -ENOMEM;
1359f1177a3SJames Smart 	}
1369f1177a3SJames Smart 
13792d7f7b0SJames Smart 	/*
138ef47575fSJames Smart 	 * Wait for the read_sparams mailbox to complete.  Driver needs
139ef47575fSJames Smart 	 * this per vport to start the FDISC.  If the mailbox fails,
140ef47575fSJames Smart 	 * just cleanup and return an error unless the failure is a
141ef47575fSJames Smart 	 * mailbox timeout.  For MBX_TIMEOUT, allow the default
142ef47575fSJames Smart 	 * mbox completion handler to take care of the cleanup.  This
143ef47575fSJames Smart 	 * is safe as the mailbox command isn't one that triggers
144ef47575fSJames Smart 	 * another mailbox.
14592d7f7b0SJames Smart 	 */
14692d7f7b0SJames Smart 	pmb->vport = vport;
14792d7f7b0SJames Smart 	rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
14892d7f7b0SJames Smart 	if (rc != MBX_SUCCESS) {
14998c9ea5cSJames Smart 		if (signal_pending(current)) {
150372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
15198c9ea5cSJames Smart 					 "1830 Signal aborted mbxCmd x%x\n",
15298c9ea5cSJames Smart 					 mb->mbxCommand);
15398c9ea5cSJames Smart 			if (rc != MBX_TIMEOUT)
154ef47575fSJames Smart 				lpfc_mbox_rsrc_cleanup(phba, pmb,
155ef47575fSJames Smart 						       MBOX_THD_UNLOCKED);
15698c9ea5cSJames Smart 			return -EINTR;
15798c9ea5cSJames Smart 		} else {
158372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
159e8b62011SJames Smart 					 "1818 VPort failed init, mbxCmd x%x "
16092d7f7b0SJames Smart 					 "READ_SPARM mbxStatus x%x, rc = x%x\n",
16192d7f7b0SJames Smart 					 mb->mbxCommand, mb->mbxStatus, rc);
162858c9f6cSJames Smart 			if (rc != MBX_TIMEOUT)
163ef47575fSJames Smart 				lpfc_mbox_rsrc_cleanup(phba, pmb,
164ef47575fSJames Smart 						       MBOX_THD_UNLOCKED);
16592d7f7b0SJames Smart 			return -EIO;
16692d7f7b0SJames Smart 		}
16798c9ea5cSJames Smart 	}
16892d7f7b0SJames Smart 
169ef47575fSJames Smart 	mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
17092d7f7b0SJames Smart 	memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
17192d7f7b0SJames Smart 	memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
17292d7f7b0SJames Smart 	       sizeof (struct lpfc_name));
17392d7f7b0SJames Smart 	memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
17492d7f7b0SJames Smart 	       sizeof (struct lpfc_name));
175ef47575fSJames Smart 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
17692d7f7b0SJames Smart 	return 0;
17792d7f7b0SJames Smart }
17892d7f7b0SJames Smart 
17992d7f7b0SJames Smart static int
lpfc_valid_wwn_format(struct lpfc_hba * phba,struct lpfc_name * wwn,const char * name_type)18092d7f7b0SJames Smart lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn,
18192d7f7b0SJames Smart 		      const char *name_type)
18292d7f7b0SJames Smart {
18392d7f7b0SJames Smart 				/* ensure that IEEE format 1 addresses
18492d7f7b0SJames Smart 				 * contain zeros in bits 59-48
18592d7f7b0SJames Smart 				 */
18692d7f7b0SJames Smart 	if (!((wwn->u.wwn[0] >> 4) == 1 &&
18792d7f7b0SJames Smart 	      ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0)))
18892d7f7b0SJames Smart 		return 1;
18992d7f7b0SJames Smart 
190cdf81160SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
191e8b62011SJames Smart 			"1822 Invalid %s: %02x:%02x:%02x:%02x:"
19292d7f7b0SJames Smart 			"%02x:%02x:%02x:%02x\n",
193e8b62011SJames Smart 			name_type,
19492d7f7b0SJames Smart 			wwn->u.wwn[0], wwn->u.wwn[1],
19592d7f7b0SJames Smart 			wwn->u.wwn[2], wwn->u.wwn[3],
19692d7f7b0SJames Smart 			wwn->u.wwn[4], wwn->u.wwn[5],
19792d7f7b0SJames Smart 			wwn->u.wwn[6], wwn->u.wwn[7]);
19892d7f7b0SJames Smart 	return 0;
19992d7f7b0SJames Smart }
20092d7f7b0SJames Smart 
20192d7f7b0SJames Smart static int
lpfc_unique_wwpn(struct lpfc_hba * phba,struct lpfc_vport * new_vport)20292d7f7b0SJames Smart lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
20392d7f7b0SJames Smart {
20492d7f7b0SJames Smart 	struct lpfc_vport *vport;
205549e55cdSJames Smart 	unsigned long flags;
20692d7f7b0SJames Smart 
207523128e5SJames Smart 	spin_lock_irqsave(&phba->port_list_lock, flags);
20892d7f7b0SJames Smart 	list_for_each_entry(vport, &phba->port_list, listentry) {
20992d7f7b0SJames Smart 		if (vport == new_vport)
21092d7f7b0SJames Smart 			continue;
21192d7f7b0SJames Smart 		/* If they match, return not unique */
21292d7f7b0SJames Smart 		if (memcmp(&vport->fc_sparam.portName,
21392d7f7b0SJames Smart 			   &new_vport->fc_sparam.portName,
214549e55cdSJames Smart 			   sizeof(struct lpfc_name)) == 0) {
215523128e5SJames Smart 			spin_unlock_irqrestore(&phba->port_list_lock, flags);
21692d7f7b0SJames Smart 			return 0;
21792d7f7b0SJames Smart 		}
218549e55cdSJames Smart 	}
219523128e5SJames Smart 	spin_unlock_irqrestore(&phba->port_list_lock, flags);
22092d7f7b0SJames Smart 	return 1;
22192d7f7b0SJames Smart }
22292d7f7b0SJames Smart 
22390160e01SJames Smart /**
2243621a710SJames Smart  * lpfc_discovery_wait - Wait for driver discovery to quiesce
22590160e01SJames Smart  * @vport: The virtual port for which this call is being executed.
22690160e01SJames Smart  *
22790160e01SJames Smart  * This driver calls this routine specifically from lpfc_vport_delete
22890160e01SJames Smart  * to enforce a synchronous execution of vport
22990160e01SJames Smart  * delete relative to discovery activities.  The
23090160e01SJames Smart  * lpfc_vport_delete routine should not return until it
23190160e01SJames Smart  * can reasonably guarantee that discovery has quiesced.
23290160e01SJames Smart  * Post FDISC LOGO, the driver must wait until its SAN teardown is
23390160e01SJames Smart  * complete and all resources recovered before allowing
23490160e01SJames Smart  * cleanup.
23590160e01SJames Smart  *
23690160e01SJames Smart  * This routine does not require any locks held.
23790160e01SJames Smart  **/
lpfc_discovery_wait(struct lpfc_vport * vport)23890160e01SJames Smart static void lpfc_discovery_wait(struct lpfc_vport *vport)
23990160e01SJames Smart {
24090160e01SJames Smart 	struct lpfc_hba *phba = vport->phba;
24190160e01SJames Smart 	uint32_t wait_flags = 0;
24290160e01SJames Smart 	unsigned long wait_time_max;
24390160e01SJames Smart 	unsigned long start_time;
24490160e01SJames Smart 
24590160e01SJames Smart 	wait_flags = FC_RSCN_MODE | FC_RSCN_DISCOVERY | FC_NLP_MORE |
24690160e01SJames Smart 		     FC_RSCN_DEFERRED | FC_NDISC_ACTIVE | FC_DISC_TMO;
24790160e01SJames Smart 
24890160e01SJames Smart 	/*
24990160e01SJames Smart 	 * The time constraint on this loop is a balance between the
25090160e01SJames Smart 	 * fabric RA_TOV value and dev_loss tmo.  The driver's
25190160e01SJames Smart 	 * devloss_tmo is 10 giving this loop a 3x multiplier minimally.
25290160e01SJames Smart 	 */
25390160e01SJames Smart 	wait_time_max = msecs_to_jiffies(((phba->fc_ratov * 3) + 3) * 1000);
25490160e01SJames Smart 	wait_time_max += jiffies;
25590160e01SJames Smart 	start_time = jiffies;
25690160e01SJames Smart 	while (time_before(jiffies, wait_time_max)) {
25790160e01SJames Smart 		if ((vport->num_disc_nodes > 0)    ||
25890160e01SJames Smart 		    (vport->fc_flag & wait_flags)  ||
25990160e01SJames Smart 		    ((vport->port_state > LPFC_VPORT_FAILED) &&
26090160e01SJames Smart 		     (vport->port_state < LPFC_VPORT_READY))) {
26121e9a0a5SJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
26290160e01SJames Smart 					"1833 Vport discovery quiesce Wait:"
26321e9a0a5SJames Smart 					" state x%x fc_flags x%x"
26490160e01SJames Smart 					" num_nodes x%x, waiting 1000 msecs"
26590160e01SJames Smart 					" total wait msecs x%x\n",
26621e9a0a5SJames Smart 					vport->port_state, vport->fc_flag,
26721e9a0a5SJames Smart 					vport->num_disc_nodes,
26890160e01SJames Smart 					jiffies_to_msecs(jiffies - start_time));
26990160e01SJames Smart 			msleep(1000);
27090160e01SJames Smart 		} else {
27190160e01SJames Smart 			/* Base case.  Wait variants satisfied.  Break out */
27221e9a0a5SJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
27390160e01SJames Smart 					 "1834 Vport discovery quiesced:"
27421e9a0a5SJames Smart 					 " state x%x fc_flags x%x"
27590160e01SJames Smart 					 " wait msecs x%x\n",
27621e9a0a5SJames Smart 					 vport->port_state, vport->fc_flag,
27790160e01SJames Smart 					 jiffies_to_msecs(jiffies
27890160e01SJames Smart 						- start_time));
27990160e01SJames Smart 			break;
28090160e01SJames Smart 		}
28190160e01SJames Smart 	}
28290160e01SJames Smart 
28390160e01SJames Smart 	if (time_after(jiffies, wait_time_max))
284372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
28590160e01SJames Smart 				 "1835 Vport discovery quiesce failed:"
28621e9a0a5SJames Smart 				 " state x%x fc_flags x%x wait msecs x%x\n",
28721e9a0a5SJames Smart 				 vport->port_state, vport->fc_flag,
28890160e01SJames Smart 				 jiffies_to_msecs(jiffies - start_time));
28990160e01SJames Smart }
29090160e01SJames Smart 
29192d7f7b0SJames Smart int
lpfc_vport_create(struct fc_vport * fc_vport,bool disable)29292d7f7b0SJames Smart lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
29392d7f7b0SJames Smart {
29492d7f7b0SJames Smart 	struct lpfc_nodelist *ndlp;
2953de2a653SJames Smart 	struct Scsi_Host *shost = fc_vport->shost;
2963de2a653SJames Smart 	struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata;
29792d7f7b0SJames Smart 	struct lpfc_hba   *phba = pport->phba;
29892d7f7b0SJames Smart 	struct lpfc_vport *vport = NULL;
29992d7f7b0SJames Smart 	int instance;
30092d7f7b0SJames Smart 	int vpi;
30192d7f7b0SJames Smart 	int rc = VPORT_ERROR;
30298c9ea5cSJames Smart 	int status;
30392d7f7b0SJames Smart 
304eada272dSJames Smart 	if ((phba->sli_rev < 3) || !(phba->cfg_enable_npiv)) {
305372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
306e8b62011SJames Smart 				"1808 Create VPORT failed: "
30792d7f7b0SJames Smart 				"NPIV is not enabled: SLImode:%d\n",
308e8b62011SJames Smart 				phba->sli_rev);
30992d7f7b0SJames Smart 		rc = VPORT_INVAL;
31092d7f7b0SJames Smart 		goto error_out;
31192d7f7b0SJames Smart 	}
31292d7f7b0SJames Smart 
313f6e84790SJames Smart 	/* NPIV is not supported if HBA has NVME Target enabled */
314f6e84790SJames Smart 	if (phba->nvmet_support) {
315372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
31642270dceSDick Kennedy 				"3189 Create VPORT failed: "
317f6e84790SJames Smart 				"NPIV is not supported on NVME Target\n");
31842270dceSDick Kennedy 		rc = VPORT_INVAL;
31942270dceSDick Kennedy 		goto error_out;
32042270dceSDick Kennedy 	}
32142270dceSDick Kennedy 
32292d7f7b0SJames Smart 	vpi = lpfc_alloc_vpi(phba);
32392d7f7b0SJames Smart 	if (vpi == 0) {
324372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
325e8b62011SJames Smart 				"1809 Create VPORT failed: "
32692d7f7b0SJames Smart 				"Max VPORTs (%d) exceeded\n",
327e8b62011SJames Smart 				phba->max_vpi);
32892d7f7b0SJames Smart 		rc = VPORT_NORESOURCES;
32992d7f7b0SJames Smart 		goto error_out;
33092d7f7b0SJames Smart 	}
33192d7f7b0SJames Smart 
33292d7f7b0SJames Smart 	/* Assign an unused board number */
33392d7f7b0SJames Smart 	if ((instance = lpfc_get_instance()) < 0) {
334372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
335e8b62011SJames Smart 				"1810 Create VPORT failed: Cannot get "
336e8b62011SJames Smart 				"instance number\n");
33792d7f7b0SJames Smart 		lpfc_free_vpi(phba, vpi);
33892d7f7b0SJames Smart 		rc = VPORT_NORESOURCES;
33992d7f7b0SJames Smart 		goto error_out;
34092d7f7b0SJames Smart 	}
34192d7f7b0SJames Smart 
3423de2a653SJames Smart 	vport = lpfc_create_port(phba, instance, &fc_vport->dev);
34392d7f7b0SJames Smart 	if (!vport) {
344372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
345e8b62011SJames Smart 				"1811 Create VPORT failed: vpi x%x\n", vpi);
34692d7f7b0SJames Smart 		lpfc_free_vpi(phba, vpi);
34792d7f7b0SJames Smart 		rc = VPORT_NORESOURCES;
34892d7f7b0SJames Smart 		goto error_out;
34992d7f7b0SJames Smart 	}
35092d7f7b0SJames Smart 
35192d7f7b0SJames Smart 	vport->vpi = vpi;
352858c9f6cSJames Smart 	lpfc_debugfs_initialize(vport);
353858c9f6cSJames Smart 
35498c9ea5cSJames Smart 	if ((status = lpfc_vport_sparm(phba, vport))) {
35598c9ea5cSJames Smart 		if (status == -EINTR) {
356372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
35798c9ea5cSJames Smart 					 "1831 Create VPORT Interrupted.\n");
35898c9ea5cSJames Smart 			rc = VPORT_ERROR;
35998c9ea5cSJames Smart 		} else {
360372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
361e8b62011SJames Smart 					 "1813 Create VPORT failed. "
362e8b62011SJames Smart 					 "Cannot get sparam\n");
36398c9ea5cSJames Smart 			rc = VPORT_NORESOURCES;
36498c9ea5cSJames Smart 		}
36592d7f7b0SJames Smart 		lpfc_free_vpi(phba, vpi);
36692d7f7b0SJames Smart 		destroy_port(vport);
36792d7f7b0SJames Smart 		goto error_out;
36892d7f7b0SJames Smart 	}
36992d7f7b0SJames Smart 
37092d7f7b0SJames Smart 	u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
37192d7f7b0SJames Smart 	u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
37292d7f7b0SJames Smart 
37392d7f7b0SJames Smart 	memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
37492d7f7b0SJames Smart 	memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
37592d7f7b0SJames Smart 
37692d7f7b0SJames Smart 	if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
37792d7f7b0SJames Smart 	    !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
378372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
379e8b62011SJames Smart 				 "1821 Create VPORT failed. "
380e8b62011SJames Smart 				 "Invalid WWN format\n");
38192d7f7b0SJames Smart 		lpfc_free_vpi(phba, vpi);
38292d7f7b0SJames Smart 		destroy_port(vport);
38392d7f7b0SJames Smart 		rc = VPORT_INVAL;
38492d7f7b0SJames Smart 		goto error_out;
38592d7f7b0SJames Smart 	}
38692d7f7b0SJames Smart 
38792d7f7b0SJames Smart 	if (!lpfc_unique_wwpn(phba, vport)) {
388372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
389e8b62011SJames Smart 				 "1823 Create VPORT failed. "
390e8b62011SJames Smart 				 "Duplicate WWN on HBA\n");
39192d7f7b0SJames Smart 		lpfc_free_vpi(phba, vpi);
39292d7f7b0SJames Smart 		destroy_port(vport);
39392d7f7b0SJames Smart 		rc = VPORT_INVAL;
39492d7f7b0SJames Smart 		goto error_out;
39592d7f7b0SJames Smart 	}
39692d7f7b0SJames Smart 
397eada272dSJames Smart 	/* Create binary sysfs attribute for vport */
398eada272dSJames Smart 	lpfc_alloc_sysfs_attr(vport);
399eada272dSJames Smart 
400572709e2SJames Smart 	/* Set the DFT_LUN_Q_DEPTH accordingly */
401572709e2SJames Smart 	vport->cfg_lun_queue_depth  = phba->pport->cfg_lun_queue_depth;
402572709e2SJames Smart 
403f6e84790SJames Smart 	/* Only the physical port can support NVME for now */
404f6e84790SJames Smart 	vport->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
405f6e84790SJames Smart 
40692d7f7b0SJames Smart 	*(struct lpfc_vport **)fc_vport->dd_data = vport;
40792d7f7b0SJames Smart 	vport->fc_vport = fc_vport;
40892d7f7b0SJames Smart 
4094258e98eSJames Smart 	/* At this point we are fully registered with SCSI Layer.  */
4104258e98eSJames Smart 	vport->load_flag |= FC_ALLOW_FDMI;
4118663cbbeSJames Smart 	if (phba->cfg_enable_SmartSAN ||
4128663cbbeSJames Smart 	    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
4134258e98eSJames Smart 		/* Setup appropriate attribute masks */
4144258e98eSJames Smart 		vport->fdmi_hba_mask = phba->pport->fdmi_hba_mask;
4154258e98eSJames Smart 		vport->fdmi_port_mask = phba->pport->fdmi_port_mask;
4164258e98eSJames Smart 	}
4174258e98eSJames Smart 
4181c6834a7SJames Smart 	/*
4191c6834a7SJames Smart 	 * In SLI4, the vpi must be activated before it can be used
4201c6834a7SJames Smart 	 * by the port.
4211c6834a7SJames Smart 	 */
4221c6834a7SJames Smart 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
423695a814eSJames Smart 	    (pport->fc_flag & FC_VFI_REGISTERED)) {
42476a95d75SJames Smart 		rc = lpfc_sli4_init_vpi(vport);
4251c6834a7SJames Smart 		if (rc) {
426372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4271c6834a7SJames Smart 					"1838 Failed to INIT_VPI on vpi %d "
4281c6834a7SJames Smart 					"status %d\n", vpi, rc);
4291c6834a7SJames Smart 			rc = VPORT_NORESOURCES;
4301c6834a7SJames Smart 			lpfc_free_vpi(phba, vpi);
4311c6834a7SJames Smart 			goto error_out;
4321c6834a7SJames Smart 		}
4331c6834a7SJames Smart 	} else if (phba->sli_rev == LPFC_SLI_REV4) {
4341c6834a7SJames Smart 		/*
4351c6834a7SJames Smart 		 * Driver cannot INIT_VPI now. Set the flags to
4361c6834a7SJames Smart 		 * init_vpi when reg_vfi complete.
4371c6834a7SJames Smart 		 */
4381c6834a7SJames Smart 		vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
4391c6834a7SJames Smart 		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
4401c6834a7SJames Smart 		rc = VPORT_OK;
4411c6834a7SJames Smart 		goto out;
4421c6834a7SJames Smart 	}
4431c6834a7SJames Smart 
44492d7f7b0SJames Smart 	if ((phba->link_state < LPFC_LINK_UP) ||
4451c6834a7SJames Smart 	    (pport->port_state < LPFC_FABRIC_CFG_LINK) ||
44676a95d75SJames Smart 	    (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
44792d7f7b0SJames Smart 		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
44892d7f7b0SJames Smart 		rc = VPORT_OK;
44992d7f7b0SJames Smart 		goto out;
45092d7f7b0SJames Smart 	}
45192d7f7b0SJames Smart 
45292d7f7b0SJames Smart 	if (disable) {
453eada272dSJames Smart 		lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
45492d7f7b0SJames Smart 		rc = VPORT_OK;
45592d7f7b0SJames Smart 		goto out;
45692d7f7b0SJames Smart 	}
45792d7f7b0SJames Smart 
45892d7f7b0SJames Smart 	/* Use the Physical nodes Fabric NDLP to determine if the link is
45992d7f7b0SJames Smart 	 * up and ready to FDISC.
46092d7f7b0SJames Smart 	 */
46192d7f7b0SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
462307e3380SJames Smart 	if (ndlp &&
463e47c9093SJames Smart 	    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
464858c9f6cSJames Smart 		if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
46592d7f7b0SJames Smart 			lpfc_set_disctmo(vport);
46692d7f7b0SJames Smart 			lpfc_initial_fdisc(vport);
46792d7f7b0SJames Smart 		} else {
468858c9f6cSJames Smart 			lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
469372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
470e8b62011SJames Smart 					 "0262 No NPIV Fabric support\n");
471858c9f6cSJames Smart 		}
472858c9f6cSJames Smart 	} else {
47392d7f7b0SJames Smart 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
47492d7f7b0SJames Smart 	}
47592d7f7b0SJames Smart 	rc = VPORT_OK;
47692d7f7b0SJames Smart 
47792d7f7b0SJames Smart out:
478ff8a44bfSJames Smart 	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
479a58cbd52SJames Smart 			 "1825 Vport Created.\n");
48092d7f7b0SJames Smart 	lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
48192d7f7b0SJames Smart error_out:
48292d7f7b0SJames Smart 	return rc;
48392d7f7b0SJames Smart }
48492d7f7b0SJames Smart 
485311464ecSJames Smart static int
lpfc_send_npiv_logo(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)4868ed190a9SJames Smart lpfc_send_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4878ed190a9SJames Smart {
4888ed190a9SJames Smart 	int rc;
4898ed190a9SJames Smart 	struct lpfc_hba *phba = vport->phba;
4908ed190a9SJames Smart 
4918ed190a9SJames Smart 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
4928ed190a9SJames Smart 
4938ed190a9SJames Smart 	spin_lock_irq(&ndlp->lock);
4948ed190a9SJames Smart 	if (!(ndlp->save_flags & NLP_WAIT_FOR_LOGO) &&
4958ed190a9SJames Smart 	    !ndlp->logo_waitq) {
4968ed190a9SJames Smart 		ndlp->logo_waitq = &waitq;
4978ed190a9SJames Smart 		ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
4988ed190a9SJames Smart 		ndlp->nlp_flag |= NLP_ISSUE_LOGO;
4998ed190a9SJames Smart 		ndlp->save_flags |= NLP_WAIT_FOR_LOGO;
5008ed190a9SJames Smart 	}
5018ed190a9SJames Smart 	spin_unlock_irq(&ndlp->lock);
5028ed190a9SJames Smart 	rc = lpfc_issue_els_npiv_logo(vport, ndlp);
5038ed190a9SJames Smart 	if (!rc) {
5048ed190a9SJames Smart 		wait_event_timeout(waitq,
5058ed190a9SJames Smart 				   (!(ndlp->save_flags & NLP_WAIT_FOR_LOGO)),
5068ed190a9SJames Smart 				   msecs_to_jiffies(phba->fc_ratov * 2000));
5078ed190a9SJames Smart 
5088ed190a9SJames Smart 		if (!(ndlp->save_flags & NLP_WAIT_FOR_LOGO))
5098ed190a9SJames Smart 			goto logo_cmpl;
5108ed190a9SJames Smart 		/* LOGO wait failed.  Correct status. */
5118ed190a9SJames Smart 		rc = -EINTR;
5128ed190a9SJames Smart 	} else {
5138ed190a9SJames Smart 		rc = -EIO;
5148ed190a9SJames Smart 	}
5158ed190a9SJames Smart 
5168ed190a9SJames Smart 	/* Error - clean up node flags. */
5178ed190a9SJames Smart 	spin_lock_irq(&ndlp->lock);
5188ed190a9SJames Smart 	ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
5198ed190a9SJames Smart 	ndlp->save_flags &= ~NLP_WAIT_FOR_LOGO;
5208ed190a9SJames Smart 	spin_unlock_irq(&ndlp->lock);
5218ed190a9SJames Smart 
5228ed190a9SJames Smart  logo_cmpl:
5238ed190a9SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
5248ed190a9SJames Smart 			 "1824 Issue LOGO completes with status %d\n",
5258ed190a9SJames Smart 			 rc);
5268ed190a9SJames Smart 	spin_lock_irq(&ndlp->lock);
5278ed190a9SJames Smart 	ndlp->logo_waitq = NULL;
5288ed190a9SJames Smart 	spin_unlock_irq(&ndlp->lock);
5298ed190a9SJames Smart 	return rc;
5308ed190a9SJames Smart }
5318ed190a9SJames Smart 
5328ed190a9SJames Smart static int
disable_vport(struct fc_vport * fc_vport)53392d7f7b0SJames Smart disable_vport(struct fc_vport *fc_vport)
53492d7f7b0SJames Smart {
53592d7f7b0SJames Smart 	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
53692d7f7b0SJames Smart 	struct lpfc_hba   *phba = vport->phba;
537f8139557SJustin Tee 	struct lpfc_nodelist *ndlp = NULL;
538fedd3b7bSJames Smart 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
53992d7f7b0SJames Smart 
5408ed190a9SJames Smart 	/* Can't disable during an outstanding delete. */
5418ed190a9SJames Smart 	if (vport->load_flag & FC_UNLOADING)
5428ed190a9SJames Smart 		return 0;
5438ed190a9SJames Smart 
54492d7f7b0SJames Smart 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
5458ed190a9SJames Smart 	if (ndlp && phba->link_state >= LPFC_LINK_UP)
5468ed190a9SJames Smart 		(void)lpfc_send_npiv_logo(vport, ndlp);
54792d7f7b0SJames Smart 
54892d7f7b0SJames Smart 	lpfc_sli_host_down(vport);
549f8139557SJustin Tee 	lpfc_cleanup_rpis(vport, 0);
55092d7f7b0SJames Smart 
55192d7f7b0SJames Smart 	lpfc_stop_vport_timers(vport);
55292d7f7b0SJames Smart 	lpfc_unreg_all_rpis(vport);
55392d7f7b0SJames Smart 	lpfc_unreg_default_rpis(vport);
55492d7f7b0SJames Smart 	/*
55592d7f7b0SJames Smart 	 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
55692d7f7b0SJames Smart 	 * scsi_host_put() to release the vport.
55792d7f7b0SJames Smart 	 */
55892d7f7b0SJames Smart 	lpfc_mbx_unreg_vpi(vport);
55908180db2SJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4) {
560fedd3b7bSJames Smart 		spin_lock_irq(shost->host_lock);
561fedd3b7bSJames Smart 		vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
562fedd3b7bSJames Smart 		spin_unlock_irq(shost->host_lock);
56308180db2SJames Smart 	}
56492d7f7b0SJames Smart 
56592d7f7b0SJames Smart 	lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
566cdf81160SJames Smart 	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
567a58cbd52SJames Smart 			 "1826 Vport Disabled.\n");
56892d7f7b0SJames Smart 	return VPORT_OK;
56992d7f7b0SJames Smart }
57092d7f7b0SJames Smart 
571311464ecSJames Smart static int
enable_vport(struct fc_vport * fc_vport)57292d7f7b0SJames Smart enable_vport(struct fc_vport *fc_vport)
57392d7f7b0SJames Smart {
57492d7f7b0SJames Smart 	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
57592d7f7b0SJames Smart 	struct lpfc_hba   *phba = vport->phba;
57692d7f7b0SJames Smart 	struct lpfc_nodelist *ndlp = NULL;
57772100cc4SJames Smart 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
57892d7f7b0SJames Smart 
57992d7f7b0SJames Smart 	if ((phba->link_state < LPFC_LINK_UP) ||
58076a95d75SJames Smart 	    (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
58192d7f7b0SJames Smart 		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
58292d7f7b0SJames Smart 		return VPORT_OK;
58392d7f7b0SJames Smart 	}
58492d7f7b0SJames Smart 
58572100cc4SJames Smart 	spin_lock_irq(shost->host_lock);
58692d7f7b0SJames Smart 	vport->load_flag |= FC_LOADING;
587104450ebSJames Smart 	if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
588104450ebSJames Smart 		spin_unlock_irq(shost->host_lock);
589104450ebSJames Smart 		lpfc_issue_init_vpi(vport);
590104450ebSJames Smart 		goto out;
591104450ebSJames Smart 	}
592104450ebSJames Smart 
59392d7f7b0SJames Smart 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
59472100cc4SJames Smart 	spin_unlock_irq(shost->host_lock);
59592d7f7b0SJames Smart 
59692d7f7b0SJames Smart 	/* Use the Physical nodes Fabric NDLP to determine if the link is
59792d7f7b0SJames Smart 	 * up and ready to FDISC.
59892d7f7b0SJames Smart 	 */
59992d7f7b0SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
600307e3380SJames Smart 	if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
601858c9f6cSJames Smart 		if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
60292d7f7b0SJames Smart 			lpfc_set_disctmo(vport);
60392d7f7b0SJames Smart 			lpfc_initial_fdisc(vport);
60492d7f7b0SJames Smart 		} else {
605858c9f6cSJames Smart 			lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
606372c187bSDick Kennedy 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
607e8b62011SJames Smart 					 "0264 No NPIV Fabric support\n");
608858c9f6cSJames Smart 		}
609858c9f6cSJames Smart 	} else {
61092d7f7b0SJames Smart 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
61192d7f7b0SJames Smart 	}
612104450ebSJames Smart 
613104450ebSJames Smart out:
614cdf81160SJames Smart 	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
615a58cbd52SJames Smart 			 "1827 Vport Enabled.\n");
61692d7f7b0SJames Smart 	return VPORT_OK;
61792d7f7b0SJames Smart }
61892d7f7b0SJames Smart 
61992d7f7b0SJames Smart int
lpfc_vport_disable(struct fc_vport * fc_vport,bool disable)62092d7f7b0SJames Smart lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
62192d7f7b0SJames Smart {
62292d7f7b0SJames Smart 	if (disable)
62392d7f7b0SJames Smart 		return disable_vport(fc_vport);
62492d7f7b0SJames Smart 	else
62592d7f7b0SJames Smart 		return enable_vport(fc_vport);
62692d7f7b0SJames Smart }
62792d7f7b0SJames Smart 
62892d7f7b0SJames Smart int
lpfc_vport_delete(struct fc_vport * fc_vport)62992d7f7b0SJames Smart lpfc_vport_delete(struct fc_vport *fc_vport)
63092d7f7b0SJames Smart {
63192d7f7b0SJames Smart 	struct lpfc_nodelist *ndlp = NULL;
63292d7f7b0SJames Smart 	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
633cc82355aSJames Smart 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
63492d7f7b0SJames Smart 	struct lpfc_hba  *phba = vport->phba;
6358ed190a9SJames Smart 	int rc;
636*0ef6e016SJustin Tee 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
63792d7f7b0SJames Smart 
63851ef4c26SJames Smart 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
639372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
64051ef4c26SJames Smart 				 "1812 vport_delete failed: Cannot delete "
64151ef4c26SJames Smart 				 "physical host\n");
64251ef4c26SJames Smart 		return VPORT_ERROR;
64351ef4c26SJames Smart 	}
64421e9a0a5SJames Smart 
64521e9a0a5SJames Smart 	/* If the vport is a static vport fail the deletion. */
64621e9a0a5SJames Smart 	if ((vport->vport_flag & STATIC_VPORT) &&
64721e9a0a5SJames Smart 		!(phba->pport->load_flag & FC_UNLOADING)) {
648372c187bSDick Kennedy 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
64921e9a0a5SJames Smart 				 "1837 vport_delete failed: Cannot delete "
65021e9a0a5SJames Smart 				 "static vport.\n");
65121e9a0a5SJames Smart 		return VPORT_ERROR;
65221e9a0a5SJames Smart 	}
653e9b11083SJames Smart 
654d439d286SJames Smart 	spin_lock_irq(&phba->hbalock);
655d439d286SJames Smart 	vport->load_flag |= FC_UNLOADING;
656d439d286SJames Smart 	spin_unlock_irq(&phba->hbalock);
657e9b11083SJames Smart 
65851ef4c26SJames Smart 	/*
65951ef4c26SJames Smart 	 * If we are not unloading the driver then prevent the vport_delete
66051ef4c26SJames Smart 	 * from happening until after this vport's discovery is finished.
66151ef4c26SJames Smart 	 */
66251ef4c26SJames Smart 	if (!(phba->pport->load_flag & FC_UNLOADING)) {
66351ef4c26SJames Smart 		int check_count = 0;
66451ef4c26SJames Smart 		while (check_count < ((phba->fc_ratov * 3) + 3) &&
66551ef4c26SJames Smart 		       vport->port_state > LPFC_VPORT_FAILED &&
66651ef4c26SJames Smart 		       vport->port_state < LPFC_VPORT_READY) {
66751ef4c26SJames Smart 			check_count++;
66851ef4c26SJames Smart 			msleep(1000);
66951ef4c26SJames Smart 		}
67051ef4c26SJames Smart 		if (vport->port_state > LPFC_VPORT_FAILED &&
67151ef4c26SJames Smart 		    vport->port_state < LPFC_VPORT_READY)
67251ef4c26SJames Smart 			return -EAGAIN;
67351ef4c26SJames Smart 	}
67403dbfe06SDick Kennedy 
67592d7f7b0SJames Smart 	/*
67603dbfe06SDick Kennedy 	 * Take early refcount for outstanding I/O requests we schedule during
67703dbfe06SDick Kennedy 	 * delete processing for unreg_vpi.  Always keep this before
67803dbfe06SDick Kennedy 	 * scsi_remove_host() as we can no longer obtain a reference through
67903dbfe06SDick Kennedy 	 * scsi_host_get() after scsi_host_remove as shost is set to SHOST_DEL.
68092d7f7b0SJames Smart 	 */
681d7c255b2SJames Smart 	if (!scsi_host_get(shost))
68292d7f7b0SJames Smart 		return VPORT_INVAL;
68303dbfe06SDick Kennedy 
684eada272dSJames Smart 	lpfc_free_sysfs_attr(vport);
685858c9f6cSJames Smart 	lpfc_debugfs_terminate(vport);
686eada272dSJames Smart 
687e9b11083SJames Smart 	/* Send the DA_ID and Fabric LOGO to cleanup Nameserver entries. */
688e47c9093SJames Smart 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
689e47c9093SJames Smart 	if (!ndlp)
690e47c9093SJames Smart 		goto skip_logo;
691307e3380SJames Smart 
692*0ef6e016SJustin Tee 	/* Send the DA_ID and Fabric LOGO to cleanup the NPIV fabric entries. */
693307e3380SJames Smart 	if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
69458da1ffbSJames Smart 	    phba->link_state >= LPFC_LINK_UP &&
69576a95d75SJames Smart 	    phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
6967ee5d43eSJames Smart 		if (vport->cfg_enable_da_id) {
697*0ef6e016SJustin Tee 			/* Send DA_ID and wait for a completion.  This is best
698*0ef6e016SJustin Tee 			 * effort.  If the DA_ID fails, likely the fabric will
699*0ef6e016SJustin Tee 			 * "leak" NportIDs but at least the driver issued the
700*0ef6e016SJustin Tee 			 * command.
701*0ef6e016SJustin Tee 			 */
702*0ef6e016SJustin Tee 			ndlp = lpfc_findnode_did(vport, NameServer_DID);
703*0ef6e016SJustin Tee 			if (!ndlp)
704*0ef6e016SJustin Tee 				goto issue_logo;
705*0ef6e016SJustin Tee 
706*0ef6e016SJustin Tee 			spin_lock_irq(&ndlp->lock);
707*0ef6e016SJustin Tee 			ndlp->da_id_waitq = &waitq;
708*0ef6e016SJustin Tee 			ndlp->save_flags |= NLP_WAIT_FOR_DA_ID;
709*0ef6e016SJustin Tee 			spin_unlock_irq(&ndlp->lock);
710*0ef6e016SJustin Tee 
7118ed190a9SJames Smart 			rc = lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0);
712*0ef6e016SJustin Tee 			if (!rc) {
713*0ef6e016SJustin Tee 				wait_event_timeout(waitq,
714*0ef6e016SJustin Tee 				   !(ndlp->save_flags & NLP_WAIT_FOR_DA_ID),
715*0ef6e016SJustin Tee 				   msecs_to_jiffies(phba->fc_ratov * 2000));
7167ee5d43eSJames Smart 			}
71773d91e50SJames Smart 
718*0ef6e016SJustin Tee 			lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT | LOG_ELS,
719*0ef6e016SJustin Tee 					 "1829 DA_ID issue status %d. "
720*0ef6e016SJustin Tee 					 "SFlag x%x NState x%x, NFlag x%x "
721*0ef6e016SJustin Tee 					 "Rpi x%x\n",
722*0ef6e016SJustin Tee 					 rc, ndlp->save_flags, ndlp->nlp_state,
723*0ef6e016SJustin Tee 					 ndlp->nlp_flag, ndlp->nlp_rpi);
724*0ef6e016SJustin Tee 
725*0ef6e016SJustin Tee 			/* Remove the waitq and save_flags.  It no
726*0ef6e016SJustin Tee 			 * longer matters if the wake happened.
727*0ef6e016SJustin Tee 			 */
728*0ef6e016SJustin Tee 			spin_lock_irq(&ndlp->lock);
729*0ef6e016SJustin Tee 			ndlp->da_id_waitq = NULL;
730*0ef6e016SJustin Tee 			ndlp->save_flags &= ~NLP_WAIT_FOR_DA_ID;
731*0ef6e016SJustin Tee 			spin_unlock_irq(&ndlp->lock);
732*0ef6e016SJustin Tee 		}
733*0ef6e016SJustin Tee 
734*0ef6e016SJustin Tee issue_logo:
73573d91e50SJames Smart 		/*
73673d91e50SJames Smart 		 * If the vpi is not registered, then a valid FDISC doesn't
73773d91e50SJames Smart 		 * exist and there is no need for a ELS LOGO.  Just cleanup
73873d91e50SJames Smart 		 * the ndlp.
73973d91e50SJames Smart 		 */
740e9b11083SJames Smart 		if (!(vport->vpi_state & LPFC_VPI_REGISTERED))
7415ffc266eSJames Smart 			goto skip_logo;
74273d91e50SJames Smart 
743e9b11083SJames Smart 		/* Issue a Fabric LOGO to cleanup fabric resources. */
744e9b11083SJames Smart 		ndlp = lpfc_findnode_did(vport, Fabric_DID);
745e9b11083SJames Smart 		if (!ndlp)
746e9b11083SJames Smart 			goto skip_logo;
7478ed190a9SJames Smart 
7488ed190a9SJames Smart 		rc = lpfc_send_npiv_logo(vport, ndlp);
7498ed190a9SJames Smart 		if (rc)
7508ed190a9SJames Smart 			goto skip_logo;
75192d7f7b0SJames Smart 	}
75292d7f7b0SJames Smart 
75390160e01SJames Smart 	if (!(phba->pport->load_flag & FC_UNLOADING))
75490160e01SJames Smart 		lpfc_discovery_wait(vport);
75590160e01SJames Smart 
75692d7f7b0SJames Smart skip_logo:
75716a3a208SJames Smart 
75876337eb8SJustin Tee 	/* Remove FC host to break driver binding. */
75976337eb8SJustin Tee 	fc_remove_host(shost);
76076337eb8SJustin Tee 	scsi_remove_host(shost);
76176337eb8SJustin Tee 
76287af33feSJames Smart 	lpfc_cleanup(vport);
76395f0ef8aSJames Smart 
76495f0ef8aSJames Smart 	/* Remove scsi host now.  The nodes are cleaned up. */
76592d7f7b0SJames Smart 	lpfc_sli_host_down(vport);
76692d7f7b0SJames Smart 	lpfc_stop_vport_timers(vport);
76787af33feSJames Smart 
76887af33feSJames Smart 	if (!(phba->pport->load_flag & FC_UNLOADING)) {
769e47c9093SJames Smart 		lpfc_unreg_all_rpis(vport);
77092d7f7b0SJames Smart 		lpfc_unreg_default_rpis(vport);
77192d7f7b0SJames Smart 		/*
77287af33feSJames Smart 		 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
77387af33feSJames Smart 		 * does the scsi_host_put() to release the vport.
77492d7f7b0SJames Smart 		 */
775cc82355aSJames Smart 		if (!(vport->vpi_state & LPFC_VPI_REGISTERED) ||
776cc82355aSJames Smart 				lpfc_mbx_unreg_vpi(vport))
777d7c255b2SJames Smart 			scsi_host_put(shost);
77803dbfe06SDick Kennedy 	} else {
779d7c255b2SJames Smart 		scsi_host_put(shost);
78003dbfe06SDick Kennedy 	}
78192d7f7b0SJames Smart 
78292d7f7b0SJames Smart 	lpfc_free_vpi(phba, vport->vpi);
78392d7f7b0SJames Smart 	vport->work_port_events = 0;
784523128e5SJames Smart 	spin_lock_irq(&phba->port_list_lock);
78592d7f7b0SJames Smart 	list_del_init(&vport->listentry);
786523128e5SJames Smart 	spin_unlock_irq(&phba->port_list_lock);
787cdf81160SJames Smart 	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
788a58cbd52SJames Smart 			 "1828 Vport Deleted.\n");
78992d7f7b0SJames Smart 	scsi_host_put(shost);
79051ef4c26SJames Smart 	return VPORT_OK;
79192d7f7b0SJames Smart }
79292d7f7b0SJames Smart 
793549e55cdSJames Smart struct lpfc_vport **
lpfc_create_vport_work_array(struct lpfc_hba * phba)794549e55cdSJames Smart lpfc_create_vport_work_array(struct lpfc_hba *phba)
795549e55cdSJames Smart {
796549e55cdSJames Smart 	struct lpfc_vport *port_iterator;
797549e55cdSJames Smart 	struct lpfc_vport **vports;
798549e55cdSJames Smart 	int index = 0;
7996396bb22SKees Cook 	vports = kcalloc(phba->max_vports + 1, sizeof(struct lpfc_vport *),
800549e55cdSJames Smart 			 GFP_KERNEL);
801549e55cdSJames Smart 	if (vports == NULL)
802549e55cdSJames Smart 		return NULL;
803523128e5SJames Smart 	spin_lock_irq(&phba->port_list_lock);
804549e55cdSJames Smart 	list_for_each_entry(port_iterator, &phba->port_list, listentry) {
805df9e1b59SJames Smart 		if (port_iterator->load_flag & FC_UNLOADING)
806df9e1b59SJames Smart 			continue;
807e8b62011SJames Smart 		if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
808372c187bSDick Kennedy 			lpfc_printf_vlog(port_iterator, KERN_ERR,
809372c187bSDick Kennedy 					 LOG_TRACE_EVENT,
810e8b62011SJames Smart 					 "1801 Create vport work array FAILED: "
811e8b62011SJames Smart 					 "cannot do scsi_host_get\n");
812549e55cdSJames Smart 			continue;
813e8b62011SJames Smart 		}
814549e55cdSJames Smart 		vports[index++] = port_iterator;
815549e55cdSJames Smart 	}
816523128e5SJames Smart 	spin_unlock_irq(&phba->port_list_lock);
817549e55cdSJames Smart 	return vports;
818549e55cdSJames Smart }
819549e55cdSJames Smart 
820549e55cdSJames Smart void
lpfc_destroy_vport_work_array(struct lpfc_hba * phba,struct lpfc_vport ** vports)82109372820SJames Smart lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
822549e55cdSJames Smart {
823549e55cdSJames Smart 	int i;
824549e55cdSJames Smart 	if (vports == NULL)
825549e55cdSJames Smart 		return;
82698fc5dd9SJames Smart 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
827549e55cdSJames Smart 		scsi_host_put(lpfc_shost_from_vport(vports[i]));
828549e55cdSJames Smart 	kfree(vports);
829549e55cdSJames Smart }
830ea2151b4SJames Smart 
831