xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_vport.c (revision 4f6cce39)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017 Broadcom. All Rights Reserved. The term      *
5  * “Broadcom” refers to Broadcom Limited 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 
53 inline 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 
87 int
88 lpfc_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 
105 static void
106 lpfc_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 
117 static int
118 lpfc_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->context1;
142 	pmb->context1 = 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_INIT | LOG_VPORT,
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_INIT | LOG_VPORT,
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 
182 static int
183 lpfc_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_VPORT,
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 
204 static int
205 lpfc_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->hbalock, 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->hbalock, flags);
219 			return 0;
220 		}
221 	}
222 	spin_unlock_irqrestore(&phba->hbalock, 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  **/
241 static 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_VPORT,
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 
294 int
295 lpfc_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_VPORT,
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 	vpi = lpfc_alloc_vpi(phba);
317 	if (vpi == 0) {
318 		lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
319 				"1809 Create VPORT failed: "
320 				"Max VPORTs (%d) exceeded\n",
321 				phba->max_vpi);
322 		rc = VPORT_NORESOURCES;
323 		goto error_out;
324 	}
325 
326 	/* Assign an unused board number */
327 	if ((instance = lpfc_get_instance()) < 0) {
328 		lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
329 				"1810 Create VPORT failed: Cannot get "
330 				"instance number\n");
331 		lpfc_free_vpi(phba, vpi);
332 		rc = VPORT_NORESOURCES;
333 		goto error_out;
334 	}
335 
336 	vport = lpfc_create_port(phba, instance, &fc_vport->dev);
337 	if (!vport) {
338 		lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
339 				"1811 Create VPORT failed: vpi x%x\n", vpi);
340 		lpfc_free_vpi(phba, vpi);
341 		rc = VPORT_NORESOURCES;
342 		goto error_out;
343 	}
344 
345 	vport->vpi = vpi;
346 	lpfc_debugfs_initialize(vport);
347 
348 	if ((status = lpfc_vport_sparm(phba, vport))) {
349 		if (status == -EINTR) {
350 			lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
351 					 "1831 Create VPORT Interrupted.\n");
352 			rc = VPORT_ERROR;
353 		} else {
354 			lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
355 					 "1813 Create VPORT failed. "
356 					 "Cannot get sparam\n");
357 			rc = VPORT_NORESOURCES;
358 		}
359 		lpfc_free_vpi(phba, vpi);
360 		destroy_port(vport);
361 		goto error_out;
362 	}
363 
364 	u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
365 	u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
366 
367 	memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
368 	memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
369 
370 	if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
371 	    !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
372 		lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
373 				 "1821 Create VPORT failed. "
374 				 "Invalid WWN format\n");
375 		lpfc_free_vpi(phba, vpi);
376 		destroy_port(vport);
377 		rc = VPORT_INVAL;
378 		goto error_out;
379 	}
380 
381 	if (!lpfc_unique_wwpn(phba, vport)) {
382 		lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
383 				 "1823 Create VPORT failed. "
384 				 "Duplicate WWN on HBA\n");
385 		lpfc_free_vpi(phba, vpi);
386 		destroy_port(vport);
387 		rc = VPORT_INVAL;
388 		goto error_out;
389 	}
390 
391 	/* Create binary sysfs attribute for vport */
392 	lpfc_alloc_sysfs_attr(vport);
393 
394 	/* Set the DFT_LUN_Q_DEPTH accordingly */
395 	vport->cfg_lun_queue_depth  = phba->pport->cfg_lun_queue_depth;
396 
397 	*(struct lpfc_vport **)fc_vport->dd_data = vport;
398 	vport->fc_vport = fc_vport;
399 
400 	/* At this point we are fully registered with SCSI Layer.  */
401 	vport->load_flag |= FC_ALLOW_FDMI;
402 	if (phba->cfg_enable_SmartSAN ||
403 	    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
404 		/* Setup appropriate attribute masks */
405 		vport->fdmi_hba_mask = phba->pport->fdmi_hba_mask;
406 		vport->fdmi_port_mask = phba->pport->fdmi_port_mask;
407 	}
408 
409 	if ((phba->nvmet_support == 0) &&
410 	    ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
411 	     (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))) {
412 		/* Create NVME binding with nvme_fc_transport. This
413 		 * ensures the vport is initialized.
414 		 */
415 		rc = lpfc_nvme_create_localport(vport);
416 		if (rc) {
417 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
418 					"6003 %s status x%x\n",
419 					"NVME registration failed, ",
420 					rc);
421 			goto error_out;
422 		}
423 	}
424 
425 	/*
426 	 * In SLI4, the vpi must be activated before it can be used
427 	 * by the port.
428 	 */
429 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
430 	    (pport->fc_flag & FC_VFI_REGISTERED)) {
431 		rc = lpfc_sli4_init_vpi(vport);
432 		if (rc) {
433 			lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
434 					"1838 Failed to INIT_VPI on vpi %d "
435 					"status %d\n", vpi, rc);
436 			rc = VPORT_NORESOURCES;
437 			lpfc_free_vpi(phba, vpi);
438 			goto error_out;
439 		}
440 	} else if (phba->sli_rev == LPFC_SLI_REV4) {
441 		/*
442 		 * Driver cannot INIT_VPI now. Set the flags to
443 		 * init_vpi when reg_vfi complete.
444 		 */
445 		vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
446 		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
447 		rc = VPORT_OK;
448 		goto out;
449 	}
450 
451 	if ((phba->link_state < LPFC_LINK_UP) ||
452 	    (pport->port_state < LPFC_FABRIC_CFG_LINK) ||
453 	    (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
454 		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
455 		rc = VPORT_OK;
456 		goto out;
457 	}
458 
459 	if (disable) {
460 		lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
461 		rc = VPORT_OK;
462 		goto out;
463 	}
464 
465 	/* Use the Physical nodes Fabric NDLP to determine if the link is
466 	 * up and ready to FDISC.
467 	 */
468 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
469 	if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
470 	    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
471 		if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
472 			lpfc_set_disctmo(vport);
473 			lpfc_initial_fdisc(vport);
474 		} else {
475 			lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
476 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
477 					 "0262 No NPIV Fabric support\n");
478 		}
479 	} else {
480 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
481 	}
482 	rc = VPORT_OK;
483 
484 out:
485 	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
486 			"1825 Vport Created.\n");
487 	lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
488 error_out:
489 	return rc;
490 }
491 
492 static int
493 disable_vport(struct fc_vport *fc_vport)
494 {
495 	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
496 	struct lpfc_hba   *phba = vport->phba;
497 	struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
498 	long timeout;
499 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
500 
501 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
502 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)
503 	    && phba->link_state >= LPFC_LINK_UP) {
504 		vport->unreg_vpi_cmpl = VPORT_INVAL;
505 		timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
506 		if (!lpfc_issue_els_npiv_logo(vport, ndlp))
507 			while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
508 				timeout = schedule_timeout(timeout);
509 	}
510 
511 	lpfc_sli_host_down(vport);
512 
513 	/* Mark all nodes for discovery so we can remove them by
514 	 * calling lpfc_cleanup_rpis(vport, 1)
515 	 */
516 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
517 		if (!NLP_CHK_NODE_ACT(ndlp))
518 			continue;
519 		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
520 			continue;
521 		lpfc_disc_state_machine(vport, ndlp, NULL,
522 					NLP_EVT_DEVICE_RECOVERY);
523 	}
524 	lpfc_cleanup_rpis(vport, 1);
525 
526 	lpfc_stop_vport_timers(vport);
527 	lpfc_unreg_all_rpis(vport);
528 	lpfc_unreg_default_rpis(vport);
529 	/*
530 	 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
531 	 * scsi_host_put() to release the vport.
532 	 */
533 	lpfc_mbx_unreg_vpi(vport);
534 	spin_lock_irq(shost->host_lock);
535 	vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
536 	spin_unlock_irq(shost->host_lock);
537 
538 	lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
539 	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
540 			 "1826 Vport Disabled.\n");
541 	return VPORT_OK;
542 }
543 
544 static int
545 enable_vport(struct fc_vport *fc_vport)
546 {
547 	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
548 	struct lpfc_hba   *phba = vport->phba;
549 	struct lpfc_nodelist *ndlp = NULL;
550 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
551 
552 	if ((phba->link_state < LPFC_LINK_UP) ||
553 	    (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
554 		lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
555 		return VPORT_OK;
556 	}
557 
558 	spin_lock_irq(shost->host_lock);
559 	vport->load_flag |= FC_LOADING;
560 	if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
561 		spin_unlock_irq(shost->host_lock);
562 		lpfc_issue_init_vpi(vport);
563 		goto out;
564 	}
565 
566 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
567 	spin_unlock_irq(shost->host_lock);
568 
569 	/* Use the Physical nodes Fabric NDLP to determine if the link is
570 	 * up and ready to FDISC.
571 	 */
572 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
573 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)
574 	    && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
575 		if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
576 			lpfc_set_disctmo(vport);
577 			lpfc_initial_fdisc(vport);
578 		} else {
579 			lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
580 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
581 					 "0264 No NPIV Fabric support\n");
582 		}
583 	} else {
584 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
585 	}
586 
587 out:
588 	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
589 			 "1827 Vport Enabled.\n");
590 	return VPORT_OK;
591 }
592 
593 int
594 lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
595 {
596 	if (disable)
597 		return disable_vport(fc_vport);
598 	else
599 		return enable_vport(fc_vport);
600 }
601 
602 
603 int
604 lpfc_vport_delete(struct fc_vport *fc_vport)
605 {
606 	struct lpfc_nodelist *ndlp = NULL;
607 	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
608 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
609 	struct lpfc_hba   *phba = vport->phba;
610 	long timeout;
611 	bool ns_ndlp_referenced = false;
612 
613 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
614 		lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
615 				 "1812 vport_delete failed: Cannot delete "
616 				 "physical host\n");
617 		return VPORT_ERROR;
618 	}
619 
620 	/* If the vport is a static vport fail the deletion. */
621 	if ((vport->vport_flag & STATIC_VPORT) &&
622 		!(phba->pport->load_flag & FC_UNLOADING)) {
623 		lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
624 				 "1837 vport_delete failed: Cannot delete "
625 				 "static vport.\n");
626 		return VPORT_ERROR;
627 	}
628 	spin_lock_irq(&phba->hbalock);
629 	vport->load_flag |= FC_UNLOADING;
630 	spin_unlock_irq(&phba->hbalock);
631 	/*
632 	 * If we are not unloading the driver then prevent the vport_delete
633 	 * from happening until after this vport's discovery is finished.
634 	 */
635 	if (!(phba->pport->load_flag & FC_UNLOADING)) {
636 		int check_count = 0;
637 		while (check_count < ((phba->fc_ratov * 3) + 3) &&
638 		       vport->port_state > LPFC_VPORT_FAILED &&
639 		       vport->port_state < LPFC_VPORT_READY) {
640 			check_count++;
641 			msleep(1000);
642 		}
643 		if (vport->port_state > LPFC_VPORT_FAILED &&
644 		    vport->port_state < LPFC_VPORT_READY)
645 			return -EAGAIN;
646 	}
647 	/*
648 	 * This is a bit of a mess.  We want to ensure the shost doesn't get
649 	 * torn down until we're done with the embedded lpfc_vport structure.
650 	 *
651 	 * Beyond holding a reference for this function, we also need a
652 	 * reference for outstanding I/O requests we schedule during delete
653 	 * processing.  But once we scsi_remove_host() we can no longer obtain
654 	 * a reference through scsi_host_get().
655 	 *
656 	 * So we take two references here.  We release one reference at the
657 	 * bottom of the function -- after delinking the vport.  And we
658 	 * release the other at the completion of the unreg_vpi that get's
659 	 * initiated after we've disposed of all other resources associated
660 	 * with the port.
661 	 */
662 	if (!scsi_host_get(shost))
663 		return VPORT_INVAL;
664 	if (!scsi_host_get(shost)) {
665 		scsi_host_put(shost);
666 		return VPORT_INVAL;
667 	}
668 	lpfc_free_sysfs_attr(vport);
669 
670 	lpfc_debugfs_terminate(vport);
671 
672 	/*
673 	 * The call to fc_remove_host might release the NameServer ndlp. Since
674 	 * we might need to use the ndlp to send the DA_ID CT command,
675 	 * increment the reference for the NameServer ndlp to prevent it from
676 	 * being released.
677 	 */
678 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
679 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
680 		lpfc_nlp_get(ndlp);
681 		ns_ndlp_referenced = true;
682 	}
683 
684 	/* Remove FC host and then SCSI host with the vport */
685 	fc_remove_host(shost);
686 	scsi_remove_host(shost);
687 
688 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
689 
690 	/* In case of driver unload, we shall not perform fabric logo as the
691 	 * worker thread already stopped at this stage and, in this case, we
692 	 * can safely skip the fabric logo.
693 	 */
694 	if (phba->pport->load_flag & FC_UNLOADING) {
695 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
696 		    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
697 		    phba->link_state >= LPFC_LINK_UP) {
698 			/* First look for the Fabric ndlp */
699 			ndlp = lpfc_findnode_did(vport, Fabric_DID);
700 			if (!ndlp)
701 				goto skip_logo;
702 			else if (!NLP_CHK_NODE_ACT(ndlp)) {
703 				ndlp = lpfc_enable_node(vport, ndlp,
704 							NLP_STE_UNUSED_NODE);
705 				if (!ndlp)
706 					goto skip_logo;
707 			}
708 			/* Remove ndlp from vport npld list */
709 			lpfc_dequeue_node(vport, ndlp);
710 
711 			/* Indicate free memory when release */
712 			spin_lock_irq(&phba->ndlp_lock);
713 			NLP_SET_FREE_REQ(ndlp);
714 			spin_unlock_irq(&phba->ndlp_lock);
715 			/* Kick off release ndlp when it can be safely done */
716 			lpfc_nlp_put(ndlp);
717 		}
718 		goto skip_logo;
719 	}
720 
721 	/* Otherwise, we will perform fabric logo as needed */
722 	if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
723 	    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
724 	    phba->link_state >= LPFC_LINK_UP &&
725 	    phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
726 		if (vport->cfg_enable_da_id) {
727 			timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
728 			if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
729 				while (vport->ct_flags && timeout)
730 					timeout = schedule_timeout(timeout);
731 			else
732 				lpfc_printf_log(vport->phba, KERN_WARNING,
733 						LOG_VPORT,
734 						"1829 CT command failed to "
735 						"delete objects on fabric\n");
736 		}
737 		/* First look for the Fabric ndlp */
738 		ndlp = lpfc_findnode_did(vport, Fabric_DID);
739 		if (!ndlp) {
740 			/* Cannot find existing Fabric ndlp, allocate one */
741 			ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
742 			if (!ndlp)
743 				goto skip_logo;
744 			lpfc_nlp_init(vport, ndlp, Fabric_DID);
745 			/* Indicate free memory when release */
746 			NLP_SET_FREE_REQ(ndlp);
747 		} else {
748 			if (!NLP_CHK_NODE_ACT(ndlp)) {
749 				ndlp = lpfc_enable_node(vport, ndlp,
750 						NLP_STE_UNUSED_NODE);
751 				if (!ndlp)
752 					goto skip_logo;
753 			}
754 
755 			/* Remove ndlp from vport list */
756 			lpfc_dequeue_node(vport, ndlp);
757 			spin_lock_irq(&phba->ndlp_lock);
758 			if (!NLP_CHK_FREE_REQ(ndlp))
759 				/* Indicate free memory when release */
760 				NLP_SET_FREE_REQ(ndlp);
761 			else {
762 				/* Skip this if ndlp is already in free mode */
763 				spin_unlock_irq(&phba->ndlp_lock);
764 				goto skip_logo;
765 			}
766 			spin_unlock_irq(&phba->ndlp_lock);
767 		}
768 
769 		/*
770 		 * If the vpi is not registered, then a valid FDISC doesn't
771 		 * exist and there is no need for a ELS LOGO.  Just cleanup
772 		 * the ndlp.
773 		 */
774 		if (!(vport->vpi_state & LPFC_VPI_REGISTERED)) {
775 			lpfc_nlp_put(ndlp);
776 			goto skip_logo;
777 		}
778 
779 		vport->unreg_vpi_cmpl = VPORT_INVAL;
780 		timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
781 		if (!lpfc_issue_els_npiv_logo(vport, ndlp))
782 			while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
783 				timeout = schedule_timeout(timeout);
784 	}
785 
786 	if (!(phba->pport->load_flag & FC_UNLOADING))
787 		lpfc_discovery_wait(vport);
788 
789 skip_logo:
790 
791 	/*
792 	 * If the NameServer ndlp has been incremented to allow the DA_ID CT
793 	 * command to be sent, decrement the ndlp now.
794 	 */
795 	if (ns_ndlp_referenced) {
796 		ndlp = lpfc_findnode_did(vport, NameServer_DID);
797 		lpfc_nlp_put(ndlp);
798 	}
799 
800 	lpfc_cleanup(vport);
801 	lpfc_sli_host_down(vport);
802 
803 	lpfc_stop_vport_timers(vport);
804 
805 	if (!(phba->pport->load_flag & FC_UNLOADING)) {
806 		lpfc_unreg_all_rpis(vport);
807 		lpfc_unreg_default_rpis(vport);
808 		/*
809 		 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
810 		 * does the scsi_host_put() to release the vport.
811 		 */
812 		if (!(vport->vpi_state & LPFC_VPI_REGISTERED) ||
813 				lpfc_mbx_unreg_vpi(vport))
814 			scsi_host_put(shost);
815 	} else
816 		scsi_host_put(shost);
817 
818 	lpfc_free_vpi(phba, vport->vpi);
819 	vport->work_port_events = 0;
820 	spin_lock_irq(&phba->hbalock);
821 	list_del_init(&vport->listentry);
822 	spin_unlock_irq(&phba->hbalock);
823 	lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
824 			 "1828 Vport Deleted.\n");
825 	scsi_host_put(shost);
826 	return VPORT_OK;
827 }
828 
829 struct lpfc_vport **
830 lpfc_create_vport_work_array(struct lpfc_hba *phba)
831 {
832 	struct lpfc_vport *port_iterator;
833 	struct lpfc_vport **vports;
834 	int index = 0;
835 	vports = kzalloc((phba->max_vports + 1) * sizeof(struct lpfc_vport *),
836 			 GFP_KERNEL);
837 	if (vports == NULL)
838 		return NULL;
839 	spin_lock_irq(&phba->hbalock);
840 	list_for_each_entry(port_iterator, &phba->port_list, listentry) {
841 		if (port_iterator->load_flag & FC_UNLOADING)
842 			continue;
843 		if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
844 			lpfc_printf_vlog(port_iterator, KERN_ERR, LOG_VPORT,
845 					 "1801 Create vport work array FAILED: "
846 					 "cannot do scsi_host_get\n");
847 			continue;
848 		}
849 		vports[index++] = port_iterator;
850 	}
851 	spin_unlock_irq(&phba->hbalock);
852 	return vports;
853 }
854 
855 void
856 lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
857 {
858 	int i;
859 	if (vports == NULL)
860 		return;
861 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
862 		scsi_host_put(lpfc_shost_from_vport(vports[i]));
863 	kfree(vports);
864 }
865 
866 
867 /**
868  * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
869  * @vport: Pointer to vport object.
870  *
871  * This function resets the statistical data for the vport. This function
872  * is called with the host_lock held
873  **/
874 void
875 lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
876 {
877 	struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
878 
879 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
880 		if (!NLP_CHK_NODE_ACT(ndlp))
881 			continue;
882 		if (ndlp->lat_data)
883 			memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
884 				sizeof(struct lpfc_scsicmd_bkt));
885 	}
886 }
887 
888 
889 /**
890  * lpfc_alloc_bucket - Allocate data buffer required for statistical data
891  * @vport: Pointer to vport object.
892  *
893  * This function allocates data buffer required for all the FC
894  * nodes of the vport to collect statistical data.
895  **/
896 void
897 lpfc_alloc_bucket(struct lpfc_vport *vport)
898 {
899 	struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
900 
901 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
902 		if (!NLP_CHK_NODE_ACT(ndlp))
903 			continue;
904 
905 		kfree(ndlp->lat_data);
906 		ndlp->lat_data = NULL;
907 
908 		if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
909 			ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
910 					 sizeof(struct lpfc_scsicmd_bkt),
911 					 GFP_ATOMIC);
912 
913 			if (!ndlp->lat_data)
914 				lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
915 					"0287 lpfc_alloc_bucket failed to "
916 					"allocate statistical data buffer DID "
917 					"0x%x\n", ndlp->nlp_DID);
918 		}
919 	}
920 }
921 
922 /**
923  * lpfc_free_bucket - Free data buffer required for statistical data
924  * @vport: Pointer to vport object.
925  *
926  * Th function frees statistical data buffer of all the FC
927  * nodes of the vport.
928  **/
929 void
930 lpfc_free_bucket(struct lpfc_vport *vport)
931 {
932 	struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
933 
934 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
935 		if (!NLP_CHK_NODE_ACT(ndlp))
936 			continue;
937 
938 		kfree(ndlp->lat_data);
939 		ndlp->lat_data = NULL;
940 	}
941 }
942