blob: cb465b253910b9972eb48639d4527162a7a0b0b9 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd4379ac2012-03-01 22:37:07 -05004 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * 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. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#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>
Paul Gortmakeracf33682011-05-27 09:47:43 -040027#include <linux/module.h>
dea31012005-04-17 16:05:31 -050028#include <linux/kthread.h>
29#include <linux/pci.h>
30#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050031#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040032#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040034#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050035#include <linux/miscdevice.h>
James Smart7bb03bb2013-04-17 20:19:16 -040036#include <linux/percpu.h>
dea31012005-04-17 16:05:31 -050037
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040038#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050039#include <scsi/scsi_device.h>
40#include <scsi/scsi_host.h>
41#include <scsi/scsi_transport_fc.h>
42
James Smartda0436e2009-05-22 14:51:39 -040043#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_hw.h"
45#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040046#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040047#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc_disc.h"
49#include "lpfc_scsi.h"
50#include "lpfc.h"
51#include "lpfc_logmsg.h"
52#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050053#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050054#include "lpfc_version.h"
55
James Smart81301a92008-12-04 22:39:46 -050056char *_dump_buf_data;
57unsigned long _dump_buf_data_order;
58char *_dump_buf_dif;
59unsigned long _dump_buf_dif_order;
60spinlock_t _dump_buf_lock;
61
James Smart7bb03bb2013-04-17 20:19:16 -040062/* Used when mapping IRQ vectors in a driver centric manner */
63uint16_t lpfc_used_cpu[LPFC_MAX_CPU];
64
dea31012005-04-17 16:05:31 -050065static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
66static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040067static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040068static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
69static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040070static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040071static void lpfc_free_els_sgl_list(struct lpfc_hba *);
72static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040073static int lpfc_init_active_sgl_array(struct lpfc_hba *);
74static void lpfc_free_active_sgl(struct lpfc_hba *);
75static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
76static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
77static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
78static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
79static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040080static void lpfc_sli4_disable_intr(struct lpfc_hba *);
81static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
dea31012005-04-17 16:05:31 -050082
83static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050084static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050085static DEFINE_IDR(lpfc_hba_index);
86
James Smarte59058c2008-08-24 21:49:00 -040087/**
James Smart3621a712009-04-06 18:47:14 -040088 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040089 * @phba: pointer to lpfc hba data structure.
90 *
91 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
92 * mailbox command. It retrieves the revision information from the HBA and
93 * collects the Vital Product Data (VPD) about the HBA for preparing the
94 * configuration of the HBA.
95 *
96 * Return codes:
97 * 0 - success.
98 * -ERESTART - requests the SLI layer to reset the HBA and try again.
99 * Any other value - indicates an error.
100 **/
dea31012005-04-17 16:05:31 -0500101int
James Smart2e0fef82007-06-17 19:56:36 -0500102lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500103{
104 lpfc_vpd_t *vp = &phba->vpd;
105 int i = 0, rc;
106 LPFC_MBOXQ_t *pmb;
107 MAILBOX_t *mb;
108 char *lpfc_vpd_data = NULL;
109 uint16_t offset = 0;
110 static char licensed[56] =
111 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400112 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500113
114 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
115 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500116 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500117 return -ENOMEM;
118 }
119
James Smart04c68492009-05-22 14:52:52 -0400120 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500121 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500122
123 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400124 if (init_key) {
125 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500126
James Smart65a29c12006-07-06 15:50:50 -0400127 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
128 *ptext = cpu_to_be32(*ptext);
129 init_key = 0;
130 }
dea31012005-04-17 16:05:31 -0500131
132 lpfc_read_nv(phba, pmb);
133 memset((char*)mb->un.varRDnvp.rsvd3, 0,
134 sizeof (mb->un.varRDnvp.rsvd3));
135 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
136 sizeof (licensed));
137
138 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
139
140 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500141 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400142 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500143 "error, mbxCmd x%x READ_NVPARM, "
144 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500145 mb->mbxCommand, mb->mbxStatus);
146 mempool_free(pmb, phba->mbox_mem_pool);
147 return -ERESTART;
148 }
149 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500150 sizeof(phba->wwnn));
151 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
152 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500153 }
154
James Smart92d7f7b2007-06-17 19:56:38 -0500155 phba->sli3_options = 0x0;
156
dea31012005-04-17 16:05:31 -0500157 /* Setup and issue mailbox READ REV command */
158 lpfc_read_rev(phba, pmb);
159 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
160 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400162 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500163 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500164 mb->mbxCommand, mb->mbxStatus);
165 mempool_free( pmb, phba->mbox_mem_pool);
166 return -ERESTART;
167 }
168
James Smart92d7f7b2007-06-17 19:56:38 -0500169
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500170 /*
171 * The value of rr must be 1 since the driver set the cv field to 1.
172 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500173 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500174 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500175 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500176 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400177 "0440 Adapter failed to init, READ_REV has "
178 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500179 mempool_free(pmb, phba->mbox_mem_pool);
180 return -ERESTART;
dea31012005-04-17 16:05:31 -0500181 }
182
James Smart495a7142008-06-14 22:52:59 -0400183 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
184 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500185 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400186 }
James Smarted957682007-06-17 19:56:37 -0500187
dea31012005-04-17 16:05:31 -0500188 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500189 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500190 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500191 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
192 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
193 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
194 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500195 vp->rev.biuRev = mb->un.varRdRev.biuRev;
196 vp->rev.smRev = mb->un.varRdRev.smRev;
197 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
198 vp->rev.endecRev = mb->un.varRdRev.endecRev;
199 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
200 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
201 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
202 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
203 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
204 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
205
James Smart92d7f7b2007-06-17 19:56:38 -0500206 /* If the sli feature level is less then 9, we must
207 * tear down all RPIs and VPIs on link down if NPIV
208 * is enabled.
209 */
210 if (vp->rev.feaLevelHigh < 9)
211 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
212
dea31012005-04-17 16:05:31 -0500213 if (lpfc_is_LC_HBA(phba->pcidev->device))
214 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
215 sizeof (phba->RandomData));
216
dea31012005-04-17 16:05:31 -0500217 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500218 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
219 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400220 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500221 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400222 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500223 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
224
225 if (rc != MBX_SUCCESS) {
226 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400227 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500228 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500229 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500230 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500231 }
James Smart04c68492009-05-22 14:52:52 -0400232 /* dump mem may return a zero when finished or we got a
233 * mailbox error, either way we are done.
234 */
235 if (mb->un.varDmp.word_cnt == 0)
236 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500237 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
238 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400239 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
240 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500241 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500242 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500243 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
244 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500245
246 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500247out_free_mbox:
248 mempool_free(pmb, phba->mbox_mem_pool);
249 return 0;
250}
251
James Smarte59058c2008-08-24 21:49:00 -0400252/**
James Smart3621a712009-04-06 18:47:14 -0400253 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400254 * @phba: pointer to lpfc hba data structure.
255 * @pmboxq: pointer to the driver internal queue element for mailbox command.
256 *
257 * This is the completion handler for driver's configuring asynchronous event
258 * mailbox command to the device. If the mailbox command returns successfully,
259 * it will set internal async event support flag to 1; otherwise, it will
260 * set internal async event support flag to 0.
261 **/
James Smart57127f12007-10-27 13:37:05 -0400262static void
263lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
264{
James Smart04c68492009-05-22 14:52:52 -0400265 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400266 phba->temp_sensor_support = 1;
267 else
268 phba->temp_sensor_support = 0;
269 mempool_free(pmboxq, phba->mbox_mem_pool);
270 return;
271}
272
James Smarte59058c2008-08-24 21:49:00 -0400273/**
James Smart3621a712009-04-06 18:47:14 -0400274 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500275 * @phba: pointer to lpfc hba data structure.
276 * @pmboxq: pointer to the driver internal queue element for mailbox command.
277 *
278 * This is the completion handler for dump mailbox command for getting
279 * wake up parameters. When this command complete, the response contain
280 * Option rom version of the HBA. This function translate the version number
281 * into a human readable string and store it in OptionROMVersion.
282 **/
283static void
284lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
285{
286 struct prog_id *prg;
287 uint32_t prog_id_word;
288 char dist = ' ';
289 /* character array used for decoding dist type. */
290 char dist_char[] = "nabx";
291
James Smart04c68492009-05-22 14:52:52 -0400292 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500293 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500294 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500295 }
James Smart97207482008-12-04 22:39:19 -0500296
297 prg = (struct prog_id *) &prog_id_word;
298
299 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400300 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500301
302 /* Decode the Option rom version word to a readable string */
303 if (prg->dist < 4)
304 dist = dist_char[prg->dist];
305
306 if ((prg->dist == 3) && (prg->num == 0))
307 sprintf(phba->OptionROMVersion, "%d.%d%d",
308 prg->ver, prg->rev, prg->lev);
309 else
310 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
311 prg->ver, prg->rev, prg->lev,
312 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500313 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500314 return;
315}
316
317/**
James Smart05580562011-05-24 11:40:48 -0400318 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
319 * cfg_soft_wwnn, cfg_soft_wwpn
320 * @vport: pointer to lpfc vport data structure.
321 *
322 *
323 * Return codes
324 * None.
325 **/
326void
327lpfc_update_vport_wwn(struct lpfc_vport *vport)
328{
329 /* If the soft name exists then update it using the service params */
330 if (vport->phba->cfg_soft_wwnn)
331 u64_to_wwn(vport->phba->cfg_soft_wwnn,
332 vport->fc_sparam.nodeName.u.wwn);
333 if (vport->phba->cfg_soft_wwpn)
334 u64_to_wwn(vport->phba->cfg_soft_wwpn,
335 vport->fc_sparam.portName.u.wwn);
336
337 /*
338 * If the name is empty or there exists a soft name
339 * then copy the service params name, otherwise use the fc name
340 */
341 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
342 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
343 sizeof(struct lpfc_name));
344 else
345 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
346 sizeof(struct lpfc_name));
347
348 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
349 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
350 sizeof(struct lpfc_name));
351 else
352 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
353 sizeof(struct lpfc_name));
354}
355
356/**
James Smart3621a712009-04-06 18:47:14 -0400357 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400358 * @phba: pointer to lpfc hba data structure.
359 *
360 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
361 * command call. It performs all internal resource and state setups on the
362 * port: post IOCB buffers, enable appropriate host interrupt attentions,
363 * ELS ring timers, etc.
364 *
365 * Return codes
366 * 0 - success.
367 * Any other value - error.
368 **/
dea31012005-04-17 16:05:31 -0500369int
James Smart2e0fef82007-06-17 19:56:36 -0500370lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500371{
James Smart2e0fef82007-06-17 19:56:36 -0500372 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400373 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500374 LPFC_MBOXQ_t *pmb;
375 MAILBOX_t *mb;
376 struct lpfc_dmabuf *mp;
377 struct lpfc_sli *psli = &phba->sli;
378 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500379 int i, j;
380 int rc;
dea31012005-04-17 16:05:31 -0500381
James Smart7af67052007-10-27 13:38:11 -0400382 spin_lock_irq(&phba->hbalock);
383 /*
384 * If the Config port completed correctly the HBA is not
385 * over heated any more.
386 */
387 if (phba->over_temp_state == HBA_OVER_TEMP)
388 phba->over_temp_state = HBA_NORMAL_TEMP;
389 spin_unlock_irq(&phba->hbalock);
390
dea31012005-04-17 16:05:31 -0500391 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
392 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500393 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500394 return -ENOMEM;
395 }
James Smart04c68492009-05-22 14:52:52 -0400396 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500397
dea31012005-04-17 16:05:31 -0500398 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500399 rc = lpfc_read_sparam(phba, pmb, 0);
400 if (rc) {
401 mempool_free(pmb, phba->mbox_mem_pool);
402 return -ENOMEM;
403 }
404
James Smarted957682007-06-17 19:56:37 -0500405 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500406 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400408 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500409 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500410 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500411 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500412 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500413 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500414 lpfc_mbuf_free(phba, mp->virt, mp->phys);
415 kfree(mp);
416 return -EIO;
417 }
418
419 mp = (struct lpfc_dmabuf *) pmb->context1;
420
James Smart2e0fef82007-06-17 19:56:36 -0500421 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500422 lpfc_mbuf_free(phba, mp->virt, mp->phys);
423 kfree(mp);
424 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400425 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400426
427 /* Update the fc_host data structures with new wwn. */
428 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
429 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400430 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400431
dea31012005-04-17 16:05:31 -0500432 /* If no serial number in VPD data, use low 6 bytes of WWNN */
433 /* This should be consolidated into parse_vpd ? - mr */
434 if (phba->SerialNumber[0] == 0) {
435 uint8_t *outptr;
436
James Smart2e0fef82007-06-17 19:56:36 -0500437 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500438 for (i = 0; i < 12; i++) {
439 status = *outptr++;
440 j = ((status & 0xf0) >> 4);
441 if (j <= 9)
442 phba->SerialNumber[i] =
443 (char)((uint8_t) 0x30 + (uint8_t) j);
444 else
445 phba->SerialNumber[i] =
446 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
447 i++;
448 j = (status & 0xf);
449 if (j <= 9)
450 phba->SerialNumber[i] =
451 (char)((uint8_t) 0x30 + (uint8_t) j);
452 else
453 phba->SerialNumber[i] =
454 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
455 }
456 }
457
dea31012005-04-17 16:05:31 -0500458 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500459 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500460 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400462 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500463 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500464 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500465 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500466 mempool_free( pmb, phba->mbox_mem_pool);
467 return -EIO;
468 }
469
James Smarta0c87cb2009-07-19 10:01:10 -0400470 /* Check if the port is disabled */
471 lpfc_sli_read_link_ste(phba);
472
dea31012005-04-17 16:05:31 -0500473 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
474 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
475 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400476 (mb->un.varRdConfig.max_xri + 1) -
477 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500478
479 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500480
481 /* Get the default values for Model Name and Description */
482 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
483
James Smart2e0fef82007-06-17 19:56:36 -0500484 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500485
James Smart0b727fe2007-10-27 13:37:25 -0400486 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart7e56aa22012-08-03 12:35:34 -0400487 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
James Smarta4bc3372006-12-02 13:34:16 -0500488 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400489 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500490 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400491 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500492 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
493
494 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500495 if (phba->sli_rev != 3)
496 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500497
James Smart93996272008-08-24 21:50:30 -0400498 /*
499 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
500 */
501 if (phba->intr_type == MSIX) {
502 rc = lpfc_config_msi(phba, pmb);
503 if (rc) {
504 mempool_free(pmb, phba->mbox_mem_pool);
505 return -EIO;
506 }
507 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
508 if (rc != MBX_SUCCESS) {
509 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
510 "0352 Config MSI mailbox command "
511 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400512 pmb->u.mb.mbxCommand,
513 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400514 mempool_free(pmb, phba->mbox_mem_pool);
515 return -EIO;
516 }
517 }
518
James Smart04c68492009-05-22 14:52:52 -0400519 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400520 /* Initialize ERATT handling flag */
521 phba->hba_flag &= ~HBA_ERATT_HANDLED;
522
dea31012005-04-17 16:05:31 -0500523 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500524 if (lpfc_readl(phba->HCregaddr, &status)) {
525 spin_unlock_irq(&phba->hbalock);
526 return -EIO;
527 }
dea31012005-04-17 16:05:31 -0500528 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
529 if (psli->num_rings > 0)
530 status |= HC_R0INT_ENA;
531 if (psli->num_rings > 1)
532 status |= HC_R1INT_ENA;
533 if (psli->num_rings > 2)
534 status |= HC_R2INT_ENA;
535 if (psli->num_rings > 3)
536 status |= HC_R3INT_ENA;
537
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500538 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
539 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400540 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500541
dea31012005-04-17 16:05:31 -0500542 writel(status, phba->HCregaddr);
543 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500544 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500545
James Smart93996272008-08-24 21:50:30 -0400546 /* Set up ring-0 (ELS) timer */
547 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400548 mod_timer(&vport->els_tmofunc,
549 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400550 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400551 mod_timer(&phba->hb_tmofunc,
552 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500553 phba->hb_outstanding = 0;
554 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400555 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400556 mod_timer(&phba->eratt_poll,
557 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
dea31012005-04-17 16:05:31 -0500558
James Smarta0c87cb2009-07-19 10:01:10 -0400559 if (phba->hba_flag & LINK_DISABLED) {
560 lpfc_printf_log(phba,
561 KERN_ERR, LOG_INIT,
562 "2598 Adapter Link is disabled.\n");
563 lpfc_down_link(phba, pmb);
564 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
565 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
566 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
567 lpfc_printf_log(phba,
568 KERN_ERR, LOG_INIT,
569 "2599 Adapter failed to issue DOWN_LINK"
570 " mbox command rc 0x%x\n", rc);
571
572 mempool_free(pmb, phba->mbox_mem_pool);
573 return -EIO;
574 }
James Smarte40a02c2010-02-26 14:13:54 -0500575 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500576 mempool_free(pmb, phba->mbox_mem_pool);
577 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
578 if (rc)
579 return rc;
dea31012005-04-17 16:05:31 -0500580 }
581 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400582 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500583 if (!pmb) {
584 phba->link_state = LPFC_HBA_ERROR;
585 return -ENOMEM;
586 }
587
James Smart57127f12007-10-27 13:37:05 -0400588 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
589 pmb->mbox_cmpl = lpfc_config_async_cmpl;
590 pmb->vport = phba->pport;
591 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500592
James Smart57127f12007-10-27 13:37:05 -0400593 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
594 lpfc_printf_log(phba,
595 KERN_ERR,
596 LOG_INIT,
597 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400598 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400599 rc);
600 mempool_free(pmb, phba->mbox_mem_pool);
601 }
James Smart97207482008-12-04 22:39:19 -0500602
603 /* Get Option rom version */
604 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500605 if (!pmb) {
606 phba->link_state = LPFC_HBA_ERROR;
607 return -ENOMEM;
608 }
609
James Smart97207482008-12-04 22:39:19 -0500610 lpfc_dump_wakeup_param(phba, pmb);
611 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
612 pmb->vport = phba->pport;
613 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
614
615 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400617 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500618 mempool_free(pmb, phba->mbox_mem_pool);
619 }
620
James Smartd7c255b2008-08-24 21:50:00 -0400621 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400622}
623
James Smarte59058c2008-08-24 21:49:00 -0400624/**
James Smart84d1b002010-02-12 14:42:33 -0500625 * lpfc_hba_init_link - Initialize the FC link
626 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400627 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500628 *
629 * This routine will issue the INIT_LINK mailbox command call.
630 * It is available to other drivers through the lpfc_hba data
631 * structure for use as a delayed link up mechanism with the
632 * module parameter lpfc_suppress_link_up.
633 *
634 * Return code
635 * 0 - success
636 * Any other value - error
637 **/
638int
James Smart6e7288d2010-06-07 15:23:35 -0400639lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500640{
James Smart1b511972011-12-13 13:23:09 -0500641 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
642}
643
644/**
645 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
646 * @phba: pointer to lpfc hba data structure.
647 * @fc_topology: desired fc topology.
648 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
649 *
650 * This routine will issue the INIT_LINK mailbox command call.
651 * It is available to other drivers through the lpfc_hba data
652 * structure for use as a delayed link up mechanism with the
653 * module parameter lpfc_suppress_link_up.
654 *
655 * Return code
656 * 0 - success
657 * Any other value - error
658 **/
659int
660lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
661 uint32_t flag)
662{
James Smart84d1b002010-02-12 14:42:33 -0500663 struct lpfc_vport *vport = phba->pport;
664 LPFC_MBOXQ_t *pmb;
665 MAILBOX_t *mb;
666 int rc;
667
668 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
669 if (!pmb) {
670 phba->link_state = LPFC_HBA_ERROR;
671 return -ENOMEM;
672 }
673 mb = &pmb->u.mb;
674 pmb->vport = vport;
675
James Smart026abb82011-12-13 13:20:45 -0500676 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
677 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
678 !(phba->lmt & LMT_1Gb)) ||
679 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
680 !(phba->lmt & LMT_2Gb)) ||
681 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
682 !(phba->lmt & LMT_4Gb)) ||
683 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
684 !(phba->lmt & LMT_8Gb)) ||
685 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
686 !(phba->lmt & LMT_10Gb)) ||
687 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
688 !(phba->lmt & LMT_16Gb))) {
689 /* Reset link speed to auto */
690 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
691 "1302 Invalid speed for this board:%d "
692 "Reset link speed to auto.\n",
693 phba->cfg_link_speed);
694 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
695 }
James Smart1b511972011-12-13 13:23:09 -0500696 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500697 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500698 if (phba->sli_rev < LPFC_SLI_REV4)
699 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400700 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500701 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
703 "0498 Adapter failed to init, mbxCmd x%x "
704 "INIT_LINK, mbxStatus x%x\n",
705 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500706 if (phba->sli_rev <= LPFC_SLI_REV3) {
707 /* Clear all interrupt enable conditions */
708 writel(0, phba->HCregaddr);
709 readl(phba->HCregaddr); /* flush */
710 /* Clear all pending interrupts */
711 writel(0xffffffff, phba->HAregaddr);
712 readl(phba->HAregaddr); /* flush */
713 }
James Smart84d1b002010-02-12 14:42:33 -0500714 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400715 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500716 mempool_free(pmb, phba->mbox_mem_pool);
717 return -EIO;
718 }
James Smarte40a02c2010-02-26 14:13:54 -0500719 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400720 if (flag == MBX_POLL)
721 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500722
723 return 0;
724}
725
726/**
727 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400728 * @phba: pointer to lpfc hba data structure.
729 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500730 *
731 * This routine will issue the DOWN_LINK mailbox command call.
732 * It is available to other drivers through the lpfc_hba data
733 * structure for use to stop the link.
734 *
735 * Return code
736 * 0 - success
737 * Any other value - error
738 **/
739int
James Smart6e7288d2010-06-07 15:23:35 -0400740lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500741{
742 LPFC_MBOXQ_t *pmb;
743 int rc;
744
745 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
746 if (!pmb) {
747 phba->link_state = LPFC_HBA_ERROR;
748 return -ENOMEM;
749 }
750
751 lpfc_printf_log(phba,
752 KERN_ERR, LOG_INIT,
753 "0491 Adapter Link is disabled.\n");
754 lpfc_down_link(phba, pmb);
755 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400756 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500757 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
758 lpfc_printf_log(phba,
759 KERN_ERR, LOG_INIT,
760 "2522 Adapter failed to issue DOWN_LINK"
761 " mbox command rc 0x%x\n", rc);
762
763 mempool_free(pmb, phba->mbox_mem_pool);
764 return -EIO;
765 }
James Smart6e7288d2010-06-07 15:23:35 -0400766 if (flag == MBX_POLL)
767 mempool_free(pmb, phba->mbox_mem_pool);
768
James Smart84d1b002010-02-12 14:42:33 -0500769 return 0;
770}
771
772/**
James Smart3621a712009-04-06 18:47:14 -0400773 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400774 * @phba: pointer to lpfc HBA data structure.
775 *
776 * This routine will do LPFC uninitialization before the HBA is reset when
777 * bringing down the SLI Layer.
778 *
779 * Return codes
780 * 0 - success.
781 * Any other value - error.
782 **/
dea31012005-04-17 16:05:31 -0500783int
James Smart2e0fef82007-06-17 19:56:36 -0500784lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500785{
James Smart1b32f6a2008-02-08 18:49:39 -0500786 struct lpfc_vport **vports;
787 int i;
James Smart3772a992009-05-22 14:50:54 -0400788
789 if (phba->sli_rev <= LPFC_SLI_REV3) {
790 /* Disable interrupts */
791 writel(0, phba->HCregaddr);
792 readl(phba->HCregaddr); /* flush */
793 }
dea31012005-04-17 16:05:31 -0500794
James Smart1b32f6a2008-02-08 18:49:39 -0500795 if (phba->pport->load_flag & FC_UNLOADING)
796 lpfc_cleanup_discovery_resources(phba->pport);
797 else {
798 vports = lpfc_create_vport_work_array(phba);
799 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400800 for (i = 0; i <= phba->max_vports &&
801 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500802 lpfc_cleanup_discovery_resources(vports[i]);
803 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500804 }
805 return 0;
dea31012005-04-17 16:05:31 -0500806}
807
James Smarte59058c2008-08-24 21:49:00 -0400808/**
James Smart3772a992009-05-22 14:50:54 -0400809 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400810 * @phba: pointer to lpfc HBA data structure.
811 *
812 * This routine will do uninitialization after the HBA is reset when bring
813 * down the SLI Layer.
814 *
815 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200816 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400817 * Any other value - error.
818 **/
James Smart3772a992009-05-22 14:50:54 -0400819static int
820lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500821{
822 struct lpfc_sli *psli = &phba->sli;
823 struct lpfc_sli_ring *pring;
824 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500825 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500826 int i;
827
James Smart92d7f7b2007-06-17 19:56:38 -0500828 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
829 lpfc_sli_hbqbuf_free_all(phba);
830 else {
831 /* Cleanup preposted buffers on the ELS ring */
832 pring = &psli->ring[LPFC_ELS_RING];
833 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
834 list_del(&mp->list);
835 pring->postbufq_cnt--;
836 lpfc_mbuf_free(phba, mp->virt, mp->phys);
837 kfree(mp);
838 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500839 }
840
James Smart09372822008-01-11 01:52:54 -0500841 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500842 for (i = 0; i < psli->num_rings; i++) {
843 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500844
845 /* At this point in time the HBA is either reset or DOA. Either
846 * way, nothing should be on txcmplq as it will NEVER complete.
847 */
848 list_splice_init(&pring->txcmplq, &completions);
James Smart09372822008-01-11 01:52:54 -0500849 spin_unlock_irq(&phba->hbalock);
850
James Smarta257bf92009-04-06 18:48:10 -0400851 /* Cancel all the IOCBs from the completions list */
852 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
853 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500854
Jamie Wellnitz41415862006-02-28 19:25:27 -0500855 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500856 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500857 }
James Smart09372822008-01-11 01:52:54 -0500858 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500859
860 return 0;
861}
James Smart5af5eee2010-10-22 11:06:38 -0400862
James Smartda0436e2009-05-22 14:51:39 -0400863/**
864 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
865 * @phba: pointer to lpfc HBA data structure.
866 *
867 * This routine will do uninitialization after the HBA is reset when bring
868 * down the SLI Layer.
869 *
870 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200871 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400872 * Any other value - error.
873 **/
874static int
875lpfc_hba_down_post_s4(struct lpfc_hba *phba)
876{
877 struct lpfc_scsi_buf *psb, *psb_next;
878 LIST_HEAD(aborts);
879 int ret;
880 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500881 struct lpfc_sglq *sglq_entry = NULL;
882
James Smartda0436e2009-05-22 14:51:39 -0400883 ret = lpfc_hba_down_post_s3(phba);
884 if (ret)
885 return ret;
886 /* At this point in time the HBA is either reset or DOA. Either
887 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
888 * on the lpfc_sgl_list so that it can either be freed if the
889 * driver is unloading or reposted if the driver is restarting
890 * the port.
891 */
892 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
893 /* scsl_buf_list */
894 /* abts_sgl_list_lock required because worker thread uses this
895 * list.
896 */
897 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500898 list_for_each_entry(sglq_entry,
899 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
900 sglq_entry->state = SGL_FREED;
901
James Smartda0436e2009-05-22 14:51:39 -0400902 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
903 &phba->sli4_hba.lpfc_sgl_list);
904 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
905 /* abts_scsi_buf_list_lock required because worker thread uses this
906 * list.
907 */
908 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
909 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
910 &aborts);
911 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
912 spin_unlock_irq(&phba->hbalock);
913
914 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
915 psb->pCmd = NULL;
916 psb->status = IOSTAT_SUCCESS;
917 }
James Smarta40fc5f2013-04-17 20:17:40 -0400918 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
919 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
920 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400921 return 0;
922}
923
924/**
925 * lpfc_hba_down_post - Wrapper func for hba down post routine
926 * @phba: pointer to lpfc HBA data structure.
927 *
928 * This routine wraps the actual SLI3 or SLI4 routine for performing
929 * uninitialization after the HBA is reset when bring down the SLI Layer.
930 *
931 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200932 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400933 * Any other value - error.
934 **/
935int
936lpfc_hba_down_post(struct lpfc_hba *phba)
937{
938 return (*phba->lpfc_hba_down_post)(phba);
939}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500940
James Smarte59058c2008-08-24 21:49:00 -0400941/**
James Smart3621a712009-04-06 18:47:14 -0400942 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400943 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
944 *
945 * This is the HBA-timer timeout handler registered to the lpfc driver. When
946 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
947 * work-port-events bitmap and the worker thread is notified. This timeout
948 * event will be used by the worker thread to invoke the actual timeout
949 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
950 * be performed in the timeout handler and the HBA timeout event bit shall
951 * be cleared by the worker thread after it has taken the event bitmap out.
952 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100953static void
James Smart858c9f62007-06-17 19:56:39 -0500954lpfc_hb_timeout(unsigned long ptr)
955{
956 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400957 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500958 unsigned long iflag;
959
960 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400961
962 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500963 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400964 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
965 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500966 phba->pport->work_port_events |= WORKER_HB_TMO;
967 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
968
James Smart93996272008-08-24 21:50:30 -0400969 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400970 if (!tmo_posted)
971 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500972 return;
973}
974
James Smarte59058c2008-08-24 21:49:00 -0400975/**
James Smart19ca7602010-11-20 23:11:55 -0500976 * lpfc_rrq_timeout - The RRQ-timer timeout handler
977 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
978 *
979 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
980 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
981 * work-port-events bitmap and the worker thread is notified. This timeout
982 * event will be used by the worker thread to invoke the actual timeout
983 * handler routine, lpfc_rrq_handler. Any periodical operations will
984 * be performed in the timeout handler and the RRQ timeout event bit shall
985 * be cleared by the worker thread after it has taken the event bitmap out.
986 **/
987static void
988lpfc_rrq_timeout(unsigned long ptr)
989{
990 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -0500991 unsigned long iflag;
992
993 phba = (struct lpfc_hba *)ptr;
994 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500995 phba->hba_flag |= HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -0500996 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500997 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -0500998}
999
1000/**
James Smart3621a712009-04-06 18:47:14 -04001001 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001002 * @phba: pointer to lpfc hba data structure.
1003 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1004 *
1005 * This is the callback function to the lpfc heart-beat mailbox command.
1006 * If configured, the lpfc driver issues the heart-beat mailbox command to
1007 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1008 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1009 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1010 * heart-beat outstanding state. Once the mailbox command comes back and
1011 * no error conditions detected, the heart-beat mailbox command timer is
1012 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1013 * state is cleared for the next heart-beat. If the timer expired with the
1014 * heart-beat outstanding state set, the driver will put the HBA offline.
1015 **/
James Smart858c9f62007-06-17 19:56:39 -05001016static void
1017lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1018{
1019 unsigned long drvr_flag;
1020
1021 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1022 phba->hb_outstanding = 0;
1023 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1024
James Smart93996272008-08-24 21:50:30 -04001025 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001026 mempool_free(pmboxq, phba->mbox_mem_pool);
1027 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1028 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001029 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001030 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001031 jiffies +
1032 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001033 return;
1034}
1035
James Smarte59058c2008-08-24 21:49:00 -04001036/**
James Smart3621a712009-04-06 18:47:14 -04001037 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001038 * @phba: pointer to lpfc hba data structure.
1039 *
1040 * This is the actual HBA-timer timeout handler to be invoked by the worker
1041 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1042 * handler performs any periodic operations needed for the device. If such
1043 * periodic event has already been attended to either in the interrupt handler
1044 * or by processing slow-ring or fast-ring events within the HBA-timer
1045 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1046 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1047 * is configured and there is no heart-beat mailbox command outstanding, a
1048 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1049 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1050 * to offline.
1051 **/
James Smart858c9f62007-06-17 19:56:39 -05001052void
1053lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1054{
James Smart45ed1192009-10-02 15:17:02 -04001055 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001056 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001057 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001058 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001059 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001060 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001061
James Smart45ed1192009-10-02 15:17:02 -04001062 vports = lpfc_create_vport_work_array(phba);
1063 if (vports != NULL)
1064 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1065 lpfc_rcv_seq_check_edtov(vports[i]);
1066 lpfc_destroy_vport_work_array(phba, vports);
1067
James Smart858c9f62007-06-17 19:56:39 -05001068 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001069 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001070 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1071 return;
1072
1073 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001074
James Smart256ec0d2013-04-17 20:14:58 -04001075 if (time_after(phba->last_completion_time +
1076 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1077 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001078 spin_unlock_irq(&phba->pport->work_port_lock);
1079 if (!phba->hb_outstanding)
1080 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001081 jiffies +
1082 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001083 else
1084 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001085 jiffies +
1086 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001087 return;
1088 }
1089 spin_unlock_irq(&phba->pport->work_port_lock);
1090
James Smart0ff10d42008-01-11 01:52:36 -05001091 if (phba->elsbuf_cnt &&
1092 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1093 spin_lock_irq(&phba->hbalock);
1094 list_splice_init(&phba->elsbuf, &completions);
1095 phba->elsbuf_cnt = 0;
1096 phba->elsbuf_prev_cnt = 0;
1097 spin_unlock_irq(&phba->hbalock);
1098
1099 while (!list_empty(&completions)) {
1100 list_remove_head(&completions, buf_ptr,
1101 struct lpfc_dmabuf, list);
1102 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1103 kfree(buf_ptr);
1104 }
1105 }
1106 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1107
James Smart858c9f62007-06-17 19:56:39 -05001108 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001109 if (phba->cfg_enable_hba_heartbeat) {
1110 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001111 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1112 (list_empty(&psli->mboxq))) {
1113 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1114 GFP_KERNEL);
1115 if (!pmboxq) {
1116 mod_timer(&phba->hb_tmofunc,
1117 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001118 msecs_to_jiffies(1000 *
1119 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001120 return;
1121 }
James Smart13815c82008-01-11 01:52:48 -05001122
James Smartbc739052010-08-04 16:11:18 -04001123 lpfc_heart_beat(phba, pmboxq);
1124 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1125 pmboxq->vport = phba->pport;
1126 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1127 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001128
James Smartbc739052010-08-04 16:11:18 -04001129 if (retval != MBX_BUSY &&
1130 retval != MBX_SUCCESS) {
1131 mempool_free(pmboxq,
1132 phba->mbox_mem_pool);
1133 mod_timer(&phba->hb_tmofunc,
1134 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001135 msecs_to_jiffies(1000 *
1136 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001137 return;
1138 }
1139 phba->skipped_hb = 0;
1140 phba->hb_outstanding = 1;
1141 } else if (time_before_eq(phba->last_completion_time,
1142 phba->skipped_hb)) {
1143 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1144 "2857 Last completion time not "
1145 " updated in %d ms\n",
1146 jiffies_to_msecs(jiffies
1147 - phba->last_completion_time));
1148 } else
1149 phba->skipped_hb = jiffies;
1150
James Smart858c9f62007-06-17 19:56:39 -05001151 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001152 jiffies +
1153 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001154 return;
James Smart13815c82008-01-11 01:52:48 -05001155 } else {
1156 /*
1157 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001158 * we need to give the hb mailbox cmd a chance to
1159 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001160 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001161 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1162 "0459 Adapter heartbeat still out"
1163 "standing:last compl time was %d ms.\n",
1164 jiffies_to_msecs(jiffies
1165 - phba->last_completion_time));
1166 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001167 jiffies +
1168 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001169 }
James Smart858c9f62007-06-17 19:56:39 -05001170 }
1171}
1172
James Smarte59058c2008-08-24 21:49:00 -04001173/**
James Smart3621a712009-04-06 18:47:14 -04001174 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001175 * @phba: pointer to lpfc hba data structure.
1176 *
1177 * This routine is called to bring the HBA offline when HBA hardware error
1178 * other than Port Error 6 has been detected.
1179 **/
James Smart09372822008-01-11 01:52:54 -05001180static void
1181lpfc_offline_eratt(struct lpfc_hba *phba)
1182{
1183 struct lpfc_sli *psli = &phba->sli;
1184
1185 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001186 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001187 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001188 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001189
1190 lpfc_offline(phba);
1191 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001192 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001193 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001194 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001195 lpfc_hba_down_post(phba);
1196 lpfc_sli_brdready(phba, HS_MBRDY);
1197 lpfc_unblock_mgmt_io(phba);
1198 phba->link_state = LPFC_HBA_ERROR;
1199 return;
1200}
1201
James Smarte59058c2008-08-24 21:49:00 -04001202/**
James Smartda0436e2009-05-22 14:51:39 -04001203 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1204 * @phba: pointer to lpfc hba data structure.
1205 *
1206 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1207 * other than Port Error 6 has been detected.
1208 **/
James Smarta88dbb62013-04-17 20:18:39 -04001209void
James Smartda0436e2009-05-22 14:51:39 -04001210lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1211{
James Smart618a5232012-06-12 13:54:36 -04001212 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001213 lpfc_offline(phba);
1214 lpfc_sli4_brdreset(phba);
1215 lpfc_hba_down_post(phba);
1216 lpfc_sli4_post_status_check(phba);
1217 lpfc_unblock_mgmt_io(phba);
1218 phba->link_state = LPFC_HBA_ERROR;
1219}
1220
1221/**
James Smarta257bf92009-04-06 18:48:10 -04001222 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1223 * @phba: pointer to lpfc hba data structure.
1224 *
1225 * This routine is invoked to handle the deferred HBA hardware error
1226 * conditions. This type of error is indicated by HBA by setting ER1
1227 * and another ER bit in the host status register. The driver will
1228 * wait until the ER1 bit clears before handling the error condition.
1229 **/
1230static void
1231lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1232{
1233 uint32_t old_host_status = phba->work_hs;
1234 struct lpfc_sli_ring *pring;
1235 struct lpfc_sli *psli = &phba->sli;
1236
James Smartf4b4c682009-05-22 14:53:12 -04001237 /* If the pci channel is offline, ignore possible errors,
1238 * since we cannot communicate with the pci card anyway.
1239 */
1240 if (pci_channel_offline(phba->pcidev)) {
1241 spin_lock_irq(&phba->hbalock);
1242 phba->hba_flag &= ~DEFER_ERATT;
1243 spin_unlock_irq(&phba->hbalock);
1244 return;
1245 }
1246
James Smarta257bf92009-04-06 18:48:10 -04001247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1248 "0479 Deferred Adapter Hardware Error "
1249 "Data: x%x x%x x%x\n",
1250 phba->work_hs,
1251 phba->work_status[0], phba->work_status[1]);
1252
1253 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001254 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001255 spin_unlock_irq(&phba->hbalock);
1256
1257
1258 /*
1259 * Firmware stops when it triggred erratt. That could cause the I/Os
1260 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1261 * SCSI layer retry it after re-establishing link.
1262 */
1263 pring = &psli->ring[psli->fcp_ring];
1264 lpfc_sli_abort_iocb_ring(phba, pring);
1265
1266 /*
1267 * There was a firmware error. Take the hba offline and then
1268 * attempt to restart it.
1269 */
James Smart618a5232012-06-12 13:54:36 -04001270 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001271 lpfc_offline(phba);
1272
1273 /* Wait for the ER1 bit to clear.*/
1274 while (phba->work_hs & HS_FFER1) {
1275 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001276 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1277 phba->work_hs = UNPLUG_ERR ;
1278 break;
1279 }
James Smarta257bf92009-04-06 18:48:10 -04001280 /* If driver is unloading let the worker thread continue */
1281 if (phba->pport->load_flag & FC_UNLOADING) {
1282 phba->work_hs = 0;
1283 break;
1284 }
1285 }
1286
1287 /*
1288 * This is to ptrotect against a race condition in which
1289 * first write to the host attention register clear the
1290 * host status register.
1291 */
1292 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1293 phba->work_hs = old_host_status & ~HS_FFER1;
1294
James Smart3772a992009-05-22 14:50:54 -04001295 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001296 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001297 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001298 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1299 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1300}
1301
James Smart3772a992009-05-22 14:50:54 -04001302static void
1303lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1304{
1305 struct lpfc_board_event_header board_event;
1306 struct Scsi_Host *shost;
1307
1308 board_event.event_type = FC_REG_BOARD_EVENT;
1309 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1310 shost = lpfc_shost_from_vport(phba->pport);
1311 fc_host_post_vendor_event(shost, fc_get_event_number(),
1312 sizeof(board_event),
1313 (char *) &board_event,
1314 LPFC_NL_VENDOR_ID);
1315}
1316
James Smarta257bf92009-04-06 18:48:10 -04001317/**
James Smart3772a992009-05-22 14:50:54 -04001318 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001319 * @phba: pointer to lpfc hba data structure.
1320 *
1321 * This routine is invoked to handle the following HBA hardware error
1322 * conditions:
1323 * 1 - HBA error attention interrupt
1324 * 2 - DMA ring index out of range
1325 * 3 - Mailbox command came back as unknown
1326 **/
James Smart3772a992009-05-22 14:50:54 -04001327static void
1328lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001329{
James Smart2e0fef82007-06-17 19:56:36 -05001330 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001331 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001332 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001333 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001334 unsigned long temperature;
1335 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001336 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001337
Linas Vepstas8d63f372007-02-14 14:28:36 -06001338 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001339 * since we cannot communicate with the pci card anyway.
1340 */
1341 if (pci_channel_offline(phba->pcidev)) {
1342 spin_lock_irq(&phba->hbalock);
1343 phba->hba_flag &= ~DEFER_ERATT;
1344 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001345 return;
James Smart3772a992009-05-22 14:50:54 -04001346 }
1347
James Smart13815c82008-01-11 01:52:48 -05001348 /* If resets are disabled then leave the HBA alone and return */
1349 if (!phba->cfg_enable_hba_reset)
1350 return;
dea31012005-04-17 16:05:31 -05001351
James Smartea2151b2008-09-07 11:52:10 -04001352 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001353 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001354
James Smarta257bf92009-04-06 18:48:10 -04001355 if (phba->hba_flag & DEFER_ERATT)
1356 lpfc_handle_deferred_eratt(phba);
1357
James Smartdcf2a4e2010-09-29 11:18:53 -04001358 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1359 if (phba->work_hs & HS_FFER6)
1360 /* Re-establishing Link */
1361 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1362 "1301 Re-establishing Link "
1363 "Data: x%x x%x x%x\n",
1364 phba->work_hs, phba->work_status[0],
1365 phba->work_status[1]);
1366 if (phba->work_hs & HS_FFER8)
1367 /* Device Zeroization */
1368 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1369 "2861 Host Authentication device "
1370 "zeroization Data:x%x x%x x%x\n",
1371 phba->work_hs, phba->work_status[0],
1372 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001373
James Smart92d7f7b2007-06-17 19:56:38 -05001374 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001375 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001376 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001377
1378 /*
1379 * Firmware stops when it triggled erratt with HS_FFER6.
1380 * That could cause the I/Os dropped by the firmware.
1381 * Error iocb (I/O) on txcmplq and let the SCSI layer
1382 * retry it after re-establishing link.
1383 */
1384 pring = &psli->ring[psli->fcp_ring];
1385 lpfc_sli_abort_iocb_ring(phba, pring);
1386
dea31012005-04-17 16:05:31 -05001387 /*
1388 * There was a firmware error. Take the hba offline and then
1389 * attempt to restart it.
1390 */
James Smart618a5232012-06-12 13:54:36 -04001391 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001392 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001393 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001394 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001395 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001396 return;
1397 }
James Smart46fa3112007-04-25 09:51:45 -04001398 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001399 } else if (phba->work_hs & HS_CRIT_TEMP) {
1400 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1401 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1402 temp_event_data.event_code = LPFC_CRIT_TEMP;
1403 temp_event_data.data = (uint32_t)temperature;
1404
1405 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001406 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001407 "(%ld), taking this port offline "
1408 "Data: x%x x%x x%x\n",
1409 temperature, phba->work_hs,
1410 phba->work_status[0], phba->work_status[1]);
1411
1412 shost = lpfc_shost_from_vport(phba->pport);
1413 fc_host_post_vendor_event(shost, fc_get_event_number(),
1414 sizeof(temp_event_data),
1415 (char *) &temp_event_data,
1416 SCSI_NL_VID_TYPE_PCI
1417 | PCI_VENDOR_ID_EMULEX);
1418
James Smart7af67052007-10-27 13:38:11 -04001419 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001420 phba->over_temp_state = HBA_OVER_TEMP;
1421 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001422 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001423
dea31012005-04-17 16:05:31 -05001424 } else {
1425 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001426 * failure is a value other than FFER6. Do not call the offline
1427 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001428 */
1429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001430 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001431 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001432 phba->work_hs,
dea31012005-04-17 16:05:31 -05001433 phba->work_status[0], phba->work_status[1]);
1434
James Smartd2873e42006-08-18 17:46:43 -04001435 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001436 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001437 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001438 sizeof(event_data), (char *) &event_data,
1439 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1440
James Smart09372822008-01-11 01:52:54 -05001441 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001442 }
James Smart93996272008-08-24 21:50:30 -04001443 return;
dea31012005-04-17 16:05:31 -05001444}
1445
James Smarte59058c2008-08-24 21:49:00 -04001446/**
James Smart618a5232012-06-12 13:54:36 -04001447 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1448 * @phba: pointer to lpfc hba data structure.
1449 * @mbx_action: flag for mailbox shutdown action.
1450 *
1451 * This routine is invoked to perform an SLI4 port PCI function reset in
1452 * response to port status register polling attention. It waits for port
1453 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1454 * During this process, interrupt vectors are freed and later requested
1455 * for handling possible port resource change.
1456 **/
1457static int
1458lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action)
1459{
1460 int rc;
1461 uint32_t intr_mode;
1462
1463 /*
1464 * On error status condition, driver need to wait for port
1465 * ready before performing reset.
1466 */
1467 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1468 if (!rc) {
1469 /* need reset: attempt for port recovery */
1470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1471 "2887 Reset Needed: Attempting Port "
1472 "Recovery...\n");
1473 lpfc_offline_prep(phba, mbx_action);
1474 lpfc_offline(phba);
1475 /* release interrupt for possible resource change */
1476 lpfc_sli4_disable_intr(phba);
1477 lpfc_sli_brdrestart(phba);
1478 /* request and enable interrupt */
1479 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1480 if (intr_mode == LPFC_INTR_ERROR) {
1481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1482 "3175 Failed to enable interrupt\n");
1483 return -EIO;
1484 } else {
1485 phba->intr_mode = intr_mode;
1486 }
1487 rc = lpfc_online(phba);
1488 if (rc == 0)
1489 lpfc_unblock_mgmt_io(phba);
1490 }
1491 return rc;
1492}
1493
1494/**
James Smartda0436e2009-05-22 14:51:39 -04001495 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1496 * @phba: pointer to lpfc hba data structure.
1497 *
1498 * This routine is invoked to handle the SLI4 HBA hardware error attention
1499 * conditions.
1500 **/
1501static void
1502lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1503{
1504 struct lpfc_vport *vport = phba->pport;
1505 uint32_t event_data;
1506 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001507 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001508 struct lpfc_register portstat_reg = {0};
1509 uint32_t reg_err1, reg_err2;
1510 uint32_t uerrlo_reg, uemasklo_reg;
1511 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smart73d91e52011-10-10 21:32:10 -04001512 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001513
1514 /* If the pci channel is offline, ignore possible errors, since
1515 * we cannot communicate with the pci card anyway.
1516 */
1517 if (pci_channel_offline(phba->pcidev))
1518 return;
1519 /* If resets are disabled then leave the HBA alone and return */
1520 if (!phba->cfg_enable_hba_reset)
1521 return;
1522
James Smart2fcee4b2010-12-15 17:57:46 -05001523 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1524 switch (if_type) {
1525 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001526 pci_rd_rc1 = lpfc_readl(
1527 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1528 &uerrlo_reg);
1529 pci_rd_rc2 = lpfc_readl(
1530 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1531 &uemasklo_reg);
1532 /* consider PCI bus read error as pci_channel_offline */
1533 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1534 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001535 lpfc_sli4_offline_eratt(phba);
1536 break;
1537 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001538 pci_rd_rc1 = lpfc_readl(
1539 phba->sli4_hba.u.if_type2.STATUSregaddr,
1540 &portstat_reg.word0);
1541 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001542 if (pci_rd_rc1 == -EIO) {
1543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1544 "3151 PCI bus read access failure: x%x\n",
1545 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001546 return;
James Smart6b5151f2012-01-18 16:24:06 -05001547 }
James Smart2e90f4b2011-12-13 13:22:37 -05001548 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1549 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001550 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1551 /* TODO: Register for Overtemp async events. */
1552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1553 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001554 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001555 spin_lock_irq(&phba->hbalock);
1556 phba->over_temp_state = HBA_OVER_TEMP;
1557 spin_unlock_irq(&phba->hbalock);
1558 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001559 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001560 }
James Smart2e90f4b2011-12-13 13:22:37 -05001561 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1562 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART)
1563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1564 "3143 Port Down: Firmware Restarted\n");
1565 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1566 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1568 "3144 Port Down: Debug Dump\n");
1569 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1570 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1572 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001573
1574 /* Check port status register for function reset */
1575 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT);
1576 if (rc == 0) {
1577 /* don't report event on forced debug dump */
1578 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1579 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1580 return;
1581 else
1582 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001583 }
James Smart618a5232012-06-12 13:54:36 -04001584 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001585 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1586 "3152 Unrecoverable error, bring the port "
1587 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001588 lpfc_sli4_offline_eratt(phba);
1589 break;
1590 case LPFC_SLI_INTF_IF_TYPE_1:
1591 default:
1592 break;
1593 }
James Smart2e90f4b2011-12-13 13:22:37 -05001594 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1595 "3123 Report dump event to upper layer\n");
1596 /* Send an internal error event to mgmt application */
1597 lpfc_board_errevt_to_mgmt(phba);
1598
1599 event_data = FC_REG_DUMP_EVENT;
1600 shost = lpfc_shost_from_vport(vport);
1601 fc_host_post_vendor_event(shost, fc_get_event_number(),
1602 sizeof(event_data), (char *) &event_data,
1603 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001604}
1605
1606/**
1607 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1608 * @phba: pointer to lpfc HBA data structure.
1609 *
1610 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1611 * routine from the API jump table function pointer from the lpfc_hba struct.
1612 *
1613 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001614 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001615 * Any other value - error.
1616 **/
1617void
1618lpfc_handle_eratt(struct lpfc_hba *phba)
1619{
1620 (*phba->lpfc_handle_eratt)(phba);
1621}
1622
1623/**
James Smart3621a712009-04-06 18:47:14 -04001624 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001625 * @phba: pointer to lpfc hba data structure.
1626 *
1627 * This routine is invoked from the worker thread to handle a HBA host
1628 * attention link event.
1629 **/
dea31012005-04-17 16:05:31 -05001630void
James Smart2e0fef82007-06-17 19:56:36 -05001631lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001632{
James Smart2e0fef82007-06-17 19:56:36 -05001633 struct lpfc_vport *vport = phba->pport;
1634 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001635 LPFC_MBOXQ_t *pmb;
1636 volatile uint32_t control;
1637 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001638 int rc = 0;
dea31012005-04-17 16:05:31 -05001639
1640 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001641 if (!pmb) {
1642 rc = 1;
dea31012005-04-17 16:05:31 -05001643 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001644 }
dea31012005-04-17 16:05:31 -05001645
1646 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001647 if (!mp) {
1648 rc = 2;
dea31012005-04-17 16:05:31 -05001649 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001650 }
dea31012005-04-17 16:05:31 -05001651
1652 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001653 if (!mp->virt) {
1654 rc = 3;
dea31012005-04-17 16:05:31 -05001655 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001656 }
dea31012005-04-17 16:05:31 -05001657
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001658 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001659 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001660
1661 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001662 lpfc_read_topology(phba, pmb, mp);
1663 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001664 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001665 /* Block ELS IOCBs until we have processed this mbox command */
1666 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001667 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001668 if (rc == MBX_NOT_FINISHED) {
1669 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001670 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001671 }
dea31012005-04-17 16:05:31 -05001672
1673 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001674 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001675 writel(HA_LATT, phba->HAregaddr);
1676 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001677 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001678
1679 return;
1680
James Smart14691152006-12-02 13:34:28 -05001681lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001682 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001683 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001684lpfc_handle_latt_free_mp:
1685 kfree(mp);
1686lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001687 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001688lpfc_handle_latt_err_exit:
1689 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001690 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001691 psli->sli_flag |= LPFC_PROCESS_LA;
1692 control = readl(phba->HCregaddr);
1693 control |= HC_LAINT_ENA;
1694 writel(control, phba->HCregaddr);
1695 readl(phba->HCregaddr); /* flush */
1696
1697 /* Clear Link Attention in HA REG */
1698 writel(HA_LATT, phba->HAregaddr);
1699 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001700 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001701 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001702 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001703
James Smart09372822008-01-11 01:52:54 -05001704 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1705 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001706
1707 return;
1708}
1709
James Smarte59058c2008-08-24 21:49:00 -04001710/**
James Smart3621a712009-04-06 18:47:14 -04001711 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001712 * @phba: pointer to lpfc hba data structure.
1713 * @vpd: pointer to the vital product data.
1714 * @len: length of the vital product data in bytes.
1715 *
1716 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1717 * an array of characters. In this routine, the ModelName, ProgramType, and
1718 * ModelDesc, etc. fields of the phba data structure will be populated.
1719 *
1720 * Return codes
1721 * 0 - pointer to the VPD passed in is NULL
1722 * 1 - success
1723 **/
James Smart3772a992009-05-22 14:50:54 -04001724int
James Smart2e0fef82007-06-17 19:56:36 -05001725lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001726{
1727 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001728 int Length;
dea31012005-04-17 16:05:31 -05001729 int i, j;
1730 int finished = 0;
1731 int index = 0;
1732
1733 if (!vpd)
1734 return 0;
1735
1736 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001737 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001738 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001739 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1740 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001741 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001742 switch (vpd[index]) {
1743 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001744 case 0x91:
dea31012005-04-17 16:05:31 -05001745 index += 1;
1746 lenlo = vpd[index];
1747 index += 1;
1748 lenhi = vpd[index];
1749 index += 1;
1750 i = ((((unsigned short)lenhi) << 8) + lenlo);
1751 index += i;
1752 break;
1753 case 0x90:
1754 index += 1;
1755 lenlo = vpd[index];
1756 index += 1;
1757 lenhi = vpd[index];
1758 index += 1;
1759 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001760 if (Length > len - index)
1761 Length = len - index;
dea31012005-04-17 16:05:31 -05001762 while (Length > 0) {
1763 /* Look for Serial Number */
1764 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1765 index += 2;
1766 i = vpd[index];
1767 index += 1;
1768 j = 0;
1769 Length -= (3+i);
1770 while(i--) {
1771 phba->SerialNumber[j++] = vpd[index++];
1772 if (j == 31)
1773 break;
1774 }
1775 phba->SerialNumber[j] = 0;
1776 continue;
1777 }
1778 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1779 phba->vpd_flag |= VPD_MODEL_DESC;
1780 index += 2;
1781 i = vpd[index];
1782 index += 1;
1783 j = 0;
1784 Length -= (3+i);
1785 while(i--) {
1786 phba->ModelDesc[j++] = vpd[index++];
1787 if (j == 255)
1788 break;
1789 }
1790 phba->ModelDesc[j] = 0;
1791 continue;
1792 }
1793 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1794 phba->vpd_flag |= VPD_MODEL_NAME;
1795 index += 2;
1796 i = vpd[index];
1797 index += 1;
1798 j = 0;
1799 Length -= (3+i);
1800 while(i--) {
1801 phba->ModelName[j++] = vpd[index++];
1802 if (j == 79)
1803 break;
1804 }
1805 phba->ModelName[j] = 0;
1806 continue;
1807 }
1808 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1809 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1810 index += 2;
1811 i = vpd[index];
1812 index += 1;
1813 j = 0;
1814 Length -= (3+i);
1815 while(i--) {
1816 phba->ProgramType[j++] = vpd[index++];
1817 if (j == 255)
1818 break;
1819 }
1820 phba->ProgramType[j] = 0;
1821 continue;
1822 }
1823 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1824 phba->vpd_flag |= VPD_PORT;
1825 index += 2;
1826 i = vpd[index];
1827 index += 1;
1828 j = 0;
1829 Length -= (3+i);
1830 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001831 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1832 (phba->sli4_hba.pport_name_sta ==
1833 LPFC_SLI4_PPNAME_GET)) {
1834 j++;
1835 index++;
1836 } else
1837 phba->Port[j++] = vpd[index++];
1838 if (j == 19)
1839 break;
dea31012005-04-17 16:05:31 -05001840 }
James Smartcd1c8302011-10-10 21:33:25 -04001841 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1842 (phba->sli4_hba.pport_name_sta ==
1843 LPFC_SLI4_PPNAME_NON))
1844 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001845 continue;
1846 }
1847 else {
1848 index += 2;
1849 i = vpd[index];
1850 index += 1;
1851 index += i;
1852 Length -= (3 + i);
1853 }
1854 }
1855 finished = 0;
1856 break;
1857 case 0x78:
1858 finished = 1;
1859 break;
1860 default:
1861 index ++;
1862 break;
1863 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001864 }
dea31012005-04-17 16:05:31 -05001865
1866 return(1);
1867}
1868
James Smarte59058c2008-08-24 21:49:00 -04001869/**
James Smart3621a712009-04-06 18:47:14 -04001870 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001871 * @phba: pointer to lpfc hba data structure.
1872 * @mdp: pointer to the data structure to hold the derived model name.
1873 * @descp: pointer to the data structure to hold the derived description.
1874 *
1875 * This routine retrieves HBA's description based on its registered PCI device
1876 * ID. The @descp passed into this function points to an array of 256 chars. It
1877 * shall be returned with the model name, maximum speed, and the host bus type.
1878 * The @mdp passed into this function points to an array of 80 chars. When the
1879 * function returns, the @mdp will be filled with the model name.
1880 **/
dea31012005-04-17 16:05:31 -05001881static void
James Smart2e0fef82007-06-17 19:56:36 -05001882lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001883{
1884 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001885 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001886 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001887 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001888 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001889 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001890 char *name;
1891 char *bus;
1892 char *function;
1893 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001894
1895 if (mdp && mdp[0] != '\0'
1896 && descp && descp[0] != '\0')
1897 return;
1898
James Smartc0c11512011-05-24 11:41:34 -04001899 if (phba->lmt & LMT_16Gb)
1900 max_speed = 16;
1901 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001902 max_speed = 10;
1903 else if (phba->lmt & LMT_8Gb)
1904 max_speed = 8;
1905 else if (phba->lmt & LMT_4Gb)
1906 max_speed = 4;
1907 else if (phba->lmt & LMT_2Gb)
1908 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04001909 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001910 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04001911 else
1912 max_speed = 0;
dea31012005-04-17 16:05:31 -05001913
1914 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001915
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001916 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001917 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001918 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001919 break;
dea31012005-04-17 16:05:31 -05001920 case PCI_DEVICE_ID_SUPERFLY:
1921 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001922 m = (typeof(m)){"LP7000", "PCI",
1923 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001924 else
James Smarta747c9c2009-11-18 15:41:10 -05001925 m = (typeof(m)){"LP7000E", "PCI",
1926 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001927 break;
1928 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001929 m = (typeof(m)){"LP8000", "PCI",
1930 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001931 break;
1932 case PCI_DEVICE_ID_CENTAUR:
1933 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001934 m = (typeof(m)){"LP9002", "PCI",
1935 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001936 else
James Smarta747c9c2009-11-18 15:41:10 -05001937 m = (typeof(m)){"LP9000", "PCI",
1938 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001939 break;
1940 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001941 m = (typeof(m)){"LP952", "PCI",
1942 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001943 break;
1944 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001945 m = (typeof(m)){"LP9802", "PCI-X",
1946 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001947 break;
1948 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001949 m = (typeof(m)){"LP10000", "PCI-X",
1950 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001951 break;
1952 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001953 m = (typeof(m)){"LPX1000", "PCI-X",
1954 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001955 break;
1956 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001957 m = (typeof(m)){"LP982", "PCI-X",
1958 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001959 break;
1960 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001961 m = (typeof(m)){"LP1050", "PCI-X",
1962 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001963 break;
1964 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001965 m = (typeof(m)){"LP11000", "PCI-X2",
1966 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001967 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001968 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001969 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1970 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001971 break;
1972 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001973 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1974 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001975 break;
1976 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001977 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001978 break;
1979 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001980 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001981 break;
1982 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001983 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001984 break;
dea31012005-04-17 16:05:31 -05001985 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001986 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001987 break;
1988 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001989 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001990 break;
1991 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001992 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001993 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001994 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001995 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001996 break;
1997 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001998 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001999 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002000 break;
dea31012005-04-17 16:05:31 -05002001 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002002 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002003 break;
2004 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002005 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002006 break;
2007 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05002008 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002009 break;
2010 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05002011 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002012 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002013 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002014 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002015 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002016 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002017 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002018 break;
James Smartb87eab32007-04-25 09:53:28 -04002019 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002020 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002021 break;
2022 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002023 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002024 break;
2025 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002026 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002027 break;
2028 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002029 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002030 break;
2031 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002032 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002033 break;
2034 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002035 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002036 break;
James Smart84774a42008-08-24 21:50:06 -04002037 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05002038 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002039 GE = 1;
2040 break;
2041 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002042 m = (typeof(m)){"LPev12000", "PCIe IOV",
2043 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002044 break;
2045 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002046 m = (typeof(m)){"LPev12000", "PCIe IOV",
2047 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002048 break;
2049 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002050 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2051 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002052 break;
James Smartda0436e2009-05-22 14:51:39 -04002053 case PCI_DEVICE_ID_TIGERSHARK:
2054 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002055 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002056 break;
James Smarta747c9c2009-11-18 15:41:10 -05002057 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002058 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002059 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2060 break;
2061 case PCI_DEVICE_ID_FALCON:
2062 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2063 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002064 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002065 case PCI_DEVICE_ID_BALIUS:
2066 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2067 "Fibre Channel Adapter"};
2068 break;
James Smart085c6472010-11-20 23:11:37 -05002069 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002070 case PCI_DEVICE_ID_LANCER_FC_VF:
2071 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002072 break;
2073 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002074 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002075 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002076 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002077 break;
James Smartf8cafd32012-08-03 12:42:51 -04002078 case PCI_DEVICE_ID_SKYHAWK:
2079 case PCI_DEVICE_ID_SKYHAWK_VF:
2080 oneConnect = 1;
2081 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2082 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002083 default:
James Smarta747c9c2009-11-18 15:41:10 -05002084 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002085 break;
dea31012005-04-17 16:05:31 -05002086 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002087
2088 if (mdp && mdp[0] == '\0')
2089 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002090 /*
2091 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002092 * and we put the port number on the end
2093 */
2094 if (descp && descp[0] == '\0') {
2095 if (oneConnect)
2096 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002097 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002098 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002099 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002100 else if (max_speed == 0)
2101 snprintf(descp, 255,
2102 "Emulex %s %s %s ",
2103 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002104 else
2105 snprintf(descp, 255,
2106 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002107 m.name, max_speed, (GE) ? "GE" : "Gb",
2108 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002109 }
dea31012005-04-17 16:05:31 -05002110}
2111
James Smarte59058c2008-08-24 21:49:00 -04002112/**
James Smart3621a712009-04-06 18:47:14 -04002113 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002114 * @phba: pointer to lpfc hba data structure.
2115 * @pring: pointer to a IOCB ring.
2116 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2117 *
2118 * This routine posts a given number of IOCBs with the associated DMA buffer
2119 * descriptors specified by the cnt argument to the given IOCB ring.
2120 *
2121 * Return codes
2122 * The number of IOCBs NOT able to be posted to the IOCB ring.
2123 **/
dea31012005-04-17 16:05:31 -05002124int
James Smart495a7142008-06-14 22:52:59 -04002125lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002126{
2127 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002128 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002129 struct lpfc_dmabuf *mp1, *mp2;
2130
2131 cnt += pring->missbufcnt;
2132
2133 /* While there are buffers to post */
2134 while (cnt > 0) {
2135 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002136 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002137 if (iocb == NULL) {
2138 pring->missbufcnt = cnt;
2139 return cnt;
2140 }
dea31012005-04-17 16:05:31 -05002141 icmd = &iocb->iocb;
2142
2143 /* 2 buffers can be posted per command */
2144 /* Allocate buffer to post */
2145 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2146 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002147 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2148 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002149 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002150 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002151 pring->missbufcnt = cnt;
2152 return cnt;
2153 }
2154
2155 INIT_LIST_HEAD(&mp1->list);
2156 /* Allocate buffer to post */
2157 if (cnt > 1) {
2158 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2159 if (mp2)
2160 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2161 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002162 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002163 kfree(mp2);
dea31012005-04-17 16:05:31 -05002164 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2165 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002166 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002167 pring->missbufcnt = cnt;
2168 return cnt;
2169 }
2170
2171 INIT_LIST_HEAD(&mp2->list);
2172 } else {
2173 mp2 = NULL;
2174 }
2175
2176 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2177 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2178 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2179 icmd->ulpBdeCount = 1;
2180 cnt--;
2181 if (mp2) {
2182 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2183 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2184 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2185 cnt--;
2186 icmd->ulpBdeCount = 2;
2187 }
2188
2189 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2190 icmd->ulpLe = 1;
2191
James Smart3772a992009-05-22 14:50:54 -04002192 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2193 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002194 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2195 kfree(mp1);
2196 cnt++;
2197 if (mp2) {
2198 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2199 kfree(mp2);
2200 cnt++;
2201 }
James Bottomley604a3e32005-10-29 10:28:33 -05002202 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002203 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002204 return cnt;
2205 }
dea31012005-04-17 16:05:31 -05002206 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002207 if (mp2)
dea31012005-04-17 16:05:31 -05002208 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002209 }
2210 pring->missbufcnt = 0;
2211 return 0;
2212}
2213
James Smarte59058c2008-08-24 21:49:00 -04002214/**
James Smart3621a712009-04-06 18:47:14 -04002215 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002216 * @phba: pointer to lpfc hba data structure.
2217 *
2218 * This routine posts initial receive IOCB buffers to the ELS ring. The
2219 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2220 * set to 64 IOCBs.
2221 *
2222 * Return codes
2223 * 0 - success (currently always success)
2224 **/
dea31012005-04-17 16:05:31 -05002225static int
James Smart2e0fef82007-06-17 19:56:36 -05002226lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002227{
2228 struct lpfc_sli *psli = &phba->sli;
2229
2230 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002231 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002232 /* Ring 2 - FCP no buffers needed */
2233
2234 return 0;
2235}
2236
2237#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2238
James Smarte59058c2008-08-24 21:49:00 -04002239/**
James Smart3621a712009-04-06 18:47:14 -04002240 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002241 * @HashResultPointer: pointer to an array as hash table.
2242 *
2243 * This routine sets up the initial values to the array of hash table entries
2244 * for the LC HBAs.
2245 **/
dea31012005-04-17 16:05:31 -05002246static void
2247lpfc_sha_init(uint32_t * HashResultPointer)
2248{
2249 HashResultPointer[0] = 0x67452301;
2250 HashResultPointer[1] = 0xEFCDAB89;
2251 HashResultPointer[2] = 0x98BADCFE;
2252 HashResultPointer[3] = 0x10325476;
2253 HashResultPointer[4] = 0xC3D2E1F0;
2254}
2255
James Smarte59058c2008-08-24 21:49:00 -04002256/**
James Smart3621a712009-04-06 18:47:14 -04002257 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002258 * @HashResultPointer: pointer to an initial/result hash table.
2259 * @HashWorkingPointer: pointer to an working hash table.
2260 *
2261 * This routine iterates an initial hash table pointed by @HashResultPointer
2262 * with the values from the working hash table pointeed by @HashWorkingPointer.
2263 * The results are putting back to the initial hash table, returned through
2264 * the @HashResultPointer as the result hash table.
2265 **/
dea31012005-04-17 16:05:31 -05002266static void
2267lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2268{
2269 int t;
2270 uint32_t TEMP;
2271 uint32_t A, B, C, D, E;
2272 t = 16;
2273 do {
2274 HashWorkingPointer[t] =
2275 S(1,
2276 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2277 8] ^
2278 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2279 } while (++t <= 79);
2280 t = 0;
2281 A = HashResultPointer[0];
2282 B = HashResultPointer[1];
2283 C = HashResultPointer[2];
2284 D = HashResultPointer[3];
2285 E = HashResultPointer[4];
2286
2287 do {
2288 if (t < 20) {
2289 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2290 } else if (t < 40) {
2291 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2292 } else if (t < 60) {
2293 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2294 } else {
2295 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2296 }
2297 TEMP += S(5, A) + E + HashWorkingPointer[t];
2298 E = D;
2299 D = C;
2300 C = S(30, B);
2301 B = A;
2302 A = TEMP;
2303 } while (++t <= 79);
2304
2305 HashResultPointer[0] += A;
2306 HashResultPointer[1] += B;
2307 HashResultPointer[2] += C;
2308 HashResultPointer[3] += D;
2309 HashResultPointer[4] += E;
2310
2311}
2312
James Smarte59058c2008-08-24 21:49:00 -04002313/**
James Smart3621a712009-04-06 18:47:14 -04002314 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002315 * @RandomChallenge: pointer to the entry of host challenge random number array.
2316 * @HashWorking: pointer to the entry of the working hash array.
2317 *
2318 * This routine calculates the working hash array referred by @HashWorking
2319 * from the challenge random numbers associated with the host, referred by
2320 * @RandomChallenge. The result is put into the entry of the working hash
2321 * array and returned by reference through @HashWorking.
2322 **/
dea31012005-04-17 16:05:31 -05002323static void
2324lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2325{
2326 *HashWorking = (*RandomChallenge ^ *HashWorking);
2327}
2328
James Smarte59058c2008-08-24 21:49:00 -04002329/**
James Smart3621a712009-04-06 18:47:14 -04002330 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002331 * @phba: pointer to lpfc hba data structure.
2332 * @hbainit: pointer to an array of unsigned 32-bit integers.
2333 *
2334 * This routine performs the special handling for LC HBA initialization.
2335 **/
dea31012005-04-17 16:05:31 -05002336void
2337lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2338{
2339 int t;
2340 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002341 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002342
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002343 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002344 if (!HashWorking)
2345 return;
2346
dea31012005-04-17 16:05:31 -05002347 HashWorking[0] = HashWorking[78] = *pwwnn++;
2348 HashWorking[1] = HashWorking[79] = *pwwnn;
2349
2350 for (t = 0; t < 7; t++)
2351 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2352
2353 lpfc_sha_init(hbainit);
2354 lpfc_sha_iterate(hbainit, HashWorking);
2355 kfree(HashWorking);
2356}
2357
James Smarte59058c2008-08-24 21:49:00 -04002358/**
James Smart3621a712009-04-06 18:47:14 -04002359 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002360 * @vport: pointer to a virtual N_Port data structure.
2361 *
2362 * This routine performs the necessary cleanups before deleting the @vport.
2363 * It invokes the discovery state machine to perform necessary state
2364 * transitions and to release the ndlps associated with the @vport. Note,
2365 * the physical port is treated as @vport 0.
2366 **/
James Smart87af33f2007-10-27 13:37:43 -04002367void
James Smart2e0fef82007-06-17 19:56:36 -05002368lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002369{
James Smart87af33f2007-10-27 13:37:43 -04002370 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002371 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002372 int i = 0;
dea31012005-04-17 16:05:31 -05002373
James Smart87af33f2007-10-27 13:37:43 -04002374 if (phba->link_state > LPFC_LINK_DOWN)
2375 lpfc_port_link_failure(vport);
2376
2377 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002378 if (!NLP_CHK_NODE_ACT(ndlp)) {
2379 ndlp = lpfc_enable_node(vport, ndlp,
2380 NLP_STE_UNUSED_NODE);
2381 if (!ndlp)
2382 continue;
2383 spin_lock_irq(&phba->ndlp_lock);
2384 NLP_SET_FREE_REQ(ndlp);
2385 spin_unlock_irq(&phba->ndlp_lock);
2386 /* Trigger the release of the ndlp memory */
2387 lpfc_nlp_put(ndlp);
2388 continue;
2389 }
2390 spin_lock_irq(&phba->ndlp_lock);
2391 if (NLP_CHK_FREE_REQ(ndlp)) {
2392 /* The ndlp should not be in memory free mode already */
2393 spin_unlock_irq(&phba->ndlp_lock);
2394 continue;
2395 } else
2396 /* Indicate request for freeing ndlp memory */
2397 NLP_SET_FREE_REQ(ndlp);
2398 spin_unlock_irq(&phba->ndlp_lock);
2399
James Smart58da1ff2008-04-07 10:15:56 -04002400 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2401 ndlp->nlp_DID == Fabric_DID) {
2402 /* Just free up ndlp with Fabric_DID for vports */
2403 lpfc_nlp_put(ndlp);
2404 continue;
2405 }
2406
James Smarteff4a012012-01-18 16:25:25 -05002407 /* take care of nodes in unused state before the state
2408 * machine taking action.
2409 */
2410 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2411 lpfc_nlp_put(ndlp);
2412 continue;
2413 }
2414
James Smart87af33f2007-10-27 13:37:43 -04002415 if (ndlp->nlp_type & NLP_FABRIC)
2416 lpfc_disc_state_machine(vport, ndlp, NULL,
2417 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002418
James Smart87af33f2007-10-27 13:37:43 -04002419 lpfc_disc_state_machine(vport, ndlp, NULL,
2420 NLP_EVT_DEVICE_RM);
2421 }
2422
James Smarta8adb832007-10-27 13:37:53 -04002423 /* At this point, ALL ndlp's should be gone
2424 * because of the previous NLP_EVT_DEVICE_RM.
2425 * Lets wait for this to happen, if needed.
2426 */
James Smart87af33f2007-10-27 13:37:43 -04002427 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002428 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002429 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002430 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002431 list_for_each_entry_safe(ndlp, next_ndlp,
2432 &vport->fc_nodes, nlp_listp) {
2433 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2434 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002435 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002436 "usgmap:x%x refcnt:%d\n",
2437 ndlp->nlp_DID, (void *)ndlp,
2438 ndlp->nlp_usg_map,
2439 atomic_read(
2440 &ndlp->kref.refcount));
2441 }
James Smarta8adb832007-10-27 13:37:53 -04002442 break;
James Smart87af33f2007-10-27 13:37:43 -04002443 }
James Smarta8adb832007-10-27 13:37:53 -04002444
2445 /* Wait for any activity on ndlps to settle */
2446 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002447 }
James Smart1151e3e2011-02-16 12:39:35 -05002448 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002449}
2450
James Smarte59058c2008-08-24 21:49:00 -04002451/**
James Smart3621a712009-04-06 18:47:14 -04002452 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002453 * @vport: pointer to a virtual N_Port data structure.
2454 *
2455 * This routine stops all the timers associated with a @vport. This function
2456 * is invoked before disabling or deleting a @vport. Note that the physical
2457 * port is treated as @vport 0.
2458 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002459void
2460lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002461{
James Smart92d7f7b2007-06-17 19:56:38 -05002462 del_timer_sync(&vport->els_tmofunc);
2463 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002464 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002465 lpfc_can_disctmo(vport);
2466 return;
dea31012005-04-17 16:05:31 -05002467}
2468
James Smarte59058c2008-08-24 21:49:00 -04002469/**
James Smartecfd03c2010-02-12 14:41:27 -05002470 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2471 * @phba: pointer to lpfc hba data structure.
2472 *
2473 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2474 * caller of this routine should already hold the host lock.
2475 **/
2476void
2477__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2478{
James Smart5ac6b302010-10-22 11:05:36 -04002479 /* Clear pending FCF rediscovery wait flag */
2480 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2481
James Smartecfd03c2010-02-12 14:41:27 -05002482 /* Now, try to stop the timer */
2483 del_timer(&phba->fcf.redisc_wait);
2484}
2485
2486/**
2487 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2488 * @phba: pointer to lpfc hba data structure.
2489 *
2490 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2491 * checks whether the FCF rediscovery wait timer is pending with the host
2492 * lock held before proceeding with disabling the timer and clearing the
2493 * wait timer pendig flag.
2494 **/
2495void
2496lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2497{
2498 spin_lock_irq(&phba->hbalock);
2499 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2500 /* FCF rediscovery timer already fired or stopped */
2501 spin_unlock_irq(&phba->hbalock);
2502 return;
2503 }
2504 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002505 /* Clear failover in progress flags */
2506 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002507 spin_unlock_irq(&phba->hbalock);
2508}
2509
2510/**
James Smart3772a992009-05-22 14:50:54 -04002511 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002512 * @phba: pointer to lpfc hba data structure.
2513 *
2514 * This routine stops all the timers associated with a HBA. This function is
2515 * invoked before either putting a HBA offline or unloading the driver.
2516 **/
James Smart3772a992009-05-22 14:50:54 -04002517void
2518lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002519{
James Smart51ef4c22007-08-02 11:10:31 -04002520 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002521 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002522 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002523 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002524 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002525 if (phba->sli_rev == LPFC_SLI_REV4) {
2526 del_timer_sync(&phba->rrq_tmr);
2527 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2528 }
James Smart3772a992009-05-22 14:50:54 -04002529 phba->hb_outstanding = 0;
2530
2531 switch (phba->pci_dev_grp) {
2532 case LPFC_PCI_DEV_LP:
2533 /* Stop any LightPulse device specific driver timers */
2534 del_timer_sync(&phba->fcp_poll_timer);
2535 break;
2536 case LPFC_PCI_DEV_OC:
2537 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002538 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002539 break;
2540 default:
2541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2542 "0297 Invalid device group (x%x)\n",
2543 phba->pci_dev_grp);
2544 break;
2545 }
James Smart2e0fef82007-06-17 19:56:36 -05002546 return;
dea31012005-04-17 16:05:31 -05002547}
2548
James Smarte59058c2008-08-24 21:49:00 -04002549/**
James Smart3621a712009-04-06 18:47:14 -04002550 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002551 * @phba: pointer to lpfc hba data structure.
2552 *
2553 * This routine marks a HBA's management interface as blocked. Once the HBA's
2554 * management interface is marked as blocked, all the user space access to
2555 * the HBA, whether they are from sysfs interface or libdfc interface will
2556 * all be blocked. The HBA is set to block the management interface when the
2557 * driver prepares the HBA interface for online or offline.
2558 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002559static void
James Smart618a5232012-06-12 13:54:36 -04002560lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002561{
2562 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002563 uint8_t actcmd = MBX_HEARTBEAT;
2564 unsigned long timeout;
2565
Adrian Bunka6ababd2007-11-05 18:07:33 +01002566 spin_lock_irqsave(&phba->hbalock, iflag);
2567 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002568 spin_unlock_irqrestore(&phba->hbalock, iflag);
2569 if (mbx_action == LPFC_MBX_NO_WAIT)
2570 return;
2571 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2572 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002573 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002574 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002575 /* Determine how long we might wait for the active mailbox
2576 * command to be gracefully completed by firmware.
2577 */
2578 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2579 phba->sli.mbox_active) * 1000) + jiffies;
2580 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002581 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002582
James Smart6e7288d2010-06-07 15:23:35 -04002583 /* Wait for the outstnading mailbox command to complete */
2584 while (phba->sli.mbox_active) {
2585 /* Check active mailbox complete status every 2ms */
2586 msleep(2);
2587 if (time_after(jiffies, timeout)) {
2588 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2589 "2813 Mgmt IO is Blocked %x "
2590 "- mbox cmd %x still active\n",
2591 phba->sli.sli_flag, actcmd);
2592 break;
2593 }
2594 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002595}
2596
James Smarte59058c2008-08-24 21:49:00 -04002597/**
James Smart6b5151f2012-01-18 16:24:06 -05002598 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2599 * @phba: pointer to lpfc hba data structure.
2600 *
2601 * Allocate RPIs for all active remote nodes. This is needed whenever
2602 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2603 * is to fixup the temporary rpi assignments.
2604 **/
2605void
2606lpfc_sli4_node_prep(struct lpfc_hba *phba)
2607{
2608 struct lpfc_nodelist *ndlp, *next_ndlp;
2609 struct lpfc_vport **vports;
2610 int i;
2611
2612 if (phba->sli_rev != LPFC_SLI_REV4)
2613 return;
2614
2615 vports = lpfc_create_vport_work_array(phba);
2616 if (vports != NULL) {
2617 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2618 if (vports[i]->load_flag & FC_UNLOADING)
2619 continue;
2620
2621 list_for_each_entry_safe(ndlp, next_ndlp,
2622 &vports[i]->fc_nodes,
2623 nlp_listp) {
2624 if (NLP_CHK_NODE_ACT(ndlp))
2625 ndlp->nlp_rpi =
2626 lpfc_sli4_alloc_rpi(phba);
2627 }
2628 }
2629 }
2630 lpfc_destroy_vport_work_array(phba, vports);
2631}
2632
2633/**
James Smart3621a712009-04-06 18:47:14 -04002634 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002635 * @phba: pointer to lpfc hba data structure.
2636 *
2637 * This routine initializes the HBA and brings a HBA online. During this
2638 * process, the management interface is blocked to prevent user space access
2639 * to the HBA interfering with the driver initialization.
2640 *
2641 * Return codes
2642 * 0 - successful
2643 * 1 - failed
2644 **/
dea31012005-04-17 16:05:31 -05002645int
James Smart2e0fef82007-06-17 19:56:36 -05002646lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002647{
Julia Lawall372bd282008-12-16 16:15:08 +01002648 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002649 struct lpfc_vport **vports;
2650 int i;
James Smart16a3a202013-04-17 20:14:38 -04002651 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002652
dea31012005-04-17 16:05:31 -05002653 if (!phba)
2654 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002655 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002656
James Smart2e0fef82007-06-17 19:56:36 -05002657 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002658 return 0;
2659
James Smarted957682007-06-17 19:56:37 -05002660 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002661 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002662
James Smart618a5232012-06-12 13:54:36 -04002663 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002664
James Smart46fa3112007-04-25 09:51:45 -04002665 if (!lpfc_sli_queue_setup(phba)) {
2666 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002667 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002668 }
2669
James Smartda0436e2009-05-22 14:51:39 -04002670 if (phba->sli_rev == LPFC_SLI_REV4) {
2671 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2672 lpfc_unblock_mgmt_io(phba);
2673 return 1;
2674 }
James Smart16a3a202013-04-17 20:14:38 -04002675 spin_lock_irq(&phba->hbalock);
2676 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2677 vpis_cleared = true;
2678 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002679 } else {
2680 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2681 lpfc_unblock_mgmt_io(phba);
2682 return 1;
2683 }
James Smart46fa3112007-04-25 09:51:45 -04002684 }
dea31012005-04-17 16:05:31 -05002685
James Smart549e55c2007-08-02 11:09:51 -04002686 vports = lpfc_create_vport_work_array(phba);
2687 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002688 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002689 struct Scsi_Host *shost;
2690 shost = lpfc_shost_from_vport(vports[i]);
2691 spin_lock_irq(shost->host_lock);
2692 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2693 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2694 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002695 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002696 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002697 if ((vpis_cleared) &&
2698 (vports[i]->port_type !=
2699 LPFC_PHYSICAL_PORT))
2700 vports[i]->vpi = 0;
2701 }
James Smart549e55c2007-08-02 11:09:51 -04002702 spin_unlock_irq(shost->host_lock);
2703 }
James Smart09372822008-01-11 01:52:54 -05002704 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002705
James Smart46fa3112007-04-25 09:51:45 -04002706 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002707 return 0;
2708}
2709
James Smarte59058c2008-08-24 21:49:00 -04002710/**
James Smart3621a712009-04-06 18:47:14 -04002711 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002712 * @phba: pointer to lpfc hba data structure.
2713 *
2714 * This routine marks a HBA's management interface as not blocked. Once the
2715 * HBA's management interface is marked as not blocked, all the user space
2716 * access to the HBA, whether they are from sysfs interface or libdfc
2717 * interface will be allowed. The HBA is set to block the management interface
2718 * when the driver prepares the HBA interface for online or offline and then
2719 * set to unblock the management interface afterwards.
2720 **/
James Smart46fa3112007-04-25 09:51:45 -04002721void
James Smart46fa3112007-04-25 09:51:45 -04002722lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2723{
2724 unsigned long iflag;
2725
James Smart2e0fef82007-06-17 19:56:36 -05002726 spin_lock_irqsave(&phba->hbalock, iflag);
2727 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2728 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002729}
2730
James Smarte59058c2008-08-24 21:49:00 -04002731/**
James Smart3621a712009-04-06 18:47:14 -04002732 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002733 * @phba: pointer to lpfc hba data structure.
2734 *
2735 * This routine is invoked to prepare a HBA to be brought offline. It performs
2736 * unregistration login to all the nodes on all vports and flushes the mailbox
2737 * queue to make it ready to be brought offline.
2738 **/
James Smart46fa3112007-04-25 09:51:45 -04002739void
James Smart618a5232012-06-12 13:54:36 -04002740lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002741{
James Smart2e0fef82007-06-17 19:56:36 -05002742 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002743 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002744 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002745 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002746 int i;
dea31012005-04-17 16:05:31 -05002747
James Smart2e0fef82007-06-17 19:56:36 -05002748 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002749 return;
dea31012005-04-17 16:05:31 -05002750
James Smart618a5232012-06-12 13:54:36 -04002751 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002752
2753 lpfc_linkdown(phba);
2754
James Smart87af33f2007-10-27 13:37:43 -04002755 /* Issue an unreg_login to all nodes on all vports */
2756 vports = lpfc_create_vport_work_array(phba);
2757 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002758 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002759 if (vports[i]->load_flag & FC_UNLOADING)
2760 continue;
James Smart72100cc2010-02-12 14:43:01 -05002761 shost = lpfc_shost_from_vport(vports[i]);
2762 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002763 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002764 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2765 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002766 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002767
James Smart87af33f2007-10-27 13:37:43 -04002768 shost = lpfc_shost_from_vport(vports[i]);
2769 list_for_each_entry_safe(ndlp, next_ndlp,
2770 &vports[i]->fc_nodes,
2771 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002772 if (!NLP_CHK_NODE_ACT(ndlp))
2773 continue;
James Smart87af33f2007-10-27 13:37:43 -04002774 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2775 continue;
2776 if (ndlp->nlp_type & NLP_FABRIC) {
2777 lpfc_disc_state_machine(vports[i], ndlp,
2778 NULL, NLP_EVT_DEVICE_RECOVERY);
2779 lpfc_disc_state_machine(vports[i], ndlp,
2780 NULL, NLP_EVT_DEVICE_RM);
2781 }
2782 spin_lock_irq(shost->host_lock);
2783 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002784 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002785 /*
2786 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002787 * RPI. Get a new RPI when the adapter port
2788 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002789 */
2790 if (phba->sli_rev == LPFC_SLI_REV4)
2791 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002792 lpfc_unreg_rpi(vports[i], ndlp);
2793 }
2794 }
2795 }
James Smart09372822008-01-11 01:52:54 -05002796 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002797
James Smart618a5232012-06-12 13:54:36 -04002798 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002799}
2800
James Smarte59058c2008-08-24 21:49:00 -04002801/**
James Smart3621a712009-04-06 18:47:14 -04002802 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002803 * @phba: pointer to lpfc hba data structure.
2804 *
2805 * This routine actually brings a HBA offline. It stops all the timers
2806 * associated with the HBA, brings down the SLI layer, and eventually
2807 * marks the HBA as in offline state for the upper layer protocol.
2808 **/
James Smart46fa3112007-04-25 09:51:45 -04002809void
James Smart2e0fef82007-06-17 19:56:36 -05002810lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002811{
James Smart549e55c2007-08-02 11:09:51 -04002812 struct Scsi_Host *shost;
2813 struct lpfc_vport **vports;
2814 int i;
James Smart46fa3112007-04-25 09:51:45 -04002815
James Smart549e55c2007-08-02 11:09:51 -04002816 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002817 return;
James Smart688a8862006-07-06 15:49:56 -04002818
James Smartda0436e2009-05-22 14:51:39 -04002819 /* stop port and all timers associated with this hba */
2820 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002821 vports = lpfc_create_vport_work_array(phba);
2822 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002823 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002824 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002825 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002826 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002827 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002828 /* Bring down the SLI Layer and cleanup. The HBA is offline
2829 now. */
2830 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002831 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002832 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002833 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002834 vports = lpfc_create_vport_work_array(phba);
2835 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002836 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002837 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002838 spin_lock_irq(shost->host_lock);
2839 vports[i]->work_port_events = 0;
2840 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2841 spin_unlock_irq(shost->host_lock);
2842 }
James Smart09372822008-01-11 01:52:54 -05002843 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002844}
2845
James Smarte59058c2008-08-24 21:49:00 -04002846/**
James Smart3621a712009-04-06 18:47:14 -04002847 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002848 * @phba: pointer to lpfc hba data structure.
2849 *
2850 * This routine is to free all the SCSI buffers and IOCBs from the driver
2851 * list back to kernel. It is called from lpfc_pci_remove_one to free
2852 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002853 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002854static void
James Smart2e0fef82007-06-17 19:56:36 -05002855lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002856{
2857 struct lpfc_scsi_buf *sb, *sb_next;
2858 struct lpfc_iocbq *io, *io_next;
2859
James Smart2e0fef82007-06-17 19:56:36 -05002860 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04002861
dea31012005-04-17 16:05:31 -05002862 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04002863
2864 spin_lock(&phba->scsi_buf_list_put_lock);
2865 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
2866 list) {
dea31012005-04-17 16:05:31 -05002867 list_del(&sb->list);
2868 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002869 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002870 kfree(sb);
2871 phba->total_scsi_bufs--;
2872 }
James Smarta40fc5f2013-04-17 20:17:40 -04002873 spin_unlock(&phba->scsi_buf_list_put_lock);
2874
2875 spin_lock(&phba->scsi_buf_list_get_lock);
2876 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
2877 list) {
2878 list_del(&sb->list);
2879 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2880 sb->dma_handle);
2881 kfree(sb);
2882 phba->total_scsi_bufs--;
2883 }
2884 spin_unlock(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05002885
2886 /* Release all the lpfc_iocbq entries maintained by this host. */
2887 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2888 list_del(&io->list);
2889 kfree(io);
2890 phba->total_iocbq_bufs--;
2891 }
James Smart6d368e52011-05-24 11:44:12 -04002892
James Smart2e0fef82007-06-17 19:56:36 -05002893 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002894}
2895
2896/**
2897 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2898 * @phba: pointer to lpfc hba data structure.
2899 *
2900 * This routine first calculates the sizes of the current els and allocated
2901 * scsi sgl lists, and then goes through all sgls to updates the physical
2902 * XRIs assigned due to port function reset. During port initialization, the
2903 * current els and allocated scsi sgl lists are 0s.
2904 *
2905 * Return codes
2906 * 0 - successful (for now, it always returns 0)
2907 **/
2908int
2909lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2910{
2911 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2912 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2913 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2914 LIST_HEAD(els_sgl_list);
2915 LIST_HEAD(scsi_sgl_list);
2916 int rc;
2917
2918 /*
2919 * update on pci function's els xri-sgl list
2920 */
2921 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2922 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2923 /* els xri-sgl expanded */
2924 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2925 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2926 "3157 ELS xri-sgl count increased from "
2927 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2928 els_xri_cnt);
2929 /* allocate the additional els sgls */
2930 for (i = 0; i < xri_cnt; i++) {
2931 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
2932 GFP_KERNEL);
2933 if (sglq_entry == NULL) {
2934 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2935 "2562 Failure to allocate an "
2936 "ELS sgl entry:%d\n", i);
2937 rc = -ENOMEM;
2938 goto out_free_mem;
2939 }
2940 sglq_entry->buff_type = GEN_BUFF_TYPE;
2941 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
2942 &sglq_entry->phys);
2943 if (sglq_entry->virt == NULL) {
2944 kfree(sglq_entry);
2945 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2946 "2563 Failure to allocate an "
2947 "ELS mbuf:%d\n", i);
2948 rc = -ENOMEM;
2949 goto out_free_mem;
2950 }
2951 sglq_entry->sgl = sglq_entry->virt;
2952 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
2953 sglq_entry->state = SGL_FREED;
2954 list_add_tail(&sglq_entry->list, &els_sgl_list);
2955 }
James Smart38c20672013-03-01 16:37:44 -05002956 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002957 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05002958 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002959 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
2960 /* els xri-sgl shrinked */
2961 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
2962 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2963 "3158 ELS xri-sgl count decreased from "
2964 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2965 els_xri_cnt);
2966 spin_lock_irq(&phba->hbalock);
2967 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
2968 spin_unlock_irq(&phba->hbalock);
2969 /* release extra els sgls from list */
2970 for (i = 0; i < xri_cnt; i++) {
2971 list_remove_head(&els_sgl_list,
2972 sglq_entry, struct lpfc_sglq, list);
2973 if (sglq_entry) {
2974 lpfc_mbuf_free(phba, sglq_entry->virt,
2975 sglq_entry->phys);
2976 kfree(sglq_entry);
2977 }
2978 }
2979 spin_lock_irq(&phba->hbalock);
2980 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2981 spin_unlock_irq(&phba->hbalock);
2982 } else
2983 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2984 "3163 ELS xri-sgl count unchanged: %d\n",
2985 els_xri_cnt);
2986 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
2987
2988 /* update xris to els sgls on the list */
2989 sglq_entry = NULL;
2990 sglq_entry_next = NULL;
2991 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
2992 &phba->sli4_hba.lpfc_sgl_list, list) {
2993 lxri = lpfc_sli4_next_xritag(phba);
2994 if (lxri == NO_XRI) {
2995 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2996 "2400 Failed to allocate xri for "
2997 "ELS sgl\n");
2998 rc = -ENOMEM;
2999 goto out_free_mem;
3000 }
3001 sglq_entry->sli4_lxritag = lxri;
3002 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3003 }
3004
3005 /*
3006 * update on pci function's allocated scsi xri-sgl list
3007 */
3008 phba->total_scsi_bufs = 0;
3009
3010 /* maximum number of xris available for scsi buffers */
3011 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3012 els_xri_cnt;
3013
3014 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3015 "2401 Current allocated SCSI xri-sgl count:%d, "
3016 "maximum SCSI xri count:%d\n",
3017 phba->sli4_hba.scsi_xri_cnt,
3018 phba->sli4_hba.scsi_xri_max);
3019
James Smarta40fc5f2013-04-17 20:17:40 -04003020 spin_lock_irq(&phba->scsi_buf_list_get_lock);
3021 spin_lock_irq(&phba->scsi_buf_list_put_lock);
3022 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3023 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
3024 spin_unlock_irq(&phba->scsi_buf_list_put_lock);
3025 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003026
3027 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3028 /* max scsi xri shrinked below the allocated scsi buffers */
3029 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3030 phba->sli4_hba.scsi_xri_max;
3031 /* release the extra allocated scsi buffers */
3032 for (i = 0; i < scsi_xri_cnt; i++) {
3033 list_remove_head(&scsi_sgl_list, psb,
3034 struct lpfc_scsi_buf, list);
3035 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
3036 psb->dma_handle);
3037 kfree(psb);
3038 }
James Smarta40fc5f2013-04-17 20:17:40 -04003039 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003040 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003041 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003042 }
3043
3044 /* update xris associated to remaining allocated scsi buffers */
3045 psb = NULL;
3046 psb_next = NULL;
3047 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3048 lxri = lpfc_sli4_next_xritag(phba);
3049 if (lxri == NO_XRI) {
3050 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3051 "2560 Failed to allocate xri for "
3052 "scsi buffer\n");
3053 rc = -ENOMEM;
3054 goto out_free_mem;
3055 }
3056 psb->cur_iocbq.sli4_lxritag = lxri;
3057 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3058 }
James Smarta40fc5f2013-04-17 20:17:40 -04003059 spin_lock_irq(&phba->scsi_buf_list_get_lock);
3060 spin_lock_irq(&phba->scsi_buf_list_put_lock);
3061 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3062 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
3063 spin_unlock_irq(&phba->scsi_buf_list_put_lock);
3064 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003065
dea31012005-04-17 16:05:31 -05003066 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003067
3068out_free_mem:
3069 lpfc_free_els_sgl_list(phba);
3070 lpfc_scsi_free(phba);
3071 return rc;
dea31012005-04-17 16:05:31 -05003072}
3073
James Smarte59058c2008-08-24 21:49:00 -04003074/**
James Smart3621a712009-04-06 18:47:14 -04003075 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003076 * @phba: pointer to lpfc hba data structure.
3077 * @instance: a unique integer ID to this FC port.
3078 * @dev: pointer to the device data structure.
3079 *
3080 * This routine creates a FC port for the upper layer protocol. The FC port
3081 * can be created on top of either a physical port or a virtual port provided
3082 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3083 * and associates the FC port created before adding the shost into the SCSI
3084 * layer.
3085 *
3086 * Return codes
3087 * @vport - pointer to the virtual N_Port data structure.
3088 * NULL - port create failed.
3089 **/
James Smart2e0fef82007-06-17 19:56:36 -05003090struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003091lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003092{
James Smart2e0fef82007-06-17 19:56:36 -05003093 struct lpfc_vport *vport;
3094 struct Scsi_Host *shost;
3095 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003096
James Smart3de2a652007-08-02 11:09:59 -04003097 if (dev != &phba->pcidev->dev)
3098 shost = scsi_host_alloc(&lpfc_vport_template,
3099 sizeof(struct lpfc_vport));
3100 else
3101 shost = scsi_host_alloc(&lpfc_template,
3102 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003103 if (!shost)
3104 goto out;
James Smart47a86172007-04-25 09:53:22 -04003105
James Smart2e0fef82007-06-17 19:56:36 -05003106 vport = (struct lpfc_vport *) shost->hostdata;
3107 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003108 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003109 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003110 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003111
James Smart3de2a652007-08-02 11:09:59 -04003112 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003113 shost->unique_id = instance;
3114 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003115 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003116 shost->this_id = -1;
3117 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003118 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003119 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003120 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003121 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3122 }
James Smart81301a92008-12-04 22:39:46 -05003123
James Smart47a86172007-04-25 09:53:22 -04003124 /*
James Smart2e0fef82007-06-17 19:56:36 -05003125 * Set initial can_queue value since 0 is no longer supported and
3126 * scsi_add_host will fail. This will be adjusted later based on the
3127 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003128 */
James Smart2e0fef82007-06-17 19:56:36 -05003129 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003130 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003131 shost->transportt = lpfc_vport_transport_template;
3132 vport->port_type = LPFC_NPIV_PORT;
3133 } else {
3134 shost->transportt = lpfc_transport_template;
3135 vport->port_type = LPFC_PHYSICAL_PORT;
3136 }
James Smart47a86172007-04-25 09:53:22 -04003137
James Smart2e0fef82007-06-17 19:56:36 -05003138 /* Initialize all internally managed lists. */
3139 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003140 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003141 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003142
James Smart2e0fef82007-06-17 19:56:36 -05003143 init_timer(&vport->fc_disctmo);
3144 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003145 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003146
James Smart2e0fef82007-06-17 19:56:36 -05003147 init_timer(&vport->fc_fdmitmo);
3148 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003149 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003150
James Smart2e0fef82007-06-17 19:56:36 -05003151 init_timer(&vport->els_tmofunc);
3152 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003153 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003154
3155 init_timer(&vport->delayed_disc_tmo);
3156 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3157 vport->delayed_disc_tmo.data = (unsigned long)vport;
3158
James Bottomleyd139b9b2009-11-05 13:33:12 -06003159 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003160 if (error)
3161 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003162
James Smart549e55c2007-08-02 11:09:51 -04003163 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003164 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003165 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003166 return vport;
James Smart47a86172007-04-25 09:53:22 -04003167
James Smart2e0fef82007-06-17 19:56:36 -05003168out_put_shost:
3169 scsi_host_put(shost);
3170out:
3171 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003172}
3173
James Smarte59058c2008-08-24 21:49:00 -04003174/**
James Smart3621a712009-04-06 18:47:14 -04003175 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003176 * @vport: pointer to an lpfc virtual N_Port data structure.
3177 *
3178 * This routine destroys a FC port from the upper layer protocol. All the
3179 * resources associated with the port are released.
3180 **/
James Smart2e0fef82007-06-17 19:56:36 -05003181void
3182destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003183{
James Smart92d7f7b2007-06-17 19:56:38 -05003184 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3185 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003186
James Smart858c9f62007-06-17 19:56:39 -05003187 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003188 fc_remove_host(shost);
3189 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003190
James Smart92d7f7b2007-06-17 19:56:38 -05003191 spin_lock_irq(&phba->hbalock);
3192 list_del_init(&vport->listentry);
3193 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003194
James Smart92d7f7b2007-06-17 19:56:38 -05003195 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003196 return;
James Smart47a86172007-04-25 09:53:22 -04003197}
3198
James Smarte59058c2008-08-24 21:49:00 -04003199/**
James Smart3621a712009-04-06 18:47:14 -04003200 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003201 *
3202 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3203 * uses the kernel idr facility to perform the task.
3204 *
3205 * Return codes:
3206 * instance - a unique integer ID allocated as the new instance.
3207 * -1 - lpfc get instance failed.
3208 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003209int
3210lpfc_get_instance(void)
3211{
Tejun Heoab516032013-02-27 17:04:44 -08003212 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003213
Tejun Heoab516032013-02-27 17:04:44 -08003214 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3215 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003216}
3217
James Smarte59058c2008-08-24 21:49:00 -04003218/**
James Smart3621a712009-04-06 18:47:14 -04003219 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003220 * @shost: pointer to SCSI host data structure.
3221 * @time: elapsed time of the scan in jiffies.
3222 *
3223 * This routine is called by the SCSI layer with a SCSI host to determine
3224 * whether the scan host is finished.
3225 *
3226 * Note: there is no scan_start function as adapter initialization will have
3227 * asynchronously kicked off the link initialization.
3228 *
3229 * Return codes
3230 * 0 - SCSI host scan is not over yet.
3231 * 1 - SCSI host scan is over.
3232 **/
James Smart47a86172007-04-25 09:53:22 -04003233int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3234{
James Smart2e0fef82007-06-17 19:56:36 -05003235 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3236 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003237 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003238
James Smart858c9f62007-06-17 19:56:39 -05003239 spin_lock_irq(shost->host_lock);
3240
James Smart51ef4c22007-08-02 11:10:31 -04003241 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003242 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003243 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003244 }
James Smart256ec0d2013-04-17 20:14:58 -04003245 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003246 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003247 "0461 Scanning longer than 30 "
3248 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003249 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003250 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003251 }
James Smart256ec0d2013-04-17 20:14:58 -04003252 if (time >= msecs_to_jiffies(15 * 1000) &&
3253 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003254 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003255 "0465 Link down longer than 15 "
3256 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003257 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003258 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003259 }
3260
James Smart2e0fef82007-06-17 19:56:36 -05003261 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003262 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003263 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003264 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003265 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003266 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003267 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003268 goto finished;
3269
3270 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003271
3272finished:
James Smart858c9f62007-06-17 19:56:39 -05003273 spin_unlock_irq(shost->host_lock);
3274 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003275}
3276
James Smarte59058c2008-08-24 21:49:00 -04003277/**
James Smart3621a712009-04-06 18:47:14 -04003278 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003279 * @shost: pointer to SCSI host data structure.
3280 *
3281 * This routine initializes a given SCSI host attributes on a FC port. The
3282 * SCSI host can be either on top of a physical port or a virtual port.
3283 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003284void lpfc_host_attrib_init(struct Scsi_Host *shost)
3285{
3286 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3287 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003288 /*
James Smart2e0fef82007-06-17 19:56:36 -05003289 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003290 */
3291
James Smart2e0fef82007-06-17 19:56:36 -05003292 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3293 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003294 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3295
3296 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003297 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003298 fc_host_supported_fc4s(shost)[2] = 1;
3299 fc_host_supported_fc4s(shost)[7] = 1;
3300
James Smart92d7f7b2007-06-17 19:56:38 -05003301 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3302 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003303
3304 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003305 if (phba->lmt & LMT_16Gb)
3306 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003307 if (phba->lmt & LMT_10Gb)
3308 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003309 if (phba->lmt & LMT_8Gb)
3310 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003311 if (phba->lmt & LMT_4Gb)
3312 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3313 if (phba->lmt & LMT_2Gb)
3314 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3315 if (phba->lmt & LMT_1Gb)
3316 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3317
3318 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003319 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3320 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003321
Mike Christie0af5d702010-09-15 16:52:31 -05003322 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3323
James Smart47a86172007-04-25 09:53:22 -04003324 /* This value is also unchanging */
3325 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003326 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003327 fc_host_active_fc4s(shost)[2] = 1;
3328 fc_host_active_fc4s(shost)[7] = 1;
3329
James Smart92d7f7b2007-06-17 19:56:38 -05003330 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003331 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003332 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003333 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003334}
dea31012005-04-17 16:05:31 -05003335
James Smarte59058c2008-08-24 21:49:00 -04003336/**
James Smartda0436e2009-05-22 14:51:39 -04003337 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003338 * @phba: pointer to lpfc hba data structure.
3339 *
James Smartda0436e2009-05-22 14:51:39 -04003340 * This routine is invoked to stop an SLI3 device port, it stops the device
3341 * from generating interrupts and stops the device driver's timers for the
3342 * device.
James Smarte59058c2008-08-24 21:49:00 -04003343 **/
James Smartdb2378e2008-02-08 18:49:51 -05003344static void
James Smartda0436e2009-05-22 14:51:39 -04003345lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003346{
James Smartda0436e2009-05-22 14:51:39 -04003347 /* Clear all interrupt enable conditions */
3348 writel(0, phba->HCregaddr);
3349 readl(phba->HCregaddr); /* flush */
3350 /* Clear all pending interrupts */
3351 writel(0xffffffff, phba->HAregaddr);
3352 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003353
James Smartda0436e2009-05-22 14:51:39 -04003354 /* Reset some HBA SLI setup states */
3355 lpfc_stop_hba_timers(phba);
3356 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003357}
3358
James Smarte59058c2008-08-24 21:49:00 -04003359/**
James Smartda0436e2009-05-22 14:51:39 -04003360 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003361 * @phba: pointer to lpfc hba data structure.
3362 *
James Smartda0436e2009-05-22 14:51:39 -04003363 * This routine is invoked to stop an SLI4 device port, it stops the device
3364 * from generating interrupts and stops the device driver's timers for the
3365 * device.
3366 **/
3367static void
3368lpfc_stop_port_s4(struct lpfc_hba *phba)
3369{
3370 /* Reset some HBA SLI4 setup states */
3371 lpfc_stop_hba_timers(phba);
3372 phba->pport->work_port_events = 0;
3373 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003374}
3375
3376/**
3377 * lpfc_stop_port - Wrapper function for stopping hba port
3378 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003379 *
James Smartda0436e2009-05-22 14:51:39 -04003380 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3381 * the API jump table function pointer from the lpfc_hba struct.
3382 **/
3383void
3384lpfc_stop_port(struct lpfc_hba *phba)
3385{
3386 phba->lpfc_stop_port(phba);
3387}
3388
3389/**
James Smartecfd03c2010-02-12 14:41:27 -05003390 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3391 * @phba: Pointer to hba for which this call is being executed.
3392 *
3393 * This routine starts the timer waiting for the FCF rediscovery to complete.
3394 **/
3395void
3396lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3397{
3398 unsigned long fcf_redisc_wait_tmo =
3399 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3400 /* Start fcf rediscovery wait period timer */
3401 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3402 spin_lock_irq(&phba->hbalock);
3403 /* Allow action to new fcf asynchronous event */
3404 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3405 /* Mark the FCF rediscovery pending state */
3406 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3407 spin_unlock_irq(&phba->hbalock);
3408}
3409
3410/**
3411 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3412 * @ptr: Map to lpfc_hba data structure pointer.
3413 *
3414 * This routine is invoked when waiting for FCF table rediscover has been
3415 * timed out. If new FCF record(s) has (have) been discovered during the
3416 * wait period, a new FCF event shall be added to the FCOE async event
3417 * list, and then worker thread shall be waked up for processing from the
3418 * worker thread context.
3419 **/
3420void
3421lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3422{
3423 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3424
3425 /* Don't send FCF rediscovery event if timer cancelled */
3426 spin_lock_irq(&phba->hbalock);
3427 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3428 spin_unlock_irq(&phba->hbalock);
3429 return;
3430 }
3431 /* Clear FCF rediscovery timer pending flag */
3432 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3433 /* FCF rediscovery event to worker thread */
3434 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3435 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003436 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003437 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003438 /* wake up worker thread */
3439 lpfc_worker_wake_up(phba);
3440}
3441
3442/**
James Smartda0436e2009-05-22 14:51:39 -04003443 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3444 * @phba: pointer to lpfc hba data structure.
3445 * @acqe_link: pointer to the async link completion queue entry.
3446 *
3447 * This routine is to parse the SLI4 link-attention link fault code and
3448 * translate it into the base driver's read link attention mailbox command
3449 * status.
3450 *
3451 * Return: Link-attention status in terms of base driver's coding.
3452 **/
3453static uint16_t
3454lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3455 struct lpfc_acqe_link *acqe_link)
3456{
3457 uint16_t latt_fault;
3458
3459 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3460 case LPFC_ASYNC_LINK_FAULT_NONE:
3461 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3462 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3463 latt_fault = 0;
3464 break;
3465 default:
3466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3467 "0398 Invalid link fault code: x%x\n",
3468 bf_get(lpfc_acqe_link_fault, acqe_link));
3469 latt_fault = MBXERR_ERROR;
3470 break;
3471 }
3472 return latt_fault;
3473}
3474
3475/**
3476 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3477 * @phba: pointer to lpfc hba data structure.
3478 * @acqe_link: pointer to the async link completion queue entry.
3479 *
3480 * This routine is to parse the SLI4 link attention type and translate it
3481 * into the base driver's link attention type coding.
3482 *
3483 * Return: Link attention type in terms of base driver's coding.
3484 **/
3485static uint8_t
3486lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3487 struct lpfc_acqe_link *acqe_link)
3488{
3489 uint8_t att_type;
3490
3491 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3492 case LPFC_ASYNC_LINK_STATUS_DOWN:
3493 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003494 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003495 break;
3496 case LPFC_ASYNC_LINK_STATUS_UP:
3497 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003498 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003499 break;
3500 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003501 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003502 break;
3503 default:
3504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3505 "0399 Invalid link attention type: x%x\n",
3506 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003507 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003508 break;
3509 }
3510 return att_type;
3511}
3512
3513/**
3514 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3515 * @phba: pointer to lpfc hba data structure.
3516 * @acqe_link: pointer to the async link completion queue entry.
3517 *
3518 * This routine is to parse the SLI4 link-attention link speed and translate
3519 * it into the base driver's link-attention link speed coding.
3520 *
3521 * Return: Link-attention link speed in terms of base driver's coding.
3522 **/
3523static uint8_t
3524lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3525 struct lpfc_acqe_link *acqe_link)
3526{
3527 uint8_t link_speed;
3528
3529 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3530 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003531 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003532 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003533 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003534 break;
3535 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003536 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003537 break;
3538 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003539 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003540 break;
3541 default:
3542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3543 "0483 Invalid link-attention link speed: x%x\n",
3544 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003545 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003546 break;
3547 }
3548 return link_speed;
3549}
3550
3551/**
James Smart8b68cd52012-09-29 11:32:37 -04003552 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3553 * @phba: pointer to lpfc hba data structure.
3554 *
3555 * This routine is to get an SLI3 FC port's link speed in Mbps.
3556 *
3557 * Return: link speed in terms of Mbps.
3558 **/
3559uint32_t
3560lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3561{
3562 uint32_t link_speed;
3563
3564 if (!lpfc_is_link_up(phba))
3565 return 0;
3566
3567 switch (phba->fc_linkspeed) {
3568 case LPFC_LINK_SPEED_1GHZ:
3569 link_speed = 1000;
3570 break;
3571 case LPFC_LINK_SPEED_2GHZ:
3572 link_speed = 2000;
3573 break;
3574 case LPFC_LINK_SPEED_4GHZ:
3575 link_speed = 4000;
3576 break;
3577 case LPFC_LINK_SPEED_8GHZ:
3578 link_speed = 8000;
3579 break;
3580 case LPFC_LINK_SPEED_10GHZ:
3581 link_speed = 10000;
3582 break;
3583 case LPFC_LINK_SPEED_16GHZ:
3584 link_speed = 16000;
3585 break;
3586 default:
3587 link_speed = 0;
3588 }
3589 return link_speed;
3590}
3591
3592/**
3593 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3594 * @phba: pointer to lpfc hba data structure.
3595 * @evt_code: asynchronous event code.
3596 * @speed_code: asynchronous event link speed code.
3597 *
3598 * This routine is to parse the giving SLI4 async event link speed code into
3599 * value of Mbps for the link speed.
3600 *
3601 * Return: link speed in terms of Mbps.
3602 **/
3603static uint32_t
3604lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3605 uint8_t speed_code)
3606{
3607 uint32_t port_speed;
3608
3609 switch (evt_code) {
3610 case LPFC_TRAILER_CODE_LINK:
3611 switch (speed_code) {
3612 case LPFC_EVT_CODE_LINK_NO_LINK:
3613 port_speed = 0;
3614 break;
3615 case LPFC_EVT_CODE_LINK_10_MBIT:
3616 port_speed = 10;
3617 break;
3618 case LPFC_EVT_CODE_LINK_100_MBIT:
3619 port_speed = 100;
3620 break;
3621 case LPFC_EVT_CODE_LINK_1_GBIT:
3622 port_speed = 1000;
3623 break;
3624 case LPFC_EVT_CODE_LINK_10_GBIT:
3625 port_speed = 10000;
3626 break;
3627 default:
3628 port_speed = 0;
3629 }
3630 break;
3631 case LPFC_TRAILER_CODE_FC:
3632 switch (speed_code) {
3633 case LPFC_EVT_CODE_FC_NO_LINK:
3634 port_speed = 0;
3635 break;
3636 case LPFC_EVT_CODE_FC_1_GBAUD:
3637 port_speed = 1000;
3638 break;
3639 case LPFC_EVT_CODE_FC_2_GBAUD:
3640 port_speed = 2000;
3641 break;
3642 case LPFC_EVT_CODE_FC_4_GBAUD:
3643 port_speed = 4000;
3644 break;
3645 case LPFC_EVT_CODE_FC_8_GBAUD:
3646 port_speed = 8000;
3647 break;
3648 case LPFC_EVT_CODE_FC_10_GBAUD:
3649 port_speed = 10000;
3650 break;
3651 case LPFC_EVT_CODE_FC_16_GBAUD:
3652 port_speed = 16000;
3653 break;
3654 default:
3655 port_speed = 0;
3656 }
3657 break;
3658 default:
3659 port_speed = 0;
3660 }
3661 return port_speed;
3662}
3663
3664/**
James Smart70f3c072010-12-15 17:57:33 -05003665 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003666 * @phba: pointer to lpfc hba data structure.
3667 * @acqe_link: pointer to the async link completion queue entry.
3668 *
James Smart70f3c072010-12-15 17:57:33 -05003669 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003670 **/
3671static void
3672lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3673 struct lpfc_acqe_link *acqe_link)
3674{
3675 struct lpfc_dmabuf *mp;
3676 LPFC_MBOXQ_t *pmb;
3677 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003678 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003679 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003680 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003681
3682 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003683 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003684 return;
James Smart32b97932009-07-19 10:01:21 -04003685 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003686 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3687 if (!pmb) {
3688 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3689 "0395 The mboxq allocation failed\n");
3690 return;
3691 }
3692 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3693 if (!mp) {
3694 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3695 "0396 The lpfc_dmabuf allocation failed\n");
3696 goto out_free_pmb;
3697 }
3698 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3699 if (!mp->virt) {
3700 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3701 "0397 The mbuf allocation failed\n");
3702 goto out_free_dmabuf;
3703 }
3704
3705 /* Cleanup any outstanding ELS commands */
3706 lpfc_els_flush_all_cmd(phba);
3707
3708 /* Block ELS IOCBs until we have done process link event */
3709 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3710
3711 /* Update link event statistics */
3712 phba->sli.slistat.link_event++;
3713
James Smart76a95d72010-11-20 23:11:48 -05003714 /* Create lpfc_handle_latt mailbox command from link ACQE */
3715 lpfc_read_topology(phba, pmb, mp);
3716 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003717 pmb->vport = phba->pport;
3718
James Smartda0436e2009-05-22 14:51:39 -04003719 /* Keep the link status for extra SLI4 state machine reference */
3720 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003721 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3722 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003723 phba->sli4_hba.link_state.duplex =
3724 bf_get(lpfc_acqe_link_duplex, acqe_link);
3725 phba->sli4_hba.link_state.status =
3726 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003727 phba->sli4_hba.link_state.type =
3728 bf_get(lpfc_acqe_link_type, acqe_link);
3729 phba->sli4_hba.link_state.number =
3730 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003731 phba->sli4_hba.link_state.fault =
3732 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003733 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003734 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3735
James Smart70f3c072010-12-15 17:57:33 -05003736 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003737 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3738 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3739 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003740 phba->sli4_hba.link_state.speed,
3741 phba->sli4_hba.link_state.topology,
3742 phba->sli4_hba.link_state.status,
3743 phba->sli4_hba.link_state.type,
3744 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003745 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003746 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003747 /*
3748 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3749 * topology info. Note: Optional for non FC-AL ports.
3750 */
3751 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3752 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3753 if (rc == MBX_NOT_FINISHED)
3754 goto out_free_dmabuf;
3755 return;
3756 }
3757 /*
3758 * For FCoE Mode: fill in all the topology information we need and call
3759 * the READ_TOPOLOGY completion routine to continue without actually
3760 * sending the READ_TOPOLOGY mailbox command to the port.
3761 */
3762 /* Parse and translate status field */
3763 mb = &pmb->u.mb;
3764 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3765
3766 /* Parse and translate link attention fields */
3767 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3768 la->eventTag = acqe_link->event_tag;
3769 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3770 bf_set(lpfc_mbx_read_top_link_spd, la,
3771 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3772
3773 /* Fake the the following irrelvant fields */
3774 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3775 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3776 bf_set(lpfc_mbx_read_top_il, la, 0);
3777 bf_set(lpfc_mbx_read_top_pb, la, 0);
3778 bf_set(lpfc_mbx_read_top_fa, la, 0);
3779 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003780
3781 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003782 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003783
3784 return;
3785
3786out_free_dmabuf:
3787 kfree(mp);
3788out_free_pmb:
3789 mempool_free(pmb, phba->mbox_mem_pool);
3790}
3791
3792/**
James Smart70f3c072010-12-15 17:57:33 -05003793 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3794 * @phba: pointer to lpfc hba data structure.
3795 * @acqe_fc: pointer to the async fc completion queue entry.
3796 *
3797 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3798 * that the event was received and then issue a read_topology mailbox command so
3799 * that the rest of the driver will treat it the same as SLI3.
3800 **/
3801static void
3802lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3803{
3804 struct lpfc_dmabuf *mp;
3805 LPFC_MBOXQ_t *pmb;
3806 int rc;
3807
3808 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3809 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3810 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3811 "2895 Non FC link Event detected.(%d)\n",
3812 bf_get(lpfc_trailer_type, acqe_fc));
3813 return;
3814 }
3815 /* Keep the link status for extra SLI4 state machine reference */
3816 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003817 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3818 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003819 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3820 phba->sli4_hba.link_state.topology =
3821 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3822 phba->sli4_hba.link_state.status =
3823 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3824 phba->sli4_hba.link_state.type =
3825 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3826 phba->sli4_hba.link_state.number =
3827 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3828 phba->sli4_hba.link_state.fault =
3829 bf_get(lpfc_acqe_link_fault, acqe_fc);
3830 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003831 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003832 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3833 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3834 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3835 "%dMbps Fault:%d\n",
3836 phba->sli4_hba.link_state.speed,
3837 phba->sli4_hba.link_state.topology,
3838 phba->sli4_hba.link_state.status,
3839 phba->sli4_hba.link_state.type,
3840 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003841 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003842 phba->sli4_hba.link_state.fault);
3843 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3844 if (!pmb) {
3845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3846 "2897 The mboxq allocation failed\n");
3847 return;
3848 }
3849 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3850 if (!mp) {
3851 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3852 "2898 The lpfc_dmabuf allocation failed\n");
3853 goto out_free_pmb;
3854 }
3855 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3856 if (!mp->virt) {
3857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3858 "2899 The mbuf allocation failed\n");
3859 goto out_free_dmabuf;
3860 }
3861
3862 /* Cleanup any outstanding ELS commands */
3863 lpfc_els_flush_all_cmd(phba);
3864
3865 /* Block ELS IOCBs until we have done process link event */
3866 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3867
3868 /* Update link event statistics */
3869 phba->sli.slistat.link_event++;
3870
3871 /* Create lpfc_handle_latt mailbox command from link ACQE */
3872 lpfc_read_topology(phba, pmb, mp);
3873 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3874 pmb->vport = phba->pport;
3875
3876 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3877 if (rc == MBX_NOT_FINISHED)
3878 goto out_free_dmabuf;
3879 return;
3880
3881out_free_dmabuf:
3882 kfree(mp);
3883out_free_pmb:
3884 mempool_free(pmb, phba->mbox_mem_pool);
3885}
3886
3887/**
3888 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3889 * @phba: pointer to lpfc hba data structure.
3890 * @acqe_fc: pointer to the async SLI completion queue entry.
3891 *
3892 * This routine is to handle the SLI4 asynchronous SLI events.
3893 **/
3894static void
3895lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3896{
James Smart4b8bae02012-06-12 13:55:07 -04003897 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04003898 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04003899 uint8_t status;
3900 struct lpfc_acqe_misconfigured_event *misconfigured;
3901
3902 /* special case misconfigured event as it contains data for all ports */
3903 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3904 LPFC_SLI_INTF_IF_TYPE_2) ||
3905 (bf_get(lpfc_trailer_type, acqe_sli) !=
3906 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3907 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3908 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3909 "x%08x SLI Event Type:%d\n",
3910 acqe_sli->event_data1, acqe_sli->event_data2,
3911 bf_get(lpfc_trailer_type, acqe_sli));
3912 return;
3913 }
3914
3915 port_name = phba->Port[0];
3916 if (port_name == 0x00)
3917 port_name = '?'; /* get port name is empty */
3918
3919 misconfigured = (struct lpfc_acqe_misconfigured_event *)
3920 &acqe_sli->event_data1;
3921
3922 /* fetch the status for this port */
3923 switch (phba->sli4_hba.lnk_info.lnk_no) {
3924 case LPFC_LINK_NUMBER_0:
3925 status = bf_get(lpfc_sli_misconfigured_port0,
3926 &misconfigured->theEvent);
3927 break;
3928 case LPFC_LINK_NUMBER_1:
3929 status = bf_get(lpfc_sli_misconfigured_port1,
3930 &misconfigured->theEvent);
3931 break;
3932 case LPFC_LINK_NUMBER_2:
3933 status = bf_get(lpfc_sli_misconfigured_port2,
3934 &misconfigured->theEvent);
3935 break;
3936 case LPFC_LINK_NUMBER_3:
3937 status = bf_get(lpfc_sli_misconfigured_port3,
3938 &misconfigured->theEvent);
3939 break;
3940 default:
3941 status = ~LPFC_SLI_EVENT_STATUS_VALID;
3942 break;
3943 }
3944
3945 switch (status) {
3946 case LPFC_SLI_EVENT_STATUS_VALID:
3947 return; /* no message if the sfp is okay */
3948 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04003949 sprintf(message, "Optics faulted/incorrectly installed/not " \
3950 "installed - Reseat optics, if issue not "
3951 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04003952 break;
3953 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
3954 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04003955 "Optics of two types installed - Remove one optic or " \
3956 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04003957 break;
3958 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04003959 sprintf(message, "Incompatible optics - Replace with " \
3960 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04003961 break;
3962 default:
3963 /* firmware is reporting a status we don't know about */
3964 sprintf(message, "Unknown event status x%02x", status);
3965 break;
3966 }
3967
3968 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3969 "3176 Misconfigured Physical Port - "
3970 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05003971}
3972
3973/**
James Smartfc2b9892010-02-26 14:15:29 -05003974 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3975 * @vport: pointer to vport data structure.
3976 *
3977 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3978 * response to a CVL event.
3979 *
3980 * Return the pointer to the ndlp with the vport if successful, otherwise
3981 * return NULL.
3982 **/
3983static struct lpfc_nodelist *
3984lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3985{
3986 struct lpfc_nodelist *ndlp;
3987 struct Scsi_Host *shost;
3988 struct lpfc_hba *phba;
3989
3990 if (!vport)
3991 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003992 phba = vport->phba;
3993 if (!phba)
3994 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003995 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3996 if (!ndlp) {
3997 /* Cannot find existing Fabric ndlp, so allocate a new one */
3998 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3999 if (!ndlp)
4000 return 0;
4001 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4002 /* Set the node type */
4003 ndlp->nlp_type |= NLP_FABRIC;
4004 /* Put ndlp onto node list */
4005 lpfc_enqueue_node(vport, ndlp);
4006 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4007 /* re-setup ndlp without removing from node list */
4008 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4009 if (!ndlp)
4010 return 0;
4011 }
James Smart63e801c2010-11-20 23:14:19 -05004012 if ((phba->pport->port_state < LPFC_FLOGI) &&
4013 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004014 return NULL;
4015 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004016 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4017 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004018 return NULL;
4019 shost = lpfc_shost_from_vport(vport);
4020 if (!shost)
4021 return NULL;
4022 lpfc_linkdown_port(vport);
4023 lpfc_cleanup_pending_mbox(vport);
4024 spin_lock_irq(shost->host_lock);
4025 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4026 spin_unlock_irq(shost->host_lock);
4027
4028 return ndlp;
4029}
4030
4031/**
4032 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4033 * @vport: pointer to lpfc hba data structure.
4034 *
4035 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4036 * response to a FCF dead event.
4037 **/
4038static void
4039lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4040{
4041 struct lpfc_vport **vports;
4042 int i;
4043
4044 vports = lpfc_create_vport_work_array(phba);
4045 if (vports)
4046 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4047 lpfc_sli4_perform_vport_cvl(vports[i]);
4048 lpfc_destroy_vport_work_array(phba, vports);
4049}
4050
4051/**
James Smart25aee402013-03-01 16:36:34 -05004052 * lpfc_sli4_perform_inuse_fcf_recovery - Perform inuse fcf recovery
4053 * @vport: pointer to lpfc hba data structure.
4054 *
4055 * This routine is to perform FCF recovery when the in-use FCF either dead or
4056 * got modified.
4057 **/
4058static void
4059lpfc_sli4_perform_inuse_fcf_recovery(struct lpfc_hba *phba,
4060 struct lpfc_acqe_fip *acqe_fip)
4061{
4062 int rc;
4063
4064 spin_lock_irq(&phba->hbalock);
4065 /* Mark the fast failover process in progress */
4066 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4067 spin_unlock_irq(&phba->hbalock);
4068
4069 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4070 "2771 Start FCF fast failover process due to in-use "
4071 "FCF DEAD/MODIFIED event: evt_tag:x%x, index:x%x\n",
4072 acqe_fip->event_tag, acqe_fip->index);
4073 rc = lpfc_sli4_redisc_fcf_table(phba);
4074 if (rc) {
4075 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4076 "2772 Issue FCF rediscover mabilbox command "
4077 "failed, fail through to FCF dead event\n");
4078 spin_lock_irq(&phba->hbalock);
4079 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4080 spin_unlock_irq(&phba->hbalock);
4081 /*
4082 * Last resort will fail over by treating this as a link
4083 * down to FCF registration.
4084 */
4085 lpfc_sli4_fcf_dead_failthrough(phba);
4086 } else {
4087 /* Reset FCF roundrobin bmask for new discovery */
4088 lpfc_sli4_clear_fcf_rr_bmask(phba);
4089 /*
4090 * Handling fast FCF failover to a DEAD FCF event is
4091 * considered equalivant to receiving CVL to all vports.
4092 */
4093 lpfc_sli4_perform_all_vport_cvl(phba);
4094 }
4095}
4096
4097/**
James Smart76a95d72010-11-20 23:11:48 -05004098 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004099 * @phba: pointer to lpfc hba data structure.
4100 * @acqe_link: pointer to the async fcoe completion queue entry.
4101 *
4102 * This routine is to handle the SLI4 asynchronous fcoe event.
4103 **/
4104static void
James Smart76a95d72010-11-20 23:11:48 -05004105lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004106 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004107{
James Smart70f3c072010-12-15 17:57:33 -05004108 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004109 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004110 struct lpfc_vport *vport;
4111 struct lpfc_nodelist *ndlp;
4112 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004113 int active_vlink_present;
4114 struct lpfc_vport **vports;
4115 int i;
James Smartda0436e2009-05-22 14:51:39 -04004116
James Smart70f3c072010-12-15 17:57:33 -05004117 phba->fc_eventTag = acqe_fip->event_tag;
4118 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004119 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004120 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4121 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4122 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004123 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4124 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004125 "2546 New FCF event, evt_tag:x%x, "
4126 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004127 acqe_fip->event_tag,
4128 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004129 else
4130 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4131 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004132 "2788 FCF param modified event, "
4133 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004134 acqe_fip->event_tag,
4135 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004136 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004137 /*
4138 * During period of FCF discovery, read the FCF
4139 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004140 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004141 */
4142 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4143 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004144 "2779 Read FCF (x%x) for updating "
4145 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004146 acqe_fip->index);
4147 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004148 }
James Smart38b92ef2010-08-04 16:11:39 -04004149
4150 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004151 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004152 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004153 spin_unlock_irq(&phba->hbalock);
4154 break;
4155 }
4156 /* If fast FCF failover rescan event is pending, do nothing */
4157 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4158 spin_unlock_irq(&phba->hbalock);
4159 break;
4160 }
4161
James Smart25aee402013-03-01 16:36:34 -05004162 /* If FCF has been in discovered state, perform rediscovery
4163 * only if the FCF with the same index of the in-use FCF got
4164 * modified during normal operation. Otherwise, do nothing.
4165 */
4166 if (phba->pport->port_state > LPFC_FLOGI) {
James Smart3804dc82010-07-14 15:31:37 -04004167 spin_unlock_irq(&phba->hbalock);
James Smart25aee402013-03-01 16:36:34 -05004168 if (phba->fcf.current_rec.fcf_indx ==
4169 acqe_fip->index) {
4170 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
4171 "3300 In-use FCF (%d) "
4172 "modified, perform FCF "
4173 "rediscovery\n",
4174 acqe_fip->index);
4175 lpfc_sli4_perform_inuse_fcf_recovery(phba,
4176 acqe_fip);
4177 }
James Smart3804dc82010-07-14 15:31:37 -04004178 break;
4179 }
4180 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004181
James Smart0c9ab6f2010-02-26 14:15:57 -05004182 /* Otherwise, scan the entire FCF table and re-discover SAN */
4183 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004184 "2770 Start FCF table scan per async FCF "
4185 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004186 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004187 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4188 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004189 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004190 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4191 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004192 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004193 break;
4194
James Smart70f3c072010-12-15 17:57:33 -05004195 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004196 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004197 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004198 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4199 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004200 break;
4201
James Smart70f3c072010-12-15 17:57:33 -05004202 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004203 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004204 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004205 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004206 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004207 /*
4208 * If we are in the middle of FCF failover process, clear
4209 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004210 */
James Smartfc2b9892010-02-26 14:15:29 -05004211 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004212 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004213 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004214 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004215 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004216 break;
4217 }
James Smart38b92ef2010-08-04 16:11:39 -04004218 spin_unlock_irq(&phba->hbalock);
4219
4220 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004221 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004222 break;
4223
4224 /*
4225 * Otherwise, request the port to rediscover the entire FCF
4226 * table for a fast recovery from case that the current FCF
4227 * is no longer valid as we are not in the middle of FCF
4228 * failover process already.
4229 */
James Smart25aee402013-03-01 16:36:34 -05004230 lpfc_sli4_perform_inuse_fcf_recovery(phba, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004231 break;
James Smart70f3c072010-12-15 17:57:33 -05004232 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004233 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004234 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004235 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004236 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004237
James Smart6669f9b2009-10-02 15:16:45 -04004238 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004239 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004240 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004241 if (!ndlp)
4242 break;
James Smart695a8142010-01-26 23:08:03 -05004243 active_vlink_present = 0;
4244
4245 vports = lpfc_create_vport_work_array(phba);
4246 if (vports) {
4247 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4248 i++) {
4249 if ((!(vports[i]->fc_flag &
4250 FC_VPORT_CVL_RCVD)) &&
4251 (vports[i]->port_state > LPFC_FDISC)) {
4252 active_vlink_present = 1;
4253 break;
4254 }
4255 }
4256 lpfc_destroy_vport_work_array(phba, vports);
4257 }
4258
4259 if (active_vlink_present) {
4260 /*
4261 * If there are other active VLinks present,
4262 * re-instantiate the Vlink using FDISC.
4263 */
James Smart256ec0d2013-04-17 20:14:58 -04004264 mod_timer(&ndlp->nlp_delayfunc,
4265 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004266 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004267 spin_lock_irq(shost->host_lock);
4268 ndlp->nlp_flag |= NLP_DELAY_TMO;
4269 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004270 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4271 vport->port_state = LPFC_FDISC;
4272 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004273 /*
4274 * Otherwise, we request port to rediscover
4275 * the entire FCF table for a fast recovery
4276 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004277 * is no longer valid if we are not already
4278 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004279 */
James Smartfc2b9892010-02-26 14:15:29 -05004280 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004281 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004282 spin_unlock_irq(&phba->hbalock);
4283 break;
4284 }
4285 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004286 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004287 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004288 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4289 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004290 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004291 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004292 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004293 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004294 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4295 LOG_DISCOVERY,
4296 "2774 Issue FCF rediscover "
4297 "mabilbox command failed, "
4298 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004299 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004300 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004301 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004302 /*
4303 * Last resort will be re-try on the
4304 * the current registered FCF entry.
4305 */
4306 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004307 } else
4308 /*
4309 * Reset FCF roundrobin bmask for new
4310 * discovery.
4311 */
James Smart7d791df2011-07-22 18:37:52 -04004312 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004313 }
4314 break;
James Smartda0436e2009-05-22 14:51:39 -04004315 default:
4316 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4317 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004318 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004319 break;
4320 }
4321}
4322
4323/**
4324 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4325 * @phba: pointer to lpfc hba data structure.
4326 * @acqe_link: pointer to the async dcbx completion queue entry.
4327 *
4328 * This routine is to handle the SLI4 asynchronous dcbx event.
4329 **/
4330static void
4331lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4332 struct lpfc_acqe_dcbx *acqe_dcbx)
4333{
James Smart4d9ab992009-10-02 15:16:39 -04004334 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004335 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4336 "0290 The SLI4 DCBX asynchronous event is not "
4337 "handled yet\n");
4338}
4339
4340/**
James Smartb19a0612010-04-06 14:48:51 -04004341 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4342 * @phba: pointer to lpfc hba data structure.
4343 * @acqe_link: pointer to the async grp5 completion queue entry.
4344 *
4345 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4346 * is an asynchronous notified of a logical link speed change. The Port
4347 * reports the logical link speed in units of 10Mbps.
4348 **/
4349static void
4350lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4351 struct lpfc_acqe_grp5 *acqe_grp5)
4352{
4353 uint16_t prev_ll_spd;
4354
4355 phba->fc_eventTag = acqe_grp5->event_tag;
4356 phba->fcoe_eventtag = acqe_grp5->event_tag;
4357 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4358 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004359 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004360 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4361 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004362 "from %dMbps to %dMbps\n", prev_ll_spd,
4363 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004364}
4365
4366/**
James Smartda0436e2009-05-22 14:51:39 -04004367 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4368 * @phba: pointer to lpfc hba data structure.
4369 *
4370 * This routine is invoked by the worker thread to process all the pending
4371 * SLI4 asynchronous events.
4372 **/
4373void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4374{
4375 struct lpfc_cq_event *cq_event;
4376
4377 /* First, declare the async event has been handled */
4378 spin_lock_irq(&phba->hbalock);
4379 phba->hba_flag &= ~ASYNC_EVENT;
4380 spin_unlock_irq(&phba->hbalock);
4381 /* Now, handle all the async events */
4382 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4383 /* Get the first event from the head of the event queue */
4384 spin_lock_irq(&phba->hbalock);
4385 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4386 cq_event, struct lpfc_cq_event, list);
4387 spin_unlock_irq(&phba->hbalock);
4388 /* Process the asynchronous event */
4389 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4390 case LPFC_TRAILER_CODE_LINK:
4391 lpfc_sli4_async_link_evt(phba,
4392 &cq_event->cqe.acqe_link);
4393 break;
4394 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004395 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004396 break;
4397 case LPFC_TRAILER_CODE_DCBX:
4398 lpfc_sli4_async_dcbx_evt(phba,
4399 &cq_event->cqe.acqe_dcbx);
4400 break;
James Smartb19a0612010-04-06 14:48:51 -04004401 case LPFC_TRAILER_CODE_GRP5:
4402 lpfc_sli4_async_grp5_evt(phba,
4403 &cq_event->cqe.acqe_grp5);
4404 break;
James Smart70f3c072010-12-15 17:57:33 -05004405 case LPFC_TRAILER_CODE_FC:
4406 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4407 break;
4408 case LPFC_TRAILER_CODE_SLI:
4409 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4410 break;
James Smartda0436e2009-05-22 14:51:39 -04004411 default:
4412 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4413 "1804 Invalid asynchrous event code: "
4414 "x%x\n", bf_get(lpfc_trailer_code,
4415 &cq_event->cqe.mcqe_cmpl));
4416 break;
4417 }
4418 /* Free the completion event processed to the free pool */
4419 lpfc_sli4_cq_event_release(phba, cq_event);
4420 }
4421}
4422
4423/**
James Smartecfd03c2010-02-12 14:41:27 -05004424 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4425 * @phba: pointer to lpfc hba data structure.
4426 *
4427 * This routine is invoked by the worker thread to process FCF table
4428 * rediscovery pending completion event.
4429 **/
4430void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4431{
4432 int rc;
4433
4434 spin_lock_irq(&phba->hbalock);
4435 /* Clear FCF rediscovery timeout event */
4436 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4437 /* Clear driver fast failover FCF record flag */
4438 phba->fcf.failover_rec.flag = 0;
4439 /* Set state for FCF fast failover */
4440 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4441 spin_unlock_irq(&phba->hbalock);
4442
4443 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004444 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004445 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004446 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004447 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004448 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4449 "2747 Issue FCF scan read FCF mailbox "
4450 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004451}
4452
4453/**
James Smartda0436e2009-05-22 14:51:39 -04004454 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4455 * @phba: pointer to lpfc hba data structure.
4456 * @dev_grp: The HBA PCI-Device group number.
4457 *
4458 * This routine is invoked to set up the per HBA PCI-Device group function
4459 * API jump table entries.
4460 *
4461 * Return: 0 if success, otherwise -ENODEV
4462 **/
4463int
4464lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004465{
4466 int rc;
4467
James Smartda0436e2009-05-22 14:51:39 -04004468 /* Set up lpfc PCI-device group */
4469 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004470
James Smartda0436e2009-05-22 14:51:39 -04004471 /* The LPFC_PCI_DEV_OC uses SLI4 */
4472 if (dev_grp == LPFC_PCI_DEV_OC)
4473 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004474
James Smartda0436e2009-05-22 14:51:39 -04004475 /* Set up device INIT API function jump table */
4476 rc = lpfc_init_api_table_setup(phba, dev_grp);
4477 if (rc)
4478 return -ENODEV;
4479 /* Set up SCSI API function jump table */
4480 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4481 if (rc)
4482 return -ENODEV;
4483 /* Set up SLI API function jump table */
4484 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4485 if (rc)
4486 return -ENODEV;
4487 /* Set up MBOX API function jump table */
4488 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4489 if (rc)
4490 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004491
James Smartda0436e2009-05-22 14:51:39 -04004492 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004493}
4494
4495/**
James Smart3621a712009-04-06 18:47:14 -04004496 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004497 * @phba: pointer to lpfc hba data structure.
4498 * @intr_mode: active interrupt mode adopted.
4499 *
4500 * This routine it invoked to log the currently used active interrupt mode
4501 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004502 **/
4503static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004504{
4505 switch (intr_mode) {
4506 case 0:
4507 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4508 "0470 Enable INTx interrupt mode.\n");
4509 break;
4510 case 1:
4511 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4512 "0481 Enabled MSI interrupt mode.\n");
4513 break;
4514 case 2:
4515 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4516 "0480 Enabled MSI-X interrupt mode.\n");
4517 break;
4518 default:
4519 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4520 "0482 Illegal interrupt mode.\n");
4521 break;
4522 }
4523 return;
4524}
4525
James Smart5b75da22008-12-04 22:39:35 -05004526/**
James Smart3772a992009-05-22 14:50:54 -04004527 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004528 * @phba: pointer to lpfc hba data structure.
4529 *
James Smart3772a992009-05-22 14:50:54 -04004530 * This routine is invoked to enable the PCI device that is common to all
4531 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004532 *
4533 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004534 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004535 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004536 **/
James Smart3772a992009-05-22 14:50:54 -04004537static int
4538lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004539{
James Smart3772a992009-05-22 14:50:54 -04004540 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004541 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004542
James Smart3772a992009-05-22 14:50:54 -04004543 /* Obtain PCI device reference */
4544 if (!phba->pcidev)
4545 goto out_error;
4546 else
4547 pdev = phba->pcidev;
4548 /* Select PCI BARs */
4549 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4550 /* Enable PCI device */
4551 if (pci_enable_device_mem(pdev))
4552 goto out_error;
4553 /* Request PCI resource for the device */
4554 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4555 goto out_disable_device;
4556 /* Set up device as PCI master and save state for EEH */
4557 pci_set_master(pdev);
4558 pci_try_set_mwi(pdev);
4559 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004560
James Smart05580562011-05-24 11:40:48 -04004561 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4562 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4563 pdev->needs_freset = 1;
4564
James Smart3772a992009-05-22 14:50:54 -04004565 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004566
James Smart3772a992009-05-22 14:50:54 -04004567out_disable_device:
4568 pci_disable_device(pdev);
4569out_error:
James Smart079b5c92011-08-21 21:48:49 -04004570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4571 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004572 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004573}
4574
4575/**
James Smart3772a992009-05-22 14:50:54 -04004576 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004577 * @phba: pointer to lpfc hba data structure.
4578 *
James Smart3772a992009-05-22 14:50:54 -04004579 * This routine is invoked to disable the PCI device that is common to all
4580 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004581 **/
4582static void
James Smart3772a992009-05-22 14:50:54 -04004583lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004584{
James Smart3772a992009-05-22 14:50:54 -04004585 struct pci_dev *pdev;
4586 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004587
James Smart3772a992009-05-22 14:50:54 -04004588 /* Obtain PCI device reference */
4589 if (!phba->pcidev)
4590 return;
4591 else
4592 pdev = phba->pcidev;
4593 /* Select PCI BARs */
4594 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4595 /* Release PCI resource and disable PCI device */
4596 pci_release_selected_regions(pdev, bars);
4597 pci_disable_device(pdev);
4598 /* Null out PCI private reference to driver */
4599 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05004600
4601 return;
4602}
4603
4604/**
James Smart3772a992009-05-22 14:50:54 -04004605 * lpfc_reset_hba - Reset a hba
4606 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004607 *
James Smart3772a992009-05-22 14:50:54 -04004608 * This routine is invoked to reset a hba device. It brings the HBA
4609 * offline, performs a board restart, and then brings the board back
4610 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4611 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004612 **/
James Smart3772a992009-05-22 14:50:54 -04004613void
4614lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004615{
James Smart3772a992009-05-22 14:50:54 -04004616 /* If resets are disabled then set error state and return. */
4617 if (!phba->cfg_enable_hba_reset) {
4618 phba->link_state = LPFC_HBA_ERROR;
4619 return;
4620 }
James Smart618a5232012-06-12 13:54:36 -04004621 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004622 lpfc_offline(phba);
4623 lpfc_sli_brdrestart(phba);
4624 lpfc_online(phba);
4625 lpfc_unblock_mgmt_io(phba);
4626}
dea31012005-04-17 16:05:31 -05004627
James Smart3772a992009-05-22 14:50:54 -04004628/**
James Smart0a96e972011-07-22 18:37:28 -04004629 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4630 * @phba: pointer to lpfc hba data structure.
4631 *
4632 * This function enables the PCI SR-IOV virtual functions to a physical
4633 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4634 * enable the number of virtual functions to the physical function. As
4635 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4636 * API call does not considered as an error condition for most of the device.
4637 **/
4638uint16_t
4639lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4640{
4641 struct pci_dev *pdev = phba->pcidev;
4642 uint16_t nr_virtfn;
4643 int pos;
4644
James Smart0a96e972011-07-22 18:37:28 -04004645 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4646 if (pos == 0)
4647 return 0;
4648
4649 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4650 return nr_virtfn;
4651}
4652
4653/**
James Smart912e3ac2011-05-24 11:42:11 -04004654 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4655 * @phba: pointer to lpfc hba data structure.
4656 * @nr_vfn: number of virtual functions to be enabled.
4657 *
4658 * This function enables the PCI SR-IOV virtual functions to a physical
4659 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4660 * enable the number of virtual functions to the physical function. As
4661 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4662 * API call does not considered as an error condition for most of the device.
4663 **/
4664int
4665lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4666{
4667 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004668 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004669 int rc;
4670
James Smart0a96e972011-07-22 18:37:28 -04004671 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4672 if (nr_vfn > max_nr_vfn) {
4673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4674 "3057 Requested vfs (%d) greater than "
4675 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4676 return -EINVAL;
4677 }
4678
James Smart912e3ac2011-05-24 11:42:11 -04004679 rc = pci_enable_sriov(pdev, nr_vfn);
4680 if (rc) {
4681 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4682 "2806 Failed to enable sriov on this device "
4683 "with vfn number nr_vf:%d, rc:%d\n",
4684 nr_vfn, rc);
4685 } else
4686 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4687 "2807 Successful enable sriov on this device "
4688 "with vfn number nr_vf:%d\n", nr_vfn);
4689 return rc;
4690}
4691
4692/**
James Smart3772a992009-05-22 14:50:54 -04004693 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4694 * @phba: pointer to lpfc hba data structure.
4695 *
4696 * This routine is invoked to set up the driver internal resources specific to
4697 * support the SLI-3 HBA device it attached to.
4698 *
4699 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004700 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004701 * other values - error
4702 **/
4703static int
4704lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4705{
4706 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004707 int rc;
dea31012005-04-17 16:05:31 -05004708
James Smart3772a992009-05-22 14:50:54 -04004709 /*
4710 * Initialize timers used by driver
4711 */
dea31012005-04-17 16:05:31 -05004712
James Smart3772a992009-05-22 14:50:54 -04004713 /* Heartbeat timer */
4714 init_timer(&phba->hb_tmofunc);
4715 phba->hb_tmofunc.function = lpfc_hb_timeout;
4716 phba->hb_tmofunc.data = (unsigned long)phba;
4717
4718 psli = &phba->sli;
4719 /* MBOX heartbeat timer */
4720 init_timer(&psli->mbox_tmo);
4721 psli->mbox_tmo.function = lpfc_mbox_timeout;
4722 psli->mbox_tmo.data = (unsigned long) phba;
4723 /* FCP polling mode timer */
4724 init_timer(&phba->fcp_poll_timer);
4725 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4726 phba->fcp_poll_timer.data = (unsigned long) phba;
4727 /* Fabric block timer */
4728 init_timer(&phba->fabric_block_timer);
4729 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4730 phba->fabric_block_timer.data = (unsigned long) phba;
4731 /* EA polling mode timer */
4732 init_timer(&phba->eratt_poll);
4733 phba->eratt_poll.function = lpfc_poll_eratt;
4734 phba->eratt_poll.data = (unsigned long) phba;
4735
4736 /* Host attention work mask setup */
4737 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4738 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4739
4740 /* Get all the module params for configuring this host */
4741 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004742 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4743 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4744 /* check for menlo minimum sg count */
4745 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4746 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4747 }
4748
James Smart2a76a282012-08-03 12:35:54 -04004749 if (!phba->sli.ring)
4750 phba->sli.ring = (struct lpfc_sli_ring *)
4751 kzalloc(LPFC_SLI3_MAX_RING *
4752 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4753 if (!phba->sli.ring)
4754 return -ENOMEM;
4755
James Smart3772a992009-05-22 14:50:54 -04004756 /*
James Smart96f70772013-04-17 20:16:15 -04004757 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04004758 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04004759 */
James Smart3772a992009-05-22 14:50:54 -04004760
James Smart96f70772013-04-17 20:16:15 -04004761 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04004762 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4763 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4764
James Smart96f70772013-04-17 20:16:15 -04004765 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
4766 if (phba->cfg_enable_bg) {
4767 /*
4768 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4769 * the FCP rsp, and a BDE for each. Sice we have no control
4770 * over how many protection data segments the SCSI Layer
4771 * will hand us (ie: there could be one for every block
4772 * in the IO), we just allocate enough BDEs to accomidate
4773 * our max amount and we need to limit lpfc_sg_seg_cnt to
4774 * minimize the risk of running out.
4775 */
4776 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4777 sizeof(struct fcp_rsp) +
4778 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
4779
4780 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
4781 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
4782
4783 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
4784 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4785 } else {
4786 /*
4787 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4788 * the FCP rsp, a BDE for each, and a BDE for up to
4789 * cfg_sg_seg_cnt data segments.
4790 */
4791 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4792 sizeof(struct fcp_rsp) +
4793 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4794
4795 /* Total BDEs in BPL for scsi_sg_list */
4796 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4797 }
4798
4799 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4800 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
4801 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4802 phba->cfg_total_seg_cnt);
4803
James Smart3772a992009-05-22 14:50:54 -04004804 phba->max_vpi = LPFC_MAX_VPI;
4805 /* This will be set to correct value after config_port mbox */
4806 phba->max_vports = 0;
4807
4808 /*
4809 * Initialize the SLI Layer to run with lpfc HBAs.
4810 */
4811 lpfc_sli_setup(phba);
4812 lpfc_sli_queue_setup(phba);
4813
4814 /* Allocate device driver memory */
4815 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4816 return -ENOMEM;
4817
James Smart912e3ac2011-05-24 11:42:11 -04004818 /*
4819 * Enable sr-iov virtual functions if supported and configured
4820 * through the module parameter.
4821 */
4822 if (phba->cfg_sriov_nr_virtfn > 0) {
4823 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4824 phba->cfg_sriov_nr_virtfn);
4825 if (rc) {
4826 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4827 "2808 Requested number of SR-IOV "
4828 "virtual functions (%d) is not "
4829 "supported\n",
4830 phba->cfg_sriov_nr_virtfn);
4831 phba->cfg_sriov_nr_virtfn = 0;
4832 }
4833 }
4834
James Smart3772a992009-05-22 14:50:54 -04004835 return 0;
4836}
4837
4838/**
4839 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4840 * @phba: pointer to lpfc hba data structure.
4841 *
4842 * This routine is invoked to unset the driver internal resources set up
4843 * specific for supporting the SLI-3 HBA device it attached to.
4844 **/
4845static void
4846lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4847{
4848 /* Free device driver memory allocated */
4849 lpfc_mem_free_all(phba);
4850
4851 return;
4852}
4853
4854/**
James Smartda0436e2009-05-22 14:51:39 -04004855 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4856 * @phba: pointer to lpfc hba data structure.
4857 *
4858 * This routine is invoked to set up the driver internal resources specific to
4859 * support the SLI-4 HBA device it attached to.
4860 *
4861 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004862 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004863 * other values - error
4864 **/
4865static int
4866lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4867{
James Smart7bb03bb2013-04-17 20:19:16 -04004868 struct lpfc_vector_map_info *cpup;
James Smartda0436e2009-05-22 14:51:39 -04004869 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004870 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04004871 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05004872 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4873 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04004874 int longs;
James Smartda0436e2009-05-22 14:51:39 -04004875
4876 /* Before proceed, wait for POST done and device ready */
4877 rc = lpfc_sli4_post_status_check(phba);
4878 if (rc)
4879 return -ENODEV;
4880
4881 /*
4882 * Initialize timers used by driver
4883 */
4884
4885 /* Heartbeat timer */
4886 init_timer(&phba->hb_tmofunc);
4887 phba->hb_tmofunc.function = lpfc_hb_timeout;
4888 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004889 init_timer(&phba->rrq_tmr);
4890 phba->rrq_tmr.function = lpfc_rrq_timeout;
4891 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004892
4893 psli = &phba->sli;
4894 /* MBOX heartbeat timer */
4895 init_timer(&psli->mbox_tmo);
4896 psli->mbox_tmo.function = lpfc_mbox_timeout;
4897 psli->mbox_tmo.data = (unsigned long) phba;
4898 /* Fabric block timer */
4899 init_timer(&phba->fabric_block_timer);
4900 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4901 phba->fabric_block_timer.data = (unsigned long) phba;
4902 /* EA polling mode timer */
4903 init_timer(&phba->eratt_poll);
4904 phba->eratt_poll.function = lpfc_poll_eratt;
4905 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004906 /* FCF rediscover timer */
4907 init_timer(&phba->fcf.redisc_wait);
4908 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4909 phba->fcf.redisc_wait.data = (unsigned long)phba;
4910
James Smartda0436e2009-05-22 14:51:39 -04004911 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004912 * Control structure for handling external multi-buffer mailbox
4913 * command pass-through.
4914 */
4915 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4916 sizeof(struct lpfc_mbox_ext_buf_ctx));
4917 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4918
4919 /*
James Smartda0436e2009-05-22 14:51:39 -04004920 * We need to do a READ_CONFIG mailbox command here before
4921 * calling lpfc_get_cfgparam. For VFs this will report the
4922 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4923 * All of the resources allocated
4924 * for this Port are tied to these values.
4925 */
4926 /* Get all the module params for configuring this host */
4927 lpfc_get_cfgparam(phba);
4928 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04004929
4930 /* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
4931 phba->cfg_fcp_io_channel = phba->cfg_fcp_eq_count;
4932
James Smartda0436e2009-05-22 14:51:39 -04004933 /* This will be set to correct value after the read_config mbox */
4934 phba->max_vports = 0;
4935
4936 /* Program the default value of vlan_id and fc_map */
4937 phba->valid_vlan = 0;
4938 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4939 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4940 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4941
4942 /*
James Smart2a76a282012-08-03 12:35:54 -04004943 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
4944 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
4945 */
4946 if (!phba->sli.ring)
4947 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04004948 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04004949 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4950 if (!phba->sli.ring)
4951 return -ENOMEM;
James Smart085c6472010-11-20 23:11:37 -05004952
James Smart09294d42013-04-17 20:16:05 -04004953 /*
4954 * It doesn't matter what family our adapter is in, we are
4955 * limited to 2 Pages, 512 SGEs, for our SGL.
4956 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
4957 */
4958 max_buf_size = (2 * SLI4_PAGE_SIZE);
4959 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
4960 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04004961
James Smartda0436e2009-05-22 14:51:39 -04004962 /*
James Smart96f70772013-04-17 20:16:15 -04004963 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04004964 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart28baac72010-02-12 14:42:03 -05004965 */
James Smart085c6472010-11-20 23:11:37 -05004966
James Smart96f70772013-04-17 20:16:15 -04004967 if (phba->cfg_enable_bg) {
4968 /*
4969 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4970 * the FCP rsp, and a SGE for each. Sice we have no control
4971 * over how many protection data segments the SCSI Layer
4972 * will hand us (ie: there could be one for every block
4973 * in the IO), we just allocate enough SGEs to accomidate
4974 * our max amount and we need to limit lpfc_sg_seg_cnt to
4975 * minimize the risk of running out.
4976 */
4977 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4978 sizeof(struct fcp_rsp) + max_buf_size;
4979
4980 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
4981 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
4982
4983 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
4984 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
4985 } else {
4986 /*
4987 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4988 * the FCP rsp, a SGE for each, and a SGE for up to
4989 * cfg_sg_seg_cnt data segments.
4990 */
4991 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4992 sizeof(struct fcp_rsp) +
4993 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
4994
4995 /* Total SGEs for scsi_sg_list */
4996 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4997 /*
4998 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
4999 * to post 1 page for the SGL.
5000 */
James Smart085c6472010-11-20 23:11:37 -05005001 }
James Smartacd68592012-01-18 16:25:09 -05005002
James Smart96f70772013-04-17 20:16:15 -04005003 /* Initialize the host templates with the updated values. */
5004 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5005 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5006
5007 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5008 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5009 else
5010 phba->cfg_sg_dma_buf_size =
5011 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5012
5013 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5014 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5015 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5016 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005017
5018 /* Initialize buffer queue management fields */
5019 hbq_count = lpfc_sli_hbq_count();
5020 for (i = 0; i < hbq_count; ++i)
5021 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5022 INIT_LIST_HEAD(&phba->rb_pend_list);
5023 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5024 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5025
5026 /*
5027 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5028 */
5029 /* Initialize the Abort scsi buffer list used by driver */
5030 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5031 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5032 /* This abort list used by worker thread */
5033 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5034
5035 /*
James Smart6d368e52011-05-24 11:44:12 -04005036 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005037 */
5038
5039 /* Driver internel slow-path CQ Event pool */
5040 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5041 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005042 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005043 /* Asynchronous event CQ Event work queue list */
5044 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5045 /* Fast-path XRI aborted CQ Event work queue list */
5046 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5047 /* Slow-path XRI aborted CQ Event work queue list */
5048 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5049 /* Receive queue CQ Event work queue list */
5050 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5051
James Smart6d368e52011-05-24 11:44:12 -04005052 /* Initialize extent block lists. */
5053 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5054 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5055 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5056 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5057
James Smartda0436e2009-05-22 14:51:39 -04005058 /* Initialize the driver internal SLI layer lists. */
5059 lpfc_sli_setup(phba);
5060 lpfc_sli_queue_setup(phba);
5061
5062 /* Allocate device driver memory */
5063 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5064 if (rc)
5065 return -ENOMEM;
5066
James Smart2fcee4b2010-12-15 17:57:46 -05005067 /* IF Type 2 ports get initialized now. */
5068 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5069 LPFC_SLI_INTF_IF_TYPE_2) {
5070 rc = lpfc_pci_function_reset(phba);
5071 if (unlikely(rc))
5072 return -ENODEV;
5073 }
5074
James Smartda0436e2009-05-22 14:51:39 -04005075 /* Create the bootstrap mailbox command */
5076 rc = lpfc_create_bootstrap_mbox(phba);
5077 if (unlikely(rc))
5078 goto out_free_mem;
5079
5080 /* Set up the host's endian order with the device. */
5081 rc = lpfc_setup_endian_order(phba);
5082 if (unlikely(rc))
5083 goto out_free_bsmbx;
5084
5085 /* Set up the hba's configuration parameters. */
5086 rc = lpfc_sli4_read_config(phba);
5087 if (unlikely(rc))
5088 goto out_free_bsmbx;
5089
James Smart2fcee4b2010-12-15 17:57:46 -05005090 /* IF Type 0 ports get initialized now. */
5091 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5092 LPFC_SLI_INTF_IF_TYPE_0) {
5093 rc = lpfc_pci_function_reset(phba);
5094 if (unlikely(rc))
5095 goto out_free_bsmbx;
5096 }
James Smartda0436e2009-05-22 14:51:39 -04005097
James Smartcb5172e2010-03-15 11:25:07 -04005098 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5099 GFP_KERNEL);
5100 if (!mboxq) {
5101 rc = -ENOMEM;
5102 goto out_free_bsmbx;
5103 }
5104
James Smartfedd3b72011-02-16 12:39:24 -05005105 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005106 lpfc_supported_pages(mboxq);
5107 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005108 if (!rc) {
5109 mqe = &mboxq->u.mqe;
5110 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5111 LPFC_MAX_SUPPORTED_PAGES);
5112 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5113 switch (pn_page[i]) {
5114 case LPFC_SLI4_PARAMETERS:
5115 phba->sli4_hba.pc_sli4_params.supported = 1;
5116 break;
5117 default:
5118 break;
5119 }
5120 }
5121 /* Read the port's SLI4 Parameters capabilities if supported. */
5122 if (phba->sli4_hba.pc_sli4_params.supported)
5123 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5124 if (rc) {
5125 mempool_free(mboxq, phba->mbox_mem_pool);
5126 rc = -EIO;
5127 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005128 }
5129 }
James Smartfedd3b72011-02-16 12:39:24 -05005130 /*
5131 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005132 * If this call fails, it isn't critical unless the SLI4 parameters come
5133 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005134 */
James Smart6d368e52011-05-24 11:44:12 -04005135 rc = lpfc_get_sli4_parameters(phba, mboxq);
5136 if (rc) {
5137 if (phba->sli4_hba.extents_in_use &&
5138 phba->sli4_hba.rpi_hdrs_in_use) {
5139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5140 "2999 Unsupported SLI4 Parameters "
5141 "Extents and RPI headers enabled.\n");
5142 goto out_free_bsmbx;
5143 }
5144 }
James Smartcb5172e2010-03-15 11:25:07 -04005145 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart5350d872011-10-10 21:33:49 -04005146 /* Verify all the SLI4 queues */
5147 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005148 if (rc)
5149 goto out_free_bsmbx;
5150
5151 /* Create driver internal CQE event pool */
5152 rc = lpfc_sli4_cq_event_pool_create(phba);
5153 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005154 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005155
James Smart8a9d2e82012-05-09 21:16:12 -04005156 /* Initialize sgl lists per host */
5157 lpfc_init_sgl_list(phba);
5158
5159 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005160 rc = lpfc_init_active_sgl_array(phba);
5161 if (rc) {
5162 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5163 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005164 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005165 }
James Smartda0436e2009-05-22 14:51:39 -04005166 rc = lpfc_sli4_init_rpi_hdrs(phba);
5167 if (rc) {
5168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5169 "1432 Failed to initialize rpi headers.\n");
5170 goto out_free_active_sgl;
5171 }
5172
James Smarta93ff372010-10-22 11:06:08 -04005173 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005174 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5175 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5176 GFP_KERNEL);
5177 if (!phba->fcf.fcf_rr_bmask) {
5178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5179 "2759 Failed allocate memory for FCF round "
5180 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005181 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005182 goto out_remove_rpi_hdrs;
5183 }
5184
James Smart67d12732012-08-03 12:36:13 -04005185 phba->sli4_hba.fcp_eq_hdl =
5186 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
5187 phba->cfg_fcp_io_channel), GFP_KERNEL);
5188 if (!phba->sli4_hba.fcp_eq_hdl) {
5189 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5190 "2572 Failed allocate memory for "
5191 "fast-path per-EQ handle array\n");
5192 rc = -ENOMEM;
5193 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005194 }
5195
5196 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart82c3e9b2012-09-29 11:29:50 -04005197 phba->cfg_fcp_io_channel), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005198 if (!phba->sli4_hba.msix_entries) {
5199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5200 "2573 Failed allocate memory for msi-x "
5201 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005202 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005203 goto out_free_fcp_eq_hdl;
5204 }
5205
James Smart7bb03bb2013-04-17 20:19:16 -04005206 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5207 phba->sli4_hba.num_present_cpu),
5208 GFP_KERNEL);
5209 if (!phba->sli4_hba.cpu_map) {
5210 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5211 "3327 Failed allocate memory for msi-x "
5212 "interrupt vector mapping\n");
5213 rc = -ENOMEM;
5214 goto out_free_msix;
5215 }
5216 /* Initialize io channels for round robin */
5217 cpup = phba->sli4_hba.cpu_map;
5218 rc = 0;
5219 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5220 cpup->channel_id = rc;
5221 rc++;
5222 if (rc >= phba->cfg_fcp_io_channel)
5223 rc = 0;
5224 }
5225
James Smart912e3ac2011-05-24 11:42:11 -04005226 /*
5227 * Enable sr-iov virtual functions if supported and configured
5228 * through the module parameter.
5229 */
5230 if (phba->cfg_sriov_nr_virtfn > 0) {
5231 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5232 phba->cfg_sriov_nr_virtfn);
5233 if (rc) {
5234 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5235 "3020 Requested number of SR-IOV "
5236 "virtual functions (%d) is not "
5237 "supported\n",
5238 phba->cfg_sriov_nr_virtfn);
5239 phba->cfg_sriov_nr_virtfn = 0;
5240 }
5241 }
5242
James Smart5248a742011-07-22 18:37:06 -04005243 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005244
James Smart7bb03bb2013-04-17 20:19:16 -04005245out_free_msix:
5246 kfree(phba->sli4_hba.msix_entries);
James Smartda0436e2009-05-22 14:51:39 -04005247out_free_fcp_eq_hdl:
5248 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005249out_free_fcf_rr_bmask:
5250 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005251out_remove_rpi_hdrs:
5252 lpfc_sli4_remove_rpi_hdrs(phba);
5253out_free_active_sgl:
5254 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005255out_destroy_cq_event_pool:
5256 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005257out_free_bsmbx:
5258 lpfc_destroy_bootstrap_mbox(phba);
5259out_free_mem:
5260 lpfc_mem_free(phba);
5261 return rc;
5262}
5263
5264/**
5265 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5266 * @phba: pointer to lpfc hba data structure.
5267 *
5268 * This routine is invoked to unset the driver internal resources set up
5269 * specific for supporting the SLI-4 HBA device it attached to.
5270 **/
5271static void
5272lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5273{
5274 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5275
James Smart7bb03bb2013-04-17 20:19:16 -04005276 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5277 kfree(phba->sli4_hba.cpu_map);
5278 phba->sli4_hba.num_present_cpu = 0;
5279 phba->sli4_hba.num_online_cpu = 0;
5280
James Smartda0436e2009-05-22 14:51:39 -04005281 /* Free memory allocated for msi-x interrupt vector entries */
5282 kfree(phba->sli4_hba.msix_entries);
5283
5284 /* Free memory allocated for fast-path work queue handles */
5285 kfree(phba->sli4_hba.fcp_eq_hdl);
5286
5287 /* Free the allocated rpi headers. */
5288 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005289 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005290
James Smart0c9ab6f2010-02-26 14:15:57 -05005291 /* Free eligible FCF index bmask */
5292 kfree(phba->fcf.fcf_rr_bmask);
5293
James Smartda0436e2009-05-22 14:51:39 -04005294 /* Free the ELS sgl list */
5295 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005296 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005297
James Smartda0436e2009-05-22 14:51:39 -04005298 /* Free the completion queue EQ event pool */
5299 lpfc_sli4_cq_event_release_all(phba);
5300 lpfc_sli4_cq_event_pool_destroy(phba);
5301
James Smart6d368e52011-05-24 11:44:12 -04005302 /* Release resource identifiers. */
5303 lpfc_sli4_dealloc_resource_identifiers(phba);
5304
James Smartda0436e2009-05-22 14:51:39 -04005305 /* Free the bsmbx region. */
5306 lpfc_destroy_bootstrap_mbox(phba);
5307
5308 /* Free the SLI Layer memory with SLI4 HBAs */
5309 lpfc_mem_free_all(phba);
5310
5311 /* Free the current connect table */
5312 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005313 &phba->fcf_conn_rec_list, list) {
5314 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005315 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005316 }
James Smartda0436e2009-05-22 14:51:39 -04005317
5318 return;
5319}
5320
5321/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005322 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005323 * @phba: The hba struct for which this call is being executed.
5324 * @dev_grp: The HBA PCI-Device group number.
5325 *
5326 * This routine sets up the device INIT interface API function jump table
5327 * in @phba struct.
5328 *
5329 * Returns: 0 - success, -ENODEV - failure.
5330 **/
5331int
5332lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5333{
James Smart84d1b002010-02-12 14:42:33 -05005334 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5335 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005336 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005337 switch (dev_grp) {
5338 case LPFC_PCI_DEV_LP:
5339 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5340 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5341 phba->lpfc_stop_port = lpfc_stop_port_s3;
5342 break;
James Smartda0436e2009-05-22 14:51:39 -04005343 case LPFC_PCI_DEV_OC:
5344 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5345 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5346 phba->lpfc_stop_port = lpfc_stop_port_s4;
5347 break;
James Smart3772a992009-05-22 14:50:54 -04005348 default:
5349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5350 "1431 Invalid HBA PCI-device group: 0x%x\n",
5351 dev_grp);
5352 return -ENODEV;
5353 break;
5354 }
5355 return 0;
5356}
5357
5358/**
5359 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5360 * @phba: pointer to lpfc hba data structure.
5361 *
5362 * This routine is invoked to set up the driver internal resources before the
5363 * device specific resource setup to support the HBA device it attached to.
5364 *
5365 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005366 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005367 * other values - error
5368 **/
5369static int
5370lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5371{
5372 /*
5373 * Driver resources common to all SLI revisions
5374 */
James Smartea2151b2008-09-07 11:52:10 -04005375 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005376 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005377
James Smarte47c9092008-02-08 18:49:26 -05005378 /* Initialize ndlp management spinlock */
5379 spin_lock_init(&phba->ndlp_lock);
5380
James Smart2e0fef82007-06-17 19:56:36 -05005381 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005382 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005383 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005384
James Smart3772a992009-05-22 14:50:54 -04005385 /* Initialize the wait queue head for the kernel thread */
5386 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005387
James Smart3772a992009-05-22 14:50:54 -04005388 /* Initialize the scsi buffer list used by driver for scsi IO */
James Smarta40fc5f2013-04-17 20:17:40 -04005389 spin_lock_init(&phba->scsi_buf_list_get_lock);
5390 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5391 spin_lock_init(&phba->scsi_buf_list_put_lock);
5392 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
dea31012005-04-17 16:05:31 -05005393
James Smart3772a992009-05-22 14:50:54 -04005394 /* Initialize the fabric iocb list */
5395 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005396
James Smart3772a992009-05-22 14:50:54 -04005397 /* Initialize list to save ELS buffers */
5398 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005399
James Smart3772a992009-05-22 14:50:54 -04005400 /* Initialize FCF connection rec list */
5401 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005402
James Smart3772a992009-05-22 14:50:54 -04005403 return 0;
5404}
dea31012005-04-17 16:05:31 -05005405
James Smart3772a992009-05-22 14:50:54 -04005406/**
5407 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5408 * @phba: pointer to lpfc hba data structure.
5409 *
5410 * This routine is invoked to set up the driver internal resources after the
5411 * device specific resource setup to support the HBA device it attached to.
5412 *
5413 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005414 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005415 * other values - error
5416 **/
5417static int
5418lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5419{
5420 int error;
5421
5422 /* Startup the kernel thread for this host adapter. */
5423 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5424 "lpfc_worker_%d", phba->brd_no);
5425 if (IS_ERR(phba->worker_thread)) {
5426 error = PTR_ERR(phba->worker_thread);
5427 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005428 }
5429
James Smart3772a992009-05-22 14:50:54 -04005430 return 0;
5431}
5432
5433/**
5434 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5435 * @phba: pointer to lpfc hba data structure.
5436 *
5437 * This routine is invoked to unset the driver internal resources set up after
5438 * the device specific resource setup for supporting the HBA device it
5439 * attached to.
5440 **/
5441static void
5442lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5443{
5444 /* Stop kernel worker thread */
5445 kthread_stop(phba->worker_thread);
5446}
5447
5448/**
5449 * lpfc_free_iocb_list - Free iocb list.
5450 * @phba: pointer to lpfc hba data structure.
5451 *
5452 * This routine is invoked to free the driver's IOCB list and memory.
5453 **/
5454static void
5455lpfc_free_iocb_list(struct lpfc_hba *phba)
5456{
5457 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5458
5459 spin_lock_irq(&phba->hbalock);
5460 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5461 &phba->lpfc_iocb_list, list) {
5462 list_del(&iocbq_entry->list);
5463 kfree(iocbq_entry);
5464 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005465 }
James Smart3772a992009-05-22 14:50:54 -04005466 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005467
James Smart3772a992009-05-22 14:50:54 -04005468 return;
5469}
dea31012005-04-17 16:05:31 -05005470
James Smart3772a992009-05-22 14:50:54 -04005471/**
5472 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5473 * @phba: pointer to lpfc hba data structure.
5474 *
5475 * This routine is invoked to allocate and initizlize the driver's IOCB
5476 * list and set up the IOCB tag array accordingly.
5477 *
5478 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005479 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005480 * other values - error
5481 **/
5482static int
5483lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5484{
5485 struct lpfc_iocbq *iocbq_entry = NULL;
5486 uint16_t iotag;
5487 int i;
dea31012005-04-17 16:05:31 -05005488
5489 /* Initialize and populate the iocb list per host. */
5490 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005491 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005492 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005493 if (iocbq_entry == NULL) {
5494 printk(KERN_ERR "%s: only allocated %d iocbs of "
5495 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005496 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005497 goto out_free_iocbq;
5498 }
5499
James Bottomley604a3e32005-10-29 10:28:33 -05005500 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5501 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005502 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005503 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005504 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005505 goto out_free_iocbq;
5506 }
James Smart6d368e52011-05-24 11:44:12 -04005507 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005508 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005509
5510 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005511 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5512 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005513 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005514 }
5515
James Smart3772a992009-05-22 14:50:54 -04005516 return 0;
5517
5518out_free_iocbq:
5519 lpfc_free_iocb_list(phba);
5520
5521 return -ENOMEM;
5522}
5523
5524/**
James Smart8a9d2e82012-05-09 21:16:12 -04005525 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005526 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005527 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005528 *
James Smart8a9d2e82012-05-09 21:16:12 -04005529 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005530 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005531void
5532lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005533{
5534 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005535
5536 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5537 list_del(&sglq_entry->list);
5538 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5539 kfree(sglq_entry);
5540 }
5541}
5542
5543/**
5544 * lpfc_free_els_sgl_list - Free els sgl list.
5545 * @phba: pointer to lpfc hba data structure.
5546 *
5547 * This routine is invoked to free the driver's els sgl list and memory.
5548 **/
5549static void
5550lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5551{
James Smartda0436e2009-05-22 14:51:39 -04005552 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005553
James Smart8a9d2e82012-05-09 21:16:12 -04005554 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005555 spin_lock_irq(&phba->hbalock);
5556 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5557 spin_unlock_irq(&phba->hbalock);
5558
James Smart8a9d2e82012-05-09 21:16:12 -04005559 /* Now free the sgl list */
5560 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005561}
5562
5563/**
5564 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5565 * @phba: pointer to lpfc hba data structure.
5566 *
5567 * This routine is invoked to allocate the driver's active sgl memory.
5568 * This array will hold the sglq_entry's for active IOs.
5569 **/
5570static int
5571lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5572{
5573 int size;
5574 size = sizeof(struct lpfc_sglq *);
5575 size *= phba->sli4_hba.max_cfg_param.max_xri;
5576
5577 phba->sli4_hba.lpfc_sglq_active_list =
5578 kzalloc(size, GFP_KERNEL);
5579 if (!phba->sli4_hba.lpfc_sglq_active_list)
5580 return -ENOMEM;
5581 return 0;
5582}
5583
5584/**
5585 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5586 * @phba: pointer to lpfc hba data structure.
5587 *
5588 * This routine is invoked to walk through the array of active sglq entries
5589 * and free all of the resources.
5590 * This is just a place holder for now.
5591 **/
5592static void
5593lpfc_free_active_sgl(struct lpfc_hba *phba)
5594{
5595 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5596}
5597
5598/**
5599 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5600 * @phba: pointer to lpfc hba data structure.
5601 *
5602 * This routine is invoked to allocate and initizlize the driver's sgl
5603 * list and set up the sgl xritag tag array accordingly.
5604 *
James Smartda0436e2009-05-22 14:51:39 -04005605 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005606static void
James Smartda0436e2009-05-22 14:51:39 -04005607lpfc_init_sgl_list(struct lpfc_hba *phba)
5608{
James Smartda0436e2009-05-22 14:51:39 -04005609 /* Initialize and populate the sglq list per host/VF. */
5610 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5611 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5612
James Smart8a9d2e82012-05-09 21:16:12 -04005613 /* els xri-sgl book keeping */
5614 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005615
James Smart8a9d2e82012-05-09 21:16:12 -04005616 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005617 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005618}
5619
5620/**
5621 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5622 * @phba: pointer to lpfc hba data structure.
5623 *
5624 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005625 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005626 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005627 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5628 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005629 *
5630 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005631 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005632 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005633 **/
5634int
5635lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5636{
5637 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005638 struct lpfc_rpi_hdr *rpi_hdr;
5639
5640 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005641 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005642 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005643 if (phba->sli4_hba.extents_in_use)
5644 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005645
5646 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5647 if (!rpi_hdr) {
5648 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5649 "0391 Error during rpi post operation\n");
5650 lpfc_sli4_remove_rpis(phba);
5651 rc = -ENODEV;
5652 }
5653
5654 return rc;
5655}
5656
5657/**
5658 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5659 * @phba: pointer to lpfc hba data structure.
5660 *
5661 * This routine is invoked to allocate a single 4KB memory region to
5662 * support rpis and stores them in the phba. This single region
5663 * provides support for up to 64 rpis. The region is used globally
5664 * by the device.
5665 *
5666 * Returns:
5667 * A valid rpi hdr on success.
5668 * A NULL pointer on any failure.
5669 **/
5670struct lpfc_rpi_hdr *
5671lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5672{
5673 uint16_t rpi_limit, curr_rpi_range;
5674 struct lpfc_dmabuf *dmabuf;
5675 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b062011-04-16 11:03:17 -04005676 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005677
James Smart6d368e52011-05-24 11:44:12 -04005678 /*
5679 * If the SLI4 port supports extents, posting the rpi header isn't
5680 * required. Set the expected maximum count and let the actual value
5681 * get set when extents are fully allocated.
5682 */
5683 if (!phba->sli4_hba.rpi_hdrs_in_use)
5684 return NULL;
5685 if (phba->sli4_hba.extents_in_use)
5686 return NULL;
5687
5688 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005689 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005690 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005691
5692 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005693 /*
5694 * Establish the starting RPI in this header block. The starting
5695 * rpi is normalized to a zero base because the physical rpi is
5696 * port based.
5697 */
James Smart97f2ecf2012-03-01 22:35:23 -05005698 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005699 spin_unlock_irq(&phba->hbalock);
5700
5701 /*
5702 * The port has a limited number of rpis. The increment here
5703 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5704 * and to allow the full max_rpi range per port.
5705 */
5706 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b062011-04-16 11:03:17 -04005707 rpi_count = rpi_limit - curr_rpi_range;
5708 else
5709 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005710
James Smart6d368e52011-05-24 11:44:12 -04005711 if (!rpi_count)
5712 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005713 /*
5714 * First allocate the protocol header region for the port. The
5715 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5716 */
5717 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5718 if (!dmabuf)
5719 return NULL;
5720
5721 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5722 LPFC_HDR_TEMPLATE_SIZE,
5723 &dmabuf->phys,
5724 GFP_KERNEL);
5725 if (!dmabuf->virt) {
5726 rpi_hdr = NULL;
5727 goto err_free_dmabuf;
5728 }
5729
5730 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5731 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5732 rpi_hdr = NULL;
5733 goto err_free_coherent;
5734 }
5735
5736 /* Save the rpi header data for cleanup later. */
5737 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5738 if (!rpi_hdr)
5739 goto err_free_coherent;
5740
5741 rpi_hdr->dmabuf = dmabuf;
5742 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5743 rpi_hdr->page_count = 1;
5744 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005745
5746 /* The rpi_hdr stores the logical index only. */
5747 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005748 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5749
5750 /*
James Smart6d368e52011-05-24 11:44:12 -04005751 * The next_rpi stores the next logical module-64 rpi value used
5752 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005753 */
James Smart9589b062011-04-16 11:03:17 -04005754 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005755 spin_unlock_irq(&phba->hbalock);
5756 return rpi_hdr;
5757
5758 err_free_coherent:
5759 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5760 dmabuf->virt, dmabuf->phys);
5761 err_free_dmabuf:
5762 kfree(dmabuf);
5763 return NULL;
5764}
5765
5766/**
5767 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5768 * @phba: pointer to lpfc hba data structure.
5769 *
5770 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005771 * to support rpis for SLI4 ports not supporting extents. This routine
5772 * presumes the caller has released all rpis consumed by fabric or port
5773 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005774 **/
5775void
5776lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5777{
5778 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5779
James Smart6d368e52011-05-24 11:44:12 -04005780 if (!phba->sli4_hba.rpi_hdrs_in_use)
5781 goto exit;
5782
James Smartda0436e2009-05-22 14:51:39 -04005783 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5784 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5785 list_del(&rpi_hdr->list);
5786 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5787 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5788 kfree(rpi_hdr->dmabuf);
5789 kfree(rpi_hdr);
5790 }
James Smart6d368e52011-05-24 11:44:12 -04005791 exit:
5792 /* There are no rpis available to the port now. */
5793 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005794}
5795
5796/**
James Smart3772a992009-05-22 14:50:54 -04005797 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5798 * @pdev: pointer to pci device data structure.
5799 *
5800 * This routine is invoked to allocate the driver hba data structure for an
5801 * HBA device. If the allocation is successful, the phba reference to the
5802 * PCI device data structure is set.
5803 *
5804 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005805 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005806 * NULL - error
5807 **/
5808static struct lpfc_hba *
5809lpfc_hba_alloc(struct pci_dev *pdev)
5810{
5811 struct lpfc_hba *phba;
5812
5813 /* Allocate memory for HBA structure */
5814 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5815 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005816 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005817 return NULL;
5818 }
5819
5820 /* Set reference to PCI device in HBA structure */
5821 phba->pcidev = pdev;
5822
5823 /* Assign an unused board number */
5824 phba->brd_no = lpfc_get_instance();
5825 if (phba->brd_no < 0) {
5826 kfree(phba);
5827 return NULL;
5828 }
5829
James Smart4fede782010-01-26 23:08:55 -05005830 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005831 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5832
James Smart3772a992009-05-22 14:50:54 -04005833 return phba;
5834}
5835
5836/**
5837 * lpfc_hba_free - Free driver hba data structure with a device.
5838 * @phba: pointer to lpfc hba data structure.
5839 *
5840 * This routine is invoked to free the driver hba data structure with an
5841 * HBA device.
5842 **/
5843static void
5844lpfc_hba_free(struct lpfc_hba *phba)
5845{
5846 /* Release the driver assigned board number */
5847 idr_remove(&lpfc_hba_index, phba->brd_no);
5848
James Smart2a76a282012-08-03 12:35:54 -04005849 /* Free memory allocated with sli rings */
5850 kfree(phba->sli.ring);
5851 phba->sli.ring = NULL;
5852
James Smart3772a992009-05-22 14:50:54 -04005853 kfree(phba);
5854 return;
5855}
5856
5857/**
5858 * lpfc_create_shost - Create hba physical port with associated scsi host.
5859 * @phba: pointer to lpfc hba data structure.
5860 *
5861 * This routine is invoked to create HBA physical port and associate a SCSI
5862 * host with it.
5863 *
5864 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005865 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005866 * other values - error
5867 **/
5868static int
5869lpfc_create_shost(struct lpfc_hba *phba)
5870{
5871 struct lpfc_vport *vport;
5872 struct Scsi_Host *shost;
5873
5874 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005875 phba->fc_edtov = FF_DEF_EDTOV;
5876 phba->fc_ratov = FF_DEF_RATOV;
5877 phba->fc_altov = FF_DEF_ALTOV;
5878 phba->fc_arbtov = FF_DEF_ARBTOV;
5879
James Smartd7c47992010-06-08 18:31:54 -04005880 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005881 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005882 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005883 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005884
5885 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005886 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005887 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005888 /* Put reference to SCSI host to driver's device private data */
5889 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005890
James Smart3772a992009-05-22 14:50:54 -04005891 return 0;
5892}
dea31012005-04-17 16:05:31 -05005893
James Smart3772a992009-05-22 14:50:54 -04005894/**
5895 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5896 * @phba: pointer to lpfc hba data structure.
5897 *
5898 * This routine is invoked to destroy HBA physical port and the associated
5899 * SCSI host.
5900 **/
5901static void
5902lpfc_destroy_shost(struct lpfc_hba *phba)
5903{
5904 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005905
James Smart3772a992009-05-22 14:50:54 -04005906 /* Destroy physical port that associated with the SCSI host */
5907 destroy_port(vport);
5908
5909 return;
5910}
5911
5912/**
5913 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5914 * @phba: pointer to lpfc hba data structure.
5915 * @shost: the shost to be used to detect Block guard settings.
5916 *
5917 * This routine sets up the local Block guard protocol settings for @shost.
5918 * This routine also allocates memory for debugging bg buffers.
5919 **/
5920static void
5921lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5922{
James Smartbbeb79b2012-06-12 13:54:27 -04005923 uint32_t old_mask;
5924 uint32_t old_guard;
5925
James Smart3772a992009-05-22 14:50:54 -04005926 int pagecnt = 10;
5927 if (lpfc_prot_mask && lpfc_prot_guard) {
5928 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5929 "1478 Registering BlockGuard with the "
5930 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04005931
5932 old_mask = lpfc_prot_mask;
5933 old_guard = lpfc_prot_guard;
5934
5935 /* Only allow supported values */
5936 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
5937 SHOST_DIX_TYPE0_PROTECTION |
5938 SHOST_DIX_TYPE1_PROTECTION);
5939 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
5940
5941 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
5942 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
5943 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
5944
5945 if (lpfc_prot_mask && lpfc_prot_guard) {
5946 if ((old_mask != lpfc_prot_mask) ||
5947 (old_guard != lpfc_prot_guard))
5948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5949 "1475 Registering BlockGuard with the "
5950 "SCSI layer: mask %d guard %d\n",
5951 lpfc_prot_mask, lpfc_prot_guard);
5952
5953 scsi_host_set_prot(shost, lpfc_prot_mask);
5954 scsi_host_set_guard(shost, lpfc_prot_guard);
5955 } else
5956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5957 "1479 Not Registering BlockGuard with the SCSI "
5958 "layer, Bad protection parameters: %d %d\n",
5959 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005960 }
James Smartbbeb79b2012-06-12 13:54:27 -04005961
James Smart81301a92008-12-04 22:39:46 -05005962 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005963 while (pagecnt) {
5964 spin_lock_init(&_dump_buf_lock);
5965 _dump_buf_data =
5966 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5967 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005968 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5969 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005970 "_dump_buf_data at 0x%p\n",
5971 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005972 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005973 memset(_dump_buf_data, 0,
5974 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005975 break;
James Smart3772a992009-05-22 14:50:54 -04005976 } else
James Smart81301a92008-12-04 22:39:46 -05005977 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005978 }
James Smart81301a92008-12-04 22:39:46 -05005979 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005980 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5981 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005982 "memory for hexdump\n");
5983 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005984 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5985 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005986 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005987 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005988 while (pagecnt) {
5989 _dump_buf_dif =
5990 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5991 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005992 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5993 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005994 "_dump_buf_dif at 0x%p\n",
5995 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005996 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005997 memset(_dump_buf_dif, 0,
5998 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005999 break;
James Smart3772a992009-05-22 14:50:54 -04006000 } else
James Smart81301a92008-12-04 22:39:46 -05006001 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006002 }
James Smart81301a92008-12-04 22:39:46 -05006003 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006004 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6005 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006006 "memory for hexdump\n");
6007 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006008 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6009 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006010 _dump_buf_dif);
6011}
6012
6013/**
6014 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6015 * @phba: pointer to lpfc hba data structure.
6016 *
6017 * This routine is invoked to perform all the necessary post initialization
6018 * setup for the device.
6019 **/
6020static void
6021lpfc_post_init_setup(struct lpfc_hba *phba)
6022{
6023 struct Scsi_Host *shost;
6024 struct lpfc_adapter_event_header adapter_event;
6025
6026 /* Get the default values for Model Name and Description */
6027 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6028
6029 /*
6030 * hba setup may have changed the hba_queue_depth so we need to
6031 * adjust the value of can_queue.
6032 */
6033 shost = pci_get_drvdata(phba->pcidev);
6034 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6035 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6036 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006037
6038 lpfc_host_attrib_init(shost);
6039
James Smart2e0fef82007-06-17 19:56:36 -05006040 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6041 spin_lock_irq(shost->host_lock);
6042 lpfc_poll_start_timer(phba);
6043 spin_unlock_irq(shost->host_lock);
6044 }
James Smart8f6d98d2006-08-01 07:34:00 -04006045
James Smart93996272008-08-24 21:50:30 -04006046 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6047 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006048 /* Send board arrival event to upper layer */
6049 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6050 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6051 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006052 sizeof(adapter_event),
6053 (char *) &adapter_event,
6054 LPFC_NL_VENDOR_ID);
6055 return;
6056}
6057
6058/**
6059 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6060 * @phba: pointer to lpfc hba data structure.
6061 *
6062 * This routine is invoked to set up the PCI device memory space for device
6063 * with SLI-3 interface spec.
6064 *
6065 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006066 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006067 * other values - error
6068 **/
6069static int
6070lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6071{
6072 struct pci_dev *pdev;
6073 unsigned long bar0map_len, bar2map_len;
6074 int i, hbq_count;
6075 void *ptr;
6076 int error = -ENODEV;
6077
6078 /* Obtain PCI device reference */
6079 if (!phba->pcidev)
6080 return error;
6081 else
6082 pdev = phba->pcidev;
6083
6084 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006085 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6086 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6087 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6088 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006089 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006090 }
6091 }
James Smart3772a992009-05-22 14:50:54 -04006092
6093 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6094 * required by each mapping.
6095 */
6096 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6097 bar0map_len = pci_resource_len(pdev, 0);
6098
6099 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6100 bar2map_len = pci_resource_len(pdev, 2);
6101
6102 /* Map HBA SLIM to a kernel virtual address. */
6103 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6104 if (!phba->slim_memmap_p) {
6105 dev_printk(KERN_ERR, &pdev->dev,
6106 "ioremap failed for SLIM memory.\n");
6107 goto out;
6108 }
6109
6110 /* Map HBA Control Registers to a kernel virtual address. */
6111 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6112 if (!phba->ctrl_regs_memmap_p) {
6113 dev_printk(KERN_ERR, &pdev->dev,
6114 "ioremap failed for HBA control registers.\n");
6115 goto out_iounmap_slim;
6116 }
6117
6118 /* Allocate memory for SLI-2 structures */
6119 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
6120 SLI2_SLIM_SIZE,
6121 &phba->slim2p.phys,
6122 GFP_KERNEL);
6123 if (!phba->slim2p.virt)
6124 goto out_iounmap;
6125
6126 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
6127 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006128 phba->mbox_ext = (phba->slim2p.virt +
6129 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006130 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6131 phba->IOCBs = (phba->slim2p.virt +
6132 offsetof(struct lpfc_sli2_slim, IOCBs));
6133
6134 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6135 lpfc_sli_hbq_size(),
6136 &phba->hbqslimp.phys,
6137 GFP_KERNEL);
6138 if (!phba->hbqslimp.virt)
6139 goto out_free_slim;
6140
6141 hbq_count = lpfc_sli_hbq_count();
6142 ptr = phba->hbqslimp.virt;
6143 for (i = 0; i < hbq_count; ++i) {
6144 phba->hbqs[i].hbq_virt = ptr;
6145 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6146 ptr += (lpfc_hbq_defs[i]->entry_count *
6147 sizeof(struct lpfc_hbq_entry));
6148 }
6149 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6150 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6151
6152 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6153
6154 INIT_LIST_HEAD(&phba->rb_pend_list);
6155
6156 phba->MBslimaddr = phba->slim_memmap_p;
6157 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6158 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6159 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6160 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006161
dea31012005-04-17 16:05:31 -05006162 return 0;
6163
dea31012005-04-17 16:05:31 -05006164out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006165 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6166 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006167out_iounmap:
6168 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006169out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006170 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006171out:
6172 return error;
6173}
6174
James Smarte59058c2008-08-24 21:49:00 -04006175/**
James Smart3772a992009-05-22 14:50:54 -04006176 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6177 * @phba: pointer to lpfc hba data structure.
6178 *
6179 * This routine is invoked to unset the PCI device memory space for device
6180 * with SLI-3 interface spec.
6181 **/
6182static void
6183lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6184{
6185 struct pci_dev *pdev;
6186
6187 /* Obtain PCI device reference */
6188 if (!phba->pcidev)
6189 return;
6190 else
6191 pdev = phba->pcidev;
6192
6193 /* Free coherent DMA memory allocated */
6194 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6195 phba->hbqslimp.virt, phba->hbqslimp.phys);
6196 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6197 phba->slim2p.virt, phba->slim2p.phys);
6198
6199 /* I/O memory unmap */
6200 iounmap(phba->ctrl_regs_memmap_p);
6201 iounmap(phba->slim_memmap_p);
6202
6203 return;
6204}
6205
6206/**
James Smartda0436e2009-05-22 14:51:39 -04006207 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6208 * @phba: pointer to lpfc hba data structure.
6209 *
6210 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6211 * done and check status.
6212 *
6213 * Return 0 if successful, otherwise -ENODEV.
6214 **/
6215int
6216lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6217{
James Smart2fcee4b2010-12-15 17:57:46 -05006218 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6219 struct lpfc_register reg_data;
6220 int i, port_error = 0;
6221 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006222
James Smart9940b972011-03-11 16:06:12 -05006223 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6224 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006225 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006226 return -ENODEV;
6227
James Smartda0436e2009-05-22 14:51:39 -04006228 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6229 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006230 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6231 &portsmphr_reg.word0) ||
6232 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006233 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006234 port_error = -ENODEV;
6235 break;
6236 }
James Smart2fcee4b2010-12-15 17:57:46 -05006237 if (LPFC_POST_STAGE_PORT_READY ==
6238 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006239 break;
James Smartda0436e2009-05-22 14:51:39 -04006240 msleep(10);
6241 }
6242
James Smart2fcee4b2010-12-15 17:57:46 -05006243 /*
6244 * If there was a port error during POST, then don't proceed with
6245 * other register reads as the data may not be valid. Just exit.
6246 */
6247 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006249 "1408 Port Failed POST - portsmphr=0x%x, "
6250 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6251 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6252 portsmphr_reg.word0,
6253 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6254 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6255 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6256 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6257 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6258 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6259 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6260 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6261 } else {
James Smart28baac72010-02-12 14:42:03 -05006262 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006263 "2534 Device Info: SLIFamily=0x%x, "
6264 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6265 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006266 bf_get(lpfc_sli_intf_sli_family,
6267 &phba->sli4_hba.sli_intf),
6268 bf_get(lpfc_sli_intf_slirev,
6269 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006270 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006271 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006272 bf_get(lpfc_sli_intf_sli_hint1,
6273 &phba->sli4_hba.sli_intf),
6274 bf_get(lpfc_sli_intf_sli_hint2,
6275 &phba->sli4_hba.sli_intf),
6276 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006277 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006278 /*
6279 * Check for other Port errors during the initialization
6280 * process. Fail the load if the port did not come up
6281 * correctly.
6282 */
6283 if_type = bf_get(lpfc_sli_intf_if_type,
6284 &phba->sli4_hba.sli_intf);
6285 switch (if_type) {
6286 case LPFC_SLI_INTF_IF_TYPE_0:
6287 phba->sli4_hba.ue_mask_lo =
6288 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6289 phba->sli4_hba.ue_mask_hi =
6290 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6291 uerrlo_reg.word0 =
6292 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6293 uerrhi_reg.word0 =
6294 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6295 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6296 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6298 "1422 Unrecoverable Error "
6299 "Detected during POST "
6300 "uerr_lo_reg=0x%x, "
6301 "uerr_hi_reg=0x%x, "
6302 "ue_mask_lo_reg=0x%x, "
6303 "ue_mask_hi_reg=0x%x\n",
6304 uerrlo_reg.word0,
6305 uerrhi_reg.word0,
6306 phba->sli4_hba.ue_mask_lo,
6307 phba->sli4_hba.ue_mask_hi);
6308 port_error = -ENODEV;
6309 }
6310 break;
6311 case LPFC_SLI_INTF_IF_TYPE_2:
6312 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006313 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6314 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006315 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6316 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006317 phba->work_status[0] =
6318 readl(phba->sli4_hba.u.if_type2.
6319 ERR1regaddr);
6320 phba->work_status[1] =
6321 readl(phba->sli4_hba.u.if_type2.
6322 ERR2regaddr);
6323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006324 "2888 Unrecoverable port error "
6325 "following POST: port status reg "
6326 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006327 "error 1=0x%x, error 2=0x%x\n",
6328 reg_data.word0,
6329 portsmphr_reg.word0,
6330 phba->work_status[0],
6331 phba->work_status[1]);
6332 port_error = -ENODEV;
6333 }
6334 break;
6335 case LPFC_SLI_INTF_IF_TYPE_1:
6336 default:
6337 break;
6338 }
James Smart28baac72010-02-12 14:42:03 -05006339 }
James Smartda0436e2009-05-22 14:51:39 -04006340 return port_error;
6341}
6342
6343/**
6344 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6345 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006346 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006347 *
6348 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6349 * memory map.
6350 **/
6351static void
James Smart2fcee4b2010-12-15 17:57:46 -05006352lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006353{
James Smart2fcee4b2010-12-15 17:57:46 -05006354 switch (if_type) {
6355 case LPFC_SLI_INTF_IF_TYPE_0:
6356 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6357 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6358 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6359 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6360 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6361 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6362 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6363 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6364 phba->sli4_hba.SLIINTFregaddr =
6365 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6366 break;
6367 case LPFC_SLI_INTF_IF_TYPE_2:
6368 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006369 phba->sli4_hba.conf_regs_memmap_p +
6370 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006371 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006372 phba->sli4_hba.conf_regs_memmap_p +
6373 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006374 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006375 phba->sli4_hba.conf_regs_memmap_p +
6376 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006377 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006378 phba->sli4_hba.conf_regs_memmap_p +
6379 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006380 phba->sli4_hba.SLIINTFregaddr =
6381 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6382 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006383 phba->sli4_hba.conf_regs_memmap_p +
6384 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006385 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006386 phba->sli4_hba.conf_regs_memmap_p +
6387 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006388 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006389 phba->sli4_hba.conf_regs_memmap_p +
6390 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006391 phba->sli4_hba.EQCQDBregaddr =
6392 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6393 phba->sli4_hba.MQDBregaddr =
6394 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6395 phba->sli4_hba.BMBXregaddr =
6396 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6397 break;
6398 case LPFC_SLI_INTF_IF_TYPE_1:
6399 default:
6400 dev_printk(KERN_ERR, &phba->pcidev->dev,
6401 "FATAL - unsupported SLI4 interface type - %d\n",
6402 if_type);
6403 break;
6404 }
James Smartda0436e2009-05-22 14:51:39 -04006405}
6406
6407/**
6408 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6409 * @phba: pointer to lpfc hba data structure.
6410 *
6411 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6412 * memory map.
6413 **/
6414static void
6415lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6416{
James Smart2fcee4b2010-12-15 17:57:46 -05006417 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6418 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006419 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006420 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006421 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006422 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006423 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006424 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006425}
6426
6427/**
6428 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6429 * @phba: pointer to lpfc hba data structure.
6430 * @vf: virtual function number
6431 *
6432 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6433 * based on the given viftual function number, @vf.
6434 *
6435 * Return 0 if successful, otherwise -ENODEV.
6436 **/
6437static int
6438lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6439{
6440 if (vf > LPFC_VIR_FUNC_MAX)
6441 return -ENODEV;
6442
6443 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006444 vf * LPFC_VFR_PAGE_SIZE +
6445 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006446 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006447 vf * LPFC_VFR_PAGE_SIZE +
6448 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006449 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6450 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6451 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6452 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6453 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6454 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6455 return 0;
6456}
6457
6458/**
6459 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6460 * @phba: pointer to lpfc hba data structure.
6461 *
6462 * This routine is invoked to create the bootstrap mailbox
6463 * region consistent with the SLI-4 interface spec. This
6464 * routine allocates all memory necessary to communicate
6465 * mailbox commands to the port and sets up all alignment
6466 * needs. No locks are expected to be held when calling
6467 * this routine.
6468 *
6469 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006470 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006471 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006472 **/
6473static int
6474lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6475{
6476 uint32_t bmbx_size;
6477 struct lpfc_dmabuf *dmabuf;
6478 struct dma_address *dma_address;
6479 uint32_t pa_addr;
6480 uint64_t phys_addr;
6481
6482 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6483 if (!dmabuf)
6484 return -ENOMEM;
6485
6486 /*
6487 * The bootstrap mailbox region is comprised of 2 parts
6488 * plus an alignment restriction of 16 bytes.
6489 */
6490 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6491 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6492 bmbx_size,
6493 &dmabuf->phys,
6494 GFP_KERNEL);
6495 if (!dmabuf->virt) {
6496 kfree(dmabuf);
6497 return -ENOMEM;
6498 }
6499 memset(dmabuf->virt, 0, bmbx_size);
6500
6501 /*
6502 * Initialize the bootstrap mailbox pointers now so that the register
6503 * operations are simple later. The mailbox dma address is required
6504 * to be 16-byte aligned. Also align the virtual memory as each
6505 * maibox is copied into the bmbx mailbox region before issuing the
6506 * command to the port.
6507 */
6508 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6509 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6510
6511 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6512 LPFC_ALIGN_16_BYTE);
6513 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6514 LPFC_ALIGN_16_BYTE);
6515
6516 /*
6517 * Set the high and low physical addresses now. The SLI4 alignment
6518 * requirement is 16 bytes and the mailbox is posted to the port
6519 * as two 30-bit addresses. The other data is a bit marking whether
6520 * the 30-bit address is the high or low address.
6521 * Upcast bmbx aphys to 64bits so shift instruction compiles
6522 * clean on 32 bit machines.
6523 */
6524 dma_address = &phba->sli4_hba.bmbx.dma_address;
6525 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6526 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6527 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6528 LPFC_BMBX_BIT1_ADDR_HI);
6529
6530 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6531 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6532 LPFC_BMBX_BIT1_ADDR_LO);
6533 return 0;
6534}
6535
6536/**
6537 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6538 * @phba: pointer to lpfc hba data structure.
6539 *
6540 * This routine is invoked to teardown the bootstrap mailbox
6541 * region and release all host resources. This routine requires
6542 * the caller to ensure all mailbox commands recovered, no
6543 * additional mailbox comands are sent, and interrupts are disabled
6544 * before calling this routine.
6545 *
6546 **/
6547static void
6548lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6549{
6550 dma_free_coherent(&phba->pcidev->dev,
6551 phba->sli4_hba.bmbx.bmbx_size,
6552 phba->sli4_hba.bmbx.dmabuf->virt,
6553 phba->sli4_hba.bmbx.dmabuf->phys);
6554
6555 kfree(phba->sli4_hba.bmbx.dmabuf);
6556 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6557}
6558
6559/**
6560 * lpfc_sli4_read_config - Get the config parameters.
6561 * @phba: pointer to lpfc hba data structure.
6562 *
6563 * This routine is invoked to read the configuration parameters from the HBA.
6564 * The configuration parameters are used to set the base and maximum values
6565 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6566 * allocation for the port.
6567 *
6568 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006569 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006570 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006571 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006572 **/
James Smartff78d8f2011-12-13 13:21:35 -05006573int
James Smartda0436e2009-05-22 14:51:39 -04006574lpfc_sli4_read_config(struct lpfc_hba *phba)
6575{
6576 LPFC_MBOXQ_t *pmb;
6577 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006578 union lpfc_sli4_cfg_shdr *shdr;
6579 uint32_t shdr_status, shdr_add_status;
6580 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6581 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006582 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006583 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006584 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006585
6586 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6587 if (!pmb) {
6588 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6589 "2011 Unable to allocate memory for issuing "
6590 "SLI_CONFIG_SPECIAL mailbox command\n");
6591 return -ENOMEM;
6592 }
6593
6594 lpfc_read_config(phba, pmb);
6595
6596 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6597 if (rc != MBX_SUCCESS) {
6598 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6599 "2012 Mailbox failed , mbxCmd x%x "
6600 "READ_CONFIG, mbxStatus x%x\n",
6601 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6602 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6603 rc = -EIO;
6604 } else {
6605 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006606 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6607 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6608 phba->sli4_hba.lnk_info.lnk_tp =
6609 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6610 phba->sli4_hba.lnk_info.lnk_no =
6611 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6612 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6613 "3081 lnk_type:%d, lnk_numb:%d\n",
6614 phba->sli4_hba.lnk_info.lnk_tp,
6615 phba->sli4_hba.lnk_info.lnk_no);
6616 } else
6617 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6618 "3082 Mailbox (x%x) returned ldv:x0\n",
6619 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006620 phba->sli4_hba.extents_in_use =
6621 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006622 phba->sli4_hba.max_cfg_param.max_xri =
6623 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6624 phba->sli4_hba.max_cfg_param.xri_base =
6625 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6626 phba->sli4_hba.max_cfg_param.max_vpi =
6627 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6628 phba->sli4_hba.max_cfg_param.vpi_base =
6629 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6630 phba->sli4_hba.max_cfg_param.max_rpi =
6631 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6632 phba->sli4_hba.max_cfg_param.rpi_base =
6633 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6634 phba->sli4_hba.max_cfg_param.max_vfi =
6635 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6636 phba->sli4_hba.max_cfg_param.vfi_base =
6637 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6638 phba->sli4_hba.max_cfg_param.max_fcfi =
6639 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006640 phba->sli4_hba.max_cfg_param.max_eq =
6641 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6642 phba->sli4_hba.max_cfg_param.max_rq =
6643 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6644 phba->sli4_hba.max_cfg_param.max_wq =
6645 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6646 phba->sli4_hba.max_cfg_param.max_cq =
6647 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6648 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6649 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6650 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6651 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006652 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6653 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006654 phba->max_vports = phba->max_vpi;
6655 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006656 "2003 cfg params Extents? %d "
6657 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006658 "VPI(B:%d M:%d) "
6659 "VFI(B:%d M:%d) "
6660 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006661 "FCFI(Count:%d)\n",
6662 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006663 phba->sli4_hba.max_cfg_param.xri_base,
6664 phba->sli4_hba.max_cfg_param.max_xri,
6665 phba->sli4_hba.max_cfg_param.vpi_base,
6666 phba->sli4_hba.max_cfg_param.max_vpi,
6667 phba->sli4_hba.max_cfg_param.vfi_base,
6668 phba->sli4_hba.max_cfg_param.max_vfi,
6669 phba->sli4_hba.max_cfg_param.rpi_base,
6670 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006671 phba->sli4_hba.max_cfg_param.max_fcfi);
6672 }
James Smart912e3ac2011-05-24 11:42:11 -04006673
6674 if (rc)
6675 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006676
6677 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04006678 if (phba->cfg_hba_queue_depth >
6679 (phba->sli4_hba.max_cfg_param.max_xri -
6680 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04006681 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04006682 phba->sli4_hba.max_cfg_param.max_xri -
6683 lpfc_sli4_get_els_iocb_cnt(phba);
James Smart912e3ac2011-05-24 11:42:11 -04006684
6685 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6686 LPFC_SLI_INTF_IF_TYPE_2)
6687 goto read_cfg_out;
6688
6689 /* get the pf# and vf# for SLI4 if_type 2 port */
6690 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6691 sizeof(struct lpfc_sli4_cfg_mhdr));
6692 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6693 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6694 length, LPFC_SLI4_MBX_EMBED);
6695
James Smart8aa134a2012-08-14 14:25:29 -04006696 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006697 shdr = (union lpfc_sli4_cfg_shdr *)
6698 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6699 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6700 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006701 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006702 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6703 "3026 Mailbox failed , mbxCmd x%x "
6704 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6705 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6706 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006707 goto read_cfg_out;
6708 }
6709
6710 /* search for fc_fcoe resrouce descriptor */
6711 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6712 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6713
James Smart8aa134a2012-08-14 14:25:29 -04006714 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6715 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6716 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6717 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6718 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6719 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6720 goto read_cfg_out;
6721
James Smart912e3ac2011-05-24 11:42:11 -04006722 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006723 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006724 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006725 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006726 phba->sli4_hba.iov.pf_number =
6727 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6728 phba->sli4_hba.iov.vf_number =
6729 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6730 break;
6731 }
6732 }
6733
6734 if (i < LPFC_RSRC_DESC_MAX_NUM)
6735 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6736 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6737 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6738 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006739 else
James Smart912e3ac2011-05-24 11:42:11 -04006740 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6741 "3028 GET_FUNCTION_CONFIG: failed to find "
6742 "Resrouce Descriptor:x%x\n",
6743 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006744
6745read_cfg_out:
6746 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006747 return rc;
6748}
6749
6750/**
James Smart2fcee4b2010-12-15 17:57:46 -05006751 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006752 * @phba: pointer to lpfc hba data structure.
6753 *
James Smart2fcee4b2010-12-15 17:57:46 -05006754 * This routine is invoked to setup the port-side endian order when
6755 * the port if_type is 0. This routine has no function for other
6756 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006757 *
6758 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006759 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006760 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006761 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006762 **/
6763static int
6764lpfc_setup_endian_order(struct lpfc_hba *phba)
6765{
6766 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006767 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006768 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6769 HOST_ENDIAN_HIGH_WORD1};
6770
James Smart2fcee4b2010-12-15 17:57:46 -05006771 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6772 switch (if_type) {
6773 case LPFC_SLI_INTF_IF_TYPE_0:
6774 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6775 GFP_KERNEL);
6776 if (!mboxq) {
6777 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6778 "0492 Unable to allocate memory for "
6779 "issuing SLI_CONFIG_SPECIAL mailbox "
6780 "command\n");
6781 return -ENOMEM;
6782 }
James Smartda0436e2009-05-22 14:51:39 -04006783
James Smart2fcee4b2010-12-15 17:57:46 -05006784 /*
6785 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6786 * two words to contain special data values and no other data.
6787 */
6788 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6789 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6790 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6791 if (rc != MBX_SUCCESS) {
6792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6793 "0493 SLI_CONFIG_SPECIAL mailbox "
6794 "failed with status x%x\n",
6795 rc);
6796 rc = -EIO;
6797 }
6798 mempool_free(mboxq, phba->mbox_mem_pool);
6799 break;
6800 case LPFC_SLI_INTF_IF_TYPE_2:
6801 case LPFC_SLI_INTF_IF_TYPE_1:
6802 default:
6803 break;
James Smartda0436e2009-05-22 14:51:39 -04006804 }
James Smartda0436e2009-05-22 14:51:39 -04006805 return rc;
6806}
6807
6808/**
James Smart5350d872011-10-10 21:33:49 -04006809 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006810 * @phba: pointer to lpfc hba data structure.
6811 *
James Smart5350d872011-10-10 21:33:49 -04006812 * This routine is invoked to check the user settable queue counts for EQs and
6813 * CQs. after this routine is called the counts will be set to valid values that
6814 * adhere to the constraints of the system's interrupt vectors and the port's
6815 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006816 *
6817 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006818 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006819 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006820 **/
6821static int
James Smart5350d872011-10-10 21:33:49 -04006822lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006823{
James Smart67d12732012-08-03 12:36:13 -04006824 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006825 uint32_t cpu;
6826 uint32_t i = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006827 uint32_t j = 0;
James Smart90695ee2012-08-14 14:25:36 -04006828
James Smartda0436e2009-05-22 14:51:39 -04006829
6830 /*
James Smart67d12732012-08-03 12:36:13 -04006831 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006832 * device parameters
6833 */
6834
James Smart67d12732012-08-03 12:36:13 -04006835 /* Sanity check on HBA EQ parameters */
6836 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006837
James Smart7bb03bb2013-04-17 20:19:16 -04006838 /* It doesn't make sense to have more io channels then online CPUs */
6839 for_each_present_cpu(cpu) {
6840 if (cpu_online(cpu))
6841 i++;
6842 j++;
James Smart90695ee2012-08-14 14:25:36 -04006843 }
James Smart7bb03bb2013-04-17 20:19:16 -04006844 phba->sli4_hba.num_online_cpu = i;
6845 phba->sli4_hba.num_present_cpu = j;
6846
James Smart90695ee2012-08-14 14:25:36 -04006847 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006848 lpfc_printf_log(phba,
6849 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006850 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04006851 "online CPUs: from %d to %d\n",
6852 cfg_fcp_io_channel, i);
James Smart90695ee2012-08-14 14:25:36 -04006853 cfg_fcp_io_channel = i;
6854 }
6855
James Smart67d12732012-08-03 12:36:13 -04006856 if (cfg_fcp_io_channel >
6857 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006858 if (phba->sli4_hba.max_cfg_param.max_eq <
6859 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04006860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6861 "2574 Not enough EQs (%d) from the "
6862 "pci function for supporting FCP "
6863 "EQs (%d)\n",
6864 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04006865 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04006866 goto out_error;
6867 }
James Smart82c3e9b2012-09-29 11:29:50 -04006868 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6869 "2575 Reducing IO channels to match number of "
6870 "available EQs: from %d to %d\n",
6871 cfg_fcp_io_channel,
6872 phba->sli4_hba.max_cfg_param.max_eq);
6873 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq;
James Smartda0436e2009-05-22 14:51:39 -04006874 }
James Smart67d12732012-08-03 12:36:13 -04006875
6876 /* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
6877
James Smartda0436e2009-05-22 14:51:39 -04006878 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04006879 phba->cfg_fcp_eq_count = cfg_fcp_io_channel;
6880 phba->cfg_fcp_wq_count = cfg_fcp_io_channel;
6881 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006882
James Smartda0436e2009-05-22 14:51:39 -04006883 /* Get EQ depth from module parameter, fake the default for now */
6884 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6885 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6886
James Smart5350d872011-10-10 21:33:49 -04006887 /* Get CQ depth from module parameter, fake the default for now */
6888 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6889 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6890
6891 return 0;
6892out_error:
6893 return -ENOMEM;
6894}
6895
6896/**
6897 * lpfc_sli4_queue_create - Create all the SLI4 queues
6898 * @phba: pointer to lpfc hba data structure.
6899 *
6900 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6901 * operation. For each SLI4 queue type, the parameters such as queue entry
6902 * count (queue depth) shall be taken from the module parameter. For now,
6903 * we just use some constant number as place holder.
6904 *
6905 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07006906 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04006907 * -ENOMEM - No availble memory
6908 * -EIO - The mailbox failed to complete successfully.
6909 **/
6910int
6911lpfc_sli4_queue_create(struct lpfc_hba *phba)
6912{
6913 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04006914 int idx;
James Smart5350d872011-10-10 21:33:49 -04006915
6916 /*
James Smart67d12732012-08-03 12:36:13 -04006917 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04006918 */
James Smart67d12732012-08-03 12:36:13 -04006919 if (!phba->cfg_fcp_io_channel)
6920 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04006921
James Smart67d12732012-08-03 12:36:13 -04006922 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6923 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6924 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6925 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6926 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6927 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6928
6929 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
6930 phba->cfg_fcp_io_channel), GFP_KERNEL);
6931 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04006932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006933 "2576 Failed allocate memory for "
6934 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04006935 goto out_error;
6936 }
James Smart67d12732012-08-03 12:36:13 -04006937
6938 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6939 phba->cfg_fcp_io_channel), GFP_KERNEL);
6940 if (!phba->sli4_hba.fcp_cq) {
6941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6942 "2577 Failed allocate memory for fast-path "
6943 "CQ record array\n");
6944 goto out_error;
6945 }
6946
6947 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6948 phba->cfg_fcp_io_channel), GFP_KERNEL);
6949 if (!phba->sli4_hba.fcp_wq) {
6950 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6951 "2578 Failed allocate memory for fast-path "
6952 "WQ record array\n");
6953 goto out_error;
6954 }
James Smartda0436e2009-05-22 14:51:39 -04006955
James Smart5350d872011-10-10 21:33:49 -04006956 /*
James Smart67d12732012-08-03 12:36:13 -04006957 * Since the first EQ can have multiple CQs associated with it,
6958 * this array is used to quickly see if we have a FCP fast-path
6959 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04006960 */
James Smart67d12732012-08-03 12:36:13 -04006961 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
6962 phba->cfg_fcp_io_channel), GFP_KERNEL);
6963 if (!phba->sli4_hba.fcp_cq_map) {
6964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6965 "2545 Failed allocate memory for fast-path "
6966 "CQ map\n");
6967 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006968 }
James Smart67d12732012-08-03 12:36:13 -04006969
6970 /*
6971 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
6972 * how many EQs to create.
6973 */
6974 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6975
6976 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04006977 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6978 phba->sli4_hba.eq_ecount);
6979 if (!qdesc) {
6980 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006981 "0497 Failed allocate EQ (%d)\n", idx);
6982 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006983 }
James Smart67d12732012-08-03 12:36:13 -04006984 phba->sli4_hba.hba_eq[idx] = qdesc;
6985
6986 /* Create Fast Path FCP CQs */
6987 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6988 phba->sli4_hba.cq_ecount);
6989 if (!qdesc) {
6990 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6991 "0499 Failed allocate fast-path FCP "
6992 "CQ (%d)\n", idx);
6993 goto out_error;
6994 }
6995 phba->sli4_hba.fcp_cq[idx] = qdesc;
6996
6997 /* Create Fast Path FCP WQs */
6998 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6999 phba->sli4_hba.wq_ecount);
7000 if (!qdesc) {
7001 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7002 "0503 Failed allocate fast-path FCP "
7003 "WQ (%d)\n", idx);
7004 goto out_error;
7005 }
7006 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007007 }
7008
James Smart67d12732012-08-03 12:36:13 -04007009
James Smartda0436e2009-05-22 14:51:39 -04007010 /*
James Smart67d12732012-08-03 12:36:13 -04007011 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007012 */
7013
James Smartda0436e2009-05-22 14:51:39 -04007014 /* Create slow-path Mailbox Command Complete Queue */
7015 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7016 phba->sli4_hba.cq_ecount);
7017 if (!qdesc) {
7018 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7019 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007020 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007021 }
7022 phba->sli4_hba.mbx_cq = qdesc;
7023
7024 /* Create slow-path ELS Complete Queue */
7025 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7026 phba->sli4_hba.cq_ecount);
7027 if (!qdesc) {
7028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7029 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007030 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007031 }
7032 phba->sli4_hba.els_cq = qdesc;
7033
James Smartda0436e2009-05-22 14:51:39 -04007034
James Smart5350d872011-10-10 21:33:49 -04007035 /*
James Smart67d12732012-08-03 12:36:13 -04007036 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007037 */
James Smartda0436e2009-05-22 14:51:39 -04007038
7039 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007040
7041 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7042 phba->sli4_hba.mq_ecount);
7043 if (!qdesc) {
7044 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7045 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007046 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007047 }
7048 phba->sli4_hba.mbx_wq = qdesc;
7049
7050 /*
James Smart67d12732012-08-03 12:36:13 -04007051 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007052 */
James Smartda0436e2009-05-22 14:51:39 -04007053
7054 /* Create slow-path ELS Work Queue */
7055 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7056 phba->sli4_hba.wq_ecount);
7057 if (!qdesc) {
7058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7059 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007060 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007061 }
7062 phba->sli4_hba.els_wq = qdesc;
7063
James Smartda0436e2009-05-22 14:51:39 -04007064 /*
7065 * Create Receive Queue (RQ)
7066 */
James Smartda0436e2009-05-22 14:51:39 -04007067
7068 /* Create Receive Queue for header */
7069 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7070 phba->sli4_hba.rq_ecount);
7071 if (!qdesc) {
7072 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7073 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007074 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007075 }
7076 phba->sli4_hba.hdr_rq = qdesc;
7077
7078 /* Create Receive Queue for data */
7079 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7080 phba->sli4_hba.rq_ecount);
7081 if (!qdesc) {
7082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7083 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007084 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007085 }
7086 phba->sli4_hba.dat_rq = qdesc;
7087
7088 return 0;
7089
James Smartda0436e2009-05-22 14:51:39 -04007090out_error:
James Smart67d12732012-08-03 12:36:13 -04007091 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007092 return -ENOMEM;
7093}
7094
7095/**
7096 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7097 * @phba: pointer to lpfc hba data structure.
7098 *
7099 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7100 * operation.
7101 *
7102 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007103 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007104 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007105 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007106 **/
James Smart5350d872011-10-10 21:33:49 -04007107void
James Smartda0436e2009-05-22 14:51:39 -04007108lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7109{
James Smart67d12732012-08-03 12:36:13 -04007110 int idx;
7111
7112 if (phba->sli4_hba.hba_eq != NULL) {
7113 /* Release HBA event queue */
7114 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7115 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7116 lpfc_sli4_queue_free(
7117 phba->sli4_hba.hba_eq[idx]);
7118 phba->sli4_hba.hba_eq[idx] = NULL;
7119 }
7120 }
7121 kfree(phba->sli4_hba.hba_eq);
7122 phba->sli4_hba.hba_eq = NULL;
7123 }
7124
7125 if (phba->sli4_hba.fcp_cq != NULL) {
7126 /* Release FCP completion queue */
7127 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7128 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7129 lpfc_sli4_queue_free(
7130 phba->sli4_hba.fcp_cq[idx]);
7131 phba->sli4_hba.fcp_cq[idx] = NULL;
7132 }
7133 }
7134 kfree(phba->sli4_hba.fcp_cq);
7135 phba->sli4_hba.fcp_cq = NULL;
7136 }
7137
7138 if (phba->sli4_hba.fcp_wq != NULL) {
7139 /* Release FCP work queue */
7140 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7141 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7142 lpfc_sli4_queue_free(
7143 phba->sli4_hba.fcp_wq[idx]);
7144 phba->sli4_hba.fcp_wq[idx] = NULL;
7145 }
7146 }
7147 kfree(phba->sli4_hba.fcp_wq);
7148 phba->sli4_hba.fcp_wq = NULL;
7149 }
7150
James Smart962bc512013-01-03 15:44:00 -05007151 if (phba->pci_bar0_memmap_p) {
7152 iounmap(phba->pci_bar0_memmap_p);
7153 phba->pci_bar0_memmap_p = NULL;
7154 }
7155 if (phba->pci_bar2_memmap_p) {
7156 iounmap(phba->pci_bar2_memmap_p);
7157 phba->pci_bar2_memmap_p = NULL;
7158 }
7159 if (phba->pci_bar4_memmap_p) {
7160 iounmap(phba->pci_bar4_memmap_p);
7161 phba->pci_bar4_memmap_p = NULL;
7162 }
7163
James Smart67d12732012-08-03 12:36:13 -04007164 /* Release FCP CQ mapping array */
7165 if (phba->sli4_hba.fcp_cq_map != NULL) {
7166 kfree(phba->sli4_hba.fcp_cq_map);
7167 phba->sli4_hba.fcp_cq_map = NULL;
7168 }
James Smartda0436e2009-05-22 14:51:39 -04007169
7170 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007171 if (phba->sli4_hba.mbx_wq != NULL) {
7172 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7173 phba->sli4_hba.mbx_wq = NULL;
7174 }
James Smartda0436e2009-05-22 14:51:39 -04007175
7176 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007177 if (phba->sli4_hba.els_wq != NULL) {
7178 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7179 phba->sli4_hba.els_wq = NULL;
7180 }
James Smartda0436e2009-05-22 14:51:39 -04007181
7182 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007183 if (phba->sli4_hba.hdr_rq != NULL) {
7184 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7185 phba->sli4_hba.hdr_rq = NULL;
7186 }
7187 if (phba->sli4_hba.dat_rq != NULL) {
7188 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7189 phba->sli4_hba.dat_rq = NULL;
7190 }
James Smartda0436e2009-05-22 14:51:39 -04007191
James Smartda0436e2009-05-22 14:51:39 -04007192 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007193 if (phba->sli4_hba.els_cq != NULL) {
7194 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7195 phba->sli4_hba.els_cq = NULL;
7196 }
James Smartda0436e2009-05-22 14:51:39 -04007197
7198 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007199 if (phba->sli4_hba.mbx_cq != NULL) {
7200 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7201 phba->sli4_hba.mbx_cq = NULL;
7202 }
James Smartda0436e2009-05-22 14:51:39 -04007203
7204 return;
7205}
7206
7207/**
7208 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7209 * @phba: pointer to lpfc hba data structure.
7210 *
7211 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7212 * operation.
7213 *
7214 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007215 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007216 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007217 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007218 **/
7219int
7220lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7221{
James Smart2a76a282012-08-03 12:35:54 -04007222 struct lpfc_sli *psli = &phba->sli;
7223 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007224 int rc = -ENOMEM;
7225 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7226 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007227 uint32_t shdr_status, shdr_add_status;
7228 union lpfc_sli4_cfg_shdr *shdr;
7229 LPFC_MBOXQ_t *mboxq;
7230 uint32_t length;
7231
7232 /* Check for dual-ULP support */
7233 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7234 if (!mboxq) {
7235 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7236 "3249 Unable to allocate memory for "
7237 "QUERY_FW_CFG mailbox command\n");
7238 return -ENOMEM;
7239 }
7240 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7241 sizeof(struct lpfc_sli4_cfg_mhdr));
7242 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7243 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7244 length, LPFC_SLI4_MBX_EMBED);
7245
7246 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7247
7248 shdr = (union lpfc_sli4_cfg_shdr *)
7249 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7250 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7251 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7252 if (shdr_status || shdr_add_status || rc) {
7253 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7254 "3250 QUERY_FW_CFG mailbox failed with status "
7255 "x%x add_status x%x, mbx status x%x\n",
7256 shdr_status, shdr_add_status, rc);
7257 if (rc != MBX_TIMEOUT)
7258 mempool_free(mboxq, phba->mbox_mem_pool);
7259 rc = -ENXIO;
7260 goto out_error;
7261 }
7262
7263 phba->sli4_hba.fw_func_mode =
7264 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7265 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7266 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7267 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7268 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7269 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7270 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7271
7272 if (rc != MBX_TIMEOUT)
7273 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007274
7275 /*
James Smart67d12732012-08-03 12:36:13 -04007276 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007277 */
7278
James Smart67d12732012-08-03 12:36:13 -04007279 /* Set up HBA event queue */
7280 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7282 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007283 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007284 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007285 }
James Smart67d12732012-08-03 12:36:13 -04007286 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7287 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7289 "0522 Fast-path EQ (%d) not "
7290 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007291 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007292 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007293 }
James Smart67d12732012-08-03 12:36:13 -04007294 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007295 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007296 if (rc) {
7297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7298 "0523 Failed setup of fast-path EQ "
7299 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
James Smart67d12732012-08-03 12:36:13 -04007300 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007301 }
7302 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007303 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007304 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007305 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007306 }
7307
James Smart67d12732012-08-03 12:36:13 -04007308 /* Set up fast-path FCP Response Complete Queue */
7309 if (!phba->sli4_hba.fcp_cq) {
7310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7311 "3148 Fast-path FCP CQ array not "
7312 "allocated\n");
7313 rc = -ENOMEM;
7314 goto out_destroy_hba_eq;
7315 }
7316
7317 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7318 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7320 "0526 Fast-path FCP CQ (%d) not "
7321 "allocated\n", fcp_cqidx);
7322 rc = -ENOMEM;
7323 goto out_destroy_fcp_cq;
7324 }
7325 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7326 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7327 if (rc) {
7328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7329 "0527 Failed setup of fast-path FCP "
7330 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7331 goto out_destroy_fcp_cq;
7332 }
7333
7334 /* Setup fcp_cq_map for fast lookup */
7335 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7336 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7337
7338 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7339 "2588 FCP CQ setup: cq[%d]-id=%d, "
7340 "parent seq[%d]-id=%d\n",
7341 fcp_cqidx,
7342 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7343 fcp_cqidx,
7344 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7345 }
7346
7347 /* Set up fast-path FCP Work Queue */
7348 if (!phba->sli4_hba.fcp_wq) {
7349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7350 "3149 Fast-path FCP WQ array not "
7351 "allocated\n");
7352 rc = -ENOMEM;
7353 goto out_destroy_fcp_cq;
7354 }
7355
7356 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7357 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7358 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7359 "0534 Fast-path FCP WQ (%d) not "
7360 "allocated\n", fcp_wqidx);
7361 rc = -ENOMEM;
7362 goto out_destroy_fcp_wq;
7363 }
7364 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7365 phba->sli4_hba.fcp_cq[fcp_wqidx],
7366 LPFC_FCP);
7367 if (rc) {
7368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7369 "0535 Failed setup of fast-path FCP "
7370 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7371 goto out_destroy_fcp_wq;
7372 }
7373
7374 /* Bind this WQ to the next FCP ring */
7375 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7376 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7377 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7378
7379 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7380 "2591 FCP WQ setup: wq[%d]-id=%d, "
7381 "parent cq[%d]-id=%d\n",
7382 fcp_wqidx,
7383 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7384 fcp_cq_index,
7385 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7386 }
James Smartda0436e2009-05-22 14:51:39 -04007387 /*
7388 * Set up Complete Queues (CQs)
7389 */
7390
7391 /* Set up slow-path MBOX Complete Queue as the first CQ */
7392 if (!phba->sli4_hba.mbx_cq) {
7393 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7394 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007395 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007396 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007397 }
James Smart67d12732012-08-03 12:36:13 -04007398 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7399 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007400 if (rc) {
7401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7402 "0529 Failed setup of slow-path mailbox CQ: "
7403 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007404 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007405 }
7406 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7407 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7408 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007409 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007410
7411 /* Set up slow-path ELS Complete Queue */
7412 if (!phba->sli4_hba.els_cq) {
7413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7414 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007415 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007416 goto out_destroy_mbx_cq;
7417 }
James Smart67d12732012-08-03 12:36:13 -04007418 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7419 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007420 if (rc) {
7421 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7422 "0531 Failed setup of slow-path ELS CQ: "
7423 "rc = 0x%x\n", rc);
7424 goto out_destroy_mbx_cq;
7425 }
7426 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7427 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7428 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007429 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007430
7431 /*
7432 * Set up all the Work Queues (WQs)
7433 */
7434
7435 /* Set up Mailbox Command Queue */
7436 if (!phba->sli4_hba.mbx_wq) {
7437 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7438 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007439 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007440 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007441 }
7442 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7443 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7444 if (rc) {
7445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7446 "0539 Failed setup of slow-path MQ: "
7447 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007448 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007449 }
7450 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7451 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7452 phba->sli4_hba.mbx_wq->queue_id,
7453 phba->sli4_hba.mbx_cq->queue_id);
7454
7455 /* Set up slow-path ELS Work Queue */
7456 if (!phba->sli4_hba.els_wq) {
7457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7458 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007459 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007460 goto out_destroy_mbx_wq;
7461 }
7462 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7463 phba->sli4_hba.els_cq, LPFC_ELS);
7464 if (rc) {
7465 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7466 "0537 Failed setup of slow-path ELS WQ: "
7467 "rc = 0x%x\n", rc);
7468 goto out_destroy_mbx_wq;
7469 }
James Smart2a76a282012-08-03 12:35:54 -04007470
7471 /* Bind this WQ to the ELS ring */
7472 pring = &psli->ring[LPFC_ELS_RING];
7473 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7474 phba->sli4_hba.els_cq->pring = pring;
7475
James Smartda0436e2009-05-22 14:51:39 -04007476 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7477 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7478 phba->sli4_hba.els_wq->queue_id,
7479 phba->sli4_hba.els_cq->queue_id);
7480
James Smartda0436e2009-05-22 14:51:39 -04007481 /*
7482 * Create Receive Queue (RQ)
7483 */
7484 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7486 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007487 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007488 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007489 }
James Smart73d91e52011-10-10 21:32:10 -04007490
7491 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7492 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7493
James Smartda0436e2009-05-22 14:51:39 -04007494 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007495 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007496 if (rc) {
7497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7498 "0541 Failed setup of Receive Queue: "
7499 "rc = 0x%x\n", rc);
7500 goto out_destroy_fcp_wq;
7501 }
James Smart73d91e52011-10-10 21:32:10 -04007502
James Smartda0436e2009-05-22 14:51:39 -04007503 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7504 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7505 "parent cq-id=%d\n",
7506 phba->sli4_hba.hdr_rq->queue_id,
7507 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007508 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007509 return 0;
7510
James Smart2e90f4b2011-12-13 13:22:37 -05007511out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007512 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7513out_destroy_mbx_wq:
7514 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007515out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007516 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7517out_destroy_mbx_cq:
7518 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007519out_destroy_fcp_wq:
7520 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7521 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7522out_destroy_fcp_cq:
7523 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7524 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7525out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007526 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007527 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007528out_error:
7529 return rc;
7530}
7531
7532/**
7533 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7534 * @phba: pointer to lpfc hba data structure.
7535 *
7536 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7537 * operation.
7538 *
7539 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007540 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007541 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007542 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007543 **/
7544void
7545lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7546{
7547 int fcp_qidx;
7548
7549 /* Unset mailbox command work queue */
7550 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7551 /* Unset ELS work queue */
7552 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7553 /* Unset unsolicited receive queue */
7554 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7555 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007556 if (phba->sli4_hba.fcp_wq) {
7557 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7558 fcp_qidx++)
7559 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7560 }
James Smartda0436e2009-05-22 14:51:39 -04007561 /* Unset mailbox command complete queue */
7562 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7563 /* Unset ELS complete queue */
7564 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007565 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007566 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007567 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7568 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007569 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007570 }
James Smartda0436e2009-05-22 14:51:39 -04007571 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007572 if (phba->sli4_hba.hba_eq) {
7573 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007574 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007575 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007576 }
James Smartda0436e2009-05-22 14:51:39 -04007577}
7578
7579/**
7580 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7581 * @phba: pointer to lpfc hba data structure.
7582 *
7583 * This routine is invoked to allocate and set up a pool of completion queue
7584 * events. The body of the completion queue event is a completion queue entry
7585 * CQE. For now, this pool is used for the interrupt service routine to queue
7586 * the following HBA completion queue events for the worker thread to process:
7587 * - Mailbox asynchronous events
7588 * - Receive queue completion unsolicited events
7589 * Later, this can be used for all the slow-path events.
7590 *
7591 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007592 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007593 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007594 **/
7595static int
7596lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7597{
7598 struct lpfc_cq_event *cq_event;
7599 int i;
7600
7601 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7602 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7603 if (!cq_event)
7604 goto out_pool_create_fail;
7605 list_add_tail(&cq_event->list,
7606 &phba->sli4_hba.sp_cqe_event_pool);
7607 }
7608 return 0;
7609
7610out_pool_create_fail:
7611 lpfc_sli4_cq_event_pool_destroy(phba);
7612 return -ENOMEM;
7613}
7614
7615/**
7616 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7617 * @phba: pointer to lpfc hba data structure.
7618 *
7619 * This routine is invoked to free the pool of completion queue events at
7620 * driver unload time. Note that, it is the responsibility of the driver
7621 * cleanup routine to free all the outstanding completion-queue events
7622 * allocated from this pool back into the pool before invoking this routine
7623 * to destroy the pool.
7624 **/
7625static void
7626lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7627{
7628 struct lpfc_cq_event *cq_event, *next_cq_event;
7629
7630 list_for_each_entry_safe(cq_event, next_cq_event,
7631 &phba->sli4_hba.sp_cqe_event_pool, list) {
7632 list_del(&cq_event->list);
7633 kfree(cq_event);
7634 }
7635}
7636
7637/**
7638 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7639 * @phba: pointer to lpfc hba data structure.
7640 *
7641 * This routine is the lock free version of the API invoked to allocate a
7642 * completion-queue event from the free pool.
7643 *
7644 * Return: Pointer to the newly allocated completion-queue event if successful
7645 * NULL otherwise.
7646 **/
7647struct lpfc_cq_event *
7648__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7649{
7650 struct lpfc_cq_event *cq_event = NULL;
7651
7652 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7653 struct lpfc_cq_event, list);
7654 return cq_event;
7655}
7656
7657/**
7658 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7659 * @phba: pointer to lpfc hba data structure.
7660 *
7661 * This routine is the lock version of the API invoked to allocate a
7662 * completion-queue event from the free pool.
7663 *
7664 * Return: Pointer to the newly allocated completion-queue event if successful
7665 * NULL otherwise.
7666 **/
7667struct lpfc_cq_event *
7668lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7669{
7670 struct lpfc_cq_event *cq_event;
7671 unsigned long iflags;
7672
7673 spin_lock_irqsave(&phba->hbalock, iflags);
7674 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7675 spin_unlock_irqrestore(&phba->hbalock, iflags);
7676 return cq_event;
7677}
7678
7679/**
7680 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7681 * @phba: pointer to lpfc hba data structure.
7682 * @cq_event: pointer to the completion queue event to be freed.
7683 *
7684 * This routine is the lock free version of the API invoked to release a
7685 * completion-queue event back into the free pool.
7686 **/
7687void
7688__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7689 struct lpfc_cq_event *cq_event)
7690{
7691 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7692}
7693
7694/**
7695 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7696 * @phba: pointer to lpfc hba data structure.
7697 * @cq_event: pointer to the completion queue event to be freed.
7698 *
7699 * This routine is the lock version of the API invoked to release a
7700 * completion-queue event back into the free pool.
7701 **/
7702void
7703lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7704 struct lpfc_cq_event *cq_event)
7705{
7706 unsigned long iflags;
7707 spin_lock_irqsave(&phba->hbalock, iflags);
7708 __lpfc_sli4_cq_event_release(phba, cq_event);
7709 spin_unlock_irqrestore(&phba->hbalock, iflags);
7710}
7711
7712/**
7713 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7714 * @phba: pointer to lpfc hba data structure.
7715 *
7716 * This routine is to free all the pending completion-queue events to the
7717 * back into the free pool for device reset.
7718 **/
7719static void
7720lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7721{
7722 LIST_HEAD(cqelist);
7723 struct lpfc_cq_event *cqe;
7724 unsigned long iflags;
7725
7726 /* Retrieve all the pending WCQEs from pending WCQE lists */
7727 spin_lock_irqsave(&phba->hbalock, iflags);
7728 /* Pending FCP XRI abort events */
7729 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7730 &cqelist);
7731 /* Pending ELS XRI abort events */
7732 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7733 &cqelist);
7734 /* Pending asynnc events */
7735 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7736 &cqelist);
7737 spin_unlock_irqrestore(&phba->hbalock, iflags);
7738
7739 while (!list_empty(&cqelist)) {
7740 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7741 lpfc_sli4_cq_event_release(phba, cqe);
7742 }
7743}
7744
7745/**
7746 * lpfc_pci_function_reset - Reset pci function.
7747 * @phba: pointer to lpfc hba data structure.
7748 *
7749 * This routine is invoked to request a PCI function reset. It will destroys
7750 * all resources assigned to the PCI function which originates this request.
7751 *
7752 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007753 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007754 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007755 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007756 **/
7757int
7758lpfc_pci_function_reset(struct lpfc_hba *phba)
7759{
7760 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007761 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007762 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007763 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007764 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007765 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007766 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007767
James Smart2fcee4b2010-12-15 17:57:46 -05007768 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7769 switch (if_type) {
7770 case LPFC_SLI_INTF_IF_TYPE_0:
7771 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7772 GFP_KERNEL);
7773 if (!mboxq) {
7774 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7775 "0494 Unable to allocate memory for "
7776 "issuing SLI_FUNCTION_RESET mailbox "
7777 "command\n");
7778 return -ENOMEM;
7779 }
7780
7781 /* Setup PCI function reset mailbox-ioctl command */
7782 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7783 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7784 LPFC_SLI4_MBX_EMBED);
7785 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7786 shdr = (union lpfc_sli4_cfg_shdr *)
7787 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7788 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7789 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7790 &shdr->response);
7791 if (rc != MBX_TIMEOUT)
7792 mempool_free(mboxq, phba->mbox_mem_pool);
7793 if (shdr_status || shdr_add_status || rc) {
7794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7795 "0495 SLI_FUNCTION_RESET mailbox "
7796 "failed with status x%x add_status x%x,"
7797 " mbx status x%x\n",
7798 shdr_status, shdr_add_status, rc);
7799 rc = -ENXIO;
7800 }
7801 break;
7802 case LPFC_SLI_INTF_IF_TYPE_2:
7803 for (num_resets = 0;
7804 num_resets < MAX_IF_TYPE_2_RESETS;
7805 num_resets++) {
7806 reg_data.word0 = 0;
7807 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7808 LPFC_SLIPORT_LITTLE_ENDIAN);
7809 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7810 LPFC_SLIPORT_INIT_PORT);
7811 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7812 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007813 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007814 pci_read_config_word(phba->pcidev,
7815 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007816 /*
7817 * Poll the Port Status Register and wait for RDY for
7818 * up to 10 seconds. If the port doesn't respond, treat
7819 * it as an error. If the port responds with RN, start
7820 * the loop again.
7821 */
7822 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007823 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007824 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7825 STATUSregaddr, &reg_data.word0)) {
7826 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007827 goto out;
James Smart9940b972011-03-11 16:06:12 -05007828 }
James Smart6b5151f2012-01-18 16:24:06 -05007829 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7830 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007831 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7832 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007833 }
7834
7835 /*
7836 * If the port responds to the init request with
7837 * reset needed, delay for a bit and restart the loop.
7838 */
James Smart6b5151f2012-01-18 16:24:06 -05007839 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007840 msleep(10);
7841 reset_again = 0;
7842 continue;
7843 }
7844
7845 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007846 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7847 (rdy_chk >= 1000)) {
7848 phba->work_status[0] = readl(
7849 phba->sli4_hba.u.if_type2.ERR1regaddr);
7850 phba->work_status[1] = readl(
7851 phba->sli4_hba.u.if_type2.ERR2regaddr);
7852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007853 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007854 "reset(%d): wait_tmo:%d ms, "
7855 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007856 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007857 num_resets, rdy_chk*10,
7858 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007859 phba->work_status[0],
7860 phba->work_status[1]);
7861 rc = -ENODEV;
7862 }
7863
7864 /*
7865 * Terminate the outer loop provided the Port indicated
7866 * ready within 10 seconds.
7867 */
7868 if (rdy_chk < 1000)
7869 break;
7870 }
James Smart05580562011-05-24 11:40:48 -04007871 /* delay driver action following IF_TYPE_2 function reset */
7872 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007873 break;
7874 case LPFC_SLI_INTF_IF_TYPE_1:
7875 default:
7876 break;
James Smartda0436e2009-05-22 14:51:39 -04007877 }
7878
James Smart73d91e52011-10-10 21:32:10 -04007879out:
James Smart2fcee4b2010-12-15 17:57:46 -05007880 /* Catch the not-ready port failure after a port reset. */
James Smart229adb02013-04-17 20:16:51 -04007881 if (num_resets >= MAX_IF_TYPE_2_RESETS) {
7882 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7883 "3317 HBA not functional: IP Reset Failed "
7884 "after (%d) retries, try: "
7885 "echo fw_reset > board_mode\n", num_resets);
James Smart2fcee4b2010-12-15 17:57:46 -05007886 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04007887 }
James Smart2fcee4b2010-12-15 17:57:46 -05007888
James Smartda0436e2009-05-22 14:51:39 -04007889 return rc;
7890}
7891
7892/**
James Smartda0436e2009-05-22 14:51:39 -04007893 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7894 * @phba: pointer to lpfc hba data structure.
7895 *
7896 * This routine is invoked to set up the PCI device memory space for device
7897 * with SLI-4 interface spec.
7898 *
7899 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007900 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007901 * other values - error
7902 **/
7903static int
7904lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7905{
7906 struct pci_dev *pdev;
7907 unsigned long bar0map_len, bar1map_len, bar2map_len;
7908 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007909 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007910
7911 /* Obtain PCI device reference */
7912 if (!phba->pcidev)
7913 return error;
7914 else
7915 pdev = phba->pcidev;
7916
7917 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007918 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7919 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7920 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7921 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007922 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007923 }
7924 }
James Smartda0436e2009-05-22 14:51:39 -04007925
James Smart2fcee4b2010-12-15 17:57:46 -05007926 /*
7927 * The BARs and register set definitions and offset locations are
7928 * dependent on the if_type.
7929 */
7930 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7931 &phba->sli4_hba.sli_intf.word0)) {
7932 return error;
7933 }
7934
7935 /* There is no SLI3 failback for SLI4 devices. */
7936 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7937 LPFC_SLI_INTF_VALID) {
7938 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7939 "2894 SLI_INTF reg contents invalid "
7940 "sli_intf reg 0x%x\n",
7941 phba->sli4_hba.sli_intf.word0);
7942 return error;
7943 }
7944
7945 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7946 /*
7947 * Get the bus address of SLI4 device Bar regions and the
7948 * number of bytes required by each mapping. The mapping of the
7949 * particular PCI BARs regions is dependent on the type of
7950 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007951 */
James Smart1dfb5a42010-02-12 14:40:50 -05007952 if (pci_resource_start(pdev, 0)) {
7953 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7954 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05007955
7956 /*
7957 * Map SLI4 PCI Config Space Register base to a kernel virtual
7958 * addr
7959 */
7960 phba->sli4_hba.conf_regs_memmap_p =
7961 ioremap(phba->pci_bar0_map, bar0map_len);
7962 if (!phba->sli4_hba.conf_regs_memmap_p) {
7963 dev_printk(KERN_ERR, &pdev->dev,
7964 "ioremap failed for SLI4 PCI config "
7965 "registers.\n");
7966 goto out;
7967 }
7968 /* Set up BAR0 PCI config space register memory map */
7969 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007970 } else {
7971 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7972 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007973 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7974 dev_printk(KERN_ERR, &pdev->dev,
7975 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7976 goto out;
7977 }
7978 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007979 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007980 if (!phba->sli4_hba.conf_regs_memmap_p) {
7981 dev_printk(KERN_ERR, &pdev->dev,
7982 "ioremap failed for SLI4 PCI config "
7983 "registers.\n");
7984 goto out;
7985 }
7986 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04007987 }
7988
James Smartc31098c2011-04-16 11:03:33 -04007989 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7990 (pci_resource_start(pdev, 2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007991 /*
7992 * Map SLI4 if type 0 HBA Control Register base to a kernel
7993 * virtual address and setup the registers.
7994 */
7995 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7996 bar1map_len = pci_resource_len(pdev, 2);
7997 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007998 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007999 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8000 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008001 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008002 goto out_iounmap_conf;
8003 }
8004 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04008005 }
8006
James Smartc31098c2011-04-16 11:03:33 -04008007 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
8008 (pci_resource_start(pdev, 4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008009 /*
8010 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8011 * virtual address and setup the registers.
8012 */
8013 phba->pci_bar2_map = pci_resource_start(pdev, 4);
8014 bar2map_len = pci_resource_len(pdev, 4);
8015 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008016 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008017 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8018 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008019 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008020 goto out_iounmap_ctrl;
8021 }
8022 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8023 if (error)
8024 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008025 }
8026
James Smartda0436e2009-05-22 14:51:39 -04008027 return 0;
8028
8029out_iounmap_all:
8030 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8031out_iounmap_ctrl:
8032 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8033out_iounmap_conf:
8034 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8035out:
8036 return error;
8037}
8038
8039/**
8040 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8041 * @phba: pointer to lpfc hba data structure.
8042 *
8043 * This routine is invoked to unset the PCI device memory space for device
8044 * with SLI-4 interface spec.
8045 **/
8046static void
8047lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8048{
James Smart2e90f4b2011-12-13 13:22:37 -05008049 uint32_t if_type;
8050 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008051
James Smart2e90f4b2011-12-13 13:22:37 -05008052 switch (if_type) {
8053 case LPFC_SLI_INTF_IF_TYPE_0:
8054 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8055 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8056 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8057 break;
8058 case LPFC_SLI_INTF_IF_TYPE_2:
8059 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8060 break;
8061 case LPFC_SLI_INTF_IF_TYPE_1:
8062 default:
8063 dev_printk(KERN_ERR, &phba->pcidev->dev,
8064 "FATAL - unsupported SLI4 interface type - %d\n",
8065 if_type);
8066 break;
8067 }
James Smartda0436e2009-05-22 14:51:39 -04008068}
8069
8070/**
James Smart3772a992009-05-22 14:50:54 -04008071 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8072 * @phba: pointer to lpfc hba data structure.
8073 *
8074 * This routine is invoked to enable the MSI-X interrupt vectors to device
8075 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
8076 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
8077 * invoked, enables either all or nothing, depending on the current
8078 * availability of PCI vector resources. The device driver is responsible
8079 * for calling the individual request_irq() to register each MSI-X vector
8080 * with a interrupt handler, which is done in this function. Note that
8081 * later when device is unloading, the driver should always call free_irq()
8082 * on all MSI-X vectors it has done request_irq() on before calling
8083 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8084 * will be left with MSI-X enabled and leaks its vectors.
8085 *
8086 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008087 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008088 * other values - error
8089 **/
8090static int
8091lpfc_sli_enable_msix(struct lpfc_hba *phba)
8092{
8093 int rc, i;
8094 LPFC_MBOXQ_t *pmb;
8095
8096 /* Set up MSI-X multi-message vectors */
8097 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8098 phba->msix_entries[i].entry = i;
8099
8100 /* Configure MSI-X capability structure */
8101 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
8102 ARRAY_SIZE(phba->msix_entries));
8103 if (rc) {
8104 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8105 "0420 PCI enable MSI-X failed (%d)\n", rc);
8106 goto msi_fail_out;
8107 }
8108 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8109 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8110 "0477 MSI-X entry[%d]: vector=x%x "
8111 "message=%d\n", i,
8112 phba->msix_entries[i].vector,
8113 phba->msix_entries[i].entry);
8114 /*
8115 * Assign MSI-X vectors to interrupt handlers
8116 */
8117
8118 /* vector-0 is associated to slow-path handler */
8119 rc = request_irq(phba->msix_entries[0].vector,
8120 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
8121 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8122 if (rc) {
8123 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8124 "0421 MSI-X slow-path request_irq failed "
8125 "(%d)\n", rc);
8126 goto msi_fail_out;
8127 }
8128
8129 /* vector-1 is associated to fast-path handler */
8130 rc = request_irq(phba->msix_entries[1].vector,
8131 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
8132 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8133
8134 if (rc) {
8135 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8136 "0429 MSI-X fast-path request_irq failed "
8137 "(%d)\n", rc);
8138 goto irq_fail_out;
8139 }
8140
8141 /*
8142 * Configure HBA MSI-X attention conditions to messages
8143 */
8144 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8145
8146 if (!pmb) {
8147 rc = -ENOMEM;
8148 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8149 "0474 Unable to allocate memory for issuing "
8150 "MBOX_CONFIG_MSI command\n");
8151 goto mem_fail_out;
8152 }
8153 rc = lpfc_config_msi(phba, pmb);
8154 if (rc)
8155 goto mbx_fail_out;
8156 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8157 if (rc != MBX_SUCCESS) {
8158 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8159 "0351 Config MSI mailbox command failed, "
8160 "mbxCmd x%x, mbxStatus x%x\n",
8161 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8162 goto mbx_fail_out;
8163 }
8164
8165 /* Free memory allocated for mailbox command */
8166 mempool_free(pmb, phba->mbox_mem_pool);
8167 return rc;
8168
8169mbx_fail_out:
8170 /* Free memory allocated for mailbox command */
8171 mempool_free(pmb, phba->mbox_mem_pool);
8172
8173mem_fail_out:
8174 /* free the irq already requested */
8175 free_irq(phba->msix_entries[1].vector, phba);
8176
8177irq_fail_out:
8178 /* free the irq already requested */
8179 free_irq(phba->msix_entries[0].vector, phba);
8180
8181msi_fail_out:
8182 /* Unconfigure MSI-X capability structure */
8183 pci_disable_msix(phba->pcidev);
8184 return rc;
8185}
8186
8187/**
8188 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8189 * @phba: pointer to lpfc hba data structure.
8190 *
8191 * This routine is invoked to release the MSI-X vectors and then disable the
8192 * MSI-X interrupt mode to device with SLI-3 interface spec.
8193 **/
8194static void
8195lpfc_sli_disable_msix(struct lpfc_hba *phba)
8196{
8197 int i;
8198
8199 /* Free up MSI-X multi-message vectors */
8200 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8201 free_irq(phba->msix_entries[i].vector, phba);
8202 /* Disable MSI-X */
8203 pci_disable_msix(phba->pcidev);
8204
8205 return;
8206}
8207
8208/**
8209 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8210 * @phba: pointer to lpfc hba data structure.
8211 *
8212 * This routine is invoked to enable the MSI interrupt mode to device with
8213 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8214 * enable the MSI vector. The device driver is responsible for calling the
8215 * request_irq() to register MSI vector with a interrupt the handler, which
8216 * is done in this function.
8217 *
8218 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008219 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008220 * other values - error
8221 */
8222static int
8223lpfc_sli_enable_msi(struct lpfc_hba *phba)
8224{
8225 int rc;
8226
8227 rc = pci_enable_msi(phba->pcidev);
8228 if (!rc)
8229 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8230 "0462 PCI enable MSI mode success.\n");
8231 else {
8232 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8233 "0471 PCI enable MSI mode failed (%d)\n", rc);
8234 return rc;
8235 }
8236
8237 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8238 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8239 if (rc) {
8240 pci_disable_msi(phba->pcidev);
8241 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8242 "0478 MSI request_irq failed (%d)\n", rc);
8243 }
8244 return rc;
8245}
8246
8247/**
8248 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8249 * @phba: pointer to lpfc hba data structure.
8250 *
8251 * This routine is invoked to disable the MSI interrupt mode to device with
8252 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8253 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8254 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8255 * its vector.
8256 */
8257static void
8258lpfc_sli_disable_msi(struct lpfc_hba *phba)
8259{
8260 free_irq(phba->pcidev->irq, phba);
8261 pci_disable_msi(phba->pcidev);
8262 return;
8263}
8264
8265/**
8266 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8267 * @phba: pointer to lpfc hba data structure.
8268 *
8269 * This routine is invoked to enable device interrupt and associate driver's
8270 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8271 * spec. Depends on the interrupt mode configured to the driver, the driver
8272 * will try to fallback from the configured interrupt mode to an interrupt
8273 * mode which is supported by the platform, kernel, and device in the order
8274 * of:
8275 * MSI-X -> MSI -> IRQ.
8276 *
8277 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008278 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008279 * other values - error
8280 **/
8281static uint32_t
8282lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8283{
8284 uint32_t intr_mode = LPFC_INTR_ERROR;
8285 int retval;
8286
8287 if (cfg_mode == 2) {
8288 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8289 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8290 if (!retval) {
8291 /* Now, try to enable MSI-X interrupt mode */
8292 retval = lpfc_sli_enable_msix(phba);
8293 if (!retval) {
8294 /* Indicate initialization to MSI-X mode */
8295 phba->intr_type = MSIX;
8296 intr_mode = 2;
8297 }
8298 }
8299 }
8300
8301 /* Fallback to MSI if MSI-X initialization failed */
8302 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8303 retval = lpfc_sli_enable_msi(phba);
8304 if (!retval) {
8305 /* Indicate initialization to MSI mode */
8306 phba->intr_type = MSI;
8307 intr_mode = 1;
8308 }
8309 }
8310
8311 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8312 if (phba->intr_type == NONE) {
8313 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8314 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8315 if (!retval) {
8316 /* Indicate initialization to INTx mode */
8317 phba->intr_type = INTx;
8318 intr_mode = 0;
8319 }
8320 }
8321 return intr_mode;
8322}
8323
8324/**
8325 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8326 * @phba: pointer to lpfc hba data structure.
8327 *
8328 * This routine is invoked to disable device interrupt and disassociate the
8329 * driver's interrupt handler(s) from interrupt vector(s) to device with
8330 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8331 * release the interrupt vector(s) for the message signaled interrupt.
8332 **/
8333static void
8334lpfc_sli_disable_intr(struct lpfc_hba *phba)
8335{
8336 /* Disable the currently initialized interrupt mode */
8337 if (phba->intr_type == MSIX)
8338 lpfc_sli_disable_msix(phba);
8339 else if (phba->intr_type == MSI)
8340 lpfc_sli_disable_msi(phba);
8341 else if (phba->intr_type == INTx)
8342 free_irq(phba->pcidev->irq, phba);
8343
8344 /* Reset interrupt management states */
8345 phba->intr_type = NONE;
8346 phba->sli.slistat.sli_intr = 0;
8347
8348 return;
8349}
8350
8351/**
James Smart7bb03bb2013-04-17 20:19:16 -04008352 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8353 * @phba: pointer to lpfc hba data structure.
8354 *
8355 * Find next available CPU to use for IRQ to CPU affinity.
8356 */
8357static int
8358lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8359{
8360 struct lpfc_vector_map_info *cpup;
8361 int cpu;
8362
8363 cpup = phba->sli4_hba.cpu_map;
8364 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8365 /* CPU must be online */
8366 if (cpu_online(cpu)) {
8367 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8368 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8369 (cpup->phys_id == phys_id)) {
8370 return cpu;
8371 }
8372 }
8373 cpup++;
8374 }
8375
8376 /*
8377 * If we get here, we have used ALL CPUs for the specific
8378 * phys_id. Now we need to clear out lpfc_used_cpu and start
8379 * reusing CPUs.
8380 */
8381
8382 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8383 if (lpfc_used_cpu[cpu] == phys_id)
8384 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8385 }
8386
8387 cpup = phba->sli4_hba.cpu_map;
8388 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8389 /* CPU must be online */
8390 if (cpu_online(cpu)) {
8391 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8392 (cpup->phys_id == phys_id)) {
8393 return cpu;
8394 }
8395 }
8396 cpup++;
8397 }
8398 return LPFC_VECTOR_MAP_EMPTY;
8399}
8400
8401/**
8402 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8403 * @phba: pointer to lpfc hba data structure.
8404 * @vectors: number of HBA vectors
8405 *
8406 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8407 * affinization across multple physical CPUs (numa nodes).
8408 * In addition, this routine will assign an IO channel for each CPU
8409 * to use when issuing I/Os.
8410 */
8411static int
8412lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8413{
8414 int i, idx, saved_chann, used_chann, cpu, phys_id;
8415 int max_phys_id, num_io_channel, first_cpu;
8416 struct lpfc_vector_map_info *cpup;
8417#ifdef CONFIG_X86
8418 struct cpuinfo_x86 *cpuinfo;
8419#endif
8420 struct cpumask *mask;
8421 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8422
8423 /* If there is no mapping, just return */
8424 if (!phba->cfg_fcp_cpu_map)
8425 return 1;
8426
8427 /* Init cpu_map array */
8428 memset(phba->sli4_hba.cpu_map, 0xff,
8429 (sizeof(struct lpfc_vector_map_info) *
8430 phba->sli4_hba.num_present_cpu));
8431
8432 max_phys_id = 0;
8433 phys_id = 0;
8434 num_io_channel = 0;
8435 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8436
8437 /* Update CPU map with physical id and core id of each CPU */
8438 cpup = phba->sli4_hba.cpu_map;
8439 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8440#ifdef CONFIG_X86
8441 cpuinfo = &cpu_data(cpu);
8442 cpup->phys_id = cpuinfo->phys_proc_id;
8443 cpup->core_id = cpuinfo->cpu_core_id;
8444#else
8445 /* No distinction between CPUs for other platforms */
8446 cpup->phys_id = 0;
8447 cpup->core_id = 0;
8448#endif
8449
8450 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8451 "3328 CPU physid %d coreid %d\n",
8452 cpup->phys_id, cpup->core_id);
8453
8454 if (cpup->phys_id > max_phys_id)
8455 max_phys_id = cpup->phys_id;
8456 cpup++;
8457 }
8458
8459 /* Now associate the HBA vectors with specific CPUs */
8460 for (idx = 0; idx < vectors; idx++) {
8461 cpup = phba->sli4_hba.cpu_map;
8462 cpu = lpfc_find_next_cpu(phba, phys_id);
8463 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8464
8465 /* Try for all phys_id's */
8466 for (i = 1; i < max_phys_id; i++) {
8467 phys_id++;
8468 if (phys_id > max_phys_id)
8469 phys_id = 0;
8470 cpu = lpfc_find_next_cpu(phba, phys_id);
8471 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8472 continue;
8473 goto found;
8474 }
8475
8476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8477 "3329 Cannot set affinity:"
8478 "Error mapping vector %d (%d)\n",
8479 idx, vectors);
8480 return 0;
8481 }
8482found:
8483 cpup += cpu;
8484 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8485 lpfc_used_cpu[cpu] = phys_id;
8486
8487 /* Associate vector with selected CPU */
8488 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8489
8490 /* Associate IO channel with selected CPU */
8491 cpup->channel_id = idx;
8492 num_io_channel++;
8493
8494 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8495 first_cpu = cpu;
8496
8497 /* Now affinitize to the selected CPU */
8498 mask = &cpup->maskbits;
8499 cpumask_clear(mask);
8500 cpumask_set_cpu(cpu, mask);
8501 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
8502 vector, mask);
8503
8504 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8505 "3330 Set Affinity: CPU %d channel %d "
8506 "irq %d (%x)\n",
8507 cpu, cpup->channel_id,
8508 phba->sli4_hba.msix_entries[idx].vector, i);
8509
8510 /* Spread vector mapping across multple physical CPU nodes */
8511 phys_id++;
8512 if (phys_id > max_phys_id)
8513 phys_id = 0;
8514 }
8515
8516 /*
8517 * Finally fill in the IO channel for any remaining CPUs.
8518 * At this point, all IO channels have been assigned to a specific
8519 * MSIx vector, mapped to a specific CPU.
8520 * Base the remaining IO channel assigned, to IO channels already
8521 * assigned to other CPUs on the same phys_id.
8522 */
8523 for (i = 0; i <= max_phys_id; i++) {
8524 /*
8525 * If there are no io channels already mapped to
8526 * this phys_id, just round robin thru the io_channels.
8527 * Setup chann[] for round robin.
8528 */
8529 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8530 chann[idx] = idx;
8531
8532 saved_chann = 0;
8533 used_chann = 0;
8534
8535 /*
8536 * First build a list of IO channels already assigned
8537 * to this phys_id before reassigning the same IO
8538 * channels to the remaining CPUs.
8539 */
8540 cpup = phba->sli4_hba.cpu_map;
8541 cpu = first_cpu;
8542 cpup += cpu;
8543 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8544 idx++) {
8545 if (cpup->phys_id == i) {
8546 /*
8547 * Save any IO channels that are
8548 * already mapped to this phys_id.
8549 */
8550 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
8551 chann[saved_chann] =
8552 cpup->channel_id;
8553 saved_chann++;
8554 goto out;
8555 }
8556
8557 /* See if we are using round-robin */
8558 if (saved_chann == 0)
8559 saved_chann =
8560 phba->cfg_fcp_io_channel;
8561
8562 /* Associate next IO channel with CPU */
8563 cpup->channel_id = chann[used_chann];
8564 num_io_channel++;
8565 used_chann++;
8566 if (used_chann == saved_chann)
8567 used_chann = 0;
8568
8569 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8570 "3331 Set IO_CHANN "
8571 "CPU %d channel %d\n",
8572 idx, cpup->channel_id);
8573 }
8574out:
8575 cpu++;
8576 if (cpu >= phba->sli4_hba.num_present_cpu) {
8577 cpup = phba->sli4_hba.cpu_map;
8578 cpu = 0;
8579 } else {
8580 cpup++;
8581 }
8582 }
8583 }
8584
8585 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8586 cpup = phba->sli4_hba.cpu_map;
8587 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8588 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8589 cpup->channel_id = 0;
8590 num_io_channel++;
8591
8592 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8593 "3332 Assign IO_CHANN "
8594 "CPU %d channel %d\n",
8595 idx, cpup->channel_id);
8596 }
8597 cpup++;
8598 }
8599 }
8600
8601 /* Sanity check */
8602 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8603 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8604 "3333 Set affinity mismatch:"
8605 "%d chann != %d cpus: %d vactors\n",
8606 num_io_channel, phba->sli4_hba.num_present_cpu,
8607 vectors);
8608
8609 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8610 return 1;
8611}
8612
8613
8614/**
James Smartda0436e2009-05-22 14:51:39 -04008615 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8616 * @phba: pointer to lpfc hba data structure.
8617 *
8618 * This routine is invoked to enable the MSI-X interrupt vectors to device
8619 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8620 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8621 * enables either all or nothing, depending on the current availability of
8622 * PCI vector resources. The device driver is responsible for calling the
8623 * individual request_irq() to register each MSI-X vector with a interrupt
8624 * handler, which is done in this function. Note that later when device is
8625 * unloading, the driver should always call free_irq() on all MSI-X vectors
8626 * it has done request_irq() on before calling pci_disable_msix(). Failure
8627 * to do so results in a BUG_ON() and a device will be left with MSI-X
8628 * enabled and leaks its vectors.
8629 *
8630 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008631 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008632 * other values - error
8633 **/
8634static int
8635lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8636{
James Smart75baf692010-06-08 18:31:21 -04008637 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008638
8639 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008640 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008641 phba->sli4_hba.msix_entries[index].entry = index;
8642
8643 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008644 vectors = phba->cfg_fcp_io_channel;
James Smart75baf692010-06-08 18:31:21 -04008645enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008646 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008647 vectors);
8648 if (rc > 1) {
8649 vectors = rc;
8650 goto enable_msix_vectors;
8651 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008652 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8653 "0484 PCI enable MSI-X failed (%d)\n", rc);
8654 goto msi_fail_out;
8655 }
James Smart75baf692010-06-08 18:31:21 -04008656
James Smartda0436e2009-05-22 14:51:39 -04008657 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008658 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008659 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8660 "0489 MSI-X entry[%d]: vector=x%x "
8661 "message=%d\n", index,
8662 phba->sli4_hba.msix_entries[index].vector,
8663 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008664
James Smart7bb03bb2013-04-17 20:19:16 -04008665 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04008666 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008667 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8668 sprintf((char *)&phba->sli4_hba.handler_name[index],
8669 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008670
James Smart67d12732012-08-03 12:36:13 -04008671 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8672 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008673 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008674 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008675 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008676 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008677 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008678 if (rc) {
8679 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8680 "0486 MSI-X fast-path (%d) "
8681 "request_irq failed (%d)\n", index, rc);
8682 goto cfg_fail_out;
8683 }
8684 }
8685
James Smart82c3e9b2012-09-29 11:29:50 -04008686 if (vectors != phba->cfg_fcp_io_channel) {
8687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8688 "3238 Reducing IO channels to match number of "
8689 "MSI-X vectors, requested %d got %d\n",
8690 phba->cfg_fcp_io_channel, vectors);
8691 phba->cfg_fcp_io_channel = vectors;
8692 }
James Smart7bb03bb2013-04-17 20:19:16 -04008693
8694 lpfc_sli4_set_affinity(phba, vectors);
James Smartda0436e2009-05-22 14:51:39 -04008695 return rc;
8696
8697cfg_fail_out:
8698 /* free the irq already requested */
James Smart67d12732012-08-03 12:36:13 -04008699 for (--index; index >= 0; index--)
8700 free_irq(phba->sli4_hba.msix_entries[index].vector,
8701 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008702
8703msi_fail_out:
8704 /* Unconfigure MSI-X capability structure */
8705 pci_disable_msix(phba->pcidev);
8706 return rc;
8707}
8708
8709/**
8710 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8711 * @phba: pointer to lpfc hba data structure.
8712 *
8713 * This routine is invoked to release the MSI-X vectors and then disable the
8714 * MSI-X interrupt mode to device with SLI-4 interface spec.
8715 **/
8716static void
8717lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8718{
8719 int index;
8720
8721 /* Free up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008722 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008723 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008724 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smart75baf692010-06-08 18:31:21 -04008725
James Smartda0436e2009-05-22 14:51:39 -04008726 /* Disable MSI-X */
8727 pci_disable_msix(phba->pcidev);
8728
8729 return;
8730}
8731
8732/**
8733 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8734 * @phba: pointer to lpfc hba data structure.
8735 *
8736 * This routine is invoked to enable the MSI interrupt mode to device with
8737 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8738 * to enable the MSI vector. The device driver is responsible for calling
8739 * the request_irq() to register MSI vector with a interrupt the handler,
8740 * which is done in this function.
8741 *
8742 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008743 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008744 * other values - error
8745 **/
8746static int
8747lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8748{
8749 int rc, index;
8750
8751 rc = pci_enable_msi(phba->pcidev);
8752 if (!rc)
8753 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8754 "0487 PCI enable MSI mode success.\n");
8755 else {
8756 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8757 "0488 PCI enable MSI mode failed (%d)\n", rc);
8758 return rc;
8759 }
8760
8761 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8762 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8763 if (rc) {
8764 pci_disable_msi(phba->pcidev);
8765 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8766 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008767 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008768 }
8769
James Smart67d12732012-08-03 12:36:13 -04008770 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008771 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8772 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8773 }
8774
James Smart75baf692010-06-08 18:31:21 -04008775 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008776}
8777
8778/**
8779 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8780 * @phba: pointer to lpfc hba data structure.
8781 *
8782 * This routine is invoked to disable the MSI interrupt mode to device with
8783 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8784 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8785 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8786 * its vector.
8787 **/
8788static void
8789lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8790{
8791 free_irq(phba->pcidev->irq, phba);
8792 pci_disable_msi(phba->pcidev);
8793 return;
8794}
8795
8796/**
8797 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8798 * @phba: pointer to lpfc hba data structure.
8799 *
8800 * This routine is invoked to enable device interrupt and associate driver's
8801 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8802 * interface spec. Depends on the interrupt mode configured to the driver,
8803 * the driver will try to fallback from the configured interrupt mode to an
8804 * interrupt mode which is supported by the platform, kernel, and device in
8805 * the order of:
8806 * MSI-X -> MSI -> IRQ.
8807 *
8808 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008809 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008810 * other values - error
8811 **/
8812static uint32_t
8813lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8814{
8815 uint32_t intr_mode = LPFC_INTR_ERROR;
8816 int retval, index;
8817
8818 if (cfg_mode == 2) {
8819 /* Preparation before conf_msi mbox cmd */
8820 retval = 0;
8821 if (!retval) {
8822 /* Now, try to enable MSI-X interrupt mode */
8823 retval = lpfc_sli4_enable_msix(phba);
8824 if (!retval) {
8825 /* Indicate initialization to MSI-X mode */
8826 phba->intr_type = MSIX;
8827 intr_mode = 2;
8828 }
8829 }
8830 }
8831
8832 /* Fallback to MSI if MSI-X initialization failed */
8833 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8834 retval = lpfc_sli4_enable_msi(phba);
8835 if (!retval) {
8836 /* Indicate initialization to MSI mode */
8837 phba->intr_type = MSI;
8838 intr_mode = 1;
8839 }
8840 }
8841
8842 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8843 if (phba->intr_type == NONE) {
8844 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8845 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8846 if (!retval) {
8847 /* Indicate initialization to INTx mode */
8848 phba->intr_type = INTx;
8849 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04008850 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04008851 index++) {
8852 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8853 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008854 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8855 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008856 }
8857 }
8858 }
8859 return intr_mode;
8860}
8861
8862/**
8863 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8864 * @phba: pointer to lpfc hba data structure.
8865 *
8866 * This routine is invoked to disable device interrupt and disassociate
8867 * the driver's interrupt handler(s) from interrupt vector(s) to device
8868 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8869 * will release the interrupt vector(s) for the message signaled interrupt.
8870 **/
8871static void
8872lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8873{
8874 /* Disable the currently initialized interrupt mode */
8875 if (phba->intr_type == MSIX)
8876 lpfc_sli4_disable_msix(phba);
8877 else if (phba->intr_type == MSI)
8878 lpfc_sli4_disable_msi(phba);
8879 else if (phba->intr_type == INTx)
8880 free_irq(phba->pcidev->irq, phba);
8881
8882 /* Reset interrupt management states */
8883 phba->intr_type = NONE;
8884 phba->sli.slistat.sli_intr = 0;
8885
8886 return;
8887}
8888
8889/**
James Smart3772a992009-05-22 14:50:54 -04008890 * lpfc_unset_hba - Unset SLI3 hba device initialization
8891 * @phba: pointer to lpfc hba data structure.
8892 *
8893 * This routine is invoked to unset the HBA device initialization steps to
8894 * a device with SLI-3 interface spec.
8895 **/
8896static void
8897lpfc_unset_hba(struct lpfc_hba *phba)
8898{
8899 struct lpfc_vport *vport = phba->pport;
8900 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8901
8902 spin_lock_irq(shost->host_lock);
8903 vport->load_flag |= FC_UNLOADING;
8904 spin_unlock_irq(shost->host_lock);
8905
James Smart72859902012-01-18 16:25:38 -05008906 kfree(phba->vpi_bmask);
8907 kfree(phba->vpi_ids);
8908
James Smart3772a992009-05-22 14:50:54 -04008909 lpfc_stop_hba_timers(phba);
8910
8911 phba->pport->work_port_events = 0;
8912
8913 lpfc_sli_hba_down(phba);
8914
8915 lpfc_sli_brdrestart(phba);
8916
8917 lpfc_sli_disable_intr(phba);
8918
8919 return;
8920}
8921
8922/**
James Smart5af5eee2010-10-22 11:06:38 -04008923 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8924 * @phba: Pointer to HBA context object.
8925 *
8926 * This function is called in the SLI4 code path to wait for completion
8927 * of device's XRIs exchange busy. It will check the XRI exchange busy
8928 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8929 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8930 * I/Os every 30 seconds, log error message, and wait forever. Only when
8931 * all XRI exchange busy complete, the driver unload shall proceed with
8932 * invoking the function reset ioctl mailbox command to the CNA and the
8933 * the rest of the driver unload resource release.
8934 **/
8935static void
8936lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8937{
8938 int wait_time = 0;
8939 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8940 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8941
8942 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8943 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8944 if (!fcp_xri_cmpl)
8945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8946 "2877 FCP XRI exchange busy "
8947 "wait time: %d seconds.\n",
8948 wait_time/1000);
8949 if (!els_xri_cmpl)
8950 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8951 "2878 ELS XRI exchange busy "
8952 "wait time: %d seconds.\n",
8953 wait_time/1000);
8954 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8955 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8956 } else {
8957 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8958 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8959 }
8960 fcp_xri_cmpl =
8961 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8962 els_xri_cmpl =
8963 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8964 }
8965}
8966
8967/**
James Smartda0436e2009-05-22 14:51:39 -04008968 * lpfc_sli4_hba_unset - Unset the fcoe hba
8969 * @phba: Pointer to HBA context object.
8970 *
8971 * This function is called in the SLI4 code path to reset the HBA's FCoE
8972 * function. The caller is not required to hold any lock. This routine
8973 * issues PCI function reset mailbox command to reset the FCoE function.
8974 * At the end of the function, it calls lpfc_hba_down_post function to
8975 * free any pending commands.
8976 **/
8977static void
8978lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8979{
8980 int wait_cnt = 0;
8981 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04008982 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04008983
8984 lpfc_stop_hba_timers(phba);
8985 phba->sli4_hba.intr_enable = 0;
8986
8987 /*
8988 * Gracefully wait out the potential current outstanding asynchronous
8989 * mailbox command.
8990 */
8991
8992 /* First, block any pending async mailbox command from posted */
8993 spin_lock_irq(&phba->hbalock);
8994 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8995 spin_unlock_irq(&phba->hbalock);
8996 /* Now, trying to wait it out if we can */
8997 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8998 msleep(10);
8999 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9000 break;
9001 }
9002 /* Forcefully release the outstanding mailbox command if timed out */
9003 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9004 spin_lock_irq(&phba->hbalock);
9005 mboxq = phba->sli.mbox_active;
9006 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9007 __lpfc_mbox_cmpl_put(phba, mboxq);
9008 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9009 phba->sli.mbox_active = NULL;
9010 spin_unlock_irq(&phba->hbalock);
9011 }
9012
James Smart5af5eee2010-10-22 11:06:38 -04009013 /* Abort all iocbs associated with the hba */
9014 lpfc_sli_hba_iocb_abort(phba);
9015
9016 /* Wait for completion of device XRI exchange busy */
9017 lpfc_sli4_xri_exchange_busy_wait(phba);
9018
James Smartda0436e2009-05-22 14:51:39 -04009019 /* Disable PCI subsystem interrupt */
9020 lpfc_sli4_disable_intr(phba);
9021
James Smart912e3ac2011-05-24 11:42:11 -04009022 /* Disable SR-IOV if enabled */
9023 if (phba->cfg_sriov_nr_virtfn)
9024 pci_disable_sriov(pdev);
9025
James Smartda0436e2009-05-22 14:51:39 -04009026 /* Stop kthread signal shall trigger work_done one more time */
9027 kthread_stop(phba->worker_thread);
9028
James Smart3677a3a2010-09-29 11:19:14 -04009029 /* Reset SLI4 HBA FCoE function */
9030 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009031 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009032
James Smartda0436e2009-05-22 14:51:39 -04009033 /* Stop the SLI4 device port */
9034 phba->pport->work_port_events = 0;
9035}
9036
James Smart28baac72010-02-12 14:42:03 -05009037 /**
9038 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9039 * @phba: Pointer to HBA context object.
9040 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9041 *
9042 * This function is called in the SLI4 code path to read the port's
9043 * sli4 capabilities.
9044 *
9045 * This function may be be called from any context that can block-wait
9046 * for the completion. The expectation is that this routine is called
9047 * typically from probe_one or from the online routine.
9048 **/
9049int
9050lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9051{
9052 int rc;
9053 struct lpfc_mqe *mqe;
9054 struct lpfc_pc_sli4_params *sli4_params;
9055 uint32_t mbox_tmo;
9056
9057 rc = 0;
9058 mqe = &mboxq->u.mqe;
9059
9060 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009061 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009062 if (!phba->sli4_hba.intr_enable)
9063 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9064 else {
James Smarta183a152011-10-10 21:32:43 -04009065 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009066 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9067 }
9068
9069 if (unlikely(rc))
9070 return 1;
9071
9072 sli4_params = &phba->sli4_hba.pc_sli4_params;
9073 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9074 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9075 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9076 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9077 &mqe->un.sli4_params);
9078 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9079 &mqe->un.sli4_params);
9080 sli4_params->proto_types = mqe->un.sli4_params.word3;
9081 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9082 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9083 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9084 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9085 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9086 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9087 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9088 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9089 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9090 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9091 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9092 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9093 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9094 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9095 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9096 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9097 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9098 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9099 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9100 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009101
9102 /* Make sure that sge_supp_len can be handled by the driver */
9103 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9104 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9105
James Smart28baac72010-02-12 14:42:03 -05009106 return rc;
9107}
9108
James Smartda0436e2009-05-22 14:51:39 -04009109/**
James Smartfedd3b72011-02-16 12:39:24 -05009110 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9111 * @phba: Pointer to HBA context object.
9112 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9113 *
9114 * This function is called in the SLI4 code path to read the port's
9115 * sli4 capabilities.
9116 *
9117 * This function may be be called from any context that can block-wait
9118 * for the completion. The expectation is that this routine is called
9119 * typically from probe_one or from the online routine.
9120 **/
9121int
9122lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9123{
9124 int rc;
9125 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9126 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009127 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009128 int length;
9129 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9130
James Smart6d368e52011-05-24 11:44:12 -04009131 /*
9132 * By default, the driver assumes the SLI4 port requires RPI
9133 * header postings. The SLI4_PARAM response will correct this
9134 * assumption.
9135 */
9136 phba->sli4_hba.rpi_hdrs_in_use = 1;
9137
James Smartfedd3b72011-02-16 12:39:24 -05009138 /* Read the port's SLI4 Config Parameters */
9139 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9140 sizeof(struct lpfc_sli4_cfg_mhdr));
9141 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9142 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9143 length, LPFC_SLI4_MBX_EMBED);
9144 if (!phba->sli4_hba.intr_enable)
9145 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009146 else {
9147 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9148 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9149 }
James Smartfedd3b72011-02-16 12:39:24 -05009150 if (unlikely(rc))
9151 return rc;
9152 sli4_params = &phba->sli4_hba.pc_sli4_params;
9153 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9154 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9155 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9156 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9157 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9158 mbx_sli4_parameters);
9159 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9160 mbx_sli4_parameters);
9161 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9162 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9163 else
9164 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9165 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9166 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
9167 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9168 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9169 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9170 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
9171 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9172 mbx_sli4_parameters);
9173 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9174 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009175 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9176 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04009177
9178 /* Make sure that sge_supp_len can be handled by the driver */
9179 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9180 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9181
James Smartfedd3b72011-02-16 12:39:24 -05009182 return 0;
9183}
9184
9185/**
James Smart3772a992009-05-22 14:50:54 -04009186 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9187 * @pdev: pointer to PCI device
9188 * @pid: pointer to PCI device identifier
9189 *
9190 * This routine is to be called to attach a device with SLI-3 interface spec
9191 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9192 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9193 * information of the device and driver to see if the driver state that it can
9194 * support this kind of device. If the match is successful, the driver core
9195 * invokes this routine. If this routine determines it can claim the HBA, it
9196 * does all the initialization that it needs to do to handle the HBA properly.
9197 *
9198 * Return code
9199 * 0 - driver can claim the device
9200 * negative value - driver can not claim the device
9201 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009202static int
James Smart3772a992009-05-22 14:50:54 -04009203lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9204{
9205 struct lpfc_hba *phba;
9206 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009207 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009208 int error;
9209 uint32_t cfg_mode, intr_mode;
9210
9211 /* Allocate memory for HBA structure */
9212 phba = lpfc_hba_alloc(pdev);
9213 if (!phba)
9214 return -ENOMEM;
9215
9216 /* Perform generic PCI device enabling operation */
9217 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009218 if (error)
James Smart3772a992009-05-22 14:50:54 -04009219 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009220
9221 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9222 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9223 if (error)
9224 goto out_disable_pci_dev;
9225
9226 /* Set up SLI-3 specific device PCI memory space */
9227 error = lpfc_sli_pci_mem_setup(phba);
9228 if (error) {
9229 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9230 "1402 Failed to set up pci memory space.\n");
9231 goto out_disable_pci_dev;
9232 }
9233
9234 /* Set up phase-1 common device driver resources */
9235 error = lpfc_setup_driver_resource_phase1(phba);
9236 if (error) {
9237 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9238 "1403 Failed to set up driver resource.\n");
9239 goto out_unset_pci_mem_s3;
9240 }
9241
9242 /* Set up SLI-3 specific device driver resources */
9243 error = lpfc_sli_driver_resource_setup(phba);
9244 if (error) {
9245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9246 "1404 Failed to set up driver resource.\n");
9247 goto out_unset_pci_mem_s3;
9248 }
9249
9250 /* Initialize and populate the iocb list per host */
9251 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9252 if (error) {
9253 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9254 "1405 Failed to initialize iocb list.\n");
9255 goto out_unset_driver_resource_s3;
9256 }
9257
9258 /* Set up common device driver resources */
9259 error = lpfc_setup_driver_resource_phase2(phba);
9260 if (error) {
9261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9262 "1406 Failed to set up driver resource.\n");
9263 goto out_free_iocb_list;
9264 }
9265
James Smart079b5c92011-08-21 21:48:49 -04009266 /* Get the default values for Model Name and Description */
9267 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9268
James Smart3772a992009-05-22 14:50:54 -04009269 /* Create SCSI host to the physical port */
9270 error = lpfc_create_shost(phba);
9271 if (error) {
9272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9273 "1407 Failed to create scsi host.\n");
9274 goto out_unset_driver_resource;
9275 }
9276
9277 /* Configure sysfs attributes */
9278 vport = phba->pport;
9279 error = lpfc_alloc_sysfs_attr(vport);
9280 if (error) {
9281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9282 "1476 Failed to allocate sysfs attr\n");
9283 goto out_destroy_shost;
9284 }
9285
James Smart6669f9b2009-10-02 15:16:45 -04009286 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009287 /* Now, trying to enable interrupt and bring up the device */
9288 cfg_mode = phba->cfg_use_msi;
9289 while (true) {
9290 /* Put device to a known state before enabling interrupt */
9291 lpfc_stop_port(phba);
9292 /* Configure and enable interrupt */
9293 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9294 if (intr_mode == LPFC_INTR_ERROR) {
9295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9296 "0431 Failed to enable interrupt.\n");
9297 error = -ENODEV;
9298 goto out_free_sysfs_attr;
9299 }
9300 /* SLI-3 HBA setup */
9301 if (lpfc_sli_hba_setup(phba)) {
9302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9303 "1477 Failed to set up hba\n");
9304 error = -ENODEV;
9305 goto out_remove_device;
9306 }
9307
9308 /* Wait 50ms for the interrupts of previous mailbox commands */
9309 msleep(50);
9310 /* Check active interrupts on message signaled interrupts */
9311 if (intr_mode == 0 ||
9312 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9313 /* Log the current active interrupt mode */
9314 phba->intr_mode = intr_mode;
9315 lpfc_log_intr_mode(phba, intr_mode);
9316 break;
9317 } else {
9318 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9319 "0447 Configure interrupt mode (%d) "
9320 "failed active interrupt test.\n",
9321 intr_mode);
9322 /* Disable the current interrupt mode */
9323 lpfc_sli_disable_intr(phba);
9324 /* Try next level of interrupt mode */
9325 cfg_mode = --intr_mode;
9326 }
9327 }
9328
9329 /* Perform post initialization setup */
9330 lpfc_post_init_setup(phba);
9331
9332 /* Check if there are static vports to be created. */
9333 lpfc_create_static_vport(phba);
9334
9335 return 0;
9336
9337out_remove_device:
9338 lpfc_unset_hba(phba);
9339out_free_sysfs_attr:
9340 lpfc_free_sysfs_attr(vport);
9341out_destroy_shost:
9342 lpfc_destroy_shost(phba);
9343out_unset_driver_resource:
9344 lpfc_unset_driver_resource_phase2(phba);
9345out_free_iocb_list:
9346 lpfc_free_iocb_list(phba);
9347out_unset_driver_resource_s3:
9348 lpfc_sli_driver_resource_unset(phba);
9349out_unset_pci_mem_s3:
9350 lpfc_sli_pci_mem_unset(phba);
9351out_disable_pci_dev:
9352 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009353 if (shost)
9354 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009355out_free_phba:
9356 lpfc_hba_free(phba);
9357 return error;
9358}
9359
9360/**
9361 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009362 * @pdev: pointer to PCI device
9363 *
James Smart3772a992009-05-22 14:50:54 -04009364 * This routine is to be called to disattach a device with SLI-3 interface
9365 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9366 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9367 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009368 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009369static void
James Smart3772a992009-05-22 14:50:54 -04009370lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009371{
James Smart2e0fef82007-06-17 19:56:36 -05009372 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9373 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009374 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009375 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009376 int i;
Tomohiro Kusumi8a4df122008-01-11 01:53:00 -05009377 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9378
James Smart549e55c2007-08-02 11:09:51 -04009379 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009380 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009381 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009382
James Smart858c9f62007-06-17 19:56:39 -05009383 lpfc_free_sysfs_attr(vport);
9384
James Smarteada2722008-12-04 22:39:13 -05009385 /* Release all the vports against this physical port */
9386 vports = lpfc_create_vport_work_array(phba);
9387 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009388 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9389 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9390 continue;
James Smarteada2722008-12-04 22:39:13 -05009391 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009392 }
James Smarteada2722008-12-04 22:39:13 -05009393 lpfc_destroy_vport_work_array(phba, vports);
9394
9395 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009396 fc_remove_host(shost);
9397 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009398 lpfc_cleanup(vport);
9399
James Smart2e0fef82007-06-17 19:56:36 -05009400 /*
9401 * Bring down the SLI Layer. This step disable all interrupts,
9402 * clears the rings, discards all mailbox commands, and resets
9403 * the HBA.
9404 */
James Smarta257bf92009-04-06 18:48:10 -04009405
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009406 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009407 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009408 /* Stop kthread signal shall trigger work_done one more time */
9409 kthread_stop(phba->worker_thread);
9410 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009411 lpfc_sli_brdrestart(phba);
9412
James Smart72859902012-01-18 16:25:38 -05009413 kfree(phba->vpi_bmask);
9414 kfree(phba->vpi_ids);
9415
James Smart3772a992009-05-22 14:50:54 -04009416 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009417 spin_lock_irq(&phba->hbalock);
9418 list_del_init(&vport->listentry);
9419 spin_unlock_irq(&phba->hbalock);
9420
James Smart858c9f62007-06-17 19:56:39 -05009421 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009422
James Smart912e3ac2011-05-24 11:42:11 -04009423 /* Disable SR-IOV if enabled */
9424 if (phba->cfg_sriov_nr_virtfn)
9425 pci_disable_sriov(pdev);
9426
James Smart5b75da22008-12-04 22:39:35 -05009427 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009428 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009429
9430 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05009431 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009432
9433 /*
9434 * Call scsi_free before mem_free since scsi bufs are released to their
9435 * corresponding pools here.
9436 */
9437 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009438 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009439
James Smart34b02dc2008-08-24 21:49:55 -04009440 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9441 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009442
James Smart2e0fef82007-06-17 19:56:36 -05009443 /* Free resources associated with SLI2 interface */
9444 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009445 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009446
9447 /* unmap adapter SLIM and Control Registers */
9448 iounmap(phba->ctrl_regs_memmap_p);
9449 iounmap(phba->slim_memmap_p);
9450
James Smart3772a992009-05-22 14:50:54 -04009451 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009452
Tomohiro Kusumi8a4df122008-01-11 01:53:00 -05009453 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009454 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009455}
9456
Linas Vepstas8d63f372007-02-14 14:28:36 -06009457/**
James Smart3772a992009-05-22 14:50:54 -04009458 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009459 * @pdev: pointer to PCI device
9460 * @msg: power management message
9461 *
James Smart3772a992009-05-22 14:50:54 -04009462 * This routine is to be called from the kernel's PCI subsystem to support
9463 * system Power Management (PM) to device with SLI-3 interface spec. When
9464 * PM invokes this method, it quiesces the device by stopping the driver's
9465 * worker thread for the device, turning off device's interrupt and DMA,
9466 * and bring the device offline. Note that as the driver implements the
9467 * minimum PM requirements to a power-aware driver's PM support for the
9468 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9469 * to the suspend() method call will be treated as SUSPEND and the driver will
9470 * fully reinitialize its device during resume() method call, the driver will
9471 * set device to PCI_D3hot state in PCI config space instead of setting it
9472 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009473 *
9474 * Return code
James Smart3772a992009-05-22 14:50:54 -04009475 * 0 - driver suspended the device
9476 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009477 **/
9478static int
James Smart3772a992009-05-22 14:50:54 -04009479lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009480{
9481 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9482 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9483
9484 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9485 "0473 PCI device Power Management suspend.\n");
9486
9487 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009488 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009489 lpfc_offline(phba);
9490 kthread_stop(phba->worker_thread);
9491
9492 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009493 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009494
9495 /* Save device state to PCI config space */
9496 pci_save_state(pdev);
9497 pci_set_power_state(pdev, PCI_D3hot);
9498
9499 return 0;
9500}
9501
9502/**
James Smart3772a992009-05-22 14:50:54 -04009503 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009504 * @pdev: pointer to PCI device
9505 *
James Smart3772a992009-05-22 14:50:54 -04009506 * This routine is to be called from the kernel's PCI subsystem to support
9507 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9508 * invokes this method, it restores the device's PCI config space state and
9509 * fully reinitializes the device and brings it online. Note that as the
9510 * driver implements the minimum PM requirements to a power-aware driver's
9511 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9512 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9513 * driver will fully reinitialize its device during resume() method call,
9514 * the device will be set to PCI_D0 directly in PCI config space before
9515 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009516 *
9517 * Return code
James Smart3772a992009-05-22 14:50:54 -04009518 * 0 - driver suspended the device
9519 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009520 **/
9521static int
James Smart3772a992009-05-22 14:50:54 -04009522lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009523{
9524 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9525 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009526 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009527 int error;
9528
9529 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9530 "0452 PCI device Power Management resume.\n");
9531
9532 /* Restore device state from PCI config space */
9533 pci_set_power_state(pdev, PCI_D0);
9534 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009535
James Smart1dfb5a42010-02-12 14:40:50 -05009536 /*
9537 * As the new kernel behavior of pci_restore_state() API call clears
9538 * device saved_state flag, need to save the restored state again.
9539 */
9540 pci_save_state(pdev);
9541
James Smart3a55b532008-12-04 22:38:54 -05009542 if (pdev->is_busmaster)
9543 pci_set_master(pdev);
9544
9545 /* Startup the kernel thread for this host adapter. */
9546 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9547 "lpfc_worker_%d", phba->brd_no);
9548 if (IS_ERR(phba->worker_thread)) {
9549 error = PTR_ERR(phba->worker_thread);
9550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9551 "0434 PM resume failed to start worker "
9552 "thread: error=x%x.\n", error);
9553 return error;
9554 }
9555
James Smart5b75da22008-12-04 22:39:35 -05009556 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009557 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009558 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009560 "0430 PM resume Failed to enable interrupt\n");
9561 return -EIO;
9562 } else
9563 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009564
9565 /* Restart HBA and bring it online */
9566 lpfc_sli_brdrestart(phba);
9567 lpfc_online(phba);
9568
James Smart5b75da22008-12-04 22:39:35 -05009569 /* Log the current active interrupt mode */
9570 lpfc_log_intr_mode(phba, phba->intr_mode);
9571
James Smart3a55b532008-12-04 22:38:54 -05009572 return 0;
9573}
9574
9575/**
James Smart891478a2009-11-18 15:40:23 -05009576 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9577 * @phba: pointer to lpfc hba data structure.
9578 *
9579 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009580 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009581 **/
9582static void
9583lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9584{
James Smarte2af0d22010-03-15 11:25:32 -04009585 struct lpfc_sli *psli = &phba->sli;
9586 struct lpfc_sli_ring *pring;
9587
James Smart891478a2009-11-18 15:40:23 -05009588 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9589 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009590
9591 /*
9592 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9593 * and let the SCSI mid-layer to retry them to recover.
9594 */
9595 pring = &psli->ring[psli->fcp_ring];
9596 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05009597}
9598
9599/**
James Smart0d878412009-10-02 15:16:56 -04009600 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9601 * @phba: pointer to lpfc hba data structure.
9602 *
9603 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9604 * disables the device interrupt and pci device, and aborts the internal FCP
9605 * pending I/Os.
9606 **/
9607static void
9608lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9609{
James Smart0d878412009-10-02 15:16:56 -04009610 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009611 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009612
James Smart75baf692010-06-08 18:31:21 -04009613 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009614 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009615
James Smarte2af0d22010-03-15 11:25:32 -04009616 /* Block all SCSI devices' I/Os on the host */
9617 lpfc_scsi_dev_block(phba);
9618
James Smartea714f32013-04-17 20:18:47 -04009619 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9620 lpfc_sli_flush_fcp_rings(phba);
9621
James Smarte2af0d22010-03-15 11:25:32 -04009622 /* stop all timers */
9623 lpfc_stop_hba_timers(phba);
9624
James Smart0d878412009-10-02 15:16:56 -04009625 /* Disable interrupt and pci device */
9626 lpfc_sli_disable_intr(phba);
9627 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -04009628}
9629
9630/**
9631 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9632 * @phba: pointer to lpfc hba data structure.
9633 *
9634 * This routine is called to prepare the SLI3 device for PCI slot permanently
9635 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9636 * pending I/Os.
9637 **/
9638static void
James Smart75baf692010-06-08 18:31:21 -04009639lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009640{
9641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009642 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009643 /* Block all SCSI devices' I/Os on the host */
9644 lpfc_scsi_dev_block(phba);
9645
9646 /* stop all timers */
9647 lpfc_stop_hba_timers(phba);
9648
James Smart0d878412009-10-02 15:16:56 -04009649 /* Clean up all driver's outstanding SCSI I/Os */
9650 lpfc_sli_flush_fcp_rings(phba);
9651}
9652
9653/**
James Smart3772a992009-05-22 14:50:54 -04009654 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009655 * @pdev: pointer to PCI device.
9656 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009657 *
James Smart3772a992009-05-22 14:50:54 -04009658 * This routine is called from the PCI subsystem for I/O error handling to
9659 * device with SLI-3 interface spec. This function is called by the PCI
9660 * subsystem after a PCI bus error affecting this device has been detected.
9661 * When this function is invoked, it will need to stop all the I/Os and
9662 * interrupt(s) to the device. Once that is done, it will return
9663 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9664 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009665 *
9666 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009667 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009668 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9669 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009670 **/
James Smart3772a992009-05-22 14:50:54 -04009671static pci_ers_result_t
9672lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009673{
James Smart51ef4c22007-08-02 11:10:31 -04009674 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9675 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009676
James Smart0d878412009-10-02 15:16:56 -04009677 switch (state) {
9678 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009679 /* Non-fatal error, prepare for recovery */
9680 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009681 return PCI_ERS_RESULT_CAN_RECOVER;
9682 case pci_channel_io_frozen:
9683 /* Fatal error, prepare for slot reset */
9684 lpfc_sli_prep_dev_for_reset(phba);
9685 return PCI_ERS_RESULT_NEED_RESET;
9686 case pci_channel_io_perm_failure:
9687 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009688 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009689 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009690 default:
9691 /* Unknown state, prepare and request slot reset */
9692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9693 "0472 Unknown PCI error state: x%x\n", state);
9694 lpfc_sli_prep_dev_for_reset(phba);
9695 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009696 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009697}
9698
9699/**
James Smart3772a992009-05-22 14:50:54 -04009700 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009701 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009702 *
James Smart3772a992009-05-22 14:50:54 -04009703 * This routine is called from the PCI subsystem for error handling to
9704 * device with SLI-3 interface spec. This is called after PCI bus has been
9705 * reset to restart the PCI card from scratch, as if from a cold-boot.
9706 * During the PCI subsystem error recovery, after driver returns
9707 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9708 * recovery and then call this routine before calling the .resume method
9709 * to recover the device. This function will initialize the HBA device,
9710 * enable the interrupt, but it will just put the HBA to offline state
9711 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009712 *
9713 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009714 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9715 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009716 */
James Smart3772a992009-05-22 14:50:54 -04009717static pci_ers_result_t
9718lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009719{
James Smart51ef4c22007-08-02 11:10:31 -04009720 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9721 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009722 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009723 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009724
9725 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009726 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009727 printk(KERN_ERR "lpfc: Cannot re-enable "
9728 "PCI device after reset.\n");
9729 return PCI_ERS_RESULT_DISCONNECT;
9730 }
9731
James Smart97207482008-12-04 22:39:19 -05009732 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009733
9734 /*
9735 * As the new kernel behavior of pci_restore_state() API call clears
9736 * device saved_state flag, need to save the restored state again.
9737 */
9738 pci_save_state(pdev);
9739
James Smart97207482008-12-04 22:39:19 -05009740 if (pdev->is_busmaster)
9741 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009742
James Smart92d7f7b2007-06-17 19:56:38 -05009743 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009744 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009745 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009746
James Smart5b75da22008-12-04 22:39:35 -05009747 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009748 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009749 if (intr_mode == LPFC_INTR_ERROR) {
9750 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9751 "0427 Cannot re-enable interrupt after "
9752 "slot reset.\n");
9753 return PCI_ERS_RESULT_DISCONNECT;
9754 } else
9755 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009756
James Smart75baf692010-06-08 18:31:21 -04009757 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009758 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009759 lpfc_offline(phba);
9760 lpfc_sli_brdrestart(phba);
9761
James Smart5b75da22008-12-04 22:39:35 -05009762 /* Log the current active interrupt mode */
9763 lpfc_log_intr_mode(phba, phba->intr_mode);
9764
Linas Vepstas8d63f372007-02-14 14:28:36 -06009765 return PCI_ERS_RESULT_RECOVERED;
9766}
9767
9768/**
James Smart3772a992009-05-22 14:50:54 -04009769 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009770 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009771 *
James Smart3772a992009-05-22 14:50:54 -04009772 * This routine is called from the PCI subsystem for error handling to device
9773 * with SLI-3 interface spec. It is called when kernel error recovery tells
9774 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9775 * error recovery. After this call, traffic can start to flow from this device
9776 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009777 */
James Smart3772a992009-05-22 14:50:54 -04009778static void
9779lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009780{
James Smart51ef4c22007-08-02 11:10:31 -04009781 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9782 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009783
James Smarte2af0d22010-03-15 11:25:32 -04009784 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009785 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009786
9787 /* Clean up Advanced Error Reporting (AER) if needed */
9788 if (phba->hba_flag & HBA_AER_ENABLED)
9789 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009790}
9791
James Smart3772a992009-05-22 14:50:54 -04009792/**
James Smartda0436e2009-05-22 14:51:39 -04009793 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9794 * @phba: pointer to lpfc hba data structure.
9795 *
9796 * returns the number of ELS/CT IOCBs to reserve
9797 **/
9798int
9799lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9800{
9801 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9802
James Smartf1126682009-06-10 17:22:44 -04009803 if (phba->sli_rev == LPFC_SLI_REV4) {
9804 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009805 return 10;
James Smartf1126682009-06-10 17:22:44 -04009806 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009807 return 25;
James Smartf1126682009-06-10 17:22:44 -04009808 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009809 return 50;
James Smartf1126682009-06-10 17:22:44 -04009810 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009811 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009812 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009813 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009814 else if (max_xri <= 2048)
9815 return 200;
9816 else
9817 return 250;
James Smartf1126682009-06-10 17:22:44 -04009818 } else
9819 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009820}
9821
9822/**
James Smart52d52442011-05-24 11:42:45 -04009823 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -04009824 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -04009825 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -04009826 *
James Smart52d52442011-05-24 11:42:45 -04009827 **/
James Smartce396282012-09-29 11:30:56 -04009828static void
9829lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -04009830{
James Smartce396282012-09-29 11:30:56 -04009831 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -05009832 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -04009833 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -04009834 struct list_head dma_buffer_list;
9835 int i, rc = 0;
9836 struct lpfc_dmabuf *dmabuf, *next;
9837 uint32_t offset = 0, temp_offset = 0;
9838
James Smartc71ab862012-10-31 14:44:33 -04009839 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -04009840 if (!fw) {
9841 rc = -ENXIO;
9842 goto out;
9843 }
9844 image = (struct lpfc_grp_hdr *)fw->data;
9845
James Smart52d52442011-05-24 11:42:45 -04009846 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009847 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9848 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9849 LPFC_FILE_TYPE_GROUP) ||
9850 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9851 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9853 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009854 "Magic:%x Type:%x ID:%x\n",
9855 be32_to_cpu(image->magic_number),
9856 bf_get_be32(lpfc_grp_hdr_file_type, image),
9857 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -04009858 rc = -EINVAL;
9859 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009860 }
9861 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009862 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009863 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -04009864 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -04009865 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009866 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009867 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9868 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9869 GFP_KERNEL);
9870 if (!dmabuf) {
9871 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009872 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009873 }
9874 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9875 SLI4_PAGE_SIZE,
9876 &dmabuf->phys,
9877 GFP_KERNEL);
9878 if (!dmabuf->virt) {
9879 kfree(dmabuf);
9880 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009881 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009882 }
9883 list_add_tail(&dmabuf->list, &dma_buffer_list);
9884 }
9885 while (offset < fw->size) {
9886 temp_offset = offset;
9887 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -04009888 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -04009889 memcpy(dmabuf->virt,
9890 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -04009891 fw->size - temp_offset);
9892 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -04009893 break;
9894 }
James Smart52d52442011-05-24 11:42:45 -04009895 memcpy(dmabuf->virt, fw->data + temp_offset,
9896 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -04009897 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -04009898 }
9899 rc = lpfc_wr_object(phba, &dma_buffer_list,
9900 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -04009901 if (rc)
9902 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009903 }
9904 rc = offset;
9905 }
James Smartce396282012-09-29 11:30:56 -04009906
9907release_out:
James Smart52d52442011-05-24 11:42:45 -04009908 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9909 list_del(&dmabuf->list);
9910 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9911 dmabuf->virt, dmabuf->phys);
9912 kfree(dmabuf);
9913 }
James Smartce396282012-09-29 11:30:56 -04009914 release_firmware(fw);
9915out:
9916 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -04009917 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -04009918 return;
James Smart52d52442011-05-24 11:42:45 -04009919}
9920
9921/**
James Smartc71ab862012-10-31 14:44:33 -04009922 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
9923 * @phba: pointer to lpfc hba data structure.
9924 *
9925 * This routine is called to perform Linux generic firmware upgrade on device
9926 * that supports such feature.
9927 **/
9928int
9929lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
9930{
9931 uint8_t file_name[ELX_MODEL_NAME_SIZE];
9932 int ret;
9933 const struct firmware *fw;
9934
9935 /* Only supported on SLI4 interface type 2 for now */
9936 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
9937 LPFC_SLI_INTF_IF_TYPE_2)
9938 return -EPERM;
9939
9940 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
9941
9942 if (fw_upgrade == INT_FW_UPGRADE) {
9943 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
9944 file_name, &phba->pcidev->dev,
9945 GFP_KERNEL, (void *)phba,
9946 lpfc_write_firmware);
9947 } else if (fw_upgrade == RUN_FW_UPGRADE) {
9948 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
9949 if (!ret)
9950 lpfc_write_firmware(fw, (void *)phba);
9951 } else {
9952 ret = -EINVAL;
9953 }
9954
9955 return ret;
9956}
9957
9958/**
James Smartda0436e2009-05-22 14:51:39 -04009959 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
9960 * @pdev: pointer to PCI device
9961 * @pid: pointer to PCI device identifier
9962 *
9963 * This routine is called from the kernel's PCI subsystem to device with
9964 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9965 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9966 * information of the device and driver to see if the driver state that it
9967 * can support this kind of device. If the match is successful, the driver
9968 * core invokes this routine. If this routine determines it can claim the HBA,
9969 * it does all the initialization that it needs to do to handle the HBA
9970 * properly.
9971 *
9972 * Return code
9973 * 0 - driver can claim the device
9974 * negative value - driver can not claim the device
9975 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009976static int
James Smartda0436e2009-05-22 14:51:39 -04009977lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
9978{
9979 struct lpfc_hba *phba;
9980 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009981 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -04009982 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -04009983 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -04009984 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04009985
9986 /* Allocate memory for HBA structure */
9987 phba = lpfc_hba_alloc(pdev);
9988 if (!phba)
9989 return -ENOMEM;
9990
9991 /* Perform generic PCI device enabling operation */
9992 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009993 if (error)
James Smartda0436e2009-05-22 14:51:39 -04009994 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -04009995
9996 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9997 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9998 if (error)
9999 goto out_disable_pci_dev;
10000
10001 /* Set up SLI-4 specific device PCI memory space */
10002 error = lpfc_sli4_pci_mem_setup(phba);
10003 if (error) {
10004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10005 "1410 Failed to set up pci memory space.\n");
10006 goto out_disable_pci_dev;
10007 }
10008
10009 /* Set up phase-1 common device driver resources */
10010 error = lpfc_setup_driver_resource_phase1(phba);
10011 if (error) {
10012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10013 "1411 Failed to set up driver resource.\n");
10014 goto out_unset_pci_mem_s4;
10015 }
10016
10017 /* Set up SLI-4 Specific device driver resources */
10018 error = lpfc_sli4_driver_resource_setup(phba);
10019 if (error) {
10020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10021 "1412 Failed to set up driver resource.\n");
10022 goto out_unset_pci_mem_s4;
10023 }
10024
10025 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010026
10027 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10028 "2821 initialize iocb list %d.\n",
10029 phba->cfg_iocb_cnt*1024);
10030 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10031
James Smartda0436e2009-05-22 14:51:39 -040010032 if (error) {
10033 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10034 "1413 Failed to initialize iocb list.\n");
10035 goto out_unset_driver_resource_s4;
10036 }
10037
James Smart19ca7602010-11-20 23:11:55 -050010038 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010039 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010040
James Smartda0436e2009-05-22 14:51:39 -040010041 /* Set up common device driver resources */
10042 error = lpfc_setup_driver_resource_phase2(phba);
10043 if (error) {
10044 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10045 "1414 Failed to set up driver resource.\n");
10046 goto out_free_iocb_list;
10047 }
10048
James Smart079b5c92011-08-21 21:48:49 -040010049 /* Get the default values for Model Name and Description */
10050 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10051
James Smartda0436e2009-05-22 14:51:39 -040010052 /* Create SCSI host to the physical port */
10053 error = lpfc_create_shost(phba);
10054 if (error) {
10055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10056 "1415 Failed to create scsi host.\n");
10057 goto out_unset_driver_resource;
10058 }
10059
10060 /* Configure sysfs attributes */
10061 vport = phba->pport;
10062 error = lpfc_alloc_sysfs_attr(vport);
10063 if (error) {
10064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10065 "1416 Failed to allocate sysfs attr\n");
10066 goto out_destroy_shost;
10067 }
10068
James Smart6669f9b2009-10-02 15:16:45 -040010069 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010070 /* Now, trying to enable interrupt and bring up the device */
10071 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010072
James Smart7b15db32013-01-03 15:43:29 -050010073 /* Put device to a known state before enabling interrupt */
10074 lpfc_stop_port(phba);
10075 /* Configure and enable interrupt */
10076 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10077 if (intr_mode == LPFC_INTR_ERROR) {
10078 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10079 "0426 Failed to enable interrupt.\n");
10080 error = -ENODEV;
10081 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010082 }
James Smart7b15db32013-01-03 15:43:29 -050010083 /* Default to single EQ for non-MSI-X */
10084 if (phba->intr_type != MSIX)
10085 adjusted_fcp_io_channel = 1;
10086 else
10087 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10088 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10089 /* Set up SLI-4 HBA */
10090 if (lpfc_sli4_hba_setup(phba)) {
10091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10092 "1421 Failed to set up hba\n");
10093 error = -ENODEV;
10094 goto out_disable_intr;
10095 }
10096
10097 /* Log the current active interrupt mode */
10098 phba->intr_mode = intr_mode;
10099 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010100
10101 /* Perform post initialization setup */
10102 lpfc_post_init_setup(phba);
10103
James Smartc71ab862012-10-31 14:44:33 -040010104 /* check for firmware upgrade or downgrade */
10105 if (phba->cfg_request_firmware_upgrade)
10106 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010107
James Smart1c6834a2009-07-19 10:01:26 -040010108 /* Check if there are static vports to be created. */
10109 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010110 return 0;
10111
10112out_disable_intr:
10113 lpfc_sli4_disable_intr(phba);
10114out_free_sysfs_attr:
10115 lpfc_free_sysfs_attr(vport);
10116out_destroy_shost:
10117 lpfc_destroy_shost(phba);
10118out_unset_driver_resource:
10119 lpfc_unset_driver_resource_phase2(phba);
10120out_free_iocb_list:
10121 lpfc_free_iocb_list(phba);
10122out_unset_driver_resource_s4:
10123 lpfc_sli4_driver_resource_unset(phba);
10124out_unset_pci_mem_s4:
10125 lpfc_sli4_pci_mem_unset(phba);
10126out_disable_pci_dev:
10127 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010128 if (shost)
10129 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010130out_free_phba:
10131 lpfc_hba_free(phba);
10132 return error;
10133}
10134
10135/**
10136 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10137 * @pdev: pointer to PCI device
10138 *
10139 * This routine is called from the kernel's PCI subsystem to device with
10140 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10141 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10142 * device to be removed from the PCI subsystem properly.
10143 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010144static void
James Smartda0436e2009-05-22 14:51:39 -040010145lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10146{
10147 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10148 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10149 struct lpfc_vport **vports;
10150 struct lpfc_hba *phba = vport->phba;
10151 int i;
10152
10153 /* Mark the device unloading flag */
10154 spin_lock_irq(&phba->hbalock);
10155 vport->load_flag |= FC_UNLOADING;
10156 spin_unlock_irq(&phba->hbalock);
10157
10158 /* Free the HBA sysfs attributes */
10159 lpfc_free_sysfs_attr(vport);
10160
10161 /* Release all the vports against this physical port */
10162 vports = lpfc_create_vport_work_array(phba);
10163 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010164 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10165 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10166 continue;
James Smartda0436e2009-05-22 14:51:39 -040010167 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010168 }
James Smartda0436e2009-05-22 14:51:39 -040010169 lpfc_destroy_vport_work_array(phba, vports);
10170
10171 /* Remove FC host and then SCSI host with the physical port */
10172 fc_remove_host(shost);
10173 scsi_remove_host(shost);
10174
10175 /* Perform cleanup on the physical port */
10176 lpfc_cleanup(vport);
10177
10178 /*
10179 * Bring down the SLI Layer. This step disables all interrupts,
10180 * clears the rings, discards all mailbox commands, and resets
10181 * the HBA FCoE function.
10182 */
10183 lpfc_debugfs_terminate(vport);
10184 lpfc_sli4_hba_unset(phba);
10185
10186 spin_lock_irq(&phba->hbalock);
10187 list_del_init(&vport->listentry);
10188 spin_unlock_irq(&phba->hbalock);
10189
James Smart3677a3a2010-09-29 11:19:14 -040010190 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010191 * buffers are released to their corresponding pools here.
10192 */
10193 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010194
James Smartda0436e2009-05-22 14:51:39 -040010195 lpfc_sli4_driver_resource_unset(phba);
10196
10197 /* Unmap adapter Control and Doorbell registers */
10198 lpfc_sli4_pci_mem_unset(phba);
10199
10200 /* Release PCI resources and disable device's PCI function */
10201 scsi_host_put(shost);
10202 lpfc_disable_pci_dev(phba);
10203
10204 /* Finally, free the driver's device data structure */
10205 lpfc_hba_free(phba);
10206
10207 return;
10208}
10209
10210/**
10211 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10212 * @pdev: pointer to PCI device
10213 * @msg: power management message
10214 *
10215 * This routine is called from the kernel's PCI subsystem to support system
10216 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10217 * this method, it quiesces the device by stopping the driver's worker
10218 * thread for the device, turning off device's interrupt and DMA, and bring
10219 * the device offline. Note that as the driver implements the minimum PM
10220 * requirements to a power-aware driver's PM support for suspend/resume -- all
10221 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10222 * method call will be treated as SUSPEND and the driver will fully
10223 * reinitialize its device during resume() method call, the driver will set
10224 * device to PCI_D3hot state in PCI config space instead of setting it
10225 * according to the @msg provided by the PM.
10226 *
10227 * Return code
10228 * 0 - driver suspended the device
10229 * Error otherwise
10230 **/
10231static int
10232lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10233{
10234 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10235 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10236
10237 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010238 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010239
10240 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010241 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010242 lpfc_offline(phba);
10243 kthread_stop(phba->worker_thread);
10244
10245 /* Disable interrupt from device */
10246 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010247 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010248
10249 /* Save device state to PCI config space */
10250 pci_save_state(pdev);
10251 pci_set_power_state(pdev, PCI_D3hot);
10252
10253 return 0;
10254}
10255
10256/**
10257 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10258 * @pdev: pointer to PCI device
10259 *
10260 * This routine is called from the kernel's PCI subsystem to support system
10261 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10262 * this method, it restores the device's PCI config space state and fully
10263 * reinitializes the device and brings it online. Note that as the driver
10264 * implements the minimum PM requirements to a power-aware driver's PM for
10265 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10266 * to the suspend() method call will be treated as SUSPEND and the driver
10267 * will fully reinitialize its device during resume() method call, the device
10268 * will be set to PCI_D0 directly in PCI config space before restoring the
10269 * state.
10270 *
10271 * Return code
10272 * 0 - driver suspended the device
10273 * Error otherwise
10274 **/
10275static int
10276lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10277{
10278 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10279 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10280 uint32_t intr_mode;
10281 int error;
10282
10283 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10284 "0292 PCI device Power Management resume.\n");
10285
10286 /* Restore device state from PCI config space */
10287 pci_set_power_state(pdev, PCI_D0);
10288 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010289
10290 /*
10291 * As the new kernel behavior of pci_restore_state() API call clears
10292 * device saved_state flag, need to save the restored state again.
10293 */
10294 pci_save_state(pdev);
10295
James Smartda0436e2009-05-22 14:51:39 -040010296 if (pdev->is_busmaster)
10297 pci_set_master(pdev);
10298
10299 /* Startup the kernel thread for this host adapter. */
10300 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10301 "lpfc_worker_%d", phba->brd_no);
10302 if (IS_ERR(phba->worker_thread)) {
10303 error = PTR_ERR(phba->worker_thread);
10304 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10305 "0293 PM resume failed to start worker "
10306 "thread: error=x%x.\n", error);
10307 return error;
10308 }
10309
10310 /* Configure and enable interrupt */
10311 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10312 if (intr_mode == LPFC_INTR_ERROR) {
10313 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10314 "0294 PM resume Failed to enable interrupt\n");
10315 return -EIO;
10316 } else
10317 phba->intr_mode = intr_mode;
10318
10319 /* Restart HBA and bring it online */
10320 lpfc_sli_brdrestart(phba);
10321 lpfc_online(phba);
10322
10323 /* Log the current active interrupt mode */
10324 lpfc_log_intr_mode(phba, phba->intr_mode);
10325
10326 return 0;
10327}
10328
10329/**
James Smart75baf692010-06-08 18:31:21 -040010330 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10331 * @phba: pointer to lpfc hba data structure.
10332 *
10333 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10334 * aborts all the outstanding SCSI I/Os to the pci device.
10335 **/
10336static void
10337lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10338{
10339 struct lpfc_sli *psli = &phba->sli;
10340 struct lpfc_sli_ring *pring;
10341
10342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10343 "2828 PCI channel I/O abort preparing for recovery\n");
10344 /*
10345 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10346 * and let the SCSI mid-layer to retry them to recover.
10347 */
10348 pring = &psli->ring[psli->fcp_ring];
10349 lpfc_sli_abort_iocb_ring(phba, pring);
10350}
10351
10352/**
10353 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10354 * @phba: pointer to lpfc hba data structure.
10355 *
10356 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10357 * disables the device interrupt and pci device, and aborts the internal FCP
10358 * pending I/Os.
10359 **/
10360static void
10361lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10362{
10363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10364 "2826 PCI channel disable preparing for reset\n");
10365
10366 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010367 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010368
10369 /* Block all SCSI devices' I/Os on the host */
10370 lpfc_scsi_dev_block(phba);
10371
James Smartea714f32013-04-17 20:18:47 -040010372 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10373 lpfc_sli_flush_fcp_rings(phba);
10374
James Smart75baf692010-06-08 18:31:21 -040010375 /* stop all timers */
10376 lpfc_stop_hba_timers(phba);
10377
10378 /* Disable interrupt and pci device */
10379 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010380 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010381 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010382}
10383
10384/**
10385 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10386 * @phba: pointer to lpfc hba data structure.
10387 *
10388 * This routine is called to prepare the SLI4 device for PCI slot permanently
10389 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10390 * pending I/Os.
10391 **/
10392static void
10393lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10394{
10395 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10396 "2827 PCI channel permanent disable for failure\n");
10397
10398 /* Block all SCSI devices' I/Os on the host */
10399 lpfc_scsi_dev_block(phba);
10400
10401 /* stop all timers */
10402 lpfc_stop_hba_timers(phba);
10403
10404 /* Clean up all driver's outstanding SCSI I/Os */
10405 lpfc_sli_flush_fcp_rings(phba);
10406}
10407
10408/**
James Smartda0436e2009-05-22 14:51:39 -040010409 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10410 * @pdev: pointer to PCI device.
10411 * @state: the current PCI connection state.
10412 *
10413 * This routine is called from the PCI subsystem for error handling to device
10414 * with SLI-4 interface spec. This function is called by the PCI subsystem
10415 * after a PCI bus error affecting this device has been detected. When this
10416 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10417 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10418 * for the PCI subsystem to perform proper recovery as desired.
10419 *
10420 * Return codes
10421 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10422 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10423 **/
10424static pci_ers_result_t
10425lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10426{
James Smart75baf692010-06-08 18:31:21 -040010427 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10428 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10429
10430 switch (state) {
10431 case pci_channel_io_normal:
10432 /* Non-fatal error, prepare for recovery */
10433 lpfc_sli4_prep_dev_for_recover(phba);
10434 return PCI_ERS_RESULT_CAN_RECOVER;
10435 case pci_channel_io_frozen:
10436 /* Fatal error, prepare for slot reset */
10437 lpfc_sli4_prep_dev_for_reset(phba);
10438 return PCI_ERS_RESULT_NEED_RESET;
10439 case pci_channel_io_perm_failure:
10440 /* Permanent failure, prepare for device down */
10441 lpfc_sli4_prep_dev_for_perm_failure(phba);
10442 return PCI_ERS_RESULT_DISCONNECT;
10443 default:
10444 /* Unknown state, prepare and request slot reset */
10445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10446 "2825 Unknown PCI error state: x%x\n", state);
10447 lpfc_sli4_prep_dev_for_reset(phba);
10448 return PCI_ERS_RESULT_NEED_RESET;
10449 }
James Smartda0436e2009-05-22 14:51:39 -040010450}
10451
10452/**
10453 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10454 * @pdev: pointer to PCI device.
10455 *
10456 * This routine is called from the PCI subsystem for error handling to device
10457 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10458 * restart the PCI card from scratch, as if from a cold-boot. During the
10459 * PCI subsystem error recovery, after the driver returns
10460 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10461 * recovery and then call this routine before calling the .resume method to
10462 * recover the device. This function will initialize the HBA device, enable
10463 * the interrupt, but it will just put the HBA to offline state without
10464 * passing any I/O traffic.
10465 *
10466 * Return codes
10467 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10468 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10469 */
10470static pci_ers_result_t
10471lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10472{
James Smart75baf692010-06-08 18:31:21 -040010473 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10474 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10475 struct lpfc_sli *psli = &phba->sli;
10476 uint32_t intr_mode;
10477
10478 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10479 if (pci_enable_device_mem(pdev)) {
10480 printk(KERN_ERR "lpfc: Cannot re-enable "
10481 "PCI device after reset.\n");
10482 return PCI_ERS_RESULT_DISCONNECT;
10483 }
10484
10485 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010486
10487 /*
10488 * As the new kernel behavior of pci_restore_state() API call clears
10489 * device saved_state flag, need to save the restored state again.
10490 */
10491 pci_save_state(pdev);
10492
James Smart75baf692010-06-08 18:31:21 -040010493 if (pdev->is_busmaster)
10494 pci_set_master(pdev);
10495
10496 spin_lock_irq(&phba->hbalock);
10497 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10498 spin_unlock_irq(&phba->hbalock);
10499
10500 /* Configure and enable interrupt */
10501 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10502 if (intr_mode == LPFC_INTR_ERROR) {
10503 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10504 "2824 Cannot re-enable interrupt after "
10505 "slot reset.\n");
10506 return PCI_ERS_RESULT_DISCONNECT;
10507 } else
10508 phba->intr_mode = intr_mode;
10509
10510 /* Log the current active interrupt mode */
10511 lpfc_log_intr_mode(phba, phba->intr_mode);
10512
James Smartda0436e2009-05-22 14:51:39 -040010513 return PCI_ERS_RESULT_RECOVERED;
10514}
10515
10516/**
10517 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10518 * @pdev: pointer to PCI device
10519 *
10520 * This routine is called from the PCI subsystem for error handling to device
10521 * with SLI-4 interface spec. It is called when kernel error recovery tells
10522 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10523 * error recovery. After this call, traffic can start to flow from this device
10524 * again.
10525 **/
10526static void
10527lpfc_io_resume_s4(struct pci_dev *pdev)
10528{
James Smart75baf692010-06-08 18:31:21 -040010529 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10530 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10531
10532 /*
10533 * In case of slot reset, as function reset is performed through
10534 * mailbox command which needs DMA to be enabled, this operation
10535 * has to be moved to the io resume phase. Taking device offline
10536 * will perform the necessary cleanup.
10537 */
10538 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10539 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010540 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010541 lpfc_offline(phba);
10542 lpfc_sli_brdrestart(phba);
10543 /* Bring the device back online */
10544 lpfc_online(phba);
10545 }
10546
10547 /* Clean up Advanced Error Reporting (AER) if needed */
10548 if (phba->hba_flag & HBA_AER_ENABLED)
10549 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010550}
10551
10552/**
James Smart3772a992009-05-22 14:50:54 -040010553 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10554 * @pdev: pointer to PCI device
10555 * @pid: pointer to PCI device identifier
10556 *
10557 * This routine is to be registered to the kernel's PCI subsystem. When an
10558 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10559 * at PCI device-specific information of the device and driver to see if the
10560 * driver state that it can support this kind of device. If the match is
10561 * successful, the driver core invokes this routine. This routine dispatches
10562 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10563 * do all the initialization that it needs to do to handle the HBA device
10564 * properly.
10565 *
10566 * Return code
10567 * 0 - driver can claim the device
10568 * negative value - driver can not claim the device
10569 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010570static int
James Smart3772a992009-05-22 14:50:54 -040010571lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10572{
10573 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010574 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010575
James Smart28baac72010-02-12 14:42:03 -050010576 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010577 return -ENODEV;
10578
James Smart8fa38512009-07-19 10:01:03 -040010579 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010580 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010581 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010582 else
James Smart3772a992009-05-22 14:50:54 -040010583 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010584
James Smart3772a992009-05-22 14:50:54 -040010585 return rc;
10586}
10587
10588/**
10589 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10590 * @pdev: pointer to PCI device
10591 *
10592 * This routine is to be registered to the kernel's PCI subsystem. When an
10593 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10594 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10595 * remove routine, which will perform all the necessary cleanup for the
10596 * device to be removed from the PCI subsystem properly.
10597 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010598static void
James Smart3772a992009-05-22 14:50:54 -040010599lpfc_pci_remove_one(struct pci_dev *pdev)
10600{
10601 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10602 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10603
10604 switch (phba->pci_dev_grp) {
10605 case LPFC_PCI_DEV_LP:
10606 lpfc_pci_remove_one_s3(pdev);
10607 break;
James Smartda0436e2009-05-22 14:51:39 -040010608 case LPFC_PCI_DEV_OC:
10609 lpfc_pci_remove_one_s4(pdev);
10610 break;
James Smart3772a992009-05-22 14:50:54 -040010611 default:
10612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10613 "1424 Invalid PCI device group: 0x%x\n",
10614 phba->pci_dev_grp);
10615 break;
10616 }
10617 return;
10618}
10619
10620/**
10621 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10622 * @pdev: pointer to PCI device
10623 * @msg: power management message
10624 *
10625 * This routine is to be registered to the kernel's PCI subsystem to support
10626 * system Power Management (PM). When PM invokes this method, it dispatches
10627 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10628 * suspend the device.
10629 *
10630 * Return code
10631 * 0 - driver suspended the device
10632 * Error otherwise
10633 **/
10634static int
10635lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10636{
10637 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10638 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10639 int rc = -ENODEV;
10640
10641 switch (phba->pci_dev_grp) {
10642 case LPFC_PCI_DEV_LP:
10643 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10644 break;
James Smartda0436e2009-05-22 14:51:39 -040010645 case LPFC_PCI_DEV_OC:
10646 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10647 break;
James Smart3772a992009-05-22 14:50:54 -040010648 default:
10649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10650 "1425 Invalid PCI device group: 0x%x\n",
10651 phba->pci_dev_grp);
10652 break;
10653 }
10654 return rc;
10655}
10656
10657/**
10658 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10659 * @pdev: pointer to PCI device
10660 *
10661 * This routine is to be registered to the kernel's PCI subsystem to support
10662 * system Power Management (PM). When PM invokes this method, it dispatches
10663 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10664 * resume the device.
10665 *
10666 * Return code
10667 * 0 - driver suspended the device
10668 * Error otherwise
10669 **/
10670static int
10671lpfc_pci_resume_one(struct pci_dev *pdev)
10672{
10673 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10674 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10675 int rc = -ENODEV;
10676
10677 switch (phba->pci_dev_grp) {
10678 case LPFC_PCI_DEV_LP:
10679 rc = lpfc_pci_resume_one_s3(pdev);
10680 break;
James Smartda0436e2009-05-22 14:51:39 -040010681 case LPFC_PCI_DEV_OC:
10682 rc = lpfc_pci_resume_one_s4(pdev);
10683 break;
James Smart3772a992009-05-22 14:50:54 -040010684 default:
10685 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10686 "1426 Invalid PCI device group: 0x%x\n",
10687 phba->pci_dev_grp);
10688 break;
10689 }
10690 return rc;
10691}
10692
10693/**
10694 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10695 * @pdev: pointer to PCI device.
10696 * @state: the current PCI connection state.
10697 *
10698 * This routine is registered to the PCI subsystem for error handling. This
10699 * function is called by the PCI subsystem after a PCI bus error affecting
10700 * this device has been detected. When this routine is invoked, it dispatches
10701 * the action to the proper SLI-3 or SLI-4 device error detected handling
10702 * routine, which will perform the proper error detected operation.
10703 *
10704 * Return codes
10705 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10706 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10707 **/
10708static pci_ers_result_t
10709lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10710{
10711 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10712 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10713 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10714
10715 switch (phba->pci_dev_grp) {
10716 case LPFC_PCI_DEV_LP:
10717 rc = lpfc_io_error_detected_s3(pdev, state);
10718 break;
James Smartda0436e2009-05-22 14:51:39 -040010719 case LPFC_PCI_DEV_OC:
10720 rc = lpfc_io_error_detected_s4(pdev, state);
10721 break;
James Smart3772a992009-05-22 14:50:54 -040010722 default:
10723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10724 "1427 Invalid PCI device group: 0x%x\n",
10725 phba->pci_dev_grp);
10726 break;
10727 }
10728 return rc;
10729}
10730
10731/**
10732 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10733 * @pdev: pointer to PCI device.
10734 *
10735 * This routine is registered to the PCI subsystem for error handling. This
10736 * function is called after PCI bus has been reset to restart the PCI card
10737 * from scratch, as if from a cold-boot. When this routine is invoked, it
10738 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10739 * routine, which will perform the proper device reset.
10740 *
10741 * Return codes
10742 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10743 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10744 **/
10745static pci_ers_result_t
10746lpfc_io_slot_reset(struct pci_dev *pdev)
10747{
10748 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10749 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10750 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10751
10752 switch (phba->pci_dev_grp) {
10753 case LPFC_PCI_DEV_LP:
10754 rc = lpfc_io_slot_reset_s3(pdev);
10755 break;
James Smartda0436e2009-05-22 14:51:39 -040010756 case LPFC_PCI_DEV_OC:
10757 rc = lpfc_io_slot_reset_s4(pdev);
10758 break;
James Smart3772a992009-05-22 14:50:54 -040010759 default:
10760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10761 "1428 Invalid PCI device group: 0x%x\n",
10762 phba->pci_dev_grp);
10763 break;
10764 }
10765 return rc;
10766}
10767
10768/**
10769 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10770 * @pdev: pointer to PCI device
10771 *
10772 * This routine is registered to the PCI subsystem for error handling. It
10773 * is called when kernel error recovery tells the lpfc driver that it is
10774 * OK to resume normal PCI operation after PCI bus error recovery. When
10775 * this routine is invoked, it dispatches the action to the proper SLI-3
10776 * or SLI-4 device io_resume routine, which will resume the device operation.
10777 **/
10778static void
10779lpfc_io_resume(struct pci_dev *pdev)
10780{
10781 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10782 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10783
10784 switch (phba->pci_dev_grp) {
10785 case LPFC_PCI_DEV_LP:
10786 lpfc_io_resume_s3(pdev);
10787 break;
James Smartda0436e2009-05-22 14:51:39 -040010788 case LPFC_PCI_DEV_OC:
10789 lpfc_io_resume_s4(pdev);
10790 break;
James Smart3772a992009-05-22 14:50:54 -040010791 default:
10792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10793 "1429 Invalid PCI device group: 0x%x\n",
10794 phba->pci_dev_grp);
10795 break;
10796 }
10797 return;
10798}
10799
dea31012005-04-17 16:05:31 -050010800static struct pci_device_id lpfc_id_table[] = {
10801 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
10802 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040010803 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
10804 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010805 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
10806 PCI_ANY_ID, PCI_ANY_ID, },
10807 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
10808 PCI_ANY_ID, PCI_ANY_ID, },
10809 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
10810 PCI_ANY_ID, PCI_ANY_ID, },
10811 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
10812 PCI_ANY_ID, PCI_ANY_ID, },
10813 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
10814 PCI_ANY_ID, PCI_ANY_ID, },
10815 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
10816 PCI_ANY_ID, PCI_ANY_ID, },
10817 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
10818 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010819 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
10820 PCI_ANY_ID, PCI_ANY_ID, },
10821 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
10822 PCI_ANY_ID, PCI_ANY_ID, },
10823 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
10824 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010825 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
10826 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010827 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
10828 PCI_ANY_ID, PCI_ANY_ID, },
10829 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
10830 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010831 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
10832 PCI_ANY_ID, PCI_ANY_ID, },
10833 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
10834 PCI_ANY_ID, PCI_ANY_ID, },
10835 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
10836 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010837 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
10838 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010839 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
10840 PCI_ANY_ID, PCI_ANY_ID, },
10841 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
10842 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010843 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
10844 PCI_ANY_ID, PCI_ANY_ID, },
10845 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
10846 PCI_ANY_ID, PCI_ANY_ID, },
10847 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
10848 PCI_ANY_ID, PCI_ANY_ID, },
10849 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
10850 PCI_ANY_ID, PCI_ANY_ID, },
10851 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
10852 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010853 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
10854 PCI_ANY_ID, PCI_ANY_ID, },
10855 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
10856 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040010857 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
10858 PCI_ANY_ID, PCI_ANY_ID, },
10859 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
10860 PCI_ANY_ID, PCI_ANY_ID, },
10861 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
10862 PCI_ANY_ID, PCI_ANY_ID, },
10863 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
10864 PCI_ANY_ID, PCI_ANY_ID, },
10865 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
10866 PCI_ANY_ID, PCI_ANY_ID, },
10867 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
10868 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010869 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
10870 PCI_ANY_ID, PCI_ANY_ID, },
10871 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
10872 PCI_ANY_ID, PCI_ANY_ID, },
10873 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
10874 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040010875 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
10876 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050010877 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
10878 PCI_ANY_ID, PCI_ANY_ID, },
10879 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040010880 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040010881 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
10882 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050010883 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
10884 PCI_ANY_ID, PCI_ANY_ID, },
10885 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
10886 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040010887 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
10888 PCI_ANY_ID, PCI_ANY_ID, },
10889 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
10890 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040010891 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
10892 PCI_ANY_ID, PCI_ANY_ID, },
10893 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
10894 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010895 { 0 }
10896};
10897
10898MODULE_DEVICE_TABLE(pci, lpfc_id_table);
10899
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070010900static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010901 .error_detected = lpfc_io_error_detected,
10902 .slot_reset = lpfc_io_slot_reset,
10903 .resume = lpfc_io_resume,
10904};
10905
dea31012005-04-17 16:05:31 -050010906static struct pci_driver lpfc_driver = {
10907 .name = LPFC_DRIVER_NAME,
10908 .id_table = lpfc_id_table,
10909 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010910 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050010911 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040010912 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050010913 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050010914};
10915
James Smart3ef6d242012-01-18 16:23:48 -050010916static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040010917 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050010918};
10919
10920static struct miscdevice lpfc_mgmt_dev = {
10921 .minor = MISC_DYNAMIC_MINOR,
10922 .name = "lpfcmgmt",
10923 .fops = &lpfc_mgmt_fop,
10924};
10925
James Smarte59058c2008-08-24 21:49:00 -040010926/**
James Smart3621a712009-04-06 18:47:14 -040010927 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040010928 *
10929 * This routine is to be invoked when the lpfc module is loaded into the
10930 * kernel. The special kernel macro module_init() is used to indicate the
10931 * role of this routine to the kernel as lpfc module entry point.
10932 *
10933 * Return codes
10934 * 0 - successful
10935 * -ENOMEM - FC attach transport failed
10936 * all others - failed
10937 */
dea31012005-04-17 16:05:31 -050010938static int __init
10939lpfc_init(void)
10940{
James Smart7bb03bb2013-04-17 20:19:16 -040010941 int cpu;
dea31012005-04-17 16:05:31 -050010942 int error = 0;
10943
10944 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010945 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050010946
James Smart3ef6d242012-01-18 16:23:48 -050010947 error = misc_register(&lpfc_mgmt_dev);
10948 if (error)
10949 printk(KERN_ERR "Could not register lpfcmgmt device, "
10950 "misc_register returned with status %d", error);
10951
James Smart7ee5d432007-10-27 13:37:17 -040010952 if (lpfc_enable_npiv) {
10953 lpfc_transport_functions.vport_create = lpfc_vport_create;
10954 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10955 }
dea31012005-04-17 16:05:31 -050010956 lpfc_transport_template =
10957 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040010958 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050010959 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040010960 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040010961 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040010962 fc_attach_transport(&lpfc_vport_transport_functions);
10963 if (lpfc_vport_transport_template == NULL) {
10964 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010965 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040010966 }
James Smart7ee5d432007-10-27 13:37:17 -040010967 }
James Smart7bb03bb2013-04-17 20:19:16 -040010968
10969 /* Initialize in case vector mapping is needed */
10970 for (cpu = 0; cpu < LPFC_MAX_CPU; cpu++)
10971 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
10972
dea31012005-04-17 16:05:31 -050010973 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050010974 if (error) {
dea31012005-04-17 16:05:31 -050010975 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040010976 if (lpfc_enable_npiv)
10977 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050010978 }
dea31012005-04-17 16:05:31 -050010979
10980 return error;
10981}
10982
James Smarte59058c2008-08-24 21:49:00 -040010983/**
James Smart3621a712009-04-06 18:47:14 -040010984 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040010985 *
10986 * This routine is invoked when the lpfc module is removed from the kernel.
10987 * The special kernel macro module_exit() is used to indicate the role of
10988 * this routine to the kernel as lpfc module exit point.
10989 */
dea31012005-04-17 16:05:31 -050010990static void __exit
10991lpfc_exit(void)
10992{
James Smart3ef6d242012-01-18 16:23:48 -050010993 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050010994 pci_unregister_driver(&lpfc_driver);
10995 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010996 if (lpfc_enable_npiv)
10997 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050010998 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040010999 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11000 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011001 (1L << _dump_buf_data_order), _dump_buf_data);
11002 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11003 }
11004
11005 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011006 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11007 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011008 (1L << _dump_buf_dif_order), _dump_buf_dif);
11009 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11010 }
dea31012005-04-17 16:05:31 -050011011}
11012
11013module_init(lpfc_init);
11014module_exit(lpfc_exit);
11015MODULE_LICENSE("GPL");
11016MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11017MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11018MODULE_VERSION("0:" LPFC_DRIVER_VERSION);