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