[SCSI] lpfc 8.3.10: Added round robin FCF failover
[cascardo/linux.git] / 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-2010 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 #include <linux/aer.h>
32
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi_transport_fc.h>
37
38 #include "lpfc_hw4.h"
39 #include "lpfc_hw.h"
40 #include "lpfc_sli.h"
41 #include "lpfc_sli4.h"
42 #include "lpfc_nl.h"
43 #include "lpfc_disc.h"
44 #include "lpfc_scsi.h"
45 #include "lpfc.h"
46 #include "lpfc_logmsg.h"
47 #include "lpfc_crtn.h"
48 #include "lpfc_vport.h"
49 #include "lpfc_version.h"
50
51 char *_dump_buf_data;
52 unsigned long _dump_buf_data_order;
53 char *_dump_buf_dif;
54 unsigned long _dump_buf_dif_order;
55 spinlock_t _dump_buf_lock;
56
57 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
58 static int lpfc_post_rcv_buf(struct lpfc_hba *);
59 static int lpfc_sli4_queue_create(struct lpfc_hba *);
60 static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
61 static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
62 static int lpfc_setup_endian_order(struct lpfc_hba *);
63 static int lpfc_sli4_read_config(struct lpfc_hba *);
64 static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
65 static void lpfc_free_sgl_list(struct lpfc_hba *);
66 static int lpfc_init_sgl_list(struct lpfc_hba *);
67 static int lpfc_init_active_sgl_array(struct lpfc_hba *);
68 static void lpfc_free_active_sgl(struct lpfc_hba *);
69 static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
70 static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
71 static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
72 static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
73 static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
74
75 static struct scsi_transport_template *lpfc_transport_template = NULL;
76 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
77 static DEFINE_IDR(lpfc_hba_index);
78
79 /**
80  * lpfc_config_port_prep - Perform lpfc initialization prior to config port
81  * @phba: pointer to lpfc hba data structure.
82  *
83  * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
84  * mailbox command. It retrieves the revision information from the HBA and
85  * collects the Vital Product Data (VPD) about the HBA for preparing the
86  * configuration of the HBA.
87  *
88  * Return codes:
89  *   0 - success.
90  *   -ERESTART - requests the SLI layer to reset the HBA and try again.
91  *   Any other value - indicates an error.
92  **/
93 int
94 lpfc_config_port_prep(struct lpfc_hba *phba)
95 {
96         lpfc_vpd_t *vp = &phba->vpd;
97         int i = 0, rc;
98         LPFC_MBOXQ_t *pmb;
99         MAILBOX_t *mb;
100         char *lpfc_vpd_data = NULL;
101         uint16_t offset = 0;
102         static char licensed[56] =
103                     "key unlock for use with gnu public licensed code only\0";
104         static int init_key = 1;
105
106         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
107         if (!pmb) {
108                 phba->link_state = LPFC_HBA_ERROR;
109                 return -ENOMEM;
110         }
111
112         mb = &pmb->u.mb;
113         phba->link_state = LPFC_INIT_MBX_CMDS;
114
115         if (lpfc_is_LC_HBA(phba->pcidev->device)) {
116                 if (init_key) {
117                         uint32_t *ptext = (uint32_t *) licensed;
118
119                         for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
120                                 *ptext = cpu_to_be32(*ptext);
121                         init_key = 0;
122                 }
123
124                 lpfc_read_nv(phba, pmb);
125                 memset((char*)mb->un.varRDnvp.rsvd3, 0,
126                         sizeof (mb->un.varRDnvp.rsvd3));
127                 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
128                          sizeof (licensed));
129
130                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
131
132                 if (rc != MBX_SUCCESS) {
133                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
134                                         "0324 Config Port initialization "
135                                         "error, mbxCmd x%x READ_NVPARM, "
136                                         "mbxStatus x%x\n",
137                                         mb->mbxCommand, mb->mbxStatus);
138                         mempool_free(pmb, phba->mbox_mem_pool);
139                         return -ERESTART;
140                 }
141                 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
142                        sizeof(phba->wwnn));
143                 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
144                        sizeof(phba->wwpn));
145         }
146
147         phba->sli3_options = 0x0;
148
149         /* Setup and issue mailbox READ REV command */
150         lpfc_read_rev(phba, pmb);
151         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
152         if (rc != MBX_SUCCESS) {
153                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
154                                 "0439 Adapter failed to init, mbxCmd x%x "
155                                 "READ_REV, mbxStatus x%x\n",
156                                 mb->mbxCommand, mb->mbxStatus);
157                 mempool_free( pmb, phba->mbox_mem_pool);
158                 return -ERESTART;
159         }
160
161
162         /*
163          * The value of rr must be 1 since the driver set the cv field to 1.
164          * This setting requires the FW to set all revision fields.
165          */
166         if (mb->un.varRdRev.rr == 0) {
167                 vp->rev.rBit = 0;
168                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
169                                 "0440 Adapter failed to init, READ_REV has "
170                                 "missing revision information.\n");
171                 mempool_free(pmb, phba->mbox_mem_pool);
172                 return -ERESTART;
173         }
174
175         if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
176                 mempool_free(pmb, phba->mbox_mem_pool);
177                 return -EINVAL;
178         }
179
180         /* Save information as VPD data */
181         vp->rev.rBit = 1;
182         memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
183         vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
184         memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
185         vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
186         memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
187         vp->rev.biuRev = mb->un.varRdRev.biuRev;
188         vp->rev.smRev = mb->un.varRdRev.smRev;
189         vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
190         vp->rev.endecRev = mb->un.varRdRev.endecRev;
191         vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
192         vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
193         vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
194         vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
195         vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
196         vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
197
198         /* If the sli feature level is less then 9, we must
199          * tear down all RPIs and VPIs on link down if NPIV
200          * is enabled.
201          */
202         if (vp->rev.feaLevelHigh < 9)
203                 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
204
205         if (lpfc_is_LC_HBA(phba->pcidev->device))
206                 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
207                                                 sizeof (phba->RandomData));
208
209         /* Get adapter VPD information */
210         lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
211         if (!lpfc_vpd_data)
212                 goto out_free_mbox;
213
214         do {
215                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
216                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
217
218                 if (rc != MBX_SUCCESS) {
219                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
220                                         "0441 VPD not present on adapter, "
221                                         "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
222                                         mb->mbxCommand, mb->mbxStatus);
223                         mb->un.varDmp.word_cnt = 0;
224                 }
225                 /* dump mem may return a zero when finished or we got a
226                  * mailbox error, either way we are done.
227                  */
228                 if (mb->un.varDmp.word_cnt == 0)
229                         break;
230                 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
231                         mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
232                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
233                                       lpfc_vpd_data + offset,
234                                       mb->un.varDmp.word_cnt);
235                 offset += mb->un.varDmp.word_cnt;
236         } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
237         lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
238
239         kfree(lpfc_vpd_data);
240 out_free_mbox:
241         mempool_free(pmb, phba->mbox_mem_pool);
242         return 0;
243 }
244
245 /**
246  * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
247  * @phba: pointer to lpfc hba data structure.
248  * @pmboxq: pointer to the driver internal queue element for mailbox command.
249  *
250  * This is the completion handler for driver's configuring asynchronous event
251  * mailbox command to the device. If the mailbox command returns successfully,
252  * it will set internal async event support flag to 1; otherwise, it will
253  * set internal async event support flag to 0.
254  **/
255 static void
256 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
257 {
258         if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
259                 phba->temp_sensor_support = 1;
260         else
261                 phba->temp_sensor_support = 0;
262         mempool_free(pmboxq, phba->mbox_mem_pool);
263         return;
264 }
265
266 /**
267  * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
268  * @phba: pointer to lpfc hba data structure.
269  * @pmboxq: pointer to the driver internal queue element for mailbox command.
270  *
271  * This is the completion handler for dump mailbox command for getting
272  * wake up parameters. When this command complete, the response contain
273  * Option rom version of the HBA. This function translate the version number
274  * into a human readable string and store it in OptionROMVersion.
275  **/
276 static void
277 lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
278 {
279         struct prog_id *prg;
280         uint32_t prog_id_word;
281         char dist = ' ';
282         /* character array used for decoding dist type. */
283         char dist_char[] = "nabx";
284
285         if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
286                 mempool_free(pmboxq, phba->mbox_mem_pool);
287                 return;
288         }
289
290         prg = (struct prog_id *) &prog_id_word;
291
292         /* word 7 contain option rom version */
293         prog_id_word = pmboxq->u.mb.un.varWords[7];
294
295         /* Decode the Option rom version word to a readable string */
296         if (prg->dist < 4)
297                 dist = dist_char[prg->dist];
298
299         if ((prg->dist == 3) && (prg->num == 0))
300                 sprintf(phba->OptionROMVersion, "%d.%d%d",
301                         prg->ver, prg->rev, prg->lev);
302         else
303                 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
304                         prg->ver, prg->rev, prg->lev,
305                         dist, prg->num);
306         mempool_free(pmboxq, phba->mbox_mem_pool);
307         return;
308 }
309
310 /**
311  * lpfc_config_port_post - Perform lpfc initialization after config port
312  * @phba: pointer to lpfc hba data structure.
313  *
314  * This routine will do LPFC initialization after the CONFIG_PORT mailbox
315  * command call. It performs all internal resource and state setups on the
316  * port: post IOCB buffers, enable appropriate host interrupt attentions,
317  * ELS ring timers, etc.
318  *
319  * Return codes
320  *   0 - success.
321  *   Any other value - error.
322  **/
323 int
324 lpfc_config_port_post(struct lpfc_hba *phba)
325 {
326         struct lpfc_vport *vport = phba->pport;
327         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
328         LPFC_MBOXQ_t *pmb;
329         MAILBOX_t *mb;
330         struct lpfc_dmabuf *mp;
331         struct lpfc_sli *psli = &phba->sli;
332         uint32_t status, timeout;
333         int i, j;
334         int rc;
335
336         spin_lock_irq(&phba->hbalock);
337         /*
338          * If the Config port completed correctly the HBA is not
339          * over heated any more.
340          */
341         if (phba->over_temp_state == HBA_OVER_TEMP)
342                 phba->over_temp_state = HBA_NORMAL_TEMP;
343         spin_unlock_irq(&phba->hbalock);
344
345         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
346         if (!pmb) {
347                 phba->link_state = LPFC_HBA_ERROR;
348                 return -ENOMEM;
349         }
350         mb = &pmb->u.mb;
351
352         /* Get login parameters for NID.  */
353         rc = lpfc_read_sparam(phba, pmb, 0);
354         if (rc) {
355                 mempool_free(pmb, phba->mbox_mem_pool);
356                 return -ENOMEM;
357         }
358
359         pmb->vport = vport;
360         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
361                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
362                                 "0448 Adapter failed init, mbxCmd x%x "
363                                 "READ_SPARM mbxStatus x%x\n",
364                                 mb->mbxCommand, mb->mbxStatus);
365                 phba->link_state = LPFC_HBA_ERROR;
366                 mp = (struct lpfc_dmabuf *) pmb->context1;
367                 mempool_free(pmb, phba->mbox_mem_pool);
368                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
369                 kfree(mp);
370                 return -EIO;
371         }
372
373         mp = (struct lpfc_dmabuf *) pmb->context1;
374
375         memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
376         lpfc_mbuf_free(phba, mp->virt, mp->phys);
377         kfree(mp);
378         pmb->context1 = NULL;
379
380         if (phba->cfg_soft_wwnn)
381                 u64_to_wwn(phba->cfg_soft_wwnn,
382                            vport->fc_sparam.nodeName.u.wwn);
383         if (phba->cfg_soft_wwpn)
384                 u64_to_wwn(phba->cfg_soft_wwpn,
385                            vport->fc_sparam.portName.u.wwn);
386         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
387                sizeof (struct lpfc_name));
388         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
389                sizeof (struct lpfc_name));
390
391         /* Update the fc_host data structures with new wwn. */
392         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
393         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
394         fc_host_max_npiv_vports(shost) = phba->max_vpi;
395
396         /* If no serial number in VPD data, use low 6 bytes of WWNN */
397         /* This should be consolidated into parse_vpd ? - mr */
398         if (phba->SerialNumber[0] == 0) {
399                 uint8_t *outptr;
400
401                 outptr = &vport->fc_nodename.u.s.IEEE[0];
402                 for (i = 0; i < 12; i++) {
403                         status = *outptr++;
404                         j = ((status & 0xf0) >> 4);
405                         if (j <= 9)
406                                 phba->SerialNumber[i] =
407                                     (char)((uint8_t) 0x30 + (uint8_t) j);
408                         else
409                                 phba->SerialNumber[i] =
410                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
411                         i++;
412                         j = (status & 0xf);
413                         if (j <= 9)
414                                 phba->SerialNumber[i] =
415                                     (char)((uint8_t) 0x30 + (uint8_t) j);
416                         else
417                                 phba->SerialNumber[i] =
418                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
419                 }
420         }
421
422         lpfc_read_config(phba, pmb);
423         pmb->vport = vport;
424         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
425                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
426                                 "0453 Adapter failed to init, mbxCmd x%x "
427                                 "READ_CONFIG, mbxStatus x%x\n",
428                                 mb->mbxCommand, mb->mbxStatus);
429                 phba->link_state = LPFC_HBA_ERROR;
430                 mempool_free( pmb, phba->mbox_mem_pool);
431                 return -EIO;
432         }
433
434         /* Check if the port is disabled */
435         lpfc_sli_read_link_ste(phba);
436
437         /* Reset the DFT_HBA_Q_DEPTH to the max xri  */
438         if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
439                 phba->cfg_hba_queue_depth =
440                         (mb->un.varRdConfig.max_xri + 1) -
441                                         lpfc_sli4_get_els_iocb_cnt(phba);
442
443         phba->lmt = mb->un.varRdConfig.lmt;
444
445         /* Get the default values for Model Name and Description */
446         lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
447
448         if ((phba->cfg_link_speed > LINK_SPEED_10G)
449             || ((phba->cfg_link_speed == LINK_SPEED_1G)
450                 && !(phba->lmt & LMT_1Gb))
451             || ((phba->cfg_link_speed == LINK_SPEED_2G)
452                 && !(phba->lmt & LMT_2Gb))
453             || ((phba->cfg_link_speed == LINK_SPEED_4G)
454                 && !(phba->lmt & LMT_4Gb))
455             || ((phba->cfg_link_speed == LINK_SPEED_8G)
456                 && !(phba->lmt & LMT_8Gb))
457             || ((phba->cfg_link_speed == LINK_SPEED_10G)
458                 && !(phba->lmt & LMT_10Gb))) {
459                 /* Reset link speed to auto */
460                 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
461                         "1302 Invalid speed for this board: "
462                         "Reset link speed to auto: x%x\n",
463                         phba->cfg_link_speed);
464                         phba->cfg_link_speed = LINK_SPEED_AUTO;
465         }
466
467         phba->link_state = LPFC_LINK_DOWN;
468
469         /* Only process IOCBs on ELS ring till hba_state is READY */
470         if (psli->ring[psli->extra_ring].cmdringaddr)
471                 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
472         if (psli->ring[psli->fcp_ring].cmdringaddr)
473                 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
474         if (psli->ring[psli->next_ring].cmdringaddr)
475                 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
476
477         /* Post receive buffers for desired rings */
478         if (phba->sli_rev != 3)
479                 lpfc_post_rcv_buf(phba);
480
481         /*
482          * Configure HBA MSI-X attention conditions to messages if MSI-X mode
483          */
484         if (phba->intr_type == MSIX) {
485                 rc = lpfc_config_msi(phba, pmb);
486                 if (rc) {
487                         mempool_free(pmb, phba->mbox_mem_pool);
488                         return -EIO;
489                 }
490                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
491                 if (rc != MBX_SUCCESS) {
492                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
493                                         "0352 Config MSI mailbox command "
494                                         "failed, mbxCmd x%x, mbxStatus x%x\n",
495                                         pmb->u.mb.mbxCommand,
496                                         pmb->u.mb.mbxStatus);
497                         mempool_free(pmb, phba->mbox_mem_pool);
498                         return -EIO;
499                 }
500         }
501
502         spin_lock_irq(&phba->hbalock);
503         /* Initialize ERATT handling flag */
504         phba->hba_flag &= ~HBA_ERATT_HANDLED;
505
506         /* Enable appropriate host interrupts */
507         status = readl(phba->HCregaddr);
508         status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
509         if (psli->num_rings > 0)
510                 status |= HC_R0INT_ENA;
511         if (psli->num_rings > 1)
512                 status |= HC_R1INT_ENA;
513         if (psli->num_rings > 2)
514                 status |= HC_R2INT_ENA;
515         if (psli->num_rings > 3)
516                 status |= HC_R3INT_ENA;
517
518         if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
519             (phba->cfg_poll & DISABLE_FCP_RING_INT))
520                 status &= ~(HC_R0INT_ENA);
521
522         writel(status, phba->HCregaddr);
523         readl(phba->HCregaddr); /* flush */
524         spin_unlock_irq(&phba->hbalock);
525
526         /* Set up ring-0 (ELS) timer */
527         timeout = phba->fc_ratov * 2;
528         mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
529         /* Set up heart beat (HB) timer */
530         mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
531         phba->hb_outstanding = 0;
532         phba->last_completion_time = jiffies;
533         /* Set up error attention (ERATT) polling timer */
534         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
535
536         if (phba->hba_flag & LINK_DISABLED) {
537                 lpfc_printf_log(phba,
538                         KERN_ERR, LOG_INIT,
539                         "2598 Adapter Link is disabled.\n");
540                 lpfc_down_link(phba, pmb);
541                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
542                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
543                 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
544                         lpfc_printf_log(phba,
545                         KERN_ERR, LOG_INIT,
546                         "2599 Adapter failed to issue DOWN_LINK"
547                         " mbox command rc 0x%x\n", rc);
548
549                         mempool_free(pmb, phba->mbox_mem_pool);
550                         return -EIO;
551                 }
552         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
553                 lpfc_init_link(phba, pmb, phba->cfg_topology,
554                         phba->cfg_link_speed);
555                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
556                 lpfc_set_loopback_flag(phba);
557                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
558                 if (rc != MBX_SUCCESS) {
559                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
560                                 "0454 Adapter failed to init, mbxCmd x%x "
561                                 "INIT_LINK, mbxStatus x%x\n",
562                                 mb->mbxCommand, mb->mbxStatus);
563
564                         /* Clear all interrupt enable conditions */
565                         writel(0, phba->HCregaddr);
566                         readl(phba->HCregaddr); /* flush */
567                         /* Clear all pending interrupts */
568                         writel(0xffffffff, phba->HAregaddr);
569                         readl(phba->HAregaddr); /* flush */
570
571                         phba->link_state = LPFC_HBA_ERROR;
572                         if (rc != MBX_BUSY)
573                                 mempool_free(pmb, phba->mbox_mem_pool);
574                         return -EIO;
575                 }
576         }
577         /* MBOX buffer will be freed in mbox compl */
578         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
579         if (!pmb) {
580                 phba->link_state = LPFC_HBA_ERROR;
581                 return -ENOMEM;
582         }
583
584         lpfc_config_async(phba, pmb, LPFC_ELS_RING);
585         pmb->mbox_cmpl = lpfc_config_async_cmpl;
586         pmb->vport = phba->pport;
587         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
588
589         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
590                 lpfc_printf_log(phba,
591                                 KERN_ERR,
592                                 LOG_INIT,
593                                 "0456 Adapter failed to issue "
594                                 "ASYNCEVT_ENABLE mbox status x%x\n",
595                                 rc);
596                 mempool_free(pmb, phba->mbox_mem_pool);
597         }
598
599         /* Get Option rom version */
600         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
601         if (!pmb) {
602                 phba->link_state = LPFC_HBA_ERROR;
603                 return -ENOMEM;
604         }
605
606         lpfc_dump_wakeup_param(phba, pmb);
607         pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
608         pmb->vport = phba->pport;
609         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
610
611         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
612                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
613                                 "to get Option ROM version status x%x\n", rc);
614                 mempool_free(pmb, phba->mbox_mem_pool);
615         }
616
617         return 0;
618 }
619
620 /**
621  * lpfc_hba_init_link - Initialize the FC link
622  * @phba: pointer to lpfc hba data structure.
623  *
624  * This routine will issue the INIT_LINK mailbox command call.
625  * It is available to other drivers through the lpfc_hba data
626  * structure for use as a delayed link up mechanism with the
627  * module parameter lpfc_suppress_link_up.
628  *
629  * Return code
630  *              0 - success
631  *              Any other value - error
632  **/
633 int
634 lpfc_hba_init_link(struct lpfc_hba *phba)
635 {
636         struct lpfc_vport *vport = phba->pport;
637         LPFC_MBOXQ_t *pmb;
638         MAILBOX_t *mb;
639         int rc;
640
641         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
642         if (!pmb) {
643                 phba->link_state = LPFC_HBA_ERROR;
644                 return -ENOMEM;
645         }
646         mb = &pmb->u.mb;
647         pmb->vport = vport;
648
649         lpfc_init_link(phba, pmb, phba->cfg_topology,
650                 phba->cfg_link_speed);
651         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
652         lpfc_set_loopback_flag(phba);
653         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
654         if (rc != MBX_SUCCESS) {
655                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
656                         "0498 Adapter failed to init, mbxCmd x%x "
657                         "INIT_LINK, mbxStatus x%x\n",
658                         mb->mbxCommand, mb->mbxStatus);
659                 /* Clear all interrupt enable conditions */
660                 writel(0, phba->HCregaddr);
661                 readl(phba->HCregaddr); /* flush */
662                 /* Clear all pending interrupts */
663                 writel(0xffffffff, phba->HAregaddr);
664                 readl(phba->HAregaddr); /* flush */
665                 phba->link_state = LPFC_HBA_ERROR;
666                 if (rc != MBX_BUSY)
667                         mempool_free(pmb, phba->mbox_mem_pool);
668                 return -EIO;
669         }
670         phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
671
672         return 0;
673 }
674
675 /**
676  * lpfc_hba_down_link - this routine downs the FC link
677  *
678  * This routine will issue the DOWN_LINK mailbox command call.
679  * It is available to other drivers through the lpfc_hba data
680  * structure for use to stop the link.
681  *
682  * Return code
683  *              0 - success
684  *              Any other value - error
685  **/
686 int
687 lpfc_hba_down_link(struct lpfc_hba *phba)
688 {
689         LPFC_MBOXQ_t *pmb;
690         int rc;
691
692         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
693         if (!pmb) {
694                 phba->link_state = LPFC_HBA_ERROR;
695                 return -ENOMEM;
696         }
697
698         lpfc_printf_log(phba,
699                 KERN_ERR, LOG_INIT,
700                 "0491 Adapter Link is disabled.\n");
701         lpfc_down_link(phba, pmb);
702         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
703         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
704         if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
705                 lpfc_printf_log(phba,
706                 KERN_ERR, LOG_INIT,
707                 "2522 Adapter failed to issue DOWN_LINK"
708                 " mbox command rc 0x%x\n", rc);
709
710                 mempool_free(pmb, phba->mbox_mem_pool);
711                 return -EIO;
712         }
713         return 0;
714 }
715
716 /**
717  * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
718  * @phba: pointer to lpfc HBA data structure.
719  *
720  * This routine will do LPFC uninitialization before the HBA is reset when
721  * bringing down the SLI Layer.
722  *
723  * Return codes
724  *   0 - success.
725  *   Any other value - error.
726  **/
727 int
728 lpfc_hba_down_prep(struct lpfc_hba *phba)
729 {
730         struct lpfc_vport **vports;
731         int i;
732
733         if (phba->sli_rev <= LPFC_SLI_REV3) {
734                 /* Disable interrupts */
735                 writel(0, phba->HCregaddr);
736                 readl(phba->HCregaddr); /* flush */
737         }
738
739         if (phba->pport->load_flag & FC_UNLOADING)
740                 lpfc_cleanup_discovery_resources(phba->pport);
741         else {
742                 vports = lpfc_create_vport_work_array(phba);
743                 if (vports != NULL)
744                         for (i = 0; i <= phba->max_vports &&
745                                 vports[i] != NULL; i++)
746                                 lpfc_cleanup_discovery_resources(vports[i]);
747                 lpfc_destroy_vport_work_array(phba, vports);
748         }
749         return 0;
750 }
751
752 /**
753  * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
754  * @phba: pointer to lpfc HBA data structure.
755  *
756  * This routine will do uninitialization after the HBA is reset when bring
757  * down the SLI Layer.
758  *
759  * Return codes
760  *   0 - success.
761  *   Any other value - error.
762  **/
763 static int
764 lpfc_hba_down_post_s3(struct lpfc_hba *phba)
765 {
766         struct lpfc_sli *psli = &phba->sli;
767         struct lpfc_sli_ring *pring;
768         struct lpfc_dmabuf *mp, *next_mp;
769         LIST_HEAD(completions);
770         int i;
771
772         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
773                 lpfc_sli_hbqbuf_free_all(phba);
774         else {
775                 /* Cleanup preposted buffers on the ELS ring */
776                 pring = &psli->ring[LPFC_ELS_RING];
777                 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
778                         list_del(&mp->list);
779                         pring->postbufq_cnt--;
780                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
781                         kfree(mp);
782                 }
783         }
784
785         spin_lock_irq(&phba->hbalock);
786         for (i = 0; i < psli->num_rings; i++) {
787                 pring = &psli->ring[i];
788
789                 /* At this point in time the HBA is either reset or DOA. Either
790                  * way, nothing should be on txcmplq as it will NEVER complete.
791                  */
792                 list_splice_init(&pring->txcmplq, &completions);
793                 pring->txcmplq_cnt = 0;
794                 spin_unlock_irq(&phba->hbalock);
795
796                 /* Cancel all the IOCBs from the completions list */
797                 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
798                                       IOERR_SLI_ABORTED);
799
800                 lpfc_sli_abort_iocb_ring(phba, pring);
801                 spin_lock_irq(&phba->hbalock);
802         }
803         spin_unlock_irq(&phba->hbalock);
804
805         return 0;
806 }
807 /**
808  * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
809  * @phba: pointer to lpfc HBA data structure.
810  *
811  * This routine will do uninitialization after the HBA is reset when bring
812  * down the SLI Layer.
813  *
814  * Return codes
815  *   0 - success.
816  *   Any other value - error.
817  **/
818 static int
819 lpfc_hba_down_post_s4(struct lpfc_hba *phba)
820 {
821         struct lpfc_scsi_buf *psb, *psb_next;
822         LIST_HEAD(aborts);
823         int ret;
824         unsigned long iflag = 0;
825         struct lpfc_sglq *sglq_entry = NULL;
826
827         ret = lpfc_hba_down_post_s3(phba);
828         if (ret)
829                 return ret;
830         /* At this point in time the HBA is either reset or DOA. Either
831          * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
832          * on the lpfc_sgl_list so that it can either be freed if the
833          * driver is unloading or reposted if the driver is restarting
834          * the port.
835          */
836         spin_lock_irq(&phba->hbalock);  /* required for lpfc_sgl_list and */
837                                         /* scsl_buf_list */
838         /* abts_sgl_list_lock required because worker thread uses this
839          * list.
840          */
841         spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
842         list_for_each_entry(sglq_entry,
843                 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
844                 sglq_entry->state = SGL_FREED;
845
846         list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
847                         &phba->sli4_hba.lpfc_sgl_list);
848         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
849         /* abts_scsi_buf_list_lock required because worker thread uses this
850          * list.
851          */
852         spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
853         list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
854                         &aborts);
855         spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
856         spin_unlock_irq(&phba->hbalock);
857
858         list_for_each_entry_safe(psb, psb_next, &aborts, list) {
859                 psb->pCmd = NULL;
860                 psb->status = IOSTAT_SUCCESS;
861         }
862         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
863         list_splice(&aborts, &phba->lpfc_scsi_buf_list);
864         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
865         return 0;
866 }
867
868 /**
869  * lpfc_hba_down_post - Wrapper func for hba down post routine
870  * @phba: pointer to lpfc HBA data structure.
871  *
872  * This routine wraps the actual SLI3 or SLI4 routine for performing
873  * uninitialization after the HBA is reset when bring down the SLI Layer.
874  *
875  * Return codes
876  *   0 - success.
877  *   Any other value - error.
878  **/
879 int
880 lpfc_hba_down_post(struct lpfc_hba *phba)
881 {
882         return (*phba->lpfc_hba_down_post)(phba);
883 }
884
885 /**
886  * lpfc_hb_timeout - The HBA-timer timeout handler
887  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
888  *
889  * This is the HBA-timer timeout handler registered to the lpfc driver. When
890  * this timer fires, a HBA timeout event shall be posted to the lpfc driver
891  * work-port-events bitmap and the worker thread is notified. This timeout
892  * event will be used by the worker thread to invoke the actual timeout
893  * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
894  * be performed in the timeout handler and the HBA timeout event bit shall
895  * be cleared by the worker thread after it has taken the event bitmap out.
896  **/
897 static void
898 lpfc_hb_timeout(unsigned long ptr)
899 {
900         struct lpfc_hba *phba;
901         uint32_t tmo_posted;
902         unsigned long iflag;
903
904         phba = (struct lpfc_hba *)ptr;
905
906         /* Check for heart beat timeout conditions */
907         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
908         tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
909         if (!tmo_posted)
910                 phba->pport->work_port_events |= WORKER_HB_TMO;
911         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
912
913         /* Tell the worker thread there is work to do */
914         if (!tmo_posted)
915                 lpfc_worker_wake_up(phba);
916         return;
917 }
918
919 /**
920  * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
921  * @phba: pointer to lpfc hba data structure.
922  * @pmboxq: pointer to the driver internal queue element for mailbox command.
923  *
924  * This is the callback function to the lpfc heart-beat mailbox command.
925  * If configured, the lpfc driver issues the heart-beat mailbox command to
926  * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
927  * heart-beat mailbox command is issued, the driver shall set up heart-beat
928  * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
929  * heart-beat outstanding state. Once the mailbox command comes back and
930  * no error conditions detected, the heart-beat mailbox command timer is
931  * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
932  * state is cleared for the next heart-beat. If the timer expired with the
933  * heart-beat outstanding state set, the driver will put the HBA offline.
934  **/
935 static void
936 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
937 {
938         unsigned long drvr_flag;
939
940         spin_lock_irqsave(&phba->hbalock, drvr_flag);
941         phba->hb_outstanding = 0;
942         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
943
944         /* Check and reset heart-beat timer is necessary */
945         mempool_free(pmboxq, phba->mbox_mem_pool);
946         if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
947                 !(phba->link_state == LPFC_HBA_ERROR) &&
948                 !(phba->pport->load_flag & FC_UNLOADING))
949                 mod_timer(&phba->hb_tmofunc,
950                         jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
951         return;
952 }
953
954 /**
955  * lpfc_hb_timeout_handler - The HBA-timer timeout handler
956  * @phba: pointer to lpfc hba data structure.
957  *
958  * This is the actual HBA-timer timeout handler to be invoked by the worker
959  * thread whenever the HBA timer fired and HBA-timeout event posted. This
960  * handler performs any periodic operations needed for the device. If such
961  * periodic event has already been attended to either in the interrupt handler
962  * or by processing slow-ring or fast-ring events within the HBA-timer
963  * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
964  * the timer for the next timeout period. If lpfc heart-beat mailbox command
965  * is configured and there is no heart-beat mailbox command outstanding, a
966  * heart-beat mailbox is issued and timer set properly. Otherwise, if there
967  * has been a heart-beat mailbox command outstanding, the HBA shall be put
968  * to offline.
969  **/
970 void
971 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
972 {
973         struct lpfc_vport **vports;
974         LPFC_MBOXQ_t *pmboxq;
975         struct lpfc_dmabuf *buf_ptr;
976         int retval, i;
977         struct lpfc_sli *psli = &phba->sli;
978         LIST_HEAD(completions);
979
980         vports = lpfc_create_vport_work_array(phba);
981         if (vports != NULL)
982                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
983                         lpfc_rcv_seq_check_edtov(vports[i]);
984         lpfc_destroy_vport_work_array(phba, vports);
985
986         if ((phba->link_state == LPFC_HBA_ERROR) ||
987                 (phba->pport->load_flag & FC_UNLOADING) ||
988                 (phba->pport->fc_flag & FC_OFFLINE_MODE))
989                 return;
990
991         spin_lock_irq(&phba->pport->work_port_lock);
992
993         if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
994                 jiffies)) {
995                 spin_unlock_irq(&phba->pport->work_port_lock);
996                 if (!phba->hb_outstanding)
997                         mod_timer(&phba->hb_tmofunc,
998                                 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
999                 else
1000                         mod_timer(&phba->hb_tmofunc,
1001                                 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1002                 return;
1003         }
1004         spin_unlock_irq(&phba->pport->work_port_lock);
1005
1006         if (phba->elsbuf_cnt &&
1007                 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1008                 spin_lock_irq(&phba->hbalock);
1009                 list_splice_init(&phba->elsbuf, &completions);
1010                 phba->elsbuf_cnt = 0;
1011                 phba->elsbuf_prev_cnt = 0;
1012                 spin_unlock_irq(&phba->hbalock);
1013
1014                 while (!list_empty(&completions)) {
1015                         list_remove_head(&completions, buf_ptr,
1016                                 struct lpfc_dmabuf, list);
1017                         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1018                         kfree(buf_ptr);
1019                 }
1020         }
1021         phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1022
1023         /* If there is no heart beat outstanding, issue a heartbeat command */
1024         if (phba->cfg_enable_hba_heartbeat) {
1025                 if (!phba->hb_outstanding) {
1026                         pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
1027                         if (!pmboxq) {
1028                                 mod_timer(&phba->hb_tmofunc,
1029                                           jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1030                                 return;
1031                         }
1032
1033                         lpfc_heart_beat(phba, pmboxq);
1034                         pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1035                         pmboxq->vport = phba->pport;
1036                         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
1037
1038                         if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
1039                                 mempool_free(pmboxq, phba->mbox_mem_pool);
1040                                 mod_timer(&phba->hb_tmofunc,
1041                                           jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1042                                 return;
1043                         }
1044                         mod_timer(&phba->hb_tmofunc,
1045                                   jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1046                         phba->hb_outstanding = 1;
1047                         return;
1048                 } else {
1049                         /*
1050                         * If heart beat timeout called with hb_outstanding set
1051                         * we need to take the HBA offline.
1052                         */
1053                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1054                                         "0459 Adapter heartbeat failure, "
1055                                         "taking this port offline.\n");
1056
1057                         spin_lock_irq(&phba->hbalock);
1058                         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1059                         spin_unlock_irq(&phba->hbalock);
1060
1061                         lpfc_offline_prep(phba);
1062                         lpfc_offline(phba);
1063                         lpfc_unblock_mgmt_io(phba);
1064                         phba->link_state = LPFC_HBA_ERROR;
1065                         lpfc_hba_down_post(phba);
1066                 }
1067         }
1068 }
1069
1070 /**
1071  * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
1072  * @phba: pointer to lpfc hba data structure.
1073  *
1074  * This routine is called to bring the HBA offline when HBA hardware error
1075  * other than Port Error 6 has been detected.
1076  **/
1077 static void
1078 lpfc_offline_eratt(struct lpfc_hba *phba)
1079 {
1080         struct lpfc_sli   *psli = &phba->sli;
1081
1082         spin_lock_irq(&phba->hbalock);
1083         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1084         spin_unlock_irq(&phba->hbalock);
1085         lpfc_offline_prep(phba);
1086
1087         lpfc_offline(phba);
1088         lpfc_reset_barrier(phba);
1089         spin_lock_irq(&phba->hbalock);
1090         lpfc_sli_brdreset(phba);
1091         spin_unlock_irq(&phba->hbalock);
1092         lpfc_hba_down_post(phba);
1093         lpfc_sli_brdready(phba, HS_MBRDY);
1094         lpfc_unblock_mgmt_io(phba);
1095         phba->link_state = LPFC_HBA_ERROR;
1096         return;
1097 }
1098
1099 /**
1100  * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1101  * @phba: pointer to lpfc hba data structure.
1102  *
1103  * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1104  * other than Port Error 6 has been detected.
1105  **/
1106 static void
1107 lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1108 {
1109         lpfc_offline_prep(phba);
1110         lpfc_offline(phba);
1111         lpfc_sli4_brdreset(phba);
1112         lpfc_hba_down_post(phba);
1113         lpfc_sli4_post_status_check(phba);
1114         lpfc_unblock_mgmt_io(phba);
1115         phba->link_state = LPFC_HBA_ERROR;
1116 }
1117
1118 /**
1119  * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1120  * @phba: pointer to lpfc hba data structure.
1121  *
1122  * This routine is invoked to handle the deferred HBA hardware error
1123  * conditions. This type of error is indicated by HBA by setting ER1
1124  * and another ER bit in the host status register. The driver will
1125  * wait until the ER1 bit clears before handling the error condition.
1126  **/
1127 static void
1128 lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1129 {
1130         uint32_t old_host_status = phba->work_hs;
1131         struct lpfc_sli_ring  *pring;
1132         struct lpfc_sli *psli = &phba->sli;
1133
1134         /* If the pci channel is offline, ignore possible errors,
1135          * since we cannot communicate with the pci card anyway.
1136          */
1137         if (pci_channel_offline(phba->pcidev)) {
1138                 spin_lock_irq(&phba->hbalock);
1139                 phba->hba_flag &= ~DEFER_ERATT;
1140                 spin_unlock_irq(&phba->hbalock);
1141                 return;
1142         }
1143
1144         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1145                 "0479 Deferred Adapter Hardware Error "
1146                 "Data: x%x x%x x%x\n",
1147                 phba->work_hs,
1148                 phba->work_status[0], phba->work_status[1]);
1149
1150         spin_lock_irq(&phba->hbalock);
1151         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1152         spin_unlock_irq(&phba->hbalock);
1153
1154
1155         /*
1156          * Firmware stops when it triggred erratt. That could cause the I/Os
1157          * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1158          * SCSI layer retry it after re-establishing link.
1159          */
1160         pring = &psli->ring[psli->fcp_ring];
1161         lpfc_sli_abort_iocb_ring(phba, pring);
1162
1163         /*
1164          * There was a firmware error. Take the hba offline and then
1165          * attempt to restart it.
1166          */
1167         lpfc_offline_prep(phba);
1168         lpfc_offline(phba);
1169
1170         /* Wait for the ER1 bit to clear.*/
1171         while (phba->work_hs & HS_FFER1) {
1172                 msleep(100);
1173                 phba->work_hs = readl(phba->HSregaddr);
1174                 /* If driver is unloading let the worker thread continue */
1175                 if (phba->pport->load_flag & FC_UNLOADING) {
1176                         phba->work_hs = 0;
1177                         break;
1178                 }
1179         }
1180
1181         /*
1182          * This is to ptrotect against a race condition in which
1183          * first write to the host attention register clear the
1184          * host status register.
1185          */
1186         if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1187                 phba->work_hs = old_host_status & ~HS_FFER1;
1188
1189         spin_lock_irq(&phba->hbalock);
1190         phba->hba_flag &= ~DEFER_ERATT;
1191         spin_unlock_irq(&phba->hbalock);
1192         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1193         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1194 }
1195
1196 static void
1197 lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1198 {
1199         struct lpfc_board_event_header board_event;
1200         struct Scsi_Host *shost;
1201
1202         board_event.event_type = FC_REG_BOARD_EVENT;
1203         board_event.subcategory = LPFC_EVENT_PORTINTERR;
1204         shost = lpfc_shost_from_vport(phba->pport);
1205         fc_host_post_vendor_event(shost, fc_get_event_number(),
1206                                   sizeof(board_event),
1207                                   (char *) &board_event,
1208                                   LPFC_NL_VENDOR_ID);
1209 }
1210
1211 /**
1212  * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1213  * @phba: pointer to lpfc hba data structure.
1214  *
1215  * This routine is invoked to handle the following HBA hardware error
1216  * conditions:
1217  * 1 - HBA error attention interrupt
1218  * 2 - DMA ring index out of range
1219  * 3 - Mailbox command came back as unknown
1220  **/
1221 static void
1222 lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1223 {
1224         struct lpfc_vport *vport = phba->pport;
1225         struct lpfc_sli   *psli = &phba->sli;
1226         struct lpfc_sli_ring  *pring;
1227         uint32_t event_data;
1228         unsigned long temperature;
1229         struct temp_event temp_event_data;
1230         struct Scsi_Host  *shost;
1231
1232         /* If the pci channel is offline, ignore possible errors,
1233          * since we cannot communicate with the pci card anyway.
1234          */
1235         if (pci_channel_offline(phba->pcidev)) {
1236                 spin_lock_irq(&phba->hbalock);
1237                 phba->hba_flag &= ~DEFER_ERATT;
1238                 spin_unlock_irq(&phba->hbalock);
1239                 return;
1240         }
1241
1242         /* If resets are disabled then leave the HBA alone and return */
1243         if (!phba->cfg_enable_hba_reset)
1244                 return;
1245
1246         /* Send an internal error event to mgmt application */
1247         lpfc_board_errevt_to_mgmt(phba);
1248
1249         if (phba->hba_flag & DEFER_ERATT)
1250                 lpfc_handle_deferred_eratt(phba);
1251
1252         if (phba->work_hs & HS_FFER6) {
1253                 /* Re-establishing Link */
1254                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1255                                 "1301 Re-establishing Link "
1256                                 "Data: x%x x%x x%x\n",
1257                                 phba->work_hs,
1258                                 phba->work_status[0], phba->work_status[1]);
1259
1260                 spin_lock_irq(&phba->hbalock);
1261                 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1262                 spin_unlock_irq(&phba->hbalock);
1263
1264                 /*
1265                 * Firmware stops when it triggled erratt with HS_FFER6.
1266                 * That could cause the I/Os dropped by the firmware.
1267                 * Error iocb (I/O) on txcmplq and let the SCSI layer
1268                 * retry it after re-establishing link.
1269                 */
1270                 pring = &psli->ring[psli->fcp_ring];
1271                 lpfc_sli_abort_iocb_ring(phba, pring);
1272
1273                 /*
1274                  * There was a firmware error.  Take the hba offline and then
1275                  * attempt to restart it.
1276                  */
1277                 lpfc_offline_prep(phba);
1278                 lpfc_offline(phba);
1279                 lpfc_sli_brdrestart(phba);
1280                 if (lpfc_online(phba) == 0) {   /* Initialize the HBA */
1281                         lpfc_unblock_mgmt_io(phba);
1282                         return;
1283                 }
1284                 lpfc_unblock_mgmt_io(phba);
1285         } else if (phba->work_hs & HS_CRIT_TEMP) {
1286                 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1287                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1288                 temp_event_data.event_code = LPFC_CRIT_TEMP;
1289                 temp_event_data.data = (uint32_t)temperature;
1290
1291                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1292                                 "0406 Adapter maximum temperature exceeded "
1293                                 "(%ld), taking this port offline "
1294                                 "Data: x%x x%x x%x\n",
1295                                 temperature, phba->work_hs,
1296                                 phba->work_status[0], phba->work_status[1]);
1297
1298                 shost = lpfc_shost_from_vport(phba->pport);
1299                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1300                                           sizeof(temp_event_data),
1301                                           (char *) &temp_event_data,
1302                                           SCSI_NL_VID_TYPE_PCI
1303                                           | PCI_VENDOR_ID_EMULEX);
1304
1305                 spin_lock_irq(&phba->hbalock);
1306                 phba->over_temp_state = HBA_OVER_TEMP;
1307                 spin_unlock_irq(&phba->hbalock);
1308                 lpfc_offline_eratt(phba);
1309
1310         } else {
1311                 /* The if clause above forces this code path when the status
1312                  * failure is a value other than FFER6. Do not call the offline
1313                  * twice. This is the adapter hardware error path.
1314                  */
1315                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1316                                 "0457 Adapter Hardware Error "
1317                                 "Data: x%x x%x x%x\n",
1318                                 phba->work_hs,
1319                                 phba->work_status[0], phba->work_status[1]);
1320
1321                 event_data = FC_REG_DUMP_EVENT;
1322                 shost = lpfc_shost_from_vport(vport);
1323                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1324                                 sizeof(event_data), (char *) &event_data,
1325                                 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1326
1327                 lpfc_offline_eratt(phba);
1328         }
1329         return;
1330 }
1331
1332 /**
1333  * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1334  * @phba: pointer to lpfc hba data structure.
1335  *
1336  * This routine is invoked to handle the SLI4 HBA hardware error attention
1337  * conditions.
1338  **/
1339 static void
1340 lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1341 {
1342         struct lpfc_vport *vport = phba->pport;
1343         uint32_t event_data;
1344         struct Scsi_Host *shost;
1345
1346         /* If the pci channel is offline, ignore possible errors, since
1347          * we cannot communicate with the pci card anyway.
1348          */
1349         if (pci_channel_offline(phba->pcidev))
1350                 return;
1351         /* If resets are disabled then leave the HBA alone and return */
1352         if (!phba->cfg_enable_hba_reset)
1353                 return;
1354
1355         /* Send an internal error event to mgmt application */
1356         lpfc_board_errevt_to_mgmt(phba);
1357
1358         /* For now, the actual action for SLI4 device handling is not
1359          * specified yet, just treated it as adaptor hardware failure
1360          */
1361         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1362                         "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1363                         phba->work_status[0], phba->work_status[1]);
1364
1365         event_data = FC_REG_DUMP_EVENT;
1366         shost = lpfc_shost_from_vport(vport);
1367         fc_host_post_vendor_event(shost, fc_get_event_number(),
1368                                   sizeof(event_data), (char *) &event_data,
1369                                   SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1370
1371         lpfc_sli4_offline_eratt(phba);
1372 }
1373
1374 /**
1375  * lpfc_handle_eratt - Wrapper func for handling hba error attention
1376  * @phba: pointer to lpfc HBA data structure.
1377  *
1378  * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1379  * routine from the API jump table function pointer from the lpfc_hba struct.
1380  *
1381  * Return codes
1382  *   0 - success.
1383  *   Any other value - error.
1384  **/
1385 void
1386 lpfc_handle_eratt(struct lpfc_hba *phba)
1387 {
1388         (*phba->lpfc_handle_eratt)(phba);
1389 }
1390
1391 /**
1392  * lpfc_handle_latt - The HBA link event handler
1393  * @phba: pointer to lpfc hba data structure.
1394  *
1395  * This routine is invoked from the worker thread to handle a HBA host
1396  * attention link event.
1397  **/
1398 void
1399 lpfc_handle_latt(struct lpfc_hba *phba)
1400 {
1401         struct lpfc_vport *vport = phba->pport;
1402         struct lpfc_sli   *psli = &phba->sli;
1403         LPFC_MBOXQ_t *pmb;
1404         volatile uint32_t control;
1405         struct lpfc_dmabuf *mp;
1406         int rc = 0;
1407
1408         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1409         if (!pmb) {
1410                 rc = 1;
1411                 goto lpfc_handle_latt_err_exit;
1412         }
1413
1414         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1415         if (!mp) {
1416                 rc = 2;
1417                 goto lpfc_handle_latt_free_pmb;
1418         }
1419
1420         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
1421         if (!mp->virt) {
1422                 rc = 3;
1423                 goto lpfc_handle_latt_free_mp;
1424         }
1425
1426         /* Cleanup any outstanding ELS commands */
1427         lpfc_els_flush_all_cmd(phba);
1428
1429         psli->slistat.link_event++;
1430         lpfc_read_la(phba, pmb, mp);
1431         pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
1432         pmb->vport = vport;
1433         /* Block ELS IOCBs until we have processed this mbox command */
1434         phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
1435         rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
1436         if (rc == MBX_NOT_FINISHED) {
1437                 rc = 4;
1438                 goto lpfc_handle_latt_free_mbuf;
1439         }
1440
1441         /* Clear Link Attention in HA REG */
1442         spin_lock_irq(&phba->hbalock);
1443         writel(HA_LATT, phba->HAregaddr);
1444         readl(phba->HAregaddr); /* flush */
1445         spin_unlock_irq(&phba->hbalock);
1446
1447         return;
1448
1449 lpfc_handle_latt_free_mbuf:
1450         phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1451         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1452 lpfc_handle_latt_free_mp:
1453         kfree(mp);
1454 lpfc_handle_latt_free_pmb:
1455         mempool_free(pmb, phba->mbox_mem_pool);
1456 lpfc_handle_latt_err_exit:
1457         /* Enable Link attention interrupts */
1458         spin_lock_irq(&phba->hbalock);
1459         psli->sli_flag |= LPFC_PROCESS_LA;
1460         control = readl(phba->HCregaddr);
1461         control |= HC_LAINT_ENA;
1462         writel(control, phba->HCregaddr);
1463         readl(phba->HCregaddr); /* flush */
1464
1465         /* Clear Link Attention in HA REG */
1466         writel(HA_LATT, phba->HAregaddr);
1467         readl(phba->HAregaddr); /* flush */
1468         spin_unlock_irq(&phba->hbalock);
1469         lpfc_linkdown(phba);
1470         phba->link_state = LPFC_HBA_ERROR;
1471
1472         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1473                      "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
1474
1475         return;
1476 }
1477
1478 /**
1479  * lpfc_parse_vpd - Parse VPD (Vital Product Data)
1480  * @phba: pointer to lpfc hba data structure.
1481  * @vpd: pointer to the vital product data.
1482  * @len: length of the vital product data in bytes.
1483  *
1484  * This routine parses the Vital Product Data (VPD). The VPD is treated as
1485  * an array of characters. In this routine, the ModelName, ProgramType, and
1486  * ModelDesc, etc. fields of the phba data structure will be populated.
1487  *
1488  * Return codes
1489  *   0 - pointer to the VPD passed in is NULL
1490  *   1 - success
1491  **/
1492 int
1493 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
1494 {
1495         uint8_t lenlo, lenhi;
1496         int Length;
1497         int i, j;
1498         int finished = 0;
1499         int index = 0;
1500
1501         if (!vpd)
1502                 return 0;
1503
1504         /* Vital Product */
1505         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1506                         "0455 Vital Product Data: x%x x%x x%x x%x\n",
1507                         (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1508                         (uint32_t) vpd[3]);
1509         while (!finished && (index < (len - 4))) {
1510                 switch (vpd[index]) {
1511                 case 0x82:
1512                 case 0x91:
1513                         index += 1;
1514                         lenlo = vpd[index];
1515                         index += 1;
1516                         lenhi = vpd[index];
1517                         index += 1;
1518                         i = ((((unsigned short)lenhi) << 8) + lenlo);
1519                         index += i;
1520                         break;
1521                 case 0x90:
1522                         index += 1;
1523                         lenlo = vpd[index];
1524                         index += 1;
1525                         lenhi = vpd[index];
1526                         index += 1;
1527                         Length = ((((unsigned short)lenhi) << 8) + lenlo);
1528                         if (Length > len - index)
1529                                 Length = len - index;
1530                         while (Length > 0) {
1531                         /* Look for Serial Number */
1532                         if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1533                                 index += 2;
1534                                 i = vpd[index];
1535                                 index += 1;
1536                                 j = 0;
1537                                 Length -= (3+i);
1538                                 while(i--) {
1539                                         phba->SerialNumber[j++] = vpd[index++];
1540                                         if (j == 31)
1541                                                 break;
1542                                 }
1543                                 phba->SerialNumber[j] = 0;
1544                                 continue;
1545                         }
1546                         else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1547                                 phba->vpd_flag |= VPD_MODEL_DESC;
1548                                 index += 2;
1549                                 i = vpd[index];
1550                                 index += 1;
1551                                 j = 0;
1552                                 Length -= (3+i);
1553                                 while(i--) {
1554                                         phba->ModelDesc[j++] = vpd[index++];
1555                                         if (j == 255)
1556                                                 break;
1557                                 }
1558                                 phba->ModelDesc[j] = 0;
1559                                 continue;
1560                         }
1561                         else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1562                                 phba->vpd_flag |= VPD_MODEL_NAME;
1563                                 index += 2;
1564                                 i = vpd[index];
1565                                 index += 1;
1566                                 j = 0;
1567                                 Length -= (3+i);
1568                                 while(i--) {
1569                                         phba->ModelName[j++] = vpd[index++];
1570                                         if (j == 79)
1571                                                 break;
1572                                 }
1573                                 phba->ModelName[j] = 0;
1574                                 continue;
1575                         }
1576                         else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1577                                 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1578                                 index += 2;
1579                                 i = vpd[index];
1580                                 index += 1;
1581                                 j = 0;
1582                                 Length -= (3+i);
1583                                 while(i--) {
1584                                         phba->ProgramType[j++] = vpd[index++];
1585                                         if (j == 255)
1586                                                 break;
1587                                 }
1588                                 phba->ProgramType[j] = 0;
1589                                 continue;
1590                         }
1591                         else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1592                                 phba->vpd_flag |= VPD_PORT;
1593                                 index += 2;
1594                                 i = vpd[index];
1595                                 index += 1;
1596                                 j = 0;
1597                                 Length -= (3+i);
1598                                 while(i--) {
1599                                 phba->Port[j++] = vpd[index++];
1600                                 if (j == 19)
1601                                         break;
1602                                 }
1603                                 phba->Port[j] = 0;
1604                                 continue;
1605                         }
1606                         else {
1607                                 index += 2;
1608                                 i = vpd[index];
1609                                 index += 1;
1610                                 index += i;
1611                                 Length -= (3 + i);
1612                         }
1613                 }
1614                 finished = 0;
1615                 break;
1616                 case 0x78:
1617                         finished = 1;
1618                         break;
1619                 default:
1620                         index ++;
1621                         break;
1622                 }
1623         }
1624
1625         return(1);
1626 }
1627
1628 /**
1629  * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
1630  * @phba: pointer to lpfc hba data structure.
1631  * @mdp: pointer to the data structure to hold the derived model name.
1632  * @descp: pointer to the data structure to hold the derived description.
1633  *
1634  * This routine retrieves HBA's description based on its registered PCI device
1635  * ID. The @descp passed into this function points to an array of 256 chars. It
1636  * shall be returned with the model name, maximum speed, and the host bus type.
1637  * The @mdp passed into this function points to an array of 80 chars. When the
1638  * function returns, the @mdp will be filled with the model name.
1639  **/
1640 static void
1641 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1642 {
1643         lpfc_vpd_t *vp;
1644         uint16_t dev_id = phba->pcidev->device;
1645         int max_speed;
1646         int GE = 0;
1647         int oneConnect = 0; /* default is not a oneConnect */
1648         struct {
1649                 char *name;
1650                 char *bus;
1651                 char *function;
1652         } m = {"<Unknown>", "", ""};
1653
1654         if (mdp && mdp[0] != '\0'
1655                 && descp && descp[0] != '\0')
1656                 return;
1657
1658         if (phba->lmt & LMT_10Gb)
1659                 max_speed = 10;
1660         else if (phba->lmt & LMT_8Gb)
1661                 max_speed = 8;
1662         else if (phba->lmt & LMT_4Gb)
1663                 max_speed = 4;
1664         else if (phba->lmt & LMT_2Gb)
1665                 max_speed = 2;
1666         else
1667                 max_speed = 1;
1668
1669         vp = &phba->vpd;
1670
1671         switch (dev_id) {
1672         case PCI_DEVICE_ID_FIREFLY:
1673                 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
1674                 break;
1675         case PCI_DEVICE_ID_SUPERFLY:
1676                 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1677                         m = (typeof(m)){"LP7000", "PCI",
1678                                         "Fibre Channel Adapter"};
1679                 else
1680                         m = (typeof(m)){"LP7000E", "PCI",
1681                                         "Fibre Channel Adapter"};
1682                 break;
1683         case PCI_DEVICE_ID_DRAGONFLY:
1684                 m = (typeof(m)){"LP8000", "PCI",
1685                                 "Fibre Channel Adapter"};
1686                 break;
1687         case PCI_DEVICE_ID_CENTAUR:
1688                 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1689                         m = (typeof(m)){"LP9002", "PCI",
1690                                         "Fibre Channel Adapter"};
1691                 else
1692                         m = (typeof(m)){"LP9000", "PCI",
1693                                         "Fibre Channel Adapter"};
1694                 break;
1695         case PCI_DEVICE_ID_RFLY:
1696                 m = (typeof(m)){"LP952", "PCI",
1697                                 "Fibre Channel Adapter"};
1698                 break;
1699         case PCI_DEVICE_ID_PEGASUS:
1700                 m = (typeof(m)){"LP9802", "PCI-X",
1701                                 "Fibre Channel Adapter"};
1702                 break;
1703         case PCI_DEVICE_ID_THOR:
1704                 m = (typeof(m)){"LP10000", "PCI-X",
1705                                 "Fibre Channel Adapter"};
1706                 break;
1707         case PCI_DEVICE_ID_VIPER:
1708                 m = (typeof(m)){"LPX1000",  "PCI-X",
1709                                 "Fibre Channel Adapter"};
1710                 break;
1711         case PCI_DEVICE_ID_PFLY:
1712                 m = (typeof(m)){"LP982", "PCI-X",
1713                                 "Fibre Channel Adapter"};
1714                 break;
1715         case PCI_DEVICE_ID_TFLY:
1716                 m = (typeof(m)){"LP1050", "PCI-X",
1717                                 "Fibre Channel Adapter"};
1718                 break;
1719         case PCI_DEVICE_ID_HELIOS:
1720                 m = (typeof(m)){"LP11000", "PCI-X2",
1721                                 "Fibre Channel Adapter"};
1722                 break;
1723         case PCI_DEVICE_ID_HELIOS_SCSP:
1724                 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1725                                 "Fibre Channel Adapter"};
1726                 break;
1727         case PCI_DEVICE_ID_HELIOS_DCSP:
1728                 m = (typeof(m)){"LP11002-SP",  "PCI-X2",
1729                                 "Fibre Channel Adapter"};
1730                 break;
1731         case PCI_DEVICE_ID_NEPTUNE:
1732                 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
1733                 break;
1734         case PCI_DEVICE_ID_NEPTUNE_SCSP:
1735                 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
1736                 break;
1737         case PCI_DEVICE_ID_NEPTUNE_DCSP:
1738                 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
1739                 break;
1740         case PCI_DEVICE_ID_BMID:
1741                 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
1742                 break;
1743         case PCI_DEVICE_ID_BSMB:
1744                 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
1745                 break;
1746         case PCI_DEVICE_ID_ZEPHYR:
1747                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1748                 break;
1749         case PCI_DEVICE_ID_ZEPHYR_SCSP:
1750                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1751                 break;
1752         case PCI_DEVICE_ID_ZEPHYR_DCSP:
1753                 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
1754                 GE = 1;
1755                 break;
1756         case PCI_DEVICE_ID_ZMID:
1757                 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
1758                 break;
1759         case PCI_DEVICE_ID_ZSMB:
1760                 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
1761                 break;
1762         case PCI_DEVICE_ID_LP101:
1763                 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
1764                 break;
1765         case PCI_DEVICE_ID_LP10000S:
1766                 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
1767                 break;
1768         case PCI_DEVICE_ID_LP11000S:
1769                 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
1770                 break;
1771         case PCI_DEVICE_ID_LPE11000S:
1772                 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
1773                 break;
1774         case PCI_DEVICE_ID_SAT:
1775                 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
1776                 break;
1777         case PCI_DEVICE_ID_SAT_MID:
1778                 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
1779                 break;
1780         case PCI_DEVICE_ID_SAT_SMB:
1781                 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
1782                 break;
1783         case PCI_DEVICE_ID_SAT_DCSP:
1784                 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
1785                 break;
1786         case PCI_DEVICE_ID_SAT_SCSP:
1787                 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
1788                 break;
1789         case PCI_DEVICE_ID_SAT_S:
1790                 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
1791                 break;
1792         case PCI_DEVICE_ID_HORNET:
1793                 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
1794                 GE = 1;
1795                 break;
1796         case PCI_DEVICE_ID_PROTEUS_VF:
1797                 m = (typeof(m)){"LPev12000", "PCIe IOV",
1798                                 "Fibre Channel Adapter"};
1799                 break;
1800         case PCI_DEVICE_ID_PROTEUS_PF:
1801                 m = (typeof(m)){"LPev12000", "PCIe IOV",
1802                                 "Fibre Channel Adapter"};
1803                 break;
1804         case PCI_DEVICE_ID_PROTEUS_S:
1805                 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1806                                 "Fibre Channel Adapter"};
1807                 break;
1808         case PCI_DEVICE_ID_TIGERSHARK:
1809                 oneConnect = 1;
1810                 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
1811                 break;
1812         case PCI_DEVICE_ID_TOMCAT:
1813                 oneConnect = 1;
1814                 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1815                 break;
1816         case PCI_DEVICE_ID_FALCON:
1817                 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1818                                 "EmulexSecure Fibre"};
1819                 break;
1820         default:
1821                 m = (typeof(m)){"Unknown", "", ""};
1822                 break;
1823         }
1824
1825         if (mdp && mdp[0] == '\0')
1826                 snprintf(mdp, 79,"%s", m.name);
1827         /* oneConnect hba requires special processing, they are all initiators
1828          * and we put the port number on the end
1829          */
1830         if (descp && descp[0] == '\0') {
1831                 if (oneConnect)
1832                         snprintf(descp, 255,
1833                                 "Emulex OneConnect %s, %s Initiator, Port %s",
1834                                 m.name, m.function,
1835                                 phba->Port);
1836                 else
1837                         snprintf(descp, 255,
1838                                 "Emulex %s %d%s %s %s",
1839                                 m.name, max_speed, (GE) ? "GE" : "Gb",
1840                                 m.bus, m.function);
1841         }
1842 }
1843
1844 /**
1845  * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
1846  * @phba: pointer to lpfc hba data structure.
1847  * @pring: pointer to a IOCB ring.
1848  * @cnt: the number of IOCBs to be posted to the IOCB ring.
1849  *
1850  * This routine posts a given number of IOCBs with the associated DMA buffer
1851  * descriptors specified by the cnt argument to the given IOCB ring.
1852  *
1853  * Return codes
1854  *   The number of IOCBs NOT able to be posted to the IOCB ring.
1855  **/
1856 int
1857 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
1858 {
1859         IOCB_t *icmd;
1860         struct lpfc_iocbq *iocb;
1861         struct lpfc_dmabuf *mp1, *mp2;
1862
1863         cnt += pring->missbufcnt;
1864
1865         /* While there are buffers to post */
1866         while (cnt > 0) {
1867                 /* Allocate buffer for  command iocb */
1868                 iocb = lpfc_sli_get_iocbq(phba);
1869                 if (iocb == NULL) {
1870                         pring->missbufcnt = cnt;
1871                         return cnt;
1872                 }
1873                 icmd = &iocb->iocb;
1874
1875                 /* 2 buffers can be posted per command */
1876                 /* Allocate buffer to post */
1877                 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1878                 if (mp1)
1879                     mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1880                 if (!mp1 || !mp1->virt) {
1881                         kfree(mp1);
1882                         lpfc_sli_release_iocbq(phba, iocb);
1883                         pring->missbufcnt = cnt;
1884                         return cnt;
1885                 }
1886
1887                 INIT_LIST_HEAD(&mp1->list);
1888                 /* Allocate buffer to post */
1889                 if (cnt > 1) {
1890                         mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1891                         if (mp2)
1892                                 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1893                                                             &mp2->phys);
1894                         if (!mp2 || !mp2->virt) {
1895                                 kfree(mp2);
1896                                 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1897                                 kfree(mp1);
1898                                 lpfc_sli_release_iocbq(phba, iocb);
1899                                 pring->missbufcnt = cnt;
1900                                 return cnt;
1901                         }
1902
1903                         INIT_LIST_HEAD(&mp2->list);
1904                 } else {
1905                         mp2 = NULL;
1906                 }
1907
1908                 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1909                 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1910                 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1911                 icmd->ulpBdeCount = 1;
1912                 cnt--;
1913                 if (mp2) {
1914                         icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1915                         icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1916                         icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1917                         cnt--;
1918                         icmd->ulpBdeCount = 2;
1919                 }
1920
1921                 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1922                 icmd->ulpLe = 1;
1923
1924                 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1925                     IOCB_ERROR) {
1926                         lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1927                         kfree(mp1);
1928                         cnt++;
1929                         if (mp2) {
1930                                 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1931                                 kfree(mp2);
1932                                 cnt++;
1933                         }
1934                         lpfc_sli_release_iocbq(phba, iocb);
1935                         pring->missbufcnt = cnt;
1936                         return cnt;
1937                 }
1938                 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
1939                 if (mp2)
1940                         lpfc_sli_ringpostbuf_put(phba, pring, mp2);
1941         }
1942         pring->missbufcnt = 0;
1943         return 0;
1944 }
1945
1946 /**
1947  * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
1948  * @phba: pointer to lpfc hba data structure.
1949  *
1950  * This routine posts initial receive IOCB buffers to the ELS ring. The
1951  * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1952  * set to 64 IOCBs.
1953  *
1954  * Return codes
1955  *   0 - success (currently always success)
1956  **/
1957 static int
1958 lpfc_post_rcv_buf(struct lpfc_hba *phba)
1959 {
1960         struct lpfc_sli *psli = &phba->sli;
1961
1962         /* Ring 0, ELS / CT buffers */
1963         lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
1964         /* Ring 2 - FCP no buffers needed */
1965
1966         return 0;
1967 }
1968
1969 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1970
1971 /**
1972  * lpfc_sha_init - Set up initial array of hash table entries
1973  * @HashResultPointer: pointer to an array as hash table.
1974  *
1975  * This routine sets up the initial values to the array of hash table entries
1976  * for the LC HBAs.
1977  **/
1978 static void
1979 lpfc_sha_init(uint32_t * HashResultPointer)
1980 {
1981         HashResultPointer[0] = 0x67452301;
1982         HashResultPointer[1] = 0xEFCDAB89;
1983         HashResultPointer[2] = 0x98BADCFE;
1984         HashResultPointer[3] = 0x10325476;
1985         HashResultPointer[4] = 0xC3D2E1F0;
1986 }
1987
1988 /**
1989  * lpfc_sha_iterate - Iterate initial hash table with the working hash table
1990  * @HashResultPointer: pointer to an initial/result hash table.
1991  * @HashWorkingPointer: pointer to an working hash table.
1992  *
1993  * This routine iterates an initial hash table pointed by @HashResultPointer
1994  * with the values from the working hash table pointeed by @HashWorkingPointer.
1995  * The results are putting back to the initial hash table, returned through
1996  * the @HashResultPointer as the result hash table.
1997  **/
1998 static void
1999 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2000 {
2001         int t;
2002         uint32_t TEMP;
2003         uint32_t A, B, C, D, E;
2004         t = 16;
2005         do {
2006                 HashWorkingPointer[t] =
2007                     S(1,
2008                       HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2009                                                                      8] ^
2010                       HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2011         } while (++t <= 79);
2012         t = 0;
2013         A = HashResultPointer[0];
2014         B = HashResultPointer[1];
2015         C = HashResultPointer[2];
2016         D = HashResultPointer[3];
2017         E = HashResultPointer[4];
2018
2019         do {
2020                 if (t < 20) {
2021                         TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2022                 } else if (t < 40) {
2023                         TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2024                 } else if (t < 60) {
2025                         TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2026                 } else {
2027                         TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2028                 }
2029                 TEMP += S(5, A) + E + HashWorkingPointer[t];
2030                 E = D;
2031                 D = C;
2032                 C = S(30, B);
2033                 B = A;
2034                 A = TEMP;
2035         } while (++t <= 79);
2036
2037         HashResultPointer[0] += A;
2038         HashResultPointer[1] += B;
2039         HashResultPointer[2] += C;
2040         HashResultPointer[3] += D;
2041         HashResultPointer[4] += E;
2042
2043 }
2044
2045 /**
2046  * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
2047  * @RandomChallenge: pointer to the entry of host challenge random number array.
2048  * @HashWorking: pointer to the entry of the working hash array.
2049  *
2050  * This routine calculates the working hash array referred by @HashWorking
2051  * from the challenge random numbers associated with the host, referred by
2052  * @RandomChallenge. The result is put into the entry of the working hash
2053  * array and returned by reference through @HashWorking.
2054  **/
2055 static void
2056 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2057 {
2058         *HashWorking = (*RandomChallenge ^ *HashWorking);
2059 }
2060
2061 /**
2062  * lpfc_hba_init - Perform special handling for LC HBA initialization
2063  * @phba: pointer to lpfc hba data structure.
2064  * @hbainit: pointer to an array of unsigned 32-bit integers.
2065  *
2066  * This routine performs the special handling for LC HBA initialization.
2067  **/
2068 void
2069 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2070 {
2071         int t;
2072         uint32_t *HashWorking;
2073         uint32_t *pwwnn = (uint32_t *) phba->wwnn;
2074
2075         HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
2076         if (!HashWorking)
2077                 return;
2078
2079         HashWorking[0] = HashWorking[78] = *pwwnn++;
2080         HashWorking[1] = HashWorking[79] = *pwwnn;
2081
2082         for (t = 0; t < 7; t++)
2083                 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2084
2085         lpfc_sha_init(hbainit);
2086         lpfc_sha_iterate(hbainit, HashWorking);
2087         kfree(HashWorking);
2088 }
2089
2090 /**
2091  * lpfc_cleanup - Performs vport cleanups before deleting a vport
2092  * @vport: pointer to a virtual N_Port data structure.
2093  *
2094  * This routine performs the necessary cleanups before deleting the @vport.
2095  * It invokes the discovery state machine to perform necessary state
2096  * transitions and to release the ndlps associated with the @vport. Note,
2097  * the physical port is treated as @vport 0.
2098  **/
2099 void
2100 lpfc_cleanup(struct lpfc_vport *vport)
2101 {
2102         struct lpfc_hba   *phba = vport->phba;
2103         struct lpfc_nodelist *ndlp, *next_ndlp;
2104         int i = 0;
2105
2106         if (phba->link_state > LPFC_LINK_DOWN)
2107                 lpfc_port_link_failure(vport);
2108
2109         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2110                 if (!NLP_CHK_NODE_ACT(ndlp)) {
2111                         ndlp = lpfc_enable_node(vport, ndlp,
2112                                                 NLP_STE_UNUSED_NODE);
2113                         if (!ndlp)
2114                                 continue;
2115                         spin_lock_irq(&phba->ndlp_lock);
2116                         NLP_SET_FREE_REQ(ndlp);
2117                         spin_unlock_irq(&phba->ndlp_lock);
2118                         /* Trigger the release of the ndlp memory */
2119                         lpfc_nlp_put(ndlp);
2120                         continue;
2121                 }
2122                 spin_lock_irq(&phba->ndlp_lock);
2123                 if (NLP_CHK_FREE_REQ(ndlp)) {
2124                         /* The ndlp should not be in memory free mode already */
2125                         spin_unlock_irq(&phba->ndlp_lock);
2126                         continue;
2127                 } else
2128                         /* Indicate request for freeing ndlp memory */
2129                         NLP_SET_FREE_REQ(ndlp);
2130                 spin_unlock_irq(&phba->ndlp_lock);
2131
2132                 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2133                     ndlp->nlp_DID == Fabric_DID) {
2134                         /* Just free up ndlp with Fabric_DID for vports */
2135                         lpfc_nlp_put(ndlp);
2136                         continue;
2137                 }
2138
2139                 if (ndlp->nlp_type & NLP_FABRIC)
2140                         lpfc_disc_state_machine(vport, ndlp, NULL,
2141                                         NLP_EVT_DEVICE_RECOVERY);
2142
2143                 lpfc_disc_state_machine(vport, ndlp, NULL,
2144                                              NLP_EVT_DEVICE_RM);
2145
2146         }
2147
2148         /* At this point, ALL ndlp's should be gone
2149          * because of the previous NLP_EVT_DEVICE_RM.
2150          * Lets wait for this to happen, if needed.
2151          */
2152         while (!list_empty(&vport->fc_nodes)) {
2153                 if (i++ > 3000) {
2154                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2155                                 "0233 Nodelist not empty\n");
2156                         list_for_each_entry_safe(ndlp, next_ndlp,
2157                                                 &vport->fc_nodes, nlp_listp) {
2158                                 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2159                                                 LOG_NODE,
2160                                                 "0282 did:x%x ndlp:x%p "
2161                                                 "usgmap:x%x refcnt:%d\n",
2162                                                 ndlp->nlp_DID, (void *)ndlp,
2163                                                 ndlp->nlp_usg_map,
2164                                                 atomic_read(
2165                                                         &ndlp->kref.refcount));
2166                         }
2167                         break;
2168                 }
2169
2170                 /* Wait for any activity on ndlps to settle */
2171                 msleep(10);
2172         }
2173 }
2174
2175 /**
2176  * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2177  * @vport: pointer to a virtual N_Port data structure.
2178  *
2179  * This routine stops all the timers associated with a @vport. This function
2180  * is invoked before disabling or deleting a @vport. Note that the physical
2181  * port is treated as @vport 0.
2182  **/
2183 void
2184 lpfc_stop_vport_timers(struct lpfc_vport *vport)
2185 {
2186         del_timer_sync(&vport->els_tmofunc);
2187         del_timer_sync(&vport->fc_fdmitmo);
2188         lpfc_can_disctmo(vport);
2189         return;
2190 }
2191
2192 /**
2193  * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2194  * @phba: pointer to lpfc hba data structure.
2195  *
2196  * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2197  * caller of this routine should already hold the host lock.
2198  **/
2199 void
2200 __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2201 {
2202         /* Clear pending FCF rediscovery wait and failover in progress flags */
2203         phba->fcf.fcf_flag &= ~(FCF_REDISC_PEND |
2204                                 FCF_DEAD_DISC |
2205                                 FCF_ACVL_DISC);
2206         /* Now, try to stop the timer */
2207         del_timer(&phba->fcf.redisc_wait);
2208 }
2209
2210 /**
2211  * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2212  * @phba: pointer to lpfc hba data structure.
2213  *
2214  * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2215  * checks whether the FCF rediscovery wait timer is pending with the host
2216  * lock held before proceeding with disabling the timer and clearing the
2217  * wait timer pendig flag.
2218  **/
2219 void
2220 lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2221 {
2222         spin_lock_irq(&phba->hbalock);
2223         if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2224                 /* FCF rediscovery timer already fired or stopped */
2225                 spin_unlock_irq(&phba->hbalock);
2226                 return;
2227         }
2228         __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2229         spin_unlock_irq(&phba->hbalock);
2230 }
2231
2232 /**
2233  * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2234  * @phba: pointer to lpfc hba data structure.
2235  *
2236  * This routine stops all the timers associated with a HBA. This function is
2237  * invoked before either putting a HBA offline or unloading the driver.
2238  **/
2239 void
2240 lpfc_stop_hba_timers(struct lpfc_hba *phba)
2241 {
2242         lpfc_stop_vport_timers(phba->pport);
2243         del_timer_sync(&phba->sli.mbox_tmo);
2244         del_timer_sync(&phba->fabric_block_timer);
2245         del_timer_sync(&phba->eratt_poll);
2246         del_timer_sync(&phba->hb_tmofunc);
2247         phba->hb_outstanding = 0;
2248
2249         switch (phba->pci_dev_grp) {
2250         case LPFC_PCI_DEV_LP:
2251                 /* Stop any LightPulse device specific driver timers */
2252                 del_timer_sync(&phba->fcp_poll_timer);
2253                 break;
2254         case LPFC_PCI_DEV_OC:
2255                 /* Stop any OneConnect device sepcific driver timers */
2256                 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2257                 break;
2258         default:
2259                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2260                                 "0297 Invalid device group (x%x)\n",
2261                                 phba->pci_dev_grp);
2262                 break;
2263         }
2264         return;
2265 }
2266
2267 /**
2268  * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2269  * @phba: pointer to lpfc hba data structure.
2270  *
2271  * This routine marks a HBA's management interface as blocked. Once the HBA's
2272  * management interface is marked as blocked, all the user space access to
2273  * the HBA, whether they are from sysfs interface or libdfc interface will
2274  * all be blocked. The HBA is set to block the management interface when the
2275  * driver prepares the HBA interface for online or offline.
2276  **/
2277 static void
2278 lpfc_block_mgmt_io(struct lpfc_hba * phba)
2279 {
2280         unsigned long iflag;
2281
2282         spin_lock_irqsave(&phba->hbalock, iflag);
2283         phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2284         spin_unlock_irqrestore(&phba->hbalock, iflag);
2285 }
2286
2287 /**
2288  * lpfc_online - Initialize and bring a HBA online
2289  * @phba: pointer to lpfc hba data structure.
2290  *
2291  * This routine initializes the HBA and brings a HBA online. During this
2292  * process, the management interface is blocked to prevent user space access
2293  * to the HBA interfering with the driver initialization.
2294  *
2295  * Return codes
2296  *   0 - successful
2297  *   1 - failed
2298  **/
2299 int
2300 lpfc_online(struct lpfc_hba *phba)
2301 {
2302         struct lpfc_vport *vport;
2303         struct lpfc_vport **vports;
2304         int i;
2305
2306         if (!phba)
2307                 return 0;
2308         vport = phba->pport;
2309
2310         if (!(vport->fc_flag & FC_OFFLINE_MODE))
2311                 return 0;
2312
2313         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2314                         "0458 Bring Adapter online\n");
2315
2316         lpfc_block_mgmt_io(phba);
2317
2318         if (!lpfc_sli_queue_setup(phba)) {
2319                 lpfc_unblock_mgmt_io(phba);
2320                 return 1;
2321         }
2322
2323         if (phba->sli_rev == LPFC_SLI_REV4) {
2324                 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2325                         lpfc_unblock_mgmt_io(phba);
2326                         return 1;
2327                 }
2328         } else {
2329                 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2330                         lpfc_unblock_mgmt_io(phba);
2331                         return 1;
2332                 }
2333         }
2334
2335         vports = lpfc_create_vport_work_array(phba);
2336         if (vports != NULL)
2337                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2338                         struct Scsi_Host *shost;
2339                         shost = lpfc_shost_from_vport(vports[i]);
2340                         spin_lock_irq(shost->host_lock);
2341                         vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2342                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2343                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2344                         if (phba->sli_rev == LPFC_SLI_REV4)
2345                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
2346                         spin_unlock_irq(shost->host_lock);
2347                 }
2348                 lpfc_destroy_vport_work_array(phba, vports);
2349
2350         lpfc_unblock_mgmt_io(phba);
2351         return 0;
2352 }
2353
2354 /**
2355  * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
2356  * @phba: pointer to lpfc hba data structure.
2357  *
2358  * This routine marks a HBA's management interface as not blocked. Once the
2359  * HBA's management interface is marked as not blocked, all the user space
2360  * access to the HBA, whether they are from sysfs interface or libdfc
2361  * interface will be allowed. The HBA is set to block the management interface
2362  * when the driver prepares the HBA interface for online or offline and then
2363  * set to unblock the management interface afterwards.
2364  **/
2365 void
2366 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2367 {
2368         unsigned long iflag;
2369
2370         spin_lock_irqsave(&phba->hbalock, iflag);
2371         phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2372         spin_unlock_irqrestore(&phba->hbalock, iflag);
2373 }
2374
2375 /**
2376  * lpfc_offline_prep - Prepare a HBA to be brought offline
2377  * @phba: pointer to lpfc hba data structure.
2378  *
2379  * This routine is invoked to prepare a HBA to be brought offline. It performs
2380  * unregistration login to all the nodes on all vports and flushes the mailbox
2381  * queue to make it ready to be brought offline.
2382  **/
2383 void
2384 lpfc_offline_prep(struct lpfc_hba * phba)
2385 {
2386         struct lpfc_vport *vport = phba->pport;
2387         struct lpfc_nodelist  *ndlp, *next_ndlp;
2388         struct lpfc_vport **vports;
2389         struct Scsi_Host *shost;
2390         int i;
2391
2392         if (vport->fc_flag & FC_OFFLINE_MODE)
2393                 return;
2394
2395         lpfc_block_mgmt_io(phba);
2396
2397         lpfc_linkdown(phba);
2398
2399         /* Issue an unreg_login to all nodes on all vports */
2400         vports = lpfc_create_vport_work_array(phba);
2401         if (vports != NULL) {
2402                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2403                         if (vports[i]->load_flag & FC_UNLOADING)
2404                                 continue;
2405                         shost = lpfc_shost_from_vport(vports[i]);
2406                         spin_lock_irq(shost->host_lock);
2407                         vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
2408                         vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2409                         vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
2410                         spin_unlock_irq(shost->host_lock);
2411
2412                         shost = lpfc_shost_from_vport(vports[i]);
2413                         list_for_each_entry_safe(ndlp, next_ndlp,
2414                                                  &vports[i]->fc_nodes,
2415                                                  nlp_listp) {
2416                                 if (!NLP_CHK_NODE_ACT(ndlp))
2417                                         continue;
2418                                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2419                                         continue;
2420                                 if (ndlp->nlp_type & NLP_FABRIC) {
2421                                         lpfc_disc_state_machine(vports[i], ndlp,
2422                                                 NULL, NLP_EVT_DEVICE_RECOVERY);
2423                                         lpfc_disc_state_machine(vports[i], ndlp,
2424                                                 NULL, NLP_EVT_DEVICE_RM);
2425                                 }
2426                                 spin_lock_irq(shost->host_lock);
2427                                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2428                                 spin_unlock_irq(shost->host_lock);
2429                                 lpfc_unreg_rpi(vports[i], ndlp);
2430                         }
2431                 }
2432         }
2433         lpfc_destroy_vport_work_array(phba, vports);
2434
2435         lpfc_sli_mbox_sys_shutdown(phba);
2436 }
2437
2438 /**
2439  * lpfc_offline - Bring a HBA offline
2440  * @phba: pointer to lpfc hba data structure.
2441  *
2442  * This routine actually brings a HBA offline. It stops all the timers
2443  * associated with the HBA, brings down the SLI layer, and eventually
2444  * marks the HBA as in offline state for the upper layer protocol.
2445  **/
2446 void
2447 lpfc_offline(struct lpfc_hba *phba)
2448 {
2449         struct Scsi_Host  *shost;
2450         struct lpfc_vport **vports;
2451         int i;
2452
2453         if (phba->pport->fc_flag & FC_OFFLINE_MODE)
2454                 return;
2455
2456         /* stop port and all timers associated with this hba */
2457         lpfc_stop_port(phba);
2458         vports = lpfc_create_vport_work_array(phba);
2459         if (vports != NULL)
2460                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
2461                         lpfc_stop_vport_timers(vports[i]);
2462         lpfc_destroy_vport_work_array(phba, vports);
2463         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2464                         "0460 Bring Adapter offline\n");
2465         /* Bring down the SLI Layer and cleanup.  The HBA is offline
2466            now.  */
2467         lpfc_sli_hba_down(phba);
2468         spin_lock_irq(&phba->hbalock);
2469         phba->work_ha = 0;
2470         spin_unlock_irq(&phba->hbalock);
2471         vports = lpfc_create_vport_work_array(phba);
2472         if (vports != NULL)
2473                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2474                         shost = lpfc_shost_from_vport(vports[i]);
2475                         spin_lock_irq(shost->host_lock);
2476                         vports[i]->work_port_events = 0;
2477                         vports[i]->fc_flag |= FC_OFFLINE_MODE;
2478                         spin_unlock_irq(shost->host_lock);
2479                 }
2480         lpfc_destroy_vport_work_array(phba, vports);
2481 }
2482
2483 /**
2484  * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
2485  * @phba: pointer to lpfc hba data structure.
2486  *
2487  * This routine is to free all the SCSI buffers and IOCBs from the driver
2488  * list back to kernel. It is called from lpfc_pci_remove_one to free
2489  * the internal resources before the device is removed from the system.
2490  *
2491  * Return codes
2492  *   0 - successful (for now, it always returns 0)
2493  **/
2494 static int
2495 lpfc_scsi_free(struct lpfc_hba *phba)
2496 {
2497         struct lpfc_scsi_buf *sb, *sb_next;
2498         struct lpfc_iocbq *io, *io_next;
2499
2500         spin_lock_irq(&phba->hbalock);
2501         /* Release all the lpfc_scsi_bufs maintained by this host. */
2502         spin_lock(&phba->scsi_buf_list_lock);
2503         list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2504                 list_del(&sb->list);
2505                 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2506                               sb->dma_handle);
2507                 kfree(sb);
2508                 phba->total_scsi_bufs--;
2509         }
2510         spin_unlock(&phba->scsi_buf_list_lock);
2511
2512         /* Release all the lpfc_iocbq entries maintained by this host. */
2513         list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2514                 list_del(&io->list);
2515                 kfree(io);
2516                 phba->total_iocbq_bufs--;
2517         }
2518         spin_unlock_irq(&phba->hbalock);
2519         return 0;
2520 }
2521
2522 /**
2523  * lpfc_create_port - Create an FC port
2524  * @phba: pointer to lpfc hba data structure.
2525  * @instance: a unique integer ID to this FC port.
2526  * @dev: pointer to the device data structure.
2527  *
2528  * This routine creates a FC port for the upper layer protocol. The FC port
2529  * can be created on top of either a physical port or a virtual port provided
2530  * by the HBA. This routine also allocates a SCSI host data structure (shost)
2531  * and associates the FC port created before adding the shost into the SCSI
2532  * layer.
2533  *
2534  * Return codes
2535  *   @vport - pointer to the virtual N_Port data structure.
2536  *   NULL - port create failed.
2537  **/
2538 struct lpfc_vport *
2539 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
2540 {
2541         struct lpfc_vport *vport;
2542         struct Scsi_Host  *shost;
2543         int error = 0;
2544
2545         if (dev != &phba->pcidev->dev)
2546                 shost = scsi_host_alloc(&lpfc_vport_template,
2547                                         sizeof(struct lpfc_vport));
2548         else
2549                 shost = scsi_host_alloc(&lpfc_template,
2550                                         sizeof(struct lpfc_vport));
2551         if (!shost)
2552                 goto out;
2553
2554         vport = (struct lpfc_vport *) shost->hostdata;
2555         vport->phba = phba;
2556         vport->load_flag |= FC_LOADING;
2557         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2558         vport->fc_rscn_flush = 0;
2559
2560         lpfc_get_vport_cfgparam(vport);
2561         shost->unique_id = instance;
2562         shost->max_id = LPFC_MAX_TARGET;
2563         shost->max_lun = vport->cfg_max_luns;
2564         shost->this_id = -1;
2565         shost->max_cmd_len = 16;
2566         if (phba->sli_rev == LPFC_SLI_REV4) {
2567                 shost->dma_boundary =
2568                         phba->sli4_hba.pc_sli4_params.sge_supp_len;
2569                 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2570         }
2571
2572         /*
2573          * Set initial can_queue value since 0 is no longer supported and
2574          * scsi_add_host will fail. This will be adjusted later based on the
2575          * max xri value determined in hba setup.
2576          */
2577         shost->can_queue = phba->cfg_hba_queue_depth - 10;
2578         if (dev != &phba->pcidev->dev) {
2579                 shost->transportt = lpfc_vport_transport_template;
2580                 vport->port_type = LPFC_NPIV_PORT;
2581         } else {
2582                 shost->transportt = lpfc_transport_template;
2583                 vport->port_type = LPFC_PHYSICAL_PORT;
2584         }
2585
2586         /* Initialize all internally managed lists. */
2587         INIT_LIST_HEAD(&vport->fc_nodes);
2588         INIT_LIST_HEAD(&vport->rcv_buffer_list);
2589         spin_lock_init(&vport->work_port_lock);
2590
2591         init_timer(&vport->fc_disctmo);
2592         vport->fc_disctmo.function = lpfc_disc_timeout;
2593         vport->fc_disctmo.data = (unsigned long)vport;
2594
2595         init_timer(&vport->fc_fdmitmo);
2596         vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
2597         vport->fc_fdmitmo.data = (unsigned long)vport;
2598
2599         init_timer(&vport->els_tmofunc);
2600         vport->els_tmofunc.function = lpfc_els_timeout;
2601         vport->els_tmofunc.data = (unsigned long)vport;
2602         if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
2603                 phba->menlo_flag |= HBA_MENLO_SUPPORT;
2604                 /* check for menlo minimum sg count */
2605                 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT) {
2606                         phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
2607                         shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2608                 }
2609         }
2610
2611         error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
2612         if (error)
2613                 goto out_put_shost;
2614
2615         spin_lock_irq(&phba->hbalock);
2616         list_add_tail(&vport->listentry, &phba->port_list);
2617         spin_unlock_irq(&phba->hbalock);
2618         return vport;
2619
2620 out_put_shost:
2621         scsi_host_put(shost);
2622 out:
2623         return NULL;
2624 }
2625
2626 /**
2627  * destroy_port -  destroy an FC port
2628  * @vport: pointer to an lpfc virtual N_Port data structure.
2629  *
2630  * This routine destroys a FC port from the upper layer protocol. All the
2631  * resources associated with the port are released.
2632  **/
2633 void
2634 destroy_port(struct lpfc_vport *vport)
2635 {
2636         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2637         struct lpfc_hba  *phba = vport->phba;
2638
2639         lpfc_debugfs_terminate(vport);
2640         fc_remove_host(shost);
2641         scsi_remove_host(shost);
2642
2643         spin_lock_irq(&phba->hbalock);
2644         list_del_init(&vport->listentry);
2645         spin_unlock_irq(&phba->hbalock);
2646
2647         lpfc_cleanup(vport);
2648         return;
2649 }
2650
2651 /**
2652  * lpfc_get_instance - Get a unique integer ID
2653  *
2654  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2655  * uses the kernel idr facility to perform the task.
2656  *
2657  * Return codes:
2658  *   instance - a unique integer ID allocated as the new instance.
2659  *   -1 - lpfc get instance failed.
2660  **/
2661 int
2662 lpfc_get_instance(void)
2663 {
2664         int instance = 0;
2665
2666         /* Assign an unused number */
2667         if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2668                 return -1;
2669         if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2670                 return -1;
2671         return instance;
2672 }
2673
2674 /**
2675  * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
2676  * @shost: pointer to SCSI host data structure.
2677  * @time: elapsed time of the scan in jiffies.
2678  *
2679  * This routine is called by the SCSI layer with a SCSI host to determine
2680  * whether the scan host is finished.
2681  *
2682  * Note: there is no scan_start function as adapter initialization will have
2683  * asynchronously kicked off the link initialization.
2684  *
2685  * Return codes
2686  *   0 - SCSI host scan is not over yet.
2687  *   1 - SCSI host scan is over.
2688  **/
2689 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2690 {
2691         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2692         struct lpfc_hba   *phba = vport->phba;
2693         int stat = 0;
2694
2695         spin_lock_irq(shost->host_lock);
2696
2697         if (vport->load_flag & FC_UNLOADING) {
2698                 stat = 1;
2699                 goto finished;
2700         }
2701         if (time >= 30 * HZ) {
2702                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2703                                 "0461 Scanning longer than 30 "
2704                                 "seconds.  Continuing initialization\n");
2705                 stat = 1;
2706                 goto finished;
2707         }
2708         if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2709                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2710                                 "0465 Link down longer than 15 "
2711                                 "seconds.  Continuing initialization\n");
2712                 stat = 1;
2713                 goto finished;
2714         }
2715
2716         if (vport->port_state != LPFC_VPORT_READY)
2717                 goto finished;
2718         if (vport->num_disc_nodes || vport->fc_prli_sent)
2719                 goto finished;
2720         if (vport->fc_map_cnt == 0 && time < 2 * HZ)
2721                 goto finished;
2722         if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
2723                 goto finished;
2724
2725         stat = 1;
2726
2727 finished:
2728         spin_unlock_irq(shost->host_lock);
2729         return stat;
2730 }
2731
2732 /**
2733  * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
2734  * @shost: pointer to SCSI host data structure.
2735  *
2736  * This routine initializes a given SCSI host attributes on a FC port. The
2737  * SCSI host can be either on top of a physical port or a virtual port.
2738  **/
2739 void lpfc_host_attrib_init(struct Scsi_Host *shost)
2740 {
2741         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2742         struct lpfc_hba   *phba = vport->phba;
2743         /*
2744          * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
2745          */
2746
2747         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2748         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
2749         fc_host_supported_classes(shost) = FC_COS_CLASS3;
2750
2751         memset(fc_host_supported_fc4s(shost), 0,
2752                sizeof(fc_host_supported_fc4s(shost)));
2753         fc_host_supported_fc4s(shost)[2] = 1;
2754         fc_host_supported_fc4s(shost)[7] = 1;
2755
2756         lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2757                                  sizeof fc_host_symbolic_name(shost));
2758
2759         fc_host_supported_speeds(shost) = 0;
2760         if (phba->lmt & LMT_10Gb)
2761                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
2762         if (phba->lmt & LMT_8Gb)
2763                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
2764         if (phba->lmt & LMT_4Gb)
2765                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2766         if (phba->lmt & LMT_2Gb)
2767                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2768         if (phba->lmt & LMT_1Gb)
2769                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2770
2771         fc_host_maxframe_size(shost) =
2772                 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2773                 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
2774
2775         /* This value is also unchanging */
2776         memset(fc_host_active_fc4s(shost), 0,
2777                sizeof(fc_host_active_fc4s(shost)));
2778         fc_host_active_fc4s(shost)[2] = 1;
2779         fc_host_active_fc4s(shost)[7] = 1;
2780
2781         fc_host_max_npiv_vports(shost) = phba->max_vpi;
2782         spin_lock_irq(shost->host_lock);
2783         vport->load_flag &= ~FC_LOADING;
2784         spin_unlock_irq(shost->host_lock);
2785 }
2786
2787 /**
2788  * lpfc_stop_port_s3 - Stop SLI3 device port
2789  * @phba: pointer to lpfc hba data structure.
2790  *
2791  * This routine is invoked to stop an SLI3 device port, it stops the device
2792  * from generating interrupts and stops the device driver's timers for the
2793  * device.
2794  **/
2795 static void
2796 lpfc_stop_port_s3(struct lpfc_hba *phba)
2797 {
2798         /* Clear all interrupt enable conditions */
2799         writel(0, phba->HCregaddr);
2800         readl(phba->HCregaddr); /* flush */
2801         /* Clear all pending interrupts */
2802         writel(0xffffffff, phba->HAregaddr);
2803         readl(phba->HAregaddr); /* flush */
2804
2805         /* Reset some HBA SLI setup states */
2806         lpfc_stop_hba_timers(phba);
2807         phba->pport->work_port_events = 0;
2808 }
2809
2810 /**
2811  * lpfc_stop_port_s4 - Stop SLI4 device port
2812  * @phba: pointer to lpfc hba data structure.
2813  *
2814  * This routine is invoked to stop an SLI4 device port, it stops the device
2815  * from generating interrupts and stops the device driver's timers for the
2816  * device.
2817  **/
2818 static void
2819 lpfc_stop_port_s4(struct lpfc_hba *phba)
2820 {
2821         /* Reset some HBA SLI4 setup states */
2822         lpfc_stop_hba_timers(phba);
2823         phba->pport->work_port_events = 0;
2824         phba->sli4_hba.intr_enable = 0;
2825 }
2826
2827 /**
2828  * lpfc_stop_port - Wrapper function for stopping hba port
2829  * @phba: Pointer to HBA context object.
2830  *
2831  * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2832  * the API jump table function pointer from the lpfc_hba struct.
2833  **/
2834 void
2835 lpfc_stop_port(struct lpfc_hba *phba)
2836 {
2837         phba->lpfc_stop_port(phba);
2838 }
2839
2840 /**
2841  * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2842  * @phba: pointer to lpfc hba data structure.
2843  *
2844  * This routine is invoked to remove the driver default fcf record from
2845  * the port.  This routine currently acts on FCF Index 0.
2846  *
2847  **/
2848 void
2849 lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2850 {
2851         int rc = 0;
2852         LPFC_MBOXQ_t *mboxq;
2853         struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2854         uint32_t mbox_tmo, req_len;
2855         uint32_t shdr_status, shdr_add_status;
2856
2857         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2858         if (!mboxq) {
2859                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2860                         "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2861                 return;
2862         }
2863
2864         req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2865                   sizeof(struct lpfc_sli4_cfg_mhdr);
2866         rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2867                               LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2868                               req_len, LPFC_SLI4_MBX_EMBED);
2869         /*
2870          * In phase 1, there is a single FCF index, 0.  In phase2, the driver
2871          * supports multiple FCF indices.
2872          */
2873         del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2874         bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2875         bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
2876                phba->fcf.current_rec.fcf_indx);
2877
2878         if (!phba->sli4_hba.intr_enable)
2879                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2880         else {
2881                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2882                 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2883         }
2884         /* The IOCTL status is embedded in the mailbox subheader. */
2885         shdr_status = bf_get(lpfc_mbox_hdr_status,
2886                              &del_fcf_record->header.cfg_shdr.response);
2887         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2888                                  &del_fcf_record->header.cfg_shdr.response);
2889         if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2890                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2891                                 "2516 DEL FCF of default FCF Index failed "
2892                                 "mbx status x%x, status x%x add_status x%x\n",
2893                                 rc, shdr_status, shdr_add_status);
2894         }
2895         if (rc != MBX_TIMEOUT)
2896                 mempool_free(mboxq, phba->mbox_mem_pool);
2897 }
2898
2899 /**
2900  * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2901  * @phba: Pointer to hba for which this call is being executed.
2902  *
2903  * This routine starts the timer waiting for the FCF rediscovery to complete.
2904  **/
2905 void
2906 lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2907 {
2908         unsigned long fcf_redisc_wait_tmo =
2909                 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2910         /* Start fcf rediscovery wait period timer */
2911         mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2912         spin_lock_irq(&phba->hbalock);
2913         /* Allow action to new fcf asynchronous event */
2914         phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2915         /* Mark the FCF rediscovery pending state */
2916         phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2917         spin_unlock_irq(&phba->hbalock);
2918 }
2919
2920 /**
2921  * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2922  * @ptr: Map to lpfc_hba data structure pointer.
2923  *
2924  * This routine is invoked when waiting for FCF table rediscover has been
2925  * timed out. If new FCF record(s) has (have) been discovered during the
2926  * wait period, a new FCF event shall be added to the FCOE async event
2927  * list, and then worker thread shall be waked up for processing from the
2928  * worker thread context.
2929  **/
2930 void
2931 lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2932 {
2933         struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2934
2935         /* Don't send FCF rediscovery event if timer cancelled */
2936         spin_lock_irq(&phba->hbalock);
2937         if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2938                 spin_unlock_irq(&phba->hbalock);
2939                 return;
2940         }
2941         /* Clear FCF rediscovery timer pending flag */
2942         phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2943         /* FCF rediscovery event to worker thread */
2944         phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2945         spin_unlock_irq(&phba->hbalock);
2946         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2947                         "2776 FCF rediscover wait timer expired, post "
2948                         "a worker thread event for FCF table scan\n");
2949         /* wake up worker thread */
2950         lpfc_worker_wake_up(phba);
2951 }
2952
2953 /**
2954  * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2955  * @phba: pointer to lpfc hba data structure.
2956  *
2957  * This function uses the QUERY_FW_CFG mailbox command to determine if the
2958  * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2959  * was successful and the firmware supports FCoE. Any other return indicates
2960  * a error. It is assumed that this function will be called before interrupts
2961  * are enabled.
2962  **/
2963 static int
2964 lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2965 {
2966         int rc = 0;
2967         LPFC_MBOXQ_t *mboxq;
2968         struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2969         uint32_t length;
2970         uint32_t shdr_status, shdr_add_status;
2971
2972         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2973         if (!mboxq) {
2974                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2975                                 "2621 Failed to allocate mbox for "
2976                                 "query firmware config cmd\n");
2977                 return -ENOMEM;
2978         }
2979         query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2980         length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2981                   sizeof(struct lpfc_sli4_cfg_mhdr));
2982         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2983                          LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2984                          length, LPFC_SLI4_MBX_EMBED);
2985         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2986         /* The IOCTL status is embedded in the mailbox subheader. */
2987         shdr_status = bf_get(lpfc_mbox_hdr_status,
2988                              &query_fw_cfg->header.cfg_shdr.response);
2989         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2990                                  &query_fw_cfg->header.cfg_shdr.response);
2991         if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2992                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2993                                 "2622 Query Firmware Config failed "
2994                                 "mbx status x%x, status x%x add_status x%x\n",
2995                                 rc, shdr_status, shdr_add_status);
2996                 return -EINVAL;
2997         }
2998         if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2999                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3000                                 "2623 FCoE Function not supported by firmware. "
3001                                 "Function mode = %08x\n",
3002                                 query_fw_cfg->function_mode);
3003                 return -EINVAL;
3004         }
3005         if (rc != MBX_TIMEOUT)
3006                 mempool_free(mboxq, phba->mbox_mem_pool);
3007         return 0;
3008 }
3009
3010 /**
3011  * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3012  * @phba: pointer to lpfc hba data structure.
3013  * @acqe_link: pointer to the async link completion queue entry.
3014  *
3015  * This routine is to parse the SLI4 link-attention link fault code and
3016  * translate it into the base driver's read link attention mailbox command
3017  * status.
3018  *
3019  * Return: Link-attention status in terms of base driver's coding.
3020  **/
3021 static uint16_t
3022 lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3023                            struct lpfc_acqe_link *acqe_link)
3024 {
3025         uint16_t latt_fault;
3026
3027         switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3028         case LPFC_ASYNC_LINK_FAULT_NONE:
3029         case LPFC_ASYNC_LINK_FAULT_LOCAL:
3030         case LPFC_ASYNC_LINK_FAULT_REMOTE:
3031                 latt_fault = 0;
3032                 break;
3033         default:
3034                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3035                                 "0398 Invalid link fault code: x%x\n",
3036                                 bf_get(lpfc_acqe_link_fault, acqe_link));
3037                 latt_fault = MBXERR_ERROR;
3038                 break;
3039         }
3040         return latt_fault;
3041 }
3042
3043 /**
3044  * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3045  * @phba: pointer to lpfc hba data structure.
3046  * @acqe_link: pointer to the async link completion queue entry.
3047  *
3048  * This routine is to parse the SLI4 link attention type and translate it
3049  * into the base driver's link attention type coding.
3050  *
3051  * Return: Link attention type in terms of base driver's coding.
3052  **/
3053 static uint8_t
3054 lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3055                           struct lpfc_acqe_link *acqe_link)
3056 {
3057         uint8_t att_type;
3058
3059         switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3060         case LPFC_ASYNC_LINK_STATUS_DOWN:
3061         case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3062                 att_type = AT_LINK_DOWN;
3063                 break;
3064         case LPFC_ASYNC_LINK_STATUS_UP:
3065                 /* Ignore physical link up events - wait for logical link up */
3066                 att_type = AT_RESERVED;
3067                 break;
3068         case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3069                 att_type = AT_LINK_UP;
3070                 break;
3071         default:
3072                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3073                                 "0399 Invalid link attention type: x%x\n",
3074                                 bf_get(lpfc_acqe_link_status, acqe_link));
3075                 att_type = AT_RESERVED;
3076                 break;
3077         }
3078         return att_type;
3079 }
3080
3081 /**
3082  * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3083  * @phba: pointer to lpfc hba data structure.
3084  * @acqe_link: pointer to the async link completion queue entry.
3085  *
3086  * This routine is to parse the SLI4 link-attention link speed and translate
3087  * it into the base driver's link-attention link speed coding.
3088  *
3089  * Return: Link-attention link speed in terms of base driver's coding.
3090  **/
3091 static uint8_t
3092 lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3093                                 struct lpfc_acqe_link *acqe_link)
3094 {
3095         uint8_t link_speed;
3096
3097         switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3098         case LPFC_ASYNC_LINK_SPEED_ZERO:
3099                 link_speed = LA_UNKNW_LINK;
3100                 break;
3101         case LPFC_ASYNC_LINK_SPEED_10MBPS:
3102                 link_speed = LA_UNKNW_LINK;
3103                 break;
3104         case LPFC_ASYNC_LINK_SPEED_100MBPS:
3105                 link_speed = LA_UNKNW_LINK;
3106                 break;
3107         case LPFC_ASYNC_LINK_SPEED_1GBPS:
3108                 link_speed = LA_1GHZ_LINK;
3109                 break;
3110         case LPFC_ASYNC_LINK_SPEED_10GBPS:
3111                 link_speed = LA_10GHZ_LINK;
3112                 break;
3113         default:
3114                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3115                                 "0483 Invalid link-attention link speed: x%x\n",
3116                                 bf_get(lpfc_acqe_link_speed, acqe_link));
3117                 link_speed = LA_UNKNW_LINK;
3118                 break;
3119         }
3120         return link_speed;
3121 }
3122
3123 /**
3124  * lpfc_sli4_async_link_evt - Process the asynchronous link event
3125  * @phba: pointer to lpfc hba data structure.
3126  * @acqe_link: pointer to the async link completion queue entry.
3127  *
3128  * This routine is to handle the SLI4 asynchronous link event.
3129  **/
3130 static void
3131 lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3132                          struct lpfc_acqe_link *acqe_link)
3133 {
3134         struct lpfc_dmabuf *mp;
3135         LPFC_MBOXQ_t *pmb;
3136         MAILBOX_t *mb;
3137         READ_LA_VAR *la;
3138         uint8_t att_type;
3139
3140         att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3141         if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3142                 return;
3143         phba->fcoe_eventtag = acqe_link->event_tag;
3144         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3145         if (!pmb) {
3146                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3147                                 "0395 The mboxq allocation failed\n");
3148                 return;
3149         }
3150         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3151         if (!mp) {
3152                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3153                                 "0396 The lpfc_dmabuf allocation failed\n");
3154                 goto out_free_pmb;
3155         }
3156         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3157         if (!mp->virt) {
3158                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3159                                 "0397 The mbuf allocation failed\n");
3160                 goto out_free_dmabuf;
3161         }
3162
3163         /* Cleanup any outstanding ELS commands */
3164         lpfc_els_flush_all_cmd(phba);
3165
3166         /* Block ELS IOCBs until we have done process link event */
3167         phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3168
3169         /* Update link event statistics */
3170         phba->sli.slistat.link_event++;
3171
3172         /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3173         lpfc_read_la(phba, pmb, mp);
3174         pmb->vport = phba->pport;
3175
3176         /* Parse and translate status field */
3177         mb = &pmb->u.mb;
3178         mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3179
3180         /* Parse and translate link attention fields */
3181         la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3182         la->eventTag = acqe_link->event_tag;
3183         la->attType = att_type;
3184         la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3185
3186         /* Fake the the following irrelvant fields */
3187         la->topology = TOPOLOGY_PT_PT;
3188         la->granted_AL_PA = 0;
3189         la->il = 0;
3190         la->pb = 0;
3191         la->fa = 0;
3192         la->mm = 0;
3193
3194         /* Keep the link status for extra SLI4 state machine reference */
3195         phba->sli4_hba.link_state.speed =
3196                                 bf_get(lpfc_acqe_link_speed, acqe_link);
3197         phba->sli4_hba.link_state.duplex =
3198                                 bf_get(lpfc_acqe_link_duplex, acqe_link);
3199         phba->sli4_hba.link_state.status =
3200                                 bf_get(lpfc_acqe_link_status, acqe_link);
3201         phba->sli4_hba.link_state.physical =
3202                                 bf_get(lpfc_acqe_link_physical, acqe_link);
3203         phba->sli4_hba.link_state.fault =
3204                                 bf_get(lpfc_acqe_link_fault, acqe_link);
3205         phba->sli4_hba.link_state.logical_speed =
3206                                 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
3207
3208         /* Invoke the lpfc_handle_latt mailbox command callback function */
3209         lpfc_mbx_cmpl_read_la(phba, pmb);
3210
3211         return;
3212
3213 out_free_dmabuf:
3214         kfree(mp);
3215 out_free_pmb:
3216         mempool_free(pmb, phba->mbox_mem_pool);
3217 }
3218
3219 /**
3220  * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3221  * @vport: pointer to vport data structure.
3222  *
3223  * This routine is to perform Clear Virtual Link (CVL) on a vport in
3224  * response to a CVL event.
3225  *
3226  * Return the pointer to the ndlp with the vport if successful, otherwise
3227  * return NULL.
3228  **/
3229 static struct lpfc_nodelist *
3230 lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3231 {
3232         struct lpfc_nodelist *ndlp;
3233         struct Scsi_Host *shost;
3234         struct lpfc_hba *phba;
3235
3236         if (!vport)
3237                 return NULL;
3238         ndlp = lpfc_findnode_did(vport, Fabric_DID);
3239         if (!ndlp)
3240                 return NULL;
3241         phba = vport->phba;
3242         if (!phba)
3243                 return NULL;
3244         if (phba->pport->port_state <= LPFC_FLOGI)
3245                 return NULL;
3246         /* If virtual link is not yet instantiated ignore CVL */
3247         if (vport->port_state <= LPFC_FDISC)
3248                 return NULL;
3249         shost = lpfc_shost_from_vport(vport);
3250         if (!shost)
3251                 return NULL;
3252         lpfc_linkdown_port(vport);
3253         lpfc_cleanup_pending_mbox(vport);
3254         spin_lock_irq(shost->host_lock);
3255         vport->fc_flag |= FC_VPORT_CVL_RCVD;
3256         spin_unlock_irq(shost->host_lock);
3257
3258         return ndlp;
3259 }
3260
3261 /**
3262  * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3263  * @vport: pointer to lpfc hba data structure.
3264  *
3265  * This routine is to perform Clear Virtual Link (CVL) on all vports in
3266  * response to a FCF dead event.
3267  **/
3268 static void
3269 lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3270 {
3271         struct lpfc_vport **vports;
3272         int i;
3273
3274         vports = lpfc_create_vport_work_array(phba);
3275         if (vports)
3276                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3277                         lpfc_sli4_perform_vport_cvl(vports[i]);
3278         lpfc_destroy_vport_work_array(phba, vports);
3279 }
3280
3281 /**
3282  * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3283  * @phba: pointer to lpfc hba data structure.
3284  * @acqe_link: pointer to the async fcoe completion queue entry.
3285  *
3286  * This routine is to handle the SLI4 asynchronous fcoe event.
3287  **/
3288 static void
3289 lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3290                          struct lpfc_acqe_fcoe *acqe_fcoe)
3291 {
3292         uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3293         int rc;
3294         struct lpfc_vport *vport;
3295         struct lpfc_nodelist *ndlp;
3296         struct Scsi_Host  *shost;
3297         int active_vlink_present;
3298         struct lpfc_vport **vports;
3299         int i;
3300
3301         phba->fc_eventTag = acqe_fcoe->event_tag;
3302         phba->fcoe_eventtag = acqe_fcoe->event_tag;
3303         switch (event_type) {
3304         case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
3305         case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
3306                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3307                         "2546 New FCF found/FCF parameter modified event: "
3308                         "evt_tag:x%x, fcf_index:x%x\n",
3309                         acqe_fcoe->event_tag, acqe_fcoe->index);
3310
3311                 spin_lock_irq(&phba->hbalock);
3312                 if ((phba->fcf.fcf_flag & FCF_SCAN_DONE) ||
3313                     (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3314                         /*
3315                          * If the current FCF is in discovered state or
3316                          * FCF discovery is in progress, do nothing.
3317                          */
3318                         spin_unlock_irq(&phba->hbalock);
3319                         break;
3320                 }
3321
3322                 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3323                         /*
3324                          * If fast FCF failover rescan event is pending,
3325                          * do nothing.
3326                          */
3327                         spin_unlock_irq(&phba->hbalock);
3328                         break;
3329                 }
3330                 spin_unlock_irq(&phba->hbalock);
3331
3332                 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
3333                     !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
3334                         /*
3335                          * During period of FCF discovery, read the FCF
3336                          * table record indexed by the event to update
3337                          * FCF round robin failover eligible FCF bmask.
3338                          */
3339                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3340                                         LOG_DISCOVERY,
3341                                         "2779 Read new FCF record with "
3342                                         "fcf_index:x%x for updating FCF "
3343                                         "round robin failover bmask\n",
3344                                         acqe_fcoe->index);
3345                         rc = lpfc_sli4_read_fcf_rec(phba, acqe_fcoe->index);
3346                 }
3347
3348                 /* Otherwise, scan the entire FCF table and re-discover SAN */
3349                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3350                                 "2770 Start FCF table scan due to new FCF "
3351                                 "event: evt_tag:x%x, fcf_index:x%x\n",
3352                                 acqe_fcoe->event_tag, acqe_fcoe->index);
3353                 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3354                                                      LPFC_FCOE_FCF_GET_FIRST);
3355                 if (rc)
3356                         lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3357                                         "2547 Issue FCF scan read FCF mailbox "
3358                                         "command failed 0x%x\n", rc);
3359                 break;
3360
3361         case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3362                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3363                         "2548 FCF Table full count 0x%x tag 0x%x\n",
3364                         bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3365                         acqe_fcoe->event_tag);
3366                 break;
3367
3368         case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3369                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3370                         "2549 FCF disconnected from network index 0x%x"
3371                         " tag 0x%x\n", acqe_fcoe->index,
3372                         acqe_fcoe->event_tag);
3373                 /* If the event is not for currently used fcf do nothing */
3374                 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
3375                         break;
3376                 /* We request port to rediscover the entire FCF table for
3377                  * a fast recovery from case that the current FCF record
3378                  * is no longer valid if we are not in the middle of FCF
3379                  * failover process already.
3380                  */
3381                 spin_lock_irq(&phba->hbalock);
3382                 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3383                         spin_unlock_irq(&phba->hbalock);
3384                         /* Update FLOGI FCF failover eligible FCF bmask */
3385                         lpfc_sli4_fcf_rr_index_clear(phba, acqe_fcoe->index);
3386                         break;
3387                 }
3388                 /* Mark the fast failover process in progress */
3389                 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
3390                 spin_unlock_irq(&phba->hbalock);
3391                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3392                                 "2771 Start FCF fast failover process due to "
3393                                 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
3394                                 "\n", acqe_fcoe->event_tag, acqe_fcoe->index);
3395                 rc = lpfc_sli4_redisc_fcf_table(phba);
3396                 if (rc) {
3397                         lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3398                                         LOG_DISCOVERY,
3399                                         "2772 Issue FCF rediscover mabilbox "
3400                                         "command failed, fail through to FCF "
3401                                         "dead event\n");
3402                         spin_lock_irq(&phba->hbalock);
3403                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
3404                         spin_unlock_irq(&phba->hbalock);
3405                         /*
3406                          * Last resort will fail over by treating this
3407                          * as a link down to FCF registration.
3408                          */
3409                         lpfc_sli4_fcf_dead_failthrough(phba);
3410                 } else
3411                         /* Handling fast FCF failover to a DEAD FCF event
3412                          * is considered equalivant to receiving CVL to all
3413                          * vports.
3414                          */
3415                         lpfc_sli4_perform_all_vport_cvl(phba);
3416                 break;
3417         case LPFC_FCOE_EVENT_TYPE_CVL:
3418                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3419                         "2718 Clear Virtual Link Received for VPI 0x%x"
3420                         " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3421                 vport = lpfc_find_vport_by_vpid(phba,
3422                                 acqe_fcoe->index - phba->vpi_base);
3423                 ndlp = lpfc_sli4_perform_vport_cvl(vport);
3424                 if (!ndlp)
3425                         break;
3426                 active_vlink_present = 0;
3427
3428                 vports = lpfc_create_vport_work_array(phba);
3429                 if (vports) {
3430                         for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3431                                         i++) {
3432                                 if ((!(vports[i]->fc_flag &
3433                                         FC_VPORT_CVL_RCVD)) &&
3434                                         (vports[i]->port_state > LPFC_FDISC)) {
3435                                         active_vlink_present = 1;
3436                                         break;
3437                                 }
3438                         }
3439                         lpfc_destroy_vport_work_array(phba, vports);
3440                 }
3441
3442                 if (active_vlink_present) {
3443                         /*
3444                          * If there are other active VLinks present,
3445                          * re-instantiate the Vlink using FDISC.
3446                          */
3447                         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3448                         shost = lpfc_shost_from_vport(vport);
3449                         spin_lock_irq(shost->host_lock);
3450                         ndlp->nlp_flag |= NLP_DELAY_TMO;
3451                         spin_unlock_irq(shost->host_lock);
3452                         ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3453                         vport->port_state = LPFC_FDISC;
3454                 } else {
3455                         /*
3456                          * Otherwise, we request port to rediscover
3457                          * the entire FCF table for a fast recovery
3458                          * from possible case that the current FCF
3459                          * is no longer valid if we are not already
3460                          * in the FCF failover process.
3461                          */
3462                         spin_lock_irq(&phba->hbalock);
3463                         if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3464                                 spin_unlock_irq(&phba->hbalock);
3465                                 break;
3466                         }
3467                         /* Mark the fast failover process in progress */
3468                         phba->fcf.fcf_flag |= FCF_ACVL_DISC;
3469                         spin_unlock_irq(&phba->hbalock);
3470                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3471                                         LOG_DISCOVERY,
3472                                         "2773 Start FCF fast failover due "
3473                                         "to CVL event: evt_tag:x%x\n",
3474                                         acqe_fcoe->event_tag);
3475                         rc = lpfc_sli4_redisc_fcf_table(phba);
3476                         if (rc) {
3477                                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3478                                                 LOG_DISCOVERY,
3479                                                 "2774 Issue FCF rediscover "
3480                                                 "mabilbox command failed, "
3481                                                 "through to CVL event\n");
3482                                 spin_lock_irq(&phba->hbalock);
3483                                 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
3484                                 spin_unlock_irq(&phba->hbalock);
3485                                 /*
3486                                  * Last resort will be re-try on the
3487                                  * the current registered FCF entry.
3488                                  */
3489                                 lpfc_retry_pport_discovery(phba);
3490                         }
3491                 }
3492                 break;
3493         default:
3494                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3495                         "0288 Unknown FCoE event type 0x%x event tag "
3496                         "0x%x\n", event_type, acqe_fcoe->event_tag);
3497                 break;
3498         }
3499 }
3500
3501 /**
3502  * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3503  * @phba: pointer to lpfc hba data structure.
3504  * @acqe_link: pointer to the async dcbx completion queue entry.
3505  *
3506  * This routine is to handle the SLI4 asynchronous dcbx event.
3507  **/
3508 static void
3509 lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3510                          struct lpfc_acqe_dcbx *acqe_dcbx)
3511 {
3512         phba->fc_eventTag = acqe_dcbx->event_tag;
3513         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3514                         "0290 The SLI4 DCBX asynchronous event is not "
3515                         "handled yet\n");
3516 }
3517
3518 /**
3519  * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3520  * @phba: pointer to lpfc hba data structure.
3521  *
3522  * This routine is invoked by the worker thread to process all the pending
3523  * SLI4 asynchronous events.
3524  **/
3525 void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3526 {
3527         struct lpfc_cq_event *cq_event;
3528
3529         /* First, declare the async event has been handled */
3530         spin_lock_irq(&phba->hbalock);
3531         phba->hba_flag &= ~ASYNC_EVENT;
3532         spin_unlock_irq(&phba->hbalock);
3533         /* Now, handle all the async events */
3534         while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3535                 /* Get the first event from the head of the event queue */
3536                 spin_lock_irq(&phba->hbalock);
3537                 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3538                                  cq_event, struct lpfc_cq_event, list);
3539                 spin_unlock_irq(&phba->hbalock);
3540                 /* Process the asynchronous event */
3541                 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3542                 case LPFC_TRAILER_CODE_LINK:
3543                         lpfc_sli4_async_link_evt(phba,
3544                                                  &cq_event->cqe.acqe_link);
3545                         break;
3546                 case LPFC_TRAILER_CODE_FCOE:
3547                         lpfc_sli4_async_fcoe_evt(phba,
3548                                                  &cq_event->cqe.acqe_fcoe);
3549                         break;
3550                 case LPFC_TRAILER_CODE_DCBX:
3551                         lpfc_sli4_async_dcbx_evt(phba,
3552                                                  &cq_event->cqe.acqe_dcbx);
3553                         break;
3554                 default:
3555                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3556                                         "1804 Invalid asynchrous event code: "
3557                                         "x%x\n", bf_get(lpfc_trailer_code,
3558                                         &cq_event->cqe.mcqe_cmpl));
3559                         break;
3560                 }
3561                 /* Free the completion event processed to the free pool */
3562                 lpfc_sli4_cq_event_release(phba, cq_event);
3563         }
3564 }
3565
3566 /**
3567  * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3568  * @phba: pointer to lpfc hba data structure.
3569  *
3570  * This routine is invoked by the worker thread to process FCF table
3571  * rediscovery pending completion event.
3572  **/
3573 void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3574 {
3575         int rc;
3576
3577         spin_lock_irq(&phba->hbalock);
3578         /* Clear FCF rediscovery timeout event */
3579         phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3580         /* Clear driver fast failover FCF record flag */
3581         phba->fcf.failover_rec.flag = 0;
3582         /* Set state for FCF fast failover */
3583         phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3584         spin_unlock_irq(&phba->hbalock);
3585
3586         /* Scan FCF table from the first entry to re-discover SAN */
3587         lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3588                         "2777 Start FCF table scan after FCF "
3589                         "rediscovery quiescent period over\n");
3590         rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
3591         if (rc)
3592                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3593                                 "2747 Issue FCF scan read FCF mailbox "
3594                                 "command failed 0x%x\n", rc);
3595 }
3596
3597 /**
3598  * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3599  * @phba: pointer to lpfc hba data structure.
3600  * @dev_grp: The HBA PCI-Device group number.
3601  *
3602  * This routine is invoked to set up the per HBA PCI-Device group function
3603  * API jump table entries.
3604  *
3605  * Return: 0 if success, otherwise -ENODEV
3606  **/
3607 int
3608 lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3609 {
3610         int rc;
3611
3612         /* Set up lpfc PCI-device group */
3613         phba->pci_dev_grp = dev_grp;
3614
3615         /* The LPFC_PCI_DEV_OC uses SLI4 */
3616         if (dev_grp == LPFC_PCI_DEV_OC)
3617                 phba->sli_rev = LPFC_SLI_REV4;
3618
3619         /* Set up device INIT API function jump table */
3620         rc = lpfc_init_api_table_setup(phba, dev_grp);
3621         if (rc)
3622                 return -ENODEV;
3623         /* Set up SCSI API function jump table */
3624         rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3625         if (rc)
3626                 return -ENODEV;
3627         /* Set up SLI API function jump table */
3628         rc = lpfc_sli_api_table_setup(phba, dev_grp);
3629         if (rc)
3630                 return -ENODEV;
3631         /* Set up MBOX API function jump table */
3632         rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3633         if (rc)
3634                 return -ENODEV;
3635
3636         return 0;
3637 }
3638
3639 /**
3640  * lpfc_log_intr_mode - Log the active interrupt mode
3641  * @phba: pointer to lpfc hba data structure.
3642  * @intr_mode: active interrupt mode adopted.
3643  *
3644  * This routine it invoked to log the currently used active interrupt mode
3645  * to the device.
3646  **/
3647 static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
3648 {
3649         switch (intr_mode) {
3650         case 0:
3651                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3652                                 "0470 Enable INTx interrupt mode.\n");
3653                 break;
3654         case 1:
3655                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3656                                 "0481 Enabled MSI interrupt mode.\n");
3657                 break;
3658         case 2:
3659                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3660                                 "0480 Enabled MSI-X interrupt mode.\n");
3661                 break;
3662         default:
3663                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3664                                 "0482 Illegal interrupt mode.\n");
3665                 break;
3666         }
3667         return;
3668 }
3669
3670 /**
3671  * lpfc_enable_pci_dev - Enable a generic PCI device.
3672  * @phba: pointer to lpfc hba data structure.
3673  *
3674  * This routine is invoked to enable the PCI device that is common to all
3675  * PCI devices.
3676  *
3677  * Return codes
3678  *      0 - successful
3679  *      other values - error
3680  **/
3681 static int
3682 lpfc_enable_pci_dev(struct lpfc_hba *phba)
3683 {
3684         struct pci_dev *pdev;
3685         int bars;
3686
3687         /* Obtain PCI device reference */
3688         if (!phba->pcidev)
3689                 goto out_error;
3690         else
3691                 pdev = phba->pcidev;
3692         /* Select PCI BARs */
3693         bars = pci_select_bars(pdev, IORESOURCE_MEM);
3694         /* Enable PCI device */
3695         if (pci_enable_device_mem(pdev))
3696                 goto out_error;
3697         /* Request PCI resource for the device */
3698         if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3699                 goto out_disable_device;
3700         /* Set up device as PCI master and save state for EEH */
3701         pci_set_master(pdev);
3702         pci_try_set_mwi(pdev);
3703         pci_save_state(pdev);
3704
3705         return 0;
3706
3707 out_disable_device:
3708         pci_disable_device(pdev);
3709 out_error:
3710         return -ENODEV;
3711 }
3712
3713 /**
3714  * lpfc_disable_pci_dev - Disable a generic PCI device.
3715  * @phba: pointer to lpfc hba data structure.
3716  *
3717  * This routine is invoked to disable the PCI device that is common to all
3718  * PCI devices.
3719  **/
3720 static void
3721 lpfc_disable_pci_dev(struct lpfc_hba *phba)
3722 {
3723         struct pci_dev *pdev;
3724         int bars;
3725
3726         /* Obtain PCI device reference */
3727         if (!phba->pcidev)
3728                 return;
3729         else
3730                 pdev = phba->pcidev;
3731         /* Select PCI BARs */
3732         bars = pci_select_bars(pdev, IORESOURCE_MEM);
3733         /* Release PCI resource and disable PCI device */
3734         pci_release_selected_regions(pdev, bars);
3735         pci_disable_device(pdev);
3736         /* Null out PCI private reference to driver */
3737         pci_set_drvdata(pdev, NULL);
3738
3739         return;
3740 }
3741
3742 /**
3743  * lpfc_reset_hba - Reset a hba
3744  * @phba: pointer to lpfc hba data structure.
3745  *
3746  * This routine is invoked to reset a hba device. It brings the HBA
3747  * offline, performs a board restart, and then brings the board back
3748  * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3749  * on outstanding mailbox commands.
3750  **/
3751 void
3752 lpfc_reset_hba(struct lpfc_hba *phba)
3753 {
3754         /* If resets are disabled then set error state and return. */
3755         if (!phba->cfg_enable_hba_reset) {
3756                 phba->link_state = LPFC_HBA_ERROR;
3757                 return;
3758         }
3759         lpfc_offline_prep(phba);
3760         lpfc_offline(phba);
3761         lpfc_sli_brdrestart(phba);
3762         lpfc_online(phba);
3763         lpfc_unblock_mgmt_io(phba);
3764 }
3765
3766 /**
3767  * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3768  * @phba: pointer to lpfc hba data structure.
3769  *
3770  * This routine is invoked to set up the driver internal resources specific to
3771  * support the SLI-3 HBA device it attached to.
3772  *
3773  * Return codes
3774  *      0 - successful
3775  *      other values - error
3776  **/
3777 static int
3778 lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3779 {
3780         struct lpfc_sli *psli;
3781
3782         /*
3783          * Initialize timers used by driver
3784          */
3785
3786         /* Heartbeat timer */
3787         init_timer(&phba->hb_tmofunc);
3788         phba->hb_tmofunc.function = lpfc_hb_timeout;
3789         phba->hb_tmofunc.data = (unsigned long)phba;
3790
3791         psli = &phba->sli;
3792         /* MBOX heartbeat timer */
3793         init_timer(&psli->mbox_tmo);
3794         psli->mbox_tmo.function = lpfc_mbox_timeout;
3795         psli->mbox_tmo.data = (unsigned long) phba;
3796         /* FCP polling mode timer */
3797         init_timer(&phba->fcp_poll_timer);
3798         phba->fcp_poll_timer.function = lpfc_poll_timeout;
3799         phba->fcp_poll_timer.data = (unsigned long) phba;
3800         /* Fabric block timer */
3801         init_timer(&phba->fabric_block_timer);
3802         phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3803         phba->fabric_block_timer.data = (unsigned long) phba;
3804         /* EA polling mode timer */
3805         init_timer(&phba->eratt_poll);
3806         phba->eratt_poll.function = lpfc_poll_eratt;
3807         phba->eratt_poll.data = (unsigned long) phba;
3808
3809         /* Host attention work mask setup */
3810         phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3811         phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3812
3813         /* Get all the module params for configuring this host */
3814         lpfc_get_cfgparam(phba);
3815         /*
3816          * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3817          * used to create the sg_dma_buf_pool must be dynamically calculated.
3818          * 2 segments are added since the IOCB needs a command and response bde.
3819          */
3820         phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3821                 sizeof(struct fcp_rsp) +
3822                         ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3823
3824         if (phba->cfg_enable_bg) {
3825                 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3826                 phba->cfg_sg_dma_buf_size +=
3827                         phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3828         }
3829
3830         /* Also reinitialize the host templates with new values. */
3831         lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3832         lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3833
3834         phba->max_vpi = LPFC_MAX_VPI;
3835         /* This will be set to correct value after config_port mbox */
3836         phba->max_vports = 0;
3837
3838         /*
3839          * Initialize the SLI Layer to run with lpfc HBAs.
3840          */
3841         lpfc_sli_setup(phba);
3842         lpfc_sli_queue_setup(phba);
3843
3844         /* Allocate device driver memory */
3845         if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3846                 return -ENOMEM;
3847
3848         return 0;
3849 }
3850
3851 /**
3852  * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3853  * @phba: pointer to lpfc hba data structure.
3854  *
3855  * This routine is invoked to unset the driver internal resources set up
3856  * specific for supporting the SLI-3 HBA device it attached to.
3857  **/
3858 static void
3859 lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3860 {
3861         /* Free device driver memory allocated */
3862         lpfc_mem_free_all(phba);
3863
3864         return;
3865 }
3866
3867 /**
3868  * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3869  * @phba: pointer to lpfc hba data structure.
3870  *
3871  * This routine is invoked to set up the driver internal resources specific to
3872  * support the SLI-4 HBA device it attached to.
3873  *
3874  * Return codes
3875  *      0 - successful
3876  *      other values - error
3877  **/
3878 static int
3879 lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3880 {
3881         struct lpfc_sli *psli;
3882         LPFC_MBOXQ_t *mboxq;
3883         int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
3884         uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
3885         struct lpfc_mqe *mqe;
3886         int longs;
3887
3888         /* Before proceed, wait for POST done and device ready */
3889         rc = lpfc_sli4_post_status_check(phba);
3890         if (rc)
3891                 return -ENODEV;
3892
3893         /*
3894          * Initialize timers used by driver
3895          */
3896
3897         /* Heartbeat timer */
3898         init_timer(&phba->hb_tmofunc);
3899         phba->hb_tmofunc.function = lpfc_hb_timeout;
3900         phba->hb_tmofunc.data = (unsigned long)phba;
3901
3902         psli = &phba->sli;
3903         /* MBOX heartbeat timer */
3904         init_timer(&psli->mbox_tmo);
3905         psli->mbox_tmo.function = lpfc_mbox_timeout;
3906         psli->mbox_tmo.data = (unsigned long) phba;
3907         /* Fabric block timer */
3908         init_timer(&phba->fabric_block_timer);
3909         phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3910         phba->fabric_block_timer.data = (unsigned long) phba;
3911         /* EA polling mode timer */
3912         init_timer(&phba->eratt_poll);
3913         phba->eratt_poll.function = lpfc_poll_eratt;
3914         phba->eratt_poll.data = (unsigned long) phba;
3915         /* FCF rediscover timer */
3916         init_timer(&phba->fcf.redisc_wait);
3917         phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
3918         phba->fcf.redisc_wait.data = (unsigned long)phba;
3919
3920         /*
3921          * We need to do a READ_CONFIG mailbox command here before
3922          * calling lpfc_get_cfgparam. For VFs this will report the
3923          * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3924          * All of the resources allocated
3925          * for this Port are tied to these values.
3926          */
3927         /* Get all the module params for configuring this host */
3928         lpfc_get_cfgparam(phba);
3929         phba->max_vpi = LPFC_MAX_VPI;
3930         /* This will be set to correct value after the read_config mbox */
3931         phba->max_vports = 0;
3932
3933         /* Program the default value of vlan_id and fc_map */
3934         phba->valid_vlan = 0;
3935         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3936         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3937         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3938
3939         /*
3940          * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3941          * used to create the sg_dma_buf_pool must be dynamically calculated.
3942          * 2 segments are added since the IOCB needs a command and response bde.
3943          * To insure that the scsi sgl does not cross a 4k page boundary only
3944          * sgl sizes of must be a power of 2.
3945          */
3946         buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
3947                     ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
3948         /* Feature Level 1 hardware is limited to 2 pages */
3949         if ((bf_get(lpfc_sli_intf_featurelevel1, &phba->sli4_hba.sli_intf) ==
3950              LPFC_SLI_INTF_FEATURELEVEL1_1))
3951                 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
3952         else
3953                 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
3954         for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
3955              dma_buf_size < max_buf_size && buf_size > dma_buf_size;
3956              dma_buf_size = dma_buf_size << 1)
3957                 ;
3958         if (dma_buf_size == max_buf_size)
3959                 phba->cfg_sg_seg_cnt = (dma_buf_size -
3960                         sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
3961                         (2 * sizeof(struct sli4_sge))) /
3962                                 sizeof(struct sli4_sge);
3963         phba->cfg_sg_dma_buf_size = dma_buf_size;
3964
3965         /* Initialize buffer queue management fields */
3966         hbq_count = lpfc_sli_hbq_count();
3967         for (i = 0; i < hbq_count; ++i)
3968                 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3969         INIT_LIST_HEAD(&phba->rb_pend_list);
3970         phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3971         phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3972
3973         /*
3974          * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3975          */
3976         /* Initialize the Abort scsi buffer list used by driver */
3977         spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3978         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3979         /* This abort list used by worker thread */
3980         spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3981
3982         /*
3983          * Initialize dirver internal slow-path work queues
3984          */
3985
3986         /* Driver internel slow-path CQ Event pool */
3987         INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3988         /* Response IOCB work queue list */
3989         INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
3990         /* Asynchronous event CQ Event work queue list */
3991         INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3992         /* Fast-path XRI aborted CQ Event work queue list */
3993         INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3994         /* Slow-path XRI aborted CQ Event work queue list */
3995         INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3996         /* Receive queue CQ Event work queue list */
3997         INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3998
3999         /* Initialize the driver internal SLI layer lists. */
4000         lpfc_sli_setup(phba);
4001         lpfc_sli_queue_setup(phba);
4002
4003         /* Allocate device driver memory */
4004         rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4005         if (rc)
4006                 return -ENOMEM;
4007
4008         /* Create the bootstrap mailbox command */
4009         rc = lpfc_create_bootstrap_mbox(phba);
4010         if (unlikely(rc))
4011                 goto out_free_mem;
4012
4013         /* Set up the host's endian order with the device. */
4014         rc = lpfc_setup_endian_order(phba);
4015         if (unlikely(rc))
4016                 goto out_free_bsmbx;
4017
4018         rc = lpfc_sli4_fw_cfg_check(phba);
4019         if (unlikely(rc))
4020                 goto out_free_bsmbx;
4021
4022         /* Set up the hba's configuration parameters. */
4023         rc = lpfc_sli4_read_config(phba);
4024         if (unlikely(rc))
4025                 goto out_free_bsmbx;
4026
4027         /* Perform a function reset */
4028         rc = lpfc_pci_function_reset(phba);
4029         if (unlikely(rc))
4030                 goto out_free_bsmbx;
4031
4032         /* Create all the SLI4 queues */
4033         rc = lpfc_sli4_queue_create(phba);
4034         if (rc)
4035                 goto out_free_bsmbx;
4036
4037         /* Create driver internal CQE event pool */
4038         rc = lpfc_sli4_cq_event_pool_create(phba);
4039         if (rc)
4040                 goto out_destroy_queue;
4041
4042         /* Initialize and populate the iocb list per host */
4043         rc = lpfc_init_sgl_list(phba);
4044         if (rc) {
4045                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4046                                 "1400 Failed to initialize sgl list.\n");
4047                 goto out_destroy_cq_event_pool;
4048         }
4049         rc = lpfc_init_active_sgl_array(phba);
4050         if (rc) {
4051                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4052                                 "1430 Failed to initialize sgl list.\n");
4053                 goto out_free_sgl_list;
4054         }
4055
4056         rc = lpfc_sli4_init_rpi_hdrs(phba);
4057         if (rc) {
4058                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4059                                 "1432 Failed to initialize rpi headers.\n");
4060                 goto out_free_active_sgl;
4061         }
4062
4063         /* Allocate eligible FCF bmask memory for FCF round robin failover */
4064         longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4065         phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4066                                          GFP_KERNEL);
4067         if (!phba->fcf.fcf_rr_bmask) {
4068                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4069                                 "2759 Failed allocate memory for FCF round "
4070                                 "robin failover bmask\n");
4071                 goto out_remove_rpi_hdrs;
4072         }
4073
4074         phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4075                                     phba->cfg_fcp_eq_count), GFP_KERNEL);
4076         if (!phba->sli4_hba.fcp_eq_hdl) {
4077                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4078                                 "2572 Failed allocate memory for fast-path "
4079                                 "per-EQ handle array\n");
4080                 goto out_free_fcf_rr_bmask;
4081         }
4082
4083         phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4084                                       phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4085         if (!phba->sli4_hba.msix_entries) {
4086                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4087                                 "2573 Failed allocate memory for msi-x "
4088                                 "interrupt vector entries\n");
4089                 goto out_free_fcp_eq_hdl;
4090         }
4091
4092         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4093                                                        GFP_KERNEL);
4094         if (!mboxq) {
4095                 rc = -ENOMEM;
4096                 goto out_free_fcp_eq_hdl;
4097         }
4098
4099         /* Get the Supported Pages. It is always available. */
4100         lpfc_supported_pages(mboxq);
4101         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4102         if (unlikely(rc)) {
4103                 rc = -EIO;
4104                 mempool_free(mboxq, phba->mbox_mem_pool);
4105                 goto out_free_fcp_eq_hdl;
4106         }
4107
4108         mqe = &mboxq->u.mqe;
4109         memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4110                LPFC_MAX_SUPPORTED_PAGES);
4111         for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4112                 switch (pn_page[i]) {
4113                 case LPFC_SLI4_PARAMETERS:
4114                         phba->sli4_hba.pc_sli4_params.supported = 1;
4115                         break;
4116                 default:
4117                         break;
4118                 }
4119         }
4120
4121         /* Read the port's SLI4 Parameters capabilities if supported. */
4122         if (phba->sli4_hba.pc_sli4_params.supported)
4123                 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4124         mempool_free(mboxq, phba->mbox_mem_pool);
4125         if (rc) {
4126                 rc = -EIO;
4127                 goto out_free_fcp_eq_hdl;
4128         }
4129         return rc;
4130
4131 out_free_fcp_eq_hdl:
4132         kfree(phba->sli4_hba.fcp_eq_hdl);
4133 out_free_fcf_rr_bmask:
4134         kfree(phba->fcf.fcf_rr_bmask);
4135 out_remove_rpi_hdrs:
4136         lpfc_sli4_remove_rpi_hdrs(phba);
4137 out_free_active_sgl:
4138         lpfc_free_active_sgl(phba);
4139 out_free_sgl_list:
4140         lpfc_free_sgl_list(phba);
4141 out_destroy_cq_event_pool:
4142         lpfc_sli4_cq_event_pool_destroy(phba);
4143 out_destroy_queue:
4144         lpfc_sli4_queue_destroy(phba);
4145 out_free_bsmbx:
4146         lpfc_destroy_bootstrap_mbox(phba);
4147 out_free_mem:
4148         lpfc_mem_free(phba);
4149         return rc;
4150 }
4151
4152 /**
4153  * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4154  * @phba: pointer to lpfc hba data structure.
4155  *
4156  * This routine is invoked to unset the driver internal resources set up
4157  * specific for supporting the SLI-4 HBA device it attached to.
4158  **/
4159 static void
4160 lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4161 {
4162         struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4163
4164         /* unregister default FCFI from the HBA */
4165         lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
4166
4167         /* Free the default FCR table */
4168         lpfc_sli_remove_dflt_fcf(phba);
4169
4170         /* Free memory allocated for msi-x interrupt vector entries */
4171         kfree(phba->sli4_hba.msix_entries);
4172
4173         /* Free memory allocated for fast-path work queue handles */
4174         kfree(phba->sli4_hba.fcp_eq_hdl);
4175
4176         /* Free the allocated rpi headers. */
4177         lpfc_sli4_remove_rpi_hdrs(phba);
4178         lpfc_sli4_remove_rpis(phba);
4179
4180         /* Free eligible FCF index bmask */
4181         kfree(phba->fcf.fcf_rr_bmask);
4182
4183         /* Free the ELS sgl list */
4184         lpfc_free_active_sgl(phba);
4185         lpfc_free_sgl_list(phba);
4186
4187         /* Free the SCSI sgl management array */
4188         kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4189
4190         /* Free the SLI4 queues */
4191         lpfc_sli4_queue_destroy(phba);
4192
4193         /* Free the completion queue EQ event pool */
4194         lpfc_sli4_cq_event_release_all(phba);
4195         lpfc_sli4_cq_event_pool_destroy(phba);
4196
4197         /* Reset SLI4 HBA FCoE function */
4198         lpfc_pci_function_reset(phba);
4199
4200         /* Free the bsmbx region. */
4201         lpfc_destroy_bootstrap_mbox(phba);
4202
4203         /* Free the SLI Layer memory with SLI4 HBAs */
4204         lpfc_mem_free_all(phba);
4205
4206         /* Free the current connect table */
4207         list_for_each_entry_safe(conn_entry, next_conn_entry,
4208                 &phba->fcf_conn_rec_list, list) {
4209                 list_del_init(&conn_entry->list);
4210                 kfree(conn_entry);
4211         }
4212
4213         return;
4214 }
4215
4216 /**
4217  * lpfc_init_api_table_setup - Set up init api fucntion jump table
4218  * @phba: The hba struct for which this call is being executed.
4219  * @dev_grp: The HBA PCI-Device group number.
4220  *
4221  * This routine sets up the device INIT interface API function jump table
4222  * in @phba struct.
4223  *
4224  * Returns: 0 - success, -ENODEV - failure.
4225  **/
4226 int
4227 lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4228 {
4229         phba->lpfc_hba_init_link = lpfc_hba_init_link;
4230         phba->lpfc_hba_down_link = lpfc_hba_down_link;
4231         switch (dev_grp) {
4232         case LPFC_PCI_DEV_LP:
4233                 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4234                 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4235                 phba->lpfc_stop_port = lpfc_stop_port_s3;
4236                 break;
4237         case LPFC_PCI_DEV_OC:
4238                 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4239                 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4240                 phba->lpfc_stop_port = lpfc_stop_port_s4;
4241                 break;
4242         default:
4243                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4244                                 "1431 Invalid HBA PCI-device group: 0x%x\n",
4245                                 dev_grp);
4246                 return -ENODEV;
4247                 break;
4248         }
4249         return 0;
4250 }
4251
4252 /**
4253  * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4254  * @phba: pointer to lpfc hba data structure.
4255  *
4256  * This routine is invoked to set up the driver internal resources before the
4257  * device specific resource setup to support the HBA device it attached to.
4258  *
4259  * Return codes
4260  *      0 - successful
4261  *      other values - error
4262  **/
4263 static int
4264 lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4265 {
4266         /*
4267          * Driver resources common to all SLI revisions
4268          */
4269         atomic_set(&phba->fast_event_count, 0);
4270         spin_lock_init(&phba->hbalock);
4271
4272         /* Initialize ndlp management spinlock */
4273         spin_lock_init(&phba->ndlp_lock);
4274
4275         INIT_LIST_HEAD(&phba->port_list);
4276         INIT_LIST_HEAD(&phba->work_list);
4277         init_waitqueue_head(&phba->wait_4_mlo_m_q);
4278
4279         /* Initialize the wait queue head for the kernel thread */
4280         init_waitqueue_head(&phba->work_waitq);
4281
4282         /* Initialize the scsi buffer list used by driver for scsi IO */
4283         spin_lock_init(&phba->scsi_buf_list_lock);
4284         INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
4285
4286         /* Initialize the fabric iocb list */
4287         INIT_LIST_HEAD(&phba->fabric_iocb_list);
4288
4289         /* Initialize list to save ELS buffers */
4290         INIT_LIST_HEAD(&phba->elsbuf);
4291
4292         /* Initialize FCF connection rec list */
4293         INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
4294
4295         return 0;
4296 }
4297
4298 /**
4299  * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4300  * @phba: pointer to lpfc hba data structure.
4301  *
4302  * This routine is invoked to set up the driver internal resources after the
4303  * device specific resource setup to support the HBA device it attached to.
4304  *
4305  * Return codes
4306  *      0 - successful
4307  *      other values - error
4308  **/
4309 static int
4310 lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4311 {
4312         int error;
4313
4314         /* Startup the kernel thread for this host adapter. */
4315         phba->worker_thread = kthread_run(lpfc_do_work, phba,
4316                                           "lpfc_worker_%d", phba->brd_no);
4317         if (IS_ERR(phba->worker_thread)) {
4318                 error = PTR_ERR(phba->worker_thread);
4319                 return error;
4320         }
4321
4322         return 0;
4323 }
4324
4325 /**
4326  * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4327  * @phba: pointer to lpfc hba data structure.
4328  *
4329  * This routine is invoked to unset the driver internal resources set up after
4330  * the device specific resource setup for supporting the HBA device it
4331  * attached to.
4332  **/
4333 static void
4334 lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4335 {
4336         /* Stop kernel worker thread */
4337         kthread_stop(phba->worker_thread);
4338 }
4339
4340 /**
4341  * lpfc_free_iocb_list - Free iocb list.
4342  * @phba: pointer to lpfc hba data structure.
4343  *
4344  * This routine is invoked to free the driver's IOCB list and memory.
4345  **/
4346 static void
4347 lpfc_free_iocb_list(struct lpfc_hba *phba)
4348 {
4349         struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4350
4351         spin_lock_irq(&phba->hbalock);
4352         list_for_each_entry_safe(iocbq_entry, iocbq_next,
4353                                  &phba->lpfc_iocb_list, list) {
4354                 list_del(&iocbq_entry->list);
4355                 kfree(iocbq_entry);
4356                 phba->total_iocbq_bufs--;
4357         }
4358         spin_unlock_irq(&phba->hbalock);
4359
4360         return;
4361 }
4362
4363 /**
4364  * lpfc_init_iocb_list - Allocate and initialize iocb list.
4365  * @phba: pointer to lpfc hba data structure.
4366  *
4367  * This routine is invoked to allocate and initizlize the driver's IOCB
4368  * list and set up the IOCB tag array accordingly.
4369  *
4370  * Return codes
4371  *      0 - successful
4372  *      other values - error
4373  **/
4374 static int
4375 lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4376 {
4377         struct lpfc_iocbq *iocbq_entry = NULL;
4378         uint16_t iotag;
4379         int i;
4380
4381         /* Initialize and populate the iocb list per host.  */
4382         INIT_LIST_HEAD(&phba->lpfc_iocb_list);
4383         for (i = 0; i < iocb_count; i++) {
4384                 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
4385                 if (iocbq_entry == NULL) {
4386                         printk(KERN_ERR "%s: only allocated %d iocbs of "
4387                                 "expected %d count. Unloading driver.\n",
4388                                 __func__, i, LPFC_IOCB_LIST_CNT);
4389                         goto out_free_iocbq;
4390                 }
4391
4392                 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4393                 if (iotag == 0) {
4394                         kfree(iocbq_entry);
4395                         printk(KERN_ERR "%s: failed to allocate IOTAG. "
4396                                 "Unloading driver.\n", __func__);
4397                         goto out_free_iocbq;
4398                 }
4399                 iocbq_entry->sli4_xritag = NO_XRI;
4400
4401                 spin_lock_irq(&phba->hbalock);
4402                 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4403                 phba->total_iocbq_bufs++;
4404                 spin_unlock_irq(&phba->hbalock);
4405         }
4406
4407         return 0;
4408
4409 out_free_iocbq:
4410         lpfc_free_iocb_list(phba);
4411
4412         return -ENOMEM;
4413 }
4414
4415 /**
4416  * lpfc_free_sgl_list - Free sgl list.
4417  * @phba: pointer to lpfc hba data structure.
4418  *
4419  * This routine is invoked to free the driver's sgl list and memory.
4420  **/
4421 static void
4422 lpfc_free_sgl_list(struct lpfc_hba *phba)
4423 {
4424         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4425         LIST_HEAD(sglq_list);
4426         int rc = 0;
4427
4428         spin_lock_irq(&phba->hbalock);
4429         list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4430         spin_unlock_irq(&phba->hbalock);
4431
4432         list_for_each_entry_safe(sglq_entry, sglq_next,
4433                                  &sglq_list, list) {
4434                 list_del(&sglq_entry->list);
4435                 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4436                 kfree(sglq_entry);
4437                 phba->sli4_hba.total_sglq_bufs--;
4438         }
4439         rc = lpfc_sli4_remove_all_sgl_pages(phba);
4440         if (rc) {
4441                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4442                         "2005 Unable to deregister pages from HBA: %x\n", rc);
4443         }
4444         kfree(phba->sli4_hba.lpfc_els_sgl_array);
4445 }
4446
4447 /**
4448  * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4449  * @phba: pointer to lpfc hba data structure.
4450  *
4451  * This routine is invoked to allocate the driver's active sgl memory.
4452  * This array will hold the sglq_entry's for active IOs.
4453  **/
4454 static int
4455 lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4456 {
4457         int size;
4458         size = sizeof(struct lpfc_sglq *);
4459         size *= phba->sli4_hba.max_cfg_param.max_xri;
4460
4461         phba->sli4_hba.lpfc_sglq_active_list =
4462                 kzalloc(size, GFP_KERNEL);
4463         if (!phba->sli4_hba.lpfc_sglq_active_list)
4464                 return -ENOMEM;
4465         return 0;
4466 }
4467
4468 /**
4469  * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4470  * @phba: pointer to lpfc hba data structure.
4471  *
4472  * This routine is invoked to walk through the array of active sglq entries
4473  * and free all of the resources.
4474  * This is just a place holder for now.
4475  **/
4476 static void
4477 lpfc_free_active_sgl(struct lpfc_hba *phba)
4478 {
4479         kfree(phba->sli4_hba.lpfc_sglq_active_list);
4480 }
4481
4482 /**
4483  * lpfc_init_sgl_list - Allocate and initialize sgl list.
4484  * @phba: pointer to lpfc hba data structure.
4485  *
4486  * This routine is invoked to allocate and initizlize the driver's sgl
4487  * list and set up the sgl xritag tag array accordingly.
4488  *
4489  * Return codes
4490  *      0 - successful
4491  *      other values - error
4492  **/
4493 static int
4494 lpfc_init_sgl_list(struct lpfc_hba *phba)
4495 {
4496         struct lpfc_sglq *sglq_entry = NULL;
4497         int i;
4498         int els_xri_cnt;
4499
4500         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4501         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4502                                 "2400 lpfc_init_sgl_list els %d.\n",
4503                                 els_xri_cnt);
4504         /* Initialize and populate the sglq list per host/VF. */
4505         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4506         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4507
4508         /* Sanity check on XRI management */
4509         if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4510                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4511                                 "2562 No room left for SCSI XRI allocation: "
4512                                 "max_xri=%d, els_xri=%d\n",
4513                                 phba->sli4_hba.max_cfg_param.max_xri,
4514                                 els_xri_cnt);
4515                 return -ENOMEM;
4516         }
4517
4518         /* Allocate memory for the ELS XRI management array */
4519         phba->sli4_hba.lpfc_els_sgl_array =
4520                         kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4521                         GFP_KERNEL);
4522
4523         if (!phba->sli4_hba.lpfc_els_sgl_array) {
4524                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4525                                 "2401 Failed to allocate memory for ELS "
4526                                 "XRI management array of size %d.\n",
4527                                 els_xri_cnt);
4528                 return -ENOMEM;
4529         }
4530
4531         /* Keep the SCSI XRI into the XRI management array */
4532         phba->sli4_hba.scsi_xri_max =
4533                         phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4534         phba->sli4_hba.scsi_xri_cnt = 0;
4535
4536         phba->sli4_hba.lpfc_scsi_psb_array =
4537                         kzalloc((sizeof(struct lpfc_scsi_buf *) *
4538                         phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4539
4540         if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4541                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4542                                 "2563 Failed to allocate memory for SCSI "
4543                                 "XRI management array of size %d.\n",
4544                                 phba->sli4_hba.scsi_xri_max);
4545                 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4546                 return -ENOMEM;
4547         }
4548
4549         for (i = 0; i < els_xri_cnt; i++) {
4550                 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4551                 if (sglq_entry == NULL) {
4552                         printk(KERN_ERR "%s: only allocated %d sgls of "
4553                                 "expected %d count. Unloading driver.\n",
4554                                 __func__, i, els_xri_cnt);
4555                         goto out_free_mem;
4556                 }
4557
4558                 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4559                 if (sglq_entry->sli4_xritag == NO_XRI) {
4560                         kfree(sglq_entry);
4561                         printk(KERN_ERR "%s: failed to allocate XRI.\n"
4562                                 "Unloading driver.\n", __func__);
4563                         goto out_free_mem;
4564                 }
4565                 sglq_entry->buff_type = GEN_BUFF_TYPE;
4566                 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4567                 if (sglq_entry->virt == NULL) {
4568                         kfree(sglq_entry);
4569                         printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4570                                 "Unloading driver.\n", __func__);
4571                         goto out_free_mem;
4572                 }
4573                 sglq_entry->sgl = sglq_entry->virt;
4574                 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4575
4576                 /* The list order is used by later block SGL registraton */
4577                 spin_lock_irq(&phba->hbalock);
4578                 sglq_entry->state = SGL_FREED;
4579                 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4580                 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4581                 phba->sli4_hba.total_sglq_bufs++;
4582                 spin_unlock_irq(&phba->hbalock);
4583         }
4584         return 0;
4585
4586 out_free_mem:
4587         kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4588         lpfc_free_sgl_list(phba);
4589         return -ENOMEM;
4590 }
4591
4592 /**
4593  * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4594  * @phba: pointer to lpfc hba data structure.
4595  *
4596  * This routine is invoked to post rpi header templates to the
4597  * HBA consistent with the SLI-4 interface spec.  This routine
4598  * posts a PAGE_SIZE memory region to the port to hold up to
4599  * PAGE_SIZE modulo 64 rpi context headers.
4600  * No locks are held here because this is an initialization routine
4601  * called only from probe or lpfc_online when interrupts are not
4602  * enabled and the driver is reinitializing the device.
4603  *
4604  * Return codes
4605  *      0 - successful
4606  *      ENOMEM - No availble memory
4607  *      EIO - The mailbox failed to complete successfully.
4608  **/
4609 int
4610 lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4611 {
4612         int rc = 0;
4613         int longs;
4614         uint16_t rpi_count;
4615         struct lpfc_rpi_hdr *rpi_hdr;
4616
4617         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4618
4619         /*
4620          * Provision an rpi bitmask range for discovery. The total count
4621          * is the difference between max and base + 1.
4622          */
4623         rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4624                     phba->sli4_hba.max_cfg_param.max_rpi - 1;
4625
4626         longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4627         phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4628                                            GFP_KERNEL);
4629         if (!phba->sli4_hba.rpi_bmask)
4630                 return -ENOMEM;
4631
4632         rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4633         if (!rpi_hdr) {
4634                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4635                                 "0391 Error during rpi post operation\n");
4636                 lpfc_sli4_remove_rpis(phba);
4637                 rc = -ENODEV;
4638         }
4639
4640         return rc;
4641 }
4642
4643 /**
4644  * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4645  * @phba: pointer to lpfc hba data structure.
4646  *
4647  * This routine is invoked to allocate a single 4KB memory region to
4648  * support rpis and stores them in the phba.  This single region
4649  * provides support for up to 64 rpis.  The region is used globally
4650  * by the device.
4651  *
4652  * Returns:
4653  *   A valid rpi hdr on success.
4654  *   A NULL pointer on any failure.
4655  **/
4656 struct lpfc_rpi_hdr *
4657 lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4658 {
4659         uint16_t rpi_limit, curr_rpi_range;
4660         struct lpfc_dmabuf *dmabuf;
4661         struct lpfc_rpi_hdr *rpi_hdr;
4662
4663         rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4664                     phba->sli4_hba.max_cfg_param.max_rpi - 1;
4665
4666         spin_lock_irq(&phba->hbalock);
4667         curr_rpi_range = phba->sli4_hba.next_rpi;
4668         spin_unlock_irq(&phba->hbalock);
4669
4670         /*
4671          * The port has a limited number of rpis. The increment here
4672          * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4673          * and to allow the full max_rpi range per port.
4674          */
4675         if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4676                 return NULL;
4677
4678         /*
4679          * First allocate the protocol header region for the port.  The
4680          * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4681          */
4682         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4683         if (!dmabuf)
4684                 return NULL;
4685
4686         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4687                                           LPFC_HDR_TEMPLATE_SIZE,
4688                                           &dmabuf->phys,
4689                                           GFP_KERNEL);
4690         if (!dmabuf->virt) {
4691                 rpi_hdr = NULL;
4692                 goto err_free_dmabuf;
4693         }
4694
4695         memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4696         if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4697                 rpi_hdr = NULL;
4698                 goto err_free_coherent;
4699         }
4700
4701         /* Save the rpi header data for cleanup later. */
4702         rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4703         if (!rpi_hdr)
4704                 goto err_free_coherent;
4705
4706         rpi_hdr->dmabuf = dmabuf;
4707         rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4708         rpi_hdr->page_count = 1;
4709         spin_lock_irq(&phba->hbalock);
4710         rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4711         list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4712
4713         /*
4714          * The next_rpi stores the next module-64 rpi value to post
4715          * in any subsequent rpi memory region postings.
4716          */
4717         phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4718         spin_unlock_irq(&phba->hbalock);
4719         return rpi_hdr;
4720
4721  err_free_coherent:
4722         dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4723                           dmabuf->virt, dmabuf->phys);
4724  err_free_dmabuf:
4725         kfree(dmabuf);
4726         return NULL;
4727 }
4728
4729 /**
4730  * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4731  * @phba: pointer to lpfc hba data structure.
4732  *
4733  * This routine is invoked to remove all memory resources allocated
4734  * to support rpis. This routine presumes the caller has released all
4735  * rpis consumed by fabric or port logins and is prepared to have
4736  * the header pages removed.
4737  **/
4738 void
4739 lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4740 {
4741         struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4742
4743         list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4744                                  &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4745                 list_del(&rpi_hdr->list);
4746                 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4747                                   rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4748                 kfree(rpi_hdr->dmabuf);
4749                 kfree(rpi_hdr);
4750         }
4751
4752         phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4753         memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4754 }
4755
4756 /**
4757  * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4758  * @pdev: pointer to pci device data structure.
4759  *
4760  * This routine is invoked to allocate the driver hba data structure for an
4761  * HBA device. If the allocation is successful, the phba reference to the
4762  * PCI device data structure is set.
4763  *
4764  * Return codes
4765  *      pointer to @phba - successful
4766  *      NULL - error
4767  **/
4768 static struct lpfc_hba *
4769 lpfc_hba_alloc(struct pci_dev *pdev)
4770 {
4771         struct lpfc_hba *phba;
4772
4773         /* Allocate memory for HBA structure */
4774         phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4775         if (!phba) {
4776                 dev_err(&pdev->dev, "failed to allocate hba struct\n");
4777                 return NULL;
4778         }
4779
4780         /* Set reference to PCI device in HBA structure */
4781         phba->pcidev = pdev;
4782
4783         /* Assign an unused board number */
4784         phba->brd_no = lpfc_get_instance();
4785         if (phba->brd_no < 0) {
4786                 kfree(phba);
4787                 return NULL;
4788         }
4789
4790         spin_lock_init(&phba->ct_ev_lock);
4791         INIT_LIST_HEAD(&phba->ct_ev_waiters);
4792
4793         return phba;
4794 }
4795
4796 /**
4797  * lpfc_hba_free - Free driver hba data structure with a device.
4798  * @phba: pointer to lpfc hba data structure.
4799  *
4800  * This routine is invoked to free the driver hba data structure with an
4801  * HBA device.
4802  **/
4803 static void
4804 lpfc_hba_free(struct lpfc_hba *phba)
4805 {
4806         /* Release the driver assigned board number */
4807         idr_remove(&lpfc_hba_index, phba->brd_no);
4808
4809         kfree(phba);
4810         return;
4811 }
4812
4813 /**
4814  * lpfc_create_shost - Create hba physical port with associated scsi host.
4815  * @phba: pointer to lpfc hba data structure.
4816  *
4817  * This routine is invoked to create HBA physical port and associate a SCSI
4818  * host with it.
4819  *
4820  * Return codes
4821  *      0 - successful
4822  *      other values - error
4823  **/
4824 static int
4825 lpfc_create_shost(struct lpfc_hba *phba)
4826 {
4827         struct lpfc_vport *vport;
4828         struct Scsi_Host  *shost;
4829
4830         /* Initialize HBA FC structure */
4831         phba->fc_edtov = FF_DEF_EDTOV;
4832         phba->fc_ratov = FF_DEF_RATOV;
4833         phba->fc_altov = FF_DEF_ALTOV;
4834         phba->fc_arbtov = FF_DEF_ARBTOV;
4835
4836         vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
4837         if (!vport)
4838                 return -ENODEV;
4839
4840         shost = lpfc_shost_from_vport(vport);
4841         phba->pport = vport;
4842         lpfc_debugfs_initialize(vport);
4843         /* Put reference to SCSI host to driver's device private data */
4844         pci_set_drvdata(phba->pcidev, shost);
4845
4846         return 0;
4847 }
4848
4849 /**
4850  * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4851  * @phba: pointer to lpfc hba data structure.
4852  *
4853  * This routine is invoked to destroy HBA physical port and the associated
4854  * SCSI host.
4855  **/
4856 static void
4857 lpfc_destroy_shost(struct lpfc_hba *phba)
4858 {
4859         struct lpfc_vport *vport = phba->pport;
4860
4861         /* Destroy physical port that associated with the SCSI host */
4862         destroy_port(vport);
4863
4864         return;
4865 }
4866
4867 /**
4868  * lpfc_setup_bg - Setup Block guard structures and debug areas.
4869  * @phba: pointer to lpfc hba data structure.
4870  * @shost: the shost to be used to detect Block guard settings.
4871  *
4872  * This routine sets up the local Block guard protocol settings for @shost.
4873  * This routine also allocates memory for debugging bg buffers.
4874  **/
4875 static void
4876 lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4877 {
4878         int pagecnt = 10;
4879         if (lpfc_prot_mask && lpfc_prot_guard) {
4880                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4881                                 "1478 Registering BlockGuard with the "
4882                                 "SCSI layer\n");
4883                 scsi_host_set_prot(shost, lpfc_prot_mask);
4884                 scsi_host_set_guard(shost, lpfc_prot_guard);
4885         }
4886         if (!_dump_buf_data) {
4887                 while (pagecnt) {
4888                         spin_lock_init(&_dump_buf_lock);
4889                         _dump_buf_data =
4890                                 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4891                         if (_dump_buf_data) {
4892                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4893                                         "9043 BLKGRD: allocated %d pages for "
4894                                        "_dump_buf_data at 0x%p\n",
4895                                        (1 << pagecnt), _dump_buf_data);
4896                                 _dump_buf_data_order = pagecnt;
4897                                 memset(_dump_buf_data, 0,
4898                                        ((1 << PAGE_SHIFT) << pagecnt));
4899                                 break;
4900                         } else
4901                                 --pagecnt;
4902                 }
4903                 if (!_dump_buf_data_order)
4904                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4905                                 "9044 BLKGRD: ERROR unable to allocate "
4906                                "memory for hexdump\n");
4907         } else
4908                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4909                         "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
4910                        "\n", _dump_buf_data);
4911         if (!_dump_buf_dif) {
4912                 while (pagecnt) {
4913                         _dump_buf_dif =
4914                                 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4915                         if (_dump_buf_dif) {
4916                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4917                                         "9046 BLKGRD: allocated %d pages for "
4918                                        "_dump_buf_dif at 0x%p\n",
4919                                        (1 << pagecnt), _dump_buf_dif);
4920                                 _dump_buf_dif_order = pagecnt;
4921                                 memset(_dump_buf_dif, 0,
4922                                        ((1 << PAGE_SHIFT) << pagecnt));
4923                                 break;
4924                         } else
4925                                 --pagecnt;
4926                 }
4927                 if (!_dump_buf_dif_order)
4928                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4929                         "9047 BLKGRD: ERROR unable to allocate "
4930                                "memory for hexdump\n");
4931         } else
4932                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4933                         "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
4934                        _dump_buf_dif);
4935 }
4936
4937 /**
4938  * lpfc_post_init_setup - Perform necessary device post initialization setup.
4939  * @phba: pointer to lpfc hba data structure.
4940  *
4941  * This routine is invoked to perform all the necessary post initialization
4942  * setup for the device.
4943  **/
4944 static void
4945 lpfc_post_init_setup(struct lpfc_hba *phba)
4946 {
4947         struct Scsi_Host  *shost;
4948         struct lpfc_adapter_event_header adapter_event;
4949
4950         /* Get the default values for Model Name and Description */
4951         lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4952
4953         /*
4954          * hba setup may have changed the hba_queue_depth so we need to
4955          * adjust the value of can_queue.
4956          */
4957         shost = pci_get_drvdata(phba->pcidev);
4958         shost->can_queue = phba->cfg_hba_queue_depth - 10;
4959         if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4960                 lpfc_setup_bg(phba, shost);
4961
4962         lpfc_host_attrib_init(shost);
4963
4964         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4965                 spin_lock_irq(shost->host_lock);
4966                 lpfc_poll_start_timer(phba);
4967                 spin_unlock_irq(shost->host_lock);
4968         }
4969
4970         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4971                         "0428 Perform SCSI scan\n");
4972         /* Send board arrival event to upper layer */
4973         adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4974         adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4975         fc_host_post_vendor_event(shost, fc_get_event_number(),
4976                                   sizeof(adapter_event),
4977                                   (char *) &adapter_event,
4978                                   LPFC_NL_VENDOR_ID);
4979         return;
4980 }
4981
4982 /**
4983  * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4984  * @phba: pointer to lpfc hba data structure.
4985  *
4986  * This routine is invoked to set up the PCI device memory space for device
4987  * with SLI-3 interface spec.
4988  *
4989  * Return codes
4990  *      0 - successful
4991  *      other values - error
4992  **/
4993 static int
4994 lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4995 {
4996         struct pci_dev *pdev;
4997         unsigned long bar0map_len, bar2map_len;
4998         int i, hbq_count;
4999         void *ptr;
5000         int error = -ENODEV;
5001
5002         /* Obtain PCI device reference */
5003         if (!phba->pcidev)
5004                 return error;
5005         else
5006                 pdev = phba->pcidev;
5007
5008         /* Set the device DMA mask size */
5009         if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5010          || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5011                 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5012                  || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
5013                         return error;
5014                 }
5015         }
5016
5017         /* Get the bus address of Bar0 and Bar2 and the number of bytes
5018          * required by each mapping.
5019          */
5020         phba->pci_bar0_map = pci_resource_start(pdev, 0);
5021         bar0map_len = pci_resource_len(pdev, 0);
5022
5023         phba->pci_bar2_map = pci_resource_start(pdev, 2);
5024         bar2map_len = pci_resource_len(pdev, 2);
5025
5026         /* Map HBA SLIM to a kernel virtual address. */
5027         phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5028         if (!phba->slim_memmap_p) {
5029                 dev_printk(KERN_ERR, &pdev->dev,
5030                            "ioremap failed for SLIM memory.\n");
5031                 goto out;
5032         }
5033
5034         /* Map HBA Control Registers to a kernel virtual address. */
5035         phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5036         if (!phba->ctrl_regs_memmap_p) {
5037                 dev_printk(KERN_ERR, &pdev->dev,
5038                            "ioremap failed for HBA control registers.\n");
5039                 goto out_iounmap_slim;
5040         }
5041
5042         /* Allocate memory for SLI-2 structures */
5043         phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5044                                                SLI2_SLIM_SIZE,
5045                                                &phba->slim2p.phys,
5046                                                GFP_KERNEL);
5047         if (!phba->slim2p.virt)
5048                 goto out_iounmap;
5049
5050         memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5051         phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
5052         phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5053         phba->IOCBs = (phba->slim2p.virt +
5054                        offsetof(struct lpfc_sli2_slim, IOCBs));
5055
5056         phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5057                                                  lpfc_sli_hbq_size(),
5058                                                  &phba->hbqslimp.phys,
5059                                                  GFP_KERNEL);
5060         if (!phba->hbqslimp.virt)
5061                 goto out_free_slim;
5062
5063         hbq_count = lpfc_sli_hbq_count();
5064         ptr = phba->hbqslimp.virt;
5065         for (i = 0; i < hbq_count; ++i) {
5066                 phba->hbqs[i].hbq_virt = ptr;
5067                 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5068                 ptr += (lpfc_hbq_defs[i]->entry_count *
5069                         sizeof(struct lpfc_hbq_entry));
5070         }
5071         phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5072         phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5073
5074         memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5075
5076         INIT_LIST_HEAD(&phba->rb_pend_list);
5077
5078         phba->MBslimaddr = phba->slim_memmap_p;
5079         phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5080         phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5081         phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5082         phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
5083
5084         return 0;
5085
5086 out_free_slim:
5087         dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5088                           phba->slim2p.virt, phba->slim2p.phys);
5089 out_iounmap:
5090         iounmap(phba->ctrl_regs_memmap_p);
5091 out_iounmap_slim:
5092         iounmap(phba->slim_memmap_p);
5093 out:
5094         return error;
5095 }
5096
5097 /**
5098  * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5099  * @phba: pointer to lpfc hba data structure.
5100  *
5101  * This routine is invoked to unset the PCI device memory space for device
5102  * with SLI-3 interface spec.
5103  **/
5104 static void
5105 lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5106 {
5107         struct pci_dev *pdev;
5108
5109         /* Obtain PCI device reference */
5110         if (!phba->pcidev)
5111                 return;
5112         else
5113                 pdev = phba->pcidev;
5114
5115         /* Free coherent DMA memory allocated */
5116         dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5117                           phba->hbqslimp.virt, phba->hbqslimp.phys);
5118         dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5119                           phba->slim2p.virt, phba->slim2p.phys);
5120
5121         /* I/O memory unmap */
5122         iounmap(phba->ctrl_regs_memmap_p);
5123         iounmap(phba->slim_memmap_p);
5124
5125         return;
5126 }
5127
5128 /**
5129  * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5130  * @phba: pointer to lpfc hba data structure.
5131  *
5132  * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5133  * done and check status.
5134  *
5135  * Return 0 if successful, otherwise -ENODEV.
5136  **/
5137 int
5138 lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5139 {
5140         struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg;
5141         int i, port_error = -ENODEV;
5142
5143         if (!phba->sli4_hba.STAregaddr)
5144                 return -ENODEV;
5145
5146         /* Wait up to 30 seconds for the SLI Port POST done and ready */
5147         for (i = 0; i < 3000; i++) {
5148                 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
5149                 /* Encounter fatal POST error, break out */
5150                 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
5151                         port_error = -ENODEV;
5152                         break;
5153                 }
5154                 if (LPFC_POST_STAGE_ARMFW_READY ==
5155                     bf_get(lpfc_hst_state_port_status, &sta_reg)) {
5156                         port_error = 0;
5157                         break;
5158                 }
5159                 msleep(10);
5160         }
5161
5162         if (port_error)
5163                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5164                         "1408 Failure HBA POST Status: sta_reg=0x%x, "
5165                         "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
5166                         "dl=x%x, pstatus=x%x\n", sta_reg.word0,
5167                         bf_get(lpfc_hst_state_perr, &sta_reg),
5168                         bf_get(lpfc_hst_state_sfi, &sta_reg),
5169                         bf_get(lpfc_hst_state_nip, &sta_reg),
5170                         bf_get(lpfc_hst_state_ipc, &sta_reg),
5171                         bf_get(lpfc_hst_state_xrom, &sta_reg),
5172                         bf_get(lpfc_hst_state_dl, &sta_reg),
5173                         bf_get(lpfc_hst_state_port_status, &sta_reg));
5174
5175         /* Log device information */
5176         phba->sli4_hba.sli_intf.word0 = readl(phba->sli4_hba.SLIINTFregaddr);
5177         if (bf_get(lpfc_sli_intf_valid,
5178                    &phba->sli4_hba.sli_intf) == LPFC_SLI_INTF_VALID) {
5179                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5180                                 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
5181                                 "FeatureL1=0x%x, FeatureL2=0x%x\n",
5182                                 bf_get(lpfc_sli_intf_sli_family,
5183                                        &phba->sli4_hba.sli_intf),
5184                                 bf_get(lpfc_sli_intf_slirev,
5185                                        &phba->sli4_hba.sli_intf),
5186                                 bf_get(lpfc_sli_intf_featurelevel1,
5187                                        &phba->sli4_hba.sli_intf),
5188                                 bf_get(lpfc_sli_intf_featurelevel2,
5189                                        &phba->sli4_hba.sli_intf));
5190         }
5191         phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
5192         phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
5193         /* With uncoverable error, log the error message and return error */
5194         uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
5195         uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
5196         if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5197             (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5198                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5199                                 "1422 HBA Unrecoverable error: "
5200                                 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
5201                                 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
5202                                 uerrlo_reg.word0, uerrhi_reg.word0,
5203                                 phba->sli4_hba.ue_mask_lo,
5204                                 phba->sli4_hba.ue_mask_hi);
5205                 return -ENODEV;
5206         }
5207
5208         return port_error;
5209 }
5210
5211 /**
5212  * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5213  * @phba: pointer to lpfc hba data structure.
5214  *
5215  * This routine is invoked to set up SLI4 BAR0 PCI config space register
5216  * memory map.
5217  **/
5218 static void
5219 lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
5220 {
5221         phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5222                                         LPFC_UERR_STATUS_LO;
5223         phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5224                                         LPFC_UERR_STATUS_HI;
5225         phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5226                                         LPFC_UE_MASK_LO;
5227         phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5228                                         LPFC_UE_MASK_HI;
5229         phba->sli4_hba.SLIINTFregaddr = phba->sli4_hba.conf_regs_memmap_p +
5230                                         LPFC_SLI_INTF;
5231 }
5232
5233 /**
5234  * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5235  * @phba: pointer to lpfc hba data structure.
5236  *
5237  * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5238  * memory map.
5239  **/
5240 static void
5241 lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5242 {
5243
5244         phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5245                                     LPFC_HST_STATE;
5246         phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5247                                     LPFC_HST_ISR0;
5248         phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5249                                     LPFC_HST_IMR0;
5250         phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5251                                      LPFC_HST_ISCR0;
5252         return;
5253 }
5254
5255 /**
5256  * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5257  * @phba: pointer to lpfc hba data structure.
5258  * @vf: virtual function number
5259  *
5260  * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5261  * based on the given viftual function number, @vf.
5262  *
5263  * Return 0 if successful, otherwise -ENODEV.
5264  **/
5265 static int
5266 lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5267 {
5268         if (vf > LPFC_VIR_FUNC_MAX)
5269                 return -ENODEV;
5270
5271         phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5272                                 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5273         phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5274                                 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5275         phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5276                                 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5277         phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5278                                 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5279         phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5280                                 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5281         return 0;
5282 }
5283
5284 /**
5285  * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5286  * @phba: pointer to lpfc hba data structure.
5287  *
5288  * This routine is invoked to create the bootstrap mailbox
5289  * region consistent with the SLI-4 interface spec.  This
5290  * routine allocates all memory necessary to communicate
5291  * mailbox commands to the port and sets up all alignment
5292  * needs.  No locks are expected to be held when calling
5293  * this routine.
5294  *
5295  * Return codes
5296  *      0 - successful
5297  *      ENOMEM - could not allocated memory.
5298  **/
5299 static int
5300 lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5301 {
5302         uint32_t bmbx_size;
5303         struct lpfc_dmabuf *dmabuf;
5304         struct dma_address *dma_address;
5305         uint32_t pa_addr;
5306         uint64_t phys_addr;
5307
5308         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5309         if (!dmabuf)
5310                 return -ENOMEM;
5311
5312         /*
5313          * The bootstrap mailbox region is comprised of 2 parts
5314          * plus an alignment restriction of 16 bytes.
5315          */
5316         bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5317         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5318                                           bmbx_size,
5319                                           &dmabuf->phys,
5320                                           GFP_KERNEL);
5321         if (!dmabuf->virt) {
5322                 kfree(dmabuf);
5323                 return -ENOMEM;
5324         }
5325         memset(dmabuf->virt, 0, bmbx_size);
5326
5327         /*
5328          * Initialize the bootstrap mailbox pointers now so that the register
5329          * operations are simple later.  The mailbox dma address is required
5330          * to be 16-byte aligned.  Also align the virtual memory as each
5331          * maibox is copied into the bmbx mailbox region before issuing the
5332          * command to the port.
5333          */
5334         phba->sli4_hba.bmbx.dmabuf = dmabuf;
5335         phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5336
5337         phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5338                                               LPFC_ALIGN_16_BYTE);
5339         phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5340                                               LPFC_ALIGN_16_BYTE);
5341
5342         /*
5343          * Set the high and low physical addresses now.  The SLI4 alignment
5344          * requirement is 16 bytes and the mailbox is posted to the port
5345          * as two 30-bit addresses.  The other data is a bit marking whether
5346          * the 30-bit address is the high or low address.
5347          * Upcast bmbx aphys to 64bits so shift instruction compiles
5348          * clean on 32 bit machines.
5349          */
5350         dma_address = &phba->sli4_hba.bmbx.dma_address;
5351         phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5352         pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5353         dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5354                                            LPFC_BMBX_BIT1_ADDR_HI);
5355
5356         pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5357         dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5358                                            LPFC_BMBX_BIT1_ADDR_LO);
5359         return 0;
5360 }
5361
5362 /**
5363  * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5364  * @phba: pointer to lpfc hba data structure.
5365  *
5366  * This routine is invoked to teardown the bootstrap mailbox
5367  * region and release all host resources. This routine requires
5368  * the caller to ensure all mailbox commands recovered, no
5369  * additional mailbox comands are sent, and interrupts are disabled
5370  * before calling this routine.
5371  *
5372  **/
5373 static void
5374 lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5375 {
5376         dma_free_coherent(&phba->pcidev->dev,
5377                           phba->sli4_hba.bmbx.bmbx_size,
5378                           phba->sli4_hba.bmbx.dmabuf->virt,
5379                           phba->sli4_hba.bmbx.dmabuf->phys);
5380
5381         kfree(phba->sli4_hba.bmbx.dmabuf);
5382         memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5383 }
5384
5385 /**
5386  * lpfc_sli4_read_config - Get the config parameters.
5387  * @phba: pointer to lpfc hba data structure.
5388  *
5389  * This routine is invoked to read the configuration parameters from the HBA.
5390  * The configuration parameters are used to set the base and maximum values
5391  * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5392  * allocation for the port.
5393  *
5394  * Return codes
5395  *      0 - successful
5396  *      ENOMEM - No availble memory
5397  *      EIO - The mailbox failed to complete successfully.
5398  **/
5399 static int
5400 lpfc_sli4_read_config(struct lpfc_hba *phba)
5401 {
5402         LPFC_MBOXQ_t *pmb;
5403         struct lpfc_mbx_read_config *rd_config;
5404         uint32_t rc = 0;
5405
5406         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5407         if (!pmb) {
5408                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5409                                 "2011 Unable to allocate memory for issuing "
5410                                 "SLI_CONFIG_SPECIAL mailbox command\n");
5411                 return -ENOMEM;
5412         }
5413
5414         lpfc_read_config(phba, pmb);
5415
5416         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5417         if (rc != MBX_SUCCESS) {
5418                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5419                         "2012 Mailbox failed , mbxCmd x%x "
5420                         "READ_CONFIG, mbxStatus x%x\n",
5421                         bf_get(lpfc_mqe_command, &pmb->u.mqe),
5422                         bf_get(lpfc_mqe_status, &pmb->u.mqe));
5423                 rc = -EIO;
5424         } else {
5425                 rd_config = &pmb->u.mqe.un.rd_config;
5426                 phba->sli4_hba.max_cfg_param.max_xri =
5427                         bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5428                 phba->sli4_hba.max_cfg_param.xri_base =
5429                         bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5430                 phba->sli4_hba.max_cfg_param.max_vpi =
5431                         bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5432                 phba->sli4_hba.max_cfg_param.vpi_base =
5433                         bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5434                 phba->sli4_hba.max_cfg_param.max_rpi =
5435                         bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5436                 phba->sli4_hba.max_cfg_param.rpi_base =
5437                         bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5438                 phba->sli4_hba.max_cfg_param.max_vfi =
5439                         bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5440                 phba->sli4_hba.max_cfg_param.vfi_base =
5441                         bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5442                 phba->sli4_hba.max_cfg_param.max_fcfi =
5443                         bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5444                 phba->sli4_hba.max_cfg_param.fcfi_base =
5445                         bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5446                 phba->sli4_hba.max_cfg_param.max_eq =
5447                         bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5448                 phba->sli4_hba.max_cfg_param.max_rq =
5449                         bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5450                 phba->sli4_hba.max_cfg_param.max_wq =
5451                         bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5452                 phba->sli4_hba.max_cfg_param.max_cq =
5453                         bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5454                 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5455                 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5456                 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5457                 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5458                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
5459                 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5460                                 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
5461                 phba->max_vports = phba->max_vpi;
5462                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5463                                 "2003 cfg params XRI(B:%d M:%d), "
5464                                 "VPI(B:%d M:%d) "
5465                                 "VFI(B:%d M:%d) "
5466                                 "RPI(B:%d M:%d) "
5467                                 "FCFI(B:%d M:%d)\n",
5468                                 phba->sli4_hba.max_cfg_param.xri_base,
5469                                 phba->sli4_hba.max_cfg_param.max_xri,
5470                                 phba->sli4_hba.max_cfg_param.vpi_base,
5471                                 phba->sli4_hba.max_cfg_param.max_vpi,
5472                                 phba->sli4_hba.max_cfg_param.vfi_base,
5473                                 phba->sli4_hba.max_cfg_param.max_vfi,
5474                                 phba->sli4_hba.max_cfg_param.rpi_base,
5475                                 phba->sli4_hba.max_cfg_param.max_rpi,
5476                                 phba->sli4_hba.max_cfg_param.fcfi_base,
5477                                 phba->sli4_hba.max_cfg_param.max_fcfi);
5478         }
5479         mempool_free(pmb, phba->mbox_mem_pool);
5480
5481         /* Reset the DFT_HBA_Q_DEPTH to the max xri  */
5482         if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
5483                 phba->cfg_hba_queue_depth =
5484                                 phba->sli4_hba.max_cfg_param.max_xri;
5485         return rc;
5486 }
5487
5488 /**
5489  * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
5490  * @phba: pointer to lpfc hba data structure.
5491  *
5492  * This routine is invoked to setup the host-side endian order to the
5493  * HBA consistent with the SLI-4 interface spec.
5494  *
5495  * Return codes
5496  *      0 - successful
5497  *      ENOMEM - No availble memory
5498  *      EIO - The mailbox failed to complete successfully.
5499  **/
5500 static int
5501 lpfc_setup_endian_order(struct lpfc_hba *phba)
5502 {
5503         LPFC_MBOXQ_t *mboxq;
5504         uint32_t rc = 0;
5505         uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5506                                       HOST_ENDIAN_HIGH_WORD1};
5507
5508         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5509         if (!mboxq) {
5510                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5511                                 "0492 Unable to allocate memory for issuing "
5512                                 "SLI_CONFIG_SPECIAL mailbox command\n");
5513                 return -ENOMEM;
5514         }
5515
5516         /*
5517          * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5518          * words to contain special data values and no other data.
5519          */
5520         memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5521         memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5522         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5523         if (rc != MBX_SUCCESS) {
5524                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5525                                 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5526                                 "status x%x\n",
5527                                 rc);
5528                 rc = -EIO;
5529         }
5530
5531         mempool_free(mboxq, phba->mbox_mem_pool);
5532         return rc;
5533 }
5534
5535 /**
5536  * lpfc_sli4_queue_create - Create all the SLI4 queues
5537  * @phba: pointer to lpfc hba data structure.
5538  *
5539  * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5540  * operation. For each SLI4 queue type, the parameters such as queue entry
5541  * count (queue depth) shall be taken from the module parameter. For now,
5542  * we just use some constant number as place holder.
5543  *
5544  * Return codes
5545  *      0 - successful
5546  *      ENOMEM - No availble memory
5547  *      EIO - The mailbox failed to complete successfully.
5548  **/
5549 static int
5550 lpfc_sli4_queue_create(struct lpfc_hba *phba)
5551 {
5552         struct lpfc_queue *qdesc;
5553         int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5554         int cfg_fcp_wq_count;
5555         int cfg_fcp_eq_count;
5556
5557         /*
5558          * Sanity check for confiugred queue parameters against the run-time
5559          * device parameters
5560          */
5561
5562         /* Sanity check on FCP fast-path WQ parameters */
5563         cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5564         if (cfg_fcp_wq_count >
5565             (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5566                 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5567                                    LPFC_SP_WQN_DEF;
5568                 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5569                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5570                                         "2581 Not enough WQs (%d) from "
5571                                         "the pci function for supporting "
5572                                         "FCP WQs (%d)\n",
5573                                         phba->sli4_hba.max_cfg_param.max_wq,
5574                                         phba->cfg_fcp_wq_count);
5575                         goto out_error;
5576                 }
5577                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5578                                 "2582 Not enough WQs (%d) from the pci "
5579                                 "function for supporting the requested "
5580                                 "FCP WQs (%d), the actual FCP WQs can "
5581                                 "be supported: %d\n",
5582                                 phba->sli4_hba.max_cfg_param.max_wq,
5583                                 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5584         }
5585         /* The actual number of FCP work queues adopted */
5586         phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5587
5588         /* Sanity check on FCP fast-path EQ parameters */
5589         cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5590         if (cfg_fcp_eq_count >
5591             (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5592                 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5593                                    LPFC_SP_EQN_DEF;
5594                 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5595                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5596                                         "2574 Not enough EQs (%d) from the "
5597                                         "pci function for supporting FCP "
5598                                         "EQs (%d)\n",
5599                                         phba->sli4_hba.max_cfg_param.max_eq,
5600                                         phba->cfg_fcp_eq_count);
5601                         goto out_error;
5602                 }
5603                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5604                                 "2575 Not enough EQs (%d) from the pci "
5605                                 "function for supporting the requested "
5606                                 "FCP EQs (%d), the actual FCP EQs can "
5607                                 "be supported: %d\n",
5608                                 phba->sli4_hba.max_cfg_param.max_eq,
5609                                 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5610         }
5611         /* It does not make sense to have more EQs than WQs */
5612         if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5613                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5614                                 "2593 The FCP EQ count(%d) cannot be greater "
5615                                 "than the FCP WQ count(%d), limiting the "
5616                                 "FCP EQ count to %d\n", cfg_fcp_eq_count,
5617                                 phba->cfg_fcp_wq_count,
5618                                 phba->cfg_fcp_wq_count);
5619                 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5620         }
5621         /* The actual number of FCP event queues adopted */
5622         phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5623         /* The overall number of event queues used */
5624         phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5625
5626         /*
5627          * Create Event Queues (EQs)
5628          */
5629
5630         /* Get EQ depth from module parameter, fake the default for now */
5631         phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5632         phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5633
5634         /* Create slow path event queue */
5635         qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5636                                       phba->sli4_hba.eq_ecount);
5637         if (!qdesc) {
5638                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5639                                 "0496 Failed allocate slow-path EQ\n");
5640                 goto out_error;
5641         }
5642         phba->sli4_hba.sp_eq = qdesc;
5643
5644         /* Create fast-path FCP Event Queue(s) */
5645         phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5646                                phba->cfg_fcp_eq_count), GFP_KERNEL);
5647         if (!phba->sli4_hba.fp_eq) {
5648                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5649                                 "2576 Failed allocate memory for fast-path "
5650                                 "EQ record array\n");
5651                 goto out_free_sp_eq;
5652         }
5653         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5654                 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5655                                               phba->sli4_hba.eq_ecount);
5656                 if (!qdesc) {
5657                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5658                                         "0497 Failed allocate fast-path EQ\n");
5659                         goto out_free_fp_eq;
5660                 }
5661                 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5662         }
5663
5664         /*
5665          * Create Complete Queues (CQs)
5666          */
5667
5668         /* Get CQ depth from module parameter, fake the default for now */
5669         phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5670         phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5671
5672         /* Create slow-path Mailbox Command Complete Queue */
5673         qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5674                                       phba->sli4_hba.cq_ecount);
5675         if (!qdesc) {
5676                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5677                                 "0500 Failed allocate slow-path mailbox CQ\n");
5678                 goto out_free_fp_eq;
5679         }
5680         phba->sli4_hba.mbx_cq = qdesc;
5681
5682         /* Create slow-path ELS Complete Queue */
5683         qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5684                                       phba->sli4_hba.cq_ecount);
5685         if (!qdesc) {
5686                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5687                                 "0501 Failed allocate slow-path ELS CQ\n");
5688                 goto out_free_mbx_cq;
5689         }
5690         phba->sli4_hba.els_cq = qdesc;
5691
5692
5693         /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5694         phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5695                                 phba->cfg_fcp_eq_count), GFP_KERNEL);
5696         if (!phba->sli4_hba.fcp_cq) {
5697                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5698                                 "2577 Failed allocate memory for fast-path "
5699                                 "CQ record array\n");
5700                 goto out_free_els_cq;
5701         }
5702         for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5703                 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5704                                               phba->sli4_hba.cq_ecount);
5705                 if (!qdesc) {
5706                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5707                                         "0499 Failed allocate fast-path FCP "
5708                                         "CQ (%d)\n", fcp_cqidx);
5709                         goto out_free_fcp_cq;
5710                 }
5711                 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5712         }
5713
5714         /* Create Mailbox Command Queue */
5715         phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5716         phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5717
5718         qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5719                                       phba->sli4_hba.mq_ecount);
5720         if (!qdesc) {
5721                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5722                                 "0505 Failed allocate slow-path MQ\n");
5723                 goto out_free_fcp_cq;
5724         }
5725         phba->sli4_hba.mbx_wq = qdesc;
5726
5727         /*
5728          * Create all the Work Queues (WQs)
5729          */
5730         phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5731         phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5732
5733         /* Create slow-path ELS Work Queue */
5734         qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5735                                       phba->sli4_hba.wq_ecount);
5736         if (!qdesc) {
5737                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5738                                 "0504 Failed allocate slow-path ELS WQ\n");
5739                 goto out_free_mbx_wq;
5740         }
5741         phba->sli4_hba.els_wq = qdesc;
5742
5743         /* Create fast-path FCP Work Queue(s) */
5744         phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5745                                 phba->cfg_fcp_wq_count), GFP_KERNEL);
5746         if (!phba->sli4_hba.fcp_wq) {
5747                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5748                                 "2578 Failed allocate memory for fast-path "
5749                                 "WQ record array\n");
5750                 goto out_free_els_wq;
5751         }
5752         for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5753                 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5754                                               phba->sli4_hba.wq_ecount);
5755                 if (!qdesc) {
5756                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5757                                         "0503 Failed allocate fast-path FCP "
5758                                         "WQ (%d)\n", fcp_wqidx);
5759                         goto out_free_fcp_wq;
5760                 }
5761                 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5762         }
5763
5764         /*
5765          * Create Receive Queue (RQ)
5766          */
5767         phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5768         phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5769
5770         /* Create Receive Queue for header */
5771         qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5772                                       phba->sli4_hba.rq_ecount);
5773         if (!qdesc) {
5774                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5775                                 "0506 Failed allocate receive HRQ\n");
5776                 goto out_free_fcp_wq;
5777         }
5778         phba->sli4_hba.hdr_rq = qdesc;
5779
5780         /* Create Receive Queue for data */
5781         qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5782                                       phba->sli4_hba.rq_ecount);
5783         if (!qdesc) {
5784                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5785                                 "0507 Failed allocate receive DRQ\n");
5786                 goto out_free_hdr_rq;
5787         }
5788         phba->sli4_hba.dat_rq = qdesc;
5789
5790         return 0;
5791
5792 out_free_hdr_rq:
5793         lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5794         phba->sli4_hba.hdr_rq = NULL;
5795 out_free_fcp_wq:
5796         for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5797                 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5798                 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5799         }
5800         kfree(phba->sli4_hba.fcp_wq);
5801 out_free_els_wq:
5802         lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5803         phba->sli4_hba.els_wq = NULL;
5804 out_free_mbx_wq:
5805         lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5806         phba->sli4_hba.mbx_wq = NULL;
5807 out_free_fcp_cq:
5808         for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5809                 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5810                 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5811         }
5812         kfree(phba->sli4_hba.fcp_cq);
5813 out_free_els_cq:
5814         lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5815         phba->sli4_hba.els_cq = NULL;
5816 out_free_mbx_cq:
5817         lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5818         phba->sli4_hba.mbx_cq = NULL;
5819 out_free_fp_eq:
5820         for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5821                 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5822                 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5823         }
5824         kfree(phba->sli4_hba.fp_eq);
5825 out_free_sp_eq:
5826         lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5827         phba->sli4_hba.sp_eq = NULL;
5828 out_error:
5829         return -ENOMEM;
5830 }
5831
5832 /**
5833  * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5834  * @phba: pointer to lpfc hba data structure.
5835  *
5836  * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5837  * operation.
5838  *
5839  * Return codes
5840  *      0 - successful
5841  *      ENOMEM - No availble memory
5842  *      EIO - The mailbox failed to complete successfully.
5843  **/
5844 static void
5845 lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5846 {
5847         int fcp_qidx;
5848
5849         /* Release mailbox command work queue */
5850         lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5851         phba->sli4_hba.mbx_wq = NULL;
5852
5853         /* Release ELS work queue */
5854         lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5855         phba->sli4_hba.els_wq = NULL;
5856
5857         /* Release FCP work queue */
5858         for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5859                 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5860         kfree(phba->sli4_hba.fcp_wq);
5861         phba->sli4_hba.fcp_wq = NULL;
5862
5863         /* Release unsolicited receive queue */
5864         lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5865         phba->sli4_hba.hdr_rq = NULL;
5866         lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5867         phba->sli4_hba.dat_rq = NULL;
5868
5869         /* Release ELS complete queue */
5870         lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5871         phba->sli4_hba.els_cq = NULL;
5872
5873         /* Release mailbox command complete queue */
5874         lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5875         phba->sli4_hba.mbx_cq = NULL;
5876
5877         /* Release FCP response complete queue */
5878         for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5879                 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5880         kfree(phba->sli4_hba.fcp_cq);
5881         phba->sli4_hba.fcp_cq = NULL;
5882
5883         /* Release fast-path event queue */
5884         for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5885                 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5886         kfree(phba->sli4_hba.fp_eq);
5887         phba->sli4_hba.fp_eq = NULL;
5888
5889         /* Release slow-path event queue */
5890         lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5891         phba->sli4_hba.sp_eq = NULL;
5892
5893         return;
5894 }
5895
5896 /**
5897  * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5898  * @phba: pointer to lpfc hba data structure.
5899  *
5900  * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5901  * operation.
5902  *
5903  * Return codes
5904  *      0 - successful
5905  *      ENOMEM - No availble memory
5906  *      EIO - The mailbox failed to complete successfully.
5907  **/
5908 int
5909 lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5910 {
5911         int rc = -ENOMEM;
5912         int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5913         int fcp_cq_index = 0;
5914
5915         /*
5916          * Set up Event Queues (EQs)
5917          */
5918
5919         /* Set up slow-path event queue */
5920         if (!phba->sli4_hba.sp_eq) {
5921                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5922                                 "0520 Slow-path EQ not allocated\n");
5923                 goto out_error;
5924         }
5925         rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5926                             LPFC_SP_DEF_IMAX);
5927         if (rc) {
5928                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5929                                 "0521 Failed setup of slow-path EQ: "
5930                                 "rc = 0x%x\n", rc);
5931                 goto out_error;
5932         }
5933         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5934                         "2583 Slow-path EQ setup: queue-id=%d\n",
5935                         phba->sli4_hba.sp_eq->queue_id);
5936
5937         /* Set up fast-path event queue */
5938         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5939                 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5940                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5941                                         "0522 Fast-path EQ (%d) not "
5942                                         "allocated\n", fcp_eqidx);
5943                         goto out_destroy_fp_eq;
5944                 }
5945                 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5946                                     phba->cfg_fcp_imax);
5947                 if (rc) {
5948                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5949                                         "0523 Failed setup of fast-path EQ "
5950                                         "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5951                         goto out_destroy_fp_eq;
5952                 }
5953                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5954                                 "2584 Fast-path EQ setup: "
5955                                 "queue[%d]-id=%d\n", fcp_eqidx,
5956                                 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5957         }
5958
5959         /*
5960          * Set up Complete Queues (CQs)
5961          */
5962
5963         /* Set up slow-path MBOX Complete Queue as the first CQ */
5964         if (!phba->sli4_hba.mbx_cq) {
5965                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5966                                 "0528 Mailbox CQ not allocated\n");
5967                 goto out_destroy_fp_eq;
5968         }
5969         rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5970                             LPFC_MCQ, LPFC_MBOX);
5971         if (rc) {
5972                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5973                                 "0529 Failed setup of slow-path mailbox CQ: "
5974                                 "rc = 0x%x\n", rc);
5975                 goto out_destroy_fp_eq;
5976         }
5977         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5978                         "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5979                         phba->sli4_hba.mbx_cq->queue_id,
5980                         phba->sli4_hba.sp_eq->queue_id);
5981
5982         /* Set up slow-path ELS Complete Queue */
5983         if (!phba->sli4_hba.els_cq) {
5984                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5985                                 "0530 ELS CQ not allocated\n");
5986                 goto out_destroy_mbx_cq;
5987         }
5988         rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5989                             LPFC_WCQ, LPFC_ELS);
5990         if (rc) {
5991                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5992                                 "0531 Failed setup of slow-path ELS CQ: "
5993                                 "rc = 0x%x\n", rc);
5994                 goto out_destroy_mbx_cq;
5995         }
5996         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5997                         "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5998                         phba->sli4_hba.els_cq->queue_id,
5999                         phba->sli4_hba.sp_eq->queue_id);
6000
6001         /* Set up fast-path FCP Response Complete Queue */
6002         for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
6003                 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6004                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6005                                         "0526 Fast-path FCP CQ (%d) not "
6006                                         "allocated\n", fcp_cqidx);
6007                         goto out_destroy_fcp_cq;
6008                 }
6009                 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
6010                                     phba->sli4_hba.fp_eq[fcp_cqidx],
6011                                     LPFC_WCQ, LPFC_FCP);
6012                 if (rc) {
6013                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6014                                         "0527 Failed setup of fast-path FCP "
6015                                         "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6016                         goto out_destroy_fcp_cq;
6017                 }
6018                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6019                                 "2588 FCP CQ setup: cq[%d]-id=%d, "
6020                                 "parent eq[%d]-id=%d\n",
6021                                 fcp_cqidx,
6022                                 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
6023                                 fcp_cqidx,
6024                                 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
6025         }
6026
6027         /*
6028          * Set up all the Work Queues (WQs)
6029          */
6030
6031         /* Set up Mailbox Command Queue */
6032         if (!phba->sli4_hba.mbx_wq) {
6033                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6034                                 "0538 Slow-path MQ not allocated\n");
6035                 goto out_destroy_fcp_cq;
6036         }
6037         rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6038                             phba->sli4_hba.mbx_cq, LPFC_MBOX);
6039         if (rc) {
6040                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6041                                 "0539 Failed setup of slow-path MQ: "
6042                                 "rc = 0x%x\n", rc);
6043                 goto out_destroy_fcp_cq;
6044         }
6045         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6046                         "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6047                         phba->sli4_hba.mbx_wq->queue_id,
6048                         phba->sli4_hba.mbx_cq->queue_id);
6049
6050         /* Set up slow-path ELS Work Queue */
6051         if (!phba->sli4_hba.els_wq) {
6052                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6053                                 "0536 Slow-path ELS WQ not allocated\n");
6054                 goto out_destroy_mbx_wq;
6055         }
6056         rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6057                             phba->sli4_hba.els_cq, LPFC_ELS);
6058         if (rc) {
6059                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6060                                 "0537 Failed setup of slow-path ELS WQ: "
6061                                 "rc = 0x%x\n", rc);
6062                 goto out_destroy_mbx_wq;
6063         }
6064         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6065                         "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6066                         phba->sli4_hba.els_wq->queue_id,
6067                         phba->sli4_hba.els_cq->queue_id);
6068
6069         /* Set up fast-path FCP Work Queue */
6070         for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6071                 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6072                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6073                                         "0534 Fast-path FCP WQ (%d) not "
6074                                         "allocated\n", fcp_wqidx);
6075                         goto out_destroy_fcp_wq;
6076                 }
6077                 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6078                                     phba->sli4_hba.fcp_cq[fcp_cq_index],
6079                                     LPFC_FCP);
6080                 if (rc) {
6081                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6082                                         "0535 Failed setup of fast-path FCP "
6083                                         "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6084                         goto out_destroy_fcp_wq;
6085                 }
6086                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6087                                 "2591 FCP WQ setup: wq[%d]-id=%d, "
6088                                 "parent cq[%d]-id=%d\n",
6089                                 fcp_wqidx,
6090                                 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6091                                 fcp_cq_index,
6092                                 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6093                 /* Round robin FCP Work Queue's Completion Queue assignment */
6094                 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
6095         }
6096
6097         /*
6098          * Create Receive Queue (RQ)
6099          */
6100         if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6101                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6102                                 "0540 Receive Queue not allocated\n");
6103                 goto out_destroy_fcp_wq;
6104         }
6105         rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
6106                             phba->sli4_hba.els_cq, LPFC_USOL);
6107         if (rc) {
6108                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6109                                 "0541 Failed setup of Receive Queue: "
6110                                 "rc = 0x%x\n", rc);
6111                 goto out_destroy_fcp_wq;
6112         }
6113         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6114                         "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6115                         "parent cq-id=%d\n",
6116                         phba->sli4_hba.hdr_rq->queue_id,
6117                         phba->sli4_hba.dat_rq->queue_id,
6118                         phba->sli4_hba.els_cq->queue_id);
6119         return 0;
6120
6121 out_destroy_fcp_wq:
6122         for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6123                 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
6124         lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6125 out_destroy_mbx_wq:
6126         lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6127 out_destroy_fcp_cq:
6128         for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6129                 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
6130         lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6131 out_destroy_mbx_cq:
6132         lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6133 out_destroy_fp_eq:
6134         for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6135                 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
6136         lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6137 out_error:
6138         return rc;
6139 }
6140
6141 /**
6142  * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6143  * @phba: pointer to lpfc hba data structure.
6144  *
6145  * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6146  * operation.
6147  *
6148  * Return codes
6149  *      0 - successful
6150  *      ENOMEM - No availble memory
6151  *      EIO - The mailbox failed to complete successfully.
6152  **/
6153 void
6154 lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6155 {
6156         int fcp_qidx;
6157
6158         /* Unset mailbox command work queue */
6159         lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6160         /* Unset ELS work queue */
6161         lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6162         /* Unset unsolicited receive queue */
6163         lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6164         /* Unset FCP work queue */
6165         for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6166                 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6167         /* Unset mailbox command complete queue */
6168         lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6169         /* Unset ELS complete queue */
6170         lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6171         /* Unset FCP response complete queue */
6172         for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6173                 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6174         /* Unset fast-path event queue */
6175         for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6176                 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6177         /* Unset slow-path event queue */
6178         lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6179 }
6180
6181 /**
6182  * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6183  * @phba: pointer to lpfc hba data structure.
6184  *
6185  * This routine is invoked to allocate and set up a pool of completion queue
6186  * events. The body of the completion queue event is a completion queue entry
6187  * CQE. For now, this pool is used for the interrupt service routine to queue
6188  * the following HBA completion queue events for the worker thread to process:
6189  *   - Mailbox asynchronous events
6190  *   - Receive queue completion unsolicited events
6191  * Later, this can be used for all the slow-path events.
6192  *
6193  * Return codes
6194  *      0 - successful
6195  *      -ENOMEM - No availble memory
6196  **/
6197 static int
6198 lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6199 {
6200         struct lpfc_cq_event *cq_event;
6201         int i;
6202
6203         for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6204                 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6205                 if (!cq_event)
6206                         goto out_pool_create_fail;
6207                 list_add_tail(&cq_event->list,
6208                               &phba->sli4_hba.sp_cqe_event_pool);
6209         }
6210         return 0;
6211
6212 out_pool_create_fail:
6213         lpfc_sli4_cq_event_pool_destroy(phba);
6214         return -ENOMEM;
6215 }
6216
6217 /**
6218  * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6219  * @phba: pointer to lpfc hba data structure.
6220  *
6221  * This routine is invoked to free the pool of completion queue events at
6222  * driver unload time. Note that, it is the responsibility of the driver
6223  * cleanup routine to free all the outstanding completion-queue events
6224  * allocated from this pool back into the pool before invoking this routine
6225  * to destroy the pool.
6226  **/
6227 static void
6228 lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6229 {
6230         struct lpfc_cq_event *cq_event, *next_cq_event;
6231
6232         list_for_each_entry_safe(cq_event, next_cq_event,
6233                                  &phba->sli4_hba.sp_cqe_event_pool, list) {
6234                 list_del(&cq_event->list);
6235                 kfree(cq_event);
6236         }
6237 }
6238
6239 /**
6240  * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6241  * @phba: pointer to lpfc hba data structure.
6242  *
6243  * This routine is the lock free version of the API invoked to allocate a
6244  * completion-queue event from the free pool.
6245  *
6246  * Return: Pointer to the newly allocated completion-queue event if successful
6247  *         NULL otherwise.
6248  **/
6249 struct lpfc_cq_event *
6250 __lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6251 {
6252         struct lpfc_cq_event *cq_event = NULL;
6253
6254         list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6255                          struct lpfc_cq_event, list);
6256         return cq_event;
6257 }
6258
6259 /**
6260  * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6261  * @phba: pointer to lpfc hba data structure.
6262  *
6263  * This routine is the lock version of the API invoked to allocate a
6264  * completion-queue event from the free pool.
6265  *
6266  * Return: Pointer to the newly allocated completion-queue event if successful
6267  *         NULL otherwise.
6268  **/
6269 struct lpfc_cq_event *
6270 lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6271 {
6272         struct lpfc_cq_event *cq_event;
6273         unsigned long iflags;
6274
6275         spin_lock_irqsave(&phba->hbalock, iflags);
6276         cq_event = __lpfc_sli4_cq_event_alloc(phba);
6277         spin_unlock_irqrestore(&phba->hbalock, iflags);
6278         return cq_event;
6279 }
6280
6281 /**
6282  * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6283  * @phba: pointer to lpfc hba data structure.
6284  * @cq_event: pointer to the completion queue event to be freed.
6285  *
6286  * This routine is the lock free version of the API invoked to release a
6287  * completion-queue event back into the free pool.
6288  **/
6289 void
6290 __lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6291                              struct lpfc_cq_event *cq_event)
6292 {
6293         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6294 }
6295
6296 /**
6297  * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6298  * @phba: pointer to lpfc hba data structure.
6299  * @cq_event: pointer to the completion queue event to be freed.
6300  *
6301  * This routine is the lock version of the API invoked to release a
6302  * completion-queue event back into the free pool.
6303  **/
6304 void
6305 lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6306                            struct lpfc_cq_event *cq_event)
6307 {
6308         unsigned long iflags;
6309         spin_lock_irqsave(&phba->hbalock, iflags);
6310         __lpfc_sli4_cq_event_release(phba, cq_event);
6311         spin_unlock_irqrestore(&phba->hbalock, iflags);
6312 }
6313
6314 /**
6315  * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6316  * @phba: pointer to lpfc hba data structure.
6317  *
6318  * This routine is to free all the pending completion-queue events to the
6319  * back into the free pool for device reset.
6320  **/
6321 static void
6322 lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6323 {
6324         LIST_HEAD(cqelist);
6325         struct lpfc_cq_event *cqe;
6326         unsigned long iflags;
6327
6328         /* Retrieve all the pending WCQEs from pending WCQE lists */
6329         spin_lock_irqsave(&phba->hbalock, iflags);
6330         /* Pending FCP XRI abort events */
6331         list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6332                          &cqelist);
6333         /* Pending ELS XRI abort events */
6334         list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6335                          &cqelist);
6336         /* Pending asynnc events */
6337         list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6338                          &cqelist);
6339         spin_unlock_irqrestore(&phba->hbalock, iflags);
6340
6341         while (!list_empty(&cqelist)) {
6342                 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6343                 lpfc_sli4_cq_event_release(phba, cqe);
6344         }
6345 }
6346
6347 /**
6348  * lpfc_pci_function_reset - Reset pci function.
6349  * @phba: pointer to lpfc hba data structure.
6350  *
6351  * This routine is invoked to request a PCI function reset. It will destroys
6352  * all resources assigned to the PCI function which originates this request.
6353  *
6354  * Return codes
6355  *      0 - successful
6356  *      ENOMEM - No availble memory
6357  *      EIO - The mailbox failed to complete successfully.
6358  **/
6359 int
6360 lpfc_pci_function_reset(struct lpfc_hba *phba)
6361 {
6362         LPFC_MBOXQ_t *mboxq;
6363         uint32_t rc = 0;
6364         uint32_t shdr_status, shdr_add_status;
6365         union lpfc_sli4_cfg_shdr *shdr;
6366
6367         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6368         if (!mboxq) {
6369                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6370                                 "0494 Unable to allocate memory for issuing "
6371                                 "SLI_FUNCTION_RESET mailbox command\n");
6372                 return -ENOMEM;
6373         }
6374
6375         /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
6376         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6377                          LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6378                          LPFC_SLI4_MBX_EMBED);
6379         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6380         shdr = (union lpfc_sli4_cfg_shdr *)
6381                 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6382         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6383         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6384         if (rc != MBX_TIMEOUT)
6385                 mempool_free(mboxq, phba->mbox_mem_pool);
6386         if (shdr_status || shdr_add_status || rc) {
6387                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6388                                 "0495 SLI_FUNCTION_RESET mailbox failed with "
6389                                 "status x%x add_status x%x, mbx status x%x\n",
6390                                 shdr_status, shdr_add_status, rc);
6391                 rc = -ENXIO;
6392         }
6393         return rc;
6394 }
6395
6396 /**
6397  * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6398  * @phba: pointer to lpfc hba data structure.
6399  * @cnt: number of nop mailbox commands to send.
6400  *
6401  * This routine is invoked to send a number @cnt of NOP mailbox command and
6402  * wait for each command to complete.
6403  *
6404  * Return: the number of NOP mailbox command completed.
6405  **/
6406 static int
6407 lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6408 {
6409         LPFC_MBOXQ_t *mboxq;
6410         int length, cmdsent;
6411         uint32_t mbox_tmo;
6412         uint32_t rc = 0;
6413         uint32_t shdr_status, shdr_add_status;
6414         union lpfc_sli4_cfg_shdr *shdr;
6415
6416         if (cnt == 0) {
6417                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6418                                 "2518 Requested to send 0 NOP mailbox cmd\n");
6419                 return cnt;
6420         }
6421
6422         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6423         if (!mboxq) {
6424                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6425                                 "2519 Unable to allocate memory for issuing "
6426                                 "NOP mailbox command\n");
6427                 return 0;
6428         }
6429
6430         /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6431         length = (sizeof(struct lpfc_mbx_nop) -
6432                   sizeof(struct lpfc_sli4_cfg_mhdr));
6433         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6434                          LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6435
6436         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6437         for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6438                 if (!phba->sli4_hba.intr_enable)
6439                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6440                 else
6441                         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6442                 if (rc == MBX_TIMEOUT)
6443                         break;
6444                 /* Check return status */
6445                 shdr = (union lpfc_sli4_cfg_shdr *)
6446                         &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6447                 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6448                 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6449                                          &shdr->response);
6450                 if (shdr_status || shdr_add_status || rc) {
6451                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6452                                         "2520 NOP mailbox command failed "
6453                                         "status x%x add_status x%x mbx "
6454                                         "status x%x\n", shdr_status,
6455                                         shdr_add_status, rc);
6456                         break;
6457                 }
6458         }
6459
6460         if (rc != MBX_TIMEOUT)
6461                 mempool_free(mboxq, phba->mbox_mem_pool);
6462
6463         return cmdsent;
6464 }
6465
6466 /**
6467  * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6468  * @phba: pointer to lpfc hba data structure.
6469  * @fcfi: fcf index.
6470  *
6471  * This routine is invoked to unregister a FCFI from device.
6472  **/
6473 void
6474 lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6475 {
6476         LPFC_MBOXQ_t *mbox;
6477         uint32_t mbox_tmo;
6478         int rc;
6479         unsigned long flags;
6480
6481         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6482
6483         if (!mbox)
6484                 return;
6485
6486         lpfc_unreg_fcfi(mbox, fcfi);
6487
6488         if (!phba->sli4_hba.intr_enable)
6489                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6490         else {
6491                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6492                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6493         }
6494         if (rc != MBX_TIMEOUT)
6495                 mempool_free(mbox, phba->mbox_mem_pool);
6496         if (rc != MBX_SUCCESS)
6497                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6498                                 "2517 Unregister FCFI command failed "
6499                                 "status %d, mbxStatus x%x\n", rc,
6500                                 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6501         else {
6502                 spin_lock_irqsave(&phba->hbalock, flags);
6503                 /* Mark the FCFI is no longer registered */
6504                 phba->fcf.fcf_flag &=
6505                         ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_SCAN_DONE);
6506                 spin_unlock_irqrestore(&phba->hbalock, flags);
6507         }
6508 }
6509
6510 /**
6511  * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6512  * @phba: pointer to lpfc hba data structure.
6513  *
6514  * This routine is invoked to set up the PCI device memory space for device
6515  * with SLI-4 interface spec.
6516  *
6517  * Return codes
6518  *      0 - successful
6519  *      other values - error
6520  **/
6521 static int
6522 lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6523 {
6524         struct pci_dev *pdev;
6525         unsigned long bar0map_len, bar1map_len, bar2map_len;
6526         int error = -ENODEV;
6527
6528         /* Obtain PCI device reference */
6529         if (!phba->pcidev)
6530                 return error;
6531         else
6532                 pdev = phba->pcidev;
6533
6534         /* Set the device DMA mask size */
6535         if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6536          || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6537                 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6538                  || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
6539                         return error;
6540                 }
6541         }
6542
6543         /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6544          * number of bytes required by each mapping. They are actually
6545          * mapping to the PCI BAR regions 0 or 1, 2, and 4 by the SLI4 device.
6546          */
6547         if (pci_resource_start(pdev, 0)) {
6548                 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6549                 bar0map_len = pci_resource_len(pdev, 0);
6550         } else {
6551                 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6552                 bar0map_len = pci_resource_len(pdev, 1);
6553         }
6554         phba->pci_bar1_map = pci_resource_start(pdev, 2);
6555         bar1map_len = pci_resource_len(pdev, 2);
6556
6557         phba->pci_bar2_map = pci_resource_start(pdev, 4);
6558         bar2map_len = pci_resource_len(pdev, 4);
6559
6560         /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6561         phba->sli4_hba.conf_regs_memmap_p =
6562                                 ioremap(phba->pci_bar0_map, bar0map_len);
6563         if (!phba->sli4_hba.conf_regs_memmap_p) {
6564                 dev_printk(KERN_ERR, &pdev->dev,
6565                            "ioremap failed for SLI4 PCI config registers.\n");
6566                 goto out;
6567         }
6568
6569         /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6570         phba->sli4_hba.ctrl_regs_memmap_p =
6571                                 ioremap(phba->pci_bar1_map, bar1map_len);
6572         if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6573                 dev_printk(KERN_ERR, &pdev->dev,
6574                            "ioremap failed for SLI4 HBA control registers.\n");
6575                 goto out_iounmap_conf;
6576         }
6577
6578         /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6579         phba->sli4_hba.drbl_regs_memmap_p =
6580                                 ioremap(phba->pci_bar2_map, bar2map_len);
6581         if (!phba->sli4_hba.drbl_regs_memmap_p) {
6582                 dev_printk(KERN_ERR, &pdev->dev,
6583                            "ioremap failed for SLI4 HBA doorbell registers.\n");
6584                 goto out_iounmap_ctrl;
6585         }
6586
6587         /* Set up BAR0 PCI config space register memory map */
6588         lpfc_sli4_bar0_register_memmap(phba);
6589
6590         /* Set up BAR1 register memory map */
6591         lpfc_sli4_bar1_register_memmap(phba);
6592
6593         /* Set up BAR2 register memory map */
6594         error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6595         if (error)
6596                 goto out_iounmap_all;
6597
6598         return 0;
6599
6600 out_iounmap_all:
6601         iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6602 out_iounmap_ctrl:
6603         iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6604 out_iounmap_conf:
6605         iounmap(phba->sli4_hba.conf_regs_memmap_p);
6606 out:
6607         return error;
6608 }
6609
6610 /**
6611  * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6612  * @phba: pointer to lpfc hba data structure.
6613  *
6614  * This routine is invoked to unset the PCI device memory space for device
6615  * with SLI-4 interface spec.
6616  **/
6617 static void
6618 lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6619 {
6620         struct pci_dev *pdev;
6621
6622         /* Obtain PCI device reference */
6623         if (!phba->pcidev)
6624                 return;
6625         else
6626                 pdev = phba->pcidev;
6627
6628         /* Free coherent DMA memory allocated */
6629
6630         /* Unmap I/O memory space */
6631         iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6632         iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6633         iounmap(phba->sli4_hba.conf_regs_memmap_p);
6634
6635         return;
6636 }
6637
6638 /**
6639  * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6640  * @phba: pointer to lpfc hba data structure.
6641  *
6642  * This routine is invoked to enable the MSI-X interrupt vectors to device
6643  * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6644  * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6645  * invoked, enables either all or nothing, depending on the current
6646  * availability of PCI vector resources. The device driver is responsible
6647  * for calling the individual request_irq() to register each MSI-X vector
6648  * with a interrupt handler, which is done in this function. Note that
6649  * later when device is unloading, the driver should always call free_irq()
6650  * on all MSI-X vectors it has done request_irq() on before calling
6651  * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6652  * will be left with MSI-X enabled and leaks its vectors.
6653  *
6654  * Return codes
6655  *   0 - successful
6656  *   other values - error
6657  **/
6658 static int
6659 lpfc_sli_enable_msix(struct lpfc_hba *phba)
6660 {
6661         int rc, i;
6662         LPFC_MBOXQ_t *pmb;
6663
6664         /* Set up MSI-X multi-message vectors */
6665         for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6666                 phba->msix_entries[i].entry = i;
6667
6668         /* Configure MSI-X capability structure */
6669         rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6670                                 ARRAY_SIZE(phba->msix_entries));
6671         if (rc) {
6672                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6673                                 "0420 PCI enable MSI-X failed (%d)\n", rc);
6674                 goto msi_fail_out;
6675         }
6676         for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6677                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6678                                 "0477 MSI-X entry[%d]: vector=x%x "
6679                                 "message=%d\n", i,
6680                                 phba->msix_entries[i].vector,
6681                                 phba->msix_entries[i].entry);
6682         /*
6683          * Assign MSI-X vectors to interrupt handlers
6684          */
6685
6686         /* vector-0 is associated to slow-path handler */
6687         rc = request_irq(phba->msix_entries[0].vector,
6688                          &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6689                          LPFC_SP_DRIVER_HANDLER_NAME, phba);
6690         if (rc) {
6691                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6692                                 "0421 MSI-X slow-path request_irq failed "
6693                                 "(%d)\n", rc);
6694                 goto msi_fail_out;
6695         }
6696
6697         /* vector-1 is associated to fast-path handler */
6698         rc = request_irq(phba->msix_entries[1].vector,
6699                          &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6700                          LPFC_FP_DRIVER_HANDLER_NAME, phba);
6701
6702         if (rc) {
6703                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6704                                 "0429 MSI-X fast-path request_irq failed "
6705                                 "(%d)\n", rc);
6706                 goto irq_fail_out;
6707         }
6708
6709         /*
6710          * Configure HBA MSI-X attention conditions to messages
6711          */
6712         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6713
6714         if (!pmb) {
6715                 rc = -ENOMEM;
6716                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6717                                 "0474 Unable to allocate memory for issuing "
6718                                 "MBOX_CONFIG_MSI command\n");
6719                 goto mem_fail_out;
6720         }
6721         rc = lpfc_config_msi(phba, pmb);
6722         if (rc)
6723                 goto mbx_fail_out;
6724         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6725         if (rc != MBX_SUCCESS) {
6726                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6727                                 "0351 Config MSI mailbox command failed, "
6728                                 "mbxCmd x%x, mbxStatus x%x\n",
6729                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6730                 goto mbx_fail_out;
6731         }
6732
6733         /* Free memory allocated for mailbox command */
6734         mempool_free(pmb, phba->mbox_mem_pool);
6735         return rc;
6736
6737 mbx_fail_out:
6738         /* Free memory allocated for mailbox command */
6739         mempool_free(pmb, phba->mbox_mem_pool);
6740
6741 mem_fail_out:
6742         /* free the irq already requested */
6743         free_irq(phba->msix_entries[1].vector, phba);
6744
6745 irq_fail_out:
6746         /* free the irq already requested */
6747         free_irq(phba->msix_entries[0].vector, phba);
6748
6749 msi_fail_out:
6750         /* Unconfigure MSI-X capability structure */
6751         pci_disable_msix(phba->pcidev);
6752         return rc;
6753 }
6754
6755 /**
6756  * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6757  * @phba: pointer to lpfc hba data structure.
6758  *
6759  * This routine is invoked to release the MSI-X vectors and then disable the
6760  * MSI-X interrupt mode to device with SLI-3 interface spec.
6761  **/
6762 static void
6763 lpfc_sli_disable_msix(struct lpfc_hba *phba)
6764 {
6765         int i;
6766
6767         /* Free up MSI-X multi-message vectors */
6768         for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6769                 free_irq(phba->msix_entries[i].vector, phba);
6770         /* Disable MSI-X */
6771         pci_disable_msix(phba->pcidev);
6772
6773         return;
6774 }
6775
6776 /**
6777  * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6778  * @phba: pointer to lpfc hba data structure.
6779  *
6780  * This routine is invoked to enable the MSI interrupt mode to device with
6781  * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6782  * enable the MSI vector. The device driver is responsible for calling the
6783  * request_irq() to register MSI vector with a interrupt the handler, which
6784  * is done in this function.
6785  *
6786  * Return codes
6787  *      0 - successful
6788  *      other values - error
6789  */
6790 static int
6791 lpfc_sli_enable_msi(struct lpfc_hba *phba)
6792 {
6793         int rc;
6794
6795         rc = pci_enable_msi(phba->pcidev);
6796         if (!rc)
6797                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6798                                 "0462 PCI enable MSI mode success.\n");
6799         else {
6800                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6801                                 "0471 PCI enable MSI mode failed (%d)\n", rc);
6802                 return rc;
6803         }
6804
6805         rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6806                          IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6807         if (rc) {
6808                 pci_disable_msi(phba->pcidev);
6809                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6810                                 "0478 MSI request_irq failed (%d)\n", rc);
6811         }
6812         return rc;
6813 }
6814
6815 /**
6816  * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6817  * @phba: pointer to lpfc hba data structure.
6818  *
6819  * This routine is invoked to disable the MSI interrupt mode to device with
6820  * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6821  * done request_irq() on before calling pci_disable_msi(). Failure to do so
6822  * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6823  * its vector.
6824  */
6825 static void
6826 lpfc_sli_disable_msi(struct lpfc_hba *phba)
6827 {
6828         free_irq(phba->pcidev->irq, phba);
6829         pci_disable_msi(phba->pcidev);
6830         return;
6831 }
6832
6833 /**
6834  * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6835  * @phba: pointer to lpfc hba data structure.
6836  *
6837  * This routine is invoked to enable device interrupt and associate driver's
6838  * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6839  * spec. Depends on the interrupt mode configured to the driver, the driver
6840  * will try to fallback from the configured interrupt mode to an interrupt
6841  * mode which is supported by the platform, kernel, and device in the order
6842  * of:
6843  * MSI-X -> MSI -> IRQ.
6844  *
6845  * Return codes
6846  *   0 - successful
6847  *   other values - error
6848  **/
6849 static uint32_t
6850 lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6851 {
6852         uint32_t intr_mode = LPFC_INTR_ERROR;
6853         int retval;
6854
6855         if (cfg_mode == 2) {
6856                 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6857                 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6858                 if (!retval) {
6859                         /* Now, try to enable MSI-X interrupt mode */
6860                         retval = lpfc_sli_enable_msix(phba);
6861                         if (!retval) {
6862                                 /* Indicate initialization to MSI-X mode */
6863                                 phba->intr_type = MSIX;
6864                                 intr_mode = 2;
6865                         }
6866                 }
6867         }
6868
6869         /* Fallback to MSI if MSI-X initialization failed */
6870         if (cfg_mode >= 1 && phba->intr_type == NONE) {
6871                 retval = lpfc_sli_enable_msi(phba);
6872                 if (!retval) {
6873                         /* Indicate initialization to MSI mode */
6874                         phba->intr_type = MSI;
6875                         intr_mode = 1;
6876                 }
6877         }
6878
6879         /* Fallback to INTx if both MSI-X/MSI initalization failed */
6880         if (phba->intr_type == NONE) {
6881                 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6882                                      IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6883                 if (!retval) {
6884                         /* Indicate initialization to INTx mode */
6885                         phba->intr_type = INTx;
6886                         intr_mode = 0;
6887                 }
6888         }
6889         return intr_mode;
6890 }
6891
6892 /**
6893  * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6894  * @phba: pointer to lpfc hba data structure.
6895  *
6896  * This routine is invoked to disable device interrupt and disassociate the
6897  * driver's interrupt handler(s) from interrupt vector(s) to device with
6898  * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6899  * release the interrupt vector(s) for the message signaled interrupt.
6900  **/
6901 static void
6902 lpfc_sli_disable_intr(struct lpfc_hba *phba)
6903 {
6904         /* Disable the currently initialized interrupt mode */
6905         if (phba->intr_type == MSIX)
6906                 lpfc_sli_disable_msix(phba);
6907         else if (phba->intr_type == MSI)
6908                 lpfc_sli_disable_msi(phba);
6909         else if (phba->intr_type == INTx)
6910                 free_irq(phba->pcidev->irq, phba);
6911
6912         /* Reset interrupt management states */
6913         phba->intr_type = NONE;
6914         phba->sli.slistat.sli_intr = 0;
6915
6916         return;
6917 }
6918
6919 /**
6920  * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6921  * @phba: pointer to lpfc hba data structure.
6922  *
6923  * This routine is invoked to enable the MSI-X interrupt vectors to device
6924  * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6925  * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6926  * enables either all or nothing, depending on the current availability of
6927  * PCI vector resources. The device driver is responsible for calling the
6928  * individual request_irq() to register each MSI-X vector with a interrupt
6929  * handler, which is done in this function. Note that later when device is
6930  * unloading, the driver should always call free_irq() on all MSI-X vectors
6931  * it has done request_irq() on before calling pci_disable_msix(). Failure
6932  * to do so results in a BUG_ON() and a device will be left with MSI-X
6933  * enabled and leaks its vectors.
6934  *
6935  * Return codes
6936  * 0 - successful
6937  * other values - error
6938  **/
6939 static int
6940 lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6941 {
6942         int rc, index;
6943
6944         /* Set up MSI-X multi-message vectors */
6945         for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6946                 phba->sli4_hba.msix_entries[index].entry = index;
6947
6948         /* Configure MSI-X capability structure */
6949         rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6950                              phba->sli4_hba.cfg_eqn);
6951         if (rc) {
6952                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6953                                 "0484 PCI enable MSI-X failed (%d)\n", rc);
6954                 goto msi_fail_out;
6955         }
6956         /* Log MSI-X vector assignment */
6957         for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6958                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6959                                 "0489 MSI-X entry[%d]: vector=x%x "
6960                                 "message=%d\n", index,
6961                                 phba->sli4_hba.msix_entries[index].vector,
6962                                 phba->sli4_hba.msix_entries[index].entry);
6963         /*
6964          * Assign MSI-X vectors to interrupt handlers
6965          */
6966
6967         /* The first vector must associated to slow-path handler for MQ */
6968         rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6969                          &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6970                          LPFC_SP_DRIVER_HANDLER_NAME, phba);
6971         if (rc) {
6972                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6973                                 "0485 MSI-X slow-path request_irq failed "
6974                                 "(%d)\n", rc);
6975                 goto msi_fail_out;
6976         }
6977
6978         /* The rest of the vector(s) are associated to fast-path handler(s) */
6979         for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6980                 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6981                 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6982                 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6983                                  &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6984                                  LPFC_FP_DRIVER_HANDLER_NAME,
6985                                  &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6986                 if (rc) {
6987                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6988                                         "0486 MSI-X fast-path (%d) "
6989                                         "request_irq failed (%d)\n", index, rc);
6990                         goto cfg_fail_out;
6991                 }
6992         }
6993
6994         return rc;
6995
6996 cfg_fail_out:
6997         /* free the irq already requested */
6998         for (--index; index >= 1; index--)
6999                 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7000                          &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7001
7002         /* free the irq already requested */
7003         free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7004
7005 msi_fail_out:
7006         /* Unconfigure MSI-X capability structure */
7007         pci_disable_msix(phba->pcidev);
7008         return rc;
7009 }
7010
7011 /**
7012  * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7013  * @phba: pointer to lpfc hba data structure.
7014  *
7015  * This routine is invoked to release the MSI-X vectors and then disable the
7016  * MSI-X interrupt mode to device with SLI-4 interface spec.
7017  **/
7018 static void
7019 lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7020 {
7021         int index;
7022
7023         /* Free up MSI-X multi-message vectors */
7024         free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7025
7026         for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
7027                 free_irq(phba->sli4_hba.msix_entries[index].vector,
7028                          &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7029         /* Disable MSI-X */
7030         pci_disable_msix(phba->pcidev);
7031
7032         return;
7033 }
7034
7035 /**
7036  * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
7037  * @phba: pointer to lpfc hba data structure.
7038  *
7039  * This routine is invoked to enable the MSI interrupt mode to device with
7040  * SLI-4 interface spec. The kernel function pci_enable_msi() is called
7041  * to enable the MSI vector. The device driver is responsible for calling
7042  * the request_irq() to register MSI vector with a interrupt the handler,
7043  * which is done in this function.
7044  *
7045  * Return codes
7046  *      0 - successful
7047  *      other values - error
7048  **/
7049 static int
7050 lpfc_sli4_enable_msi(struct lpfc_hba *phba)
7051 {
7052         int rc, index;
7053
7054         rc = pci_enable_msi(phba->pcidev);
7055         if (!rc)
7056                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7057                                 "0487 PCI enable MSI mode success.\n");
7058         else {
7059                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7060                                 "0488 PCI enable MSI mode failed (%d)\n", rc);
7061                 return rc;
7062         }
7063
7064         rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7065                          IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7066         if (rc) {
7067                 pci_disable_msi(phba->pcidev);
7068                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7069                                 "0490 MSI request_irq failed (%d)\n", rc);
7070         }
7071
7072         for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
7073                 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7074                 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7075         }
7076
7077         return rc;
7078 }
7079
7080 /**
7081  * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
7082  * @phba: pointer to lpfc hba data structure.
7083  *
7084  * This routine is invoked to disable the MSI interrupt mode to device with
7085  * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
7086  * done request_irq() on before calling pci_disable_msi(). Failure to do so
7087  * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7088  * its vector.
7089  **/
7090 static void
7091 lpfc_sli4_disable_msi(struct lpfc_hba *phba)
7092 {
7093         free_irq(phba->pcidev->irq, phba);
7094         pci_disable_msi(phba->pcidev);
7095         return;
7096 }
7097
7098 /**
7099  * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
7100  * @phba: pointer to lpfc hba data structure.
7101  *
7102  * This routine is invoked to enable device interrupt and associate driver's
7103  * interrupt handler(s) to interrupt vector(s) to device with SLI-4
7104  * interface spec. Depends on the interrupt mode configured to the driver,
7105  * the driver will try to fallback from the configured interrupt mode to an
7106  * interrupt mode which is supported by the platform, kernel, and device in
7107  * the order of:
7108  * MSI-X -> MSI -> IRQ.
7109  *
7110  * Return codes
7111  *      0 - successful
7112  *      other values - error
7113  **/
7114 static uint32_t
7115 lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7116 {
7117         uint32_t intr_mode = LPFC_INTR_ERROR;
7118         int retval, index;
7119
7120         if (cfg_mode == 2) {
7121                 /* Preparation before conf_msi mbox cmd */
7122                 retval = 0;
7123                 if (!retval) {
7124                         /* Now, try to enable MSI-X interrupt mode */
7125                         retval = lpfc_sli4_enable_msix(phba);
7126                         if (!retval) {
7127                                 /* Indicate initialization to MSI-X mode */
7128                                 phba->intr_type = MSIX;
7129                                 intr_mode = 2;
7130                         }
7131                 }
7132         }
7133
7134         /* Fallback to MSI if MSI-X initialization failed */
7135         if (cfg_mode >= 1 && phba->intr_type == NONE) {
7136                 retval = lpfc_sli4_enable_msi(phba);
7137                 if (!retval) {
7138                         /* Indicate initialization to MSI mode */
7139                         phba->intr_type = MSI;
7140                         intr_mode = 1;
7141                 }
7142         }
7143
7144         /* Fallback to INTx if both MSI-X/MSI initalization failed */
7145         if (phba->intr_type == NONE) {
7146                 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7147                                      IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7148                 if (!retval) {
7149                         /* Indicate initialization to INTx mode */
7150                         phba->intr_type = INTx;
7151                         intr_mode = 0;
7152                         for (index = 0; index < phba->cfg_fcp_eq_count;
7153                              index++) {
7154                                 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7155                                 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7156                         }
7157                 }
7158         }
7159         return intr_mode;
7160 }
7161
7162 /**
7163  * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7164  * @phba: pointer to lpfc hba data structure.
7165  *
7166  * This routine is invoked to disable device interrupt and disassociate
7167  * the driver's interrupt handler(s) from interrupt vector(s) to device
7168  * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7169  * will release the interrupt vector(s) for the message signaled interrupt.
7170  **/
7171 static void
7172 lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7173 {
7174         /* Disable the currently initialized interrupt mode */
7175         if (phba->intr_type == MSIX)
7176                 lpfc_sli4_disable_msix(phba);
7177         else if (phba->intr_type == MSI)
7178                 lpfc_sli4_disable_msi(phba);
7179         else if (phba->intr_type == INTx)
7180                 free_irq(phba->pcidev->irq, phba);
7181
7182         /* Reset interrupt management states */
7183         phba->intr_type = NONE;
7184         phba->sli.slistat.sli_intr = 0;
7185
7186         return;
7187 }
7188
7189 /**
7190  * lpfc_unset_hba - Unset SLI3 hba device initialization
7191  * @phba: pointer to lpfc hba data structure.
7192  *
7193  * This routine is invoked to unset the HBA device initialization steps to
7194  * a device with SLI-3 interface spec.
7195  **/
7196 static void
7197 lpfc_unset_hba(struct lpfc_hba *phba)
7198 {
7199         struct lpfc_vport *vport = phba->pport;
7200         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
7201
7202         spin_lock_irq(shost->host_lock);
7203         vport->load_flag |= FC_UNLOADING;
7204         spin_unlock_irq(shost->host_lock);
7205
7206         lpfc_stop_hba_timers(phba);
7207
7208         phba->pport->work_port_events = 0;
7209
7210         lpfc_sli_hba_down(phba);
7211
7212         lpfc_sli_brdrestart(phba);
7213
7214         lpfc_sli_disable_intr(phba);
7215
7216         return;
7217 }
7218
7219 /**
7220  * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7221  * @phba: pointer to lpfc hba data structure.
7222  *
7223  * This routine is invoked to unset the HBA device initialization steps to
7224  * a device with SLI-4 interface spec.
7225  **/
7226 static void
7227 lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7228 {
7229         struct lpfc_vport *vport = phba->pport;
7230         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
7231
7232         spin_lock_irq(shost->host_lock);
7233         vport->load_flag |= FC_UNLOADING;
7234         spin_unlock_irq(shost->host_lock);
7235
7236         phba->pport->work_port_events = 0;
7237
7238         lpfc_sli4_hba_down(phba);
7239
7240         lpfc_sli4_disable_intr(phba);
7241
7242         return;
7243 }
7244
7245 /**
7246  * lpfc_sli4_hba_unset - Unset the fcoe hba
7247  * @phba: Pointer to HBA context object.
7248  *
7249  * This function is called in the SLI4 code path to reset the HBA's FCoE
7250  * function. The caller is not required to hold any lock. This routine
7251  * issues PCI function reset mailbox command to reset the FCoE function.
7252  * At the end of the function, it calls lpfc_hba_down_post function to
7253  * free any pending commands.
7254  **/
7255 static void
7256 lpfc_sli4_hba_unset(struct lpfc_hba *phba)
7257 {
7258         int wait_cnt = 0;
7259         LPFC_MBOXQ_t *mboxq;
7260
7261         lpfc_stop_hba_timers(phba);
7262         phba->sli4_hba.intr_enable = 0;
7263
7264         /*
7265          * Gracefully wait out the potential current outstanding asynchronous
7266          * mailbox command.
7267          */
7268
7269         /* First, block any pending async mailbox command from posted */
7270         spin_lock_irq(&phba->hbalock);
7271         phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7272         spin_unlock_irq(&phba->hbalock);
7273         /* Now, trying to wait it out if we can */
7274         while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7275                 msleep(10);
7276                 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7277                         break;
7278         }
7279         /* Forcefully release the outstanding mailbox command if timed out */
7280         if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7281                 spin_lock_irq(&phba->hbalock);
7282                 mboxq = phba->sli.mbox_active;
7283                 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7284                 __lpfc_mbox_cmpl_put(phba, mboxq);
7285                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7286                 phba->sli.mbox_active = NULL;
7287                 spin_unlock_irq(&phba->hbalock);
7288         }
7289
7290         /* Tear down the queues in the HBA */
7291         lpfc_sli4_queue_unset(phba);
7292
7293         /* Disable PCI subsystem interrupt */
7294         lpfc_sli4_disable_intr(phba);
7295
7296         /* Stop kthread signal shall trigger work_done one more time */
7297         kthread_stop(phba->worker_thread);
7298
7299         /* Stop the SLI4 device port */
7300         phba->pport->work_port_events = 0;
7301 }
7302
7303  /**
7304  * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
7305  * @phba: Pointer to HBA context object.
7306  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7307  *
7308  * This function is called in the SLI4 code path to read the port's
7309  * sli4 capabilities.
7310  *
7311  * This function may be be called from any context that can block-wait
7312  * for the completion.  The expectation is that this routine is called
7313  * typically from probe_one or from the online routine.
7314  **/
7315 int
7316 lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7317 {
7318         int rc;
7319         struct lpfc_mqe *mqe;
7320         struct lpfc_pc_sli4_params *sli4_params;
7321         uint32_t mbox_tmo;
7322
7323         rc = 0;
7324         mqe = &mboxq->u.mqe;
7325
7326         /* Read the port's SLI4 Parameters port capabilities */
7327         lpfc_sli4_params(mboxq);
7328         if (!phba->sli4_hba.intr_enable)
7329                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7330         else {
7331                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
7332                 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7333         }
7334
7335         if (unlikely(rc))
7336                 return 1;
7337
7338         sli4_params = &phba->sli4_hba.pc_sli4_params;
7339         sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
7340         sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
7341         sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
7342         sli4_params->featurelevel_1 = bf_get(featurelevel_1,
7343                                              &mqe->un.sli4_params);
7344         sli4_params->featurelevel_2 = bf_get(featurelevel_2,
7345                                              &mqe->un.sli4_params);
7346         sli4_params->proto_types = mqe->un.sli4_params.word3;
7347         sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
7348         sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
7349         sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
7350         sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
7351         sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
7352         sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
7353         sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
7354         sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
7355         sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
7356         sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
7357         sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
7358         sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
7359         sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
7360         sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
7361         sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
7362         sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
7363         sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
7364         sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
7365         sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
7366         sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
7367         return rc;
7368 }
7369
7370 /**
7371  * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
7372  * @pdev: pointer to PCI device
7373  * @pid: pointer to PCI device identifier
7374  *
7375  * This routine is to be called to attach a device with SLI-3 interface spec
7376  * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7377  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7378  * information of the device and driver to see if the driver state that it can
7379  * support this kind of device. If the match is successful, the driver core
7380  * invokes this routine. If this routine determines it can claim the HBA, it
7381  * does all the initialization that it needs to do to handle the HBA properly.
7382  *
7383  * Return code
7384  *      0 - driver can claim the device
7385  *      negative value - driver can not claim the device
7386  **/
7387 static int __devinit
7388 lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7389 {
7390         struct lpfc_hba   *phba;
7391         struct lpfc_vport *vport = NULL;
7392         struct Scsi_Host  *shost = NULL;
7393         int error;
7394         uint32_t cfg_mode, intr_mode;
7395
7396         /* Allocate memory for HBA structure */
7397         phba = lpfc_hba_alloc(pdev);
7398         if (!phba)
7399                 return -ENOMEM;
7400
7401         /* Perform generic PCI device enabling operation */
7402         error = lpfc_enable_pci_dev(phba);
7403         if (error) {
7404                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7405                                 "1401 Failed to enable pci device.\n");
7406                 goto out_free_phba;
7407         }
7408
7409         /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7410         error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7411         if (error)
7412                 goto out_disable_pci_dev;
7413
7414         /* Set up SLI-3 specific device PCI memory space */
7415         error = lpfc_sli_pci_mem_setup(phba);
7416         if (error) {
7417                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7418                                 "1402 Failed to set up pci memory space.\n");
7419                 goto out_disable_pci_dev;
7420         }
7421
7422         /* Set up phase-1 common device driver resources */
7423         error = lpfc_setup_driver_resource_phase1(phba);
7424         if (error) {
7425                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7426                                 "1403 Failed to set up driver resource.\n");
7427                 goto out_unset_pci_mem_s3;
7428         }
7429
7430         /* Set up SLI-3 specific device driver resources */
7431         error = lpfc_sli_driver_resource_setup(phba);
7432         if (error) {
7433                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7434                                 "1404 Failed to set up driver resource.\n");
7435                 goto out_unset_pci_mem_s3;
7436         }
7437
7438         /* Initialize and populate the iocb list per host */
7439         error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7440         if (error) {
7441                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7442                                 "1405 Failed to initialize iocb list.\n");
7443                 goto out_unset_driver_resource_s3;
7444         }
7445
7446         /* Set up common device driver resources */
7447         error = lpfc_setup_driver_resource_phase2(phba);
7448         if (error) {
7449                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7450                                 "1406 Failed to set up driver resource.\n");
7451                 goto out_free_iocb_list;
7452         }
7453
7454         /* Create SCSI host to the physical port */
7455         error = lpfc_create_shost(phba);
7456         if (error) {
7457                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7458                                 "1407 Failed to create scsi host.\n");
7459                 goto out_unset_driver_resource;
7460         }
7461
7462         /* Configure sysfs attributes */
7463         vport = phba->pport;
7464         error = lpfc_alloc_sysfs_attr(vport);
7465         if (error) {
7466                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7467                                 "1476 Failed to allocate sysfs attr\n");
7468                 goto out_destroy_shost;
7469         }
7470
7471         shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
7472         /* Now, trying to enable interrupt and bring up the device */
7473         cfg_mode = phba->cfg_use_msi;
7474         while (true) {
7475                 /* Put device to a known state before enabling interrupt */
7476                 lpfc_stop_port(phba);
7477                 /* Configure and enable interrupt */
7478                 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
7479                 if (intr_mode == LPFC_INTR_ERROR) {
7480                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7481                                         "0431 Failed to enable interrupt.\n");
7482                         error = -ENODEV;
7483                         goto out_free_sysfs_attr;
7484                 }
7485                 /* SLI-3 HBA setup */
7486                 if (lpfc_sli_hba_setup(phba)) {
7487                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7488                                         "1477 Failed to set up hba\n");
7489                         error = -ENODEV;
7490                         goto out_remove_device;
7491                 }
7492
7493                 /* Wait 50ms for the interrupts of previous mailbox commands */
7494                 msleep(50);
7495                 /* Check active interrupts on message signaled interrupts */
7496                 if (intr_mode == 0 ||
7497                     phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
7498                         /* Log the current active interrupt mode */
7499                         phba->intr_mode = intr_mode;
7500                         lpfc_log_intr_mode(phba, intr_mode);
7501                         break;
7502                 } else {
7503                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7504                                         "0447 Configure interrupt mode (%d) "
7505                                         "failed active interrupt test.\n",
7506                                         intr_mode);
7507                         /* Disable the current interrupt mode */
7508                         lpfc_sli_disable_intr(phba);
7509                         /* Try next level of interrupt mode */
7510                         cfg_mode = --intr_mode;
7511                 }
7512         }
7513
7514         /* Perform post initialization setup */
7515         lpfc_post_init_setup(phba);
7516
7517         /* Check if there are static vports to be created. */
7518         lpfc_create_static_vport(phba);
7519
7520         return 0;
7521
7522 out_remove_device:
7523         lpfc_unset_hba(phba);
7524 out_free_sysfs_attr:
7525         lpfc_free_sysfs_attr(vport);
7526 out_destroy_shost:
7527         lpfc_destroy_shost(phba);
7528 out_unset_driver_resource:
7529         lpfc_unset_driver_resource_phase2(phba);
7530 out_free_iocb_list:
7531         lpfc_free_iocb_list(phba);
7532 out_unset_driver_resource_s3:
7533         lpfc_sli_driver_resource_unset(phba);
7534 out_unset_pci_mem_s3:
7535         lpfc_sli_pci_mem_unset(phba);
7536 out_disable_pci_dev:
7537         lpfc_disable_pci_dev(phba);
7538         if (shost)
7539                 scsi_host_put(shost);
7540 out_free_phba:
7541         lpfc_hba_free(phba);
7542         return error;
7543 }
7544
7545 /**
7546  * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
7547  * @pdev: pointer to PCI device
7548  *
7549  * This routine is to be called to disattach a device with SLI-3 interface
7550  * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7551  * removed from PCI bus, it performs all the necessary cleanup for the HBA
7552  * device to be removed from the PCI subsystem properly.
7553  **/
7554 static void __devexit
7555 lpfc_pci_remove_one_s3(struct pci_dev *pdev)
7556 {
7557         struct Scsi_Host  *shost = pci_get_drvdata(pdev);
7558         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7559         struct lpfc_vport **vports;
7560         struct lpfc_hba   *phba = vport->phba;
7561         int i;
7562         int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7563
7564         spin_lock_irq(&phba->hbalock);
7565         vport->load_flag |= FC_UNLOADING;
7566         spin_unlock_irq(&phba->hbalock);
7567
7568         lpfc_free_sysfs_attr(vport);
7569
7570         /* Release all the vports against this physical port */
7571         vports = lpfc_create_vport_work_array(phba);
7572         if (vports != NULL)
7573                 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
7574                         fc_vport_terminate(vports[i]->fc_vport);
7575         lpfc_destroy_vport_work_array(phba, vports);
7576
7577         /* Remove FC host and then SCSI host with the physical port */
7578         fc_remove_host(shost);
7579         scsi_remove_host(shost);
7580         lpfc_cleanup(vport);
7581
7582         /*
7583          * Bring down the SLI Layer. This step disable all interrupts,
7584          * clears the rings, discards all mailbox commands, and resets
7585          * the HBA.
7586          */
7587
7588         /* HBA interrupt will be diabled after this call */
7589         lpfc_sli_hba_down(phba);
7590         /* Stop kthread signal shall trigger work_done one more time */
7591         kthread_stop(phba->worker_thread);
7592         /* Final cleanup of txcmplq and reset the HBA */
7593         lpfc_sli_brdrestart(phba);
7594
7595         lpfc_stop_hba_timers(phba);
7596         spin_lock_irq(&phba->hbalock);
7597         list_del_init(&vport->listentry);
7598         spin_unlock_irq(&phba->hbalock);
7599
7600         lpfc_debugfs_terminate(vport);
7601
7602         /* Disable interrupt */
7603         lpfc_sli_disable_intr(phba);
7604
7605         pci_set_drvdata(pdev, NULL);
7606         scsi_host_put(shost);
7607
7608         /*
7609          * Call scsi_free before mem_free since scsi bufs are released to their
7610          * corresponding pools here.
7611          */
7612         lpfc_scsi_free(phba);
7613         lpfc_mem_free_all(phba);
7614
7615         dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7616                           phba->hbqslimp.virt, phba->hbqslimp.phys);
7617
7618         /* Free resources associated with SLI2 interface */
7619         dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7620                           phba->slim2p.virt, phba->slim2p.phys);
7621
7622         /* unmap adapter SLIM and Control Registers */
7623         iounmap(phba->ctrl_regs_memmap_p);
7624         iounmap(phba->slim_memmap_p);
7625
7626         lpfc_hba_free(phba);
7627
7628         pci_release_selected_regions(pdev, bars);
7629         pci_disable_device(pdev);
7630 }
7631
7632 /**
7633  * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
7634  * @pdev: pointer to PCI device
7635  * @msg: power management message
7636  *
7637  * This routine is to be called from the kernel's PCI subsystem to support
7638  * system Power Management (PM) to device with SLI-3 interface spec. When
7639  * PM invokes this method, it quiesces the device by stopping the driver's
7640  * worker thread for the device, turning off device's interrupt and DMA,
7641  * and bring the device offline. Note that as the driver implements the
7642  * minimum PM requirements to a power-aware driver's PM support for the
7643  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7644  * to the suspend() method call will be treated as SUSPEND and the driver will
7645  * fully reinitialize its device during resume() method call, the driver will
7646  * set device to PCI_D3hot state in PCI config space instead of setting it
7647  * according to the @msg provided by the PM.
7648  *
7649  * Return code
7650  *      0 - driver suspended the device
7651  *      Error otherwise
7652  **/
7653 static int
7654 lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
7655 {
7656         struct Scsi_Host *shost = pci_get_drvdata(pdev);
7657         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7658
7659         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7660                         "0473 PCI device Power Management suspend.\n");
7661
7662         /* Bring down the device */
7663         lpfc_offline_prep(phba);
7664         lpfc_offline(phba);
7665         kthread_stop(phba->worker_thread);
7666
7667         /* Disable interrupt from device */
7668         lpfc_sli_disable_intr(phba);
7669
7670         /* Save device state to PCI config space */
7671         pci_save_state(pdev);
7672         pci_set_power_state(pdev, PCI_D3hot);
7673
7674         return 0;
7675 }
7676
7677 /**
7678  * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
7679  * @pdev: pointer to PCI device
7680  *
7681  * This routine is to be called from the kernel's PCI subsystem to support
7682  * system Power Management (PM) to device with SLI-3 interface spec. When PM
7683  * invokes this method, it restores the device's PCI config space state and
7684  * fully reinitializes the device and brings it online. Note that as the
7685  * driver implements the minimum PM requirements to a power-aware driver's
7686  * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7687  * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7688  * driver will fully reinitialize its device during resume() method call,
7689  * the device will be set to PCI_D0 directly in PCI config space before
7690  * restoring the state.
7691  *
7692  * Return code
7693  *      0 - driver suspended the device
7694  *      Error otherwise
7695  **/
7696 static int
7697 lpfc_pci_resume_one_s3(struct pci_dev *pdev)
7698 {
7699         struct Scsi_Host *shost = pci_get_drvdata(pdev);
7700         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7701         uint32_t intr_mode;
7702         int error;
7703
7704         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7705                         "0452 PCI device Power Management resume.\n");
7706
7707         /* Restore device state from PCI config space */
7708         pci_set_power_state(pdev, PCI_D0);
7709         pci_restore_state(pdev);
7710
7711         /*
7712          * As the new kernel behavior of pci_restore_state() API call clears
7713          * device saved_state flag, need to save the restored state again.
7714          */
7715         pci_save_state(pdev);
7716
7717         if (pdev->is_busmaster)
7718                 pci_set_master(pdev);
7719
7720         /* Startup the kernel thread for this host adapter. */
7721         phba->worker_thread = kthread_run(lpfc_do_work, phba,
7722                                         "lpfc_worker_%d", phba->brd_no);
7723         if (IS_ERR(phba->worker_thread)) {
7724                 error = PTR_ERR(phba->worker_thread);
7725                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7726                                 "0434 PM resume failed to start worker "
7727                                 "thread: error=x%x.\n", error);
7728                 return error;
7729         }
7730
7731         /* Configure and enable interrupt */
7732         intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
7733         if (intr_mode == LPFC_INTR_ERROR) {
7734                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7735                                 "0430 PM resume Failed to enable interrupt\n");
7736                 return -EIO;
7737         } else
7738                 phba->intr_mode = intr_mode;
7739
7740         /* Restart HBA and bring it online */
7741         lpfc_sli_brdrestart(phba);
7742         lpfc_online(phba);
7743
7744         /* Log the current active interrupt mode */
7745         lpfc_log_intr_mode(phba, phba->intr_mode);
7746
7747         return 0;
7748 }
7749
7750 /**
7751  * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7752  * @phba: pointer to lpfc hba data structure.
7753  *
7754  * This routine is called to prepare the SLI3 device for PCI slot recover. It
7755  * aborts and stops all the on-going I/Os on the pci device.
7756  **/
7757 static void
7758 lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7759 {
7760         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7761                         "2723 PCI channel I/O abort preparing for recovery\n");
7762         /* Prepare for bringing HBA offline */
7763         lpfc_offline_prep(phba);
7764         /* Clear sli active flag to prevent sysfs access to HBA */
7765         spin_lock_irq(&phba->hbalock);
7766         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
7767         spin_unlock_irq(&phba->hbalock);
7768         /* Stop and flush all I/Os and bring HBA offline */
7769         lpfc_offline(phba);
7770 }
7771
7772 /**
7773  * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7774  * @phba: pointer to lpfc hba data structure.
7775  *
7776  * This routine is called to prepare the SLI3 device for PCI slot reset. It
7777  * disables the device interrupt and pci device, and aborts the internal FCP
7778  * pending I/Os.
7779  **/
7780 static void
7781 lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7782 {
7783         struct lpfc_sli *psli = &phba->sli;
7784         struct lpfc_sli_ring  *pring;
7785
7786         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7787                         "2710 PCI channel disable preparing for reset\n");
7788         /* Disable interrupt and pci device */
7789         lpfc_sli_disable_intr(phba);
7790         pci_disable_device(phba->pcidev);
7791         /*
7792          * There may be I/Os dropped by the firmware.
7793          * Error iocb (I/O) on txcmplq and let the SCSI layer
7794          * retry it after re-establishing link.
7795          */
7796         pring = &psli->ring[psli->fcp_ring];
7797         lpfc_sli_abort_iocb_ring(phba, pring);
7798 }
7799
7800 /**
7801  * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7802  * @phba: pointer to lpfc hba data structure.
7803  *
7804  * This routine is called to prepare the SLI3 device for PCI slot permanently
7805  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7806  * pending I/Os.
7807  **/
7808 static void
7809 lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7810 {
7811         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7812                         "2711 PCI channel permanent disable for failure\n");
7813         /* Clean up all driver's outstanding SCSI I/Os */
7814         lpfc_sli_flush_fcp_rings(phba);
7815 }
7816
7817 /**
7818  * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
7819  * @pdev: pointer to PCI device.
7820  * @state: the current PCI connection state.
7821  *
7822  * This routine is called from the PCI subsystem for I/O error handling to
7823  * device with SLI-3 interface spec. This function is called by the PCI
7824  * subsystem after a PCI bus error affecting this device has been detected.
7825  * When this function is invoked, it will need to stop all the I/Os and
7826  * interrupt(s) to the device. Once that is done, it will return
7827  * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7828  * as desired.
7829  *
7830  * Return codes
7831  *      PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
7832  *      PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7833  *      PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7834  **/
7835 static pci_ers_result_t
7836 lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
7837 {
7838         struct Scsi_Host *shost = pci_get_drvdata(pdev);
7839         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7840
7841         /* Block all SCSI devices' I/Os on the host */
7842         lpfc_scsi_dev_block(phba);
7843
7844         switch (state) {
7845         case pci_channel_io_normal:
7846                 /* Non-fatal error, prepare for recovery */
7847                 lpfc_sli_prep_dev_for_recover(phba);
7848                 return PCI_ERS_RESULT_CAN_RECOVER;
7849         case pci_channel_io_frozen:
7850                 /* Fatal error, prepare for slot reset */
7851                 lpfc_sli_prep_dev_for_reset(phba);
7852                 return PCI_ERS_RESULT_NEED_RESET;
7853         case pci_channel_io_perm_failure:
7854                 /* Permanent failure, prepare for device down */
7855                 lpfc_prep_dev_for_perm_failure(phba);
7856                 return PCI_ERS_RESULT_DISCONNECT;
7857         default:
7858                 /* Unknown state, prepare and request slot reset */
7859                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7860                                 "0472 Unknown PCI error state: x%x\n", state);
7861                 lpfc_sli_prep_dev_for_reset(phba);
7862                 return PCI_ERS_RESULT_NEED_RESET;
7863         }
7864 }
7865
7866 /**
7867  * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
7868  * @pdev: pointer to PCI device.
7869  *
7870  * This routine is called from the PCI subsystem for error handling to
7871  * device with SLI-3 interface spec. This is called after PCI bus has been
7872  * reset to restart the PCI card from scratch, as if from a cold-boot.
7873  * During the PCI subsystem error recovery, after driver returns
7874  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7875  * recovery and then call this routine before calling the .resume method
7876  * to recover the device. This function will initialize the HBA device,
7877  * enable the interrupt, but it will just put the HBA to offline state
7878  * without passing any I/O traffic.
7879  *
7880  * Return codes
7881  *      PCI_ERS_RESULT_RECOVERED - the device has been recovered
7882  *      PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7883  */
7884 static pci_ers_result_t
7885 lpfc_io_slot_reset_s3(struct pci_dev *pdev)
7886 {
7887         struct Scsi_Host *shost = pci_get_drvdata(pdev);
7888         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7889         struct lpfc_sli *psli = &phba->sli;
7890         uint32_t intr_mode;
7891
7892         dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
7893         if (pci_enable_device_mem(pdev)) {
7894                 printk(KERN_ERR "lpfc: Cannot re-enable "
7895                         "PCI device after reset.\n");
7896                 return PCI_ERS_RESULT_DISCONNECT;
7897         }
7898
7899         pci_restore_state(pdev);
7900
7901         /*
7902          * As the new kernel behavior of pci_restore_state() API call clears
7903          * device saved_state flag, need to save the restored state again.
7904          */
7905         pci_save_state(pdev);
7906
7907         if (pdev->is_busmaster)
7908                 pci_set_master(pdev);
7909
7910         spin_lock_irq(&phba->hbalock);
7911         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7912         spin_unlock_irq(&phba->hbalock);
7913
7914         /* Configure and enable interrupt */
7915         intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
7916         if (intr_mode == LPFC_INTR_ERROR) {
7917                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7918                                 "0427 Cannot re-enable interrupt after "
7919                                 "slot reset.\n");
7920                 return PCI_ERS_RESULT_DISCONNECT;
7921         } else
7922                 phba->intr_mode = intr_mode;
7923
7924         /* Take device offline; this will perform cleanup */
7925         lpfc_offline(phba);
7926         lpfc_sli_brdrestart(phba);
7927
7928         /* Log the current active interrupt mode */
7929         lpfc_log_intr_mode(phba, phba->intr_mode);
7930
7931         return PCI_ERS_RESULT_RECOVERED;
7932 }
7933
7934 /**
7935  * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
7936  * @pdev: pointer to PCI device
7937  *
7938  * This routine is called from the PCI subsystem for error handling to device
7939  * with SLI-3 interface spec. It is called when kernel error recovery tells
7940  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7941  * error recovery. After this call, traffic can start to flow from this device
7942  * again.
7943  */
7944 static void
7945 lpfc_io_resume_s3(struct pci_dev *pdev)
7946 {
7947         struct Scsi_Host *shost = pci_get_drvdata(pdev);
7948         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7949
7950         /* Bring the device online */
7951         lpfc_online(phba);
7952
7953         /* Clean up Advanced Error Reporting (AER) if needed */
7954         if (phba->hba_flag & HBA_AER_ENABLED)
7955                 pci_cleanup_aer_uncorrect_error_status(pdev);
7956 }
7957
7958 /**
7959  * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7960  * @phba: pointer to lpfc hba data structure.
7961  *
7962  * returns the number of ELS/CT IOCBs to reserve
7963  **/
7964 int
7965 lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7966 {
7967         int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7968
7969         if (phba->sli_rev == LPFC_SLI_REV4) {
7970                 if (max_xri <= 100)
7971                         return 10;
7972                 else if (max_xri <= 256)
7973                         return 25;
7974                 else if (max_xri <= 512)
7975                         return 50;
7976                 else if (max_xri <= 1024)
7977                         return 100;
7978                 else
7979                         return 150;
7980         } else
7981                 return 0;
7982 }
7983
7984 /**
7985  * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
7986  * @pdev: pointer to PCI device
7987  * @pid: pointer to PCI device identifier
7988  *
7989  * This routine is called from the kernel's PCI subsystem to device with
7990  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7991  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7992  * information of the device and driver to see if the driver state that it
7993  * can support this kind of device. If the match is successful, the driver
7994  * core invokes this routine. If this routine determines it can claim the HBA,
7995  * it does all the initialization that it needs to do to handle the HBA
7996  * properly.
7997  *
7998  * Return code
7999  *      0 - driver can claim the device
8000  *      negative value - driver can not claim the device
8001  **/
8002 static int __devinit
8003 lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
8004 {
8005         struct lpfc_hba   *phba;
8006         struct lpfc_vport *vport = NULL;
8007         struct Scsi_Host  *shost = NULL;
8008         int error;
8009         uint32_t cfg_mode, intr_mode;
8010         int mcnt;
8011
8012         /* Allocate memory for HBA structure */
8013         phba = lpfc_hba_alloc(pdev);
8014         if (!phba)
8015                 return -ENOMEM;
8016
8017         /* Perform generic PCI device enabling operation */
8018         error = lpfc_enable_pci_dev(phba);
8019         if (error) {
8020                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8021                                 "1409 Failed to enable pci device.\n");
8022                 goto out_free_phba;
8023         }
8024
8025         /* Set up SLI API function jump table for PCI-device group-1 HBAs */
8026         error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
8027         if (error)
8028                 goto out_disable_pci_dev;
8029
8030         /* Set up SLI-4 specific device PCI memory space */
8031         error = lpfc_sli4_pci_mem_setup(phba);
8032         if (error) {
8033                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8034                                 "1410 Failed to set up pci memory space.\n");
8035                 goto out_disable_pci_dev;
8036         }
8037
8038         /* Set up phase-1 common device driver resources */
8039         error = lpfc_setup_driver_resource_phase1(phba);
8040         if (error) {
8041                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8042                                 "1411 Failed to set up driver resource.\n");
8043                 goto out_unset_pci_mem_s4;
8044         }
8045
8046         /* Set up SLI-4 Specific device driver resources */
8047         error = lpfc_sli4_driver_resource_setup(phba);
8048         if (error) {
8049                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8050                                 "1412 Failed to set up driver resource.\n");
8051                 goto out_unset_pci_mem_s4;
8052         }
8053
8054         /* Initialize and populate the iocb list per host */
8055         error = lpfc_init_iocb_list(phba,
8056                         phba->sli4_hba.max_cfg_param.max_xri);
8057         if (error) {
8058                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8059                                 "1413 Failed to initialize iocb list.\n");
8060                 goto out_unset_driver_resource_s4;
8061         }
8062
8063         /* Set up common device driver resources */
8064         error = lpfc_setup_driver_resource_phase2(phba);
8065         if (error) {
8066                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8067                                 "1414 Failed to set up driver resource.\n");
8068                 goto out_free_iocb_list;
8069         }
8070
8071         /* Create SCSI host to the physical port */
8072         error = lpfc_create_shost(phba);
8073         if (error) {
8074                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8075                                 "1415 Failed to create scsi host.\n");
8076                 goto out_unset_driver_resource;
8077         }
8078
8079         /* Configure sysfs attributes */
8080         vport = phba->pport;
8081         error = lpfc_alloc_sysfs_attr(vport);
8082         if (error) {
8083                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8084                                 "1416 Failed to allocate sysfs attr\n");
8085                 goto out_destroy_shost;
8086         }
8087
8088         shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
8089         /* Now, trying to enable interrupt and bring up the device */
8090         cfg_mode = phba->cfg_use_msi;
8091         while (true) {
8092                 /* Put device to a known state before enabling interrupt */
8093                 lpfc_stop_port(phba);
8094                 /* Configure and enable interrupt */
8095                 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
8096                 if (intr_mode == LPFC_INTR_ERROR) {
8097                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8098                                         "0426 Failed to enable interrupt.\n");
8099                         error = -ENODEV;
8100                         goto out_free_sysfs_attr;
8101                 }
8102                 /* Default to single FCP EQ for non-MSI-X */
8103                 if (phba->intr_type != MSIX)
8104                         phba->cfg_fcp_eq_count = 1;
8105                 /* Set up SLI-4 HBA */
8106                 if (lpfc_sli4_hba_setup(phba)) {
8107                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8108                                         "1421 Failed to set up hba\n");
8109                         error = -ENODEV;
8110                         goto out_disable_intr;
8111                 }
8112
8113                 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
8114                 if (intr_mode != 0)
8115                         mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
8116                                                             LPFC_ACT_INTR_CNT);
8117
8118                 /* Check active interrupts received only for MSI/MSI-X */
8119                 if (intr_mode == 0 ||
8120                     phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
8121                         /* Log the current active interrupt mode */
8122                         phba->intr_mode = intr_mode;
8123                         lpfc_log_intr_mode(phba, intr_mode);
8124                         break;
8125                 }
8126                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8127                                 "0451 Configure interrupt mode (%d) "
8128                                 "failed active interrupt test.\n",
8129                                 intr_mode);
8130                 /* Unset the preivous SLI-4 HBA setup */
8131                 lpfc_sli4_unset_hba(phba);
8132                 /* Try next level of interrupt mode */
8133                 cfg_mode = --intr_mode;
8134         }
8135
8136         /* Perform post initialization setup */
8137         lpfc_post_init_setup(phba);
8138
8139         /* Check if there are static vports to be created. */
8140         lpfc_create_static_vport(phba);
8141
8142         return 0;
8143
8144 out_disable_intr:
8145         lpfc_sli4_disable_intr(phba);
8146 out_free_sysfs_attr:
8147         lpfc_free_sysfs_attr(vport);
8148 out_destroy_shost:
8149         lpfc_destroy_shost(phba);
8150 out_unset_driver_resource:
8151         lpfc_unset_driver_resource_phase2(phba);
8152 out_free_iocb_list:
8153         lpfc_free_iocb_list(phba);
8154 out_unset_driver_resource_s4:
8155         lpfc_sli4_driver_resource_unset(phba);
8156 out_unset_pci_mem_s4:
8157         lpfc_sli4_pci_mem_unset(phba);
8158 out_disable_pci_dev:
8159         lpfc_disable_pci_dev(phba);
8160         if (shost)
8161                 scsi_host_put(shost);
8162 out_free_phba:
8163         lpfc_hba_free(phba);
8164         return error;
8165 }
8166
8167 /**
8168  * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
8169  * @pdev: pointer to PCI device
8170  *
8171  * This routine is called from the kernel's PCI subsystem to device with
8172  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8173  * removed from PCI bus, it performs all the necessary cleanup for the HBA
8174  * device to be removed from the PCI subsystem properly.
8175  **/
8176 static void __devexit
8177 lpfc_pci_remove_one_s4(struct pci_dev *pdev)
8178 {
8179         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8180         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
8181         struct lpfc_vport **vports;
8182         struct lpfc_hba *phba = vport->phba;
8183         int i;
8184
8185         /* Mark the device unloading flag */
8186         spin_lock_irq(&phba->hbalock);
8187         vport->load_flag |= FC_UNLOADING;
8188         spin_unlock_irq(&phba->hbalock);
8189
8190         /* Free the HBA sysfs attributes */
8191         lpfc_free_sysfs_attr(vport);
8192
8193         /* Release all the vports against this physical port */
8194         vports = lpfc_create_vport_work_array(phba);
8195         if (vports != NULL)
8196                 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
8197                         fc_vport_terminate(vports[i]->fc_vport);
8198         lpfc_destroy_vport_work_array(phba, vports);
8199
8200         /* Remove FC host and then SCSI host with the physical port */
8201         fc_remove_host(shost);
8202         scsi_remove_host(shost);
8203
8204         /* Perform cleanup on the physical port */
8205         lpfc_cleanup(vport);
8206
8207         /*
8208          * Bring down the SLI Layer. This step disables all interrupts,
8209          * clears the rings, discards all mailbox commands, and resets
8210          * the HBA FCoE function.
8211          */
8212         lpfc_debugfs_terminate(vport);
8213         lpfc_sli4_hba_unset(phba);
8214
8215         spin_lock_irq(&phba->hbalock);
8216         list_del_init(&vport->listentry);
8217         spin_unlock_irq(&phba->hbalock);
8218
8219         /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
8220          * buffers are released to their corresponding pools here.
8221          */
8222         lpfc_scsi_free(phba);
8223         lpfc_sli4_driver_resource_unset(phba);
8224
8225         /* Unmap adapter Control and Doorbell registers */
8226         lpfc_sli4_pci_mem_unset(phba);
8227
8228         /* Release PCI resources and disable device's PCI function */
8229         scsi_host_put(shost);
8230         lpfc_disable_pci_dev(phba);
8231
8232         /* Finally, free the driver's device data structure */
8233         lpfc_hba_free(phba);
8234
8235         return;
8236 }
8237
8238 /**
8239  * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
8240  * @pdev: pointer to PCI device
8241  * @msg: power management message
8242  *
8243  * This routine is called from the kernel's PCI subsystem to support system
8244  * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
8245  * this method, it quiesces the device by stopping the driver's worker
8246  * thread for the device, turning off device's interrupt and DMA, and bring
8247  * the device offline. Note that as the driver implements the minimum PM
8248  * requirements to a power-aware driver's PM support for suspend/resume -- all
8249  * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
8250  * method call will be treated as SUSPEND and the driver will fully
8251  * reinitialize its device during resume() method call, the driver will set
8252  * device to PCI_D3hot state in PCI config space instead of setting it
8253  * according to the @msg provided by the PM.
8254  *
8255  * Return code
8256  *      0 - driver suspended the device
8257  *      Error otherwise
8258  **/
8259 static int
8260 lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
8261 {
8262         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8263         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8264
8265         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8266                         "0298 PCI device Power Management suspend.\n");
8267
8268         /* Bring down the device */
8269         lpfc_offline_prep(phba);
8270         lpfc_offline(phba);
8271         kthread_stop(phba->worker_thread);
8272
8273         /* Disable interrupt from device */
8274         lpfc_sli4_disable_intr(phba);
8275
8276         /* Save device state to PCI config space */
8277         pci_save_state(pdev);
8278         pci_set_power_state(pdev, PCI_D3hot);
8279
8280         return 0;
8281 }
8282
8283 /**
8284  * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
8285  * @pdev: pointer to PCI device
8286  *
8287  * This routine is called from the kernel's PCI subsystem to support system
8288  * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
8289  * this method, it restores the device's PCI config space state and fully
8290  * reinitializes the device and brings it online. Note that as the driver
8291  * implements the minimum PM requirements to a power-aware driver's PM for
8292  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8293  * to the suspend() method call will be treated as SUSPEND and the driver
8294  * will fully reinitialize its device during resume() method call, the device
8295  * will be set to PCI_D0 directly in PCI config space before restoring the
8296  * state.
8297  *
8298  * Return code
8299  *      0 - driver suspended the device
8300  *      Error otherwise
8301  **/
8302 static int
8303 lpfc_pci_resume_one_s4(struct pci_dev *pdev)
8304 {
8305         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8306         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8307         uint32_t intr_mode;
8308         int error;
8309
8310         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8311                         "0292 PCI device Power Management resume.\n");
8312
8313         /* Restore device state from PCI config space */
8314         pci_set_power_state(pdev, PCI_D0);
8315         pci_restore_state(pdev);
8316
8317         /*
8318          * As the new kernel behavior of pci_restore_state() API call clears
8319          * device saved_state flag, need to save the restored state again.
8320          */
8321         pci_save_state(pdev);
8322
8323         if (pdev->is_busmaster)
8324                 pci_set_master(pdev);
8325
8326          /* Startup the kernel thread for this host adapter. */
8327         phba->worker_thread = kthread_run(lpfc_do_work, phba,
8328                                         "lpfc_worker_%d", phba->brd_no);
8329         if (IS_ERR(phba->worker_thread)) {
8330                 error = PTR_ERR(phba->worker_thread);
8331                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8332                                 "0293 PM resume failed to start worker "
8333                                 "thread: error=x%x.\n", error);
8334                 return error;
8335         }
8336
8337         /* Configure and enable interrupt */
8338         intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
8339         if (intr_mode == LPFC_INTR_ERROR) {
8340                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8341                                 "0294 PM resume Failed to enable interrupt\n");
8342                 return -EIO;
8343         } else
8344                 phba->intr_mode = intr_mode;
8345
8346         /* Restart HBA and bring it online */
8347         lpfc_sli_brdrestart(phba);
8348         lpfc_online(phba);
8349
8350         /* Log the current active interrupt mode */
8351         lpfc_log_intr_mode(phba, phba->intr_mode);
8352
8353         return 0;
8354 }
8355
8356 /**
8357  * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
8358  * @pdev: pointer to PCI device.
8359  * @state: the current PCI connection state.
8360  *
8361  * This routine is called from the PCI subsystem for error handling to device
8362  * with SLI-4 interface spec. This function is called by the PCI subsystem
8363  * after a PCI bus error affecting this device has been detected. When this
8364  * function is invoked, it will need to stop all the I/Os and interrupt(s)
8365  * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
8366  * for the PCI subsystem to perform proper recovery as desired.
8367  *
8368  * Return codes
8369  *      PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8370  *      PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8371  **/
8372 static pci_ers_result_t
8373 lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8374 {
8375         return PCI_ERS_RESULT_NEED_RESET;
8376 }
8377
8378 /**
8379  * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
8380  * @pdev: pointer to PCI device.
8381  *
8382  * This routine is called from the PCI subsystem for error handling to device
8383  * with SLI-4 interface spec. It is called after PCI bus has been reset to
8384  * restart the PCI card from scratch, as if from a cold-boot. During the
8385  * PCI subsystem error recovery, after the driver returns
8386  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8387  * recovery and then call this routine before calling the .resume method to
8388  * recover the device. This function will initialize the HBA device, enable
8389  * the interrupt, but it will just put the HBA to offline state without
8390  * passing any I/O traffic.
8391  *
8392  * Return codes
8393  *      PCI_ERS_RESULT_RECOVERED - the device has been recovered
8394  *      PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8395  */
8396 static pci_ers_result_t
8397 lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8398 {
8399         return PCI_ERS_RESULT_RECOVERED;
8400 }
8401
8402 /**
8403  * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
8404  * @pdev: pointer to PCI device
8405  *
8406  * This routine is called from the PCI subsystem for error handling to device
8407  * with SLI-4 interface spec. It is called when kernel error recovery tells
8408  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8409  * error recovery. After this call, traffic can start to flow from this device
8410  * again.
8411  **/
8412 static void
8413 lpfc_io_resume_s4(struct pci_dev *pdev)
8414 {
8415         return;
8416 }
8417
8418 /**
8419  * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
8420  * @pdev: pointer to PCI device
8421  * @pid: pointer to PCI device identifier
8422  *
8423  * This routine is to be registered to the kernel's PCI subsystem. When an
8424  * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
8425  * at PCI device-specific information of the device and driver to see if the
8426  * driver state that it can support this kind of device. If the match is
8427  * successful, the driver core invokes this routine. This routine dispatches
8428  * the action to the proper SLI-3 or SLI-4 device probing routine, which will
8429  * do all the initialization that it needs to do to handle the HBA device
8430  * properly.
8431  *
8432  * Return code
8433  *      0 - driver can claim the device
8434  *      negative value - driver can not claim the device
8435  **/
8436 static int __devinit
8437 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
8438 {
8439         int rc;
8440         struct lpfc_sli_intf intf;
8441
8442         if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
8443                 return -ENODEV;
8444
8445         if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
8446             (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
8447                 rc = lpfc_pci_probe_one_s4(pdev, pid);
8448         else
8449                 rc = lpfc_pci_probe_one_s3(pdev, pid);
8450
8451         return rc;
8452 }
8453
8454 /**
8455  * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
8456  * @pdev: pointer to PCI device
8457  *
8458  * This routine is to be registered to the kernel's PCI subsystem. When an
8459  * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
8460  * This routine dispatches the action to the proper SLI-3 or SLI-4 device
8461  * remove routine, which will perform all the necessary cleanup for the
8462  * device to be removed from the PCI subsystem properly.
8463  **/
8464 static void __devexit
8465 lpfc_pci_remove_one(struct pci_dev *pdev)
8466 {
8467         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8468         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8469
8470         switch (phba->pci_dev_grp) {
8471         case LPFC_PCI_DEV_LP:
8472                 lpfc_pci_remove_one_s3(pdev);
8473                 break;
8474         case LPFC_PCI_DEV_OC:
8475                 lpfc_pci_remove_one_s4(pdev);
8476                 break;
8477         default:
8478                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8479                                 "1424 Invalid PCI device group: 0x%x\n",
8480                                 phba->pci_dev_grp);
8481                 break;
8482         }
8483         return;
8484 }
8485
8486 /**
8487  * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
8488  * @pdev: pointer to PCI device
8489  * @msg: power management message
8490  *
8491  * This routine is to be registered to the kernel's PCI subsystem to support
8492  * system Power Management (PM). When PM invokes this method, it dispatches
8493  * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
8494  * suspend the device.
8495  *
8496  * Return code
8497  *      0 - driver suspended the device
8498  *      Error otherwise
8499  **/
8500 static int
8501 lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
8502 {
8503         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8504         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8505         int rc = -ENODEV;
8506
8507         switch (phba->pci_dev_grp) {
8508         case LPFC_PCI_DEV_LP:
8509                 rc = lpfc_pci_suspend_one_s3(pdev, msg);
8510                 break;
8511         case LPFC_PCI_DEV_OC:
8512                 rc = lpfc_pci_suspend_one_s4(pdev, msg);
8513                 break;
8514         default:
8515                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8516                                 "1425 Invalid PCI device group: 0x%x\n",
8517                                 phba->pci_dev_grp);
8518                 break;
8519         }
8520         return rc;
8521 }
8522
8523 /**
8524  * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
8525  * @pdev: pointer to PCI device
8526  *
8527  * This routine is to be registered to the kernel's PCI subsystem to support
8528  * system Power Management (PM). When PM invokes this method, it dispatches
8529  * the action to the proper SLI-3 or SLI-4 device resume routine, which will
8530  * resume the device.
8531  *
8532  * Return code
8533  *      0 - driver suspended the device
8534  *      Error otherwise
8535  **/
8536 static int
8537 lpfc_pci_resume_one(struct pci_dev *pdev)
8538 {
8539         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8540         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8541         int rc = -ENODEV;
8542
8543         switch (phba->pci_dev_grp) {
8544         case LPFC_PCI_DEV_LP:
8545                 rc = lpfc_pci_resume_one_s3(pdev);
8546                 break;
8547         case LPFC_PCI_DEV_OC:
8548                 rc = lpfc_pci_resume_one_s4(pdev);
8549                 break;
8550         default:
8551                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8552                                 "1426 Invalid PCI device group: 0x%x\n",
8553                                 phba->pci_dev_grp);
8554                 break;
8555         }
8556         return rc;
8557 }
8558
8559 /**
8560  * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8561  * @pdev: pointer to PCI device.
8562  * @state: the current PCI connection state.
8563  *
8564  * This routine is registered to the PCI subsystem for error handling. This
8565  * function is called by the PCI subsystem after a PCI bus error affecting
8566  * this device has been detected. When this routine is invoked, it dispatches
8567  * the action to the proper SLI-3 or SLI-4 device error detected handling
8568  * routine, which will perform the proper error detected operation.
8569  *
8570  * Return codes
8571  *      PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8572  *      PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8573  **/
8574 static pci_ers_result_t
8575 lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8576 {
8577         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8578         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8579         pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8580
8581         switch (phba->pci_dev_grp) {
8582         case LPFC_PCI_DEV_LP:
8583                 rc = lpfc_io_error_detected_s3(pdev, state);
8584                 break;
8585         case LPFC_PCI_DEV_OC:
8586                 rc = lpfc_io_error_detected_s4(pdev, state);
8587                 break;
8588         default:
8589                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8590                                 "1427 Invalid PCI device group: 0x%x\n",
8591                                 phba->pci_dev_grp);
8592                 break;
8593         }
8594         return rc;
8595 }
8596
8597 /**
8598  * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8599  * @pdev: pointer to PCI device.
8600  *
8601  * This routine is registered to the PCI subsystem for error handling. This
8602  * function is called after PCI bus has been reset to restart the PCI card
8603  * from scratch, as if from a cold-boot. When this routine is invoked, it
8604  * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8605  * routine, which will perform the proper device reset.
8606  *
8607  * Return codes
8608  *      PCI_ERS_RESULT_RECOVERED - the device has been recovered
8609  *      PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8610  **/
8611 static pci_ers_result_t
8612 lpfc_io_slot_reset(struct pci_dev *pdev)
8613 {
8614         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8615         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8616         pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8617
8618         switch (phba->pci_dev_grp) {
8619         case LPFC_PCI_DEV_LP:
8620                 rc = lpfc_io_slot_reset_s3(pdev);
8621                 break;
8622         case LPFC_PCI_DEV_OC:
8623                 rc = lpfc_io_slot_reset_s4(pdev);
8624                 break;
8625         default:
8626                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8627                                 "1428 Invalid PCI device group: 0x%x\n",
8628                                 phba->pci_dev_grp);
8629                 break;
8630         }
8631         return rc;
8632 }
8633
8634 /**
8635  * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8636  * @pdev: pointer to PCI device
8637  *
8638  * This routine is registered to the PCI subsystem for error handling. It
8639  * is called when kernel error recovery tells the lpfc driver that it is
8640  * OK to resume normal PCI operation after PCI bus error recovery. When
8641  * this routine is invoked, it dispatches the action to the proper SLI-3
8642  * or SLI-4 device io_resume routine, which will resume the device operation.
8643  **/
8644 static void
8645 lpfc_io_resume(struct pci_dev *pdev)
8646 {
8647         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8648         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8649
8650         switch (phba->pci_dev_grp) {
8651         case LPFC_PCI_DEV_LP:
8652                 lpfc_io_resume_s3(pdev);
8653                 break;
8654         case LPFC_PCI_DEV_OC:
8655                 lpfc_io_resume_s4(pdev);
8656                 break;
8657         default:
8658                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8659                                 "1429 Invalid PCI device group: 0x%x\n",
8660                                 phba->pci_dev_grp);
8661                 break;
8662         }
8663         return;
8664 }
8665
8666 static struct pci_device_id lpfc_id_table[] = {
8667         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8668                 PCI_ANY_ID, PCI_ANY_ID, },
8669         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8670                 PCI_ANY_ID, PCI_ANY_ID, },
8671         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8672                 PCI_ANY_ID, PCI_ANY_ID, },
8673         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8674                 PCI_ANY_ID, PCI_ANY_ID, },
8675         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8676                 PCI_ANY_ID, PCI_ANY_ID, },
8677         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8678                 PCI_ANY_ID, PCI_ANY_ID, },
8679         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8680                 PCI_ANY_ID, PCI_ANY_ID, },
8681         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8682                 PCI_ANY_ID, PCI_ANY_ID, },
8683         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8684                 PCI_ANY_ID, PCI_ANY_ID, },
8685         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8686                 PCI_ANY_ID, PCI_ANY_ID, },
8687         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8688                 PCI_ANY_ID, PCI_ANY_ID, },
8689         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8690                 PCI_ANY_ID, PCI_ANY_ID, },
8691         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8692                 PCI_ANY_ID, PCI_ANY_ID, },
8693         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8694                 PCI_ANY_ID, PCI_ANY_ID, },
8695         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8696                 PCI_ANY_ID, PCI_ANY_ID, },
8697         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8698                 PCI_ANY_ID, PCI_ANY_ID, },
8699         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8700                 PCI_ANY_ID, PCI_ANY_ID, },
8701         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8702                 PCI_ANY_ID, PCI_ANY_ID, },
8703         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8704                 PCI_ANY_ID, PCI_ANY_ID, },
8705         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8706                 PCI_ANY_ID, PCI_ANY_ID, },
8707         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8708                 PCI_ANY_ID, PCI_ANY_ID, },
8709         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8710                 PCI_ANY_ID, PCI_ANY_ID, },
8711         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8712                 PCI_ANY_ID, PCI_ANY_ID, },
8713         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8714                 PCI_ANY_ID, PCI_ANY_ID, },
8715         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8716                 PCI_ANY_ID, PCI_ANY_ID, },
8717         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8718                 PCI_ANY_ID, PCI_ANY_ID, },
8719         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8720                 PCI_ANY_ID, PCI_ANY_ID, },
8721         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8722                 PCI_ANY_ID, PCI_ANY_ID, },
8723         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8724                 PCI_ANY_ID, PCI_ANY_ID, },
8725         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8726                 PCI_ANY_ID, PCI_ANY_ID, },
8727         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8728                 PCI_ANY_ID, PCI_ANY_ID, },
8729         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8730                 PCI_ANY_ID, PCI_ANY_ID, },
8731         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8732                 PCI_ANY_ID, PCI_ANY_ID, },
8733         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8734                 PCI_ANY_ID, PCI_ANY_ID, },
8735         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8736                 PCI_ANY_ID, PCI_ANY_ID, },
8737         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8738                 PCI_ANY_ID, PCI_ANY_ID, },
8739         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8740                 PCI_ANY_ID, PCI_ANY_ID, },
8741         {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8742                 PCI_ANY_ID, PCI_ANY_ID, },
8743         {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8744                 PCI_ANY_ID, PCI_ANY_ID, },
8745         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
8746                 PCI_ANY_ID, PCI_ANY_ID, },
8747         { 0 }
8748 };
8749
8750 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8751
8752 static struct pci_error_handlers lpfc_err_handler = {
8753         .error_detected = lpfc_io_error_detected,
8754         .slot_reset = lpfc_io_slot_reset,
8755         .resume = lpfc_io_resume,
8756 };
8757
8758 static struct pci_driver lpfc_driver = {
8759         .name           = LPFC_DRIVER_NAME,
8760         .id_table       = lpfc_id_table,
8761         .probe          = lpfc_pci_probe_one,
8762         .remove         = __devexit_p(lpfc_pci_remove_one),
8763         .suspend        = lpfc_pci_suspend_one,
8764         .resume         = lpfc_pci_resume_one,
8765         .err_handler    = &lpfc_err_handler,
8766 };
8767
8768 /**
8769  * lpfc_init - lpfc module initialization routine
8770  *
8771  * This routine is to be invoked when the lpfc module is loaded into the
8772  * kernel. The special kernel macro module_init() is used to indicate the
8773  * role of this routine to the kernel as lpfc module entry point.
8774  *
8775  * Return codes
8776  *   0 - successful
8777  *   -ENOMEM - FC attach transport failed
8778  *   all others - failed
8779  */
8780 static int __init
8781 lpfc_init(void)
8782 {
8783         int error = 0;
8784
8785         printk(LPFC_MODULE_DESC "\n");
8786         printk(LPFC_COPYRIGHT "\n");
8787
8788         if (lpfc_enable_npiv) {
8789                 lpfc_transport_functions.vport_create = lpfc_vport_create;
8790                 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8791         }
8792         lpfc_transport_template =
8793                                 fc_attach_transport(&lpfc_transport_functions);
8794         if (lpfc_transport_template == NULL)
8795                 return -ENOMEM;
8796         if (lpfc_enable_npiv) {
8797                 lpfc_vport_transport_template =
8798                         fc_attach_transport(&lpfc_vport_transport_functions);
8799                 if (lpfc_vport_transport_template == NULL) {
8800                         fc_release_transport(lpfc_transport_template);
8801                         return -ENOMEM;
8802                 }
8803         }
8804         error = pci_register_driver(&lpfc_driver);
8805         if (error) {
8806                 fc_release_transport(lpfc_transport_template);
8807                 if (lpfc_enable_npiv)
8808                         fc_release_transport(lpfc_vport_transport_template);
8809         }
8810
8811         return error;
8812 }
8813
8814 /**
8815  * lpfc_exit - lpfc module removal routine
8816  *
8817  * This routine is invoked when the lpfc module is removed from the kernel.
8818  * The special kernel macro module_exit() is used to indicate the role of
8819  * this routine to the kernel as lpfc module exit point.
8820  */
8821 static void __exit
8822 lpfc_exit(void)
8823 {
8824         pci_unregister_driver(&lpfc_driver);
8825         fc_release_transport(lpfc_transport_template);
8826         if (lpfc_enable_npiv)
8827                 fc_release_transport(lpfc_vport_transport_template);
8828         if (_dump_buf_data) {
8829                 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8830                                 "_dump_buf_data at 0x%p\n",
8831                                 (1L << _dump_buf_data_order), _dump_buf_data);
8832                 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8833         }
8834
8835         if (_dump_buf_dif) {
8836                 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8837                                 "_dump_buf_dif at 0x%p\n",
8838                                 (1L << _dump_buf_dif_order), _dump_buf_dif);
8839                 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8840         }
8841 }
8842
8843 module_init(lpfc_init);
8844 module_exit(lpfc_exit);
8845 MODULE_LICENSE("GPL");
8846 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8847 MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8848 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);