GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / lpfc / lpfc_vport.c
blob1655507a682c8aef382a513fea1918ae78c7a14b
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
5 * 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 *******************************************************************/
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/slab.h>
30 #include <linux/spinlock.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport_fc.h>
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_logmsg.h"
45 #include "lpfc_crtn.h"
46 #include "lpfc_version.h"
47 #include "lpfc_vport.h"
49 inline void lpfc_vport_set_state(struct lpfc_vport *vport,
50 enum fc_vport_state new_state)
52 struct fc_vport *fc_vport = vport->fc_vport;
54 if (fc_vport) {
56 * When the transport defines fc_vport_set state we will replace
57 * this code with the following line
59 /* fc_vport_set_state(fc_vport, new_state); */
60 if (new_state != FC_VPORT_INITIALIZING)
61 fc_vport->vport_last_state = fc_vport->vport_state;
62 fc_vport->vport_state = new_state;
65 /* for all the error states we will set the invternal state to FAILED */
66 switch (new_state) {
67 case FC_VPORT_NO_FABRIC_SUPP:
68 case FC_VPORT_NO_FABRIC_RSCS:
69 case FC_VPORT_FABRIC_LOGOUT:
70 case FC_VPORT_FABRIC_REJ_WWN:
71 case FC_VPORT_FAILED:
72 vport->port_state = LPFC_VPORT_FAILED;
73 break;
74 case FC_VPORT_LINKDOWN:
75 vport->port_state = LPFC_VPORT_UNKNOWN;
76 break;
77 default:
78 /* do nothing */
79 break;
83 static int
84 lpfc_alloc_vpi(struct lpfc_hba *phba)
86 int vpi;
88 spin_lock_irq(&phba->hbalock);
89 /* Start at bit 1 because vpi zero is reserved for the physical port */
90 vpi = find_next_zero_bit(phba->vpi_bmask, (phba->max_vpi + 1), 1);
91 if (vpi > phba->max_vpi)
92 vpi = 0;
93 else
94 set_bit(vpi, phba->vpi_bmask);
95 if (phba->sli_rev == LPFC_SLI_REV4)
96 phba->sli4_hba.max_cfg_param.vpi_used++;
97 spin_unlock_irq(&phba->hbalock);
98 return vpi;
101 static void
102 lpfc_free_vpi(struct lpfc_hba *phba, int vpi)
104 if (vpi == 0)
105 return;
106 spin_lock_irq(&phba->hbalock);
107 clear_bit(vpi, phba->vpi_bmask);
108 if (phba->sli_rev == LPFC_SLI_REV4)
109 phba->sli4_hba.max_cfg_param.vpi_used--;
110 spin_unlock_irq(&phba->hbalock);
113 static int
114 lpfc_vport_sparm(struct lpfc_hba *phba, struct lpfc_vport *vport)
116 LPFC_MBOXQ_t *pmb;
117 MAILBOX_t *mb;
118 struct lpfc_dmabuf *mp;
119 int rc;
121 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
122 if (!pmb) {
123 return -ENOMEM;
125 mb = &pmb->u.mb;
127 rc = lpfc_read_sparam(phba, pmb, vport->vpi);
128 if (rc) {
129 mempool_free(pmb, phba->mbox_mem_pool);
130 return -ENOMEM;
134 * Grab buffer pointer and clear context1 so we can use
135 * lpfc_sli_issue_box_wait
137 mp = (struct lpfc_dmabuf *) pmb->context1;
138 pmb->context1 = NULL;
140 pmb->vport = vport;
141 rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
142 if (rc != MBX_SUCCESS) {
143 if (signal_pending(current)) {
144 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
145 "1830 Signal aborted mbxCmd x%x\n",
146 mb->mbxCommand);
147 lpfc_mbuf_free(phba, mp->virt, mp->phys);
148 kfree(mp);
149 if (rc != MBX_TIMEOUT)
150 mempool_free(pmb, phba->mbox_mem_pool);
151 return -EINTR;
152 } else {
153 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
154 "1818 VPort failed init, mbxCmd x%x "
155 "READ_SPARM mbxStatus x%x, rc = x%x\n",
156 mb->mbxCommand, mb->mbxStatus, rc);
157 lpfc_mbuf_free(phba, mp->virt, mp->phys);
158 kfree(mp);
159 if (rc != MBX_TIMEOUT)
160 mempool_free(pmb, phba->mbox_mem_pool);
161 return -EIO;
165 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
166 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
167 sizeof (struct lpfc_name));
168 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
169 sizeof (struct lpfc_name));
171 lpfc_mbuf_free(phba, mp->virt, mp->phys);
172 kfree(mp);
173 mempool_free(pmb, phba->mbox_mem_pool);
175 return 0;
178 static int
179 lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn,
180 const char *name_type)
182 /* ensure that IEEE format 1 addresses
183 * contain zeros in bits 59-48
185 if (!((wwn->u.wwn[0] >> 4) == 1 &&
186 ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0)))
187 return 1;
189 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
190 "1822 Invalid %s: %02x:%02x:%02x:%02x:"
191 "%02x:%02x:%02x:%02x\n",
192 name_type,
193 wwn->u.wwn[0], wwn->u.wwn[1],
194 wwn->u.wwn[2], wwn->u.wwn[3],
195 wwn->u.wwn[4], wwn->u.wwn[5],
196 wwn->u.wwn[6], wwn->u.wwn[7]);
197 return 0;
200 static int
201 lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
203 struct lpfc_vport *vport;
204 unsigned long flags;
206 spin_lock_irqsave(&phba->hbalock, flags);
207 list_for_each_entry(vport, &phba->port_list, listentry) {
208 if (vport == new_vport)
209 continue;
210 /* If they match, return not unique */
211 if (memcmp(&vport->fc_sparam.portName,
212 &new_vport->fc_sparam.portName,
213 sizeof(struct lpfc_name)) == 0) {
214 spin_unlock_irqrestore(&phba->hbalock, flags);
215 return 0;
218 spin_unlock_irqrestore(&phba->hbalock, flags);
219 return 1;
223 * lpfc_discovery_wait - Wait for driver discovery to quiesce
224 * @vport: The virtual port for which this call is being executed.
226 * This driver calls this routine specifically from lpfc_vport_delete
227 * to enforce a synchronous execution of vport
228 * delete relative to discovery activities. The
229 * lpfc_vport_delete routine should not return until it
230 * can reasonably guarantee that discovery has quiesced.
231 * Post FDISC LOGO, the driver must wait until its SAN teardown is
232 * complete and all resources recovered before allowing
233 * cleanup.
235 * This routine does not require any locks held.
237 static void lpfc_discovery_wait(struct lpfc_vport *vport)
239 struct lpfc_hba *phba = vport->phba;
240 uint32_t wait_flags = 0;
241 unsigned long wait_time_max;
242 unsigned long start_time;
244 wait_flags = FC_RSCN_MODE | FC_RSCN_DISCOVERY | FC_NLP_MORE |
245 FC_RSCN_DEFERRED | FC_NDISC_ACTIVE | FC_DISC_TMO;
248 * The time constraint on this loop is a balance between the
249 * fabric RA_TOV value and dev_loss tmo. The driver's
250 * devloss_tmo is 10 giving this loop a 3x multiplier minimally.
252 wait_time_max = msecs_to_jiffies(((phba->fc_ratov * 3) + 3) * 1000);
253 wait_time_max += jiffies;
254 start_time = jiffies;
255 while (time_before(jiffies, wait_time_max)) {
256 if ((vport->num_disc_nodes > 0) ||
257 (vport->fc_flag & wait_flags) ||
258 ((vport->port_state > LPFC_VPORT_FAILED) &&
259 (vport->port_state < LPFC_VPORT_READY))) {
260 lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
261 "1833 Vport discovery quiesce Wait:"
262 " state x%x fc_flags x%x"
263 " num_nodes x%x, waiting 1000 msecs"
264 " total wait msecs x%x\n",
265 vport->port_state, vport->fc_flag,
266 vport->num_disc_nodes,
267 jiffies_to_msecs(jiffies - start_time));
268 msleep(1000);
269 } else {
270 /* Base case. Wait variants satisfied. Break out */
271 lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
272 "1834 Vport discovery quiesced:"
273 " state x%x fc_flags x%x"
274 " wait msecs x%x\n",
275 vport->port_state, vport->fc_flag,
276 jiffies_to_msecs(jiffies
277 - start_time));
278 break;
282 if (time_after(jiffies, wait_time_max))
283 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
284 "1835 Vport discovery quiesce failed:"
285 " state x%x fc_flags x%x wait msecs x%x\n",
286 vport->port_state, vport->fc_flag,
287 jiffies_to_msecs(jiffies - start_time));
291 lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
293 struct lpfc_nodelist *ndlp;
294 struct Scsi_Host *shost = fc_vport->shost;
295 struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata;
296 struct lpfc_hba *phba = pport->phba;
297 struct lpfc_vport *vport = NULL;
298 int instance;
299 int vpi;
300 int rc = VPORT_ERROR;
301 int status;
303 if ((phba->sli_rev < 3) || !(phba->cfg_enable_npiv)) {
304 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
305 "1808 Create VPORT failed: "
306 "NPIV is not enabled: SLImode:%d\n",
307 phba->sli_rev);
308 rc = VPORT_INVAL;
309 goto error_out;
312 vpi = lpfc_alloc_vpi(phba);
313 if (vpi == 0) {
314 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
315 "1809 Create VPORT failed: "
316 "Max VPORTs (%d) exceeded\n",
317 phba->max_vpi);
318 rc = VPORT_NORESOURCES;
319 goto error_out;
322 /* Assign an unused board number */
323 if ((instance = lpfc_get_instance()) < 0) {
324 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
325 "1810 Create VPORT failed: Cannot get "
326 "instance number\n");
327 lpfc_free_vpi(phba, vpi);
328 rc = VPORT_NORESOURCES;
329 goto error_out;
332 vport = lpfc_create_port(phba, instance, &fc_vport->dev);
333 if (!vport) {
334 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
335 "1811 Create VPORT failed: vpi x%x\n", vpi);
336 lpfc_free_vpi(phba, vpi);
337 rc = VPORT_NORESOURCES;
338 goto error_out;
341 vport->vpi = vpi;
342 lpfc_debugfs_initialize(vport);
344 if ((status = lpfc_vport_sparm(phba, vport))) {
345 if (status == -EINTR) {
346 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
347 "1831 Create VPORT Interrupted.\n");
348 rc = VPORT_ERROR;
349 } else {
350 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
351 "1813 Create VPORT failed. "
352 "Cannot get sparam\n");
353 rc = VPORT_NORESOURCES;
355 lpfc_free_vpi(phba, vpi);
356 destroy_port(vport);
357 goto error_out;
360 u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
361 u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
363 memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
364 memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
366 if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
367 !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
368 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
369 "1821 Create VPORT failed. "
370 "Invalid WWN format\n");
371 lpfc_free_vpi(phba, vpi);
372 destroy_port(vport);
373 rc = VPORT_INVAL;
374 goto error_out;
377 if (!lpfc_unique_wwpn(phba, vport)) {
378 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
379 "1823 Create VPORT failed. "
380 "Duplicate WWN on HBA\n");
381 lpfc_free_vpi(phba, vpi);
382 destroy_port(vport);
383 rc = VPORT_INVAL;
384 goto error_out;
387 /* Create binary sysfs attribute for vport */
388 lpfc_alloc_sysfs_attr(vport);
390 *(struct lpfc_vport **)fc_vport->dd_data = vport;
391 vport->fc_vport = fc_vport;
394 * In SLI4, the vpi must be activated before it can be used
395 * by the port.
397 if ((phba->sli_rev == LPFC_SLI_REV4) &&
398 (pport->fc_flag & FC_VFI_REGISTERED)) {
399 rc = lpfc_sli4_init_vpi(phba, vpi);
400 if (rc) {
401 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
402 "1838 Failed to INIT_VPI on vpi %d "
403 "status %d\n", vpi, rc);
404 rc = VPORT_NORESOURCES;
405 lpfc_free_vpi(phba, vpi);
406 goto error_out;
408 } else if (phba->sli_rev == LPFC_SLI_REV4) {
410 * Driver cannot INIT_VPI now. Set the flags to
411 * init_vpi when reg_vfi complete.
413 vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
414 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
415 rc = VPORT_OK;
416 goto out;
419 if ((phba->link_state < LPFC_LINK_UP) ||
420 (pport->port_state < LPFC_FABRIC_CFG_LINK) ||
421 (phba->fc_topology == TOPOLOGY_LOOP)) {
422 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
423 rc = VPORT_OK;
424 goto out;
427 if (disable) {
428 lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
429 rc = VPORT_OK;
430 goto out;
433 /* Use the Physical nodes Fabric NDLP to determine if the link is
434 * up and ready to FDISC.
436 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
437 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
438 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
439 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
440 lpfc_set_disctmo(vport);
441 lpfc_initial_fdisc(vport);
442 } else {
443 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
444 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
445 "0262 No NPIV Fabric support\n");
447 } else {
448 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
450 rc = VPORT_OK;
452 out:
453 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
454 "1825 Vport Created.\n");
455 lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
456 error_out:
457 return rc;
460 static int
461 disable_vport(struct fc_vport *fc_vport)
463 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
464 struct lpfc_hba *phba = vport->phba;
465 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
466 long timeout;
468 ndlp = lpfc_findnode_did(vport, Fabric_DID);
469 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
470 && phba->link_state >= LPFC_LINK_UP) {
471 vport->unreg_vpi_cmpl = VPORT_INVAL;
472 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
473 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
474 while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
475 timeout = schedule_timeout(timeout);
478 lpfc_sli_host_down(vport);
480 /* Mark all nodes for discovery so we can remove them by
481 * calling lpfc_cleanup_rpis(vport, 1)
483 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
484 if (!NLP_CHK_NODE_ACT(ndlp))
485 continue;
486 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
487 continue;
488 lpfc_disc_state_machine(vport, ndlp, NULL,
489 NLP_EVT_DEVICE_RECOVERY);
491 lpfc_cleanup_rpis(vport, 1);
493 lpfc_stop_vport_timers(vport);
494 lpfc_unreg_all_rpis(vport);
495 lpfc_unreg_default_rpis(vport);
497 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
498 * scsi_host_put() to release the vport.
500 lpfc_mbx_unreg_vpi(vport);
502 lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
503 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
504 "1826 Vport Disabled.\n");
505 return VPORT_OK;
508 static int
509 enable_vport(struct fc_vport *fc_vport)
511 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
512 struct lpfc_hba *phba = vport->phba;
513 struct lpfc_nodelist *ndlp = NULL;
514 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
516 if ((phba->link_state < LPFC_LINK_UP) ||
517 (phba->fc_topology == TOPOLOGY_LOOP)) {
518 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
519 return VPORT_OK;
522 spin_lock_irq(shost->host_lock);
523 vport->load_flag |= FC_LOADING;
524 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
525 spin_unlock_irq(shost->host_lock);
527 /* Use the Physical nodes Fabric NDLP to determine if the link is
528 * up and ready to FDISC.
530 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
531 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
532 && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
533 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
534 lpfc_set_disctmo(vport);
535 lpfc_initial_fdisc(vport);
536 } else {
537 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
538 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
539 "0264 No NPIV Fabric support\n");
541 } else {
542 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
544 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
545 "1827 Vport Enabled.\n");
546 return VPORT_OK;
550 lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
552 if (disable)
553 return disable_vport(fc_vport);
554 else
555 return enable_vport(fc_vport);
560 lpfc_vport_delete(struct fc_vport *fc_vport)
562 struct lpfc_nodelist *ndlp = NULL;
563 struct Scsi_Host *shost = (struct Scsi_Host *) fc_vport->shost;
564 struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
565 struct lpfc_hba *phba = vport->phba;
566 long timeout;
568 if (vport->port_type == LPFC_PHYSICAL_PORT) {
569 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
570 "1812 vport_delete failed: Cannot delete "
571 "physical host\n");
572 return VPORT_ERROR;
575 /* If the vport is a static vport fail the deletion. */
576 if ((vport->vport_flag & STATIC_VPORT) &&
577 !(phba->pport->load_flag & FC_UNLOADING)) {
578 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
579 "1837 vport_delete failed: Cannot delete "
580 "static vport.\n");
581 return VPORT_ERROR;
585 * If we are not unloading the driver then prevent the vport_delete
586 * from happening until after this vport's discovery is finished.
588 if (!(phba->pport->load_flag & FC_UNLOADING)) {
589 int check_count = 0;
590 while (check_count < ((phba->fc_ratov * 3) + 3) &&
591 vport->port_state > LPFC_VPORT_FAILED &&
592 vport->port_state < LPFC_VPORT_READY) {
593 check_count++;
594 msleep(1000);
596 if (vport->port_state > LPFC_VPORT_FAILED &&
597 vport->port_state < LPFC_VPORT_READY)
598 return -EAGAIN;
601 * This is a bit of a mess. We want to ensure the shost doesn't get
602 * torn down until we're done with the embedded lpfc_vport structure.
604 * Beyond holding a reference for this function, we also need a
605 * reference for outstanding I/O requests we schedule during delete
606 * processing. But once we scsi_remove_host() we can no longer obtain
607 * a reference through scsi_host_get().
609 * So we take two references here. We release one reference at the
610 * bottom of the function -- after delinking the vport. And we
611 * release the other at the completion of the unreg_vpi that get's
612 * initiated after we've disposed of all other resources associated
613 * with the port.
615 if (!scsi_host_get(shost))
616 return VPORT_INVAL;
617 if (!scsi_host_get(shost)) {
618 scsi_host_put(shost);
619 return VPORT_INVAL;
621 spin_lock_irq(&phba->hbalock);
622 vport->load_flag |= FC_UNLOADING;
623 spin_unlock_irq(&phba->hbalock);
625 lpfc_free_sysfs_attr(vport);
627 lpfc_debugfs_terminate(vport);
629 /* Remove FC host and then SCSI host with the vport */
630 fc_remove_host(lpfc_shost_from_vport(vport));
631 scsi_remove_host(lpfc_shost_from_vport(vport));
633 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
635 /* In case of driver unload, we shall not perform fabric logo as the
636 * worker thread already stopped at this stage and, in this case, we
637 * can safely skip the fabric logo.
639 if (phba->pport->load_flag & FC_UNLOADING) {
640 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
641 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
642 phba->link_state >= LPFC_LINK_UP) {
643 /* First look for the Fabric ndlp */
644 ndlp = lpfc_findnode_did(vport, Fabric_DID);
645 if (!ndlp)
646 goto skip_logo;
647 else 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;
653 /* Remove ndlp from vport npld list */
654 lpfc_dequeue_node(vport, ndlp);
656 /* Indicate free memory when release */
657 spin_lock_irq(&phba->ndlp_lock);
658 NLP_SET_FREE_REQ(ndlp);
659 spin_unlock_irq(&phba->ndlp_lock);
660 /* Kick off release ndlp when it can be safely done */
661 lpfc_nlp_put(ndlp);
663 goto skip_logo;
666 /* Otherwise, we will perform fabric logo as needed */
667 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
668 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
669 phba->link_state >= LPFC_LINK_UP &&
670 phba->fc_topology != TOPOLOGY_LOOP) {
671 if (vport->cfg_enable_da_id) {
672 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
673 if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
674 while (vport->ct_flags && timeout)
675 timeout = schedule_timeout(timeout);
676 else
677 lpfc_printf_log(vport->phba, KERN_WARNING,
678 LOG_VPORT,
679 "1829 CT command failed to "
680 "delete objects on fabric\n");
682 /* First look for the Fabric ndlp */
683 ndlp = lpfc_findnode_did(vport, Fabric_DID);
684 if (!ndlp) {
685 /* Cannot find existing Fabric ndlp, allocate one */
686 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
687 if (!ndlp)
688 goto skip_logo;
689 lpfc_nlp_init(vport, ndlp, Fabric_DID);
690 /* Indicate free memory when release */
691 NLP_SET_FREE_REQ(ndlp);
692 } else {
693 if (!NLP_CHK_NODE_ACT(ndlp))
694 ndlp = lpfc_enable_node(vport, ndlp,
695 NLP_STE_UNUSED_NODE);
696 if (!ndlp)
697 goto skip_logo;
699 /* Remove ndlp from vport npld list */
700 lpfc_dequeue_node(vport, ndlp);
701 spin_lock_irq(&phba->ndlp_lock);
702 if (!NLP_CHK_FREE_REQ(ndlp))
703 /* Indicate free memory when release */
704 NLP_SET_FREE_REQ(ndlp);
705 else {
706 /* Skip this if ndlp is already in free mode */
707 spin_unlock_irq(&phba->ndlp_lock);
708 goto skip_logo;
710 spin_unlock_irq(&phba->ndlp_lock);
712 if (!(vport->vpi_state & LPFC_VPI_REGISTERED))
713 goto skip_logo;
714 vport->unreg_vpi_cmpl = VPORT_INVAL;
715 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
716 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
717 while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
718 timeout = schedule_timeout(timeout);
721 if (!(phba->pport->load_flag & FC_UNLOADING))
722 lpfc_discovery_wait(vport);
724 skip_logo:
725 lpfc_cleanup(vport);
726 lpfc_sli_host_down(vport);
728 lpfc_stop_vport_timers(vport);
730 if (!(phba->pport->load_flag & FC_UNLOADING)) {
731 lpfc_unreg_all_rpis(vport);
732 lpfc_unreg_default_rpis(vport);
734 * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
735 * does the scsi_host_put() to release the vport.
737 if (lpfc_mbx_unreg_vpi(vport))
738 scsi_host_put(shost);
739 } else
740 scsi_host_put(shost);
742 lpfc_free_vpi(phba, vport->vpi);
743 vport->work_port_events = 0;
744 spin_lock_irq(&phba->hbalock);
745 list_del_init(&vport->listentry);
746 spin_unlock_irq(&phba->hbalock);
747 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
748 "1828 Vport Deleted.\n");
749 scsi_host_put(shost);
750 return VPORT_OK;
753 struct lpfc_vport **
754 lpfc_create_vport_work_array(struct lpfc_hba *phba)
756 struct lpfc_vport *port_iterator;
757 struct lpfc_vport **vports;
758 int index = 0;
759 vports = kzalloc((phba->max_vports + 1) * sizeof(struct lpfc_vport *),
760 GFP_KERNEL);
761 if (vports == NULL)
762 return NULL;
763 spin_lock_irq(&phba->hbalock);
764 list_for_each_entry(port_iterator, &phba->port_list, listentry) {
765 if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
766 if (!(port_iterator->load_flag & FC_UNLOADING))
767 lpfc_printf_vlog(port_iterator, KERN_ERR,
768 LOG_VPORT,
769 "1801 Create vport work array FAILED: "
770 "cannot do scsi_host_get\n");
771 continue;
773 vports[index++] = port_iterator;
775 spin_unlock_irq(&phba->hbalock);
776 return vports;
779 void
780 lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
782 int i;
783 if (vports == NULL)
784 return;
785 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
786 scsi_host_put(lpfc_shost_from_vport(vports[i]));
787 kfree(vports);
792 * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
793 * @vport: Pointer to vport object.
795 * This function resets the statistical data for the vport. This function
796 * is called with the host_lock held
798 void
799 lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
801 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
803 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
804 if (!NLP_CHK_NODE_ACT(ndlp))
805 continue;
806 if (ndlp->lat_data)
807 memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
808 sizeof(struct lpfc_scsicmd_bkt));
814 * lpfc_alloc_bucket - Allocate data buffer required for statistical data
815 * @vport: Pointer to vport object.
817 * This function allocates data buffer required for all the FC
818 * nodes of the vport to collect statistical data.
820 void
821 lpfc_alloc_bucket(struct lpfc_vport *vport)
823 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
825 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
826 if (!NLP_CHK_NODE_ACT(ndlp))
827 continue;
829 kfree(ndlp->lat_data);
830 ndlp->lat_data = NULL;
832 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
833 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
834 sizeof(struct lpfc_scsicmd_bkt),
835 GFP_ATOMIC);
837 if (!ndlp->lat_data)
838 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
839 "0287 lpfc_alloc_bucket failed to "
840 "allocate statistical data buffer DID "
841 "0x%x\n", ndlp->nlp_DID);
847 * lpfc_free_bucket - Free data buffer required for statistical data
848 * @vport: Pointer to vport object.
850 * Th function frees statistical data buffer of all the FC
851 * nodes of the vport.
853 void
854 lpfc_free_bucket(struct lpfc_vport *vport)
856 struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
858 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
859 if (!NLP_CHK_NODE_ACT(ndlp))
860 continue;
862 kfree(ndlp->lat_data);
863 ndlp->lat_data = NULL;