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