9244aa64b3beec357a7abe92031b4cbac4acd3d9
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_init.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2010 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/idr.h>
26 #include <linux/interrupt.h>
27 #include <linux/kthread.h>
28 #include <linux/pci.h>
29 #include <linux/spinlock.h>
30 #include <linux/ctype.h>
31 #include <linux/aer.h>
32 #include <linux/slab.h>
33
34 #include <scsi/scsi.h>
35 #include <scsi/scsi_device.h>
36 #include <scsi/scsi_host.h>
37 #include <scsi/scsi_transport_fc.h>
38
39 #include "lpfc_hw4.h"
40 #include "lpfc_hw.h"
41 #include "lpfc_sli.h"
42 #include "lpfc_sli4.h"
43 #include "lpfc_nl.h"
44 #include "lpfc_disc.h"
45 #include "lpfc_scsi.h"
46 #include "lpfc.h"
47 #include "lpfc_logmsg.h"
48 #include "lpfc_crtn.h"
49 #include "lpfc_vport.h"
50 #include "lpfc_version.h"
51
52 char *_dump_buf_data;
53 unsigned long _dump_buf_data_order;
54 char *_dump_buf_dif;
55 unsigned long _dump_buf_dif_order;
56 spinlock_t _dump_buf_lock;
57
58 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
59 static int lpfc_post_rcv_buf(struct lpfc_hba *);
60 static int lpfc_sli4_queue_create(struct lpfc_hba *);
61 static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
62 static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
63 static int lpfc_setup_endian_order(struct lpfc_hba *);
64 static int lpfc_sli4_read_config(struct lpfc_hba *);
65 static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
66 static void lpfc_free_sgl_list(struct lpfc_hba *);
67 static int lpfc_init_sgl_list(struct lpfc_hba *);
68 static int lpfc_init_active_sgl_array(struct lpfc_hba *);
69 static void lpfc_free_active_sgl(struct lpfc_hba *);
70 static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
71 static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
72 static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
73 static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
74 static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
75
76 static struct scsi_transport_template *lpfc_transport_template = NULL;
77 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
78 static DEFINE_IDR(lpfc_hba_index);
79
80 /**
81  * lpfc_config_port_prep - Perform lpfc initialization prior to config port
82  * @phba: pointer to lpfc hba data structure.
83  *
84  * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
85  * mailbox command. It retrieves the revision information from the HBA and
86  * collects the Vital Product Data (VPD) about the HBA for preparing the
87  * configuration of the HBA.
88  *
89  * Return codes:
90  *   0 - success.
91  *   -ERESTART - requests the SLI layer to reset the HBA and try again.
92  *   Any other value - indicates an error.
93  **/
94 int
95 lpfc_config_port_prep(struct lpfc_hba *phba)
96 {
97         lpfc_vpd_t *vp = &phba->vpd;
98         int i = 0, rc;
99         LPFC_MBOXQ_t *pmb;
100         MAILBOX_t *mb;
101         char *lpfc_vpd_data = NULL;
102         uint16_t offset = 0;
103         static char licensed[56] =
104                     "key unlock for use with gnu public licensed code only\0";
105         static int init_key = 1;
106
107         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
108         if (!pmb) {
109                 phba->link_state = LPFC_HBA_ERROR;
110                 return -ENOMEM;
111         }
112
113         mb = &pmb->u.mb;
114         phba->link_state = LPFC_INIT_MBX_CMDS;
115
116         if (lpfc_is_LC_HBA(phba->pcidev->device)) {
117                 if (init_key) {
118                         uint32_t *ptext = (uint32_t *) licensed;
119
120                         for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
121                                 *ptext = cpu_to_be32(*ptext);
122                         init_key = 0;
123                 }
124
125                 lpfc_read_nv(phba, pmb);
126                 memset((char*)mb->un.varRDnvp.rsvd3, 0,
127                         sizeof (mb->un.varRDnvp.rsvd3));
128                 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
129                          sizeof (licensed));
130
131                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
132
133                 if (rc != MBX_SUCCESS) {
134                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
135                                         "0324 Config Port initialization "
136                                         "error, mbxCmd x%x READ_NVPARM, "
137                                         "mbxStatus x%x\n",
138                                         mb->mbxCommand, mb->mbxStatus);
139                         mempool_free(pmb, phba->mbox_mem_pool);
140                         return -ERESTART;
141                 }
142                 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
143                        sizeof(phba->wwnn));
144                 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
145                        sizeof(phba->wwpn));
146         }
147
148         phba->sli3_options = 0x0;
149
150         /* Setup and issue mailbox READ REV command */
151         lpfc_read_rev(phba, pmb);
152         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
153         if (rc != MBX_SUCCESS) {
154                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
155                                 "0439 Adapter failed to init, mbxCmd x%x "
156                                 "READ_REV, mbxStatus x%x\n",
157                                 mb->mbxCommand, mb->mbxStatus);
158                 mempool_free( pmb, phba->mbox_mem_pool);
159                 return -ERESTART;
160         }
161
162
163         /*
164          * The value of rr must be 1 since the driver set the cv field to 1.
165          * This setting requires the FW to set all revision fields.
166          */
167         if (mb->un.varRdRev.rr == 0) {
168                 vp->rev.rBit = 0;
169                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
170                                 "0440 Adapter failed to init, READ_REV has "
171                                 "missing revision information.\n");
172                 mempool_free(pmb, phba->mbox_mem_pool);
173                 return -ERESTART;
174         }
175
176         if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
177                 mempool_free(pmb, phba->mbox_mem_pool);
178                 return -EINVAL;
179         }
180
181         /* Save information as VPD data */
182         vp->rev.rBit = 1;
183         memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
184         vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
185         memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
186         vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
187         memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
188         vp->rev.biuRev = mb->un.varRdRev.biuRev;
189         vp->rev.smRev = mb->un.varRdRev.smRev;
190         vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
191         vp->rev.endecRev = mb->un.varRdRev.endecRev;
192         vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
193         vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
194         vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
195         vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
196         vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
197         vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
198
199         /* If the sli feature level is less then 9, we must
200          * tear down all RPIs and VPIs on link down if NPIV
201          * is enabled.
202          */
203         if (vp->rev.feaLevelHigh < 9)
204                 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
205
206         if (lpfc_is_LC_HBA(phba->pcidev->device))
207                 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
208                                                 sizeof (phba->RandomData));
209
210         /* Get adapter VPD information */
211         lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
212         if (!lpfc_vpd_data)
213                 goto out_free_mbox;
214
215         do {
216                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
217                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
218
219                 if (rc != MBX_SUCCESS) {
220                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
221                                         "0441 VPD not present on adapter, "
222                                         "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
223                                         mb->mbxCommand, mb->mbxStatus);
224                         mb->un.varDmp.word_cnt = 0;
225                 }
226                 /* dump mem may return a zero when finished or we got a
227                  * mailbox error, either way we are done.
228                  */
229                 if (mb->un.varDmp.word_cnt == 0)
230                         break;
231                 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
232                         mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
233                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
234                                       lpfc_vpd_data + offset,
235                                       mb->un.varDmp.word_cnt);
236                 offset += mb->un.varDmp.word_cnt;
237         } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
238         lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
239
240         kfree(lpfc_vpd_data);
241 out_free_mbox:
242         mempool_free(pmb, phba->mbox_mem_pool);
243         return 0;
244 }
245
246 /**
247  * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
248  * @phba: pointer to lpfc hba data structure.
249  * @pmboxq: pointer to the driver internal queue element for mailbox command.
250  *
251  * This is the completion handler for driver's configuring asynchronous event
252  * mailbox command to the device. If the mailbox command returns successfully,
253  * it will set internal async event support flag to 1; otherwise, it will
254  * set internal async event support flag to 0.
255  **/
256 static void
257 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
258 {
259         if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
260                 phba->temp_sensor_support = 1;
261         else
262                 phba->temp_sensor_support = 0;
263         mempool_free(pmboxq, phba->mbox_mem_pool);
264         return;
265 }
266
267 /**
268  * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
269  * @phba: pointer to lpfc hba data structure.
270  * @pmboxq: pointer to the driver internal queue element for mailbox command.
271  *
272  * This is the completion handler for dump mailbox command for getting
273  * wake up parameters. When this command complete, the response contain
274  * Option rom version of the HBA. This function translate the version number
275  * into a human readable string and store it in OptionROMVersion.
276  **/
277 static void
278 lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
279 {
280         struct prog_id *prg;
281         uint32_t prog_id_word;
282         char dist = ' ';
283         /* character array used for decoding dist type. */
284         char dist_char[] = "nabx";
285
286         if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
287                 mempool_free(pmboxq, phba->mbox_mem_pool);
288                 return;
289         }
290
291         prg = (struct prog_id *) &prog_id_word;
292
293         /* word 7 contain option rom version */
294         prog_id_word = pmboxq->u.mb.un.varWords[7];
295
296         /* Decode the Option rom version word to a readable string */
297         if (prg->dist < 4)
298                 dist = dist_char[prg->dist];
299
300         if ((prg->dist == 3) && (prg->num == 0))
301                 sprintf(phba->OptionROMVersion, "%d.%d%d",
302                         prg->ver, prg->rev, prg->lev);
303         else
304                 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
305                         prg->ver, prg->rev, prg->lev,
306                         dist, prg->num);
307         mempool_free(pmboxq, phba->mbox_mem_pool);
308         return;
309 }
310
311 /**
312  * lpfc_config_port_post - Perform lpfc initialization after config port
313  * @phba: pointer to lpfc hba data structure.
314  *
315  * This routine will do LPFC initialization after the CONFIG_PORT mailbox
316  * command call. It performs all internal resource and state setups on the
317  * port: post IOCB buffers, enable appropriate host interrupt attentions,
318  * ELS ring timers, etc.
319  *
320  * Return codes
321  *   0 - success.
322  *   Any other value - error.
323  **/
324 int
325 lpfc_config_port_post(struct lpfc_hba *phba)
326 {
327         struct lpfc_vport *vport = phba->pport;
328         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
329         LPFC_MBOXQ_t *pmb;
330         MAILBOX_t *mb;
331         struct lpfc_dmabuf *mp;
332         struct lpfc_sli *psli = &phba->sli;
333         uint32_t status, timeout;
334         int i, j;
335         int rc;
336
337         spin_lock_irq(&phba->hbalock);
338         /*
339          * If the Config port completed correctly the HBA is not
340          * over heated any more.
341          */
342         if (phba->over_temp_state == HBA_OVER_TEMP)
343                 phba->over_temp_state = HBA_NORMAL_TEMP;
344         spin_unlock_irq(&phba->hbalock);
345
346         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
347         if (!pmb) {
348                 phba->link_state = LPFC_HBA_ERROR;
349                 return -ENOMEM;
350         }
351         mb = &pmb->u.mb;
352
353         /* Get login parameters for NID.  */
354         rc = lpfc_read_sparam(phba, pmb, 0);
355         if (rc) {
356                 mempool_free(pmb, phba->mbox_mem_pool);
357                 return -ENOMEM;
358         }
359
360         pmb->vport = vport;
361         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
362                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
363                                 "0448 Adapter failed init, mbxCmd x%x "
364                                 "READ_SPARM mbxStatus x%x\n",
365                                 mb->mbxCommand, mb->mbxStatus);
366                 phba->link_state = LPFC_HBA_ERROR;
367                 mp = (struct lpfc_dmabuf *) pmb->context1;
368                 mempool_free(pmb, phba->mbox_mem_pool);
369                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
370                 kfree(mp);
371                 return -EIO;
372         }
373
374         mp = (struct lpfc_dmabuf *) pmb->context1;
375
376         memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
377         lpfc_mbuf_free(phba, mp->virt, mp->phys);
378         kfree(mp);
379         pmb->context1 = NULL;
380
381         if (phba->cfg_soft_wwnn)
382                 u64_to_wwn(phba->cfg_soft_wwnn,
383                            vport->fc_sparam.nodeName.u.wwn);
384         if (phba->cfg_soft_wwpn)
385                 u64_to_wwn(phba->cfg_soft_wwpn,
386                            vport->fc_sparam.portName.u.wwn);
387         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
388                sizeof (struct lpfc_name));
389         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
390                sizeof (struct lpfc_name));
391
392         /* Update the fc_host data structures with new wwn. */
393         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
394         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
395         fc_host_max_npiv_vports(shost) = phba->max_vpi;
396
397         /* If no serial number in VPD data, use low 6 bytes of WWNN */
398         /* This should be consolidated into parse_vpd ? - mr */
399         if (phba->SerialNumber[0] == 0) {
400                 uint8_t *outptr;
401
402                 outptr = &vport->fc_nodename.u.s.IEEE[0];
403                 for (i = 0; i < 12; i++) {
404                         status = *outptr++;
405                         j = ((status & 0xf0) >> 4);
406                         if (j <= 9)
407                                 phba->SerialNumber[i] =
408                                     (char)((uint8_t) 0x30 + (uint8_t) j);
409                         else
410                                 phba->SerialNumber[i] =
411                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
412                         i++;
413                         j = (status & 0xf);
414                         if (j <= 9)
415                                 phba->SerialNumber[i] =
416                                     (char)((uint8_t) 0x30 + (uint8_t) j);
417                         else
418                                 phba->SerialNumber[i] =
419                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
420                 }
421         }
422
423         lpfc_read_config(phba, pmb);
424         pmb->vport = vport;
425         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
426                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
427                                 "0453 Adapter failed to init, mbxCmd x%x "
428                                 "READ_CONFIG, mbxStatus x%x\n",
429                                 mb->mbxCommand, mb->mbxStatus);
430                 phba->link_state = LPFC_HBA_ERROR;
431                 mempool_free( pmb, phba->mbox_mem_pool);
432                 return -EIO;
433         }
434
435         /* Check if the port is disabled */
436         lpfc_sli_read_link_ste(phba);
437
438         /* Reset the DFT_HBA_Q_DEPTH to the max xri  */
439         if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
440                 phba->cfg_hba_queue_depth =
441                         (mb->un.varRdConfig.max_xri + 1) -
442                                         lpfc_sli4_get_els_iocb_cnt(phba);
443
444         phba->lmt = mb->un.varRdConfig.lmt;
445
446         /* Get the default values for Model Name and Description */
447         lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
448
449         if ((phba->cfg_link_speed > LINK_SPEED_10G)
450             || ((phba->cfg_link_speed == LINK_SPEED_1G)
451                 && !(phba->lmt & LMT_1Gb))
452             || ((phba->cfg_link_speed == LINK_SPEED_2G)
453                 && !(phba->lmt & LMT_2Gb))
454             || ((phba->cfg_link_speed == LINK_SPEED_4G)
455                 && !(phba->lmt & LMT_4Gb))
456             || ((phba->cfg_link_speed == LINK_SPEED_8G)
457                 && !(phba->lmt & LMT_8Gb))
458             || ((phba->cfg_link_speed == LINK_SPEED_10G)
459                 && !(phba->lmt & LMT_10Gb))) {
460                 /* Reset link speed to auto */
461                 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
462                         "1302 Invalid speed for this board: "
463                         "Reset link speed to auto: x%x\n",
464                         phba->cfg_link_speed);
465                         phba->cfg_link_speed = LINK_SPEED_AUTO;
466         }
467
468         phba->link_state = LPFC_LINK_DOWN;
469
470         /* Only process IOCBs on ELS ring till hba_state is READY */
471         if (psli->ring[psli->extra_ring].cmdringaddr)
472                 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
473         if (psli->ring[psli->fcp_ring].cmdringaddr)
474                 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
475         if (psli->ring[psli->next_ring].cmdringaddr)
476                 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
477
478         /* Post receive buffers for desired rings */
479         if (phba->sli_rev != 3)
480                 lpfc_post_rcv_buf(phba);
481
482         /*
483          * Configure HBA MSI-X attention conditions to messages if MSI-X mode
484          */
485         if (phba->intr_type == MSIX) {
486                 rc = lpfc_config_msi(phba, pmb);
487                 if (rc) {
488                         mempool_free(pmb, phba->mbox_mem_pool);
489                         return -EIO;
490                 }
491                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
492                 if (rc != MBX_SUCCESS) {
493                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
494                                         "0352 Config MSI mailbox command "
495                                         "failed, mbxCmd x%x, mbxStatus x%x\n",
496                                         pmb->u.mb.mbxCommand,
497                                         pmb->u.mb.mbxStatus);
498                         mempool_free(pmb, phba->mbox_mem_pool);
499                         return -EIO;
500                 }
501         }
502
503         spin_lock_irq(&phba->hbalock);
504         /* Initialize ERATT handling flag */
505         phba->hba_flag &= ~HBA_ERATT_HANDLED;
506
507         /* Enable appropriate host interrupts */
508         status = readl(phba->HCregaddr);
509         status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
510         if (psli->num_rings > 0)
511                 status |= HC_R0INT_ENA;
512         if (psli->num_rings > 1)
513                 status |= HC_R1INT_ENA;
514         if (psli->num_rings > 2)
515                 status |= HC_R2INT_ENA;
516         if (psli->num_rings > 3)
517                 status |= HC_R3INT_ENA;
518
519         if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
520             (phba->cfg_poll & DISABLE_FCP_RING_INT))
521                 status &= ~(HC_R0INT_ENA);
522
523         writel(status, phba->HCregaddr);
524         readl(phba->HCregaddr); /* flush */
525         spin_unlock_irq(&phba->hbalock);
526
527         /* Set up ring-0 (ELS) timer */
528         timeout = phba->fc_ratov * 2;
529         mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
530         /* Set up heart beat (HB) timer */
531         mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
532         phba->hb_outstanding = 0;
533         phba->last_completion_time = jiffies;
534         /* Set up error attention (ERATT) polling timer */
535         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
536
537         if (phba->hba_flag & LINK_DISABLED) {
538                 lpfc_printf_log(phba,
539                         KERN_ERR, LOG_INIT,
540                         "2598 Adapter Link is disabled.\n");
541                 lpfc_down_link(phba, pmb);
542                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
543                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
544                 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
545                         lpfc_printf_log(phba,
546                         KERN_ERR, LOG_INIT,
547                         "2599 Adapter failed to issue DOWN_LINK"
548                         " mbox command rc 0x%x\n", rc);
549
550                         mempool_free(pmb, phba->mbox_mem_pool);
551                         return -EIO;
552                 }
553         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
554                 lpfc_init_link(phba, pmb, phba->cfg_topology,
555                         phba->cfg_link_speed);
556                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
557                 lpfc_set_loopback_flag(phba);
558                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
559                 if (rc != MBX_SUCCESS) {
560                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
561                                 "0454 Adapter failed to init, mbxCmd x%x "
562                                 "INIT_LINK, mbxStatus x%x\n",
563                                 mb->mbxCommand, mb->mbxStatus);
564
565                         /* Clear all interrupt enable conditions */
566                         writel(0, phba->HCregaddr);
567                         readl(phba->HCregaddr); /* flush */
568                         /* Clear all pending interrupts */
569                         writel(0xffffffff, phba->HAregaddr);
570                         readl(phba->HAregaddr); /* flush */
571
572                         phba->link_state = LPFC_HBA_ERROR;
573                         if (rc != MBX_BUSY)
574                                 mempool_free(pmb, phba->mbox_mem_pool);
575                         return -EIO;
576                 }
577         }
578         /* MBOX buffer will be freed in mbox compl */
579         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
580         if (!pmb) {
581                 phba->link_state = LPFC_HBA_ERROR;
582                 return -ENOMEM;
583         }
584
585         lpfc_config_async(phba, pmb, LPFC_ELS_RING);
586         pmb->mbox_cmpl = lpfc_config_async_cmpl;
587         pmb->vport = phba->pport;
588         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
589
590         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
591                 lpfc_printf_log(phba,
592                                 KERN_ERR,
593                                 LOG_INIT,
594                                 "0456 Adapter failed to issue "
595                                 "ASYNCEVT_ENABLE mbox status x%x\n",
596                                 rc);
597                 mempool_free(pmb, phba->mbox_mem_pool);
598         }
599
600         /* Get Option rom version */
601         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
602         if (!pmb) {
603                 phba->link_state = LPFC_HBA_ERROR;
604                 return -ENOMEM;
605         }
606
607         lpfc_dump_wakeup_param(phba, pmb);
608         pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
609         pmb->vport = phba->pport;
610         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
611
612         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
613                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
614                                 "to get Option ROM version status x%x\n", rc);
615                 mempool_free(pmb, phba->mbox_mem_pool);
616         }
617
618         return 0;
619 }
620
621 /**
622  * lpfc_hba_init_link - Initialize the FC link
623  * @phba: pointer to lpfc hba data structure.
624  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
625  *
626  * This routine will issue the INIT_LINK mailbox command call.
627  * It is available to other drivers through the lpfc_hba data
628  * structure for use as a delayed link up mechanism with the
629  * module parameter lpfc_suppress_link_up.
630  *
631  * Return code
632  *              0 - success
633  *              Any other value - error
634  **/
635 int
636 lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
637 {
638         struct lpfc_vport *vport = phba->pport;
639         LPFC_MBOXQ_t *pmb;
640         MAILBOX_t *mb;
641         int rc;
642
643         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
644         if (!pmb) {
645                 phba->link_state = LPFC_HBA_ERROR;
646                 return -ENOMEM;
647         }
648         mb = &pmb->u.mb;
649         pmb->vport = vport;
650
651         lpfc_init_link(phba, pmb, phba->cfg_topology,
652                 phba->cfg_link_speed);
653         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
654         lpfc_set_loopback_flag(phba);
655         rc = lpfc_sli_issue_mbox(phba, pmb, flag);
656         if (rc != MBX_SUCCESS) {
657                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
658                         "0498 Adapter failed to init, mbxCmd x%x "
659                         "INIT_LINK, mbxStatus x%x\n",
660                         mb->mbxCommand, mb->mbxStatus);
661                 /* Clear all interrupt enable conditions */
662                 writel(0, phba->HCregaddr);
663                 readl(phba->HCregaddr); /* flush */
664                 /* Clear all pending interrupts */
665                 writel(0xffffffff, phba->HAregaddr);
666                 readl(phba->HAregaddr); /* flush */
667                 phba->link_state = LPFC_HBA_ERROR;
668                 if (rc != MBX_BUSY || flag == MBX_POLL)
669                         mempool_free(pmb, phba->mbox_mem_pool);
670                 return -EIO;
671         }
672         phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
673         if (flag == MBX_POLL)
674                 mempool_free(pmb, phba->mbox_mem_pool);
675
676         return 0;
677 }
678
679 /**
680  * lpfc_hba_down_link - this routine downs the FC link
681  * @phba: pointer to lpfc hba data structure.
682  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
683  *
684  * This routine will issue the DOWN_LINK mailbox command call.
685  * It is available to other drivers through the lpfc_hba data
686  * structure for use to stop the link.
687  *
688  * Return code
689  *              0 - success
690  *              Any other value - error
691  **/
692 int
693 lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
694 {
695         LPFC_MBOXQ_t *pmb;
696         int rc;
697
698         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
699         if (!pmb) {
700                 phba->link_state = LPFC_HBA_ERROR;
701                 return -ENOMEM;
702         }
703
704         lpfc_printf_log(phba,
705                 KERN_ERR, LOG_INIT,
706                 "0491 Adapter Link is disabled.\n");
707         lpfc_down_link(phba, pmb);
708         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
709         rc = lpfc_sli_issue_mbox(phba, pmb, flag);
710         if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
711                 lpfc_printf_log(phba,
712                 KERN_ERR, LOG_INIT,
713                 "2522 Adapter failed to issue DOWN_LINK"
714                 " mbox command rc 0x%x\n", rc);
715
716                 mempool_free(pmb, phba->mbox_mem_pool);
717                 return -EIO;
718         }
719         if (flag == MBX_POLL)
720                 mempool_free(pmb, phba->mbox_mem_pool);
721
722         return 0;
723 }
724
725 /**
726  * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
727  * @phba: pointer to lpfc HBA data structure.
728  *
729  * This routine will do LPFC uninitialization before the HBA is reset when
730  * bringing down the SLI Layer.
731  *
732  * Return codes
733  *   0 - success.
734  *   Any other value - error.
735  **/
736 int
737 lpfc_hba_down_prep(struct lpfc_hba *phba)
738 {
739         struct lpfc_vport **vports;
740         int i;
741
742         if (phba->sli_rev <= LPFC_SLI_REV3) {
743                 /* Disable interrupts */
744                 writel(0, phba->HCregaddr);
745                 readl(phba->HCregaddr); /* flush */
746         }
747
748         if (phba->pport->load_flag & FC_UNLOADING)
749                 lpfc_cleanup_discovery_resources(phba->pport);
750         else {
751                 vports = lpfc_create_vport_work_array(phba);
752                 if (vports != NULL)
753                         for (i = 0; i <= phba->max_vports &&
754                                 vports[i] != NULL; i++)
755                                 lpfc_cleanup_discovery_resources(vports[i]);
756                 lpfc_destroy_vport_work_array(phba, vports);
757         }
758         return 0;
759 }
760
761 /**
762  * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
763  * @phba: pointer to lpfc HBA data structure.
764  *
765  * This routine will do uninitialization after the HBA is reset when bring
766  * down the SLI Layer.
767  *
768  * Return codes
769  *   0 - success.
770  *   Any other value - error.
771  **/
772 static int
773 lpfc_hba_down_post_s3(struct lpfc_hba *phba)
774 {
775         struct lpfc_sli *psli = &phba->sli;
776         struct lpfc_sli_ring *pring;
777         struct lpfc_dmabuf *mp, *next_mp;
778         LIST_HEAD(completions);
779         int i;
780
781         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
782                 lpfc_sli_hbqbuf_free_all(phba);
783         else {
784                 /* Cleanup preposted buffers on the ELS ring */
785                 pring = &psli->ring[LPFC_ELS_RING];
786                 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
787                         list_del(&mp->list);
788                         pring->postbufq_cnt--;
789                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
790                         kfree(mp);
791                 }
792         }
793
794         spin_lock_irq(&phba->hbalock);
795         for (i = 0; i < psli->num_rings; i++) {
796                 pring = &psli->ring[i];
797
798                 /* At this point in time the HBA is either reset or DOA. Either
799                  * way, nothing should be on txcmplq as it will NEVER complete.
800                  */
801                 list_splice_init(&pring->txcmplq, &completions);
802                 pring->txcmplq_cnt = 0;
803                 spin_unlock_irq(&phba->hbalock);
804
805                 /* Cancel all the IOCBs from the completions list */
806                 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
807                                       IOERR_SLI_ABORTED);
808
809                 lpfc_sli_abort_iocb_ring(phba, pring);
810                 spin_lock_irq(&phba->hbalock);
811         }
812         spin_unlock_irq(&phba->hbalock);
813
814         return 0;
815 }
816 /**
817  * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
818  * @phba: pointer to lpfc HBA data structure.
819  *
820  * This routine will do uninitialization after the HBA is reset when bring
821  * down the SLI Layer.
822  *
823  * Return codes
824  *   0 - success.
825  *   Any other value - error.
826  **/
827 static int
828 lpfc_hba_down_post_s4(struct lpfc_hba *phba)
829 {
830         struct lpfc_scsi_buf *psb, *psb_next;
831         LIST_HEAD(aborts);
832         int ret;
833         unsigned long iflag = 0;
834         struct lpfc_sglq *sglq_entry = NULL;
835
836         ret = lpfc_hba_down_post_s3(phba);
837         if (ret)
838                 return ret;
839         /* At this point in time the HBA is either reset or DOA. Either
840          * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
841          * on the lpfc_sgl_list so that it can either be freed if the
842          * driver is unloading or reposted if the driver is restarting
843          * the port.
844          */
845         spin_lock_irq(&phba->hbalock);  /* required for lpfc_sgl_list and */
846                                         /* scsl_buf_list */
847         /* abts_sgl_list_lock required because worker thread uses this
848          * list.
849          */
850         spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
851         list_for_each_entry(sglq_entry,
852                 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
853                 sglq_entry->state = SGL_FREED;
854
855         list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
856                         &phba->sli4_hba.lpfc_sgl_list);
857         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
858         /* abts_scsi_buf_list_lock required because worker thread uses this
859          * list.
860          */
861         spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
862         list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
863                         &aborts);
864         spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
865         spin_unlock_irq(&phba->hbalock);
866
867         list_for_each_entry_safe(psb, psb_next, &aborts, list) {
868                 psb->pCmd = NULL;
869                 psb->status = IOSTAT_SUCCESS;
870         }
871         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
872         list_splice(&aborts, &phba->lpfc_scsi_buf_list);
873         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
874         return 0;
875 }
876
877 /**
878  * lpfc_hba_down_post - Wrapper func for hba down post routine
879  * @phba: pointer to lpfc HBA data structure.
880  *
881  * This routine wraps the actual SLI3 or SLI4 routine for performing
882  * uninitialization after the HBA is reset when bring down the SLI Layer.
883  *
884  * Return codes
885  *   0 - success.
886  *   Any other value - error.
887  **/
888 int
889 lpfc_hba_down_post(struct lpfc_hba *phba)
890 {
891         return (*phba->lpfc_hba_down_post)(phba);
892 }
893
894 /**
895  * lpfc_hb_timeout - The HBA-timer timeout handler
896  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
897  *
898  * This is the HBA-timer timeout handler registered to the lpfc driver. When
899  * this timer fires, a HBA timeout event shall be posted to the lpfc driver
900  * work-port-events bitmap and the worker thread is notified. This timeout
901  * event will be used by the worker thread to invoke the actual timeout
902  * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
903  * be performed in the timeout handler and the HBA timeout event bit shall
904  * be cleared by the worker thread after it has taken the event bitmap out.
905  **/
906 static void
907 lpfc_hb_timeout(unsigned long ptr)
908 {
909         struct lpfc_hba *phba;
910         uint32_t tmo_posted;
911         unsigned long iflag;
912
913         phba = (struct lpfc_hba *)ptr;
914
915         /* Check for heart beat timeout conditions */
916         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
917         tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
918         if (!tmo_posted)
919                 phba->pport->work_port_events |= WORKER_HB_TMO;
920         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
921
922         /* Tell the worker thread there is work to do */
923         if (!tmo_posted)
924                 lpfc_worker_wake_up(phba);
925         return;
926 }
927
928 /**
929  * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
930  * @phba: pointer to lpfc hba data structure.
931  * @pmboxq: pointer to the driver internal queue element for mailbox command.
932  *
933  * This is the callback function to the lpfc heart-beat mailbox command.
934  * If configured, the lpfc driver issues the heart-beat mailbox command to
935  * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
936  * heart-beat mailbox command is issued, the driver shall set up heart-beat
937  * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
938  * heart-beat outstanding state. Once the mailbox command comes back and
939  * no error conditions detected, the heart-beat mailbox command timer is
940  * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
941  * state is cleared for the next heart-beat. If the timer expired with the
942  * heart-beat outstanding state set, the driver will put the HBA offline.
943  **/
944 static void
945 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
946 {
947         unsigned long drvr_flag;
948
949         spin_lock_irqsave(&phba->hbalock, drvr_flag);
950         phba->hb_outstanding = 0;
951         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
952
953         /* Check and reset heart-beat timer is necessary */
954         mempool_free(pmboxq, phba->mbox_mem_pool);
955         if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
956                 !(phba->link_state == LPFC_HBA_ERROR) &&
957                 !(phba->pport->load_flag & FC_UNLOADING))
958                 mod_timer(&phba->hb_tmofunc,
959                         jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
960         return;
961 }
962
963 /**
964  * lpfc_hb_timeout_handler - The HBA-timer timeout handler
965  * @phba: pointer to lpfc hba data structure.
966  *
967  * This is the actual HBA-timer timeout handler to be invoked by the worker
968  * thread whenever the HBA timer fired and HBA-timeout event posted. This
969  * handler performs any periodic operations needed for the device. If such
970  * periodic event has already been attended to either in the interrupt handler
971  * or by processing slow-ring or fast-ring events within the HBA-timer
972  * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
973  * the timer for the next timeout period. If lpfc heart-beat mailbox command
974  * is configured and there is no heart-beat mailbox command outstanding, a
975  * heart-beat mailbox is issued and timer set properly. Otherwise, if there
976  * has been a heart-beat mailbox command outstanding, the HBA shall be put
977  * to offline.
978  **/
979 void
980 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
981 {
982         struct lpfc_vport **vports;
983         LPFC_MBOXQ_t *pmboxq;
984         struct lpfc_dmabuf *buf_ptr;
985         int retval, i;
986         struct lpfc_sli *psli = &phba->sli;
987         LIST_HEAD(completions);
988
989         vports = lpfc_create_vport_work_array(phba);
990         if (vports != NULL)
991                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
992                         lpfc_rcv_seq_check_edtov(vports[i]);
993         lpfc_destroy_vport_work_array(phba, vports);
994
995         if ((phba->link_state == LPFC_HBA_ERROR) ||
996                 (phba->pport->load_flag & FC_UNLOADING) ||
997                 (phba->pport->fc_flag & FC_OFFLINE_MODE))
998                 return;
999
1000         spin_lock_irq(&phba->pport->work_port_lock);
1001
1002         if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1003                 jiffies)) {
1004                 spin_unlock_irq(&phba->pport->work_port_lock);
1005                 if (!phba->hb_outstanding)
1006                         mod_timer(&phba->hb_tmofunc,
1007                                 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1008                 else
1009                         mod_timer(&phba->hb_tmofunc,
1010                                 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1011                 return;
1012         }
1013         spin_unlock_irq(&phba->pport->work_port_lock);
1014
1015         if (phba->elsbuf_cnt &&
1016                 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1017                 spin_lock_irq(&phba->hbalock);
1018                 list_splice_init(&phba->elsbuf, &completions);
1019                 phba->elsbuf_cnt = 0;
1020                 phba->elsbuf_prev_cnt = 0;
1021                 spin_unlock_irq(&phba->hbalock);
1022
1023                 while (!list_empty(&completions)) {
1024                         list_remove_head(&completions, buf_ptr,
1025                                 struct lpfc_dmabuf, list);
1026                         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1027                         kfree(buf_ptr);
1028                 }
1029         }
1030         phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1031
1032         /* If there is no heart beat outstanding, issue a heartbeat command */
1033         if (phba->cfg_enable_hba_heartbeat) {
1034                 if (!phba->hb_outstanding) {
1035                         if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1036                                 (list_empty(&psli->mboxq))) {
1037                                 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1038                                                         GFP_KERNEL);
1039                                 if (!pmboxq) {
1040                                         mod_timer(&phba->hb_tmofunc,
1041                                                  jiffies +
1042                                                  HZ * LPFC_HB_MBOX_INTERVAL);
1043                                         return;
1044                                 }
1045
1046                                 lpfc_heart_beat(phba, pmboxq);
1047                                 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1048                                 pmboxq->vport = phba->pport;
1049                                 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1050                                                 MBX_NOWAIT);
1051
1052                                 if (retval != MBX_BUSY &&
1053                                         retval != MBX_SUCCESS) {
1054                                         mempool_free(pmboxq,
1055                                                         phba->mbox_mem_pool);
1056                                         mod_timer(&phba->hb_tmofunc,
1057                                                 jiffies +
1058                                                 HZ * LPFC_HB_MBOX_INTERVAL);
1059                                         return;
1060                                 }
1061                                 phba->skipped_hb = 0;
1062                                 phba->hb_outstanding = 1;
1063                         } else if (time_before_eq(phba->last_completion_time,
1064                                         phba->skipped_hb)) {
1065                                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1066                                         "2857 Last completion time not "
1067                                         " updated in %d ms\n",
1068                                         jiffies_to_msecs(jiffies
1069                                                  - phba->last_completion_time));
1070                         } else
1071                                 phba->skipped_hb = jiffies;
1072
1073                         mod_timer(&phba->hb_tmofunc,
1074                                   jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1075                         return;
1076                 } else {
1077                         /*
1078                         * If heart beat timeout called with hb_outstanding set
1079                         * we need to take the HBA offline.
1080                         */
1081                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1082                                         "0459 Adapter heartbeat failure, "
1083                                         "taking this port offline.\n");
1084
1085                         spin_lock_irq(&phba->hbalock);
1086                         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1087                         spin_unlock_irq(&phba->hbalock);
1088
1089                         lpfc_offline_prep(phba);
1090                         lpfc_offline(phba);
1091                         lpfc_unblock_mgmt_io(phba);
1092                         phba->link_state = LPFC_HBA_ERROR;
1093                         lpfc_hba_down_post(phba);
1094                 }
1095         }
1096 }
1097
1098 /**
1099  * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
1100  * @phba: pointer to lpfc hba data structure.
1101  *
1102  * This routine is called to bring the HBA offline when HBA hardware error
1103  * other than Port Error 6 has been detected.
1104  **/
1105 static void
1106 lpfc_offline_eratt(struct lpfc_hba *phba)
1107 {
1108         struct lpfc_sli   *psli = &phba->sli;
1109
1110         spin_lock_irq(&phba->hbalock);
1111         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1112         spin_unlock_irq(&phba->hbalock);
1113         lpfc_offline_prep(phba);
1114
1115         lpfc_offline(phba);
1116         lpfc_reset_barrier(phba);
1117         spin_lock_irq(&phba->hbalock);
1118         lpfc_sli_brdreset(phba);
1119         spin_unlock_irq(&phba->hbalock);
1120         lpfc_hba_down_post(phba);
1121         lpfc_sli_brdready(phba, HS_MBRDY);
1122         lpfc_unblock_mgmt_io(phba);
1123         phba->link_state = LPFC_HBA_ERROR;
1124         return;
1125 }
1126
1127 /**
1128  * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1129  * @phba: pointer to lpfc hba data structure.
1130  *
1131  * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1132  * other than Port Error 6 has been detected.
1133  **/
1134 static void
1135 lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1136 {
1137         lpfc_offline_prep(phba);
1138         lpfc_offline(phba);
1139         lpfc_sli4_brdreset(phba);
1140         lpfc_hba_down_post(phba);
1141         lpfc_sli4_post_status_check(phba);
1142         lpfc_unblock_mgmt_io(phba);
1143         phba->link_state = LPFC_HBA_ERROR;
1144 }
1145
1146 /**
1147  * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1148  * @phba: pointer to lpfc hba data structure.
1149  *
1150  * This routine is invoked to handle the deferred HBA hardware error
1151  * conditions. This type of error is indicated by HBA by setting ER1
1152  * and another ER bit in the host status register. The driver will
1153  * wait until the ER1 bit clears before handling the error condition.
1154  **/
1155 static void
1156 lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1157 {
1158         uint32_t old_host_status = phba->work_hs;
1159         struct lpfc_sli_ring  *pring;
1160         struct lpfc_sli *psli = &phba->sli;
1161
1162         /* If the pci channel is offline, ignore possible errors,
1163          * since we cannot communicate with the pci card anyway.
1164          */
1165         if (pci_channel_offline(phba->pcidev)) {
1166                 spin_lock_irq(&phba->hbalock);
1167                 phba->hba_flag &= ~DEFER_ERATT;
1168                 spin_unlock_irq(&phba->hbalock);
1169                 return;
1170         }
1171
1172         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1173                 "0479 Deferred Adapter Hardware Error "
1174                 "Data: x%x x%x x%x\n",
1175                 phba->work_hs,
1176                 phba->work_status[0], phba->work_status[1]);
1177
1178         spin_lock_irq(&phba->hbalock);
1179         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1180         spin_unlock_irq(&phba->hbalock);
1181
1182
1183         /*
1184          * Firmware stops when it triggred erratt. That could cause the I/Os
1185          * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1186          * SCSI layer retry it after re-establishing link.
1187          */
1188         pring = &psli->ring[psli->fcp_ring];
1189         lpfc_sli_abort_iocb_ring(phba, pring);
1190
1191         /*
1192          * There was a firmware error. Take the hba offline and then
1193          * attempt to restart it.
1194          */
1195         lpfc_offline_prep(phba);
1196         lpfc_offline(phba);
1197
1198         /* Wait for the ER1 bit to clear.*/
1199         while (phba->work_hs & HS_FFER1) {
1200                 msleep(100);
1201                 phba->work_hs = readl(phba->HSregaddr);
1202                 /* If driver is unloading let the worker thread continue */
1203                 if (phba->pport->load_flag & FC_UNLOADING) {
1204                         phba->work_hs = 0;
1205                         break;
1206                 }
1207         }
1208
1209         /*
1210          * This is to ptrotect against a race condition in which
1211          * first write to the host attention register clear the
1212          * host status register.
1213          */
1214         if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1215                 phba->work_hs = old_host_status & ~HS_FFER1;
1216
1217         spin_lock_irq(&phba->hbalock);
1218         phba->hba_flag &= ~DEFER_ERATT;
1219         spin_unlock_irq(&phba->hbalock);
1220         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1221         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1222 }
1223
1224 static void
1225 lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1226 {
1227         struct lpfc_board_event_header board_event;
1228         struct Scsi_Host *shost;
1229
1230         board_event.event_type = FC_REG_BOARD_EVENT;
1231         board_event.subcategory = LPFC_EVENT_PORTINTERR;
1232         shost = lpfc_shost_from_vport(phba->pport);
1233         fc_host_post_vendor_event(shost, fc_get_event_number(),
1234                                   sizeof(board_event),
1235                                   (char *) &board_event,
1236                                   LPFC_NL_VENDOR_ID);
1237 }
1238
1239 /**
1240  * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1241  * @phba: pointer to lpfc hba data structure.
1242  *
1243  * This routine is invoked to handle the following HBA hardware error
1244  * conditions:
1245  * 1 - HBA error attention interrupt
1246  * 2 - DMA ring index out of range
1247  * 3 - Mailbox command came back as unknown
1248  **/
1249 static void
1250 lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1251 {
1252         struct lpfc_vport *vport = phba->pport;
1253         struct lpfc_sli   *psli = &phba->sli;
1254         struct lpfc_sli_ring  *pring;
1255         uint32_t event_data;
1256         unsigned long temperature;
1257         struct temp_event temp_event_data;
1258         struct Scsi_Host  *shost;
1259
1260         /* If the pci channel is offline, ignore possible errors,
1261          * since we cannot communicate with the pci card anyway.
1262          */
1263         if (pci_channel_offline(phba->pcidev)) {
1264                 spin_lock_irq(&phba->hbalock);
1265                 phba->hba_flag &= ~DEFER_ERATT;
1266                 spin_unlock_irq(&phba->hbalock);
1267                 return;
1268         }
1269
1270         /* If resets are disabled then leave the HBA alone and return */
1271         if (!phba->cfg_enable_hba_reset)
1272                 return;
1273
1274         /* Send an internal error event to mgmt application */
1275         lpfc_board_errevt_to_mgmt(phba);
1276
1277         if (phba->hba_flag & DEFER_ERATT)
1278                 lpfc_handle_deferred_eratt(phba);
1279
1280         if (phba->work_hs & HS_FFER6) {
1281                 /* Re-establishing Link */
1282                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1283                                 "1301 Re-establishing Link "
1284                                 "Data: x%x x%x x%x\n",
1285                                 phba->work_hs,
1286                                 phba->work_status[0], phba->work_status[1]);
1287
1288                 spin_lock_irq(&phba->hbalock);
1289                 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1290                 spin_unlock_irq(&phba->hbalock);
1291
1292                 /*
1293                 * Firmware stops when it triggled erratt with HS_FFER6.
1294                 * That could cause the I/Os dropped by the firmware.
1295                 * Error iocb (I/O) on txcmplq and let the SCSI layer
1296                 * retry it after re-establishing link.
1297                 */
1298                 pring = &psli->ring[psli->fcp_ring];
1299                 lpfc_sli_abort_iocb_ring(phba, pring);
1300
1301                 /*
1302                  * There was a firmware error.  Take the hba offline and then
1303                  * attempt to restart it.
1304                  */
1305                 lpfc_offline_prep(phba);
1306                 lpfc_offline(phba);
1307                 lpfc_sli_brdrestart(phba);
1308                 if (lpfc_online(phba) == 0) {   /* Initialize the HBA */
1309                         lpfc_unblock_mgmt_io(phba);
1310                         return;
1311                 }
1312                 lpfc_unblock_mgmt_io(phba);
1313         } else if (phba->work_hs & HS_CRIT_TEMP) {
1314                 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1315                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1316                 temp_event_data.event_code = LPFC_CRIT_TEMP;
1317                 temp_event_data.data = (uint32_t)temperature;
1318
1319                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1320                                 "0406 Adapter maximum temperature exceeded "
1321                                 "(%ld), taking this port offline "
1322                                 "Data: x%x x%x x%x\n",
1323                                 temperature, phba->work_hs,
1324                                 phba->work_status[0], phba->work_status[1]);
1325
1326                 shost = lpfc_shost_from_vport(phba->pport);
1327                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1328                                           sizeof(temp_event_data),
1329                                           (char *) &temp_event_data,
1330                                           SCSI_NL_VID_TYPE_PCI
1331                                           | PCI_VENDOR_ID_EMULEX);
1332
1333                 spin_lock_irq(&phba->hbalock);
1334                 phba->over_temp_state = HBA_OVER_TEMP;
1335                 spin_unlock_irq(&phba->hbalock);
1336                 lpfc_offline_eratt(phba);
1337
1338         } else {
1339                 /* The if clause above forces this code path when the status
1340                  * failure is a value other than FFER6. Do not call the offline
1341                  * twice. This is the adapter hardware error path.
1342                  */
1343                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1344                                 "0457 Adapter Hardware Error "
1345                                 "Data: x%x x%x x%x\n",
1346                                 phba->work_hs,
1347                                 phba->work_status[0], phba->work_status[1]);
1348
1349                 event_data = FC_REG_DUMP_EVENT;
1350                 shost = lpfc_shost_from_vport(vport);
1351                 fc_host_post_vendor_event(shost, fc_get_event_number(),
1352                                 sizeof(event_data), (char *) &event_data,
1353                                 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1354
1355                 lpfc_offline_eratt(phba);
1356         }
1357         return;
1358 }
1359
1360 /**
1361  * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1362  * @phba: pointer to lpfc hba data structure.
1363  *
1364  * This routine is invoked to handle the SLI4 HBA hardware error attention
1365  * conditions.
1366  **/
1367 static void
1368 lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1369 {
1370         struct lpfc_vport *vport = phba->pport;
1371         uint32_t event_data;
1372         struct Scsi_Host *shost;
1373
1374         /* If the pci channel is offline, ignore possible errors, since
1375          * we cannot communicate with the pci card anyway.
1376          */
1377         if (pci_channel_offline(phba->pcidev))
1378                 return;
1379         /* If resets are disabled then leave the HBA alone and return */
1380         if (!phba->cfg_enable_hba_reset)
1381                 return;
1382
1383         /* Send an internal error event to mgmt application */
1384         lpfc_board_errevt_to_mgmt(phba);
1385
1386         /* For now, the actual action for SLI4 device handling is not
1387          * specified yet, just treated it as adaptor hardware failure
1388          */
1389         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1390                         "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1391                         phba->work_status[0], phba->work_status[1]);
1392
1393         event_data = FC_REG_DUMP_EVENT;
1394         shost = lpfc_shost_from_vport(vport);
1395         fc_host_post_vendor_event(shost, fc_get_event_number(),
1396                                   sizeof(event_data), (char *) &event_data,
1397                                   SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1398
1399         lpfc_sli4_offline_eratt(phba);
1400 }
1401
1402 /**
1403  * lpfc_handle_eratt - Wrapper func for handling hba error attention
1404  * @phba: pointer to lpfc HBA data structure.
1405  *
1406  * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1407  * routine from the API jump table function pointer from the lpfc_hba struct.
1408  *
1409  * Return codes
1410  *   0 - success.
1411  *   Any other value - error.
1412  **/
1413 void
1414 lpfc_handle_eratt(struct lpfc_hba *phba)
1415 {
1416         (*phba->lpfc_handle_eratt)(phba);
1417 }
1418
1419 /**
1420  * lpfc_handle_latt - The HBA link event handler
1421  * @phba: pointer to lpfc hba data structure.
1422  *
1423  * This routine is invoked from the worker thread to handle a HBA host
1424  * attention link event.
1425  **/
1426 void
1427 lpfc_handle_latt(struct lpfc_hba *phba)
1428 {
1429         struct lpfc_vport *vport = phba->pport;
1430         struct lpfc_sli   *psli = &phba->sli;
1431         LPFC_MBOXQ_t *pmb;
1432         volatile uint32_t control;
1433         struct lpfc_dmabuf *mp;
1434         int rc = 0;
1435
1436         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1437         if (!pmb) {
1438                 rc = 1;
1439                 goto lpfc_handle_latt_err_exit;
1440         }
1441
1442         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1443         if (!mp) {
1444                 rc = 2;
1445                 goto lpfc_handle_latt_free_pmb;
1446         }
1447
1448         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
1449         if (!mp->virt) {
1450                 rc = 3;
1451                 goto lpfc_handle_latt_free_mp;
1452         }
1453
1454         /* Cleanup any outstanding ELS commands */
1455         lpfc_els_flush_all_cmd(phba);
1456
1457         psli->slistat.link_event++;
1458         lpfc_read_la(phba, pmb, mp);
1459         pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
1460         pmb->vport = vport;
1461         /* Block ELS IOCBs until we have processed this mbox command */
1462         phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
1463         rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
1464         if (rc == MBX_NOT_FINISHED) {
1465                 rc = 4;
1466                 goto lpfc_handle_latt_free_mbuf;
1467         }
1468
1469         /* Clear Link Attention in HA REG */
1470         spin_lock_irq(&phba->hbalock);
1471         writel(HA_LATT, phba->HAregaddr);
1472         readl(phba->HAregaddr); /* flush */
1473         spin_unlock_irq(&phba->hbalock);
1474
1475         return;
1476
1477 lpfc_handle_latt_free_mbuf:
1478         phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1479         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1480 lpfc_handle_latt_free_mp:
1481         kfree(mp);
1482 lpfc_handle_latt_free_pmb:
1483         mempool_free(pmb, phba->mbox_mem_pool);
1484 lpfc_handle_latt_err_exit:
1485         /* Enable Link attention interrupts */
1486         spin_lock_irq(&phba->hbalock);
1487         psli->sli_flag |= LPFC_PROCESS_LA;
1488         control = readl(phba->HCregaddr);
1489         control |= HC_LAINT_ENA;
1490         writel(control, phba->HCregaddr);
1491         readl(phba->HCregaddr); /* flush */
1492
1493         /* Clear Link Attention in HA REG */
1494         writel(HA_LATT, phba->HAregaddr);
1495         readl(phba->HAregaddr); /* flush */
1496         spin_unlock_irq(&phba->hbalock);
1497         lpfc_linkdown(phba);
1498         phba->link_state = LPFC_HBA_ERROR;
1499
1500         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1501                      "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
1502
1503         return;
1504 }
1505
1506 /**
1507  * lpfc_parse_vpd - Parse VPD (Vital Product Data)
1508  * @phba: pointer to lpfc hba data structure.
1509  * @vpd: pointer to the vital product data.
1510  * @len: length of the vital product data in bytes.
1511  *
1512  * This routine parses the Vital Product Data (VPD). The VPD is treated as
1513  * an array of characters. In this routine, the ModelName, ProgramType, and
1514  * ModelDesc, etc. fields of the phba data structure will be populated.
1515  *
1516  * Return codes
1517  *   0 - pointer to the VPD passed in is NULL
1518  *   1 - success
1519  **/
1520 int
1521 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
1522 {
1523         uint8_t lenlo, lenhi;
1524         int Length;
1525         int i, j;
1526         int finished = 0;
1527         int index = 0;
1528
1529         if (!vpd)
1530                 return 0;
1531
1532         /* Vital Product */
1533         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1534                         "0455 Vital Product Data: x%x x%x x%x x%x\n",
1535                         (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1536                         (uint32_t) vpd[3]);
1537         while (!finished && (index < (len - 4))) {
1538                 switch (vpd[index]) {
1539                 case 0x82:
1540                 case 0x91:
1541                         index += 1;
1542                         lenlo = vpd[index];
1543                         index += 1;
1544                         lenhi = vpd[index];
1545                         index += 1;
1546                         i = ((((unsigned short)lenhi) << 8) + lenlo);
1547                         index += i;
1548                         break;
1549                 case 0x90:
1550                         index += 1;
1551                         lenlo = vpd[index];
1552                         index += 1;
1553                         lenhi = vpd[index];
1554                         index += 1;
1555                         Length = ((((unsigned short)lenhi) << 8) + lenlo);
1556                         if (Length > len - index)
1557                                 Length = len - index;
1558                         while (Length > 0) {
1559                         /* Look for Serial Number */
1560                         if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1561                                 index += 2;
1562                                 i = vpd[index];
1563                                 index += 1;
1564                                 j = 0;
1565                                 Length -= (3+i);
1566                                 while(i--) {
1567                                         phba->SerialNumber[j++] = vpd[index++];
1568                                         if (j == 31)
1569                                                 break;
1570                                 }
1571                                 phba->SerialNumber[j] = 0;
1572                                 continue;
1573                         }
1574                         else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1575                                 phba->vpd_flag |= VPD_MODEL_DESC;
1576                                 index += 2;
1577                                 i = vpd[index];
1578                                 index += 1;
1579                                 j = 0;
1580                                 Length -= (3+i);
1581                                 while(i--) {
1582                                         phba->ModelDesc[j++] = vpd[index++];
1583                                         if (j == 255)
1584                                                 break;
1585                                 }
1586                                 phba->ModelDesc[j] = 0;
1587                                 continue;
1588                         }
1589                         else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1590                                 phba->vpd_flag |= VPD_MODEL_NAME;
1591                                 index += 2;
1592                                 i = vpd[index];
1593                                 index += 1;
1594                                 j = 0;
1595                                 Length -= (3+i);
1596                                 while(i--) {
1597                                         phba->ModelName[j++] = vpd[index++];
1598                                         if (j == 79)
1599                                                 break;
1600                                 }
1601                                 phba->ModelName[j] = 0;
1602                                 continue;
1603                         }
1604                         else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1605                                 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1606                                 index += 2;
1607                                 i = vpd[index];
1608                                 index += 1;
1609                                 j = 0;
1610                                 Length -= (3+i);
1611                                 while(i--) {
1612                                         phba->ProgramType[j++] = vpd[index++];
1613                                         if (j == 255)
1614                                                 break;
1615                                 }
1616                                 phba->ProgramType[j] = 0;
1617                                 continue;
1618                         }
1619                         else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1620                                 phba->vpd_flag |= VPD_PORT;
1621                                 index += 2;
1622                                 i = vpd[index];
1623                                 index += 1;
1624                                 j = 0;
1625                                 Length -= (3+i);
1626                                 while(i--) {
1627                                 phba->Port[j++] = vpd[index++];
1628                                 if (j == 19)
1629                                         break;
1630                                 }
1631                                 phba->Port[j] = 0;
1632                                 continue;
1633                         }
1634                         else {
1635                                 index += 2;
1636                                 i = vpd[index];
1637                                 index += 1;
1638                                 index += i;
1639                                 Length -= (3 + i);
1640                         }
1641                 }
1642                 finished = 0;
1643                 break;
1644                 case 0x78:
1645                         finished = 1;
1646                         break;
1647                 default:
1648                         index ++;
1649                         break;
1650                 }
1651         }
1652
1653         return(1);
1654 }
1655
1656 /**
1657  * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
1658  * @phba: pointer to lpfc hba data structure.
1659  * @mdp: pointer to the data structure to hold the derived model name.
1660  * @descp: pointer to the data structure to hold the derived description.
1661  *
1662  * This routine retrieves HBA's description based on its registered PCI device
1663  * ID. The @descp passed into this function points to an array of 256 chars. It
1664  * shall be returned with the model name, maximum speed, and the host bus type.
1665  * The @mdp passed into this function points to an array of 80 chars. When the
1666  * function returns, the @mdp will be filled with the model name.
1667  **/
1668 static void
1669 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1670 {
1671         lpfc_vpd_t *vp;
1672         uint16_t dev_id = phba->pcidev->device;
1673         int max_speed;
1674         int GE = 0;
1675         int oneConnect = 0; /* default is not a oneConnect */
1676         struct {
1677                 char *name;
1678                 char *bus;
1679                 char *function;
1680         } m = {"<Unknown>", "", ""};
1681
1682         if (mdp && mdp[0] != '\0'
1683                 && descp && descp[0] != '\0')
1684                 return;
1685
1686         if (phba->lmt & LMT_10Gb)
1687                 max_speed = 10;
1688         else if (phba->lmt & LMT_8Gb)
1689                 max_speed = 8;
1690         else if (phba->lmt & LMT_4Gb)
1691                 max_speed = 4;
1692         else if (phba->lmt & LMT_2Gb)
1693                 max_speed = 2;
1694         else
1695                 max_speed = 1;
1696
1697         vp = &phba->vpd;
1698
1699         switch (dev_id) {
1700         case PCI_DEVICE_ID_FIREFLY:
1701                 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
1702                 break;
1703         case PCI_DEVICE_ID_SUPERFLY:
1704                 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1705                         m = (typeof(m)){"LP7000", "PCI",
1706                                         "Fibre Channel Adapter"};
1707                 else
1708                         m = (typeof(m)){"LP7000E", "PCI",
1709                                         "Fibre Channel Adapter"};
1710                 break;
1711         case PCI_DEVICE_ID_DRAGONFLY:
1712                 m = (typeof(m)){"LP8000", "PCI",
1713                                 "Fibre Channel Adapter"};
1714                 break;
1715         case PCI_DEVICE_ID_CENTAUR:
1716                 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1717                         m = (typeof(m)){"LP9002", "PCI",
1718                                         "Fibre Channel Adapter"};
1719                 else
1720                         m = (typeof(m)){"LP9000", "PCI",
1721                                         "Fibre Channel Adapter"};
1722                 break;
1723         case PCI_DEVICE_ID_RFLY:
1724                 m = (typeof(m)){"LP952", "PCI",
1725                                 "Fibre Channel Adapter"};
1726                 break;
1727         case PCI_DEVICE_ID_PEGASUS:
1728                 m = (typeof(m)){"LP9802", "PCI-X",
1729                                 "Fibre Channel Adapter"};
1730                 break;
1731         case PCI_DEVICE_ID_THOR:
1732                 m = (typeof(m)){"LP10000", "PCI-X",
1733                                 "Fibre Channel Adapter"};
1734                 break;
1735         case PCI_DEVICE_ID_VIPER:
1736                 m = (typeof(m)){"LPX1000",  "PCI-X",
1737                                 "Fibre Channel Adapter"};
1738                 break;
1739         case PCI_DEVICE_ID_PFLY:
1740                 m = (typeof(m)){"LP982", "PCI-X",
1741                                 "Fibre Channel Adapter"};
1742                 break;
1743         case PCI_DEVICE_ID_TFLY:
1744                 m = (typeof(m)){"LP1050", "PCI-X",
1745                                 "Fibre Channel Adapter"};
1746                 break;
1747         case PCI_DEVICE_ID_HELIOS:
1748                 m = (typeof(m)){"LP11000", "PCI-X2",
1749                                 "Fibre Channel Adapter"};
1750                 break;
1751         case PCI_DEVICE_ID_HELIOS_SCSP:
1752                 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1753                                 "Fibre Channel Adapter"};
1754                 break;
1755         case PCI_DEVICE_ID_HELIOS_DCSP:
1756                 m = (typeof(m)){"LP11002-SP",  "PCI-X2",
1757                                 "Fibre Channel Adapter"};
1758                 break;
1759         case PCI_DEVICE_ID_NEPTUNE:
1760                 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
1761                 break;
1762         case PCI_DEVICE_ID_NEPTUNE_SCSP:
1763                 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
1764                 break;
1765         case PCI_DEVICE_ID_NEPTUNE_DCSP:
1766                 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
1767                 break;
1768         case PCI_DEVICE_ID_BMID:
1769                 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
1770                 break;
1771         case PCI_DEVICE_ID_BSMB:
1772                 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
1773                 break;
1774         case PCI_DEVICE_ID_ZEPHYR:
1775                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1776                 break;
1777         case PCI_DEVICE_ID_ZEPHYR_SCSP:
1778                 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1779                 break;
1780         case PCI_DEVICE_ID_ZEPHYR_DCSP:
1781                 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
1782                 GE = 1;
1783                 break;
1784         case PCI_DEVICE_ID_ZMID:
1785                 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
1786                 break;
1787         case PCI_DEVICE_ID_ZSMB:
1788                 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
1789                 break;
1790         case PCI_DEVICE_ID_LP101:
1791                 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
1792                 break;
1793         case PCI_DEVICE_ID_LP10000S:
1794                 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
1795                 break;
1796         case PCI_DEVICE_ID_LP11000S:
1797                 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
1798                 break;
1799         case PCI_DEVICE_ID_LPE11000S:
1800                 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
1801                 break;
1802         case PCI_DEVICE_ID_SAT:
1803                 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
1804                 break;
1805         case PCI_DEVICE_ID_SAT_MID:
1806                 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
1807                 break;
1808         case PCI_DEVICE_ID_SAT_SMB:
1809                 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
1810                 break;
1811         case PCI_DEVICE_ID_SAT_DCSP:
1812                 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
1813                 break;
1814         case PCI_DEVICE_ID_SAT_SCSP:
1815                 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
1816                 break;
1817         case PCI_DEVICE_ID_SAT_S:
1818                 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
1819                 break;
1820         case PCI_DEVICE_ID_HORNET:
1821                 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
1822                 GE = 1;
1823                 break;
1824         case PCI_DEVICE_ID_PROTEUS_VF:
1825                 m = (typeof(m)){"LPev12000", "PCIe IOV",
1826                                 "Fibre Channel Adapter"};
1827                 break;
1828         case PCI_DEVICE_ID_PROTEUS_PF:
1829                 m = (typeof(m)){"LPev12000", "PCIe IOV",
1830                                 "Fibre Channel Adapter"};
1831                 break;
1832         case PCI_DEVICE_ID_PROTEUS_S:
1833                 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1834                                 "Fibre Channel Adapter"};
1835                 break;
1836         case PCI_DEVICE_ID_TIGERSHARK:
1837                 oneConnect = 1;
1838                 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
1839                 break;
1840         case PCI_DEVICE_ID_TOMCAT:
1841                 oneConnect = 1;
1842                 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1843                 break;
1844         case PCI_DEVICE_ID_FALCON:
1845                 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1846                                 "EmulexSecure Fibre"};
1847                 break;
1848         case PCI_DEVICE_ID_BALIUS:
1849                 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
1850                                 "Fibre Channel Adapter"};
1851                 break;
1852         default:
1853                 m = (typeof(m)){"Unknown", "", ""};
1854                 break;
1855         }
1856
1857         if (mdp && mdp[0] == '\0')
1858                 snprintf(mdp, 79,"%s", m.name);
1859         /* oneConnect hba requires special processing, they are all initiators
1860          * and we put the port number on the end
1861          */
1862         if (descp && descp[0] == '\0') {
1863                 if (oneConnect)
1864                         snprintf(descp, 255,
1865                                 "Emulex OneConnect %s, %s Initiator, Port %s",
1866                                 m.name, m.function,
1867                                 phba->Port);
1868                 else
1869                         snprintf(descp, 255,
1870                                 "Emulex %s %d%s %s %s",
1871                                 m.name, max_speed, (GE) ? "GE" : "Gb",
1872                                 m.bus, m.function);
1873         }
1874 }
1875
1876 /**
1877  * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
1878  * @phba: pointer to lpfc hba data structure.
1879  * @pring: pointer to a IOCB ring.
1880  * @cnt: the number of IOCBs to be posted to the IOCB ring.
1881  *
1882  * This routine posts a given number of IOCBs with the associated DMA buffer
1883  * descriptors specified by the cnt argument to the given IOCB ring.
1884  *
1885  * Return codes
1886  *   The number of IOCBs NOT able to be posted to the IOCB ring.
1887  **/
1888 int
1889 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
1890 {
1891         IOCB_t *icmd;
1892         struct lpfc_iocbq *iocb;
1893         struct lpfc_dmabuf *mp1, *mp2;
1894
1895         cnt += pring->missbufcnt;
1896
1897         /* While there are buffers to post */
1898         while (cnt > 0) {
1899                 /* Allocate buffer for  command iocb */
1900                 iocb = lpfc_sli_get_iocbq(phba);
1901                 if (iocb == NULL) {
1902                         pring->missbufcnt = cnt;
1903                         return cnt;
1904                 }
1905                 icmd = &iocb->iocb;
1906
1907                 /* 2 buffers can be posted per command */
1908                 /* Allocate buffer to post */
1909                 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1910                 if (mp1)
1911                     mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1912                 if (!mp1 || !mp1->virt) {
1913                         kfree(mp1);
1914                         lpfc_sli_release_iocbq(phba, iocb);
1915                         pring->missbufcnt = cnt;
1916                         return cnt;
1917                 }
1918
1919                 INIT_LIST_HEAD(&mp1->list);
1920                 /* Allocate buffer to post */
1921                 if (cnt > 1) {
1922                         mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1923                         if (mp2)
1924                                 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1925                                                             &mp2->phys);
1926                         if (!mp2 || !mp2->virt) {
1927                                 kfree(mp2);
1928                                 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1929                                 kfree(mp1);
1930                                 lpfc_sli_release_iocbq(phba, iocb);
1931                                 pring->missbufcnt = cnt;
1932                                 return cnt;
1933                         }
1934
1935                         INIT_LIST_HEAD(&mp2->list);
1936                 } else {
1937                         mp2 = NULL;
1938                 }
1939
1940                 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1941                 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1942                 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1943                 icmd->ulpBdeCount = 1;
1944                 cnt--;
1945                 if (mp2) {
1946                         icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1947                         icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1948                         icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1949                         cnt--;
1950                         icmd->ulpBdeCount = 2;
1951                 }
1952
1953                 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1954                 icmd->ulpLe = 1;
1955
1956                 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1957                     IOCB_ERROR) {
1958                         lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1959                         kfree(mp1);
1960                         cnt++;
1961                         if (mp2) {
1962                                 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1963                                 kfree(mp2);
1964                                 cnt++;
1965                         }
1966                         lpfc_sli_release_iocbq(phba, iocb);
1967                         pring->missbufcnt = cnt;
1968                         return cnt;
1969                 }
1970                 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
1971                 if (mp2)
1972                         lpfc_sli_ringpostbuf_put(phba, pring, mp2);
1973         }
1974         pring->missbufcnt = 0;
1975         return 0;
1976 }
1977
1978 /**
1979  * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
1980  * @phba: pointer to lpfc hba data structure.
1981  *
1982  * This routine posts initial receive IOCB buffers to the ELS ring. The
1983  * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1984  * set to 64 IOCBs.
1985  *
1986  * Return codes
1987  *   0 - success (currently always success)
1988  **/
1989 static int
1990 lpfc_post_rcv_buf(struct lpfc_hba *phba)
1991 {
1992         struct lpfc_sli *psli = &phba->sli;
1993
1994         /* Ring 0, ELS / CT buffers */
1995         lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
1996         /* Ring 2 - FCP no buffers needed */
1997
1998         return 0;
1999 }
2000
2001 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2002
2003 /**
2004  * lpfc_sha_init - Set up initial array of hash table entries
2005  * @HashResultPointer: pointer to an array as hash table.
2006  *
2007  * This routine sets up the initial values to the array of hash table entries
2008  * for the LC HBAs.
2009  **/
2010 static void
2011 lpfc_sha_init(uint32_t * HashResultPointer)
2012 {
2013         HashResultPointer[0] = 0x67452301;
2014         HashResultPointer[1] = 0xEFCDAB89;
2015         HashResultPointer[2] = 0x98BADCFE;
2016         HashResultPointer[3] = 0x10325476;
2017         HashResultPointer[4] = 0xC3D2E1F0;
2018 }
2019
2020 /**
2021  * lpfc_sha_iterate - Iterate initial hash table with the working hash table
2022  * @HashResultPointer: pointer to an initial/result hash table.
2023  * @HashWorkingPointer: pointer to an working hash table.
2024  *
2025  * This routine iterates an initial hash table pointed by @HashResultPointer
2026  * with the values from the working hash table pointeed by @HashWorkingPointer.
2027  * The results are putting back to the initial hash table, returned through
2028  * the @HashResultPointer as the result hash table.
2029  **/
2030 static void
2031 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2032 {
2033         int t;
2034         uint32_t TEMP;
2035         uint32_t A, B, C, D, E;
2036         t = 16;
2037         do {
2038                 HashWorkingPointer[t] =
2039                     S(1,
2040                       HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2041                                                                      8] ^
2042                       HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2043         } while (++t <= 79);
2044         t = 0;
2045         A = HashResultPointer[0];
2046         B = HashResultPointer[1];
2047         C = HashResultPointer[2];
2048         D = HashResultPointer[3];
2049         E = HashResultPointer[4];
2050
2051         do {
2052                 if (t < 20) {
2053                         TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2054                 } else if (t < 40) {
2055                         TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2056                 } else if (t < 60) {
2057                         TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2058                 } else {
2059                         TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2060                 }
2061                 TEMP += S(5, A) + E + HashWorkingPointer[t];
2062                 E = D;
2063                 D = C;
2064                 C = S(30, B);
2065                 B = A;
2066                 A = TEMP;
2067         } while (++t <= 79);
2068
2069         HashResultPointer[0] += A;
2070         HashResultPointer[1] += B;
2071         HashResultPointer[2] += C;
2072         HashResultPointer[3] += D;
2073         HashResultPointer[4] += E;
2074
2075 }
2076
2077 /**
2078  * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
2079  * @RandomChallenge: pointer to the entry of host challenge random number array.
2080  * @HashWorking: pointer to the entry of the working hash array.
2081  *
2082  * This routine calculates the working hash array referred by @HashWorking
2083  * from the challenge random numbers associated with the host, referred by
2084  * @RandomChallenge. The result is put into the entry of the working hash
2085  * array and returned by reference through @HashWorking.
2086  **/
2087 static void
2088 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2089 {
2090         *HashWorking = (*RandomChallenge ^ *HashWorking);
2091 }
2092
2093 /**
2094  * lpfc_hba_init - Perform special handling for LC HBA initialization
2095  * @phba: pointer to lpfc hba data structure.
2096  * @hbainit: pointer to an array of unsigned 32-bit integers.
2097  *
2098  * This routine performs the special handling for LC HBA initialization.
2099  **/
2100 void
2101 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2102 {
2103         int t;
2104         uint32_t *HashWorking;
2105         uint32_t *pwwnn = (uint32_t *) phba->wwnn;
2106
2107         HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
2108         if (!HashWorking)
2109                 return;
2110
2111         HashWorking[0] = HashWorking[78] = *pwwnn++;
2112         HashWorking[1] = HashWorking[79] = *pwwnn;
2113
2114         for (t = 0; t < 7; t++)
2115                 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2116
2117         lpfc_sha_init(hbainit);
2118         lpfc_sha_iterate(hbainit, HashWorking);
2119         kfree(HashWorking);
2120 }
2121
2122 /**
2123  * lpfc_cleanup - Performs vport cleanups before deleting a vport
2124  * @vport: pointer to a virtual N_Port data structure.
2125  *
2126  * This routine performs the necessary cleanups before deleting the @vport.
2127  * It invokes the discovery state machine to perform necessary state
2128  * transitions and to release the ndlps associated with the @vport. Note,
2129  * the physical port is treated as @vport 0.
2130  **/
2131 void
2132 lpfc_cleanup(struct lpfc_vport *vport)
2133 {
2134         struct lpfc_hba   *phba = vport->phba;
2135         struct lpfc_nodelist *ndlp, *next_ndlp;
2136         int i = 0;
2137
2138         if (phba->link_state > LPFC_LINK_DOWN)
2139                 lpfc_port_link_failure(vport);
2140
2141         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2142                 if (!NLP_CHK_NODE_ACT(ndlp)) {
2143                         ndlp = lpfc_enable_node(vport, ndlp,
2144                                                 NLP_STE_UNUSED_NODE);
2145                         if (!ndlp)
2146                                 continue;
2147                         spin_lock_irq(&phba->ndlp_lock);
2148                         NLP_SET_FREE_REQ(ndlp);
2149                         spin_unlock_irq(&phba->ndlp_lock);
2150                         /* Trigger the release of the ndlp memory */
2151                         lpfc_nlp_put(ndlp);
2152                         continue;
2153                 }
2154                 spin_lock_irq(&phba->ndlp_lock);
2155                 if (NLP_CHK_FREE_REQ(ndlp)) {
2156                         /* The ndlp should not be in memory free mode already */
2157                         spin_unlock_irq(&phba->ndlp_lock);
2158                         continue;
2159                 } else
2160                         /* Indicate request for freeing ndlp memory */
2161                         NLP_SET_FREE_REQ(ndlp);
2162                 spin_unlock_irq(&phba->ndlp_lock);
2163
2164                 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2165                     ndlp->nlp_DID == Fabric_DID) {
2166                         /* Just free up ndlp with Fabric_DID for vports */
2167                         lpfc_nlp_put(ndlp);
2168                         continue;
2169                 }
2170
2171                 if (ndlp->nlp_type & NLP_FABRIC)
2172                         lpfc_disc_state_machine(vport, ndlp, NULL,
2173                                         NLP_EVT_DEVICE_RECOVERY);
2174
2175                 lpfc_disc_state_machine(vport, ndlp, NULL,
2176                                              NLP_EVT_DEVICE_RM);
2177
2178         }
2179
2180         /* At this point, ALL ndlp's should be gone
2181          * because of the previous NLP_EVT_DEVICE_RM.
2182          * Lets wait for this to happen, if needed.
2183          */
2184         while (!list_empty(&vport->fc_nodes)) {
2185                 if (i++ > 3000) {
2186                         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2187                                 "0233 Nodelist not empty\n");
2188                         list_for_each_entry_safe(ndlp, next_ndlp,
2189                                                 &vport->fc_nodes, nlp_listp) {
2190                                 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2191                                                 LOG_NODE,
2192                                                 "0282 did:x%x ndlp:x%p "
2193                                                 "usgmap:x%x refcnt:%d\n",
2194                                                 ndlp->nlp_DID, (void *)ndlp,
2195                                                 ndlp->nlp_usg_map,
2196                                                 atomic_read(
2197                                                         &ndlp->kref.refcount));
2198                         }
2199                         break;
2200                 }
2201
2202                 /* Wait for any activity on ndlps to settle */
2203                 msleep(10);
2204         }
2205 }
2206
2207 /**
2208  * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2209  * @vport: pointer to a virtual N_Port data structure.
2210  *
2211  * This routine stops all the timers associated with a @vport. This function
2212  * is invoked before disabling or deleting a @vport. Note that the physical
2213  * port is treated as @vport 0.
2214  **/
2215 void
2216 lpfc_stop_vport_timers(struct lpfc_vport *vport)
2217 {
2218         del_timer_sync(&vport->els_tmofunc);
2219         del_timer_sync(&vport->fc_fdmitmo);
2220         lpfc_can_disctmo(vport);
2221         return;
2222 }
2223
2224 /**
2225  * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2226  * @phba: pointer to lpfc hba data structure.
2227  *
2228  * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2229  * caller of this routine should already hold the host lock.
2230  **/
2231 void
2232 __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2233 {
2234         /* Clear pending FCF rediscovery wait and failover in progress flags */
2235         phba->fcf.fcf_flag &= ~(FCF_REDISC_PEND |
2236                                 FCF_DEAD_DISC |
2237                                 FCF_ACVL_DISC);
2238         /* Now, try to stop the timer */
2239         del_timer(&phba->fcf.redisc_wait);
2240 }
2241
2242 /**
2243  * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2244  * @phba: pointer to lpfc hba data structure.
2245  *
2246  * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2247  * checks whether the FCF rediscovery wait timer is pending with the host
2248  * lock held before proceeding with disabling the timer and clearing the
2249  * wait timer pendig flag.
2250  **/
2251 void
2252 lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2253 {
2254         spin_lock_irq(&phba->hbalock);
2255         if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2256                 /* FCF rediscovery timer already fired or stopped */
2257                 spin_unlock_irq(&phba->hbalock);
2258                 return;
2259         }
2260         __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2261         spin_unlock_irq(&phba->hbalock);
2262 }
2263
2264 /**
2265  * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2266  * @phba: pointer to lpfc hba data structure.
2267  *
2268  * This routine stops all the timers associated with a HBA. This function is
2269  * invoked before either putting a HBA offline or unloading the driver.
2270  **/
2271 void
2272 lpfc_stop_hba_timers(struct lpfc_hba *phba)
2273 {
2274         lpfc_stop_vport_timers(phba->pport);
2275         del_timer_sync(&phba->sli.mbox_tmo);
2276         del_timer_sync(&phba->fabric_block_timer);
2277         del_timer_sync(&phba->eratt_poll);
2278         del_timer_sync(&phba->hb_tmofunc);
2279         phba->hb_outstanding = 0;
2280
2281         switch (phba->pci_dev_grp) {
2282         case LPFC_PCI_DEV_LP:
2283                 /* Stop any LightPulse device specific driver timers */
2284                 del_timer_sync(&phba->fcp_poll_timer);
2285                 break;
2286         case LPFC_PCI_DEV_OC:
2287                 /* Stop any OneConnect device sepcific driver timers */
2288                 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2289                 break;
2290         default:
2291                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2292                                 "0297 Invalid device group (x%x)\n",
2293                                 phba->pci_dev_grp);
2294                 break;
2295         }
2296         return;
2297 }
2298
2299 /**
2300  * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2301  * @phba: pointer to lpfc hba data structure.
2302  *
2303  * This routine marks a HBA's management interface as blocked. Once the HBA's
2304  * management interface is marked as blocked, all the user space access to
2305  * the HBA, whether they are from sysfs interface or libdfc interface will
2306  * all be blocked. The HBA is set to block the management interface when the
2307  * driver prepares the HBA interface for online or offline.
2308  **/
2309 static void
2310 lpfc_block_mgmt_io(struct lpfc_hba * phba)
2311 {
2312         unsigned long iflag;
2313         uint8_t actcmd = MBX_HEARTBEAT;
2314         unsigned long timeout;
2315
2316
2317         spin_lock_irqsave(&phba->hbalock, iflag);
2318         phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2319         if (phba->sli.mbox_active)
2320                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
2321         spin_unlock_irqrestore(&phba->hbalock, iflag);
2322         /* Determine how long we might wait for the active mailbox
2323          * command to be gracefully completed by firmware.
2324          */
2325         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
2326                         jiffies;
2327         /* Wait for the outstnading mailbox command to complete */
2328         while (phba->sli.mbox_active) {
2329                 /* Check active mailbox complete status every 2ms */
2330                 msleep(2);
2331                 if (time_after(jiffies, timeout)) {
2332                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2333                                 "2813 Mgmt IO is Blocked %x "
2334                                 "- mbox cmd %x still active\n",
2335                                 phba->sli.sli_flag, actcmd);
2336                         break;
2337                 }
2338         }
2339 }
2340
2341 /**
2342  * lpfc_online - Initialize and bring a HBA online
2343  * @phba: pointer to lpfc hba data structure.
2344  *
2345  * This routine initializes the HBA and brings a HBA online. During this
2346  * process, the management interface is blocked to prevent user space access
2347  * to the HBA interfering with the driver initialization.
2348  *
2349  * Return codes
2350  *   0 - successful
2351  *   1 - failed
2352  **/
2353 int
2354 lpfc_online(struct lpfc_hba *phba)
2355 {
2356         struct lpfc_vport *vport;
2357         struct lpfc_vport **vports;
2358         int i;
2359
2360         if (!phba)
2361                 return 0;
2362         vport = phba->pport;
2363
2364         if (!(vport->fc_flag & FC_OFFLINE_MODE))
2365                 return 0;
2366
2367         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2368                         "0458 Bring Adapter online\n");
2369
2370         lpfc_block_mgmt_io(phba);
2371
2372         if (!lpfc_sli_queue_setup(phba)) {
2373                 lpfc_unblock_mgmt_io(phba);
2374                 return 1;
2375         }
2376
2377         if (phba->sli_rev == LPFC_SLI_REV4) {
2378                 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2379                         lpfc_unblock_mgmt_io(phba);
2380                         return 1;
2381                 }
2382         } else {
2383                 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2384                         lpfc_unblock_mgmt_io(phba);
2385                         return 1;
2386                 }
2387         }
2388
2389         vports = lpfc_create_vport_work_array(phba);
2390         if (vports != NULL)
2391                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2392                         struct Scsi_Host *shost;
2393                         shost = lpfc_shost_from_vport(vports[i]);
2394                         spin_lock_irq(shost->host_lock);
2395                         vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2396                         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2397                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2398                         if (phba->sli_rev == LPFC_SLI_REV4)
2399                                 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
2400                         spin_unlock_irq(shost->host_lock);
2401                 }
2402                 lpfc_destroy_vport_work_array(phba, vports);
2403
2404         lpfc_unblock_mgmt_io(phba);
2405         return 0;
2406 }
2407
2408 /**
2409  * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
2410  * @phba: pointer to lpfc hba data structure.
2411  *
2412  * This routine marks a HBA's management interface as not blocked. Once the
2413  * HBA's management interface is marked as not blocked, all the user space
2414  * access to the HBA, whether they are from sysfs interface or libdfc
2415  * interface will be allowed. The HBA is set to block the management interface
2416  * when the driver prepares the HBA interface for online or offline and then
2417  * set to unblock the management interface afterwards.
2418  **/
2419 void
2420 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2421 {
2422         unsigned long iflag;
2423
2424         spin_lock_irqsave(&phba->hbalock, iflag);
2425         phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2426         spin_unlock_irqrestore(&phba->hbalock, iflag);
2427 }
2428
2429 /**
2430  * lpfc_offline_prep - Prepare a HBA to be brought offline
2431  * @phba: pointer to lpfc hba data structure.
2432  *
2433  * This routine is invoked to prepare a HBA to be brought offline. It performs
2434  * unregistration login to all the nodes on all vports and flushes the mailbox
2435  * queue to make it ready to be brought offline.
2436  **/
2437 void
2438 lpfc_offline_prep(struct lpfc_hba * phba)
2439 {
2440         struct lpfc_vport *vport = phba->pport;
2441         struct lpfc_nodelist  *ndlp, *next_ndlp;
2442         struct lpfc_vport **vports;
2443         struct Scsi_Host *shost;
2444         int i;
2445
2446         if (vport->fc_flag & FC_OFFLINE_MODE)
2447                 return;
2448
2449         lpfc_block_mgmt_io(phba);
2450
2451         lpfc_linkdown(phba);
2452
2453         /* Issue an unreg_login to all nodes on all vports */
2454         vports = lpfc_create_vport_work_array(phba);
2455         if (vports != NULL) {
2456                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2457                         if (vports[i]->load_flag & FC_UNLOADING)
2458                                 continue;
2459                         shost = lpfc_shost_from_vport(vports[i]);
2460                         spin_lock_irq(shost->host_lock);
2461                         vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
2462                         vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2463                         vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
2464                         spin_unlock_irq(shost->host_lock);
2465
2466                         shost = lpfc_shost_from_vport(vports[i]);
2467                         list_for_each_entry_safe(ndlp, next_ndlp,
2468                                                  &vports[i]->fc_nodes,
2469                                                  nlp_listp) {
2470                                 if (!NLP_CHK_NODE_ACT(ndlp))
2471                                         continue;
2472                                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2473                                         continue;
2474                                 if (ndlp->nlp_type & NLP_FABRIC) {
2475                                         lpfc_disc_state_machine(vports[i], ndlp,
2476                                                 NULL, NLP_EVT_DEVICE_RECOVERY);
2477                                         lpfc_disc_state_machine(vports[i], ndlp,
2478                                                 NULL, NLP_EVT_DEVICE_RM);
2479                                 }
2480                                 spin_lock_irq(shost->host_lock);
2481                                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2482                                 spin_unlock_irq(shost->host_lock);
2483                                 lpfc_unreg_rpi(vports[i], ndlp);
2484                         }
2485                 }
2486         }
2487         lpfc_destroy_vport_work_array(phba, vports);
2488
2489         lpfc_sli_mbox_sys_shutdown(phba);
2490 }
2491
2492 /**
2493  * lpfc_offline - Bring a HBA offline
2494  * @phba: pointer to lpfc hba data structure.
2495  *
2496  * This routine actually brings a HBA offline. It stops all the timers
2497  * associated with the HBA, brings down the SLI layer, and eventually
2498  * marks the HBA as in offline state for the upper layer protocol.
2499  **/
2500 void
2501 lpfc_offline(struct lpfc_hba *phba)
2502 {
2503         struct Scsi_Host  *shost;
2504         struct lpfc_vport **vports;
2505         int i;
2506
2507         if (phba->pport->fc_flag & FC_OFFLINE_MODE)
2508                 return;
2509
2510         /* stop port and all timers associated with this hba */
2511         lpfc_stop_port(phba);
2512         vports = lpfc_create_vport_work_array(phba);
2513         if (vports != NULL)
2514                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
2515                         lpfc_stop_vport_timers(vports[i]);
2516         lpfc_destroy_vport_work_array(phba, vports);
2517         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2518                         "0460 Bring Adapter offline\n");
2519         /* Bring down the SLI Layer and cleanup.  The HBA is offline
2520            now.  */
2521         lpfc_sli_hba_down(phba);
2522         spin_lock_irq(&phba->hbalock);
2523         phba->work_ha = 0;
2524         spin_unlock_irq(&phba->hbalock);
2525         vports = lpfc_create_vport_work_array(phba);
2526         if (vports != NULL)
2527                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2528                         shost = lpfc_shost_from_vport(vports[i]);
2529                         spin_lock_irq(shost->host_lock);
2530                         vports[i]->work_port_events = 0;
2531                         vports[i]->fc_flag |= FC_OFFLINE_MODE;
2532                         spin_unlock_irq(shost->host_lock);
2533                 }
2534         lpfc_destroy_vport_work_array(phba, vports);
2535 }
2536
2537 /**
2538  * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
2539  * @phba: pointer to lpfc hba data structure.
2540  *
2541  * This routine is to free all the SCSI buffers and IOCBs from the driver
2542  * list back to kernel. It is called from lpfc_pci_remove_one to free
2543  * the internal resources before the device is removed from the system.
2544  *
2545  * Return codes
2546  *   0 - successful (for now, it always returns 0)
2547  **/
2548 static int
2549 lpfc_scsi_free(struct lpfc_hba *phba)
2550 {
2551         struct lpfc_scsi_buf *sb, *sb_next;
2552         struct lpfc_iocbq *io, *io_next;
2553
2554         spin_lock_irq(&phba->hbalock);
2555         /* Release all the lpfc_scsi_bufs maintained by this host. */
2556         spin_lock(&phba->scsi_buf_list_lock);
2557         list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2558                 list_del(&sb->list);
2559                 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2560                               sb->dma_handle);
2561                 kfree(sb);
2562                 phba->total_scsi_bufs--;
2563         }
2564         spin_unlock(&phba->scsi_buf_list_lock);
2565
2566         /* Release all the lpfc_iocbq entries maintained by this host. */
2567         list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2568                 list_del(&io->list);
2569                 kfree(io);
2570                 phba->total_iocbq_bufs--;
2571         }
2572         spin_unlock_irq(&phba->hbalock);
2573         return 0;
2574 }
2575
2576 /**
2577  * lpfc_create_port - Create an FC port
2578  * @phba: pointer to lpfc hba data structure.
2579  * @instance: a unique integer ID to this FC port.
2580  * @dev: pointer to the device data structure.
2581  *
2582  * This routine creates a FC port for the upper layer protocol. The FC port
2583  * can be created on top of either a physical port or a virtual port provided
2584  * by the HBA. This routine also allocates a SCSI host data structure (shost)
2585  * and associates the FC port created before adding the shost into the SCSI
2586  * layer.
2587  *
2588  * Return codes
2589  *   @vport - pointer to the virtual N_Port data structure.
2590  *   NULL - port create failed.
2591  **/
2592 struct lpfc_vport *
2593 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
2594 {
2595         struct lpfc_vport *vport;
2596         struct Scsi_Host  *shost;
2597         int error = 0;
2598
2599         if (dev != &phba->pcidev->dev)
2600                 shost = scsi_host_alloc(&lpfc_vport_template,
2601                                         sizeof(struct lpfc_vport));
2602         else
2603                 shost = scsi_host_alloc(&lpfc_template,
2604                                         sizeof(struct lpfc_vport));
2605         if (!shost)
2606                 goto out;
2607
2608         vport = (struct lpfc_vport *) shost->hostdata;
2609         vport->phba = phba;
2610         vport->load_flag |= FC_LOADING;
2611         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2612         vport->fc_rscn_flush = 0;
2613
2614         lpfc_get_vport_cfgparam(vport);
2615         shost->unique_id = instance;
2616         shost->max_id = LPFC_MAX_TARGET;
2617         shost->max_lun = vport->cfg_max_luns;
2618         shost->this_id = -1;
2619         shost->max_cmd_len = 16;
2620         if (phba->sli_rev == LPFC_SLI_REV4) {
2621                 shost->dma_boundary =
2622                         phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
2623                 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2624         }
2625
2626         /*
2627          * Set initial can_queue value since 0 is no longer supported and
2628          * scsi_add_host will fail. This will be adjusted later based on the
2629          * max xri value determined in hba setup.
2630          */
2631         shost->can_queue = phba->cfg_hba_queue_depth - 10;
2632         if (dev != &phba->pcidev->dev) {
2633                 shost->transportt = lpfc_vport_transport_template;
2634                 vport->port_type = LPFC_NPIV_PORT;
2635         } else {
2636                 shost->transportt = lpfc_transport_template;
2637                 vport->port_type = LPFC_PHYSICAL_PORT;
2638         }
2639
2640         /* Initialize all internally managed lists. */
2641         INIT_LIST_HEAD(&vport->fc_nodes);
2642         INIT_LIST_HEAD(&vport->rcv_buffer_list);
2643         spin_lock_init(&vport->work_port_lock);
2644
2645         init_timer(&vport->fc_disctmo);
2646         vport->fc_disctmo.function = lpfc_disc_timeout;
2647         vport->fc_disctmo.data = (unsigned long)vport;
2648
2649         init_timer(&vport->fc_fdmitmo);
2650         vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
2651         vport->fc_fdmitmo.data = (unsigned long)vport;
2652
2653         init_timer(&vport->els_tmofunc);
2654         vport->els_tmofunc.function = lpfc_els_timeout;
2655         vport->els_tmofunc.data = (unsigned long)vport;
2656         error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
2657         if (error)
2658                 goto out_put_shost;
2659
2660         spin_lock_irq(&phba->hbalock);
2661         list_add_tail(&vport->listentry, &phba->port_list);
2662         spin_unlock_irq(&phba->hbalock);
2663         return vport;
2664
2665 out_put_shost:
2666         scsi_host_put(shost);
2667 out:
2668         return NULL;
2669 }
2670
2671 /**
2672  * destroy_port -  destroy an FC port
2673  * @vport: pointer to an lpfc virtual N_Port data structure.
2674  *
2675  * This routine destroys a FC port from the upper layer protocol. All the
2676  * resources associated with the port are released.
2677  **/
2678 void
2679 destroy_port(struct lpfc_vport *vport)
2680 {
2681         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2682         struct lpfc_hba  *phba = vport->phba;
2683
2684         lpfc_debugfs_terminate(vport);
2685         fc_remove_host(shost);
2686         scsi_remove_host(shost);
2687
2688         spin_lock_irq(&phba->hbalock);
2689         list_del_init(&vport->listentry);
2690         spin_unlock_irq(&phba->hbalock);
2691
2692         lpfc_cleanup(vport);
2693         return;
2694 }
2695
2696 /**
2697  * lpfc_get_instance - Get a unique integer ID
2698  *
2699  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2700  * uses the kernel idr facility to perform the task.
2701  *
2702  * Return codes:
2703  *   instance - a unique integer ID allocated as the new instance.
2704  *   -1 - lpfc get instance failed.
2705  **/
2706 int
2707 lpfc_get_instance(void)
2708 {
2709         int instance = 0;
2710
2711         /* Assign an unused number */
2712         if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2713                 return -1;
2714         if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2715                 return -1;
2716         return instance;
2717 }
2718
2719 /**
2720  * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
2721  * @shost: pointer to SCSI host data structure.
2722  * @time: elapsed time of the scan in jiffies.
2723  *
2724  * This routine is called by the SCSI layer with a SCSI host to determine
2725  * whether the scan host is finished.
2726  *
2727  * Note: there is no scan_start function as adapter initialization will have
2728  * asynchronously kicked off the link initialization.
2729  *
2730  * Return codes
2731  *   0 - SCSI host scan is not over yet.
2732  *   1 - SCSI host scan is over.
2733  **/
2734 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2735 {
2736         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2737         struct lpfc_hba   *phba = vport->phba;
2738         int stat = 0;
2739
2740         spin_lock_irq(shost->host_lock);
2741
2742         if (vport->load_flag & FC_UNLOADING) {
2743                 stat = 1;
2744                 goto finished;
2745         }
2746         if (time >= 30 * HZ) {
2747                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2748                                 "0461 Scanning longer than 30 "
2749                                 "seconds.  Continuing initialization\n");
2750                 stat = 1;
2751                 goto finished;
2752         }
2753         if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2754                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2755                                 "0465 Link down longer than 15 "
2756                                 "seconds.  Continuing initialization\n");
2757                 stat = 1;
2758                 goto finished;
2759         }
2760
2761         if (vport->port_state != LPFC_VPORT_READY)
2762                 goto finished;
2763         if (vport->num_disc_nodes || vport->fc_prli_sent)
2764                 goto finished;
2765         if (vport->fc_map_cnt == 0 && time < 2 * HZ)
2766                 goto finished;
2767         if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
2768                 goto finished;
2769
2770         stat = 1;
2771
2772 finished:
2773         spin_unlock_irq(shost->host_lock);
2774         return stat;
2775 }
2776
2777 /**
2778  * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
2779  * @shost: pointer to SCSI host data structure.
2780  *
2781  * This routine initializes a given SCSI host attributes on a FC port. The
2782  * SCSI host can be either on top of a physical port or a virtual port.
2783  **/
2784 void lpfc_host_attrib_init(struct Scsi_Host *shost)
2785 {
2786         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2787         struct lpfc_hba   *phba = vport->phba;
2788         /*
2789          * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
2790          */
2791
2792         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2793         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
2794         fc_host_supported_classes(shost) = FC_COS_CLASS3;
2795
2796         memset(fc_host_supported_fc4s(shost), 0,
2797                sizeof(fc_host_supported_fc4s(shost)));
2798         fc_host_supported_fc4s(shost)[2] = 1;
2799         fc_host_supported_fc4s(shost)[7] = 1;
2800
2801         lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2802                                  sizeof fc_host_symbolic_name(shost));
2803
2804         fc_host_supported_speeds(shost) = 0;
2805         if (phba->lmt & LMT_10Gb)
2806                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
2807         if (phba->lmt & LMT_8Gb)
2808                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
2809         if (phba->lmt & LMT_4Gb)
2810                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2811         if (phba->lmt & LMT_2Gb)
2812                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2813         if (phba->lmt & LMT_1Gb)
2814                 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2815
2816         fc_host_maxframe_size(shost) =
2817                 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2818                 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
2819
2820         /* This value is also unchanging */
2821         memset(fc_host_active_fc4s(shost), 0,
2822                sizeof(fc_host_active_fc4s(shost)));
2823         fc_host_active_fc4s(shost)[2] = 1;
2824         fc_host_active_fc4s(shost)[7] = 1;
2825
2826         fc_host_max_npiv_vports(shost) = phba->max_vpi;
2827         spin_lock_irq(shost->host_lock);
2828         vport->load_flag &= ~FC_LOADING;
2829         spin_unlock_irq(shost->host_lock);
2830 }
2831
2832 /**
2833  * lpfc_stop_port_s3 - Stop SLI3 device port
2834  * @phba: pointer to lpfc hba data structure.
2835  *
2836  * This routine is invoked to stop an SLI3 device port, it stops the device
2837  * from generating interrupts and stops the device driver's timers for the
2838  * device.
2839  **/
2840 static void
2841 lpfc_stop_port_s3(struct lpfc_hba *phba)
2842 {
2843         /* Clear all interrupt enable conditions */
2844         writel(0, phba->HCregaddr);
2845         readl(phba->HCregaddr); /* flush */
2846         /* Clear all pending interrupts */
2847         writel(0xffffffff, phba->HAregaddr);
2848         readl(phba->HAregaddr); /* flush */
2849
2850         /* Reset some HBA SLI setup states */
2851         lpfc_stop_hba_timers(phba);
2852         phba->pport->work_port_events = 0;
2853 }
2854
2855 /**
2856  * lpfc_stop_port_s4 - Stop SLI4 device port
2857  * @phba: pointer to lpfc hba data structure.
2858  *
2859  * This routine is invoked to stop an SLI4 device port, it stops the device
2860  * from generating interrupts and stops the device driver's timers for the
2861  * device.
2862  **/
2863 static void
2864 lpfc_stop_port_s4(struct lpfc_hba *phba)
2865 {
2866         /* Reset some HBA SLI4 setup states */
2867         lpfc_stop_hba_timers(phba);
2868         phba->pport->work_port_events = 0;
2869         phba->sli4_hba.intr_enable = 0;
2870 }
2871
2872 /**
2873  * lpfc_stop_port - Wrapper function for stopping hba port
2874  * @phba: Pointer to HBA context object.
2875  *
2876  * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2877  * the API jump table function pointer from the lpfc_hba struct.
2878  **/
2879 void
2880 lpfc_stop_port(struct lpfc_hba *phba)
2881 {
2882         phba->lpfc_stop_port(phba);
2883 }
2884
2885 /**
2886  * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2887  * @phba: pointer to lpfc hba data structure.
2888  *
2889  * This routine is invoked to remove the driver default fcf record from
2890  * the port.  This routine currently acts on FCF Index 0.
2891  *
2892  **/
2893 void
2894 lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2895 {
2896         int rc = 0;
2897         LPFC_MBOXQ_t *mboxq;
2898         struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2899         uint32_t mbox_tmo, req_len;
2900         uint32_t shdr_status, shdr_add_status;
2901
2902         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2903         if (!mboxq) {
2904                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2905                         "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2906                 return;
2907         }
2908
2909         req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2910                   sizeof(struct lpfc_sli4_cfg_mhdr);
2911         rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2912                               LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2913                               req_len, LPFC_SLI4_MBX_EMBED);
2914         /*
2915          * In phase 1, there is a single FCF index, 0.  In phase2, the driver
2916          * supports multiple FCF indices.
2917          */
2918         del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2919         bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2920         bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
2921                phba->fcf.current_rec.fcf_indx);
2922
2923         if (!phba->sli4_hba.intr_enable)
2924                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2925         else {
2926                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2927                 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2928         }
2929         /* The IOCTL status is embedded in the mailbox subheader. */
2930         shdr_status = bf_get(lpfc_mbox_hdr_status,
2931                              &del_fcf_record->header.cfg_shdr.response);
2932         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2933                                  &del_fcf_record->header.cfg_shdr.response);
2934         if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2935                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2936                                 "2516 DEL FCF of default FCF Index failed "
2937                                 "mbx status x%x, status x%x add_status x%x\n",
2938                                 rc, shdr_status, shdr_add_status);
2939         }
2940         if (rc != MBX_TIMEOUT)
2941                 mempool_free(mboxq, phba->mbox_mem_pool);
2942 }
2943
2944 /**
2945  * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2946  * @phba: Pointer to hba for which this call is being executed.
2947  *
2948  * This routine starts the timer waiting for the FCF rediscovery to complete.
2949  **/
2950 void
2951 lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2952 {
2953         unsigned long fcf_redisc_wait_tmo =
2954                 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2955         /* Start fcf rediscovery wait period timer */
2956         mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2957         spin_lock_irq(&phba->hbalock);
2958         /* Allow action to new fcf asynchronous event */
2959         phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2960         /* Mark the FCF rediscovery pending state */
2961         phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2962         spin_unlock_irq(&phba->hbalock);
2963 }
2964
2965 /**
2966  * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2967  * @ptr: Map to lpfc_hba data structure pointer.
2968  *
2969  * This routine is invoked when waiting for FCF table rediscover has been
2970  * timed out. If new FCF record(s) has (have) been discovered during the
2971  * wait period, a new FCF event shall be added to the FCOE async event
2972  * list, and then worker thread shall be waked up for processing from the
2973  * worker thread context.
2974  **/
2975 void
2976 lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2977 {
2978         struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2979
2980         /* Don't send FCF rediscovery event if timer cancelled */
2981         spin_lock_irq(&phba->hbalock);
2982         if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2983                 spin_unlock_irq(&phba->hbalock);
2984                 return;
2985         }
2986         /* Clear FCF rediscovery timer pending flag */
2987         phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2988         /* FCF rediscovery event to worker thread */
2989         phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2990         spin_unlock_irq(&phba->hbalock);
2991         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2992                         "2776 FCF rediscover wait timer expired, post "
2993                         "a worker thread event for FCF table scan\n");
2994         /* wake up worker thread */
2995         lpfc_worker_wake_up(phba);
2996 }
2997
2998 /**
2999  * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
3000  * @phba: pointer to lpfc hba data structure.
3001  *
3002  * This function uses the QUERY_FW_CFG mailbox command to determine if the
3003  * firmware loaded supports FCoE. A return of zero indicates that the mailbox
3004  * was successful and the firmware supports FCoE. Any other return indicates
3005  * a error. It is assumed that this function will be called before interrupts
3006  * are enabled.
3007  **/
3008 static int
3009 lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
3010 {
3011         int rc = 0;
3012         LPFC_MBOXQ_t *mboxq;
3013         struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
3014         uint32_t length;
3015         uint32_t shdr_status, shdr_add_status;
3016
3017         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3018         if (!mboxq) {
3019                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3020                                 "2621 Failed to allocate mbox for "
3021                                 "query firmware config cmd\n");
3022                 return -ENOMEM;
3023         }
3024         query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
3025         length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
3026                   sizeof(struct lpfc_sli4_cfg_mhdr));
3027         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
3028                          LPFC_MBOX_OPCODE_QUERY_FW_CFG,
3029                          length, LPFC_SLI4_MBX_EMBED);
3030         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3031         /* The IOCTL status is embedded in the mailbox subheader. */
3032         shdr_status = bf_get(lpfc_mbox_hdr_status,
3033                              &query_fw_cfg->header.cfg_shdr.response);
3034         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
3035                                  &query_fw_cfg->header.cfg_shdr.response);
3036         if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
3037                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3038                                 "2622 Query Firmware Config failed "
3039                                 "mbx status x%x, status x%x add_status x%x\n",
3040                                 rc, shdr_status, shdr_add_status);
3041                 return -EINVAL;
3042         }
3043         if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
3044                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3045                                 "2623 FCoE Function not supported by firmware. "
3046                                 "Function mode = %08x\n",
3047                                 query_fw_cfg->function_mode);
3048                 return -EINVAL;
3049         }
3050         if (rc != MBX_TIMEOUT)
3051                 mempool_free(mboxq, phba->mbox_mem_pool);
3052         return 0;
3053 }
3054
3055 /**
3056  * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3057  * @phba: pointer to lpfc hba data structure.
3058  * @acqe_link: pointer to the async link completion queue entry.
3059  *
3060  * This routine is to parse the SLI4 link-attention link fault code and
3061  * translate it into the base driver's read link attention mailbox command
3062  * status.
3063  *
3064  * Return: Link-attention status in terms of base driver's coding.
3065  **/
3066 static uint16_t
3067 lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3068                            struct lpfc_acqe_link *acqe_link)
3069 {
3070         uint16_t latt_fault;
3071
3072         switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3073         case LPFC_ASYNC_LINK_FAULT_NONE:
3074         case LPFC_ASYNC_LINK_FAULT_LOCAL:
3075         case LPFC_ASYNC_LINK_FAULT_REMOTE:
3076                 latt_fault = 0;
3077                 break;
3078         default:
3079                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3080                                 "0398 Invalid link fault code: x%x\n",
3081                                 bf_get(lpfc_acqe_link_fault, acqe_link));
3082                 latt_fault = MBXERR_ERROR;
3083                 break;
3084         }
3085         return latt_fault;
3086 }
3087
3088 /**
3089  * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3090  * @phba: pointer to lpfc hba data structure.
3091  * @acqe_link: pointer to the async link completion queue entry.
3092  *
3093  * This routine is to parse the SLI4 link attention type and translate it
3094  * into the base driver's link attention type coding.
3095  *
3096  * Return: Link attention type in terms of base driver's coding.
3097  **/
3098 static uint8_t
3099 lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3100                           struct lpfc_acqe_link *acqe_link)
3101 {
3102         uint8_t att_type;
3103
3104         switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3105         case LPFC_ASYNC_LINK_STATUS_DOWN:
3106         case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3107                 att_type = AT_LINK_DOWN;
3108                 break;
3109         case LPFC_ASYNC_LINK_STATUS_UP:
3110                 /* Ignore physical link up events - wait for logical link up */
3111                 att_type = AT_RESERVED;
3112                 break;
3113         case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3114                 att_type = AT_LINK_UP;
3115                 break;
3116         default:
3117                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3118                                 "0399 Invalid link attention type: x%x\n",
3119                                 bf_get(lpfc_acqe_link_status, acqe_link));
3120                 att_type = AT_RESERVED;
3121                 break;
3122         }
3123         return att_type;
3124 }
3125
3126 /**
3127  * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3128  * @phba: pointer to lpfc hba data structure.
3129  * @acqe_link: pointer to the async link completion queue entry.
3130  *
3131  * This routine is to parse the SLI4 link-attention link speed and translate
3132  * it into the base driver's link-attention link speed coding.
3133  *
3134  * Return: Link-attention link speed in terms of base driver's coding.
3135  **/
3136 static uint8_t
3137 lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3138                                 struct lpfc_acqe_link *acqe_link)
3139 {
3140         uint8_t link_speed;
3141
3142         switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3143         case LPFC_ASYNC_LINK_SPEED_ZERO:
3144                 link_speed = LA_UNKNW_LINK;
3145                 break;
3146         case LPFC_ASYNC_LINK_SPEED_10MBPS:
3147                 link_speed = LA_UNKNW_LINK;
3148                 break;
3149         case LPFC_ASYNC_LINK_SPEED_100MBPS:
3150                 link_speed = LA_UNKNW_LINK;
3151                 break;
3152         case LPFC_ASYNC_LINK_SPEED_1GBPS:
3153                 link_speed = LA_1GHZ_LINK;
3154                 break;
3155         case LPFC_ASYNC_LINK_SPEED_10GBPS:
3156                 link_speed = LA_10GHZ_LINK;
3157                 break;
3158         default:
3159                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3160                                 "0483 Invalid link-attention link speed: x%x\n",
3161                                 bf_get(lpfc_acqe_link_speed, acqe_link));
3162                 link_speed = LA_UNKNW_LINK;
3163                 break;
3164         }
3165         return link_speed;
3166 }
3167
3168 /**
3169  * lpfc_sli4_async_link_evt - Process the asynchronous link event
3170  * @phba: pointer to lpfc hba data structure.
3171  * @acqe_link: pointer to the async link completion queue entry.
3172  *
3173  * This routine is to handle the SLI4 asynchronous link event.
3174  **/
3175 static void
3176 lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3177                          struct lpfc_acqe_link *acqe_link)
3178 {
3179         struct lpfc_dmabuf *mp;
3180         LPFC_MBOXQ_t *pmb;
3181         MAILBOX_t *mb;
3182         READ_LA_VAR *la;
3183         uint8_t att_type;
3184
3185         att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3186         if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3187                 return;
3188         phba->fcoe_eventtag = acqe_link->event_tag;
3189         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3190         if (!pmb) {
3191                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3192                                 "0395 The mboxq allocation failed\n");
3193                 return;
3194         }
3195         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3196         if (!mp) {
3197                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3198                                 "0396 The lpfc_dmabuf allocation failed\n");
3199                 goto out_free_pmb;
3200         }
3201         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3202         if (!mp->virt) {
3203                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3204                                 "0397 The mbuf allocation failed\n");
3205                 goto out_free_dmabuf;
3206         }
3207
3208         /* Cleanup any outstanding ELS commands */
3209         lpfc_els_flush_all_cmd(phba);
3210
3211         /* Block ELS IOCBs until we have done process link event */
3212         phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3213
3214         /* Update link event statistics */
3215         phba->sli.slistat.link_event++;
3216
3217         /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3218         lpfc_read_la(phba, pmb, mp);
3219         pmb->vport = phba->pport;
3220
3221         /* Parse and translate status field */
3222         mb = &pmb->u.mb;
3223         mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3224
3225         /* Parse and translate link attention fields */
3226         la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3227         la->eventTag = acqe_link->event_tag;
3228         la->attType = att_type;
3229         la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3230
3231         /* Fake the the following irrelvant fields */
3232         la->topology = TOPOLOGY_PT_PT;
3233         la->granted_AL_PA = 0;
3234         la->il = 0;
3235         la->pb = 0;
3236         la->fa = 0;
3237         la->mm = 0;
3238
3239         /* Keep the link status for extra SLI4 state machine reference */
3240         phba->sli4_hba.link_state.speed =
3241                                 bf_get(lpfc_acqe_link_speed, acqe_link);
3242         phba->sli4_hba.link_state.duplex =
3243                                 bf_get(lpfc_acqe_link_duplex, acqe_link);
3244         phba->sli4_hba.link_state.status =
3245                                 bf_get(lpfc_acqe_link_status, acqe_link);
3246         phba->sli4_hba.link_state.physical =
3247                                 bf_get(lpfc_acqe_link_physical, acqe_link);
3248         phba->sli4_hba.link_state.fault =
3249                                 bf_get(lpfc_acqe_link_fault, acqe_link);
3250         phba->sli4_hba.link_state.logical_speed =
3251                                 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
3252
3253         /* Invoke the lpfc_handle_latt mailbox command callback function */
3254         lpfc_mbx_cmpl_read_la(phba, pmb);
3255
3256         return;
3257
3258 out_free_dmabuf:
3259         kfree(mp);
3260 out_free_pmb:
3261         mempool_free(pmb, phba->mbox_mem_pool);
3262 }
3263
3264 /**
3265  * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3266  * @vport: pointer to vport data structure.
3267  *
3268  * This routine is to perform Clear Virtual Link (CVL) on a vport in
3269  * response to a CVL event.
3270  *
3271  * Return the pointer to the ndlp with the vport if successful, otherwise
3272  * return NULL.
3273  **/
3274 static struct lpfc_nodelist *
3275 lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3276 {
3277         struct lpfc_nodelist *ndlp;
3278         struct Scsi_Host *shost;
3279         struct lpfc_hba *phba;
3280
3281         if (!vport)
3282                 return NULL;
3283         phba = vport->phba;
3284         if (!phba)
3285                 return NULL;
3286         ndlp = lpfc_findnode_did(vport, Fabric_DID);
3287         if (!ndlp) {
3288                 /* Cannot find existing Fabric ndlp, so allocate a new one */
3289                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3290                 if (!ndlp)
3291                         return 0;
3292                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3293                 /* Set the node type */
3294                 ndlp->nlp_type |= NLP_FABRIC;
3295                 /* Put ndlp onto node list */
3296                 lpfc_enqueue_node(vport, ndlp);
3297         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3298                 /* re-setup ndlp without removing from node list */
3299                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3300                 if (!ndlp)
3301                         return 0;
3302         }
3303         if (phba->pport->port_state <= LPFC_FLOGI)
3304                 return NULL;
3305         /* If virtual link is not yet instantiated ignore CVL */
3306         if (vport->port_state <= LPFC_FDISC)
3307                 return NULL;
3308         shost = lpfc_shost_from_vport(vport);
3309         if (!shost)
3310                 return NULL;
3311         lpfc_linkdown_port(vport);
3312         lpfc_cleanup_pending_mbox(vport);
3313         spin_lock_irq(shost->host_lock);
3314         vport->fc_flag |= FC_VPORT_CVL_RCVD;
3315         spin_unlock_irq(shost->host_lock);
3316
3317         return ndlp;
3318 }
3319
3320 /**
3321  * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3322  * @vport: pointer to lpfc hba data structure.
3323  *
3324  * This routine is to perform Clear Virtual Link (CVL) on all vports in
3325  * response to a FCF dead event.
3326  **/
3327 static void
3328 lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3329 {
3330         struct lpfc_vport **vports;
3331         int i;
3332
3333         vports = lpfc_create_vport_work_array(phba);
3334         if (vports)
3335                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3336                         lpfc_sli4_perform_vport_cvl(vports[i]);
3337         lpfc_destroy_vport_work_array(phba, vports);
3338 }
3339
3340 /**
3341  * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3342  * @phba: pointer to lpfc hba data structure.
3343  * @acqe_link: pointer to the async fcoe completion queue entry.
3344  *
3345  * This routine is to handle the SLI4 asynchronous fcoe event.
3346  **/
3347 static void
3348 lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3349                          struct lpfc_acqe_fcoe *acqe_fcoe)
3350 {
3351         uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3352         int rc;
3353         struct lpfc_vport *vport;
3354         struct lpfc_nodelist *ndlp;
3355         struct Scsi_Host  *shost;
3356         int active_vlink_present;
3357         struct lpfc_vport **vports;
3358         int i;
3359
3360         phba->fc_eventTag = acqe_fcoe->event_tag;
3361         phba->fcoe_eventtag = acqe_fcoe->event_tag;
3362         switch (event_type) {
3363         case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
3364         case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
3365                 if (event_type == LPFC_FCOE_EVENT_TYPE_NEW_FCF)
3366                         lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3367                                         LOG_DISCOVERY,
3368                                         "2546 New FCF found event: "
3369                                         "evt_tag:x%x, fcf_index:x%x\n",
3370                                         acqe_fcoe->event_tag,
3371                                         acqe_fcoe->index);
3372                 else
3373                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3374                                         LOG_DISCOVERY,
3375                                         "2788 FCF parameter modified event: "
3376                                         "evt_tag:x%x, fcf_index:x%x\n",
3377                                         acqe_fcoe->event_tag,
3378                                         acqe_fcoe->index);
3379                 /* If the FCF discovery is in progress, do nothing. */
3380                 spin_lock_irq(&phba->hbalock);
3381                 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
3382                         spin_unlock_irq(&phba->hbalock);
3383                         break;
3384                 }
3385                 /* If fast FCF failover rescan event is pending, do nothing */
3386                 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3387                         spin_unlock_irq(&phba->hbalock);
3388                         break;
3389                 }
3390                 spin_unlock_irq(&phba->hbalock);
3391
3392                 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
3393                     !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
3394                         /*
3395                          * During period of FCF discovery, read the FCF
3396                          * table record indexed by the event to update
3397                          * FCF round robin failover eligible FCF bmask.
3398                          */
3399                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3400                                         LOG_DISCOVERY,
3401                                         "2779 Read new FCF record with "
3402                                         "fcf_index:x%x for updating FCF "
3403                                         "round robin failover bmask\n",
3404                                         acqe_fcoe->index);
3405                         rc = lpfc_sli4_read_fcf_rec(phba, acqe_fcoe->index);
3406                 }
3407                 /* If the FCF has been in discovered state, do nothing. */
3408                 spin_lock_irq(&phba->hbalock);
3409                 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3410                         spin_unlock_irq(&phba->hbalock);
3411                         break;
3412                 }
3413                 spin_unlock_irq(&phba->hbalock);
3414                 /* Otherwise, scan the entire FCF table and re-discover SAN */
3415                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3416                                 "2770 Start FCF table scan due to new FCF "
3417                                 "event: evt_tag:x%x, fcf_index:x%x\n",
3418                                 acqe_fcoe->event_tag, acqe_fcoe->index);
3419                 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3420                                                      LPFC_FCOE_FCF_GET_FIRST);
3421                 if (rc)
3422                         lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3423                                         "2547 Issue FCF scan read FCF mailbox "
3424                                         "command failed 0x%x\n", rc);
3425                 break;
3426
3427         case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3428                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3429                         "2548 FCF Table full count 0x%x tag 0x%x\n",
3430                         bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3431                         acqe_fcoe->event_tag);
3432                 break;
3433
3434         case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3435                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3436                         "2549 FCF disconnected from network index 0x%x"
3437                         " tag 0x%x\n", acqe_fcoe->index,
3438                         acqe_fcoe->event_tag);
3439                 /* If the event is not for currently used fcf do nothing */
3440                 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
3441                         break;
3442                 /* We request port to rediscover the entire FCF table for
3443                  * a fast recovery from case that the current FCF record
3444                  * is no longer valid if we are not in the middle of FCF
3445                  * failover process already.
3446                  */
3447                 spin_lock_irq(&phba->hbalock);
3448                 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3449                         spin_unlock_irq(&phba->hbalock);
3450                         /* Update FLOGI FCF failover eligible FCF bmask */
3451                         lpfc_sli4_fcf_rr_index_clear(phba, acqe_fcoe->index);
3452                         break;
3453                 }
3454                 /* Mark the fast failover process in progress */
3455                 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
3456                 spin_unlock_irq(&phba->hbalock);
3457                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3458                                 "2771 Start FCF fast failover process due to "
3459                                 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
3460                                 "\n", acqe_fcoe->event_tag, acqe_fcoe->index);
3461                 rc = lpfc_sli4_redisc_fcf_table(phba);
3462                 if (rc) {
3463                         lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3464                                         LOG_DISCOVERY,
3465                                         "2772 Issue FCF rediscover mabilbox "
3466                                         "command failed, fail through to FCF "
3467                                         "dead event\n");
3468                         spin_lock_irq(&phba->hbalock);
3469                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
3470                         spin_unlock_irq(&phba->hbalock);
3471                         /*
3472                          * Last resort will fail over by treating this
3473                          * as a link down to FCF registration.
3474                          */
3475                         lpfc_sli4_fcf_dead_failthrough(phba);
3476                 } else
3477                         /* Handling fast FCF failover to a DEAD FCF event
3478                          * is considered equalivant to receiving CVL to all
3479                          * vports.
3480                          */
3481                         lpfc_sli4_perform_all_vport_cvl(phba);
3482                 break;
3483         case LPFC_FCOE_EVENT_TYPE_CVL:
3484                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3485                         "2718 Clear Virtual Link Received for VPI 0x%x"
3486                         " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3487                 vport = lpfc_find_vport_by_vpid(phba,
3488                                 acqe_fcoe->index - phba->vpi_base);
3489                 ndlp = lpfc_sli4_perform_vport_cvl(vport);
3490                 if (!ndlp)
3491                         break;
3492                 active_vlink_present = 0;
3493
3494                 vports = lpfc_create_vport_work_array(phba);
3495                 if (vports) {
3496                         for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3497                                         i++) {
3498                                 if ((!(vports[i]->fc_flag &
3499                                         FC_VPORT_CVL_RCVD)) &&
3500                                         (vports[i]->port_state > LPFC_FDISC)) {
3501                                         active_vlink_present = 1;
3502                                         break;
3503                                 }
3504                         }
3505                         lpfc_destroy_vport_work_array(phba, vports);
3506                 }
3507
3508                 if (active_vlink_present) {
3509                         /*
3510                          * If there are other active VLinks present,
3511                          * re-instantiate the Vlink using FDISC.
3512                          */
3513                         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3514                         shost = lpfc_shost_from_vport(vport);
3515                         spin_lock_irq(shost->host_lock);
3516                         ndlp->nlp_flag |= NLP_DELAY_TMO;
3517                         spin_unlock_irq(shost->host_lock);
3518                         ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3519                         vport->port_state = LPFC_FDISC;
3520                 } else {
3521                         /*
3522                          * Otherwise, we request port to rediscover
3523                          * the entire FCF table for a fast recovery
3524                          * from possible case that the current FCF
3525                          * is no longer valid if we are not already
3526                          * in the FCF failover process.
3527                          */
3528                         spin_lock_irq(&phba->hbalock);
3529                         if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3530                                 spin_unlock_irq(&phba->hbalock);
3531                                 break;
3532                         }
3533                         /* Mark the fast failover process in progress */
3534                         phba->fcf.fcf_flag |= FCF_ACVL_DISC;
3535                         spin_unlock_irq(&phba->hbalock);
3536                         lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3537                                         LOG_DISCOVERY,
3538                                         "2773 Start FCF fast failover due "
3539                                         "to CVL event: evt_tag:x%x\n",
3540                                         acqe_fcoe->event_tag);
3541                         rc = lpfc_sli4_redisc_fcf_table(phba);
3542                         if (rc) {
3543                                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3544                                                 LOG_DISCOVERY,
3545                                                 "2774 Issue FCF rediscover "
3546                                                 "mabilbox command failed, "
3547                                                 "through to CVL event\n");
3548                                 spin_lock_irq(&phba->hbalock);
3549                                 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
3550                                 spin_unlock_irq(&phba->hbalock);
3551                                 /*
3552                                  * Last resort will be re-try on the
3553                                  * the current registered FCF entry.
3554                                  */
3555                                 lpfc_retry_pport_discovery(phba);
3556                         }
3557                 }
3558                 break;
3559         default:
3560                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3561                         "0288 Unknown FCoE event type 0x%x event tag "
3562                         "0x%x\n", event_type, acqe_fcoe->event_tag);
3563                 break;
3564         }
3565 }
3566
3567 /**
3568  * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3569  * @phba: pointer to lpfc hba data structure.
3570  * @acqe_link: pointer to the async dcbx completion queue entry.
3571  *
3572  * This routine is to handle the SLI4 asynchronous dcbx event.
3573  **/
3574 static void
3575 lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3576                          struct lpfc_acqe_dcbx *acqe_dcbx)
3577 {
3578         phba->fc_eventTag = acqe_dcbx->event_tag;
3579         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3580                         "0290 The SLI4 DCBX asynchronous event is not "
3581                         "handled yet\n");
3582 }
3583
3584 /**
3585  * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3586  * @phba: pointer to lpfc hba data structure.
3587  * @acqe_link: pointer to the async grp5 completion queue entry.
3588  *
3589  * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3590  * is an asynchronous notified of a logical link speed change.  The Port
3591  * reports the logical link speed in units of 10Mbps.
3592  **/
3593 static void
3594 lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3595                          struct lpfc_acqe_grp5 *acqe_grp5)
3596 {
3597         uint16_t prev_ll_spd;
3598
3599         phba->fc_eventTag = acqe_grp5->event_tag;
3600         phba->fcoe_eventtag = acqe_grp5->event_tag;
3601         prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3602         phba->sli4_hba.link_state.logical_speed =
3603                 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3604         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3605                         "2789 GRP5 Async Event: Updating logical link speed "
3606                         "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3607                         (phba->sli4_hba.link_state.logical_speed*10));
3608 }
3609
3610 /**
3611  * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3612  * @phba: pointer to lpfc hba data structure.
3613  *
3614  * This routine is invoked by the worker thread to process all the pending
3615  * SLI4 asynchronous events.
3616  **/
3617 void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3618 {
3619         struct lpfc_cq_event *cq_event;
3620
3621         /* First, declare the async event has been handled */
3622         spin_lock_irq(&phba->hbalock);
3623         phba->hba_flag &= ~ASYNC_EVENT;
3624         spin_unlock_irq(&phba->hbalock);
3625         /* Now, handle all the async events */
3626         while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3627                 /* Get the first event from the head of the event queue */
3628                 spin_lock_irq(&phba->hbalock);
3629                 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3630                                  cq_event, struct lpfc_cq_event, list);
3631                 spin_unlock_irq(&phba->hbalock);
3632                 /* Process the asynchronous event */
3633                 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3634                 case LPFC_TRAILER_CODE_LINK:
3635                         lpfc_sli4_async_link_evt(phba,
3636                                                  &cq_event->cqe.acqe_link);
3637                         break;
3638                 case LPFC_TRAILER_CODE_FCOE:
3639                         lpfc_sli4_async_fcoe_evt(phba,
3640                                                  &cq_event->cqe.acqe_fcoe);
3641                         break;
3642                 case LPFC_TRAILER_CODE_DCBX:
3643                         lpfc_sli4_async_dcbx_evt(phba,
3644                                                  &cq_event->cqe.acqe_dcbx);
3645                         break;
3646                 case LPFC_TRAILER_CODE_GRP5:
3647                         lpfc_sli4_async_grp5_evt(phba,
3648                                                  &cq_event->cqe.acqe_grp5);
3649                         break;
3650                 default:
3651                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3652                                         "1804 Invalid asynchrous event code: "
3653                                         "x%x\n", bf_get(lpfc_trailer_code,
3654                                         &cq_event->cqe.mcqe_cmpl));
3655                         break;
3656                 }
3657                 /* Free the completion event processed to the free pool */
3658                 lpfc_sli4_cq_event_release(phba, cq_event);
3659         }
3660 }
3661
3662 /**
3663  * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3664  * @phba: pointer to lpfc hba data structure.
3665  *
3666  * This routine is invoked by the worker thread to process FCF table
3667  * rediscovery pending completion event.
3668  **/
3669 void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3670 {
3671         int rc;
3672
3673         spin_lock_irq(&phba->hbalock);
3674         /* Clear FCF rediscovery timeout event */
3675         phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3676         /* Clear driver fast failover FCF record flag */
3677         phba->fcf.failover_rec.flag = 0;
3678         /* Set state for FCF fast failover */
3679         phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3680         spin_unlock_irq(&phba->hbalock);
3681
3682         /* Scan FCF table from the first entry to re-discover SAN */
3683         lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3684                         "2777 Start FCF table scan after FCF "
3685                         "rediscovery quiescent period over\n");
3686         rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
3687         if (rc)
3688                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3689                                 "2747 Issue FCF scan read FCF mailbox "
3690                                 "command failed 0x%x\n", rc);
3691 }
3692
3693 /**
3694  * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3695  * @phba: pointer to lpfc hba data structure.
3696  * @dev_grp: The HBA PCI-Device group number.
3697  *
3698  * This routine is invoked to set up the per HBA PCI-Device group function
3699  * API jump table entries.
3700  *
3701  * Return: 0 if success, otherwise -ENODEV
3702  **/
3703 int
3704 lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3705 {
3706         int rc;
3707
3708         /* Set up lpfc PCI-device group */
3709         phba->pci_dev_grp = dev_grp;
3710
3711         /* The LPFC_PCI_DEV_OC uses SLI4 */
3712         if (dev_grp == LPFC_PCI_DEV_OC)
3713                 phba->sli_rev = LPFC_SLI_REV4;
3714
3715         /* Set up device INIT API function jump table */
3716         rc = lpfc_init_api_table_setup(phba, dev_grp);
3717         if (rc)
3718                 return -ENODEV;
3719         /* Set up SCSI API function jump table */
3720         rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3721         if (rc)
3722                 return -ENODEV;
3723         /* Set up SLI API function jump table */
3724         rc = lpfc_sli_api_table_setup(phba, dev_grp);
3725         if (rc)
3726                 return -ENODEV;
3727         /* Set up MBOX API function jump table */
3728         rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3729         if (rc)
3730                 return -ENODEV;
3731
3732         return 0;
3733 }
3734
3735 /**
3736  * lpfc_log_intr_mode - Log the active interrupt mode
3737  * @phba: pointer to lpfc hba data structure.
3738  * @intr_mode: active interrupt mode adopted.
3739  *
3740  * This routine it invoked to log the currently used active interrupt mode
3741  * to the device.
3742  **/
3743 static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
3744 {
3745         switch (intr_mode) {
3746         case 0:
3747                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3748                                 "0470 Enable INTx interrupt mode.\n");
3749                 break;
3750         case 1:
3751                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3752                                 "0481 Enabled MSI interrupt mode.\n");
3753                 break;
3754         case 2:
3755                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3756                                 "0480 Enabled MSI-X interrupt mode.\n");
3757                 break;
3758         default:
3759                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3760                                 "0482 Illegal interrupt mode.\n");
3761                 break;
3762         }
3763         return;
3764 }
3765
3766 /**
3767  * lpfc_enable_pci_dev - Enable a generic PCI device.
3768  * @phba: pointer to lpfc hba data structure.
3769  *
3770  * This routine is invoked to enable the PCI device that is common to all
3771  * PCI devices.
3772  *
3773  * Return codes
3774  *      0 - successful
3775  *      other values - error
3776  **/
3777 static int
3778 lpfc_enable_pci_dev(struct lpfc_hba *phba)
3779 {
3780         struct pci_dev *pdev;
3781         int bars;
3782
3783         /* Obtain PCI device reference */
3784         if (!phba->pcidev)
3785                 goto out_error;
3786         else
3787                 pdev = phba->pcidev;
3788         /* Select PCI BARs */
3789         bars = pci_select_bars(pdev, IORESOURCE_MEM);
3790         /* Enable PCI device */
3791         if (pci_enable_device_mem(pdev))
3792                 goto out_error;
3793         /* Request PCI resource for the device */
3794         if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3795                 goto out_disable_device;
3796         /* Set up device as PCI master and save state for EEH */
3797         pci_set_master(pdev);
3798         pci_try_set_mwi(pdev);
3799         pci_save_state(pdev);
3800
3801         return 0;
3802
3803 out_disable_device:
3804         pci_disable_device(pdev);
3805 out_error:
3806         return -ENODEV;
3807 }
3808
3809 /**
3810  * lpfc_disable_pci_dev - Disable a generic PCI device.
3811  * @phba: pointer to lpfc hba data structure.
3812  *
3813  * This routine is invoked to disable the PCI device that is common to all
3814  * PCI devices.
3815  **/
3816 static void
3817 lpfc_disable_pci_dev(struct lpfc_hba *phba)
3818 {
3819         struct pci_dev *pdev;
3820         int bars;
3821
3822         /* Obtain PCI device reference */
3823         if (!phba->pcidev)
3824                 return;
3825         else
3826                 pdev = phba->pcidev;
3827         /* Select PCI BARs */
3828         bars = pci_select_bars(pdev, IORESOURCE_MEM);
3829         /* Release PCI resource and disable PCI device */
3830         pci_release_selected_regions(pdev, bars);
3831         pci_disable_device(pdev);
3832         /* Null out PCI private reference to driver */
3833         pci_set_drvdata(pdev, NULL);
3834
3835         return;
3836 }
3837
3838 /**
3839  * lpfc_reset_hba - Reset a hba
3840  * @phba: pointer to lpfc hba data structure.
3841  *
3842  * This routine is invoked to reset a hba device. It brings the HBA
3843  * offline, performs a board restart, and then brings the board back
3844  * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3845  * on outstanding mailbox commands.
3846  **/
3847 void
3848 lpfc_reset_hba(struct lpfc_hba *phba)
3849 {
3850         /* If resets are disabled then set error state and return. */
3851         if (!phba->cfg_enable_hba_reset) {
3852                 phba->link_state = LPFC_HBA_ERROR;
3853                 return;
3854         }
3855         lpfc_offline_prep(phba);
3856         lpfc_offline(phba);
3857         lpfc_sli_brdrestart(phba);
3858         lpfc_online(phba);
3859         lpfc_unblock_mgmt_io(phba);
3860 }
3861
3862 /**
3863  * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3864  * @phba: pointer to lpfc hba data structure.
3865  *
3866  * This routine is invoked to set up the driver internal resources specific to
3867  * support the SLI-3 HBA device it attached to.
3868  *
3869  * Return codes
3870  *      0 - successful
3871  *      other values - error
3872  **/
3873 static int
3874 lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3875 {
3876         struct lpfc_sli *psli;
3877
3878         /*
3879          * Initialize timers used by driver
3880          */
3881
3882         /* Heartbeat timer */
3883         init_timer(&phba->hb_tmofunc);
3884         phba->hb_tmofunc.function = lpfc_hb_timeout;
3885         phba->hb_tmofunc.data = (unsigned long)phba;
3886
3887         psli = &phba->sli;
3888         /* MBOX heartbeat timer */
3889         init_timer(&psli->mbox_tmo);
3890         psli->mbox_tmo.function = lpfc_mbox_timeout;
3891         psli->mbox_tmo.data = (unsigned long) phba;
3892         /* FCP polling mode timer */
3893         init_timer(&phba->fcp_poll_timer);
3894         phba->fcp_poll_timer.function = lpfc_poll_timeout;
3895         phba->fcp_poll_timer.data = (unsigned long) phba;
3896         /* Fabric block timer */
3897         init_timer(&phba->fabric_block_timer);
3898         phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3899         phba->fabric_block_timer.data = (unsigned long) phba;
3900         /* EA polling mode timer */
3901         init_timer(&phba->eratt_poll);
3902         phba->eratt_poll.function = lpfc_poll_eratt;
3903         phba->eratt_poll.data = (unsigned long) phba;
3904
3905         /* Host attention work mask setup */
3906         phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3907         phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3908
3909         /* Get all the module params for configuring this host */
3910         lpfc_get_cfgparam(phba);
3911         if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
3912                 phba->menlo_flag |= HBA_MENLO_SUPPORT;
3913                 /* check for menlo minimum sg count */
3914                 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
3915                         phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
3916         }
3917
3918         /*
3919          * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3920          * used to create the sg_dma_buf_pool must be dynamically calculated.
3921          * 2 segments are added since the IOCB needs a command and response bde.
3922          */
3923         phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3924                 sizeof(struct fcp_rsp) +
3925                         ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3926
3927         if (phba->cfg_enable_bg) {
3928                 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3929                 phba->cfg_sg_dma_buf_size +=
3930                         phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3931         }
3932
3933         /* Also reinitialize the host templates with new values. */
3934         lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3935         lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3936
3937         phba->max_vpi = LPFC_MAX_VPI;
3938         /* This will be set to correct value after config_port mbox */
3939         phba->max_vports = 0;
3940
3941         /*
3942          * Initialize the SLI Layer to run with lpfc HBAs.
3943          */
3944         lpfc_sli_setup(phba);
3945         lpfc_sli_queue_setup(phba);
3946
3947         /* Allocate device driver memory */
3948         if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3949                 return -ENOMEM;
3950
3951         return 0;
3952 }
3953
3954 /**
3955  * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3956  * @phba: pointer to lpfc hba data structure.
3957  *
3958  * This routine is invoked to unset the driver internal resources set up
3959  * specific for supporting the SLI-3 HBA device it attached to.
3960  **/
3961 static void
3962 lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3963 {
3964         /* Free device driver memory allocated */
3965         lpfc_mem_free_all(phba);
3966
3967         return;
3968 }
3969
3970 /**
3971  * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3972  * @phba: pointer to lpfc hba data structure.
3973  *
3974  * This routine is invoked to set up the driver internal resources specific to
3975  * support the SLI-4 HBA device it attached to.
3976  *
3977  * Return codes
3978  *      0 - successful
3979  *      other values - error
3980  **/
3981 static int
3982 lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3983 {
3984         struct lpfc_sli *psli;
3985         LPFC_MBOXQ_t *mboxq;
3986         int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
3987         uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
3988         struct lpfc_mqe *mqe;
3989         int longs;
3990
3991         /* Before proceed, wait for POST done and device ready */
3992         rc = lpfc_sli4_post_status_check(phba);
3993         if (rc)
3994                 return -ENODEV;
3995
3996         /*
3997          * Initialize timers used by driver
3998          */
3999
4000         /* Heartbeat timer */
4001         init_timer(&phba->hb_tmofunc);
4002         phba->hb_tmofunc.function = lpfc_hb_timeout;
4003         phba->hb_tmofunc.data = (unsigned long)phba;
4004
4005         psli = &phba->sli;
4006         /* MBOX heartbeat timer */
4007         init_timer(&psli->mbox_tmo);
4008         psli->mbox_tmo.function = lpfc_mbox_timeout;
4009         psli->mbox_tmo.data = (unsigned long) phba;
4010         /* Fabric block timer */
4011         init_timer(&phba->fabric_block_timer);
4012         phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4013         phba->fabric_block_timer.data = (unsigned long) phba;
4014         /* EA polling mode timer */
4015         init_timer(&phba->eratt_poll);
4016         phba->eratt_poll.function = lpfc_poll_eratt;
4017         phba->eratt_poll.data = (unsigned long) phba;
4018         /* FCF rediscover timer */
4019         init_timer(&phba->fcf.redisc_wait);
4020         phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4021         phba->fcf.redisc_wait.data = (unsigned long)phba;
4022
4023         /*
4024          * We need to do a READ_CONFIG mailbox command here before
4025          * calling lpfc_get_cfgparam. For VFs this will report the
4026          * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4027          * All of the resources allocated
4028          * for this Port are tied to these values.
4029          */
4030         /* Get all the module params for configuring this host */
4031         lpfc_get_cfgparam(phba);
4032         phba->max_vpi = LPFC_MAX_VPI;
4033         /* This will be set to correct value after the read_config mbox */
4034         phba->max_vports = 0;
4035
4036         /* Program the default value of vlan_id and fc_map */
4037         phba->valid_vlan = 0;
4038         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4039         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4040         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4041
4042         /*
4043          * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4044          * used to create the sg_dma_buf_pool must be dynamically calculated.
4045          * 2 segments are added since the IOCB needs a command and response bde.
4046          * To insure that the scsi sgl does not cross a 4k page boundary only
4047          * sgl sizes of must be a power of 2.
4048          */
4049         buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
4050                     ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
4051         /* Feature Level 1 hardware is limited to 2 pages */
4052         if ((bf_get(lpfc_sli_intf_featurelevel1, &phba->sli4_hba.sli_intf) ==
4053              LPFC_SLI_INTF_FEATURELEVEL1_1))
4054                 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4055         else
4056                 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4057         for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4058              dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4059              dma_buf_size = dma_buf_size << 1)
4060                 ;
4061         if (dma_buf_size == max_buf_size)
4062                 phba->cfg_sg_seg_cnt = (dma_buf_size -
4063                         sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4064                         (2 * sizeof(struct sli4_sge))) /
4065                                 sizeof(struct sli4_sge);
4066         phba->cfg_sg_dma_buf_size = dma_buf_size;
4067
4068         /* Initialize buffer queue management fields */
4069         hbq_count = lpfc_sli_hbq_count();
4070         for (i = 0; i < hbq_count; ++i)
4071                 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4072         INIT_LIST_HEAD(&phba->rb_pend_list);
4073         phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4074         phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4075
4076         /*
4077          * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4078          */
4079         /* Initialize the Abort scsi buffer list used by driver */
4080         spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4081         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4082         /* This abort list used by worker thread */
4083         spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4084
4085         /*
4086          * Initialize dirver internal slow-path work queues
4087          */
4088
4089         /* Driver internel slow-path CQ Event pool */
4090         INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4091         /* Response IOCB work queue list */
4092         INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
4093         /* Asynchronous event CQ Event work queue list */
4094         INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4095         /* Fast-path XRI aborted CQ Event work queue list */
4096         INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4097         /* Slow-path XRI aborted CQ Event work queue list */
4098         INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4099         /* Receive queue CQ Event work queue list */
4100         INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4101
4102         /* Initialize the driver internal SLI layer lists. */
4103         lpfc_sli_setup(phba);
4104         lpfc_sli_queue_setup(phba);
4105
4106         /* Allocate device driver memory */
4107         rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4108         if (rc)
4109                 return -ENOMEM;
4110
4111         /* Create the bootstrap mailbox command */
4112         rc = lpfc_create_bootstrap_mbox(phba);
4113         if (unlikely(rc))
4114                 goto out_free_mem;
4115
4116         /* Set up the host's endian order with the device. */
4117         rc = lpfc_setup_endian_order(phba);
4118         if (unlikely(rc))
4119                 goto out_free_bsmbx;
4120
4121         rc = lpfc_sli4_fw_cfg_check(phba);
4122         if (unlikely(rc))
4123                 goto out_free_bsmbx;
4124
4125         /* Set up the hba's configuration parameters. */
4126         rc = lpfc_sli4_read_config(phba);
4127         if (unlikely(rc))
4128                 goto out_free_bsmbx;
4129
4130         /* Perform a function reset */
4131         rc = lpfc_pci_function_reset(phba);
4132         if (unlikely(rc))
4133                 goto out_free_bsmbx;
4134
4135         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4136                                                        GFP_KERNEL);
4137         if (!mboxq) {
4138                 rc = -ENOMEM;
4139                 goto out_free_bsmbx;
4140         }
4141
4142         /* Get the Supported Pages. It is always available. */
4143         lpfc_supported_pages(mboxq);
4144         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4145         if (unlikely(rc)) {
4146                 rc = -EIO;
4147                 mempool_free(mboxq, phba->mbox_mem_pool);
4148                 goto out_free_bsmbx;
4149         }
4150
4151         mqe = &mboxq->u.mqe;
4152         memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4153                LPFC_MAX_SUPPORTED_PAGES);
4154         for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4155                 switch (pn_page[i]) {
4156                 case LPFC_SLI4_PARAMETERS:
4157                         phba->sli4_hba.pc_sli4_params.supported = 1;
4158                         break;
4159                 default:
4160                         break;
4161                 }
4162         }
4163
4164         /* Read the port's SLI4 Parameters capabilities if supported. */
4165         if (phba->sli4_hba.pc_sli4_params.supported)
4166                 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4167         mempool_free(mboxq, phba->mbox_mem_pool);
4168         if (rc) {
4169                 rc = -EIO;
4170                 goto out_free_bsmbx;
4171         }
4172         /* Create all the SLI4 queues */
4173         rc = lpfc_sli4_queue_create(phba);
4174         if (rc)
4175                 goto out_free_bsmbx;
4176
4177         /* Create driver internal CQE event pool */
4178         rc = lpfc_sli4_cq_event_pool_create(phba);
4179         if (rc)
4180                 goto out_destroy_queue;
4181
4182         /* Initialize and populate the iocb list per host */
4183         rc = lpfc_init_sgl_list(phba);
4184         if (rc) {
4185                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4186                                 "1400 Failed to initialize sgl list.\n");
4187                 goto out_destroy_cq_event_pool;
4188         }
4189         rc = lpfc_init_active_sgl_array(phba);
4190         if (rc) {
4191                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4192                                 "1430 Failed to initialize sgl list.\n");
4193                 goto out_free_sgl_list;
4194         }
4195
4196         rc = lpfc_sli4_init_rpi_hdrs(phba);
4197         if (rc) {
4198                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4199                                 "1432 Failed to initialize rpi headers.\n");
4200                 goto out_free_active_sgl;
4201         }
4202
4203         /* Allocate eligible FCF bmask memory for FCF round robin failover */
4204         longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4205         phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4206                                          GFP_KERNEL);
4207         if (!phba->fcf.fcf_rr_bmask) {
4208                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4209                                 "2759 Failed allocate memory for FCF round "
4210                                 "robin failover bmask\n");
4211                 goto out_remove_rpi_hdrs;
4212         }
4213
4214         phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4215                                     phba->cfg_fcp_eq_count), GFP_KERNEL);
4216         if (!phba->sli4_hba.fcp_eq_hdl) {
4217                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4218                                 "2572 Failed allocate memory for fast-path "
4219                                 "per-EQ handle array\n");
4220                 goto out_free_fcf_rr_bmask;
4221         }
4222
4223         phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4224                                       phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4225         if (!phba->sli4_hba.msix_entries) {
4226                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4227                                 "2573 Failed allocate memory for msi-x "
4228                                 "interrupt vector entries\n");
4229                 goto out_free_fcp_eq_hdl;
4230         }
4231
4232         return rc;
4233
4234 out_free_fcp_eq_hdl:
4235         kfree(phba->sli4_hba.fcp_eq_hdl);
4236 out_free_fcf_rr_bmask:
4237         kfree(phba->fcf.fcf_rr_bmask);
4238 out_remove_rpi_hdrs:
4239         lpfc_sli4_remove_rpi_hdrs(phba);
4240 out_free_active_sgl:
4241         lpfc_free_active_sgl(phba);
4242 out_free_sgl_list:
4243         lpfc_free_sgl_list(phba);
4244 out_destroy_cq_event_pool:
4245         lpfc_sli4_cq_event_pool_destroy(phba);
4246 out_destroy_queue:
4247         lpfc_sli4_queue_destroy(phba);
4248 out_free_bsmbx:
4249         lpfc_destroy_bootstrap_mbox(phba);
4250 out_free_mem:
4251         lpfc_mem_free(phba);
4252         return rc;
4253 }
4254
4255 /**
4256  * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4257  * @phba: pointer to lpfc hba data structure.
4258  *
4259  * This routine is invoked to unset the driver internal resources set up
4260  * specific for supporting the SLI-4 HBA device it attached to.
4261  **/
4262 static void
4263 lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4264 {
4265         struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4266
4267         /* unregister default FCFI from the HBA */
4268         lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
4269
4270         /* Free the default FCR table */
4271         lpfc_sli_remove_dflt_fcf(phba);
4272
4273         /* Free memory allocated for msi-x interrupt vector entries */
4274         kfree(phba->sli4_hba.msix_entries);
4275
4276         /* Free memory allocated for fast-path work queue handles */
4277         kfree(phba->sli4_hba.fcp_eq_hdl);
4278
4279         /* Free the allocated rpi headers. */
4280         lpfc_sli4_remove_rpi_hdrs(phba);
4281         lpfc_sli4_remove_rpis(phba);
4282
4283         /* Free eligible FCF index bmask */
4284         kfree(phba->fcf.fcf_rr_bmask);
4285
4286         /* Free the ELS sgl list */
4287         lpfc_free_active_sgl(phba);
4288         lpfc_free_sgl_list(phba);
4289
4290         /* Free the SCSI sgl management array */
4291         kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4292
4293         /* Free the SLI4 queues */
4294         lpfc_sli4_queue_destroy(phba);
4295
4296         /* Free the completion queue EQ event pool */
4297         lpfc_sli4_cq_event_release_all(phba);
4298         lpfc_sli4_cq_event_pool_destroy(phba);
4299
4300         /* Reset SLI4 HBA FCoE function */
4301         lpfc_pci_function_reset(phba);
4302
4303         /* Free the bsmbx region. */
4304         lpfc_destroy_bootstrap_mbox(phba);
4305
4306         /* Free the SLI Layer memory with SLI4 HBAs */
4307         lpfc_mem_free_all(phba);
4308
4309         /* Free the current connect table */
4310         list_for_each_entry_safe(conn_entry, next_conn_entry,
4311                 &phba->fcf_conn_rec_list, list) {
4312                 list_del_init(&conn_entry->list);
4313                 kfree(conn_entry);
4314         }
4315
4316         return;
4317 }
4318
4319 /**
4320  * lpfc_init_api_table_setup - Set up init api fucntion jump table
4321  * @phba: The hba struct for which this call is being executed.
4322  * @dev_grp: The HBA PCI-Device group number.
4323  *
4324  * This routine sets up the device INIT interface API function jump table
4325  * in @phba struct.
4326  *
4327  * Returns: 0 - success, -ENODEV - failure.
4328  **/
4329 int
4330 lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4331 {
4332         phba->lpfc_hba_init_link = lpfc_hba_init_link;
4333         phba->lpfc_hba_down_link = lpfc_hba_down_link;
4334         switch (dev_grp) {
4335         case LPFC_PCI_DEV_LP:
4336                 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4337                 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4338                 phba->lpfc_stop_port = lpfc_stop_port_s3;
4339                 break;
4340         case LPFC_PCI_DEV_OC:
4341                 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4342                 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4343                 phba->lpfc_stop_port = lpfc_stop_port_s4;
4344                 break;
4345         default:
4346                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4347                                 "1431 Invalid HBA PCI-device group: 0x%x\n",
4348                                 dev_grp);
4349                 return -ENODEV;
4350                 break;
4351         }
4352         return 0;
4353 }
4354
4355 /**
4356  * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4357  * @phba: pointer to lpfc hba data structure.
4358  *
4359  * This routine is invoked to set up the driver internal resources before the
4360  * device specific resource setup to support the HBA device it attached to.
4361  *
4362  * Return codes
4363  *      0 - successful
4364  *      other values - error
4365  **/
4366 static int
4367 lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4368 {
4369         /*
4370          * Driver resources common to all SLI revisions
4371          */
4372         atomic_set(&phba->fast_event_count, 0);
4373         spin_lock_init(&phba->hbalock);
4374
4375         /* Initialize ndlp management spinlock */
4376         spin_lock_init(&phba->ndlp_lock);
4377
4378         INIT_LIST_HEAD(&phba->port_list);
4379         INIT_LIST_HEAD(&phba->work_list);
4380         init_waitqueue_head(&phba->wait_4_mlo_m_q);
4381
4382         /* Initialize the wait queue head for the kernel thread */
4383         init_waitqueue_head(&phba->work_waitq);
4384
4385         /* Initialize the scsi buffer list used by driver for scsi IO */
4386         spin_lock_init(&phba->scsi_buf_list_lock);
4387         INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
4388
4389         /* Initialize the fabric iocb list */
4390         INIT_LIST_HEAD(&phba->fabric_iocb_list);
4391
4392         /* Initialize list to save ELS buffers */
4393         INIT_LIST_HEAD(&phba->elsbuf);
4394
4395         /* Initialize FCF connection rec list */
4396         INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
4397
4398         return 0;
4399 }
4400
4401 /**
4402  * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4403  * @phba: pointer to lpfc hba data structure.
4404  *
4405  * This routine is invoked to set up the driver internal resources after the
4406  * device specific resource setup to support the HBA device it attached to.
4407  *
4408  * Return codes
4409  *      0 - successful
4410  *      other values - error
4411  **/
4412 static int
4413 lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4414 {
4415         int error;
4416
4417         /* Startup the kernel thread for this host adapter. */
4418         phba->worker_thread = kthread_run(lpfc_do_work, phba,
4419                                           "lpfc_worker_%d", phba->brd_no);
4420         if (IS_ERR(phba->worker_thread)) {
4421                 error = PTR_ERR(phba->worker_thread);
4422                 return error;
4423         }
4424
4425         return 0;
4426 }
4427
4428 /**
4429  * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4430  * @phba: pointer to lpfc hba data structure.
4431  *
4432  * This routine is invoked to unset the driver internal resources set up after
4433  * the device specific resource setup for supporting the HBA device it
4434  * attached to.
4435  **/
4436 static void
4437 lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4438 {
4439         /* Stop kernel worker thread */
4440         kthread_stop(phba->worker_thread);
4441 }
4442
4443 /**
4444  * lpfc_free_iocb_list - Free iocb list.
4445  * @phba: pointer to lpfc hba data structure.
4446  *
4447  * This routine is invoked to free the driver's IOCB list and memory.
4448  **/
4449 static void
4450 lpfc_free_iocb_list(struct lpfc_hba *phba)
4451 {
4452         struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4453
4454         spin_lock_irq(&phba->hbalock);
4455         list_for_each_entry_safe(iocbq_entry, iocbq_next,
4456                                  &phba->lpfc_iocb_list, list) {
4457                 list_del(&iocbq_entry->list);
4458                 kfree(iocbq_entry);
4459                 phba->total_iocbq_bufs--;
4460         }
4461         spin_unlock_irq(&phba->hbalock);
4462
4463         return;
4464 }
4465
4466 /**
4467  * lpfc_init_iocb_list - Allocate and initialize iocb list.
4468  * @phba: pointer to lpfc hba data structure.
4469  *
4470  * This routine is invoked to allocate and initizlize the driver's IOCB
4471  * list and set up the IOCB tag array accordingly.
4472  *
4473  * Return codes
4474  *      0 - successful
4475  *      other values - error
4476  **/
4477 static int
4478 lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4479 {
4480         struct lpfc_iocbq *iocbq_entry = NULL;
4481         uint16_t iotag;
4482         int i;
4483
4484         /* Initialize and populate the iocb list per host.  */
4485         INIT_LIST_HEAD(&phba->lpfc_iocb_list);
4486         for (i = 0; i < iocb_count; i++) {
4487                 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
4488                 if (iocbq_entry == NULL) {
4489                         printk(KERN_ERR "%s: only allocated %d iocbs of "
4490                                 "expected %d count. Unloading driver.\n",
4491                                 __func__, i, LPFC_IOCB_LIST_CNT);
4492                         goto out_free_iocbq;
4493                 }
4494
4495                 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4496                 if (iotag == 0) {
4497                         kfree(iocbq_entry);
4498                         printk(KERN_ERR "%s: failed to allocate IOTAG. "
4499                                 "Unloading driver.\n", __func__);
4500                         goto out_free_iocbq;
4501                 }
4502                 iocbq_entry->sli4_xritag = NO_XRI;
4503
4504                 spin_lock_irq(&phba->hbalock);
4505                 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4506                 phba->total_iocbq_bufs++;
4507                 spin_unlock_irq(&phba->hbalock);
4508         }
4509
4510         return 0;
4511
4512 out_free_iocbq:
4513         lpfc_free_iocb_list(phba);
4514
4515         return -ENOMEM;
4516 }
4517
4518 /**
4519  * lpfc_free_sgl_list - Free sgl list.
4520  * @phba: pointer to lpfc hba data structure.
4521  *
4522  * This routine is invoked to free the driver's sgl list and memory.
4523  **/
4524 static void
4525 lpfc_free_sgl_list(struct lpfc_hba *phba)
4526 {
4527         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4528         LIST_HEAD(sglq_list);
4529         int rc = 0;
4530
4531         spin_lock_irq(&phba->hbalock);
4532         list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4533         spin_unlock_irq(&phba->hbalock);
4534
4535         list_for_each_entry_safe(sglq_entry, sglq_next,
4536                                  &sglq_list, list) {
4537                 list_del(&sglq_entry->list);
4538                 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4539                 kfree(sglq_entry);
4540                 phba->sli4_hba.total_sglq_bufs--;
4541         }
4542         rc = lpfc_sli4_remove_all_sgl_pages(phba);
4543         if (rc) {
4544                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4545                         "2005 Unable to deregister pages from HBA: %x\n", rc);
4546         }
4547         kfree(phba->sli4_hba.lpfc_els_sgl_array);
4548 }
4549
4550 /**
4551  * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4552  * @phba: pointer to lpfc hba data structure.
4553  *
4554  * This routine is invoked to allocate the driver's active sgl memory.
4555  * This array will hold the sglq_entry's for active IOs.
4556  **/
4557 static int
4558 lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4559 {
4560         int size;
4561         size = sizeof(struct lpfc_sglq *);
4562         size *= phba->sli4_hba.max_cfg_param.max_xri;
4563
4564         phba->sli4_hba.lpfc_sglq_active_list =
4565                 kzalloc(size, GFP_KERNEL);
4566         if (!phba->sli4_hba.lpfc_sglq_active_list)
4567                 return -ENOMEM;
4568         return 0;
4569 }
4570
4571 /**
4572  * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4573  * @phba: pointer to lpfc hba data structure.
4574  *
4575  * This routine is invoked to walk through the array of active sglq entries
4576  * and free all of the resources.
4577  * This is just a place holder for now.
4578  **/
4579 static void
4580 lpfc_free_active_sgl(struct lpfc_hba *phba)
4581 {
4582         kfree(phba->sli4_hba.lpfc_sglq_active_list);
4583 }
4584
4585 /**
4586  * lpfc_init_sgl_list - Allocate and initialize sgl list.
4587  * @phba: pointer to lpfc hba data structure.
4588  *
4589  * This routine is invoked to allocate and initizlize the driver's sgl
4590  * list and set up the sgl xritag tag array accordingly.
4591  *
4592  * Return codes
4593  *      0 - successful
4594  *      other values - error
4595  **/
4596 static int
4597 lpfc_init_sgl_list(struct lpfc_hba *phba)
4598 {
4599         struct lpfc_sglq *sglq_entry = NULL;
4600         int i;
4601         int els_xri_cnt;
4602
4603         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4604         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4605                                 "2400 lpfc_init_sgl_list els %d.\n",
4606                                 els_xri_cnt);
4607         /* Initialize and populate the sglq list per host/VF. */
4608         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4609         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4610
4611         /* Sanity check on XRI management */
4612         if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4613                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4614                                 "2562 No room left for SCSI XRI allocation: "
4615                                 "max_xri=%d, els_xri=%d\n",
4616                                 phba->sli4_hba.max_cfg_param.max_xri,
4617                                 els_xri_cnt);
4618                 return -ENOMEM;
4619         }
4620
4621         /* Allocate memory for the ELS XRI management array */
4622         phba->sli4_hba.lpfc_els_sgl_array =
4623                         kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4624                         GFP_KERNEL);
4625
4626         if (!phba->sli4_hba.lpfc_els_sgl_array) {
4627                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4628                                 "2401 Failed to allocate memory for ELS "
4629                                 "XRI management array of size %d.\n",
4630                                 els_xri_cnt);
4631                 return -ENOMEM;
4632         }
4633
4634         /* Keep the SCSI XRI into the XRI management array */
4635         phba->sli4_hba.scsi_xri_max =
4636                         phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4637         phba->sli4_hba.scsi_xri_cnt = 0;
4638
4639         phba->sli4_hba.lpfc_scsi_psb_array =
4640                         kzalloc((sizeof(struct lpfc_scsi_buf *) *
4641                         phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4642
4643         if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4644                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4645                                 "2563 Failed to allocate memory for SCSI "
4646                                 "XRI management array of size %d.\n",
4647                                 phba->sli4_hba.scsi_xri_max);
4648                 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4649                 return -ENOMEM;
4650         }
4651
4652         for (i = 0; i < els_xri_cnt; i++) {
4653                 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4654                 if (sglq_entry == NULL) {
4655                         printk(KERN_ERR "%s: only allocated %d sgls of "
4656                                 "expected %d count. Unloading driver.\n",
4657                                 __func__, i, els_xri_cnt);
4658                         goto out_free_mem;
4659                 }
4660
4661                 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4662                 if (sglq_entry->sli4_xritag == NO_XRI) {
4663                         kfree(sglq_entry);
4664                         printk(KERN_ERR "%s: failed to allocate XRI.\n"
4665                                 "Unloading driver.\n", __func__);
4666                         goto out_free_mem;
4667                 }
4668                 sglq_entry->buff_type = GEN_BUFF_TYPE;
4669                 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4670                 if (sglq_entry->virt == NULL) {
4671                         kfree(sglq_entry);
4672                         printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4673                                 "Unloading driver.\n", __func__);
4674                         goto out_free_mem;
4675                 }
4676                 sglq_entry->sgl = sglq_entry->virt;
4677                 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4678
4679                 /* The list order is used by later block SGL registraton */
4680                 spin_lock_irq(&phba->hbalock);
4681                 sglq_entry->state = SGL_FREED;
4682                 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4683                 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4684                 phba->sli4_hba.total_sglq_bufs++;
4685                 spin_unlock_irq(&phba->hbalock);
4686         }
4687         return 0;
4688
4689 out_free_mem:
4690         kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4691         lpfc_free_sgl_list(phba);
4692         return -ENOMEM;
4693 }
4694
4695 /**
4696  * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4697  * @phba: pointer to lpfc hba data structure.
4698  *
4699  * This routine is invoked to post rpi header templates to the
4700  * HBA consistent with the SLI-4 interface spec.  This routine
4701  * posts a PAGE_SIZE memory region to the port to hold up to
4702  * PAGE_SIZE modulo 64 rpi context headers.
4703  * No locks are held here because this is an initialization routine
4704  * called only from probe or lpfc_online when interrupts are not
4705  * enabled and the driver is reinitializing the device.
4706  *
4707  * Return codes
4708  *      0 - successful
4709  *      ENOMEM - No availble memory
4710  *      EIO - The mailbox failed to complete successfully.
4711  **/
4712 int
4713 lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4714 {
4715         int rc = 0;
4716         int longs;
4717         uint16_t rpi_count;
4718         struct lpfc_rpi_hdr *rpi_hdr;
4719
4720         INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4721
4722         /*
4723          * Provision an rpi bitmask range for discovery. The total count
4724          * is the difference between max and base + 1.
4725          */
4726         rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4727                     phba->sli4_hba.max_cfg_param.max_rpi - 1;
4728
4729         longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4730         phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4731                                            GFP_KERNEL);
4732         if (!phba->sli4_hba.rpi_bmask)
4733                 return -ENOMEM;
4734
4735         rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4736         if (!rpi_hdr) {
4737                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4738                                 "0391 Error during rpi post operation\n");
4739                 lpfc_sli4_remove_rpis(phba);
4740                 rc = -ENODEV;
4741         }
4742
4743         return rc;
4744 }
4745
4746 /**
4747  * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4748  * @phba: pointer to lpfc hba data structure.
4749  *
4750  * This routine is invoked to allocate a single 4KB memory region to
4751  * support rpis and stores them in the phba.  This single region
4752  * provides support for up to 64 rpis.  The region is used globally
4753  * by the device.
4754  *
4755  * Returns:
4756  *   A valid rpi hdr on success.
4757  *   A NULL pointer on any failure.
4758  **/
4759 struct lpfc_rpi_hdr *
4760 lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4761 {
4762         uint16_t rpi_limit, curr_rpi_range;
4763         struct lpfc_dmabuf *dmabuf;
4764         struct lpfc_rpi_hdr *rpi_hdr;
4765
4766         rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4767                     phba->sli4_hba.max_cfg_param.max_rpi - 1;
4768
4769         spin_lock_irq(&phba->hbalock);
4770         curr_rpi_range = phba->sli4_hba.next_rpi;
4771         spin_unlock_irq(&phba->hbalock);
4772
4773         /*
4774          * The port has a limited number of rpis. The increment here
4775          * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4776          * and to allow the full max_rpi range per port.
4777          */
4778         if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4779                 return NULL;
4780
4781         /*
4782          * First allocate the protocol header region for the port.  The
4783          * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4784          */
4785         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4786         if (!dmabuf)
4787                 return NULL;
4788
4789         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4790                                           LPFC_HDR_TEMPLATE_SIZE,
4791                                           &dmabuf->phys,
4792                                           GFP_KERNEL);
4793         if (!dmabuf->virt) {
4794                 rpi_hdr = NULL;
4795                 goto err_free_dmabuf;
4796         }
4797
4798         memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4799         if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4800                 rpi_hdr = NULL;
4801                 goto err_free_coherent;
4802         }
4803
4804         /* Save the rpi header data for cleanup later. */
4805         rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4806         if (!rpi_hdr)
4807                 goto err_free_coherent;
4808
4809         rpi_hdr->dmabuf = dmabuf;
4810         rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4811         rpi_hdr->page_count = 1;
4812         spin_lock_irq(&phba->hbalock);
4813         rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4814         list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4815
4816         /*
4817          * The next_rpi stores the next module-64 rpi value to post
4818          * in any subsequent rpi memory region postings.
4819          */
4820         phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4821         spin_unlock_irq(&phba->hbalock);
4822         return rpi_hdr;
4823
4824  err_free_coherent:
4825         dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4826                           dmabuf->virt, dmabuf->phys);
4827  err_free_dmabuf:
4828         kfree(dmabuf);
4829         return NULL;
4830 }
4831
4832 /**
4833  * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4834  * @phba: pointer to lpfc hba data structure.
4835  *
4836  * This routine is invoked to remove all memory resources allocated
4837  * to support rpis. This routine presumes the caller has released all
4838  * rpis consumed by fabric or port logins and is prepared to have
4839  * the header pages removed.
4840  **/
4841 void
4842 lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4843 {
4844         struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4845
4846         list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4847                                  &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4848                 list_del(&rpi_hdr->list);
4849                 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4850                                   rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4851                 kfree(rpi_hdr->dmabuf);
4852                 kfree(rpi_hdr);
4853         }
4854
4855         phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4856         memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4857 }
4858
4859 /**
4860  * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4861  * @pdev: pointer to pci device data structure.
4862  *
4863  * This routine is invoked to allocate the driver hba data structure for an
4864  * HBA device. If the allocation is successful, the phba reference to the
4865  * PCI device data structure is set.
4866  *
4867  * Return codes
4868  *      pointer to @phba - successful
4869  *      NULL - error
4870  **/
4871 static struct lpfc_hba *
4872 lpfc_hba_alloc(struct pci_dev *pdev)
4873 {
4874         struct lpfc_hba *phba;
4875
4876         /* Allocate memory for HBA structure */
4877         phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4878         if (!phba) {
4879                 dev_err(&pdev->dev, "failed to allocate hba struct\n");
4880                 return NULL;
4881         }
4882
4883         /* Set reference to PCI device in HBA structure */
4884         phba->pcidev = pdev;
4885
4886         /* Assign an unused board number */
4887         phba->brd_no = lpfc_get_instance();
4888         if (phba->brd_no < 0) {
4889                 kfree(phba);
4890                 return NULL;
4891         }
4892
4893         spin_lock_init(&phba->ct_ev_lock);
4894         INIT_LIST_HEAD(&phba->ct_ev_waiters);
4895
4896         return phba;
4897 }
4898
4899 /**
4900  * lpfc_hba_free - Free driver hba data structure with a device.
4901  * @phba: pointer to lpfc hba data structure.
4902  *
4903  * This routine is invoked to free the driver hba data structure with an
4904  * HBA device.
4905  **/
4906 static void
4907 lpfc_hba_free(struct lpfc_hba *phba)
4908 {
4909         /* Release the driver assigned board number */
4910         idr_remove(&lpfc_hba_index, phba->brd_no);
4911
4912         kfree(phba);
4913         return;
4914 }
4915
4916 /**
4917  * lpfc_create_shost - Create hba physical port with associated scsi host.
4918  * @phba: pointer to lpfc hba data structure.
4919  *
4920  * This routine is invoked to create HBA physical port and associate a SCSI
4921  * host with it.
4922  *
4923  * Return codes
4924  *      0 - successful
4925  *      other values - error
4926  **/
4927 static int
4928 lpfc_create_shost(struct lpfc_hba *phba)
4929 {
4930         struct lpfc_vport *vport;
4931         struct Scsi_Host  *shost;
4932
4933         /* Initialize HBA FC structure */
4934         phba->fc_edtov = FF_DEF_EDTOV;
4935         phba->fc_ratov = FF_DEF_RATOV;
4936         phba->fc_altov = FF_DEF_ALTOV;
4937         phba->fc_arbtov = FF_DEF_ARBTOV;
4938
4939         atomic_set(&phba->sdev_cnt, 0);
4940         vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
4941         if (!vport)
4942                 return -ENODEV;
4943
4944         shost = lpfc_shost_from_vport(vport);
4945         phba->pport = vport;
4946         lpfc_debugfs_initialize(vport);
4947         /* Put reference to SCSI host to driver's device private data */
4948         pci_set_drvdata(phba->pcidev, shost);
4949
4950         return 0;
4951 }
4952
4953 /**
4954  * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4955  * @phba: pointer to lpfc hba data structure.
4956  *
4957  * This routine is invoked to destroy HBA physical port and the associated
4958  * SCSI host.
4959  **/
4960 static void
4961 lpfc_destroy_shost(struct lpfc_hba *phba)
4962 {
4963         struct lpfc_vport *vport = phba->pport;
4964
4965         /* Destroy physical port that associated with the SCSI host */
4966         destroy_port(vport);
4967
4968         return;
4969 }
4970
4971 /**
4972  * lpfc_setup_bg - Setup Block guard structures and debug areas.
4973  * @phba: pointer to lpfc hba data structure.
4974  * @shost: the shost to be used to detect Block guard settings.
4975  *
4976  * This routine sets up the local Block guard protocol settings for @shost.
4977  * This routine also allocates memory for debugging bg buffers.
4978  **/
4979 static void
4980 lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4981 {
4982         int pagecnt = 10;
4983         if (lpfc_prot_mask && lpfc_prot_guard) {
4984                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4985                                 "1478 Registering BlockGuard with the "
4986                                 "SCSI layer\n");
4987                 scsi_host_set_prot(shost, lpfc_prot_mask);
4988                 scsi_host_set_guard(shost, lpfc_prot_guard);
4989         }
4990         if (!_dump_buf_data) {
4991                 while (pagecnt) {
4992                         spin_lock_init(&_dump_buf_lock);
4993                         _dump_buf_data =
4994                                 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4995                         if (_dump_buf_data) {
4996                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4997                                         "9043 BLKGRD: allocated %d pages for "
4998                                        "_dump_buf_data at 0x%p\n",
4999                                        (1 << pagecnt), _dump_buf_data);
5000                                 _dump_buf_data_order = pagecnt;
5001                                 memset(_dump_buf_data, 0,
5002                                        ((1 << PAGE_SHIFT) << pagecnt));
5003                                 break;
5004                         } else
5005                                 --pagecnt;
5006                 }
5007                 if (!_dump_buf_data_order)
5008                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5009                                 "9044 BLKGRD: ERROR unable to allocate "
5010                                "memory for hexdump\n");
5011         } else
5012                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5013                         "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
5014                        "\n", _dump_buf_data);
5015         if (!_dump_buf_dif) {
5016                 while (pagecnt) {
5017                         _dump_buf_dif =
5018                                 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5019                         if (_dump_buf_dif) {
5020                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5021                                         "9046 BLKGRD: allocated %d pages for "
5022                                        "_dump_buf_dif at 0x%p\n",
5023                                        (1 << pagecnt), _dump_buf_dif);
5024                                 _dump_buf_dif_order = pagecnt;
5025                                 memset(_dump_buf_dif, 0,
5026                                        ((1 << PAGE_SHIFT) << pagecnt));
5027                                 break;
5028                         } else
5029                                 --pagecnt;
5030                 }
5031                 if (!_dump_buf_dif_order)
5032                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5033                         "9047 BLKGRD: ERROR unable to allocate "
5034                                "memory for hexdump\n");
5035         } else
5036                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5037                         "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
5038                        _dump_buf_dif);
5039 }
5040
5041 /**
5042  * lpfc_post_init_setup - Perform necessary device post initialization setup.
5043  * @phba: pointer to lpfc hba data structure.
5044  *
5045  * This routine is invoked to perform all the necessary post initialization
5046  * setup for the device.
5047  **/
5048 static void
5049 lpfc_post_init_setup(struct lpfc_hba *phba)
5050 {
5051         struct Scsi_Host  *shost;
5052         struct lpfc_adapter_event_header adapter_event;
5053
5054         /* Get the default values for Model Name and Description */
5055         lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5056
5057         /*
5058          * hba setup may have changed the hba_queue_depth so we need to
5059          * adjust the value of can_queue.
5060          */
5061         shost = pci_get_drvdata(phba->pcidev);
5062         shost->can_queue = phba->cfg_hba_queue_depth - 10;
5063         if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5064                 lpfc_setup_bg(phba, shost);
5065
5066         lpfc_host_attrib_init(shost);
5067
5068         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5069                 spin_lock_irq(shost->host_lock);
5070                 lpfc_poll_start_timer(phba);
5071                 spin_unlock_irq(shost->host_lock);
5072         }
5073
5074         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5075                         "0428 Perform SCSI scan\n");
5076         /* Send board arrival event to upper layer */
5077         adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5078         adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5079         fc_host_post_vendor_event(shost, fc_get_event_number(),
5080                                   sizeof(adapter_event),
5081                                   (char *) &adapter_event,
5082                                   LPFC_NL_VENDOR_ID);
5083         return;
5084 }
5085
5086 /**
5087  * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5088  * @phba: pointer to lpfc hba data structure.
5089  *
5090  * This routine is invoked to set up the PCI device memory space for device
5091  * with SLI-3 interface spec.
5092  *
5093  * Return codes
5094  *      0 - successful
5095  *      other values - error
5096  **/
5097 static int
5098 lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5099 {
5100         struct pci_dev *pdev;
5101         unsigned long bar0map_len, bar2map_len;
5102         int i, hbq_count;
5103         void *ptr;
5104         int error = -ENODEV;
5105
5106         /* Obtain PCI device reference */
5107         if (!phba->pcidev)
5108                 return error;
5109         else
5110                 pdev = phba->pcidev;
5111
5112         /* Set the device DMA mask size */
5113         if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5114          || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5115                 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5116                  || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
5117                         return error;
5118                 }
5119         }
5120
5121         /* Get the bus address of Bar0 and Bar2 and the number of bytes
5122          * required by each mapping.
5123          */
5124         phba->pci_bar0_map = pci_resource_start(pdev, 0);
5125         bar0map_len = pci_resource_len(pdev, 0);
5126
5127         phba->pci_bar2_map = pci_resource_start(pdev, 2);
5128         bar2map_len = pci_resource_len(pdev, 2);
5129
5130         /* Map HBA SLIM to a kernel virtual address. */
5131         phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5132         if (!phba->slim_memmap_p) {
5133                 dev_printk(KERN_ERR, &pdev->dev,
5134                            "ioremap failed for SLIM memory.\n");
5135                 goto out;
5136         }
5137
5138         /* Map HBA Control Registers to a kernel virtual address. */
5139         phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5140         if (!phba->ctrl_regs_memmap_p) {
5141                 dev_printk(KERN_ERR, &pdev->dev,
5142                            "ioremap failed for HBA control registers.\n");
5143                 goto out_iounmap_slim;
5144         }
5145
5146         /* Allocate memory for SLI-2 structures */
5147         phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5148                                                SLI2_SLIM_SIZE,
5149                                                &phba->slim2p.phys,
5150                                                GFP_KERNEL);
5151         if (!phba->slim2p.virt)
5152                 goto out_iounmap;
5153
5154         memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5155         phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
5156         phba->mbox_ext = (phba->slim2p.virt +
5157                 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
5158         phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5159         phba->IOCBs = (phba->slim2p.virt +
5160                        offsetof(struct lpfc_sli2_slim, IOCBs));
5161
5162         phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5163                                                  lpfc_sli_hbq_size(),
5164                                                  &phba->hbqslimp.phys,
5165                                                  GFP_KERNEL);
5166         if (!phba->hbqslimp.virt)
5167                 goto out_free_slim;
5168
5169         hbq_count = lpfc_sli_hbq_count();
5170         ptr = phba->hbqslimp.virt;
5171         for (i = 0; i < hbq_count; ++i) {
5172                 phba->hbqs[i].hbq_virt = ptr;
5173                 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5174                 ptr += (lpfc_hbq_defs[i]->entry_count *
5175                         sizeof(struct lpfc_hbq_entry));
5176         }
5177         phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5178         phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5179
5180         memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5181
5182         INIT_LIST_HEAD(&phba->rb_pend_list);
5183
5184         phba->MBslimaddr = phba->slim_memmap_p;
5185         phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5186         phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5187         phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5188         phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
5189
5190         return 0;
5191
5192 out_free_slim:
5193         dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5194                           phba->slim2p.virt, phba->slim2p.phys);
5195 out_iounmap:
5196         iounmap(phba->ctrl_regs_memmap_p);
5197 out_iounmap_slim:
5198         iounmap(phba->slim_memmap_p);
5199 out:
5200         return error;
5201 }
5202
5203 /**
5204  * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5205  * @phba: pointer to lpfc hba data structure.
5206  *
5207  * This routine is invoked to unset the PCI device memory space for device
5208  * with SLI-3 interface spec.
5209  **/
5210 static void
5211 lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5212 {
5213         struct pci_dev *pdev;
5214
5215         /* Obtain PCI device reference */
5216         if (!phba->pcidev)
5217                 return;
5218         else
5219                 pdev = phba->pcidev;
5220
5221         /* Free coherent DMA memory allocated */
5222         dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5223                           phba->hbqslimp.virt, phba->hbqslimp.phys);
5224         dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5225                           phba->slim2p.virt, phba->slim2p.phys);
5226
5227         /* I/O memory unmap */
5228         iounmap(phba->ctrl_regs_memmap_p);
5229         iounmap(phba->slim_memmap_p);
5230
5231         return;
5232 }
5233
5234 /**
5235  * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5236  * @phba: pointer to lpfc hba data structure.
5237  *
5238  * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5239  * done and check status.
5240  *
5241  * Return 0 if successful, otherwise -ENODEV.
5242  **/
5243 int
5244 lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5245 {
5246         struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg;
5247         int i, port_error = -ENODEV;
5248
5249         if (!phba->sli4_hba.STAregaddr)
5250                 return -ENODEV;
5251
5252         /* Wait up to 30 seconds for the SLI Port POST done and ready */
5253         for (i = 0; i < 3000; i++) {
5254                 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
5255                 /* Encounter fatal POST error, break out */
5256                 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
5257                         port_error = -ENODEV;
5258                         break;
5259                 }
5260                 if (LPFC_POST_STAGE_ARMFW_READY ==
5261                     bf_get(lpfc_hst_state_port_status, &sta_reg)) {
5262                         port_error = 0;
5263                         break;
5264                 }
5265                 msleep(10);
5266         }
5267
5268         if (port_error)
5269                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5270                         "1408 Failure HBA POST Status: sta_reg=0x%x, "
5271                         "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
5272                         "dl=x%x, pstatus=x%x\n", sta_reg.word0,
5273                         bf_get(lpfc_hst_state_perr, &sta_reg),
5274                         bf_get(lpfc_hst_state_sfi, &sta_reg),
5275                         bf_get(lpfc_hst_state_nip, &sta_reg),
5276                         bf_get(lpfc_hst_state_ipc, &sta_reg),
5277                         bf_get(lpfc_hst_state_xrom, &sta_reg),
5278                         bf_get(lpfc_hst_state_dl, &sta_reg),
5279                         bf_get(lpfc_hst_state_port_status, &sta_reg));
5280
5281         /* Log device information */
5282         phba->sli4_hba.sli_intf.word0 = readl(phba->sli4_hba.SLIINTFregaddr);
5283         if (bf_get(lpfc_sli_intf_valid,
5284                    &phba->sli4_hba.sli_intf) == LPFC_SLI_INTF_VALID) {
5285                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5286                                 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
5287                                 "FeatureL1=0x%x, FeatureL2=0x%x\n",
5288                                 bf_get(lpfc_sli_intf_sli_family,
5289                                        &phba->sli4_hba.sli_intf),
5290                                 bf_get(lpfc_sli_intf_slirev,
5291                                        &phba->sli4_hba.sli_intf),
5292                                 bf_get(lpfc_sli_intf_featurelevel1,
5293                                        &phba->sli4_hba.sli_intf),
5294                                 bf_get(lpfc_sli_intf_featurelevel2,
5295                                        &phba->sli4_hba.sli_intf));
5296         }
5297         phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
5298         phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
5299         /* With uncoverable error, log the error message and return error */
5300         uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
5301         uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
5302         if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5303             (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5304                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5305                                 "1422 HBA Unrecoverable error: "
5306                                 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
5307                                 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
5308                                 uerrlo_reg.word0, uerrhi_reg.word0,
5309                                 phba->sli4_hba.ue_mask_lo,
5310                                 phba->sli4_hba.ue_mask_hi);
5311                 return -ENODEV;
5312         }
5313
5314         return port_error;
5315 }
5316
5317 /**
5318  * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5319  * @phba: pointer to lpfc hba data structure.
5320  *
5321  * This routine is invoked to set up SLI4 BAR0 PCI config space register
5322  * memory map.
5323  **/
5324 static void
5325 lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
5326 {
5327         phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5328                                         LPFC_UERR_STATUS_LO;
5329         phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5330                                         LPFC_UERR_STATUS_HI;
5331         phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5332                                         LPFC_UE_MASK_LO;
5333         phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5334                                         LPFC_UE_MASK_HI;
5335         phba->sli4_hba.SLIINTFregaddr = phba->sli4_hba.conf_regs_memmap_p +
5336                                         LPFC_SLI_INTF;
5337 }
5338
5339 /**
5340  * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5341  * @phba: pointer to lpfc hba data structure.
5342  *
5343  * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5344  * memory map.
5345  **/
5346 static void
5347 lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5348 {
5349
5350         phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5351                                     LPFC_HST_STATE;
5352         phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5353                                     LPFC_HST_ISR0;
5354         phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5355                                     LPFC_HST_IMR0;
5356         phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5357                                      LPFC_HST_ISCR0;
5358         return;
5359 }
5360
5361 /**
5362  * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5363  * @phba: pointer to lpfc hba data structure.
5364  * @vf: virtual function number
5365  *
5366  * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5367  * based on the given viftual function number, @vf.
5368  *
5369  * Return 0 if successful, otherwise -ENODEV.
5370  **/
5371 static int
5372 lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5373 {
5374         if (vf > LPFC_VIR_FUNC_MAX)
5375                 return -ENODEV;
5376
5377         phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5378                                 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5379         phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5380                                 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5381         phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5382                                 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5383         phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5384                                 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5385         phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5386                                 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5387         return 0;
5388 }
5389
5390 /**
5391  * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5392  * @phba: pointer to lpfc hba data structure.
5393  *
5394  * This routine is invoked to create the bootstrap mailbox
5395  * region consistent with the SLI-4 interface spec.  This
5396  * routine allocates all memory necessary to communicate
5397  * mailbox commands to the port and sets up all alignment
5398  * needs.  No locks are expected to be held when calling
5399  * this routine.
5400  *
5401  * Return codes
5402  *      0 - successful
5403  *      ENOMEM - could not allocated memory.
5404  **/
5405 static int
5406 lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5407 {
5408         uint32_t bmbx_size;
5409         struct lpfc_dmabuf *dmabuf;
5410         struct dma_address *dma_address;
5411         uint32_t pa_addr;
5412         uint64_t phys_addr;
5413
5414         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5415         if (!dmabuf)
5416                 return -ENOMEM;
5417
5418         /*
5419          * The bootstrap mailbox region is comprised of 2 parts
5420          * plus an alignment restriction of 16 bytes.
5421          */
5422         bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5423         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5424                                           bmbx_size,
5425                                           &dmabuf->phys,
5426                                           GFP_KERNEL);
5427         if (!dmabuf->virt) {
5428                 kfree(dmabuf);
5429                 return -ENOMEM;
5430         }
5431         memset(dmabuf->virt, 0, bmbx_size);
5432
5433         /*
5434          * Initialize the bootstrap mailbox pointers now so that the register
5435          * operations are simple later.  The mailbox dma address is required
5436          * to be 16-byte aligned.  Also align the virtual memory as each
5437          * maibox is copied into the bmbx mailbox region before issuing the
5438          * command to the port.
5439          */
5440         phba->sli4_hba.bmbx.dmabuf = dmabuf;
5441         phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5442
5443         phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5444                                               LPFC_ALIGN_16_BYTE);
5445         phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5446                                               LPFC_ALIGN_16_BYTE);
5447
5448         /*
5449          * Set the high and low physical addresses now.  The SLI4 alignment
5450          * requirement is 16 bytes and the mailbox is posted to the port
5451          * as two 30-bit addresses.  The other data is a bit marking whether
5452          * the 30-bit address is the high or low address.
5453          * Upcast bmbx aphys to 64bits so shift instruction compiles
5454          * clean on 32 bit machines.
5455          */
5456         dma_address = &phba->sli4_hba.bmbx.dma_address;
5457         phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5458         pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5459         dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5460                                            LPFC_BMBX_BIT1_ADDR_HI);
5461
5462         pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5463         dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5464                                            LPFC_BMBX_BIT1_ADDR_LO);
5465         return 0;
5466 }
5467
5468 /**
5469  * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5470  * @phba: pointer to lpfc hba data structure.
5471  *
5472  * This routine is invoked to teardown the bootstrap mailbox
5473  * region and release all host resources. This routine requires
5474  * the caller to ensure all mailbox commands recovered, no
5475  * additional mailbox comands are sent, and interrupts are disabled
5476  * before calling this routine.
5477  *
5478  **/
5479 static void
5480 lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5481 {
5482         dma_free_coherent(&phba->pcidev->dev,
5483                           phba->sli4_hba.bmbx.bmbx_size,
5484                           phba->sli4_hba.bmbx.dmabuf->virt,
5485                           phba->sli4_hba.bmbx.dmabuf->phys);
5486
5487         kfree(phba->sli4_hba.bmbx.dmabuf);
5488         memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5489 }
5490
5491 /**
5492  * lpfc_sli4_read_config - Get the config parameters.
5493  * @phba: pointer to lpfc hba data structure.
5494  *
5495  * This routine is invoked to read the configuration parameters from the HBA.
5496  * The configuration parameters are used to set the base and maximum values
5497  * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5498  * allocation for the port.
5499  *
5500  * Return codes
5501  *      0 - successful
5502  *      ENOMEM - No availble memory
5503  *      EIO - The mailbox failed to complete successfully.
5504  **/
5505 static int
5506 lpfc_sli4_read_config(struct lpfc_hba *phba)
5507 {
5508         LPFC_MBOXQ_t *pmb;
5509         struct lpfc_mbx_read_config *rd_config;
5510         uint32_t rc = 0;
5511
5512         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5513         if (!pmb) {
5514                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5515                                 "2011 Unable to allocate memory for issuing "
5516                                 "SLI_CONFIG_SPECIAL mailbox command\n");
5517                 return -ENOMEM;
5518         }
5519
5520         lpfc_read_config(phba, pmb);
5521
5522         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5523         if (rc != MBX_SUCCESS) {
5524                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5525                         "2012 Mailbox failed , mbxCmd x%x "
5526                         "READ_CONFIG, mbxStatus x%x\n",
5527                         bf_get(lpfc_mqe_command, &pmb->u.mqe),
5528                         bf_get(lpfc_mqe_status, &pmb->u.mqe));
5529                 rc = -EIO;
5530         } else {
5531                 rd_config = &pmb->u.mqe.un.rd_config;
5532                 phba->sli4_hba.max_cfg_param.max_xri =
5533                         bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5534                 phba->sli4_hba.max_cfg_param.xri_base =
5535                         bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5536                 phba->sli4_hba.max_cfg_param.max_vpi =
5537                         bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5538                 phba->sli4_hba.max_cfg_param.vpi_base =
5539                         bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5540                 phba->sli4_hba.max_cfg_param.max_rpi =
5541                         bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5542                 phba->sli4_hba.max_cfg_param.rpi_base =
5543                         bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5544                 phba->sli4_hba.max_cfg_param.max_vfi =
5545                         bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5546                 phba->sli4_hba.max_cfg_param.vfi_base =
5547                         bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5548                 phba->sli4_hba.max_cfg_param.max_fcfi =
5549                         bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5550                 phba->sli4_hba.max_cfg_param.fcfi_base =
5551                         bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5552                 phba->sli4_hba.max_cfg_param.max_eq =
5553                         bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5554                 phba->sli4_hba.max_cfg_param.max_rq =
5555                         bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5556                 phba->sli4_hba.max_cfg_param.max_wq =
5557                         bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5558                 phba->sli4_hba.max_cfg_param.max_cq =
5559                         bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5560                 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5561                 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5562                 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5563                 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5564                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
5565                 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5566                                 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
5567                 phba->max_vports = phba->max_vpi;
5568                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5569                                 "2003 cfg params XRI(B:%d M:%d), "
5570                                 "VPI(B:%d M:%d) "
5571                                 "VFI(B:%d M:%d) "
5572                                 "RPI(B:%d M:%d) "
5573                                 "FCFI(B:%d M:%d)\n",
5574                                 phba->sli4_hba.max_cfg_param.xri_base,
5575                                 phba->sli4_hba.max_cfg_param.max_xri,
5576                                 phba->sli4_hba.max_cfg_param.vpi_base,
5577                                 phba->sli4_hba.max_cfg_param.max_vpi,
5578                                 phba->sli4_hba.max_cfg_param.vfi_base,
5579                                 phba->sli4_hba.max_cfg_param.max_vfi,
5580                                 phba->sli4_hba.max_cfg_param.rpi_base,
5581                                 phba->sli4_hba.max_cfg_param.max_rpi,
5582                                 phba->sli4_hba.max_cfg_param.fcfi_base,
5583                                 phba->sli4_hba.max_cfg_param.max_fcfi);
5584         }
5585         mempool_free(pmb, phba->mbox_mem_pool);
5586
5587         /* Reset the DFT_HBA_Q_DEPTH to the max xri  */
5588         if (phba->cfg_hba_queue_depth >
5589                 (phba->sli4_hba.max_cfg_param.max_xri -
5590                         lpfc_sli4_get_els_iocb_cnt(phba)))
5591                 phba->cfg_hba_queue_depth =
5592                         phba->sli4_hba.max_cfg_param.max_xri -
5593                                 lpfc_sli4_get_els_iocb_cnt(phba);
5594         return rc;
5595 }
5596
5597 /**
5598  * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
5599  * @phba: pointer to lpfc hba data structure.
5600  *
5601  * This routine is invoked to setup the host-side endian order to the
5602  * HBA consistent with the SLI-4 interface spec.
5603  *
5604  * Return codes
5605  *      0 - successful
5606  *      ENOMEM - No availble memory
5607  *      EIO - The mailbox failed to complete successfully.
5608  **/
5609 static int
5610 lpfc_setup_endian_order(struct lpfc_hba *phba)
5611 {
5612         LPFC_MBOXQ_t *mboxq;
5613         uint32_t rc = 0;
5614         uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5615                                       HOST_ENDIAN_HIGH_WORD1};
5616
5617         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5618         if (!mboxq) {
5619                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5620                                 "0492 Unable to allocate memory for issuing "
5621                                 "SLI_CONFIG_SPECIAL mailbox command\n");
5622                 return -ENOMEM;
5623         }
5624
5625         /*
5626          * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5627          * words to contain special data values and no other data.
5628          */
5629         memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5630         memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5631         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5632         if (rc != MBX_SUCCESS) {
5633                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5634                                 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5635                                 "status x%x\n",
5636                                 rc);
5637                 rc = -EIO;
5638         }
5639
5640         mempool_free(mboxq, phba->mbox_mem_pool);
5641         return rc;
5642 }
5643
5644 /**
5645  * lpfc_sli4_queue_create - Create all the SLI4 queues
5646  * @phba: pointer to lpfc hba data structure.
5647  *
5648  * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5649  * operation. For each SLI4 queue type, the parameters such as queue entry
5650  * count (queue depth) shall be taken from the module parameter. For now,
5651  * we just use some constant number as place holder.
5652  *
5653  * Return codes
5654  *      0 - successful
5655  *      ENOMEM - No availble memory
5656  *      EIO - The mailbox failed to complete successfully.
5657  **/
5658 static int
5659 lpfc_sli4_queue_create(struct lpfc_hba *phba)
5660 {
5661         struct lpfc_queue *qdesc;
5662         int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5663         int cfg_fcp_wq_count;
5664         int cfg_fcp_eq_count;
5665
5666         /*
5667          * Sanity check for confiugred queue parameters against the run-time
5668          * device parameters
5669          */
5670
5671         /* Sanity check on FCP fast-path WQ parameters */
5672         cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5673         if (cfg_fcp_wq_count >
5674             (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5675                 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5676                                    LPFC_SP_WQN_DEF;
5677                 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5678                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5679                                         "2581 Not enough WQs (%d) from "
5680                                         "the pci function for supporting "
5681                                         "FCP WQs (%d)\n",
5682                                         phba->sli4_hba.max_cfg_param.max_wq,
5683                                         phba->cfg_fcp_wq_count);
5684                         goto out_error;
5685                 }
5686                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5687                                 "2582 Not enough WQs (%d) from the pci "
5688                                 "function for supporting the requested "
5689                                 "FCP WQs (%d), the actual FCP WQs can "
5690                                 "be supported: %d\n",
5691                                 phba->sli4_hba.max_cfg_param.max_wq,
5692                                 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5693         }
5694         /* The actual number of FCP work queues adopted */
5695         phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5696
5697         /* Sanity check on FCP fast-path EQ parameters */
5698         cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5699         if (cfg_fcp_eq_count >
5700             (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5701                 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5702                                    LPFC_SP_EQN_DEF;
5703                 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5704                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5705                                         "2574 Not enough EQs (%d) from the "
5706                                         "pci function for supporting FCP "
5707                                         "EQs (%d)\n",
5708                                         phba->sli4_hba.max_cfg_param.max_eq,
5709                                         phba->cfg_fcp_eq_count);
5710                         goto out_error;
5711                 }
5712                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5713                                 "2575 Not enough EQs (%d) from the pci "
5714                                 "function for supporting the requested "
5715                                 "FCP EQs (%d), the actual FCP EQs can "
5716                                 "be supported: %d\n",
5717                                 phba->sli4_hba.max_cfg_param.max_eq,
5718                                 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5719         }
5720         /* It does not make sense to have more EQs than WQs */
5721         if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5722                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5723                                 "2593 The FCP EQ count(%d) cannot be greater "
5724                                 "than the FCP WQ count(%d), limiting the "
5725                                 "FCP EQ count to %d\n", cfg_fcp_eq_count,
5726                                 phba->cfg_fcp_wq_count,
5727                                 phba->cfg_fcp_wq_count);
5728                 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5729         }
5730         /* The actual number of FCP event queues adopted */
5731         phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5732         /* The overall number of event queues used */
5733         phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5734
5735         /*
5736          * Create Event Queues (EQs)
5737          */
5738
5739         /* Get EQ depth from module parameter, fake the default for now */
5740         phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5741         phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5742
5743         /* Create slow path event queue */
5744         qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5745                                       phba->sli4_hba.eq_ecount);
5746         if (!qdesc) {
5747                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5748                                 "0496 Failed allocate slow-path EQ\n");
5749                 goto out_error;
5750         }
5751         phba->sli4_hba.sp_eq = qdesc;
5752
5753         /* Create fast-path FCP Event Queue(s) */
5754         phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5755                                phba->cfg_fcp_eq_count), GFP_KERNEL);
5756         if (!phba->sli4_hba.fp_eq) {
5757                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5758                                 "2576 Failed allocate memory for fast-path "
5759                                 "EQ record array\n");
5760                 goto out_free_sp_eq;
5761         }
5762         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5763                 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5764                                               phba->sli4_hba.eq_ecount);
5765                 if (!qdesc) {
5766                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5767                                         "0497 Failed allocate fast-path EQ\n");
5768                         goto out_free_fp_eq;
5769                 }
5770                 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5771         }
5772
5773         /*
5774          * Create Complete Queues (CQs)
5775          */
5776
5777         /* Get CQ depth from module parameter, fake the default for now */
5778         phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5779         phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5780
5781         /* Create slow-path Mailbox Command Complete Queue */
5782         qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5783                                       phba->sli4_hba.cq_ecount);
5784         if (!qdesc) {
5785                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5786                                 "0500 Failed allocate slow-path mailbox CQ\n");
5787                 goto out_free_fp_eq;
5788         }
5789         phba->sli4_hba.mbx_cq = qdesc;
5790
5791         /* Create slow-path ELS Complete Queue&