Merge branch 'linus' into x86/cpu
[linux-2.6] / drivers / scsi / lpfc / lpfc_init.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 #include <linux/ctype.h>
31
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport_fc.h>
36
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_disc.h"
40 #include "lpfc_scsi.h"
41 #include "lpfc.h"
42 #include "lpfc_logmsg.h"
43 #include "lpfc_crtn.h"
44 #include "lpfc_vport.h"
45 #include "lpfc_version.h"
46
47 static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
48 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
49 static int lpfc_post_rcv_buf(struct lpfc_hba *);
50
51 static struct scsi_transport_template *lpfc_transport_template = NULL;
52 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
53 static DEFINE_IDR(lpfc_hba_index);
54
55 /************************************************************************/
56 /*                                                                      */
57 /*    lpfc_config_port_prep                                             */
58 /*    This routine will do LPFC initialization prior to the             */
59 /*    CONFIG_PORT mailbox command. This will be initialized             */
60 /*    as a SLI layer callback routine.                                  */
61 /*    This routine returns 0 on success or -ERESTART if it wants        */
62 /*    the SLI layer to reset the HBA and try again. Any                 */
63 /*    other return value indicates an error.                            */
64 /*                                                                      */
65 /************************************************************************/
66 int
67 lpfc_config_port_prep(struct lpfc_hba *phba)
68 {
69         lpfc_vpd_t *vp = &phba->vpd;
70         int i = 0, rc;
71         LPFC_MBOXQ_t *pmb;
72         MAILBOX_t *mb;
73         char *lpfc_vpd_data = NULL;
74         uint16_t offset = 0;
75         static char licensed[56] =
76                     "key unlock for use with gnu public licensed code only\0";
77         static int init_key = 1;
78
79         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
80         if (!pmb) {
81                 phba->link_state = LPFC_HBA_ERROR;
82                 return -ENOMEM;
83         }
84
85         mb = &pmb->mb;
86         phba->link_state = LPFC_INIT_MBX_CMDS;
87
88         if (lpfc_is_LC_HBA(phba->pcidev->device)) {
89                 if (init_key) {
90                         uint32_t *ptext = (uint32_t *) licensed;
91
92                         for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
93                                 *ptext = cpu_to_be32(*ptext);
94                         init_key = 0;
95                 }
96
97                 lpfc_read_nv(phba, pmb);
98                 memset((char*)mb->un.varRDnvp.rsvd3, 0,
99                         sizeof (mb->un.varRDnvp.rsvd3));
100                 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
101                          sizeof (licensed));
102
103                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
104
105                 if (rc != MBX_SUCCESS) {
106                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
107                                         "0324 Config Port initialization "
108                                         "error, mbxCmd x%x READ_NVPARM, "
109                                         "mbxStatus x%x\n",
110                                         mb->mbxCommand, mb->mbxStatus);
111                         mempool_free(pmb, phba->mbox_mem_pool);
112                         return -ERESTART;
113                 }
114                 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
115                        sizeof(phba->wwnn));
116                 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
117                        sizeof(phba->wwpn));
118         }
119
120         phba->sli3_options = 0x0;
121
122         /* Setup and issue mailbox READ REV command */
123         lpfc_read_rev(phba, pmb);
124         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
125         if (rc != MBX_SUCCESS) {
126                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
127                                 "0439 Adapter failed to init, mbxCmd x%x "
128                                 "READ_REV, mbxStatus x%x\n",
129                                 mb->mbxCommand, mb->mbxStatus);
130                 mempool_free( pmb, phba->mbox_mem_pool);
131                 return -ERESTART;
132         }
133
134
135         /*
136          * The value of rr must be 1 since the driver set the cv field to 1.
137          * This setting requires the FW to set all revision fields.
138          */
139         if (mb->un.varRdRev.rr == 0) {
140                 vp->rev.rBit = 0;
141                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
142                                 "0440 Adapter failed to init, READ_REV has "
143                                 "missing revision information.\n");
144                 mempool_free(pmb, phba->mbox_mem_pool);
145                 return -ERESTART;
146         }
147
148         if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
149                 mempool_free(pmb, phba->mbox_mem_pool);
150                 return -EINVAL;
151         }
152
153         /* Save information as VPD data */
154         vp->rev.rBit = 1;
155         memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
156         vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
157         memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
158         vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
159         memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
160         vp->rev.biuRev = mb->un.varRdRev.biuRev;
161         vp->rev.smRev = mb->un.varRdRev.smRev;
162         vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
163         vp->rev.endecRev = mb->un.varRdRev.endecRev;
164         vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
165         vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
166         vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
167         vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
168         vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
169         vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
170
171         /* If the sli feature level is less then 9, we must
172          * tear down all RPIs and VPIs on link down if NPIV
173          * is enabled.
174          */
175         if (vp->rev.feaLevelHigh < 9)
176                 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
177
178         if (lpfc_is_LC_HBA(phba->pcidev->device))
179                 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
180                                                 sizeof (phba->RandomData));
181
182         /* Get adapter VPD information */
183         pmb->context2 = kmalloc(DMP_RSP_SIZE, GFP_KERNEL);
184         if (!pmb->context2)
185                 goto out_free_mbox;
186         lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
187         if (!lpfc_vpd_data)
188                 goto out_free_context2;
189
190         do {
191                 lpfc_dump_mem(phba, pmb, offset);
192                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
193
194                 if (rc != MBX_SUCCESS) {
195                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
196                                         "0441 VPD not present on adapter, "
197                                         "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
198                                         mb->mbxCommand, mb->mbxStatus);
199                         mb->un.varDmp.word_cnt = 0;
200                 }
201                 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
202                         mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
203                 lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
204                                       mb->un.varDmp.word_cnt);
205                 offset += mb->un.varDmp.word_cnt;
206         } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
207         lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
208
209         kfree(lpfc_vpd_data);
210 out_free_context2:
211         kfree(pmb->context2);
212 out_free_mbox:
213         mempool_free(pmb, phba->mbox_mem_pool);
214         return 0;
215 }
216
217 /* Completion handler for config async event mailbox command. */
218 static void
219 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
220 {
221         if (pmboxq->mb.mbxStatus == MBX_SUCCESS)
222                 phba->temp_sensor_support = 1;
223         else
224                 phba->temp_sensor_support = 0;
225         mempool_free(pmboxq, phba->mbox_mem_pool);
226         return;
227 }
228
229 /************************************************************************/
230 /*                                                                      */
231 /*    lpfc_config_port_post                                             */
232 /*    This routine will do LPFC initialization after the                */
233 /*    CONFIG_PORT mailbox command. This will be initialized             */
234 /*    as a SLI layer callback routine.                                  */
235 /*    This routine returns 0 on success. Any other return value         */
236 /*    indicates an error.                                               */
237 /*                                                                      */
238 /************************************************************************/
239 int
240 lpfc_config_port_post(struct lpfc_hba *phba)
241 {
242         struct lpfc_vport *vport = phba->pport;
243         LPFC_MBOXQ_t *pmb;
244         MAILBOX_t *mb;
245         struct lpfc_dmabuf *mp;
246         struct lpfc_sli *psli = &phba->sli;
247         uint32_t status, timeout;
248         int i, j;
249         int rc;
250
251         spin_lock_irq(&phba->hbalock);
252         /*
253          * If the Config port completed correctly the HBA is not
254          * over heated any more.
255          */
256         if (phba->over_temp_state == HBA_OVER_TEMP)
257                 phba->over_temp_state = HBA_NORMAL_TEMP;
258         spin_unlock_irq(&phba->hbalock);
259
260         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
261         if (!pmb) {
262                 phba->link_state = LPFC_HBA_ERROR;
263                 return -ENOMEM;
264         }
265         mb = &pmb->mb;
266
267         /* Get login parameters for NID.  */
268         lpfc_read_sparam(phba, pmb, 0);
269         pmb->vport = vport;
270         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
271                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
272                                 "0448 Adapter failed init, mbxCmd x%x "
273                                 "READ_SPARM mbxStatus x%x\n",
274                                 mb->mbxCommand, mb->mbxStatus);
275                 phba->link_state = LPFC_HBA_ERROR;
276                 mp = (struct lpfc_dmabuf *) pmb->context1;
277                 mempool_free( pmb, phba->mbox_mem_pool);
278                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
279                 kfree(mp);
280                 return -EIO;
281         }
282
283         mp = (struct lpfc_dmabuf *) pmb->context1;
284
285         memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
286         lpfc_mbuf_free(phba, mp->virt, mp->phys);
287         kfree(mp);
288         pmb->context1 = NULL;
289
290         if (phba->cfg_soft_wwnn)
291                 u64_to_wwn(phba->cfg_soft_wwnn,
292                            vport->fc_sparam.nodeName.u.wwn);
293         if (phba->cfg_soft_wwpn)
294                 u64_to_wwn(phba->cfg_soft_wwpn,
295                            vport->fc_sparam.portName.u.wwn);
296         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
297                sizeof (struct lpfc_name));
298         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
299                sizeof (struct lpfc_name));
300         /* If no serial number in VPD data, use low 6 bytes of WWNN */
301         /* This should be consolidated into parse_vpd ? - mr */
302         if (phba->SerialNumber[0] == 0) {
303                 uint8_t *outptr;
304
305                 outptr = &vport->fc_nodename.u.s.IEEE[0];
306                 for (i = 0; i < 12; i++) {
307                         status = *outptr++;
308                         j = ((status & 0xf0) >> 4);
309                         if (j <= 9)
310                                 phba->SerialNumber[i] =
311                                     (char)((uint8_t) 0x30 + (uint8_t) j);
312                         else
313                                 phba->SerialNumber[i] =
314                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
315                         i++;
316                         j = (status & 0xf);
317                         if (j <= 9)
318                                 phba->SerialNumber[i] =
319                                     (char)((uint8_t) 0x30 + (uint8_t) j);
320                         else
321                                 phba->SerialNumber[i] =
322                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
323                 }
324         }
325
326         lpfc_read_config(phba, pmb);
327         pmb->vport = vport;
328         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
329                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
330                                 "0453 Adapter failed to init, mbxCmd x%x "
331                                 "READ_CONFIG, mbxStatus x%x\n",
332                                 mb->mbxCommand, mb->mbxStatus);
333                 phba->link_state = LPFC_HBA_ERROR;
334                 mempool_free( pmb, phba->mbox_mem_pool);
335                 return -EIO;
336         }
337
338         /* Reset the DFT_HBA_Q_DEPTH to the max xri  */
339         if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
340                 phba->cfg_hba_queue_depth =
341                         mb->un.varRdConfig.max_xri + 1;
342
343         phba->lmt = mb->un.varRdConfig.lmt;
344
345         /* Get the default values for Model Name and Description */
346         lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
347
348         if ((phba->cfg_link_speed > LINK_SPEED_10G)
349             || ((phba->cfg_link_speed == LINK_SPEED_1G)
350                 && !(phba->lmt & LMT_1Gb))
351             || ((phba->cfg_link_speed == LINK_SPEED_2G)
352                 && !(phba->lmt & LMT_2Gb))
353             || ((phba->cfg_link_speed == LINK_SPEED_4G)
354                 && !(phba->lmt & LMT_4Gb))
355             || ((phba->cfg_link_speed == LINK_SPEED_8G)
356                 && !(phba->lmt & LMT_8Gb))
357             || ((phba->cfg_link_speed == LINK_SPEED_10G)
358                 && !(phba->lmt & LMT_10Gb))) {
359                 /* Reset link speed to auto */
360                 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
361                         "1302 Invalid speed for this board: "
362                         "Reset link speed to auto: x%x\n",
363                         phba->cfg_link_speed);
364                         phba->cfg_link_speed = LINK_SPEED_AUTO;
365         }
366
367         phba->link_state = LPFC_LINK_DOWN;
368
369         /* Only process IOCBs on ELS ring till hba_state is READY */
370         if (psli->ring[psli->extra_ring].cmdringaddr)
371                 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
372         if (psli->ring[psli->fcp_ring].cmdringaddr)
373                 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
374         if (psli->ring[psli->next_ring].cmdringaddr)
375                 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
376
377         /* Post receive buffers for desired rings */
378         if (phba->sli_rev != 3)
379                 lpfc_post_rcv_buf(phba);
380
381         /* Enable appropriate host interrupts */
382         spin_lock_irq(&phba->hbalock);
383         status = readl(phba->HCregaddr);
384         status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
385         if (psli->num_rings > 0)
386                 status |= HC_R0INT_ENA;
387         if (psli->num_rings > 1)
388                 status |= HC_R1INT_ENA;
389         if (psli->num_rings > 2)
390                 status |= HC_R2INT_ENA;
391         if (psli->num_rings > 3)
392                 status |= HC_R3INT_ENA;
393
394         if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
395             (phba->cfg_poll & DISABLE_FCP_RING_INT))
396                 status &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
397
398         writel(status, phba->HCregaddr);
399         readl(phba->HCregaddr); /* flush */
400         spin_unlock_irq(&phba->hbalock);
401
402         /*
403          * Setup the ring 0 (els)  timeout handler
404          */
405         timeout = phba->fc_ratov << 1;
406         mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
407         mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
408         phba->hb_outstanding = 0;
409         phba->last_completion_time = jiffies;
410
411         lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
412         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
413         pmb->vport = vport;
414         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
415         lpfc_set_loopback_flag(phba);
416         if (rc != MBX_SUCCESS) {
417                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
418                                 "0454 Adapter failed to init, mbxCmd x%x "
419                                 "INIT_LINK, mbxStatus x%x\n",
420                                 mb->mbxCommand, mb->mbxStatus);
421
422                 /* Clear all interrupt enable conditions */
423                 writel(0, phba->HCregaddr);
424                 readl(phba->HCregaddr); /* flush */
425                 /* Clear all pending interrupts */
426                 writel(0xffffffff, phba->HAregaddr);
427                 readl(phba->HAregaddr); /* flush */
428
429                 phba->link_state = LPFC_HBA_ERROR;
430                 if (rc != MBX_BUSY)
431                         mempool_free(pmb, phba->mbox_mem_pool);
432                 return -EIO;
433         }
434         /* MBOX buffer will be freed in mbox compl */
435         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
436         lpfc_config_async(phba, pmb, LPFC_ELS_RING);
437         pmb->mbox_cmpl = lpfc_config_async_cmpl;
438         pmb->vport = phba->pport;
439         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
440
441         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
442                 lpfc_printf_log(phba,
443                                 KERN_ERR,
444                                 LOG_INIT,
445                                 "0456 Adapter failed to issue "
446                                 "ASYNCEVT_ENABLE mbox status x%x \n.",
447                                 rc);
448                 mempool_free(pmb, phba->mbox_mem_pool);
449         }
450         return (0);
451 }
452
453 /************************************************************************/
454 /*                                                                      */
455 /*    lpfc_hba_down_prep                                                */
456 /*    This routine will do LPFC uninitialization before the             */
457 /*    HBA is reset when bringing down the SLI Layer. This will be       */
458 /*    initialized as a SLI layer callback routine.                      */
459 /*    This routine returns 0 on success. Any other return value         */
460 /*    indicates an error.                                               */
461 /*                                                                      */
462 /************************************************************************/
463 int
464 lpfc_hba_down_prep(struct lpfc_hba *phba)
465 {
466         struct lpfc_vport **vports;
467         int i;
468         /* Disable interrupts */
469         writel(0, phba->HCregaddr);
470         readl(phba->HCregaddr); /* flush */
471
472         if (phba->pport->load_flag & FC_UNLOADING)
473                 lpfc_cleanup_discovery_resources(phba->pport);
474         else {
475                 vports = lpfc_create_vport_work_array(phba);
476                 if (vports != NULL)
477                         for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
478                                 lpfc_cleanup_discovery_resources(vports[i]);
479                 lpfc_destroy_vport_work_array(phba, vports);
480         }
481         return 0;
482 }
483
484 /************************************************************************/
485 /*                                                                      */
486 /*    lpfc_hba_down_post                                                */
487 /*    This routine will do uninitialization after the HBA is reset      */
488 /*    when bringing down the SLI Layer.                                 */
489 /*    This routine returns 0 on success. Any other return value         */
490 /*    indicates an error.                                               */
491 /*                                                                      */
492 /************************************************************************/
493 int
494 lpfc_hba_down_post(struct lpfc_hba *phba)
495 {
496         struct lpfc_sli *psli = &phba->sli;
497         struct lpfc_sli_ring *pring;
498         struct lpfc_dmabuf *mp, *next_mp;
499         struct lpfc_iocbq *iocb;
500         IOCB_t *cmd = NULL;
501         LIST_HEAD(completions);
502         int i;
503
504         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
505                 lpfc_sli_hbqbuf_free_all(phba);
506         else {
507                 /* Cleanup preposted buffers on the ELS ring */
508                 pring = &psli->ring[LPFC_ELS_RING];
509                 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
510                         list_del(&mp->list);
511                         pring->postbufq_cnt--;
512                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
513                         kfree(mp);
514                 }
515         }
516
517         spin_lock_irq(&phba->hbalock);
518         for (i = 0; i < psli->num_rings; i++) {
519                 pring = &psli->ring[i];
520
521                 /* At this point in time the HBA is either reset or DOA. Either
522                  * way, nothing should be on txcmplq as it will NEVER complete.
523                  */
524                 list_splice_init(&pring->txcmplq, &completions);
525                 pring->txcmplq_cnt = 0;
526                 spin_unlock_irq(&phba->hbalock);
527
528                 while (!list_empty(&completions)) {
529                         iocb = list_get_first(&completions, struct lpfc_iocbq,
530                                 list);
531                         cmd = &iocb->iocb;
532                         list_del_init(&iocb->list);
533
534                         if (!iocb->iocb_cmpl)
535                                 lpfc_sli_release_iocbq(phba, iocb);
536                         else {
537                                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
538                                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
539                                 (iocb->iocb_cmpl) (phba, iocb, iocb);
540                         }
541                 }
542
543                 lpfc_sli_abort_iocb_ring(phba, pring);
544                 spin_lock_irq(&phba->hbalock);
545         }
546         spin_unlock_irq(&phba->hbalock);
547
548         return 0;
549 }
550
551 /* HBA heart beat timeout handler */
552 static void
553 lpfc_hb_timeout(unsigned long ptr)
554 {
555         struct lpfc_hba *phba;
556         uint32_t tmo_posted;
557         unsigned long iflag;
558
559         phba = (struct lpfc_hba *)ptr;
560         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
561         tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
562         if (!tmo_posted)
563                 phba->pport->work_port_events |= WORKER_HB_TMO;
564         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
565
566         if (!tmo_posted)
567                 lpfc_worker_wake_up(phba);
568         return;
569 }
570
571 static void
572 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
573 {
574         unsigned long drvr_flag;
575
576         spin_lock_irqsave(&phba->hbalock, drvr_flag);
577         phba->hb_outstanding = 0;
578         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
579
580         mempool_free(pmboxq, phba->mbox_mem_pool);
581         if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
582                 !(phba->link_state == LPFC_HBA_ERROR) &&
583                 !(phba->pport->load_flag & FC_UNLOADING))
584                 mod_timer(&phba->hb_tmofunc,
585                         jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
586         return;
587 }
588
589 void
590 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
591 {
592         LPFC_MBOXQ_t *pmboxq;
593         struct lpfc_dmabuf *buf_ptr;
594         int retval;
595         struct lpfc_sli *psli = &phba->sli;
596         LIST_HEAD(completions);
597
598         if ((phba->link_state == LPFC_HBA_ERROR) ||
599                 (phba->pport->load_flag & FC_UNLOADING) ||
600                 (phba->pport->fc_flag & FC_OFFLINE_MODE))
601                 return;
602
603         spin_lock_irq(&phba->pport->work_port_lock);
604         /* If the timer is already canceled do nothing */
605         if (!(phba->pport->work_port_events & WORKER_HB_TMO)) {
606                 spin_unlock_irq(&phba->pport->work_port_lock);
607                 return;
608         }
609
610         if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
611                 jiffies)) {
612                 spin_unlock_irq(&phba->pport->work_port_lock);
613                 if (!phba->hb_outstanding)
614                         mod_timer(&phba->hb_tmofunc,
615                                 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
616                 else
617                         mod_timer(&phba->hb_tmofunc,
618                                 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
619                 return;
620         }
621         spin_unlock_irq(&phba->pport->work_port_lock);
622
623         if (phba->elsbuf_cnt &&
624                 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
625                 spin_lock_irq(&phba->hbalock);
626                 list_splice_init(&phba->elsbuf, &completions);
627                 phba->elsbuf_cnt = 0;
628                 phba->elsbuf_prev_cnt = 0;
629                 spin_unlock_irq(&phba->hbalock);
630
631                 while (!list_empty(&completions)) {
632                         list_remove_head(&completions, buf_ptr,
633                                 struct lpfc_dmabuf, list);
634                         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
635                         kfree(buf_ptr);
636                 }
637         }
638         phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
639
640         /* If there is no heart beat outstanding, issue a heartbeat command */
641         if (phba->cfg_enable_hba_heartbeat) {
642                 if (!phba->hb_outstanding) {
643                         pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
644                         if (!pmboxq) {
645                                 mod_timer(&phba->hb_tmofunc,
646                                           jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
647                                 return;
648                         }
649
650                         lpfc_heart_beat(phba, pmboxq);
651                         pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
652                         pmboxq->vport = phba->pport;
653                         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
654
655                         if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
656                                 mempool_free(pmboxq, phba->mbox_mem_pool);
657                                 mod_timer(&phba->hb_tmofunc,
658                                           jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
659                                 return;
660                         }
661                         mod_timer(&phba->hb_tmofunc,
662                                   jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
663                         phba->hb_outstanding = 1;
664                         return;
665                 } else {
666                         /*
667                         * If heart beat timeout called with hb_outstanding set
668                         * we need to take the HBA offline.
669                         */
670                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
671                                         "0459 Adapter heartbeat failure, "
672                                         "taking this port offline.\n");
673
674                         spin_lock_irq(&phba->hbalock);
675                         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
676                         spin_unlock_irq(&phba->hbalock);
677
678                         lpfc_offline_prep(phba);
679                         lpfc_offline(phba);
680                         lpfc_unblock_mgmt_io(phba);
681                         phba->link_state = LPFC_HBA_ERROR;
682                         lpfc_hba_down_post(phba);
683                 }
684         }
685 }
686
687 static void
688 lpfc_offline_eratt(struct lpfc_hba *phba)
689 {
690         struct lpfc_sli   *psli = &phba->sli;
691
692         spin_lock_irq(&phba->hbalock);
693         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
694         spin_unlock_irq(&phba->hbalock);
695         lpfc_offline_prep(phba);
696
697         lpfc_offline(phba);
698         lpfc_reset_barrier(phba);
699         lpfc_sli_brdreset(phba);
700         lpfc_hba_down_post(phba);
701         lpfc_sli_brdready(phba, HS_MBRDY);
702         lpfc_unblock_mgmt_io(phba);
703         phba->link_state = LPFC_HBA_ERROR;
704         return;
705 }
706
707 /************************************************************************/
708 /*                                                                      */
709 /*    lpfc_handle_eratt                                                 */
710 /*    This routine will handle processing a Host Attention              */
711 /*    Error Status event. This will be initialized                      */
712 /*    as a SLI layer callback routine.                                  */
713 /*                                                                      */
714 /************************************************************************/
715 void
716 lpfc_handle_eratt(struct lpfc_hba *phba)
717 {
718         struct lpfc_vport *vport = phba->pport;
719         struct lpfc_sli   *psli = &phba->sli;
720         struct lpfc_sli_ring  *pring;
721         uint32_t event_data;
722         unsigned long temperature;
723         struct temp_event temp_event_data;
724         struct Scsi_Host  *shost;
725
726         /* If the pci channel is offline, ignore possible errors,
727          * since we cannot communicate with the pci card anyway. */
728         if (pci_channel_offline(phba->pcidev))
729                 return;
730         /* If resets are disabled then leave the HBA alone and return */
731         if (!phba->cfg_enable_hba_reset)
732                 return;
733
734         if (phba->work_hs & HS_FFER6) {
735                 /* Re-establishing Link */
736                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
737                                 "1301 Re-establishing Link "
738                                 "Data: x%x x%x x%x\n",
739                                 phba->work_hs,
740                                 phba->work_status[0], phba->work_status[1]);
741
742                 spin_lock_irq(&phba->hbalock);
743                 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
744                 spin_unlock_irq(&phba->hbalock);
745
746                 /*
747                 * Firmware stops when it triggled erratt with HS_FFER6.
748                 * That could cause the I/Os dropped by the firmware.
749                 * Error iocb (I/O) on txcmplq and let the SCSI layer
750                 * retry it after re-establishing link.
751                 */
752                 pring = &psli->ring[psli->fcp_ring];
753                 lpfc_sli_abort_iocb_ring(phba, pring);
754
755                 /*
756                  * There was a firmware error.  Take the hba offline and then
757                  * attempt to restart it.
758                  */
759                 lpfc_offline_prep(phba);
760                 lpfc_offline(phba);
761                 lpfc_sli_brdrestart(phba);
762                 if (lpfc_online(phba) == 0) {   /* Initialize the HBA */
763                         lpfc_unblock_mgmt_io(phba);
764                         return;
765                 }
766                 lpfc_unblock_mgmt_io(phba);
767         } else if (phba->work_hs & HS_CRIT_TEMP) {
768                 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
769                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
770                 temp_event_data.event_code = LPFC_CRIT_TEMP;
771                 temp_event_data.data = (uint32_t)temperature;
772
773                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
774                                 "0459 Adapter maximum temperature exceeded "
775                                 "(%ld), taking this port offline "
776                                 "Data: x%x x%x x%x\n",
777                                 temperature, phba->work_hs,
778                                 phba->work_status[0], phba->work_status[1]);
779
780                 shost = lpfc_shost_from_vport(phba->pport);
781                 fc_host_post_vendor_event(shost, fc_get_event_number(),
782                                           sizeof(temp_event_data),
783                                           (char *) &temp_event_data,
784                                           SCSI_NL_VID_TYPE_PCI
785                                           | PCI_VENDOR_ID_EMULEX);
786
787                 spin_lock_irq(&phba->hbalock);
788                 phba->over_temp_state = HBA_OVER_TEMP;
789                 spin_unlock_irq(&phba->hbalock);
790                 lpfc_offline_eratt(phba);
791
792         } else {
793                 /* The if clause above forces this code path when the status
794                  * failure is a value other than FFER6.  Do not call the offline
795                  *  twice. This is the adapter hardware error path.
796                  */
797                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
798                                 "0457 Adapter Hardware Error "
799                                 "Data: x%x x%x x%x\n",
800                                 phba->work_hs,
801                                 phba->work_status[0], phba->work_status[1]);
802
803                 event_data = FC_REG_DUMP_EVENT;
804                 shost = lpfc_shost_from_vport(vport);
805                 fc_host_post_vendor_event(shost, fc_get_event_number(),
806                                 sizeof(event_data), (char *) &event_data,
807                                 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
808
809                 lpfc_offline_eratt(phba);
810         }
811 }
812
813 /************************************************************************/
814 /*                                                                      */
815 /*    lpfc_handle_latt                                                  */
816 /*    This routine will handle processing a Host Attention              */
817 /*    Link Status event. This will be initialized                       */
818 /*    as a SLI layer callback routine.                                  */
819 /*                                                                      */
820 /************************************************************************/
821 void
822 lpfc_handle_latt(struct lpfc_hba *phba)
823 {
824         struct lpfc_vport *vport = phba->pport;
825         struct lpfc_sli   *psli = &phba->sli;
826         LPFC_MBOXQ_t *pmb;
827         volatile uint32_t control;
828         struct lpfc_dmabuf *mp;
829         int rc = 0;
830
831         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
832         if (!pmb) {
833                 rc = 1;
834                 goto lpfc_handle_latt_err_exit;
835         }
836
837         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
838         if (!mp) {
839                 rc = 2;
840                 goto lpfc_handle_latt_free_pmb;
841         }
842
843         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
844         if (!mp->virt) {
845                 rc = 3;
846                 goto lpfc_handle_latt_free_mp;
847         }
848
849         /* Cleanup any outstanding ELS commands */
850         lpfc_els_flush_all_cmd(phba);
851
852         psli->slistat.link_event++;
853         lpfc_read_la(phba, pmb, mp);
854         pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
855         pmb->vport = vport;
856         /* Block ELS IOCBs until we have processed this mbox command */
857         phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
858         rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
859         if (rc == MBX_NOT_FINISHED) {
860                 rc = 4;
861                 goto lpfc_handle_latt_free_mbuf;
862         }
863
864         /* Clear Link Attention in HA REG */
865         spin_lock_irq(&phba->hbalock);
866         writel(HA_LATT, phba->HAregaddr);
867         readl(phba->HAregaddr); /* flush */
868         spin_unlock_irq(&phba->hbalock);
869
870         return;
871
872 lpfc_handle_latt_free_mbuf:
873         phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
874         lpfc_mbuf_free(phba, mp->virt, mp->phys);
875 lpfc_handle_latt_free_mp:
876         kfree(mp);
877 lpfc_handle_latt_free_pmb:
878         mempool_free(pmb, phba->mbox_mem_pool);
879 lpfc_handle_latt_err_exit:
880         /* Enable Link attention interrupts */
881         spin_lock_irq(&phba->hbalock);
882         psli->sli_flag |= LPFC_PROCESS_LA;
883         control = readl(phba->HCregaddr);
884         control |= HC_LAINT_ENA;
885         writel(control, phba->HCregaddr);
886         readl(phba->HCregaddr); /* flush */
887
888         /* Clear Link Attention in HA REG */
889         writel(HA_LATT, phba->HAregaddr);
890         readl(phba->HAregaddr); /* flush */
891         spin_unlock_irq(&phba->hbalock);
892         lpfc_linkdown(phba);
893         phba->link_state = LPFC_HBA_ERROR;
894
895         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
896                      "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
897
898         return;
899 }
900
901 /************************************************************************/
902 /*                                                                      */
903 /*   lpfc_parse_vpd                                                     */
904 /*   This routine will parse the VPD data                               */
905 /*                                                                      */
906 /************************************************************************/
907 static int
908 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
909 {
910         uint8_t lenlo, lenhi;
911         int Length;
912         int i, j;
913         int finished = 0;
914         int index = 0;
915
916         if (!vpd)
917                 return 0;
918
919         /* Vital Product */
920         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
921                         "0455 Vital Product Data: x%x x%x x%x x%x\n",
922                         (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
923                         (uint32_t) vpd[3]);
924         while (!finished && (index < (len - 4))) {
925                 switch (vpd[index]) {
926                 case 0x82:
927                 case 0x91:
928                         index += 1;
929                         lenlo = vpd[index];
930                         index += 1;
931                         lenhi = vpd[index];
932                         index += 1;
933                         i = ((((unsigned short)lenhi) << 8) + lenlo);
934                         index += i;
935                         break;
936                 case 0x90:
937                         index += 1;
938                         lenlo = vpd[index];
939                         index += 1;
940                         lenhi = vpd[index];
941                         index += 1;
942                         Length = ((((unsigned short)lenhi) << 8) + lenlo);
943                         if (Length > len - index)
944                                 Length = len - index;
945                         while (Length > 0) {
946                         /* Look for Serial Number */
947                         if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
948                                 index += 2;
949                                 i = vpd[index];
950                                 index += 1;
951                                 j = 0;
952                                 Length -= (3+i);
953                                 while(i--) {
954                                         phba->SerialNumber[j++] = vpd[index++];
955                                         if (j == 31)
956                                                 break;
957                                 }
958                                 phba->SerialNumber[j] = 0;
959                                 continue;
960                         }
961                         else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
962                                 phba->vpd_flag |= VPD_MODEL_DESC;
963                                 index += 2;
964                                 i = vpd[index];
965                                 index += 1;
966                                 j = 0;
967                                 Length -= (3+i);
968                                 while(i--) {
969                                         phba->ModelDesc[j++] = vpd[index++];
970                                         if (j == 255)
971                                                 break;
972                                 }
973                                 phba->ModelDesc[j] = 0;
974                                 continue;
975                         }
976                         else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
977                                 phba->vpd_flag |= VPD_MODEL_NAME;
978                                 index += 2;
979                                 i = vpd[index];
980                                 index += 1;
981                                 j = 0;
982                                 Length -= (3+i);
983                                 while(i--) {
984                                         phba->ModelName[j++] = vpd[index++];
985                                         if (j == 79)
986                                                 break;
987                                 }
988                                 phba->ModelName[j] = 0;
989                                 continue;
990                         }
991                         else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
992                                 phba->vpd_flag |= VPD_PROGRAM_TYPE;
993                                 index += 2;
994                                 i = vpd[index];
995                                 index += 1;
996                                 j = 0;
997                                 Length -= (3+i);
998                                 while(i--) {
999                                         phba->ProgramType[j++] = vpd[index++];
1000                                         if (j == 255)
1001                                                 break;
1002                                 }
1003                                 phba->ProgramType[j] = 0;
1004                                 continue;
1005                         }
1006                         else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1007                                 phba->vpd_flag |= VPD_PORT;
1008                                 index += 2;
1009                                 i = vpd[index];
1010                                 index += 1;
1011                                 j = 0;
1012                                 Length -= (3+i);
1013                                 while(i--) {
1014                                 phba->Port[j++] = vpd[index++];
1015                                 if (j == 19)
1016                                         break;
1017                                 }
1018                                 phba->Port[j] = 0;
1019                                 continue;
1020                         }
1021                         else {
1022                                 index += 2;
1023                                 i = vpd[index];
1024                                 index += 1;
1025                                 index += i;
1026                                 Length -= (3 + i);
1027                         }
1028                 }
1029                 finished = 0;
1030                 break;
1031                 case 0x78:
1032                         finished = 1;
1033                         break;
1034                 default:
1035                         index ++;
1036                         break;
1037                 }
1038         }
1039
1040         return(1);
1041 }
1042
1043 static void
1044 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1045 {
1046         lpfc_vpd_t *vp;
1047         uint16_t dev_id = phba->pcidev->device;
1048         int max_speed;
1049         struct {
1050                 char * name;
1051                 int    max_speed;
1052                 char * bus;
1053         } m = {"<Unknown>", 0, ""};
1054
1055         if (mdp && mdp[0] != '\0'
1056                 && descp && descp[0] != '\0')
1057                 return;
1058
1059         if (phba->lmt & LMT_10Gb)
1060                 max_speed = 10;
1061         else if (phba->lmt & LMT_8Gb)
1062                 max_speed = 8;
1063         else if (phba->lmt & LMT_4Gb)
1064                 max_speed = 4;
1065         else if (phba->lmt & LMT_2Gb)
1066                 max_speed = 2;
1067         else
1068                 max_speed = 1;
1069
1070         vp = &phba->vpd;
1071
1072         switch (dev_id) {
1073         case PCI_DEVICE_ID_FIREFLY:
1074                 m = (typeof(m)){"LP6000", max_speed, "PCI"};
1075                 break;
1076         case PCI_DEVICE_ID_SUPERFLY:
1077                 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1078                         m = (typeof(m)){"LP7000", max_speed,  "PCI"};
1079                 else
1080                         m = (typeof(m)){"LP7000E", max_speed, "PCI"};
1081                 break;
1082         case PCI_DEVICE_ID_DRAGONFLY:
1083                 m = (typeof(m)){"LP8000", max_speed, "PCI"};
1084                 break;
1085         case PCI_DEVICE_ID_CENTAUR:
1086                 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1087                         m = (typeof(m)){"LP9002", max_speed, "PCI"};
1088                 else
1089                         m = (typeof(m)){"LP9000", max_speed, "PCI"};
1090                 break;
1091         case PCI_DEVICE_ID_RFLY:
1092                 m = (typeof(m)){"LP952", max_speed, "PCI"};
1093                 break;
1094         case PCI_DEVICE_ID_PEGASUS:
1095                 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
1096                 break;
1097         case PCI_DEVICE_ID_THOR:
1098                 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
1099                 break;
1100         case PCI_DEVICE_ID_VIPER:
1101                 m = (typeof(m)){"LPX1000", max_speed,  "PCI-X"};
1102                 break;
1103         case PCI_DEVICE_ID_PFLY:
1104                 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
1105                 break;
1106         case PCI_DEVICE_ID_TFLY:
1107                 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
1108                 break;
1109         case PCI_DEVICE_ID_HELIOS:
1110                 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
1111                 break;
1112         case PCI_DEVICE_ID_HELIOS_SCSP:
1113                 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
1114                 break;
1115         case PCI_DEVICE_ID_HELIOS_DCSP:
1116                 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
1117                 break;
1118         case PCI_DEVICE_ID_NEPTUNE:
1119                 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
1120                 break;
1121         case PCI_DEVICE_ID_NEPTUNE_SCSP:
1122                 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
1123                 break;
1124         case PCI_DEVICE_ID_NEPTUNE_DCSP:
1125                 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
1126                 break;
1127         case PCI_DEVICE_ID_BMID:
1128                 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
1129                 break;
1130         case PCI_DEVICE_ID_BSMB:
1131                 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
1132                 break;
1133         case PCI_DEVICE_ID_ZEPHYR:
1134                 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
1135                 break;
1136         case PCI_DEVICE_ID_ZEPHYR_SCSP:
1137                 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
1138                 break;
1139         case PCI_DEVICE_ID_ZEPHYR_DCSP:
1140                 m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
1141                 break;
1142         case PCI_DEVICE_ID_ZMID:
1143                 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
1144                 break;
1145         case PCI_DEVICE_ID_ZSMB:
1146                 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
1147                 break;
1148         case PCI_DEVICE_ID_LP101:
1149                 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
1150                 break;
1151         case PCI_DEVICE_ID_LP10000S:
1152                 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
1153                 break;
1154         case PCI_DEVICE_ID_LP11000S:
1155                 m = (typeof(m)){"LP11000-S", max_speed,
1156                         "PCI-X2"};
1157                 break;
1158         case PCI_DEVICE_ID_LPE11000S:
1159                 m = (typeof(m)){"LPe11000-S", max_speed,
1160                         "PCIe"};
1161                 break;
1162         case PCI_DEVICE_ID_SAT:
1163                 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1164                 break;
1165         case PCI_DEVICE_ID_SAT_MID:
1166                 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1167                 break;
1168         case PCI_DEVICE_ID_SAT_SMB:
1169                 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1170                 break;
1171         case PCI_DEVICE_ID_SAT_DCSP:
1172                 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1173                 break;
1174         case PCI_DEVICE_ID_SAT_SCSP:
1175                 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1176                 break;
1177         case PCI_DEVICE_ID_SAT_S:
1178                 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1179                 break;
1180         default:
1181                 m = (typeof(m)){ NULL };
1182                 break;
1183         }
1184
1185         if (mdp && mdp[0] == '\0')
1186                 snprintf(mdp, 79,"%s", m.name);
1187         if (descp && descp[0] == '\0')
1188                 snprintf(descp, 255,
1189                          "Emulex %s %dGb %s Fibre Channel Adapter",
1190                          m.name, m.max_speed, m.bus);
1191 }
1192
1193 /**************************************************/
1194 /*   lpfc_post_buffer                             */
1195 /*                                                */
1196 /*   This routine will post count buffers to the  */
1197 /*   ring with the QUE_RING_BUF_CN command. This  */
1198 /*   allows 3 buffers / command to be posted.     */
1199 /*   Returns the number of buffers NOT posted.    */
1200 /**************************************************/
1201 int
1202 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
1203 {
1204         IOCB_t *icmd;
1205         struct lpfc_iocbq *iocb;
1206         struct lpfc_dmabuf *mp1, *mp2;
1207
1208         cnt += pring->missbufcnt;
1209
1210         /* While there are buffers to post */
1211         while (cnt > 0) {
1212                 /* Allocate buffer for  command iocb */
1213                 iocb = lpfc_sli_get_iocbq(phba);
1214                 if (iocb == NULL) {
1215                         pring->missbufcnt = cnt;
1216                         return cnt;
1217                 }
1218                 icmd = &iocb->iocb;
1219
1220                 /* 2 buffers can be posted per command */
1221                 /* Allocate buffer to post */
1222                 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1223                 if (mp1)
1224                     mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1225                 if (!mp1 || !mp1->virt) {
1226                         kfree(mp1);
1227                         lpfc_sli_release_iocbq(phba, iocb);
1228                         pring->missbufcnt = cnt;
1229                         return cnt;
1230                 }
1231
1232                 INIT_LIST_HEAD(&mp1->list);
1233                 /* Allocate buffer to post */
1234                 if (cnt > 1) {
1235                         mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1236                         if (mp2)
1237                                 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1238                                                             &mp2->phys);
1239                         if (!mp2 || !mp2->virt) {
1240                                 kfree(mp2);
1241                                 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1242                                 kfree(mp1);
1243                                 lpfc_sli_release_iocbq(phba, iocb);
1244                                 pring->missbufcnt = cnt;
1245                                 return cnt;
1246                         }
1247
1248                         INIT_LIST_HEAD(&mp2->list);
1249                 } else {
1250                         mp2 = NULL;
1251                 }
1252
1253                 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1254                 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1255                 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1256                 icmd->ulpBdeCount = 1;
1257                 cnt--;
1258                 if (mp2) {
1259                         icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1260                         icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1261                         icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1262                         cnt--;
1263                         icmd->ulpBdeCount = 2;
1264                 }
1265
1266                 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1267                 icmd->ulpLe = 1;
1268
1269                 if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
1270                         lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1271                         kfree(mp1);
1272                         cnt++;
1273                         if (mp2) {
1274                                 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1275                                 kfree(mp2);
1276                                 cnt++;
1277                         }
1278                         lpfc_sli_release_iocbq(phba, iocb);
1279                         pring->missbufcnt = cnt;
1280                         return cnt;
1281                 }
1282                 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
1283                 if (mp2)
1284                         lpfc_sli_ringpostbuf_put(phba, pring, mp2);
1285         }
1286         pring->missbufcnt = 0;
1287         return 0;
1288 }
1289
1290 /************************************************************************/
1291 /*                                                                      */
1292 /*   lpfc_post_rcv_buf                                                  */
1293 /*   This routine post initial rcv buffers to the configured rings      */
1294 /*                                                                      */
1295 /************************************************************************/
1296 static int
1297 lpfc_post_rcv_buf(struct lpfc_hba *phba)
1298 {
1299         struct lpfc_sli *psli = &phba->sli;
1300
1301         /* Ring 0, ELS / CT buffers */
1302         lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
1303         /* Ring 2 - FCP no buffers needed */
1304
1305         return 0;
1306 }
1307
1308 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1309
1310 /************************************************************************/
1311 /*                                                                      */
1312 /*   lpfc_sha_init                                                      */
1313 /*                                                                      */
1314 /************************************************************************/
1315 static void
1316 lpfc_sha_init(uint32_t * HashResultPointer)
1317 {
1318         HashResultPointer[0] = 0x67452301;
1319         HashResultPointer[1] = 0xEFCDAB89;
1320         HashResultPointer[2] = 0x98BADCFE;
1321         HashResultPointer[3] = 0x10325476;
1322         HashResultPointer[4] = 0xC3D2E1F0;
1323 }
1324
1325 /************************************************************************/
1326 /*                                                                      */
1327 /*   lpfc_sha_iterate                                                   */
1328 /*                                                                      */
1329 /************************************************************************/
1330 static void
1331 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1332 {
1333         int t;
1334         uint32_t TEMP;
1335         uint32_t A, B, C, D, E;
1336         t = 16;
1337         do {
1338                 HashWorkingPointer[t] =
1339                     S(1,
1340                       HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1341                                                                      8] ^
1342                       HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1343         } while (++t <= 79);
1344         t = 0;
1345         A = HashResultPointer[0];
1346         B = HashResultPointer[1];
1347         C = HashResultPointer[2];
1348         D = HashResultPointer[3];
1349         E = HashResultPointer[4];
1350
1351         do {
1352                 if (t < 20) {
1353                         TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1354                 } else if (t < 40) {
1355                         TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1356                 } else if (t < 60) {
1357                         TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1358                 } else {
1359                         TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1360                 }
1361                 TEMP += S(5, A) + E + HashWorkingPointer[t];
1362                 E = D;
1363                 D = C;
1364                 C = S(30, B);
1365                 B = A;
1366                 A = TEMP;
1367         } while (++t <= 79);
1368
1369         HashResultPointer[0] += A;
1370         HashResultPointer[1] += B;
1371         HashResultPointer[2] += C;
1372         HashResultPointer[3] += D;
1373         HashResultPointer[4] += E;
1374
1375 }
1376
1377 /************************************************************************/
1378 /*                                                                      */
1379 /*   lpfc_challenge_key                                                 */
1380 /*                                                                      */
1381 /************************************************************************/
1382 static void
1383 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1384 {
1385         *HashWorking = (*RandomChallenge ^ *HashWorking);
1386 }
1387
1388 /************************************************************************/
1389 /*                                                                      */
1390 /*   lpfc_hba_init                                                      */
1391 /*                                                                      */
1392 /************************************************************************/
1393 void
1394 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1395 {
1396         int t;
1397         uint32_t *HashWorking;
1398         uint32_t *pwwnn = (uint32_t *) phba->wwnn;
1399
1400         HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
1401         if (!HashWorking)
1402                 return;
1403
1404         HashWorking[0] = HashWorking[78] = *pwwnn++;
1405         HashWorking[1] = HashWorking[79] = *pwwnn;
1406
1407         for (t = 0; t < 7; t++)
1408                 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1409
1410         lpfc_sha_init(hbainit);
1411         lpfc_sha_iterate(hbainit, HashWorking);
1412         kfree(HashWorking);
1413 }
1414
1415 void
1416 lpfc_cleanup(struct lpfc_vport *vport)
1417 {
1418         struct lpfc_hba   *phba = vport->phba;
1419         struct lpfc_nodelist *ndlp, *next_ndlp;
1420         int i = 0;
1421
1422         if (phba->link_state > LPFC_LINK_DOWN)
1423                 lpfc_port_link_failure(vport);
1424
1425         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1426                 if (!NLP_CHK_NODE_ACT(ndlp)) {
1427                         ndlp = lpfc_enable_node(vport, ndlp,
1428                                                 NLP_STE_UNUSED_NODE);
1429                         if (!ndlp)
1430                                 continue;
1431                         spin_lock_irq(&phba->ndlp_lock);
1432                         NLP_SET_FREE_REQ(ndlp);
1433                         spin_unlock_irq(&phba->ndlp_lock);
1434                         /* Trigger the release of the ndlp memory */
1435                         lpfc_nlp_put(ndlp);
1436                         continue;
1437                 }
1438                 spin_lock_irq(&phba->ndlp_lock);
1439                 if (NLP_CHK_FREE_REQ(ndlp)) {
1440                         /* The ndlp should not be in memory free mode already */
1441                         spin_unlock_irq(&phba->ndlp_lock);
1442                         continue;
1443                 } else
1444                         /* Indicate request for freeing ndlp memory */
1445                         NLP_SET_FREE_REQ(ndlp);
1446                 spin_unlock_irq(&phba->ndlp_lock);
1447
1448                 if (vport->port_type != LPFC_PHYSICAL_PORT &&
1449                     ndlp->nlp_DID == Fabric_DID) {
1450                         /* Just free up ndlp with Fabric_DID for vports */
1451                         lpfc_nlp_put(ndlp);
1452                         continue;
1453                 }
1454
1455                 if (ndlp->nlp_type & NLP_FABRIC)
1456                         lpfc_disc_state_machine(vport, ndlp, NULL,
1457                                         NLP_EVT_DEVICE_RECOVERY);
1458
1459                 lpfc_disc_state_machine(vport, ndlp, NULL,
1460                                              NLP_EVT_DEVICE_RM);
1461
1462                 /* nlp_type zero is not defined, nlp_flag zero also not defined,
1463                  * nlp_state is unused, this happens when
1464                  * an initiator has logged
1465                  * into us so cleanup this ndlp.
1466                  */
1467                 if ((ndlp->nlp_type == 0) && (ndlp->nlp_flag == 0) &&
1468                         (ndlp->nlp_state == 0))
1469                         lpfc_nlp_put(ndlp);
1470         }
1471
1472         /* At this point, ALL ndlp's should be gone
1473          * because of the previous NLP_EVT_DEVICE_RM.
1474          * Lets wait for this to happen, if needed.
1475          */
1476         while (!list_empty(&vport->fc_nodes)) {
1477
1478                 if (i++ > 3000) {
1479                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1480                                 "0233 Nodelist not empty\n");
1481                         list_for_each_entry_safe(ndlp, next_ndlp,
1482                                                 &vport->fc_nodes, nlp_listp) {
1483                                 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
1484                                                 LOG_NODE,
1485                                                 "0282: did:x%x ndlp:x%p "
1486                                                 "usgmap:x%x refcnt:%d\n",
1487                                                 ndlp->nlp_DID, (void *)ndlp,
1488                                                 ndlp->nlp_usg_map,
1489                                                 atomic_read(
1490                                                         &ndlp->kref.refcount));
1491                         }
1492                         break;
1493                 }
1494
1495                 /* Wait for any activity on ndlps to settle */
1496                 msleep(10);
1497         }
1498         return;
1499 }
1500
1501 void
1502 lpfc_stop_vport_timers(struct lpfc_vport *vport)
1503 {
1504         del_timer_sync(&vport->els_tmofunc);
1505         del_timer_sync(&vport->fc_fdmitmo);
1506         lpfc_can_disctmo(vport);
1507         return;
1508 }
1509
1510 static void
1511 lpfc_stop_phba_timers(struct lpfc_hba *phba)
1512 {
1513         del_timer_sync(&phba->fcp_poll_timer);
1514         lpfc_stop_vport_timers(phba->pport);
1515         del_timer_sync(&phba->sli.mbox_tmo);
1516         del_timer_sync(&phba->fabric_block_timer);
1517         phba->hb_outstanding = 0;
1518         del_timer_sync(&phba->hb_tmofunc);
1519         return;
1520 }
1521
1522 static void
1523 lpfc_block_mgmt_io(struct lpfc_hba * phba)
1524 {
1525         unsigned long iflag;
1526
1527         spin_lock_irqsave(&phba->hbalock, iflag);
1528         phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1529         spin_unlock_irqrestore(&phba->hbalock, iflag);
1530 }
1531
1532 int
1533 lpfc_online(struct lpfc_hba *phba)
1534 {
1535         struct lpfc_vport *vport = phba->pport;
1536         struct lpfc_vport **vports;
1537         int i;
1538
1539         if (!phba)
1540                 return 0;
1541
1542         if (!(vport->fc_flag & FC_OFFLINE_MODE))
1543                 return 0;
1544
1545         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1546                         "0458 Bring Adapter online\n");
1547
1548         lpfc_block_mgmt_io(phba);
1549
1550         if (!lpfc_sli_queue_setup(phba)) {
1551                 lpfc_unblock_mgmt_io(phba);
1552                 return 1;
1553         }
1554
1555         if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
1556                 lpfc_unblock_mgmt_io(phba);
1557                 return 1;
1558         }
1559
1560         vports = lpfc_create_vport_work_array(phba);
1561         if (vports != NULL)
1562                 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1563                         struct Scsi_Host *shost;
1564                         shost = lpfc_shost_from_vport(vports[i]);
1565                         spin_lock_irq(shost->host_lock);
1566                         vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
1567                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
1568                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1569                         spin_unlock_irq(shost->host_lock);
1570                 }
1571                 lpfc_destroy_vport_work_array(phba, vports);
1572
1573         lpfc_unblock_mgmt_io(phba);
1574         return 0;
1575 }
1576
1577 void
1578 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1579 {
1580         unsigned long iflag;
1581
1582         spin_lock_irqsave(&phba->hbalock, iflag);
1583         phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1584         spin_unlock_irqrestore(&phba->hbalock, iflag);
1585 }
1586
1587 void
1588 lpfc_offline_prep(struct lpfc_hba * phba)
1589 {
1590         struct lpfc_vport *vport = phba->pport;
1591         struct lpfc_nodelist  *ndlp, *next_ndlp;
1592         struct lpfc_vport **vports;
1593         int i;
1594
1595         if (vport->fc_flag & FC_OFFLINE_MODE)
1596                 return;
1597
1598         lpfc_block_mgmt_io(phba);
1599
1600         lpfc_linkdown(phba);
1601
1602         /* Issue an unreg_login to all nodes on all vports */
1603         vports = lpfc_create_vport_work_array(phba);
1604         if (vports != NULL) {
1605                 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1606                         struct Scsi_Host *shost;
1607
1608                         if (vports[i]->load_flag & FC_UNLOADING)
1609                                 continue;
1610                         shost = lpfc_shost_from_vport(vports[i]);
1611                         list_for_each_entry_safe(ndlp, next_ndlp,
1612                                                  &vports[i]->fc_nodes,
1613                                                  nlp_listp) {
1614                                 if (!NLP_CHK_NODE_ACT(ndlp))
1615                                         continue;
1616                                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1617                                         continue;
1618                                 if (ndlp->nlp_type & NLP_FABRIC) {
1619                                         lpfc_disc_state_machine(vports[i], ndlp,
1620                                                 NULL, NLP_EVT_DEVICE_RECOVERY);
1621                                         lpfc_disc_state_machine(vports[i], ndlp,
1622                                                 NULL, NLP_EVT_DEVICE_RM);
1623                                 }
1624                                 spin_lock_irq(shost->host_lock);
1625                                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1626                                 spin_unlock_irq(shost->host_lock);
1627                                 lpfc_unreg_rpi(vports[i], ndlp);
1628                         }
1629                 }
1630         }
1631         lpfc_destroy_vport_work_array(phba, vports);
1632
1633         lpfc_sli_flush_mbox_queue(phba);
1634 }
1635
1636 void
1637 lpfc_offline(struct lpfc_hba *phba)
1638 {
1639         struct Scsi_Host  *shost;
1640         struct lpfc_vport **vports;
1641         int i;
1642
1643         if (phba->pport->fc_flag & FC_OFFLINE_MODE)
1644                 return;
1645
1646         /* stop all timers associated with this hba */
1647         lpfc_stop_phba_timers(phba);
1648         vports = lpfc_create_vport_work_array(phba);
1649         if (vports != NULL)
1650                 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
1651                         lpfc_stop_vport_timers(vports[i]);
1652         lpfc_destroy_vport_work_array(phba, vports);
1653         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1654                         "0460 Bring Adapter offline\n");
1655         /* Bring down the SLI Layer and cleanup.  The HBA is offline
1656            now.  */
1657         lpfc_sli_hba_down(phba);
1658         spin_lock_irq(&phba->hbalock);
1659         phba->work_ha = 0;
1660         spin_unlock_irq(&phba->hbalock);
1661         vports = lpfc_create_vport_work_array(phba);
1662         if (vports != NULL)
1663                 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1664                         shost = lpfc_shost_from_vport(vports[i]);
1665                         spin_lock_irq(shost->host_lock);
1666                         vports[i]->work_port_events = 0;
1667                         vports[i]->fc_flag |= FC_OFFLINE_MODE;
1668                         spin_unlock_irq(shost->host_lock);
1669                 }
1670         lpfc_destroy_vport_work_array(phba, vports);
1671 }
1672
1673 /******************************************************************************
1674 * Function name: lpfc_scsi_free
1675 *
1676 * Description: Called from lpfc_pci_remove_one free internal driver resources
1677 *
1678 ******************************************************************************/
1679 static int
1680 lpfc_scsi_free(struct lpfc_hba *phba)
1681 {
1682         struct lpfc_scsi_buf *sb, *sb_next;
1683         struct lpfc_iocbq *io, *io_next;
1684
1685         spin_lock_irq(&phba->hbalock);
1686         /* Release all the lpfc_scsi_bufs maintained by this host. */
1687         list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1688                 list_del(&sb->list);
1689                 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
1690                               sb->dma_handle);
1691                 kfree(sb);
1692                 phba->total_scsi_bufs--;
1693         }
1694
1695         /* Release all the lpfc_iocbq entries maintained by this host. */
1696         list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1697                 list_del(&io->list);
1698                 kfree(io);
1699                 phba->total_iocbq_bufs--;
1700         }
1701
1702         spin_unlock_irq(&phba->hbalock);
1703
1704         return 0;
1705 }
1706
1707 struct lpfc_vport *
1708 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
1709 {
1710         struct lpfc_vport *vport;
1711         struct Scsi_Host  *shost;
1712         int error = 0;
1713
1714         if (dev != &phba->pcidev->dev)
1715                 shost = scsi_host_alloc(&lpfc_vport_template,
1716                                         sizeof(struct lpfc_vport));
1717         else
1718                 shost = scsi_host_alloc(&lpfc_template,
1719                                         sizeof(struct lpfc_vport));
1720         if (!shost)
1721                 goto out;
1722
1723         vport = (struct lpfc_vport *) shost->hostdata;
1724         vport->phba = phba;
1725         vport->load_flag |= FC_LOADING;
1726         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1727         vport->fc_rscn_flush = 0;
1728
1729         lpfc_get_vport_cfgparam(vport);
1730         shost->unique_id = instance;
1731         shost->max_id = LPFC_MAX_TARGET;
1732         shost->max_lun = vport->cfg_max_luns;
1733         shost->this_id = -1;
1734         shost->max_cmd_len = 16;
1735         /*
1736          * Set initial can_queue value since 0 is no longer supported and
1737          * scsi_add_host will fail. This will be adjusted later based on the
1738          * max xri value determined in hba setup.
1739          */
1740         shost->can_queue = phba->cfg_hba_queue_depth - 10;
1741         if (dev != &phba->pcidev->dev) {
1742                 shost->transportt = lpfc_vport_transport_template;
1743                 vport->port_type = LPFC_NPIV_PORT;
1744         } else {
1745                 shost->transportt = lpfc_transport_template;
1746                 vport->port_type = LPFC_PHYSICAL_PORT;
1747         }
1748
1749         /* Initialize all internally managed lists. */
1750         INIT_LIST_HEAD(&vport->fc_nodes);
1751         spin_lock_init(&vport->work_port_lock);
1752
1753         init_timer(&vport->fc_disctmo);
1754         vport->fc_disctmo.function = lpfc_disc_timeout;
1755         vport->fc_disctmo.data = (unsigned long)vport;
1756
1757         init_timer(&vport->fc_fdmitmo);
1758         vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
1759         vport->fc_fdmitmo.data = (unsigned long)vport;
1760
1761         init_timer(&vport->els_tmofunc);
1762         vport->els_tmofunc.function = lpfc_els_timeout;
1763         vport->els_tmofunc.data = (unsigned long)vport;
1764
1765         error = scsi_add_host(shost, dev);
1766         if (error)
1767                 goto out_put_shost;
1768
1769         spin_lock_irq(&phba->hbalock);
1770         list_add_tail(&vport->listentry, &phba->port_list);
1771         spin_unlock_irq(&phba->hbalock);
1772         return vport;
1773
1774 out_put_shost:
1775         scsi_host_put(shost);
1776 out:
1777         return NULL;
1778 }
1779
1780 void
1781 destroy_port(struct lpfc_vport *vport)
1782 {
1783         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1784         struct lpfc_hba  *phba = vport->phba;
1785
1786         kfree(vport->vname);
1787
1788         lpfc_debugfs_terminate(vport);
1789         fc_remove_host(shost);
1790         scsi_remove_host(shost);
1791
1792         spin_lock_irq(&phba->hbalock);
1793         list_del_init(&vport->listentry);
1794         spin_unlock_irq(&phba->hbalock);
1795
1796         lpfc_cleanup(vport);
1797         return;
1798 }
1799
1800 int
1801 lpfc_get_instance(void)
1802 {
1803         int instance = 0;
1804
1805         /* Assign an unused number */
1806         if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
1807                 return -1;
1808         if (idr_get_new(&lpfc_hba_index, NULL, &instance))
1809                 return -1;
1810         return instance;
1811 }
1812
1813 /*
1814  * Note: there is no scan_start function as adapter initialization
1815  * will have asynchronously kicked off the link initialization.
1816  */
1817
1818 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
1819 {
1820         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1821         struct lpfc_hba   *phba = vport->phba;
1822         int stat = 0;
1823
1824         spin_lock_irq(shost->host_lock);
1825
1826         if (vport->load_flag & FC_UNLOADING) {
1827                 stat = 1;
1828                 goto finished;
1829         }
1830         if (time >= 30 * HZ) {
1831                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1832                                 "0461 Scanning longer than 30 "
1833                                 "seconds.  Continuing initialization\n");
1834                 stat = 1;
1835                 goto finished;
1836         }
1837         if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
1838                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1839                                 "0465 Link down longer than 15 "
1840                                 "seconds.  Continuing initialization\n");
1841                 stat = 1;
1842                 goto finished;
1843         }
1844
1845         if (vport->port_state != LPFC_VPORT_READY)
1846                 goto finished;
1847         if (vport->num_disc_nodes || vport->fc_prli_sent)
1848                 goto finished;
1849         if (vport->fc_map_cnt == 0 && time < 2 * HZ)
1850                 goto finished;
1851         if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
1852                 goto finished;
1853
1854         stat = 1;
1855
1856 finished:
1857         spin_unlock_irq(shost->host_lock);
1858         return stat;
1859 }
1860
1861 void lpfc_host_attrib_init(struct Scsi_Host *shost)
1862 {
1863         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1864         struct lpfc_hba   *phba = vport->phba;
1865         /*
1866          * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
1867          */
1868
1869         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
1870         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
1871         fc_host_supported_classes(shost) = FC_COS_CLASS3;
1872
1873         memset(fc_host_supported_fc4s(shost), 0,
1874                sizeof(fc_host_supported_fc4s(shost)));
1875         fc_host_supported_fc4s(shost)[2] = 1;
1876         fc_host_supported_fc4s(shost)[7] = 1;
1877
1878         lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
1879                                  sizeof fc_host_symbolic_name(shost));
1880
1881         fc_host_supported_speeds(shost) = 0;
1882         if (phba->lmt & LMT_10Gb)
1883                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
1884         if (phba->lmt & LMT_8Gb)
1885                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
1886         if (phba->lmt & LMT_4Gb)
1887                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
1888         if (phba->lmt & LMT_2Gb)
1889                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
1890         if (phba->lmt & LMT_1Gb)
1891                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
1892
1893         fc_host_maxframe_size(shost) =
1894                 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
1895                 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
1896
1897         /* This value is also unchanging */
1898         memset(fc_host_active_fc4s(shost), 0,
1899                sizeof(fc_host_active_fc4s(shost)));
1900         fc_host_active_fc4s(shost)[2] = 1;
1901         fc_host_active_fc4s(shost)[7] = 1;
1902
1903         fc_host_max_npiv_vports(shost) = phba->max_vpi;
1904         spin_lock_irq(shost->host_lock);
1905         vport->load_flag &= ~FC_LOADING;
1906         spin_unlock_irq(shost->host_lock);
1907 }
1908
1909 static int
1910 lpfc_enable_msix(struct lpfc_hba *phba)
1911 {
1912         int error;
1913
1914         phba->msix_entries[0].entry = 0;
1915         phba->msix_entries[0].vector = 0;
1916
1917         error = pci_enable_msix(phba->pcidev, phba->msix_entries,
1918                                 ARRAY_SIZE(phba->msix_entries));
1919         if (error) {
1920                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1921                                 "0420 Enable MSI-X failed (%d), continuing "
1922                                 "with MSI\n", error);
1923                 pci_disable_msix(phba->pcidev);
1924                 return error;
1925         }
1926
1927         error = request_irq(phba->msix_entries[0].vector, lpfc_intr_handler, 0,
1928                             LPFC_DRIVER_NAME, phba);
1929         if (error) {
1930                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1931                                 "0421 MSI-X request_irq failed (%d), "
1932                                 "continuing with MSI\n", error);
1933                 pci_disable_msix(phba->pcidev);
1934         }
1935         return error;
1936 }
1937
1938 static void
1939 lpfc_disable_msix(struct lpfc_hba *phba)
1940 {
1941         free_irq(phba->msix_entries[0].vector, phba);
1942         pci_disable_msix(phba->pcidev);
1943 }
1944
1945 static int __devinit
1946 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
1947 {
1948         struct lpfc_vport *vport = NULL;
1949         struct lpfc_hba   *phba;
1950         struct lpfc_sli   *psli;
1951         struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
1952         struct Scsi_Host  *shost = NULL;
1953         void *ptr;
1954         unsigned long bar0map_len, bar2map_len;
1955         int error = -ENODEV, retval;
1956         int  i, hbq_count;
1957         uint16_t iotag;
1958         int bars = pci_select_bars(pdev, IORESOURCE_MEM);
1959
1960         if (pci_enable_device_mem(pdev))
1961                 goto out;
1962         if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
1963                 goto out_disable_device;
1964
1965         phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
1966         if (!phba)
1967                 goto out_release_regions;
1968
1969         spin_lock_init(&phba->hbalock);
1970
1971         /* Initialize ndlp management spinlock */
1972         spin_lock_init(&phba->ndlp_lock);
1973
1974         phba->pcidev = pdev;
1975
1976         /* Assign an unused board number */
1977         if ((phba->brd_no = lpfc_get_instance()) < 0)
1978                 goto out_free_phba;
1979
1980         INIT_LIST_HEAD(&phba->port_list);
1981         /*
1982          * Get all the module params for configuring this host and then
1983          * establish the host.
1984          */
1985         lpfc_get_cfgparam(phba);
1986         phba->max_vpi = LPFC_MAX_VPI;
1987
1988         /* Initialize timers used by driver */
1989         init_timer(&phba->hb_tmofunc);
1990         phba->hb_tmofunc.function = lpfc_hb_timeout;
1991         phba->hb_tmofunc.data = (unsigned long)phba;
1992
1993         psli = &phba->sli;
1994         init_timer(&psli->mbox_tmo);
1995         psli->mbox_tmo.function = lpfc_mbox_timeout;
1996         psli->mbox_tmo.data = (unsigned long) phba;
1997         init_timer(&phba->fcp_poll_timer);
1998         phba->fcp_poll_timer.function = lpfc_poll_timeout;
1999         phba->fcp_poll_timer.data = (unsigned long) phba;
2000         init_timer(&phba->fabric_block_timer);
2001         phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
2002         phba->fabric_block_timer.data = (unsigned long) phba;
2003
2004         pci_set_master(pdev);
2005         pci_try_set_mwi(pdev);
2006
2007         if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
2008                 if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
2009                         goto out_idr_remove;
2010
2011         /*
2012          * Get the bus address of Bar0 and Bar2 and the number of bytes
2013          * required by each mapping.
2014          */
2015         phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
2016         bar0map_len        = pci_resource_len(phba->pcidev, 0);
2017
2018         phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
2019         bar2map_len        = pci_resource_len(phba->pcidev, 2);
2020
2021         /* Map HBA SLIM to a kernel virtual address. */
2022         phba->slim_memmap_p      = ioremap(phba->pci_bar0_map, bar0map_len);
2023         if (!phba->slim_memmap_p) {
2024                 error = -ENODEV;
2025                 dev_printk(KERN_ERR, &pdev->dev,
2026                            "ioremap failed for SLIM memory.\n");
2027                 goto out_idr_remove;
2028         }
2029
2030         /* Map HBA Control Registers to a kernel virtual address. */
2031         phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
2032         if (!phba->ctrl_regs_memmap_p) {
2033                 error = -ENODEV;
2034                 dev_printk(KERN_ERR, &pdev->dev,
2035                            "ioremap failed for HBA control registers.\n");
2036                 goto out_iounmap_slim;
2037         }
2038
2039         /* Allocate memory for SLI-2 structures */
2040         phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
2041                                           &phba->slim2p_mapping, GFP_KERNEL);
2042         if (!phba->slim2p)
2043                 goto out_iounmap;
2044
2045         memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
2046
2047         phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
2048                                                  lpfc_sli_hbq_size(),
2049                                                  &phba->hbqslimp.phys,
2050                                                  GFP_KERNEL);
2051         if (!phba->hbqslimp.virt)
2052                 goto out_free_slim;
2053
2054         hbq_count = lpfc_sli_hbq_count();
2055         ptr = phba->hbqslimp.virt;
2056         for (i = 0; i < hbq_count; ++i) {
2057                 phba->hbqs[i].hbq_virt = ptr;
2058                 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
2059                 ptr += (lpfc_hbq_defs[i]->entry_count *
2060                         sizeof(struct lpfc_hbq_entry));
2061         }
2062         phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
2063         phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer  = lpfc_els_hbq_free;
2064
2065         memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
2066
2067         INIT_LIST_HEAD(&phba->hbqbuf_in_list);
2068
2069         /* Initialize the SLI Layer to run with lpfc HBAs. */
2070         lpfc_sli_setup(phba);
2071         lpfc_sli_queue_setup(phba);
2072
2073         retval = lpfc_mem_alloc(phba);
2074         if (retval) {
2075                 error = retval;
2076                 goto out_free_hbqslimp;
2077         }
2078
2079         /* Initialize and populate the iocb list per host.  */
2080         INIT_LIST_HEAD(&phba->lpfc_iocb_list);
2081         for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
2082                 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
2083                 if (iocbq_entry == NULL) {
2084                         printk(KERN_ERR "%s: only allocated %d iocbs of "
2085                                 "expected %d count. Unloading driver.\n",
2086                                 __func__, i, LPFC_IOCB_LIST_CNT);
2087                         error = -ENOMEM;
2088                         goto out_free_iocbq;
2089                 }
2090
2091                 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
2092                 if (iotag == 0) {
2093                         kfree (iocbq_entry);
2094                         printk(KERN_ERR "%s: failed to allocate IOTAG. "
2095                                "Unloading driver.\n",
2096                                 __func__);
2097                         error = -ENOMEM;
2098                         goto out_free_iocbq;
2099                 }
2100
2101                 spin_lock_irq(&phba->hbalock);
2102                 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
2103                 phba->total_iocbq_bufs++;
2104                 spin_unlock_irq(&phba->hbalock);
2105         }
2106
2107         /* Initialize HBA structure */
2108         phba->fc_edtov = FF_DEF_EDTOV;
2109         phba->fc_ratov = FF_DEF_RATOV;
2110         phba->fc_altov = FF_DEF_ALTOV;
2111         phba->fc_arbtov = FF_DEF_ARBTOV;
2112
2113         INIT_LIST_HEAD(&phba->work_list);
2114         phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
2115         phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
2116
2117         /* Initialize the wait queue head for the kernel thread */
2118         init_waitqueue_head(&phba->work_waitq);
2119
2120         /* Startup the kernel thread for this host adapter. */
2121         phba->worker_thread = kthread_run(lpfc_do_work, phba,
2122                                        "lpfc_worker_%d", phba->brd_no);
2123         if (IS_ERR(phba->worker_thread)) {
2124                 error = PTR_ERR(phba->worker_thread);
2125                 goto out_free_iocbq;
2126         }
2127
2128         /* Initialize the list of scsi buffers used by driver for scsi IO. */
2129         spin_lock_init(&phba->scsi_buf_list_lock);
2130         INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
2131
2132         /* Initialize list of fabric iocbs */
2133         INIT_LIST_HEAD(&phba->fabric_iocb_list);
2134
2135         /* Initialize list to save ELS buffers */
2136         INIT_LIST_HEAD(&phba->elsbuf);
2137
2138         vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
2139         if (!vport)
2140                 goto out_kthread_stop;
2141
2142         shost = lpfc_shost_from_vport(vport);
2143         phba->pport = vport;
2144         lpfc_debugfs_initialize(vport);
2145
2146         pci_set_drvdata(pdev, shost);
2147         phba->intr_type = NONE;
2148
2149         if (phba->cfg_use_msi == 2) {
2150                 error = lpfc_enable_msix(phba);
2151                 if (!error)
2152                         phba->intr_type = MSIX;
2153         }
2154
2155         /* Fallback to MSI if MSI-X initialization failed */
2156         if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
2157                 retval = pci_enable_msi(phba->pcidev);
2158                 if (!retval)
2159                         phba->intr_type = MSI;
2160                 else
2161                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2162                                         "0452 Enable MSI failed, continuing "
2163                                         "with IRQ\n");
2164         }
2165
2166         /* MSI-X is the only case the doesn't need to call request_irq */
2167         if (phba->intr_type != MSIX) {
2168                 retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2169                                      IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2170                 if (retval) {
2171                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0451 Enable "
2172                                         "interrupt handler failed\n");
2173                         error = retval;
2174                         goto out_disable_msi;
2175                 } else if (phba->intr_type != MSI)
2176                         phba->intr_type = INTx;
2177         }
2178
2179         phba->MBslimaddr = phba->slim_memmap_p;
2180         phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
2181         phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
2182         phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
2183         phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
2184
2185         if (lpfc_alloc_sysfs_attr(vport)) {
2186                 error = -ENOMEM;
2187                 goto out_free_irq;
2188         }
2189
2190         if (lpfc_sli_hba_setup(phba)) {
2191                 error = -ENODEV;
2192                 goto out_remove_device;
2193         }
2194
2195         /*
2196          * hba setup may have changed the hba_queue_depth so we need to adjust
2197          * the value of can_queue.
2198          */
2199         shost->can_queue = phba->cfg_hba_queue_depth - 10;
2200
2201         lpfc_host_attrib_init(shost);
2202
2203         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2204                 spin_lock_irq(shost->host_lock);
2205                 lpfc_poll_start_timer(phba);
2206                 spin_unlock_irq(shost->host_lock);
2207         }
2208
2209         scsi_scan_host(shost);
2210
2211         return 0;
2212
2213 out_remove_device:
2214         lpfc_free_sysfs_attr(vport);
2215         spin_lock_irq(shost->host_lock);
2216         vport->load_flag |= FC_UNLOADING;
2217         spin_unlock_irq(shost->host_lock);
2218 out_free_irq:
2219         lpfc_stop_phba_timers(phba);
2220         phba->pport->work_port_events = 0;
2221
2222         if (phba->intr_type == MSIX)
2223                 lpfc_disable_msix(phba);
2224         else
2225                 free_irq(phba->pcidev->irq, phba);
2226
2227 out_disable_msi:
2228         if (phba->intr_type == MSI)
2229                 pci_disable_msi(phba->pcidev);
2230         destroy_port(vport);
2231 out_kthread_stop:
2232         kthread_stop(phba->worker_thread);
2233 out_free_iocbq:
2234         list_for_each_entry_safe(iocbq_entry, iocbq_next,
2235                                                 &phba->lpfc_iocb_list, list) {
2236                 kfree(iocbq_entry);
2237                 phba->total_iocbq_bufs--;
2238         }
2239         lpfc_mem_free(phba);
2240 out_free_hbqslimp:
2241         dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2242                           phba->hbqslimp.phys);
2243 out_free_slim:
2244         dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
2245                                                         phba->slim2p_mapping);
2246 out_iounmap:
2247         iounmap(phba->ctrl_regs_memmap_p);
2248 out_iounmap_slim:
2249         iounmap(phba->slim_memmap_p);
2250 out_idr_remove:
2251         idr_remove(&lpfc_hba_index, phba->brd_no);
2252 out_free_phba:
2253         kfree(phba);
2254 out_release_regions:
2255         pci_release_selected_regions(pdev, bars);
2256 out_disable_device:
2257         pci_disable_device(pdev);
2258 out:
2259         pci_set_drvdata(pdev, NULL);
2260         if (shost)
2261                 scsi_host_put(shost);
2262         return error;
2263 }
2264
2265 static void __devexit
2266 lpfc_pci_remove_one(struct pci_dev *pdev)
2267 {
2268         struct Scsi_Host  *shost = pci_get_drvdata(pdev);
2269         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2270         struct lpfc_hba   *phba = vport->phba;
2271         int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2272
2273         spin_lock_irq(&phba->hbalock);
2274         vport->load_flag |= FC_UNLOADING;
2275         spin_unlock_irq(&phba->hbalock);
2276
2277         kfree(vport->vname);
2278         lpfc_free_sysfs_attr(vport);
2279
2280         kthread_stop(phba->worker_thread);
2281
2282         fc_remove_host(shost);
2283         scsi_remove_host(shost);
2284         lpfc_cleanup(vport);
2285
2286         /*
2287          * Bring down the SLI Layer. This step disable all interrupts,
2288          * clears the rings, discards all mailbox commands, and resets
2289          * the HBA.
2290          */
2291         lpfc_sli_hba_down(phba);
2292         lpfc_sli_brdrestart(phba);
2293
2294         lpfc_stop_phba_timers(phba);
2295         spin_lock_irq(&phba->hbalock);
2296         list_del_init(&vport->listentry);
2297         spin_unlock_irq(&phba->hbalock);
2298
2299         lpfc_debugfs_terminate(vport);
2300
2301         if (phba->intr_type == MSIX)
2302                 lpfc_disable_msix(phba);
2303         else {
2304                 free_irq(phba->pcidev->irq, phba);
2305                 if (phba->intr_type == MSI)
2306                         pci_disable_msi(phba->pcidev);
2307         }
2308
2309         pci_set_drvdata(pdev, NULL);
2310         scsi_host_put(shost);
2311
2312         /*
2313          * Call scsi_free before mem_free since scsi bufs are released to their
2314          * corresponding pools here.
2315          */
2316         lpfc_scsi_free(phba);
2317         lpfc_mem_free(phba);
2318
2319         dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2320                           phba->hbqslimp.phys);
2321
2322         /* Free resources associated with SLI2 interface */
2323         dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
2324                           phba->slim2p, phba->slim2p_mapping);
2325
2326         /* unmap adapter SLIM and Control Registers */
2327         iounmap(phba->ctrl_regs_memmap_p);
2328         iounmap(phba->slim_memmap_p);
2329
2330         idr_remove(&lpfc_hba_index, phba->brd_no);
2331
2332         kfree(phba);
2333
2334         pci_release_selected_regions(pdev, bars);
2335         pci_disable_device(pdev);
2336 }
2337
2338 /**
2339  * lpfc_io_error_detected - called when PCI error is detected
2340  * @pdev: Pointer to PCI device
2341  * @state: The current pci conneection state
2342  *
2343  * This function is called after a PCI bus error affecting
2344  * this device has been detected.
2345  */
2346 static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
2347                                 pci_channel_state_t state)
2348 {
2349         struct Scsi_Host *shost = pci_get_drvdata(pdev);
2350         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2351         struct lpfc_sli *psli = &phba->sli;
2352         struct lpfc_sli_ring  *pring;
2353
2354         if (state == pci_channel_io_perm_failure)
2355                 return PCI_ERS_RESULT_DISCONNECT;
2356
2357         pci_disable_device(pdev);
2358         /*
2359          * There may be I/Os dropped by the firmware.
2360          * Error iocb (I/O) on txcmplq and let the SCSI layer
2361          * retry it after re-establishing link.
2362          */
2363         pring = &psli->ring[psli->fcp_ring];
2364         lpfc_sli_abort_iocb_ring(phba, pring);
2365
2366         if (phba->intr_type == MSIX)
2367                 lpfc_disable_msix(phba);
2368         else {
2369                 free_irq(phba->pcidev->irq, phba);
2370                 if (phba->intr_type == MSI)
2371                         pci_disable_msi(phba->pcidev);
2372         }
2373
2374         /* Request a slot reset. */
2375         return PCI_ERS_RESULT_NEED_RESET;
2376 }
2377
2378 /**
2379  * lpfc_io_slot_reset - called after the pci bus has been reset.
2380  * @pdev: Pointer to PCI device
2381  *
2382  * Restart the card from scratch, as if from a cold-boot.
2383  */
2384 static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
2385 {
2386         struct Scsi_Host *shost = pci_get_drvdata(pdev);
2387         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2388         struct lpfc_sli *psli = &phba->sli;
2389         int error, retval;
2390
2391         dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
2392         if (pci_enable_device_mem(pdev)) {
2393                 printk(KERN_ERR "lpfc: Cannot re-enable "
2394                         "PCI device after reset.\n");
2395                 return PCI_ERS_RESULT_DISCONNECT;
2396         }
2397
2398         pci_set_master(pdev);
2399
2400         spin_lock_irq(&phba->hbalock);
2401         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2402         spin_unlock_irq(&phba->hbalock);
2403
2404         /* Enable configured interrupt method */
2405         phba->intr_type = NONE;
2406         if (phba->cfg_use_msi == 2) {
2407                 error = lpfc_enable_msix(phba);
2408                 if (!error)
2409                         phba->intr_type = MSIX;
2410         }
2411
2412         /* Fallback to MSI if MSI-X initialization failed */
2413         if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
2414                 retval = pci_enable_msi(phba->pcidev);
2415                 if (!retval)
2416                         phba->intr_type = MSI;
2417                 else
2418                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2419                                         "0470 Enable MSI failed, continuing "
2420                                         "with IRQ\n");
2421         }
2422
2423         /* MSI-X is the only case the doesn't need to call request_irq */
2424         if (phba->intr_type != MSIX) {
2425                 retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2426                                      IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2427                 if (retval) {
2428                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2429                                         "0471 Enable interrupt handler "
2430                                         "failed\n");
2431                 } else if (phba->intr_type != MSI)
2432                         phba->intr_type = INTx;
2433         }
2434
2435         /* Take device offline; this will perform cleanup */
2436         lpfc_offline(phba);
2437         lpfc_sli_brdrestart(phba);
2438
2439         return PCI_ERS_RESULT_RECOVERED;
2440 }
2441
2442 /**
2443  * lpfc_io_resume - called when traffic can start flowing again.
2444  * @pdev: Pointer to PCI device
2445  *
2446  * This callback is called when the error recovery driver tells us that
2447  * its OK to resume normal operation.
2448  */
2449 static void lpfc_io_resume(struct pci_dev *pdev)
2450 {
2451         struct Scsi_Host *shost = pci_get_drvdata(pdev);
2452         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2453
2454         lpfc_online(phba);
2455 }
2456
2457 static struct pci_device_id lpfc_id_table[] = {
2458         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
2459                 PCI_ANY_ID, PCI_ANY_ID, },
2460         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
2461                 PCI_ANY_ID, PCI_ANY_ID, },
2462         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
2463                 PCI_ANY_ID, PCI_ANY_ID, },
2464         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
2465                 PCI_ANY_ID, PCI_ANY_ID, },
2466         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
2467                 PCI_ANY_ID, PCI_ANY_ID, },
2468         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
2469                 PCI_ANY_ID, PCI_ANY_ID, },
2470         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
2471                 PCI_ANY_ID, PCI_ANY_ID, },
2472         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
2473                 PCI_ANY_ID, PCI_ANY_ID, },
2474         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
2475                 PCI_ANY_ID, PCI_ANY_ID, },
2476         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
2477                 PCI_ANY_ID, PCI_ANY_ID, },
2478         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
2479                 PCI_ANY_ID, PCI_ANY_ID, },
2480         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
2481                 PCI_ANY_ID, PCI_ANY_ID, },
2482         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
2483                 PCI_ANY_ID, PCI_ANY_ID, },
2484         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
2485                 PCI_ANY_ID, PCI_ANY_ID, },
2486         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
2487                 PCI_ANY_ID, PCI_ANY_ID, },
2488         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
2489                 PCI_ANY_ID, PCI_ANY_ID, },
2490         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
2491                 PCI_ANY_ID, PCI_ANY_ID, },
2492         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
2493                 PCI_ANY_ID, PCI_ANY_ID, },
2494         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
2495                 PCI_ANY_ID, PCI_ANY_ID, },
2496         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
2497                 PCI_ANY_ID, PCI_ANY_ID, },
2498         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
2499                 PCI_ANY_ID, PCI_ANY_ID, },
2500         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
2501                 PCI_ANY_ID, PCI_ANY_ID, },
2502         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
2503                 PCI_ANY_ID, PCI_ANY_ID, },
2504         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
2505                 PCI_ANY_ID, PCI_ANY_ID, },
2506         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
2507                 PCI_ANY_ID, PCI_ANY_ID, },
2508         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
2509                 PCI_ANY_ID, PCI_ANY_ID, },
2510         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
2511                 PCI_ANY_ID, PCI_ANY_ID, },
2512         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
2513                 PCI_ANY_ID, PCI_ANY_ID, },
2514         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
2515                 PCI_ANY_ID, PCI_ANY_ID, },
2516         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
2517                 PCI_ANY_ID, PCI_ANY_ID, },
2518         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
2519                 PCI_ANY_ID, PCI_ANY_ID, },
2520         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
2521                 PCI_ANY_ID, PCI_ANY_ID, },
2522         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
2523                 PCI_ANY_ID, PCI_ANY_ID, },
2524         { 0 }
2525 };
2526
2527 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
2528
2529 static struct pci_error_handlers lpfc_err_handler = {
2530         .error_detected = lpfc_io_error_detected,
2531         .slot_reset = lpfc_io_slot_reset,
2532         .resume = lpfc_io_resume,
2533 };
2534
2535 static struct pci_driver lpfc_driver = {
2536         .name           = LPFC_DRIVER_NAME,
2537         .id_table       = lpfc_id_table,
2538         .probe          = lpfc_pci_probe_one,
2539         .remove         = __devexit_p(lpfc_pci_remove_one),
2540         .err_handler    = &lpfc_err_handler,
2541 };
2542
2543 static int __init
2544 lpfc_init(void)
2545 {
2546         int error = 0;
2547
2548         printk(LPFC_MODULE_DESC "\n");
2549         printk(LPFC_COPYRIGHT "\n");
2550
2551         if (lpfc_enable_npiv) {
2552                 lpfc_transport_functions.vport_create = lpfc_vport_create;
2553                 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
2554         }
2555         lpfc_transport_template =
2556                                 fc_attach_transport(&lpfc_transport_functions);
2557         if (lpfc_transport_template == NULL)
2558                 return -ENOMEM;
2559         if (lpfc_enable_npiv) {
2560                 lpfc_vport_transport_template =
2561                         fc_attach_transport(&lpfc_vport_transport_functions);
2562                 if (lpfc_vport_transport_template == NULL) {
2563                         fc_release_transport(lpfc_transport_template);
2564                         return -ENOMEM;
2565                 }
2566         }
2567         error = pci_register_driver(&lpfc_driver);
2568         if (error) {
2569                 fc_release_transport(lpfc_transport_template);
2570                 fc_release_transport(lpfc_vport_transport_template);
2571         }
2572
2573         return error;
2574 }
2575
2576 static void __exit
2577 lpfc_exit(void)
2578 {
2579         pci_unregister_driver(&lpfc_driver);
2580         fc_release_transport(lpfc_transport_template);
2581         if (lpfc_enable_npiv)
2582                 fc_release_transport(lpfc_vport_transport_template);
2583 }
2584
2585 module_init(lpfc_init);
2586 module_exit(lpfc_exit);
2587 MODULE_LICENSE("GPL");
2588 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
2589 MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
2590 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);