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