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