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