1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for         *
3 * Fibre Channel Host Bus Adapters.                                *
4 * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6 * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7 * EMULEX and SLI are trademarks of Emulex.                        *
8 * www.broadcom.com                                                *
9 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10 *                                                                 *
11 * This program is free software; you can redistribute it and/or   *
12 * modify it under the terms of version 2 of the GNU General       *
13 * Public License as published by the Free Software Foundation.    *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20 * more details, a copy of which can be found in the file COPYING  *
21 * included with this package.                                     *
22 *******************************************************************/
23
24#include <linux/blkdev.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/idr.h>
28#include <linux/interrupt.h>
29#include <linux/kthread.h>
30#include <linux/pci.h>
31#include <linux/slab.h>
32#include <linux/spinlock.h>
33#include <linux/sched/signal.h>
34
35#include <scsi/scsi.h>
36#include <scsi/scsi_device.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_transport_fc.h>
39
40#include "lpfc_hw4.h"
41#include "lpfc_hw.h"
42#include "lpfc_sli.h"
43#include "lpfc_sli4.h"
44#include "lpfc_nl.h"
45#include "lpfc_disc.h"
46#include "lpfc_scsi.h"
47#include "lpfc.h"
48#include "lpfc_logmsg.h"
49#include "lpfc_crtn.h"
50#include "lpfc_version.h"
51#include "lpfc_vport.h"
52
53inline void lpfc_vport_set_state(struct lpfc_vport *vport,
54				 enum fc_vport_state new_state)
55{
56	struct fc_vport *fc_vport = vport->fc_vport;
57
58	if (fc_vport) {
59		/*
60		 * When the transport defines fc_vport_set state we will replace
61		 * this code with the following line
62		 */
63		/* fc_vport_set_state(fc_vport, new_state); */
64		if (new_state != FC_VPORT_INITIALIZING)
65			fc_vport->vport_last_state = fc_vport->vport_state;
66		fc_vport->vport_state = new_state;
67	}
68
69	/* for all the error states we will set the invternal state to FAILED */
70	switch (new_state) {
71	case FC_VPORT_NO_FABRIC_SUPP:
72	case FC_VPORT_NO_FABRIC_RSCS:
73	case FC_VPORT_FABRIC_LOGOUT:
74	case FC_VPORT_FABRIC_REJ_WWN:
75	case FC_VPORT_FAILED:
76		vport->port_state = LPFC_VPORT_FAILED;
77		break;
78	case FC_VPORT_LINKDOWN:
79		vport->port_state = LPFC_VPORT_UNKNOWN;
80		break;
81	default:
82		/* do nothing */
83		break;
84	}
85}
86
87int
88lpfc_alloc_vpi(struct lpfc_hba *phba)
89{
90	unsigned long vpi;
91
92	spin_lock_irq(&phba->hbalock);
93	/* Start at bit 1 because vpi zero is reserved for the physical port */
94	vpi = find_next_zero_bit(phba->vpi_bmask, (phba->max_vpi + 1), 1);
95	if (vpi > phba->max_vpi)
96		vpi = 0;
97	else
98		set_bit(vpi, phba->vpi_bmask);
99	if (phba->sli_rev == LPFC_SLI_REV4)
100		phba->sli4_hba.max_cfg_param.vpi_used++;
101	spin_unlock_irq(&phba->hbalock);
102	return vpi;
103}
104
105static void
106lpfc_free_vpi(struct lpfc_hba *phba, int vpi)
107{
108	if (vpi == 0)
109		return;
110	spin_lock_irq(&phba->hbalock);
111	clear_bit(vpi, phba->vpi_bmask);
112	if (phba->sli_rev == LPFC_SLI_REV4)
113		phba->sli4_hba.max_cfg_param.vpi_used--;
114	spin_unlock_irq(&phba->hbalock);
115}
116
117static int
118lpfc_vport_sparm(struct lpfc_hba *phba, struct lpfc_vport *vport)
119{
120	LPFC_MBOXQ_t *pmb;
121	MAILBOX_t *mb;
122	struct lpfc_dmabuf *mp;
123	int  rc;
124
125	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
126	if (!pmb) {
127		return -ENOMEM;
128	}
129	mb = &pmb->u.mb;
130
131	rc = lpfc_read_sparam(phba, pmb, vport->vpi);
132	if (rc) {
133		mempool_free(pmb, phba->mbox_mem_pool);
134		return -ENOMEM;
135	}
136
137	/*
138	 * Grab buffer pointer and clear context1 so we can use
139	 * lpfc_sli_issue_box_wait
140	 */
141	mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
142	pmb->ctx_buf = NULL;
143
144	pmb->vport = vport;
145	rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
146	if (rc != MBX_SUCCESS) {
147		if (signal_pending(current)) {
148			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
149					 "1830 Signal aborted mbxCmd x%x\n",
150					 mb->mbxCommand);
151			lpfc_mbuf_free(phba, mp->virt, mp->phys);
152			kfree(mp);
153			if (rc != MBX_TIMEOUT)
154				mempool_free(pmb, phba->mbox_mem_pool);
155			return -EINTR;
156		} else {
157			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
158					 "1818 VPort failed init, mbxCmd x%x "
159					 "READ_SPARM mbxStatus x%x, rc = x%x\n",
160					 mb->mbxCommand, mb->mbxStatus, rc);
161			lpfc_mbuf_free(phba, mp->virt, mp->phys);
162			kfree(mp);
163			if (rc != MBX_TIMEOUT)
164				mempool_free(pmb, phba->mbox_mem_pool);
165			return -EIO;
166		}
167	}
168
169	memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
170	memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
171	       sizeof (struct lpfc_name));
172	memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
173	       sizeof (struct lpfc_name));
174
175	lpfc_mbuf_free(phba, mp->virt, mp->phys);
176	kfree(mp);
177	mempool_free(pmb, phba->mbox_mem_pool);
178
179	return 0;
180}
181
182static int
183lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn,
184		      const char *name_type)
185{
186				/* ensure that IEEE format 1 addresses
187				 * contain zeros in bits 59-48
188				 */
189	if (!((wwn->u.wwn[0] >> 4) == 1 &&
190	      ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0)))
191		return 1;
192
193	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
194			"1822 Invalid %s: %02x:%02x:%02x:%02x:"
195			"%02x:%02x:%02x:%02x\n",
196			name_type,
197			wwn->u.wwn[0], wwn->u.wwn[1],
198			wwn->u.wwn[2], wwn->u.wwn[3],
199			wwn->u.wwn[4], wwn->u.wwn[5],
200			wwn->u.wwn[6], wwn->u.wwn[7]);
201	return 0;
202}
203
204static int
205lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
206{
207	struct lpfc_vport *vport;
208	unsigned long flags;
209
210	spin_lock_irqsave(&phba->port_list_lock, flags);
211	list_for_each_entry(vport, &phba->port_list, listentry) {
212		if (vport == new_vport)
213			continue;
214		/* If they match, return not unique */
215		if (memcmp(&vport->fc_sparam.portName,
216			   &new_vport->fc_sparam.portName,
217			   sizeof(struct lpfc_name)) == 0) {
218			spin_unlock_irqrestore(&phba->port_list_lock, flags);
219			return 0;
220		}
221	}
222	spin_unlock_irqrestore(&phba->port_list_lock, flags);
223	return 1;
224}
225
226/**
227 * lpfc_discovery_wait - Wait for driver discovery to quiesce
228 * @vport: The virtual port for which this call is being executed.
229 *
230 * This driver calls this routine specifically from lpfc_vport_delete
231 * to enforce a synchronous execution of vport
232 * delete relative to discovery activities.  The
233 * lpfc_vport_delete routine should not return until it
234 * can reasonably guarantee that discovery has quiesced.
235 * Post FDISC LOGO, the driver must wait until its SAN teardown is
236 * complete and all resources recovered before allowing
237 * cleanup.
238 *
239 * This routine does not require any locks held.
240 **/
241static void lpfc_discovery_wait(struct lpfc_vport *vport)
242{
243	struct lpfc_hba *phba = vport->phba;
244	uint32_t wait_flags = 0;
245	unsigned long wait_time_max;
246	unsigned long start_time;
247
248	wait_flags = FC_RSCN_MODE | FC_RSCN_DISCOVERY | FC_NLP_MORE |
249		     FC_RSCN_DEFERRED | FC_NDISC_ACTIVE | FC_DISC_TMO;
250
251	/*
252	 * The time constraint on this loop is a balance between the
253	 * fabric RA_TOV value and dev_loss tmo.  The driver's
254	 * devloss_tmo is 10 giving this loop a 3x multiplier minimally.
255	 */
256	wait_time_max = msecs_to_jiffies(((phba->fc_ratov * 3) + 3) * 1000);
257	wait_time_max += jiffies;
258	start_time = jiffies;
259	while (time_before(jiffies, wait_time_max)) {
260		if ((vport->num_disc_nodes > 0)    ||
261		    (vport->fc_flag & wait_flags)  ||
262		    ((vport->port_state > LPFC_VPORT_FAILED) &&
263		     (vport->port_state < LPFC_VPORT_READY))) {
264			lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
265					"1833 Vport discovery quiesce Wait:"
266					" state x%x fc_flags x%x"
267					" num_nodes x%x, waiting 1000 msecs"
268					" total wait msecs x%x\n",
269					vport->port_state, vport->fc_flag,
270					vport->num_disc_nodes,
271					jiffies_to_msecs(jiffies - start_time));
272			msleep(1000);
273		} else {
274			/* Base case.  Wait variants satisfied.  Break out */
275			lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
276					 "1834 Vport discovery quiesced:"
277					 " state x%x fc_flags x%x"
278					 " wait msecs x%x\n",
279					 vport->port_state, vport->fc_flag,
280					 jiffies_to_msecs(jiffies
281						- start_time));
282			break;
283		}
284	}
285
286	if (time_after(jiffies, wait_time_max))
287		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
288				 "1835 Vport discovery quiesce failed:"
289				 " state x%x fc_flags x%x wait msecs x%x\n",
290				 vport->port_state, vport->fc_flag,
291				 jiffies_to_msecs(jiffies - start_time));
292}
293
294int
295lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
296{
297	struct lpfc_nodelist *ndlp;
298	struct Scsi_Host *shost = fc_vport->shost;
299	struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata;
300	struct lpfc_hba   *phba = pport->phba;
301	struct lpfc_vport *vport = NULL;
302	int instance;
303	int vpi;
304	int rc = VPORT_ERROR;
305	int status;
306
307	if ((phba->sli_rev < 3) || !(phba->cfg_enable_npiv)) {
308		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
309				"1808 Create VPORT failed: "
310				"NPIV is not enabled: SLImode:%d\n",
311				phba->sli_rev);
312		rc = VPORT_INVAL;
313		goto error_out;
314	}
315
316	/* NPIV is not supported if HBA has NVME Target enabled */
317	if (phba->nvmet_support) {
318		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
319				"3189 Create VPORT failed: "
320				"NPIV is not supported on NVME Target\n");
321		rc = VPORT_INVAL;
322		goto error_out;
323	}
324
325	vpi = lpfc_alloc_vpi(phba);
326	if (vpi == 0) {
327		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
328				"1809 Create VPORT failed: "
329				"Max VPORTs (%d) exceeded\n",
330				phba->max_vpi);
331		rc = VPORT_NORESOURCES;
332		goto error_out;
333	}
334
335	/* Assign an unused board number */
336	if ((instance = lpfc_get_instance()) < 0) {
337		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
338				"1810 Create VPORT failed: Cannot get "
339				"instance number\n");
340		lpfc_free_vpi(phba, vpi);
341		rc = VPORT_NORESOURCES;
342		goto error_out;
343	}
344
345	vport = lpfc_create_port(phba, instance, &fc_vport->dev);
346	if (!vport) {
347		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
348				"1811 Create VPORT failed: vpi x%x\n", vpi);
349		lpfc_free_vpi(phba, vpi);
350		rc = VPORT_NORESOURCES;
351		goto error_out;
352	}
353
354	vport->vpi = vpi;
355	lpfc_debugfs_initialize(vport);
356
357	if ((status = lpfc_vport_sparm(phba, vport))) {
358		if (status == -EINTR) {
359			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
360					 "1831 Create VPORT Interrupted.\n");
361			rc = VPORT_ERROR;
362		} else {
363			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
364					 "1813 Create VPORT failed. "
365					 "Cannot get sparam\n");
366			rc = VPORT_NORESOURCES;
367		}
368		lpfc_free_vpi(phba, vpi);
369		destroy_port(vport);
370		goto error_out;
371	}
372
373	u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
374	u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
375
376	memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
377	memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
378
379	if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
380	    !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
381		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
382				 "1821 Create VPORT failed. "
383				 "Invalid WWN format\n");
384		lpfc_free_vpi(phba, vpi);
385		destroy_port(vport);
386		rc = VPORT_INVAL;
387		goto error_out;
388	}
389
390	if (!lpfc_unique_wwpn(phba, vport)) {
391		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
392				 "1823 Create VPORT failed. "
393				 "Duplicate WWN on HBA\n");
394		lpfc_free_vpi(phba, vpi);
395		destroy_port(vport);
396		rc = VPORT_INVAL;
397		goto error_out;
398	}
399
400	/* Create binary sysfs attribute for vport */
401	lpfc_alloc_sysfs_attr(vport);
402
403	/* Set the DFT_LUN_Q_DEPTH accordingly */
404	vport->cfg_lun_queue_depth  = phba->pport->cfg_lun_queue_depth;
405
406	/* Only the physical port can support NVME for now */
407	vport->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
408
409	*(struct lpfc_vport **)fc_vport->dd_data = vport;
410	vport->fc_vport = fc_vport;
411
412	/* At this point we are fully registered with SCSI Layer.  */
413	vport->load_flag |= FC_ALLOW_FDMI;
414	if (phba->cfg_enable_SmartSAN ||
415	    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
416		/* Setup appropriate attribute masks */
417		vport->fdmi_hba_mask = phba->pport->fdmi_hba_mask;
418		vport->fdmi_port_mask = phba->pport->fdmi_port_mask;
419	}
420
421	/*
422	 * In SLI4, the vpi must be activated before it can be used
423	 * by the port.
424	 */
425	if ((phba->sli_rev == LPFC_SLI_REV4) &&
426	    (pport->fc_flag & FC_VFI_REGISTERED)) {
427		rc = lpfc_sli4_init_vpi(vport);
428		if (rc) {
429			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
430					"1838 Failed to INIT_VPI on vpi %d "
431					"status %d\n", vpi, rc);
432			rc = VPORT_NORESOURCES;
433			lpfc_free_vpi(phba, vpi);
434			goto error_out;
435		}
436	} else if (phba->sli_rev == LPFC_SLI_REV4) {
437		/*
438		 * Driver cannot INIT_VPI now. Set the flags to
439		 * init_vpi when reg_vfi complete.
440		 */
441		vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
442		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
443		rc = VPORT_OK;
444		goto out;
445	}
446
447	if ((phba->link_state < LPFC_LINK_UP) ||
448	    (pport->port_state < LPFC_FABRIC_CFG_LINK) ||
449	    (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
450		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
451		rc = VPORT_OK;
452		goto out;
453	}
454
455	if (disable) {
456		lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
457		rc = VPORT_OK;
458		goto out;
459	}
460
461	/* Use the Physical nodes Fabric NDLP to determine if the link is
462	 * up and ready to FDISC.
463	 */
464	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
465	if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
466	    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
467		if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
468			lpfc_set_disctmo(vport);
469			lpfc_initial_fdisc(vport);
470		} else {
471			lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
472			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
473					 "0262 No NPIV Fabric support\n");
474		}
475	} else {
476		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
477	}
478	rc = VPORT_OK;
479
480out:
481	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
482			 "1825 Vport Created.\n");
483	lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
484error_out:
485	return rc;
486}
487
488static int
489disable_vport(struct fc_vport *fc_vport)
490{
491	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
492	struct lpfc_hba   *phba = vport->phba;
493	struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
494	long timeout;
495	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
496
497	ndlp = lpfc_findnode_did(vport, Fabric_DID);
498	if (ndlp && NLP_CHK_NODE_ACT(ndlp)
499	    && phba->link_state >= LPFC_LINK_UP) {
500		vport->unreg_vpi_cmpl = VPORT_INVAL;
501		timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
502		if (!lpfc_issue_els_npiv_logo(vport, ndlp))
503			while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
504				timeout = schedule_timeout(timeout);
505	}
506
507	lpfc_sli_host_down(vport);
508
509	/* Mark all nodes for discovery so we can remove them by
510	 * calling lpfc_cleanup_rpis(vport, 1)
511	 */
512	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
513		if (!NLP_CHK_NODE_ACT(ndlp))
514			continue;
515		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
516			continue;
517		lpfc_disc_state_machine(vport, ndlp, NULL,
518					NLP_EVT_DEVICE_RECOVERY);
519	}
520	lpfc_cleanup_rpis(vport, 1);
521
522	lpfc_stop_vport_timers(vport);
523	lpfc_unreg_all_rpis(vport);
524	lpfc_unreg_default_rpis(vport);
525	/*
526	 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
527	 * scsi_host_put() to release the vport.
528	 */
529	lpfc_mbx_unreg_vpi(vport);
530	if (phba->sli_rev == LPFC_SLI_REV4) {
531		spin_lock_irq(shost->host_lock);
532		vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
533		spin_unlock_irq(shost->host_lock);
534	}
535
536	lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
537	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
538			 "1826 Vport Disabled.\n");
539	return VPORT_OK;
540}
541
542static int
543enable_vport(struct fc_vport *fc_vport)
544{
545	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
546	struct lpfc_hba   *phba = vport->phba;
547	struct lpfc_nodelist *ndlp = NULL;
548	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
549
550	if ((phba->link_state < LPFC_LINK_UP) ||
551	    (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
552		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
553		return VPORT_OK;
554	}
555
556	spin_lock_irq(shost->host_lock);
557	vport->load_flag |= FC_LOADING;
558	if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
559		spin_unlock_irq(shost->host_lock);
560		lpfc_issue_init_vpi(vport);
561		goto out;
562	}
563
564	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
565	spin_unlock_irq(shost->host_lock);
566
567	/* Use the Physical nodes Fabric NDLP to determine if the link is
568	 * up and ready to FDISC.
569	 */
570	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
571	if (ndlp && NLP_CHK_NODE_ACT(ndlp)
572	    && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
573		if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
574			lpfc_set_disctmo(vport);
575			lpfc_initial_fdisc(vport);
576		} else {
577			lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
578			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
579					 "0264 No NPIV Fabric support\n");
580		}
581	} else {
582		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
583	}
584
585out:
586	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
587			 "1827 Vport Enabled.\n");
588	return VPORT_OK;
589}
590
591int
592lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
593{
594	if (disable)
595		return disable_vport(fc_vport);
596	else
597		return enable_vport(fc_vport);
598}
599
600
601int
602lpfc_vport_delete(struct fc_vport *fc_vport)
603{
604	struct lpfc_nodelist *ndlp = NULL;
605	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
606	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
607	struct lpfc_hba   *phba = vport->phba;
608	long timeout;
609	bool ns_ndlp_referenced = false;
610
611	if (vport->port_type == LPFC_PHYSICAL_PORT) {
612		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
613				 "1812 vport_delete failed: Cannot delete "
614				 "physical host\n");
615		return VPORT_ERROR;
616	}
617
618	/* If the vport is a static vport fail the deletion. */
619	if ((vport->vport_flag & STATIC_VPORT) &&
620		!(phba->pport->load_flag & FC_UNLOADING)) {
621		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
622				 "1837 vport_delete failed: Cannot delete "
623				 "static vport.\n");
624		return VPORT_ERROR;
625	}
626	spin_lock_irq(&phba->hbalock);
627	vport->load_flag |= FC_UNLOADING;
628	spin_unlock_irq(&phba->hbalock);
629	/*
630	 * If we are not unloading the driver then prevent the vport_delete
631	 * from happening until after this vport's discovery is finished.
632	 */
633	if (!(phba->pport->load_flag & FC_UNLOADING)) {
634		int check_count = 0;
635		while (check_count < ((phba->fc_ratov * 3) + 3) &&
636		       vport->port_state > LPFC_VPORT_FAILED &&
637		       vport->port_state < LPFC_VPORT_READY) {
638			check_count++;
639			msleep(1000);
640		}
641		if (vport->port_state > LPFC_VPORT_FAILED &&
642		    vport->port_state < LPFC_VPORT_READY)
643			return -EAGAIN;
644	}
645
646	/*
647	 * Take early refcount for outstanding I/O requests we schedule during
648	 * delete processing for unreg_vpi.  Always keep this before
649	 * scsi_remove_host() as we can no longer obtain a reference through
650	 * scsi_host_get() after scsi_host_remove as shost is set to SHOST_DEL.
651	 */
652	if (!scsi_host_get(shost))
653		return VPORT_INVAL;
654
655	lpfc_free_sysfs_attr(vport);
656
657	lpfc_debugfs_terminate(vport);
658
659	/*
660	 * The call to fc_remove_host might release the NameServer ndlp. Since
661	 * we might need to use the ndlp to send the DA_ID CT command,
662	 * increment the reference for the NameServer ndlp to prevent it from
663	 * being released.
664	 */
665	ndlp = lpfc_findnode_did(vport, NameServer_DID);
666	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
667		lpfc_nlp_get(ndlp);
668		ns_ndlp_referenced = true;
669	}
670
671	/* Remove FC host and then SCSI host with the vport */
672	fc_remove_host(shost);
673	scsi_remove_host(shost);
674
675	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
676
677	/* In case of driver unload, we shall not perform fabric logo as the
678	 * worker thread already stopped at this stage and, in this case, we
679	 * can safely skip the fabric logo.
680	 */
681	if (phba->pport->load_flag & FC_UNLOADING) {
682		if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
683		    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
684		    phba->link_state >= LPFC_LINK_UP) {
685			/* First look for the Fabric ndlp */
686			ndlp = lpfc_findnode_did(vport, Fabric_DID);
687			if (!ndlp)
688				goto skip_logo;
689			else if (!NLP_CHK_NODE_ACT(ndlp)) {
690				ndlp = lpfc_enable_node(vport, ndlp,
691							NLP_STE_UNUSED_NODE);
692				if (!ndlp)
693					goto skip_logo;
694			}
695			/* Remove ndlp from vport npld list */
696			lpfc_dequeue_node(vport, ndlp);
697
698			/* Indicate free memory when release */
699			spin_lock_irq(&phba->ndlp_lock);
700			NLP_SET_FREE_REQ(ndlp);
701			spin_unlock_irq(&phba->ndlp_lock);
702			/* Kick off release ndlp when it can be safely done */
703			lpfc_nlp_put(ndlp);
704		}
705		goto skip_logo;
706	}
707
708	/* Otherwise, we will perform fabric logo as needed */
709	if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
710	    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
711	    phba->link_state >= LPFC_LINK_UP &&
712	    phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
713		if (vport->cfg_enable_da_id) {
714			timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
715			if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
716				while (vport->ct_flags && timeout)
717					timeout = schedule_timeout(timeout);
718			else
719				lpfc_printf_log(vport->phba, KERN_WARNING,
720						LOG_VPORT,
721						"1829 CT command failed to "
722						"delete objects on fabric\n");
723		}
724		/* First look for the Fabric ndlp */
725		ndlp = lpfc_findnode_did(vport, Fabric_DID);
726		if (!ndlp) {
727			/* Cannot find existing Fabric ndlp, allocate one */
728			ndlp = lpfc_nlp_init(vport, Fabric_DID);
729			if (!ndlp)
730				goto skip_logo;
731			/* Indicate free memory when release */
732			NLP_SET_FREE_REQ(ndlp);
733		} else {
734			if (!NLP_CHK_NODE_ACT(ndlp)) {
735				ndlp = lpfc_enable_node(vport, ndlp,
736						NLP_STE_UNUSED_NODE);
737				if (!ndlp)
738					goto skip_logo;
739			}
740
741			/* Remove ndlp from vport list */
742			lpfc_dequeue_node(vport, ndlp);
743			spin_lock_irq(&phba->ndlp_lock);
744			if (!NLP_CHK_FREE_REQ(ndlp))
745				/* Indicate free memory when release */
746				NLP_SET_FREE_REQ(ndlp);
747			else {
748				/* Skip this if ndlp is already in free mode */
749				spin_unlock_irq(&phba->ndlp_lock);
750				goto skip_logo;
751			}
752			spin_unlock_irq(&phba->ndlp_lock);
753		}
754
755		/*
756		 * If the vpi is not registered, then a valid FDISC doesn't
757		 * exist and there is no need for a ELS LOGO.  Just cleanup
758		 * the ndlp.
759		 */
760		if (!(vport->vpi_state & LPFC_VPI_REGISTERED)) {
761			lpfc_nlp_put(ndlp);
762			goto skip_logo;
763		}
764
765		vport->unreg_vpi_cmpl = VPORT_INVAL;
766		timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
767		if (!lpfc_issue_els_npiv_logo(vport, ndlp))
768			while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
769				timeout = schedule_timeout(timeout);
770	}
771
772	if (!(phba->pport->load_flag & FC_UNLOADING))
773		lpfc_discovery_wait(vport);
774
775skip_logo:
776
777	/*
778	 * If the NameServer ndlp has been incremented to allow the DA_ID CT
779	 * command to be sent, decrement the ndlp now.
780	 */
781	if (ns_ndlp_referenced) {
782		ndlp = lpfc_findnode_did(vport, NameServer_DID);
783		lpfc_nlp_put(ndlp);
784	}
785
786	lpfc_cleanup(vport);
787	lpfc_sli_host_down(vport);
788
789	lpfc_stop_vport_timers(vport);
790
791	if (!(phba->pport->load_flag & FC_UNLOADING)) {
792		lpfc_unreg_all_rpis(vport);
793		lpfc_unreg_default_rpis(vport);
794		/*
795		 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
796		 * does the scsi_host_put() to release the vport.
797		 */
798		if (!(vport->vpi_state & LPFC_VPI_REGISTERED) ||
799				lpfc_mbx_unreg_vpi(vport))
800			scsi_host_put(shost);
801	} else {
802		scsi_host_put(shost);
803	}
804
805	lpfc_free_vpi(phba, vport->vpi);
806	vport->work_port_events = 0;
807	spin_lock_irq(&phba->port_list_lock);
808	list_del_init(&vport->listentry);
809	spin_unlock_irq(&phba->port_list_lock);
810	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
811			 "1828 Vport Deleted.\n");
812	scsi_host_put(shost);
813	return VPORT_OK;
814}
815
816struct lpfc_vport **
817lpfc_create_vport_work_array(struct lpfc_hba *phba)
818{
819	struct lpfc_vport *port_iterator;
820	struct lpfc_vport **vports;
821	int index = 0;
822	vports = kcalloc(phba->max_vports + 1, sizeof(struct lpfc_vport *),
823			 GFP_KERNEL);
824	if (vports == NULL)
825		return NULL;
826	spin_lock_irq(&phba->port_list_lock);
827	list_for_each_entry(port_iterator, &phba->port_list, listentry) {
828		if (port_iterator->load_flag & FC_UNLOADING)
829			continue;
830		if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
831			lpfc_printf_vlog(port_iterator, KERN_ERR,
832					 LOG_TRACE_EVENT,
833					 "1801 Create vport work array FAILED: "
834					 "cannot do scsi_host_get\n");
835			continue;
836		}
837		vports[index++] = port_iterator;
838	}
839	spin_unlock_irq(&phba->port_list_lock);
840	return vports;
841}
842
843void
844lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
845{
846	int i;
847	if (vports == NULL)
848		return;
849	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
850		scsi_host_put(lpfc_shost_from_vport(vports[i]));
851	kfree(vports);
852}
853
854
855/**
856 * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
857 * @vport: Pointer to vport object.
858 *
859 * This function resets the statistical data for the vport. This function
860 * is called with the host_lock held
861 **/
862void
863lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
864{
865	struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
866
867	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
868		if (!NLP_CHK_NODE_ACT(ndlp))
869			continue;
870		if (ndlp->lat_data)
871			memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
872				sizeof(struct lpfc_scsicmd_bkt));
873	}
874}
875
876
877/**
878 * lpfc_alloc_bucket - Allocate data buffer required for statistical data
879 * @vport: Pointer to vport object.
880 *
881 * This function allocates data buffer required for all the FC
882 * nodes of the vport to collect statistical data.
883 **/
884void
885lpfc_alloc_bucket(struct lpfc_vport *vport)
886{
887	struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
888
889	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
890		if (!NLP_CHK_NODE_ACT(ndlp))
891			continue;
892
893		kfree(ndlp->lat_data);
894		ndlp->lat_data = NULL;
895
896		if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
897			ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
898					 sizeof(struct lpfc_scsicmd_bkt),
899					 GFP_ATOMIC);
900
901			if (!ndlp->lat_data)
902				lpfc_printf_vlog(vport, KERN_ERR,
903					LOG_TRACE_EVENT,
904					"0287 lpfc_alloc_bucket failed to "
905					"allocate statistical data buffer DID "
906					"0x%x\n", ndlp->nlp_DID);
907		}
908	}
909}
910
911/**
912 * lpfc_free_bucket - Free data buffer required for statistical data
913 * @vport: Pointer to vport object.
914 *
915 * Th function frees statistical data buffer of all the FC
916 * nodes of the vport.
917 **/
918void
919lpfc_free_bucket(struct lpfc_vport *vport)
920{
921	struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
922
923	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
924		if (!NLP_CHK_NODE_ACT(ndlp))
925			continue;
926
927		kfree(ndlp->lat_data);
928		ndlp->lat_data = NULL;
929	}
930}
931