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