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