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