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. *
9 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
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 *******************************************************************/
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>
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_device.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_transport_fc.h>
43 #include "lpfc_sli4.h"
45 #include "lpfc_disc.h"
46 #include "lpfc_scsi.h"
48 #include "lpfc_logmsg.h"
49 #include "lpfc_crtn.h"
50 #include "lpfc_version.h"
51 #include "lpfc_vport.h"
53 inline void lpfc_vport_set_state(struct lpfc_vport
*vport
,
54 enum fc_vport_state new_state
)
56 struct fc_vport
*fc_vport
= vport
->fc_vport
;
60 * When the transport defines fc_vport_set state we will replace
61 * this code with the following line
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
;
69 /* for all the error states we will set the invternal state to FAILED */
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
:
76 vport
->port_state
= LPFC_VPORT_FAILED
;
78 case FC_VPORT_LINKDOWN
:
79 vport
->port_state
= LPFC_VPORT_UNKNOWN
;
88 lpfc_alloc_vpi(struct lpfc_hba
*phba
)
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
)
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
);
106 lpfc_free_vpi(struct lpfc_hba
*phba
, int vpi
)
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
);
118 lpfc_vport_sparm(struct lpfc_hba
*phba
, struct lpfc_vport
*vport
)
122 struct lpfc_dmabuf
*mp
;
125 pmb
= mempool_alloc(phba
->mbox_mem_pool
, GFP_KERNEL
);
131 rc
= lpfc_read_sparam(phba
, pmb
, vport
->vpi
);
133 mempool_free(pmb
, phba
->mbox_mem_pool
);
138 * Grab buffer pointer and clear context1 so we can use
139 * lpfc_sli_issue_box_wait
141 mp
= (struct lpfc_dmabuf
*)pmb
->ctx_buf
;
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",
151 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
153 if (rc
!= MBX_TIMEOUT
)
154 mempool_free(pmb
, phba
->mbox_mem_pool
);
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
);
163 if (rc
!= MBX_TIMEOUT
)
164 mempool_free(pmb
, phba
->mbox_mem_pool
);
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
));
175 lpfc_mbuf_free(phba
, mp
->virt
, mp
->phys
);
177 mempool_free(pmb
, phba
->mbox_mem_pool
);
183 lpfc_valid_wwn_format(struct lpfc_hba
*phba
, struct lpfc_name
*wwn
,
184 const char *name_type
)
186 /* ensure that IEEE format 1 addresses
187 * contain zeros in bits 59-48
189 if (!((wwn
->u
.wwn
[0] >> 4) == 1 &&
190 ((wwn
->u
.wwn
[0] & 0xf) != 0 || (wwn
->u
.wwn
[1] & 0xf) != 0)))
193 lpfc_printf_log(phba
, KERN_ERR
, LOG_VPORT
,
194 "1822 Invalid %s: %02x:%02x:%02x:%02x:"
195 "%02x:%02x:%02x:%02x\n",
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]);
205 lpfc_unique_wwpn(struct lpfc_hba
*phba
, struct lpfc_vport
*new_vport
)
207 struct lpfc_vport
*vport
;
210 spin_lock_irqsave(&phba
->port_list_lock
, flags
);
211 list_for_each_entry(vport
, &phba
->port_list
, listentry
) {
212 if (vport
== new_vport
)
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
);
222 spin_unlock_irqrestore(&phba
->port_list_lock
, flags
);
227 * lpfc_discovery_wait - Wait for driver discovery to quiesce
228 * @vport: The virtual port for which this call is being executed.
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
239 * This routine does not require any locks held.
241 static void lpfc_discovery_wait(struct lpfc_vport
*vport
)
243 struct lpfc_hba
*phba
= vport
->phba
;
244 uint32_t wait_flags
= 0;
245 unsigned long wait_time_max
;
246 unsigned long start_time
;
248 wait_flags
= FC_RSCN_MODE
| FC_RSCN_DISCOVERY
| FC_NLP_MORE
|
249 FC_RSCN_DEFERRED
| FC_NDISC_ACTIVE
| FC_DISC_TMO
;
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.
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
));
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"
279 vport
->port_state
, vport
->fc_flag
,
280 jiffies_to_msecs(jiffies
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
));
295 lpfc_vport_create(struct fc_vport
*fc_vport
, bool disable
)
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
;
304 int rc
= VPORT_ERROR
;
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",
316 /* NPIV is not supported if HBA has NVME Target enabled */
317 if (phba
->nvmet_support
) {
318 lpfc_printf_log(phba
, KERN_ERR
, LOG_VPORT
,
319 "3189 Create VPORT failed: "
320 "NPIV is not supported on NVME Target\n");
325 vpi
= lpfc_alloc_vpi(phba
);
327 lpfc_printf_log(phba
, KERN_ERR
, LOG_VPORT
,
328 "1809 Create VPORT failed: "
329 "Max VPORTs (%d) exceeded\n",
331 rc
= VPORT_NORESOURCES
;
335 /* Assign an unused board number */
336 if ((instance
= lpfc_get_instance()) < 0) {
337 lpfc_printf_log(phba
, KERN_ERR
, LOG_VPORT
,
338 "1810 Create VPORT failed: Cannot get "
339 "instance number\n");
340 lpfc_free_vpi(phba
, vpi
);
341 rc
= VPORT_NORESOURCES
;
345 vport
= lpfc_create_port(phba
, instance
, &fc_vport
->dev
);
347 lpfc_printf_log(phba
, KERN_ERR
, LOG_VPORT
,
348 "1811 Create VPORT failed: vpi x%x\n", vpi
);
349 lpfc_free_vpi(phba
, vpi
);
350 rc
= VPORT_NORESOURCES
;
355 lpfc_debugfs_initialize(vport
);
357 if ((status
= lpfc_vport_sparm(phba
, vport
))) {
358 if (status
== -EINTR
) {
359 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_VPORT
,
360 "1831 Create VPORT Interrupted.\n");
363 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_VPORT
,
364 "1813 Create VPORT failed. "
365 "Cannot get sparam\n");
366 rc
= VPORT_NORESOURCES
;
368 lpfc_free_vpi(phba
, vpi
);
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
);
376 memcpy(&vport
->fc_sparam
.portName
, vport
->fc_portname
.u
.wwn
, 8);
377 memcpy(&vport
->fc_sparam
.nodeName
, vport
->fc_nodename
.u
.wwn
, 8);
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_VPORT
,
382 "1821 Create VPORT failed. "
383 "Invalid WWN format\n");
384 lpfc_free_vpi(phba
, vpi
);
390 if (!lpfc_unique_wwpn(phba
, vport
)) {
391 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_VPORT
,
392 "1823 Create VPORT failed. "
393 "Duplicate WWN on HBA\n");
394 lpfc_free_vpi(phba
, vpi
);
400 /* Create binary sysfs attribute for vport */
401 lpfc_alloc_sysfs_attr(vport
);
403 /* Set the DFT_LUN_Q_DEPTH accordingly */
404 vport
->cfg_lun_queue_depth
= phba
->pport
->cfg_lun_queue_depth
;
406 /* Only the physical port can support NVME for now */
407 vport
->cfg_enable_fc4_type
= LPFC_ENABLE_FCP
;
409 *(struct lpfc_vport
**)fc_vport
->dd_data
= vport
;
410 vport
->fc_vport
= fc_vport
;
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
;
422 * In SLI4, the vpi must be activated before it can be used
425 if ((phba
->sli_rev
== LPFC_SLI_REV4
) &&
426 (pport
->fc_flag
& FC_VFI_REGISTERED
)) {
427 rc
= lpfc_sli4_init_vpi(vport
);
429 lpfc_printf_log(phba
, KERN_ERR
, LOG_VPORT
,
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
);
436 } else if (phba
->sli_rev
== LPFC_SLI_REV4
) {
438 * Driver cannot INIT_VPI now. Set the flags to
439 * init_vpi when reg_vfi complete.
441 vport
->fc_flag
|= FC_VPORT_NEEDS_INIT_VPI
;
442 lpfc_vport_set_state(vport
, FC_VPORT_LINKDOWN
);
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
);
456 lpfc_vport_set_state(vport
, FC_VPORT_DISABLED
);
461 /* Use the Physical nodes Fabric NDLP to determine if the link is
462 * up and ready to FDISC.
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
);
471 lpfc_vport_set_state(vport
, FC_VPORT_NO_FABRIC_SUPP
);
472 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_ELS
,
473 "0262 No NPIV Fabric support\n");
476 lpfc_vport_set_state(vport
, FC_VPORT_FAILED
);
481 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_VPORT
,
482 "1825 Vport Created.\n");
483 lpfc_host_attrib_init(lpfc_shost_from_vport(vport
));
489 disable_vport(struct fc_vport
*fc_vport
)
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
;
495 struct Scsi_Host
*shost
= lpfc_shost_from_vport(vport
);
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
);
507 lpfc_sli_host_down(vport
);
509 /* Mark all nodes for discovery so we can remove them by
510 * calling lpfc_cleanup_rpis(vport, 1)
512 list_for_each_entry_safe(ndlp
, next_ndlp
, &vport
->fc_nodes
, nlp_listp
) {
513 if (!NLP_CHK_NODE_ACT(ndlp
))
515 if (ndlp
->nlp_state
== NLP_STE_UNUSED_NODE
)
517 lpfc_disc_state_machine(vport
, ndlp
, NULL
,
518 NLP_EVT_DEVICE_RECOVERY
);
520 lpfc_cleanup_rpis(vport
, 1);
522 lpfc_stop_vport_timers(vport
);
523 lpfc_unreg_all_rpis(vport
);
524 lpfc_unreg_default_rpis(vport
);
526 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
527 * scsi_host_put() to release the vport.
529 lpfc_mbx_unreg_vpi(vport
);
530 spin_lock_irq(shost
->host_lock
);
531 vport
->fc_flag
|= FC_VPORT_NEEDS_INIT_VPI
;
532 spin_unlock_irq(shost
->host_lock
);
534 lpfc_vport_set_state(vport
, FC_VPORT_DISABLED
);
535 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_VPORT
,
536 "1826 Vport Disabled.\n");
541 enable_vport(struct fc_vport
*fc_vport
)
543 struct lpfc_vport
*vport
= *(struct lpfc_vport
**)fc_vport
->dd_data
;
544 struct lpfc_hba
*phba
= vport
->phba
;
545 struct lpfc_nodelist
*ndlp
= NULL
;
546 struct Scsi_Host
*shost
= lpfc_shost_from_vport(vport
);
548 if ((phba
->link_state
< LPFC_LINK_UP
) ||
549 (phba
->fc_topology
== LPFC_TOPOLOGY_LOOP
)) {
550 lpfc_vport_set_state(vport
, FC_VPORT_LINKDOWN
);
554 spin_lock_irq(shost
->host_lock
);
555 vport
->load_flag
|= FC_LOADING
;
556 if (vport
->fc_flag
& FC_VPORT_NEEDS_INIT_VPI
) {
557 spin_unlock_irq(shost
->host_lock
);
558 lpfc_issue_init_vpi(vport
);
562 vport
->fc_flag
|= FC_VPORT_NEEDS_REG_VPI
;
563 spin_unlock_irq(shost
->host_lock
);
565 /* Use the Physical nodes Fabric NDLP to determine if the link is
566 * up and ready to FDISC.
568 ndlp
= lpfc_findnode_did(phba
->pport
, Fabric_DID
);
569 if (ndlp
&& NLP_CHK_NODE_ACT(ndlp
)
570 && ndlp
->nlp_state
== NLP_STE_UNMAPPED_NODE
) {
571 if (phba
->link_flag
& LS_NPIV_FAB_SUPPORTED
) {
572 lpfc_set_disctmo(vport
);
573 lpfc_initial_fdisc(vport
);
575 lpfc_vport_set_state(vport
, FC_VPORT_NO_FABRIC_SUPP
);
576 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_ELS
,
577 "0264 No NPIV Fabric support\n");
580 lpfc_vport_set_state(vport
, FC_VPORT_FAILED
);
584 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_VPORT
,
585 "1827 Vport Enabled.\n");
590 lpfc_vport_disable(struct fc_vport
*fc_vport
, bool disable
)
593 return disable_vport(fc_vport
);
595 return enable_vport(fc_vport
);
600 lpfc_vport_delete(struct fc_vport
*fc_vport
)
602 struct lpfc_nodelist
*ndlp
= NULL
;
603 struct lpfc_vport
*vport
= *(struct lpfc_vport
**)fc_vport
->dd_data
;
604 struct Scsi_Host
*shost
= lpfc_shost_from_vport(vport
);
605 struct lpfc_hba
*phba
= vport
->phba
;
607 bool ns_ndlp_referenced
= false;
609 if (vport
->port_type
== LPFC_PHYSICAL_PORT
) {
610 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_VPORT
,
611 "1812 vport_delete failed: Cannot delete "
616 /* If the vport is a static vport fail the deletion. */
617 if ((vport
->vport_flag
& STATIC_VPORT
) &&
618 !(phba
->pport
->load_flag
& FC_UNLOADING
)) {
619 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_VPORT
,
620 "1837 vport_delete failed: Cannot delete "
624 spin_lock_irq(&phba
->hbalock
);
625 vport
->load_flag
|= FC_UNLOADING
;
626 spin_unlock_irq(&phba
->hbalock
);
628 * If we are not unloading the driver then prevent the vport_delete
629 * from happening until after this vport's discovery is finished.
631 if (!(phba
->pport
->load_flag
& FC_UNLOADING
)) {
633 while (check_count
< ((phba
->fc_ratov
* 3) + 3) &&
634 vport
->port_state
> LPFC_VPORT_FAILED
&&
635 vport
->port_state
< LPFC_VPORT_READY
) {
639 if (vport
->port_state
> LPFC_VPORT_FAILED
&&
640 vport
->port_state
< LPFC_VPORT_READY
)
644 * This is a bit of a mess. We want to ensure the shost doesn't get
645 * torn down until we're done with the embedded lpfc_vport structure.
647 * Beyond holding a reference for this function, we also need a
648 * reference for outstanding I/O requests we schedule during delete
649 * processing. But once we scsi_remove_host() we can no longer obtain
650 * a reference through scsi_host_get().
652 * So we take two references here. We release one reference at the
653 * bottom of the function -- after delinking the vport. And we
654 * release the other at the completion of the unreg_vpi that get's
655 * initiated after we've disposed of all other resources associated
658 if (!scsi_host_get(shost
))
660 if (!scsi_host_get(shost
)) {
661 scsi_host_put(shost
);
664 lpfc_free_sysfs_attr(vport
);
666 lpfc_debugfs_terminate(vport
);
669 * The call to fc_remove_host might release the NameServer ndlp. Since
670 * we might need to use the ndlp to send the DA_ID CT command,
671 * increment the reference for the NameServer ndlp to prevent it from
674 ndlp
= lpfc_findnode_did(vport
, NameServer_DID
);
675 if (ndlp
&& NLP_CHK_NODE_ACT(ndlp
)) {
677 ns_ndlp_referenced
= true;
680 /* Remove FC host and then SCSI host with the vport */
681 fc_remove_host(shost
);
682 scsi_remove_host(shost
);
684 ndlp
= lpfc_findnode_did(phba
->pport
, Fabric_DID
);
686 /* In case of driver unload, we shall not perform fabric logo as the
687 * worker thread already stopped at this stage and, in this case, we
688 * can safely skip the fabric logo.
690 if (phba
->pport
->load_flag
& FC_UNLOADING
) {
691 if (ndlp
&& NLP_CHK_NODE_ACT(ndlp
) &&
692 ndlp
->nlp_state
== NLP_STE_UNMAPPED_NODE
&&
693 phba
->link_state
>= LPFC_LINK_UP
) {
694 /* First look for the Fabric ndlp */
695 ndlp
= lpfc_findnode_did(vport
, Fabric_DID
);
698 else if (!NLP_CHK_NODE_ACT(ndlp
)) {
699 ndlp
= lpfc_enable_node(vport
, ndlp
,
700 NLP_STE_UNUSED_NODE
);
704 /* Remove ndlp from vport npld list */
705 lpfc_dequeue_node(vport
, ndlp
);
707 /* Indicate free memory when release */
708 spin_lock_irq(&phba
->ndlp_lock
);
709 NLP_SET_FREE_REQ(ndlp
);
710 spin_unlock_irq(&phba
->ndlp_lock
);
711 /* Kick off release ndlp when it can be safely done */
717 /* Otherwise, we will perform fabric logo as needed */
718 if (ndlp
&& NLP_CHK_NODE_ACT(ndlp
) &&
719 ndlp
->nlp_state
== NLP_STE_UNMAPPED_NODE
&&
720 phba
->link_state
>= LPFC_LINK_UP
&&
721 phba
->fc_topology
!= LPFC_TOPOLOGY_LOOP
) {
722 if (vport
->cfg_enable_da_id
) {
723 timeout
= msecs_to_jiffies(phba
->fc_ratov
* 2000);
724 if (!lpfc_ns_cmd(vport
, SLI_CTNS_DA_ID
, 0, 0))
725 while (vport
->ct_flags
&& timeout
)
726 timeout
= schedule_timeout(timeout
);
728 lpfc_printf_log(vport
->phba
, KERN_WARNING
,
730 "1829 CT command failed to "
731 "delete objects on fabric\n");
733 /* First look for the Fabric ndlp */
734 ndlp
= lpfc_findnode_did(vport
, Fabric_DID
);
736 /* Cannot find existing Fabric ndlp, allocate one */
737 ndlp
= lpfc_nlp_init(vport
, Fabric_DID
);
740 /* Indicate free memory when release */
741 NLP_SET_FREE_REQ(ndlp
);
743 if (!NLP_CHK_NODE_ACT(ndlp
)) {
744 ndlp
= lpfc_enable_node(vport
, ndlp
,
745 NLP_STE_UNUSED_NODE
);
750 /* Remove ndlp from vport list */
751 lpfc_dequeue_node(vport
, ndlp
);
752 spin_lock_irq(&phba
->ndlp_lock
);
753 if (!NLP_CHK_FREE_REQ(ndlp
))
754 /* Indicate free memory when release */
755 NLP_SET_FREE_REQ(ndlp
);
757 /* Skip this if ndlp is already in free mode */
758 spin_unlock_irq(&phba
->ndlp_lock
);
761 spin_unlock_irq(&phba
->ndlp_lock
);
765 * If the vpi is not registered, then a valid FDISC doesn't
766 * exist and there is no need for a ELS LOGO. Just cleanup
769 if (!(vport
->vpi_state
& LPFC_VPI_REGISTERED
)) {
774 vport
->unreg_vpi_cmpl
= VPORT_INVAL
;
775 timeout
= msecs_to_jiffies(phba
->fc_ratov
* 2000);
776 if (!lpfc_issue_els_npiv_logo(vport
, ndlp
))
777 while (vport
->unreg_vpi_cmpl
== VPORT_INVAL
&& timeout
)
778 timeout
= schedule_timeout(timeout
);
781 if (!(phba
->pport
->load_flag
& FC_UNLOADING
))
782 lpfc_discovery_wait(vport
);
787 * If the NameServer ndlp has been incremented to allow the DA_ID CT
788 * command to be sent, decrement the ndlp now.
790 if (ns_ndlp_referenced
) {
791 ndlp
= lpfc_findnode_did(vport
, NameServer_DID
);
796 lpfc_sli_host_down(vport
);
798 lpfc_stop_vport_timers(vport
);
800 if (!(phba
->pport
->load_flag
& FC_UNLOADING
)) {
801 lpfc_unreg_all_rpis(vport
);
802 lpfc_unreg_default_rpis(vport
);
804 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
805 * does the scsi_host_put() to release the vport.
807 if (!(vport
->vpi_state
& LPFC_VPI_REGISTERED
) ||
808 lpfc_mbx_unreg_vpi(vport
))
809 scsi_host_put(shost
);
811 scsi_host_put(shost
);
813 lpfc_free_vpi(phba
, vport
->vpi
);
814 vport
->work_port_events
= 0;
815 spin_lock_irq(&phba
->port_list_lock
);
816 list_del_init(&vport
->listentry
);
817 spin_unlock_irq(&phba
->port_list_lock
);
818 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_VPORT
,
819 "1828 Vport Deleted.\n");
820 scsi_host_put(shost
);
825 lpfc_create_vport_work_array(struct lpfc_hba
*phba
)
827 struct lpfc_vport
*port_iterator
;
828 struct lpfc_vport
**vports
;
830 vports
= kcalloc(phba
->max_vports
+ 1, sizeof(struct lpfc_vport
*),
834 spin_lock_irq(&phba
->port_list_lock
);
835 list_for_each_entry(port_iterator
, &phba
->port_list
, listentry
) {
836 if (port_iterator
->load_flag
& FC_UNLOADING
)
838 if (!scsi_host_get(lpfc_shost_from_vport(port_iterator
))) {
839 lpfc_printf_vlog(port_iterator
, KERN_ERR
, LOG_VPORT
,
840 "1801 Create vport work array FAILED: "
841 "cannot do scsi_host_get\n");
844 vports
[index
++] = port_iterator
;
846 spin_unlock_irq(&phba
->port_list_lock
);
851 lpfc_destroy_vport_work_array(struct lpfc_hba
*phba
, struct lpfc_vport
**vports
)
856 for (i
= 0; i
<= phba
->max_vports
&& vports
[i
] != NULL
; i
++)
857 scsi_host_put(lpfc_shost_from_vport(vports
[i
]));
863 * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
864 * @vport: Pointer to vport object.
866 * This function resets the statistical data for the vport. This function
867 * is called with the host_lock held
870 lpfc_vport_reset_stat_data(struct lpfc_vport
*vport
)
872 struct lpfc_nodelist
*ndlp
= NULL
, *next_ndlp
= NULL
;
874 list_for_each_entry_safe(ndlp
, next_ndlp
, &vport
->fc_nodes
, nlp_listp
) {
875 if (!NLP_CHK_NODE_ACT(ndlp
))
878 memset(ndlp
->lat_data
, 0, LPFC_MAX_BUCKET_COUNT
*
879 sizeof(struct lpfc_scsicmd_bkt
));
885 * lpfc_alloc_bucket - Allocate data buffer required for statistical data
886 * @vport: Pointer to vport object.
888 * This function allocates data buffer required for all the FC
889 * nodes of the vport to collect statistical data.
892 lpfc_alloc_bucket(struct lpfc_vport
*vport
)
894 struct lpfc_nodelist
*ndlp
= NULL
, *next_ndlp
= NULL
;
896 list_for_each_entry_safe(ndlp
, next_ndlp
, &vport
->fc_nodes
, nlp_listp
) {
897 if (!NLP_CHK_NODE_ACT(ndlp
))
900 kfree(ndlp
->lat_data
);
901 ndlp
->lat_data
= NULL
;
903 if (ndlp
->nlp_state
== NLP_STE_MAPPED_NODE
) {
904 ndlp
->lat_data
= kcalloc(LPFC_MAX_BUCKET_COUNT
,
905 sizeof(struct lpfc_scsicmd_bkt
),
909 lpfc_printf_vlog(vport
, KERN_ERR
, LOG_NODE
,
910 "0287 lpfc_alloc_bucket failed to "
911 "allocate statistical data buffer DID "
912 "0x%x\n", ndlp
->nlp_DID
);
918 * lpfc_free_bucket - Free data buffer required for statistical data
919 * @vport: Pointer to vport object.
921 * Th function frees statistical data buffer of all the FC
922 * nodes of the vport.
925 lpfc_free_bucket(struct lpfc_vport
*vport
)
927 struct lpfc_nodelist
*ndlp
= NULL
, *next_ndlp
= NULL
;
929 list_for_each_entry_safe(ndlp
, next_ndlp
, &vport
->fc_nodes
, nlp_listp
) {
930 if (!NLP_CHK_NODE_ACT(ndlp
))
933 kfree(ndlp
->lat_data
);
934 ndlp
->lat_data
= NULL
;