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