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