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