File Index
Files and directories
Sign in
FILE

lpfc_vport.c

File preview with syntax highlighting.

linux-6.19.14/drivers/scsi/lpfc/lpfc_vport.c C
  1/*******************************************************************
  2 * This file is part of the Emulex Linux Device Driver for         *
  3 * Fibre Channel Host Bus Adapters.                                *
  4 * Copyright (C) 2017-2024 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	 * Wait for the read_sparams mailbox to complete.  Driver needs
139	 * this per vport to start the FDISC.  If the mailbox fails,
140	 * just cleanup and return an error unless the failure is a
141	 * mailbox timeout.  For MBX_TIMEOUT, allow the default
142	 * mbox completion handler to take care of the cleanup.  This
143	 * is safe as the mailbox command isn't one that triggers
144	 * another mailbox.
145	 */
146	pmb->vport = vport;
147	rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
148	if (rc != MBX_SUCCESS) {
149		if (signal_pending(current)) {
150			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
151					 "1830 Signal aborted mbxCmd x%x\n",
152					 mb->mbxCommand);
153			if (rc != MBX_TIMEOUT)
154				lpfc_mbox_rsrc_cleanup(phba, pmb,
155						       MBOX_THD_UNLOCKED);
156			return -EINTR;
157		} else {
158			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
159					 "1818 VPort failed init, mbxCmd x%x "
160					 "READ_SPARM mbxStatus x%x, rc = x%x\n",
161					 mb->mbxCommand, mb->mbxStatus, rc);
162			if (rc != MBX_TIMEOUT)
163				lpfc_mbox_rsrc_cleanup(phba, pmb,
164						       MBOX_THD_UNLOCKED);
165			return -EIO;
166		}
167	}
168
169	mp = pmb->ctx_buf;
170	memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
171	memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
172	       sizeof (struct lpfc_name));
173	memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
174	       sizeof (struct lpfc_name));
175	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
176	return 0;
177}
178
179static int
180lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn,
181		      const char *name_type)
182{
183				/* ensure that IEEE format 1 addresses
184				 * contain zeros in bits 59-48
185				 */
186	if (!((wwn->u.wwn[0] >> 4) == 1 &&
187	      ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0)))
188		return 1;
189
190	lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
191			"1822 Invalid %s: %02x:%02x:%02x:%02x:"
192			"%02x:%02x:%02x:%02x\n",
193			name_type,
194			wwn->u.wwn[0], wwn->u.wwn[1],
195			wwn->u.wwn[2], wwn->u.wwn[3],
196			wwn->u.wwn[4], wwn->u.wwn[5],
197			wwn->u.wwn[6], wwn->u.wwn[7]);
198	return 0;
199}
200
201static int
202lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
203{
204	struct lpfc_vport *vport;
205	unsigned long flags;
206
207	spin_lock_irqsave(&phba->port_list_lock, flags);
208	list_for_each_entry(vport, &phba->port_list, listentry) {
209		if (vport == new_vport)
210			continue;
211		/* If they match, return not unique */
212		if (memcmp(&vport->fc_sparam.portName,
213			   &new_vport->fc_sparam.portName,
214			   sizeof(struct lpfc_name)) == 0) {
215			spin_unlock_irqrestore(&phba->port_list_lock, flags);
216			return 0;
217		}
218	}
219	spin_unlock_irqrestore(&phba->port_list_lock, flags);
220	return 1;
221}
222
223/**
224 * lpfc_discovery_wait - Wait for driver discovery to quiesce
225 * @vport: The virtual port for which this call is being executed.
226 *
227 * This driver calls this routine specifically from lpfc_vport_delete
228 * to enforce a synchronous execution of vport
229 * delete relative to discovery activities.  The
230 * lpfc_vport_delete routine should not return until it
231 * can reasonably guarantee that discovery has quiesced.
232 * Post FDISC LOGO, the driver must wait until its SAN teardown is
233 * complete and all resources recovered before allowing
234 * cleanup.
235 *
236 * This routine does not require any locks held.
237 **/
238static void lpfc_discovery_wait(struct lpfc_vport *vport)
239{
240	struct lpfc_hba *phba = vport->phba;
241	unsigned long wait_time_max;
242	unsigned long start_time;
243
244	/*
245	 * The time constraint on this loop is a balance between the
246	 * fabric RA_TOV value and dev_loss tmo.  The driver's
247	 * devloss_tmo is 10 giving this loop a 3x multiplier minimally.
248	 */
249	wait_time_max = secs_to_jiffies((phba->fc_ratov * 3) + 3);
250	wait_time_max += jiffies;
251	start_time = jiffies;
252	while (time_before(jiffies, wait_time_max)) {
253		if ((vport->num_disc_nodes > 0)    ||
254		    test_bit(FC_RSCN_MODE, &vport->fc_flag) ||
255		    test_bit(FC_RSCN_DISCOVERY, &vport->fc_flag) ||
256		    test_bit(FC_NLP_MORE, &vport->fc_flag) ||
257		    test_bit(FC_RSCN_DEFERRED, &vport->fc_flag) ||
258		    test_bit(FC_NDISC_ACTIVE, &vport->fc_flag) ||
259		    test_bit(FC_DISC_TMO, &vport->fc_flag) ||
260		    ((vport->port_state > LPFC_VPORT_FAILED) &&
261		     (vport->port_state < LPFC_VPORT_READY))) {
262			lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
263					"1833 Vport discovery quiesce Wait: "
264					"state x%x fc_flags x%lx "
265					"num_nodes x%x, waiting 1000 msecs "
266					"total wait msecs x%x\n",
267					vport->port_state, vport->fc_flag,
268					vport->num_disc_nodes,
269					jiffies_to_msecs(jiffies - start_time));
270			msleep(1000);
271		} else {
272			/* Base case.  Wait variants satisfied.  Break out */
273			lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
274					 "1834 Vport discovery quiesced: "
275					 "state x%x fc_flags x%lx "
276					 "wait msecs x%x\n",
277					 vport->port_state, vport->fc_flag,
278					 jiffies_to_msecs(jiffies
279						- start_time));
280			break;
281		}
282	}
283
284	if (time_after(jiffies, wait_time_max))
285		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
286				 "1835 Vport discovery quiesce failed:"
287				 " state x%x fc_flags x%lx wait msecs x%x\n",
288				 vport->port_state, vport->fc_flag,
289				 jiffies_to_msecs(jiffies - start_time));
290}
291
292int
293lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
294{
295	struct lpfc_nodelist *ndlp;
296	struct Scsi_Host *shost = fc_vport->shost;
297	struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata;
298	struct lpfc_hba   *phba = pport->phba;
299	struct lpfc_vport *vport = NULL;
300	int instance;
301	int vpi;
302	int rc = VPORT_ERROR;
303	int status;
304
305	if ((phba->sli_rev < 3) || !(phba->cfg_enable_npiv)) {
306		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
307				"1808 Create VPORT failed: "
308				"NPIV is not enabled: SLImode:%d\n",
309				phba->sli_rev);
310		rc = VPORT_INVAL;
311		goto error_out;
312	}
313
314	/* NPIV is not supported if HBA has NVME Target enabled */
315	if (phba->nvmet_support) {
316		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
317				"3189 Create VPORT failed: "
318				"NPIV is not supported on NVME Target\n");
319		rc = VPORT_INVAL;
320		goto error_out;
321	}
322
323	vpi = lpfc_alloc_vpi(phba);
324	if (vpi == 0) {
325		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
326				"1809 Create VPORT failed: "
327				"Max VPORTs (%d) exceeded\n",
328				phba->max_vpi);
329		rc = VPORT_NORESOURCES;
330		goto error_out;
331	}
332
333	/* Assign an unused board number */
334	if ((instance = lpfc_get_instance()) < 0) {
335		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
336				"1810 Create VPORT failed: Cannot get "
337				"instance number\n");
338		lpfc_free_vpi(phba, vpi);
339		rc = VPORT_NORESOURCES;
340		goto error_out;
341	}
342
343	vport = lpfc_create_port(phba, instance, &fc_vport->dev);
344	if (!vport) {
345		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
346				"1811 Create VPORT failed: vpi x%x\n", vpi);
347		lpfc_free_vpi(phba, vpi);
348		rc = VPORT_NORESOURCES;
349		goto error_out;
350	}
351
352	vport->vpi = vpi;
353	lpfc_debugfs_initialize(vport);
354
355	if ((status = lpfc_vport_sparm(phba, vport))) {
356		if (status == -EINTR) {
357			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
358					 "1831 Create VPORT Interrupted.\n");
359			rc = VPORT_ERROR;
360		} else {
361			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
362					 "1813 Create VPORT failed. "
363					 "Cannot get sparam\n");
364			rc = VPORT_NORESOURCES;
365		}
366		lpfc_free_vpi(phba, vpi);
367		destroy_port(vport);
368		goto error_out;
369	}
370
371	u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
372	u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
373
374	memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
375	memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
376
377	if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
378	    !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
379		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
380				 "1821 Create VPORT failed. "
381				 "Invalid WWN format\n");
382		lpfc_free_vpi(phba, vpi);
383		destroy_port(vport);
384		rc = VPORT_INVAL;
385		goto error_out;
386	}
387
388	if (!lpfc_unique_wwpn(phba, vport)) {
389		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
390				 "1823 Create VPORT failed. "
391				 "Duplicate WWN on HBA\n");
392		lpfc_free_vpi(phba, vpi);
393		destroy_port(vport);
394		rc = VPORT_INVAL;
395		goto error_out;
396	}
397
398	/* Create binary sysfs attribute for vport */
399	lpfc_alloc_sysfs_attr(vport);
400
401	/* Set the DFT_LUN_Q_DEPTH accordingly */
402	vport->cfg_lun_queue_depth  = phba->pport->cfg_lun_queue_depth;
403
404	/* Only the physical port can support NVME for now */
405	vport->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
406
407	*(struct lpfc_vport **)fc_vport->dd_data = vport;
408	vport->fc_vport = fc_vport;
409
410	/* At this point we are fully registered with SCSI Layer.  */
411	set_bit(FC_ALLOW_FDMI, &vport->load_flag);
412	if (phba->cfg_enable_SmartSAN ||
413	    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
414		/* Setup appropriate attribute masks */
415		vport->fdmi_hba_mask = phba->pport->fdmi_hba_mask;
416		vport->fdmi_port_mask = phba->pport->fdmi_port_mask;
417	}
418
419	/*
420	 * In SLI4, the vpi must be activated before it can be used
421	 * by the port.
422	 */
423	if ((phba->sli_rev == LPFC_SLI_REV4) &&
424	    test_bit(FC_VFI_REGISTERED, &pport->fc_flag)) {
425		rc = lpfc_sli4_init_vpi(vport);
426		if (rc) {
427			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
428					"1838 Failed to INIT_VPI on vpi %d "
429					"status %d\n", vpi, rc);
430			rc = VPORT_NORESOURCES;
431			lpfc_free_vpi(phba, vpi);
432			goto error_out;
433		}
434	} else if (phba->sli_rev == LPFC_SLI_REV4) {
435		/*
436		 * Driver cannot INIT_VPI now. Set the flags to
437		 * init_vpi when reg_vfi complete.
438		 */
439		set_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag);
440		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
441		rc = VPORT_OK;
442		goto out;
443	}
444
445	if ((phba->link_state < LPFC_LINK_UP) ||
446	    (pport->port_state < LPFC_FABRIC_CFG_LINK) ||
447	    (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
448		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
449		rc = VPORT_OK;
450		goto out;
451	}
452
453	if (disable) {
454		lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
455		rc = VPORT_OK;
456		goto out;
457	}
458
459	/* Use the Physical nodes Fabric NDLP to determine if the link is
460	 * up and ready to FDISC.
461	 */
462	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
463	if (ndlp &&
464	    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
465		if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
466			lpfc_set_disctmo(vport);
467			lpfc_initial_fdisc(vport);
468		} else {
469			lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
470			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
471					 "0262 No NPIV Fabric support\n");
472		}
473	} else {
474		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
475	}
476	rc = VPORT_OK;
477
478out:
479	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
480			 "1825 Vport Created.\n");
481	lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
482error_out:
483	return rc;
484}
485
486static int
487lpfc_send_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
488{
489	int rc;
490	struct lpfc_hba *phba = vport->phba;
491
492	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
493
494	spin_lock_irq(&ndlp->lock);
495	if (!test_bit(NLP_WAIT_FOR_LOGO, &ndlp->save_flags) &&
496	    !ndlp->logo_waitq) {
497		ndlp->logo_waitq = &waitq;
498		ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
499		set_bit(NLP_ISSUE_LOGO, &ndlp->nlp_flag);
500		set_bit(NLP_WAIT_FOR_LOGO, &ndlp->save_flags);
501	}
502	spin_unlock_irq(&ndlp->lock);
503	rc = lpfc_issue_els_npiv_logo(vport, ndlp);
504	if (!rc) {
505		wait_event_timeout(waitq,
506				   !test_bit(NLP_WAIT_FOR_LOGO,
507					     &ndlp->save_flags),
508				   secs_to_jiffies(phba->fc_ratov * 2));
509
510		if (!test_bit(NLP_WAIT_FOR_LOGO, &ndlp->save_flags))
511			goto logo_cmpl;
512		/* LOGO wait failed.  Correct status. */
513		rc = -EINTR;
514	} else {
515		rc = -EIO;
516	}
517
518	/* Error - clean up node flags. */
519	clear_bit(NLP_ISSUE_LOGO, &ndlp->nlp_flag);
520	clear_bit(NLP_WAIT_FOR_LOGO, &ndlp->save_flags);
521
522 logo_cmpl:
523	lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
524			 "1824 Issue LOGO completes with status %d\n",
525			 rc);
526	spin_lock_irq(&ndlp->lock);
527	ndlp->logo_waitq = NULL;
528	spin_unlock_irq(&ndlp->lock);
529	return rc;
530}
531
532static int
533disable_vport(struct fc_vport *fc_vport)
534{
535	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
536	struct lpfc_hba   *phba = vport->phba;
537	struct lpfc_nodelist *ndlp = NULL;
538
539	/* Can't disable during an outstanding delete. */
540	if (test_bit(FC_UNLOADING, &vport->load_flag))
541		return 0;
542
543	ndlp = lpfc_findnode_did(vport, Fabric_DID);
544	if (ndlp && phba->link_state >= LPFC_LINK_UP)
545		(void)lpfc_send_npiv_logo(vport, ndlp);
546
547	lpfc_sli_host_down(vport);
548	lpfc_cleanup_rpis(vport, 0);
549
550	lpfc_stop_vport_timers(vport);
551	lpfc_unreg_all_rpis(vport);
552	lpfc_unreg_default_rpis(vport);
553	/*
554	 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
555	 * scsi_host_put() to release the vport.
556	 */
557	lpfc_mbx_unreg_vpi(vport);
558	if (phba->sli_rev == LPFC_SLI_REV4)
559		set_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag);
560
561	lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
562	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
563			 "1826 Vport Disabled.\n");
564	return VPORT_OK;
565}
566
567static int
568enable_vport(struct fc_vport *fc_vport)
569{
570	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
571	struct lpfc_hba   *phba = vport->phba;
572	struct lpfc_nodelist *ndlp = NULL;
573
574	if ((phba->link_state < LPFC_LINK_UP) ||
575	    (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
576		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
577		return VPORT_OK;
578	}
579
580	set_bit(FC_LOADING, &vport->load_flag);
581	if (test_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag)) {
582		lpfc_issue_init_vpi(vport);
583		goto out;
584	}
585
586	set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
587
588	/* Use the Physical nodes Fabric NDLP to determine if the link is
589	 * up and ready to FDISC.
590	 */
591	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
592	if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
593		if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
594			lpfc_set_disctmo(vport);
595			lpfc_initial_fdisc(vport);
596		} else {
597			lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
598			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
599					 "0264 No NPIV Fabric support\n");
600		}
601	} else {
602		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
603	}
604
605out:
606	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
607			 "1827 Vport Enabled.\n");
608	return VPORT_OK;
609}
610
611int
612lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
613{
614	if (disable)
615		return disable_vport(fc_vport);
616	else
617		return enable_vport(fc_vport);
618}
619
620int
621lpfc_vport_delete(struct fc_vport *fc_vport)
622{
623	struct lpfc_nodelist *ndlp = NULL;
624	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
625	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
626	struct lpfc_hba  *phba = vport->phba;
627	int rc;
628	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
629
630	if (vport->port_type == LPFC_PHYSICAL_PORT) {
631		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
632				 "1812 vport_delete failed: Cannot delete "
633				 "physical host\n");
634		return VPORT_ERROR;
635	}
636
637	/* If the vport is a static vport fail the deletion. */
638	if ((vport->vport_flag & STATIC_VPORT) &&
639		!test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
640		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
641				 "1837 vport_delete failed: Cannot delete "
642				 "static vport.\n");
643		return VPORT_ERROR;
644	}
645
646	set_bit(FC_UNLOADING, &vport->load_flag);
647
648	/*
649	 * If we are not unloading the driver then prevent the vport_delete
650	 * from happening until after this vport's discovery is finished.
651	 */
652	if (!test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
653		int check_count = 0;
654		while (check_count < ((phba->fc_ratov * 3) + 3) &&
655		       vport->port_state > LPFC_VPORT_FAILED &&
656		       vport->port_state < LPFC_VPORT_READY) {
657			check_count++;
658			msleep(1000);
659		}
660		if (vport->port_state > LPFC_VPORT_FAILED &&
661		    vport->port_state < LPFC_VPORT_READY)
662			return -EAGAIN;
663	}
664
665	/*
666	 * Take early refcount for outstanding I/O requests we schedule during
667	 * delete processing for unreg_vpi.  Always keep this before
668	 * scsi_remove_host() as we can no longer obtain a reference through
669	 * scsi_host_get() after scsi_remove_host as shost is set to SHOST_DEL.
670	 */
671	if (!scsi_host_get(shost))
672		return VPORT_INVAL;
673
674	lpfc_free_sysfs_attr(vport);
675	lpfc_debugfs_terminate(vport);
676
677	/* Send the DA_ID and Fabric LOGO to cleanup Nameserver entries. */
678	ndlp = lpfc_findnode_did(vport, Fabric_DID);
679	if (!ndlp)
680		goto skip_logo;
681
682	/* Send the DA_ID and Fabric LOGO to cleanup the NPIV fabric entries. */
683	if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
684	    phba->link_state >= LPFC_LINK_UP &&
685	    phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
686		if (vport->cfg_enable_da_id) {
687			/* Send DA_ID and wait for a completion.  This is best
688			 * effort.  If the DA_ID fails, likely the fabric will
689			 * "leak" NportIDs but at least the driver issued the
690			 * command.
691			 */
692			ndlp = lpfc_findnode_did(vport, NameServer_DID);
693			if (!ndlp)
694				goto issue_logo;
695
696			spin_lock_irq(&ndlp->lock);
697			ndlp->da_id_waitq = &waitq;
698			spin_unlock_irq(&ndlp->lock);
699			set_bit(NLP_WAIT_FOR_DA_ID, &ndlp->save_flags);
700
701			rc = lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0);
702			if (!rc) {
703				wait_event_timeout(waitq,
704				   !test_bit(NLP_WAIT_FOR_DA_ID,
705					     &ndlp->save_flags),
706				   secs_to_jiffies(phba->fc_ratov * 2));
707			}
708
709			lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT | LOG_ELS,
710					 "1829 DA_ID issue status %d. "
711					 "SFlag x%lx NState x%x, NFlag x%lx "
712					 "Rpi x%x\n",
713					 rc, ndlp->save_flags, ndlp->nlp_state,
714					 ndlp->nlp_flag, ndlp->nlp_rpi);
715
716			/* Remove the waitq and save_flags.  It no
717			 * longer matters if the wake happened.
718			 */
719			spin_lock_irq(&ndlp->lock);
720			ndlp->da_id_waitq = NULL;
721			spin_unlock_irq(&ndlp->lock);
722			clear_bit(NLP_WAIT_FOR_DA_ID, &ndlp->save_flags);
723		}
724
725issue_logo:
726		/*
727		 * If the vpi is not registered, then a valid FDISC doesn't
728		 * exist and there is no need for a ELS LOGO.  Just cleanup
729		 * the ndlp.
730		 */
731		if (!(vport->vpi_state & LPFC_VPI_REGISTERED))
732			goto skip_logo;
733
734		/* Issue a Fabric LOGO to cleanup fabric resources. */
735		ndlp = lpfc_findnode_did(vport, Fabric_DID);
736		if (!ndlp)
737			goto skip_logo;
738
739		rc = lpfc_send_npiv_logo(vport, ndlp);
740		if (rc)
741			goto skip_logo;
742	}
743
744	if (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
745		lpfc_discovery_wait(vport);
746
747skip_logo:
748
749	/* Remove FC host to break driver binding. */
750	fc_remove_host(shost);
751	scsi_remove_host(shost);
752
753	lpfc_cleanup(vport);
754
755	/* Remove scsi host now.  The nodes are cleaned up. */
756	lpfc_sli_host_down(vport);
757	lpfc_stop_vport_timers(vport);
758
759	if (!test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
760		lpfc_unreg_all_rpis(vport);
761		lpfc_unreg_default_rpis(vport);
762		/*
763		 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
764		 * does the scsi_host_put() to release the vport.
765		 */
766		if (!(vport->vpi_state & LPFC_VPI_REGISTERED) ||
767				lpfc_mbx_unreg_vpi(vport))
768			scsi_host_put(shost);
769	} else {
770		scsi_host_put(shost);
771	}
772
773	lpfc_free_vpi(phba, vport->vpi);
774	vport->work_port_events = 0;
775	spin_lock_irq(&phba->port_list_lock);
776	list_del_init(&vport->listentry);
777	spin_unlock_irq(&phba->port_list_lock);
778	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
779			 "1828 Vport Deleted.\n");
780	scsi_host_put(shost);
781	return VPORT_OK;
782}
783
784struct lpfc_vport **
785lpfc_create_vport_work_array(struct lpfc_hba *phba)
786{
787	struct lpfc_vport *port_iterator;
788	struct lpfc_vport **vports;
789	int index = 0;
790	vports = kcalloc(phba->max_vports + 1, sizeof(struct lpfc_vport *),
791			 GFP_KERNEL);
792	if (vports == NULL)
793		return NULL;
794	spin_lock_irq(&phba->port_list_lock);
795	list_for_each_entry(port_iterator, &phba->port_list, listentry) {
796		if (test_bit(FC_UNLOADING, &port_iterator->load_flag))
797			continue;
798		if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
799			lpfc_printf_vlog(port_iterator, KERN_ERR,
800					 LOG_TRACE_EVENT,
801					 "1801 Create vport work array FAILED: "
802					 "cannot do scsi_host_get\n");
803			continue;
804		}
805		vports[index++] = port_iterator;
806	}
807	spin_unlock_irq(&phba->port_list_lock);
808	return vports;
809}
810
811void
812lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
813{
814	int i;
815	if (vports == NULL)
816		return;
817	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
818		scsi_host_put(lpfc_shost_from_vport(vports[i]));
819	kfree(vports);
820}
821
~/ Read-only file preview.