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