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