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