blob: 917ad56b0aff33c358e9632630142c75003f53e9 [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 Smarte47c9092008-02-08 18:49:26 -05004 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
James Smart92d7f7b2007-06-17 19:56:38 -05005 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
20 *******************************************************************/
21
22#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
27#include <linux/kthread.h>
28#include <linux/pci.h>
29#include <linux/spinlock.h>
30
31#include <scsi/scsi.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_host.h>
34#include <scsi/scsi_transport_fc.h>
35#include "lpfc_hw.h"
36#include "lpfc_sli.h"
James Smartea2151b2008-09-07 11:52:10 -040037#include "lpfc_nl.h"
James Smart92d7f7b2007-06-17 19:56:38 -050038#include "lpfc_disc.h"
39#include "lpfc_scsi.h"
40#include "lpfc.h"
41#include "lpfc_logmsg.h"
42#include "lpfc_crtn.h"
43#include "lpfc_version.h"
44#include "lpfc_vport.h"
45
46inline void lpfc_vport_set_state(struct lpfc_vport *vport,
47 enum fc_vport_state new_state)
48{
49 struct fc_vport *fc_vport = vport->fc_vport;
50
51 if (fc_vport) {
52 /*
53 * When the transport defines fc_vport_set state we will replace
54 * this code with the following line
55 */
56 /* fc_vport_set_state(fc_vport, new_state); */
57 if (new_state != FC_VPORT_INITIALIZING)
58 fc_vport->vport_last_state = fc_vport->vport_state;
59 fc_vport->vport_state = new_state;
60 }
61
62 /* for all the error states we will set the invternal state to FAILED */
63 switch (new_state) {
64 case FC_VPORT_NO_FABRIC_SUPP:
65 case FC_VPORT_NO_FABRIC_RSCS:
66 case FC_VPORT_FABRIC_LOGOUT:
67 case FC_VPORT_FABRIC_REJ_WWN:
68 case FC_VPORT_FAILED:
69 vport->port_state = LPFC_VPORT_FAILED;
70 break;
71 case FC_VPORT_LINKDOWN:
72 vport->port_state = LPFC_VPORT_UNKNOWN;
73 break;
74 default:
75 /* do nothing */
76 break;
77 }
78}
79
80static int
81lpfc_alloc_vpi(struct lpfc_hba *phba)
82{
83 int vpi;
84
85 spin_lock_irq(&phba->hbalock);
James Smart858c9f62007-06-17 19:56:39 -050086 /* Start at bit 1 because vpi zero is reserved for the physical port */
87 vpi = find_next_zero_bit(phba->vpi_bmask, (phba->max_vpi + 1), 1);
James Smart92d7f7b2007-06-17 19:56:38 -050088 if (vpi > phba->max_vpi)
89 vpi = 0;
90 else
91 set_bit(vpi, phba->vpi_bmask);
92 spin_unlock_irq(&phba->hbalock);
93 return vpi;
94}
95
96static void
97lpfc_free_vpi(struct lpfc_hba *phba, int vpi)
98{
99 spin_lock_irq(&phba->hbalock);
100 clear_bit(vpi, phba->vpi_bmask);
101 spin_unlock_irq(&phba->hbalock);
102}
103
104static int
105lpfc_vport_sparm(struct lpfc_hba *phba, struct lpfc_vport *vport)
106{
107 LPFC_MBOXQ_t *pmb;
108 MAILBOX_t *mb;
109 struct lpfc_dmabuf *mp;
110 int rc;
111
112 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
113 if (!pmb) {
114 return -ENOMEM;
115 }
116 mb = &pmb->mb;
117
118 lpfc_read_sparam(phba, pmb, vport->vpi);
119 /*
120 * Grab buffer pointer and clear context1 so we can use
121 * lpfc_sli_issue_box_wait
122 */
123 mp = (struct lpfc_dmabuf *) pmb->context1;
124 pmb->context1 = NULL;
125
126 pmb->vport = vport;
127 rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
128 if (rc != MBX_SUCCESS) {
James Smart98c9ea52007-10-27 13:37:33 -0400129 if (signal_pending(current)) {
130 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
131 "1830 Signal aborted mbxCmd x%x\n",
132 mb->mbxCommand);
133 lpfc_mbuf_free(phba, mp->virt, mp->phys);
134 kfree(mp);
135 if (rc != MBX_TIMEOUT)
136 mempool_free(pmb, phba->mbox_mem_pool);
137 return -EINTR;
138 } else {
139 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
140 "1818 VPort failed init, mbxCmd x%x "
141 "READ_SPARM mbxStatus x%x, rc = x%x\n",
142 mb->mbxCommand, mb->mbxStatus, rc);
143 lpfc_mbuf_free(phba, mp->virt, mp->phys);
144 kfree(mp);
145 if (rc != MBX_TIMEOUT)
146 mempool_free(pmb, phba->mbox_mem_pool);
147 return -EIO;
148 }
James Smart92d7f7b2007-06-17 19:56:38 -0500149 }
150
151 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
152 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
153 sizeof (struct lpfc_name));
154 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
155 sizeof (struct lpfc_name));
156
157 lpfc_mbuf_free(phba, mp->virt, mp->phys);
158 kfree(mp);
159 mempool_free(pmb, phba->mbox_mem_pool);
160
161 return 0;
162}
163
164static int
165lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn,
166 const char *name_type)
167{
168 /* ensure that IEEE format 1 addresses
169 * contain zeros in bits 59-48
170 */
171 if (!((wwn->u.wwn[0] >> 4) == 1 &&
172 ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0)))
173 return 1;
174
175 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400176 "1822 Invalid %s: %02x:%02x:%02x:%02x:"
James Smart92d7f7b2007-06-17 19:56:38 -0500177 "%02x:%02x:%02x:%02x\n",
James Smarte8b62012007-08-02 11:10:09 -0400178 name_type,
James Smart92d7f7b2007-06-17 19:56:38 -0500179 wwn->u.wwn[0], wwn->u.wwn[1],
180 wwn->u.wwn[2], wwn->u.wwn[3],
181 wwn->u.wwn[4], wwn->u.wwn[5],
182 wwn->u.wwn[6], wwn->u.wwn[7]);
183 return 0;
184}
185
186static int
187lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
188{
189 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400190 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -0500191
James Smart549e55c2007-08-02 11:09:51 -0400192 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500193 list_for_each_entry(vport, &phba->port_list, listentry) {
194 if (vport == new_vport)
195 continue;
196 /* If they match, return not unique */
197 if (memcmp(&vport->fc_sparam.portName,
James Smart549e55c2007-08-02 11:09:51 -0400198 &new_vport->fc_sparam.portName,
199 sizeof(struct lpfc_name)) == 0) {
200 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500201 return 0;
James Smart549e55c2007-08-02 11:09:51 -0400202 }
James Smart92d7f7b2007-06-17 19:56:38 -0500203 }
James Smart549e55c2007-08-02 11:09:51 -0400204 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -0500205 return 1;
206}
207
James Smart90160e02008-08-24 21:49:45 -0400208/**
James Smart3621a712009-04-06 18:47:14 -0400209 * lpfc_discovery_wait - Wait for driver discovery to quiesce
James Smart90160e02008-08-24 21:49:45 -0400210 * @vport: The virtual port for which this call is being executed.
211 *
212 * This driver calls this routine specifically from lpfc_vport_delete
213 * to enforce a synchronous execution of vport
214 * delete relative to discovery activities. The
215 * lpfc_vport_delete routine should not return until it
216 * can reasonably guarantee that discovery has quiesced.
217 * Post FDISC LOGO, the driver must wait until its SAN teardown is
218 * complete and all resources recovered before allowing
219 * cleanup.
220 *
221 * This routine does not require any locks held.
222 **/
223static void lpfc_discovery_wait(struct lpfc_vport *vport)
224{
225 struct lpfc_hba *phba = vport->phba;
226 uint32_t wait_flags = 0;
227 unsigned long wait_time_max;
228 unsigned long start_time;
229
230 wait_flags = FC_RSCN_MODE | FC_RSCN_DISCOVERY | FC_NLP_MORE |
231 FC_RSCN_DEFERRED | FC_NDISC_ACTIVE | FC_DISC_TMO;
232
233 /*
234 * The time constraint on this loop is a balance between the
235 * fabric RA_TOV value and dev_loss tmo. The driver's
236 * devloss_tmo is 10 giving this loop a 3x multiplier minimally.
237 */
238 wait_time_max = msecs_to_jiffies(((phba->fc_ratov * 3) + 3) * 1000);
239 wait_time_max += jiffies;
240 start_time = jiffies;
241 while (time_before(jiffies, wait_time_max)) {
242 if ((vport->num_disc_nodes > 0) ||
243 (vport->fc_flag & wait_flags) ||
244 ((vport->port_state > LPFC_VPORT_FAILED) &&
245 (vport->port_state < LPFC_VPORT_READY))) {
246 lpfc_printf_log(phba, KERN_INFO, LOG_VPORT,
247 "1833 Vport discovery quiesce Wait:"
248 " vpi x%x state x%x fc_flags x%x"
249 " num_nodes x%x, waiting 1000 msecs"
250 " total wait msecs x%x\n",
251 vport->vpi, vport->port_state,
252 vport->fc_flag, vport->num_disc_nodes,
253 jiffies_to_msecs(jiffies - start_time));
254 msleep(1000);
255 } else {
256 /* Base case. Wait variants satisfied. Break out */
257 lpfc_printf_log(phba, KERN_INFO, LOG_VPORT,
258 "1834 Vport discovery quiesced:"
259 " vpi x%x state x%x fc_flags x%x"
260 " wait msecs x%x\n",
261 vport->vpi, vport->port_state,
262 vport->fc_flag,
263 jiffies_to_msecs(jiffies
264 - start_time));
265 break;
266 }
267 }
268
269 if (time_after(jiffies, wait_time_max))
270 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
271 "1835 Vport discovery quiesce failed:"
272 " vpi x%x state x%x fc_flags x%x"
273 " wait msecs x%x\n",
274 vport->vpi, vport->port_state,
275 vport->fc_flag,
276 jiffies_to_msecs(jiffies - start_time));
277}
278
James Smart92d7f7b2007-06-17 19:56:38 -0500279int
280lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
281{
282 struct lpfc_nodelist *ndlp;
James Smart3de2a652007-08-02 11:09:59 -0400283 struct Scsi_Host *shost = fc_vport->shost;
284 struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500285 struct lpfc_hba *phba = pport->phba;
286 struct lpfc_vport *vport = NULL;
287 int instance;
288 int vpi;
289 int rc = VPORT_ERROR;
James Smart98c9ea52007-10-27 13:37:33 -0400290 int status;
James Smart92d7f7b2007-06-17 19:56:38 -0500291
James Smarteada2722008-12-04 22:39:13 -0500292 if ((phba->sli_rev < 3) || !(phba->cfg_enable_npiv)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500293 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400294 "1808 Create VPORT failed: "
James Smart92d7f7b2007-06-17 19:56:38 -0500295 "NPIV is not enabled: SLImode:%d\n",
James Smarte8b62012007-08-02 11:10:09 -0400296 phba->sli_rev);
James Smart92d7f7b2007-06-17 19:56:38 -0500297 rc = VPORT_INVAL;
298 goto error_out;
299 }
300
301 vpi = lpfc_alloc_vpi(phba);
302 if (vpi == 0) {
303 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400304 "1809 Create VPORT failed: "
James Smart92d7f7b2007-06-17 19:56:38 -0500305 "Max VPORTs (%d) exceeded\n",
James Smarte8b62012007-08-02 11:10:09 -0400306 phba->max_vpi);
James Smart92d7f7b2007-06-17 19:56:38 -0500307 rc = VPORT_NORESOURCES;
308 goto error_out;
309 }
310
311
312 /* Assign an unused board number */
313 if ((instance = lpfc_get_instance()) < 0) {
314 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400315 "1810 Create VPORT failed: Cannot get "
316 "instance number\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500317 lpfc_free_vpi(phba, vpi);
318 rc = VPORT_NORESOURCES;
319 goto error_out;
320 }
321
James Smart3de2a652007-08-02 11:09:59 -0400322 vport = lpfc_create_port(phba, instance, &fc_vport->dev);
James Smart92d7f7b2007-06-17 19:56:38 -0500323 if (!vport) {
324 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400325 "1811 Create VPORT failed: vpi x%x\n", vpi);
James Smart92d7f7b2007-06-17 19:56:38 -0500326 lpfc_free_vpi(phba, vpi);
327 rc = VPORT_NORESOURCES;
328 goto error_out;
329 }
330
331 vport->vpi = vpi;
James Smart858c9f62007-06-17 19:56:39 -0500332 lpfc_debugfs_initialize(vport);
333
James Smart98c9ea52007-10-27 13:37:33 -0400334 if ((status = lpfc_vport_sparm(phba, vport))) {
335 if (status == -EINTR) {
336 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
337 "1831 Create VPORT Interrupted.\n");
338 rc = VPORT_ERROR;
339 } else {
340 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
341 "1813 Create VPORT failed. "
342 "Cannot get sparam\n");
343 rc = VPORT_NORESOURCES;
344 }
James Smart92d7f7b2007-06-17 19:56:38 -0500345 lpfc_free_vpi(phba, vpi);
346 destroy_port(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500347 goto error_out;
348 }
349
350 memcpy(vport->fc_portname.u.wwn, vport->fc_sparam.portName.u.wwn, 8);
351 memcpy(vport->fc_nodename.u.wwn, vport->fc_sparam.nodeName.u.wwn, 8);
James Smart92d7f7b2007-06-17 19:56:38 -0500352 if (fc_vport->node_name != 0)
353 u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
354 if (fc_vport->port_name != 0)
355 u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
356
357 memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
358 memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
359
360 if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
361 !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
James Smarte8b62012007-08-02 11:10:09 -0400362 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
363 "1821 Create VPORT failed. "
364 "Invalid WWN format\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500365 lpfc_free_vpi(phba, vpi);
366 destroy_port(vport);
367 rc = VPORT_INVAL;
368 goto error_out;
369 }
370
371 if (!lpfc_unique_wwpn(phba, vport)) {
James Smarte8b62012007-08-02 11:10:09 -0400372 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
373 "1823 Create VPORT failed. "
374 "Duplicate WWN on HBA\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500375 lpfc_free_vpi(phba, vpi);
376 destroy_port(vport);
377 rc = VPORT_INVAL;
378 goto error_out;
379 }
380
James Smarteada2722008-12-04 22:39:13 -0500381 /* Create binary sysfs attribute for vport */
382 lpfc_alloc_sysfs_attr(vport);
383
James Smart92d7f7b2007-06-17 19:56:38 -0500384 *(struct lpfc_vport **)fc_vport->dd_data = vport;
385 vport->fc_vport = fc_vport;
386
387 if ((phba->link_state < LPFC_LINK_UP) ||
388 (phba->fc_topology == TOPOLOGY_LOOP)) {
389 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
390 rc = VPORT_OK;
391 goto out;
392 }
393
394 if (disable) {
James Smarteada2722008-12-04 22:39:13 -0500395 lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
James Smart92d7f7b2007-06-17 19:56:38 -0500396 rc = VPORT_OK;
397 goto out;
398 }
399
400 /* Use the Physical nodes Fabric NDLP to determine if the link is
401 * up and ready to FDISC.
402 */
403 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
James Smarte47c9092008-02-08 18:49:26 -0500404 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
405 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
James Smart858c9f62007-06-17 19:56:39 -0500406 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
407 lpfc_set_disctmo(vport);
408 lpfc_initial_fdisc(vport);
409 } else {
410 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -0400411 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
412 "0262 No NPIV Fabric support\n");
James Smart858c9f62007-06-17 19:56:39 -0500413 }
James Smart92d7f7b2007-06-17 19:56:38 -0500414 } else {
415 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
416 }
417 rc = VPORT_OK;
418
419out:
James Smarta58cbd52007-08-02 11:09:43 -0400420 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
421 "1825 Vport Created.\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500422 lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
423error_out:
424 return rc;
425}
426
James Smart311464e2007-08-02 11:10:37 -0400427static int
James Smart92d7f7b2007-06-17 19:56:38 -0500428disable_vport(struct fc_vport *fc_vport)
429{
430 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
431 struct lpfc_hba *phba = vport->phba;
432 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
433 long timeout;
434
435 ndlp = lpfc_findnode_did(vport, Fabric_DID);
James Smarte47c9092008-02-08 18:49:26 -0500436 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
437 && phba->link_state >= LPFC_LINK_UP) {
James Smart92d7f7b2007-06-17 19:56:38 -0500438 vport->unreg_vpi_cmpl = VPORT_INVAL;
439 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
440 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
441 while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
442 timeout = schedule_timeout(timeout);
443 }
444
445 lpfc_sli_host_down(vport);
446
447 /* Mark all nodes for discovery so we can remove them by
448 * calling lpfc_cleanup_rpis(vport, 1)
449 */
450 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500451 if (!NLP_CHK_NODE_ACT(ndlp))
452 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500453 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
454 continue;
455 lpfc_disc_state_machine(vport, ndlp, NULL,
456 NLP_EVT_DEVICE_RECOVERY);
457 }
458 lpfc_cleanup_rpis(vport, 1);
459
460 lpfc_stop_vport_timers(vport);
461 lpfc_unreg_all_rpis(vport);
462 lpfc_unreg_default_rpis(vport);
463 /*
464 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
465 * scsi_host_put() to release the vport.
466 */
467 lpfc_mbx_unreg_vpi(vport);
468
469 lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
James Smarta58cbd52007-08-02 11:09:43 -0400470 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
471 "1826 Vport Disabled.\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500472 return VPORT_OK;
473}
474
James Smart311464e2007-08-02 11:10:37 -0400475static int
James Smart92d7f7b2007-06-17 19:56:38 -0500476enable_vport(struct fc_vport *fc_vport)
477{
478 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
479 struct lpfc_hba *phba = vport->phba;
480 struct lpfc_nodelist *ndlp = NULL;
481
482 if ((phba->link_state < LPFC_LINK_UP) ||
483 (phba->fc_topology == TOPOLOGY_LOOP)) {
484 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
485 return VPORT_OK;
486 }
487
488 vport->load_flag |= FC_LOADING;
489 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
490
491 /* Use the Physical nodes Fabric NDLP to determine if the link is
492 * up and ready to FDISC.
493 */
494 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
James Smarte47c9092008-02-08 18:49:26 -0500495 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
496 && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
James Smart858c9f62007-06-17 19:56:39 -0500497 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
498 lpfc_set_disctmo(vport);
499 lpfc_initial_fdisc(vport);
500 } else {
501 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smarte8b62012007-08-02 11:10:09 -0400502 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
503 "0264 No NPIV Fabric support\n");
James Smart858c9f62007-06-17 19:56:39 -0500504 }
James Smart92d7f7b2007-06-17 19:56:38 -0500505 } else {
506 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
507 }
James Smarta58cbd52007-08-02 11:09:43 -0400508 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
509 "1827 Vport Enabled.\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500510 return VPORT_OK;
511}
512
513int
514lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
515{
516 if (disable)
517 return disable_vport(fc_vport);
518 else
519 return enable_vport(fc_vport);
520}
521
522
523int
524lpfc_vport_delete(struct fc_vport *fc_vport)
525{
526 struct lpfc_nodelist *ndlp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -0500527 struct Scsi_Host *shost = (struct Scsi_Host *) fc_vport->shost;
528 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
529 struct lpfc_hba *phba = vport->phba;
530 long timeout;
James Smart92d7f7b2007-06-17 19:56:38 -0500531
James Smart51ef4c22007-08-02 11:10:31 -0400532 if (vport->port_type == LPFC_PHYSICAL_PORT) {
533 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
534 "1812 vport_delete failed: Cannot delete "
535 "physical host\n");
536 return VPORT_ERROR;
537 }
538 /*
539 * If we are not unloading the driver then prevent the vport_delete
540 * from happening until after this vport's discovery is finished.
541 */
542 if (!(phba->pport->load_flag & FC_UNLOADING)) {
543 int check_count = 0;
544 while (check_count < ((phba->fc_ratov * 3) + 3) &&
545 vport->port_state > LPFC_VPORT_FAILED &&
546 vport->port_state < LPFC_VPORT_READY) {
547 check_count++;
548 msleep(1000);
549 }
550 if (vport->port_state > LPFC_VPORT_FAILED &&
551 vport->port_state < LPFC_VPORT_READY)
552 return -EAGAIN;
553 }
James Smart92d7f7b2007-06-17 19:56:38 -0500554 /*
555 * This is a bit of a mess. We want to ensure the shost doesn't get
556 * torn down until we're done with the embedded lpfc_vport structure.
557 *
558 * Beyond holding a reference for this function, we also need a
559 * reference for outstanding I/O requests we schedule during delete
560 * processing. But once we scsi_remove_host() we can no longer obtain
561 * a reference through scsi_host_get().
562 *
563 * So we take two references here. We release one reference at the
564 * bottom of the function -- after delinking the vport. And we
565 * release the other at the completion of the unreg_vpi that get's
566 * initiated after we've disposed of all other resources associated
567 * with the port.
568 */
James Smartd7c255b2008-08-24 21:50:00 -0400569 if (!scsi_host_get(shost))
James Smart92d7f7b2007-06-17 19:56:38 -0500570 return VPORT_INVAL;
James Smartd7c255b2008-08-24 21:50:00 -0400571 if (!scsi_host_get(shost)) {
572 scsi_host_put(shost);
573 return VPORT_INVAL;
574 }
James Smart51ef4c22007-08-02 11:10:31 -0400575 spin_lock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500576 vport->load_flag |= FC_UNLOADING;
James Smart51ef4c22007-08-02 11:10:31 -0400577 spin_unlock_irq(&phba->hbalock);
James Smarteada2722008-12-04 22:39:13 -0500578
579 lpfc_free_sysfs_attr(vport);
580
James Smart858c9f62007-06-17 19:56:39 -0500581 lpfc_debugfs_terminate(vport);
James Smarteada2722008-12-04 22:39:13 -0500582
583 /* Remove FC host and then SCSI host with the vport */
James Smart92d7f7b2007-06-17 19:56:38 -0500584 fc_remove_host(lpfc_shost_from_vport(vport));
585 scsi_remove_host(lpfc_shost_from_vport(vport));
586
587 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
James Smarte47c9092008-02-08 18:49:26 -0500588
589 /* In case of driver unload, we shall not perform fabric logo as the
590 * worker thread already stopped at this stage and, in this case, we
591 * can safely skip the fabric logo.
592 */
593 if (phba->pport->load_flag & FC_UNLOADING) {
594 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
595 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
596 phba->link_state >= LPFC_LINK_UP) {
597 /* First look for the Fabric ndlp */
598 ndlp = lpfc_findnode_did(vport, Fabric_DID);
599 if (!ndlp)
600 goto skip_logo;
601 else if (!NLP_CHK_NODE_ACT(ndlp)) {
602 ndlp = lpfc_enable_node(vport, ndlp,
603 NLP_STE_UNUSED_NODE);
604 if (!ndlp)
605 goto skip_logo;
606 }
607 /* Remove ndlp from vport npld list */
608 lpfc_dequeue_node(vport, ndlp);
609
610 /* Indicate free memory when release */
611 spin_lock_irq(&phba->ndlp_lock);
612 NLP_SET_FREE_REQ(ndlp);
613 spin_unlock_irq(&phba->ndlp_lock);
614 /* Kick off release ndlp when it can be safely done */
615 lpfc_nlp_put(ndlp);
616 }
617 goto skip_logo;
618 }
619
620 /* Otherwise, we will perform fabric logo as needed */
621 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
622 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
James Smart58da1ff2008-04-07 10:15:56 -0400623 phba->link_state >= LPFC_LINK_UP &&
624 phba->fc_topology != TOPOLOGY_LOOP) {
James Smart7ee5d432007-10-27 13:37:17 -0400625 if (vport->cfg_enable_da_id) {
626 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
627 if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
628 while (vport->ct_flags && timeout)
629 timeout = schedule_timeout(timeout);
630 else
631 lpfc_printf_log(vport->phba, KERN_WARNING,
632 LOG_VPORT,
633 "1829 CT command failed to "
634 "delete objects on fabric. \n");
635 }
James Smart92d7f7b2007-06-17 19:56:38 -0500636 /* First look for the Fabric ndlp */
637 ndlp = lpfc_findnode_did(vport, Fabric_DID);
638 if (!ndlp) {
639 /* Cannot find existing Fabric ndlp, allocate one */
640 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
641 if (!ndlp)
642 goto skip_logo;
643 lpfc_nlp_init(vport, ndlp, Fabric_DID);
James Smarte47c9092008-02-08 18:49:26 -0500644 /* Indicate free memory when release */
645 NLP_SET_FREE_REQ(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500646 } else {
James Smarte47c9092008-02-08 18:49:26 -0500647 if (!NLP_CHK_NODE_ACT(ndlp))
648 ndlp = lpfc_enable_node(vport, ndlp,
649 NLP_STE_UNUSED_NODE);
650 if (!ndlp)
651 goto skip_logo;
652
653 /* Remove ndlp from vport npld list */
James Smart92d7f7b2007-06-17 19:56:38 -0500654 lpfc_dequeue_node(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -0500655 spin_lock_irq(&phba->ndlp_lock);
656 if (!NLP_CHK_FREE_REQ(ndlp))
657 /* Indicate free memory when release */
658 NLP_SET_FREE_REQ(ndlp);
659 else {
660 /* Skip this if ndlp is already in free mode */
661 spin_unlock_irq(&phba->ndlp_lock);
662 goto skip_logo;
663 }
664 spin_unlock_irq(&phba->ndlp_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500665 }
666 vport->unreg_vpi_cmpl = VPORT_INVAL;
667 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
James Smartd7c255b2008-08-24 21:50:00 -0400668 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
669 goto skip_logo;
James Smart92d7f7b2007-06-17 19:56:38 -0500670 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
671 while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
672 timeout = schedule_timeout(timeout);
673 }
674
James Smart90160e02008-08-24 21:49:45 -0400675 if (!(phba->pport->load_flag & FC_UNLOADING))
676 lpfc_discovery_wait(vport);
677
James Smart92d7f7b2007-06-17 19:56:38 -0500678skip_logo:
James Smart87af33f2007-10-27 13:37:43 -0400679 lpfc_cleanup(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500680 lpfc_sli_host_down(vport);
681
James Smart92d7f7b2007-06-17 19:56:38 -0500682 lpfc_stop_vport_timers(vport);
James Smart87af33f2007-10-27 13:37:43 -0400683
684 if (!(phba->pport->load_flag & FC_UNLOADING)) {
James Smarte47c9092008-02-08 18:49:26 -0500685 lpfc_unreg_all_rpis(vport);
James Smart87af33f2007-10-27 13:37:43 -0400686 lpfc_unreg_default_rpis(vport);
687 /*
688 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
689 * does the scsi_host_put() to release the vport.
690 */
James Smartd7c255b2008-08-24 21:50:00 -0400691 if (lpfc_mbx_unreg_vpi(vport))
692 scsi_host_put(shost);
693 } else
694 scsi_host_put(shost);
James Smart92d7f7b2007-06-17 19:56:38 -0500695
696 lpfc_free_vpi(phba, vport->vpi);
697 vport->work_port_events = 0;
698 spin_lock_irq(&phba->hbalock);
699 list_del_init(&vport->listentry);
700 spin_unlock_irq(&phba->hbalock);
James Smarta58cbd52007-08-02 11:09:43 -0400701 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
702 "1828 Vport Deleted.\n");
James Smart92d7f7b2007-06-17 19:56:38 -0500703 scsi_host_put(shost);
James Smart51ef4c22007-08-02 11:10:31 -0400704 return VPORT_OK;
James Smart92d7f7b2007-06-17 19:56:38 -0500705}
706
James Smart549e55c2007-08-02 11:09:51 -0400707struct lpfc_vport **
708lpfc_create_vport_work_array(struct lpfc_hba *phba)
709{
710 struct lpfc_vport *port_iterator;
711 struct lpfc_vport **vports;
712 int index = 0;
James Smart09372822008-01-11 01:52:54 -0500713 vports = kzalloc((phba->max_vpi + 1) * sizeof(struct lpfc_vport *),
James Smart549e55c2007-08-02 11:09:51 -0400714 GFP_KERNEL);
715 if (vports == NULL)
716 return NULL;
717 spin_lock_irq(&phba->hbalock);
718 list_for_each_entry(port_iterator, &phba->port_list, listentry) {
James Smarte8b62012007-08-02 11:10:09 -0400719 if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
James Smart51ef4c22007-08-02 11:10:31 -0400720 lpfc_printf_vlog(port_iterator, KERN_WARNING, LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -0400721 "1801 Create vport work array FAILED: "
722 "cannot do scsi_host_get\n");
James Smart549e55c2007-08-02 11:09:51 -0400723 continue;
James Smarte8b62012007-08-02 11:10:09 -0400724 }
James Smart549e55c2007-08-02 11:09:51 -0400725 vports[index++] = port_iterator;
726 }
727 spin_unlock_irq(&phba->hbalock);
728 return vports;
729}
730
731void
James Smart09372822008-01-11 01:52:54 -0500732lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
James Smart549e55c2007-08-02 11:09:51 -0400733{
734 int i;
735 if (vports == NULL)
736 return;
James Smart09372822008-01-11 01:52:54 -0500737 for (i=0; vports[i] != NULL && i <= phba->max_vpi; i++)
James Smart549e55c2007-08-02 11:09:51 -0400738 scsi_host_put(lpfc_shost_from_vport(vports[i]));
739 kfree(vports);
740}
James Smartea2151b2008-09-07 11:52:10 -0400741
742
743/**
James Smart3621a712009-04-06 18:47:14 -0400744 * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
James Smartea2151b2008-09-07 11:52:10 -0400745 * @vport: Pointer to vport object.
746 *
747 * This function resets the statistical data for the vport. This function
748 * is called with the host_lock held
749 **/
750void
751lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
752{
753 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
754
755 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
756 if (!NLP_CHK_NODE_ACT(ndlp))
757 continue;
758 if (ndlp->lat_data)
759 memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
760 sizeof(struct lpfc_scsicmd_bkt));
761 }
762}
763
764
765/**
James Smart3621a712009-04-06 18:47:14 -0400766 * lpfc_alloc_bucket - Allocate data buffer required for statistical data
James Smartea2151b2008-09-07 11:52:10 -0400767 * @vport: Pointer to vport object.
768 *
769 * This function allocates data buffer required for all the FC
770 * nodes of the vport to collect statistical data.
771 **/
772void
773lpfc_alloc_bucket(struct lpfc_vport *vport)
774{
775 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
776
777 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
778 if (!NLP_CHK_NODE_ACT(ndlp))
779 continue;
780
781 kfree(ndlp->lat_data);
782 ndlp->lat_data = NULL;
783
784 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
785 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
786 sizeof(struct lpfc_scsicmd_bkt),
787 GFP_ATOMIC);
788
789 if (!ndlp->lat_data)
790 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
791 "0287 lpfc_alloc_bucket failed to "
792 "allocate statistical data buffer DID "
793 "0x%x\n", ndlp->nlp_DID);
794 }
795 }
796}
797
798/**
James Smart3621a712009-04-06 18:47:14 -0400799 * lpfc_free_bucket - Free data buffer required for statistical data
James Smartea2151b2008-09-07 11:52:10 -0400800 * @vport: Pointer to vport object.
801 *
802 * Th function frees statistical data buffer of all the FC
803 * nodes of the vport.
804 **/
805void
806lpfc_free_bucket(struct lpfc_vport *vport)
807{
808 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
809
810 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
811 if (!NLP_CHK_NODE_ACT(ndlp))
812 continue;
813
814 kfree(ndlp->lat_data);
815 ndlp->lat_data = NULL;
816 }
817}