Merge branch 'x86/urgent' into x86/setup
[linux-2.6] / drivers / scsi / lpfc / lpfc_vport.c
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  *******************************************************************/
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"
37 #include "lpfc_nl.h"
38 #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
46 inline 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
80 static int
81 lpfc_alloc_vpi(struct lpfc_hba *phba)
82 {
83         int  vpi;
84
85         spin_lock_irq(&phba->hbalock);
86         /* 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);
88         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
96 static void
97 lpfc_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
104 static int
105 lpfc_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) {
129                 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                 }
149         }
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
164 static int
165 lpfc_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,
176                         "1822 Invalid %s: %02x:%02x:%02x:%02x:"
177                         "%02x:%02x:%02x:%02x\n",
178                         name_type,
179                         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
186 static int
187 lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
188 {
189         struct lpfc_vport *vport;
190         unsigned long flags;
191
192         spin_lock_irqsave(&phba->hbalock, flags);
193         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,
198                            &new_vport->fc_sparam.portName,
199                            sizeof(struct lpfc_name)) == 0) {
200                         spin_unlock_irqrestore(&phba->hbalock, flags);
201                         return 0;
202                 }
203         }
204         spin_unlock_irqrestore(&phba->hbalock, flags);
205         return 1;
206 }
207
208 /**
209  * lpfc_discovery_wait - Wait for driver discovery to quiesce
210  * @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  **/
223 static 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
279 int
280 lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
281 {
282         struct lpfc_nodelist *ndlp;
283         struct Scsi_Host *shost = fc_vport->shost;
284         struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata;
285         struct lpfc_hba   *phba = pport->phba;
286         struct lpfc_vport *vport = NULL;
287         int instance;
288         int vpi;
289         int rc = VPORT_ERROR;
290         int status;
291
292         if ((phba->sli_rev < 3) || !(phba->cfg_enable_npiv)) {
293                 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
294                                 "1808 Create VPORT failed: "
295                                 "NPIV is not enabled: SLImode:%d\n",
296                                 phba->sli_rev);
297                 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,
304                                 "1809 Create VPORT failed: "
305                                 "Max VPORTs (%d) exceeded\n",
306                                 phba->max_vpi);
307                 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,
315                                 "1810 Create VPORT failed: Cannot get "
316                                 "instance number\n");
317                 lpfc_free_vpi(phba, vpi);
318                 rc = VPORT_NORESOURCES;
319                 goto error_out;
320         }
321
322         vport = lpfc_create_port(phba, instance, &fc_vport->dev);
323         if (!vport) {
324                 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
325                                 "1811 Create VPORT failed: vpi x%x\n", vpi);
326                 lpfc_free_vpi(phba, vpi);
327                 rc = VPORT_NORESOURCES;
328                 goto error_out;
329         }
330
331         vport->vpi = vpi;
332         lpfc_debugfs_initialize(vport);
333
334         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                 }
345                 lpfc_free_vpi(phba, vpi);
346                 destroy_port(vport);
347                 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);
352         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")) {
362                 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
363                                  "1821 Create VPORT failed. "
364                                  "Invalid WWN format\n");
365                 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)) {
372                 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
373                                  "1823 Create VPORT failed. "
374                                  "Duplicate WWN on HBA\n");
375                 lpfc_free_vpi(phba, vpi);
376                 destroy_port(vport);
377                 rc = VPORT_INVAL;
378                 goto error_out;
379         }
380
381         /* Create binary sysfs attribute for vport */
382         lpfc_alloc_sysfs_attr(vport);
383
384         *(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) {
395                 lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
396                 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);
404         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
405             ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
406                 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);
411                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
412                                          "0262 No NPIV Fabric support\n");
413                 }
414         } else {
415                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
416         }
417         rc = VPORT_OK;
418
419 out:
420         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
421                         "1825 Vport Created.\n");
422         lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
423 error_out:
424         return rc;
425 }
426
427 static int
428 disable_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);
436         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
437             && phba->link_state >= LPFC_LINK_UP) {
438                 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) {
451                 if (!NLP_CHK_NODE_ACT(ndlp))
452                         continue;
453                 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);
470         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
471                          "1826 Vport Disabled.\n");
472         return VPORT_OK;
473 }
474
475 static int
476 enable_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);
495         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
496             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
497                 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);
502                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
503                                          "0264 No NPIV Fabric support\n");
504                 }
505         } else {
506                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
507         }
508         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
509                          "1827 Vport Enabled.\n");
510         return VPORT_OK;
511 }
512
513 int
514 lpfc_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
523 int
524 lpfc_vport_delete(struct fc_vport *fc_vport)
525 {
526         struct lpfc_nodelist *ndlp = NULL;
527         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;
531
532         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         }
554         /*
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          */
569         if (!scsi_host_get(shost))
570                 return VPORT_INVAL;
571         if (!scsi_host_get(shost)) {
572                 scsi_host_put(shost);
573                 return VPORT_INVAL;
574         }
575         spin_lock_irq(&phba->hbalock);
576         vport->load_flag |= FC_UNLOADING;
577         spin_unlock_irq(&phba->hbalock);
578
579         lpfc_free_sysfs_attr(vport);
580
581         lpfc_debugfs_terminate(vport);
582
583         /* Remove FC host and then SCSI host with the vport */
584         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);
588
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 &&
623             phba->link_state >= LPFC_LINK_UP &&
624             phba->fc_topology != TOPOLOGY_LOOP) {
625                 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                 }
636                 /* 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);
644                         /* Indicate free memory when release */
645                         NLP_SET_FREE_REQ(ndlp);
646                 } else {
647                         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 */
654                         lpfc_dequeue_node(vport, ndlp);
655                         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);
665                 }
666                 vport->unreg_vpi_cmpl = VPORT_INVAL;
667                 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
668                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
669                         goto skip_logo;
670                 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
671                         while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
672                                 timeout = schedule_timeout(timeout);
673         }
674
675         if (!(phba->pport->load_flag & FC_UNLOADING))
676                 lpfc_discovery_wait(vport);
677
678 skip_logo:
679         lpfc_cleanup(vport);
680         lpfc_sli_host_down(vport);
681
682         lpfc_stop_vport_timers(vport);
683
684         if (!(phba->pport->load_flag & FC_UNLOADING)) {
685                 lpfc_unreg_all_rpis(vport);
686                 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                  */
691                 if (lpfc_mbx_unreg_vpi(vport))
692                         scsi_host_put(shost);
693         } else
694                 scsi_host_put(shost);
695
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);
701         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
702                          "1828 Vport Deleted.\n");
703         scsi_host_put(shost);
704         return VPORT_OK;
705 }
706
707 struct lpfc_vport **
708 lpfc_create_vport_work_array(struct lpfc_hba *phba)
709 {
710         struct lpfc_vport *port_iterator;
711         struct lpfc_vport **vports;
712         int index = 0;
713         vports = kzalloc((phba->max_vpi + 1) * sizeof(struct lpfc_vport *),
714                          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) {
719                 if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
720                         lpfc_printf_vlog(port_iterator, KERN_WARNING, LOG_VPORT,
721                                          "1801 Create vport work array FAILED: "
722                                          "cannot do scsi_host_get\n");
723                         continue;
724                 }
725                 vports[index++] = port_iterator;
726         }
727         spin_unlock_irq(&phba->hbalock);
728         return vports;
729 }
730
731 void
732 lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
733 {
734         int i;
735         if (vports == NULL)
736                 return;
737         for (i=0; vports[i] != NULL && i <= phba->max_vpi; i++)
738                 scsi_host_put(lpfc_shost_from_vport(vports[i]));
739         kfree(vports);
740 }
741
742
743 /**
744  * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
745  * @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  **/
750 void
751 lpfc_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 /**
766  * lpfc_alloc_bucket - Allocate data buffer required for statistical data
767  * @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  **/
772 void
773 lpfc_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 /**
799  * lpfc_free_bucket - Free data buffer required for statistical data
800  * @vport: Pointer to vport object.
801  *
802  * Th function frees statistical data buffer of all the FC
803  * nodes of the vport.
804  **/
805 void
806 lpfc_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 }