blob: 03fa3f02e33e58de7a5d22ed09f227bbafea30d0 [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 Smart50611572016-03-31 14:12:34 -07004 * Copyright (C) 2004-2016 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 Gortmakeracf3368f2011-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>
James Smart895427b2017-02-12 13:52:30 -080037#include <linux/msi.h>
dea31012005-04-17 16:05:31 -050038
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040039#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050040#include <scsi/scsi_device.h>
41#include <scsi/scsi_host.h>
42#include <scsi/scsi_transport_fc.h>
43
James Smartda0436e2009-05-22 14:51:39 -040044#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050045#include "lpfc_hw.h"
46#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040047#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040048#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050050#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080051#include "lpfc_scsi.h"
52#include "lpfc_nvme.h"
dea31012005-04-17 16:05:31 -050053#include "lpfc_logmsg.h"
54#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050055#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050056#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070057#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050058
James Smart81301a92008-12-04 22:39:46 -050059char *_dump_buf_data;
60unsigned long _dump_buf_data_order;
61char *_dump_buf_dif;
62unsigned long _dump_buf_dif_order;
63spinlock_t _dump_buf_lock;
64
James Smart7bb03bb2013-04-17 20:19:16 -040065/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040066uint16_t *lpfc_used_cpu;
67uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040068
dea31012005-04-17 16:05:31 -050069static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
70static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040071static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040072static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
73static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040074static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040075static void lpfc_free_els_sgl_list(struct lpfc_hba *);
James Smartf358dd02017-02-12 13:52:34 -080076static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040077static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040078static int lpfc_init_active_sgl_array(struct lpfc_hba *);
79static void lpfc_free_active_sgl(struct lpfc_hba *);
80static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
81static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
82static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
83static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
84static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040085static void lpfc_sli4_disable_intr(struct lpfc_hba *);
86static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050087static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050088
89static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050090static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050091static DEFINE_IDR(lpfc_hba_index);
James Smartf358dd02017-02-12 13:52:34 -080092#define LPFC_NVMET_BUF_POST 254
dea31012005-04-17 16:05:31 -050093
James Smarte59058c2008-08-24 21:49:00 -040094/**
James Smart3621a712009-04-06 18:47:14 -040095 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040096 * @phba: pointer to lpfc hba data structure.
97 *
98 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
99 * mailbox command. It retrieves the revision information from the HBA and
100 * collects the Vital Product Data (VPD) about the HBA for preparing the
101 * configuration of the HBA.
102 *
103 * Return codes:
104 * 0 - success.
105 * -ERESTART - requests the SLI layer to reset the HBA and try again.
106 * Any other value - indicates an error.
107 **/
dea31012005-04-17 16:05:31 -0500108int
James Smart2e0fef82007-06-17 19:56:36 -0500109lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500110{
111 lpfc_vpd_t *vp = &phba->vpd;
112 int i = 0, rc;
113 LPFC_MBOXQ_t *pmb;
114 MAILBOX_t *mb;
115 char *lpfc_vpd_data = NULL;
116 uint16_t offset = 0;
117 static char licensed[56] =
118 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400119 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500120
121 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
122 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500123 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500124 return -ENOMEM;
125 }
126
James Smart04c68492009-05-22 14:52:52 -0400127 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500128 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500129
130 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400131 if (init_key) {
132 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500133
James Smart65a29c12006-07-06 15:50:50 -0400134 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
135 *ptext = cpu_to_be32(*ptext);
136 init_key = 0;
137 }
dea31012005-04-17 16:05:31 -0500138
139 lpfc_read_nv(phba, pmb);
140 memset((char*)mb->un.varRDnvp.rsvd3, 0,
141 sizeof (mb->un.varRDnvp.rsvd3));
142 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
143 sizeof (licensed));
144
145 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
146
147 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500148 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400149 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500150 "error, mbxCmd x%x READ_NVPARM, "
151 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500152 mb->mbxCommand, mb->mbxStatus);
153 mempool_free(pmb, phba->mbox_mem_pool);
154 return -ERESTART;
155 }
156 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500157 sizeof(phba->wwnn));
158 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
159 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500160 }
161
James Smart92d7f7b2007-06-17 19:56:38 -0500162 phba->sli3_options = 0x0;
163
dea31012005-04-17 16:05:31 -0500164 /* Setup and issue mailbox READ REV command */
165 lpfc_read_rev(phba, pmb);
166 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
167 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400169 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500170 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500171 mb->mbxCommand, mb->mbxStatus);
172 mempool_free( pmb, phba->mbox_mem_pool);
173 return -ERESTART;
174 }
175
James Smart92d7f7b2007-06-17 19:56:38 -0500176
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500177 /*
178 * The value of rr must be 1 since the driver set the cv field to 1.
179 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500180 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500181 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500182 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400184 "0440 Adapter failed to init, READ_REV has "
185 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500186 mempool_free(pmb, phba->mbox_mem_pool);
187 return -ERESTART;
dea31012005-04-17 16:05:31 -0500188 }
189
James Smart495a7142008-06-14 22:52:59 -0400190 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
191 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500192 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400193 }
James Smarted957682007-06-17 19:56:37 -0500194
dea31012005-04-17 16:05:31 -0500195 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500196 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500197 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500198 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
199 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
200 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
201 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500202 vp->rev.biuRev = mb->un.varRdRev.biuRev;
203 vp->rev.smRev = mb->un.varRdRev.smRev;
204 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
205 vp->rev.endecRev = mb->un.varRdRev.endecRev;
206 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
207 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
208 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
209 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
210 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
211 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
212
James Smart92d7f7b2007-06-17 19:56:38 -0500213 /* If the sli feature level is less then 9, we must
214 * tear down all RPIs and VPIs on link down if NPIV
215 * is enabled.
216 */
217 if (vp->rev.feaLevelHigh < 9)
218 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
219
dea31012005-04-17 16:05:31 -0500220 if (lpfc_is_LC_HBA(phba->pcidev->device))
221 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
222 sizeof (phba->RandomData));
223
dea31012005-04-17 16:05:31 -0500224 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500225 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
226 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400227 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500228 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400229 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500230 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
231
232 if (rc != MBX_SUCCESS) {
233 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400234 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500235 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500236 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500237 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500238 }
James Smart04c68492009-05-22 14:52:52 -0400239 /* dump mem may return a zero when finished or we got a
240 * mailbox error, either way we are done.
241 */
242 if (mb->un.varDmp.word_cnt == 0)
243 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500244 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
245 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400246 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
247 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500248 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500249 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500250 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
251 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500252
253 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500254out_free_mbox:
255 mempool_free(pmb, phba->mbox_mem_pool);
256 return 0;
257}
258
James Smarte59058c2008-08-24 21:49:00 -0400259/**
James Smart3621a712009-04-06 18:47:14 -0400260 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400261 * @phba: pointer to lpfc hba data structure.
262 * @pmboxq: pointer to the driver internal queue element for mailbox command.
263 *
264 * This is the completion handler for driver's configuring asynchronous event
265 * mailbox command to the device. If the mailbox command returns successfully,
266 * it will set internal async event support flag to 1; otherwise, it will
267 * set internal async event support flag to 0.
268 **/
James Smart57127f12007-10-27 13:37:05 -0400269static void
270lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
271{
James Smart04c68492009-05-22 14:52:52 -0400272 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400273 phba->temp_sensor_support = 1;
274 else
275 phba->temp_sensor_support = 0;
276 mempool_free(pmboxq, phba->mbox_mem_pool);
277 return;
278}
279
James Smarte59058c2008-08-24 21:49:00 -0400280/**
James Smart3621a712009-04-06 18:47:14 -0400281 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500282 * @phba: pointer to lpfc hba data structure.
283 * @pmboxq: pointer to the driver internal queue element for mailbox command.
284 *
285 * This is the completion handler for dump mailbox command for getting
286 * wake up parameters. When this command complete, the response contain
287 * Option rom version of the HBA. This function translate the version number
288 * into a human readable string and store it in OptionROMVersion.
289 **/
290static void
291lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
292{
293 struct prog_id *prg;
294 uint32_t prog_id_word;
295 char dist = ' ';
296 /* character array used for decoding dist type. */
297 char dist_char[] = "nabx";
298
James Smart04c68492009-05-22 14:52:52 -0400299 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500300 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500301 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500302 }
James Smart97207482008-12-04 22:39:19 -0500303
304 prg = (struct prog_id *) &prog_id_word;
305
306 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400307 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500308
309 /* Decode the Option rom version word to a readable string */
310 if (prg->dist < 4)
311 dist = dist_char[prg->dist];
312
313 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400314 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500315 prg->ver, prg->rev, prg->lev);
316 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400317 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500318 prg->ver, prg->rev, prg->lev,
319 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500320 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500321 return;
322}
323
324/**
James Smart05580562011-05-24 11:40:48 -0400325 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
326 * cfg_soft_wwnn, cfg_soft_wwpn
327 * @vport: pointer to lpfc vport data structure.
328 *
329 *
330 * Return codes
331 * None.
332 **/
333void
334lpfc_update_vport_wwn(struct lpfc_vport *vport)
335{
336 /* If the soft name exists then update it using the service params */
337 if (vport->phba->cfg_soft_wwnn)
338 u64_to_wwn(vport->phba->cfg_soft_wwnn,
339 vport->fc_sparam.nodeName.u.wwn);
340 if (vport->phba->cfg_soft_wwpn)
341 u64_to_wwn(vport->phba->cfg_soft_wwpn,
342 vport->fc_sparam.portName.u.wwn);
343
344 /*
345 * If the name is empty or there exists a soft name
346 * then copy the service params name, otherwise use the fc name
347 */
348 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
349 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
350 sizeof(struct lpfc_name));
351 else
352 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
353 sizeof(struct lpfc_name));
354
355 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
356 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
357 sizeof(struct lpfc_name));
358 else
359 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
360 sizeof(struct lpfc_name));
361}
362
363/**
James Smart3621a712009-04-06 18:47:14 -0400364 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400365 * @phba: pointer to lpfc hba data structure.
366 *
367 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
368 * command call. It performs all internal resource and state setups on the
369 * port: post IOCB buffers, enable appropriate host interrupt attentions,
370 * ELS ring timers, etc.
371 *
372 * Return codes
373 * 0 - success.
374 * Any other value - error.
375 **/
dea31012005-04-17 16:05:31 -0500376int
James Smart2e0fef82007-06-17 19:56:36 -0500377lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500378{
James Smart2e0fef82007-06-17 19:56:36 -0500379 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400380 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500381 LPFC_MBOXQ_t *pmb;
382 MAILBOX_t *mb;
383 struct lpfc_dmabuf *mp;
384 struct lpfc_sli *psli = &phba->sli;
385 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500386 int i, j;
387 int rc;
dea31012005-04-17 16:05:31 -0500388
James Smart7af67052007-10-27 13:38:11 -0400389 spin_lock_irq(&phba->hbalock);
390 /*
391 * If the Config port completed correctly the HBA is not
392 * over heated any more.
393 */
394 if (phba->over_temp_state == HBA_OVER_TEMP)
395 phba->over_temp_state = HBA_NORMAL_TEMP;
396 spin_unlock_irq(&phba->hbalock);
397
dea31012005-04-17 16:05:31 -0500398 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
399 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500400 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500401 return -ENOMEM;
402 }
James Smart04c68492009-05-22 14:52:52 -0400403 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500404
dea31012005-04-17 16:05:31 -0500405 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500406 rc = lpfc_read_sparam(phba, pmb, 0);
407 if (rc) {
408 mempool_free(pmb, phba->mbox_mem_pool);
409 return -ENOMEM;
410 }
411
James Smarted957682007-06-17 19:56:37 -0500412 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500413 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500414 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400415 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500416 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500417 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500418 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500419 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500420 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500421 lpfc_mbuf_free(phba, mp->virt, mp->phys);
422 kfree(mp);
423 return -EIO;
424 }
425
426 mp = (struct lpfc_dmabuf *) pmb->context1;
427
James Smart2e0fef82007-06-17 19:56:36 -0500428 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500429 lpfc_mbuf_free(phba, mp->virt, mp->phys);
430 kfree(mp);
431 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400432 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400433
434 /* Update the fc_host data structures with new wwn. */
435 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
436 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400437 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400438
dea31012005-04-17 16:05:31 -0500439 /* If no serial number in VPD data, use low 6 bytes of WWNN */
440 /* This should be consolidated into parse_vpd ? - mr */
441 if (phba->SerialNumber[0] == 0) {
442 uint8_t *outptr;
443
James Smart2e0fef82007-06-17 19:56:36 -0500444 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500445 for (i = 0; i < 12; i++) {
446 status = *outptr++;
447 j = ((status & 0xf0) >> 4);
448 if (j <= 9)
449 phba->SerialNumber[i] =
450 (char)((uint8_t) 0x30 + (uint8_t) j);
451 else
452 phba->SerialNumber[i] =
453 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
454 i++;
455 j = (status & 0xf);
456 if (j <= 9)
457 phba->SerialNumber[i] =
458 (char)((uint8_t) 0x30 + (uint8_t) j);
459 else
460 phba->SerialNumber[i] =
461 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
462 }
463 }
464
dea31012005-04-17 16:05:31 -0500465 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500466 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500467 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500468 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400469 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500470 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500471 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500472 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500473 mempool_free( pmb, phba->mbox_mem_pool);
474 return -EIO;
475 }
476
James Smarta0c87cb2009-07-19 10:01:10 -0400477 /* Check if the port is disabled */
478 lpfc_sli_read_link_ste(phba);
479
dea31012005-04-17 16:05:31 -0500480 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400481 i = (mb->un.varRdConfig.max_xri + 1);
482 if (phba->cfg_hba_queue_depth > i) {
483 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
484 "3359 HBA queue depth changed from %d to %d\n",
485 phba->cfg_hba_queue_depth, i);
486 phba->cfg_hba_queue_depth = i;
487 }
488
489 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
490 i = (mb->un.varRdConfig.max_xri >> 3);
491 if (phba->pport->cfg_lun_queue_depth > i) {
492 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
493 "3360 LUN queue depth changed from %d to %d\n",
494 phba->pport->cfg_lun_queue_depth, i);
495 phba->pport->cfg_lun_queue_depth = i;
496 }
dea31012005-04-17 16:05:31 -0500497
498 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500499
500 /* Get the default values for Model Name and Description */
501 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
502
James Smart2e0fef82007-06-17 19:56:36 -0500503 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500504
James Smart0b727fe2007-10-27 13:37:25 -0400505 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800506 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
507 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
508 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
509 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500510
511 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500512 if (phba->sli_rev != 3)
513 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500514
James Smart93996272008-08-24 21:50:30 -0400515 /*
516 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
517 */
518 if (phba->intr_type == MSIX) {
519 rc = lpfc_config_msi(phba, pmb);
520 if (rc) {
521 mempool_free(pmb, phba->mbox_mem_pool);
522 return -EIO;
523 }
524 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
525 if (rc != MBX_SUCCESS) {
526 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
527 "0352 Config MSI mailbox command "
528 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400529 pmb->u.mb.mbxCommand,
530 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400531 mempool_free(pmb, phba->mbox_mem_pool);
532 return -EIO;
533 }
534 }
535
James Smart04c68492009-05-22 14:52:52 -0400536 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400537 /* Initialize ERATT handling flag */
538 phba->hba_flag &= ~HBA_ERATT_HANDLED;
539
dea31012005-04-17 16:05:31 -0500540 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500541 if (lpfc_readl(phba->HCregaddr, &status)) {
542 spin_unlock_irq(&phba->hbalock);
543 return -EIO;
544 }
dea31012005-04-17 16:05:31 -0500545 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
546 if (psli->num_rings > 0)
547 status |= HC_R0INT_ENA;
548 if (psli->num_rings > 1)
549 status |= HC_R1INT_ENA;
550 if (psli->num_rings > 2)
551 status |= HC_R2INT_ENA;
552 if (psli->num_rings > 3)
553 status |= HC_R3INT_ENA;
554
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500555 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
556 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400557 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500558
dea31012005-04-17 16:05:31 -0500559 writel(status, phba->HCregaddr);
560 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500561 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500562
James Smart93996272008-08-24 21:50:30 -0400563 /* Set up ring-0 (ELS) timer */
564 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400565 mod_timer(&vport->els_tmofunc,
566 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400567 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400568 mod_timer(&phba->hb_tmofunc,
569 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500570 phba->hb_outstanding = 0;
571 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400572 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400573 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700574 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500575
James Smarta0c87cb2009-07-19 10:01:10 -0400576 if (phba->hba_flag & LINK_DISABLED) {
577 lpfc_printf_log(phba,
578 KERN_ERR, LOG_INIT,
579 "2598 Adapter Link is disabled.\n");
580 lpfc_down_link(phba, pmb);
581 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
582 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
583 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
584 lpfc_printf_log(phba,
585 KERN_ERR, LOG_INIT,
586 "2599 Adapter failed to issue DOWN_LINK"
587 " mbox command rc 0x%x\n", rc);
588
589 mempool_free(pmb, phba->mbox_mem_pool);
590 return -EIO;
591 }
James Smarte40a02c2010-02-26 14:13:54 -0500592 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500593 mempool_free(pmb, phba->mbox_mem_pool);
594 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
595 if (rc)
596 return rc;
dea31012005-04-17 16:05:31 -0500597 }
598 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400599 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500600 if (!pmb) {
601 phba->link_state = LPFC_HBA_ERROR;
602 return -ENOMEM;
603 }
604
James Smart57127f12007-10-27 13:37:05 -0400605 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
606 pmb->mbox_cmpl = lpfc_config_async_cmpl;
607 pmb->vport = phba->pport;
608 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500609
James Smart57127f12007-10-27 13:37:05 -0400610 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
611 lpfc_printf_log(phba,
612 KERN_ERR,
613 LOG_INIT,
614 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400615 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400616 rc);
617 mempool_free(pmb, phba->mbox_mem_pool);
618 }
James Smart97207482008-12-04 22:39:19 -0500619
620 /* Get Option rom version */
621 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500622 if (!pmb) {
623 phba->link_state = LPFC_HBA_ERROR;
624 return -ENOMEM;
625 }
626
James Smart97207482008-12-04 22:39:19 -0500627 lpfc_dump_wakeup_param(phba, pmb);
628 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
629 pmb->vport = phba->pport;
630 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
631
632 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400634 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500635 mempool_free(pmb, phba->mbox_mem_pool);
636 }
637
James Smartd7c255b2008-08-24 21:50:00 -0400638 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400639}
640
James Smarte59058c2008-08-24 21:49:00 -0400641/**
James Smart84d1b002010-02-12 14:42:33 -0500642 * lpfc_hba_init_link - Initialize the FC link
643 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400644 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500645 *
646 * This routine will issue the INIT_LINK mailbox command call.
647 * It is available to other drivers through the lpfc_hba data
648 * structure for use as a delayed link up mechanism with the
649 * module parameter lpfc_suppress_link_up.
650 *
651 * Return code
652 * 0 - success
653 * Any other value - error
654 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400655static int
James Smart6e7288d2010-06-07 15:23:35 -0400656lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500657{
James Smart1b511972011-12-13 13:23:09 -0500658 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
659}
660
661/**
662 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
663 * @phba: pointer to lpfc hba data structure.
664 * @fc_topology: desired fc topology.
665 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
666 *
667 * This routine will issue the INIT_LINK mailbox command call.
668 * It is available to other drivers through the lpfc_hba data
669 * structure for use as a delayed link up mechanism with the
670 * module parameter lpfc_suppress_link_up.
671 *
672 * Return code
673 * 0 - success
674 * Any other value - error
675 **/
676int
677lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
678 uint32_t flag)
679{
James Smart84d1b002010-02-12 14:42:33 -0500680 struct lpfc_vport *vport = phba->pport;
681 LPFC_MBOXQ_t *pmb;
682 MAILBOX_t *mb;
683 int rc;
684
685 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
686 if (!pmb) {
687 phba->link_state = LPFC_HBA_ERROR;
688 return -ENOMEM;
689 }
690 mb = &pmb->u.mb;
691 pmb->vport = vport;
692
James Smart026abb82011-12-13 13:20:45 -0500693 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
694 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
695 !(phba->lmt & LMT_1Gb)) ||
696 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
697 !(phba->lmt & LMT_2Gb)) ||
698 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
699 !(phba->lmt & LMT_4Gb)) ||
700 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
701 !(phba->lmt & LMT_8Gb)) ||
702 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
703 !(phba->lmt & LMT_10Gb)) ||
704 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400705 !(phba->lmt & LMT_16Gb)) ||
706 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
707 !(phba->lmt & LMT_32Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500708 /* Reset link speed to auto */
709 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
710 "1302 Invalid speed for this board:%d "
711 "Reset link speed to auto.\n",
712 phba->cfg_link_speed);
713 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
714 }
James Smart1b511972011-12-13 13:23:09 -0500715 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500716 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500717 if (phba->sli_rev < LPFC_SLI_REV4)
718 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400719 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500720 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
722 "0498 Adapter failed to init, mbxCmd x%x "
723 "INIT_LINK, mbxStatus x%x\n",
724 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500725 if (phba->sli_rev <= LPFC_SLI_REV3) {
726 /* Clear all interrupt enable conditions */
727 writel(0, phba->HCregaddr);
728 readl(phba->HCregaddr); /* flush */
729 /* Clear all pending interrupts */
730 writel(0xffffffff, phba->HAregaddr);
731 readl(phba->HAregaddr); /* flush */
732 }
James Smart84d1b002010-02-12 14:42:33 -0500733 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400734 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500735 mempool_free(pmb, phba->mbox_mem_pool);
736 return -EIO;
737 }
James Smarte40a02c2010-02-26 14:13:54 -0500738 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400739 if (flag == MBX_POLL)
740 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500741
742 return 0;
743}
744
745/**
746 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400747 * @phba: pointer to lpfc hba data structure.
748 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500749 *
750 * This routine will issue the DOWN_LINK mailbox command call.
751 * It is available to other drivers through the lpfc_hba data
752 * structure for use to stop the link.
753 *
754 * Return code
755 * 0 - success
756 * Any other value - error
757 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400758static int
James Smart6e7288d2010-06-07 15:23:35 -0400759lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500760{
761 LPFC_MBOXQ_t *pmb;
762 int rc;
763
764 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
765 if (!pmb) {
766 phba->link_state = LPFC_HBA_ERROR;
767 return -ENOMEM;
768 }
769
770 lpfc_printf_log(phba,
771 KERN_ERR, LOG_INIT,
772 "0491 Adapter Link is disabled.\n");
773 lpfc_down_link(phba, pmb);
774 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400775 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500776 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
777 lpfc_printf_log(phba,
778 KERN_ERR, LOG_INIT,
779 "2522 Adapter failed to issue DOWN_LINK"
780 " mbox command rc 0x%x\n", rc);
781
782 mempool_free(pmb, phba->mbox_mem_pool);
783 return -EIO;
784 }
James Smart6e7288d2010-06-07 15:23:35 -0400785 if (flag == MBX_POLL)
786 mempool_free(pmb, phba->mbox_mem_pool);
787
James Smart84d1b002010-02-12 14:42:33 -0500788 return 0;
789}
790
791/**
James Smart3621a712009-04-06 18:47:14 -0400792 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400793 * @phba: pointer to lpfc HBA data structure.
794 *
795 * This routine will do LPFC uninitialization before the HBA is reset when
796 * bringing down the SLI Layer.
797 *
798 * Return codes
799 * 0 - success.
800 * Any other value - error.
801 **/
dea31012005-04-17 16:05:31 -0500802int
James Smart2e0fef82007-06-17 19:56:36 -0500803lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500804{
James Smart1b32f6a2008-02-08 18:49:39 -0500805 struct lpfc_vport **vports;
806 int i;
James Smart3772a992009-05-22 14:50:54 -0400807
808 if (phba->sli_rev <= LPFC_SLI_REV3) {
809 /* Disable interrupts */
810 writel(0, phba->HCregaddr);
811 readl(phba->HCregaddr); /* flush */
812 }
dea31012005-04-17 16:05:31 -0500813
James Smart1b32f6a2008-02-08 18:49:39 -0500814 if (phba->pport->load_flag & FC_UNLOADING)
815 lpfc_cleanup_discovery_resources(phba->pport);
816 else {
817 vports = lpfc_create_vport_work_array(phba);
818 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400819 for (i = 0; i <= phba->max_vports &&
820 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500821 lpfc_cleanup_discovery_resources(vports[i]);
822 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500823 }
824 return 0;
dea31012005-04-17 16:05:31 -0500825}
826
James Smarte59058c2008-08-24 21:49:00 -0400827/**
James Smart68e814f2014-05-21 08:04:59 -0400828 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
829 * rspiocb which got deferred
830 *
831 * @phba: pointer to lpfc HBA data structure.
832 *
833 * This routine will cleanup completed slow path events after HBA is reset
834 * when bringing down the SLI Layer.
835 *
836 *
837 * Return codes
838 * void.
839 **/
840static void
841lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
842{
843 struct lpfc_iocbq *rspiocbq;
844 struct hbq_dmabuf *dmabuf;
845 struct lpfc_cq_event *cq_event;
846
847 spin_lock_irq(&phba->hbalock);
848 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
849 spin_unlock_irq(&phba->hbalock);
850
851 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
852 /* Get the response iocb from the head of work queue */
853 spin_lock_irq(&phba->hbalock);
854 list_remove_head(&phba->sli4_hba.sp_queue_event,
855 cq_event, struct lpfc_cq_event, list);
856 spin_unlock_irq(&phba->hbalock);
857
858 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
859 case CQE_CODE_COMPL_WQE:
860 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
861 cq_event);
862 lpfc_sli_release_iocbq(phba, rspiocbq);
863 break;
864 case CQE_CODE_RECEIVE:
865 case CQE_CODE_RECEIVE_V1:
866 dmabuf = container_of(cq_event, struct hbq_dmabuf,
867 cq_event);
868 lpfc_in_buf_free(phba, &dmabuf->dbuf);
869 }
870 }
871}
872
873/**
James Smartbcece5f2014-04-04 13:51:34 -0400874 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
875 * @phba: pointer to lpfc HBA data structure.
876 *
877 * This routine will cleanup posted ELS buffers after the HBA is reset
878 * when bringing down the SLI Layer.
879 *
880 *
881 * Return codes
882 * void.
883 **/
884static void
885lpfc_hba_free_post_buf(struct lpfc_hba *phba)
886{
887 struct lpfc_sli *psli = &phba->sli;
888 struct lpfc_sli_ring *pring;
889 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400890 LIST_HEAD(buflist);
891 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400892
893 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
894 lpfc_sli_hbqbuf_free_all(phba);
895 else {
896 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800897 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400898 spin_lock_irq(&phba->hbalock);
899 list_splice_init(&pring->postbufq, &buflist);
900 spin_unlock_irq(&phba->hbalock);
901
902 count = 0;
903 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400904 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400905 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400906 lpfc_mbuf_free(phba, mp->virt, mp->phys);
907 kfree(mp);
908 }
James Smart07eab622014-04-04 13:51:44 -0400909
910 spin_lock_irq(&phba->hbalock);
911 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400912 spin_unlock_irq(&phba->hbalock);
913 }
914}
915
916/**
917 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
918 * @phba: pointer to lpfc HBA data structure.
919 *
920 * This routine will cleanup the txcmplq after the HBA is reset when bringing
921 * down the SLI Layer.
922 *
923 * Return codes
924 * void
925 **/
926static void
927lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
928{
929 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800930 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400931 struct lpfc_sli_ring *pring;
932 LIST_HEAD(completions);
933 int i;
934
James Smart895427b2017-02-12 13:52:30 -0800935 if (phba->sli_rev != LPFC_SLI_REV4) {
936 for (i = 0; i < psli->num_rings; i++) {
937 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -0400938 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -0800939 /* At this point in time the HBA is either reset or DOA
940 * Nothing should be on txcmplq as it will
941 * NEVER complete.
942 */
943 list_splice_init(&pring->txcmplq, &completions);
944 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400945 spin_unlock_irq(&phba->hbalock);
946
James Smart895427b2017-02-12 13:52:30 -0800947 lpfc_sli_abort_iocb_ring(phba, pring);
948 }
James Smartbcece5f2014-04-04 13:51:34 -0400949 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -0800950 lpfc_sli_cancel_iocbs(phba, &completions,
951 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
952 return;
953 }
954 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
955 pring = qp->pring;
956 if (!pring)
957 continue;
958 spin_lock_irq(&pring->ring_lock);
959 list_splice_init(&pring->txcmplq, &completions);
960 pring->txcmplq_cnt = 0;
961 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -0400962 lpfc_sli_abort_iocb_ring(phba, pring);
963 }
James Smart895427b2017-02-12 13:52:30 -0800964 /* Cancel all the IOCBs from the completions list */
965 lpfc_sli_cancel_iocbs(phba, &completions,
966 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -0400967}
968
969/**
James Smart3772a992009-05-22 14:50:54 -0400970 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400971 int i;
James Smarte59058c2008-08-24 21:49:00 -0400972 * @phba: pointer to lpfc HBA data structure.
973 *
974 * This routine will do uninitialization after the HBA is reset when bring
975 * down the SLI Layer.
976 *
977 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200978 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400979 * Any other value - error.
980 **/
James Smart3772a992009-05-22 14:50:54 -0400981static int
982lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500983{
James Smartbcece5f2014-04-04 13:51:34 -0400984 lpfc_hba_free_post_buf(phba);
985 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500986 return 0;
987}
James Smart5af5eee2010-10-22 11:06:38 -0400988
James Smartda0436e2009-05-22 14:51:39 -0400989/**
990 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
991 * @phba: pointer to lpfc HBA data structure.
992 *
993 * This routine will do uninitialization after the HBA is reset when bring
994 * down the SLI Layer.
995 *
996 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200997 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400998 * Any other value - error.
999 **/
1000static int
1001lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1002{
1003 struct lpfc_scsi_buf *psb, *psb_next;
1004 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001005 LIST_HEAD(nvme_aborts);
James Smartda0436e2009-05-22 14:51:39 -04001006 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -05001007 struct lpfc_sglq *sglq_entry = NULL;
1008
James Smart895427b2017-02-12 13:52:30 -08001009
1010 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001011 lpfc_hba_clean_txcmplq(phba);
1012
James Smartda0436e2009-05-22 14:51:39 -04001013 /* At this point in time the HBA is either reset or DOA. Either
1014 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001015 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001016 * driver is unloading or reposted if the driver is restarting
1017 * the port.
1018 */
James Smart895427b2017-02-12 13:52:30 -08001019 spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
James Smartda0436e2009-05-22 14:51:39 -04001020 /* scsl_buf_list */
James Smart895427b2017-02-12 13:52:30 -08001021 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001022 * list.
1023 */
James Smart895427b2017-02-12 13:52:30 -08001024 spin_lock(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001025 list_for_each_entry(sglq_entry,
1026 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1027 sglq_entry->state = SGL_FREED;
James Smartf358dd02017-02-12 13:52:34 -08001028 list_for_each_entry(sglq_entry,
1029 &phba->sli4_hba.lpfc_abts_nvmet_sgl_list, list)
1030 sglq_entry->state = SGL_FREED;
James Smart0f65ff62010-02-26 14:14:23 -05001031
James Smartda0436e2009-05-22 14:51:39 -04001032 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001033 &phba->sli4_hba.lpfc_els_sgl_list);
1034
James Smartf358dd02017-02-12 13:52:34 -08001035 if (phba->sli4_hba.nvme_wq)
1036 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_sgl_list,
1037 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1038
James Smart895427b2017-02-12 13:52:30 -08001039 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001040 /* abts_scsi_buf_list_lock required because worker thread uses this
1041 * list.
1042 */
James Smart895427b2017-02-12 13:52:30 -08001043 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
1044 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1045 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1046 &aborts);
1047 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1048 }
1049
1050 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1051 spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1052 list_splice_init(&phba->sli4_hba.lpfc_abts_nvme_buf_list,
1053 &nvme_aborts);
1054 spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1055 }
1056
James Smartda0436e2009-05-22 14:51:39 -04001057 spin_unlock_irq(&phba->hbalock);
1058
1059 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1060 psb->pCmd = NULL;
1061 psb->status = IOSTAT_SUCCESS;
1062 }
James Smarta40fc5f2013-04-17 20:17:40 -04001063 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1064 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1065 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smart68e814f2014-05-21 08:04:59 -04001066
James Smart895427b2017-02-12 13:52:30 -08001067 list_for_each_entry_safe(psb, psb_next, &nvme_aborts, list) {
1068 psb->pCmd = NULL;
1069 psb->status = IOSTAT_SUCCESS;
1070 }
1071 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
1072 list_splice(&nvme_aborts, &phba->lpfc_nvme_buf_list_put);
1073 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
1074
James Smart68e814f2014-05-21 08:04:59 -04001075 lpfc_sli4_free_sp_events(phba);
James Smartda0436e2009-05-22 14:51:39 -04001076 return 0;
1077}
1078
1079/**
1080 * lpfc_hba_down_post - Wrapper func for hba down post routine
1081 * @phba: pointer to lpfc HBA data structure.
1082 *
1083 * This routine wraps the actual SLI3 or SLI4 routine for performing
1084 * uninitialization after the HBA is reset when bring down the SLI Layer.
1085 *
1086 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001087 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001088 * Any other value - error.
1089 **/
1090int
1091lpfc_hba_down_post(struct lpfc_hba *phba)
1092{
1093 return (*phba->lpfc_hba_down_post)(phba);
1094}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001095
James Smarte59058c2008-08-24 21:49:00 -04001096/**
James Smart3621a712009-04-06 18:47:14 -04001097 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001098 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1099 *
1100 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1101 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1102 * work-port-events bitmap and the worker thread is notified. This timeout
1103 * event will be used by the worker thread to invoke the actual timeout
1104 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1105 * be performed in the timeout handler and the HBA timeout event bit shall
1106 * be cleared by the worker thread after it has taken the event bitmap out.
1107 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001108static void
James Smart858c9f62007-06-17 19:56:39 -05001109lpfc_hb_timeout(unsigned long ptr)
1110{
1111 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001112 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001113 unsigned long iflag;
1114
1115 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001116
1117 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001118 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001119 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1120 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001121 phba->pport->work_port_events |= WORKER_HB_TMO;
1122 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1123
James Smart93996272008-08-24 21:50:30 -04001124 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001125 if (!tmo_posted)
1126 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001127 return;
1128}
1129
James Smarte59058c2008-08-24 21:49:00 -04001130/**
James Smart19ca7602010-11-20 23:11:55 -05001131 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1132 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1133 *
1134 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1135 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1136 * work-port-events bitmap and the worker thread is notified. This timeout
1137 * event will be used by the worker thread to invoke the actual timeout
1138 * handler routine, lpfc_rrq_handler. Any periodical operations will
1139 * be performed in the timeout handler and the RRQ timeout event bit shall
1140 * be cleared by the worker thread after it has taken the event bitmap out.
1141 **/
1142static void
1143lpfc_rrq_timeout(unsigned long ptr)
1144{
1145 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001146 unsigned long iflag;
1147
1148 phba = (struct lpfc_hba *)ptr;
1149 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001150 if (!(phba->pport->load_flag & FC_UNLOADING))
1151 phba->hba_flag |= HBA_RRQ_ACTIVE;
1152 else
1153 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001154 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001155
1156 if (!(phba->pport->load_flag & FC_UNLOADING))
1157 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001158}
1159
1160/**
James Smart3621a712009-04-06 18:47:14 -04001161 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001162 * @phba: pointer to lpfc hba data structure.
1163 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1164 *
1165 * This is the callback function to the lpfc heart-beat mailbox command.
1166 * If configured, the lpfc driver issues the heart-beat mailbox command to
1167 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1168 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1169 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1170 * heart-beat outstanding state. Once the mailbox command comes back and
1171 * no error conditions detected, the heart-beat mailbox command timer is
1172 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1173 * state is cleared for the next heart-beat. If the timer expired with the
1174 * heart-beat outstanding state set, the driver will put the HBA offline.
1175 **/
James Smart858c9f62007-06-17 19:56:39 -05001176static void
1177lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1178{
1179 unsigned long drvr_flag;
1180
1181 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1182 phba->hb_outstanding = 0;
1183 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1184
James Smart93996272008-08-24 21:50:30 -04001185 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001186 mempool_free(pmboxq, phba->mbox_mem_pool);
1187 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1188 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001189 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001190 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001191 jiffies +
1192 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001193 return;
1194}
1195
James Smarte59058c2008-08-24 21:49:00 -04001196/**
James Smart3621a712009-04-06 18:47:14 -04001197 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001198 * @phba: pointer to lpfc hba data structure.
1199 *
1200 * This is the actual HBA-timer timeout handler to be invoked by the worker
1201 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1202 * handler performs any periodic operations needed for the device. If such
1203 * periodic event has already been attended to either in the interrupt handler
1204 * or by processing slow-ring or fast-ring events within the HBA-timer
1205 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1206 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1207 * is configured and there is no heart-beat mailbox command outstanding, a
1208 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1209 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1210 * to offline.
1211 **/
James Smart858c9f62007-06-17 19:56:39 -05001212void
1213lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1214{
James Smart45ed1192009-10-02 15:17:02 -04001215 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001216 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001217 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001218 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001219 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001220 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001221
James Smart45ed1192009-10-02 15:17:02 -04001222 vports = lpfc_create_vport_work_array(phba);
1223 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001224 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001225 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001226 lpfc_fdmi_num_disc_check(vports[i]);
1227 }
James Smart45ed1192009-10-02 15:17:02 -04001228 lpfc_destroy_vport_work_array(phba, vports);
1229
James Smart858c9f62007-06-17 19:56:39 -05001230 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001231 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001232 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1233 return;
1234
1235 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001236
James Smart256ec0d2013-04-17 20:14:58 -04001237 if (time_after(phba->last_completion_time +
1238 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1239 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001240 spin_unlock_irq(&phba->pport->work_port_lock);
1241 if (!phba->hb_outstanding)
1242 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001243 jiffies +
1244 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001245 else
1246 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001247 jiffies +
1248 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001249 return;
1250 }
1251 spin_unlock_irq(&phba->pport->work_port_lock);
1252
James Smart0ff10d42008-01-11 01:52:36 -05001253 if (phba->elsbuf_cnt &&
1254 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1255 spin_lock_irq(&phba->hbalock);
1256 list_splice_init(&phba->elsbuf, &completions);
1257 phba->elsbuf_cnt = 0;
1258 phba->elsbuf_prev_cnt = 0;
1259 spin_unlock_irq(&phba->hbalock);
1260
1261 while (!list_empty(&completions)) {
1262 list_remove_head(&completions, buf_ptr,
1263 struct lpfc_dmabuf, list);
1264 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1265 kfree(buf_ptr);
1266 }
1267 }
1268 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1269
James Smart858c9f62007-06-17 19:56:39 -05001270 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001271 if (phba->cfg_enable_hba_heartbeat) {
1272 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001273 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1274 (list_empty(&psli->mboxq))) {
1275 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1276 GFP_KERNEL);
1277 if (!pmboxq) {
1278 mod_timer(&phba->hb_tmofunc,
1279 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001280 msecs_to_jiffies(1000 *
1281 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001282 return;
1283 }
James Smart13815c82008-01-11 01:52:48 -05001284
James Smartbc739052010-08-04 16:11:18 -04001285 lpfc_heart_beat(phba, pmboxq);
1286 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1287 pmboxq->vport = phba->pport;
1288 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1289 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001290
James Smartbc739052010-08-04 16:11:18 -04001291 if (retval != MBX_BUSY &&
1292 retval != MBX_SUCCESS) {
1293 mempool_free(pmboxq,
1294 phba->mbox_mem_pool);
1295 mod_timer(&phba->hb_tmofunc,
1296 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001297 msecs_to_jiffies(1000 *
1298 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001299 return;
1300 }
1301 phba->skipped_hb = 0;
1302 phba->hb_outstanding = 1;
1303 } else if (time_before_eq(phba->last_completion_time,
1304 phba->skipped_hb)) {
1305 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1306 "2857 Last completion time not "
1307 " updated in %d ms\n",
1308 jiffies_to_msecs(jiffies
1309 - phba->last_completion_time));
1310 } else
1311 phba->skipped_hb = jiffies;
1312
James Smart858c9f62007-06-17 19:56:39 -05001313 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001314 jiffies +
1315 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001316 return;
James Smart13815c82008-01-11 01:52:48 -05001317 } else {
1318 /*
1319 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001320 * we need to give the hb mailbox cmd a chance to
1321 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001322 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001323 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1324 "0459 Adapter heartbeat still out"
1325 "standing:last compl time was %d ms.\n",
1326 jiffies_to_msecs(jiffies
1327 - phba->last_completion_time));
1328 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001329 jiffies +
1330 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001331 }
James Smart4258e982015-12-16 18:11:58 -05001332 } else {
1333 mod_timer(&phba->hb_tmofunc,
1334 jiffies +
1335 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001336 }
1337}
1338
James Smarte59058c2008-08-24 21:49:00 -04001339/**
James Smart3621a712009-04-06 18:47:14 -04001340 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001341 * @phba: pointer to lpfc hba data structure.
1342 *
1343 * This routine is called to bring the HBA offline when HBA hardware error
1344 * other than Port Error 6 has been detected.
1345 **/
James Smart09372822008-01-11 01:52:54 -05001346static void
1347lpfc_offline_eratt(struct lpfc_hba *phba)
1348{
1349 struct lpfc_sli *psli = &phba->sli;
1350
1351 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001352 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001353 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001354 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001355
1356 lpfc_offline(phba);
1357 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001358 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001359 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001360 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001361 lpfc_hba_down_post(phba);
1362 lpfc_sli_brdready(phba, HS_MBRDY);
1363 lpfc_unblock_mgmt_io(phba);
1364 phba->link_state = LPFC_HBA_ERROR;
1365 return;
1366}
1367
James Smarte59058c2008-08-24 21:49:00 -04001368/**
James Smartda0436e2009-05-22 14:51:39 -04001369 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1370 * @phba: pointer to lpfc hba data structure.
1371 *
1372 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1373 * other than Port Error 6 has been detected.
1374 **/
James Smarta88dbb62013-04-17 20:18:39 -04001375void
James Smartda0436e2009-05-22 14:51:39 -04001376lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1377{
James Smart946727d2015-04-07 15:07:09 -04001378 spin_lock_irq(&phba->hbalock);
1379 phba->link_state = LPFC_HBA_ERROR;
1380 spin_unlock_irq(&phba->hbalock);
1381
James Smart618a5232012-06-12 13:54:36 -04001382 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001383 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001384 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001385 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001386}
1387
1388/**
James Smarta257bf92009-04-06 18:48:10 -04001389 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1390 * @phba: pointer to lpfc hba data structure.
1391 *
1392 * This routine is invoked to handle the deferred HBA hardware error
1393 * conditions. This type of error is indicated by HBA by setting ER1
1394 * and another ER bit in the host status register. The driver will
1395 * wait until the ER1 bit clears before handling the error condition.
1396 **/
1397static void
1398lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1399{
1400 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001401 struct lpfc_sli *psli = &phba->sli;
1402
James Smartf4b4c682009-05-22 14:53:12 -04001403 /* If the pci channel is offline, ignore possible errors,
1404 * since we cannot communicate with the pci card anyway.
1405 */
1406 if (pci_channel_offline(phba->pcidev)) {
1407 spin_lock_irq(&phba->hbalock);
1408 phba->hba_flag &= ~DEFER_ERATT;
1409 spin_unlock_irq(&phba->hbalock);
1410 return;
1411 }
1412
James Smarta257bf92009-04-06 18:48:10 -04001413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1414 "0479 Deferred Adapter Hardware Error "
1415 "Data: x%x x%x x%x\n",
1416 phba->work_hs,
1417 phba->work_status[0], phba->work_status[1]);
1418
1419 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001420 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001421 spin_unlock_irq(&phba->hbalock);
1422
1423
1424 /*
1425 * Firmware stops when it triggred erratt. That could cause the I/Os
1426 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1427 * SCSI layer retry it after re-establishing link.
1428 */
James Smartdb55fba2014-04-04 13:52:02 -04001429 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001430
1431 /*
1432 * There was a firmware error. Take the hba offline and then
1433 * attempt to restart it.
1434 */
James Smart618a5232012-06-12 13:54:36 -04001435 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001436 lpfc_offline(phba);
1437
1438 /* Wait for the ER1 bit to clear.*/
1439 while (phba->work_hs & HS_FFER1) {
1440 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001441 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1442 phba->work_hs = UNPLUG_ERR ;
1443 break;
1444 }
James Smarta257bf92009-04-06 18:48:10 -04001445 /* If driver is unloading let the worker thread continue */
1446 if (phba->pport->load_flag & FC_UNLOADING) {
1447 phba->work_hs = 0;
1448 break;
1449 }
1450 }
1451
1452 /*
1453 * This is to ptrotect against a race condition in which
1454 * first write to the host attention register clear the
1455 * host status register.
1456 */
1457 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1458 phba->work_hs = old_host_status & ~HS_FFER1;
1459
James Smart3772a992009-05-22 14:50:54 -04001460 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001461 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001462 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001463 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1464 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1465}
1466
James Smart3772a992009-05-22 14:50:54 -04001467static void
1468lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1469{
1470 struct lpfc_board_event_header board_event;
1471 struct Scsi_Host *shost;
1472
1473 board_event.event_type = FC_REG_BOARD_EVENT;
1474 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1475 shost = lpfc_shost_from_vport(phba->pport);
1476 fc_host_post_vendor_event(shost, fc_get_event_number(),
1477 sizeof(board_event),
1478 (char *) &board_event,
1479 LPFC_NL_VENDOR_ID);
1480}
1481
James Smarta257bf92009-04-06 18:48:10 -04001482/**
James Smart3772a992009-05-22 14:50:54 -04001483 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001484 * @phba: pointer to lpfc hba data structure.
1485 *
1486 * This routine is invoked to handle the following HBA hardware error
1487 * conditions:
1488 * 1 - HBA error attention interrupt
1489 * 2 - DMA ring index out of range
1490 * 3 - Mailbox command came back as unknown
1491 **/
James Smart3772a992009-05-22 14:50:54 -04001492static void
1493lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001494{
James Smart2e0fef82007-06-17 19:56:36 -05001495 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001496 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001497 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001498 unsigned long temperature;
1499 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001500 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001501
Linas Vepstas8d63f372007-02-14 14:28:36 -06001502 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001503 * since we cannot communicate with the pci card anyway.
1504 */
1505 if (pci_channel_offline(phba->pcidev)) {
1506 spin_lock_irq(&phba->hbalock);
1507 phba->hba_flag &= ~DEFER_ERATT;
1508 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001509 return;
James Smart3772a992009-05-22 14:50:54 -04001510 }
1511
James Smart13815c82008-01-11 01:52:48 -05001512 /* If resets are disabled then leave the HBA alone and return */
1513 if (!phba->cfg_enable_hba_reset)
1514 return;
dea31012005-04-17 16:05:31 -05001515
James Smartea2151b2008-09-07 11:52:10 -04001516 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001517 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001518
James Smarta257bf92009-04-06 18:48:10 -04001519 if (phba->hba_flag & DEFER_ERATT)
1520 lpfc_handle_deferred_eratt(phba);
1521
James Smartdcf2a4e2010-09-29 11:18:53 -04001522 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1523 if (phba->work_hs & HS_FFER6)
1524 /* Re-establishing Link */
1525 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1526 "1301 Re-establishing Link "
1527 "Data: x%x x%x x%x\n",
1528 phba->work_hs, phba->work_status[0],
1529 phba->work_status[1]);
1530 if (phba->work_hs & HS_FFER8)
1531 /* Device Zeroization */
1532 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1533 "2861 Host Authentication device "
1534 "zeroization Data:x%x x%x x%x\n",
1535 phba->work_hs, phba->work_status[0],
1536 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001537
James Smart92d7f7b2007-06-17 19:56:38 -05001538 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001539 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001540 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001541
1542 /*
1543 * Firmware stops when it triggled erratt with HS_FFER6.
1544 * That could cause the I/Os dropped by the firmware.
1545 * Error iocb (I/O) on txcmplq and let the SCSI layer
1546 * retry it after re-establishing link.
1547 */
James Smartdb55fba2014-04-04 13:52:02 -04001548 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001549
dea31012005-04-17 16:05:31 -05001550 /*
1551 * There was a firmware error. Take the hba offline and then
1552 * attempt to restart it.
1553 */
James Smart618a5232012-06-12 13:54:36 -04001554 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001555 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001556 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001557 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001558 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001559 return;
1560 }
James Smart46fa3112007-04-25 09:51:45 -04001561 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001562 } else if (phba->work_hs & HS_CRIT_TEMP) {
1563 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1564 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1565 temp_event_data.event_code = LPFC_CRIT_TEMP;
1566 temp_event_data.data = (uint32_t)temperature;
1567
1568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001569 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001570 "(%ld), taking this port offline "
1571 "Data: x%x x%x x%x\n",
1572 temperature, phba->work_hs,
1573 phba->work_status[0], phba->work_status[1]);
1574
1575 shost = lpfc_shost_from_vport(phba->pport);
1576 fc_host_post_vendor_event(shost, fc_get_event_number(),
1577 sizeof(temp_event_data),
1578 (char *) &temp_event_data,
1579 SCSI_NL_VID_TYPE_PCI
1580 | PCI_VENDOR_ID_EMULEX);
1581
James Smart7af67052007-10-27 13:38:11 -04001582 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001583 phba->over_temp_state = HBA_OVER_TEMP;
1584 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001585 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001586
dea31012005-04-17 16:05:31 -05001587 } else {
1588 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001589 * failure is a value other than FFER6. Do not call the offline
1590 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001591 */
1592 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001593 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001594 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001595 phba->work_hs,
dea31012005-04-17 16:05:31 -05001596 phba->work_status[0], phba->work_status[1]);
1597
James Smartd2873e42006-08-18 17:46:43 -04001598 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001599 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001600 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001601 sizeof(event_data), (char *) &event_data,
1602 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1603
James Smart09372822008-01-11 01:52:54 -05001604 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001605 }
James Smart93996272008-08-24 21:50:30 -04001606 return;
dea31012005-04-17 16:05:31 -05001607}
1608
James Smarte59058c2008-08-24 21:49:00 -04001609/**
James Smart618a5232012-06-12 13:54:36 -04001610 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1611 * @phba: pointer to lpfc hba data structure.
1612 * @mbx_action: flag for mailbox shutdown action.
1613 *
1614 * This routine is invoked to perform an SLI4 port PCI function reset in
1615 * response to port status register polling attention. It waits for port
1616 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1617 * During this process, interrupt vectors are freed and later requested
1618 * for handling possible port resource change.
1619 **/
1620static int
James Smarte10b2022014-02-20 09:57:43 -05001621lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1622 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001623{
1624 int rc;
1625 uint32_t intr_mode;
1626
James Smart65791f12016-07-06 12:35:56 -07001627 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1628 LPFC_SLI_INTF_IF_TYPE_2) {
1629 /*
1630 * On error status condition, driver need to wait for port
1631 * ready before performing reset.
1632 */
1633 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001634 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001635 return rc;
James Smart618a5232012-06-12 13:54:36 -04001636 }
James Smart0e916ee2016-07-06 12:36:06 -07001637
James Smart65791f12016-07-06 12:35:56 -07001638 /* need reset: attempt for port recovery */
1639 if (en_rn_msg)
1640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1641 "2887 Reset Needed: Attempting Port "
1642 "Recovery...\n");
1643 lpfc_offline_prep(phba, mbx_action);
1644 lpfc_offline(phba);
1645 /* release interrupt for possible resource change */
1646 lpfc_sli4_disable_intr(phba);
1647 lpfc_sli_brdrestart(phba);
1648 /* request and enable interrupt */
1649 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1650 if (intr_mode == LPFC_INTR_ERROR) {
1651 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1652 "3175 Failed to enable interrupt\n");
1653 return -EIO;
1654 }
1655 phba->intr_mode = intr_mode;
1656 rc = lpfc_online(phba);
1657 if (rc == 0)
1658 lpfc_unblock_mgmt_io(phba);
1659
James Smart618a5232012-06-12 13:54:36 -04001660 return rc;
1661}
1662
1663/**
James Smartda0436e2009-05-22 14:51:39 -04001664 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1665 * @phba: pointer to lpfc hba data structure.
1666 *
1667 * This routine is invoked to handle the SLI4 HBA hardware error attention
1668 * conditions.
1669 **/
1670static void
1671lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1672{
1673 struct lpfc_vport *vport = phba->pport;
1674 uint32_t event_data;
1675 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001676 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001677 struct lpfc_register portstat_reg = {0};
1678 uint32_t reg_err1, reg_err2;
1679 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001680 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001681 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001682 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001683 struct lpfc_register portsmphr_reg;
1684 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001685
1686 /* If the pci channel is offline, ignore possible errors, since
1687 * we cannot communicate with the pci card anyway.
1688 */
1689 if (pci_channel_offline(phba->pcidev))
1690 return;
James Smartda0436e2009-05-22 14:51:39 -04001691
James Smart65791f12016-07-06 12:35:56 -07001692 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001693 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1694 switch (if_type) {
1695 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001696 pci_rd_rc1 = lpfc_readl(
1697 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1698 &uerrlo_reg);
1699 pci_rd_rc2 = lpfc_readl(
1700 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1701 &uemasklo_reg);
1702 /* consider PCI bus read error as pci_channel_offline */
1703 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1704 return;
James Smart65791f12016-07-06 12:35:56 -07001705 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1706 lpfc_sli4_offline_eratt(phba);
1707 return;
1708 }
1709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1710 "7623 Checking UE recoverable");
1711
1712 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1713 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1714 &portsmphr_reg.word0))
1715 continue;
1716
1717 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1718 &portsmphr_reg);
1719 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1720 LPFC_PORT_SEM_UE_RECOVERABLE)
1721 break;
1722 /*Sleep for 1Sec, before checking SEMAPHORE */
1723 msleep(1000);
1724 }
1725
1726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1727 "4827 smphr_port_status x%x : Waited %dSec",
1728 smphr_port_status, i);
1729
1730 /* Recoverable UE, reset the HBA device */
1731 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1732 LPFC_PORT_SEM_UE_RECOVERABLE) {
1733 for (i = 0; i < 20; i++) {
1734 msleep(1000);
1735 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1736 &portsmphr_reg.word0) &&
1737 (LPFC_POST_STAGE_PORT_READY ==
1738 bf_get(lpfc_port_smphr_port_status,
1739 &portsmphr_reg))) {
1740 rc = lpfc_sli4_port_sta_fn_reset(phba,
1741 LPFC_MBX_NO_WAIT, en_rn_msg);
1742 if (rc == 0)
1743 return;
1744 lpfc_printf_log(phba,
1745 KERN_ERR, LOG_INIT,
1746 "4215 Failed to recover UE");
1747 break;
1748 }
1749 }
1750 }
1751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1752 "7624 Firmware not ready: Failing UE recovery,"
1753 " waited %dSec", i);
James Smart2fcee4b2010-12-15 17:57:46 -05001754 lpfc_sli4_offline_eratt(phba);
1755 break;
James Smart946727d2015-04-07 15:07:09 -04001756
James Smart2fcee4b2010-12-15 17:57:46 -05001757 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001758 pci_rd_rc1 = lpfc_readl(
1759 phba->sli4_hba.u.if_type2.STATUSregaddr,
1760 &portstat_reg.word0);
1761 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001762 if (pci_rd_rc1 == -EIO) {
1763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1764 "3151 PCI bus read access failure: x%x\n",
1765 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001766 return;
James Smart6b5151f2012-01-18 16:24:06 -05001767 }
James Smart2e90f4b2011-12-13 13:22:37 -05001768 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1769 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001770 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1772 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001773 "taking port offline Data: x%x x%x\n",
1774 reg_err1, reg_err2);
1775
James Smart310429e2016-07-06 12:35:54 -07001776 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001777 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1778 temp_event_data.event_code = LPFC_CRIT_TEMP;
1779 temp_event_data.data = 0xFFFFFFFF;
1780
1781 shost = lpfc_shost_from_vport(phba->pport);
1782 fc_host_post_vendor_event(shost, fc_get_event_number(),
1783 sizeof(temp_event_data),
1784 (char *)&temp_event_data,
1785 SCSI_NL_VID_TYPE_PCI
1786 | PCI_VENDOR_ID_EMULEX);
1787
James Smart2fcee4b2010-12-15 17:57:46 -05001788 spin_lock_irq(&phba->hbalock);
1789 phba->over_temp_state = HBA_OVER_TEMP;
1790 spin_unlock_irq(&phba->hbalock);
1791 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001792 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001793 }
James Smart2e90f4b2011-12-13 13:22:37 -05001794 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001795 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001797 "3143 Port Down: Firmware Update "
1798 "Detected\n");
1799 en_rn_msg = false;
1800 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001801 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1803 "3144 Port Down: Debug Dump\n");
1804 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1805 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1807 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001808
James Smart946727d2015-04-07 15:07:09 -04001809 /* If resets are disabled then leave the HBA alone and return */
1810 if (!phba->cfg_enable_hba_reset)
1811 return;
1812
James Smart618a5232012-06-12 13:54:36 -04001813 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001814 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1815 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001816 if (rc == 0) {
1817 /* don't report event on forced debug dump */
1818 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1819 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1820 return;
1821 else
1822 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001823 }
James Smart618a5232012-06-12 13:54:36 -04001824 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001825 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1826 "3152 Unrecoverable error, bring the port "
1827 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001828 lpfc_sli4_offline_eratt(phba);
1829 break;
1830 case LPFC_SLI_INTF_IF_TYPE_1:
1831 default:
1832 break;
1833 }
James Smart2e90f4b2011-12-13 13:22:37 -05001834 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1835 "3123 Report dump event to upper layer\n");
1836 /* Send an internal error event to mgmt application */
1837 lpfc_board_errevt_to_mgmt(phba);
1838
1839 event_data = FC_REG_DUMP_EVENT;
1840 shost = lpfc_shost_from_vport(vport);
1841 fc_host_post_vendor_event(shost, fc_get_event_number(),
1842 sizeof(event_data), (char *) &event_data,
1843 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001844}
1845
1846/**
1847 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1848 * @phba: pointer to lpfc HBA data structure.
1849 *
1850 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1851 * routine from the API jump table function pointer from the lpfc_hba struct.
1852 *
1853 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001854 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001855 * Any other value - error.
1856 **/
1857void
1858lpfc_handle_eratt(struct lpfc_hba *phba)
1859{
1860 (*phba->lpfc_handle_eratt)(phba);
1861}
1862
1863/**
James Smart3621a712009-04-06 18:47:14 -04001864 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001865 * @phba: pointer to lpfc hba data structure.
1866 *
1867 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08001868 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04001869 **/
dea31012005-04-17 16:05:31 -05001870void
James Smart2e0fef82007-06-17 19:56:36 -05001871lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001872{
James Smart2e0fef82007-06-17 19:56:36 -05001873 struct lpfc_vport *vport = phba->pport;
1874 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001875 LPFC_MBOXQ_t *pmb;
1876 volatile uint32_t control;
1877 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001878 int rc = 0;
dea31012005-04-17 16:05:31 -05001879
1880 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001881 if (!pmb) {
1882 rc = 1;
dea31012005-04-17 16:05:31 -05001883 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001884 }
dea31012005-04-17 16:05:31 -05001885
1886 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001887 if (!mp) {
1888 rc = 2;
dea31012005-04-17 16:05:31 -05001889 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001890 }
dea31012005-04-17 16:05:31 -05001891
1892 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001893 if (!mp->virt) {
1894 rc = 3;
dea31012005-04-17 16:05:31 -05001895 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001896 }
dea31012005-04-17 16:05:31 -05001897
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001898 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001899 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001900
1901 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001902 lpfc_read_topology(phba, pmb, mp);
1903 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001904 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001905 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08001906 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001907 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001908 if (rc == MBX_NOT_FINISHED) {
1909 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001910 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001911 }
dea31012005-04-17 16:05:31 -05001912
1913 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001914 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001915 writel(HA_LATT, phba->HAregaddr);
1916 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001917 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001918
1919 return;
1920
James Smart14691152006-12-02 13:34:28 -05001921lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08001922 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001923 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001924lpfc_handle_latt_free_mp:
1925 kfree(mp);
1926lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001927 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001928lpfc_handle_latt_err_exit:
1929 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001930 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001931 psli->sli_flag |= LPFC_PROCESS_LA;
1932 control = readl(phba->HCregaddr);
1933 control |= HC_LAINT_ENA;
1934 writel(control, phba->HCregaddr);
1935 readl(phba->HCregaddr); /* flush */
1936
1937 /* Clear Link Attention in HA REG */
1938 writel(HA_LATT, phba->HAregaddr);
1939 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001940 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001941 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001942 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001943
James Smart09372822008-01-11 01:52:54 -05001944 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1945 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001946
1947 return;
1948}
1949
James Smarte59058c2008-08-24 21:49:00 -04001950/**
James Smart3621a712009-04-06 18:47:14 -04001951 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001952 * @phba: pointer to lpfc hba data structure.
1953 * @vpd: pointer to the vital product data.
1954 * @len: length of the vital product data in bytes.
1955 *
1956 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1957 * an array of characters. In this routine, the ModelName, ProgramType, and
1958 * ModelDesc, etc. fields of the phba data structure will be populated.
1959 *
1960 * Return codes
1961 * 0 - pointer to the VPD passed in is NULL
1962 * 1 - success
1963 **/
James Smart3772a992009-05-22 14:50:54 -04001964int
James Smart2e0fef82007-06-17 19:56:36 -05001965lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001966{
1967 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001968 int Length;
dea31012005-04-17 16:05:31 -05001969 int i, j;
1970 int finished = 0;
1971 int index = 0;
1972
1973 if (!vpd)
1974 return 0;
1975
1976 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001977 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001978 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001979 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1980 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001981 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001982 switch (vpd[index]) {
1983 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001984 case 0x91:
dea31012005-04-17 16:05:31 -05001985 index += 1;
1986 lenlo = vpd[index];
1987 index += 1;
1988 lenhi = vpd[index];
1989 index += 1;
1990 i = ((((unsigned short)lenhi) << 8) + lenlo);
1991 index += i;
1992 break;
1993 case 0x90:
1994 index += 1;
1995 lenlo = vpd[index];
1996 index += 1;
1997 lenhi = vpd[index];
1998 index += 1;
1999 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002000 if (Length > len - index)
2001 Length = len - index;
dea31012005-04-17 16:05:31 -05002002 while (Length > 0) {
2003 /* Look for Serial Number */
2004 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2005 index += 2;
2006 i = vpd[index];
2007 index += 1;
2008 j = 0;
2009 Length -= (3+i);
2010 while(i--) {
2011 phba->SerialNumber[j++] = vpd[index++];
2012 if (j == 31)
2013 break;
2014 }
2015 phba->SerialNumber[j] = 0;
2016 continue;
2017 }
2018 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2019 phba->vpd_flag |= VPD_MODEL_DESC;
2020 index += 2;
2021 i = vpd[index];
2022 index += 1;
2023 j = 0;
2024 Length -= (3+i);
2025 while(i--) {
2026 phba->ModelDesc[j++] = vpd[index++];
2027 if (j == 255)
2028 break;
2029 }
2030 phba->ModelDesc[j] = 0;
2031 continue;
2032 }
2033 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2034 phba->vpd_flag |= VPD_MODEL_NAME;
2035 index += 2;
2036 i = vpd[index];
2037 index += 1;
2038 j = 0;
2039 Length -= (3+i);
2040 while(i--) {
2041 phba->ModelName[j++] = vpd[index++];
2042 if (j == 79)
2043 break;
2044 }
2045 phba->ModelName[j] = 0;
2046 continue;
2047 }
2048 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2049 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2050 index += 2;
2051 i = vpd[index];
2052 index += 1;
2053 j = 0;
2054 Length -= (3+i);
2055 while(i--) {
2056 phba->ProgramType[j++] = vpd[index++];
2057 if (j == 255)
2058 break;
2059 }
2060 phba->ProgramType[j] = 0;
2061 continue;
2062 }
2063 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2064 phba->vpd_flag |= VPD_PORT;
2065 index += 2;
2066 i = vpd[index];
2067 index += 1;
2068 j = 0;
2069 Length -= (3+i);
2070 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002071 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2072 (phba->sli4_hba.pport_name_sta ==
2073 LPFC_SLI4_PPNAME_GET)) {
2074 j++;
2075 index++;
2076 } else
2077 phba->Port[j++] = vpd[index++];
2078 if (j == 19)
2079 break;
dea31012005-04-17 16:05:31 -05002080 }
James Smartcd1c8302011-10-10 21:33:25 -04002081 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2082 (phba->sli4_hba.pport_name_sta ==
2083 LPFC_SLI4_PPNAME_NON))
2084 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002085 continue;
2086 }
2087 else {
2088 index += 2;
2089 i = vpd[index];
2090 index += 1;
2091 index += i;
2092 Length -= (3 + i);
2093 }
2094 }
2095 finished = 0;
2096 break;
2097 case 0x78:
2098 finished = 1;
2099 break;
2100 default:
2101 index ++;
2102 break;
2103 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002104 }
dea31012005-04-17 16:05:31 -05002105
2106 return(1);
2107}
2108
James Smarte59058c2008-08-24 21:49:00 -04002109/**
James Smart3621a712009-04-06 18:47:14 -04002110 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002111 * @phba: pointer to lpfc hba data structure.
2112 * @mdp: pointer to the data structure to hold the derived model name.
2113 * @descp: pointer to the data structure to hold the derived description.
2114 *
2115 * This routine retrieves HBA's description based on its registered PCI device
2116 * ID. The @descp passed into this function points to an array of 256 chars. It
2117 * shall be returned with the model name, maximum speed, and the host bus type.
2118 * The @mdp passed into this function points to an array of 80 chars. When the
2119 * function returns, the @mdp will be filled with the model name.
2120 **/
dea31012005-04-17 16:05:31 -05002121static void
James Smart2e0fef82007-06-17 19:56:36 -05002122lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002123{
2124 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002125 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002126 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002127 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002128 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002129 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002130 char *name;
2131 char *bus;
2132 char *function;
2133 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002134
2135 if (mdp && mdp[0] != '\0'
2136 && descp && descp[0] != '\0')
2137 return;
2138
James Smartd38dd522015-08-31 16:48:17 -04002139 if (phba->lmt & LMT_32Gb)
2140 max_speed = 32;
2141 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002142 max_speed = 16;
2143 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002144 max_speed = 10;
2145 else if (phba->lmt & LMT_8Gb)
2146 max_speed = 8;
2147 else if (phba->lmt & LMT_4Gb)
2148 max_speed = 4;
2149 else if (phba->lmt & LMT_2Gb)
2150 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002151 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002152 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002153 else
2154 max_speed = 0;
dea31012005-04-17 16:05:31 -05002155
2156 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002157
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002158 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002159 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002160 m = (typeof(m)){"LP6000", "PCI",
2161 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002162 break;
dea31012005-04-17 16:05:31 -05002163 case PCI_DEVICE_ID_SUPERFLY:
2164 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002165 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002166 else
James Smart12222f42014-05-21 08:05:19 -04002167 m = (typeof(m)){"LP7000E", "PCI", ""};
2168 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002169 break;
2170 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002171 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002172 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002173 break;
2174 case PCI_DEVICE_ID_CENTAUR:
2175 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002176 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002177 else
James Smart12222f42014-05-21 08:05:19 -04002178 m = (typeof(m)){"LP9000", "PCI", ""};
2179 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002180 break;
2181 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002182 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002183 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002184 break;
2185 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002186 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002187 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002188 break;
2189 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002190 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002191 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002192 break;
2193 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002194 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002195 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002196 break;
2197 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002198 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002199 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002200 break;
2201 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002202 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002203 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002204 break;
2205 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002206 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002207 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002208 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002209 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002210 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002211 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002212 break;
2213 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002214 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002215 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002216 break;
2217 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002218 m = (typeof(m)){"LPe1000", "PCIe",
2219 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002220 break;
2221 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002222 m = (typeof(m)){"LPe1000-SP", "PCIe",
2223 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002224 break;
2225 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002226 m = (typeof(m)){"LPe1002-SP", "PCIe",
2227 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002228 break;
dea31012005-04-17 16:05:31 -05002229 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002230 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002231 break;
2232 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002233 m = (typeof(m)){"LP111", "PCI-X2",
2234 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002235 break;
2236 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002237 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002238 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002239 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002240 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002241 break;
2242 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002243 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002244 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002245 break;
dea31012005-04-17 16:05:31 -05002246 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002247 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002248 break;
2249 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002250 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002251 break;
2252 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002253 m = (typeof(m)){"LP101", "PCI-X",
2254 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002255 break;
2256 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002257 m = (typeof(m)){"LP10000-S", "PCI",
2258 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002259 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002260 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002261 m = (typeof(m)){"LP11000-S", "PCI-X2",
2262 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002263 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002264 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002265 m = (typeof(m)){"LPe11000-S", "PCIe",
2266 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002267 break;
James Smartb87eab32007-04-25 09:53:28 -04002268 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002269 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002270 break;
2271 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002272 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002273 break;
2274 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002275 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002276 break;
2277 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002278 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002279 break;
2280 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002281 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002282 break;
2283 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002284 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002285 break;
James Smart84774a42008-08-24 21:50:06 -04002286 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002287 m = (typeof(m)){"LP21000", "PCIe",
2288 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002289 GE = 1;
2290 break;
2291 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002292 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002293 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002294 break;
2295 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002296 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002297 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002298 break;
2299 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002300 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002301 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002302 break;
James Smartda0436e2009-05-22 14:51:39 -04002303 case PCI_DEVICE_ID_TIGERSHARK:
2304 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002305 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002306 break;
James Smarta747c9c2009-11-18 15:41:10 -05002307 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002308 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002309 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2310 break;
2311 case PCI_DEVICE_ID_FALCON:
2312 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2313 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002314 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002315 case PCI_DEVICE_ID_BALIUS:
2316 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002317 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002318 break;
James Smart085c6472010-11-20 23:11:37 -05002319 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002320 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002321 break;
James Smart12222f42014-05-21 08:05:19 -04002322 case PCI_DEVICE_ID_LANCER_FC_VF:
2323 m = (typeof(m)){"LPe16000", "PCIe",
2324 "Obsolete, Unsupported Fibre Channel Adapter"};
2325 break;
James Smart085c6472010-11-20 23:11:37 -05002326 case PCI_DEVICE_ID_LANCER_FCOE:
2327 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002328 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002329 break;
James Smart12222f42014-05-21 08:05:19 -04002330 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2331 oneConnect = 1;
2332 m = (typeof(m)){"OCe15100", "PCIe",
2333 "Obsolete, Unsupported FCoE"};
2334 break;
James Smartd38dd522015-08-31 16:48:17 -04002335 case PCI_DEVICE_ID_LANCER_G6_FC:
2336 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2337 break;
James Smartf8cafd32012-08-03 12:42:51 -04002338 case PCI_DEVICE_ID_SKYHAWK:
2339 case PCI_DEVICE_ID_SKYHAWK_VF:
2340 oneConnect = 1;
2341 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2342 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002343 default:
James Smarta747c9c2009-11-18 15:41:10 -05002344 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002345 break;
dea31012005-04-17 16:05:31 -05002346 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002347
2348 if (mdp && mdp[0] == '\0')
2349 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002350 /*
2351 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002352 * and we put the port number on the end
2353 */
2354 if (descp && descp[0] == '\0') {
2355 if (oneConnect)
2356 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002357 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002358 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002359 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002360 else if (max_speed == 0)
2361 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002362 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002363 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002364 else
2365 snprintf(descp, 255,
2366 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002367 m.name, max_speed, (GE) ? "GE" : "Gb",
2368 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002369 }
dea31012005-04-17 16:05:31 -05002370}
2371
James Smarte59058c2008-08-24 21:49:00 -04002372/**
James Smart3621a712009-04-06 18:47:14 -04002373 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002374 * @phba: pointer to lpfc hba data structure.
2375 * @pring: pointer to a IOCB ring.
2376 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2377 *
2378 * This routine posts a given number of IOCBs with the associated DMA buffer
2379 * descriptors specified by the cnt argument to the given IOCB ring.
2380 *
2381 * Return codes
2382 * The number of IOCBs NOT able to be posted to the IOCB ring.
2383 **/
dea31012005-04-17 16:05:31 -05002384int
James Smart495a7142008-06-14 22:52:59 -04002385lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002386{
2387 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002388 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002389 struct lpfc_dmabuf *mp1, *mp2;
2390
2391 cnt += pring->missbufcnt;
2392
2393 /* While there are buffers to post */
2394 while (cnt > 0) {
2395 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002396 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002397 if (iocb == NULL) {
2398 pring->missbufcnt = cnt;
2399 return cnt;
2400 }
dea31012005-04-17 16:05:31 -05002401 icmd = &iocb->iocb;
2402
2403 /* 2 buffers can be posted per command */
2404 /* Allocate buffer to post */
2405 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2406 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002407 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2408 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002409 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002410 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002411 pring->missbufcnt = cnt;
2412 return cnt;
2413 }
2414
2415 INIT_LIST_HEAD(&mp1->list);
2416 /* Allocate buffer to post */
2417 if (cnt > 1) {
2418 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2419 if (mp2)
2420 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2421 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002422 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002423 kfree(mp2);
dea31012005-04-17 16:05:31 -05002424 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2425 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002426 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002427 pring->missbufcnt = cnt;
2428 return cnt;
2429 }
2430
2431 INIT_LIST_HEAD(&mp2->list);
2432 } else {
2433 mp2 = NULL;
2434 }
2435
2436 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2437 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2438 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2439 icmd->ulpBdeCount = 1;
2440 cnt--;
2441 if (mp2) {
2442 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2443 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2444 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2445 cnt--;
2446 icmd->ulpBdeCount = 2;
2447 }
2448
2449 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2450 icmd->ulpLe = 1;
2451
James Smart3772a992009-05-22 14:50:54 -04002452 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2453 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002454 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2455 kfree(mp1);
2456 cnt++;
2457 if (mp2) {
2458 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2459 kfree(mp2);
2460 cnt++;
2461 }
James Bottomley604a3e32005-10-29 10:28:33 -05002462 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002463 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002464 return cnt;
2465 }
dea31012005-04-17 16:05:31 -05002466 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002467 if (mp2)
dea31012005-04-17 16:05:31 -05002468 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002469 }
2470 pring->missbufcnt = 0;
2471 return 0;
2472}
2473
James Smarte59058c2008-08-24 21:49:00 -04002474/**
James Smart3621a712009-04-06 18:47:14 -04002475 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002476 * @phba: pointer to lpfc hba data structure.
2477 *
2478 * This routine posts initial receive IOCB buffers to the ELS ring. The
2479 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002480 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002481 *
2482 * Return codes
2483 * 0 - success (currently always success)
2484 **/
dea31012005-04-17 16:05:31 -05002485static int
James Smart2e0fef82007-06-17 19:56:36 -05002486lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002487{
2488 struct lpfc_sli *psli = &phba->sli;
2489
2490 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002491 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002492 /* Ring 2 - FCP no buffers needed */
2493
2494 return 0;
2495}
2496
2497#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2498
James Smarte59058c2008-08-24 21:49:00 -04002499/**
James Smart3621a712009-04-06 18:47:14 -04002500 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002501 * @HashResultPointer: pointer to an array as hash table.
2502 *
2503 * This routine sets up the initial values to the array of hash table entries
2504 * for the LC HBAs.
2505 **/
dea31012005-04-17 16:05:31 -05002506static void
2507lpfc_sha_init(uint32_t * HashResultPointer)
2508{
2509 HashResultPointer[0] = 0x67452301;
2510 HashResultPointer[1] = 0xEFCDAB89;
2511 HashResultPointer[2] = 0x98BADCFE;
2512 HashResultPointer[3] = 0x10325476;
2513 HashResultPointer[4] = 0xC3D2E1F0;
2514}
2515
James Smarte59058c2008-08-24 21:49:00 -04002516/**
James Smart3621a712009-04-06 18:47:14 -04002517 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002518 * @HashResultPointer: pointer to an initial/result hash table.
2519 * @HashWorkingPointer: pointer to an working hash table.
2520 *
2521 * This routine iterates an initial hash table pointed by @HashResultPointer
2522 * with the values from the working hash table pointeed by @HashWorkingPointer.
2523 * The results are putting back to the initial hash table, returned through
2524 * the @HashResultPointer as the result hash table.
2525 **/
dea31012005-04-17 16:05:31 -05002526static void
2527lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2528{
2529 int t;
2530 uint32_t TEMP;
2531 uint32_t A, B, C, D, E;
2532 t = 16;
2533 do {
2534 HashWorkingPointer[t] =
2535 S(1,
2536 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2537 8] ^
2538 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2539 } while (++t <= 79);
2540 t = 0;
2541 A = HashResultPointer[0];
2542 B = HashResultPointer[1];
2543 C = HashResultPointer[2];
2544 D = HashResultPointer[3];
2545 E = HashResultPointer[4];
2546
2547 do {
2548 if (t < 20) {
2549 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2550 } else if (t < 40) {
2551 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2552 } else if (t < 60) {
2553 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2554 } else {
2555 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2556 }
2557 TEMP += S(5, A) + E + HashWorkingPointer[t];
2558 E = D;
2559 D = C;
2560 C = S(30, B);
2561 B = A;
2562 A = TEMP;
2563 } while (++t <= 79);
2564
2565 HashResultPointer[0] += A;
2566 HashResultPointer[1] += B;
2567 HashResultPointer[2] += C;
2568 HashResultPointer[3] += D;
2569 HashResultPointer[4] += E;
2570
2571}
2572
James Smarte59058c2008-08-24 21:49:00 -04002573/**
James Smart3621a712009-04-06 18:47:14 -04002574 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002575 * @RandomChallenge: pointer to the entry of host challenge random number array.
2576 * @HashWorking: pointer to the entry of the working hash array.
2577 *
2578 * This routine calculates the working hash array referred by @HashWorking
2579 * from the challenge random numbers associated with the host, referred by
2580 * @RandomChallenge. The result is put into the entry of the working hash
2581 * array and returned by reference through @HashWorking.
2582 **/
dea31012005-04-17 16:05:31 -05002583static void
2584lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2585{
2586 *HashWorking = (*RandomChallenge ^ *HashWorking);
2587}
2588
James Smarte59058c2008-08-24 21:49:00 -04002589/**
James Smart3621a712009-04-06 18:47:14 -04002590 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002591 * @phba: pointer to lpfc hba data structure.
2592 * @hbainit: pointer to an array of unsigned 32-bit integers.
2593 *
2594 * This routine performs the special handling for LC HBA initialization.
2595 **/
dea31012005-04-17 16:05:31 -05002596void
2597lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2598{
2599 int t;
2600 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002601 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002602
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002603 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002604 if (!HashWorking)
2605 return;
2606
dea31012005-04-17 16:05:31 -05002607 HashWorking[0] = HashWorking[78] = *pwwnn++;
2608 HashWorking[1] = HashWorking[79] = *pwwnn;
2609
2610 for (t = 0; t < 7; t++)
2611 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2612
2613 lpfc_sha_init(hbainit);
2614 lpfc_sha_iterate(hbainit, HashWorking);
2615 kfree(HashWorking);
2616}
2617
James Smarte59058c2008-08-24 21:49:00 -04002618/**
James Smart3621a712009-04-06 18:47:14 -04002619 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002620 * @vport: pointer to a virtual N_Port data structure.
2621 *
2622 * This routine performs the necessary cleanups before deleting the @vport.
2623 * It invokes the discovery state machine to perform necessary state
2624 * transitions and to release the ndlps associated with the @vport. Note,
2625 * the physical port is treated as @vport 0.
2626 **/
James Smart87af33f2007-10-27 13:37:43 -04002627void
James Smart2e0fef82007-06-17 19:56:36 -05002628lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002629{
James Smart87af33f2007-10-27 13:37:43 -04002630 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002631 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002632 int i = 0;
dea31012005-04-17 16:05:31 -05002633
James Smart87af33f2007-10-27 13:37:43 -04002634 if (phba->link_state > LPFC_LINK_DOWN)
2635 lpfc_port_link_failure(vport);
2636
2637 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002638 if (!NLP_CHK_NODE_ACT(ndlp)) {
2639 ndlp = lpfc_enable_node(vport, ndlp,
2640 NLP_STE_UNUSED_NODE);
2641 if (!ndlp)
2642 continue;
2643 spin_lock_irq(&phba->ndlp_lock);
2644 NLP_SET_FREE_REQ(ndlp);
2645 spin_unlock_irq(&phba->ndlp_lock);
2646 /* Trigger the release of the ndlp memory */
2647 lpfc_nlp_put(ndlp);
2648 continue;
2649 }
2650 spin_lock_irq(&phba->ndlp_lock);
2651 if (NLP_CHK_FREE_REQ(ndlp)) {
2652 /* The ndlp should not be in memory free mode already */
2653 spin_unlock_irq(&phba->ndlp_lock);
2654 continue;
2655 } else
2656 /* Indicate request for freeing ndlp memory */
2657 NLP_SET_FREE_REQ(ndlp);
2658 spin_unlock_irq(&phba->ndlp_lock);
2659
James Smart58da1ff2008-04-07 10:15:56 -04002660 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2661 ndlp->nlp_DID == Fabric_DID) {
2662 /* Just free up ndlp with Fabric_DID for vports */
2663 lpfc_nlp_put(ndlp);
2664 continue;
2665 }
2666
James Smarteff4a012012-01-18 16:25:25 -05002667 /* take care of nodes in unused state before the state
2668 * machine taking action.
2669 */
2670 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2671 lpfc_nlp_put(ndlp);
2672 continue;
2673 }
2674
James Smart87af33f2007-10-27 13:37:43 -04002675 if (ndlp->nlp_type & NLP_FABRIC)
2676 lpfc_disc_state_machine(vport, ndlp, NULL,
2677 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002678
James Smarta0f2d3e2017-02-12 13:52:31 -08002679 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
2680 /* Remove the NVME transport reference now and
2681 * continue to remove the node.
2682 */
2683 lpfc_nlp_put(ndlp);
2684 }
2685
James Smart87af33f2007-10-27 13:37:43 -04002686 lpfc_disc_state_machine(vport, ndlp, NULL,
2687 NLP_EVT_DEVICE_RM);
2688 }
2689
James Smarta8adb832007-10-27 13:37:53 -04002690 /* At this point, ALL ndlp's should be gone
2691 * because of the previous NLP_EVT_DEVICE_RM.
2692 * Lets wait for this to happen, if needed.
2693 */
James Smart87af33f2007-10-27 13:37:43 -04002694 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002695 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002696 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002697 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002698 list_for_each_entry_safe(ndlp, next_ndlp,
2699 &vport->fc_nodes, nlp_listp) {
2700 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2701 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002702 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002703 "usgmap:x%x refcnt:%d\n",
2704 ndlp->nlp_DID, (void *)ndlp,
2705 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002706 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002707 }
James Smarta8adb832007-10-27 13:37:53 -04002708 break;
James Smart87af33f2007-10-27 13:37:43 -04002709 }
James Smarta8adb832007-10-27 13:37:53 -04002710
2711 /* Wait for any activity on ndlps to settle */
2712 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002713 }
James Smart1151e3e2011-02-16 12:39:35 -05002714 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002715}
2716
James Smarte59058c2008-08-24 21:49:00 -04002717/**
James Smart3621a712009-04-06 18:47:14 -04002718 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002719 * @vport: pointer to a virtual N_Port data structure.
2720 *
2721 * This routine stops all the timers associated with a @vport. This function
2722 * is invoked before disabling or deleting a @vport. Note that the physical
2723 * port is treated as @vport 0.
2724 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002725void
2726lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002727{
James Smart92d7f7b2007-06-17 19:56:38 -05002728 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002729 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002730 lpfc_can_disctmo(vport);
2731 return;
dea31012005-04-17 16:05:31 -05002732}
2733
James Smarte59058c2008-08-24 21:49:00 -04002734/**
James Smartecfd03c2010-02-12 14:41:27 -05002735 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2736 * @phba: pointer to lpfc hba data structure.
2737 *
2738 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2739 * caller of this routine should already hold the host lock.
2740 **/
2741void
2742__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2743{
James Smart5ac6b302010-10-22 11:05:36 -04002744 /* Clear pending FCF rediscovery wait flag */
2745 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2746
James Smartecfd03c2010-02-12 14:41:27 -05002747 /* Now, try to stop the timer */
2748 del_timer(&phba->fcf.redisc_wait);
2749}
2750
2751/**
2752 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2753 * @phba: pointer to lpfc hba data structure.
2754 *
2755 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2756 * checks whether the FCF rediscovery wait timer is pending with the host
2757 * lock held before proceeding with disabling the timer and clearing the
2758 * wait timer pendig flag.
2759 **/
2760void
2761lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2762{
2763 spin_lock_irq(&phba->hbalock);
2764 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2765 /* FCF rediscovery timer already fired or stopped */
2766 spin_unlock_irq(&phba->hbalock);
2767 return;
2768 }
2769 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002770 /* Clear failover in progress flags */
2771 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002772 spin_unlock_irq(&phba->hbalock);
2773}
2774
2775/**
James Smart3772a992009-05-22 14:50:54 -04002776 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002777 * @phba: pointer to lpfc hba data structure.
2778 *
2779 * This routine stops all the timers associated with a HBA. This function is
2780 * invoked before either putting a HBA offline or unloading the driver.
2781 **/
James Smart3772a992009-05-22 14:50:54 -04002782void
2783lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002784{
James Smart51ef4c22007-08-02 11:10:31 -04002785 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002786 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002787 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002788 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002789 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002790 if (phba->sli_rev == LPFC_SLI_REV4) {
2791 del_timer_sync(&phba->rrq_tmr);
2792 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2793 }
James Smart3772a992009-05-22 14:50:54 -04002794 phba->hb_outstanding = 0;
2795
2796 switch (phba->pci_dev_grp) {
2797 case LPFC_PCI_DEV_LP:
2798 /* Stop any LightPulse device specific driver timers */
2799 del_timer_sync(&phba->fcp_poll_timer);
2800 break;
2801 case LPFC_PCI_DEV_OC:
2802 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002803 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002804 break;
2805 default:
2806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2807 "0297 Invalid device group (x%x)\n",
2808 phba->pci_dev_grp);
2809 break;
2810 }
James Smart2e0fef82007-06-17 19:56:36 -05002811 return;
dea31012005-04-17 16:05:31 -05002812}
2813
James Smarte59058c2008-08-24 21:49:00 -04002814/**
James Smart3621a712009-04-06 18:47:14 -04002815 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002816 * @phba: pointer to lpfc hba data structure.
2817 *
2818 * This routine marks a HBA's management interface as blocked. Once the HBA's
2819 * management interface is marked as blocked, all the user space access to
2820 * the HBA, whether they are from sysfs interface or libdfc interface will
2821 * all be blocked. The HBA is set to block the management interface when the
2822 * driver prepares the HBA interface for online or offline.
2823 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002824static void
James Smart618a5232012-06-12 13:54:36 -04002825lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002826{
2827 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002828 uint8_t actcmd = MBX_HEARTBEAT;
2829 unsigned long timeout;
2830
Adrian Bunka6ababd2007-11-05 18:07:33 +01002831 spin_lock_irqsave(&phba->hbalock, iflag);
2832 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002833 spin_unlock_irqrestore(&phba->hbalock, iflag);
2834 if (mbx_action == LPFC_MBX_NO_WAIT)
2835 return;
2836 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2837 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002838 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002839 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002840 /* Determine how long we might wait for the active mailbox
2841 * command to be gracefully completed by firmware.
2842 */
2843 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2844 phba->sli.mbox_active) * 1000) + jiffies;
2845 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002846 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002847
James Smart6e7288d2010-06-07 15:23:35 -04002848 /* Wait for the outstnading mailbox command to complete */
2849 while (phba->sli.mbox_active) {
2850 /* Check active mailbox complete status every 2ms */
2851 msleep(2);
2852 if (time_after(jiffies, timeout)) {
2853 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2854 "2813 Mgmt IO is Blocked %x "
2855 "- mbox cmd %x still active\n",
2856 phba->sli.sli_flag, actcmd);
2857 break;
2858 }
2859 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002860}
2861
James Smarte59058c2008-08-24 21:49:00 -04002862/**
James Smart6b5151f2012-01-18 16:24:06 -05002863 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2864 * @phba: pointer to lpfc hba data structure.
2865 *
2866 * Allocate RPIs for all active remote nodes. This is needed whenever
2867 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2868 * is to fixup the temporary rpi assignments.
2869 **/
2870void
2871lpfc_sli4_node_prep(struct lpfc_hba *phba)
2872{
2873 struct lpfc_nodelist *ndlp, *next_ndlp;
2874 struct lpfc_vport **vports;
2875 int i;
2876
2877 if (phba->sli_rev != LPFC_SLI_REV4)
2878 return;
2879
2880 vports = lpfc_create_vport_work_array(phba);
2881 if (vports != NULL) {
2882 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2883 if (vports[i]->load_flag & FC_UNLOADING)
2884 continue;
2885
2886 list_for_each_entry_safe(ndlp, next_ndlp,
2887 &vports[i]->fc_nodes,
2888 nlp_listp) {
James Smartbe6bb942015-04-07 15:07:22 -04002889 if (NLP_CHK_NODE_ACT(ndlp)) {
James Smart6b5151f2012-01-18 16:24:06 -05002890 ndlp->nlp_rpi =
2891 lpfc_sli4_alloc_rpi(phba);
James Smartbe6bb942015-04-07 15:07:22 -04002892 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
2893 LOG_NODE,
2894 "0009 rpi:%x DID:%x "
2895 "flg:%x map:%x %p\n",
2896 ndlp->nlp_rpi,
2897 ndlp->nlp_DID,
2898 ndlp->nlp_flag,
2899 ndlp->nlp_usg_map,
2900 ndlp);
2901 }
James Smart6b5151f2012-01-18 16:24:06 -05002902 }
2903 }
2904 }
2905 lpfc_destroy_vport_work_array(phba, vports);
2906}
2907
2908/**
James Smart3621a712009-04-06 18:47:14 -04002909 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002910 * @phba: pointer to lpfc hba data structure.
2911 *
2912 * This routine initializes the HBA and brings a HBA online. During this
2913 * process, the management interface is blocked to prevent user space access
2914 * to the HBA interfering with the driver initialization.
2915 *
2916 * Return codes
2917 * 0 - successful
2918 * 1 - failed
2919 **/
dea31012005-04-17 16:05:31 -05002920int
James Smart2e0fef82007-06-17 19:56:36 -05002921lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002922{
Julia Lawall372bd282008-12-16 16:15:08 +01002923 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002924 struct lpfc_vport **vports;
2925 int i;
James Smart16a3a202013-04-17 20:14:38 -04002926 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002927
dea31012005-04-17 16:05:31 -05002928 if (!phba)
2929 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002930 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002931
James Smart2e0fef82007-06-17 19:56:36 -05002932 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002933 return 0;
2934
James Smarted957682007-06-17 19:56:37 -05002935 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002936 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002937
James Smart618a5232012-06-12 13:54:36 -04002938 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002939
James Smartda0436e2009-05-22 14:51:39 -04002940 if (phba->sli_rev == LPFC_SLI_REV4) {
2941 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2942 lpfc_unblock_mgmt_io(phba);
2943 return 1;
2944 }
James Smart16a3a202013-04-17 20:14:38 -04002945 spin_lock_irq(&phba->hbalock);
2946 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2947 vpis_cleared = true;
2948 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002949 } else {
James Smart895427b2017-02-12 13:52:30 -08002950 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04002951 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2952 lpfc_unblock_mgmt_io(phba);
2953 return 1;
2954 }
James Smart46fa3112007-04-25 09:51:45 -04002955 }
dea31012005-04-17 16:05:31 -05002956
James Smart549e55c2007-08-02 11:09:51 -04002957 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01002958 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002959 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002960 struct Scsi_Host *shost;
2961 shost = lpfc_shost_from_vport(vports[i]);
2962 spin_lock_irq(shost->host_lock);
2963 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2964 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2965 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002966 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002967 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002968 if ((vpis_cleared) &&
2969 (vports[i]->port_type !=
2970 LPFC_PHYSICAL_PORT))
2971 vports[i]->vpi = 0;
2972 }
James Smart549e55c2007-08-02 11:09:51 -04002973 spin_unlock_irq(shost->host_lock);
2974 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01002975 }
2976 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002977
James Smart46fa3112007-04-25 09:51:45 -04002978 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002979 return 0;
2980}
2981
James Smarte59058c2008-08-24 21:49:00 -04002982/**
James Smart3621a712009-04-06 18:47:14 -04002983 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002984 * @phba: pointer to lpfc hba data structure.
2985 *
2986 * This routine marks a HBA's management interface as not blocked. Once the
2987 * HBA's management interface is marked as not blocked, all the user space
2988 * access to the HBA, whether they are from sysfs interface or libdfc
2989 * interface will be allowed. The HBA is set to block the management interface
2990 * when the driver prepares the HBA interface for online or offline and then
2991 * set to unblock the management interface afterwards.
2992 **/
James Smart46fa3112007-04-25 09:51:45 -04002993void
James Smart46fa3112007-04-25 09:51:45 -04002994lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2995{
2996 unsigned long iflag;
2997
James Smart2e0fef82007-06-17 19:56:36 -05002998 spin_lock_irqsave(&phba->hbalock, iflag);
2999 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3000 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003001}
3002
James Smarte59058c2008-08-24 21:49:00 -04003003/**
James Smart3621a712009-04-06 18:47:14 -04003004 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003005 * @phba: pointer to lpfc hba data structure.
3006 *
3007 * This routine is invoked to prepare a HBA to be brought offline. It performs
3008 * unregistration login to all the nodes on all vports and flushes the mailbox
3009 * queue to make it ready to be brought offline.
3010 **/
James Smart46fa3112007-04-25 09:51:45 -04003011void
James Smart618a5232012-06-12 13:54:36 -04003012lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003013{
James Smart2e0fef82007-06-17 19:56:36 -05003014 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003015 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003016 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003017 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003018 int i;
dea31012005-04-17 16:05:31 -05003019
James Smart2e0fef82007-06-17 19:56:36 -05003020 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003021 return;
dea31012005-04-17 16:05:31 -05003022
James Smart618a5232012-06-12 13:54:36 -04003023 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003024
3025 lpfc_linkdown(phba);
3026
James Smart87af33f2007-10-27 13:37:43 -04003027 /* Issue an unreg_login to all nodes on all vports */
3028 vports = lpfc_create_vport_work_array(phba);
3029 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003030 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003031 if (vports[i]->load_flag & FC_UNLOADING)
3032 continue;
James Smart72100cc2010-02-12 14:43:01 -05003033 shost = lpfc_shost_from_vport(vports[i]);
3034 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003035 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003036 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3037 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003038 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003039
James Smart87af33f2007-10-27 13:37:43 -04003040 shost = lpfc_shost_from_vport(vports[i]);
3041 list_for_each_entry_safe(ndlp, next_ndlp,
3042 &vports[i]->fc_nodes,
3043 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003044 if (!NLP_CHK_NODE_ACT(ndlp))
3045 continue;
James Smart87af33f2007-10-27 13:37:43 -04003046 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3047 continue;
3048 if (ndlp->nlp_type & NLP_FABRIC) {
3049 lpfc_disc_state_machine(vports[i], ndlp,
3050 NULL, NLP_EVT_DEVICE_RECOVERY);
3051 lpfc_disc_state_machine(vports[i], ndlp,
3052 NULL, NLP_EVT_DEVICE_RM);
3053 }
3054 spin_lock_irq(shost->host_lock);
3055 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003056 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003057 /*
3058 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003059 * RPI. Get a new RPI when the adapter port
3060 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003061 */
James Smartbe6bb942015-04-07 15:07:22 -04003062 if (phba->sli_rev == LPFC_SLI_REV4) {
3063 lpfc_printf_vlog(ndlp->vport,
3064 KERN_INFO, LOG_NODE,
3065 "0011 lpfc_offline: "
3066 "ndlp:x%p did %x "
3067 "usgmap:x%x rpi:%x\n",
3068 ndlp, ndlp->nlp_DID,
3069 ndlp->nlp_usg_map,
3070 ndlp->nlp_rpi);
3071
James Smart6b5151f2012-01-18 16:24:06 -05003072 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003073 }
James Smart87af33f2007-10-27 13:37:43 -04003074 lpfc_unreg_rpi(vports[i], ndlp);
3075 }
3076 }
3077 }
James Smart09372822008-01-11 01:52:54 -05003078 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003079
James Smart618a5232012-06-12 13:54:36 -04003080 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04003081}
3082
James Smarte59058c2008-08-24 21:49:00 -04003083/**
James Smart3621a712009-04-06 18:47:14 -04003084 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003085 * @phba: pointer to lpfc hba data structure.
3086 *
3087 * This routine actually brings a HBA offline. It stops all the timers
3088 * associated with the HBA, brings down the SLI layer, and eventually
3089 * marks the HBA as in offline state for the upper layer protocol.
3090 **/
James Smart46fa3112007-04-25 09:51:45 -04003091void
James Smart2e0fef82007-06-17 19:56:36 -05003092lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003093{
James Smart549e55c2007-08-02 11:09:51 -04003094 struct Scsi_Host *shost;
3095 struct lpfc_vport **vports;
3096 int i;
James Smart46fa3112007-04-25 09:51:45 -04003097
James Smart549e55c2007-08-02 11:09:51 -04003098 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003099 return;
James Smart688a8862006-07-06 15:49:56 -04003100
James Smartda0436e2009-05-22 14:51:39 -04003101 /* stop port and all timers associated with this hba */
3102 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04003103 vports = lpfc_create_vport_work_array(phba);
3104 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003105 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003106 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003107 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003108 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003109 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003110 /* Bring down the SLI Layer and cleanup. The HBA is offline
3111 now. */
3112 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003113 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003114 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003115 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003116 vports = lpfc_create_vport_work_array(phba);
3117 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003118 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003119 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003120 spin_lock_irq(shost->host_lock);
3121 vports[i]->work_port_events = 0;
3122 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3123 spin_unlock_irq(shost->host_lock);
3124 }
James Smart09372822008-01-11 01:52:54 -05003125 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003126}
3127
James Smarte59058c2008-08-24 21:49:00 -04003128/**
James Smart3621a712009-04-06 18:47:14 -04003129 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003130 * @phba: pointer to lpfc hba data structure.
3131 *
3132 * This routine is to free all the SCSI buffers and IOCBs from the driver
3133 * list back to kernel. It is called from lpfc_pci_remove_one to free
3134 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003135 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003136static void
James Smart2e0fef82007-06-17 19:56:36 -05003137lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003138{
3139 struct lpfc_scsi_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003140
James Smart895427b2017-02-12 13:52:30 -08003141 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3142 return;
3143
James Smart2e0fef82007-06-17 19:56:36 -05003144 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003145
dea31012005-04-17 16:05:31 -05003146 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003147
3148 spin_lock(&phba->scsi_buf_list_put_lock);
3149 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3150 list) {
dea31012005-04-17 16:05:31 -05003151 list_del(&sb->list);
James Smart895427b2017-02-12 13:52:30 -08003152 pci_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003153 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003154 kfree(sb);
3155 phba->total_scsi_bufs--;
3156 }
James Smarta40fc5f2013-04-17 20:17:40 -04003157 spin_unlock(&phba->scsi_buf_list_put_lock);
3158
3159 spin_lock(&phba->scsi_buf_list_get_lock);
3160 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3161 list) {
3162 list_del(&sb->list);
James Smart895427b2017-02-12 13:52:30 -08003163 pci_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003164 sb->dma_handle);
3165 kfree(sb);
3166 phba->total_scsi_bufs--;
3167 }
3168 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003169 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003170}
James Smart8a9d2e82012-05-09 21:16:12 -04003171/**
James Smart895427b2017-02-12 13:52:30 -08003172 * lpfc_nvme_free - Free all the NVME buffers and IOCBs from driver lists
3173 * @phba: pointer to lpfc hba data structure.
3174 *
3175 * This routine is to free all the NVME buffers and IOCBs from the driver
3176 * list back to kernel. It is called from lpfc_pci_remove_one to free
3177 * the internal resources before the device is removed from the system.
3178 **/
3179static void
3180lpfc_nvme_free(struct lpfc_hba *phba)
3181{
3182 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart895427b2017-02-12 13:52:30 -08003183
3184 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3185 return;
3186
3187 spin_lock_irq(&phba->hbalock);
3188
3189 /* Release all the lpfc_nvme_bufs maintained by this host. */
3190 spin_lock(&phba->nvme_buf_list_put_lock);
3191 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3192 &phba->lpfc_nvme_buf_list_put, list) {
3193 list_del(&lpfc_ncmd->list);
3194 pci_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3195 lpfc_ncmd->dma_handle);
3196 kfree(lpfc_ncmd);
3197 phba->total_nvme_bufs--;
3198 }
3199 spin_unlock(&phba->nvme_buf_list_put_lock);
3200
3201 spin_lock(&phba->nvme_buf_list_get_lock);
3202 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3203 &phba->lpfc_nvme_buf_list_get, list) {
3204 list_del(&lpfc_ncmd->list);
3205 pci_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3206 lpfc_ncmd->dma_handle);
3207 kfree(lpfc_ncmd);
3208 phba->total_nvme_bufs--;
3209 }
3210 spin_unlock(&phba->nvme_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003211 spin_unlock_irq(&phba->hbalock);
3212}
3213/**
3214 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003215 * @phba: pointer to lpfc hba data structure.
3216 *
3217 * This routine first calculates the sizes of the current els and allocated
3218 * scsi sgl lists, and then goes through all sgls to updates the physical
3219 * XRIs assigned due to port function reset. During port initialization, the
3220 * current els and allocated scsi sgl lists are 0s.
3221 *
3222 * Return codes
3223 * 0 - successful (for now, it always returns 0)
3224 **/
3225int
James Smart895427b2017-02-12 13:52:30 -08003226lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003227{
3228 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003229 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003230 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003231 int rc;
3232
3233 /*
3234 * update on pci function's els xri-sgl list
3235 */
3236 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003237
James Smart8a9d2e82012-05-09 21:16:12 -04003238 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3239 /* els xri-sgl expanded */
3240 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3241 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3242 "3157 ELS xri-sgl count increased from "
3243 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3244 els_xri_cnt);
3245 /* allocate the additional els sgls */
3246 for (i = 0; i < xri_cnt; i++) {
3247 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3248 GFP_KERNEL);
3249 if (sglq_entry == NULL) {
3250 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3251 "2562 Failure to allocate an "
3252 "ELS sgl entry:%d\n", i);
3253 rc = -ENOMEM;
3254 goto out_free_mem;
3255 }
3256 sglq_entry->buff_type = GEN_BUFF_TYPE;
3257 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3258 &sglq_entry->phys);
3259 if (sglq_entry->virt == NULL) {
3260 kfree(sglq_entry);
3261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3262 "2563 Failure to allocate an "
3263 "ELS mbuf:%d\n", i);
3264 rc = -ENOMEM;
3265 goto out_free_mem;
3266 }
3267 sglq_entry->sgl = sglq_entry->virt;
3268 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3269 sglq_entry->state = SGL_FREED;
3270 list_add_tail(&sglq_entry->list, &els_sgl_list);
3271 }
James Smart38c20672013-03-01 16:37:44 -05003272 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003273 spin_lock(&phba->sli4_hba.sgl_list_lock);
3274 list_splice_init(&els_sgl_list,
3275 &phba->sli4_hba.lpfc_els_sgl_list);
3276 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05003277 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003278 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3279 /* els xri-sgl shrinked */
3280 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3281 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3282 "3158 ELS xri-sgl count decreased from "
3283 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3284 els_xri_cnt);
3285 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003286 spin_lock(&phba->sli4_hba.sgl_list_lock);
3287 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3288 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003289 /* release extra els sgls from list */
3290 for (i = 0; i < xri_cnt; i++) {
3291 list_remove_head(&els_sgl_list,
3292 sglq_entry, struct lpfc_sglq, list);
3293 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003294 __lpfc_mbuf_free(phba, sglq_entry->virt,
3295 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003296 kfree(sglq_entry);
3297 }
3298 }
James Smart895427b2017-02-12 13:52:30 -08003299 list_splice_init(&els_sgl_list,
3300 &phba->sli4_hba.lpfc_els_sgl_list);
3301 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003302 spin_unlock_irq(&phba->hbalock);
3303 } else
3304 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3305 "3163 ELS xri-sgl count unchanged: %d\n",
3306 els_xri_cnt);
3307 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3308
3309 /* update xris to els sgls on the list */
3310 sglq_entry = NULL;
3311 sglq_entry_next = NULL;
3312 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003313 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003314 lxri = lpfc_sli4_next_xritag(phba);
3315 if (lxri == NO_XRI) {
3316 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3317 "2400 Failed to allocate xri for "
3318 "ELS sgl\n");
3319 rc = -ENOMEM;
3320 goto out_free_mem;
3321 }
3322 sglq_entry->sli4_lxritag = lxri;
3323 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3324 }
James Smart895427b2017-02-12 13:52:30 -08003325 return 0;
3326
3327out_free_mem:
3328 lpfc_free_els_sgl_list(phba);
3329 return rc;
3330}
3331
3332/**
James Smartf358dd02017-02-12 13:52:34 -08003333 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3334 * @phba: pointer to lpfc hba data structure.
3335 *
3336 * This routine first calculates the sizes of the current els and allocated
3337 * scsi sgl lists, and then goes through all sgls to updates the physical
3338 * XRIs assigned due to port function reset. During port initialization, the
3339 * current els and allocated scsi sgl lists are 0s.
3340 *
3341 * Return codes
3342 * 0 - successful (for now, it always returns 0)
3343 **/
3344int
3345lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3346{
3347 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3348 uint16_t i, lxri, xri_cnt, els_xri_cnt;
3349 uint16_t nvmet_xri_cnt, tot_cnt;
3350 LIST_HEAD(nvmet_sgl_list);
3351 int rc;
3352
3353 /*
3354 * update on pci function's nvmet xri-sgl list
3355 */
3356 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3357 nvmet_xri_cnt = 0;
3358 tot_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3359
3360 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3361 /* els xri-sgl expanded */
3362 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3363 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3364 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3365 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3366 /* allocate the additional nvmet sgls */
3367 for (i = 0; i < xri_cnt; i++) {
3368 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3369 GFP_KERNEL);
3370 if (sglq_entry == NULL) {
3371 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3372 "6303 Failure to allocate an "
3373 "NVMET sgl entry:%d\n", i);
3374 rc = -ENOMEM;
3375 goto out_free_mem;
3376 }
3377 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3378 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3379 &sglq_entry->phys);
3380 if (sglq_entry->virt == NULL) {
3381 kfree(sglq_entry);
3382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3383 "6304 Failure to allocate an "
3384 "NVMET buf:%d\n", i);
3385 rc = -ENOMEM;
3386 goto out_free_mem;
3387 }
3388 sglq_entry->sgl = sglq_entry->virt;
3389 memset(sglq_entry->sgl, 0,
3390 phba->cfg_sg_dma_buf_size);
3391 sglq_entry->state = SGL_FREED;
3392 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3393 }
3394 spin_lock_irq(&phba->hbalock);
3395 spin_lock(&phba->sli4_hba.sgl_list_lock);
3396 list_splice_init(&nvmet_sgl_list,
3397 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3398 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3399 spin_unlock_irq(&phba->hbalock);
3400 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3401 /* nvmet xri-sgl shrunk */
3402 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3403 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3404 "6305 NVMET xri-sgl count decreased from "
3405 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3406 nvmet_xri_cnt);
3407 spin_lock_irq(&phba->hbalock);
3408 spin_lock(&phba->sli4_hba.sgl_list_lock);
3409 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3410 &nvmet_sgl_list);
3411 /* release extra nvmet sgls from list */
3412 for (i = 0; i < xri_cnt; i++) {
3413 list_remove_head(&nvmet_sgl_list,
3414 sglq_entry, struct lpfc_sglq, list);
3415 if (sglq_entry) {
3416 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3417 sglq_entry->phys);
3418 kfree(sglq_entry);
3419 }
3420 }
3421 list_splice_init(&nvmet_sgl_list,
3422 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3423 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3424 spin_unlock_irq(&phba->hbalock);
3425 } else
3426 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3427 "6306 NVMET xri-sgl count unchanged: %d\n",
3428 nvmet_xri_cnt);
3429 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3430
3431 /* update xris to nvmet sgls on the list */
3432 sglq_entry = NULL;
3433 sglq_entry_next = NULL;
3434 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3435 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3436 lxri = lpfc_sli4_next_xritag(phba);
3437 if (lxri == NO_XRI) {
3438 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3439 "6307 Failed to allocate xri for "
3440 "NVMET sgl\n");
3441 rc = -ENOMEM;
3442 goto out_free_mem;
3443 }
3444 sglq_entry->sli4_lxritag = lxri;
3445 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3446 }
3447 return 0;
3448
3449out_free_mem:
3450 lpfc_free_nvmet_sgl_list(phba);
3451 return rc;
3452}
3453
3454/**
James Smart895427b2017-02-12 13:52:30 -08003455 * lpfc_sli4_scsi_sgl_update - update xri-sgl sizing and mapping
3456 * @phba: pointer to lpfc hba data structure.
3457 *
3458 * This routine first calculates the sizes of the current els and allocated
3459 * scsi sgl lists, and then goes through all sgls to updates the physical
3460 * XRIs assigned due to port function reset. During port initialization, the
3461 * current els and allocated scsi sgl lists are 0s.
3462 *
3463 * Return codes
3464 * 0 - successful (for now, it always returns 0)
3465 **/
3466int
3467lpfc_sli4_scsi_sgl_update(struct lpfc_hba *phba)
3468{
3469 struct lpfc_scsi_buf *psb, *psb_next;
3470 uint16_t i, lxri, els_xri_cnt, scsi_xri_cnt;
3471 LIST_HEAD(scsi_sgl_list);
3472 int rc;
3473
3474 /*
3475 * update on pci function's els xri-sgl list
3476 */
3477 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3478 phba->total_scsi_bufs = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003479
3480 /*
3481 * update on pci function's allocated scsi xri-sgl list
3482 */
James Smart8a9d2e82012-05-09 21:16:12 -04003483 /* maximum number of xris available for scsi buffers */
3484 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3485 els_xri_cnt;
3486
James Smart895427b2017-02-12 13:52:30 -08003487 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3488 return 0;
3489
3490 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3491 phba->sli4_hba.scsi_xri_max = /* Split them up */
3492 (phba->sli4_hba.scsi_xri_max *
3493 phba->cfg_xri_split) / 100;
James Smart8a9d2e82012-05-09 21:16:12 -04003494
James Smarta40fc5f2013-04-17 20:17:40 -04003495 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003496 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003497 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3498 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003499 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003500 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003501
3502 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3503 /* max scsi xri shrinked below the allocated scsi buffers */
3504 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3505 phba->sli4_hba.scsi_xri_max;
3506 /* release the extra allocated scsi buffers */
3507 for (i = 0; i < scsi_xri_cnt; i++) {
3508 list_remove_head(&scsi_sgl_list, psb,
3509 struct lpfc_scsi_buf, list);
James Smarta2fc4aef2014-09-03 12:57:55 -04003510 if (psb) {
James Smart895427b2017-02-12 13:52:30 -08003511 pci_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smarta2fc4aef2014-09-03 12:57:55 -04003512 psb->data, psb->dma_handle);
3513 kfree(psb);
3514 }
James Smart8a9d2e82012-05-09 21:16:12 -04003515 }
James Smarta40fc5f2013-04-17 20:17:40 -04003516 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003517 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003518 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003519 }
3520
3521 /* update xris associated to remaining allocated scsi buffers */
3522 psb = NULL;
3523 psb_next = NULL;
3524 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3525 lxri = lpfc_sli4_next_xritag(phba);
3526 if (lxri == NO_XRI) {
3527 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3528 "2560 Failed to allocate xri for "
3529 "scsi buffer\n");
3530 rc = -ENOMEM;
3531 goto out_free_mem;
3532 }
3533 psb->cur_iocbq.sli4_lxritag = lxri;
3534 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3535 }
James Smarta40fc5f2013-04-17 20:17:40 -04003536 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003537 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003538 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3539 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003540 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003541 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05003542 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003543
3544out_free_mem:
James Smart8a9d2e82012-05-09 21:16:12 -04003545 lpfc_scsi_free(phba);
3546 return rc;
dea31012005-04-17 16:05:31 -05003547}
3548
James Smarte59058c2008-08-24 21:49:00 -04003549/**
James Smart895427b2017-02-12 13:52:30 -08003550 * lpfc_sli4_nvme_sgl_update - update xri-sgl sizing and mapping
3551 * @phba: pointer to lpfc hba data structure.
3552 *
3553 * This routine first calculates the sizes of the current els and allocated
3554 * scsi sgl lists, and then goes through all sgls to updates the physical
3555 * XRIs assigned due to port function reset. During port initialization, the
3556 * current els and allocated scsi sgl lists are 0s.
3557 *
3558 * Return codes
3559 * 0 - successful (for now, it always returns 0)
3560 **/
3561int
3562lpfc_sli4_nvme_sgl_update(struct lpfc_hba *phba)
3563{
3564 struct lpfc_nvme_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
3565 uint16_t i, lxri, els_xri_cnt;
3566 uint16_t nvme_xri_cnt, nvme_xri_max;
3567 LIST_HEAD(nvme_sgl_list);
3568 int rc;
3569
3570 phba->total_nvme_bufs = 0;
3571
3572 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3573 return 0;
3574 /*
3575 * update on pci function's allocated nvme xri-sgl list
3576 */
3577
3578 /* maximum number of xris available for nvme buffers */
3579 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3580 nvme_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3581 phba->sli4_hba.nvme_xri_max = nvme_xri_max;
3582 phba->sli4_hba.nvme_xri_max -= phba->sli4_hba.scsi_xri_max;
3583
3584 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3585 "6074 Current allocated NVME xri-sgl count:%d, "
3586 "maximum NVME xri count:%d\n",
3587 phba->sli4_hba.nvme_xri_cnt,
3588 phba->sli4_hba.nvme_xri_max);
3589
3590 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3591 spin_lock(&phba->nvme_buf_list_put_lock);
3592 list_splice_init(&phba->lpfc_nvme_buf_list_get, &nvme_sgl_list);
3593 list_splice(&phba->lpfc_nvme_buf_list_put, &nvme_sgl_list);
3594 spin_unlock(&phba->nvme_buf_list_put_lock);
3595 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3596
3597 if (phba->sli4_hba.nvme_xri_cnt > phba->sli4_hba.nvme_xri_max) {
3598 /* max nvme xri shrunk below the allocated nvme buffers */
3599 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3600 nvme_xri_cnt = phba->sli4_hba.nvme_xri_cnt -
3601 phba->sli4_hba.nvme_xri_max;
3602 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3603 /* release the extra allocated nvme buffers */
3604 for (i = 0; i < nvme_xri_cnt; i++) {
3605 list_remove_head(&nvme_sgl_list, lpfc_ncmd,
3606 struct lpfc_nvme_buf, list);
3607 if (lpfc_ncmd) {
3608 pci_pool_free(phba->lpfc_sg_dma_buf_pool,
3609 lpfc_ncmd->data,
3610 lpfc_ncmd->dma_handle);
3611 kfree(lpfc_ncmd);
3612 }
3613 }
3614 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3615 phba->sli4_hba.nvme_xri_cnt -= nvme_xri_cnt;
3616 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3617 }
3618
3619 /* update xris associated to remaining allocated nvme buffers */
3620 lpfc_ncmd = NULL;
3621 lpfc_ncmd_next = NULL;
3622 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3623 &nvme_sgl_list, list) {
3624 lxri = lpfc_sli4_next_xritag(phba);
3625 if (lxri == NO_XRI) {
3626 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3627 "6075 Failed to allocate xri for "
3628 "nvme buffer\n");
3629 rc = -ENOMEM;
3630 goto out_free_mem;
3631 }
3632 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
3633 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3634 }
3635 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3636 spin_lock(&phba->nvme_buf_list_put_lock);
3637 list_splice_init(&nvme_sgl_list, &phba->lpfc_nvme_buf_list_get);
3638 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
3639 spin_unlock(&phba->nvme_buf_list_put_lock);
3640 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3641 return 0;
3642
3643out_free_mem:
3644 lpfc_nvme_free(phba);
3645 return rc;
3646}
3647
3648/**
James Smart3621a712009-04-06 18:47:14 -04003649 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003650 * @phba: pointer to lpfc hba data structure.
3651 * @instance: a unique integer ID to this FC port.
3652 * @dev: pointer to the device data structure.
3653 *
3654 * This routine creates a FC port for the upper layer protocol. The FC port
3655 * can be created on top of either a physical port or a virtual port provided
3656 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3657 * and associates the FC port created before adding the shost into the SCSI
3658 * layer.
3659 *
3660 * Return codes
3661 * @vport - pointer to the virtual N_Port data structure.
3662 * NULL - port create failed.
3663 **/
James Smart2e0fef82007-06-17 19:56:36 -05003664struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003665lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003666{
James Smart2e0fef82007-06-17 19:56:36 -05003667 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08003668 struct Scsi_Host *shost = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003669 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003670
James Smart895427b2017-02-12 13:52:30 -08003671 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
3672 if (dev != &phba->pcidev->dev) {
3673 shost = scsi_host_alloc(&lpfc_vport_template,
3674 sizeof(struct lpfc_vport));
3675 } else {
3676 if (phba->sli_rev == LPFC_SLI_REV4)
3677 shost = scsi_host_alloc(&lpfc_template,
3678 sizeof(struct lpfc_vport));
3679 else
3680 shost = scsi_host_alloc(&lpfc_template_s3,
3681 sizeof(struct lpfc_vport));
3682 }
3683 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
3684 shost = scsi_host_alloc(&lpfc_template_nvme,
James Smartea4142f2015-04-07 15:07:13 -04003685 sizeof(struct lpfc_vport));
3686 }
James Smart2e0fef82007-06-17 19:56:36 -05003687 if (!shost)
3688 goto out;
James Smart47a86172007-04-25 09:53:22 -04003689
James Smart2e0fef82007-06-17 19:56:36 -05003690 vport = (struct lpfc_vport *) shost->hostdata;
3691 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003692 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003693 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003694 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04003695 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08003696
James Smart2e0fef82007-06-17 19:56:36 -05003697 shost->unique_id = instance;
3698 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003699 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003700 shost->this_id = -1;
3701 shost->max_cmd_len = 16;
James Smart8b0dff12015-05-22 10:42:38 -04003702 shost->nr_hw_queues = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04003703 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003704 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003705 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003706 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3707 }
James Smart81301a92008-12-04 22:39:46 -05003708
James Smart47a86172007-04-25 09:53:22 -04003709 /*
James Smart2e0fef82007-06-17 19:56:36 -05003710 * Set initial can_queue value since 0 is no longer supported and
3711 * scsi_add_host will fail. This will be adjusted later based on the
3712 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003713 */
James Smart2e0fef82007-06-17 19:56:36 -05003714 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003715 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003716 shost->transportt = lpfc_vport_transport_template;
3717 vport->port_type = LPFC_NPIV_PORT;
3718 } else {
3719 shost->transportt = lpfc_transport_template;
3720 vport->port_type = LPFC_PHYSICAL_PORT;
3721 }
James Smart47a86172007-04-25 09:53:22 -04003722
James Smart2e0fef82007-06-17 19:56:36 -05003723 /* Initialize all internally managed lists. */
3724 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003725 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003726 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003727
James Smart2e0fef82007-06-17 19:56:36 -05003728 init_timer(&vport->fc_disctmo);
3729 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003730 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003731
James Smart2e0fef82007-06-17 19:56:36 -05003732 init_timer(&vport->els_tmofunc);
3733 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003734 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003735
3736 init_timer(&vport->delayed_disc_tmo);
3737 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3738 vport->delayed_disc_tmo.data = (unsigned long)vport;
3739
James Bottomleyd139b9b2009-11-05 13:33:12 -06003740 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003741 if (error)
3742 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003743
James Smart549e55c2007-08-02 11:09:51 -04003744 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003745 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003746 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003747 return vport;
James Smart47a86172007-04-25 09:53:22 -04003748
James Smart2e0fef82007-06-17 19:56:36 -05003749out_put_shost:
3750 scsi_host_put(shost);
3751out:
3752 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003753}
3754
James Smarte59058c2008-08-24 21:49:00 -04003755/**
James Smart3621a712009-04-06 18:47:14 -04003756 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003757 * @vport: pointer to an lpfc virtual N_Port data structure.
3758 *
3759 * This routine destroys a FC port from the upper layer protocol. All the
3760 * resources associated with the port are released.
3761 **/
James Smart2e0fef82007-06-17 19:56:36 -05003762void
3763destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003764{
James Smart92d7f7b2007-06-17 19:56:38 -05003765 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3766 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003767
James Smart858c9f62007-06-17 19:56:39 -05003768 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003769 fc_remove_host(shost);
3770 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003771
James Smart92d7f7b2007-06-17 19:56:38 -05003772 spin_lock_irq(&phba->hbalock);
3773 list_del_init(&vport->listentry);
3774 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003775
James Smart92d7f7b2007-06-17 19:56:38 -05003776 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003777 return;
James Smart47a86172007-04-25 09:53:22 -04003778}
3779
James Smarte59058c2008-08-24 21:49:00 -04003780/**
James Smart3621a712009-04-06 18:47:14 -04003781 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003782 *
3783 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3784 * uses the kernel idr facility to perform the task.
3785 *
3786 * Return codes:
3787 * instance - a unique integer ID allocated as the new instance.
3788 * -1 - lpfc get instance failed.
3789 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003790int
3791lpfc_get_instance(void)
3792{
Tejun Heoab516032013-02-27 17:04:44 -08003793 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003794
Tejun Heoab516032013-02-27 17:04:44 -08003795 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3796 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003797}
3798
James Smarte59058c2008-08-24 21:49:00 -04003799/**
James Smart3621a712009-04-06 18:47:14 -04003800 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003801 * @shost: pointer to SCSI host data structure.
3802 * @time: elapsed time of the scan in jiffies.
3803 *
3804 * This routine is called by the SCSI layer with a SCSI host to determine
3805 * whether the scan host is finished.
3806 *
3807 * Note: there is no scan_start function as adapter initialization will have
3808 * asynchronously kicked off the link initialization.
3809 *
3810 * Return codes
3811 * 0 - SCSI host scan is not over yet.
3812 * 1 - SCSI host scan is over.
3813 **/
James Smart47a86172007-04-25 09:53:22 -04003814int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3815{
James Smart2e0fef82007-06-17 19:56:36 -05003816 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3817 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003818 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003819
James Smart858c9f62007-06-17 19:56:39 -05003820 spin_lock_irq(shost->host_lock);
3821
James Smart51ef4c22007-08-02 11:10:31 -04003822 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003823 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003824 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003825 }
James Smart256ec0d2013-04-17 20:14:58 -04003826 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003827 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003828 "0461 Scanning longer than 30 "
3829 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003830 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003831 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003832 }
James Smart256ec0d2013-04-17 20:14:58 -04003833 if (time >= msecs_to_jiffies(15 * 1000) &&
3834 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003835 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003836 "0465 Link down longer than 15 "
3837 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003838 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003839 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003840 }
3841
James Smart2e0fef82007-06-17 19:56:36 -05003842 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003843 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003844 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003845 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003846 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003847 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003848 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003849 goto finished;
3850
3851 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003852
3853finished:
James Smart858c9f62007-06-17 19:56:39 -05003854 spin_unlock_irq(shost->host_lock);
3855 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003856}
3857
James Smarte59058c2008-08-24 21:49:00 -04003858/**
James Smart3621a712009-04-06 18:47:14 -04003859 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003860 * @shost: pointer to SCSI host data structure.
3861 *
3862 * This routine initializes a given SCSI host attributes on a FC port. The
3863 * SCSI host can be either on top of a physical port or a virtual port.
3864 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003865void lpfc_host_attrib_init(struct Scsi_Host *shost)
3866{
3867 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3868 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003869 /*
James Smart2e0fef82007-06-17 19:56:36 -05003870 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003871 */
3872
James Smart2e0fef82007-06-17 19:56:36 -05003873 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3874 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003875 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3876
3877 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003878 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003879 fc_host_supported_fc4s(shost)[2] = 1;
3880 fc_host_supported_fc4s(shost)[7] = 1;
3881
James Smart92d7f7b2007-06-17 19:56:38 -05003882 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3883 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003884
3885 fc_host_supported_speeds(shost) = 0;
James Smartd38dd522015-08-31 16:48:17 -04003886 if (phba->lmt & LMT_32Gb)
3887 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
James Smart88a2cfb2011-07-22 18:36:33 -04003888 if (phba->lmt & LMT_16Gb)
3889 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003890 if (phba->lmt & LMT_10Gb)
3891 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003892 if (phba->lmt & LMT_8Gb)
3893 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003894 if (phba->lmt & LMT_4Gb)
3895 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3896 if (phba->lmt & LMT_2Gb)
3897 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3898 if (phba->lmt & LMT_1Gb)
3899 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3900
3901 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003902 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3903 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003904
Mike Christie0af5d702010-09-15 16:52:31 -05003905 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3906
James Smart47a86172007-04-25 09:53:22 -04003907 /* This value is also unchanging */
3908 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003909 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003910 fc_host_active_fc4s(shost)[2] = 1;
3911 fc_host_active_fc4s(shost)[7] = 1;
3912
James Smart92d7f7b2007-06-17 19:56:38 -05003913 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003914 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003915 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003916 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003917}
dea31012005-04-17 16:05:31 -05003918
James Smarte59058c2008-08-24 21:49:00 -04003919/**
James Smartda0436e2009-05-22 14:51:39 -04003920 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003921 * @phba: pointer to lpfc hba data structure.
3922 *
James Smartda0436e2009-05-22 14:51:39 -04003923 * This routine is invoked to stop an SLI3 device port, it stops the device
3924 * from generating interrupts and stops the device driver's timers for the
3925 * device.
James Smarte59058c2008-08-24 21:49:00 -04003926 **/
James Smartdb2378e2008-02-08 18:49:51 -05003927static void
James Smartda0436e2009-05-22 14:51:39 -04003928lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003929{
James Smartda0436e2009-05-22 14:51:39 -04003930 /* Clear all interrupt enable conditions */
3931 writel(0, phba->HCregaddr);
3932 readl(phba->HCregaddr); /* flush */
3933 /* Clear all pending interrupts */
3934 writel(0xffffffff, phba->HAregaddr);
3935 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003936
James Smartda0436e2009-05-22 14:51:39 -04003937 /* Reset some HBA SLI setup states */
3938 lpfc_stop_hba_timers(phba);
3939 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003940}
3941
James Smarte59058c2008-08-24 21:49:00 -04003942/**
James Smartda0436e2009-05-22 14:51:39 -04003943 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003944 * @phba: pointer to lpfc hba data structure.
3945 *
James Smartda0436e2009-05-22 14:51:39 -04003946 * This routine is invoked to stop an SLI4 device port, it stops the device
3947 * from generating interrupts and stops the device driver's timers for the
3948 * device.
3949 **/
3950static void
3951lpfc_stop_port_s4(struct lpfc_hba *phba)
3952{
3953 /* Reset some HBA SLI4 setup states */
3954 lpfc_stop_hba_timers(phba);
3955 phba->pport->work_port_events = 0;
3956 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003957}
3958
3959/**
3960 * lpfc_stop_port - Wrapper function for stopping hba port
3961 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003962 *
James Smartda0436e2009-05-22 14:51:39 -04003963 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3964 * the API jump table function pointer from the lpfc_hba struct.
3965 **/
3966void
3967lpfc_stop_port(struct lpfc_hba *phba)
3968{
3969 phba->lpfc_stop_port(phba);
3970}
3971
3972/**
James Smartecfd03c2010-02-12 14:41:27 -05003973 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3974 * @phba: Pointer to hba for which this call is being executed.
3975 *
3976 * This routine starts the timer waiting for the FCF rediscovery to complete.
3977 **/
3978void
3979lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3980{
3981 unsigned long fcf_redisc_wait_tmo =
3982 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3983 /* Start fcf rediscovery wait period timer */
3984 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3985 spin_lock_irq(&phba->hbalock);
3986 /* Allow action to new fcf asynchronous event */
3987 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3988 /* Mark the FCF rediscovery pending state */
3989 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3990 spin_unlock_irq(&phba->hbalock);
3991}
3992
3993/**
3994 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3995 * @ptr: Map to lpfc_hba data structure pointer.
3996 *
3997 * This routine is invoked when waiting for FCF table rediscover has been
3998 * timed out. If new FCF record(s) has (have) been discovered during the
3999 * wait period, a new FCF event shall be added to the FCOE async event
4000 * list, and then worker thread shall be waked up for processing from the
4001 * worker thread context.
4002 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004003static void
James Smartecfd03c2010-02-12 14:41:27 -05004004lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
4005{
4006 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
4007
4008 /* Don't send FCF rediscovery event if timer cancelled */
4009 spin_lock_irq(&phba->hbalock);
4010 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4011 spin_unlock_irq(&phba->hbalock);
4012 return;
4013 }
4014 /* Clear FCF rediscovery timer pending flag */
4015 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4016 /* FCF rediscovery event to worker thread */
4017 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4018 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004019 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004020 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004021 /* wake up worker thread */
4022 lpfc_worker_wake_up(phba);
4023}
4024
4025/**
James Smartda0436e2009-05-22 14:51:39 -04004026 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4027 * @phba: pointer to lpfc hba data structure.
4028 * @acqe_link: pointer to the async link completion queue entry.
4029 *
4030 * This routine is to parse the SLI4 link-attention link fault code and
4031 * translate it into the base driver's read link attention mailbox command
4032 * status.
4033 *
4034 * Return: Link-attention status in terms of base driver's coding.
4035 **/
4036static uint16_t
4037lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4038 struct lpfc_acqe_link *acqe_link)
4039{
4040 uint16_t latt_fault;
4041
4042 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4043 case LPFC_ASYNC_LINK_FAULT_NONE:
4044 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4045 case LPFC_ASYNC_LINK_FAULT_REMOTE:
4046 latt_fault = 0;
4047 break;
4048 default:
4049 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4050 "0398 Invalid link fault code: x%x\n",
4051 bf_get(lpfc_acqe_link_fault, acqe_link));
4052 latt_fault = MBXERR_ERROR;
4053 break;
4054 }
4055 return latt_fault;
4056}
4057
4058/**
4059 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4060 * @phba: pointer to lpfc hba data structure.
4061 * @acqe_link: pointer to the async link completion queue entry.
4062 *
4063 * This routine is to parse the SLI4 link attention type and translate it
4064 * into the base driver's link attention type coding.
4065 *
4066 * Return: Link attention type in terms of base driver's coding.
4067 **/
4068static uint8_t
4069lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4070 struct lpfc_acqe_link *acqe_link)
4071{
4072 uint8_t att_type;
4073
4074 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4075 case LPFC_ASYNC_LINK_STATUS_DOWN:
4076 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004077 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004078 break;
4079 case LPFC_ASYNC_LINK_STATUS_UP:
4080 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004081 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004082 break;
4083 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004084 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004085 break;
4086 default:
4087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4088 "0399 Invalid link attention type: x%x\n",
4089 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004090 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004091 break;
4092 }
4093 return att_type;
4094}
4095
4096/**
James Smart8b68cd52012-09-29 11:32:37 -04004097 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4098 * @phba: pointer to lpfc hba data structure.
4099 *
4100 * This routine is to get an SLI3 FC port's link speed in Mbps.
4101 *
4102 * Return: link speed in terms of Mbps.
4103 **/
4104uint32_t
4105lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4106{
4107 uint32_t link_speed;
4108
4109 if (!lpfc_is_link_up(phba))
4110 return 0;
4111
James Smarta085e872015-12-16 18:12:02 -05004112 if (phba->sli_rev <= LPFC_SLI_REV3) {
4113 switch (phba->fc_linkspeed) {
4114 case LPFC_LINK_SPEED_1GHZ:
4115 link_speed = 1000;
4116 break;
4117 case LPFC_LINK_SPEED_2GHZ:
4118 link_speed = 2000;
4119 break;
4120 case LPFC_LINK_SPEED_4GHZ:
4121 link_speed = 4000;
4122 break;
4123 case LPFC_LINK_SPEED_8GHZ:
4124 link_speed = 8000;
4125 break;
4126 case LPFC_LINK_SPEED_10GHZ:
4127 link_speed = 10000;
4128 break;
4129 case LPFC_LINK_SPEED_16GHZ:
4130 link_speed = 16000;
4131 break;
4132 default:
4133 link_speed = 0;
4134 }
4135 } else {
4136 if (phba->sli4_hba.link_state.logical_speed)
4137 link_speed =
4138 phba->sli4_hba.link_state.logical_speed;
4139 else
4140 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004141 }
4142 return link_speed;
4143}
4144
4145/**
4146 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4147 * @phba: pointer to lpfc hba data structure.
4148 * @evt_code: asynchronous event code.
4149 * @speed_code: asynchronous event link speed code.
4150 *
4151 * This routine is to parse the giving SLI4 async event link speed code into
4152 * value of Mbps for the link speed.
4153 *
4154 * Return: link speed in terms of Mbps.
4155 **/
4156static uint32_t
4157lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4158 uint8_t speed_code)
4159{
4160 uint32_t port_speed;
4161
4162 switch (evt_code) {
4163 case LPFC_TRAILER_CODE_LINK:
4164 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004165 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004166 port_speed = 0;
4167 break;
James Smart26d830e2015-04-07 15:07:17 -04004168 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004169 port_speed = 10;
4170 break;
James Smart26d830e2015-04-07 15:07:17 -04004171 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004172 port_speed = 100;
4173 break;
James Smart26d830e2015-04-07 15:07:17 -04004174 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004175 port_speed = 1000;
4176 break;
James Smart26d830e2015-04-07 15:07:17 -04004177 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004178 port_speed = 10000;
4179 break;
James Smart26d830e2015-04-07 15:07:17 -04004180 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4181 port_speed = 20000;
4182 break;
4183 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4184 port_speed = 25000;
4185 break;
4186 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4187 port_speed = 40000;
4188 break;
James Smart8b68cd52012-09-29 11:32:37 -04004189 default:
4190 port_speed = 0;
4191 }
4192 break;
4193 case LPFC_TRAILER_CODE_FC:
4194 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004195 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004196 port_speed = 0;
4197 break;
James Smart26d830e2015-04-07 15:07:17 -04004198 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004199 port_speed = 1000;
4200 break;
James Smart26d830e2015-04-07 15:07:17 -04004201 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004202 port_speed = 2000;
4203 break;
James Smart26d830e2015-04-07 15:07:17 -04004204 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004205 port_speed = 4000;
4206 break;
James Smart26d830e2015-04-07 15:07:17 -04004207 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004208 port_speed = 8000;
4209 break;
James Smart26d830e2015-04-07 15:07:17 -04004210 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004211 port_speed = 10000;
4212 break;
James Smart26d830e2015-04-07 15:07:17 -04004213 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004214 port_speed = 16000;
4215 break;
James Smartd38dd522015-08-31 16:48:17 -04004216 case LPFC_FC_LA_SPEED_32G:
4217 port_speed = 32000;
4218 break;
James Smart8b68cd52012-09-29 11:32:37 -04004219 default:
4220 port_speed = 0;
4221 }
4222 break;
4223 default:
4224 port_speed = 0;
4225 }
4226 return port_speed;
4227}
4228
4229/**
James Smart70f3c072010-12-15 17:57:33 -05004230 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004231 * @phba: pointer to lpfc hba data structure.
4232 * @acqe_link: pointer to the async link completion queue entry.
4233 *
James Smart70f3c072010-12-15 17:57:33 -05004234 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004235 **/
4236static void
4237lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4238 struct lpfc_acqe_link *acqe_link)
4239{
4240 struct lpfc_dmabuf *mp;
4241 LPFC_MBOXQ_t *pmb;
4242 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004243 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004244 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004245 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004246
4247 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004248 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004249 return;
James Smart32b97932009-07-19 10:01:21 -04004250 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004251 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4252 if (!pmb) {
4253 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4254 "0395 The mboxq allocation failed\n");
4255 return;
4256 }
4257 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4258 if (!mp) {
4259 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4260 "0396 The lpfc_dmabuf allocation failed\n");
4261 goto out_free_pmb;
4262 }
4263 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4264 if (!mp->virt) {
4265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4266 "0397 The mbuf allocation failed\n");
4267 goto out_free_dmabuf;
4268 }
4269
4270 /* Cleanup any outstanding ELS commands */
4271 lpfc_els_flush_all_cmd(phba);
4272
4273 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004274 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04004275
4276 /* Update link event statistics */
4277 phba->sli.slistat.link_event++;
4278
James Smart76a95d72010-11-20 23:11:48 -05004279 /* Create lpfc_handle_latt mailbox command from link ACQE */
4280 lpfc_read_topology(phba, pmb, mp);
4281 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04004282 pmb->vport = phba->pport;
4283
James Smartda0436e2009-05-22 14:51:39 -04004284 /* Keep the link status for extra SLI4 state machine reference */
4285 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004286 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4287 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004288 phba->sli4_hba.link_state.duplex =
4289 bf_get(lpfc_acqe_link_duplex, acqe_link);
4290 phba->sli4_hba.link_state.status =
4291 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05004292 phba->sli4_hba.link_state.type =
4293 bf_get(lpfc_acqe_link_type, acqe_link);
4294 phba->sli4_hba.link_state.number =
4295 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04004296 phba->sli4_hba.link_state.fault =
4297 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05004298 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004299 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4300
James Smart70f3c072010-12-15 17:57:33 -05004301 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04004302 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4303 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4304 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05004305 phba->sli4_hba.link_state.speed,
4306 phba->sli4_hba.link_state.topology,
4307 phba->sli4_hba.link_state.status,
4308 phba->sli4_hba.link_state.type,
4309 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004310 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004311 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05004312 /*
4313 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4314 * topology info. Note: Optional for non FC-AL ports.
4315 */
4316 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4317 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4318 if (rc == MBX_NOT_FINISHED)
4319 goto out_free_dmabuf;
4320 return;
4321 }
4322 /*
4323 * For FCoE Mode: fill in all the topology information we need and call
4324 * the READ_TOPOLOGY completion routine to continue without actually
4325 * sending the READ_TOPOLOGY mailbox command to the port.
4326 */
4327 /* Parse and translate status field */
4328 mb = &pmb->u.mb;
4329 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
4330
4331 /* Parse and translate link attention fields */
4332 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4333 la->eventTag = acqe_link->event_tag;
4334 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4335 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004336 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004337
4338 /* Fake the the following irrelvant fields */
4339 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4340 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4341 bf_set(lpfc_mbx_read_top_il, la, 0);
4342 bf_set(lpfc_mbx_read_top_pb, la, 0);
4343 bf_set(lpfc_mbx_read_top_fa, la, 0);
4344 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004345
4346 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004347 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004348
4349 return;
4350
4351out_free_dmabuf:
4352 kfree(mp);
4353out_free_pmb:
4354 mempool_free(pmb, phba->mbox_mem_pool);
4355}
4356
4357/**
James Smart70f3c072010-12-15 17:57:33 -05004358 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4359 * @phba: pointer to lpfc hba data structure.
4360 * @acqe_fc: pointer to the async fc completion queue entry.
4361 *
4362 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4363 * that the event was received and then issue a read_topology mailbox command so
4364 * that the rest of the driver will treat it the same as SLI3.
4365 **/
4366static void
4367lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4368{
4369 struct lpfc_dmabuf *mp;
4370 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07004371 MAILBOX_t *mb;
4372 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05004373 int rc;
4374
4375 if (bf_get(lpfc_trailer_type, acqe_fc) !=
4376 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4377 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4378 "2895 Non FC link Event detected.(%d)\n",
4379 bf_get(lpfc_trailer_type, acqe_fc));
4380 return;
4381 }
4382 /* Keep the link status for extra SLI4 state machine reference */
4383 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004384 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4385 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05004386 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4387 phba->sli4_hba.link_state.topology =
4388 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4389 phba->sli4_hba.link_state.status =
4390 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4391 phba->sli4_hba.link_state.type =
4392 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4393 phba->sli4_hba.link_state.number =
4394 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4395 phba->sli4_hba.link_state.fault =
4396 bf_get(lpfc_acqe_link_fault, acqe_fc);
4397 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004398 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05004399 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4400 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
4401 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4402 "%dMbps Fault:%d\n",
4403 phba->sli4_hba.link_state.speed,
4404 phba->sli4_hba.link_state.topology,
4405 phba->sli4_hba.link_state.status,
4406 phba->sli4_hba.link_state.type,
4407 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004408 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004409 phba->sli4_hba.link_state.fault);
4410 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4411 if (!pmb) {
4412 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4413 "2897 The mboxq allocation failed\n");
4414 return;
4415 }
4416 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4417 if (!mp) {
4418 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4419 "2898 The lpfc_dmabuf allocation failed\n");
4420 goto out_free_pmb;
4421 }
4422 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4423 if (!mp->virt) {
4424 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4425 "2899 The mbuf allocation failed\n");
4426 goto out_free_dmabuf;
4427 }
4428
4429 /* Cleanup any outstanding ELS commands */
4430 lpfc_els_flush_all_cmd(phba);
4431
4432 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004433 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05004434
4435 /* Update link event statistics */
4436 phba->sli.slistat.link_event++;
4437
4438 /* Create lpfc_handle_latt mailbox command from link ACQE */
4439 lpfc_read_topology(phba, pmb, mp);
4440 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4441 pmb->vport = phba->pport;
4442
James Smart7bdedb32016-07-06 12:36:00 -07004443 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
4444 /* Parse and translate status field */
4445 mb = &pmb->u.mb;
4446 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba,
4447 (void *)acqe_fc);
4448
4449 /* Parse and translate link attention fields */
4450 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4451 la->eventTag = acqe_fc->event_tag;
4452 bf_set(lpfc_mbx_read_top_att_type, la,
4453 LPFC_FC_LA_TYPE_LINK_DOWN);
4454
4455 /* Invoke the mailbox command callback function */
4456 lpfc_mbx_cmpl_read_topology(phba, pmb);
4457
4458 return;
4459 }
4460
James Smart70f3c072010-12-15 17:57:33 -05004461 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4462 if (rc == MBX_NOT_FINISHED)
4463 goto out_free_dmabuf;
4464 return;
4465
4466out_free_dmabuf:
4467 kfree(mp);
4468out_free_pmb:
4469 mempool_free(pmb, phba->mbox_mem_pool);
4470}
4471
4472/**
4473 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4474 * @phba: pointer to lpfc hba data structure.
4475 * @acqe_fc: pointer to the async SLI completion queue entry.
4476 *
4477 * This routine is to handle the SLI4 asynchronous SLI events.
4478 **/
4479static void
4480lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4481{
James Smart4b8bae02012-06-12 13:55:07 -04004482 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04004483 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04004484 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04004485 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05004486 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04004487 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04004488 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04004489 struct Scsi_Host *shost;
James Smart4b8bae02012-06-12 13:55:07 -04004490
James Smart946727d2015-04-07 15:07:09 -04004491 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4492
James Smart448193b2015-12-16 18:12:05 -05004493 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4494 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4495 "x%08x SLI Event Type:%d\n",
4496 acqe_sli->event_data1, acqe_sli->event_data2,
4497 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004498
4499 port_name = phba->Port[0];
4500 if (port_name == 0x00)
4501 port_name = '?'; /* get port name is empty */
4502
James Smart946727d2015-04-07 15:07:09 -04004503 switch (evt_type) {
4504 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4505 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4506 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4507 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4508
4509 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4510 "3190 Over Temperature:%d Celsius- Port Name %c\n",
4511 acqe_sli->event_data1, port_name);
4512
James Smart310429e2016-07-06 12:35:54 -07004513 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04004514 shost = lpfc_shost_from_vport(phba->pport);
4515 fc_host_post_vendor_event(shost, fc_get_event_number(),
4516 sizeof(temp_event_data),
4517 (char *)&temp_event_data,
4518 SCSI_NL_VID_TYPE_PCI
4519 | PCI_VENDOR_ID_EMULEX);
4520 break;
4521 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4522 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4523 temp_event_data.event_code = LPFC_NORMAL_TEMP;
4524 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4525
4526 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4527 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
4528 acqe_sli->event_data1, port_name);
4529
4530 shost = lpfc_shost_from_vport(phba->pport);
4531 fc_host_post_vendor_event(shost, fc_get_event_number(),
4532 sizeof(temp_event_data),
4533 (char *)&temp_event_data,
4534 SCSI_NL_VID_TYPE_PCI
4535 | PCI_VENDOR_ID_EMULEX);
4536 break;
4537 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4538 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04004539 &acqe_sli->event_data1;
4540
James Smart946727d2015-04-07 15:07:09 -04004541 /* fetch the status for this port */
4542 switch (phba->sli4_hba.lnk_info.lnk_no) {
4543 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05004544 status = bf_get(lpfc_sli_misconfigured_port0_state,
4545 &misconfigured->theEvent);
4546 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04004547 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004548 break;
4549 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05004550 status = bf_get(lpfc_sli_misconfigured_port1_state,
4551 &misconfigured->theEvent);
4552 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04004553 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004554 break;
4555 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05004556 status = bf_get(lpfc_sli_misconfigured_port2_state,
4557 &misconfigured->theEvent);
4558 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04004559 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004560 break;
4561 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05004562 status = bf_get(lpfc_sli_misconfigured_port3_state,
4563 &misconfigured->theEvent);
4564 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04004565 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004566 break;
4567 default:
James Smart448193b2015-12-16 18:12:05 -05004568 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4569 "3296 "
4570 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4571 "event: Invalid link %d",
4572 phba->sli4_hba.lnk_info.lnk_no);
4573 return;
James Smart946727d2015-04-07 15:07:09 -04004574 }
James Smart4b8bae02012-06-12 13:55:07 -04004575
James Smart448193b2015-12-16 18:12:05 -05004576 /* Skip if optic state unchanged */
4577 if (phba->sli4_hba.lnk_info.optic_state == status)
4578 return;
4579
James Smart946727d2015-04-07 15:07:09 -04004580 switch (status) {
4581 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05004582 sprintf(message, "Physical Link is functional");
4583 break;
James Smart946727d2015-04-07 15:07:09 -04004584 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4585 sprintf(message, "Optics faulted/incorrectly "
4586 "installed/not installed - Reseat optics, "
4587 "if issue not resolved, replace.");
4588 break;
4589 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4590 sprintf(message,
4591 "Optics of two types installed - Remove one "
4592 "optic or install matching pair of optics.");
4593 break;
4594 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4595 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04004596 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04004597 break;
James Smart448193b2015-12-16 18:12:05 -05004598 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4599 sprintf(message, "Unqualified optics - Replace with "
4600 "Avago optics for Warranty and Technical "
4601 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004602 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004603 break;
4604 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4605 sprintf(message, "Uncertified optics - Replace with "
4606 "Avago-certified optics to enable link "
4607 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004608 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004609 break;
James Smart946727d2015-04-07 15:07:09 -04004610 default:
4611 /* firmware is reporting a status we don't know about */
4612 sprintf(message, "Unknown event status x%02x", status);
4613 break;
4614 }
James Smart448193b2015-12-16 18:12:05 -05004615 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04004616 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05004617 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04004618 break;
4619 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4620 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4621 "3192 Remote DPort Test Initiated - "
4622 "Event Data1:x%08x Event Data2: x%08x\n",
4623 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04004624 break;
4625 default:
James Smart946727d2015-04-07 15:07:09 -04004626 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4627 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
4628 "x%08x SLI Event Type:%d\n",
4629 acqe_sli->event_data1, acqe_sli->event_data2,
4630 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004631 break;
4632 }
James Smart70f3c072010-12-15 17:57:33 -05004633}
4634
4635/**
James Smartfc2b9892010-02-26 14:15:29 -05004636 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4637 * @vport: pointer to vport data structure.
4638 *
4639 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4640 * response to a CVL event.
4641 *
4642 * Return the pointer to the ndlp with the vport if successful, otherwise
4643 * return NULL.
4644 **/
4645static struct lpfc_nodelist *
4646lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4647{
4648 struct lpfc_nodelist *ndlp;
4649 struct Scsi_Host *shost;
4650 struct lpfc_hba *phba;
4651
4652 if (!vport)
4653 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004654 phba = vport->phba;
4655 if (!phba)
4656 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004657 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4658 if (!ndlp) {
4659 /* Cannot find existing Fabric ndlp, so allocate a new one */
4660 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4661 if (!ndlp)
4662 return 0;
4663 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4664 /* Set the node type */
4665 ndlp->nlp_type |= NLP_FABRIC;
4666 /* Put ndlp onto node list */
4667 lpfc_enqueue_node(vport, ndlp);
4668 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4669 /* re-setup ndlp without removing from node list */
4670 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4671 if (!ndlp)
4672 return 0;
4673 }
James Smart63e801c2010-11-20 23:14:19 -05004674 if ((phba->pport->port_state < LPFC_FLOGI) &&
4675 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004676 return NULL;
4677 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004678 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4679 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004680 return NULL;
4681 shost = lpfc_shost_from_vport(vport);
4682 if (!shost)
4683 return NULL;
4684 lpfc_linkdown_port(vport);
4685 lpfc_cleanup_pending_mbox(vport);
4686 spin_lock_irq(shost->host_lock);
4687 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4688 spin_unlock_irq(shost->host_lock);
4689
4690 return ndlp;
4691}
4692
4693/**
4694 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4695 * @vport: pointer to lpfc hba data structure.
4696 *
4697 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4698 * response to a FCF dead event.
4699 **/
4700static void
4701lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4702{
4703 struct lpfc_vport **vports;
4704 int i;
4705
4706 vports = lpfc_create_vport_work_array(phba);
4707 if (vports)
4708 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4709 lpfc_sli4_perform_vport_cvl(vports[i]);
4710 lpfc_destroy_vport_work_array(phba, vports);
4711}
4712
4713/**
James Smart76a95d72010-11-20 23:11:48 -05004714 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004715 * @phba: pointer to lpfc hba data structure.
4716 * @acqe_link: pointer to the async fcoe completion queue entry.
4717 *
4718 * This routine is to handle the SLI4 asynchronous fcoe event.
4719 **/
4720static void
James Smart76a95d72010-11-20 23:11:48 -05004721lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004722 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004723{
James Smart70f3c072010-12-15 17:57:33 -05004724 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004725 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004726 struct lpfc_vport *vport;
4727 struct lpfc_nodelist *ndlp;
4728 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004729 int active_vlink_present;
4730 struct lpfc_vport **vports;
4731 int i;
James Smartda0436e2009-05-22 14:51:39 -04004732
James Smart70f3c072010-12-15 17:57:33 -05004733 phba->fc_eventTag = acqe_fip->event_tag;
4734 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004735 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004736 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4737 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4738 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004739 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4740 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004741 "2546 New FCF event, evt_tag:x%x, "
4742 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004743 acqe_fip->event_tag,
4744 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004745 else
4746 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4747 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004748 "2788 FCF param modified event, "
4749 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004750 acqe_fip->event_tag,
4751 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004752 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004753 /*
4754 * During period of FCF discovery, read the FCF
4755 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004756 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004757 */
4758 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4759 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004760 "2779 Read FCF (x%x) for updating "
4761 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004762 acqe_fip->index);
4763 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004764 }
James Smart38b92ef2010-08-04 16:11:39 -04004765
4766 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004767 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004768 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004769 spin_unlock_irq(&phba->hbalock);
4770 break;
4771 }
4772 /* If fast FCF failover rescan event is pending, do nothing */
4773 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4774 spin_unlock_irq(&phba->hbalock);
4775 break;
4776 }
4777
James Smartc2b97122013-05-31 17:05:36 -04004778 /* If the FCF has been in discovered state, do nothing. */
4779 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004780 spin_unlock_irq(&phba->hbalock);
4781 break;
4782 }
4783 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004784
James Smart0c9ab6f2010-02-26 14:15:57 -05004785 /* Otherwise, scan the entire FCF table and re-discover SAN */
4786 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004787 "2770 Start FCF table scan per async FCF "
4788 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004789 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004790 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4791 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004792 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004793 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4794 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004795 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004796 break;
4797
James Smart70f3c072010-12-15 17:57:33 -05004798 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004799 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004800 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004801 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4802 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004803 break;
4804
James Smart70f3c072010-12-15 17:57:33 -05004805 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004806 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004807 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004808 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004809 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004810 /*
4811 * If we are in the middle of FCF failover process, clear
4812 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004813 */
James Smartfc2b9892010-02-26 14:15:29 -05004814 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07004815 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
4816 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05004817 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004818 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004819 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004820 break;
4821 }
James Smart38b92ef2010-08-04 16:11:39 -04004822 spin_unlock_irq(&phba->hbalock);
4823
4824 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004825 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004826 break;
4827
4828 /*
4829 * Otherwise, request the port to rediscover the entire FCF
4830 * table for a fast recovery from case that the current FCF
4831 * is no longer valid as we are not in the middle of FCF
4832 * failover process already.
4833 */
James Smartc2b97122013-05-31 17:05:36 -04004834 spin_lock_irq(&phba->hbalock);
4835 /* Mark the fast failover process in progress */
4836 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4837 spin_unlock_irq(&phba->hbalock);
4838
4839 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4840 "2771 Start FCF fast failover process due to "
4841 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4842 "\n", acqe_fip->event_tag, acqe_fip->index);
4843 rc = lpfc_sli4_redisc_fcf_table(phba);
4844 if (rc) {
4845 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4846 LOG_DISCOVERY,
4847 "2772 Issue FCF rediscover mabilbox "
4848 "command failed, fail through to FCF "
4849 "dead event\n");
4850 spin_lock_irq(&phba->hbalock);
4851 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4852 spin_unlock_irq(&phba->hbalock);
4853 /*
4854 * Last resort will fail over by treating this
4855 * as a link down to FCF registration.
4856 */
4857 lpfc_sli4_fcf_dead_failthrough(phba);
4858 } else {
4859 /* Reset FCF roundrobin bmask for new discovery */
4860 lpfc_sli4_clear_fcf_rr_bmask(phba);
4861 /*
4862 * Handling fast FCF failover to a DEAD FCF event is
4863 * considered equalivant to receiving CVL to all vports.
4864 */
4865 lpfc_sli4_perform_all_vport_cvl(phba);
4866 }
James Smartda0436e2009-05-22 14:51:39 -04004867 break;
James Smart70f3c072010-12-15 17:57:33 -05004868 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004869 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004870 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004871 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004872 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004873
James Smart6669f9b2009-10-02 15:16:45 -04004874 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004875 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004876 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004877 if (!ndlp)
4878 break;
James Smart695a8142010-01-26 23:08:03 -05004879 active_vlink_present = 0;
4880
4881 vports = lpfc_create_vport_work_array(phba);
4882 if (vports) {
4883 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4884 i++) {
4885 if ((!(vports[i]->fc_flag &
4886 FC_VPORT_CVL_RCVD)) &&
4887 (vports[i]->port_state > LPFC_FDISC)) {
4888 active_vlink_present = 1;
4889 break;
4890 }
4891 }
4892 lpfc_destroy_vport_work_array(phba, vports);
4893 }
4894
James Smartcc823552015-05-21 13:55:26 -04004895 /*
4896 * Don't re-instantiate if vport is marked for deletion.
4897 * If we are here first then vport_delete is going to wait
4898 * for discovery to complete.
4899 */
4900 if (!(vport->load_flag & FC_UNLOADING) &&
4901 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05004902 /*
4903 * If there are other active VLinks present,
4904 * re-instantiate the Vlink using FDISC.
4905 */
James Smart256ec0d2013-04-17 20:14:58 -04004906 mod_timer(&ndlp->nlp_delayfunc,
4907 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004908 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004909 spin_lock_irq(shost->host_lock);
4910 ndlp->nlp_flag |= NLP_DELAY_TMO;
4911 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004912 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4913 vport->port_state = LPFC_FDISC;
4914 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004915 /*
4916 * Otherwise, we request port to rediscover
4917 * the entire FCF table for a fast recovery
4918 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004919 * is no longer valid if we are not already
4920 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004921 */
James Smartfc2b9892010-02-26 14:15:29 -05004922 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004923 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004924 spin_unlock_irq(&phba->hbalock);
4925 break;
4926 }
4927 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004928 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004929 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004930 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4931 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004932 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004933 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004934 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004935 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004936 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4937 LOG_DISCOVERY,
4938 "2774 Issue FCF rediscover "
4939 "mabilbox command failed, "
4940 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004941 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004942 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004943 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004944 /*
4945 * Last resort will be re-try on the
4946 * the current registered FCF entry.
4947 */
4948 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004949 } else
4950 /*
4951 * Reset FCF roundrobin bmask for new
4952 * discovery.
4953 */
James Smart7d791df2011-07-22 18:37:52 -04004954 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004955 }
4956 break;
James Smartda0436e2009-05-22 14:51:39 -04004957 default:
4958 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4959 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004960 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004961 break;
4962 }
4963}
4964
4965/**
4966 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4967 * @phba: pointer to lpfc hba data structure.
4968 * @acqe_link: pointer to the async dcbx completion queue entry.
4969 *
4970 * This routine is to handle the SLI4 asynchronous dcbx event.
4971 **/
4972static void
4973lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4974 struct lpfc_acqe_dcbx *acqe_dcbx)
4975{
James Smart4d9ab992009-10-02 15:16:39 -04004976 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004977 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4978 "0290 The SLI4 DCBX asynchronous event is not "
4979 "handled yet\n");
4980}
4981
4982/**
James Smartb19a0612010-04-06 14:48:51 -04004983 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4984 * @phba: pointer to lpfc hba data structure.
4985 * @acqe_link: pointer to the async grp5 completion queue entry.
4986 *
4987 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4988 * is an asynchronous notified of a logical link speed change. The Port
4989 * reports the logical link speed in units of 10Mbps.
4990 **/
4991static void
4992lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4993 struct lpfc_acqe_grp5 *acqe_grp5)
4994{
4995 uint16_t prev_ll_spd;
4996
4997 phba->fc_eventTag = acqe_grp5->event_tag;
4998 phba->fcoe_eventtag = acqe_grp5->event_tag;
4999 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5000 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005001 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04005002 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5003 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04005004 "from %dMbps to %dMbps\n", prev_ll_spd,
5005 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04005006}
5007
5008/**
James Smartda0436e2009-05-22 14:51:39 -04005009 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5010 * @phba: pointer to lpfc hba data structure.
5011 *
5012 * This routine is invoked by the worker thread to process all the pending
5013 * SLI4 asynchronous events.
5014 **/
5015void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5016{
5017 struct lpfc_cq_event *cq_event;
5018
5019 /* First, declare the async event has been handled */
5020 spin_lock_irq(&phba->hbalock);
5021 phba->hba_flag &= ~ASYNC_EVENT;
5022 spin_unlock_irq(&phba->hbalock);
5023 /* Now, handle all the async events */
5024 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5025 /* Get the first event from the head of the event queue */
5026 spin_lock_irq(&phba->hbalock);
5027 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5028 cq_event, struct lpfc_cq_event, list);
5029 spin_unlock_irq(&phba->hbalock);
5030 /* Process the asynchronous event */
5031 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5032 case LPFC_TRAILER_CODE_LINK:
5033 lpfc_sli4_async_link_evt(phba,
5034 &cq_event->cqe.acqe_link);
5035 break;
5036 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05005037 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005038 break;
5039 case LPFC_TRAILER_CODE_DCBX:
5040 lpfc_sli4_async_dcbx_evt(phba,
5041 &cq_event->cqe.acqe_dcbx);
5042 break;
James Smartb19a0612010-04-06 14:48:51 -04005043 case LPFC_TRAILER_CODE_GRP5:
5044 lpfc_sli4_async_grp5_evt(phba,
5045 &cq_event->cqe.acqe_grp5);
5046 break;
James Smart70f3c072010-12-15 17:57:33 -05005047 case LPFC_TRAILER_CODE_FC:
5048 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5049 break;
5050 case LPFC_TRAILER_CODE_SLI:
5051 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5052 break;
James Smartda0436e2009-05-22 14:51:39 -04005053 default:
5054 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5055 "1804 Invalid asynchrous event code: "
5056 "x%x\n", bf_get(lpfc_trailer_code,
5057 &cq_event->cqe.mcqe_cmpl));
5058 break;
5059 }
5060 /* Free the completion event processed to the free pool */
5061 lpfc_sli4_cq_event_release(phba, cq_event);
5062 }
5063}
5064
5065/**
James Smartecfd03c2010-02-12 14:41:27 -05005066 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5067 * @phba: pointer to lpfc hba data structure.
5068 *
5069 * This routine is invoked by the worker thread to process FCF table
5070 * rediscovery pending completion event.
5071 **/
5072void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5073{
5074 int rc;
5075
5076 spin_lock_irq(&phba->hbalock);
5077 /* Clear FCF rediscovery timeout event */
5078 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5079 /* Clear driver fast failover FCF record flag */
5080 phba->fcf.failover_rec.flag = 0;
5081 /* Set state for FCF fast failover */
5082 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5083 spin_unlock_irq(&phba->hbalock);
5084
5085 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05005086 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005087 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05005088 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05005089 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005090 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5091 "2747 Issue FCF scan read FCF mailbox "
5092 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05005093}
5094
5095/**
James Smartda0436e2009-05-22 14:51:39 -04005096 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5097 * @phba: pointer to lpfc hba data structure.
5098 * @dev_grp: The HBA PCI-Device group number.
5099 *
5100 * This routine is invoked to set up the per HBA PCI-Device group function
5101 * API jump table entries.
5102 *
5103 * Return: 0 if success, otherwise -ENODEV
5104 **/
5105int
5106lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05005107{
5108 int rc;
5109
James Smartda0436e2009-05-22 14:51:39 -04005110 /* Set up lpfc PCI-device group */
5111 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05005112
James Smartda0436e2009-05-22 14:51:39 -04005113 /* The LPFC_PCI_DEV_OC uses SLI4 */
5114 if (dev_grp == LPFC_PCI_DEV_OC)
5115 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05005116
James Smartda0436e2009-05-22 14:51:39 -04005117 /* Set up device INIT API function jump table */
5118 rc = lpfc_init_api_table_setup(phba, dev_grp);
5119 if (rc)
5120 return -ENODEV;
5121 /* Set up SCSI API function jump table */
5122 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5123 if (rc)
5124 return -ENODEV;
5125 /* Set up SLI API function jump table */
5126 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5127 if (rc)
5128 return -ENODEV;
5129 /* Set up MBOX API function jump table */
5130 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5131 if (rc)
5132 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005133
James Smartda0436e2009-05-22 14:51:39 -04005134 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005135}
5136
5137/**
James Smart3621a712009-04-06 18:47:14 -04005138 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05005139 * @phba: pointer to lpfc hba data structure.
5140 * @intr_mode: active interrupt mode adopted.
5141 *
5142 * This routine it invoked to log the currently used active interrupt mode
5143 * to the device.
James Smart3772a992009-05-22 14:50:54 -04005144 **/
5145static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05005146{
5147 switch (intr_mode) {
5148 case 0:
5149 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5150 "0470 Enable INTx interrupt mode.\n");
5151 break;
5152 case 1:
5153 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5154 "0481 Enabled MSI interrupt mode.\n");
5155 break;
5156 case 2:
5157 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5158 "0480 Enabled MSI-X interrupt mode.\n");
5159 break;
5160 default:
5161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5162 "0482 Illegal interrupt mode.\n");
5163 break;
5164 }
5165 return;
5166}
5167
James Smart5b75da22008-12-04 22:39:35 -05005168/**
James Smart3772a992009-05-22 14:50:54 -04005169 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005170 * @phba: pointer to lpfc hba data structure.
5171 *
James Smart3772a992009-05-22 14:50:54 -04005172 * This routine is invoked to enable the PCI device that is common to all
5173 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005174 *
5175 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005176 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005177 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05005178 **/
James Smart3772a992009-05-22 14:50:54 -04005179static int
5180lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005181{
James Smart3772a992009-05-22 14:50:54 -04005182 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005183
James Smart3772a992009-05-22 14:50:54 -04005184 /* Obtain PCI device reference */
5185 if (!phba->pcidev)
5186 goto out_error;
5187 else
5188 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005189 /* Enable PCI device */
5190 if (pci_enable_device_mem(pdev))
5191 goto out_error;
5192 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005193 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04005194 goto out_disable_device;
5195 /* Set up device as PCI master and save state for EEH */
5196 pci_set_master(pdev);
5197 pci_try_set_mwi(pdev);
5198 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005199
James Smart05580562011-05-24 11:40:48 -04005200 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07005201 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04005202 pdev->needs_freset = 1;
5203
James Smart3772a992009-05-22 14:50:54 -04005204 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005205
James Smart3772a992009-05-22 14:50:54 -04005206out_disable_device:
5207 pci_disable_device(pdev);
5208out_error:
James Smart079b5c92011-08-21 21:48:49 -04005209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005210 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04005211 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005212}
5213
5214/**
James Smart3772a992009-05-22 14:50:54 -04005215 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005216 * @phba: pointer to lpfc hba data structure.
5217 *
James Smart3772a992009-05-22 14:50:54 -04005218 * This routine is invoked to disable the PCI device that is common to all
5219 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005220 **/
5221static void
James Smart3772a992009-05-22 14:50:54 -04005222lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005223{
James Smart3772a992009-05-22 14:50:54 -04005224 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005225
James Smart3772a992009-05-22 14:50:54 -04005226 /* Obtain PCI device reference */
5227 if (!phba->pcidev)
5228 return;
5229 else
5230 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005231 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005232 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04005233 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005234
5235 return;
5236}
5237
5238/**
James Smart3772a992009-05-22 14:50:54 -04005239 * lpfc_reset_hba - Reset a hba
5240 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04005241 *
James Smart3772a992009-05-22 14:50:54 -04005242 * This routine is invoked to reset a hba device. It brings the HBA
5243 * offline, performs a board restart, and then brings the board back
5244 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
5245 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04005246 **/
James Smart3772a992009-05-22 14:50:54 -04005247void
5248lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05005249{
James Smart3772a992009-05-22 14:50:54 -04005250 /* If resets are disabled then set error state and return. */
5251 if (!phba->cfg_enable_hba_reset) {
5252 phba->link_state = LPFC_HBA_ERROR;
5253 return;
5254 }
James Smartee620212014-04-04 13:51:53 -04005255 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
5256 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5257 else
5258 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04005259 lpfc_offline(phba);
5260 lpfc_sli_brdrestart(phba);
5261 lpfc_online(phba);
5262 lpfc_unblock_mgmt_io(phba);
5263}
dea31012005-04-17 16:05:31 -05005264
James Smart3772a992009-05-22 14:50:54 -04005265/**
James Smart0a96e972011-07-22 18:37:28 -04005266 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
5267 * @phba: pointer to lpfc hba data structure.
5268 *
5269 * This function enables the PCI SR-IOV virtual functions to a physical
5270 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5271 * enable the number of virtual functions to the physical function. As
5272 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5273 * API call does not considered as an error condition for most of the device.
5274 **/
5275uint16_t
5276lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
5277{
5278 struct pci_dev *pdev = phba->pcidev;
5279 uint16_t nr_virtfn;
5280 int pos;
5281
James Smart0a96e972011-07-22 18:37:28 -04005282 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5283 if (pos == 0)
5284 return 0;
5285
5286 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
5287 return nr_virtfn;
5288}
5289
5290/**
James Smart912e3ac2011-05-24 11:42:11 -04005291 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
5292 * @phba: pointer to lpfc hba data structure.
5293 * @nr_vfn: number of virtual functions to be enabled.
5294 *
5295 * This function enables the PCI SR-IOV virtual functions to a physical
5296 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5297 * enable the number of virtual functions to the physical function. As
5298 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5299 * API call does not considered as an error condition for most of the device.
5300 **/
5301int
5302lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
5303{
5304 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04005305 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04005306 int rc;
5307
James Smart0a96e972011-07-22 18:37:28 -04005308 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
5309 if (nr_vfn > max_nr_vfn) {
5310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5311 "3057 Requested vfs (%d) greater than "
5312 "supported vfs (%d)", nr_vfn, max_nr_vfn);
5313 return -EINVAL;
5314 }
5315
James Smart912e3ac2011-05-24 11:42:11 -04005316 rc = pci_enable_sriov(pdev, nr_vfn);
5317 if (rc) {
5318 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5319 "2806 Failed to enable sriov on this device "
5320 "with vfn number nr_vf:%d, rc:%d\n",
5321 nr_vfn, rc);
5322 } else
5323 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5324 "2807 Successful enable sriov on this device "
5325 "with vfn number nr_vf:%d\n", nr_vfn);
5326 return rc;
5327}
5328
5329/**
James Smart895427b2017-02-12 13:52:30 -08005330 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04005331 * @phba: pointer to lpfc hba data structure.
5332 *
James Smart895427b2017-02-12 13:52:30 -08005333 * This routine is invoked to set up the driver internal resources before the
5334 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04005335 *
5336 * Return codes
James Smart895427b2017-02-12 13:52:30 -08005337 * 0 - successful
5338 * other values - error
James Smart3772a992009-05-22 14:50:54 -04005339 **/
5340static int
James Smart895427b2017-02-12 13:52:30 -08005341lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04005342{
James Smart895427b2017-02-12 13:52:30 -08005343 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05005344
James Smart3772a992009-05-22 14:50:54 -04005345 /*
James Smart895427b2017-02-12 13:52:30 -08005346 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04005347 */
James Smart895427b2017-02-12 13:52:30 -08005348 atomic_set(&phba->fast_event_count, 0);
5349 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005350
James Smart895427b2017-02-12 13:52:30 -08005351 /* Initialize ndlp management spinlock */
5352 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04005353
James Smart895427b2017-02-12 13:52:30 -08005354 INIT_LIST_HEAD(&phba->port_list);
5355 INIT_LIST_HEAD(&phba->work_list);
5356 init_waitqueue_head(&phba->wait_4_mlo_m_q);
5357
5358 /* Initialize the wait queue head for the kernel thread */
5359 init_waitqueue_head(&phba->work_waitq);
5360
5361 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08005362 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08005363 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
5364 "SCSI" : " "),
5365 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08005366 "NVME" : " "),
5367 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08005368
5369 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5370 /* Initialize the scsi buffer list used by driver for scsi IO */
5371 spin_lock_init(&phba->scsi_buf_list_get_lock);
5372 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5373 spin_lock_init(&phba->scsi_buf_list_put_lock);
5374 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
5375 }
5376
5377 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
5378 (phba->nvmet_support == 0)) {
5379 /* Initialize the NVME buffer list used by driver for NVME IO */
5380 spin_lock_init(&phba->nvme_buf_list_get_lock);
5381 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_get);
5382 spin_lock_init(&phba->nvme_buf_list_put_lock);
5383 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
5384 }
5385
5386 /* Initialize the fabric iocb list */
5387 INIT_LIST_HEAD(&phba->fabric_iocb_list);
5388
5389 /* Initialize list to save ELS buffers */
5390 INIT_LIST_HEAD(&phba->elsbuf);
5391
5392 /* Initialize FCF connection rec list */
5393 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5394
5395 /* Initialize OAS configuration list */
5396 spin_lock_init(&phba->devicelock);
5397 INIT_LIST_HEAD(&phba->luns);
5398
James Smart3772a992009-05-22 14:50:54 -04005399 /* MBOX heartbeat timer */
5400 init_timer(&psli->mbox_tmo);
5401 psli->mbox_tmo.function = lpfc_mbox_timeout;
5402 psli->mbox_tmo.data = (unsigned long) phba;
James Smart3772a992009-05-22 14:50:54 -04005403 /* Fabric block timer */
5404 init_timer(&phba->fabric_block_timer);
5405 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
5406 phba->fabric_block_timer.data = (unsigned long) phba;
5407 /* EA polling mode timer */
5408 init_timer(&phba->eratt_poll);
5409 phba->eratt_poll.function = lpfc_poll_eratt;
5410 phba->eratt_poll.data = (unsigned long) phba;
James Smart895427b2017-02-12 13:52:30 -08005411 /* Heartbeat timer */
5412 init_timer(&phba->hb_tmofunc);
5413 phba->hb_tmofunc.function = lpfc_hb_timeout;
5414 phba->hb_tmofunc.data = (unsigned long)phba;
5415
5416 return 0;
5417}
5418
5419/**
5420 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
5421 * @phba: pointer to lpfc hba data structure.
5422 *
5423 * This routine is invoked to set up the driver internal resources specific to
5424 * support the SLI-3 HBA device it attached to.
5425 *
5426 * Return codes
5427 * 0 - successful
5428 * other values - error
5429 **/
5430static int
5431lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5432{
5433 int rc;
5434
5435 /*
5436 * Initialize timers used by driver
5437 */
5438
5439 /* FCP polling mode timer */
5440 init_timer(&phba->fcp_poll_timer);
5441 phba->fcp_poll_timer.function = lpfc_poll_timeout;
5442 phba->fcp_poll_timer.data = (unsigned long) phba;
James Smart3772a992009-05-22 14:50:54 -04005443
5444 /* Host attention work mask setup */
5445 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5446 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5447
5448 /* Get all the module params for configuring this host */
5449 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08005450 /* Set up phase-1 common device driver resources */
5451
5452 rc = lpfc_setup_driver_resource_phase1(phba);
5453 if (rc)
5454 return -ENODEV;
5455
James Smart49198b32010-04-06 15:04:33 -04005456 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5457 phba->menlo_flag |= HBA_MENLO_SUPPORT;
5458 /* check for menlo minimum sg count */
5459 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5460 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5461 }
5462
James Smart895427b2017-02-12 13:52:30 -08005463 if (!phba->sli.sli3_ring)
5464 phba->sli.sli3_ring = kzalloc(LPFC_SLI3_MAX_RING *
James Smart2a76a282012-08-03 12:35:54 -04005465 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08005466 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04005467 return -ENOMEM;
5468
James Smart3772a992009-05-22 14:50:54 -04005469 /*
James Smart96f70772013-04-17 20:16:15 -04005470 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04005471 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04005472 */
James Smart3772a992009-05-22 14:50:54 -04005473
James Smart96f70772013-04-17 20:16:15 -04005474 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04005475 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
Bodo Stroesser5f406fa2015-08-31 16:48:10 -04005476 lpfc_template_s3.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04005477
James Smart96f70772013-04-17 20:16:15 -04005478 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5479 if (phba->cfg_enable_bg) {
5480 /*
5481 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5482 * the FCP rsp, and a BDE for each. Sice we have no control
5483 * over how many protection data segments the SCSI Layer
5484 * will hand us (ie: there could be one for every block
5485 * in the IO), we just allocate enough BDEs to accomidate
5486 * our max amount and we need to limit lpfc_sg_seg_cnt to
5487 * minimize the risk of running out.
5488 */
5489 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5490 sizeof(struct fcp_rsp) +
5491 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5492
5493 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5494 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5495
5496 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5497 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5498 } else {
5499 /*
5500 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5501 * the FCP rsp, a BDE for each, and a BDE for up to
5502 * cfg_sg_seg_cnt data segments.
5503 */
5504 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5505 sizeof(struct fcp_rsp) +
5506 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5507
5508 /* Total BDEs in BPL for scsi_sg_list */
5509 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5510 }
5511
5512 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5513 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5514 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5515 phba->cfg_total_seg_cnt);
5516
James Smart3772a992009-05-22 14:50:54 -04005517 phba->max_vpi = LPFC_MAX_VPI;
5518 /* This will be set to correct value after config_port mbox */
5519 phba->max_vports = 0;
5520
5521 /*
5522 * Initialize the SLI Layer to run with lpfc HBAs.
5523 */
5524 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08005525 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04005526
5527 /* Allocate device driver memory */
5528 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5529 return -ENOMEM;
5530
James Smart912e3ac2011-05-24 11:42:11 -04005531 /*
5532 * Enable sr-iov virtual functions if supported and configured
5533 * through the module parameter.
5534 */
5535 if (phba->cfg_sriov_nr_virtfn > 0) {
5536 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5537 phba->cfg_sriov_nr_virtfn);
5538 if (rc) {
5539 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5540 "2808 Requested number of SR-IOV "
5541 "virtual functions (%d) is not "
5542 "supported\n",
5543 phba->cfg_sriov_nr_virtfn);
5544 phba->cfg_sriov_nr_virtfn = 0;
5545 }
5546 }
5547
James Smart3772a992009-05-22 14:50:54 -04005548 return 0;
5549}
5550
5551/**
5552 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5553 * @phba: pointer to lpfc hba data structure.
5554 *
5555 * This routine is invoked to unset the driver internal resources set up
5556 * specific for supporting the SLI-3 HBA device it attached to.
5557 **/
5558static void
5559lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5560{
5561 /* Free device driver memory allocated */
5562 lpfc_mem_free_all(phba);
5563
5564 return;
5565}
5566
5567/**
James Smartda0436e2009-05-22 14:51:39 -04005568 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5569 * @phba: pointer to lpfc hba data structure.
5570 *
5571 * This routine is invoked to set up the driver internal resources specific to
5572 * support the SLI-4 HBA device it attached to.
5573 *
5574 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005575 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005576 * other values - error
5577 **/
5578static int
5579lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5580{
James Smart28baac72010-02-12 14:42:03 -05005581 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08005582 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08005583 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05005584 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5585 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04005586 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05005587 int fof_vectors = 0;
James Smartf358dd02017-02-12 13:52:34 -08005588 uint64_t wwn;
James Smartda0436e2009-05-22 14:51:39 -04005589
James Smart895427b2017-02-12 13:52:30 -08005590 phba->sli4_hba.num_online_cpu = num_online_cpus();
5591 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
5592 phba->sli4_hba.curr_disp_cpu = 0;
5593
James Smart716d3bc2013-09-06 12:18:28 -04005594 /* Get all the module params for configuring this host */
5595 lpfc_get_cfgparam(phba);
5596
James Smart895427b2017-02-12 13:52:30 -08005597 /* Set up phase-1 common device driver resources */
5598 rc = lpfc_setup_driver_resource_phase1(phba);
5599 if (rc)
5600 return -ENODEV;
5601
James Smartda0436e2009-05-22 14:51:39 -04005602 /* Before proceed, wait for POST done and device ready */
5603 rc = lpfc_sli4_post_status_check(phba);
5604 if (rc)
5605 return -ENODEV;
5606
5607 /*
5608 * Initialize timers used by driver
5609 */
5610
James Smart19ca7602010-11-20 23:11:55 -05005611 init_timer(&phba->rrq_tmr);
5612 phba->rrq_tmr.function = lpfc_rrq_timeout;
5613 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04005614
James Smartecfd03c2010-02-12 14:41:27 -05005615 /* FCF rediscover timer */
5616 init_timer(&phba->fcf.redisc_wait);
5617 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
5618 phba->fcf.redisc_wait.data = (unsigned long)phba;
5619
James Smartda0436e2009-05-22 14:51:39 -04005620 /*
James Smart7ad20aa2011-05-24 11:44:28 -04005621 * Control structure for handling external multi-buffer mailbox
5622 * command pass-through.
5623 */
5624 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5625 sizeof(struct lpfc_mbox_ext_buf_ctx));
5626 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5627
James Smartda0436e2009-05-22 14:51:39 -04005628 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04005629
James Smartda0436e2009-05-22 14:51:39 -04005630 /* This will be set to correct value after the read_config mbox */
5631 phba->max_vports = 0;
5632
5633 /* Program the default value of vlan_id and fc_map */
5634 phba->valid_vlan = 0;
5635 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5636 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5637 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5638
5639 /*
James Smart2a76a282012-08-03 12:35:54 -04005640 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08005641 * we will associate a new ring, for each EQ/CQ/WQ tuple.
5642 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04005643 */
James Smart085c6472010-11-20 23:11:37 -05005644
James Smart09294d42013-04-17 20:16:05 -04005645 /*
5646 * It doesn't matter what family our adapter is in, we are
5647 * limited to 2 Pages, 512 SGEs, for our SGL.
5648 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5649 */
5650 max_buf_size = (2 * SLI4_PAGE_SIZE);
5651 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5652 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04005653
James Smartda0436e2009-05-22 14:51:39 -04005654 /*
James Smart895427b2017-02-12 13:52:30 -08005655 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
5656 * used to create the sg_dma_buf_pool must be calculated.
James Smart28baac72010-02-12 14:42:03 -05005657 */
James Smart96f70772013-04-17 20:16:15 -04005658 if (phba->cfg_enable_bg) {
5659 /*
James Smart895427b2017-02-12 13:52:30 -08005660 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
5661 * the FCP rsp, and a SGE. Sice we have no control
5662 * over how many protection segments the SCSI Layer
James Smart96f70772013-04-17 20:16:15 -04005663 * will hand us (ie: there could be one for every block
James Smart895427b2017-02-12 13:52:30 -08005664 * in the IO), just allocate enough SGEs to accomidate
5665 * our max amount and we need to limit lpfc_sg_seg_cnt
5666 * to minimize the risk of running out.
James Smart96f70772013-04-17 20:16:15 -04005667 */
5668 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005669 sizeof(struct fcp_rsp) + max_buf_size;
James Smart96f70772013-04-17 20:16:15 -04005670
5671 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5672 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5673
5674 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
James Smart895427b2017-02-12 13:52:30 -08005675 phba->cfg_sg_seg_cnt =
5676 LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
James Smart96f70772013-04-17 20:16:15 -04005677 } else {
5678 /*
James Smart895427b2017-02-12 13:52:30 -08005679 * The scsi_buf for a regular I/O holds the FCP cmnd,
James Smart96f70772013-04-17 20:16:15 -04005680 * the FCP rsp, a SGE for each, and a SGE for up to
5681 * cfg_sg_seg_cnt data segments.
5682 */
5683 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005684 sizeof(struct fcp_rsp) +
5685 ((phba->cfg_sg_seg_cnt + 2) *
5686 sizeof(struct sli4_sge));
James Smart96f70772013-04-17 20:16:15 -04005687
5688 /* Total SGEs for scsi_sg_list */
5689 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
James Smart895427b2017-02-12 13:52:30 -08005690
James Smart96f70772013-04-17 20:16:15 -04005691 /*
James Smart895427b2017-02-12 13:52:30 -08005692 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only
5693 * need to post 1 page for the SGL.
James Smart96f70772013-04-17 20:16:15 -04005694 */
James Smart085c6472010-11-20 23:11:37 -05005695 }
James Smartacd68592012-01-18 16:25:09 -05005696
James Smart96f70772013-04-17 20:16:15 -04005697 /* Initialize the host templates with the updated values. */
5698 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5699 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5700
5701 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5702 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5703 else
5704 phba->cfg_sg_dma_buf_size =
5705 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5706
5707 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5708 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5709 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5710 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005711
5712 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08005713 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04005714 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5715 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5716
5717 /*
5718 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5719 */
James Smart895427b2017-02-12 13:52:30 -08005720 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5721 /* Initialize the Abort scsi buffer list used by driver */
5722 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5723 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5724 }
5725
5726 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
5727 /* Initialize the Abort nvme buffer list used by driver */
5728 spin_lock_init(&phba->sli4_hba.abts_nvme_buf_list_lock);
5729 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
5730 }
5731
James Smartda0436e2009-05-22 14:51:39 -04005732 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08005733 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smartf358dd02017-02-12 13:52:34 -08005734 spin_lock_init(&phba->sli4_hba.nvmet_io_lock);
James Smartda0436e2009-05-22 14:51:39 -04005735
5736 /*
James Smart6d368e52011-05-24 11:44:12 -04005737 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005738 */
5739
5740 /* Driver internel slow-path CQ Event pool */
5741 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5742 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005743 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005744 /* Asynchronous event CQ Event work queue list */
5745 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5746 /* Fast-path XRI aborted CQ Event work queue list */
5747 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5748 /* Slow-path XRI aborted CQ Event work queue list */
5749 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5750 /* Receive queue CQ Event work queue list */
5751 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5752
James Smart6d368e52011-05-24 11:44:12 -04005753 /* Initialize extent block lists. */
5754 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5755 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5756 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5757 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5758
James Smart448193b2015-12-16 18:12:05 -05005759 /* initialize optic_state to 0xFF */
5760 phba->sli4_hba.lnk_info.optic_state = 0xff;
5761
James Smartda0436e2009-05-22 14:51:39 -04005762 /* Allocate device driver memory */
5763 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5764 if (rc)
5765 return -ENOMEM;
5766
James Smart2fcee4b2010-12-15 17:57:46 -05005767 /* IF Type 2 ports get initialized now. */
5768 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5769 LPFC_SLI_INTF_IF_TYPE_2) {
5770 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08005771 if (unlikely(rc)) {
5772 rc = -ENODEV;
5773 goto out_free_mem;
5774 }
James Smart946727d2015-04-07 15:07:09 -04005775 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05005776 }
5777
James Smartda0436e2009-05-22 14:51:39 -04005778 /* Create the bootstrap mailbox command */
5779 rc = lpfc_create_bootstrap_mbox(phba);
5780 if (unlikely(rc))
5781 goto out_free_mem;
5782
5783 /* Set up the host's endian order with the device. */
5784 rc = lpfc_setup_endian_order(phba);
5785 if (unlikely(rc))
5786 goto out_free_bsmbx;
5787
5788 /* Set up the hba's configuration parameters. */
5789 rc = lpfc_sli4_read_config(phba);
5790 if (unlikely(rc))
5791 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005792 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5793 if (unlikely(rc))
5794 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005795
James Smart2fcee4b2010-12-15 17:57:46 -05005796 /* IF Type 0 ports get initialized now. */
5797 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5798 LPFC_SLI_INTF_IF_TYPE_0) {
5799 rc = lpfc_pci_function_reset(phba);
5800 if (unlikely(rc))
5801 goto out_free_bsmbx;
5802 }
James Smartda0436e2009-05-22 14:51:39 -04005803
James Smartcb5172e2010-03-15 11:25:07 -04005804 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5805 GFP_KERNEL);
5806 if (!mboxq) {
5807 rc = -ENOMEM;
5808 goto out_free_bsmbx;
5809 }
5810
James Smartf358dd02017-02-12 13:52:34 -08005811 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08005812 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08005813 if (lpfc_enable_nvmet_cnt) {
5814
5815 /* First get WWN of HBA instance */
5816 lpfc_read_nv(phba, mboxq);
5817 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5818 if (rc != MBX_SUCCESS) {
5819 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5820 "6016 Mailbox failed , mbxCmd x%x "
5821 "READ_NV, mbxStatus x%x\n",
5822 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5823 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
5824 rc = -EIO;
5825 goto out_free_bsmbx;
5826 }
5827 mb = &mboxq->u.mb;
5828 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
5829 sizeof(uint64_t));
5830 wwn = cpu_to_be64(wwn);
5831 phba->sli4_hba.wwnn.u.name = wwn;
5832 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
5833 sizeof(uint64_t));
5834 /* wwn is WWPN of HBA instance */
5835 wwn = cpu_to_be64(wwn);
5836 phba->sli4_hba.wwpn.u.name = wwn;
5837
5838 /* Check to see if it matches any module parameter */
5839 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
5840 if (wwn == lpfc_enable_nvmet[i]) {
5841 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5842 "6017 NVME Target %016llx\n",
5843 wwn);
5844 phba->nvmet_support = 1; /* a match */
5845 }
5846 }
5847 }
James Smart895427b2017-02-12 13:52:30 -08005848
5849 lpfc_nvme_mod_param_dep(phba);
5850
James Smartfedd3b72011-02-16 12:39:24 -05005851 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005852 lpfc_supported_pages(mboxq);
5853 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005854 if (!rc) {
5855 mqe = &mboxq->u.mqe;
5856 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5857 LPFC_MAX_SUPPORTED_PAGES);
5858 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5859 switch (pn_page[i]) {
5860 case LPFC_SLI4_PARAMETERS:
5861 phba->sli4_hba.pc_sli4_params.supported = 1;
5862 break;
5863 default:
5864 break;
5865 }
5866 }
5867 /* Read the port's SLI4 Parameters capabilities if supported. */
5868 if (phba->sli4_hba.pc_sli4_params.supported)
5869 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5870 if (rc) {
5871 mempool_free(mboxq, phba->mbox_mem_pool);
5872 rc = -EIO;
5873 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005874 }
5875 }
James Smart65791f12016-07-06 12:35:56 -07005876
James Smartfedd3b72011-02-16 12:39:24 -05005877 /*
5878 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005879 * If this call fails, it isn't critical unless the SLI4 parameters come
5880 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005881 */
James Smart6d368e52011-05-24 11:44:12 -04005882 rc = lpfc_get_sli4_parameters(phba, mboxq);
5883 if (rc) {
5884 if (phba->sli4_hba.extents_in_use &&
5885 phba->sli4_hba.rpi_hdrs_in_use) {
5886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5887 "2999 Unsupported SLI4 Parameters "
5888 "Extents and RPI headers enabled.\n");
James Smart6d368e52011-05-24 11:44:12 -04005889 }
James Smart895427b2017-02-12 13:52:30 -08005890 mempool_free(mboxq, phba->mbox_mem_pool);
5891 goto out_free_bsmbx;
James Smart6d368e52011-05-24 11:44:12 -04005892 }
James Smart895427b2017-02-12 13:52:30 -08005893
James Smartcb5172e2010-03-15 11:25:07 -04005894 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005895
5896 /* Verify OAS is supported */
5897 lpfc_sli4_oas_verify(phba);
5898 if (phba->cfg_fof)
5899 fof_vectors = 1;
5900
James Smart5350d872011-10-10 21:33:49 -04005901 /* Verify all the SLI4 queues */
5902 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005903 if (rc)
5904 goto out_free_bsmbx;
5905
5906 /* Create driver internal CQE event pool */
5907 rc = lpfc_sli4_cq_event_pool_create(phba);
5908 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005909 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005910
James Smart8a9d2e82012-05-09 21:16:12 -04005911 /* Initialize sgl lists per host */
5912 lpfc_init_sgl_list(phba);
5913
5914 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005915 rc = lpfc_init_active_sgl_array(phba);
5916 if (rc) {
5917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5918 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005919 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005920 }
James Smartda0436e2009-05-22 14:51:39 -04005921 rc = lpfc_sli4_init_rpi_hdrs(phba);
5922 if (rc) {
5923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5924 "1432 Failed to initialize rpi headers.\n");
5925 goto out_free_active_sgl;
5926 }
5927
James Smarta93ff372010-10-22 11:06:08 -04005928 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005929 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5930 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5931 GFP_KERNEL);
5932 if (!phba->fcf.fcf_rr_bmask) {
5933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5934 "2759 Failed allocate memory for FCF round "
5935 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005936 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005937 goto out_remove_rpi_hdrs;
5938 }
5939
James Smart895427b2017-02-12 13:52:30 -08005940 phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
5941 sizeof(struct lpfc_hba_eq_hdl),
5942 GFP_KERNEL);
5943 if (!phba->sli4_hba.hba_eq_hdl) {
James Smart67d12732012-08-03 12:36:13 -04005944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5945 "2572 Failed allocate memory for "
5946 "fast-path per-EQ handle array\n");
5947 rc = -ENOMEM;
5948 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005949 }
5950
James Smart895427b2017-02-12 13:52:30 -08005951 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
5952 sizeof(struct lpfc_vector_map_info),
5953 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04005954 if (!phba->sli4_hba.cpu_map) {
5955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5956 "3327 Failed allocate memory for msi-x "
5957 "interrupt vector mapping\n");
5958 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08005959 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04005960 }
James Smartb246de12013-05-31 17:03:07 -04005961 if (lpfc_used_cpu == NULL) {
James Smart895427b2017-02-12 13:52:30 -08005962 lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
5963 GFP_KERNEL);
James Smartb246de12013-05-31 17:03:07 -04005964 if (!lpfc_used_cpu) {
5965 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5966 "3335 Failed allocate memory for msi-x "
5967 "interrupt vector mapping\n");
5968 kfree(phba->sli4_hba.cpu_map);
5969 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08005970 goto out_free_hba_eq_hdl;
James Smartb246de12013-05-31 17:03:07 -04005971 }
5972 for (i = 0; i < lpfc_present_cpu; i++)
5973 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5974 }
5975
James Smart912e3ac2011-05-24 11:42:11 -04005976 /*
5977 * Enable sr-iov virtual functions if supported and configured
5978 * through the module parameter.
5979 */
5980 if (phba->cfg_sriov_nr_virtfn > 0) {
5981 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5982 phba->cfg_sriov_nr_virtfn);
5983 if (rc) {
5984 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5985 "3020 Requested number of SR-IOV "
5986 "virtual functions (%d) is not "
5987 "supported\n",
5988 phba->cfg_sriov_nr_virtfn);
5989 phba->cfg_sriov_nr_virtfn = 0;
5990 }
5991 }
5992
James Smart5248a742011-07-22 18:37:06 -04005993 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005994
James Smart895427b2017-02-12 13:52:30 -08005995out_free_hba_eq_hdl:
5996 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005997out_free_fcf_rr_bmask:
5998 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005999out_remove_rpi_hdrs:
6000 lpfc_sli4_remove_rpi_hdrs(phba);
6001out_free_active_sgl:
6002 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04006003out_destroy_cq_event_pool:
6004 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006005out_free_bsmbx:
6006 lpfc_destroy_bootstrap_mbox(phba);
6007out_free_mem:
6008 lpfc_mem_free(phba);
6009 return rc;
6010}
6011
6012/**
6013 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6014 * @phba: pointer to lpfc hba data structure.
6015 *
6016 * This routine is invoked to unset the driver internal resources set up
6017 * specific for supporting the SLI-4 HBA device it attached to.
6018 **/
6019static void
6020lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6021{
6022 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6023
James Smart7bb03bb2013-04-17 20:19:16 -04006024 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
6025 kfree(phba->sli4_hba.cpu_map);
6026 phba->sli4_hba.num_present_cpu = 0;
6027 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05006028 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006029
James Smartda0436e2009-05-22 14:51:39 -04006030 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08006031 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04006032
6033 /* Free the allocated rpi headers. */
6034 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04006035 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04006036
James Smart0c9ab6f2010-02-26 14:15:57 -05006037 /* Free eligible FCF index bmask */
6038 kfree(phba->fcf.fcf_rr_bmask);
6039
James Smartda0436e2009-05-22 14:51:39 -04006040 /* Free the ELS sgl list */
6041 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006042 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08006043 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04006044
James Smartda0436e2009-05-22 14:51:39 -04006045 /* Free the completion queue EQ event pool */
6046 lpfc_sli4_cq_event_release_all(phba);
6047 lpfc_sli4_cq_event_pool_destroy(phba);
6048
James Smart6d368e52011-05-24 11:44:12 -04006049 /* Release resource identifiers. */
6050 lpfc_sli4_dealloc_resource_identifiers(phba);
6051
James Smartda0436e2009-05-22 14:51:39 -04006052 /* Free the bsmbx region. */
6053 lpfc_destroy_bootstrap_mbox(phba);
6054
6055 /* Free the SLI Layer memory with SLI4 HBAs */
6056 lpfc_mem_free_all(phba);
6057
6058 /* Free the current connect table */
6059 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006060 &phba->fcf_conn_rec_list, list) {
6061 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04006062 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006063 }
James Smartda0436e2009-05-22 14:51:39 -04006064
6065 return;
6066}
6067
6068/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006069 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04006070 * @phba: The hba struct for which this call is being executed.
6071 * @dev_grp: The HBA PCI-Device group number.
6072 *
6073 * This routine sets up the device INIT interface API function jump table
6074 * in @phba struct.
6075 *
6076 * Returns: 0 - success, -ENODEV - failure.
6077 **/
6078int
6079lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6080{
James Smart84d1b002010-02-12 14:42:33 -05006081 phba->lpfc_hba_init_link = lpfc_hba_init_link;
6082 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05006083 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04006084 switch (dev_grp) {
6085 case LPFC_PCI_DEV_LP:
6086 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6087 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6088 phba->lpfc_stop_port = lpfc_stop_port_s3;
6089 break;
James Smartda0436e2009-05-22 14:51:39 -04006090 case LPFC_PCI_DEV_OC:
6091 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6092 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6093 phba->lpfc_stop_port = lpfc_stop_port_s4;
6094 break;
James Smart3772a992009-05-22 14:50:54 -04006095 default:
6096 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6097 "1431 Invalid HBA PCI-device group: 0x%x\n",
6098 dev_grp);
6099 return -ENODEV;
6100 break;
6101 }
6102 return 0;
6103}
6104
6105/**
James Smart3772a992009-05-22 14:50:54 -04006106 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6107 * @phba: pointer to lpfc hba data structure.
6108 *
6109 * This routine is invoked to set up the driver internal resources after the
6110 * device specific resource setup to support the HBA device it attached to.
6111 *
6112 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006113 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006114 * other values - error
6115 **/
6116static int
6117lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6118{
6119 int error;
6120
6121 /* Startup the kernel thread for this host adapter. */
6122 phba->worker_thread = kthread_run(lpfc_do_work, phba,
6123 "lpfc_worker_%d", phba->brd_no);
6124 if (IS_ERR(phba->worker_thread)) {
6125 error = PTR_ERR(phba->worker_thread);
6126 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006127 }
6128
James Smart3772a992009-05-22 14:50:54 -04006129 return 0;
6130}
6131
6132/**
6133 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6134 * @phba: pointer to lpfc hba data structure.
6135 *
6136 * This routine is invoked to unset the driver internal resources set up after
6137 * the device specific resource setup for supporting the HBA device it
6138 * attached to.
6139 **/
6140static void
6141lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6142{
6143 /* Stop kernel worker thread */
6144 kthread_stop(phba->worker_thread);
6145}
6146
6147/**
6148 * lpfc_free_iocb_list - Free iocb list.
6149 * @phba: pointer to lpfc hba data structure.
6150 *
6151 * This routine is invoked to free the driver's IOCB list and memory.
6152 **/
6153static void
6154lpfc_free_iocb_list(struct lpfc_hba *phba)
6155{
6156 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
6157
6158 spin_lock_irq(&phba->hbalock);
6159 list_for_each_entry_safe(iocbq_entry, iocbq_next,
6160 &phba->lpfc_iocb_list, list) {
6161 list_del(&iocbq_entry->list);
6162 kfree(iocbq_entry);
6163 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006164 }
James Smart3772a992009-05-22 14:50:54 -04006165 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006166
James Smart3772a992009-05-22 14:50:54 -04006167 return;
6168}
dea31012005-04-17 16:05:31 -05006169
James Smart3772a992009-05-22 14:50:54 -04006170/**
6171 * lpfc_init_iocb_list - Allocate and initialize iocb list.
6172 * @phba: pointer to lpfc hba data structure.
6173 *
6174 * This routine is invoked to allocate and initizlize the driver's IOCB
6175 * list and set up the IOCB tag array accordingly.
6176 *
6177 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006178 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006179 * other values - error
6180 **/
6181static int
6182lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6183{
6184 struct lpfc_iocbq *iocbq_entry = NULL;
6185 uint16_t iotag;
6186 int i;
dea31012005-04-17 16:05:31 -05006187
6188 /* Initialize and populate the iocb list per host. */
6189 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04006190 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07006191 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05006192 if (iocbq_entry == NULL) {
6193 printk(KERN_ERR "%s: only allocated %d iocbs of "
6194 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07006195 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05006196 goto out_free_iocbq;
6197 }
6198
James Bottomley604a3e32005-10-29 10:28:33 -05006199 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6200 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04006201 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05006202 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04006203 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05006204 goto out_free_iocbq;
6205 }
James Smart6d368e52011-05-24 11:44:12 -04006206 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04006207 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05006208
6209 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006210 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6211 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05006212 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006213 }
6214
James Smart3772a992009-05-22 14:50:54 -04006215 return 0;
6216
6217out_free_iocbq:
6218 lpfc_free_iocb_list(phba);
6219
6220 return -ENOMEM;
6221}
6222
6223/**
James Smart8a9d2e82012-05-09 21:16:12 -04006224 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006225 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04006226 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006227 *
James Smart8a9d2e82012-05-09 21:16:12 -04006228 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04006229 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006230void
6231lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04006232{
6233 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04006234
6235 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6236 list_del(&sglq_entry->list);
6237 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6238 kfree(sglq_entry);
6239 }
6240}
6241
6242/**
6243 * lpfc_free_els_sgl_list - Free els sgl list.
6244 * @phba: pointer to lpfc hba data structure.
6245 *
6246 * This routine is invoked to free the driver's els sgl list and memory.
6247 **/
6248static void
6249lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6250{
James Smartda0436e2009-05-22 14:51:39 -04006251 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006252
James Smart8a9d2e82012-05-09 21:16:12 -04006253 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04006254 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006255 spin_lock(&phba->sli4_hba.sgl_list_lock);
6256 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6257 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04006258 spin_unlock_irq(&phba->hbalock);
6259
James Smart8a9d2e82012-05-09 21:16:12 -04006260 /* Now free the sgl list */
6261 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006262}
6263
6264/**
James Smartf358dd02017-02-12 13:52:34 -08006265 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
6266 * @phba: pointer to lpfc hba data structure.
6267 *
6268 * This routine is invoked to free the driver's nvmet sgl list and memory.
6269 **/
6270static void
6271lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
6272{
6273 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6274 LIST_HEAD(sglq_list);
6275
6276 /* Retrieve all nvmet sgls from driver list */
6277 spin_lock_irq(&phba->hbalock);
6278 spin_lock(&phba->sli4_hba.sgl_list_lock);
6279 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
6280 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6281 spin_unlock_irq(&phba->hbalock);
6282
6283 /* Now free the sgl list */
6284 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
6285 list_del(&sglq_entry->list);
6286 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
6287 kfree(sglq_entry);
6288 }
6289}
6290
6291/**
James Smartda0436e2009-05-22 14:51:39 -04006292 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6293 * @phba: pointer to lpfc hba data structure.
6294 *
6295 * This routine is invoked to allocate the driver's active sgl memory.
6296 * This array will hold the sglq_entry's for active IOs.
6297 **/
6298static int
6299lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6300{
6301 int size;
6302 size = sizeof(struct lpfc_sglq *);
6303 size *= phba->sli4_hba.max_cfg_param.max_xri;
6304
6305 phba->sli4_hba.lpfc_sglq_active_list =
6306 kzalloc(size, GFP_KERNEL);
6307 if (!phba->sli4_hba.lpfc_sglq_active_list)
6308 return -ENOMEM;
6309 return 0;
6310}
6311
6312/**
6313 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6314 * @phba: pointer to lpfc hba data structure.
6315 *
6316 * This routine is invoked to walk through the array of active sglq entries
6317 * and free all of the resources.
6318 * This is just a place holder for now.
6319 **/
6320static void
6321lpfc_free_active_sgl(struct lpfc_hba *phba)
6322{
6323 kfree(phba->sli4_hba.lpfc_sglq_active_list);
6324}
6325
6326/**
6327 * lpfc_init_sgl_list - Allocate and initialize sgl list.
6328 * @phba: pointer to lpfc hba data structure.
6329 *
6330 * This routine is invoked to allocate and initizlize the driver's sgl
6331 * list and set up the sgl xritag tag array accordingly.
6332 *
James Smartda0436e2009-05-22 14:51:39 -04006333 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006334static void
James Smartda0436e2009-05-22 14:51:39 -04006335lpfc_init_sgl_list(struct lpfc_hba *phba)
6336{
James Smartda0436e2009-05-22 14:51:39 -04006337 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08006338 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04006339 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08006340 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
6341 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04006342
James Smart8a9d2e82012-05-09 21:16:12 -04006343 /* els xri-sgl book keeping */
6344 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006345
James Smart8a9d2e82012-05-09 21:16:12 -04006346 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04006347 phba->sli4_hba.scsi_xri_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -08006348
6349 /* nvme xri-buffer book keeping */
6350 phba->sli4_hba.nvme_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006351}
6352
6353/**
6354 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6355 * @phba: pointer to lpfc hba data structure.
6356 *
6357 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04006358 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04006359 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04006360 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
6361 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04006362 *
6363 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006364 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04006365 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04006366 **/
6367int
6368lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6369{
6370 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006371 struct lpfc_rpi_hdr *rpi_hdr;
6372
6373 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05006374 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04006375 return rc;
James Smart6d368e52011-05-24 11:44:12 -04006376 if (phba->sli4_hba.extents_in_use)
6377 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04006378
6379 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6380 if (!rpi_hdr) {
6381 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6382 "0391 Error during rpi post operation\n");
6383 lpfc_sli4_remove_rpis(phba);
6384 rc = -ENODEV;
6385 }
6386
6387 return rc;
6388}
6389
6390/**
6391 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6392 * @phba: pointer to lpfc hba data structure.
6393 *
6394 * This routine is invoked to allocate a single 4KB memory region to
6395 * support rpis and stores them in the phba. This single region
6396 * provides support for up to 64 rpis. The region is used globally
6397 * by the device.
6398 *
6399 * Returns:
6400 * A valid rpi hdr on success.
6401 * A NULL pointer on any failure.
6402 **/
6403struct lpfc_rpi_hdr *
6404lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6405{
6406 uint16_t rpi_limit, curr_rpi_range;
6407 struct lpfc_dmabuf *dmabuf;
6408 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04006409 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006410
James Smart6d368e52011-05-24 11:44:12 -04006411 /*
6412 * If the SLI4 port supports extents, posting the rpi header isn't
6413 * required. Set the expected maximum count and let the actual value
6414 * get set when extents are fully allocated.
6415 */
6416 if (!phba->sli4_hba.rpi_hdrs_in_use)
6417 return NULL;
6418 if (phba->sli4_hba.extents_in_use)
6419 return NULL;
6420
6421 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04006422 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04006423 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04006424
6425 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006426 /*
6427 * Establish the starting RPI in this header block. The starting
6428 * rpi is normalized to a zero base because the physical rpi is
6429 * port based.
6430 */
James Smart97f2ecf2012-03-01 22:35:23 -05006431 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04006432 spin_unlock_irq(&phba->hbalock);
6433
6434 /*
6435 * The port has a limited number of rpis. The increment here
6436 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
6437 * and to allow the full max_rpi range per port.
6438 */
6439 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04006440 rpi_count = rpi_limit - curr_rpi_range;
6441 else
6442 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04006443
James Smart6d368e52011-05-24 11:44:12 -04006444 if (!rpi_count)
6445 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04006446 /*
6447 * First allocate the protocol header region for the port. The
6448 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6449 */
6450 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6451 if (!dmabuf)
6452 return NULL;
6453
Joe Perches1aee3832014-09-03 12:56:12 -04006454 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6455 LPFC_HDR_TEMPLATE_SIZE,
6456 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006457 if (!dmabuf->virt) {
6458 rpi_hdr = NULL;
6459 goto err_free_dmabuf;
6460 }
6461
James Smartda0436e2009-05-22 14:51:39 -04006462 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6463 rpi_hdr = NULL;
6464 goto err_free_coherent;
6465 }
6466
6467 /* Save the rpi header data for cleanup later. */
6468 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6469 if (!rpi_hdr)
6470 goto err_free_coherent;
6471
6472 rpi_hdr->dmabuf = dmabuf;
6473 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6474 rpi_hdr->page_count = 1;
6475 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006476
6477 /* The rpi_hdr stores the logical index only. */
6478 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04006479 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6480
6481 /*
James Smart6d368e52011-05-24 11:44:12 -04006482 * The next_rpi stores the next logical module-64 rpi value used
6483 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04006484 */
James Smart9589b0622011-04-16 11:03:17 -04006485 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006486 spin_unlock_irq(&phba->hbalock);
6487 return rpi_hdr;
6488
6489 err_free_coherent:
6490 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6491 dmabuf->virt, dmabuf->phys);
6492 err_free_dmabuf:
6493 kfree(dmabuf);
6494 return NULL;
6495}
6496
6497/**
6498 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6499 * @phba: pointer to lpfc hba data structure.
6500 *
6501 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04006502 * to support rpis for SLI4 ports not supporting extents. This routine
6503 * presumes the caller has released all rpis consumed by fabric or port
6504 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04006505 **/
6506void
6507lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6508{
6509 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6510
James Smart6d368e52011-05-24 11:44:12 -04006511 if (!phba->sli4_hba.rpi_hdrs_in_use)
6512 goto exit;
6513
James Smartda0436e2009-05-22 14:51:39 -04006514 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6515 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6516 list_del(&rpi_hdr->list);
6517 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6518 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6519 kfree(rpi_hdr->dmabuf);
6520 kfree(rpi_hdr);
6521 }
James Smart6d368e52011-05-24 11:44:12 -04006522 exit:
6523 /* There are no rpis available to the port now. */
6524 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04006525}
6526
6527/**
James Smart3772a992009-05-22 14:50:54 -04006528 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6529 * @pdev: pointer to pci device data structure.
6530 *
6531 * This routine is invoked to allocate the driver hba data structure for an
6532 * HBA device. If the allocation is successful, the phba reference to the
6533 * PCI device data structure is set.
6534 *
6535 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006536 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04006537 * NULL - error
6538 **/
6539static struct lpfc_hba *
6540lpfc_hba_alloc(struct pci_dev *pdev)
6541{
6542 struct lpfc_hba *phba;
6543
6544 /* Allocate memory for HBA structure */
6545 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6546 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02006547 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04006548 return NULL;
6549 }
6550
6551 /* Set reference to PCI device in HBA structure */
6552 phba->pcidev = pdev;
6553
6554 /* Assign an unused board number */
6555 phba->brd_no = lpfc_get_instance();
6556 if (phba->brd_no < 0) {
6557 kfree(phba);
6558 return NULL;
6559 }
James Smart65791f12016-07-06 12:35:56 -07006560 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04006561
James Smart4fede782010-01-26 23:08:55 -05006562 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04006563 INIT_LIST_HEAD(&phba->ct_ev_waiters);
6564
James Smart3772a992009-05-22 14:50:54 -04006565 return phba;
6566}
6567
6568/**
6569 * lpfc_hba_free - Free driver hba data structure with a device.
6570 * @phba: pointer to lpfc hba data structure.
6571 *
6572 * This routine is invoked to free the driver hba data structure with an
6573 * HBA device.
6574 **/
6575static void
6576lpfc_hba_free(struct lpfc_hba *phba)
6577{
6578 /* Release the driver assigned board number */
6579 idr_remove(&lpfc_hba_index, phba->brd_no);
6580
James Smart895427b2017-02-12 13:52:30 -08006581 /* Free memory allocated with sli3 rings */
6582 kfree(phba->sli.sli3_ring);
6583 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04006584
James Smart3772a992009-05-22 14:50:54 -04006585 kfree(phba);
6586 return;
6587}
6588
6589/**
6590 * lpfc_create_shost - Create hba physical port with associated scsi host.
6591 * @phba: pointer to lpfc hba data structure.
6592 *
6593 * This routine is invoked to create HBA physical port and associate a SCSI
6594 * host with it.
6595 *
6596 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006597 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006598 * other values - error
6599 **/
6600static int
6601lpfc_create_shost(struct lpfc_hba *phba)
6602{
6603 struct lpfc_vport *vport;
6604 struct Scsi_Host *shost;
6605
6606 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05006607 phba->fc_edtov = FF_DEF_EDTOV;
6608 phba->fc_ratov = FF_DEF_RATOV;
6609 phba->fc_altov = FF_DEF_ALTOV;
6610 phba->fc_arbtov = FF_DEF_ARBTOV;
6611
James Smartd7c47992010-06-08 18:31:54 -04006612 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04006613 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05006614 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04006615 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05006616
6617 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006618 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08006619
James Smartf358dd02017-02-12 13:52:34 -08006620 if (phba->nvmet_support) {
6621 /* Only 1 vport (pport) will support NVME target */
6622 if (phba->txrdy_payload_pool == NULL) {
6623 phba->txrdy_payload_pool = pci_pool_create(
6624 "txrdy_pool", phba->pcidev,
6625 TXRDY_PAYLOAD_LEN, 16, 0);
6626 if (phba->txrdy_payload_pool) {
6627 phba->targetport = NULL;
6628 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
6629 lpfc_printf_log(phba, KERN_INFO,
6630 LOG_INIT | LOG_NVME_DISC,
6631 "6076 NVME Target Found\n");
6632 }
6633 }
6634 }
6635
James Smart858c9f62007-06-17 19:56:39 -05006636 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04006637 /* Put reference to SCSI host to driver's device private data */
6638 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05006639
James Smart4258e982015-12-16 18:11:58 -05006640 /*
6641 * At this point we are fully registered with PSA. In addition,
6642 * any initial discovery should be completed.
6643 */
6644 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07006645 if (phba->cfg_enable_SmartSAN ||
6646 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05006647
6648 /* Setup appropriate attribute masks */
6649 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07006650 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05006651 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6652 else
6653 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6654 }
James Smart3772a992009-05-22 14:50:54 -04006655 return 0;
6656}
dea31012005-04-17 16:05:31 -05006657
James Smart3772a992009-05-22 14:50:54 -04006658/**
6659 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6660 * @phba: pointer to lpfc hba data structure.
6661 *
6662 * This routine is invoked to destroy HBA physical port and the associated
6663 * SCSI host.
6664 **/
6665static void
6666lpfc_destroy_shost(struct lpfc_hba *phba)
6667{
6668 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04006669
James Smart3772a992009-05-22 14:50:54 -04006670 /* Destroy physical port that associated with the SCSI host */
6671 destroy_port(vport);
6672
6673 return;
6674}
6675
6676/**
6677 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6678 * @phba: pointer to lpfc hba data structure.
6679 * @shost: the shost to be used to detect Block guard settings.
6680 *
6681 * This routine sets up the local Block guard protocol settings for @shost.
6682 * This routine also allocates memory for debugging bg buffers.
6683 **/
6684static void
6685lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6686{
James Smartbbeb79b2012-06-12 13:54:27 -04006687 uint32_t old_mask;
6688 uint32_t old_guard;
6689
James Smart3772a992009-05-22 14:50:54 -04006690 int pagecnt = 10;
James Smartb3b98b72016-10-13 15:06:06 -07006691 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04006692 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6693 "1478 Registering BlockGuard with the "
6694 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006695
James Smartb3b98b72016-10-13 15:06:06 -07006696 old_mask = phba->cfg_prot_mask;
6697 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04006698
6699 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07006700 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04006701 SHOST_DIX_TYPE0_PROTECTION |
6702 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07006703 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
6704 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04006705
6706 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07006707 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6708 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04006709
James Smartb3b98b72016-10-13 15:06:06 -07006710 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6711 if ((old_mask != phba->cfg_prot_mask) ||
6712 (old_guard != phba->cfg_prot_guard))
James Smartbbeb79b2012-06-12 13:54:27 -04006713 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6714 "1475 Registering BlockGuard with the "
6715 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07006716 phba->cfg_prot_mask,
6717 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006718
James Smartb3b98b72016-10-13 15:06:06 -07006719 scsi_host_set_prot(shost, phba->cfg_prot_mask);
6720 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006721 } else
6722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6723 "1479 Not Registering BlockGuard with the SCSI "
6724 "layer, Bad protection parameters: %d %d\n",
6725 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006726 }
James Smartbbeb79b2012-06-12 13:54:27 -04006727
James Smart81301a92008-12-04 22:39:46 -05006728 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006729 while (pagecnt) {
6730 spin_lock_init(&_dump_buf_lock);
6731 _dump_buf_data =
6732 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6733 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006734 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6735 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006736 "_dump_buf_data at 0x%p\n",
6737 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006738 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006739 memset(_dump_buf_data, 0,
6740 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006741 break;
James Smart3772a992009-05-22 14:50:54 -04006742 } else
James Smart81301a92008-12-04 22:39:46 -05006743 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006744 }
James Smart81301a92008-12-04 22:39:46 -05006745 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006746 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6747 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006748 "memory for hexdump\n");
6749 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006750 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6751 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006752 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006753 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006754 while (pagecnt) {
6755 _dump_buf_dif =
6756 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6757 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006758 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6759 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006760 "_dump_buf_dif at 0x%p\n",
6761 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006762 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006763 memset(_dump_buf_dif, 0,
6764 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006765 break;
James Smart3772a992009-05-22 14:50:54 -04006766 } else
James Smart81301a92008-12-04 22:39:46 -05006767 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006768 }
James Smart81301a92008-12-04 22:39:46 -05006769 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006770 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6771 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006772 "memory for hexdump\n");
6773 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006774 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6775 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006776 _dump_buf_dif);
6777}
6778
6779/**
6780 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6781 * @phba: pointer to lpfc hba data structure.
6782 *
6783 * This routine is invoked to perform all the necessary post initialization
6784 * setup for the device.
6785 **/
6786static void
6787lpfc_post_init_setup(struct lpfc_hba *phba)
6788{
6789 struct Scsi_Host *shost;
6790 struct lpfc_adapter_event_header adapter_event;
6791
6792 /* Get the default values for Model Name and Description */
6793 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6794
6795 /*
6796 * hba setup may have changed the hba_queue_depth so we need to
6797 * adjust the value of can_queue.
6798 */
6799 shost = pci_get_drvdata(phba->pcidev);
6800 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6801 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6802 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006803
6804 lpfc_host_attrib_init(shost);
6805
James Smart2e0fef82007-06-17 19:56:36 -05006806 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6807 spin_lock_irq(shost->host_lock);
6808 lpfc_poll_start_timer(phba);
6809 spin_unlock_irq(shost->host_lock);
6810 }
James Smart8f6d98d2006-08-01 07:34:00 -04006811
James Smart93996272008-08-24 21:50:30 -04006812 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6813 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006814 /* Send board arrival event to upper layer */
6815 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6816 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6817 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006818 sizeof(adapter_event),
6819 (char *) &adapter_event,
6820 LPFC_NL_VENDOR_ID);
6821 return;
6822}
6823
6824/**
6825 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6826 * @phba: pointer to lpfc hba data structure.
6827 *
6828 * This routine is invoked to set up the PCI device memory space for device
6829 * with SLI-3 interface spec.
6830 *
6831 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006832 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006833 * other values - error
6834 **/
6835static int
6836lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6837{
6838 struct pci_dev *pdev;
6839 unsigned long bar0map_len, bar2map_len;
6840 int i, hbq_count;
6841 void *ptr;
6842 int error = -ENODEV;
6843
6844 /* Obtain PCI device reference */
6845 if (!phba->pcidev)
6846 return error;
6847 else
6848 pdev = phba->pcidev;
6849
6850 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006851 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6852 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6853 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6854 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006855 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006856 }
6857 }
James Smart3772a992009-05-22 14:50:54 -04006858
6859 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6860 * required by each mapping.
6861 */
6862 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6863 bar0map_len = pci_resource_len(pdev, 0);
6864
6865 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6866 bar2map_len = pci_resource_len(pdev, 2);
6867
6868 /* Map HBA SLIM to a kernel virtual address. */
6869 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6870 if (!phba->slim_memmap_p) {
6871 dev_printk(KERN_ERR, &pdev->dev,
6872 "ioremap failed for SLIM memory.\n");
6873 goto out;
6874 }
6875
6876 /* Map HBA Control Registers to a kernel virtual address. */
6877 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6878 if (!phba->ctrl_regs_memmap_p) {
6879 dev_printk(KERN_ERR, &pdev->dev,
6880 "ioremap failed for HBA control registers.\n");
6881 goto out_iounmap_slim;
6882 }
6883
6884 /* Allocate memory for SLI-2 structures */
Joe Perches1aee3832014-09-03 12:56:12 -04006885 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6886 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04006887 if (!phba->slim2p.virt)
6888 goto out_iounmap;
6889
James Smart3772a992009-05-22 14:50:54 -04006890 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006891 phba->mbox_ext = (phba->slim2p.virt +
6892 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006893 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6894 phba->IOCBs = (phba->slim2p.virt +
6895 offsetof(struct lpfc_sli2_slim, IOCBs));
6896
6897 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6898 lpfc_sli_hbq_size(),
6899 &phba->hbqslimp.phys,
6900 GFP_KERNEL);
6901 if (!phba->hbqslimp.virt)
6902 goto out_free_slim;
6903
6904 hbq_count = lpfc_sli_hbq_count();
6905 ptr = phba->hbqslimp.virt;
6906 for (i = 0; i < hbq_count; ++i) {
6907 phba->hbqs[i].hbq_virt = ptr;
6908 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6909 ptr += (lpfc_hbq_defs[i]->entry_count *
6910 sizeof(struct lpfc_hbq_entry));
6911 }
6912 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6913 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6914
6915 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6916
James Smart3772a992009-05-22 14:50:54 -04006917 phba->MBslimaddr = phba->slim_memmap_p;
6918 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6919 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6920 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6921 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006922
dea31012005-04-17 16:05:31 -05006923 return 0;
6924
dea31012005-04-17 16:05:31 -05006925out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006926 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6927 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006928out_iounmap:
6929 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006930out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006931 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006932out:
6933 return error;
6934}
6935
James Smarte59058c2008-08-24 21:49:00 -04006936/**
James Smart3772a992009-05-22 14:50:54 -04006937 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6938 * @phba: pointer to lpfc hba data structure.
6939 *
6940 * This routine is invoked to unset the PCI device memory space for device
6941 * with SLI-3 interface spec.
6942 **/
6943static void
6944lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6945{
6946 struct pci_dev *pdev;
6947
6948 /* Obtain PCI device reference */
6949 if (!phba->pcidev)
6950 return;
6951 else
6952 pdev = phba->pcidev;
6953
6954 /* Free coherent DMA memory allocated */
6955 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6956 phba->hbqslimp.virt, phba->hbqslimp.phys);
6957 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6958 phba->slim2p.virt, phba->slim2p.phys);
6959
6960 /* I/O memory unmap */
6961 iounmap(phba->ctrl_regs_memmap_p);
6962 iounmap(phba->slim_memmap_p);
6963
6964 return;
6965}
6966
6967/**
James Smartda0436e2009-05-22 14:51:39 -04006968 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6969 * @phba: pointer to lpfc hba data structure.
6970 *
6971 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6972 * done and check status.
6973 *
6974 * Return 0 if successful, otherwise -ENODEV.
6975 **/
6976int
6977lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6978{
James Smart2fcee4b2010-12-15 17:57:46 -05006979 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6980 struct lpfc_register reg_data;
6981 int i, port_error = 0;
6982 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006983
James Smart9940b972011-03-11 16:06:12 -05006984 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6985 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006986 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006987 return -ENODEV;
6988
James Smartda0436e2009-05-22 14:51:39 -04006989 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6990 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006991 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6992 &portsmphr_reg.word0) ||
6993 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006994 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006995 port_error = -ENODEV;
6996 break;
6997 }
James Smart2fcee4b2010-12-15 17:57:46 -05006998 if (LPFC_POST_STAGE_PORT_READY ==
6999 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04007000 break;
James Smartda0436e2009-05-22 14:51:39 -04007001 msleep(10);
7002 }
7003
James Smart2fcee4b2010-12-15 17:57:46 -05007004 /*
7005 * If there was a port error during POST, then don't proceed with
7006 * other register reads as the data may not be valid. Just exit.
7007 */
7008 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04007009 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007010 "1408 Port Failed POST - portsmphr=0x%x, "
7011 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7012 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7013 portsmphr_reg.word0,
7014 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7015 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7016 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7017 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7018 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7019 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7020 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7021 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7022 } else {
James Smart28baac72010-02-12 14:42:03 -05007023 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007024 "2534 Device Info: SLIFamily=0x%x, "
7025 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7026 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05007027 bf_get(lpfc_sli_intf_sli_family,
7028 &phba->sli4_hba.sli_intf),
7029 bf_get(lpfc_sli_intf_slirev,
7030 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007031 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05007032 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007033 bf_get(lpfc_sli_intf_sli_hint1,
7034 &phba->sli4_hba.sli_intf),
7035 bf_get(lpfc_sli_intf_sli_hint2,
7036 &phba->sli4_hba.sli_intf),
7037 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05007038 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05007039 /*
7040 * Check for other Port errors during the initialization
7041 * process. Fail the load if the port did not come up
7042 * correctly.
7043 */
7044 if_type = bf_get(lpfc_sli_intf_if_type,
7045 &phba->sli4_hba.sli_intf);
7046 switch (if_type) {
7047 case LPFC_SLI_INTF_IF_TYPE_0:
7048 phba->sli4_hba.ue_mask_lo =
7049 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7050 phba->sli4_hba.ue_mask_hi =
7051 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7052 uerrlo_reg.word0 =
7053 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7054 uerrhi_reg.word0 =
7055 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7056 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7057 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7059 "1422 Unrecoverable Error "
7060 "Detected during POST "
7061 "uerr_lo_reg=0x%x, "
7062 "uerr_hi_reg=0x%x, "
7063 "ue_mask_lo_reg=0x%x, "
7064 "ue_mask_hi_reg=0x%x\n",
7065 uerrlo_reg.word0,
7066 uerrhi_reg.word0,
7067 phba->sli4_hba.ue_mask_lo,
7068 phba->sli4_hba.ue_mask_hi);
7069 port_error = -ENODEV;
7070 }
7071 break;
7072 case LPFC_SLI_INTF_IF_TYPE_2:
7073 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05007074 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7075 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04007076 (bf_get(lpfc_sliport_status_err, &reg_data) &&
7077 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007078 phba->work_status[0] =
7079 readl(phba->sli4_hba.u.if_type2.
7080 ERR1regaddr);
7081 phba->work_status[1] =
7082 readl(phba->sli4_hba.u.if_type2.
7083 ERR2regaddr);
7084 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007085 "2888 Unrecoverable port error "
7086 "following POST: port status reg "
7087 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007088 "error 1=0x%x, error 2=0x%x\n",
7089 reg_data.word0,
7090 portsmphr_reg.word0,
7091 phba->work_status[0],
7092 phba->work_status[1]);
7093 port_error = -ENODEV;
7094 }
7095 break;
7096 case LPFC_SLI_INTF_IF_TYPE_1:
7097 default:
7098 break;
7099 }
James Smart28baac72010-02-12 14:42:03 -05007100 }
James Smartda0436e2009-05-22 14:51:39 -04007101 return port_error;
7102}
7103
7104/**
7105 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7106 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05007107 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04007108 *
7109 * This routine is invoked to set up SLI4 BAR0 PCI config space register
7110 * memory map.
7111 **/
7112static void
James Smart2fcee4b2010-12-15 17:57:46 -05007113lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04007114{
James Smart2fcee4b2010-12-15 17:57:46 -05007115 switch (if_type) {
7116 case LPFC_SLI_INTF_IF_TYPE_0:
7117 phba->sli4_hba.u.if_type0.UERRLOregaddr =
7118 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7119 phba->sli4_hba.u.if_type0.UERRHIregaddr =
7120 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7121 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7122 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7123 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7124 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7125 phba->sli4_hba.SLIINTFregaddr =
7126 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7127 break;
7128 case LPFC_SLI_INTF_IF_TYPE_2:
7129 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007130 phba->sli4_hba.conf_regs_memmap_p +
7131 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007132 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007133 phba->sli4_hba.conf_regs_memmap_p +
7134 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007135 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007136 phba->sli4_hba.conf_regs_memmap_p +
7137 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007138 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007139 phba->sli4_hba.conf_regs_memmap_p +
7140 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007141 phba->sli4_hba.SLIINTFregaddr =
7142 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7143 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007144 phba->sli4_hba.conf_regs_memmap_p +
7145 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007146 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05007147 phba->sli4_hba.conf_regs_memmap_p +
7148 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05007149 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05007150 phba->sli4_hba.conf_regs_memmap_p +
7151 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05007152 phba->sli4_hba.EQCQDBregaddr =
7153 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
7154 phba->sli4_hba.MQDBregaddr =
7155 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
7156 phba->sli4_hba.BMBXregaddr =
7157 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7158 break;
7159 case LPFC_SLI_INTF_IF_TYPE_1:
7160 default:
7161 dev_printk(KERN_ERR, &phba->pcidev->dev,
7162 "FATAL - unsupported SLI4 interface type - %d\n",
7163 if_type);
7164 break;
7165 }
James Smartda0436e2009-05-22 14:51:39 -04007166}
7167
7168/**
7169 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
7170 * @phba: pointer to lpfc hba data structure.
7171 *
7172 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
7173 * memory map.
7174 **/
7175static void
7176lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
7177{
James Smart2fcee4b2010-12-15 17:57:46 -05007178 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7179 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04007180 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007181 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04007182 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007183 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04007184 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007185 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04007186}
7187
7188/**
7189 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
7190 * @phba: pointer to lpfc hba data structure.
7191 * @vf: virtual function number
7192 *
7193 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
7194 * based on the given viftual function number, @vf.
7195 *
7196 * Return 0 if successful, otherwise -ENODEV.
7197 **/
7198static int
7199lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
7200{
7201 if (vf > LPFC_VIR_FUNC_MAX)
7202 return -ENODEV;
7203
7204 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05007205 vf * LPFC_VFR_PAGE_SIZE +
7206 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007207 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05007208 vf * LPFC_VFR_PAGE_SIZE +
7209 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007210 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7211 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
7212 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7213 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7214 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7215 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7216 return 0;
7217}
7218
7219/**
7220 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7221 * @phba: pointer to lpfc hba data structure.
7222 *
7223 * This routine is invoked to create the bootstrap mailbox
7224 * region consistent with the SLI-4 interface spec. This
7225 * routine allocates all memory necessary to communicate
7226 * mailbox commands to the port and sets up all alignment
7227 * needs. No locks are expected to be held when calling
7228 * this routine.
7229 *
7230 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007231 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04007232 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04007233 **/
7234static int
7235lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7236{
7237 uint32_t bmbx_size;
7238 struct lpfc_dmabuf *dmabuf;
7239 struct dma_address *dma_address;
7240 uint32_t pa_addr;
7241 uint64_t phys_addr;
7242
7243 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7244 if (!dmabuf)
7245 return -ENOMEM;
7246
7247 /*
7248 * The bootstrap mailbox region is comprised of 2 parts
7249 * plus an alignment restriction of 16 bytes.
7250 */
7251 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Joe Perches1aee3832014-09-03 12:56:12 -04007252 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7253 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007254 if (!dmabuf->virt) {
7255 kfree(dmabuf);
7256 return -ENOMEM;
7257 }
James Smartda0436e2009-05-22 14:51:39 -04007258
7259 /*
7260 * Initialize the bootstrap mailbox pointers now so that the register
7261 * operations are simple later. The mailbox dma address is required
7262 * to be 16-byte aligned. Also align the virtual memory as each
7263 * maibox is copied into the bmbx mailbox region before issuing the
7264 * command to the port.
7265 */
7266 phba->sli4_hba.bmbx.dmabuf = dmabuf;
7267 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7268
7269 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7270 LPFC_ALIGN_16_BYTE);
7271 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7272 LPFC_ALIGN_16_BYTE);
7273
7274 /*
7275 * Set the high and low physical addresses now. The SLI4 alignment
7276 * requirement is 16 bytes and the mailbox is posted to the port
7277 * as two 30-bit addresses. The other data is a bit marking whether
7278 * the 30-bit address is the high or low address.
7279 * Upcast bmbx aphys to 64bits so shift instruction compiles
7280 * clean on 32 bit machines.
7281 */
7282 dma_address = &phba->sli4_hba.bmbx.dma_address;
7283 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7284 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7285 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7286 LPFC_BMBX_BIT1_ADDR_HI);
7287
7288 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7289 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7290 LPFC_BMBX_BIT1_ADDR_LO);
7291 return 0;
7292}
7293
7294/**
7295 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7296 * @phba: pointer to lpfc hba data structure.
7297 *
7298 * This routine is invoked to teardown the bootstrap mailbox
7299 * region and release all host resources. This routine requires
7300 * the caller to ensure all mailbox commands recovered, no
7301 * additional mailbox comands are sent, and interrupts are disabled
7302 * before calling this routine.
7303 *
7304 **/
7305static void
7306lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7307{
7308 dma_free_coherent(&phba->pcidev->dev,
7309 phba->sli4_hba.bmbx.bmbx_size,
7310 phba->sli4_hba.bmbx.dmabuf->virt,
7311 phba->sli4_hba.bmbx.dmabuf->phys);
7312
7313 kfree(phba->sli4_hba.bmbx.dmabuf);
7314 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7315}
7316
7317/**
7318 * lpfc_sli4_read_config - Get the config parameters.
7319 * @phba: pointer to lpfc hba data structure.
7320 *
7321 * This routine is invoked to read the configuration parameters from the HBA.
7322 * The configuration parameters are used to set the base and maximum values
7323 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7324 * allocation for the port.
7325 *
7326 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007327 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007328 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007329 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007330 **/
James Smartff78d8f2011-12-13 13:21:35 -05007331int
James Smartda0436e2009-05-22 14:51:39 -04007332lpfc_sli4_read_config(struct lpfc_hba *phba)
7333{
7334 LPFC_MBOXQ_t *pmb;
7335 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04007336 union lpfc_sli4_cfg_shdr *shdr;
7337 uint32_t shdr_status, shdr_add_status;
7338 struct lpfc_mbx_get_func_cfg *get_func_cfg;
7339 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04007340 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07007341 uint16_t forced_link_speed;
7342 uint32_t if_type;
James Smart8aa134a2012-08-14 14:25:29 -04007343 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04007344
7345 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7346 if (!pmb) {
7347 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7348 "2011 Unable to allocate memory for issuing "
7349 "SLI_CONFIG_SPECIAL mailbox command\n");
7350 return -ENOMEM;
7351 }
7352
7353 lpfc_read_config(phba, pmb);
7354
7355 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7356 if (rc != MBX_SUCCESS) {
7357 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7358 "2012 Mailbox failed , mbxCmd x%x "
7359 "READ_CONFIG, mbxStatus x%x\n",
7360 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7361 bf_get(lpfc_mqe_status, &pmb->u.mqe));
7362 rc = -EIO;
7363 } else {
7364 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05007365 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7366 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7367 phba->sli4_hba.lnk_info.lnk_tp =
7368 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7369 phba->sli4_hba.lnk_info.lnk_no =
7370 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7371 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7372 "3081 lnk_type:%d, lnk_numb:%d\n",
7373 phba->sli4_hba.lnk_info.lnk_tp,
7374 phba->sli4_hba.lnk_info.lnk_no);
7375 } else
7376 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7377 "3082 Mailbox (x%x) returned ldv:x0\n",
7378 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04007379 phba->sli4_hba.extents_in_use =
7380 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007381 phba->sli4_hba.max_cfg_param.max_xri =
7382 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7383 phba->sli4_hba.max_cfg_param.xri_base =
7384 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7385 phba->sli4_hba.max_cfg_param.max_vpi =
7386 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7387 phba->sli4_hba.max_cfg_param.vpi_base =
7388 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7389 phba->sli4_hba.max_cfg_param.max_rpi =
7390 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7391 phba->sli4_hba.max_cfg_param.rpi_base =
7392 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7393 phba->sli4_hba.max_cfg_param.max_vfi =
7394 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7395 phba->sli4_hba.max_cfg_param.vfi_base =
7396 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7397 phba->sli4_hba.max_cfg_param.max_fcfi =
7398 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007399 phba->sli4_hba.max_cfg_param.max_eq =
7400 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7401 phba->sli4_hba.max_cfg_param.max_rq =
7402 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7403 phba->sli4_hba.max_cfg_param.max_wq =
7404 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7405 phba->sli4_hba.max_cfg_param.max_cq =
7406 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7407 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7408 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7409 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7410 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05007411 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7412 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04007413 phba->max_vports = phba->max_vpi;
7414 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04007415 "2003 cfg params Extents? %d "
7416 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04007417 "VPI(B:%d M:%d) "
7418 "VFI(B:%d M:%d) "
7419 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08007420 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04007421 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04007422 phba->sli4_hba.max_cfg_param.xri_base,
7423 phba->sli4_hba.max_cfg_param.max_xri,
7424 phba->sli4_hba.max_cfg_param.vpi_base,
7425 phba->sli4_hba.max_cfg_param.max_vpi,
7426 phba->sli4_hba.max_cfg_param.vfi_base,
7427 phba->sli4_hba.max_cfg_param.max_vfi,
7428 phba->sli4_hba.max_cfg_param.rpi_base,
7429 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08007430 phba->sli4_hba.max_cfg_param.max_fcfi,
7431 phba->sli4_hba.max_cfg_param.max_eq,
7432 phba->sli4_hba.max_cfg_param.max_cq,
7433 phba->sli4_hba.max_cfg_param.max_wq,
7434 phba->sli4_hba.max_cfg_param.max_rq);
7435
James Smartda0436e2009-05-22 14:51:39 -04007436 }
James Smart912e3ac2011-05-24 11:42:11 -04007437
7438 if (rc)
7439 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04007440
James Smartc6918162016-10-13 15:06:16 -07007441 /* Update link speed if forced link speed is supported */
7442 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7443 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7444 forced_link_speed =
7445 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7446 if (forced_link_speed) {
7447 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7448
7449 switch (forced_link_speed) {
7450 case LINK_SPEED_1G:
7451 phba->cfg_link_speed =
7452 LPFC_USER_LINK_SPEED_1G;
7453 break;
7454 case LINK_SPEED_2G:
7455 phba->cfg_link_speed =
7456 LPFC_USER_LINK_SPEED_2G;
7457 break;
7458 case LINK_SPEED_4G:
7459 phba->cfg_link_speed =
7460 LPFC_USER_LINK_SPEED_4G;
7461 break;
7462 case LINK_SPEED_8G:
7463 phba->cfg_link_speed =
7464 LPFC_USER_LINK_SPEED_8G;
7465 break;
7466 case LINK_SPEED_10G:
7467 phba->cfg_link_speed =
7468 LPFC_USER_LINK_SPEED_10G;
7469 break;
7470 case LINK_SPEED_16G:
7471 phba->cfg_link_speed =
7472 LPFC_USER_LINK_SPEED_16G;
7473 break;
7474 case LINK_SPEED_32G:
7475 phba->cfg_link_speed =
7476 LPFC_USER_LINK_SPEED_32G;
7477 break;
7478 case 0xffff:
7479 phba->cfg_link_speed =
7480 LPFC_USER_LINK_SPEED_AUTO;
7481 break;
7482 default:
7483 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7484 "0047 Unrecognized link "
7485 "speed : %d\n",
7486 forced_link_speed);
7487 phba->cfg_link_speed =
7488 LPFC_USER_LINK_SPEED_AUTO;
7489 }
7490 }
7491 }
7492
James Smartda0436e2009-05-22 14:51:39 -04007493 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04007494 length = phba->sli4_hba.max_cfg_param.max_xri -
7495 lpfc_sli4_get_els_iocb_cnt(phba);
7496 if (phba->cfg_hba_queue_depth > length) {
7497 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7498 "3361 HBA queue depth changed from %d to %d\n",
7499 phba->cfg_hba_queue_depth, length);
7500 phba->cfg_hba_queue_depth = length;
7501 }
James Smart912e3ac2011-05-24 11:42:11 -04007502
7503 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
7504 LPFC_SLI_INTF_IF_TYPE_2)
7505 goto read_cfg_out;
7506
7507 /* get the pf# and vf# for SLI4 if_type 2 port */
7508 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7509 sizeof(struct lpfc_sli4_cfg_mhdr));
7510 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7511 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7512 length, LPFC_SLI4_MBX_EMBED);
7513
James Smart8aa134a2012-08-14 14:25:29 -04007514 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04007515 shdr = (union lpfc_sli4_cfg_shdr *)
7516 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7517 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7518 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04007519 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04007520 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7521 "3026 Mailbox failed , mbxCmd x%x "
7522 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7523 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7524 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04007525 goto read_cfg_out;
7526 }
7527
7528 /* search for fc_fcoe resrouce descriptor */
7529 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04007530
James Smart8aa134a2012-08-14 14:25:29 -04007531 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7532 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7533 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7534 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7535 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7536 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7537 goto read_cfg_out;
7538
James Smart912e3ac2011-05-24 11:42:11 -04007539 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04007540 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04007541 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04007542 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04007543 phba->sli4_hba.iov.pf_number =
7544 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7545 phba->sli4_hba.iov.vf_number =
7546 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7547 break;
7548 }
7549 }
7550
7551 if (i < LPFC_RSRC_DESC_MAX_NUM)
7552 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7553 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7554 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7555 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04007556 else
James Smart912e3ac2011-05-24 11:42:11 -04007557 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7558 "3028 GET_FUNCTION_CONFIG: failed to find "
7559 "Resrouce Descriptor:x%x\n",
7560 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04007561
7562read_cfg_out:
7563 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007564 return rc;
7565}
7566
7567/**
James Smart2fcee4b2010-12-15 17:57:46 -05007568 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04007569 * @phba: pointer to lpfc hba data structure.
7570 *
James Smart2fcee4b2010-12-15 17:57:46 -05007571 * This routine is invoked to setup the port-side endian order when
7572 * the port if_type is 0. This routine has no function for other
7573 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04007574 *
7575 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007576 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007577 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007578 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007579 **/
7580static int
7581lpfc_setup_endian_order(struct lpfc_hba *phba)
7582{
7583 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007584 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007585 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7586 HOST_ENDIAN_HIGH_WORD1};
7587
James Smart2fcee4b2010-12-15 17:57:46 -05007588 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7589 switch (if_type) {
7590 case LPFC_SLI_INTF_IF_TYPE_0:
7591 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7592 GFP_KERNEL);
7593 if (!mboxq) {
7594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7595 "0492 Unable to allocate memory for "
7596 "issuing SLI_CONFIG_SPECIAL mailbox "
7597 "command\n");
7598 return -ENOMEM;
7599 }
James Smartda0436e2009-05-22 14:51:39 -04007600
James Smart2fcee4b2010-12-15 17:57:46 -05007601 /*
7602 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7603 * two words to contain special data values and no other data.
7604 */
7605 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7606 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7607 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7608 if (rc != MBX_SUCCESS) {
7609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7610 "0493 SLI_CONFIG_SPECIAL mailbox "
7611 "failed with status x%x\n",
7612 rc);
7613 rc = -EIO;
7614 }
7615 mempool_free(mboxq, phba->mbox_mem_pool);
7616 break;
7617 case LPFC_SLI_INTF_IF_TYPE_2:
7618 case LPFC_SLI_INTF_IF_TYPE_1:
7619 default:
7620 break;
James Smartda0436e2009-05-22 14:51:39 -04007621 }
James Smartda0436e2009-05-22 14:51:39 -04007622 return rc;
7623}
7624
7625/**
James Smart895427b2017-02-12 13:52:30 -08007626 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04007627 * @phba: pointer to lpfc hba data structure.
7628 *
James Smart895427b2017-02-12 13:52:30 -08007629 * This routine is invoked to check the user settable queue counts for EQs.
7630 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04007631 * adhere to the constraints of the system's interrupt vectors and the port's
7632 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04007633 *
7634 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007635 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007636 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007637 **/
7638static int
James Smart5350d872011-10-10 21:33:49 -04007639lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04007640{
James Smart895427b2017-02-12 13:52:30 -08007641 int io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05007642 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04007643
7644 /*
James Smart67d12732012-08-03 12:36:13 -04007645 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04007646 * device parameters
7647 */
7648
James Smart67d12732012-08-03 12:36:13 -04007649 /* Sanity check on HBA EQ parameters */
James Smart895427b2017-02-12 13:52:30 -08007650 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04007651
James Smart895427b2017-02-12 13:52:30 -08007652 if (phba->sli4_hba.num_online_cpu < io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04007653 lpfc_printf_log(phba,
7654 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04007655 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04007656 "online CPUs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007657 io_channel, phba->sli4_hba.num_online_cpu);
7658 io_channel = phba->sli4_hba.num_online_cpu;
James Smart90695ee2012-08-14 14:25:36 -04007659 }
7660
James Smart895427b2017-02-12 13:52:30 -08007661 if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04007662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7663 "2575 Reducing IO channels to match number of "
7664 "available EQs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007665 io_channel,
James Smart82c3e9b2012-09-29 11:29:50 -04007666 phba->sli4_hba.max_cfg_param.max_eq);
James Smart895427b2017-02-12 13:52:30 -08007667 io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04007668 }
James Smart67d12732012-08-03 12:36:13 -04007669
James Smart895427b2017-02-12 13:52:30 -08007670 /* The actual number of FCP / NVME event queues adopted */
7671 if (io_channel != phba->io_channel_irqs)
7672 phba->io_channel_irqs = io_channel;
7673 if (phba->cfg_fcp_io_channel > io_channel)
7674 phba->cfg_fcp_io_channel = io_channel;
7675 if (phba->cfg_nvme_io_channel > io_channel)
7676 phba->cfg_nvme_io_channel = io_channel;
7677
7678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08007679 "2574 IO channels: irqs %d fcp %d nvme %d\n",
James Smart895427b2017-02-12 13:52:30 -08007680 phba->io_channel_irqs, phba->cfg_fcp_io_channel,
7681 phba->cfg_nvme_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04007682
James Smartda0436e2009-05-22 14:51:39 -04007683 /* Get EQ depth from module parameter, fake the default for now */
7684 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7685 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7686
James Smart5350d872011-10-10 21:33:49 -04007687 /* Get CQ depth from module parameter, fake the default for now */
7688 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7689 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04007690 return 0;
James Smart895427b2017-02-12 13:52:30 -08007691}
7692
7693static int
7694lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
7695{
7696 struct lpfc_queue *qdesc;
7697 int cnt;
7698
7699 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7700 phba->sli4_hba.cq_ecount);
7701 if (!qdesc) {
7702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7703 "0508 Failed allocate fast-path NVME CQ (%d)\n",
7704 wqidx);
7705 return 1;
7706 }
7707 phba->sli4_hba.nvme_cq[wqidx] = qdesc;
7708
7709 cnt = LPFC_NVME_WQSIZE;
7710 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_WQE128_SIZE, cnt);
7711 if (!qdesc) {
7712 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7713 "0509 Failed allocate fast-path NVME WQ (%d)\n",
7714 wqidx);
7715 return 1;
7716 }
7717 phba->sli4_hba.nvme_wq[wqidx] = qdesc;
7718 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7719 return 0;
7720}
7721
7722static int
7723lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
7724{
7725 struct lpfc_queue *qdesc;
7726 uint32_t wqesize;
7727
7728 /* Create Fast Path FCP CQs */
7729 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7730 phba->sli4_hba.cq_ecount);
7731 if (!qdesc) {
7732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7733 "0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
7734 return 1;
7735 }
7736 phba->sli4_hba.fcp_cq[wqidx] = qdesc;
7737
7738 /* Create Fast Path FCP WQs */
7739 wqesize = (phba->fcp_embed_io) ?
7740 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
7741 qdesc = lpfc_sli4_queue_alloc(phba, wqesize, phba->sli4_hba.wq_ecount);
7742 if (!qdesc) {
7743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7744 "0503 Failed allocate fast-path FCP WQ (%d)\n",
7745 wqidx);
7746 return 1;
7747 }
7748 phba->sli4_hba.fcp_wq[wqidx] = qdesc;
7749 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7750 return 0;
James Smart5350d872011-10-10 21:33:49 -04007751}
7752
7753/**
7754 * lpfc_sli4_queue_create - Create all the SLI4 queues
7755 * @phba: pointer to lpfc hba data structure.
7756 *
7757 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
7758 * operation. For each SLI4 queue type, the parameters such as queue entry
7759 * count (queue depth) shall be taken from the module parameter. For now,
7760 * we just use some constant number as place holder.
7761 *
7762 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07007763 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04007764 * -ENOMEM - No availble memory
7765 * -EIO - The mailbox failed to complete successfully.
7766 **/
7767int
7768lpfc_sli4_queue_create(struct lpfc_hba *phba)
7769{
7770 struct lpfc_queue *qdesc;
James Smart895427b2017-02-12 13:52:30 -08007771 int idx, io_channel;
James Smart5350d872011-10-10 21:33:49 -04007772
7773 /*
James Smart67d12732012-08-03 12:36:13 -04007774 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08007775 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04007776 */
James Smart895427b2017-02-12 13:52:30 -08007777 io_channel = phba->io_channel_irqs;
7778 if (!io_channel)
James Smart67d12732012-08-03 12:36:13 -04007779 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04007780
James Smart67d12732012-08-03 12:36:13 -04007781 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
7782 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
7783 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
7784 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
7785 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
7786 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08007787 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7788 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7789 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7790 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04007791
James Smart895427b2017-02-12 13:52:30 -08007792 phba->sli4_hba.hba_eq = kcalloc(io_channel,
7793 sizeof(struct lpfc_queue *),
7794 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04007795 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04007796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007797 "2576 Failed allocate memory for "
7798 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007799 goto out_error;
7800 }
James Smart67d12732012-08-03 12:36:13 -04007801
James Smart895427b2017-02-12 13:52:30 -08007802 if (phba->cfg_fcp_io_channel) {
7803 phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
7804 sizeof(struct lpfc_queue *),
7805 GFP_KERNEL);
7806 if (!phba->sli4_hba.fcp_cq) {
7807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7808 "2577 Failed allocate memory for "
7809 "fast-path CQ record array\n");
7810 goto out_error;
7811 }
7812 phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
7813 sizeof(struct lpfc_queue *),
7814 GFP_KERNEL);
7815 if (!phba->sli4_hba.fcp_wq) {
7816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7817 "2578 Failed allocate memory for "
7818 "fast-path FCP WQ record array\n");
7819 goto out_error;
7820 }
7821 /*
7822 * Since the first EQ can have multiple CQs associated with it,
7823 * this array is used to quickly see if we have a FCP fast-path
7824 * CQ match.
7825 */
7826 phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
7827 sizeof(uint16_t),
7828 GFP_KERNEL);
7829 if (!phba->sli4_hba.fcp_cq_map) {
7830 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7831 "2545 Failed allocate memory for "
7832 "fast-path CQ map\n");
7833 goto out_error;
7834 }
James Smart67d12732012-08-03 12:36:13 -04007835 }
7836
James Smart895427b2017-02-12 13:52:30 -08007837 if (phba->cfg_nvme_io_channel) {
7838 phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
7839 sizeof(struct lpfc_queue *),
7840 GFP_KERNEL);
7841 if (!phba->sli4_hba.nvme_cq) {
7842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7843 "6077 Failed allocate memory for "
7844 "fast-path CQ record array\n");
7845 goto out_error;
7846 }
7847
7848
7849 phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
7850 sizeof(struct lpfc_queue *),
7851 GFP_KERNEL);
7852 if (!phba->sli4_hba.nvme_wq) {
7853 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7854 "2581 Failed allocate memory for "
7855 "fast-path NVME WQ record array\n");
7856 goto out_error;
7857 }
7858
7859 /*
7860 * Since the first EQ can have multiple CQs associated with it,
7861 * this array is used to quickly see if we have a NVME fast-path
7862 * CQ match.
7863 */
7864 phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
7865 sizeof(uint16_t),
7866 GFP_KERNEL);
7867 if (!phba->sli4_hba.nvme_cq_map) {
7868 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7869 "6078 Failed allocate memory for "
7870 "fast-path CQ map\n");
7871 goto out_error;
7872 }
James Smart67d12732012-08-03 12:36:13 -04007873 }
James Smartda0436e2009-05-22 14:51:39 -04007874
James Smart895427b2017-02-12 13:52:30 -08007875 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04007876
James Smart895427b2017-02-12 13:52:30 -08007877 /* Create HBA Event Queues (EQs) */
7878 for (idx = 0; idx < io_channel; idx++) {
James Smart67d12732012-08-03 12:36:13 -04007879 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04007880 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
7881 phba->sli4_hba.eq_ecount);
7882 if (!qdesc) {
7883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007884 "0497 Failed allocate EQ (%d)\n", idx);
7885 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007886 }
James Smart67d12732012-08-03 12:36:13 -04007887 phba->sli4_hba.hba_eq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007888 }
7889
James Smart895427b2017-02-12 13:52:30 -08007890 /* FCP and NVME io channels are not required to be balanced */
7891
7892 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
7893 if (lpfc_alloc_fcp_wq_cq(phba, idx))
7894 goto out_error;
7895
7896 for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
7897 if (lpfc_alloc_nvme_wq_cq(phba, idx))
7898 goto out_error;
James Smart67d12732012-08-03 12:36:13 -04007899
James Smartda0436e2009-05-22 14:51:39 -04007900 /*
James Smart67d12732012-08-03 12:36:13 -04007901 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007902 */
7903
James Smartda0436e2009-05-22 14:51:39 -04007904 /* Create slow-path Mailbox Command Complete Queue */
7905 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7906 phba->sli4_hba.cq_ecount);
7907 if (!qdesc) {
7908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7909 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007910 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007911 }
7912 phba->sli4_hba.mbx_cq = qdesc;
7913
7914 /* Create slow-path ELS Complete Queue */
7915 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7916 phba->sli4_hba.cq_ecount);
7917 if (!qdesc) {
7918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7919 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007920 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007921 }
7922 phba->sli4_hba.els_cq = qdesc;
7923
James Smartda0436e2009-05-22 14:51:39 -04007924
James Smart5350d872011-10-10 21:33:49 -04007925 /*
James Smart67d12732012-08-03 12:36:13 -04007926 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007927 */
James Smartda0436e2009-05-22 14:51:39 -04007928
7929 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007930
7931 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7932 phba->sli4_hba.mq_ecount);
7933 if (!qdesc) {
7934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7935 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007936 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007937 }
7938 phba->sli4_hba.mbx_wq = qdesc;
7939
7940 /*
James Smart67d12732012-08-03 12:36:13 -04007941 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007942 */
James Smartda0436e2009-05-22 14:51:39 -04007943
7944 /* Create slow-path ELS Work Queue */
7945 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7946 phba->sli4_hba.wq_ecount);
7947 if (!qdesc) {
7948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7949 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007950 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007951 }
7952 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08007953 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7954
7955 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
7956 /* Create NVME LS Complete Queue */
7957 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7958 phba->sli4_hba.cq_ecount);
7959 if (!qdesc) {
7960 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7961 "6079 Failed allocate NVME LS CQ\n");
7962 goto out_error;
7963 }
7964 phba->sli4_hba.nvmels_cq = qdesc;
7965
7966 /* Create NVME LS Work Queue */
7967 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7968 phba->sli4_hba.wq_ecount);
7969 if (!qdesc) {
7970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7971 "6080 Failed allocate NVME LS WQ\n");
7972 goto out_error;
7973 }
7974 phba->sli4_hba.nvmels_wq = qdesc;
7975 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7976 }
James Smartda0436e2009-05-22 14:51:39 -04007977
James Smartda0436e2009-05-22 14:51:39 -04007978 /*
7979 * Create Receive Queue (RQ)
7980 */
James Smartda0436e2009-05-22 14:51:39 -04007981
7982 /* Create Receive Queue for header */
7983 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7984 phba->sli4_hba.rq_ecount);
7985 if (!qdesc) {
7986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7987 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007988 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007989 }
7990 phba->sli4_hba.hdr_rq = qdesc;
7991
7992 /* Create Receive Queue for data */
7993 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7994 phba->sli4_hba.rq_ecount);
7995 if (!qdesc) {
7996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7997 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007998 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007999 }
8000 phba->sli4_hba.dat_rq = qdesc;
8001
James Smart1ba981f2014-02-20 09:56:45 -05008002 /* Create the Queues needed for Flash Optimized Fabric operations */
8003 if (phba->cfg_fof)
8004 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04008005 return 0;
8006
James Smartda0436e2009-05-22 14:51:39 -04008007out_error:
James Smart67d12732012-08-03 12:36:13 -04008008 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04008009 return -ENOMEM;
8010}
8011
James Smart895427b2017-02-12 13:52:30 -08008012static inline void
8013__lpfc_sli4_release_queue(struct lpfc_queue **qp)
8014{
8015 if (*qp != NULL) {
8016 lpfc_sli4_queue_free(*qp);
8017 *qp = NULL;
8018 }
8019}
8020
8021static inline void
8022lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
8023{
8024 int idx;
8025
8026 if (*qs == NULL)
8027 return;
8028
8029 for (idx = 0; idx < max; idx++)
8030 __lpfc_sli4_release_queue(&(*qs)[idx]);
8031
8032 kfree(*qs);
8033 *qs = NULL;
8034}
8035
8036static inline void
8037lpfc_sli4_release_queue_map(uint16_t **qmap)
8038{
8039 if (*qmap != NULL) {
8040 kfree(*qmap);
8041 *qmap = NULL;
8042 }
8043}
8044
James Smartda0436e2009-05-22 14:51:39 -04008045/**
8046 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
8047 * @phba: pointer to lpfc hba data structure.
8048 *
8049 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
8050 * operation.
8051 *
8052 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008053 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008054 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008055 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008056 **/
James Smart5350d872011-10-10 21:33:49 -04008057void
James Smartda0436e2009-05-22 14:51:39 -04008058lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
8059{
James Smart1ba981f2014-02-20 09:56:45 -05008060 if (phba->cfg_fof)
8061 lpfc_fof_queue_destroy(phba);
8062
James Smart895427b2017-02-12 13:52:30 -08008063 /* Release HBA eqs */
8064 lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
James Smart67d12732012-08-03 12:36:13 -04008065
James Smart895427b2017-02-12 13:52:30 -08008066 /* Release FCP cqs */
8067 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
8068 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04008069
James Smart895427b2017-02-12 13:52:30 -08008070 /* Release FCP wqs */
8071 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
8072 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04008073
8074 /* Release FCP CQ mapping array */
James Smart895427b2017-02-12 13:52:30 -08008075 lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
8076
8077 /* Release NVME cqs */
8078 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
8079 phba->cfg_nvme_io_channel);
8080
8081 /* Release NVME wqs */
8082 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
8083 phba->cfg_nvme_io_channel);
8084
8085 /* Release NVME CQ mapping array */
8086 lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
James Smartda0436e2009-05-22 14:51:39 -04008087
8088 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08008089 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04008090
8091 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08008092 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
8093
8094 /* Release ELS work queue */
8095 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04008096
8097 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08008098 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
8099 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04008100
James Smartda0436e2009-05-22 14:51:39 -04008101 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08008102 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
8103
8104 /* Release NVME LS complete queue */
8105 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04008106
8107 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08008108 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
8109
8110 /* Everything on this list has been freed */
8111 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8112}
8113
8114int
8115lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
8116 struct lpfc_queue *drq, int count)
8117{
8118 int rc, i;
8119 struct lpfc_rqe hrqe;
8120 struct lpfc_rqe drqe;
8121 struct lpfc_rqb *rqbp;
8122 struct rqb_dmabuf *rqb_buffer;
8123 LIST_HEAD(rqb_buf_list);
8124
8125 rqbp = hrq->rqbp;
8126 for (i = 0; i < count; i++) {
8127 rqb_buffer = (rqbp->rqb_alloc_buffer)(phba);
8128 if (!rqb_buffer)
8129 break;
8130 rqb_buffer->hrq = hrq;
8131 rqb_buffer->drq = drq;
8132 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
8133 }
8134 while (!list_empty(&rqb_buf_list)) {
8135 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
8136 hbuf.list);
8137
8138 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
8139 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
8140 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
8141 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
8142 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
8143 if (rc < 0) {
8144 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
8145 } else {
8146 list_add_tail(&rqb_buffer->hbuf.list,
8147 &rqbp->rqb_buffer_list);
8148 rqbp->buffer_count++;
8149 }
8150 }
8151 return 1;
8152}
8153
8154int
8155lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
8156{
8157 struct lpfc_rqb *rqbp;
8158 struct lpfc_dmabuf *h_buf;
8159 struct rqb_dmabuf *rqb_buffer;
8160
8161 rqbp = rq->rqbp;
8162 while (!list_empty(&rqbp->rqb_buffer_list)) {
8163 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
8164 struct lpfc_dmabuf, list);
8165
8166 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
8167 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
8168 rqbp->buffer_count--;
8169 }
8170 return 1;
8171}
8172
8173static int
8174lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
8175 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
8176 int qidx, uint32_t qtype)
8177{
8178 struct lpfc_sli_ring *pring;
8179 int rc;
8180
8181 if (!eq || !cq || !wq) {
8182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8183 "6085 Fast-path %s (%d) not allocated\n",
8184 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
8185 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04008186 }
James Smartda0436e2009-05-22 14:51:39 -04008187
James Smart895427b2017-02-12 13:52:30 -08008188 /* create the Cq first */
8189 rc = lpfc_cq_create(phba, cq, eq,
8190 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
8191 if (rc) {
8192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8193 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
8194 qidx, (uint32_t)rc);
8195 return rc;
8196 }
8197
8198 if (qtype != LPFC_MBOX) {
8199 /* Setup nvme_cq_map for fast lookup */
8200 if (cq_map)
8201 *cq_map = cq->queue_id;
8202
8203 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8204 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
8205 qidx, cq->queue_id, qidx, eq->queue_id);
8206
8207 /* create the wq */
8208 rc = lpfc_wq_create(phba, wq, cq, qtype);
8209 if (rc) {
8210 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8211 "6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8212 qidx, (uint32_t)rc);
8213 /* no need to tear down cq - caller will do so */
8214 return rc;
8215 }
8216
8217 /* Bind this CQ/WQ to the NVME ring */
8218 pring = wq->pring;
8219 pring->sli.sli4.wqp = (void *)wq;
8220 cq->pring = pring;
8221
8222 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8223 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8224 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8225 } else {
8226 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8227 if (rc) {
8228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8229 "0539 Failed setup of slow-path MQ: "
8230 "rc = 0x%x\n", rc);
8231 /* no need to tear down cq - caller will do so */
8232 return rc;
8233 }
8234
8235 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8236 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8237 phba->sli4_hba.mbx_wq->queue_id,
8238 phba->sli4_hba.mbx_cq->queue_id);
8239 }
8240
8241 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008242}
8243
8244/**
8245 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8246 * @phba: pointer to lpfc hba data structure.
8247 *
8248 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8249 * operation.
8250 *
8251 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008252 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008253 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008254 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008255 **/
8256int
8257lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8258{
James Smart962bc512013-01-03 15:44:00 -05008259 uint32_t shdr_status, shdr_add_status;
8260 union lpfc_sli4_cfg_shdr *shdr;
8261 LPFC_MBOXQ_t *mboxq;
James Smart895427b2017-02-12 13:52:30 -08008262 int qidx;
8263 uint32_t length, io_channel;
8264 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05008265
8266 /* Check for dual-ULP support */
8267 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8268 if (!mboxq) {
8269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8270 "3249 Unable to allocate memory for "
8271 "QUERY_FW_CFG mailbox command\n");
8272 return -ENOMEM;
8273 }
8274 length = (sizeof(struct lpfc_mbx_query_fw_config) -
8275 sizeof(struct lpfc_sli4_cfg_mhdr));
8276 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8277 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8278 length, LPFC_SLI4_MBX_EMBED);
8279
8280 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8281
8282 shdr = (union lpfc_sli4_cfg_shdr *)
8283 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8284 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8285 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8286 if (shdr_status || shdr_add_status || rc) {
8287 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8288 "3250 QUERY_FW_CFG mailbox failed with status "
8289 "x%x add_status x%x, mbx status x%x\n",
8290 shdr_status, shdr_add_status, rc);
8291 if (rc != MBX_TIMEOUT)
8292 mempool_free(mboxq, phba->mbox_mem_pool);
8293 rc = -ENXIO;
8294 goto out_error;
8295 }
8296
8297 phba->sli4_hba.fw_func_mode =
8298 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8299 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8300 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04008301 phba->sli4_hba.physical_port =
8302 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05008303 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8304 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8305 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8306 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8307
8308 if (rc != MBX_TIMEOUT)
8309 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008310
8311 /*
James Smart67d12732012-08-03 12:36:13 -04008312 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04008313 */
James Smart895427b2017-02-12 13:52:30 -08008314 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04008315
James Smart67d12732012-08-03 12:36:13 -04008316 /* Set up HBA event queue */
James Smart895427b2017-02-12 13:52:30 -08008317 if (io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05008318 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8319 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008320 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04008321 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05008322 }
James Smart895427b2017-02-12 13:52:30 -08008323 for (qidx = 0; qidx < io_channel; qidx++) {
8324 if (!phba->sli4_hba.hba_eq[qidx]) {
James Smartda0436e2009-05-22 14:51:39 -04008325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8326 "0522 Fast-path EQ (%d) not "
James Smart895427b2017-02-12 13:52:30 -08008327 "allocated\n", qidx);
James Smart1b511972011-12-13 13:23:09 -05008328 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008329 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008330 }
James Smart895427b2017-02-12 13:52:30 -08008331 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8332 phba->cfg_fcp_imax);
James Smartda0436e2009-05-22 14:51:39 -04008333 if (rc) {
8334 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8335 "0523 Failed setup of fast-path EQ "
James Smart895427b2017-02-12 13:52:30 -08008336 "(%d), rc = 0x%x\n", qidx,
James Smarta2fc4aef2014-09-03 12:57:55 -04008337 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008338 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008339 }
8340 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008341 "2584 HBA EQ setup: queue[%d]-id=%d\n",
8342 qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04008343 }
8344
James Smart895427b2017-02-12 13:52:30 -08008345 if (phba->cfg_nvme_io_channel) {
8346 if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
James Smart67d12732012-08-03 12:36:13 -04008347 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008348 "6084 Fast-path NVME %s array not allocated\n",
8349 (phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
James Smart67d12732012-08-03 12:36:13 -04008350 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008351 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008352 }
8353
James Smart895427b2017-02-12 13:52:30 -08008354 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8355 rc = lpfc_create_wq_cq(phba,
8356 phba->sli4_hba.hba_eq[
8357 qidx % io_channel],
8358 phba->sli4_hba.nvme_cq[qidx],
8359 phba->sli4_hba.nvme_wq[qidx],
8360 &phba->sli4_hba.nvme_cq_map[qidx],
8361 qidx, LPFC_NVME);
8362 if (rc) {
8363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8364 "6123 Failed to setup fastpath "
8365 "NVME WQ/CQ (%d), rc = 0x%x\n",
8366 qidx, (uint32_t)rc);
8367 goto out_destroy;
8368 }
8369 }
James Smart67d12732012-08-03 12:36:13 -04008370 }
8371
James Smart895427b2017-02-12 13:52:30 -08008372 if (phba->cfg_fcp_io_channel) {
8373 /* Set up fast-path FCP Response Complete Queue */
8374 if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
James Smart67d12732012-08-03 12:36:13 -04008375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008376 "3148 Fast-path FCP %s array not allocated\n",
8377 phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
James Smart67d12732012-08-03 12:36:13 -04008378 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008379 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008380 }
8381
James Smart895427b2017-02-12 13:52:30 -08008382 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8383 rc = lpfc_create_wq_cq(phba,
8384 phba->sli4_hba.hba_eq[
8385 qidx % io_channel],
8386 phba->sli4_hba.fcp_cq[qidx],
8387 phba->sli4_hba.fcp_wq[qidx],
8388 &phba->sli4_hba.fcp_cq_map[qidx],
8389 qidx, LPFC_FCP);
8390 if (rc) {
8391 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8392 "0535 Failed to setup fastpath "
8393 "FCP WQ/CQ (%d), rc = 0x%x\n",
8394 qidx, (uint32_t)rc);
8395 goto out_destroy;
8396 }
8397 }
James Smart67d12732012-08-03 12:36:13 -04008398 }
James Smartda0436e2009-05-22 14:51:39 -04008399
8400 /*
James Smart895427b2017-02-12 13:52:30 -08008401 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008402 */
8403
James Smart895427b2017-02-12 13:52:30 -08008404 /* Set up slow-path MBOX CQ/MQ */
8405
8406 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008408 "0528 %s not allocated\n",
8409 phba->sli4_hba.mbx_cq ?
8410 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05008411 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008412 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008413 }
James Smart895427b2017-02-12 13:52:30 -08008414
8415 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8416 phba->sli4_hba.mbx_cq,
8417 phba->sli4_hba.mbx_wq,
8418 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04008419 if (rc) {
8420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008421 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8422 (uint32_t)rc);
8423 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008424 }
James Smartda0436e2009-05-22 14:51:39 -04008425
James Smart895427b2017-02-12 13:52:30 -08008426 /* Set up slow-path ELS WQ/CQ */
8427 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008429 "0530 ELS %s not allocated\n",
8430 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05008431 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008432 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008433 }
James Smart895427b2017-02-12 13:52:30 -08008434 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8435 phba->sli4_hba.els_cq,
8436 phba->sli4_hba.els_wq,
8437 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04008438 if (rc) {
8439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008440 "0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
8441 (uint32_t)rc);
8442 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008443 }
8444 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8445 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
8446 phba->sli4_hba.els_wq->queue_id,
8447 phba->sli4_hba.els_cq->queue_id);
8448
James Smart895427b2017-02-12 13:52:30 -08008449 if (phba->cfg_nvme_io_channel) {
8450 /* Set up NVME LS Complete Queue */
8451 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
8452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8453 "6091 LS %s not allocated\n",
8454 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
8455 rc = -ENOMEM;
8456 goto out_destroy;
8457 }
8458 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8459 phba->sli4_hba.nvmels_cq,
8460 phba->sli4_hba.nvmels_wq,
8461 NULL, 0, LPFC_NVME_LS);
8462 if (rc) {
8463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8464 "0529 Failed setup of NVVME LS WQ/CQ: "
8465 "rc = 0x%x\n", (uint32_t)rc);
8466 goto out_destroy;
8467 }
8468
8469 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8470 "6096 ELS WQ setup: wq-id=%d, "
8471 "parent cq-id=%d\n",
8472 phba->sli4_hba.nvmels_wq->queue_id,
8473 phba->sli4_hba.nvmels_cq->queue_id);
8474 }
8475
James Smartda0436e2009-05-22 14:51:39 -04008476 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
8477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8478 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008479 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008480 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008481 }
James Smart73d91e52011-10-10 21:32:10 -04008482
8483 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
8484 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
8485
James Smartda0436e2009-05-22 14:51:39 -04008486 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04008487 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04008488 if (rc) {
8489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8490 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04008491 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008492 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008493 }
James Smart73d91e52011-10-10 21:32:10 -04008494
James Smartda0436e2009-05-22 14:51:39 -04008495 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8496 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
8497 "parent cq-id=%d\n",
8498 phba->sli4_hba.hdr_rq->queue_id,
8499 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04008500 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05008501
8502 if (phba->cfg_fof) {
8503 rc = lpfc_fof_queue_setup(phba);
8504 if (rc) {
8505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8506 "0549 Failed setup of FOF Queues: "
8507 "rc = 0x%x\n", rc);
James Smart895427b2017-02-12 13:52:30 -08008508 goto out_destroy;
James Smart1ba981f2014-02-20 09:56:45 -05008509 }
8510 }
James Smart2c9c5a02015-04-07 15:07:15 -04008511
8512 /*
8513 * Configure EQ delay multipier for interrupt coalescing using
8514 * MODIFY_EQ_DELAY for all EQs created, LPFC_MAX_EQ_DELAY at a time.
8515 */
James Smart895427b2017-02-12 13:52:30 -08008516 for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY)
8517 lpfc_modify_hba_eq_delay(phba, qidx);
James Smartda0436e2009-05-22 14:51:39 -04008518 return 0;
8519
James Smart895427b2017-02-12 13:52:30 -08008520out_destroy:
8521 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04008522out_error:
8523 return rc;
8524}
8525
8526/**
8527 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
8528 * @phba: pointer to lpfc hba data structure.
8529 *
8530 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
8531 * operation.
8532 *
8533 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008534 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008535 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008536 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008537 **/
8538void
8539lpfc_sli4_queue_unset(struct lpfc_hba *phba)
8540{
James Smart895427b2017-02-12 13:52:30 -08008541 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04008542
James Smart1ba981f2014-02-20 09:56:45 -05008543 /* Unset the queues created for Flash Optimized Fabric operations */
8544 if (phba->cfg_fof)
8545 lpfc_fof_queue_destroy(phba);
James Smart895427b2017-02-12 13:52:30 -08008546
James Smartda0436e2009-05-22 14:51:39 -04008547 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08008548 if (phba->sli4_hba.mbx_wq)
8549 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
8550
8551 /* Unset NVME LS work queue */
8552 if (phba->sli4_hba.nvmels_wq)
8553 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
8554
James Smartda0436e2009-05-22 14:51:39 -04008555 /* Unset ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08008556 if (phba->sli4_hba.els_cq)
8557 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
8558
James Smartda0436e2009-05-22 14:51:39 -04008559 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08008560 if (phba->sli4_hba.hdr_rq)
8561 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
8562 phba->sli4_hba.dat_rq);
8563
James Smartda0436e2009-05-22 14:51:39 -04008564 /* Unset FCP work queue */
James Smart895427b2017-02-12 13:52:30 -08008565 if (phba->sli4_hba.fcp_wq)
8566 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8567 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
8568
8569 /* Unset NVME work queue */
8570 if (phba->sli4_hba.nvme_wq) {
8571 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8572 lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
James Smart67d12732012-08-03 12:36:13 -04008573 }
James Smart895427b2017-02-12 13:52:30 -08008574
James Smartda0436e2009-05-22 14:51:39 -04008575 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08008576 if (phba->sli4_hba.mbx_cq)
8577 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
8578
James Smartda0436e2009-05-22 14:51:39 -04008579 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08008580 if (phba->sli4_hba.els_cq)
8581 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
8582
8583 /* Unset NVME LS complete queue */
8584 if (phba->sli4_hba.nvmels_cq)
8585 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
8586
8587 /* Unset NVME response complete queue */
8588 if (phba->sli4_hba.nvme_cq)
8589 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8590 lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
8591
James Smartda0436e2009-05-22 14:51:39 -04008592 /* Unset FCP response complete queue */
James Smart895427b2017-02-12 13:52:30 -08008593 if (phba->sli4_hba.fcp_cq)
8594 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8595 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
8596
James Smartda0436e2009-05-22 14:51:39 -04008597 /* Unset fast-path event queue */
James Smart895427b2017-02-12 13:52:30 -08008598 if (phba->sli4_hba.hba_eq)
8599 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
8600 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
James Smartda0436e2009-05-22 14:51:39 -04008601}
8602
8603/**
8604 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
8605 * @phba: pointer to lpfc hba data structure.
8606 *
8607 * This routine is invoked to allocate and set up a pool of completion queue
8608 * events. The body of the completion queue event is a completion queue entry
8609 * CQE. For now, this pool is used for the interrupt service routine to queue
8610 * the following HBA completion queue events for the worker thread to process:
8611 * - Mailbox asynchronous events
8612 * - Receive queue completion unsolicited events
8613 * Later, this can be used for all the slow-path events.
8614 *
8615 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008616 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008617 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04008618 **/
8619static int
8620lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
8621{
8622 struct lpfc_cq_event *cq_event;
8623 int i;
8624
8625 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
8626 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
8627 if (!cq_event)
8628 goto out_pool_create_fail;
8629 list_add_tail(&cq_event->list,
8630 &phba->sli4_hba.sp_cqe_event_pool);
8631 }
8632 return 0;
8633
8634out_pool_create_fail:
8635 lpfc_sli4_cq_event_pool_destroy(phba);
8636 return -ENOMEM;
8637}
8638
8639/**
8640 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
8641 * @phba: pointer to lpfc hba data structure.
8642 *
8643 * This routine is invoked to free the pool of completion queue events at
8644 * driver unload time. Note that, it is the responsibility of the driver
8645 * cleanup routine to free all the outstanding completion-queue events
8646 * allocated from this pool back into the pool before invoking this routine
8647 * to destroy the pool.
8648 **/
8649static void
8650lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
8651{
8652 struct lpfc_cq_event *cq_event, *next_cq_event;
8653
8654 list_for_each_entry_safe(cq_event, next_cq_event,
8655 &phba->sli4_hba.sp_cqe_event_pool, list) {
8656 list_del(&cq_event->list);
8657 kfree(cq_event);
8658 }
8659}
8660
8661/**
8662 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8663 * @phba: pointer to lpfc hba data structure.
8664 *
8665 * This routine is the lock free version of the API invoked to allocate a
8666 * completion-queue event from the free pool.
8667 *
8668 * Return: Pointer to the newly allocated completion-queue event if successful
8669 * NULL otherwise.
8670 **/
8671struct lpfc_cq_event *
8672__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
8673{
8674 struct lpfc_cq_event *cq_event = NULL;
8675
8676 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
8677 struct lpfc_cq_event, list);
8678 return cq_event;
8679}
8680
8681/**
8682 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8683 * @phba: pointer to lpfc hba data structure.
8684 *
8685 * This routine is the lock version of the API invoked to allocate a
8686 * completion-queue event from the free pool.
8687 *
8688 * Return: Pointer to the newly allocated completion-queue event if successful
8689 * NULL otherwise.
8690 **/
8691struct lpfc_cq_event *
8692lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
8693{
8694 struct lpfc_cq_event *cq_event;
8695 unsigned long iflags;
8696
8697 spin_lock_irqsave(&phba->hbalock, iflags);
8698 cq_event = __lpfc_sli4_cq_event_alloc(phba);
8699 spin_unlock_irqrestore(&phba->hbalock, iflags);
8700 return cq_event;
8701}
8702
8703/**
8704 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
8705 * @phba: pointer to lpfc hba data structure.
8706 * @cq_event: pointer to the completion queue event to be freed.
8707 *
8708 * This routine is the lock free version of the API invoked to release a
8709 * completion-queue event back into the free pool.
8710 **/
8711void
8712__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
8713 struct lpfc_cq_event *cq_event)
8714{
8715 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
8716}
8717
8718/**
8719 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
8720 * @phba: pointer to lpfc hba data structure.
8721 * @cq_event: pointer to the completion queue event to be freed.
8722 *
8723 * This routine is the lock version of the API invoked to release a
8724 * completion-queue event back into the free pool.
8725 **/
8726void
8727lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
8728 struct lpfc_cq_event *cq_event)
8729{
8730 unsigned long iflags;
8731 spin_lock_irqsave(&phba->hbalock, iflags);
8732 __lpfc_sli4_cq_event_release(phba, cq_event);
8733 spin_unlock_irqrestore(&phba->hbalock, iflags);
8734}
8735
8736/**
8737 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
8738 * @phba: pointer to lpfc hba data structure.
8739 *
8740 * This routine is to free all the pending completion-queue events to the
8741 * back into the free pool for device reset.
8742 **/
8743static void
8744lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
8745{
8746 LIST_HEAD(cqelist);
8747 struct lpfc_cq_event *cqe;
8748 unsigned long iflags;
8749
8750 /* Retrieve all the pending WCQEs from pending WCQE lists */
8751 spin_lock_irqsave(&phba->hbalock, iflags);
8752 /* Pending FCP XRI abort events */
8753 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8754 &cqelist);
8755 /* Pending ELS XRI abort events */
8756 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8757 &cqelist);
8758 /* Pending asynnc events */
8759 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
8760 &cqelist);
8761 spin_unlock_irqrestore(&phba->hbalock, iflags);
8762
8763 while (!list_empty(&cqelist)) {
8764 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
8765 lpfc_sli4_cq_event_release(phba, cqe);
8766 }
8767}
8768
8769/**
8770 * lpfc_pci_function_reset - Reset pci function.
8771 * @phba: pointer to lpfc hba data structure.
8772 *
8773 * This routine is invoked to request a PCI function reset. It will destroys
8774 * all resources assigned to the PCI function which originates this request.
8775 *
8776 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008777 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008778 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008779 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008780 **/
8781int
8782lpfc_pci_function_reset(struct lpfc_hba *phba)
8783{
8784 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05008785 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04008786 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04008787 uint32_t rdy_chk;
8788 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04008789 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05008790 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05008791 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04008792
James Smart2fcee4b2010-12-15 17:57:46 -05008793 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8794 switch (if_type) {
8795 case LPFC_SLI_INTF_IF_TYPE_0:
8796 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8797 GFP_KERNEL);
8798 if (!mboxq) {
8799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8800 "0494 Unable to allocate memory for "
8801 "issuing SLI_FUNCTION_RESET mailbox "
8802 "command\n");
8803 return -ENOMEM;
8804 }
8805
8806 /* Setup PCI function reset mailbox-ioctl command */
8807 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8808 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
8809 LPFC_SLI4_MBX_EMBED);
8810 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8811 shdr = (union lpfc_sli4_cfg_shdr *)
8812 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8813 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8814 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
8815 &shdr->response);
8816 if (rc != MBX_TIMEOUT)
8817 mempool_free(mboxq, phba->mbox_mem_pool);
8818 if (shdr_status || shdr_add_status || rc) {
8819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8820 "0495 SLI_FUNCTION_RESET mailbox "
8821 "failed with status x%x add_status x%x,"
8822 " mbx status x%x\n",
8823 shdr_status, shdr_add_status, rc);
8824 rc = -ENXIO;
8825 }
8826 break;
8827 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2f6fa2c2014-09-03 12:57:08 -04008828wait:
8829 /*
8830 * Poll the Port Status Register and wait for RDY for
8831 * up to 30 seconds. If the port doesn't respond, treat
8832 * it as an error.
8833 */
James Smart77d093f2015-04-07 15:07:08 -04008834 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -04008835 if (lpfc_readl(phba->sli4_hba.u.if_type2.
8836 STATUSregaddr, &reg_data.word0)) {
8837 rc = -ENODEV;
8838 goto out;
8839 }
8840 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
8841 break;
8842 msleep(20);
8843 }
8844
8845 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
8846 phba->work_status[0] = readl(
8847 phba->sli4_hba.u.if_type2.ERR1regaddr);
8848 phba->work_status[1] = readl(
8849 phba->sli4_hba.u.if_type2.ERR2regaddr);
8850 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8851 "2890 Port not ready, port status reg "
8852 "0x%x error 1=0x%x, error 2=0x%x\n",
8853 reg_data.word0,
8854 phba->work_status[0],
8855 phba->work_status[1]);
8856 rc = -ENODEV;
8857 goto out;
8858 }
8859
8860 if (!port_reset) {
8861 /*
8862 * Reset the port now
8863 */
James Smart2fcee4b2010-12-15 17:57:46 -05008864 reg_data.word0 = 0;
8865 bf_set(lpfc_sliport_ctrl_end, &reg_data,
8866 LPFC_SLIPORT_LITTLE_ENDIAN);
8867 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
8868 LPFC_SLIPORT_INIT_PORT);
8869 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
8870 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05008871 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05008872 pci_read_config_word(phba->pcidev,
8873 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05008874
James Smart2f6fa2c2014-09-03 12:57:08 -04008875 port_reset = 1;
8876 msleep(20);
8877 goto wait;
8878 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
8879 rc = -ENODEV;
8880 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -05008881 }
8882 break;
James Smart2f6fa2c2014-09-03 12:57:08 -04008883
James Smart2fcee4b2010-12-15 17:57:46 -05008884 case LPFC_SLI_INTF_IF_TYPE_1:
8885 default:
8886 break;
James Smartda0436e2009-05-22 14:51:39 -04008887 }
8888
James Smart73d91e52011-10-10 21:32:10 -04008889out:
James Smart2fcee4b2010-12-15 17:57:46 -05008890 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -04008891 if (rc) {
James Smart229adb02013-04-17 20:16:51 -04008892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8893 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -04008894 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008895 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04008896 }
James Smart2fcee4b2010-12-15 17:57:46 -05008897
James Smartda0436e2009-05-22 14:51:39 -04008898 return rc;
8899}
8900
8901/**
James Smartda0436e2009-05-22 14:51:39 -04008902 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
8903 * @phba: pointer to lpfc hba data structure.
8904 *
8905 * This routine is invoked to set up the PCI device memory space for device
8906 * with SLI-4 interface spec.
8907 *
8908 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008909 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008910 * other values - error
8911 **/
8912static int
8913lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
8914{
8915 struct pci_dev *pdev;
8916 unsigned long bar0map_len, bar1map_len, bar2map_len;
8917 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05008918 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04008919
8920 /* Obtain PCI device reference */
8921 if (!phba->pcidev)
8922 return error;
8923 else
8924 pdev = phba->pcidev;
8925
8926 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05008927 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
8928 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
8929 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
8930 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04008931 return error;
Michael Reed8e685972009-09-18 12:02:05 -05008932 }
8933 }
James Smartda0436e2009-05-22 14:51:39 -04008934
James Smart2fcee4b2010-12-15 17:57:46 -05008935 /*
8936 * The BARs and register set definitions and offset locations are
8937 * dependent on the if_type.
8938 */
8939 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
8940 &phba->sli4_hba.sli_intf.word0)) {
8941 return error;
8942 }
8943
8944 /* There is no SLI3 failback for SLI4 devices. */
8945 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
8946 LPFC_SLI_INTF_VALID) {
8947 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8948 "2894 SLI_INTF reg contents invalid "
8949 "sli_intf reg 0x%x\n",
8950 phba->sli4_hba.sli_intf.word0);
8951 return error;
8952 }
8953
8954 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8955 /*
8956 * Get the bus address of SLI4 device Bar regions and the
8957 * number of bytes required by each mapping. The mapping of the
8958 * particular PCI BARs regions is dependent on the type of
8959 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04008960 */
James Smartf5ca6f22013-09-06 12:21:09 -04008961 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
8962 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
8963 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05008964
8965 /*
8966 * Map SLI4 PCI Config Space Register base to a kernel virtual
8967 * addr
8968 */
8969 phba->sli4_hba.conf_regs_memmap_p =
8970 ioremap(phba->pci_bar0_map, bar0map_len);
8971 if (!phba->sli4_hba.conf_regs_memmap_p) {
8972 dev_printk(KERN_ERR, &pdev->dev,
8973 "ioremap failed for SLI4 PCI config "
8974 "registers.\n");
8975 goto out;
8976 }
James Smartf5ca6f22013-09-06 12:21:09 -04008977 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008978 /* Set up BAR0 PCI config space register memory map */
8979 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05008980 } else {
8981 phba->pci_bar0_map = pci_resource_start(pdev, 1);
8982 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05008983 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8984 dev_printk(KERN_ERR, &pdev->dev,
8985 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
8986 goto out;
8987 }
8988 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008989 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008990 if (!phba->sli4_hba.conf_regs_memmap_p) {
8991 dev_printk(KERN_ERR, &pdev->dev,
8992 "ioremap failed for SLI4 PCI config "
8993 "registers.\n");
8994 goto out;
8995 }
8996 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04008997 }
8998
James Smartc31098c2011-04-16 11:03:33 -04008999 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04009000 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05009001 /*
9002 * Map SLI4 if type 0 HBA Control Register base to a kernel
9003 * virtual address and setup the registers.
9004 */
James Smartf5ca6f22013-09-06 12:21:09 -04009005 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
9006 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05009007 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009008 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009009 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
9010 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04009011 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009012 goto out_iounmap_conf;
9013 }
James Smartf5ca6f22013-09-06 12:21:09 -04009014 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009015 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04009016 }
9017
James Smartc31098c2011-04-16 11:03:33 -04009018 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04009019 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05009020 /*
9021 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
9022 * virtual address and setup the registers.
9023 */
James Smartf5ca6f22013-09-06 12:21:09 -04009024 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
9025 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05009026 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009027 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009028 if (!phba->sli4_hba.drbl_regs_memmap_p) {
9029 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04009030 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009031 goto out_iounmap_ctrl;
9032 }
James Smartf5ca6f22013-09-06 12:21:09 -04009033 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009034 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
9035 if (error)
9036 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04009037 }
9038
James Smartda0436e2009-05-22 14:51:39 -04009039 return 0;
9040
9041out_iounmap_all:
9042 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9043out_iounmap_ctrl:
9044 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9045out_iounmap_conf:
9046 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9047out:
9048 return error;
9049}
9050
9051/**
9052 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
9053 * @phba: pointer to lpfc hba data structure.
9054 *
9055 * This routine is invoked to unset the PCI device memory space for device
9056 * with SLI-4 interface spec.
9057 **/
9058static void
9059lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
9060{
James Smart2e90f4b2011-12-13 13:22:37 -05009061 uint32_t if_type;
9062 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04009063
James Smart2e90f4b2011-12-13 13:22:37 -05009064 switch (if_type) {
9065 case LPFC_SLI_INTF_IF_TYPE_0:
9066 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9067 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9068 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9069 break;
9070 case LPFC_SLI_INTF_IF_TYPE_2:
9071 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9072 break;
9073 case LPFC_SLI_INTF_IF_TYPE_1:
9074 default:
9075 dev_printk(KERN_ERR, &phba->pcidev->dev,
9076 "FATAL - unsupported SLI4 interface type - %d\n",
9077 if_type);
9078 break;
9079 }
James Smartda0436e2009-05-22 14:51:39 -04009080}
9081
9082/**
James Smart3772a992009-05-22 14:50:54 -04009083 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
9084 * @phba: pointer to lpfc hba data structure.
9085 *
9086 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009087 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -04009088 *
9089 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009090 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009091 * other values - error
9092 **/
9093static int
9094lpfc_sli_enable_msix(struct lpfc_hba *phba)
9095{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009096 int rc;
James Smart3772a992009-05-22 14:50:54 -04009097 LPFC_MBOXQ_t *pmb;
9098
9099 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009100 rc = pci_alloc_irq_vectors(phba->pcidev,
9101 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
9102 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -04009103 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9104 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009105 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -04009106 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009107
James Smart3772a992009-05-22 14:50:54 -04009108 /*
9109 * Assign MSI-X vectors to interrupt handlers
9110 */
9111
9112 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009113 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -04009114 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04009115 LPFC_SP_DRIVER_HANDLER_NAME, phba);
9116 if (rc) {
9117 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9118 "0421 MSI-X slow-path request_irq failed "
9119 "(%d)\n", rc);
9120 goto msi_fail_out;
9121 }
9122
9123 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009124 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -04009125 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04009126 LPFC_FP_DRIVER_HANDLER_NAME, phba);
9127
9128 if (rc) {
9129 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9130 "0429 MSI-X fast-path request_irq failed "
9131 "(%d)\n", rc);
9132 goto irq_fail_out;
9133 }
9134
9135 /*
9136 * Configure HBA MSI-X attention conditions to messages
9137 */
9138 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9139
9140 if (!pmb) {
9141 rc = -ENOMEM;
9142 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9143 "0474 Unable to allocate memory for issuing "
9144 "MBOX_CONFIG_MSI command\n");
9145 goto mem_fail_out;
9146 }
9147 rc = lpfc_config_msi(phba, pmb);
9148 if (rc)
9149 goto mbx_fail_out;
9150 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9151 if (rc != MBX_SUCCESS) {
9152 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
9153 "0351 Config MSI mailbox command failed, "
9154 "mbxCmd x%x, mbxStatus x%x\n",
9155 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
9156 goto mbx_fail_out;
9157 }
9158
9159 /* Free memory allocated for mailbox command */
9160 mempool_free(pmb, phba->mbox_mem_pool);
9161 return rc;
9162
9163mbx_fail_out:
9164 /* Free memory allocated for mailbox command */
9165 mempool_free(pmb, phba->mbox_mem_pool);
9166
9167mem_fail_out:
9168 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009169 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -04009170
9171irq_fail_out:
9172 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009173 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -04009174
9175msi_fail_out:
9176 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009177 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009178
9179vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -04009180 return rc;
9181}
9182
9183/**
James Smart3772a992009-05-22 14:50:54 -04009184 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
9185 * @phba: pointer to lpfc hba data structure.
9186 *
9187 * This routine is invoked to enable the MSI interrupt mode to device with
9188 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
9189 * enable the MSI vector. The device driver is responsible for calling the
9190 * request_irq() to register MSI vector with a interrupt the handler, which
9191 * is done in this function.
9192 *
9193 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009194 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009195 * other values - error
9196 */
9197static int
9198lpfc_sli_enable_msi(struct lpfc_hba *phba)
9199{
9200 int rc;
9201
9202 rc = pci_enable_msi(phba->pcidev);
9203 if (!rc)
9204 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9205 "0462 PCI enable MSI mode success.\n");
9206 else {
9207 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9208 "0471 PCI enable MSI mode failed (%d)\n", rc);
9209 return rc;
9210 }
9211
9212 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009213 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -04009214 if (rc) {
9215 pci_disable_msi(phba->pcidev);
9216 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9217 "0478 MSI request_irq failed (%d)\n", rc);
9218 }
9219 return rc;
9220}
9221
9222/**
James Smart3772a992009-05-22 14:50:54 -04009223 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9224 * @phba: pointer to lpfc hba data structure.
9225 *
9226 * This routine is invoked to enable device interrupt and associate driver's
9227 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9228 * spec. Depends on the interrupt mode configured to the driver, the driver
9229 * will try to fallback from the configured interrupt mode to an interrupt
9230 * mode which is supported by the platform, kernel, and device in the order
9231 * of:
9232 * MSI-X -> MSI -> IRQ.
9233 *
9234 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009235 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009236 * other values - error
9237 **/
9238static uint32_t
9239lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9240{
9241 uint32_t intr_mode = LPFC_INTR_ERROR;
9242 int retval;
9243
9244 if (cfg_mode == 2) {
9245 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9246 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9247 if (!retval) {
9248 /* Now, try to enable MSI-X interrupt mode */
9249 retval = lpfc_sli_enable_msix(phba);
9250 if (!retval) {
9251 /* Indicate initialization to MSI-X mode */
9252 phba->intr_type = MSIX;
9253 intr_mode = 2;
9254 }
9255 }
9256 }
9257
9258 /* Fallback to MSI if MSI-X initialization failed */
9259 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9260 retval = lpfc_sli_enable_msi(phba);
9261 if (!retval) {
9262 /* Indicate initialization to MSI mode */
9263 phba->intr_type = MSI;
9264 intr_mode = 1;
9265 }
9266 }
9267
9268 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9269 if (phba->intr_type == NONE) {
9270 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9271 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9272 if (!retval) {
9273 /* Indicate initialization to INTx mode */
9274 phba->intr_type = INTx;
9275 intr_mode = 0;
9276 }
9277 }
9278 return intr_mode;
9279}
9280
9281/**
9282 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9283 * @phba: pointer to lpfc hba data structure.
9284 *
9285 * This routine is invoked to disable device interrupt and disassociate the
9286 * driver's interrupt handler(s) from interrupt vector(s) to device with
9287 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9288 * release the interrupt vector(s) for the message signaled interrupt.
9289 **/
9290static void
9291lpfc_sli_disable_intr(struct lpfc_hba *phba)
9292{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009293 int nr_irqs, i;
9294
James Smart3772a992009-05-22 14:50:54 -04009295 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009296 nr_irqs = LPFC_MSIX_VECTORS;
9297 else
9298 nr_irqs = 1;
9299
9300 for (i = 0; i < nr_irqs; i++)
9301 free_irq(pci_irq_vector(phba->pcidev, i), phba);
9302 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -04009303
9304 /* Reset interrupt management states */
9305 phba->intr_type = NONE;
9306 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -04009307}
9308
9309/**
James Smart895427b2017-02-12 13:52:30 -08009310 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -04009311 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08009312 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -04009313 *
James Smart895427b2017-02-12 13:52:30 -08009314 * The routine will figure out the CPU affinity assignment for every
9315 * MSI-X vector allocated for the HBA. The hba_eq_hdl will be updated
9316 * with a pointer to the CPU mask that defines ALL the CPUs this vector
9317 * can be associated with. If the vector can be unquely associated with
9318 * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
9319 * In addition, the CPU to IO channel mapping will be calculated
9320 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -04009321 */
James Smart895427b2017-02-12 13:52:30 -08009322static void
9323lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -04009324{
9325 struct lpfc_vector_map_info *cpup;
James Smart895427b2017-02-12 13:52:30 -08009326 int index = 0;
9327 int vec = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009328 int cpu;
James Smart7bb03bb2013-04-17 20:19:16 -04009329#ifdef CONFIG_X86
9330 struct cpuinfo_x86 *cpuinfo;
9331#endif
James Smart7bb03bb2013-04-17 20:19:16 -04009332
9333 /* Init cpu_map array */
9334 memset(phba->sli4_hba.cpu_map, 0xff,
9335 (sizeof(struct lpfc_vector_map_info) *
James Smart895427b2017-02-12 13:52:30 -08009336 phba->sli4_hba.num_present_cpu));
James Smart7bb03bb2013-04-17 20:19:16 -04009337
9338 /* Update CPU map with physical id and core id of each CPU */
9339 cpup = phba->sli4_hba.cpu_map;
9340 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
9341#ifdef CONFIG_X86
9342 cpuinfo = &cpu_data(cpu);
9343 cpup->phys_id = cpuinfo->phys_proc_id;
9344 cpup->core_id = cpuinfo->cpu_core_id;
9345#else
9346 /* No distinction between CPUs for other platforms */
9347 cpup->phys_id = 0;
9348 cpup->core_id = 0;
9349#endif
James Smart895427b2017-02-12 13:52:30 -08009350 cpup->channel_id = index; /* For now round robin */
9351 cpup->irq = pci_irq_vector(phba->pcidev, vec);
9352 vec++;
9353 if (vec >= vectors)
9354 vec = 0;
9355 index++;
9356 if (index >= phba->cfg_fcp_io_channel)
9357 index = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009358 cpup++;
9359 }
James Smart7bb03bb2013-04-17 20:19:16 -04009360}
9361
9362
9363/**
James Smartda0436e2009-05-22 14:51:39 -04009364 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
9365 * @phba: pointer to lpfc hba data structure.
9366 *
9367 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009368 * with SLI-4 interface spec.
James Smartda0436e2009-05-22 14:51:39 -04009369 *
9370 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009371 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009372 * other values - error
9373 **/
9374static int
9375lpfc_sli4_enable_msix(struct lpfc_hba *phba)
9376{
James Smart75baf692010-06-08 18:31:21 -04009377 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04009378
9379 /* Set up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -08009380 vectors = phba->io_channel_irqs;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009381 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05009382 vectors++;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009383
James Smartf358dd02017-02-12 13:52:34 -08009384 rc = pci_alloc_irq_vectors(phba->pcidev,
9385 (phba->nvmet_support) ? 1 : 2,
9386 vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009387 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -04009388 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9389 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009390 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -04009391 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009392 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -04009393
James Smart7bb03bb2013-04-17 20:19:16 -04009394 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04009395 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04009396 memset(&phba->sli4_hba.handler_name[index], 0, 16);
James Smarta2fc4aef2014-09-03 12:57:55 -04009397 snprintf((char *)&phba->sli4_hba.handler_name[index],
9398 LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -04009399 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04009400
James Smart895427b2017-02-12 13:52:30 -08009401 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9402 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9403 atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009404 if (phba->cfg_fof && (index == (vectors - 1)))
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009405 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009406 &lpfc_sli4_fof_intr_handler, 0,
James Smart1ba981f2014-02-20 09:56:45 -05009407 (char *)&phba->sli4_hba.handler_name[index],
James Smart895427b2017-02-12 13:52:30 -08009408 &phba->sli4_hba.hba_eq_hdl[index]);
James Smart1ba981f2014-02-20 09:56:45 -05009409 else
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009410 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009411 &lpfc_sli4_hba_intr_handler, 0,
James Smart4305f182012-08-03 12:36:33 -04009412 (char *)&phba->sli4_hba.handler_name[index],
James Smart895427b2017-02-12 13:52:30 -08009413 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009414 if (rc) {
9415 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9416 "0486 MSI-X fast-path (%d) "
9417 "request_irq failed (%d)\n", index, rc);
9418 goto cfg_fail_out;
9419 }
9420 }
9421
James Smart1ba981f2014-02-20 09:56:45 -05009422 if (phba->cfg_fof)
9423 vectors--;
9424
James Smart895427b2017-02-12 13:52:30 -08009425 if (vectors != phba->io_channel_irqs) {
James Smart82c3e9b2012-09-29 11:29:50 -04009426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9427 "3238 Reducing IO channels to match number of "
9428 "MSI-X vectors, requested %d got %d\n",
James Smart895427b2017-02-12 13:52:30 -08009429 phba->io_channel_irqs, vectors);
9430 if (phba->cfg_fcp_io_channel > vectors)
9431 phba->cfg_fcp_io_channel = vectors;
9432 if (phba->cfg_nvme_io_channel > vectors)
9433 phba->cfg_nvme_io_channel = vectors;
9434 if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
9435 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
9436 else
9437 phba->io_channel_irqs = phba->cfg_nvme_io_channel;
James Smart82c3e9b2012-09-29 11:29:50 -04009438 }
James Smart895427b2017-02-12 13:52:30 -08009439 lpfc_cpu_affinity_check(phba, vectors);
James Smart7bb03bb2013-04-17 20:19:16 -04009440
James Smartda0436e2009-05-22 14:51:39 -04009441 return rc;
9442
9443cfg_fail_out:
9444 /* free the irq already requested */
James Smart895427b2017-02-12 13:52:30 -08009445 for (--index; index >= 0; index--)
9446 free_irq(pci_irq_vector(phba->pcidev, index),
9447 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009448
James Smartda0436e2009-05-22 14:51:39 -04009449 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009450 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009451
9452vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -04009453 return rc;
9454}
9455
9456/**
James Smartda0436e2009-05-22 14:51:39 -04009457 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
9458 * @phba: pointer to lpfc hba data structure.
9459 *
9460 * This routine is invoked to enable the MSI interrupt mode to device with
9461 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
9462 * to enable the MSI vector. The device driver is responsible for calling
9463 * the request_irq() to register MSI vector with a interrupt the handler,
9464 * which is done in this function.
9465 *
9466 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009467 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009468 * other values - error
9469 **/
9470static int
9471lpfc_sli4_enable_msi(struct lpfc_hba *phba)
9472{
9473 int rc, index;
9474
9475 rc = pci_enable_msi(phba->pcidev);
9476 if (!rc)
9477 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9478 "0487 PCI enable MSI mode success.\n");
9479 else {
9480 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9481 "0488 PCI enable MSI mode failed (%d)\n", rc);
9482 return rc;
9483 }
9484
9485 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009486 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04009487 if (rc) {
9488 pci_disable_msi(phba->pcidev);
9489 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9490 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04009491 return rc;
James Smartda0436e2009-05-22 14:51:39 -04009492 }
9493
James Smart895427b2017-02-12 13:52:30 -08009494 for (index = 0; index < phba->io_channel_irqs; index++) {
9495 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9496 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smartda0436e2009-05-22 14:51:39 -04009497 }
9498
James Smart1ba981f2014-02-20 09:56:45 -05009499 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -08009500 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9501 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smart1ba981f2014-02-20 09:56:45 -05009502 }
James Smart75baf692010-06-08 18:31:21 -04009503 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009504}
9505
9506/**
James Smartda0436e2009-05-22 14:51:39 -04009507 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
9508 * @phba: pointer to lpfc hba data structure.
9509 *
9510 * This routine is invoked to enable device interrupt and associate driver's
9511 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
9512 * interface spec. Depends on the interrupt mode configured to the driver,
9513 * the driver will try to fallback from the configured interrupt mode to an
9514 * interrupt mode which is supported by the platform, kernel, and device in
9515 * the order of:
9516 * MSI-X -> MSI -> IRQ.
9517 *
9518 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009519 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009520 * other values - error
9521 **/
9522static uint32_t
9523lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9524{
9525 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -08009526 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -04009527
9528 if (cfg_mode == 2) {
9529 /* Preparation before conf_msi mbox cmd */
9530 retval = 0;
9531 if (!retval) {
9532 /* Now, try to enable MSI-X interrupt mode */
9533 retval = lpfc_sli4_enable_msix(phba);
9534 if (!retval) {
9535 /* Indicate initialization to MSI-X mode */
9536 phba->intr_type = MSIX;
9537 intr_mode = 2;
9538 }
9539 }
9540 }
9541
9542 /* Fallback to MSI if MSI-X initialization failed */
9543 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9544 retval = lpfc_sli4_enable_msi(phba);
9545 if (!retval) {
9546 /* Indicate initialization to MSI mode */
9547 phba->intr_type = MSI;
9548 intr_mode = 1;
9549 }
9550 }
9551
9552 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9553 if (phba->intr_type == NONE) {
9554 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9555 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9556 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -08009557 struct lpfc_hba_eq_hdl *eqhdl;
9558
James Smartda0436e2009-05-22 14:51:39 -04009559 /* Indicate initialization to INTx mode */
9560 phba->intr_type = INTx;
9561 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -08009562
9563 for (idx = 0; idx < phba->io_channel_irqs; idx++) {
9564 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9565 eqhdl->idx = idx;
9566 eqhdl->phba = phba;
9567 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04009568 }
James Smart1ba981f2014-02-20 09:56:45 -05009569 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -08009570 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9571 eqhdl->idx = idx;
9572 eqhdl->phba = phba;
9573 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009574 }
James Smartda0436e2009-05-22 14:51:39 -04009575 }
9576 }
9577 return intr_mode;
9578}
9579
9580/**
9581 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9582 * @phba: pointer to lpfc hba data structure.
9583 *
9584 * This routine is invoked to disable device interrupt and disassociate
9585 * the driver's interrupt handler(s) from interrupt vector(s) to device
9586 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9587 * will release the interrupt vector(s) for the message signaled interrupt.
9588 **/
9589static void
9590lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9591{
9592 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009593 if (phba->intr_type == MSIX) {
9594 int index;
9595
9596 /* Free up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -08009597 for (index = 0; index < phba->io_channel_irqs; index++)
9598 free_irq(pci_irq_vector(phba->pcidev, index),
9599 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009600
9601 if (phba->cfg_fof)
James Smart895427b2017-02-12 13:52:30 -08009602 free_irq(pci_irq_vector(phba->pcidev, index),
9603 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009604 } else {
James Smartda0436e2009-05-22 14:51:39 -04009605 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009606 }
9607
9608 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -04009609
9610 /* Reset interrupt management states */
9611 phba->intr_type = NONE;
9612 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -04009613}
9614
9615/**
James Smart3772a992009-05-22 14:50:54 -04009616 * lpfc_unset_hba - Unset SLI3 hba device initialization
9617 * @phba: pointer to lpfc hba data structure.
9618 *
9619 * This routine is invoked to unset the HBA device initialization steps to
9620 * a device with SLI-3 interface spec.
9621 **/
9622static void
9623lpfc_unset_hba(struct lpfc_hba *phba)
9624{
9625 struct lpfc_vport *vport = phba->pport;
9626 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9627
9628 spin_lock_irq(shost->host_lock);
9629 vport->load_flag |= FC_UNLOADING;
9630 spin_unlock_irq(shost->host_lock);
9631
James Smart72859902012-01-18 16:25:38 -05009632 kfree(phba->vpi_bmask);
9633 kfree(phba->vpi_ids);
9634
James Smart3772a992009-05-22 14:50:54 -04009635 lpfc_stop_hba_timers(phba);
9636
9637 phba->pport->work_port_events = 0;
9638
9639 lpfc_sli_hba_down(phba);
9640
9641 lpfc_sli_brdrestart(phba);
9642
9643 lpfc_sli_disable_intr(phba);
9644
9645 return;
9646}
9647
9648/**
James Smart5af5eee2010-10-22 11:06:38 -04009649 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9650 * @phba: Pointer to HBA context object.
9651 *
9652 * This function is called in the SLI4 code path to wait for completion
9653 * of device's XRIs exchange busy. It will check the XRI exchange busy
9654 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9655 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9656 * I/Os every 30 seconds, log error message, and wait forever. Only when
9657 * all XRI exchange busy complete, the driver unload shall proceed with
9658 * invoking the function reset ioctl mailbox command to the CNA and the
9659 * the rest of the driver unload resource release.
9660 **/
9661static void
9662lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9663{
9664 int wait_time = 0;
James Smart895427b2017-02-12 13:52:30 -08009665 int nvme_xri_cmpl = 1;
9666 int fcp_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -04009667 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08009668 int nvmet_xri_cmpl =
9669 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_sgl_list);
James Smart5af5eee2010-10-22 11:06:38 -04009670
James Smart895427b2017-02-12 13:52:30 -08009671 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
9672 fcp_xri_cmpl =
9673 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9674 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
9675 nvme_xri_cmpl =
9676 list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
9677
James Smartf358dd02017-02-12 13:52:34 -08009678 while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl ||
9679 !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -04009680 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart895427b2017-02-12 13:52:30 -08009681 if (!nvme_xri_cmpl)
9682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9683 "6100 NVME XRI exchange busy "
9684 "wait time: %d seconds.\n",
9685 wait_time/1000);
James Smart5af5eee2010-10-22 11:06:38 -04009686 if (!fcp_xri_cmpl)
9687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9688 "2877 FCP XRI exchange busy "
9689 "wait time: %d seconds.\n",
9690 wait_time/1000);
9691 if (!els_xri_cmpl)
9692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9693 "2878 ELS XRI exchange busy "
9694 "wait time: %d seconds.\n",
9695 wait_time/1000);
9696 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9697 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9698 } else {
9699 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9700 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9701 }
James Smart895427b2017-02-12 13:52:30 -08009702 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
9703 nvme_xri_cmpl = list_empty(
9704 &phba->sli4_hba.lpfc_abts_nvme_buf_list);
9705
9706 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
9707 fcp_xri_cmpl = list_empty(
9708 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
9709
James Smart5af5eee2010-10-22 11:06:38 -04009710 els_xri_cmpl =
9711 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08009712
9713 nvmet_xri_cmpl =
9714 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_sgl_list);
James Smart5af5eee2010-10-22 11:06:38 -04009715 }
9716}
9717
9718/**
James Smartda0436e2009-05-22 14:51:39 -04009719 * lpfc_sli4_hba_unset - Unset the fcoe hba
9720 * @phba: Pointer to HBA context object.
9721 *
9722 * This function is called in the SLI4 code path to reset the HBA's FCoE
9723 * function. The caller is not required to hold any lock. This routine
9724 * issues PCI function reset mailbox command to reset the FCoE function.
9725 * At the end of the function, it calls lpfc_hba_down_post function to
9726 * free any pending commands.
9727 **/
9728static void
9729lpfc_sli4_hba_unset(struct lpfc_hba *phba)
9730{
9731 int wait_cnt = 0;
9732 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04009733 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04009734
9735 lpfc_stop_hba_timers(phba);
9736 phba->sli4_hba.intr_enable = 0;
9737
9738 /*
9739 * Gracefully wait out the potential current outstanding asynchronous
9740 * mailbox command.
9741 */
9742
9743 /* First, block any pending async mailbox command from posted */
9744 spin_lock_irq(&phba->hbalock);
9745 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9746 spin_unlock_irq(&phba->hbalock);
9747 /* Now, trying to wait it out if we can */
9748 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9749 msleep(10);
9750 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9751 break;
9752 }
9753 /* Forcefully release the outstanding mailbox command if timed out */
9754 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9755 spin_lock_irq(&phba->hbalock);
9756 mboxq = phba->sli.mbox_active;
9757 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9758 __lpfc_mbox_cmpl_put(phba, mboxq);
9759 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9760 phba->sli.mbox_active = NULL;
9761 spin_unlock_irq(&phba->hbalock);
9762 }
9763
James Smart5af5eee2010-10-22 11:06:38 -04009764 /* Abort all iocbs associated with the hba */
9765 lpfc_sli_hba_iocb_abort(phba);
9766
9767 /* Wait for completion of device XRI exchange busy */
9768 lpfc_sli4_xri_exchange_busy_wait(phba);
9769
James Smartda0436e2009-05-22 14:51:39 -04009770 /* Disable PCI subsystem interrupt */
9771 lpfc_sli4_disable_intr(phba);
9772
James Smart912e3ac2011-05-24 11:42:11 -04009773 /* Disable SR-IOV if enabled */
9774 if (phba->cfg_sriov_nr_virtfn)
9775 pci_disable_sriov(pdev);
9776
James Smartda0436e2009-05-22 14:51:39 -04009777 /* Stop kthread signal shall trigger work_done one more time */
9778 kthread_stop(phba->worker_thread);
9779
James Smart3677a3a2010-09-29 11:19:14 -04009780 /* Reset SLI4 HBA FCoE function */
9781 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009782 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009783
James Smartda0436e2009-05-22 14:51:39 -04009784 /* Stop the SLI4 device port */
9785 phba->pport->work_port_events = 0;
9786}
9787
James Smart28baac72010-02-12 14:42:03 -05009788 /**
9789 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9790 * @phba: Pointer to HBA context object.
9791 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9792 *
9793 * This function is called in the SLI4 code path to read the port's
9794 * sli4 capabilities.
9795 *
9796 * This function may be be called from any context that can block-wait
9797 * for the completion. The expectation is that this routine is called
9798 * typically from probe_one or from the online routine.
9799 **/
9800int
9801lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9802{
9803 int rc;
9804 struct lpfc_mqe *mqe;
9805 struct lpfc_pc_sli4_params *sli4_params;
9806 uint32_t mbox_tmo;
9807
9808 rc = 0;
9809 mqe = &mboxq->u.mqe;
9810
9811 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009812 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009813 if (!phba->sli4_hba.intr_enable)
9814 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9815 else {
James Smarta183a152011-10-10 21:32:43 -04009816 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009817 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9818 }
9819
9820 if (unlikely(rc))
9821 return 1;
9822
9823 sli4_params = &phba->sli4_hba.pc_sli4_params;
9824 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9825 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9826 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9827 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9828 &mqe->un.sli4_params);
9829 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9830 &mqe->un.sli4_params);
9831 sli4_params->proto_types = mqe->un.sli4_params.word3;
9832 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9833 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9834 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9835 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9836 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9837 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9838 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9839 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9840 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9841 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9842 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9843 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9844 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9845 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9846 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9847 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9848 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9849 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9850 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9851 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009852
9853 /* Make sure that sge_supp_len can be handled by the driver */
9854 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9855 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9856
James Smart28baac72010-02-12 14:42:03 -05009857 return rc;
9858}
9859
James Smartda0436e2009-05-22 14:51:39 -04009860/**
James Smartfedd3b72011-02-16 12:39:24 -05009861 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9862 * @phba: Pointer to HBA context object.
9863 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9864 *
9865 * This function is called in the SLI4 code path to read the port's
9866 * sli4 capabilities.
9867 *
9868 * This function may be be called from any context that can block-wait
9869 * for the completion. The expectation is that this routine is called
9870 * typically from probe_one or from the online routine.
9871 **/
9872int
9873lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9874{
9875 int rc;
9876 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9877 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009878 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009879 int length;
9880 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9881
James Smart6d368e52011-05-24 11:44:12 -04009882 /*
9883 * By default, the driver assumes the SLI4 port requires RPI
9884 * header postings. The SLI4_PARAM response will correct this
9885 * assumption.
9886 */
9887 phba->sli4_hba.rpi_hdrs_in_use = 1;
9888
James Smartfedd3b72011-02-16 12:39:24 -05009889 /* Read the port's SLI4 Config Parameters */
9890 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9891 sizeof(struct lpfc_sli4_cfg_mhdr));
9892 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9893 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9894 length, LPFC_SLI4_MBX_EMBED);
9895 if (!phba->sli4_hba.intr_enable)
9896 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009897 else {
9898 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9899 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9900 }
James Smartfedd3b72011-02-16 12:39:24 -05009901 if (unlikely(rc))
9902 return rc;
9903 sli4_params = &phba->sli4_hba.pc_sli4_params;
9904 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9905 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9906 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9907 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9908 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9909 mbx_sli4_parameters);
9910 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9911 mbx_sli4_parameters);
9912 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9913 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9914 else
9915 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9916 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9917 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -05009918 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009919 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9920 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9921 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9922 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009923 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009924 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9925 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -08009926 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009927 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9928 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009929 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9930 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -08009931 phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
9932 bf_get(cfg_xib, mbx_sli4_parameters));
9933
9934 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
9935 !phba->nvme_support) {
9936 phba->nvme_support = 0;
9937 phba->nvmet_support = 0;
9938 phba->cfg_nvme_io_channel = 0;
9939 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
9940 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
9941 "6101 Disabling NVME support: "
9942 "Not supported by firmware: %d %d\n",
9943 bf_get(cfg_nvme, mbx_sli4_parameters),
9944 bf_get(cfg_xib, mbx_sli4_parameters));
9945
9946 /* If firmware doesn't support NVME, just use SCSI support */
9947 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
9948 return -ENODEV;
9949 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
9950 }
James Smart05580562011-05-24 11:40:48 -04009951
James Smartf358dd02017-02-12 13:52:34 -08009952 if (bf_get(cfg_xib, mbx_sli4_parameters) && phba->cfg_suppress_rsp)
9953 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
9954
James Smart05580562011-05-24 11:40:48 -04009955 /* Make sure that sge_supp_len can be handled by the driver */
9956 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9957 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9958
James Smartb5c53952016-03-31 14:12:30 -07009959 /*
9960 * Issue IOs with CDB embedded in WQE to minimized the number
9961 * of DMAs the firmware has to do. Setting this to 1 also forces
9962 * the driver to use 128 bytes WQEs for FCP IOs.
9963 */
9964 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
9965 phba->fcp_embed_io = 1;
9966 else
9967 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -07009968
9969 /*
9970 * Check if the SLI port supports MDS Diagnostics
9971 */
9972 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
9973 phba->mds_diags_support = 1;
9974 else
9975 phba->mds_diags_support = 0;
James Smartfedd3b72011-02-16 12:39:24 -05009976 return 0;
9977}
9978
9979/**
James Smart3772a992009-05-22 14:50:54 -04009980 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9981 * @pdev: pointer to PCI device
9982 * @pid: pointer to PCI device identifier
9983 *
9984 * This routine is to be called to attach a device with SLI-3 interface spec
9985 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9986 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9987 * information of the device and driver to see if the driver state that it can
9988 * support this kind of device. If the match is successful, the driver core
9989 * invokes this routine. If this routine determines it can claim the HBA, it
9990 * does all the initialization that it needs to do to handle the HBA properly.
9991 *
9992 * Return code
9993 * 0 - driver can claim the device
9994 * negative value - driver can not claim the device
9995 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009996static int
James Smart3772a992009-05-22 14:50:54 -04009997lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9998{
9999 struct lpfc_hba *phba;
10000 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010001 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040010002 int error;
10003 uint32_t cfg_mode, intr_mode;
10004
10005 /* Allocate memory for HBA structure */
10006 phba = lpfc_hba_alloc(pdev);
10007 if (!phba)
10008 return -ENOMEM;
10009
10010 /* Perform generic PCI device enabling operation */
10011 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010012 if (error)
James Smart3772a992009-05-22 14:50:54 -040010013 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040010014
10015 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
10016 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
10017 if (error)
10018 goto out_disable_pci_dev;
10019
10020 /* Set up SLI-3 specific device PCI memory space */
10021 error = lpfc_sli_pci_mem_setup(phba);
10022 if (error) {
10023 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10024 "1402 Failed to set up pci memory space.\n");
10025 goto out_disable_pci_dev;
10026 }
10027
James Smart3772a992009-05-22 14:50:54 -040010028 /* Set up SLI-3 specific device driver resources */
10029 error = lpfc_sli_driver_resource_setup(phba);
10030 if (error) {
10031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10032 "1404 Failed to set up driver resource.\n");
10033 goto out_unset_pci_mem_s3;
10034 }
10035
10036 /* Initialize and populate the iocb list per host */
10037 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
10038 if (error) {
10039 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10040 "1405 Failed to initialize iocb list.\n");
10041 goto out_unset_driver_resource_s3;
10042 }
10043
10044 /* Set up common device driver resources */
10045 error = lpfc_setup_driver_resource_phase2(phba);
10046 if (error) {
10047 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10048 "1406 Failed to set up driver resource.\n");
10049 goto out_free_iocb_list;
10050 }
10051
James Smart079b5c92011-08-21 21:48:49 -040010052 /* Get the default values for Model Name and Description */
10053 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10054
James Smart3772a992009-05-22 14:50:54 -040010055 /* Create SCSI host to the physical port */
10056 error = lpfc_create_shost(phba);
10057 if (error) {
10058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10059 "1407 Failed to create scsi host.\n");
10060 goto out_unset_driver_resource;
10061 }
10062
10063 /* Configure sysfs attributes */
10064 vport = phba->pport;
10065 error = lpfc_alloc_sysfs_attr(vport);
10066 if (error) {
10067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10068 "1476 Failed to allocate sysfs attr\n");
10069 goto out_destroy_shost;
10070 }
10071
James Smart6669f9b2009-10-02 15:16:45 -040010072 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040010073 /* Now, trying to enable interrupt and bring up the device */
10074 cfg_mode = phba->cfg_use_msi;
10075 while (true) {
10076 /* Put device to a known state before enabling interrupt */
10077 lpfc_stop_port(phba);
10078 /* Configure and enable interrupt */
10079 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
10080 if (intr_mode == LPFC_INTR_ERROR) {
10081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10082 "0431 Failed to enable interrupt.\n");
10083 error = -ENODEV;
10084 goto out_free_sysfs_attr;
10085 }
10086 /* SLI-3 HBA setup */
10087 if (lpfc_sli_hba_setup(phba)) {
10088 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10089 "1477 Failed to set up hba\n");
10090 error = -ENODEV;
10091 goto out_remove_device;
10092 }
10093
10094 /* Wait 50ms for the interrupts of previous mailbox commands */
10095 msleep(50);
10096 /* Check active interrupts on message signaled interrupts */
10097 if (intr_mode == 0 ||
10098 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
10099 /* Log the current active interrupt mode */
10100 phba->intr_mode = intr_mode;
10101 lpfc_log_intr_mode(phba, intr_mode);
10102 break;
10103 } else {
10104 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10105 "0447 Configure interrupt mode (%d) "
10106 "failed active interrupt test.\n",
10107 intr_mode);
10108 /* Disable the current interrupt mode */
10109 lpfc_sli_disable_intr(phba);
10110 /* Try next level of interrupt mode */
10111 cfg_mode = --intr_mode;
10112 }
10113 }
10114
10115 /* Perform post initialization setup */
10116 lpfc_post_init_setup(phba);
10117
10118 /* Check if there are static vports to be created. */
10119 lpfc_create_static_vport(phba);
10120
10121 return 0;
10122
10123out_remove_device:
10124 lpfc_unset_hba(phba);
10125out_free_sysfs_attr:
10126 lpfc_free_sysfs_attr(vport);
10127out_destroy_shost:
10128 lpfc_destroy_shost(phba);
10129out_unset_driver_resource:
10130 lpfc_unset_driver_resource_phase2(phba);
10131out_free_iocb_list:
10132 lpfc_free_iocb_list(phba);
10133out_unset_driver_resource_s3:
10134 lpfc_sli_driver_resource_unset(phba);
10135out_unset_pci_mem_s3:
10136 lpfc_sli_pci_mem_unset(phba);
10137out_disable_pci_dev:
10138 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010139 if (shost)
10140 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040010141out_free_phba:
10142 lpfc_hba_free(phba);
10143 return error;
10144}
10145
10146/**
10147 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040010148 * @pdev: pointer to PCI device
10149 *
James Smart3772a992009-05-22 14:50:54 -040010150 * This routine is to be called to disattach a device with SLI-3 interface
10151 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10152 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10153 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040010154 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010155static void
James Smart3772a992009-05-22 14:50:54 -040010156lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050010157{
James Smart2e0fef82007-06-17 19:56:36 -050010158 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10159 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050010160 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050010161 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050010162 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050010163
James Smart549e55c2007-08-02 11:09:51 -040010164 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040010165 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040010166 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010167
James Smart858c9f62007-06-17 19:56:39 -050010168 lpfc_free_sysfs_attr(vport);
10169
James Smarteada2722008-12-04 22:39:13 -050010170 /* Release all the vports against this physical port */
10171 vports = lpfc_create_vport_work_array(phba);
10172 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010173 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10174 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10175 continue;
James Smarteada2722008-12-04 22:39:13 -050010176 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010177 }
James Smarteada2722008-12-04 22:39:13 -050010178 lpfc_destroy_vport_work_array(phba, vports);
10179
10180 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050010181 fc_remove_host(shost);
10182 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -040010183 lpfc_cleanup(vport);
10184
James Smart2e0fef82007-06-17 19:56:36 -050010185 /*
10186 * Bring down the SLI Layer. This step disable all interrupts,
10187 * clears the rings, discards all mailbox commands, and resets
10188 * the HBA.
10189 */
James Smarta257bf92009-04-06 18:48:10 -040010190
Justin P. Mattock48e34d02010-12-30 15:07:58 -080010191 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050010192 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040010193 /* Stop kthread signal shall trigger work_done one more time */
10194 kthread_stop(phba->worker_thread);
10195 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050010196 lpfc_sli_brdrestart(phba);
10197
James Smart72859902012-01-18 16:25:38 -050010198 kfree(phba->vpi_bmask);
10199 kfree(phba->vpi_ids);
10200
James Smart3772a992009-05-22 14:50:54 -040010201 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -050010202 spin_lock_irq(&phba->hbalock);
10203 list_del_init(&vport->listentry);
10204 spin_unlock_irq(&phba->hbalock);
10205
James Smart858c9f62007-06-17 19:56:39 -050010206 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050010207
James Smart912e3ac2011-05-24 11:42:11 -040010208 /* Disable SR-IOV if enabled */
10209 if (phba->cfg_sriov_nr_virtfn)
10210 pci_disable_sriov(pdev);
10211
James Smart5b75da22008-12-04 22:39:35 -050010212 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010213 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050010214
James Smart858c9f62007-06-17 19:56:39 -050010215 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050010216
10217 /*
10218 * Call scsi_free before mem_free since scsi bufs are released to their
10219 * corresponding pools here.
10220 */
10221 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -040010222 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010223
James Smart34b02dc2008-08-24 21:49:55 -040010224 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
10225 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050010226
James Smart2e0fef82007-06-17 19:56:36 -050010227 /* Free resources associated with SLI2 interface */
10228 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040010229 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050010230
10231 /* unmap adapter SLIM and Control Registers */
10232 iounmap(phba->ctrl_regs_memmap_p);
10233 iounmap(phba->slim_memmap_p);
10234
James Smart3772a992009-05-22 14:50:54 -040010235 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010236
Johannes Thumshirne0c04832016-06-07 09:44:03 +020010237 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050010238 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050010239}
10240
Linas Vepstas8d63f372007-02-14 14:28:36 -060010241/**
James Smart3772a992009-05-22 14:50:54 -040010242 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010243 * @pdev: pointer to PCI device
10244 * @msg: power management message
10245 *
James Smart3772a992009-05-22 14:50:54 -040010246 * This routine is to be called from the kernel's PCI subsystem to support
10247 * system Power Management (PM) to device with SLI-3 interface spec. When
10248 * PM invokes this method, it quiesces the device by stopping the driver's
10249 * worker thread for the device, turning off device's interrupt and DMA,
10250 * and bring the device offline. Note that as the driver implements the
10251 * minimum PM requirements to a power-aware driver's PM support for the
10252 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10253 * to the suspend() method call will be treated as SUSPEND and the driver will
10254 * fully reinitialize its device during resume() method call, the driver will
10255 * set device to PCI_D3hot state in PCI config space instead of setting it
10256 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050010257 *
10258 * Return code
James Smart3772a992009-05-22 14:50:54 -040010259 * 0 - driver suspended the device
10260 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010261 **/
10262static int
James Smart3772a992009-05-22 14:50:54 -040010263lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050010264{
10265 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10266 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10267
10268 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10269 "0473 PCI device Power Management suspend.\n");
10270
10271 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010272 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050010273 lpfc_offline(phba);
10274 kthread_stop(phba->worker_thread);
10275
10276 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040010277 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050010278
10279 /* Save device state to PCI config space */
10280 pci_save_state(pdev);
10281 pci_set_power_state(pdev, PCI_D3hot);
10282
10283 return 0;
10284}
10285
10286/**
James Smart3772a992009-05-22 14:50:54 -040010287 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010288 * @pdev: pointer to PCI device
10289 *
James Smart3772a992009-05-22 14:50:54 -040010290 * This routine is to be called from the kernel's PCI subsystem to support
10291 * system Power Management (PM) to device with SLI-3 interface spec. When PM
10292 * invokes this method, it restores the device's PCI config space state and
10293 * fully reinitializes the device and brings it online. Note that as the
10294 * driver implements the minimum PM requirements to a power-aware driver's
10295 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
10296 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
10297 * driver will fully reinitialize its device during resume() method call,
10298 * the device will be set to PCI_D0 directly in PCI config space before
10299 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050010300 *
10301 * Return code
James Smart3772a992009-05-22 14:50:54 -040010302 * 0 - driver suspended the device
10303 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010304 **/
10305static int
James Smart3772a992009-05-22 14:50:54 -040010306lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050010307{
10308 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10309 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050010310 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010311 int error;
10312
10313 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10314 "0452 PCI device Power Management resume.\n");
10315
10316 /* Restore device state from PCI config space */
10317 pci_set_power_state(pdev, PCI_D0);
10318 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040010319
James Smart1dfb5a42010-02-12 14:40:50 -050010320 /*
10321 * As the new kernel behavior of pci_restore_state() API call clears
10322 * device saved_state flag, need to save the restored state again.
10323 */
10324 pci_save_state(pdev);
10325
James Smart3a55b532008-12-04 22:38:54 -050010326 if (pdev->is_busmaster)
10327 pci_set_master(pdev);
10328
10329 /* Startup the kernel thread for this host adapter. */
10330 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10331 "lpfc_worker_%d", phba->brd_no);
10332 if (IS_ERR(phba->worker_thread)) {
10333 error = PTR_ERR(phba->worker_thread);
10334 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10335 "0434 PM resume failed to start worker "
10336 "thread: error=x%x.\n", error);
10337 return error;
10338 }
10339
James Smart5b75da22008-12-04 22:39:35 -050010340 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010341 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010342 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -050010343 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -050010344 "0430 PM resume Failed to enable interrupt\n");
10345 return -EIO;
10346 } else
10347 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010348
10349 /* Restart HBA and bring it online */
10350 lpfc_sli_brdrestart(phba);
10351 lpfc_online(phba);
10352
James Smart5b75da22008-12-04 22:39:35 -050010353 /* Log the current active interrupt mode */
10354 lpfc_log_intr_mode(phba, phba->intr_mode);
10355
James Smart3a55b532008-12-04 22:38:54 -050010356 return 0;
10357}
10358
10359/**
James Smart891478a2009-11-18 15:40:23 -050010360 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
10361 * @phba: pointer to lpfc hba data structure.
10362 *
10363 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040010364 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050010365 **/
10366static void
10367lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
10368{
10369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10370 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040010371
10372 /*
10373 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10374 * and let the SCSI mid-layer to retry them to recover.
10375 */
James Smartdb55fba2014-04-04 13:52:02 -040010376 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050010377}
10378
10379/**
James Smart0d878412009-10-02 15:16:56 -040010380 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
10381 * @phba: pointer to lpfc hba data structure.
10382 *
10383 * This routine is called to prepare the SLI3 device for PCI slot reset. It
10384 * disables the device interrupt and pci device, and aborts the internal FCP
10385 * pending I/Os.
10386 **/
10387static void
10388lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
10389{
James Smart0d878412009-10-02 15:16:56 -040010390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010391 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040010392
James Smart75baf692010-06-08 18:31:21 -040010393 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010394 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010395
James Smarte2af0d22010-03-15 11:25:32 -040010396 /* Block all SCSI devices' I/Os on the host */
10397 lpfc_scsi_dev_block(phba);
10398
James Smartea714f32013-04-17 20:18:47 -040010399 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10400 lpfc_sli_flush_fcp_rings(phba);
10401
James Smarte2af0d22010-03-15 11:25:32 -040010402 /* stop all timers */
10403 lpfc_stop_hba_timers(phba);
10404
James Smart0d878412009-10-02 15:16:56 -040010405 /* Disable interrupt and pci device */
10406 lpfc_sli_disable_intr(phba);
10407 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040010408}
10409
10410/**
10411 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
10412 * @phba: pointer to lpfc hba data structure.
10413 *
10414 * This routine is called to prepare the SLI3 device for PCI slot permanently
10415 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10416 * pending I/Os.
10417 **/
10418static void
James Smart75baf692010-06-08 18:31:21 -040010419lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040010420{
10421 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010422 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040010423 /* Block all SCSI devices' I/Os on the host */
10424 lpfc_scsi_dev_block(phba);
10425
10426 /* stop all timers */
10427 lpfc_stop_hba_timers(phba);
10428
James Smart0d878412009-10-02 15:16:56 -040010429 /* Clean up all driver's outstanding SCSI I/Os */
10430 lpfc_sli_flush_fcp_rings(phba);
10431}
10432
10433/**
James Smart3772a992009-05-22 14:50:54 -040010434 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040010435 * @pdev: pointer to PCI device.
10436 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010437 *
James Smart3772a992009-05-22 14:50:54 -040010438 * This routine is called from the PCI subsystem for I/O error handling to
10439 * device with SLI-3 interface spec. This function is called by the PCI
10440 * subsystem after a PCI bus error affecting this device has been detected.
10441 * When this function is invoked, it will need to stop all the I/Os and
10442 * interrupt(s) to the device. Once that is done, it will return
10443 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
10444 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040010445 *
10446 * Return codes
James Smart0d878412009-10-02 15:16:56 -040010447 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040010448 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10449 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040010450 **/
James Smart3772a992009-05-22 14:50:54 -040010451static pci_ers_result_t
10452lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010453{
James Smart51ef4c22007-08-02 11:10:31 -040010454 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10455 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010456
James Smart0d878412009-10-02 15:16:56 -040010457 switch (state) {
10458 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050010459 /* Non-fatal error, prepare for recovery */
10460 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040010461 return PCI_ERS_RESULT_CAN_RECOVER;
10462 case pci_channel_io_frozen:
10463 /* Fatal error, prepare for slot reset */
10464 lpfc_sli_prep_dev_for_reset(phba);
10465 return PCI_ERS_RESULT_NEED_RESET;
10466 case pci_channel_io_perm_failure:
10467 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040010468 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010469 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040010470 default:
10471 /* Unknown state, prepare and request slot reset */
10472 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10473 "0472 Unknown PCI error state: x%x\n", state);
10474 lpfc_sli_prep_dev_for_reset(phba);
10475 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040010476 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060010477}
10478
10479/**
James Smart3772a992009-05-22 14:50:54 -040010480 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040010481 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010482 *
James Smart3772a992009-05-22 14:50:54 -040010483 * This routine is called from the PCI subsystem for error handling to
10484 * device with SLI-3 interface spec. This is called after PCI bus has been
10485 * reset to restart the PCI card from scratch, as if from a cold-boot.
10486 * During the PCI subsystem error recovery, after driver returns
10487 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10488 * recovery and then call this routine before calling the .resume method
10489 * to recover the device. This function will initialize the HBA device,
10490 * enable the interrupt, but it will just put the HBA to offline state
10491 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040010492 *
10493 * Return codes
James Smart3772a992009-05-22 14:50:54 -040010494 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10495 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060010496 */
James Smart3772a992009-05-22 14:50:54 -040010497static pci_ers_result_t
10498lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010499{
James Smart51ef4c22007-08-02 11:10:31 -040010500 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10501 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010502 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050010503 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010504
10505 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110010506 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010507 printk(KERN_ERR "lpfc: Cannot re-enable "
10508 "PCI device after reset.\n");
10509 return PCI_ERS_RESULT_DISCONNECT;
10510 }
10511
James Smart97207482008-12-04 22:39:19 -050010512 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010513
10514 /*
10515 * As the new kernel behavior of pci_restore_state() API call clears
10516 * device saved_state flag, need to save the restored state again.
10517 */
10518 pci_save_state(pdev);
10519
James Smart97207482008-12-04 22:39:19 -050010520 if (pdev->is_busmaster)
10521 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010522
James Smart92d7f7b2007-06-17 19:56:38 -050010523 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010524 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050010525 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010526
James Smart5b75da22008-12-04 22:39:35 -050010527 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010528 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010529 if (intr_mode == LPFC_INTR_ERROR) {
10530 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10531 "0427 Cannot re-enable interrupt after "
10532 "slot reset.\n");
10533 return PCI_ERS_RESULT_DISCONNECT;
10534 } else
10535 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010536
James Smart75baf692010-06-08 18:31:21 -040010537 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040010538 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010539 lpfc_offline(phba);
10540 lpfc_sli_brdrestart(phba);
10541
James Smart5b75da22008-12-04 22:39:35 -050010542 /* Log the current active interrupt mode */
10543 lpfc_log_intr_mode(phba, phba->intr_mode);
10544
Linas Vepstas8d63f372007-02-14 14:28:36 -060010545 return PCI_ERS_RESULT_RECOVERED;
10546}
10547
10548/**
James Smart3772a992009-05-22 14:50:54 -040010549 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040010550 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060010551 *
James Smart3772a992009-05-22 14:50:54 -040010552 * This routine is called from the PCI subsystem for error handling to device
10553 * with SLI-3 interface spec. It is called when kernel error recovery tells
10554 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10555 * error recovery. After this call, traffic can start to flow from this device
10556 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010557 */
James Smart3772a992009-05-22 14:50:54 -040010558static void
10559lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010560{
James Smart51ef4c22007-08-02 11:10:31 -040010561 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10562 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010563
James Smarte2af0d22010-03-15 11:25:32 -040010564 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040010565 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -040010566
10567 /* Clean up Advanced Error Reporting (AER) if needed */
10568 if (phba->hba_flag & HBA_AER_ENABLED)
10569 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010570}
10571
James Smart3772a992009-05-22 14:50:54 -040010572/**
James Smartda0436e2009-05-22 14:51:39 -040010573 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
10574 * @phba: pointer to lpfc hba data structure.
10575 *
10576 * returns the number of ELS/CT IOCBs to reserve
10577 **/
10578int
10579lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
10580{
10581 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
10582
James Smartf1126682009-06-10 17:22:44 -040010583 if (phba->sli_rev == LPFC_SLI_REV4) {
10584 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040010585 return 10;
James Smartf1126682009-06-10 17:22:44 -040010586 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040010587 return 25;
James Smartf1126682009-06-10 17:22:44 -040010588 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040010589 return 50;
James Smartf1126682009-06-10 17:22:44 -040010590 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040010591 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040010592 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040010593 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040010594 else if (max_xri <= 2048)
10595 return 200;
10596 else
10597 return 250;
James Smartf1126682009-06-10 17:22:44 -040010598 } else
10599 return 0;
James Smartda0436e2009-05-22 14:51:39 -040010600}
10601
10602/**
James Smart895427b2017-02-12 13:52:30 -080010603 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
10604 * @phba: pointer to lpfc hba data structure.
10605 *
James Smartf358dd02017-02-12 13:52:34 -080010606 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080010607 **/
10608int
10609lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
10610{
10611 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
10612
James Smartf358dd02017-02-12 13:52:34 -080010613 if (phba->nvmet_support)
10614 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080010615 return max_xri;
10616}
10617
10618
10619/**
James Smart52d52442011-05-24 11:42:45 -040010620 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040010621 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040010622 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040010623 *
James Smart52d52442011-05-24 11:42:45 -040010624 **/
James Smartce396282012-09-29 11:30:56 -040010625static void
10626lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040010627{
James Smartce396282012-09-29 11:30:56 -040010628 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050010629 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040010630 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040010631 struct list_head dma_buffer_list;
10632 int i, rc = 0;
10633 struct lpfc_dmabuf *dmabuf, *next;
10634 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070010635 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040010636
James Smartc71ab862012-10-31 14:44:33 -040010637 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040010638 if (!fw) {
10639 rc = -ENXIO;
10640 goto out;
10641 }
10642 image = (struct lpfc_grp_hdr *)fw->data;
10643
James Smart6b6ef5d2016-10-13 15:06:17 -070010644 magic_number = be32_to_cpu(image->magic_number);
10645 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
10646 fid = bf_get_be32(lpfc_grp_hdr_id, image),
10647 fsize = be32_to_cpu(image->size);
10648
James Smart52d52442011-05-24 11:42:45 -040010649 INIT_LIST_HEAD(&dma_buffer_list);
James Smart6b6ef5d2016-10-13 15:06:17 -070010650 if ((magic_number != LPFC_GROUP_OJECT_MAGIC_G5 &&
10651 magic_number != LPFC_GROUP_OJECT_MAGIC_G6) ||
10652 ftype != LPFC_FILE_TYPE_GROUP || fsize != fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10654 "3022 Invalid FW image found. "
Arnd Bergmannefe583c2016-10-17 14:35:46 +020010655 "Magic:%x Type:%x ID:%x Size %d %zd\n",
James Smart6b6ef5d2016-10-13 15:06:17 -070010656 magic_number, ftype, fid, fsize, fw->size);
James Smartce396282012-09-29 11:30:56 -040010657 rc = -EINVAL;
10658 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010659 }
10660 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040010661 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040010662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040010663 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040010664 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040010665 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040010666 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
10667 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
10668 GFP_KERNEL);
10669 if (!dmabuf) {
10670 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010671 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010672 }
10673 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10674 SLI4_PAGE_SIZE,
10675 &dmabuf->phys,
10676 GFP_KERNEL);
10677 if (!dmabuf->virt) {
10678 kfree(dmabuf);
10679 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010680 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010681 }
10682 list_add_tail(&dmabuf->list, &dma_buffer_list);
10683 }
10684 while (offset < fw->size) {
10685 temp_offset = offset;
10686 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040010687 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010688 memcpy(dmabuf->virt,
10689 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040010690 fw->size - temp_offset);
10691 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040010692 break;
10693 }
James Smart52d52442011-05-24 11:42:45 -040010694 memcpy(dmabuf->virt, fw->data + temp_offset,
10695 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040010696 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040010697 }
10698 rc = lpfc_wr_object(phba, &dma_buffer_list,
10699 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040010700 if (rc)
10701 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010702 }
10703 rc = offset;
10704 }
James Smartce396282012-09-29 11:30:56 -040010705
10706release_out:
James Smart52d52442011-05-24 11:42:45 -040010707 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
10708 list_del(&dmabuf->list);
10709 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
10710 dmabuf->virt, dmabuf->phys);
10711 kfree(dmabuf);
10712 }
James Smartce396282012-09-29 11:30:56 -040010713 release_firmware(fw);
10714out:
10715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040010716 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040010717 return;
James Smart52d52442011-05-24 11:42:45 -040010718}
10719
10720/**
James Smartc71ab862012-10-31 14:44:33 -040010721 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
10722 * @phba: pointer to lpfc hba data structure.
10723 *
10724 * This routine is called to perform Linux generic firmware upgrade on device
10725 * that supports such feature.
10726 **/
10727int
10728lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
10729{
10730 uint8_t file_name[ELX_MODEL_NAME_SIZE];
10731 int ret;
10732 const struct firmware *fw;
10733
10734 /* Only supported on SLI4 interface type 2 for now */
10735 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10736 LPFC_SLI_INTF_IF_TYPE_2)
10737 return -EPERM;
10738
10739 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
10740
10741 if (fw_upgrade == INT_FW_UPGRADE) {
10742 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
10743 file_name, &phba->pcidev->dev,
10744 GFP_KERNEL, (void *)phba,
10745 lpfc_write_firmware);
10746 } else if (fw_upgrade == RUN_FW_UPGRADE) {
10747 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
10748 if (!ret)
10749 lpfc_write_firmware(fw, (void *)phba);
10750 } else {
10751 ret = -EINVAL;
10752 }
10753
10754 return ret;
10755}
10756
10757/**
James Smartda0436e2009-05-22 14:51:39 -040010758 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
10759 * @pdev: pointer to PCI device
10760 * @pid: pointer to PCI device identifier
10761 *
10762 * This routine is called from the kernel's PCI subsystem to device with
10763 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10764 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10765 * information of the device and driver to see if the driver state that it
10766 * can support this kind of device. If the match is successful, the driver
10767 * core invokes this routine. If this routine determines it can claim the HBA,
10768 * it does all the initialization that it needs to do to handle the HBA
10769 * properly.
10770 *
10771 * Return code
10772 * 0 - driver can claim the device
10773 * negative value - driver can not claim the device
10774 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010775static int
James Smartda0436e2009-05-22 14:51:39 -040010776lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10777{
10778 struct lpfc_hba *phba;
10779 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010780 struct Scsi_Host *shost = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040010781 int error;
James Smartda0436e2009-05-22 14:51:39 -040010782 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040010783
10784 /* Allocate memory for HBA structure */
10785 phba = lpfc_hba_alloc(pdev);
10786 if (!phba)
10787 return -ENOMEM;
10788
10789 /* Perform generic PCI device enabling operation */
10790 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010791 if (error)
James Smartda0436e2009-05-22 14:51:39 -040010792 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040010793
10794 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
10795 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
10796 if (error)
10797 goto out_disable_pci_dev;
10798
10799 /* Set up SLI-4 specific device PCI memory space */
10800 error = lpfc_sli4_pci_mem_setup(phba);
10801 if (error) {
10802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10803 "1410 Failed to set up pci memory space.\n");
10804 goto out_disable_pci_dev;
10805 }
10806
James Smartda0436e2009-05-22 14:51:39 -040010807 /* Set up SLI-4 Specific device driver resources */
10808 error = lpfc_sli4_driver_resource_setup(phba);
10809 if (error) {
10810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10811 "1412 Failed to set up driver resource.\n");
10812 goto out_unset_pci_mem_s4;
10813 }
10814
10815 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010816
10817 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10818 "2821 initialize iocb list %d.\n",
10819 phba->cfg_iocb_cnt*1024);
10820 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10821
James Smartda0436e2009-05-22 14:51:39 -040010822 if (error) {
10823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10824 "1413 Failed to initialize iocb list.\n");
10825 goto out_unset_driver_resource_s4;
10826 }
10827
James Smart19ca7602010-11-20 23:11:55 -050010828 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010829 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010830
James Smartda0436e2009-05-22 14:51:39 -040010831 /* Set up common device driver resources */
10832 error = lpfc_setup_driver_resource_phase2(phba);
10833 if (error) {
10834 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10835 "1414 Failed to set up driver resource.\n");
10836 goto out_free_iocb_list;
10837 }
10838
James Smart079b5c92011-08-21 21:48:49 -040010839 /* Get the default values for Model Name and Description */
10840 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10841
James Smartda0436e2009-05-22 14:51:39 -040010842 /* Create SCSI host to the physical port */
10843 error = lpfc_create_shost(phba);
10844 if (error) {
10845 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10846 "1415 Failed to create scsi host.\n");
10847 goto out_unset_driver_resource;
10848 }
10849
10850 /* Configure sysfs attributes */
10851 vport = phba->pport;
10852 error = lpfc_alloc_sysfs_attr(vport);
10853 if (error) {
10854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10855 "1416 Failed to allocate sysfs attr\n");
10856 goto out_destroy_shost;
10857 }
10858
James Smart6669f9b2009-10-02 15:16:45 -040010859 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010860 /* Now, trying to enable interrupt and bring up the device */
10861 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010862
James Smart7b15db32013-01-03 15:43:29 -050010863 /* Put device to a known state before enabling interrupt */
10864 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080010865
James Smart7b15db32013-01-03 15:43:29 -050010866 /* Configure and enable interrupt */
10867 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10868 if (intr_mode == LPFC_INTR_ERROR) {
10869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10870 "0426 Failed to enable interrupt.\n");
10871 error = -ENODEV;
10872 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010873 }
James Smart7b15db32013-01-03 15:43:29 -050010874 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080010875 if (phba->intr_type != MSIX) {
10876 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10877 phba->cfg_fcp_io_channel = 1;
10878 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
10879 phba->cfg_nvme_io_channel = 1;
10880 phba->io_channel_irqs = 1;
10881 }
10882
10883
James Smart7b15db32013-01-03 15:43:29 -050010884 /* Set up SLI-4 HBA */
10885 if (lpfc_sli4_hba_setup(phba)) {
10886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10887 "1421 Failed to set up hba\n");
10888 error = -ENODEV;
10889 goto out_disable_intr;
10890 }
10891
10892 /* Log the current active interrupt mode */
10893 phba->intr_mode = intr_mode;
10894 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010895
10896 /* Perform post initialization setup */
10897 lpfc_post_init_setup(phba);
10898
James Smart01649562017-02-12 13:52:32 -080010899 /* NVME support in FW earlier in the driver load corrects the
10900 * FC4 type making a check for nvme_support unnecessary.
10901 */
10902 if ((phba->nvmet_support == 0) &&
10903 (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
10904 /* Create NVME binding with nvme_fc_transport. This
10905 * ensures the vport is initialized.
10906 */
10907 error = lpfc_nvme_create_localport(vport);
10908 if (error) {
10909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10910 "6004 NVME registration failed, "
10911 "error x%x\n",
10912 error);
10913 goto out_disable_intr;
10914 }
10915 }
James Smart895427b2017-02-12 13:52:30 -080010916
James Smartc71ab862012-10-31 14:44:33 -040010917 /* check for firmware upgrade or downgrade */
10918 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040010919 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010920
James Smart1c6834a2009-07-19 10:01:26 -040010921 /* Check if there are static vports to be created. */
10922 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010923 return 0;
10924
10925out_disable_intr:
10926 lpfc_sli4_disable_intr(phba);
10927out_free_sysfs_attr:
10928 lpfc_free_sysfs_attr(vport);
10929out_destroy_shost:
10930 lpfc_destroy_shost(phba);
10931out_unset_driver_resource:
10932 lpfc_unset_driver_resource_phase2(phba);
10933out_free_iocb_list:
10934 lpfc_free_iocb_list(phba);
10935out_unset_driver_resource_s4:
10936 lpfc_sli4_driver_resource_unset(phba);
10937out_unset_pci_mem_s4:
10938 lpfc_sli4_pci_mem_unset(phba);
10939out_disable_pci_dev:
10940 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010941 if (shost)
10942 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010943out_free_phba:
10944 lpfc_hba_free(phba);
10945 return error;
10946}
10947
10948/**
10949 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10950 * @pdev: pointer to PCI device
10951 *
10952 * This routine is called from the kernel's PCI subsystem to device with
10953 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10954 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10955 * device to be removed from the PCI subsystem properly.
10956 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010957static void
James Smartda0436e2009-05-22 14:51:39 -040010958lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10959{
10960 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10961 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10962 struct lpfc_vport **vports;
10963 struct lpfc_hba *phba = vport->phba;
10964 int i;
10965
10966 /* Mark the device unloading flag */
10967 spin_lock_irq(&phba->hbalock);
10968 vport->load_flag |= FC_UNLOADING;
10969 spin_unlock_irq(&phba->hbalock);
10970
10971 /* Free the HBA sysfs attributes */
10972 lpfc_free_sysfs_attr(vport);
10973
10974 /* Release all the vports against this physical port */
10975 vports = lpfc_create_vport_work_array(phba);
10976 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010977 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10978 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10979 continue;
James Smartda0436e2009-05-22 14:51:39 -040010980 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010981 }
James Smartda0436e2009-05-22 14:51:39 -040010982 lpfc_destroy_vport_work_array(phba, vports);
10983
10984 /* Remove FC host and then SCSI host with the physical port */
10985 fc_remove_host(shost);
10986 scsi_remove_host(shost);
James Smartf358dd02017-02-12 13:52:34 -080010987 /* todo: tgt: remove targetport */
James Smartda0436e2009-05-22 14:51:39 -040010988
James Smart895427b2017-02-12 13:52:30 -080010989 /* Perform ndlp cleanup on the physical port. The nvme localport
10990 * is destroyed after to ensure all rports are io-disabled.
10991 */
James Smart01649562017-02-12 13:52:32 -080010992 lpfc_nvme_destroy_localport(vport);
James Smartda0436e2009-05-22 14:51:39 -040010993 lpfc_cleanup(vport);
10994
10995 /*
10996 * Bring down the SLI Layer. This step disables all interrupts,
10997 * clears the rings, discards all mailbox commands, and resets
10998 * the HBA FCoE function.
10999 */
11000 lpfc_debugfs_terminate(vport);
11001 lpfc_sli4_hba_unset(phba);
11002
11003 spin_lock_irq(&phba->hbalock);
11004 list_del_init(&vport->listentry);
11005 spin_unlock_irq(&phba->hbalock);
11006
James Smart3677a3a2010-09-29 11:19:14 -040011007 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040011008 * buffers are released to their corresponding pools here.
11009 */
11010 lpfc_scsi_free(phba);
James Smart895427b2017-02-12 13:52:30 -080011011 lpfc_nvme_free(phba);
James Smart01649562017-02-12 13:52:32 -080011012 lpfc_free_iocb_list(phba);
James Smart67d12732012-08-03 12:36:13 -040011013
James Smartda0436e2009-05-22 14:51:39 -040011014 lpfc_sli4_driver_resource_unset(phba);
11015
11016 /* Unmap adapter Control and Doorbell registers */
11017 lpfc_sli4_pci_mem_unset(phba);
11018
11019 /* Release PCI resources and disable device's PCI function */
11020 scsi_host_put(shost);
11021 lpfc_disable_pci_dev(phba);
11022
11023 /* Finally, free the driver's device data structure */
11024 lpfc_hba_free(phba);
11025
11026 return;
11027}
11028
11029/**
11030 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
11031 * @pdev: pointer to PCI device
11032 * @msg: power management message
11033 *
11034 * This routine is called from the kernel's PCI subsystem to support system
11035 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
11036 * this method, it quiesces the device by stopping the driver's worker
11037 * thread for the device, turning off device's interrupt and DMA, and bring
11038 * the device offline. Note that as the driver implements the minimum PM
11039 * requirements to a power-aware driver's PM support for suspend/resume -- all
11040 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
11041 * method call will be treated as SUSPEND and the driver will fully
11042 * reinitialize its device during resume() method call, the driver will set
11043 * device to PCI_D3hot state in PCI config space instead of setting it
11044 * according to the @msg provided by the PM.
11045 *
11046 * Return code
11047 * 0 - driver suspended the device
11048 * Error otherwise
11049 **/
11050static int
11051lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
11052{
11053 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11054 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11055
11056 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040011057 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040011058
11059 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040011060 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040011061 lpfc_offline(phba);
11062 kthread_stop(phba->worker_thread);
11063
11064 /* Disable interrupt from device */
11065 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040011066 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040011067
11068 /* Save device state to PCI config space */
11069 pci_save_state(pdev);
11070 pci_set_power_state(pdev, PCI_D3hot);
11071
11072 return 0;
11073}
11074
11075/**
11076 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
11077 * @pdev: pointer to PCI device
11078 *
11079 * This routine is called from the kernel's PCI subsystem to support system
11080 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
11081 * this method, it restores the device's PCI config space state and fully
11082 * reinitializes the device and brings it online. Note that as the driver
11083 * implements the minimum PM requirements to a power-aware driver's PM for
11084 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11085 * to the suspend() method call will be treated as SUSPEND and the driver
11086 * will fully reinitialize its device during resume() method call, the device
11087 * will be set to PCI_D0 directly in PCI config space before restoring the
11088 * state.
11089 *
11090 * Return code
11091 * 0 - driver suspended the device
11092 * Error otherwise
11093 **/
11094static int
11095lpfc_pci_resume_one_s4(struct pci_dev *pdev)
11096{
11097 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11098 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11099 uint32_t intr_mode;
11100 int error;
11101
11102 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11103 "0292 PCI device Power Management resume.\n");
11104
11105 /* Restore device state from PCI config space */
11106 pci_set_power_state(pdev, PCI_D0);
11107 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050011108
11109 /*
11110 * As the new kernel behavior of pci_restore_state() API call clears
11111 * device saved_state flag, need to save the restored state again.
11112 */
11113 pci_save_state(pdev);
11114
James Smartda0436e2009-05-22 14:51:39 -040011115 if (pdev->is_busmaster)
11116 pci_set_master(pdev);
11117
11118 /* Startup the kernel thread for this host adapter. */
11119 phba->worker_thread = kthread_run(lpfc_do_work, phba,
11120 "lpfc_worker_%d", phba->brd_no);
11121 if (IS_ERR(phba->worker_thread)) {
11122 error = PTR_ERR(phba->worker_thread);
11123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11124 "0293 PM resume failed to start worker "
11125 "thread: error=x%x.\n", error);
11126 return error;
11127 }
11128
11129 /* Configure and enable interrupt */
11130 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11131 if (intr_mode == LPFC_INTR_ERROR) {
11132 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11133 "0294 PM resume Failed to enable interrupt\n");
11134 return -EIO;
11135 } else
11136 phba->intr_mode = intr_mode;
11137
11138 /* Restart HBA and bring it online */
11139 lpfc_sli_brdrestart(phba);
11140 lpfc_online(phba);
11141
11142 /* Log the current active interrupt mode */
11143 lpfc_log_intr_mode(phba, phba->intr_mode);
11144
11145 return 0;
11146}
11147
11148/**
James Smart75baf692010-06-08 18:31:21 -040011149 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
11150 * @phba: pointer to lpfc hba data structure.
11151 *
11152 * This routine is called to prepare the SLI4 device for PCI slot recover. It
11153 * aborts all the outstanding SCSI I/Os to the pci device.
11154 **/
11155static void
11156lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
11157{
James Smart75baf692010-06-08 18:31:21 -040011158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11159 "2828 PCI channel I/O abort preparing for recovery\n");
11160 /*
11161 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11162 * and let the SCSI mid-layer to retry them to recover.
11163 */
James Smartdb55fba2014-04-04 13:52:02 -040011164 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040011165}
11166
11167/**
11168 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
11169 * @phba: pointer to lpfc hba data structure.
11170 *
11171 * This routine is called to prepare the SLI4 device for PCI slot reset. It
11172 * disables the device interrupt and pci device, and aborts the internal FCP
11173 * pending I/Os.
11174 **/
11175static void
11176lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
11177{
11178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11179 "2826 PCI channel disable preparing for reset\n");
11180
11181 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040011182 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011183
11184 /* Block all SCSI devices' I/Os on the host */
11185 lpfc_scsi_dev_block(phba);
11186
James Smartea714f32013-04-17 20:18:47 -040011187 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
11188 lpfc_sli_flush_fcp_rings(phba);
11189
James Smart75baf692010-06-08 18:31:21 -040011190 /* stop all timers */
11191 lpfc_stop_hba_timers(phba);
11192
11193 /* Disable interrupt and pci device */
11194 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040011195 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040011196 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040011197}
11198
11199/**
11200 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
11201 * @phba: pointer to lpfc hba data structure.
11202 *
11203 * This routine is called to prepare the SLI4 device for PCI slot permanently
11204 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11205 * pending I/Os.
11206 **/
11207static void
11208lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11209{
11210 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11211 "2827 PCI channel permanent disable for failure\n");
11212
11213 /* Block all SCSI devices' I/Os on the host */
11214 lpfc_scsi_dev_block(phba);
11215
11216 /* stop all timers */
11217 lpfc_stop_hba_timers(phba);
11218
11219 /* Clean up all driver's outstanding SCSI I/Os */
11220 lpfc_sli_flush_fcp_rings(phba);
11221}
11222
11223/**
James Smartda0436e2009-05-22 14:51:39 -040011224 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
11225 * @pdev: pointer to PCI device.
11226 * @state: the current PCI connection state.
11227 *
11228 * This routine is called from the PCI subsystem for error handling to device
11229 * with SLI-4 interface spec. This function is called by the PCI subsystem
11230 * after a PCI bus error affecting this device has been detected. When this
11231 * function is invoked, it will need to stop all the I/Os and interrupt(s)
11232 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
11233 * for the PCI subsystem to perform proper recovery as desired.
11234 *
11235 * Return codes
11236 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11237 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11238 **/
11239static pci_ers_result_t
11240lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
11241{
James Smart75baf692010-06-08 18:31:21 -040011242 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11243 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11244
11245 switch (state) {
11246 case pci_channel_io_normal:
11247 /* Non-fatal error, prepare for recovery */
11248 lpfc_sli4_prep_dev_for_recover(phba);
11249 return PCI_ERS_RESULT_CAN_RECOVER;
11250 case pci_channel_io_frozen:
11251 /* Fatal error, prepare for slot reset */
11252 lpfc_sli4_prep_dev_for_reset(phba);
11253 return PCI_ERS_RESULT_NEED_RESET;
11254 case pci_channel_io_perm_failure:
11255 /* Permanent failure, prepare for device down */
11256 lpfc_sli4_prep_dev_for_perm_failure(phba);
11257 return PCI_ERS_RESULT_DISCONNECT;
11258 default:
11259 /* Unknown state, prepare and request slot reset */
11260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11261 "2825 Unknown PCI error state: x%x\n", state);
11262 lpfc_sli4_prep_dev_for_reset(phba);
11263 return PCI_ERS_RESULT_NEED_RESET;
11264 }
James Smartda0436e2009-05-22 14:51:39 -040011265}
11266
11267/**
11268 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
11269 * @pdev: pointer to PCI device.
11270 *
11271 * This routine is called from the PCI subsystem for error handling to device
11272 * with SLI-4 interface spec. It is called after PCI bus has been reset to
11273 * restart the PCI card from scratch, as if from a cold-boot. During the
11274 * PCI subsystem error recovery, after the driver returns
11275 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11276 * recovery and then call this routine before calling the .resume method to
11277 * recover the device. This function will initialize the HBA device, enable
11278 * the interrupt, but it will just put the HBA to offline state without
11279 * passing any I/O traffic.
11280 *
11281 * Return codes
11282 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11283 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11284 */
11285static pci_ers_result_t
11286lpfc_io_slot_reset_s4(struct pci_dev *pdev)
11287{
James Smart75baf692010-06-08 18:31:21 -040011288 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11289 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11290 struct lpfc_sli *psli = &phba->sli;
11291 uint32_t intr_mode;
11292
11293 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11294 if (pci_enable_device_mem(pdev)) {
11295 printk(KERN_ERR "lpfc: Cannot re-enable "
11296 "PCI device after reset.\n");
11297 return PCI_ERS_RESULT_DISCONNECT;
11298 }
11299
11300 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040011301
11302 /*
11303 * As the new kernel behavior of pci_restore_state() API call clears
11304 * device saved_state flag, need to save the restored state again.
11305 */
11306 pci_save_state(pdev);
11307
James Smart75baf692010-06-08 18:31:21 -040011308 if (pdev->is_busmaster)
11309 pci_set_master(pdev);
11310
11311 spin_lock_irq(&phba->hbalock);
11312 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11313 spin_unlock_irq(&phba->hbalock);
11314
11315 /* Configure and enable interrupt */
11316 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11317 if (intr_mode == LPFC_INTR_ERROR) {
11318 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11319 "2824 Cannot re-enable interrupt after "
11320 "slot reset.\n");
11321 return PCI_ERS_RESULT_DISCONNECT;
11322 } else
11323 phba->intr_mode = intr_mode;
11324
11325 /* Log the current active interrupt mode */
11326 lpfc_log_intr_mode(phba, phba->intr_mode);
11327
James Smartda0436e2009-05-22 14:51:39 -040011328 return PCI_ERS_RESULT_RECOVERED;
11329}
11330
11331/**
11332 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
11333 * @pdev: pointer to PCI device
11334 *
11335 * This routine is called from the PCI subsystem for error handling to device
11336 * with SLI-4 interface spec. It is called when kernel error recovery tells
11337 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11338 * error recovery. After this call, traffic can start to flow from this device
11339 * again.
11340 **/
11341static void
11342lpfc_io_resume_s4(struct pci_dev *pdev)
11343{
James Smart75baf692010-06-08 18:31:21 -040011344 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11345 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11346
11347 /*
11348 * In case of slot reset, as function reset is performed through
11349 * mailbox command which needs DMA to be enabled, this operation
11350 * has to be moved to the io resume phase. Taking device offline
11351 * will perform the necessary cleanup.
11352 */
11353 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
11354 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040011355 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011356 lpfc_offline(phba);
11357 lpfc_sli_brdrestart(phba);
11358 /* Bring the device back online */
11359 lpfc_online(phba);
11360 }
11361
11362 /* Clean up Advanced Error Reporting (AER) if needed */
11363 if (phba->hba_flag & HBA_AER_ENABLED)
11364 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040011365}
11366
11367/**
James Smart3772a992009-05-22 14:50:54 -040011368 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
11369 * @pdev: pointer to PCI device
11370 * @pid: pointer to PCI device identifier
11371 *
11372 * This routine is to be registered to the kernel's PCI subsystem. When an
11373 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
11374 * at PCI device-specific information of the device and driver to see if the
11375 * driver state that it can support this kind of device. If the match is
11376 * successful, the driver core invokes this routine. This routine dispatches
11377 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
11378 * do all the initialization that it needs to do to handle the HBA device
11379 * properly.
11380 *
11381 * Return code
11382 * 0 - driver can claim the device
11383 * negative value - driver can not claim the device
11384 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011385static int
James Smart3772a992009-05-22 14:50:54 -040011386lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
11387{
11388 int rc;
James Smart8fa38512009-07-19 10:01:03 -040011389 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040011390
James Smart28baac72010-02-12 14:42:03 -050011391 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040011392 return -ENODEV;
11393
James Smart8fa38512009-07-19 10:01:03 -040011394 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050011395 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040011396 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011397 else
James Smart3772a992009-05-22 14:50:54 -040011398 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011399
James Smart3772a992009-05-22 14:50:54 -040011400 return rc;
11401}
11402
11403/**
11404 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
11405 * @pdev: pointer to PCI device
11406 *
11407 * This routine is to be registered to the kernel's PCI subsystem. When an
11408 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
11409 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
11410 * remove routine, which will perform all the necessary cleanup for the
11411 * device to be removed from the PCI subsystem properly.
11412 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011413static void
James Smart3772a992009-05-22 14:50:54 -040011414lpfc_pci_remove_one(struct pci_dev *pdev)
11415{
11416 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11417 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11418
11419 switch (phba->pci_dev_grp) {
11420 case LPFC_PCI_DEV_LP:
11421 lpfc_pci_remove_one_s3(pdev);
11422 break;
James Smartda0436e2009-05-22 14:51:39 -040011423 case LPFC_PCI_DEV_OC:
11424 lpfc_pci_remove_one_s4(pdev);
11425 break;
James Smart3772a992009-05-22 14:50:54 -040011426 default:
11427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11428 "1424 Invalid PCI device group: 0x%x\n",
11429 phba->pci_dev_grp);
11430 break;
11431 }
11432 return;
11433}
11434
11435/**
11436 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
11437 * @pdev: pointer to PCI device
11438 * @msg: power management message
11439 *
11440 * This routine is to be registered to the kernel's PCI subsystem to support
11441 * system Power Management (PM). When PM invokes this method, it dispatches
11442 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
11443 * suspend the device.
11444 *
11445 * Return code
11446 * 0 - driver suspended the device
11447 * Error otherwise
11448 **/
11449static int
11450lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
11451{
11452 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11453 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11454 int rc = -ENODEV;
11455
11456 switch (phba->pci_dev_grp) {
11457 case LPFC_PCI_DEV_LP:
11458 rc = lpfc_pci_suspend_one_s3(pdev, msg);
11459 break;
James Smartda0436e2009-05-22 14:51:39 -040011460 case LPFC_PCI_DEV_OC:
11461 rc = lpfc_pci_suspend_one_s4(pdev, msg);
11462 break;
James Smart3772a992009-05-22 14:50:54 -040011463 default:
11464 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11465 "1425 Invalid PCI device group: 0x%x\n",
11466 phba->pci_dev_grp);
11467 break;
11468 }
11469 return rc;
11470}
11471
11472/**
11473 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
11474 * @pdev: pointer to PCI device
11475 *
11476 * This routine is to be registered to the kernel's PCI subsystem to support
11477 * system Power Management (PM). When PM invokes this method, it dispatches
11478 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
11479 * resume the device.
11480 *
11481 * Return code
11482 * 0 - driver suspended the device
11483 * Error otherwise
11484 **/
11485static int
11486lpfc_pci_resume_one(struct pci_dev *pdev)
11487{
11488 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11489 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11490 int rc = -ENODEV;
11491
11492 switch (phba->pci_dev_grp) {
11493 case LPFC_PCI_DEV_LP:
11494 rc = lpfc_pci_resume_one_s3(pdev);
11495 break;
James Smartda0436e2009-05-22 14:51:39 -040011496 case LPFC_PCI_DEV_OC:
11497 rc = lpfc_pci_resume_one_s4(pdev);
11498 break;
James Smart3772a992009-05-22 14:50:54 -040011499 default:
11500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11501 "1426 Invalid PCI device group: 0x%x\n",
11502 phba->pci_dev_grp);
11503 break;
11504 }
11505 return rc;
11506}
11507
11508/**
11509 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
11510 * @pdev: pointer to PCI device.
11511 * @state: the current PCI connection state.
11512 *
11513 * This routine is registered to the PCI subsystem for error handling. This
11514 * function is called by the PCI subsystem after a PCI bus error affecting
11515 * this device has been detected. When this routine is invoked, it dispatches
11516 * the action to the proper SLI-3 or SLI-4 device error detected handling
11517 * routine, which will perform the proper error detected operation.
11518 *
11519 * Return codes
11520 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11521 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11522 **/
11523static pci_ers_result_t
11524lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
11525{
11526 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11527 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11528 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11529
11530 switch (phba->pci_dev_grp) {
11531 case LPFC_PCI_DEV_LP:
11532 rc = lpfc_io_error_detected_s3(pdev, state);
11533 break;
James Smartda0436e2009-05-22 14:51:39 -040011534 case LPFC_PCI_DEV_OC:
11535 rc = lpfc_io_error_detected_s4(pdev, state);
11536 break;
James Smart3772a992009-05-22 14:50:54 -040011537 default:
11538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11539 "1427 Invalid PCI device group: 0x%x\n",
11540 phba->pci_dev_grp);
11541 break;
11542 }
11543 return rc;
11544}
11545
11546/**
11547 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
11548 * @pdev: pointer to PCI device.
11549 *
11550 * This routine is registered to the PCI subsystem for error handling. This
11551 * function is called after PCI bus has been reset to restart the PCI card
11552 * from scratch, as if from a cold-boot. When this routine is invoked, it
11553 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
11554 * routine, which will perform the proper device reset.
11555 *
11556 * Return codes
11557 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11558 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11559 **/
11560static pci_ers_result_t
11561lpfc_io_slot_reset(struct pci_dev *pdev)
11562{
11563 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11564 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11565 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11566
11567 switch (phba->pci_dev_grp) {
11568 case LPFC_PCI_DEV_LP:
11569 rc = lpfc_io_slot_reset_s3(pdev);
11570 break;
James Smartda0436e2009-05-22 14:51:39 -040011571 case LPFC_PCI_DEV_OC:
11572 rc = lpfc_io_slot_reset_s4(pdev);
11573 break;
James Smart3772a992009-05-22 14:50:54 -040011574 default:
11575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11576 "1428 Invalid PCI device group: 0x%x\n",
11577 phba->pci_dev_grp);
11578 break;
11579 }
11580 return rc;
11581}
11582
11583/**
11584 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
11585 * @pdev: pointer to PCI device
11586 *
11587 * This routine is registered to the PCI subsystem for error handling. It
11588 * is called when kernel error recovery tells the lpfc driver that it is
11589 * OK to resume normal PCI operation after PCI bus error recovery. When
11590 * this routine is invoked, it dispatches the action to the proper SLI-3
11591 * or SLI-4 device io_resume routine, which will resume the device operation.
11592 **/
11593static void
11594lpfc_io_resume(struct pci_dev *pdev)
11595{
11596 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11597 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11598
11599 switch (phba->pci_dev_grp) {
11600 case LPFC_PCI_DEV_LP:
11601 lpfc_io_resume_s3(pdev);
11602 break;
James Smartda0436e2009-05-22 14:51:39 -040011603 case LPFC_PCI_DEV_OC:
11604 lpfc_io_resume_s4(pdev);
11605 break;
James Smart3772a992009-05-22 14:50:54 -040011606 default:
11607 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11608 "1429 Invalid PCI device group: 0x%x\n",
11609 phba->pci_dev_grp);
11610 break;
11611 }
11612 return;
11613}
11614
James Smart1ba981f2014-02-20 09:56:45 -050011615/**
11616 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
11617 * @phba: pointer to lpfc hba data structure.
11618 *
11619 * This routine checks to see if OAS is supported for this adapter. If
11620 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
11621 * the enable oas flag is cleared and the pool created for OAS device data
11622 * is destroyed.
11623 *
11624 **/
11625void
11626lpfc_sli4_oas_verify(struct lpfc_hba *phba)
11627{
11628
11629 if (!phba->cfg_EnableXLane)
11630 return;
11631
11632 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
11633 phba->cfg_fof = 1;
11634 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040011635 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050011636 if (phba->device_data_mem_pool)
11637 mempool_destroy(phba->device_data_mem_pool);
11638 phba->device_data_mem_pool = NULL;
11639 }
11640
11641 return;
11642}
11643
11644/**
11645 * lpfc_fof_queue_setup - Set up all the fof queues
11646 * @phba: pointer to lpfc hba data structure.
11647 *
11648 * This routine is invoked to set up all the fof queues for the FC HBA
11649 * operation.
11650 *
11651 * Return codes
11652 * 0 - successful
11653 * -ENOMEM - No available memory
11654 **/
11655int
11656lpfc_fof_queue_setup(struct lpfc_hba *phba)
11657{
James Smart895427b2017-02-12 13:52:30 -080011658 struct lpfc_sli_ring *pring;
James Smart1ba981f2014-02-20 09:56:45 -050011659 int rc;
11660
11661 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
11662 if (rc)
11663 return -ENOMEM;
11664
James Smartf38fa0b2014-04-04 13:52:21 -040011665 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011666
11667 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
11668 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
11669 if (rc)
11670 goto out_oas_cq;
11671
11672 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
11673 phba->sli4_hba.oas_cq, LPFC_FCP);
11674 if (rc)
11675 goto out_oas_wq;
11676
James Smart895427b2017-02-12 13:52:30 -080011677 /* Bind this CQ/WQ to the NVME ring */
11678 pring = phba->sli4_hba.oas_wq->pring;
11679 pring->sli.sli4.wqp =
11680 (void *)phba->sli4_hba.oas_wq;
11681 phba->sli4_hba.oas_cq->pring = pring;
James Smart1ba981f2014-02-20 09:56:45 -050011682 }
11683
11684 return 0;
11685
11686out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040011687 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050011688out_oas_cq:
11689 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
11690 return rc;
11691
11692}
11693
11694/**
11695 * lpfc_fof_queue_create - Create all the fof queues
11696 * @phba: pointer to lpfc hba data structure.
11697 *
11698 * This routine is invoked to allocate all the fof queues for the FC HBA
11699 * operation. For each SLI4 queue type, the parameters such as queue entry
11700 * count (queue depth) shall be taken from the module parameter. For now,
11701 * we just use some constant number as place holder.
11702 *
11703 * Return codes
11704 * 0 - successful
11705 * -ENOMEM - No availble memory
11706 * -EIO - The mailbox failed to complete successfully.
11707 **/
11708int
11709lpfc_fof_queue_create(struct lpfc_hba *phba)
11710{
11711 struct lpfc_queue *qdesc;
11712
11713 /* Create FOF EQ */
11714 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
11715 phba->sli4_hba.eq_ecount);
11716 if (!qdesc)
11717 goto out_error;
11718
11719 phba->sli4_hba.fof_eq = qdesc;
11720
James Smartf38fa0b2014-04-04 13:52:21 -040011721 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011722
11723 /* Create OAS CQ */
11724 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
11725 phba->sli4_hba.cq_ecount);
11726 if (!qdesc)
11727 goto out_error;
11728
11729 phba->sli4_hba.oas_cq = qdesc;
11730
11731 /* Create OAS WQ */
11732 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
11733 phba->sli4_hba.wq_ecount);
11734 if (!qdesc)
11735 goto out_error;
11736
11737 phba->sli4_hba.oas_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -080011738 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
James Smart1ba981f2014-02-20 09:56:45 -050011739
11740 }
11741 return 0;
11742
11743out_error:
11744 lpfc_fof_queue_destroy(phba);
11745 return -ENOMEM;
11746}
11747
11748/**
11749 * lpfc_fof_queue_destroy - Destroy all the fof queues
11750 * @phba: pointer to lpfc hba data structure.
11751 *
11752 * This routine is invoked to release all the SLI4 queues with the FC HBA
11753 * operation.
11754 *
11755 * Return codes
11756 * 0 - successful
11757 **/
11758int
11759lpfc_fof_queue_destroy(struct lpfc_hba *phba)
11760{
11761 /* Release FOF Event queue */
11762 if (phba->sli4_hba.fof_eq != NULL) {
11763 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
11764 phba->sli4_hba.fof_eq = NULL;
11765 }
11766
11767 /* Release OAS Completion queue */
11768 if (phba->sli4_hba.oas_cq != NULL) {
11769 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
11770 phba->sli4_hba.oas_cq = NULL;
11771 }
11772
11773 /* Release OAS Work queue */
11774 if (phba->sli4_hba.oas_wq != NULL) {
11775 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
11776 phba->sli4_hba.oas_wq = NULL;
11777 }
11778 return 0;
11779}
11780
dea31012005-04-17 16:05:31 -050011781MODULE_DEVICE_TABLE(pci, lpfc_id_table);
11782
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070011783static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011784 .error_detected = lpfc_io_error_detected,
11785 .slot_reset = lpfc_io_slot_reset,
11786 .resume = lpfc_io_resume,
11787};
11788
dea31012005-04-17 16:05:31 -050011789static struct pci_driver lpfc_driver = {
11790 .name = LPFC_DRIVER_NAME,
11791 .id_table = lpfc_id_table,
11792 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011793 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050011794 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040011795 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050011796 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050011797};
11798
James Smart3ef6d242012-01-18 16:23:48 -050011799static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040011800 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050011801};
11802
11803static struct miscdevice lpfc_mgmt_dev = {
11804 .minor = MISC_DYNAMIC_MINOR,
11805 .name = "lpfcmgmt",
11806 .fops = &lpfc_mgmt_fop,
11807};
11808
James Smarte59058c2008-08-24 21:49:00 -040011809/**
James Smart3621a712009-04-06 18:47:14 -040011810 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040011811 *
11812 * This routine is to be invoked when the lpfc module is loaded into the
11813 * kernel. The special kernel macro module_init() is used to indicate the
11814 * role of this routine to the kernel as lpfc module entry point.
11815 *
11816 * Return codes
11817 * 0 - successful
11818 * -ENOMEM - FC attach transport failed
11819 * all others - failed
11820 */
dea31012005-04-17 16:05:31 -050011821static int __init
11822lpfc_init(void)
11823{
11824 int error = 0;
11825
11826 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040011827 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050011828
James Smart3ef6d242012-01-18 16:23:48 -050011829 error = misc_register(&lpfc_mgmt_dev);
11830 if (error)
11831 printk(KERN_ERR "Could not register lpfcmgmt device, "
11832 "misc_register returned with status %d", error);
11833
James Smart458c0832016-07-06 12:36:07 -070011834 lpfc_transport_functions.vport_create = lpfc_vport_create;
11835 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050011836 lpfc_transport_template =
11837 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040011838 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050011839 return -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070011840 lpfc_vport_transport_template =
11841 fc_attach_transport(&lpfc_vport_transport_functions);
11842 if (lpfc_vport_transport_template == NULL) {
11843 fc_release_transport(lpfc_transport_template);
11844 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040011845 }
James Smart7bb03bb2013-04-17 20:19:16 -040011846
11847 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040011848 lpfc_used_cpu = NULL;
James Smart2ea259e2017-02-12 13:52:27 -080011849 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040011850
dea31012005-04-17 16:05:31 -050011851 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050011852 if (error) {
dea31012005-04-17 16:05:31 -050011853 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070011854 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050011855 }
dea31012005-04-17 16:05:31 -050011856
11857 return error;
11858}
11859
James Smarte59058c2008-08-24 21:49:00 -040011860/**
James Smart3621a712009-04-06 18:47:14 -040011861 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040011862 *
11863 * This routine is invoked when the lpfc module is removed from the kernel.
11864 * The special kernel macro module_exit() is used to indicate the role of
11865 * this routine to the kernel as lpfc module exit point.
11866 */
dea31012005-04-17 16:05:31 -050011867static void __exit
11868lpfc_exit(void)
11869{
James Smart3ef6d242012-01-18 16:23:48 -050011870 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050011871 pci_unregister_driver(&lpfc_driver);
11872 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070011873 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050011874 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040011875 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11876 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011877 (1L << _dump_buf_data_order), _dump_buf_data);
11878 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11879 }
11880
11881 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011882 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11883 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011884 (1L << _dump_buf_dif_order), _dump_buf_dif);
11885 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11886 }
James Smartb246de12013-05-31 17:03:07 -040011887 kfree(lpfc_used_cpu);
Johannes Thumshirn79739672015-08-31 16:48:11 -040011888 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050011889}
11890
11891module_init(lpfc_init);
11892module_exit(lpfc_exit);
11893MODULE_LICENSE("GPL");
11894MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11895MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11896MODULE_VERSION("0:" LPFC_DRIVER_VERSION);