]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/lpfc/lpfc_vport.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bart/ide-2.6
[linux-2.6-omap-h63xx.git] / 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         int size;
292
293         if ((phba->sli_rev < 3) ||
294                 !(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
295                 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
296                                 "1808 Create VPORT failed: "
297                                 "NPIV is not enabled: SLImode:%d\n",
298                                 phba->sli_rev);
299                 rc = VPORT_INVAL;
300                 goto error_out;
301         }
302
303         vpi = lpfc_alloc_vpi(phba);
304         if (vpi == 0) {
305                 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
306                                 "1809 Create VPORT failed: "
307                                 "Max VPORTs (%d) exceeded\n",
308                                 phba->max_vpi);
309                 rc = VPORT_NORESOURCES;
310                 goto error_out;
311         }
312
313
314         /* Assign an unused board number */
315         if ((instance = lpfc_get_instance()) < 0) {
316                 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
317                                 "1810 Create VPORT failed: Cannot get "
318                                 "instance number\n");
319                 lpfc_free_vpi(phba, vpi);
320                 rc = VPORT_NORESOURCES;
321                 goto error_out;
322         }
323
324         vport = lpfc_create_port(phba, instance, &fc_vport->dev);
325         if (!vport) {
326                 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
327                                 "1811 Create VPORT failed: vpi x%x\n", vpi);
328                 lpfc_free_vpi(phba, vpi);
329                 rc = VPORT_NORESOURCES;
330                 goto error_out;
331         }
332
333         vport->vpi = vpi;
334         lpfc_debugfs_initialize(vport);
335
336         if ((status = lpfc_vport_sparm(phba, vport))) {
337                 if (status == -EINTR) {
338                         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
339                                          "1831 Create VPORT Interrupted.\n");
340                         rc = VPORT_ERROR;
341                 } else {
342                         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
343                                          "1813 Create VPORT failed. "
344                                          "Cannot get sparam\n");
345                         rc = VPORT_NORESOURCES;
346                 }
347                 lpfc_free_vpi(phba, vpi);
348                 destroy_port(vport);
349                 goto error_out;
350         }
351
352         memcpy(vport->fc_portname.u.wwn, vport->fc_sparam.portName.u.wwn, 8);
353         memcpy(vport->fc_nodename.u.wwn, vport->fc_sparam.nodeName.u.wwn, 8);
354         size = strnlen(fc_vport->symbolic_name, LPFC_VNAME_LEN);
355         if (size) {
356                 vport->vname = kzalloc(size+1, GFP_KERNEL);
357                 if (!vport->vname) {
358                         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
359                                          "1814 Create VPORT failed. "
360                                          "vname allocation failed.\n");
361                         rc = VPORT_ERROR;
362                         lpfc_free_vpi(phba, vpi);
363                         destroy_port(vport);
364                         goto error_out;
365                 }
366                 memcpy(vport->vname, fc_vport->symbolic_name, size+1);
367         }
368         if (fc_vport->node_name != 0)
369                 u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
370         if (fc_vport->port_name != 0)
371                 u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
372
373         memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
374         memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
375
376         if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
377             !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
378                 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
379                                  "1821 Create VPORT failed. "
380                                  "Invalid WWN format\n");
381                 lpfc_free_vpi(phba, vpi);
382                 destroy_port(vport);
383                 rc = VPORT_INVAL;
384                 goto error_out;
385         }
386
387         if (!lpfc_unique_wwpn(phba, vport)) {
388                 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
389                                  "1823 Create VPORT failed. "
390                                  "Duplicate WWN on HBA\n");
391                 lpfc_free_vpi(phba, vpi);
392                 destroy_port(vport);
393                 rc = VPORT_INVAL;
394                 goto error_out;
395         }
396
397         *(struct lpfc_vport **)fc_vport->dd_data = vport;
398         vport->fc_vport = fc_vport;
399
400         if ((phba->link_state < LPFC_LINK_UP) ||
401             (phba->fc_topology == TOPOLOGY_LOOP)) {
402                 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
403                 rc = VPORT_OK;
404                 goto out;
405         }
406
407         if (disable) {
408                 rc = VPORT_OK;
409                 goto out;
410         }
411
412         /* Use the Physical nodes Fabric NDLP to determine if the link is
413          * up and ready to FDISC.
414          */
415         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
416         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
417             ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
418                 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
419                         lpfc_set_disctmo(vport);
420                         lpfc_initial_fdisc(vport);
421                 } else {
422                         lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
423                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
424                                          "0262 No NPIV Fabric support\n");
425                 }
426         } else {
427                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
428         }
429         rc = VPORT_OK;
430
431 out:
432         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
433                         "1825 Vport Created.\n");
434         lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
435 error_out:
436         return rc;
437 }
438
439 static int
440 disable_vport(struct fc_vport *fc_vport)
441 {
442         struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
443         struct lpfc_hba   *phba = vport->phba;
444         struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
445         long timeout;
446
447         ndlp = lpfc_findnode_did(vport, Fabric_DID);
448         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
449             && phba->link_state >= LPFC_LINK_UP) {
450                 vport->unreg_vpi_cmpl = VPORT_INVAL;
451                 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
452                 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
453                         while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
454                                 timeout = schedule_timeout(timeout);
455         }
456
457         lpfc_sli_host_down(vport);
458
459         /* Mark all nodes for discovery so we can remove them by
460          * calling lpfc_cleanup_rpis(vport, 1)
461          */
462         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
463                 if (!NLP_CHK_NODE_ACT(ndlp))
464                         continue;
465                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
466                         continue;
467                 lpfc_disc_state_machine(vport, ndlp, NULL,
468                                         NLP_EVT_DEVICE_RECOVERY);
469         }
470         lpfc_cleanup_rpis(vport, 1);
471
472         lpfc_stop_vport_timers(vport);
473         lpfc_unreg_all_rpis(vport);
474         lpfc_unreg_default_rpis(vport);
475         /*
476          * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
477          * scsi_host_put() to release the vport.
478          */
479         lpfc_mbx_unreg_vpi(vport);
480
481         lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
482         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
483                          "1826 Vport Disabled.\n");
484         return VPORT_OK;
485 }
486
487 static int
488 enable_vport(struct fc_vport *fc_vport)
489 {
490         struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
491         struct lpfc_hba   *phba = vport->phba;
492         struct lpfc_nodelist *ndlp = NULL;
493
494         if ((phba->link_state < LPFC_LINK_UP) ||
495             (phba->fc_topology == TOPOLOGY_LOOP)) {
496                 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
497                 return VPORT_OK;
498         }
499
500         vport->load_flag |= FC_LOADING;
501         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
502
503         /* Use the Physical nodes Fabric NDLP to determine if the link is
504          * up and ready to FDISC.
505          */
506         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
507         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
508             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
509                 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
510                         lpfc_set_disctmo(vport);
511                         lpfc_initial_fdisc(vport);
512                 } else {
513                         lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
514                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
515                                          "0264 No NPIV Fabric support\n");
516                 }
517         } else {
518                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
519         }
520         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
521                          "1827 Vport Enabled.\n");
522         return VPORT_OK;
523 }
524
525 int
526 lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
527 {
528         if (disable)
529                 return disable_vport(fc_vport);
530         else
531                 return enable_vport(fc_vport);
532 }
533
534
535 int
536 lpfc_vport_delete(struct fc_vport *fc_vport)
537 {
538         struct lpfc_nodelist *ndlp = NULL;
539         struct Scsi_Host *shost = (struct Scsi_Host *) fc_vport->shost;
540         struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
541         struct lpfc_hba   *phba = vport->phba;
542         long timeout;
543
544         if (vport->port_type == LPFC_PHYSICAL_PORT) {
545                 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
546                                  "1812 vport_delete failed: Cannot delete "
547                                  "physical host\n");
548                 return VPORT_ERROR;
549         }
550         /*
551          * If we are not unloading the driver then prevent the vport_delete
552          * from happening until after this vport's discovery is finished.
553          */
554         if (!(phba->pport->load_flag & FC_UNLOADING)) {
555                 int check_count = 0;
556                 while (check_count < ((phba->fc_ratov * 3) + 3) &&
557                        vport->port_state > LPFC_VPORT_FAILED &&
558                        vport->port_state < LPFC_VPORT_READY) {
559                         check_count++;
560                         msleep(1000);
561                 }
562                 if (vport->port_state > LPFC_VPORT_FAILED &&
563                     vport->port_state < LPFC_VPORT_READY)
564                         return -EAGAIN;
565         }
566         /*
567          * This is a bit of a mess.  We want to ensure the shost doesn't get
568          * torn down until we're done with the embedded lpfc_vport structure.
569          *
570          * Beyond holding a reference for this function, we also need a
571          * reference for outstanding I/O requests we schedule during delete
572          * processing.  But once we scsi_remove_host() we can no longer obtain
573          * a reference through scsi_host_get().
574          *
575          * So we take two references here.  We release one reference at the
576          * bottom of the function -- after delinking the vport.  And we
577          * release the other at the completion of the unreg_vpi that get's
578          * initiated after we've disposed of all other resources associated
579          * with the port.
580          */
581         if (!scsi_host_get(shost))
582                 return VPORT_INVAL;
583         if (!scsi_host_get(shost)) {
584                 scsi_host_put(shost);
585                 return VPORT_INVAL;
586         }
587         spin_lock_irq(&phba->hbalock);
588         vport->load_flag |= FC_UNLOADING;
589         spin_unlock_irq(&phba->hbalock);
590         kfree(vport->vname);
591         lpfc_debugfs_terminate(vport);
592         fc_remove_host(lpfc_shost_from_vport(vport));
593         scsi_remove_host(lpfc_shost_from_vport(vport));
594
595         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
596
597         /* In case of driver unload, we shall not perform fabric logo as the
598          * worker thread already stopped at this stage and, in this case, we
599          * can safely skip the fabric logo.
600          */
601         if (phba->pport->load_flag & FC_UNLOADING) {
602                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
603                     ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
604                     phba->link_state >= LPFC_LINK_UP) {
605                         /* First look for the Fabric ndlp */
606                         ndlp = lpfc_findnode_did(vport, Fabric_DID);
607                         if (!ndlp)
608                                 goto skip_logo;
609                         else if (!NLP_CHK_NODE_ACT(ndlp)) {
610                                 ndlp = lpfc_enable_node(vport, ndlp,
611                                                         NLP_STE_UNUSED_NODE);
612                                 if (!ndlp)
613                                         goto skip_logo;
614                         }
615                         /* Remove ndlp from vport npld list */
616                         lpfc_dequeue_node(vport, ndlp);
617
618                         /* Indicate free memory when release */
619                         spin_lock_irq(&phba->ndlp_lock);
620                         NLP_SET_FREE_REQ(ndlp);
621                         spin_unlock_irq(&phba->ndlp_lock);
622                         /* Kick off release ndlp when it can be safely done */
623                         lpfc_nlp_put(ndlp);
624                 }
625                 goto skip_logo;
626         }
627
628         /* Otherwise, we will perform fabric logo as needed */
629         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
630             ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
631             phba->link_state >= LPFC_LINK_UP &&
632             phba->fc_topology != TOPOLOGY_LOOP) {
633                 if (vport->cfg_enable_da_id) {
634                         timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
635                         if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
636                                 while (vport->ct_flags && timeout)
637                                         timeout = schedule_timeout(timeout);
638                         else
639                                 lpfc_printf_log(vport->phba, KERN_WARNING,
640                                                 LOG_VPORT,
641                                                 "1829 CT command failed to "
642                                                 "delete objects on fabric. \n");
643                 }
644                 /* First look for the Fabric ndlp */
645                 ndlp = lpfc_findnode_did(vport, Fabric_DID);
646                 if (!ndlp) {
647                         /* Cannot find existing Fabric ndlp, allocate one */
648                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
649                         if (!ndlp)
650                                 goto skip_logo;
651                         lpfc_nlp_init(vport, ndlp, Fabric_DID);
652                         /* Indicate free memory when release */
653                         NLP_SET_FREE_REQ(ndlp);
654                 } else {
655                         if (!NLP_CHK_NODE_ACT(ndlp))
656                                 ndlp = lpfc_enable_node(vport, ndlp,
657                                                 NLP_STE_UNUSED_NODE);
658                                 if (!ndlp)
659                                         goto skip_logo;
660
661                         /* Remove ndlp from vport npld list */
662                         lpfc_dequeue_node(vport, ndlp);
663                         spin_lock_irq(&phba->ndlp_lock);
664                         if (!NLP_CHK_FREE_REQ(ndlp))
665                                 /* Indicate free memory when release */
666                                 NLP_SET_FREE_REQ(ndlp);
667                         else {
668                                 /* Skip this if ndlp is already in free mode */
669                                 spin_unlock_irq(&phba->ndlp_lock);
670                                 goto skip_logo;
671                         }
672                         spin_unlock_irq(&phba->ndlp_lock);
673                 }
674                 vport->unreg_vpi_cmpl = VPORT_INVAL;
675                 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
676                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
677                         goto skip_logo;
678                 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
679                         while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
680                                 timeout = schedule_timeout(timeout);
681         }
682
683         if (!(phba->pport->load_flag & FC_UNLOADING))
684                 lpfc_discovery_wait(vport);
685
686 skip_logo:
687         lpfc_cleanup(vport);
688         lpfc_sli_host_down(vport);
689
690         lpfc_stop_vport_timers(vport);
691
692         if (!(phba->pport->load_flag & FC_UNLOADING)) {
693                 lpfc_unreg_all_rpis(vport);
694                 lpfc_unreg_default_rpis(vport);
695                 /*
696                  * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
697                  * does the scsi_host_put() to release the vport.
698                  */
699                 if (lpfc_mbx_unreg_vpi(vport))
700                         scsi_host_put(shost);
701         } else
702                 scsi_host_put(shost);
703
704         lpfc_free_vpi(phba, vport->vpi);
705         vport->work_port_events = 0;
706         spin_lock_irq(&phba->hbalock);
707         list_del_init(&vport->listentry);
708         spin_unlock_irq(&phba->hbalock);
709         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
710                          "1828 Vport Deleted.\n");
711         scsi_host_put(shost);
712         return VPORT_OK;
713 }
714
715 struct lpfc_vport **
716 lpfc_create_vport_work_array(struct lpfc_hba *phba)
717 {
718         struct lpfc_vport *port_iterator;
719         struct lpfc_vport **vports;
720         int index = 0;
721         vports = kzalloc((phba->max_vpi + 1) * sizeof(struct lpfc_vport *),
722                          GFP_KERNEL);
723         if (vports == NULL)
724                 return NULL;
725         spin_lock_irq(&phba->hbalock);
726         list_for_each_entry(port_iterator, &phba->port_list, listentry) {
727                 if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
728                         lpfc_printf_vlog(port_iterator, KERN_WARNING, LOG_VPORT,
729                                          "1801 Create vport work array FAILED: "
730                                          "cannot do scsi_host_get\n");
731                         continue;
732                 }
733                 vports[index++] = port_iterator;
734         }
735         spin_unlock_irq(&phba->hbalock);
736         return vports;
737 }
738
739 void
740 lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
741 {
742         int i;
743         if (vports == NULL)
744                 return;
745         for (i=0; vports[i] != NULL && i <= phba->max_vpi; i++)
746                 scsi_host_put(lpfc_shost_from_vport(vports[i]));
747         kfree(vports);
748 }
749
750
751 /**
752  * lpfc_vport_reset_stat_data: Reset the statistical data for the vport.
753  * @vport: Pointer to vport object.
754  *
755  * This function resets the statistical data for the vport. This function
756  * is called with the host_lock held
757  **/
758 void
759 lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
760 {
761         struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
762
763         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
764                 if (!NLP_CHK_NODE_ACT(ndlp))
765                         continue;
766                 if (ndlp->lat_data)
767                         memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
768                                 sizeof(struct lpfc_scsicmd_bkt));
769         }
770 }
771
772
773 /**
774  * lpfc_alloc_bucket: Allocate data buffer required for collecting
775  *  statistical data.
776  * @vport: Pointer to vport object.
777  *
778  * This function allocates data buffer required for all the FC
779  * nodes of the vport to collect statistical data.
780  **/
781 void
782 lpfc_alloc_bucket(struct lpfc_vport *vport)
783 {
784         struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
785
786         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
787                 if (!NLP_CHK_NODE_ACT(ndlp))
788                         continue;
789
790                 kfree(ndlp->lat_data);
791                 ndlp->lat_data = NULL;
792
793                 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
794                         ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
795                                          sizeof(struct lpfc_scsicmd_bkt),
796                                          GFP_ATOMIC);
797
798                         if (!ndlp->lat_data)
799                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
800                                         "0287 lpfc_alloc_bucket failed to "
801                                         "allocate statistical data buffer DID "
802                                         "0x%x\n", ndlp->nlp_DID);
803                 }
804         }
805 }
806
807 /**
808  * lpfc_free_bucket: Free data buffer required for collecting
809  *  statistical data.
810  * @vport: Pointer to vport object.
811  *
812  * Th function frees statistical data buffer of all the FC
813  * nodes of the vport.
814  **/
815 void
816 lpfc_free_bucket(struct lpfc_vport *vport)
817 {
818         struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
819
820         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
821                 if (!NLP_CHK_NODE_ACT(ndlp))
822                         continue;
823
824                 kfree(ndlp->lat_data);
825                 ndlp->lat_data = NULL;
826         }
827 }