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