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