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