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