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