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