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