]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/scsi/lpfc/lpfc_vport.c
2578d5fd9537e808ff3af01d215b0130b7c932c0
[mv-sheeva.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_disc.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_version.h"
43 #include "lpfc_vport.h"
44
45 inline void lpfc_vport_set_state(struct lpfc_vport *vport,
46                                  enum fc_vport_state new_state)
47 {
48         struct fc_vport *fc_vport = vport->fc_vport;
49
50         if (fc_vport) {
51                 /*
52                  * When the transport defines fc_vport_set state we will replace
53                  * this code with the following line
54                  */
55                 /* fc_vport_set_state(fc_vport, new_state); */
56                 if (new_state != FC_VPORT_INITIALIZING)
57                         fc_vport->vport_last_state = fc_vport->vport_state;
58                 fc_vport->vport_state = new_state;
59         }
60
61         /* for all the error states we will set the invternal state to FAILED */
62         switch (new_state) {
63         case FC_VPORT_NO_FABRIC_SUPP:
64         case FC_VPORT_NO_FABRIC_RSCS:
65         case FC_VPORT_FABRIC_LOGOUT:
66         case FC_VPORT_FABRIC_REJ_WWN:
67         case FC_VPORT_FAILED:
68                 vport->port_state = LPFC_VPORT_FAILED;
69                 break;
70         case FC_VPORT_LINKDOWN:
71                 vport->port_state = LPFC_VPORT_UNKNOWN;
72                 break;
73         default:
74                 /* do nothing */
75                 break;
76         }
77 }
78
79 static int
80 lpfc_alloc_vpi(struct lpfc_hba *phba)
81 {
82         int  vpi;
83
84         spin_lock_irq(&phba->hbalock);
85         /* Start at bit 1 because vpi zero is reserved for the physical port */
86         vpi = find_next_zero_bit(phba->vpi_bmask, (phba->max_vpi + 1), 1);
87         if (vpi > phba->max_vpi)
88                 vpi = 0;
89         else
90                 set_bit(vpi, phba->vpi_bmask);
91         spin_unlock_irq(&phba->hbalock);
92         return vpi;
93 }
94
95 static void
96 lpfc_free_vpi(struct lpfc_hba *phba, int vpi)
97 {
98         spin_lock_irq(&phba->hbalock);
99         clear_bit(vpi, phba->vpi_bmask);
100         spin_unlock_irq(&phba->hbalock);
101 }
102
103 static int
104 lpfc_vport_sparm(struct lpfc_hba *phba, struct lpfc_vport *vport)
105 {
106         LPFC_MBOXQ_t *pmb;
107         MAILBOX_t *mb;
108         struct lpfc_dmabuf *mp;
109         int  rc;
110
111         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
112         if (!pmb) {
113                 return -ENOMEM;
114         }
115         mb = &pmb->mb;
116
117         lpfc_read_sparam(phba, pmb, vport->vpi);
118         /*
119          * Grab buffer pointer and clear context1 so we can use
120          * lpfc_sli_issue_box_wait
121          */
122         mp = (struct lpfc_dmabuf *) pmb->context1;
123         pmb->context1 = NULL;
124
125         pmb->vport = vport;
126         rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
127         if (rc != MBX_SUCCESS) {
128                 if (signal_pending(current)) {
129                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
130                                          "1830 Signal aborted mbxCmd x%x\n",
131                                          mb->mbxCommand);
132                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
133                         kfree(mp);
134                         if (rc != MBX_TIMEOUT)
135                                 mempool_free(pmb, phba->mbox_mem_pool);
136                         return -EINTR;
137                 } else {
138                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
139                                          "1818 VPort failed init, mbxCmd x%x "
140                                          "READ_SPARM mbxStatus x%x, rc = x%x\n",
141                                          mb->mbxCommand, mb->mbxStatus, rc);
142                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
143                         kfree(mp);
144                         if (rc != MBX_TIMEOUT)
145                                 mempool_free(pmb, phba->mbox_mem_pool);
146                         return -EIO;
147                 }
148         }
149
150         memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
151         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
152                sizeof (struct lpfc_name));
153         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
154                sizeof (struct lpfc_name));
155
156         lpfc_mbuf_free(phba, mp->virt, mp->phys);
157         kfree(mp);
158         mempool_free(pmb, phba->mbox_mem_pool);
159
160         return 0;
161 }
162
163 static int
164 lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn,
165                       const char *name_type)
166 {
167                                 /* ensure that IEEE format 1 addresses
168                                  * contain zeros in bits 59-48
169                                  */
170         if (!((wwn->u.wwn[0] >> 4) == 1 &&
171               ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0)))
172                 return 1;
173
174         lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
175                         "1822 Invalid %s: %02x:%02x:%02x:%02x:"
176                         "%02x:%02x:%02x:%02x\n",
177                         name_type,
178                         wwn->u.wwn[0], wwn->u.wwn[1],
179                         wwn->u.wwn[2], wwn->u.wwn[3],
180                         wwn->u.wwn[4], wwn->u.wwn[5],
181                         wwn->u.wwn[6], wwn->u.wwn[7]);
182         return 0;
183 }
184
185 static int
186 lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
187 {
188         struct lpfc_vport *vport;
189         unsigned long flags;
190
191         spin_lock_irqsave(&phba->hbalock, flags);
192         list_for_each_entry(vport, &phba->port_list, listentry) {
193                 if (vport == new_vport)
194                         continue;
195                 /* If they match, return not unique */
196                 if (memcmp(&vport->fc_sparam.portName,
197                            &new_vport->fc_sparam.portName,
198                            sizeof(struct lpfc_name)) == 0) {
199                         spin_unlock_irqrestore(&phba->hbalock, flags);
200                         return 0;
201                 }
202         }
203         spin_unlock_irqrestore(&phba->hbalock, flags);
204         return 1;
205 }
206
207 /**
208  * lpfc_discovery_wait: Wait for driver discovery to quiesce.
209  * @vport: The virtual port for which this call is being executed.
210  *
211  * This driver calls this routine specifically from lpfc_vport_delete
212  * to enforce a synchronous execution of vport
213  * delete relative to discovery activities.  The
214  * lpfc_vport_delete routine should not return until it
215  * can reasonably guarantee that discovery has quiesced.
216  * Post FDISC LOGO, the driver must wait until its SAN teardown is
217  * complete and all resources recovered before allowing
218  * cleanup.
219  *
220  * This routine does not require any locks held.
221  **/
222 static void lpfc_discovery_wait(struct lpfc_vport *vport)
223 {
224         struct lpfc_hba *phba = vport->phba;
225         uint32_t wait_flags = 0;
226         unsigned long wait_time_max;
227         unsigned long start_time;
228
229         wait_flags = FC_RSCN_MODE | FC_RSCN_DISCOVERY | FC_NLP_MORE |
230                      FC_RSCN_DEFERRED | FC_NDISC_ACTIVE | FC_DISC_TMO;
231
232         /*
233          * The time constraint on this loop is a balance between the
234          * fabric RA_TOV value and dev_loss tmo.  The driver's
235          * devloss_tmo is 10 giving this loop a 3x multiplier minimally.
236          */
237         wait_time_max = msecs_to_jiffies(((phba->fc_ratov * 3) + 3) * 1000);
238         wait_time_max += jiffies;
239         start_time = jiffies;
240         while (time_before(jiffies, wait_time_max)) {
241                 if ((vport->num_disc_nodes > 0)    ||
242                     (vport->fc_flag & wait_flags)  ||
243                     ((vport->port_state > LPFC_VPORT_FAILED) &&
244                      (vport->port_state < LPFC_VPORT_READY))) {
245                         lpfc_printf_log(phba, KERN_INFO, LOG_VPORT,
246                                         "1833 Vport discovery quiesce Wait:"
247                                         " vpi x%x state x%x fc_flags x%x"
248                                         " num_nodes x%x, waiting 1000 msecs"
249                                         " total wait msecs x%x\n",
250                                         vport->vpi, vport->port_state,
251                                         vport->fc_flag, vport->num_disc_nodes,
252                                         jiffies_to_msecs(jiffies - start_time));
253                         msleep(1000);
254                 } else {
255                         /* Base case.  Wait variants satisfied.  Break out */
256                         lpfc_printf_log(phba, KERN_INFO, LOG_VPORT,
257                                          "1834 Vport discovery quiesced:"
258                                          " vpi x%x state x%x fc_flags x%x"
259                                          " wait msecs x%x\n",
260                                          vport->vpi, vport->port_state,
261                                          vport->fc_flag,
262                                          jiffies_to_msecs(jiffies
263                                                 - start_time));
264                         break;
265                 }
266         }
267
268         if (time_after(jiffies, wait_time_max))
269                 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
270                                 "1835 Vport discovery quiesce failed:"
271                                 " vpi x%x state x%x fc_flags x%x"
272                                 " wait msecs x%x\n",
273                                 vport->vpi, vport->port_state,
274                                 vport->fc_flag,
275                                 jiffies_to_msecs(jiffies - start_time));
276 }
277
278 int
279 lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
280 {
281         struct lpfc_nodelist *ndlp;
282         struct Scsi_Host *shost = fc_vport->shost;
283         struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata;
284         struct lpfc_hba   *phba = pport->phba;
285         struct lpfc_vport *vport = NULL;
286         int instance;
287         int vpi;
288         int rc = VPORT_ERROR;
289         int status;
290         int size;
291
292         if ((phba->sli_rev < 3) ||
293                 !(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
294                 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
295                                 "1808 Create VPORT failed: "
296                                 "NPIV is not enabled: SLImode:%d\n",
297                                 phba->sli_rev);
298                 rc = VPORT_INVAL;
299                 goto error_out;
300         }
301
302         vpi = lpfc_alloc_vpi(phba);
303         if (vpi == 0) {
304                 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
305                                 "1809 Create VPORT failed: "
306                                 "Max VPORTs (%d) exceeded\n",
307                                 phba->max_vpi);
308                 rc = VPORT_NORESOURCES;
309                 goto error_out;
310         }
311
312
313         /* Assign an unused board number */
314         if ((instance = lpfc_get_instance()) < 0) {
315                 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
316                                 "1810 Create VPORT failed: Cannot get "
317                                 "instance number\n");
318                 lpfc_free_vpi(phba, vpi);
319                 rc = VPORT_NORESOURCES;
320                 goto error_out;
321         }
322
323         vport = lpfc_create_port(phba, instance, &fc_vport->dev);
324         if (!vport) {
325                 lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
326                                 "1811 Create VPORT failed: vpi x%x\n", vpi);
327                 lpfc_free_vpi(phba, vpi);
328                 rc = VPORT_NORESOURCES;
329                 goto error_out;
330         }
331
332         vport->vpi = vpi;
333         lpfc_debugfs_initialize(vport);
334
335         if ((status = lpfc_vport_sparm(phba, vport))) {
336                 if (status == -EINTR) {
337                         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
338                                          "1831 Create VPORT Interrupted.\n");
339                         rc = VPORT_ERROR;
340                 } else {
341                         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
342                                          "1813 Create VPORT failed. "
343                                          "Cannot get sparam\n");
344                         rc = VPORT_NORESOURCES;
345                 }
346                 lpfc_free_vpi(phba, vpi);
347                 destroy_port(vport);
348                 goto error_out;
349         }
350
351         memcpy(vport->fc_portname.u.wwn, vport->fc_sparam.portName.u.wwn, 8);
352         memcpy(vport->fc_nodename.u.wwn, vport->fc_sparam.nodeName.u.wwn, 8);
353         size = strnlen(fc_vport->symbolic_name, LPFC_VNAME_LEN);
354         if (size) {
355                 vport->vname = kzalloc(size+1, GFP_KERNEL);
356                 if (!vport->vname) {
357                         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
358                                          "1814 Create VPORT failed. "
359                                          "vname allocation failed.\n");
360                         rc = VPORT_ERROR;
361                         lpfc_free_vpi(phba, vpi);
362                         destroy_port(vport);
363                         goto error_out;
364                 }
365                 memcpy(vport->vname, fc_vport->symbolic_name, size+1);
366         }
367         if (fc_vport->node_name != 0)
368                 u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
369         if (fc_vport->port_name != 0)
370                 u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
371
372         memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
373         memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
374
375         if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
376             !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
377                 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
378                                  "1821 Create VPORT failed. "
379                                  "Invalid WWN format\n");
380                 lpfc_free_vpi(phba, vpi);
381                 destroy_port(vport);
382                 rc = VPORT_INVAL;
383                 goto error_out;
384         }
385
386         if (!lpfc_unique_wwpn(phba, vport)) {
387                 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
388                                  "1823 Create VPORT failed. "
389                                  "Duplicate WWN on HBA\n");
390                 lpfc_free_vpi(phba, vpi);
391                 destroy_port(vport);
392                 rc = VPORT_INVAL;
393                 goto error_out;
394         }
395
396         *(struct lpfc_vport **)fc_vport->dd_data = vport;
397         vport->fc_vport = fc_vport;
398
399         if ((phba->link_state < LPFC_LINK_UP) ||
400             (phba->fc_topology == TOPOLOGY_LOOP)) {
401                 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
402                 rc = VPORT_OK;
403                 goto out;
404         }
405
406         if (disable) {
407                 rc = VPORT_OK;
408                 goto out;
409         }
410
411         /* Use the Physical nodes Fabric NDLP to determine if the link is
412          * up and ready to FDISC.
413          */
414         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
415         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
416             ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
417                 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
418                         lpfc_set_disctmo(vport);
419                         lpfc_initial_fdisc(vport);
420                 } else {
421                         lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
422                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
423                                          "0262 No NPIV Fabric support\n");
424                 }
425         } else {
426                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
427         }
428         rc = VPORT_OK;
429
430 out:
431         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
432                         "1825 Vport Created.\n");
433         lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
434 error_out:
435         return rc;
436 }
437
438 static int
439 disable_vport(struct fc_vport *fc_vport)
440 {
441         struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
442         struct lpfc_hba   *phba = vport->phba;
443         struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
444         long timeout;
445
446         ndlp = lpfc_findnode_did(vport, Fabric_DID);
447         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
448             && phba->link_state >= LPFC_LINK_UP) {
449                 vport->unreg_vpi_cmpl = VPORT_INVAL;
450                 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
451                 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
452                         while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
453                                 timeout = schedule_timeout(timeout);
454         }
455
456         lpfc_sli_host_down(vport);
457
458         /* Mark all nodes for discovery so we can remove them by
459          * calling lpfc_cleanup_rpis(vport, 1)
460          */
461         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
462                 if (!NLP_CHK_NODE_ACT(ndlp))
463                         continue;
464                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
465                         continue;
466                 lpfc_disc_state_machine(vport, ndlp, NULL,
467                                         NLP_EVT_DEVICE_RECOVERY);
468         }
469         lpfc_cleanup_rpis(vport, 1);
470
471         lpfc_stop_vport_timers(vport);
472         lpfc_unreg_all_rpis(vport);
473         lpfc_unreg_default_rpis(vport);
474         /*
475          * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
476          * scsi_host_put() to release the vport.
477          */
478         lpfc_mbx_unreg_vpi(vport);
479
480         lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
481         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
482                          "1826 Vport Disabled.\n");
483         return VPORT_OK;
484 }
485
486 static int
487 enable_vport(struct fc_vport *fc_vport)
488 {
489         struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
490         struct lpfc_hba   *phba = vport->phba;
491         struct lpfc_nodelist *ndlp = NULL;
492
493         if ((phba->link_state < LPFC_LINK_UP) ||
494             (phba->fc_topology == TOPOLOGY_LOOP)) {
495                 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
496                 return VPORT_OK;
497         }
498
499         vport->load_flag |= FC_LOADING;
500         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
501
502         /* Use the Physical nodes Fabric NDLP to determine if the link is
503          * up and ready to FDISC.
504          */
505         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
506         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
507             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
508                 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
509                         lpfc_set_disctmo(vport);
510                         lpfc_initial_fdisc(vport);
511                 } else {
512                         lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
513                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
514                                          "0264 No NPIV Fabric support\n");
515                 }
516         } else {
517                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
518         }
519         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
520                          "1827 Vport Enabled.\n");
521         return VPORT_OK;
522 }
523
524 int
525 lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
526 {
527         if (disable)
528                 return disable_vport(fc_vport);
529         else
530                 return enable_vport(fc_vport);
531 }
532
533
534 int
535 lpfc_vport_delete(struct fc_vport *fc_vport)
536 {
537         struct lpfc_nodelist *ndlp = NULL;
538         struct Scsi_Host *shost = (struct Scsi_Host *) fc_vport->shost;
539         struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
540         struct lpfc_hba   *phba = vport->phba;
541         long timeout;
542
543         if (vport->port_type == LPFC_PHYSICAL_PORT) {
544                 lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
545                                  "1812 vport_delete failed: Cannot delete "
546                                  "physical host\n");
547                 return VPORT_ERROR;
548         }
549         /*
550          * If we are not unloading the driver then prevent the vport_delete
551          * from happening until after this vport's discovery is finished.
552          */
553         if (!(phba->pport->load_flag & FC_UNLOADING)) {
554                 int check_count = 0;
555                 while (check_count < ((phba->fc_ratov * 3) + 3) &&
556                        vport->port_state > LPFC_VPORT_FAILED &&
557                        vport->port_state < LPFC_VPORT_READY) {
558                         check_count++;
559                         msleep(1000);
560                 }
561                 if (vport->port_state > LPFC_VPORT_FAILED &&
562                     vport->port_state < LPFC_VPORT_READY)
563                         return -EAGAIN;
564         }
565         /*
566          * This is a bit of a mess.  We want to ensure the shost doesn't get
567          * torn down until we're done with the embedded lpfc_vport structure.
568          *
569          * Beyond holding a reference for this function, we also need a
570          * reference for outstanding I/O requests we schedule during delete
571          * processing.  But once we scsi_remove_host() we can no longer obtain
572          * a reference through scsi_host_get().
573          *
574          * So we take two references here.  We release one reference at the
575          * bottom of the function -- after delinking the vport.  And we
576          * release the other at the completion of the unreg_vpi that get's
577          * initiated after we've disposed of all other resources associated
578          * with the port.
579          */
580         if (!scsi_host_get(shost))
581                 return VPORT_INVAL;
582         if (!scsi_host_get(shost)) {
583                 scsi_host_put(shost);
584                 return VPORT_INVAL;
585         }
586         spin_lock_irq(&phba->hbalock);
587         vport->load_flag |= FC_UNLOADING;
588         spin_unlock_irq(&phba->hbalock);
589         kfree(vport->vname);
590         lpfc_debugfs_terminate(vport);
591         fc_remove_host(lpfc_shost_from_vport(vport));
592         scsi_remove_host(lpfc_shost_from_vport(vport));
593
594         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
595
596         /* In case of driver unload, we shall not perform fabric logo as the
597          * worker thread already stopped at this stage and, in this case, we
598          * can safely skip the fabric logo.
599          */
600         if (phba->pport->load_flag & FC_UNLOADING) {
601                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
602                     ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
603                     phba->link_state >= LPFC_LINK_UP) {
604                         /* First look for the Fabric ndlp */
605                         ndlp = lpfc_findnode_did(vport, Fabric_DID);
606                         if (!ndlp)
607                                 goto skip_logo;
608                         else if (!NLP_CHK_NODE_ACT(ndlp)) {
609                                 ndlp = lpfc_enable_node(vport, ndlp,
610                                                         NLP_STE_UNUSED_NODE);
611                                 if (!ndlp)
612                                         goto skip_logo;
613                         }
614                         /* Remove ndlp from vport npld list */
615                         lpfc_dequeue_node(vport, ndlp);
616
617                         /* Indicate free memory when release */
618                         spin_lock_irq(&phba->ndlp_lock);
619                         NLP_SET_FREE_REQ(ndlp);
620                         spin_unlock_irq(&phba->ndlp_lock);
621                         /* Kick off release ndlp when it can be safely done */
622                         lpfc_nlp_put(ndlp);
623                 }
624                 goto skip_logo;
625         }
626
627         /* Otherwise, we will perform fabric logo as needed */
628         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
629             ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
630             phba->link_state >= LPFC_LINK_UP &&
631             phba->fc_topology != TOPOLOGY_LOOP) {
632                 if (vport->cfg_enable_da_id) {
633                         timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
634                         if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
635                                 while (vport->ct_flags && timeout)
636                                         timeout = schedule_timeout(timeout);
637                         else
638                                 lpfc_printf_log(vport->phba, KERN_WARNING,
639                                                 LOG_VPORT,
640                                                 "1829 CT command failed to "
641                                                 "delete objects on fabric. \n");
642                 }
643                 /* First look for the Fabric ndlp */
644                 ndlp = lpfc_findnode_did(vport, Fabric_DID);
645                 if (!ndlp) {
646                         /* Cannot find existing Fabric ndlp, allocate one */
647                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
648                         if (!ndlp)
649                                 goto skip_logo;
650                         lpfc_nlp_init(vport, ndlp, Fabric_DID);
651                         /* Indicate free memory when release */
652                         NLP_SET_FREE_REQ(ndlp);
653                 } else {
654                         if (!NLP_CHK_NODE_ACT(ndlp))
655                                 ndlp = lpfc_enable_node(vport, ndlp,
656                                                 NLP_STE_UNUSED_NODE);
657                                 if (!ndlp)
658                                         goto skip_logo;
659
660                         /* Remove ndlp from vport npld list */
661                         lpfc_dequeue_node(vport, ndlp);
662                         spin_lock_irq(&phba->ndlp_lock);
663                         if (!NLP_CHK_FREE_REQ(ndlp))
664                                 /* Indicate free memory when release */
665                                 NLP_SET_FREE_REQ(ndlp);
666                         else {
667                                 /* Skip this if ndlp is already in free mode */
668                                 spin_unlock_irq(&phba->ndlp_lock);
669                                 goto skip_logo;
670                         }
671                         spin_unlock_irq(&phba->ndlp_lock);
672                 }
673                 vport->unreg_vpi_cmpl = VPORT_INVAL;
674                 timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
675                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
676                         goto skip_logo;
677                 if (!lpfc_issue_els_npiv_logo(vport, ndlp))
678                         while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
679                                 timeout = schedule_timeout(timeout);
680         }
681
682         if (!(phba->pport->load_flag & FC_UNLOADING))
683                 lpfc_discovery_wait(vport);
684
685 skip_logo:
686         lpfc_cleanup(vport);
687         lpfc_sli_host_down(vport);
688
689         lpfc_stop_vport_timers(vport);
690
691         if (!(phba->pport->load_flag & FC_UNLOADING)) {
692                 lpfc_unreg_all_rpis(vport);
693                 lpfc_unreg_default_rpis(vport);
694                 /*
695                  * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
696                  * does the scsi_host_put() to release the vport.
697                  */
698                 if (lpfc_mbx_unreg_vpi(vport))
699                         scsi_host_put(shost);
700         } else
701                 scsi_host_put(shost);
702
703         lpfc_free_vpi(phba, vport->vpi);
704         vport->work_port_events = 0;
705         spin_lock_irq(&phba->hbalock);
706         list_del_init(&vport->listentry);
707         spin_unlock_irq(&phba->hbalock);
708         lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
709                          "1828 Vport Deleted.\n");
710         scsi_host_put(shost);
711         return VPORT_OK;
712 }
713
714 struct lpfc_vport **
715 lpfc_create_vport_work_array(struct lpfc_hba *phba)
716 {
717         struct lpfc_vport *port_iterator;
718         struct lpfc_vport **vports;
719         int index = 0;
720         vports = kzalloc((phba->max_vpi + 1) * sizeof(struct lpfc_vport *),
721                          GFP_KERNEL);
722         if (vports == NULL)
723                 return NULL;
724         spin_lock_irq(&phba->hbalock);
725         list_for_each_entry(port_iterator, &phba->port_list, listentry) {
726                 if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
727                         lpfc_printf_vlog(port_iterator, KERN_WARNING, LOG_VPORT,
728                                          "1801 Create vport work array FAILED: "
729                                          "cannot do scsi_host_get\n");
730                         continue;
731                 }
732                 vports[index++] = port_iterator;
733         }
734         spin_unlock_irq(&phba->hbalock);
735         return vports;
736 }
737
738 void
739 lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
740 {
741         int i;
742         if (vports == NULL)
743                 return;
744         for (i=0; vports[i] != NULL && i <= phba->max_vpi; i++)
745                 scsi_host_put(lpfc_shost_from_vport(vports[i]));
746         kfree(vports);
747 }