blob: c0eeea0694cbbbd2bdb09085d40d0ca5ada96155 [file] [log] [blame]
James Smart92d7f7b2007-06-17 19:56:38 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
James Smartd080abe2017-02-12 13:52:39 -08004 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James Smart92d7f7b2007-06-17 19:56:38 -05007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James Smart92d7f7b2007-06-17 19:56:38 -05009 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
James Smart92d7f7b2007-06-17 19:56:38 -050032#include <linux/spinlock.h>
33
34#include <scsi/scsi.h>
35#include <scsi/scsi_device.h>
36#include <scsi/scsi_host.h>
37#include <scsi/scsi_transport_fc.h>
James Smart2ea259e2017-02-12 13:52:27 -080038
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_hw4.h"
James Smart92d7f7b2007-06-17 19:56:38 -050040#include "lpfc_hw.h"
41#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040043#include "lpfc_nl.h"
James Smart92d7f7b2007-06-17 19:56:38 -050044#include "lpfc_disc.h"
45#include "lpfc_scsi.h"
46#include "lpfc.h"
47#include "lpfc_logmsg.h"
48#include "lpfc_crtn.h"
49#include "lpfc_version.h"
50#include "lpfc_vport.h"
51
52inline void lpfc_vport_set_state(struct lpfc_vport *vport,
53 enum fc_vport_state new_state)
54{
55 struct fc_vport *fc_vport = vport->fc_vport;
56
57 if (fc_vport) {
58 /*
59 * When the transport defines fc_vport_set state we will replace
60 * this code with the following line
61 */
62 /* fc_vport_set_state(fc_vport, new_state); */
63 if (new_state != FC_VPORT_INITIALIZING)
64 fc_vport->vport_last_state = fc_vport->vport_state;
65 fc_vport->vport_state = new_state;
66 }
67
68 /* for all the error states we will set the invternal state to FAILED */
69 switch (new_state) {
70 case FC_VPORT_NO_FABRIC_SUPP:
71 case FC_VPORT_NO_FABRIC_RSCS:
72 case FC_VPORT_FABRIC_LOGOUT:
73 case FC_VPORT_FABRIC_REJ_WWN:
74 case FC_VPORT_FAILED:
75 vport->port_state = LPFC_VPORT_FAILED;
76 break;
77 case FC_VPORT_LINKDOWN:
78 vport->port_state = LPFC_VPORT_UNKNOWN;
79 break;
80 default:
81 /* do nothing */
82 break;
83 }
84}
85
James Smart16a3a202013-04-17 20:14:38 -040086int
James Smart92d7f7b2007-06-17 19:56:38 -050087lpfc_alloc_vpi(struct lpfc_hba *phba)
88{
James Smart6d368e52011-05-24 11:44:12 -040089 unsigned long vpi;
James Smart92d7f7b2007-06-17 19:56:38 -050090
91 spin_lock_irq(&phba->hbalock);
James Smart858c9f62007-06-17 19:56:39 -050092 /* Start at bit 1 because vpi zero is reserved for the physical port */
93 vpi = find_next_zero_bit(phba->vpi_bmask, (phba->max_vpi + 1), 1);
James Smart92d7f7b2007-06-17 19:56:38 -050094 if (vpi > phba->max_vpi)
95 vpi = 0;
96 else
97 set_bit(vpi, phba->vpi_bmask);
James Smartda0436e2009-05-22 14:51:39 -040098 if (phba->sli_rev == LPFC_SLI_REV4)
99 phba->sli4_hba.max_cfg_param.vpi_used++;
James Smart92d7f7b2007-06-17 19:56:38 -0500100 spin_unlock_irq(&phba->hbalock);
101 return vpi;
102}
103
104static void
105lpfc_free_vpi(struct lpfc_hba *phba, int vpi)
106{
James Smartda0436e2009-05-22 14:51:39 -0400107 if (vpi == 0)
108 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500109 spin_lock_irq(&phba->hbalock);
110 clear_bit(vpi, phba->vpi_bmask);
James Smartda0436e2009-05-22 14:51:39 -0400111 if (phba->sli_rev == LPFC_SLI_REV4)
112 phba->sli4_hba.max_cfg_param.vpi_used--;
James Smart92d7f7b2007-06-17 19:56:38 -0500113 spin_unlock_irq(&phba->hbalock);
114}
115
116static int
117lpfc_vport_sparm(struct lpfc_hba *phba, struct lpfc_vport *vport)
118{
119 LPFC_MBOXQ_t *pmb;
120 MAILBOX_t *mb;
121 struct lpfc_dmabuf *mp;
122 int rc;
123
124 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
125 if (!pmb) {
126 return -ENOMEM;
127 }
James Smartf4b4c682009-05-22 14:53:12 -0400128 mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -0500129
James Smart9f1177a2010-02-26 14:12:57 -0500130 rc = lpfc_read_sparam(phba, pmb, vport->vpi);
131 if (rc) {
132 mempool_free(pmb, phba->mbox_mem_pool);
133 return -ENOMEM;
134 }
135
James Smart92d7f7b2007-06-17 19:56:38 -0500136 /*
137 * Grab buffer pointer and clear context1 so we can use
138 * lpfc_sli_issue_box_wait
139 */
140 mp = (struct lpfc_dmabuf *) pmb->context1;
141 pmb->context1 = NULL;
142
143 pmb->vport = vport;
144 rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
145 if (rc != MBX_SUCCESS) {
James Smart98c9ea52007-10-27 13:37:33 -0400146 if (signal_pending(current)) {
147 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
148 "1830 Signal aborted mbxCmd x%x\n",
149 mb->mbxCommand);
150 lpfc_mbuf_free(phba, mp->virt, mp->phys);
151 kfree(mp);
152 if (rc != MBX_TIMEOUT)
153 mempool_free(pmb, phba->mbox_mem_pool);
154 return -EINTR;
155 } else {
156 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
157 "1818 VPort failed init, mbxCmd x%x "
158 "READ_SPARM mbxStatus x%x, rc = x%x\n",
159 mb->mbxCommand, mb->mbxStatus, rc);
160 lpfc_mbuf_free(phba, mp->virt, mp->phys);
161 kfree(mp);
162 if (rc != MBX_TIMEOUT)
163 mempool_free(pmb, phba->mbox_mem_pool);
164 return -EIO;
165 }
James Smart92d7f7b2007-06-17 19:56:38 -0500166 }
167
168 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
169 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
170 sizeof (struct lpfc_name));
171 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
172 sizeof (struct lpfc_name));
173
174 lpfc_mbuf_free(phba, mp->virt, mp->phys);
175 kfree(mp);
176 mempool_free(pmb, phba->mbox_mem_pool);
177
178 return 0;
179}
180
181static int
182lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn,
183 const char *name_type)
184{
185 /* ensure that IEEE format 1 addresses
186 * contain zeros in bits 59-48
187 */
188 if (!((wwn->u.wwn[0] >> 4) == 1 &&
189 ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0)))
190 return 1;
191
192 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400193 "1822 Invalid %s: %02x:%02x:%02x:%02x:"
James Smart92d7f7b2007-06-17 19:56:38 -0500194 "%02x:%02x:%02x:%02x\n",
James Smarte8b62012007-08-02 11:10:09 -0400195 name_type,
James Smart92d7f7b2007-06-17 19:56:38 -0500196 wwn->u.wwn[0], wwn->u.wwn[1],
197 wwn->u.wwn[2], wwn->u.wwn[3],
198 wwn->u.wwn[4], wwn->u.wwn[5],
199 wwn->u.wwn[6], wwn->u.wwn[7]);
200 return 0;
201}
202
203static int
204lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
205{
206 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400207 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500208
James Smart549e55c2007-08-02 11:09:51 -0400209 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500210 list_for_each_entry(vport, &phba->port_list, listentry) {
211 if (vport == new_vport)
212 continue;
213 /* If they match, return not unique */
214 if (memcmp(&vport->fc_sparam.portName,
James Smart549e55c2007-08-02 11:09:51 -0400215 &new_vport->fc_sparam.portName,
216 sizeof(struct lpfc_name)) == 0) {
217 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500218 return 0;
James Smart549e55c2007-08-02 11:09:51 -0400219 }
James Smart92d7f7b2007-06-17 19:56:38 -0500220 }
James Smart549e55c2007-08-02 11:09:51 -0400221 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500222 return 1;
223}
224
James Smart90160e02008-08-24 21:49:45 -0400225/**
James Smart3621a712009-04-06 18:47:14 -0400226 * lpfc_discovery_wait - Wait for driver discovery to quiesce
James Smart90160e02008-08-24 21:49:45 -0400227 * @vport: The virtual port for which this call is being executed.
228 *
229 * This driver calls this routine specifically from lpfc_vport_delete
230 * to enforce a synchronous execution of vport
231 * delete relative to discovery activities. The
232 * lpfc_vport_delete routine should not return until it
233 * can reasonably guarantee that discovery has quiesced.
234 * Post FDISC LOGO, the driver must wait until its SAN teardown is
235 * complete and all resources recovered before allowing
236 * cleanup.
237 *
238 * This routine does not require any locks held.
239 **/
240static void lpfc_discovery_wait(struct lpfc_vport *vport)
241{
242 struct lpfc_hba *phba = vport->phba;
243 uint32_t wait_flags = 0;
244 unsigned long wait_time_max;
245 unsigned long start_time;
246
247 wait_flags = FC_RSCN_MODE | FC_RSCN_DISCOVERY | FC_NLP_MORE |
248 FC_RSCN_DEFERRED | FC_NDISC_ACTIVE | FC_DISC_TMO;
249
250 /*
251 * The time constraint on this loop is a balance between the
252 * fabric RA_TOV value and dev_loss tmo. The driver's
253 * devloss_tmo is 10 giving this loop a 3x multiplier minimally.
254 */
255 wait_time_max = msecs_to_jiffies(((phba->fc_ratov * 3) + 3) * 1000);
256 wait_time_max += jiffies;
257 start_time = jiffies;
258 while (time_before(jiffies, wait_time_max)) {
259 if ((vport->num_disc_nodes > 0) ||
260 (vport->fc_flag & wait_flags) ||
261 ((vport->port_state > LPFC_VPORT_FAILED) &&
262 (vport->port_state < LPFC_VPORT_READY))) {
James Smart21e9a0a2009-05-22 14:53:21 -0400263 lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
James Smart90160e02008-08-24 21:49:45 -0400264 "1833 Vport discovery quiesce Wait:"
James Smart21e9a0a2009-05-22 14:53:21 -0400265 " state x%x fc_flags x%x"
James Smart90160e02008-08-24 21:49:45 -0400266 " num_nodes x%x, waiting 1000 msecs"
267 " total wait msecs x%x\n",
James Smart21e9a0a2009-05-22 14:53:21 -0400268 vport->port_state, vport->fc_flag,
269 vport->num_disc_nodes,
James Smart90160e02008-08-24 21:49:45 -0400270 jiffies_to_msecs(jiffies - start_time));
271 msleep(1000);
272 } else {
273 /* Base case. Wait variants satisfied. Break out */
James Smart21e9a0a2009-05-22 14:53:21 -0400274 lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
James Smart90160e02008-08-24 21:49:45 -0400275 "1834 Vport discovery quiesced:"
James Smart21e9a0a2009-05-22 14:53:21 -0400276 " state x%x fc_flags x%x"
James Smart90160e02008-08-24 21:49:45 -0400277 " wait msecs x%x\n",
James Smart21e9a0a2009-05-22 14:53:21 -0400278 vport->port_state, vport->fc_flag,
James Smart90160e02008-08-24 21:49:45 -0400279 jiffies_to_msecs(jiffies
280 - start_time));
281 break;
282 }
283 }
284
285 if (time_after(jiffies, wait_time_max))
James Smart21e9a0a2009-05-22 14:53:21 -0400286 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
James Smart90160e02008-08-24 21:49:45 -0400287 "1835 Vport discovery quiesce failed:"
James Smart21e9a0a2009-05-22 14:53:21 -0400288 " state x%x fc_flags x%x wait msecs x%x\n",
289 vport->port_state, vport->fc_flag,
James Smart90160e02008-08-24 21:49:45 -0400290 jiffies_to_msecs(jiffies - start_time));
291}
292
James Smart92d7f7b2007-06-17 19:56:38 -0500293int
294lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
295{
296 struct lpfc_nodelist *ndlp;
James Smart3de2a652007-08-02 11:09:59 -0400297 struct Scsi_Host *shost = fc_vport->shost;
298 struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500299 struct lpfc_hba *phba = pport->phba;
300 struct lpfc_vport *vport = NULL;
301 int instance;
302 int vpi;
303 int rc = VPORT_ERROR;
James Smart98c9ea52007-10-27 13:37:33 -0400304 int status;
James Smart92d7f7b2007-06-17 19:56:38 -0500305
James Smarteada2722008-12-04 22:39:13 -0500306 if ((phba->sli_rev < 3) || !(phba->cfg_enable_npiv)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500307 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400308 "1808 Create VPORT failed: "
James Smart92d7f7b2007-06-17 19:56:38 -0500309 "NPIV is not enabled: SLImode:%d\n",
James Smarte8b62012007-08-02 11:10:09 -0400310 phba->sli_rev);
James Smart92d7f7b2007-06-17 19:56:38 -0500311 rc = VPORT_INVAL;
312 goto error_out;
313 }
314
315 vpi = lpfc_alloc_vpi(phba);
316 if (vpi == 0) {
317 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400318 "1809 Create VPORT failed: "
James Smart92d7f7b2007-06-17 19:56:38 -0500319 "Max VPORTs (%d) exceeded\n",
James Smarte8b62012007-08-02 11:10:09 -0400320 phba->max_vpi);
James Smart92d7f7b2007-06-17 19:56:38 -0500321 rc = VPORT_NORESOURCES;
322 goto error_out;
323 }
324
James Smart92d7f7b2007-06-17 19:56:38 -0500325 /* Assign an unused board number */
326 if ((instance = lpfc_get_instance()) < 0) {
327 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400328 "1810 Create VPORT failed: Cannot get "
329 "instance number\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500330 lpfc_free_vpi(phba, vpi);
331 rc = VPORT_NORESOURCES;
332 goto error_out;
333 }
334
James Smart3de2a652007-08-02 11:09:59 -0400335 vport = lpfc_create_port(phba, instance, &fc_vport->dev);
James Smart92d7f7b2007-06-17 19:56:38 -0500336 if (!vport) {
337 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400338 "1811 Create VPORT failed: vpi x%x\n", vpi);
James Smart92d7f7b2007-06-17 19:56:38 -0500339 lpfc_free_vpi(phba, vpi);
340 rc = VPORT_NORESOURCES;
341 goto error_out;
342 }
343
344 vport->vpi = vpi;
James Smart858c9f62007-06-17 19:56:39 -0500345 lpfc_debugfs_initialize(vport);
346
James Smart98c9ea52007-10-27 13:37:33 -0400347 if ((status = lpfc_vport_sparm(phba, vport))) {
348 if (status == -EINTR) {
349 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
350 "1831 Create VPORT Interrupted.\n");
351 rc = VPORT_ERROR;
352 } else {
353 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
354 "1813 Create VPORT failed. "
355 "Cannot get sparam\n");
356 rc = VPORT_NORESOURCES;
357 }
James Smart92d7f7b2007-06-17 19:56:38 -0500358 lpfc_free_vpi(phba, vpi);
359 destroy_port(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500360 goto error_out;
361 }
362
James Smart1c6834a2009-07-19 10:01:26 -0400363 u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
364 u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -0500365
366 memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
367 memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
368
369 if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
370 !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
James Smarte8b62012007-08-02 11:10:09 -0400371 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
372 "1821 Create VPORT failed. "
373 "Invalid WWN format\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500374 lpfc_free_vpi(phba, vpi);
375 destroy_port(vport);
376 rc = VPORT_INVAL;
377 goto error_out;
378 }
379
380 if (!lpfc_unique_wwpn(phba, vport)) {
James Smarte8b62012007-08-02 11:10:09 -0400381 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
382 "1823 Create VPORT failed. "
383 "Duplicate WWN on HBA\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500384 lpfc_free_vpi(phba, vpi);
385 destroy_port(vport);
386 rc = VPORT_INVAL;
387 goto error_out;
388 }
389
James Smarteada2722008-12-04 22:39:13 -0500390 /* Create binary sysfs attribute for vport */
391 lpfc_alloc_sysfs_attr(vport);
392
James Smart572709e2013-07-15 18:32:43 -0400393 /* Set the DFT_LUN_Q_DEPTH accordingly */
394 vport->cfg_lun_queue_depth = phba->pport->cfg_lun_queue_depth;
395
James Smart92d7f7b2007-06-17 19:56:38 -0500396 *(struct lpfc_vport **)fc_vport->dd_data = vport;
397 vport->fc_vport = fc_vport;
398
James Smart4258e982015-12-16 18:11:58 -0500399 /* At this point we are fully registered with SCSI Layer. */
400 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -0700401 if (phba->cfg_enable_SmartSAN ||
402 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -0500403 /* Setup appropriate attribute masks */
404 vport->fdmi_hba_mask = phba->pport->fdmi_hba_mask;
405 vport->fdmi_port_mask = phba->pport->fdmi_port_mask;
406 }
407
James Smart01649562017-02-12 13:52:32 -0800408 if ((phba->nvmet_support == 0) &&
409 ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
410 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))) {
411 /* Create NVME binding with nvme_fc_transport. This
412 * ensures the vport is initialized.
413 */
414 rc = lpfc_nvme_create_localport(vport);
415 if (rc) {
416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
417 "6003 %s status x%x\n",
418 "NVME registration failed, ",
419 rc);
420 goto error_out;
421 }
422 }
James Smart895427b2017-02-12 13:52:30 -0800423
James Smart1c6834a2009-07-19 10:01:26 -0400424 /*
425 * In SLI4, the vpi must be activated before it can be used
426 * by the port.
427 */
428 if ((phba->sli_rev == LPFC_SLI_REV4) &&
James Smart76a95d72010-11-20 23:11:48 -0500429 (pport->fc_flag & FC_VFI_REGISTERED)) {
430 rc = lpfc_sli4_init_vpi(vport);
James Smart1c6834a2009-07-19 10:01:26 -0400431 if (rc) {
432 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
433 "1838 Failed to INIT_VPI on vpi %d "
434 "status %d\n", vpi, rc);
435 rc = VPORT_NORESOURCES;
436 lpfc_free_vpi(phba, vpi);
437 goto error_out;
438 }
439 } else if (phba->sli_rev == LPFC_SLI_REV4) {
440 /*
441 * Driver cannot INIT_VPI now. Set the flags to
442 * init_vpi when reg_vfi complete.
443 */
444 vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
445 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
446 rc = VPORT_OK;
447 goto out;
448 }
449
James Smart92d7f7b2007-06-17 19:56:38 -0500450 if ((phba->link_state < LPFC_LINK_UP) ||
James Smart1c6834a2009-07-19 10:01:26 -0400451 (pport->port_state < LPFC_FABRIC_CFG_LINK) ||
James Smart76a95d72010-11-20 23:11:48 -0500452 (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500453 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
454 rc = VPORT_OK;
455 goto out;
456 }
457
458 if (disable) {
James Smarteada2722008-12-04 22:39:13 -0500459 lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
James Smart92d7f7b2007-06-17 19:56:38 -0500460 rc = VPORT_OK;
461 goto out;
462 }
463
464 /* Use the Physical nodes Fabric NDLP to determine if the link is
465 * up and ready to FDISC.
466 */
467 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
James Smarte47c9092008-02-08 18:49:26 -0500468 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
469 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
James Smart858c9f62007-06-17 19:56:39 -0500470 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
471 lpfc_set_disctmo(vport);
472 lpfc_initial_fdisc(vport);
473 } else {
474 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -0400475 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
476 "0262 No NPIV Fabric support\n");
James Smart858c9f62007-06-17 19:56:39 -0500477 }
James Smart92d7f7b2007-06-17 19:56:38 -0500478 } else {
479 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
480 }
481 rc = VPORT_OK;
482
483out:
James Smarta58cbd52007-08-02 11:09:43 -0400484 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
485 "1825 Vport Created.\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500486 lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
487error_out:
488 return rc;
489}
490
James Smart311464e2007-08-02 11:10:37 -0400491static int
James Smart92d7f7b2007-06-17 19:56:38 -0500492disable_vport(struct fc_vport *fc_vport)
493{
494 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
495 struct lpfc_hba *phba = vport->phba;
496 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
497 long timeout;
James Smartfedd3b72011-02-16 12:39:24 -0500498 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500499
500 ndlp = lpfc_findnode_did(vport, Fabric_DID);
James Smarte47c9092008-02-08 18:49:26 -0500501 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
502 && phba->link_state >= LPFC_LINK_UP) {
James Smart92d7f7b2007-06-17 19:56:38 -0500503 vport->unreg_vpi_cmpl = VPORT_INVAL;
504 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
505 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
506 while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
507 timeout = schedule_timeout(timeout);
508 }
509
510 lpfc_sli_host_down(vport);
511
512 /* Mark all nodes for discovery so we can remove them by
513 * calling lpfc_cleanup_rpis(vport, 1)
514 */
515 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500516 if (!NLP_CHK_NODE_ACT(ndlp))
517 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500518 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
519 continue;
520 lpfc_disc_state_machine(vport, ndlp, NULL,
521 NLP_EVT_DEVICE_RECOVERY);
522 }
523 lpfc_cleanup_rpis(vport, 1);
524
525 lpfc_stop_vport_timers(vport);
526 lpfc_unreg_all_rpis(vport);
527 lpfc_unreg_default_rpis(vport);
528 /*
529 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
530 * scsi_host_put() to release the vport.
531 */
532 lpfc_mbx_unreg_vpi(vport);
James Smartfedd3b72011-02-16 12:39:24 -0500533 spin_lock_irq(shost->host_lock);
534 vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
535 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500536
537 lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
James Smarta58cbd52007-08-02 11:09:43 -0400538 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
539 "1826 Vport Disabled.\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500540 return VPORT_OK;
541}
542
James Smart311464e2007-08-02 11:10:37 -0400543static int
James Smart92d7f7b2007-06-17 19:56:38 -0500544enable_vport(struct fc_vport *fc_vport)
545{
546 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
547 struct lpfc_hba *phba = vport->phba;
548 struct lpfc_nodelist *ndlp = NULL;
James Smart72100cc2010-02-12 14:43:01 -0500549 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500550
551 if ((phba->link_state < LPFC_LINK_UP) ||
James Smart76a95d72010-11-20 23:11:48 -0500552 (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500553 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
554 return VPORT_OK;
555 }
556
James Smart72100cc2010-02-12 14:43:01 -0500557 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500558 vport->load_flag |= FC_LOADING;
James Smart104450e2016-12-19 15:07:25 -0800559 if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
560 spin_unlock_irq(shost->host_lock);
561 lpfc_issue_init_vpi(vport);
562 goto out;
563 }
564
James Smart92d7f7b2007-06-17 19:56:38 -0500565 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -0500566 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500567
568 /* Use the Physical nodes Fabric NDLP to determine if the link is
569 * up and ready to FDISC.
570 */
571 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
James Smarte47c9092008-02-08 18:49:26 -0500572 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
573 && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
James Smart858c9f62007-06-17 19:56:39 -0500574 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
575 lpfc_set_disctmo(vport);
576 lpfc_initial_fdisc(vport);
577 } else {
578 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -0400579 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
580 "0264 No NPIV Fabric support\n");
James Smart858c9f62007-06-17 19:56:39 -0500581 }
James Smart92d7f7b2007-06-17 19:56:38 -0500582 } else {
583 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
584 }
James Smart104450e2016-12-19 15:07:25 -0800585
586out:
James Smarta58cbd52007-08-02 11:09:43 -0400587 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
588 "1827 Vport Enabled.\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500589 return VPORT_OK;
590}
591
592int
593lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
594{
595 if (disable)
596 return disable_vport(fc_vport);
597 else
598 return enable_vport(fc_vport);
599}
600
601
602int
603lpfc_vport_delete(struct fc_vport *fc_vport)
604{
605 struct lpfc_nodelist *ndlp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -0500606 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
James Smartcc823552015-05-21 13:55:26 -0400607 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500608 struct lpfc_hba *phba = vport->phba;
609 long timeout;
James Smart16a3a202013-04-17 20:14:38 -0400610 bool ns_ndlp_referenced = false;
James Smart92d7f7b2007-06-17 19:56:38 -0500611
James Smart51ef4c22007-08-02 11:10:31 -0400612 if (vport->port_type == LPFC_PHYSICAL_PORT) {
613 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
614 "1812 vport_delete failed: Cannot delete "
615 "physical host\n");
616 return VPORT_ERROR;
617 }
James Smart21e9a0a2009-05-22 14:53:21 -0400618
619 /* If the vport is a static vport fail the deletion. */
620 if ((vport->vport_flag & STATIC_VPORT) &&
621 !(phba->pport->load_flag & FC_UNLOADING)) {
622 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
623 "1837 vport_delete failed: Cannot delete "
624 "static vport.\n");
625 return VPORT_ERROR;
626 }
James Smartd439d282010-09-29 11:18:45 -0400627 spin_lock_irq(&phba->hbalock);
628 vport->load_flag |= FC_UNLOADING;
629 spin_unlock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -0400630 /*
631 * If we are not unloading the driver then prevent the vport_delete
632 * from happening until after this vport's discovery is finished.
633 */
634 if (!(phba->pport->load_flag & FC_UNLOADING)) {
635 int check_count = 0;
636 while (check_count < ((phba->fc_ratov * 3) + 3) &&
637 vport->port_state > LPFC_VPORT_FAILED &&
638 vport->port_state < LPFC_VPORT_READY) {
639 check_count++;
640 msleep(1000);
641 }
642 if (vport->port_state > LPFC_VPORT_FAILED &&
643 vport->port_state < LPFC_VPORT_READY)
644 return -EAGAIN;
645 }
James Smart92d7f7b2007-06-17 19:56:38 -0500646 /*
647 * This is a bit of a mess. We want to ensure the shost doesn't get
648 * torn down until we're done with the embedded lpfc_vport structure.
649 *
650 * Beyond holding a reference for this function, we also need a
651 * reference for outstanding I/O requests we schedule during delete
652 * processing. But once we scsi_remove_host() we can no longer obtain
653 * a reference through scsi_host_get().
654 *
655 * So we take two references here. We release one reference at the
656 * bottom of the function -- after delinking the vport. And we
657 * release the other at the completion of the unreg_vpi that get's
658 * initiated after we've disposed of all other resources associated
659 * with the port.
660 */
James Smartd7c255b2008-08-24 21:50:00 -0400661 if (!scsi_host_get(shost))
James Smart92d7f7b2007-06-17 19:56:38 -0500662 return VPORT_INVAL;
James Smartd7c255b2008-08-24 21:50:00 -0400663 if (!scsi_host_get(shost)) {
664 scsi_host_put(shost);
665 return VPORT_INVAL;
666 }
James Smarteada2722008-12-04 22:39:13 -0500667 lpfc_free_sysfs_attr(vport);
668
James Smart858c9f62007-06-17 19:56:39 -0500669 lpfc_debugfs_terminate(vport);
James Smarteada2722008-12-04 22:39:13 -0500670
James Smart16a3a202013-04-17 20:14:38 -0400671 /*
672 * The call to fc_remove_host might release the NameServer ndlp. Since
673 * we might need to use the ndlp to send the DA_ID CT command,
674 * increment the reference for the NameServer ndlp to prevent it from
675 * being released.
676 */
677 ndlp = lpfc_findnode_did(vport, NameServer_DID);
678 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
679 lpfc_nlp_get(ndlp);
680 ns_ndlp_referenced = true;
681 }
682
James Smarteada2722008-12-04 22:39:13 -0500683 /* Remove FC host and then SCSI host with the vport */
James Smartcc823552015-05-21 13:55:26 -0400684 fc_remove_host(shost);
685 scsi_remove_host(shost);
James Smart92d7f7b2007-06-17 19:56:38 -0500686
687 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
James Smarte47c9092008-02-08 18:49:26 -0500688
689 /* In case of driver unload, we shall not perform fabric logo as the
690 * worker thread already stopped at this stage and, in this case, we
691 * can safely skip the fabric logo.
692 */
693 if (phba->pport->load_flag & FC_UNLOADING) {
694 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
695 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
696 phba->link_state >= LPFC_LINK_UP) {
697 /* First look for the Fabric ndlp */
698 ndlp = lpfc_findnode_did(vport, Fabric_DID);
699 if (!ndlp)
700 goto skip_logo;
701 else if (!NLP_CHK_NODE_ACT(ndlp)) {
702 ndlp = lpfc_enable_node(vport, ndlp,
703 NLP_STE_UNUSED_NODE);
704 if (!ndlp)
705 goto skip_logo;
706 }
707 /* Remove ndlp from vport npld list */
708 lpfc_dequeue_node(vport, ndlp);
709
710 /* Indicate free memory when release */
711 spin_lock_irq(&phba->ndlp_lock);
712 NLP_SET_FREE_REQ(ndlp);
713 spin_unlock_irq(&phba->ndlp_lock);
714 /* Kick off release ndlp when it can be safely done */
715 lpfc_nlp_put(ndlp);
716 }
717 goto skip_logo;
718 }
719
720 /* Otherwise, we will perform fabric logo as needed */
721 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
722 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
James Smart58da1ff2008-04-07 10:15:56 -0400723 phba->link_state >= LPFC_LINK_UP &&
James Smart76a95d72010-11-20 23:11:48 -0500724 phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
James Smart7ee5d432007-10-27 13:37:17 -0400725 if (vport->cfg_enable_da_id) {
726 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
727 if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
728 while (vport->ct_flags && timeout)
729 timeout = schedule_timeout(timeout);
730 else
731 lpfc_printf_log(vport->phba, KERN_WARNING,
732 LOG_VPORT,
733 "1829 CT command failed to "
James Smarte4e74272009-07-19 10:01:38 -0400734 "delete objects on fabric\n");
James Smart7ee5d432007-10-27 13:37:17 -0400735 }
James Smart92d7f7b2007-06-17 19:56:38 -0500736 /* First look for the Fabric ndlp */
737 ndlp = lpfc_findnode_did(vport, Fabric_DID);
738 if (!ndlp) {
739 /* Cannot find existing Fabric ndlp, allocate one */
740 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
741 if (!ndlp)
742 goto skip_logo;
743 lpfc_nlp_init(vport, ndlp, Fabric_DID);
James Smarte47c9092008-02-08 18:49:26 -0500744 /* Indicate free memory when release */
745 NLP_SET_FREE_REQ(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500746 } else {
James Smart73d91e52011-10-10 21:32:10 -0400747 if (!NLP_CHK_NODE_ACT(ndlp)) {
James Smarte47c9092008-02-08 18:49:26 -0500748 ndlp = lpfc_enable_node(vport, ndlp,
749 NLP_STE_UNUSED_NODE);
750 if (!ndlp)
751 goto skip_logo;
James Smart73d91e52011-10-10 21:32:10 -0400752 }
James Smarte47c9092008-02-08 18:49:26 -0500753
James Smart73d91e52011-10-10 21:32:10 -0400754 /* Remove ndlp from vport list */
James Smart92d7f7b2007-06-17 19:56:38 -0500755 lpfc_dequeue_node(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -0500756 spin_lock_irq(&phba->ndlp_lock);
757 if (!NLP_CHK_FREE_REQ(ndlp))
758 /* Indicate free memory when release */
759 NLP_SET_FREE_REQ(ndlp);
760 else {
761 /* Skip this if ndlp is already in free mode */
762 spin_unlock_irq(&phba->ndlp_lock);
763 goto skip_logo;
764 }
765 spin_unlock_irq(&phba->ndlp_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500766 }
James Smart73d91e52011-10-10 21:32:10 -0400767
768 /*
769 * If the vpi is not registered, then a valid FDISC doesn't
770 * exist and there is no need for a ELS LOGO. Just cleanup
771 * the ndlp.
772 */
773 if (!(vport->vpi_state & LPFC_VPI_REGISTERED)) {
774 lpfc_nlp_put(ndlp);
James Smart5ffc2662009-11-18 15:39:44 -0500775 goto skip_logo;
James Smart73d91e52011-10-10 21:32:10 -0400776 }
777
James Smart92d7f7b2007-06-17 19:56:38 -0500778 vport->unreg_vpi_cmpl = VPORT_INVAL;
779 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
780 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
781 while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
782 timeout = schedule_timeout(timeout);
783 }
784
James Smart90160e02008-08-24 21:49:45 -0400785 if (!(phba->pport->load_flag & FC_UNLOADING))
786 lpfc_discovery_wait(vport);
787
James Smart92d7f7b2007-06-17 19:56:38 -0500788skip_logo:
James Smart16a3a202013-04-17 20:14:38 -0400789
790 /*
791 * If the NameServer ndlp has been incremented to allow the DA_ID CT
792 * command to be sent, decrement the ndlp now.
793 */
794 if (ns_ndlp_referenced) {
795 ndlp = lpfc_findnode_did(vport, NameServer_DID);
796 lpfc_nlp_put(ndlp);
797 }
798
James Smart87af33f2007-10-27 13:37:43 -0400799 lpfc_cleanup(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500800 lpfc_sli_host_down(vport);
801
James Smart92d7f7b2007-06-17 19:56:38 -0500802 lpfc_stop_vport_timers(vport);
James Smart87af33f2007-10-27 13:37:43 -0400803
804 if (!(phba->pport->load_flag & FC_UNLOADING)) {
James Smarte47c9092008-02-08 18:49:26 -0500805 lpfc_unreg_all_rpis(vport);
James Smart87af33f2007-10-27 13:37:43 -0400806 lpfc_unreg_default_rpis(vport);
807 /*
808 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
809 * does the scsi_host_put() to release the vport.
810 */
James Smartcc823552015-05-21 13:55:26 -0400811 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) ||
812 lpfc_mbx_unreg_vpi(vport))
James Smartd7c255b2008-08-24 21:50:00 -0400813 scsi_host_put(shost);
814 } else
815 scsi_host_put(shost);
James Smart92d7f7b2007-06-17 19:56:38 -0500816
817 lpfc_free_vpi(phba, vport->vpi);
818 vport->work_port_events = 0;
819 spin_lock_irq(&phba->hbalock);
820 list_del_init(&vport->listentry);
821 spin_unlock_irq(&phba->hbalock);
James Smarta58cbd52007-08-02 11:09:43 -0400822 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
823 "1828 Vport Deleted.\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500824 scsi_host_put(shost);
James Smart51ef4c22007-08-02 11:10:31 -0400825 return VPORT_OK;
James Smart92d7f7b2007-06-17 19:56:38 -0500826}
827
James Smart549e55c2007-08-02 11:09:51 -0400828struct lpfc_vport **
829lpfc_create_vport_work_array(struct lpfc_hba *phba)
830{
831 struct lpfc_vport *port_iterator;
832 struct lpfc_vport **vports;
833 int index = 0;
James Smart21e9a0a2009-05-22 14:53:21 -0400834 vports = kzalloc((phba->max_vports + 1) * sizeof(struct lpfc_vport *),
James Smart549e55c2007-08-02 11:09:51 -0400835 GFP_KERNEL);
836 if (vports == NULL)
837 return NULL;
838 spin_lock_irq(&phba->hbalock);
839 list_for_each_entry(port_iterator, &phba->port_list, listentry) {
James Smartdf9e1b52011-12-13 13:22:17 -0500840 if (port_iterator->load_flag & FC_UNLOADING)
841 continue;
James Smarte8b62012007-08-02 11:10:09 -0400842 if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
James Smartdf9e1b52011-12-13 13:22:17 -0500843 lpfc_printf_vlog(port_iterator, KERN_ERR, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400844 "1801 Create vport work array FAILED: "
845 "cannot do scsi_host_get\n");
James Smart549e55c2007-08-02 11:09:51 -0400846 continue;
James Smarte8b62012007-08-02 11:10:09 -0400847 }
James Smart549e55c2007-08-02 11:09:51 -0400848 vports[index++] = port_iterator;
849 }
850 spin_unlock_irq(&phba->hbalock);
851 return vports;
852}
853
854void
James Smart09372822008-01-11 01:52:54 -0500855lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
James Smart549e55c2007-08-02 11:09:51 -0400856{
857 int i;
858 if (vports == NULL)
859 return;
James Smart98fc5dd2010-06-07 15:24:29 -0400860 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400861 scsi_host_put(lpfc_shost_from_vport(vports[i]));
862 kfree(vports);
863}
James Smartea2151b2008-09-07 11:52:10 -0400864
865
866/**
James Smart3621a712009-04-06 18:47:14 -0400867 * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
James Smartea2151b2008-09-07 11:52:10 -0400868 * @vport: Pointer to vport object.
869 *
870 * This function resets the statistical data for the vport. This function
871 * is called with the host_lock held
872 **/
873void
874lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
875{
876 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
877
878 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
879 if (!NLP_CHK_NODE_ACT(ndlp))
880 continue;
881 if (ndlp->lat_data)
882 memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
883 sizeof(struct lpfc_scsicmd_bkt));
884 }
885}
886
887
888/**
James Smart3621a712009-04-06 18:47:14 -0400889 * lpfc_alloc_bucket - Allocate data buffer required for statistical data
James Smartea2151b2008-09-07 11:52:10 -0400890 * @vport: Pointer to vport object.
891 *
892 * This function allocates data buffer required for all the FC
893 * nodes of the vport to collect statistical data.
894 **/
895void
896lpfc_alloc_bucket(struct lpfc_vport *vport)
897{
898 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
899
900 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
901 if (!NLP_CHK_NODE_ACT(ndlp))
902 continue;
903
904 kfree(ndlp->lat_data);
905 ndlp->lat_data = NULL;
906
907 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
908 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
909 sizeof(struct lpfc_scsicmd_bkt),
910 GFP_ATOMIC);
911
912 if (!ndlp->lat_data)
913 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
914 "0287 lpfc_alloc_bucket failed to "
915 "allocate statistical data buffer DID "
916 "0x%x\n", ndlp->nlp_DID);
917 }
918 }
919}
920
921/**
James Smart3621a712009-04-06 18:47:14 -0400922 * lpfc_free_bucket - Free data buffer required for statistical data
James Smartea2151b2008-09-07 11:52:10 -0400923 * @vport: Pointer to vport object.
924 *
925 * Th function frees statistical data buffer of all the FC
926 * nodes of the vport.
927 **/
928void
929lpfc_free_bucket(struct lpfc_vport *vport)
930{
931 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
932
933 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
934 if (!NLP_CHK_NODE_ACT(ndlp))
935 continue;
936
937 kfree(ndlp->lat_data);
938 ndlp->lat_data = NULL;
939 }
940}