blob: 55f720ac6c8ce4d82da3178d565b91d86c20aa56 [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 Smart67073c62021-03-01 09:18:21 -08004 * Copyright (C) 2017-2021 Broadcom. All Rights Reserved. The term *
James Smart3e21d1c2018-05-04 20:37:59 -07005 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
23
dea31012005-04-17 16:05:31 -050024#include <linux/blkdev.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/idr.h>
28#include <linux/interrupt.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040029#include <linux/module.h>
dea31012005-04-17 16:05:31 -050030#include <linux/kthread.h>
31#include <linux/pci.h>
32#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050033#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040036#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050037#include <linux/miscdevice.h>
James Smart7bb03bb2013-04-17 20:19:16 -040038#include <linux/percpu.h>
James Smart895427b2017-02-12 13:52:30 -080039#include <linux/msi.h>
James Smart6a828b02019-01-28 11:14:31 -080040#include <linux/irq.h>
Maurizio Lombardi286871a2017-08-23 16:55:48 -070041#include <linux/bitops.h>
James Smart31f06d22019-08-14 16:56:31 -070042#include <linux/crash_dump.h>
James Smartdcaa2132019-11-04 16:57:06 -080043#include <linux/cpu.h>
James Smart93a4d6f2019-11-04 16:57:05 -080044#include <linux/cpuhotplug.h>
dea31012005-04-17 16:05:31 -050045
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040046#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050047#include <scsi/scsi_device.h>
48#include <scsi/scsi_host.h>
49#include <scsi/scsi_transport_fc.h>
James Smart86c67372017-04-21 16:05:04 -070050#include <scsi/scsi_tcq.h>
51#include <scsi/fc/fc_fs.h>
52
James Smartda0436e2009-05-22 14:51:39 -040053#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050054#include "lpfc_hw.h"
55#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040056#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040057#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050058#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050059#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080060#include "lpfc_scsi.h"
61#include "lpfc_nvme.h"
dea31012005-04-17 16:05:31 -050062#include "lpfc_logmsg.h"
63#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050064#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050065#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070066#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050067
James Smart93a4d6f2019-11-04 16:57:05 -080068static enum cpuhp_state lpfc_cpuhp_state;
James Smart7bb03bb2013-04-17 20:19:16 -040069/* Used when mapping IRQ vectors in a driver centric manner */
YueHaibingd7b761b2019-05-31 23:28:41 +080070static uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040071
James Smart93a4d6f2019-11-04 16:57:05 -080072static void __lpfc_cpuhp_remove(struct lpfc_hba *phba);
73static void lpfc_cpuhp_remove(struct lpfc_hba *phba);
74static void lpfc_cpuhp_add(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050075static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
76static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040077static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040078static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
79static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040080static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040081static void lpfc_free_els_sgl_list(struct lpfc_hba *);
James Smartf358dd02017-02-12 13:52:34 -080082static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040083static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040084static int lpfc_init_active_sgl_array(struct lpfc_hba *);
85static void lpfc_free_active_sgl(struct lpfc_hba *);
86static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
87static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
88static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
89static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
90static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040091static void lpfc_sli4_disable_intr(struct lpfc_hba *);
92static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050093static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
James Smart6a828b02019-01-28 11:14:31 -080094static uint16_t lpfc_find_cpu_handle(struct lpfc_hba *, uint16_t, int);
James Smartaa6ff302019-05-21 17:49:09 -070095static void lpfc_setup_bg(struct lpfc_hba *, struct Scsi_Host *);
dea31012005-04-17 16:05:31 -050096
97static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050098static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050099static DEFINE_IDR(lpfc_hba_index);
James Smartf358dd02017-02-12 13:52:34 -0800100#define LPFC_NVMET_BUF_POST 254
Gaurav Srivastava5e633302021-06-08 10:05:49 +0530101static int lpfc_vmid_res_alloc(struct lpfc_hba *phba, struct lpfc_vport *vport);
dea31012005-04-17 16:05:31 -0500102
James Smarte59058c2008-08-24 21:49:00 -0400103/**
James Smart3621a712009-04-06 18:47:14 -0400104 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -0400105 * @phba: pointer to lpfc hba data structure.
106 *
107 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
108 * mailbox command. It retrieves the revision information from the HBA and
109 * collects the Vital Product Data (VPD) about the HBA for preparing the
110 * configuration of the HBA.
111 *
112 * Return codes:
113 * 0 - success.
114 * -ERESTART - requests the SLI layer to reset the HBA and try again.
115 * Any other value - indicates an error.
116 **/
dea31012005-04-17 16:05:31 -0500117int
James Smart2e0fef82007-06-17 19:56:36 -0500118lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500119{
120 lpfc_vpd_t *vp = &phba->vpd;
121 int i = 0, rc;
122 LPFC_MBOXQ_t *pmb;
123 MAILBOX_t *mb;
124 char *lpfc_vpd_data = NULL;
125 uint16_t offset = 0;
126 static char licensed[56] =
127 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400128 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500129
130 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
131 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500132 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500133 return -ENOMEM;
134 }
135
James Smart04c68492009-05-22 14:52:52 -0400136 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500137 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500138
139 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400140 if (init_key) {
141 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500142
James Smart65a29c12006-07-06 15:50:50 -0400143 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
144 *ptext = cpu_to_be32(*ptext);
145 init_key = 0;
146 }
dea31012005-04-17 16:05:31 -0500147
148 lpfc_read_nv(phba, pmb);
149 memset((char*)mb->un.varRDnvp.rsvd3, 0,
150 sizeof (mb->un.varRDnvp.rsvd3));
151 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
152 sizeof (licensed));
153
154 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
155
156 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700157 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400158 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500159 "error, mbxCmd x%x READ_NVPARM, "
160 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500161 mb->mbxCommand, mb->mbxStatus);
162 mempool_free(pmb, phba->mbox_mem_pool);
163 return -ERESTART;
164 }
165 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500166 sizeof(phba->wwnn));
167 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
168 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500169 }
170
Martin Wilckdfb75132018-11-12 09:58:37 +0100171 /*
172 * Clear all option bits except LPFC_SLI3_BG_ENABLED,
173 * which was already set in lpfc_get_cfgparam()
174 */
175 phba->sli3_options &= (uint32_t)LPFC_SLI3_BG_ENABLED;
James Smart92d7f7b2007-06-17 19:56:38 -0500176
dea31012005-04-17 16:05:31 -0500177 /* Setup and issue mailbox READ REV command */
178 lpfc_read_rev(phba, pmb);
179 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
180 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700181 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400182 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500183 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500184 mb->mbxCommand, mb->mbxStatus);
185 mempool_free( pmb, phba->mbox_mem_pool);
186 return -ERESTART;
187 }
188
James Smart92d7f7b2007-06-17 19:56:38 -0500189
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500190 /*
191 * The value of rr must be 1 since the driver set the cv field to 1.
192 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500193 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500194 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500195 vp->rev.rBit = 0;
Dick Kennedy372c1872020-06-30 14:50:00 -0700196 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400197 "0440 Adapter failed to init, READ_REV has "
198 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500199 mempool_free(pmb, phba->mbox_mem_pool);
200 return -ERESTART;
dea31012005-04-17 16:05:31 -0500201 }
202
James Smart495a7142008-06-14 22:52:59 -0400203 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
204 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500205 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400206 }
James Smarted957682007-06-17 19:56:37 -0500207
dea31012005-04-17 16:05:31 -0500208 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500209 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500210 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500211 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
212 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
213 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
214 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500215 vp->rev.biuRev = mb->un.varRdRev.biuRev;
216 vp->rev.smRev = mb->un.varRdRev.smRev;
217 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
218 vp->rev.endecRev = mb->un.varRdRev.endecRev;
219 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
220 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
221 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
222 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
223 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
224 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
225
James Smart92d7f7b2007-06-17 19:56:38 -0500226 /* If the sli feature level is less then 9, we must
227 * tear down all RPIs and VPIs on link down if NPIV
228 * is enabled.
229 */
230 if (vp->rev.feaLevelHigh < 9)
231 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
232
dea31012005-04-17 16:05:31 -0500233 if (lpfc_is_LC_HBA(phba->pcidev->device))
234 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
235 sizeof (phba->RandomData));
236
dea31012005-04-17 16:05:31 -0500237 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500238 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
239 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400240 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500241 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400242 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500243 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
244
245 if (rc != MBX_SUCCESS) {
246 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400247 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500248 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500249 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500250 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500251 }
James Smart04c68492009-05-22 14:52:52 -0400252 /* dump mem may return a zero when finished or we got a
253 * mailbox error, either way we are done.
254 */
255 if (mb->un.varDmp.word_cnt == 0)
256 break;
Dick Kennedyd91e3ab2020-06-30 14:49:52 -0700257
James Smarte4ec1022021-04-21 16:45:11 -0700258 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
259 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400260 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smarte4ec1022021-04-21 16:45:11 -0700261 lpfc_vpd_data + offset,
262 mb->un.varDmp.word_cnt);
263 offset += mb->un.varDmp.word_cnt;
264 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
Dick Kennedyd91e3ab2020-06-30 14:49:52 -0700265
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500266 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500267
268 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500269out_free_mbox:
270 mempool_free(pmb, phba->mbox_mem_pool);
271 return 0;
272}
273
James Smarte59058c2008-08-24 21:49:00 -0400274/**
James Smart3621a712009-04-06 18:47:14 -0400275 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400276 * @phba: pointer to lpfc hba data structure.
277 * @pmboxq: pointer to the driver internal queue element for mailbox command.
278 *
279 * This is the completion handler for driver's configuring asynchronous event
280 * mailbox command to the device. If the mailbox command returns successfully,
281 * it will set internal async event support flag to 1; otherwise, it will
282 * set internal async event support flag to 0.
283 **/
James Smart57127f12007-10-27 13:37:05 -0400284static void
285lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
286{
James Smart04c68492009-05-22 14:52:52 -0400287 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400288 phba->temp_sensor_support = 1;
289 else
290 phba->temp_sensor_support = 0;
291 mempool_free(pmboxq, phba->mbox_mem_pool);
292 return;
293}
294
James Smarte59058c2008-08-24 21:49:00 -0400295/**
James Smart3621a712009-04-06 18:47:14 -0400296 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500297 * @phba: pointer to lpfc hba data structure.
298 * @pmboxq: pointer to the driver internal queue element for mailbox command.
299 *
300 * This is the completion handler for dump mailbox command for getting
301 * wake up parameters. When this command complete, the response contain
302 * Option rom version of the HBA. This function translate the version number
303 * into a human readable string and store it in OptionROMVersion.
304 **/
305static void
306lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
307{
308 struct prog_id *prg;
309 uint32_t prog_id_word;
310 char dist = ' ';
311 /* character array used for decoding dist type. */
312 char dist_char[] = "nabx";
313
James Smart04c68492009-05-22 14:52:52 -0400314 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500315 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500316 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500317 }
James Smart97207482008-12-04 22:39:19 -0500318
319 prg = (struct prog_id *) &prog_id_word;
320
321 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400322 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500323
324 /* Decode the Option rom version word to a readable string */
325 if (prg->dist < 4)
326 dist = dist_char[prg->dist];
327
328 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400329 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500330 prg->ver, prg->rev, prg->lev);
331 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400332 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500333 prg->ver, prg->rev, prg->lev,
334 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500335 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500336 return;
337}
338
339/**
James Smart05580562011-05-24 11:40:48 -0400340 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
341 * cfg_soft_wwnn, cfg_soft_wwpn
342 * @vport: pointer to lpfc vport data structure.
343 *
344 *
345 * Return codes
346 * None.
347 **/
348void
349lpfc_update_vport_wwn(struct lpfc_vport *vport)
350{
James Smartaeb3c812017-04-21 16:05:02 -0700351 uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
352 u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
353
James Smart05580562011-05-24 11:40:48 -0400354 /* If the soft name exists then update it using the service params */
355 if (vport->phba->cfg_soft_wwnn)
356 u64_to_wwn(vport->phba->cfg_soft_wwnn,
357 vport->fc_sparam.nodeName.u.wwn);
358 if (vport->phba->cfg_soft_wwpn)
359 u64_to_wwn(vport->phba->cfg_soft_wwpn,
360 vport->fc_sparam.portName.u.wwn);
361
362 /*
363 * If the name is empty or there exists a soft name
364 * then copy the service params name, otherwise use the fc name
365 */
366 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
367 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
368 sizeof(struct lpfc_name));
369 else
370 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
371 sizeof(struct lpfc_name));
372
James Smartaeb3c812017-04-21 16:05:02 -0700373 /*
374 * If the port name has changed, then set the Param changes flag
375 * to unreg the login
376 */
377 if (vport->fc_portname.u.wwn[0] != 0 &&
378 memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
379 sizeof(struct lpfc_name)))
380 vport->vport_flag |= FAWWPN_PARAM_CHG;
381
382 if (vport->fc_portname.u.wwn[0] == 0 ||
383 vport->phba->cfg_soft_wwpn ||
384 (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
385 vport->vport_flag & FAWWPN_SET) {
James Smart05580562011-05-24 11:40:48 -0400386 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
387 sizeof(struct lpfc_name));
James Smartaeb3c812017-04-21 16:05:02 -0700388 vport->vport_flag &= ~FAWWPN_SET;
389 if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
390 vport->vport_flag |= FAWWPN_SET;
391 }
James Smart05580562011-05-24 11:40:48 -0400392 else
393 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
394 sizeof(struct lpfc_name));
395}
396
397/**
James Smart3621a712009-04-06 18:47:14 -0400398 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400399 * @phba: pointer to lpfc hba data structure.
400 *
401 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
402 * command call. It performs all internal resource and state setups on the
403 * port: post IOCB buffers, enable appropriate host interrupt attentions,
404 * ELS ring timers, etc.
405 *
406 * Return codes
407 * 0 - success.
408 * Any other value - error.
409 **/
dea31012005-04-17 16:05:31 -0500410int
James Smart2e0fef82007-06-17 19:56:36 -0500411lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500412{
James Smart2e0fef82007-06-17 19:56:36 -0500413 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400414 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500415 LPFC_MBOXQ_t *pmb;
416 MAILBOX_t *mb;
417 struct lpfc_dmabuf *mp;
418 struct lpfc_sli *psli = &phba->sli;
419 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500420 int i, j;
421 int rc;
dea31012005-04-17 16:05:31 -0500422
James Smart7af67052007-10-27 13:38:11 -0400423 spin_lock_irq(&phba->hbalock);
424 /*
425 * If the Config port completed correctly the HBA is not
426 * over heated any more.
427 */
428 if (phba->over_temp_state == HBA_OVER_TEMP)
429 phba->over_temp_state = HBA_NORMAL_TEMP;
430 spin_unlock_irq(&phba->hbalock);
431
dea31012005-04-17 16:05:31 -0500432 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
433 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500434 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500435 return -ENOMEM;
436 }
James Smart04c68492009-05-22 14:52:52 -0400437 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500438
dea31012005-04-17 16:05:31 -0500439 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500440 rc = lpfc_read_sparam(phba, pmb, 0);
441 if (rc) {
442 mempool_free(pmb, phba->mbox_mem_pool);
443 return -ENOMEM;
444 }
445
James Smarted957682007-06-17 19:56:37 -0500446 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500447 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700448 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400449 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500450 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500451 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500452 phba->link_state = LPFC_HBA_ERROR;
James Smart3e1f0712018-11-29 16:09:29 -0800453 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart9f1177a2010-02-26 14:12:57 -0500454 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500455 lpfc_mbuf_free(phba, mp->virt, mp->phys);
456 kfree(mp);
457 return -EIO;
458 }
459
James Smart3e1f0712018-11-29 16:09:29 -0800460 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
dea31012005-04-17 16:05:31 -0500461
James Smart2e0fef82007-06-17 19:56:36 -0500462 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500463 lpfc_mbuf_free(phba, mp->virt, mp->phys);
464 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -0800465 pmb->ctx_buf = NULL;
James Smart05580562011-05-24 11:40:48 -0400466 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400467
468 /* Update the fc_host data structures with new wwn. */
469 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
470 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400471 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400472
dea31012005-04-17 16:05:31 -0500473 /* If no serial number in VPD data, use low 6 bytes of WWNN */
474 /* This should be consolidated into parse_vpd ? - mr */
475 if (phba->SerialNumber[0] == 0) {
476 uint8_t *outptr;
477
James Smart2e0fef82007-06-17 19:56:36 -0500478 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500479 for (i = 0; i < 12; i++) {
480 status = *outptr++;
481 j = ((status & 0xf0) >> 4);
482 if (j <= 9)
483 phba->SerialNumber[i] =
484 (char)((uint8_t) 0x30 + (uint8_t) j);
485 else
486 phba->SerialNumber[i] =
487 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
488 i++;
489 j = (status & 0xf);
490 if (j <= 9)
491 phba->SerialNumber[i] =
492 (char)((uint8_t) 0x30 + (uint8_t) j);
493 else
494 phba->SerialNumber[i] =
495 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
496 }
497 }
498
dea31012005-04-17 16:05:31 -0500499 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500500 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500501 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700502 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400503 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500504 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500505 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500506 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500507 mempool_free( pmb, phba->mbox_mem_pool);
508 return -EIO;
509 }
510
James Smarta0c87cb2009-07-19 10:01:10 -0400511 /* Check if the port is disabled */
512 lpfc_sli_read_link_ste(phba);
513
dea31012005-04-17 16:05:31 -0500514 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartf6770e72020-01-27 16:23:09 -0800515 if (phba->cfg_hba_queue_depth > mb->un.varRdConfig.max_xri) {
James Smart572709e2013-07-15 18:32:43 -0400516 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
517 "3359 HBA queue depth changed from %d to %d\n",
James Smartf6770e72020-01-27 16:23:09 -0800518 phba->cfg_hba_queue_depth,
519 mb->un.varRdConfig.max_xri);
520 phba->cfg_hba_queue_depth = mb->un.varRdConfig.max_xri;
James Smart572709e2013-07-15 18:32:43 -0400521 }
dea31012005-04-17 16:05:31 -0500522
523 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500524
525 /* Get the default values for Model Name and Description */
526 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
527
James Smart2e0fef82007-06-17 19:56:36 -0500528 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500529
James Smart0b727fe2007-10-27 13:37:25 -0400530 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800531 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
532 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
533 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
534 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500535
536 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500537 if (phba->sli_rev != 3)
538 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500539
James Smart93996272008-08-24 21:50:30 -0400540 /*
541 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
542 */
543 if (phba->intr_type == MSIX) {
544 rc = lpfc_config_msi(phba, pmb);
545 if (rc) {
546 mempool_free(pmb, phba->mbox_mem_pool);
547 return -EIO;
548 }
549 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
550 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700551 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart93996272008-08-24 21:50:30 -0400552 "0352 Config MSI mailbox command "
553 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400554 pmb->u.mb.mbxCommand,
555 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400556 mempool_free(pmb, phba->mbox_mem_pool);
557 return -EIO;
558 }
559 }
560
James Smart04c68492009-05-22 14:52:52 -0400561 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400562 /* Initialize ERATT handling flag */
563 phba->hba_flag &= ~HBA_ERATT_HANDLED;
564
dea31012005-04-17 16:05:31 -0500565 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500566 if (lpfc_readl(phba->HCregaddr, &status)) {
567 spin_unlock_irq(&phba->hbalock);
568 return -EIO;
569 }
dea31012005-04-17 16:05:31 -0500570 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
571 if (psli->num_rings > 0)
572 status |= HC_R0INT_ENA;
573 if (psli->num_rings > 1)
574 status |= HC_R1INT_ENA;
575 if (psli->num_rings > 2)
576 status |= HC_R2INT_ENA;
577 if (psli->num_rings > 3)
578 status |= HC_R3INT_ENA;
579
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500580 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
581 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400582 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500583
dea31012005-04-17 16:05:31 -0500584 writel(status, phba->HCregaddr);
585 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500586 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500587
James Smart93996272008-08-24 21:50:30 -0400588 /* Set up ring-0 (ELS) timer */
589 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400590 mod_timer(&vport->els_tmofunc,
591 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400592 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400593 mod_timer(&phba->hb_tmofunc,
594 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smarta22d73b2021-01-04 10:02:38 -0800595 phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
James Smart858c9f62007-06-17 19:56:39 -0500596 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400597 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400598 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700599 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500600
James Smarta0c87cb2009-07-19 10:01:10 -0400601 if (phba->hba_flag & LINK_DISABLED) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700602 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
603 "2598 Adapter Link is disabled.\n");
James Smarta0c87cb2009-07-19 10:01:10 -0400604 lpfc_down_link(phba, pmb);
605 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
606 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
607 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700608 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
609 "2599 Adapter failed to issue DOWN_LINK"
610 " mbox command rc 0x%x\n", rc);
James Smarta0c87cb2009-07-19 10:01:10 -0400611
612 mempool_free(pmb, phba->mbox_mem_pool);
613 return -EIO;
614 }
James Smarte40a02c2010-02-26 14:13:54 -0500615 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500616 mempool_free(pmb, phba->mbox_mem_pool);
617 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
618 if (rc)
619 return rc;
dea31012005-04-17 16:05:31 -0500620 }
621 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400622 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500623 if (!pmb) {
624 phba->link_state = LPFC_HBA_ERROR;
625 return -ENOMEM;
626 }
627
James Smart57127f12007-10-27 13:37:05 -0400628 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
629 pmb->mbox_cmpl = lpfc_config_async_cmpl;
630 pmb->vport = phba->pport;
631 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500632
James Smart57127f12007-10-27 13:37:05 -0400633 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700634 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart57127f12007-10-27 13:37:05 -0400635 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400636 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400637 rc);
638 mempool_free(pmb, phba->mbox_mem_pool);
639 }
James Smart97207482008-12-04 22:39:19 -0500640
641 /* Get Option rom version */
642 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500643 if (!pmb) {
644 phba->link_state = LPFC_HBA_ERROR;
645 return -ENOMEM;
646 }
647
James Smart97207482008-12-04 22:39:19 -0500648 lpfc_dump_wakeup_param(phba, pmb);
649 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
650 pmb->vport = phba->pport;
651 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
652
653 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700654 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
655 "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400656 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500657 mempool_free(pmb, phba->mbox_mem_pool);
658 }
659
James Smartd7c255b2008-08-24 21:50:00 -0400660 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400661}
662
James Smarte59058c2008-08-24 21:49:00 -0400663/**
James Smart84d1b002010-02-12 14:42:33 -0500664 * lpfc_hba_init_link - Initialize the FC link
665 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400666 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500667 *
668 * This routine will issue the INIT_LINK mailbox command call.
669 * It is available to other drivers through the lpfc_hba data
670 * structure for use as a delayed link up mechanism with the
671 * module parameter lpfc_suppress_link_up.
672 *
673 * Return code
674 * 0 - success
675 * Any other value - error
676 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400677static int
James Smart6e7288d2010-06-07 15:23:35 -0400678lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500679{
James Smart1b511972011-12-13 13:23:09 -0500680 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
681}
682
683/**
684 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
685 * @phba: pointer to lpfc hba data structure.
686 * @fc_topology: desired fc topology.
687 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
688 *
689 * This routine will issue the INIT_LINK mailbox command call.
690 * It is available to other drivers through the lpfc_hba data
691 * structure for use as a delayed link up mechanism with the
692 * module parameter lpfc_suppress_link_up.
693 *
694 * Return code
695 * 0 - success
696 * Any other value - error
697 **/
698int
699lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
700 uint32_t flag)
701{
James Smart84d1b002010-02-12 14:42:33 -0500702 struct lpfc_vport *vport = phba->pport;
703 LPFC_MBOXQ_t *pmb;
704 MAILBOX_t *mb;
705 int rc;
706
707 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
708 if (!pmb) {
709 phba->link_state = LPFC_HBA_ERROR;
710 return -ENOMEM;
711 }
712 mb = &pmb->u.mb;
713 pmb->vport = vport;
714
James Smart026abb82011-12-13 13:20:45 -0500715 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
716 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
717 !(phba->lmt & LMT_1Gb)) ||
718 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
719 !(phba->lmt & LMT_2Gb)) ||
720 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
721 !(phba->lmt & LMT_4Gb)) ||
722 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
723 !(phba->lmt & LMT_8Gb)) ||
724 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
725 !(phba->lmt & LMT_10Gb)) ||
726 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400727 !(phba->lmt & LMT_16Gb)) ||
728 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
James Smartfbd8a6b2018-02-22 08:18:45 -0800729 !(phba->lmt & LMT_32Gb)) ||
730 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_64G) &&
731 !(phba->lmt & LMT_64Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500732 /* Reset link speed to auto */
Dick Kennedy372c1872020-06-30 14:50:00 -0700733 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
734 "1302 Invalid speed for this board:%d "
735 "Reset link speed to auto.\n",
736 phba->cfg_link_speed);
James Smart026abb82011-12-13 13:20:45 -0500737 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
738 }
James Smart1b511972011-12-13 13:23:09 -0500739 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500740 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500741 if (phba->sli_rev < LPFC_SLI_REV4)
742 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400743 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500744 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700745 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
746 "0498 Adapter failed to init, mbxCmd x%x "
747 "INIT_LINK, mbxStatus x%x\n",
748 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500749 if (phba->sli_rev <= LPFC_SLI_REV3) {
750 /* Clear all interrupt enable conditions */
751 writel(0, phba->HCregaddr);
752 readl(phba->HCregaddr); /* flush */
753 /* Clear all pending interrupts */
754 writel(0xffffffff, phba->HAregaddr);
755 readl(phba->HAregaddr); /* flush */
756 }
James Smart84d1b002010-02-12 14:42:33 -0500757 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400758 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500759 mempool_free(pmb, phba->mbox_mem_pool);
760 return -EIO;
761 }
James Smarte40a02c2010-02-26 14:13:54 -0500762 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400763 if (flag == MBX_POLL)
764 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500765
766 return 0;
767}
768
769/**
770 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400771 * @phba: pointer to lpfc hba data structure.
772 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500773 *
774 * This routine will issue the DOWN_LINK mailbox command call.
775 * It is available to other drivers through the lpfc_hba data
776 * structure for use to stop the link.
777 *
778 * Return code
779 * 0 - success
780 * Any other value - error
781 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400782static int
James Smart6e7288d2010-06-07 15:23:35 -0400783lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500784{
785 LPFC_MBOXQ_t *pmb;
786 int rc;
787
788 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
789 if (!pmb) {
790 phba->link_state = LPFC_HBA_ERROR;
791 return -ENOMEM;
792 }
793
Dick Kennedy372c1872020-06-30 14:50:00 -0700794 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
795 "0491 Adapter Link is disabled.\n");
James Smart84d1b002010-02-12 14:42:33 -0500796 lpfc_down_link(phba, pmb);
797 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400798 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500799 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700800 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
801 "2522 Adapter failed to issue DOWN_LINK"
802 " mbox command rc 0x%x\n", rc);
James Smart84d1b002010-02-12 14:42:33 -0500803
804 mempool_free(pmb, phba->mbox_mem_pool);
805 return -EIO;
806 }
James Smart6e7288d2010-06-07 15:23:35 -0400807 if (flag == MBX_POLL)
808 mempool_free(pmb, phba->mbox_mem_pool);
809
James Smart84d1b002010-02-12 14:42:33 -0500810 return 0;
811}
812
813/**
James Smart3621a712009-04-06 18:47:14 -0400814 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400815 * @phba: pointer to lpfc HBA data structure.
816 *
817 * This routine will do LPFC uninitialization before the HBA is reset when
818 * bringing down the SLI Layer.
819 *
820 * Return codes
821 * 0 - success.
822 * Any other value - error.
823 **/
dea31012005-04-17 16:05:31 -0500824int
James Smart2e0fef82007-06-17 19:56:36 -0500825lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500826{
James Smart1b32f6a2008-02-08 18:49:39 -0500827 struct lpfc_vport **vports;
828 int i;
James Smart3772a992009-05-22 14:50:54 -0400829
830 if (phba->sli_rev <= LPFC_SLI_REV3) {
831 /* Disable interrupts */
832 writel(0, phba->HCregaddr);
833 readl(phba->HCregaddr); /* flush */
834 }
dea31012005-04-17 16:05:31 -0500835
James Smart1b32f6a2008-02-08 18:49:39 -0500836 if (phba->pport->load_flag & FC_UNLOADING)
837 lpfc_cleanup_discovery_resources(phba->pport);
838 else {
839 vports = lpfc_create_vport_work_array(phba);
840 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400841 for (i = 0; i <= phba->max_vports &&
842 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500843 lpfc_cleanup_discovery_resources(vports[i]);
844 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500845 }
846 return 0;
dea31012005-04-17 16:05:31 -0500847}
848
James Smarte59058c2008-08-24 21:49:00 -0400849/**
James Smart68e814f2014-05-21 08:04:59 -0400850 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
851 * rspiocb which got deferred
852 *
853 * @phba: pointer to lpfc HBA data structure.
854 *
855 * This routine will cleanup completed slow path events after HBA is reset
856 * when bringing down the SLI Layer.
857 *
858 *
859 * Return codes
860 * void.
861 **/
862static void
863lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
864{
865 struct lpfc_iocbq *rspiocbq;
866 struct hbq_dmabuf *dmabuf;
867 struct lpfc_cq_event *cq_event;
868
869 spin_lock_irq(&phba->hbalock);
870 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
871 spin_unlock_irq(&phba->hbalock);
872
873 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
874 /* Get the response iocb from the head of work queue */
875 spin_lock_irq(&phba->hbalock);
876 list_remove_head(&phba->sli4_hba.sp_queue_event,
877 cq_event, struct lpfc_cq_event, list);
878 spin_unlock_irq(&phba->hbalock);
879
880 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
881 case CQE_CODE_COMPL_WQE:
882 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
883 cq_event);
884 lpfc_sli_release_iocbq(phba, rspiocbq);
885 break;
886 case CQE_CODE_RECEIVE:
887 case CQE_CODE_RECEIVE_V1:
888 dmabuf = container_of(cq_event, struct hbq_dmabuf,
889 cq_event);
890 lpfc_in_buf_free(phba, &dmabuf->dbuf);
891 }
892 }
893}
894
895/**
James Smartbcece5f2014-04-04 13:51:34 -0400896 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
897 * @phba: pointer to lpfc HBA data structure.
898 *
899 * This routine will cleanup posted ELS buffers after the HBA is reset
900 * when bringing down the SLI Layer.
901 *
902 *
903 * Return codes
904 * void.
905 **/
906static void
907lpfc_hba_free_post_buf(struct lpfc_hba *phba)
908{
909 struct lpfc_sli *psli = &phba->sli;
910 struct lpfc_sli_ring *pring;
911 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400912 LIST_HEAD(buflist);
913 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400914
915 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
916 lpfc_sli_hbqbuf_free_all(phba);
917 else {
918 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800919 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400920 spin_lock_irq(&phba->hbalock);
921 list_splice_init(&pring->postbufq, &buflist);
922 spin_unlock_irq(&phba->hbalock);
923
924 count = 0;
925 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400926 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400927 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400928 lpfc_mbuf_free(phba, mp->virt, mp->phys);
929 kfree(mp);
930 }
James Smart07eab622014-04-04 13:51:44 -0400931
932 spin_lock_irq(&phba->hbalock);
933 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400934 spin_unlock_irq(&phba->hbalock);
935 }
936}
937
938/**
939 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
940 * @phba: pointer to lpfc HBA data structure.
941 *
942 * This routine will cleanup the txcmplq after the HBA is reset when bringing
943 * down the SLI Layer.
944 *
945 * Return codes
946 * void
947 **/
948static void
949lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
950{
951 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800952 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400953 struct lpfc_sli_ring *pring;
954 LIST_HEAD(completions);
955 int i;
James Smartc1dd9112018-01-30 15:58:57 -0800956 struct lpfc_iocbq *piocb, *next_iocb;
James Smartbcece5f2014-04-04 13:51:34 -0400957
James Smart895427b2017-02-12 13:52:30 -0800958 if (phba->sli_rev != LPFC_SLI_REV4) {
959 for (i = 0; i < psli->num_rings; i++) {
960 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -0400961 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -0800962 /* At this point in time the HBA is either reset or DOA
963 * Nothing should be on txcmplq as it will
964 * NEVER complete.
965 */
966 list_splice_init(&pring->txcmplq, &completions);
967 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400968 spin_unlock_irq(&phba->hbalock);
969
James Smart895427b2017-02-12 13:52:30 -0800970 lpfc_sli_abort_iocb_ring(phba, pring);
971 }
James Smartbcece5f2014-04-04 13:51:34 -0400972 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -0800973 lpfc_sli_cancel_iocbs(phba, &completions,
974 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
975 return;
976 }
977 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
978 pring = qp->pring;
979 if (!pring)
980 continue;
981 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -0800982 list_for_each_entry_safe(piocb, next_iocb,
983 &pring->txcmplq, list)
984 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart895427b2017-02-12 13:52:30 -0800985 list_splice_init(&pring->txcmplq, &completions);
986 pring->txcmplq_cnt = 0;
987 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -0400988 lpfc_sli_abort_iocb_ring(phba, pring);
989 }
James Smart895427b2017-02-12 13:52:30 -0800990 /* Cancel all the IOCBs from the completions list */
991 lpfc_sli_cancel_iocbs(phba, &completions,
992 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -0400993}
994
995/**
James Smart3772a992009-05-22 14:50:54 -0400996 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400997 * @phba: pointer to lpfc HBA data structure.
998 *
999 * This routine will do uninitialization after the HBA is reset when bring
1000 * down the SLI Layer.
1001 *
1002 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001003 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -04001004 * Any other value - error.
1005 **/
James Smart3772a992009-05-22 14:50:54 -04001006static int
1007lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001008{
James Smartbcece5f2014-04-04 13:51:34 -04001009 lpfc_hba_free_post_buf(phba);
1010 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001011 return 0;
1012}
James Smart5af5eee2010-10-22 11:06:38 -04001013
James Smartda0436e2009-05-22 14:51:39 -04001014/**
1015 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1016 * @phba: pointer to lpfc HBA data structure.
1017 *
1018 * This routine will do uninitialization after the HBA is reset when bring
1019 * down the SLI Layer.
1020 *
1021 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001022 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001023 * Any other value - error.
1024 **/
1025static int
1026lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1027{
James Smartc4908502019-01-28 11:14:28 -08001028 struct lpfc_io_buf *psb, *psb_next;
James Smart7cacae22020-03-31 09:50:03 -07001029 struct lpfc_async_xchg_ctx *ctxp, *ctxp_next;
James Smart5e5b5112019-01-28 11:14:22 -08001030 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -04001031 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001032 LIST_HEAD(nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001033 LIST_HEAD(nvmet_aborts);
James Smart0f65ff62010-02-26 14:14:23 -05001034 struct lpfc_sglq *sglq_entry = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08001035 int cnt, idx;
James Smart0f65ff62010-02-26 14:14:23 -05001036
James Smart895427b2017-02-12 13:52:30 -08001037
1038 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001039 lpfc_hba_clean_txcmplq(phba);
1040
James Smartda0436e2009-05-22 14:51:39 -04001041 /* At this point in time the HBA is either reset or DOA. Either
1042 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001043 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001044 * driver is unloading or reposted if the driver is restarting
1045 * the port.
1046 */
James Smarta7892412021-04-11 18:31:15 -07001047
James Smart895427b2017-02-12 13:52:30 -08001048 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001049 * list.
1050 */
James Smarta7892412021-04-11 18:31:15 -07001051 spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001052 list_for_each_entry(sglq_entry,
1053 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1054 sglq_entry->state = SGL_FREED;
1055
James Smartda0436e2009-05-22 14:51:39 -04001056 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001057 &phba->sli4_hba.lpfc_els_sgl_list);
1058
James Smartf358dd02017-02-12 13:52:34 -08001059
James Smarta7892412021-04-11 18:31:15 -07001060 spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001061
1062 /* abts_xxxx_buf_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001063 * list.
1064 */
James Smarta7892412021-04-11 18:31:15 -07001065 spin_lock_irq(&phba->hbalock);
James Smart5e5b5112019-01-28 11:14:22 -08001066 cnt = 0;
1067 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
1068 qp = &phba->sli4_hba.hdwq[idx];
1069
James Smartc00f62e2019-08-14 16:57:11 -07001070 spin_lock(&qp->abts_io_buf_list_lock);
1071 list_splice_init(&qp->lpfc_abts_io_buf_list,
James Smart895427b2017-02-12 13:52:30 -08001072 &aborts);
James Smart895427b2017-02-12 13:52:30 -08001073
James Smart0794d602019-01-28 11:14:19 -08001074 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
James Smart86c67372017-04-21 16:05:04 -07001075 psb->pCmd = NULL;
1076 psb->status = IOSTAT_SUCCESS;
James Smartcf1a1d32017-12-08 17:18:03 -08001077 cnt++;
James Smart86c67372017-04-21 16:05:04 -07001078 }
James Smart5e5b5112019-01-28 11:14:22 -08001079 spin_lock(&qp->io_buf_list_put_lock);
1080 list_splice_init(&aborts, &qp->lpfc_io_buf_list_put);
1081 qp->put_io_bufs += qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -07001082 qp->put_io_bufs += qp->abts_nvme_io_bufs;
James Smart5e5b5112019-01-28 11:14:22 -08001083 qp->abts_scsi_io_bufs = 0;
James Smartc00f62e2019-08-14 16:57:11 -07001084 qp->abts_nvme_io_bufs = 0;
James Smart5e5b5112019-01-28 11:14:22 -08001085 spin_unlock(&qp->io_buf_list_put_lock);
James Smartc00f62e2019-08-14 16:57:11 -07001086 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001087 }
James Smart731eedc2019-03-12 16:30:12 -07001088 spin_unlock_irq(&phba->hbalock);
James Smart5e5b5112019-01-28 11:14:22 -08001089
1090 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart731eedc2019-03-12 16:30:12 -07001091 spin_lock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001092 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1093 &nvmet_aborts);
James Smart731eedc2019-03-12 16:30:12 -07001094 spin_unlock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07001095 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
James Smart7b7f5512020-03-31 09:50:04 -07001096 ctxp->flag &= ~(LPFC_NVME_XBUSY | LPFC_NVME_ABORT_OP);
James Smart6c621a22017-05-15 15:20:45 -07001097 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
James Smart86c67372017-04-21 16:05:04 -07001098 }
James Smart895427b2017-02-12 13:52:30 -08001099 }
James Smart895427b2017-02-12 13:52:30 -08001100
James Smart68e814f2014-05-21 08:04:59 -04001101 lpfc_sli4_free_sp_events(phba);
James Smart5e5b5112019-01-28 11:14:22 -08001102 return cnt;
James Smartda0436e2009-05-22 14:51:39 -04001103}
1104
1105/**
1106 * lpfc_hba_down_post - Wrapper func for hba down post routine
1107 * @phba: pointer to lpfc HBA data structure.
1108 *
1109 * This routine wraps the actual SLI3 or SLI4 routine for performing
1110 * uninitialization after the HBA is reset when bring down the SLI Layer.
1111 *
1112 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001113 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001114 * Any other value - error.
1115 **/
1116int
1117lpfc_hba_down_post(struct lpfc_hba *phba)
1118{
1119 return (*phba->lpfc_hba_down_post)(phba);
1120}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001121
James Smarte59058c2008-08-24 21:49:00 -04001122/**
James Smart3621a712009-04-06 18:47:14 -04001123 * lpfc_hb_timeout - The HBA-timer timeout handler
Lee Jonesfe614ac2020-07-23 13:24:22 +01001124 * @t: timer context used to obtain the pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04001125 *
1126 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1127 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1128 * work-port-events bitmap and the worker thread is notified. This timeout
1129 * event will be used by the worker thread to invoke the actual timeout
1130 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1131 * be performed in the timeout handler and the HBA timeout event bit shall
1132 * be cleared by the worker thread after it has taken the event bitmap out.
1133 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001134static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001135lpfc_hb_timeout(struct timer_list *t)
James Smart858c9f62007-06-17 19:56:39 -05001136{
1137 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001138 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001139 unsigned long iflag;
1140
Kees Cookf22eb4d2017-09-06 20:24:26 -07001141 phba = from_timer(phba, t, hb_tmofunc);
James Smart93996272008-08-24 21:50:30 -04001142
1143 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001144 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001145 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1146 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001147 phba->pport->work_port_events |= WORKER_HB_TMO;
1148 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1149
James Smart93996272008-08-24 21:50:30 -04001150 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001151 if (!tmo_posted)
1152 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001153 return;
1154}
1155
James Smarte59058c2008-08-24 21:49:00 -04001156/**
James Smart19ca7602010-11-20 23:11:55 -05001157 * lpfc_rrq_timeout - The RRQ-timer timeout handler
Lee Jonesfe614ac2020-07-23 13:24:22 +01001158 * @t: timer context used to obtain the pointer to lpfc hba data structure.
James Smart19ca7602010-11-20 23:11:55 -05001159 *
1160 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1161 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1162 * work-port-events bitmap and the worker thread is notified. This timeout
1163 * event will be used by the worker thread to invoke the actual timeout
1164 * handler routine, lpfc_rrq_handler. Any periodical operations will
1165 * be performed in the timeout handler and the RRQ timeout event bit shall
1166 * be cleared by the worker thread after it has taken the event bitmap out.
1167 **/
1168static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001169lpfc_rrq_timeout(struct timer_list *t)
James Smart19ca7602010-11-20 23:11:55 -05001170{
1171 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001172 unsigned long iflag;
1173
Kees Cookf22eb4d2017-09-06 20:24:26 -07001174 phba = from_timer(phba, t, rrq_tmr);
James Smart19ca7602010-11-20 23:11:55 -05001175 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001176 if (!(phba->pport->load_flag & FC_UNLOADING))
1177 phba->hba_flag |= HBA_RRQ_ACTIVE;
1178 else
1179 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001180 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001181
1182 if (!(phba->pport->load_flag & FC_UNLOADING))
1183 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001184}
1185
1186/**
James Smart3621a712009-04-06 18:47:14 -04001187 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001188 * @phba: pointer to lpfc hba data structure.
1189 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1190 *
1191 * This is the callback function to the lpfc heart-beat mailbox command.
1192 * If configured, the lpfc driver issues the heart-beat mailbox command to
1193 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1194 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1195 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1196 * heart-beat outstanding state. Once the mailbox command comes back and
1197 * no error conditions detected, the heart-beat mailbox command timer is
1198 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1199 * state is cleared for the next heart-beat. If the timer expired with the
1200 * heart-beat outstanding state set, the driver will put the HBA offline.
1201 **/
James Smart858c9f62007-06-17 19:56:39 -05001202static void
1203lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1204{
1205 unsigned long drvr_flag;
1206
1207 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarta22d73b2021-01-04 10:02:38 -08001208 phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
James Smart858c9f62007-06-17 19:56:39 -05001209 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1210
James Smarta22d73b2021-01-04 10:02:38 -08001211 /* Check and reset heart-beat timer if necessary */
James Smart858c9f62007-06-17 19:56:39 -05001212 mempool_free(pmboxq, phba->mbox_mem_pool);
1213 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1214 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001215 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001216 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001217 jiffies +
1218 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001219 return;
1220}
1221
Lee Jonesfe614ac2020-07-23 13:24:22 +01001222/*
Dick Kennedy317aeb82020-06-30 14:49:59 -07001223 * lpfc_idle_stat_delay_work - idle_stat tracking
1224 *
1225 * This routine tracks per-cq idle_stat and determines polling decisions.
1226 *
1227 * Return codes:
1228 * None
1229 **/
1230static void
1231lpfc_idle_stat_delay_work(struct work_struct *work)
1232{
1233 struct lpfc_hba *phba = container_of(to_delayed_work(work),
1234 struct lpfc_hba,
1235 idle_stat_delay_work);
1236 struct lpfc_queue *cq;
1237 struct lpfc_sli4_hdw_queue *hdwq;
1238 struct lpfc_idle_stat *idle_stat;
1239 u32 i, idle_percent;
1240 u64 wall, wall_idle, diff_wall, diff_idle, busy_time;
1241
1242 if (phba->pport->load_flag & FC_UNLOADING)
1243 return;
1244
1245 if (phba->link_state == LPFC_HBA_ERROR ||
1246 phba->pport->fc_flag & FC_OFFLINE_MODE)
1247 goto requeue;
1248
1249 for_each_present_cpu(i) {
1250 hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
1251 cq = hdwq->io_cq;
1252
1253 /* Skip if we've already handled this cq's primary CPU */
1254 if (cq->chann != i)
1255 continue;
1256
1257 idle_stat = &phba->sli4_hba.idle_stat[i];
1258
1259 /* get_cpu_idle_time returns values as running counters. Thus,
1260 * to know the amount for this period, the prior counter values
1261 * need to be subtracted from the current counter values.
1262 * From there, the idle time stat can be calculated as a
1263 * percentage of 100 - the sum of the other consumption times.
1264 */
1265 wall_idle = get_cpu_idle_time(i, &wall, 1);
1266 diff_idle = wall_idle - idle_stat->prev_idle;
1267 diff_wall = wall - idle_stat->prev_wall;
1268
1269 if (diff_wall <= diff_idle)
1270 busy_time = 0;
1271 else
1272 busy_time = diff_wall - diff_idle;
1273
1274 idle_percent = div64_u64(100 * busy_time, diff_wall);
1275 idle_percent = 100 - idle_percent;
1276
1277 if (idle_percent < 15)
1278 cq->poll_mode = LPFC_QUEUE_WORK;
1279 else
1280 cq->poll_mode = LPFC_IRQ_POLL;
1281
1282 idle_stat->prev_idle = wall_idle;
1283 idle_stat->prev_wall = wall;
1284 }
1285
1286requeue:
1287 schedule_delayed_work(&phba->idle_stat_delay_work,
1288 msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
1289}
1290
James Smart32517fc2019-01-28 11:14:33 -08001291static void
1292lpfc_hb_eq_delay_work(struct work_struct *work)
1293{
1294 struct lpfc_hba *phba = container_of(to_delayed_work(work),
1295 struct lpfc_hba, eq_delay_work);
1296 struct lpfc_eq_intr_info *eqi, *eqi_new;
1297 struct lpfc_queue *eq, *eq_next;
James Smart8156d372019-10-18 14:18:26 -07001298 unsigned char *ena_delay = NULL;
James Smart32517fc2019-01-28 11:14:33 -08001299 uint32_t usdelay;
1300 int i;
1301
1302 if (!phba->cfg_auto_imax || phba->pport->load_flag & FC_UNLOADING)
1303 return;
1304
1305 if (phba->link_state == LPFC_HBA_ERROR ||
1306 phba->pport->fc_flag & FC_OFFLINE_MODE)
1307 goto requeue;
1308
James Smart8156d372019-10-18 14:18:26 -07001309 ena_delay = kcalloc(phba->sli4_hba.num_possible_cpu, sizeof(*ena_delay),
1310 GFP_KERNEL);
1311 if (!ena_delay)
James Smart32517fc2019-01-28 11:14:33 -08001312 goto requeue;
1313
James Smart8156d372019-10-18 14:18:26 -07001314 for (i = 0; i < phba->cfg_irq_chann; i++) {
1315 /* Get the EQ corresponding to the IRQ vector */
1316 eq = phba->sli4_hba.hba_eq_hdl[i].eq;
1317 if (!eq)
1318 continue;
1319 if (eq->q_mode || eq->q_flag & HBA_EQ_DELAY_CHK) {
1320 eq->q_flag &= ~HBA_EQ_DELAY_CHK;
1321 ena_delay[eq->last_cpu] = 1;
James Smart8d34a592019-08-14 16:56:35 -07001322 }
James Smart8156d372019-10-18 14:18:26 -07001323 }
James Smart32517fc2019-01-28 11:14:33 -08001324
1325 for_each_present_cpu(i) {
James Smart32517fc2019-01-28 11:14:33 -08001326 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, i);
James Smart8156d372019-10-18 14:18:26 -07001327 if (ena_delay[i]) {
1328 usdelay = (eqi->icnt >> 10) * LPFC_EQ_DELAY_STEP;
1329 if (usdelay > LPFC_MAX_AUTO_EQ_DELAY)
1330 usdelay = LPFC_MAX_AUTO_EQ_DELAY;
1331 } else {
1332 usdelay = 0;
James Smart8d34a592019-08-14 16:56:35 -07001333 }
James Smart32517fc2019-01-28 11:14:33 -08001334
James Smart32517fc2019-01-28 11:14:33 -08001335 eqi->icnt = 0;
1336
1337 list_for_each_entry_safe(eq, eq_next, &eqi->list, cpu_list) {
James Smart8156d372019-10-18 14:18:26 -07001338 if (unlikely(eq->last_cpu != i)) {
James Smart32517fc2019-01-28 11:14:33 -08001339 eqi_new = per_cpu_ptr(phba->sli4_hba.eq_info,
1340 eq->last_cpu);
1341 list_move_tail(&eq->cpu_list, &eqi_new->list);
1342 continue;
1343 }
1344 if (usdelay != eq->q_mode)
1345 lpfc_modify_hba_eq_delay(phba, eq->hdwq, 1,
1346 usdelay);
1347 }
1348 }
1349
James Smart8156d372019-10-18 14:18:26 -07001350 kfree(ena_delay);
James Smart32517fc2019-01-28 11:14:33 -08001351
1352requeue:
1353 queue_delayed_work(phba->wq, &phba->eq_delay_work,
1354 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
1355}
1356
James Smarte59058c2008-08-24 21:49:00 -04001357/**
James Smartc4908502019-01-28 11:14:28 -08001358 * lpfc_hb_mxp_handler - Multi-XRI pools handler to adjust XRI distribution
1359 * @phba: pointer to lpfc hba data structure.
1360 *
1361 * For each heartbeat, this routine does some heuristic methods to adjust
1362 * XRI distribution. The goal is to fully utilize free XRIs.
1363 **/
1364static void lpfc_hb_mxp_handler(struct lpfc_hba *phba)
1365{
1366 u32 i;
1367 u32 hwq_count;
1368
1369 hwq_count = phba->cfg_hdw_queue;
1370 for (i = 0; i < hwq_count; i++) {
1371 /* Adjust XRIs in private pool */
1372 lpfc_adjust_pvt_pool_count(phba, i);
1373
1374 /* Adjust high watermark */
1375 lpfc_adjust_high_watermark(phba, i);
1376
1377#ifdef LPFC_MXP_STAT
1378 /* Snapshot pbl, pvt and busy count */
1379 lpfc_snapshot_mxp(phba, i);
1380#endif
1381 }
1382}
1383
James Smarte59058c2008-08-24 21:49:00 -04001384/**
James Smarta22d73b2021-01-04 10:02:38 -08001385 * lpfc_issue_hb_mbox - Issues heart-beat mailbox command
1386 * @phba: pointer to lpfc hba data structure.
1387 *
1388 * If a HB mbox is not already in progrees, this routine will allocate
1389 * a LPFC_MBOXQ_t, populate it with a MBX_HEARTBEAT (0x31) command,
1390 * and issue it. The HBA_HBEAT_INP flag means the command is in progress.
1391 **/
1392int
1393lpfc_issue_hb_mbox(struct lpfc_hba *phba)
1394{
1395 LPFC_MBOXQ_t *pmboxq;
1396 int retval;
1397
1398 /* Is a Heartbeat mbox already in progress */
1399 if (phba->hba_flag & HBA_HBEAT_INP)
1400 return 0;
1401
1402 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1403 if (!pmboxq)
1404 return -ENOMEM;
1405
1406 lpfc_heart_beat(phba, pmboxq);
1407 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1408 pmboxq->vport = phba->pport;
1409 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
1410
1411 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
1412 mempool_free(pmboxq, phba->mbox_mem_pool);
1413 return -ENXIO;
1414 }
1415 phba->hba_flag |= HBA_HBEAT_INP;
1416
1417 return 0;
1418}
1419
1420/**
1421 * lpfc_issue_hb_tmo - Signals heartbeat timer to issue mbox command
1422 * @phba: pointer to lpfc hba data structure.
1423 *
1424 * The heartbeat timer (every 5 sec) will fire. If the HBA_HBEAT_TMO
1425 * flag is set, it will force a MBX_HEARTBEAT mbox command, regardless
1426 * of the value of lpfc_enable_hba_heartbeat.
1427 * If lpfc_enable_hba_heartbeat is set, the timeout routine will always
1428 * try to issue a MBX_HEARTBEAT mbox command.
1429 **/
1430void
1431lpfc_issue_hb_tmo(struct lpfc_hba *phba)
1432{
1433 if (phba->cfg_enable_hba_heartbeat)
1434 return;
1435 phba->hba_flag |= HBA_HBEAT_TMO;
1436}
1437
1438/**
James Smart3621a712009-04-06 18:47:14 -04001439 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001440 * @phba: pointer to lpfc hba data structure.
1441 *
1442 * This is the actual HBA-timer timeout handler to be invoked by the worker
1443 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1444 * handler performs any periodic operations needed for the device. If such
1445 * periodic event has already been attended to either in the interrupt handler
1446 * or by processing slow-ring or fast-ring events within the HBA-timer
1447 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1448 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1449 * is configured and there is no heart-beat mailbox command outstanding, a
1450 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1451 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1452 * to offline.
1453 **/
James Smart858c9f62007-06-17 19:56:39 -05001454void
1455lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1456{
James Smart45ed1192009-10-02 15:17:02 -04001457 struct lpfc_vport **vports;
James Smart0ff10d42008-01-11 01:52:36 -05001458 struct lpfc_dmabuf *buf_ptr;
James Smarta22d73b2021-01-04 10:02:38 -08001459 int retval = 0;
1460 int i, tmo;
James Smart858c9f62007-06-17 19:56:39 -05001461 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001462 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001463
James Smartc4908502019-01-28 11:14:28 -08001464 if (phba->cfg_xri_rebalancing) {
1465 /* Multi-XRI pools handler */
1466 lpfc_hb_mxp_handler(phba);
1467 }
James Smart858c9f62007-06-17 19:56:39 -05001468
James Smart45ed1192009-10-02 15:17:02 -04001469 vports = lpfc_create_vport_work_array(phba);
1470 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001471 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001472 lpfc_rcv_seq_check_edtov(vports[i]);
James Smarte3ba04c2019-12-18 15:58:02 -08001473 lpfc_fdmi_change_check(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001474 }
James Smart45ed1192009-10-02 15:17:02 -04001475 lpfc_destroy_vport_work_array(phba, vports);
1476
James Smart858c9f62007-06-17 19:56:39 -05001477 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001478 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001479 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1480 return;
1481
James Smart0ff10d42008-01-11 01:52:36 -05001482 if (phba->elsbuf_cnt &&
1483 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1484 spin_lock_irq(&phba->hbalock);
1485 list_splice_init(&phba->elsbuf, &completions);
1486 phba->elsbuf_cnt = 0;
1487 phba->elsbuf_prev_cnt = 0;
1488 spin_unlock_irq(&phba->hbalock);
1489
1490 while (!list_empty(&completions)) {
1491 list_remove_head(&completions, buf_ptr,
1492 struct lpfc_dmabuf, list);
1493 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1494 kfree(buf_ptr);
1495 }
1496 }
1497 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1498
James Smart858c9f62007-06-17 19:56:39 -05001499 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001500 if (phba->cfg_enable_hba_heartbeat) {
James Smarta22d73b2021-01-04 10:02:38 -08001501 /* If IOs are completing, no need to issue a MBX_HEARTBEAT */
1502 spin_lock_irq(&phba->pport->work_port_lock);
1503 if (time_after(phba->last_completion_time +
1504 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1505 jiffies)) {
1506 spin_unlock_irq(&phba->pport->work_port_lock);
1507 if (phba->hba_flag & HBA_HBEAT_INP)
1508 tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
1509 else
1510 tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
1511 goto out;
1512 }
1513 spin_unlock_irq(&phba->pport->work_port_lock);
1514
1515 /* Check if a MBX_HEARTBEAT is already in progress */
1516 if (phba->hba_flag & HBA_HBEAT_INP) {
1517 /*
1518 * If heart beat timeout called with HBA_HBEAT_INP set
1519 * we need to give the hb mailbox cmd a chance to
1520 * complete or TMO.
1521 */
1522 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1523 "0459 Adapter heartbeat still outstanding: "
1524 "last compl time was %d ms.\n",
1525 jiffies_to_msecs(jiffies
1526 - phba->last_completion_time));
1527 tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
1528 } else {
James Smartbc739052010-08-04 16:11:18 -04001529 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1530 (list_empty(&psli->mboxq))) {
James Smart13815c82008-01-11 01:52:48 -05001531
James Smarta22d73b2021-01-04 10:02:38 -08001532 retval = lpfc_issue_hb_mbox(phba);
1533 if (retval) {
1534 tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
1535 goto out;
James Smartbc739052010-08-04 16:11:18 -04001536 }
1537 phba->skipped_hb = 0;
James Smartbc739052010-08-04 16:11:18 -04001538 } else if (time_before_eq(phba->last_completion_time,
1539 phba->skipped_hb)) {
1540 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1541 "2857 Last completion time not "
1542 " updated in %d ms\n",
1543 jiffies_to_msecs(jiffies
1544 - phba->last_completion_time));
1545 } else
1546 phba->skipped_hb = jiffies;
1547
James Smarta22d73b2021-01-04 10:02:38 -08001548 tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
1549 goto out;
James Smart858c9f62007-06-17 19:56:39 -05001550 }
James Smart4258e982015-12-16 18:11:58 -05001551 } else {
James Smarta22d73b2021-01-04 10:02:38 -08001552 /* Check to see if we want to force a MBX_HEARTBEAT */
1553 if (phba->hba_flag & HBA_HBEAT_TMO) {
1554 retval = lpfc_issue_hb_mbox(phba);
1555 if (retval)
1556 tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
1557 else
1558 tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
1559 goto out;
1560 }
1561 tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
James Smart858c9f62007-06-17 19:56:39 -05001562 }
James Smarta22d73b2021-01-04 10:02:38 -08001563out:
1564 mod_timer(&phba->hb_tmofunc, jiffies + msecs_to_jiffies(tmo));
James Smart858c9f62007-06-17 19:56:39 -05001565}
1566
James Smarte59058c2008-08-24 21:49:00 -04001567/**
James Smart3621a712009-04-06 18:47:14 -04001568 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001569 * @phba: pointer to lpfc hba data structure.
1570 *
1571 * This routine is called to bring the HBA offline when HBA hardware error
1572 * other than Port Error 6 has been detected.
1573 **/
James Smart09372822008-01-11 01:52:54 -05001574static void
1575lpfc_offline_eratt(struct lpfc_hba *phba)
1576{
1577 struct lpfc_sli *psli = &phba->sli;
1578
1579 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001580 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001581 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001582 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001583
1584 lpfc_offline(phba);
1585 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001586 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001587 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001588 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001589 lpfc_hba_down_post(phba);
1590 lpfc_sli_brdready(phba, HS_MBRDY);
1591 lpfc_unblock_mgmt_io(phba);
1592 phba->link_state = LPFC_HBA_ERROR;
1593 return;
1594}
1595
James Smarte59058c2008-08-24 21:49:00 -04001596/**
James Smartda0436e2009-05-22 14:51:39 -04001597 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1598 * @phba: pointer to lpfc hba data structure.
1599 *
1600 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1601 * other than Port Error 6 has been detected.
1602 **/
James Smarta88dbb62013-04-17 20:18:39 -04001603void
James Smartda0436e2009-05-22 14:51:39 -04001604lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1605{
James Smart946727d2015-04-07 15:07:09 -04001606 spin_lock_irq(&phba->hbalock);
1607 phba->link_state = LPFC_HBA_ERROR;
1608 spin_unlock_irq(&phba->hbalock);
1609
James Smart618a5232012-06-12 13:54:36 -04001610 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartc00f62e2019-08-14 16:57:11 -07001611 lpfc_sli_flush_io_rings(phba);
James Smartda0436e2009-05-22 14:51:39 -04001612 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001613 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001614 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001615}
1616
1617/**
James Smarta257bf92009-04-06 18:48:10 -04001618 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1619 * @phba: pointer to lpfc hba data structure.
1620 *
1621 * This routine is invoked to handle the deferred HBA hardware error
1622 * conditions. This type of error is indicated by HBA by setting ER1
1623 * and another ER bit in the host status register. The driver will
1624 * wait until the ER1 bit clears before handling the error condition.
1625 **/
1626static void
1627lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1628{
1629 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001630 struct lpfc_sli *psli = &phba->sli;
1631
James Smartf4b4c682009-05-22 14:53:12 -04001632 /* If the pci channel is offline, ignore possible errors,
1633 * since we cannot communicate with the pci card anyway.
1634 */
1635 if (pci_channel_offline(phba->pcidev)) {
1636 spin_lock_irq(&phba->hbalock);
1637 phba->hba_flag &= ~DEFER_ERATT;
1638 spin_unlock_irq(&phba->hbalock);
1639 return;
1640 }
1641
Dick Kennedy372c1872020-06-30 14:50:00 -07001642 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1643 "0479 Deferred Adapter Hardware Error "
1644 "Data: x%x x%x x%x\n",
1645 phba->work_hs, phba->work_status[0],
1646 phba->work_status[1]);
James Smarta257bf92009-04-06 18:48:10 -04001647
1648 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001649 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001650 spin_unlock_irq(&phba->hbalock);
1651
1652
1653 /*
1654 * Firmware stops when it triggred erratt. That could cause the I/Os
1655 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1656 * SCSI layer retry it after re-establishing link.
1657 */
James Smartdb55fba2014-04-04 13:52:02 -04001658 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001659
1660 /*
1661 * There was a firmware error. Take the hba offline and then
1662 * attempt to restart it.
1663 */
James Smart618a5232012-06-12 13:54:36 -04001664 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001665 lpfc_offline(phba);
1666
1667 /* Wait for the ER1 bit to clear.*/
1668 while (phba->work_hs & HS_FFER1) {
1669 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001670 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1671 phba->work_hs = UNPLUG_ERR ;
1672 break;
1673 }
James Smarta257bf92009-04-06 18:48:10 -04001674 /* If driver is unloading let the worker thread continue */
1675 if (phba->pport->load_flag & FC_UNLOADING) {
1676 phba->work_hs = 0;
1677 break;
1678 }
1679 }
1680
1681 /*
1682 * This is to ptrotect against a race condition in which
1683 * first write to the host attention register clear the
1684 * host status register.
1685 */
1686 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1687 phba->work_hs = old_host_status & ~HS_FFER1;
1688
James Smart3772a992009-05-22 14:50:54 -04001689 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001690 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001691 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001692 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1693 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1694}
1695
James Smart3772a992009-05-22 14:50:54 -04001696static void
1697lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1698{
1699 struct lpfc_board_event_header board_event;
1700 struct Scsi_Host *shost;
1701
1702 board_event.event_type = FC_REG_BOARD_EVENT;
1703 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1704 shost = lpfc_shost_from_vport(phba->pport);
1705 fc_host_post_vendor_event(shost, fc_get_event_number(),
1706 sizeof(board_event),
1707 (char *) &board_event,
1708 LPFC_NL_VENDOR_ID);
1709}
1710
James Smarta257bf92009-04-06 18:48:10 -04001711/**
James Smart3772a992009-05-22 14:50:54 -04001712 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001713 * @phba: pointer to lpfc hba data structure.
1714 *
1715 * This routine is invoked to handle the following HBA hardware error
1716 * conditions:
1717 * 1 - HBA error attention interrupt
1718 * 2 - DMA ring index out of range
1719 * 3 - Mailbox command came back as unknown
1720 **/
James Smart3772a992009-05-22 14:50:54 -04001721static void
1722lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001723{
James Smart2e0fef82007-06-17 19:56:36 -05001724 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001725 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001726 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001727 unsigned long temperature;
1728 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001729 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001730
Linas Vepstas8d63f372007-02-14 14:28:36 -06001731 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001732 * since we cannot communicate with the pci card anyway.
1733 */
1734 if (pci_channel_offline(phba->pcidev)) {
1735 spin_lock_irq(&phba->hbalock);
1736 phba->hba_flag &= ~DEFER_ERATT;
1737 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001738 return;
James Smart3772a992009-05-22 14:50:54 -04001739 }
1740
James Smart13815c82008-01-11 01:52:48 -05001741 /* If resets are disabled then leave the HBA alone and return */
1742 if (!phba->cfg_enable_hba_reset)
1743 return;
dea31012005-04-17 16:05:31 -05001744
James Smartea2151b2008-09-07 11:52:10 -04001745 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001746 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001747
James Smarta257bf92009-04-06 18:48:10 -04001748 if (phba->hba_flag & DEFER_ERATT)
1749 lpfc_handle_deferred_eratt(phba);
1750
James Smartdcf2a4e2010-09-29 11:18:53 -04001751 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1752 if (phba->work_hs & HS_FFER6)
1753 /* Re-establishing Link */
1754 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1755 "1301 Re-establishing Link "
1756 "Data: x%x x%x x%x\n",
1757 phba->work_hs, phba->work_status[0],
1758 phba->work_status[1]);
1759 if (phba->work_hs & HS_FFER8)
1760 /* Device Zeroization */
1761 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1762 "2861 Host Authentication device "
1763 "zeroization Data:x%x x%x x%x\n",
1764 phba->work_hs, phba->work_status[0],
1765 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001766
James Smart92d7f7b2007-06-17 19:56:38 -05001767 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001768 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001769 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001770
1771 /*
1772 * Firmware stops when it triggled erratt with HS_FFER6.
1773 * That could cause the I/Os dropped by the firmware.
1774 * Error iocb (I/O) on txcmplq and let the SCSI layer
1775 * retry it after re-establishing link.
1776 */
James Smartdb55fba2014-04-04 13:52:02 -04001777 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001778
dea31012005-04-17 16:05:31 -05001779 /*
1780 * There was a firmware error. Take the hba offline and then
1781 * attempt to restart it.
1782 */
James Smart618a5232012-06-12 13:54:36 -04001783 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001784 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001785 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001786 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001787 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001788 return;
1789 }
James Smart46fa3112007-04-25 09:51:45 -04001790 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001791 } else if (phba->work_hs & HS_CRIT_TEMP) {
1792 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1793 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1794 temp_event_data.event_code = LPFC_CRIT_TEMP;
1795 temp_event_data.data = (uint32_t)temperature;
1796
Dick Kennedy372c1872020-06-30 14:50:00 -07001797 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd7c255b2008-08-24 21:50:00 -04001798 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001799 "(%ld), taking this port offline "
1800 "Data: x%x x%x x%x\n",
1801 temperature, phba->work_hs,
1802 phba->work_status[0], phba->work_status[1]);
1803
1804 shost = lpfc_shost_from_vport(phba->pport);
1805 fc_host_post_vendor_event(shost, fc_get_event_number(),
1806 sizeof(temp_event_data),
1807 (char *) &temp_event_data,
1808 SCSI_NL_VID_TYPE_PCI
1809 | PCI_VENDOR_ID_EMULEX);
1810
James Smart7af67052007-10-27 13:38:11 -04001811 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001812 phba->over_temp_state = HBA_OVER_TEMP;
1813 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001814 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001815
dea31012005-04-17 16:05:31 -05001816 } else {
1817 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001818 * failure is a value other than FFER6. Do not call the offline
1819 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001820 */
Dick Kennedy372c1872020-06-30 14:50:00 -07001821 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001822 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001823 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001824 phba->work_hs,
dea31012005-04-17 16:05:31 -05001825 phba->work_status[0], phba->work_status[1]);
1826
James Smartd2873e42006-08-18 17:46:43 -04001827 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001828 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001829 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001830 sizeof(event_data), (char *) &event_data,
1831 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1832
James Smart09372822008-01-11 01:52:54 -05001833 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001834 }
James Smart93996272008-08-24 21:50:30 -04001835 return;
dea31012005-04-17 16:05:31 -05001836}
1837
James Smarte59058c2008-08-24 21:49:00 -04001838/**
James Smart618a5232012-06-12 13:54:36 -04001839 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1840 * @phba: pointer to lpfc hba data structure.
1841 * @mbx_action: flag for mailbox shutdown action.
Lee Jonesfe614ac2020-07-23 13:24:22 +01001842 * @en_rn_msg: send reset/port recovery message.
James Smart618a5232012-06-12 13:54:36 -04001843 * This routine is invoked to perform an SLI4 port PCI function reset in
1844 * response to port status register polling attention. It waits for port
1845 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1846 * During this process, interrupt vectors are freed and later requested
1847 * for handling possible port resource change.
1848 **/
1849static int
James Smarte10b2022014-02-20 09:57:43 -05001850lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1851 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001852{
1853 int rc;
1854 uint32_t intr_mode;
1855
James Smart27d6ac02018-02-22 08:18:42 -08001856 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart65791f12016-07-06 12:35:56 -07001857 LPFC_SLI_INTF_IF_TYPE_2) {
1858 /*
1859 * On error status condition, driver need to wait for port
1860 * ready before performing reset.
1861 */
1862 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001863 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001864 return rc;
James Smart618a5232012-06-12 13:54:36 -04001865 }
James Smart0e916ee2016-07-06 12:36:06 -07001866
James Smart65791f12016-07-06 12:35:56 -07001867 /* need reset: attempt for port recovery */
1868 if (en_rn_msg)
James Smart0b3ad322021-01-04 10:02:39 -08001869 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart65791f12016-07-06 12:35:56 -07001870 "2887 Reset Needed: Attempting Port "
1871 "Recovery...\n");
James Smart3ba62162021-01-04 10:02:31 -08001872
1873 /* If we are no wait, the HBA has been reset and is not
1874 * functional, thus we should clear LPFC_SLI_ACTIVE flag.
1875 */
1876 if (mbx_action == LPFC_MBX_NO_WAIT) {
1877 spin_lock_irq(&phba->hbalock);
1878 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
1879 spin_unlock_irq(&phba->hbalock);
1880 }
1881
James Smart65791f12016-07-06 12:35:56 -07001882 lpfc_offline_prep(phba, mbx_action);
James Smartc00f62e2019-08-14 16:57:11 -07001883 lpfc_sli_flush_io_rings(phba);
James Smart65791f12016-07-06 12:35:56 -07001884 lpfc_offline(phba);
1885 /* release interrupt for possible resource change */
1886 lpfc_sli4_disable_intr(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08001887 rc = lpfc_sli_brdrestart(phba);
1888 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001889 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5a9eeff2018-11-29 16:09:32 -08001890 "6309 Failed to restart board\n");
1891 return rc;
1892 }
James Smart65791f12016-07-06 12:35:56 -07001893 /* request and enable interrupt */
1894 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1895 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001896 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07001897 "3175 Failed to enable interrupt\n");
1898 return -EIO;
1899 }
1900 phba->intr_mode = intr_mode;
1901 rc = lpfc_online(phba);
1902 if (rc == 0)
1903 lpfc_unblock_mgmt_io(phba);
1904
James Smart618a5232012-06-12 13:54:36 -04001905 return rc;
1906}
1907
1908/**
James Smartda0436e2009-05-22 14:51:39 -04001909 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1910 * @phba: pointer to lpfc hba data structure.
1911 *
1912 * This routine is invoked to handle the SLI4 HBA hardware error attention
1913 * conditions.
1914 **/
1915static void
1916lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1917{
1918 struct lpfc_vport *vport = phba->pport;
1919 uint32_t event_data;
1920 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001921 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001922 struct lpfc_register portstat_reg = {0};
1923 uint32_t reg_err1, reg_err2;
1924 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001925 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001926 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001927 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001928 struct lpfc_register portsmphr_reg;
1929 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001930
1931 /* If the pci channel is offline, ignore possible errors, since
1932 * we cannot communicate with the pci card anyway.
1933 */
James Smart32a93102019-03-12 16:30:13 -07001934 if (pci_channel_offline(phba->pcidev)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001935 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32a93102019-03-12 16:30:13 -07001936 "3166 pci channel is offline\n");
1937 lpfc_sli4_offline_eratt(phba);
James Smartda0436e2009-05-22 14:51:39 -04001938 return;
James Smart32a93102019-03-12 16:30:13 -07001939 }
James Smartda0436e2009-05-22 14:51:39 -04001940
James Smart65791f12016-07-06 12:35:56 -07001941 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001942 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1943 switch (if_type) {
1944 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001945 pci_rd_rc1 = lpfc_readl(
1946 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1947 &uerrlo_reg);
1948 pci_rd_rc2 = lpfc_readl(
1949 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1950 &uemasklo_reg);
1951 /* consider PCI bus read error as pci_channel_offline */
1952 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1953 return;
James Smart65791f12016-07-06 12:35:56 -07001954 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1955 lpfc_sli4_offline_eratt(phba);
1956 return;
1957 }
Dick Kennedy372c1872020-06-30 14:50:00 -07001958 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07001959 "7623 Checking UE recoverable");
1960
1961 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1962 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1963 &portsmphr_reg.word0))
1964 continue;
1965
1966 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1967 &portsmphr_reg);
1968 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1969 LPFC_PORT_SEM_UE_RECOVERABLE)
1970 break;
1971 /*Sleep for 1Sec, before checking SEMAPHORE */
1972 msleep(1000);
1973 }
1974
Dick Kennedy372c1872020-06-30 14:50:00 -07001975 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07001976 "4827 smphr_port_status x%x : Waited %dSec",
1977 smphr_port_status, i);
1978
1979 /* Recoverable UE, reset the HBA device */
1980 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1981 LPFC_PORT_SEM_UE_RECOVERABLE) {
1982 for (i = 0; i < 20; i++) {
1983 msleep(1000);
1984 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1985 &portsmphr_reg.word0) &&
1986 (LPFC_POST_STAGE_PORT_READY ==
1987 bf_get(lpfc_port_smphr_port_status,
1988 &portsmphr_reg))) {
1989 rc = lpfc_sli4_port_sta_fn_reset(phba,
1990 LPFC_MBX_NO_WAIT, en_rn_msg);
1991 if (rc == 0)
1992 return;
Dick Kennedy372c1872020-06-30 14:50:00 -07001993 lpfc_printf_log(phba, KERN_ERR,
1994 LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07001995 "4215 Failed to recover UE");
1996 break;
1997 }
1998 }
1999 }
Dick Kennedy372c1872020-06-30 14:50:00 -07002000 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07002001 "7624 Firmware not ready: Failing UE recovery,"
2002 " waited %dSec", i);
James Smart8c24a4f2019-08-14 16:56:53 -07002003 phba->link_state = LPFC_HBA_ERROR;
James Smart2fcee4b2010-12-15 17:57:46 -05002004 break;
James Smart946727d2015-04-07 15:07:09 -04002005
James Smart2fcee4b2010-12-15 17:57:46 -05002006 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08002007 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2e90f4b2011-12-13 13:22:37 -05002008 pci_rd_rc1 = lpfc_readl(
2009 phba->sli4_hba.u.if_type2.STATUSregaddr,
2010 &portstat_reg.word0);
2011 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05002012 if (pci_rd_rc1 == -EIO) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002013 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6b5151f2012-01-18 16:24:06 -05002014 "3151 PCI bus read access failure: x%x\n",
2015 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart32a93102019-03-12 16:30:13 -07002016 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05002017 return;
James Smart6b5151f2012-01-18 16:24:06 -05002018 }
James Smart2e90f4b2011-12-13 13:22:37 -05002019 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
2020 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05002021 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002022 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2023 "2889 Port Overtemperature event, "
2024 "taking port offline Data: x%x x%x\n",
2025 reg_err1, reg_err2);
James Smart946727d2015-04-07 15:07:09 -04002026
James Smart310429e2016-07-06 12:35:54 -07002027 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04002028 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
2029 temp_event_data.event_code = LPFC_CRIT_TEMP;
2030 temp_event_data.data = 0xFFFFFFFF;
2031
2032 shost = lpfc_shost_from_vport(phba->pport);
2033 fc_host_post_vendor_event(shost, fc_get_event_number(),
2034 sizeof(temp_event_data),
2035 (char *)&temp_event_data,
2036 SCSI_NL_VID_TYPE_PCI
2037 | PCI_VENDOR_ID_EMULEX);
2038
James Smart2fcee4b2010-12-15 17:57:46 -05002039 spin_lock_irq(&phba->hbalock);
2040 phba->over_temp_state = HBA_OVER_TEMP;
2041 spin_unlock_irq(&phba->hbalock);
2042 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04002043 return;
James Smart2fcee4b2010-12-15 17:57:46 -05002044 }
James Smart2e90f4b2011-12-13 13:22:37 -05002045 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05002046 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002047 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte10b2022014-02-20 09:57:43 -05002048 "3143 Port Down: Firmware Update "
2049 "Detected\n");
2050 en_rn_msg = false;
2051 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05002052 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
Dick Kennedy372c1872020-06-30 14:50:00 -07002053 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2e90f4b2011-12-13 13:22:37 -05002054 "3144 Port Down: Debug Dump\n");
2055 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
2056 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
Dick Kennedy372c1872020-06-30 14:50:00 -07002057 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2e90f4b2011-12-13 13:22:37 -05002058 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04002059
James Smart946727d2015-04-07 15:07:09 -04002060 /* If resets are disabled then leave the HBA alone and return */
2061 if (!phba->cfg_enable_hba_reset)
2062 return;
2063
James Smart618a5232012-06-12 13:54:36 -04002064 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05002065 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
2066 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04002067 if (rc == 0) {
2068 /* don't report event on forced debug dump */
2069 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
2070 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
2071 return;
2072 else
2073 break;
James Smart2fcee4b2010-12-15 17:57:46 -05002074 }
James Smart618a5232012-06-12 13:54:36 -04002075 /* fall through for not able to recover */
Dick Kennedy372c1872020-06-30 14:50:00 -07002076 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8c24a4f2019-08-14 16:56:53 -07002077 "3152 Unrecoverable error\n");
2078 phba->link_state = LPFC_HBA_ERROR;
James Smart2fcee4b2010-12-15 17:57:46 -05002079 break;
2080 case LPFC_SLI_INTF_IF_TYPE_1:
2081 default:
2082 break;
2083 }
James Smart2e90f4b2011-12-13 13:22:37 -05002084 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2085 "3123 Report dump event to upper layer\n");
2086 /* Send an internal error event to mgmt application */
2087 lpfc_board_errevt_to_mgmt(phba);
2088
2089 event_data = FC_REG_DUMP_EVENT;
2090 shost = lpfc_shost_from_vport(vport);
2091 fc_host_post_vendor_event(shost, fc_get_event_number(),
2092 sizeof(event_data), (char *) &event_data,
2093 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04002094}
2095
2096/**
2097 * lpfc_handle_eratt - Wrapper func for handling hba error attention
2098 * @phba: pointer to lpfc HBA data structure.
2099 *
2100 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
2101 * routine from the API jump table function pointer from the lpfc_hba struct.
2102 *
2103 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002104 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04002105 * Any other value - error.
2106 **/
2107void
2108lpfc_handle_eratt(struct lpfc_hba *phba)
2109{
2110 (*phba->lpfc_handle_eratt)(phba);
2111}
2112
2113/**
James Smart3621a712009-04-06 18:47:14 -04002114 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04002115 * @phba: pointer to lpfc hba data structure.
2116 *
2117 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08002118 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002119 **/
dea31012005-04-17 16:05:31 -05002120void
James Smart2e0fef82007-06-17 19:56:36 -05002121lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002122{
James Smart2e0fef82007-06-17 19:56:36 -05002123 struct lpfc_vport *vport = phba->pport;
2124 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002125 LPFC_MBOXQ_t *pmb;
2126 volatile uint32_t control;
2127 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05002128 int rc = 0;
dea31012005-04-17 16:05:31 -05002129
2130 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002131 if (!pmb) {
2132 rc = 1;
dea31012005-04-17 16:05:31 -05002133 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05002134 }
dea31012005-04-17 16:05:31 -05002135
2136 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002137 if (!mp) {
2138 rc = 2;
dea31012005-04-17 16:05:31 -05002139 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05002140 }
dea31012005-04-17 16:05:31 -05002141
2142 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05002143 if (!mp->virt) {
2144 rc = 3;
dea31012005-04-17 16:05:31 -05002145 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05002146 }
dea31012005-04-17 16:05:31 -05002147
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05002148 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04002149 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05002150
2151 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05002152 lpfc_read_topology(phba, pmb, mp);
2153 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05002154 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04002155 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08002156 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04002157 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05002158 if (rc == MBX_NOT_FINISHED) {
2159 rc = 4;
James Smart14691152006-12-02 13:34:28 -05002160 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05002161 }
dea31012005-04-17 16:05:31 -05002162
2163 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05002164 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002165 writel(HA_LATT, phba->HAregaddr);
2166 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002167 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002168
2169 return;
2170
James Smart14691152006-12-02 13:34:28 -05002171lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08002172 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05002173 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002174lpfc_handle_latt_free_mp:
2175 kfree(mp);
2176lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04002177 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002178lpfc_handle_latt_err_exit:
2179 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05002180 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002181 psli->sli_flag |= LPFC_PROCESS_LA;
2182 control = readl(phba->HCregaddr);
2183 control |= HC_LAINT_ENA;
2184 writel(control, phba->HCregaddr);
2185 readl(phba->HCregaddr); /* flush */
2186
2187 /* Clear Link Attention in HA REG */
2188 writel(HA_LATT, phba->HAregaddr);
2189 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002190 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002191 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002192 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002193
Dick Kennedy372c1872020-06-30 14:50:00 -07002194 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2195 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05002196
2197 return;
2198}
2199
James Smarte59058c2008-08-24 21:49:00 -04002200/**
James Smart3621a712009-04-06 18:47:14 -04002201 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04002202 * @phba: pointer to lpfc hba data structure.
2203 * @vpd: pointer to the vital product data.
2204 * @len: length of the vital product data in bytes.
2205 *
2206 * This routine parses the Vital Product Data (VPD). The VPD is treated as
2207 * an array of characters. In this routine, the ModelName, ProgramType, and
2208 * ModelDesc, etc. fields of the phba data structure will be populated.
2209 *
2210 * Return codes
2211 * 0 - pointer to the VPD passed in is NULL
2212 * 1 - success
2213 **/
James Smart3772a992009-05-22 14:50:54 -04002214int
James Smart2e0fef82007-06-17 19:56:36 -05002215lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05002216{
2217 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05002218 int Length;
dea31012005-04-17 16:05:31 -05002219 int i, j;
2220 int finished = 0;
2221 int index = 0;
2222
2223 if (!vpd)
2224 return 0;
2225
2226 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05002227 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002228 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05002229 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2230 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002231 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05002232 switch (vpd[index]) {
2233 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002234 case 0x91:
dea31012005-04-17 16:05:31 -05002235 index += 1;
2236 lenlo = vpd[index];
2237 index += 1;
2238 lenhi = vpd[index];
2239 index += 1;
2240 i = ((((unsigned short)lenhi) << 8) + lenlo);
2241 index += i;
2242 break;
2243 case 0x90:
2244 index += 1;
2245 lenlo = vpd[index];
2246 index += 1;
2247 lenhi = vpd[index];
2248 index += 1;
2249 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002250 if (Length > len - index)
2251 Length = len - index;
dea31012005-04-17 16:05:31 -05002252 while (Length > 0) {
2253 /* Look for Serial Number */
2254 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2255 index += 2;
2256 i = vpd[index];
2257 index += 1;
2258 j = 0;
2259 Length -= (3+i);
2260 while(i--) {
2261 phba->SerialNumber[j++] = vpd[index++];
2262 if (j == 31)
2263 break;
2264 }
2265 phba->SerialNumber[j] = 0;
2266 continue;
2267 }
2268 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2269 phba->vpd_flag |= VPD_MODEL_DESC;
2270 index += 2;
2271 i = vpd[index];
2272 index += 1;
2273 j = 0;
2274 Length -= (3+i);
2275 while(i--) {
2276 phba->ModelDesc[j++] = vpd[index++];
2277 if (j == 255)
2278 break;
2279 }
2280 phba->ModelDesc[j] = 0;
2281 continue;
2282 }
2283 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2284 phba->vpd_flag |= VPD_MODEL_NAME;
2285 index += 2;
2286 i = vpd[index];
2287 index += 1;
2288 j = 0;
2289 Length -= (3+i);
2290 while(i--) {
2291 phba->ModelName[j++] = vpd[index++];
2292 if (j == 79)
2293 break;
2294 }
2295 phba->ModelName[j] = 0;
2296 continue;
2297 }
2298 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2299 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2300 index += 2;
2301 i = vpd[index];
2302 index += 1;
2303 j = 0;
2304 Length -= (3+i);
2305 while(i--) {
2306 phba->ProgramType[j++] = vpd[index++];
2307 if (j == 255)
2308 break;
2309 }
2310 phba->ProgramType[j] = 0;
2311 continue;
2312 }
2313 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2314 phba->vpd_flag |= VPD_PORT;
2315 index += 2;
2316 i = vpd[index];
2317 index += 1;
2318 j = 0;
2319 Length -= (3+i);
2320 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002321 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2322 (phba->sli4_hba.pport_name_sta ==
2323 LPFC_SLI4_PPNAME_GET)) {
2324 j++;
2325 index++;
2326 } else
2327 phba->Port[j++] = vpd[index++];
2328 if (j == 19)
2329 break;
dea31012005-04-17 16:05:31 -05002330 }
James Smartcd1c8302011-10-10 21:33:25 -04002331 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2332 (phba->sli4_hba.pport_name_sta ==
2333 LPFC_SLI4_PPNAME_NON))
2334 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002335 continue;
2336 }
2337 else {
2338 index += 2;
2339 i = vpd[index];
2340 index += 1;
2341 index += i;
2342 Length -= (3 + i);
2343 }
2344 }
2345 finished = 0;
2346 break;
2347 case 0x78:
2348 finished = 1;
2349 break;
2350 default:
2351 index ++;
2352 break;
2353 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002354 }
dea31012005-04-17 16:05:31 -05002355
2356 return(1);
2357}
2358
James Smarte59058c2008-08-24 21:49:00 -04002359/**
James Smart3621a712009-04-06 18:47:14 -04002360 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002361 * @phba: pointer to lpfc hba data structure.
2362 * @mdp: pointer to the data structure to hold the derived model name.
2363 * @descp: pointer to the data structure to hold the derived description.
2364 *
2365 * This routine retrieves HBA's description based on its registered PCI device
2366 * ID. The @descp passed into this function points to an array of 256 chars. It
2367 * shall be returned with the model name, maximum speed, and the host bus type.
2368 * The @mdp passed into this function points to an array of 80 chars. When the
2369 * function returns, the @mdp will be filled with the model name.
2370 **/
dea31012005-04-17 16:05:31 -05002371static void
James Smart2e0fef82007-06-17 19:56:36 -05002372lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002373{
2374 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002375 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002376 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002377 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002378 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002379 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002380 char *name;
2381 char *bus;
2382 char *function;
2383 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002384
2385 if (mdp && mdp[0] != '\0'
2386 && descp && descp[0] != '\0')
2387 return;
2388
James Smartfbd8a6b2018-02-22 08:18:45 -08002389 if (phba->lmt & LMT_64Gb)
2390 max_speed = 64;
2391 else if (phba->lmt & LMT_32Gb)
James Smartd38dd522015-08-31 16:48:17 -04002392 max_speed = 32;
2393 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002394 max_speed = 16;
2395 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002396 max_speed = 10;
2397 else if (phba->lmt & LMT_8Gb)
2398 max_speed = 8;
2399 else if (phba->lmt & LMT_4Gb)
2400 max_speed = 4;
2401 else if (phba->lmt & LMT_2Gb)
2402 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002403 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002404 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002405 else
2406 max_speed = 0;
dea31012005-04-17 16:05:31 -05002407
2408 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002409
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002410 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002411 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002412 m = (typeof(m)){"LP6000", "PCI",
2413 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002414 break;
dea31012005-04-17 16:05:31 -05002415 case PCI_DEVICE_ID_SUPERFLY:
2416 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002417 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002418 else
James Smart12222f42014-05-21 08:05:19 -04002419 m = (typeof(m)){"LP7000E", "PCI", ""};
2420 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002421 break;
2422 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002423 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002424 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002425 break;
2426 case PCI_DEVICE_ID_CENTAUR:
2427 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002428 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002429 else
James Smart12222f42014-05-21 08:05:19 -04002430 m = (typeof(m)){"LP9000", "PCI", ""};
2431 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002432 break;
2433 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002434 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002435 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002436 break;
2437 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002438 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002439 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002440 break;
2441 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002442 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002443 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002444 break;
2445 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002446 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002447 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002448 break;
2449 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002450 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002451 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002452 break;
2453 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002454 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002455 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002456 break;
2457 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002458 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002459 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002460 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002461 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002462 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002463 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002464 break;
2465 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002466 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002467 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002468 break;
2469 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002470 m = (typeof(m)){"LPe1000", "PCIe",
2471 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002472 break;
2473 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002474 m = (typeof(m)){"LPe1000-SP", "PCIe",
2475 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002476 break;
2477 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002478 m = (typeof(m)){"LPe1002-SP", "PCIe",
2479 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002480 break;
dea31012005-04-17 16:05:31 -05002481 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002482 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002483 break;
2484 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002485 m = (typeof(m)){"LP111", "PCI-X2",
2486 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002487 break;
2488 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002489 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002490 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002491 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002492 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002493 break;
2494 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002495 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002496 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002497 break;
dea31012005-04-17 16:05:31 -05002498 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002499 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002500 break;
2501 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002502 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002503 break;
2504 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002505 m = (typeof(m)){"LP101", "PCI-X",
2506 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002507 break;
2508 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002509 m = (typeof(m)){"LP10000-S", "PCI",
2510 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002511 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002512 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002513 m = (typeof(m)){"LP11000-S", "PCI-X2",
2514 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002515 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002516 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002517 m = (typeof(m)){"LPe11000-S", "PCIe",
2518 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002519 break;
James Smartb87eab32007-04-25 09:53:28 -04002520 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002521 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002522 break;
2523 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002524 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002525 break;
2526 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002527 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002528 break;
2529 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002530 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002531 break;
2532 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002533 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002534 break;
2535 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002536 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002537 break;
James Smart84774a42008-08-24 21:50:06 -04002538 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002539 m = (typeof(m)){"LP21000", "PCIe",
2540 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002541 GE = 1;
2542 break;
2543 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002544 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002545 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002546 break;
2547 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002548 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002549 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002550 break;
2551 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002552 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002553 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002554 break;
James Smartda0436e2009-05-22 14:51:39 -04002555 case PCI_DEVICE_ID_TIGERSHARK:
2556 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002557 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002558 break;
James Smarta747c9c2009-11-18 15:41:10 -05002559 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002560 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002561 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2562 break;
2563 case PCI_DEVICE_ID_FALCON:
2564 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2565 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002566 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002567 case PCI_DEVICE_ID_BALIUS:
2568 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002569 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002570 break;
James Smart085c6472010-11-20 23:11:37 -05002571 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002572 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002573 break;
James Smart12222f42014-05-21 08:05:19 -04002574 case PCI_DEVICE_ID_LANCER_FC_VF:
2575 m = (typeof(m)){"LPe16000", "PCIe",
2576 "Obsolete, Unsupported Fibre Channel Adapter"};
2577 break;
James Smart085c6472010-11-20 23:11:37 -05002578 case PCI_DEVICE_ID_LANCER_FCOE:
2579 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002580 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002581 break;
James Smart12222f42014-05-21 08:05:19 -04002582 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2583 oneConnect = 1;
2584 m = (typeof(m)){"OCe15100", "PCIe",
2585 "Obsolete, Unsupported FCoE"};
2586 break;
James Smartd38dd522015-08-31 16:48:17 -04002587 case PCI_DEVICE_ID_LANCER_G6_FC:
2588 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2589 break;
James Smartc238b9b2018-02-22 08:18:44 -08002590 case PCI_DEVICE_ID_LANCER_G7_FC:
2591 m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
2592 break;
James Smartf8cafd32012-08-03 12:42:51 -04002593 case PCI_DEVICE_ID_SKYHAWK:
2594 case PCI_DEVICE_ID_SKYHAWK_VF:
2595 oneConnect = 1;
2596 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2597 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002598 default:
James Smarta747c9c2009-11-18 15:41:10 -05002599 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002600 break;
dea31012005-04-17 16:05:31 -05002601 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002602
2603 if (mdp && mdp[0] == '\0')
2604 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002605 /*
2606 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002607 * and we put the port number on the end
2608 */
2609 if (descp && descp[0] == '\0') {
2610 if (oneConnect)
2611 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002612 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002613 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002614 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002615 else if (max_speed == 0)
2616 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002617 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002618 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002619 else
2620 snprintf(descp, 255,
2621 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002622 m.name, max_speed, (GE) ? "GE" : "Gb",
2623 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002624 }
dea31012005-04-17 16:05:31 -05002625}
2626
James Smarte59058c2008-08-24 21:49:00 -04002627/**
James Smart3621a712009-04-06 18:47:14 -04002628 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002629 * @phba: pointer to lpfc hba data structure.
2630 * @pring: pointer to a IOCB ring.
2631 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2632 *
2633 * This routine posts a given number of IOCBs with the associated DMA buffer
2634 * descriptors specified by the cnt argument to the given IOCB ring.
2635 *
2636 * Return codes
2637 * The number of IOCBs NOT able to be posted to the IOCB ring.
2638 **/
dea31012005-04-17 16:05:31 -05002639int
James Smart495a7142008-06-14 22:52:59 -04002640lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002641{
2642 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002643 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002644 struct lpfc_dmabuf *mp1, *mp2;
2645
2646 cnt += pring->missbufcnt;
2647
2648 /* While there are buffers to post */
2649 while (cnt > 0) {
2650 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002651 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002652 if (iocb == NULL) {
2653 pring->missbufcnt = cnt;
2654 return cnt;
2655 }
dea31012005-04-17 16:05:31 -05002656 icmd = &iocb->iocb;
2657
2658 /* 2 buffers can be posted per command */
2659 /* Allocate buffer to post */
2660 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2661 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002662 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2663 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002664 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002665 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002666 pring->missbufcnt = cnt;
2667 return cnt;
2668 }
2669
2670 INIT_LIST_HEAD(&mp1->list);
2671 /* Allocate buffer to post */
2672 if (cnt > 1) {
2673 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2674 if (mp2)
2675 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2676 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002677 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002678 kfree(mp2);
dea31012005-04-17 16:05:31 -05002679 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2680 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002681 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002682 pring->missbufcnt = cnt;
2683 return cnt;
2684 }
2685
2686 INIT_LIST_HEAD(&mp2->list);
2687 } else {
2688 mp2 = NULL;
2689 }
2690
2691 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2692 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2693 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2694 icmd->ulpBdeCount = 1;
2695 cnt--;
2696 if (mp2) {
2697 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2698 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2699 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2700 cnt--;
2701 icmd->ulpBdeCount = 2;
2702 }
2703
2704 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2705 icmd->ulpLe = 1;
2706
James Smart3772a992009-05-22 14:50:54 -04002707 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2708 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002709 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2710 kfree(mp1);
2711 cnt++;
2712 if (mp2) {
2713 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2714 kfree(mp2);
2715 cnt++;
2716 }
James Bottomley604a3e32005-10-29 10:28:33 -05002717 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002718 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002719 return cnt;
2720 }
dea31012005-04-17 16:05:31 -05002721 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002722 if (mp2)
dea31012005-04-17 16:05:31 -05002723 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002724 }
2725 pring->missbufcnt = 0;
2726 return 0;
2727}
2728
James Smarte59058c2008-08-24 21:49:00 -04002729/**
James Smart3621a712009-04-06 18:47:14 -04002730 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002731 * @phba: pointer to lpfc hba data structure.
2732 *
2733 * This routine posts initial receive IOCB buffers to the ELS ring. The
2734 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002735 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002736 *
2737 * Return codes
2738 * 0 - success (currently always success)
2739 **/
dea31012005-04-17 16:05:31 -05002740static int
James Smart2e0fef82007-06-17 19:56:36 -05002741lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002742{
2743 struct lpfc_sli *psli = &phba->sli;
2744
2745 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002746 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002747 /* Ring 2 - FCP no buffers needed */
2748
2749 return 0;
2750}
2751
2752#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2753
James Smarte59058c2008-08-24 21:49:00 -04002754/**
James Smart3621a712009-04-06 18:47:14 -04002755 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002756 * @HashResultPointer: pointer to an array as hash table.
2757 *
2758 * This routine sets up the initial values to the array of hash table entries
2759 * for the LC HBAs.
2760 **/
dea31012005-04-17 16:05:31 -05002761static void
2762lpfc_sha_init(uint32_t * HashResultPointer)
2763{
2764 HashResultPointer[0] = 0x67452301;
2765 HashResultPointer[1] = 0xEFCDAB89;
2766 HashResultPointer[2] = 0x98BADCFE;
2767 HashResultPointer[3] = 0x10325476;
2768 HashResultPointer[4] = 0xC3D2E1F0;
2769}
2770
James Smarte59058c2008-08-24 21:49:00 -04002771/**
James Smart3621a712009-04-06 18:47:14 -04002772 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002773 * @HashResultPointer: pointer to an initial/result hash table.
2774 * @HashWorkingPointer: pointer to an working hash table.
2775 *
2776 * This routine iterates an initial hash table pointed by @HashResultPointer
2777 * with the values from the working hash table pointeed by @HashWorkingPointer.
2778 * The results are putting back to the initial hash table, returned through
2779 * the @HashResultPointer as the result hash table.
2780 **/
dea31012005-04-17 16:05:31 -05002781static void
2782lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2783{
2784 int t;
2785 uint32_t TEMP;
2786 uint32_t A, B, C, D, E;
2787 t = 16;
2788 do {
2789 HashWorkingPointer[t] =
2790 S(1,
2791 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2792 8] ^
2793 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2794 } while (++t <= 79);
2795 t = 0;
2796 A = HashResultPointer[0];
2797 B = HashResultPointer[1];
2798 C = HashResultPointer[2];
2799 D = HashResultPointer[3];
2800 E = HashResultPointer[4];
2801
2802 do {
2803 if (t < 20) {
2804 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2805 } else if (t < 40) {
2806 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2807 } else if (t < 60) {
2808 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2809 } else {
2810 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2811 }
2812 TEMP += S(5, A) + E + HashWorkingPointer[t];
2813 E = D;
2814 D = C;
2815 C = S(30, B);
2816 B = A;
2817 A = TEMP;
2818 } while (++t <= 79);
2819
2820 HashResultPointer[0] += A;
2821 HashResultPointer[1] += B;
2822 HashResultPointer[2] += C;
2823 HashResultPointer[3] += D;
2824 HashResultPointer[4] += E;
2825
2826}
2827
James Smarte59058c2008-08-24 21:49:00 -04002828/**
James Smart3621a712009-04-06 18:47:14 -04002829 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002830 * @RandomChallenge: pointer to the entry of host challenge random number array.
2831 * @HashWorking: pointer to the entry of the working hash array.
2832 *
2833 * This routine calculates the working hash array referred by @HashWorking
2834 * from the challenge random numbers associated with the host, referred by
2835 * @RandomChallenge. The result is put into the entry of the working hash
2836 * array and returned by reference through @HashWorking.
2837 **/
dea31012005-04-17 16:05:31 -05002838static void
2839lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2840{
2841 *HashWorking = (*RandomChallenge ^ *HashWorking);
2842}
2843
James Smarte59058c2008-08-24 21:49:00 -04002844/**
James Smart3621a712009-04-06 18:47:14 -04002845 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002846 * @phba: pointer to lpfc hba data structure.
2847 * @hbainit: pointer to an array of unsigned 32-bit integers.
2848 *
2849 * This routine performs the special handling for LC HBA initialization.
2850 **/
dea31012005-04-17 16:05:31 -05002851void
2852lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2853{
2854 int t;
2855 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002856 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002857
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002858 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002859 if (!HashWorking)
2860 return;
2861
dea31012005-04-17 16:05:31 -05002862 HashWorking[0] = HashWorking[78] = *pwwnn++;
2863 HashWorking[1] = HashWorking[79] = *pwwnn;
2864
2865 for (t = 0; t < 7; t++)
2866 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2867
2868 lpfc_sha_init(hbainit);
2869 lpfc_sha_iterate(hbainit, HashWorking);
2870 kfree(HashWorking);
2871}
2872
James Smarte59058c2008-08-24 21:49:00 -04002873/**
James Smart3621a712009-04-06 18:47:14 -04002874 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002875 * @vport: pointer to a virtual N_Port data structure.
2876 *
2877 * This routine performs the necessary cleanups before deleting the @vport.
2878 * It invokes the discovery state machine to perform necessary state
2879 * transitions and to release the ndlps associated with the @vport. Note,
2880 * the physical port is treated as @vport 0.
2881 **/
James Smart87af33f2007-10-27 13:37:43 -04002882void
James Smart2e0fef82007-06-17 19:56:36 -05002883lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002884{
James Smart87af33f2007-10-27 13:37:43 -04002885 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002886 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002887 int i = 0;
dea31012005-04-17 16:05:31 -05002888
James Smart87af33f2007-10-27 13:37:43 -04002889 if (phba->link_state > LPFC_LINK_DOWN)
2890 lpfc_port_link_failure(vport);
2891
Gaurav Srivastava5e633302021-06-08 10:05:49 +05302892 /* Clean up VMID resources */
2893 if (lpfc_is_vmid_enabled(phba))
2894 lpfc_vmid_vport_cleanup(vport);
2895
James Smart87af33f2007-10-27 13:37:43 -04002896 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smart58da1ff2008-04-07 10:15:56 -04002897 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2898 ndlp->nlp_DID == Fabric_DID) {
2899 /* Just free up ndlp with Fabric_DID for vports */
2900 lpfc_nlp_put(ndlp);
2901 continue;
2902 }
2903
James Smarta70e63e2020-11-15 11:26:38 -08002904 if (ndlp->nlp_DID == Fabric_Cntl_DID &&
2905 ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
James Smarteff4a012012-01-18 16:25:25 -05002906 lpfc_nlp_put(ndlp);
2907 continue;
2908 }
2909
James Smarte9b11082020-11-15 11:26:33 -08002910 /* Fabric Ports not in UNMAPPED state are cleaned up in the
2911 * DEVICE_RM event.
2912 */
2913 if (ndlp->nlp_type & NLP_FABRIC &&
2914 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
James Smart87af33f2007-10-27 13:37:43 -04002915 lpfc_disc_state_machine(vport, ndlp, NULL,
2916 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002917
James Smarte9b11082020-11-15 11:26:33 -08002918 if (!(ndlp->fc4_xpt_flags & (NVME_XPT_REGD|SCSI_XPT_REGD)))
2919 lpfc_disc_state_machine(vport, ndlp, NULL,
2920 NLP_EVT_DEVICE_RM);
James Smart87af33f2007-10-27 13:37:43 -04002921 }
2922
James Smarta8adb832007-10-27 13:37:53 -04002923 /* At this point, ALL ndlp's should be gone
2924 * because of the previous NLP_EVT_DEVICE_RM.
2925 * Lets wait for this to happen, if needed.
2926 */
James Smart87af33f2007-10-27 13:37:43 -04002927 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002928 if (i++ > 3000) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002929 lpfc_printf_vlog(vport, KERN_ERR,
2930 LOG_TRACE_EVENT,
James Smarta8adb832007-10-27 13:37:53 -04002931 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002932 list_for_each_entry_safe(ndlp, next_ndlp,
2933 &vport->fc_nodes, nlp_listp) {
2934 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
James Smarte9b11082020-11-15 11:26:33 -08002935 LOG_TRACE_EVENT,
2936 "0282 did:x%x ndlp:x%px "
2937 "refcnt:%d xflags x%x nflag x%x\n",
2938 ndlp->nlp_DID, (void *)ndlp,
2939 kref_read(&ndlp->kref),
2940 ndlp->fc4_xpt_flags,
2941 ndlp->nlp_flag);
James Smarte47c9092008-02-08 18:49:26 -05002942 }
James Smarta8adb832007-10-27 13:37:53 -04002943 break;
James Smart87af33f2007-10-27 13:37:43 -04002944 }
James Smarta8adb832007-10-27 13:37:53 -04002945
2946 /* Wait for any activity on ndlps to settle */
2947 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002948 }
James Smart1151e3e2011-02-16 12:39:35 -05002949 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002950}
2951
James Smarte59058c2008-08-24 21:49:00 -04002952/**
James Smart3621a712009-04-06 18:47:14 -04002953 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002954 * @vport: pointer to a virtual N_Port data structure.
2955 *
2956 * This routine stops all the timers associated with a @vport. This function
2957 * is invoked before disabling or deleting a @vport. Note that the physical
2958 * port is treated as @vport 0.
2959 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002960void
2961lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002962{
James Smart92d7f7b2007-06-17 19:56:38 -05002963 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002964 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002965 lpfc_can_disctmo(vport);
2966 return;
dea31012005-04-17 16:05:31 -05002967}
2968
James Smarte59058c2008-08-24 21:49:00 -04002969/**
James Smartecfd03c2010-02-12 14:41:27 -05002970 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2971 * @phba: pointer to lpfc hba data structure.
2972 *
2973 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2974 * caller of this routine should already hold the host lock.
2975 **/
2976void
2977__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2978{
James Smart5ac6b302010-10-22 11:05:36 -04002979 /* Clear pending FCF rediscovery wait flag */
2980 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2981
James Smartecfd03c2010-02-12 14:41:27 -05002982 /* Now, try to stop the timer */
2983 del_timer(&phba->fcf.redisc_wait);
2984}
2985
2986/**
2987 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2988 * @phba: pointer to lpfc hba data structure.
2989 *
2990 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2991 * checks whether the FCF rediscovery wait timer is pending with the host
2992 * lock held before proceeding with disabling the timer and clearing the
2993 * wait timer pendig flag.
2994 **/
2995void
2996lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2997{
2998 spin_lock_irq(&phba->hbalock);
2999 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3000 /* FCF rediscovery timer already fired or stopped */
3001 spin_unlock_irq(&phba->hbalock);
3002 return;
3003 }
3004 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04003005 /* Clear failover in progress flags */
3006 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05003007 spin_unlock_irq(&phba->hbalock);
3008}
3009
3010/**
James Smart3772a992009-05-22 14:50:54 -04003011 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04003012 * @phba: pointer to lpfc hba data structure.
3013 *
3014 * This routine stops all the timers associated with a HBA. This function is
3015 * invoked before either putting a HBA offline or unloading the driver.
3016 **/
James Smart3772a992009-05-22 14:50:54 -04003017void
3018lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003019{
James Smartcdb42be2019-01-28 11:14:21 -08003020 if (phba->pport)
3021 lpfc_stop_vport_timers(phba->pport);
James Smart32517fc2019-01-28 11:14:33 -08003022 cancel_delayed_work_sync(&phba->eq_delay_work);
Dick Kennedy317aeb82020-06-30 14:49:59 -07003023 cancel_delayed_work_sync(&phba->idle_stat_delay_work);
James Smart2e0fef82007-06-17 19:56:36 -05003024 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05003025 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04003026 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04003027 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05003028 if (phba->sli_rev == LPFC_SLI_REV4) {
3029 del_timer_sync(&phba->rrq_tmr);
3030 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
3031 }
James Smarta22d73b2021-01-04 10:02:38 -08003032 phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
James Smart3772a992009-05-22 14:50:54 -04003033
3034 switch (phba->pci_dev_grp) {
3035 case LPFC_PCI_DEV_LP:
3036 /* Stop any LightPulse device specific driver timers */
3037 del_timer_sync(&phba->fcp_poll_timer);
3038 break;
3039 case LPFC_PCI_DEV_OC:
Paul Walmsleycc0e5f12019-07-11 20:52:33 -07003040 /* Stop any OneConnect device specific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05003041 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04003042 break;
3043 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07003044 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04003045 "0297 Invalid device group (x%x)\n",
3046 phba->pci_dev_grp);
3047 break;
3048 }
James Smart2e0fef82007-06-17 19:56:36 -05003049 return;
dea31012005-04-17 16:05:31 -05003050}
3051
James Smarte59058c2008-08-24 21:49:00 -04003052/**
James Smart3621a712009-04-06 18:47:14 -04003053 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04003054 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01003055 * @mbx_action: flag for mailbox no wait action.
James Smarte59058c2008-08-24 21:49:00 -04003056 *
3057 * This routine marks a HBA's management interface as blocked. Once the HBA's
3058 * management interface is marked as blocked, all the user space access to
3059 * the HBA, whether they are from sysfs interface or libdfc interface will
3060 * all be blocked. The HBA is set to block the management interface when the
3061 * driver prepares the HBA interface for online or offline.
3062 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01003063static void
James Smart618a5232012-06-12 13:54:36 -04003064lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01003065{
3066 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04003067 uint8_t actcmd = MBX_HEARTBEAT;
3068 unsigned long timeout;
3069
Adrian Bunka6ababd2007-11-05 18:07:33 +01003070 spin_lock_irqsave(&phba->hbalock, iflag);
3071 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04003072 spin_unlock_irqrestore(&phba->hbalock, iflag);
3073 if (mbx_action == LPFC_MBX_NO_WAIT)
3074 return;
3075 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
3076 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003077 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04003078 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04003079 /* Determine how long we might wait for the active mailbox
3080 * command to be gracefully completed by firmware.
3081 */
3082 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3083 phba->sli.mbox_active) * 1000) + jiffies;
3084 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003085 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003086
James Smart6e7288d2010-06-07 15:23:35 -04003087 /* Wait for the outstnading mailbox command to complete */
3088 while (phba->sli.mbox_active) {
3089 /* Check active mailbox complete status every 2ms */
3090 msleep(2);
3091 if (time_after(jiffies, timeout)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003092 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3093 "2813 Mgmt IO is Blocked %x "
3094 "- mbox cmd %x still active\n",
3095 phba->sli.sli_flag, actcmd);
James Smart6e7288d2010-06-07 15:23:35 -04003096 break;
3097 }
3098 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003099}
3100
James Smarte59058c2008-08-24 21:49:00 -04003101/**
James Smart6b5151f2012-01-18 16:24:06 -05003102 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
3103 * @phba: pointer to lpfc hba data structure.
3104 *
3105 * Allocate RPIs for all active remote nodes. This is needed whenever
3106 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
3107 * is to fixup the temporary rpi assignments.
3108 **/
3109void
3110lpfc_sli4_node_prep(struct lpfc_hba *phba)
3111{
3112 struct lpfc_nodelist *ndlp, *next_ndlp;
3113 struct lpfc_vport **vports;
James Smart9d3d3402017-04-21 16:05:00 -07003114 int i, rpi;
James Smart6b5151f2012-01-18 16:24:06 -05003115
3116 if (phba->sli_rev != LPFC_SLI_REV4)
3117 return;
3118
3119 vports = lpfc_create_vport_work_array(phba);
James Smart9d3d3402017-04-21 16:05:00 -07003120 if (vports == NULL)
3121 return;
James Smart6b5151f2012-01-18 16:24:06 -05003122
James Smart9d3d3402017-04-21 16:05:00 -07003123 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3124 if (vports[i]->load_flag & FC_UNLOADING)
3125 continue;
3126
3127 list_for_each_entry_safe(ndlp, next_ndlp,
3128 &vports[i]->fc_nodes,
3129 nlp_listp) {
James Smart9d3d3402017-04-21 16:05:00 -07003130 rpi = lpfc_sli4_alloc_rpi(phba);
3131 if (rpi == LPFC_RPI_ALLOC_ERROR) {
James Smart307e3382020-11-15 11:26:30 -08003132 /* TODO print log? */
James Smart9d3d3402017-04-21 16:05:00 -07003133 continue;
James Smart6b5151f2012-01-18 16:24:06 -05003134 }
James Smart9d3d3402017-04-21 16:05:00 -07003135 ndlp->nlp_rpi = rpi;
James Smart0f154222019-09-21 20:58:52 -07003136 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
3137 LOG_NODE | LOG_DISCOVERY,
3138 "0009 Assign RPI x%x to ndlp x%px "
James Smart307e3382020-11-15 11:26:30 -08003139 "DID:x%06x flg:x%x\n",
James Smart0f154222019-09-21 20:58:52 -07003140 ndlp->nlp_rpi, ndlp, ndlp->nlp_DID,
James Smart307e3382020-11-15 11:26:30 -08003141 ndlp->nlp_flag);
James Smart6b5151f2012-01-18 16:24:06 -05003142 }
3143 }
3144 lpfc_destroy_vport_work_array(phba, vports);
3145}
3146
3147/**
James Smartc4908502019-01-28 11:14:28 -08003148 * lpfc_create_expedite_pool - create expedite pool
3149 * @phba: pointer to lpfc hba data structure.
3150 *
3151 * This routine moves a batch of XRIs from lpfc_io_buf_list_put of HWQ 0
3152 * to expedite pool. Mark them as expedite.
3153 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003154static void lpfc_create_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003155{
3156 struct lpfc_sli4_hdw_queue *qp;
3157 struct lpfc_io_buf *lpfc_ncmd;
3158 struct lpfc_io_buf *lpfc_ncmd_next;
3159 struct lpfc_epd_pool *epd_pool;
3160 unsigned long iflag;
3161
3162 epd_pool = &phba->epd_pool;
3163 qp = &phba->sli4_hba.hdwq[0];
3164
3165 spin_lock_init(&epd_pool->lock);
3166 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3167 spin_lock(&epd_pool->lock);
3168 INIT_LIST_HEAD(&epd_pool->list);
3169 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3170 &qp->lpfc_io_buf_list_put, list) {
3171 list_move_tail(&lpfc_ncmd->list, &epd_pool->list);
3172 lpfc_ncmd->expedite = true;
3173 qp->put_io_bufs--;
3174 epd_pool->count++;
3175 if (epd_pool->count >= XRI_BATCH)
3176 break;
3177 }
3178 spin_unlock(&epd_pool->lock);
3179 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3180}
3181
3182/**
3183 * lpfc_destroy_expedite_pool - destroy expedite pool
3184 * @phba: pointer to lpfc hba data structure.
3185 *
3186 * This routine returns XRIs from expedite pool to lpfc_io_buf_list_put
3187 * of HWQ 0. Clear the mark.
3188 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003189static void lpfc_destroy_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003190{
3191 struct lpfc_sli4_hdw_queue *qp;
3192 struct lpfc_io_buf *lpfc_ncmd;
3193 struct lpfc_io_buf *lpfc_ncmd_next;
3194 struct lpfc_epd_pool *epd_pool;
3195 unsigned long iflag;
3196
3197 epd_pool = &phba->epd_pool;
3198 qp = &phba->sli4_hba.hdwq[0];
3199
3200 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3201 spin_lock(&epd_pool->lock);
3202 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3203 &epd_pool->list, list) {
3204 list_move_tail(&lpfc_ncmd->list,
3205 &qp->lpfc_io_buf_list_put);
3206 lpfc_ncmd->flags = false;
3207 qp->put_io_bufs++;
3208 epd_pool->count--;
3209 }
3210 spin_unlock(&epd_pool->lock);
3211 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3212}
3213
3214/**
3215 * lpfc_create_multixri_pools - create multi-XRI pools
3216 * @phba: pointer to lpfc hba data structure.
3217 *
3218 * This routine initialize public, private per HWQ. Then, move XRIs from
3219 * lpfc_io_buf_list_put to public pool. High and low watermark are also
3220 * Initialized.
3221 **/
3222void lpfc_create_multixri_pools(struct lpfc_hba *phba)
3223{
3224 u32 i, j;
3225 u32 hwq_count;
3226 u32 count_per_hwq;
3227 struct lpfc_io_buf *lpfc_ncmd;
3228 struct lpfc_io_buf *lpfc_ncmd_next;
3229 unsigned long iflag;
3230 struct lpfc_sli4_hdw_queue *qp;
3231 struct lpfc_multixri_pool *multixri_pool;
3232 struct lpfc_pbl_pool *pbl_pool;
3233 struct lpfc_pvt_pool *pvt_pool;
3234
3235 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3236 "1234 num_hdw_queue=%d num_present_cpu=%d common_xri_cnt=%d\n",
3237 phba->cfg_hdw_queue, phba->sli4_hba.num_present_cpu,
3238 phba->sli4_hba.io_xri_cnt);
3239
3240 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3241 lpfc_create_expedite_pool(phba);
3242
3243 hwq_count = phba->cfg_hdw_queue;
3244 count_per_hwq = phba->sli4_hba.io_xri_cnt / hwq_count;
3245
3246 for (i = 0; i < hwq_count; i++) {
3247 multixri_pool = kzalloc(sizeof(*multixri_pool), GFP_KERNEL);
3248
3249 if (!multixri_pool) {
3250 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3251 "1238 Failed to allocate memory for "
3252 "multixri_pool\n");
3253
3254 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3255 lpfc_destroy_expedite_pool(phba);
3256
3257 j = 0;
3258 while (j < i) {
3259 qp = &phba->sli4_hba.hdwq[j];
3260 kfree(qp->p_multixri_pool);
3261 j++;
3262 }
3263 phba->cfg_xri_rebalancing = 0;
3264 return;
3265 }
3266
3267 qp = &phba->sli4_hba.hdwq[i];
3268 qp->p_multixri_pool = multixri_pool;
3269
3270 multixri_pool->xri_limit = count_per_hwq;
3271 multixri_pool->rrb_next_hwqid = i;
3272
3273 /* Deal with public free xri pool */
3274 pbl_pool = &multixri_pool->pbl_pool;
3275 spin_lock_init(&pbl_pool->lock);
3276 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3277 spin_lock(&pbl_pool->lock);
3278 INIT_LIST_HEAD(&pbl_pool->list);
3279 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3280 &qp->lpfc_io_buf_list_put, list) {
3281 list_move_tail(&lpfc_ncmd->list, &pbl_pool->list);
3282 qp->put_io_bufs--;
3283 pbl_pool->count++;
3284 }
3285 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3286 "1235 Moved %d buffers from PUT list over to pbl_pool[%d]\n",
3287 pbl_pool->count, i);
3288 spin_unlock(&pbl_pool->lock);
3289 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3290
3291 /* Deal with private free xri pool */
3292 pvt_pool = &multixri_pool->pvt_pool;
3293 pvt_pool->high_watermark = multixri_pool->xri_limit / 2;
3294 pvt_pool->low_watermark = XRI_BATCH;
3295 spin_lock_init(&pvt_pool->lock);
3296 spin_lock_irqsave(&pvt_pool->lock, iflag);
3297 INIT_LIST_HEAD(&pvt_pool->list);
3298 pvt_pool->count = 0;
3299 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
3300 }
3301}
3302
3303/**
3304 * lpfc_destroy_multixri_pools - destroy multi-XRI pools
3305 * @phba: pointer to lpfc hba data structure.
3306 *
3307 * This routine returns XRIs from public/private to lpfc_io_buf_list_put.
3308 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003309static void lpfc_destroy_multixri_pools(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003310{
3311 u32 i;
3312 u32 hwq_count;
3313 struct lpfc_io_buf *lpfc_ncmd;
3314 struct lpfc_io_buf *lpfc_ncmd_next;
3315 unsigned long iflag;
3316 struct lpfc_sli4_hdw_queue *qp;
3317 struct lpfc_multixri_pool *multixri_pool;
3318 struct lpfc_pbl_pool *pbl_pool;
3319 struct lpfc_pvt_pool *pvt_pool;
3320
3321 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3322 lpfc_destroy_expedite_pool(phba);
3323
James Smartc00f62e2019-08-14 16:57:11 -07003324 if (!(phba->pport->load_flag & FC_UNLOADING))
3325 lpfc_sli_flush_io_rings(phba);
James Smartc66a9192019-03-12 16:30:19 -07003326
James Smartc4908502019-01-28 11:14:28 -08003327 hwq_count = phba->cfg_hdw_queue;
3328
3329 for (i = 0; i < hwq_count; i++) {
3330 qp = &phba->sli4_hba.hdwq[i];
3331 multixri_pool = qp->p_multixri_pool;
3332 if (!multixri_pool)
3333 continue;
3334
3335 qp->p_multixri_pool = NULL;
3336
3337 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3338
3339 /* Deal with public free xri pool */
3340 pbl_pool = &multixri_pool->pbl_pool;
3341 spin_lock(&pbl_pool->lock);
3342
3343 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3344 "1236 Moving %d buffers from pbl_pool[%d] TO PUT list\n",
3345 pbl_pool->count, i);
3346
3347 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3348 &pbl_pool->list, list) {
3349 list_move_tail(&lpfc_ncmd->list,
3350 &qp->lpfc_io_buf_list_put);
3351 qp->put_io_bufs++;
3352 pbl_pool->count--;
3353 }
3354
3355 INIT_LIST_HEAD(&pbl_pool->list);
3356 pbl_pool->count = 0;
3357
3358 spin_unlock(&pbl_pool->lock);
3359
3360 /* Deal with private free xri pool */
3361 pvt_pool = &multixri_pool->pvt_pool;
3362 spin_lock(&pvt_pool->lock);
3363
3364 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3365 "1237 Moving %d buffers from pvt_pool[%d] TO PUT list\n",
3366 pvt_pool->count, i);
3367
3368 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3369 &pvt_pool->list, list) {
3370 list_move_tail(&lpfc_ncmd->list,
3371 &qp->lpfc_io_buf_list_put);
3372 qp->put_io_bufs++;
3373 pvt_pool->count--;
3374 }
3375
3376 INIT_LIST_HEAD(&pvt_pool->list);
3377 pvt_pool->count = 0;
3378
3379 spin_unlock(&pvt_pool->lock);
3380 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3381
3382 kfree(multixri_pool);
3383 }
3384}
3385
3386/**
James Smart3621a712009-04-06 18:47:14 -04003387 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04003388 * @phba: pointer to lpfc hba data structure.
3389 *
3390 * This routine initializes the HBA and brings a HBA online. During this
3391 * process, the management interface is blocked to prevent user space access
3392 * to the HBA interfering with the driver initialization.
3393 *
3394 * Return codes
3395 * 0 - successful
3396 * 1 - failed
3397 **/
dea31012005-04-17 16:05:31 -05003398int
James Smart2e0fef82007-06-17 19:56:36 -05003399lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003400{
Julia Lawall372bd282008-12-16 16:15:08 +01003401 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04003402 struct lpfc_vport **vports;
Dick Kennedya145fda2017-08-23 16:55:44 -07003403 int i, error = 0;
James Smart16a3a202013-04-17 20:14:38 -04003404 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05003405
dea31012005-04-17 16:05:31 -05003406 if (!phba)
3407 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01003408 vport = phba->pport;
dea31012005-04-17 16:05:31 -05003409
James Smart2e0fef82007-06-17 19:56:36 -05003410 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05003411 return 0;
3412
James Smarted957682007-06-17 19:56:37 -05003413 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003414 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05003415
James Smart618a5232012-06-12 13:54:36 -04003416 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05003417
James Smartda0436e2009-05-22 14:51:39 -04003418 if (phba->sli_rev == LPFC_SLI_REV4) {
3419 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3420 lpfc_unblock_mgmt_io(phba);
3421 return 1;
3422 }
James Smart16a3a202013-04-17 20:14:38 -04003423 spin_lock_irq(&phba->hbalock);
3424 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3425 vpis_cleared = true;
3426 spin_unlock_irq(&phba->hbalock);
Dick Kennedya145fda2017-08-23 16:55:44 -07003427
3428 /* Reestablish the local initiator port.
3429 * The offline process destroyed the previous lport.
3430 */
3431 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3432 !phba->nvmet_support) {
3433 error = lpfc_nvme_create_localport(phba->pport);
3434 if (error)
Dick Kennedy372c1872020-06-30 14:50:00 -07003435 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedya145fda2017-08-23 16:55:44 -07003436 "6132 NVME restore reg failed "
3437 "on nvmei error x%x\n", error);
3438 }
James Smartda0436e2009-05-22 14:51:39 -04003439 } else {
James Smart895427b2017-02-12 13:52:30 -08003440 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04003441 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3442 lpfc_unblock_mgmt_io(phba);
3443 return 1;
3444 }
James Smart46fa3112007-04-25 09:51:45 -04003445 }
dea31012005-04-17 16:05:31 -05003446
James Smart549e55c2007-08-02 11:09:51 -04003447 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003448 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003449 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003450 struct Scsi_Host *shost;
3451 shost = lpfc_shost_from_vport(vports[i]);
3452 spin_lock_irq(shost->host_lock);
3453 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3454 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3455 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003456 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04003457 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003458 if ((vpis_cleared) &&
3459 (vports[i]->port_type !=
3460 LPFC_PHYSICAL_PORT))
3461 vports[i]->vpi = 0;
3462 }
James Smart549e55c2007-08-02 11:09:51 -04003463 spin_unlock_irq(shost->host_lock);
3464 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003465 }
3466 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003467
James Smartc4908502019-01-28 11:14:28 -08003468 if (phba->cfg_xri_rebalancing)
3469 lpfc_create_multixri_pools(phba);
3470
James Smart93a4d6f2019-11-04 16:57:05 -08003471 lpfc_cpuhp_add(phba);
3472
James Smart46fa3112007-04-25 09:51:45 -04003473 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003474 return 0;
3475}
3476
James Smarte59058c2008-08-24 21:49:00 -04003477/**
James Smart3621a712009-04-06 18:47:14 -04003478 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04003479 * @phba: pointer to lpfc hba data structure.
3480 *
3481 * This routine marks a HBA's management interface as not blocked. Once the
3482 * HBA's management interface is marked as not blocked, all the user space
3483 * access to the HBA, whether they are from sysfs interface or libdfc
3484 * interface will be allowed. The HBA is set to block the management interface
3485 * when the driver prepares the HBA interface for online or offline and then
3486 * set to unblock the management interface afterwards.
3487 **/
James Smart46fa3112007-04-25 09:51:45 -04003488void
James Smart46fa3112007-04-25 09:51:45 -04003489lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3490{
3491 unsigned long iflag;
3492
James Smart2e0fef82007-06-17 19:56:36 -05003493 spin_lock_irqsave(&phba->hbalock, iflag);
3494 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3495 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003496}
3497
James Smarte59058c2008-08-24 21:49:00 -04003498/**
James Smart3621a712009-04-06 18:47:14 -04003499 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003500 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01003501 * @mbx_action: flag for mailbox shutdown action.
James Smarte59058c2008-08-24 21:49:00 -04003502 *
3503 * This routine is invoked to prepare a HBA to be brought offline. It performs
3504 * unregistration login to all the nodes on all vports and flushes the mailbox
3505 * queue to make it ready to be brought offline.
3506 **/
James Smart46fa3112007-04-25 09:51:45 -04003507void
James Smart618a5232012-06-12 13:54:36 -04003508lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003509{
James Smart2e0fef82007-06-17 19:56:36 -05003510 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003511 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003512 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003513 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003514 int i;
dea31012005-04-17 16:05:31 -05003515
James Smart2e0fef82007-06-17 19:56:36 -05003516 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003517 return;
dea31012005-04-17 16:05:31 -05003518
James Smart618a5232012-06-12 13:54:36 -04003519 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003520
3521 lpfc_linkdown(phba);
3522
James Smart87af33f2007-10-27 13:37:43 -04003523 /* Issue an unreg_login to all nodes on all vports */
3524 vports = lpfc_create_vport_work_array(phba);
3525 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003526 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003527 if (vports[i]->load_flag & FC_UNLOADING)
3528 continue;
James Smart72100cc2010-02-12 14:43:01 -05003529 shost = lpfc_shost_from_vport(vports[i]);
3530 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003531 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003532 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3533 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003534 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003535
James Smart87af33f2007-10-27 13:37:43 -04003536 shost = lpfc_shost_from_vport(vports[i]);
3537 list_for_each_entry_safe(ndlp, next_ndlp,
3538 &vports[i]->fc_nodes,
3539 nlp_listp) {
James Smart0f154222019-09-21 20:58:52 -07003540
James Smartc6adba12020-11-15 11:26:34 -08003541 spin_lock_irq(&ndlp->lock);
James Smart87af33f2007-10-27 13:37:43 -04003542 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smartc6adba12020-11-15 11:26:34 -08003543 spin_unlock_irq(&ndlp->lock);
James Smartaffbe242021-07-07 11:43:42 -07003544
3545 lpfc_unreg_rpi(vports[i], ndlp);
James Smart6b5151f2012-01-18 16:24:06 -05003546 /*
3547 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003548 * RPI. Get a new RPI when the adapter port
3549 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003550 */
James Smartbe6bb942015-04-07 15:07:22 -04003551 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smarte9b11082020-11-15 11:26:33 -08003552 lpfc_printf_vlog(vports[i], KERN_INFO,
James Smart0f154222019-09-21 20:58:52 -07003553 LOG_NODE | LOG_DISCOVERY,
3554 "0011 Free RPI x%x on "
James Smartf1156122021-04-11 18:31:24 -07003555 "ndlp: x%px did x%x\n",
James Smart0f154222019-09-21 20:58:52 -07003556 ndlp->nlp_rpi, ndlp,
James Smart307e3382020-11-15 11:26:30 -08003557 ndlp->nlp_DID);
James Smart6b5151f2012-01-18 16:24:06 -05003558 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart0f154222019-09-21 20:58:52 -07003559 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
James Smartbe6bb942015-04-07 15:07:22 -04003560 }
James Smart307e3382020-11-15 11:26:30 -08003561
3562 if (ndlp->nlp_type & NLP_FABRIC) {
3563 lpfc_disc_state_machine(vports[i], ndlp,
3564 NULL, NLP_EVT_DEVICE_RECOVERY);
James Smarte9b11082020-11-15 11:26:33 -08003565
3566 /* Don't remove the node unless the
3567 * has been unregistered with the
3568 * transport. If so, let dev_loss
3569 * take care of the node.
3570 */
3571 if (!(ndlp->fc4_xpt_flags &
3572 (NVME_XPT_REGD | SCSI_XPT_REGD)))
3573 lpfc_disc_state_machine
3574 (vports[i], ndlp,
3575 NULL,
3576 NLP_EVT_DEVICE_RM);
James Smart307e3382020-11-15 11:26:30 -08003577 }
James Smart87af33f2007-10-27 13:37:43 -04003578 }
3579 }
3580 }
James Smart09372822008-01-11 01:52:54 -05003581 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003582
James Smart618a5232012-06-12 13:54:36 -04003583 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
Dick Kennedyf485c182017-09-29 17:34:34 -07003584
3585 if (phba->wq)
3586 flush_workqueue(phba->wq);
James Smart46fa3112007-04-25 09:51:45 -04003587}
3588
James Smarte59058c2008-08-24 21:49:00 -04003589/**
James Smart3621a712009-04-06 18:47:14 -04003590 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003591 * @phba: pointer to lpfc hba data structure.
3592 *
3593 * This routine actually brings a HBA offline. It stops all the timers
3594 * associated with the HBA, brings down the SLI layer, and eventually
3595 * marks the HBA as in offline state for the upper layer protocol.
3596 **/
James Smart46fa3112007-04-25 09:51:45 -04003597void
James Smart2e0fef82007-06-17 19:56:36 -05003598lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003599{
James Smart549e55c2007-08-02 11:09:51 -04003600 struct Scsi_Host *shost;
3601 struct lpfc_vport **vports;
3602 int i;
James Smart46fa3112007-04-25 09:51:45 -04003603
James Smart549e55c2007-08-02 11:09:51 -04003604 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003605 return;
James Smart688a8862006-07-06 15:49:56 -04003606
James Smartda0436e2009-05-22 14:51:39 -04003607 /* stop port and all timers associated with this hba */
3608 lpfc_stop_port(phba);
Dick Kennedy4b40d022017-08-23 16:55:38 -07003609
3610 /* Tear down the local and target port registrations. The
3611 * nvme transports need to cleanup.
3612 */
3613 lpfc_nvmet_destroy_targetport(phba);
3614 lpfc_nvme_destroy_localport(phba->pport);
3615
James Smart51ef4c22007-08-02 11:10:31 -04003616 vports = lpfc_create_vport_work_array(phba);
3617 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003618 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003619 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003620 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003621 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003622 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003623 /* Bring down the SLI Layer and cleanup. The HBA is offline
3624 now. */
3625 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003626 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003627 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003628 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003629 vports = lpfc_create_vport_work_array(phba);
3630 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003631 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003632 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003633 spin_lock_irq(shost->host_lock);
3634 vports[i]->work_port_events = 0;
3635 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3636 spin_unlock_irq(shost->host_lock);
3637 }
James Smart09372822008-01-11 01:52:54 -05003638 lpfc_destroy_vport_work_array(phba, vports);
James Smartf0871ab2021-01-04 10:02:32 -08003639 /* If OFFLINE flag is clear (i.e. unloading), cpuhp removal is handled
3640 * in hba_unset
3641 */
3642 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
3643 __lpfc_cpuhp_remove(phba);
James Smartc4908502019-01-28 11:14:28 -08003644
3645 if (phba->cfg_xri_rebalancing)
3646 lpfc_destroy_multixri_pools(phba);
dea31012005-04-17 16:05:31 -05003647}
3648
James Smarte59058c2008-08-24 21:49:00 -04003649/**
James Smart3621a712009-04-06 18:47:14 -04003650 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003651 * @phba: pointer to lpfc hba data structure.
3652 *
3653 * This routine is to free all the SCSI buffers and IOCBs from the driver
3654 * list back to kernel. It is called from lpfc_pci_remove_one to free
3655 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003656 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003657static void
James Smart2e0fef82007-06-17 19:56:36 -05003658lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003659{
James Smartc4908502019-01-28 11:14:28 -08003660 struct lpfc_io_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003661
James Smart895427b2017-02-12 13:52:30 -08003662 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3663 return;
3664
James Smart2e0fef82007-06-17 19:56:36 -05003665 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003666
dea31012005-04-17 16:05:31 -05003667 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003668
3669 spin_lock(&phba->scsi_buf_list_put_lock);
3670 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3671 list) {
dea31012005-04-17 16:05:31 -05003672 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003673 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003674 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003675 kfree(sb);
3676 phba->total_scsi_bufs--;
3677 }
James Smarta40fc5f2013-04-17 20:17:40 -04003678 spin_unlock(&phba->scsi_buf_list_put_lock);
3679
3680 spin_lock(&phba->scsi_buf_list_get_lock);
3681 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3682 list) {
3683 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003684 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003685 sb->dma_handle);
3686 kfree(sb);
3687 phba->total_scsi_bufs--;
3688 }
3689 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003690 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003691}
James Smart0794d602019-01-28 11:14:19 -08003692
James Smart8a9d2e82012-05-09 21:16:12 -04003693/**
James Smart5e5b5112019-01-28 11:14:22 -08003694 * lpfc_io_free - Free all the IO buffers and IOCBs from driver lists
James Smart895427b2017-02-12 13:52:30 -08003695 * @phba: pointer to lpfc hba data structure.
3696 *
James Smart0794d602019-01-28 11:14:19 -08003697 * This routine is to free all the IO buffers and IOCBs from the driver
James Smart895427b2017-02-12 13:52:30 -08003698 * list back to kernel. It is called from lpfc_pci_remove_one to free
3699 * the internal resources before the device is removed from the system.
3700 **/
James Smartc4908502019-01-28 11:14:28 -08003701void
James Smart5e5b5112019-01-28 11:14:22 -08003702lpfc_io_free(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08003703{
James Smartc4908502019-01-28 11:14:28 -08003704 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart5e5b5112019-01-28 11:14:22 -08003705 struct lpfc_sli4_hdw_queue *qp;
3706 int idx;
James Smart895427b2017-02-12 13:52:30 -08003707
James Smart5e5b5112019-01-28 11:14:22 -08003708 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3709 qp = &phba->sli4_hba.hdwq[idx];
3710 /* Release all the lpfc_nvme_bufs maintained by this host. */
3711 spin_lock(&qp->io_buf_list_put_lock);
3712 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3713 &qp->lpfc_io_buf_list_put,
3714 list) {
3715 list_del(&lpfc_ncmd->list);
3716 qp->put_io_bufs--;
3717 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3718 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
James Smartd79c9e92019-08-14 16:57:09 -07003719 if (phba->cfg_xpsgl && !phba->nvmet_support)
3720 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
3721 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
James Smart5e5b5112019-01-28 11:14:22 -08003722 kfree(lpfc_ncmd);
3723 qp->total_io_bufs--;
3724 }
3725 spin_unlock(&qp->io_buf_list_put_lock);
James Smart895427b2017-02-12 13:52:30 -08003726
James Smart5e5b5112019-01-28 11:14:22 -08003727 spin_lock(&qp->io_buf_list_get_lock);
3728 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3729 &qp->lpfc_io_buf_list_get,
3730 list) {
3731 list_del(&lpfc_ncmd->list);
3732 qp->get_io_bufs--;
3733 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3734 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
James Smartd79c9e92019-08-14 16:57:09 -07003735 if (phba->cfg_xpsgl && !phba->nvmet_support)
3736 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
3737 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
James Smart5e5b5112019-01-28 11:14:22 -08003738 kfree(lpfc_ncmd);
3739 qp->total_io_bufs--;
3740 }
3741 spin_unlock(&qp->io_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003742 }
James Smart895427b2017-02-12 13:52:30 -08003743}
James Smart0794d602019-01-28 11:14:19 -08003744
James Smart895427b2017-02-12 13:52:30 -08003745/**
3746 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003747 * @phba: pointer to lpfc hba data structure.
3748 *
3749 * This routine first calculates the sizes of the current els and allocated
3750 * scsi sgl lists, and then goes through all sgls to updates the physical
3751 * XRIs assigned due to port function reset. During port initialization, the
3752 * current els and allocated scsi sgl lists are 0s.
3753 *
3754 * Return codes
3755 * 0 - successful (for now, it always returns 0)
3756 **/
3757int
James Smart895427b2017-02-12 13:52:30 -08003758lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003759{
3760 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003761 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003762 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003763 int rc;
3764
3765 /*
3766 * update on pci function's els xri-sgl list
3767 */
3768 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003769
James Smart8a9d2e82012-05-09 21:16:12 -04003770 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3771 /* els xri-sgl expanded */
3772 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3773 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3774 "3157 ELS xri-sgl count increased from "
3775 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3776 els_xri_cnt);
3777 /* allocate the additional els sgls */
3778 for (i = 0; i < xri_cnt; i++) {
3779 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3780 GFP_KERNEL);
3781 if (sglq_entry == NULL) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003782 lpfc_printf_log(phba, KERN_ERR,
3783 LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04003784 "2562 Failure to allocate an "
3785 "ELS sgl entry:%d\n", i);
3786 rc = -ENOMEM;
3787 goto out_free_mem;
3788 }
3789 sglq_entry->buff_type = GEN_BUFF_TYPE;
3790 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3791 &sglq_entry->phys);
3792 if (sglq_entry->virt == NULL) {
3793 kfree(sglq_entry);
Dick Kennedy372c1872020-06-30 14:50:00 -07003794 lpfc_printf_log(phba, KERN_ERR,
3795 LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04003796 "2563 Failure to allocate an "
3797 "ELS mbuf:%d\n", i);
3798 rc = -ENOMEM;
3799 goto out_free_mem;
3800 }
3801 sglq_entry->sgl = sglq_entry->virt;
3802 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3803 sglq_entry->state = SGL_FREED;
3804 list_add_tail(&sglq_entry->list, &els_sgl_list);
3805 }
James Smarta7892412021-04-11 18:31:15 -07003806 spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart895427b2017-02-12 13:52:30 -08003807 list_splice_init(&els_sgl_list,
3808 &phba->sli4_hba.lpfc_els_sgl_list);
James Smarta7892412021-04-11 18:31:15 -07003809 spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003810 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3811 /* els xri-sgl shrinked */
3812 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3813 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3814 "3158 ELS xri-sgl count decreased from "
3815 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3816 els_xri_cnt);
James Smarta7892412021-04-11 18:31:15 -07003817 spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart895427b2017-02-12 13:52:30 -08003818 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3819 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003820 /* release extra els sgls from list */
3821 for (i = 0; i < xri_cnt; i++) {
3822 list_remove_head(&els_sgl_list,
3823 sglq_entry, struct lpfc_sglq, list);
3824 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003825 __lpfc_mbuf_free(phba, sglq_entry->virt,
3826 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003827 kfree(sglq_entry);
3828 }
3829 }
James Smart895427b2017-02-12 13:52:30 -08003830 list_splice_init(&els_sgl_list,
3831 &phba->sli4_hba.lpfc_els_sgl_list);
James Smarta7892412021-04-11 18:31:15 -07003832 spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003833 } else
3834 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3835 "3163 ELS xri-sgl count unchanged: %d\n",
3836 els_xri_cnt);
3837 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3838
3839 /* update xris to els sgls on the list */
3840 sglq_entry = NULL;
3841 sglq_entry_next = NULL;
3842 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003843 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003844 lxri = lpfc_sli4_next_xritag(phba);
3845 if (lxri == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003846 lpfc_printf_log(phba, KERN_ERR,
3847 LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04003848 "2400 Failed to allocate xri for "
3849 "ELS sgl\n");
3850 rc = -ENOMEM;
3851 goto out_free_mem;
3852 }
3853 sglq_entry->sli4_lxritag = lxri;
3854 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3855 }
James Smart895427b2017-02-12 13:52:30 -08003856 return 0;
3857
3858out_free_mem:
3859 lpfc_free_els_sgl_list(phba);
3860 return rc;
3861}
3862
3863/**
James Smartf358dd02017-02-12 13:52:34 -08003864 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3865 * @phba: pointer to lpfc hba data structure.
3866 *
3867 * This routine first calculates the sizes of the current els and allocated
3868 * scsi sgl lists, and then goes through all sgls to updates the physical
3869 * XRIs assigned due to port function reset. During port initialization, the
3870 * current els and allocated scsi sgl lists are 0s.
3871 *
3872 * Return codes
3873 * 0 - successful (for now, it always returns 0)
3874 **/
3875int
3876lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3877{
3878 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3879 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart6c621a22017-05-15 15:20:45 -07003880 uint16_t nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003881 LIST_HEAD(nvmet_sgl_list);
3882 int rc;
3883
3884 /*
3885 * update on pci function's nvmet xri-sgl list
3886 */
3887 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart61f3d4b2017-05-15 15:20:41 -07003888
James Smart6c621a22017-05-15 15:20:45 -07003889 /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3890 nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003891 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3892 /* els xri-sgl expanded */
3893 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3894 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3895 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3896 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3897 /* allocate the additional nvmet sgls */
3898 for (i = 0; i < xri_cnt; i++) {
3899 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3900 GFP_KERNEL);
3901 if (sglq_entry == NULL) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003902 lpfc_printf_log(phba, KERN_ERR,
3903 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08003904 "6303 Failure to allocate an "
3905 "NVMET sgl entry:%d\n", i);
3906 rc = -ENOMEM;
3907 goto out_free_mem;
3908 }
3909 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3910 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3911 &sglq_entry->phys);
3912 if (sglq_entry->virt == NULL) {
3913 kfree(sglq_entry);
Dick Kennedy372c1872020-06-30 14:50:00 -07003914 lpfc_printf_log(phba, KERN_ERR,
3915 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08003916 "6304 Failure to allocate an "
3917 "NVMET buf:%d\n", i);
3918 rc = -ENOMEM;
3919 goto out_free_mem;
3920 }
3921 sglq_entry->sgl = sglq_entry->virt;
3922 memset(sglq_entry->sgl, 0,
3923 phba->cfg_sg_dma_buf_size);
3924 sglq_entry->state = SGL_FREED;
3925 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3926 }
3927 spin_lock_irq(&phba->hbalock);
3928 spin_lock(&phba->sli4_hba.sgl_list_lock);
3929 list_splice_init(&nvmet_sgl_list,
3930 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3931 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3932 spin_unlock_irq(&phba->hbalock);
3933 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3934 /* nvmet xri-sgl shrunk */
3935 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3936 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3937 "6305 NVMET xri-sgl count decreased from "
3938 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3939 nvmet_xri_cnt);
3940 spin_lock_irq(&phba->hbalock);
3941 spin_lock(&phba->sli4_hba.sgl_list_lock);
3942 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3943 &nvmet_sgl_list);
3944 /* release extra nvmet sgls from list */
3945 for (i = 0; i < xri_cnt; i++) {
3946 list_remove_head(&nvmet_sgl_list,
3947 sglq_entry, struct lpfc_sglq, list);
3948 if (sglq_entry) {
3949 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3950 sglq_entry->phys);
3951 kfree(sglq_entry);
3952 }
3953 }
3954 list_splice_init(&nvmet_sgl_list,
3955 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3956 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3957 spin_unlock_irq(&phba->hbalock);
3958 } else
3959 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3960 "6306 NVMET xri-sgl count unchanged: %d\n",
3961 nvmet_xri_cnt);
3962 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3963
3964 /* update xris to nvmet sgls on the list */
3965 sglq_entry = NULL;
3966 sglq_entry_next = NULL;
3967 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3968 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3969 lxri = lpfc_sli4_next_xritag(phba);
3970 if (lxri == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003971 lpfc_printf_log(phba, KERN_ERR,
3972 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08003973 "6307 Failed to allocate xri for "
3974 "NVMET sgl\n");
3975 rc = -ENOMEM;
3976 goto out_free_mem;
3977 }
3978 sglq_entry->sli4_lxritag = lxri;
3979 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3980 }
3981 return 0;
3982
3983out_free_mem:
3984 lpfc_free_nvmet_sgl_list(phba);
3985 return rc;
3986}
3987
James Smart5e5b5112019-01-28 11:14:22 -08003988int
3989lpfc_io_buf_flush(struct lpfc_hba *phba, struct list_head *cbuf)
3990{
3991 LIST_HEAD(blist);
3992 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003993 struct lpfc_io_buf *lpfc_cmd;
3994 struct lpfc_io_buf *iobufp, *prev_iobufp;
James Smart5e5b5112019-01-28 11:14:22 -08003995 int idx, cnt, xri, inserted;
3996
3997 cnt = 0;
3998 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3999 qp = &phba->sli4_hba.hdwq[idx];
4000 spin_lock_irq(&qp->io_buf_list_get_lock);
4001 spin_lock(&qp->io_buf_list_put_lock);
4002
4003 /* Take everything off the get and put lists */
4004 list_splice_init(&qp->lpfc_io_buf_list_get, &blist);
4005 list_splice(&qp->lpfc_io_buf_list_put, &blist);
4006 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
4007 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
4008 cnt += qp->get_io_bufs + qp->put_io_bufs;
4009 qp->get_io_bufs = 0;
4010 qp->put_io_bufs = 0;
4011 qp->total_io_bufs = 0;
4012 spin_unlock(&qp->io_buf_list_put_lock);
4013 spin_unlock_irq(&qp->io_buf_list_get_lock);
4014 }
4015
4016 /*
4017 * Take IO buffers off blist and put on cbuf sorted by XRI.
4018 * This is because POST_SGL takes a sequential range of XRIs
4019 * to post to the firmware.
4020 */
4021 for (idx = 0; idx < cnt; idx++) {
James Smartc4908502019-01-28 11:14:28 -08004022 list_remove_head(&blist, lpfc_cmd, struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08004023 if (!lpfc_cmd)
4024 return cnt;
4025 if (idx == 0) {
4026 list_add_tail(&lpfc_cmd->list, cbuf);
4027 continue;
4028 }
4029 xri = lpfc_cmd->cur_iocbq.sli4_xritag;
4030 inserted = 0;
4031 prev_iobufp = NULL;
4032 list_for_each_entry(iobufp, cbuf, list) {
4033 if (xri < iobufp->cur_iocbq.sli4_xritag) {
4034 if (prev_iobufp)
4035 list_add(&lpfc_cmd->list,
4036 &prev_iobufp->list);
4037 else
4038 list_add(&lpfc_cmd->list, cbuf);
4039 inserted = 1;
4040 break;
4041 }
4042 prev_iobufp = iobufp;
4043 }
4044 if (!inserted)
4045 list_add_tail(&lpfc_cmd->list, cbuf);
4046 }
4047 return cnt;
4048}
4049
4050int
4051lpfc_io_buf_replenish(struct lpfc_hba *phba, struct list_head *cbuf)
4052{
4053 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08004054 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -08004055 int idx, cnt;
4056
4057 qp = phba->sli4_hba.hdwq;
4058 cnt = 0;
4059 while (!list_empty(cbuf)) {
4060 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
4061 list_remove_head(cbuf, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -08004062 struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08004063 if (!lpfc_cmd)
4064 return cnt;
4065 cnt++;
4066 qp = &phba->sli4_hba.hdwq[idx];
James Smart1fbf9742019-01-28 11:14:26 -08004067 lpfc_cmd->hdwq_no = idx;
4068 lpfc_cmd->hdwq = qp;
James Smart5e5b5112019-01-28 11:14:22 -08004069 lpfc_cmd->cur_iocbq.wqe_cmpl = NULL;
4070 lpfc_cmd->cur_iocbq.iocb_cmpl = NULL;
4071 spin_lock(&qp->io_buf_list_put_lock);
4072 list_add_tail(&lpfc_cmd->list,
4073 &qp->lpfc_io_buf_list_put);
4074 qp->put_io_bufs++;
4075 qp->total_io_bufs++;
4076 spin_unlock(&qp->io_buf_list_put_lock);
4077 }
4078 }
4079 return cnt;
4080}
4081
James Smartf358dd02017-02-12 13:52:34 -08004082/**
James Smart5e5b5112019-01-28 11:14:22 -08004083 * lpfc_sli4_io_sgl_update - update xri-sgl sizing and mapping
James Smart895427b2017-02-12 13:52:30 -08004084 * @phba: pointer to lpfc hba data structure.
4085 *
4086 * This routine first calculates the sizes of the current els and allocated
4087 * scsi sgl lists, and then goes through all sgls to updates the physical
4088 * XRIs assigned due to port function reset. During port initialization, the
4089 * current els and allocated scsi sgl lists are 0s.
4090 *
4091 * Return codes
4092 * 0 - successful (for now, it always returns 0)
4093 **/
4094int
James Smart5e5b5112019-01-28 11:14:22 -08004095lpfc_sli4_io_sgl_update(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08004096{
James Smartc4908502019-01-28 11:14:28 -08004097 struct lpfc_io_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
James Smart0794d602019-01-28 11:14:19 -08004098 uint16_t i, lxri, els_xri_cnt;
James Smart5e5b5112019-01-28 11:14:22 -08004099 uint16_t io_xri_cnt, io_xri_max;
4100 LIST_HEAD(io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004101 int rc, cnt;
James Smart895427b2017-02-12 13:52:30 -08004102
4103 /*
James Smart0794d602019-01-28 11:14:19 -08004104 * update on pci function's allocated nvme xri-sgl list
James Smart895427b2017-02-12 13:52:30 -08004105 */
James Smart0794d602019-01-28 11:14:19 -08004106
4107 /* maximum number of xris available for nvme buffers */
James Smart895427b2017-02-12 13:52:30 -08004108 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart5e5b5112019-01-28 11:14:22 -08004109 io_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4110 phba->sli4_hba.io_xri_max = io_xri_max;
James Smart8a9d2e82012-05-09 21:16:12 -04004111
James Smarte8c0a772017-04-21 16:04:49 -07004112 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004113 "6074 Current allocated XRI sgl count:%d, "
4114 "maximum XRI count:%d\n",
James Smart5e5b5112019-01-28 11:14:22 -08004115 phba->sli4_hba.io_xri_cnt,
4116 phba->sli4_hba.io_xri_max);
James Smarte8c0a772017-04-21 16:04:49 -07004117
James Smart5e5b5112019-01-28 11:14:22 -08004118 cnt = lpfc_io_buf_flush(phba, &io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004119
James Smart5e5b5112019-01-28 11:14:22 -08004120 if (phba->sli4_hba.io_xri_cnt > phba->sli4_hba.io_xri_max) {
James Smart0794d602019-01-28 11:14:19 -08004121 /* max nvme xri shrunk below the allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004122 io_xri_cnt = phba->sli4_hba.io_xri_cnt -
4123 phba->sli4_hba.io_xri_max;
James Smart0794d602019-01-28 11:14:19 -08004124 /* release the extra allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004125 for (i = 0; i < io_xri_cnt; i++) {
4126 list_remove_head(&io_sgl_list, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -08004127 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -08004128 if (lpfc_ncmd) {
Romain Perier771db5c2017-07-06 10:13:05 +02004129 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart0794d602019-01-28 11:14:19 -08004130 lpfc_ncmd->data,
4131 lpfc_ncmd->dma_handle);
4132 kfree(lpfc_ncmd);
James Smarta2fc4aef2014-09-03 12:57:55 -04004133 }
James Smart8a9d2e82012-05-09 21:16:12 -04004134 }
James Smart5e5b5112019-01-28 11:14:22 -08004135 phba->sli4_hba.io_xri_cnt -= io_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04004136 }
4137
James Smart0794d602019-01-28 11:14:19 -08004138 /* update xris associated to remaining allocated nvme buffers */
4139 lpfc_ncmd = NULL;
4140 lpfc_ncmd_next = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08004141 phba->sli4_hba.io_xri_cnt = cnt;
James Smart0794d602019-01-28 11:14:19 -08004142 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
James Smart5e5b5112019-01-28 11:14:22 -08004143 &io_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04004144 lxri = lpfc_sli4_next_xritag(phba);
4145 if (lxri == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004146 lpfc_printf_log(phba, KERN_ERR,
4147 LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -08004148 "6075 Failed to allocate xri for "
4149 "nvme buffer\n");
James Smart8a9d2e82012-05-09 21:16:12 -04004150 rc = -ENOMEM;
4151 goto out_free_mem;
4152 }
James Smart0794d602019-01-28 11:14:19 -08004153 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
4154 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smart8a9d2e82012-05-09 21:16:12 -04004155 }
James Smart5e5b5112019-01-28 11:14:22 -08004156 cnt = lpfc_io_buf_replenish(phba, &io_sgl_list);
dea31012005-04-17 16:05:31 -05004157 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04004158
4159out_free_mem:
James Smart5e5b5112019-01-28 11:14:22 -08004160 lpfc_io_free(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04004161 return rc;
dea31012005-04-17 16:05:31 -05004162}
4163
James Smart0794d602019-01-28 11:14:19 -08004164/**
James Smart5e5b5112019-01-28 11:14:22 -08004165 * lpfc_new_io_buf - IO buffer allocator for HBA with SLI4 IF spec
Lee Jonesfe614ac2020-07-23 13:24:22 +01004166 * @phba: Pointer to lpfc hba data structure.
4167 * @num_to_alloc: The requested number of buffers to allocate.
James Smart0794d602019-01-28 11:14:19 -08004168 *
4169 * This routine allocates nvme buffers for device with SLI-4 interface spec,
4170 * the nvme buffer contains all the necessary information needed to initiate
4171 * an I/O. After allocating up to @num_to_allocate IO buffers and put
4172 * them on a list, it post them to the port by using SGL block post.
4173 *
4174 * Return codes:
James Smart5e5b5112019-01-28 11:14:22 -08004175 * int - number of IO buffers that were allocated and posted.
James Smart0794d602019-01-28 11:14:19 -08004176 * 0 = failure, less than num_to_alloc is a partial failure.
4177 **/
4178int
James Smart5e5b5112019-01-28 11:14:22 -08004179lpfc_new_io_buf(struct lpfc_hba *phba, int num_to_alloc)
James Smart0794d602019-01-28 11:14:19 -08004180{
James Smartc4908502019-01-28 11:14:28 -08004181 struct lpfc_io_buf *lpfc_ncmd;
James Smart0794d602019-01-28 11:14:19 -08004182 struct lpfc_iocbq *pwqeq;
4183 uint16_t iotag, lxri = 0;
4184 int bcnt, num_posted;
4185 LIST_HEAD(prep_nblist);
4186 LIST_HEAD(post_nblist);
4187 LIST_HEAD(nvme_nblist);
4188
James Smart5e5b5112019-01-28 11:14:22 -08004189 phba->sli4_hba.io_xri_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -08004190 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
James Smart7f9989b2019-08-27 14:27:46 -07004191 lpfc_ncmd = kzalloc(sizeof(*lpfc_ncmd), GFP_KERNEL);
James Smart0794d602019-01-28 11:14:19 -08004192 if (!lpfc_ncmd)
4193 break;
4194 /*
4195 * Get memory from the pci pool to map the virt space to
4196 * pci bus space for an I/O. The DMA buffer includes the
4197 * number of SGE's necessary to support the sg_tablesize.
4198 */
Thomas Meyera5c990e2019-05-29 22:21:36 +02004199 lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
4200 GFP_KERNEL,
4201 &lpfc_ncmd->dma_handle);
James Smart0794d602019-01-28 11:14:19 -08004202 if (!lpfc_ncmd->data) {
4203 kfree(lpfc_ncmd);
4204 break;
4205 }
James Smart0794d602019-01-28 11:14:19 -08004206
James Smartd79c9e92019-08-14 16:57:09 -07004207 if (phba->cfg_xpsgl && !phba->nvmet_support) {
4208 INIT_LIST_HEAD(&lpfc_ncmd->dma_sgl_xtra_list);
4209 } else {
4210 /*
4211 * 4K Page alignment is CRITICAL to BlockGuard, double
4212 * check to be sure.
4213 */
4214 if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
4215 (((unsigned long)(lpfc_ncmd->data) &
4216 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004217 lpfc_printf_log(phba, KERN_ERR,
4218 LOG_TRACE_EVENT,
James Smartd79c9e92019-08-14 16:57:09 -07004219 "3369 Memory alignment err: "
4220 "addr=%lx\n",
4221 (unsigned long)lpfc_ncmd->data);
4222 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4223 lpfc_ncmd->data,
4224 lpfc_ncmd->dma_handle);
4225 kfree(lpfc_ncmd);
4226 break;
4227 }
James Smart0794d602019-01-28 11:14:19 -08004228 }
4229
James Smartd79c9e92019-08-14 16:57:09 -07004230 INIT_LIST_HEAD(&lpfc_ncmd->dma_cmd_rsp_list);
4231
James Smart0794d602019-01-28 11:14:19 -08004232 lxri = lpfc_sli4_next_xritag(phba);
4233 if (lxri == NO_XRI) {
4234 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4235 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4236 kfree(lpfc_ncmd);
4237 break;
4238 }
4239 pwqeq = &lpfc_ncmd->cur_iocbq;
4240
4241 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
4242 iotag = lpfc_sli_next_iotag(phba, pwqeq);
4243 if (iotag == 0) {
4244 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4245 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4246 kfree(lpfc_ncmd);
Dick Kennedy372c1872020-06-30 14:50:00 -07004247 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -08004248 "6121 Failed to allocate IOTAG for"
4249 " XRI:0x%x\n", lxri);
4250 lpfc_sli4_free_xri(phba, lxri);
4251 break;
4252 }
4253 pwqeq->sli4_lxritag = lxri;
4254 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
4255 pwqeq->context1 = lpfc_ncmd;
4256
4257 /* Initialize local short-hand pointers. */
4258 lpfc_ncmd->dma_sgl = lpfc_ncmd->data;
4259 lpfc_ncmd->dma_phys_sgl = lpfc_ncmd->dma_handle;
4260 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
James Smartc2017262019-01-28 11:14:37 -08004261 spin_lock_init(&lpfc_ncmd->buf_lock);
James Smart0794d602019-01-28 11:14:19 -08004262
4263 /* add the nvme buffer to a post list */
4264 list_add_tail(&lpfc_ncmd->list, &post_nblist);
James Smart5e5b5112019-01-28 11:14:22 -08004265 phba->sli4_hba.io_xri_cnt++;
James Smart0794d602019-01-28 11:14:19 -08004266 }
4267 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
4268 "6114 Allocate %d out of %d requested new NVME "
4269 "buffers\n", bcnt, num_to_alloc);
4270
4271 /* post the list of nvme buffer sgls to port if available */
4272 if (!list_empty(&post_nblist))
James Smart5e5b5112019-01-28 11:14:22 -08004273 num_posted = lpfc_sli4_post_io_sgl_list(
James Smart0794d602019-01-28 11:14:19 -08004274 phba, &post_nblist, bcnt);
4275 else
4276 num_posted = 0;
4277
4278 return num_posted;
4279}
4280
James Smart96418b52017-03-04 09:30:31 -08004281static uint64_t
4282lpfc_get_wwpn(struct lpfc_hba *phba)
4283{
4284 uint64_t wwn;
4285 int rc;
4286 LPFC_MBOXQ_t *mboxq;
4287 MAILBOX_t *mb;
4288
James Smart96418b52017-03-04 09:30:31 -08004289 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4290 GFP_KERNEL);
4291 if (!mboxq)
4292 return (uint64_t)-1;
4293
4294 /* First get WWN of HBA instance */
4295 lpfc_read_nv(phba, mboxq);
4296 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4297 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004298 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart96418b52017-03-04 09:30:31 -08004299 "6019 Mailbox failed , mbxCmd x%x "
4300 "READ_NV, mbxStatus x%x\n",
4301 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4302 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
4303 mempool_free(mboxq, phba->mbox_mem_pool);
4304 return (uint64_t) -1;
4305 }
4306 mb = &mboxq->u.mb;
4307 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
4308 /* wwn is WWPN of HBA instance */
4309 mempool_free(mboxq, phba->mbox_mem_pool);
4310 if (phba->sli_rev == LPFC_SLI_REV4)
4311 return be64_to_cpu(wwn);
4312 else
Maurizio Lombardi286871a2017-08-23 16:55:48 -07004313 return rol64(wwn, 32);
James Smart96418b52017-03-04 09:30:31 -08004314}
4315
James Smarte59058c2008-08-24 21:49:00 -04004316/**
Gaurav Srivastava5e633302021-06-08 10:05:49 +05304317 * lpfc_vmid_res_alloc - Allocates resources for VMID
4318 * @phba: pointer to lpfc hba data structure.
4319 * @vport: pointer to vport data structure
4320 *
4321 * This routine allocated the resources needed for the VMID.
4322 *
4323 * Return codes
4324 * 0 on Success
4325 * Non-0 on Failure
4326 */
4327static int
4328lpfc_vmid_res_alloc(struct lpfc_hba *phba, struct lpfc_vport *vport)
4329{
4330 /* VMID feature is supported only on SLI4 */
4331 if (phba->sli_rev == LPFC_SLI_REV3) {
4332 phba->cfg_vmid_app_header = 0;
4333 phba->cfg_vmid_priority_tagging = 0;
4334 }
4335
4336 if (lpfc_is_vmid_enabled(phba)) {
4337 vport->vmid =
4338 kcalloc(phba->cfg_max_vmid, sizeof(struct lpfc_vmid),
4339 GFP_KERNEL);
4340 if (!vport->vmid)
4341 return -ENOMEM;
4342
4343 rwlock_init(&vport->vmid_lock);
4344
4345 /* Set the VMID parameters for the vport */
4346 vport->vmid_priority_tagging = phba->cfg_vmid_priority_tagging;
4347 vport->vmid_inactivity_timeout =
4348 phba->cfg_vmid_inactivity_timeout;
4349 vport->max_vmid = phba->cfg_max_vmid;
4350 vport->cur_vmid_cnt = 0;
4351
4352 vport->vmid_priority_range = bitmap_zalloc
4353 (LPFC_VMID_MAX_PRIORITY_RANGE, GFP_KERNEL);
4354
4355 if (!vport->vmid_priority_range) {
4356 kfree(vport->vmid);
4357 return -ENOMEM;
4358 }
4359
4360 hash_init(vport->hash_table);
4361 }
4362 return 0;
4363}
4364
4365/**
James Smart3621a712009-04-06 18:47:14 -04004366 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04004367 * @phba: pointer to lpfc hba data structure.
4368 * @instance: a unique integer ID to this FC port.
4369 * @dev: pointer to the device data structure.
4370 *
4371 * This routine creates a FC port for the upper layer protocol. The FC port
4372 * can be created on top of either a physical port or a virtual port provided
4373 * by the HBA. This routine also allocates a SCSI host data structure (shost)
4374 * and associates the FC port created before adding the shost into the SCSI
4375 * layer.
4376 *
4377 * Return codes
4378 * @vport - pointer to the virtual N_Port data structure.
4379 * NULL - port create failed.
4380 **/
James Smart2e0fef82007-06-17 19:56:36 -05004381struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04004382lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04004383{
James Smart2e0fef82007-06-17 19:56:36 -05004384 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08004385 struct Scsi_Host *shost = NULL;
James Smartc90b4482020-03-22 11:12:56 -07004386 struct scsi_host_template *template;
James Smart2e0fef82007-06-17 19:56:36 -05004387 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08004388 int i;
4389 uint64_t wwn;
4390 bool use_no_reset_hba = false;
James Smart56bc8022017-06-15 22:56:43 -07004391 int rc;
James Smart96418b52017-03-04 09:30:31 -08004392
James Smart56bc8022017-06-15 22:56:43 -07004393 if (lpfc_no_hba_reset_cnt) {
4394 if (phba->sli_rev < LPFC_SLI_REV4 &&
4395 dev == &phba->pcidev->dev) {
4396 /* Reset the port first */
4397 lpfc_sli_brdrestart(phba);
4398 rc = lpfc_sli_chipset_init(phba);
4399 if (rc)
4400 return NULL;
4401 }
4402 wwn = lpfc_get_wwpn(phba);
4403 }
James Smart96418b52017-03-04 09:30:31 -08004404
4405 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
4406 if (wwn == lpfc_no_hba_reset[i]) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004407 lpfc_printf_log(phba, KERN_ERR,
4408 LOG_TRACE_EVENT,
James Smart96418b52017-03-04 09:30:31 -08004409 "6020 Setting use_no_reset port=%llx\n",
4410 wwn);
4411 use_no_reset_hba = true;
4412 break;
4413 }
4414 }
James Smart47a86172007-04-25 09:53:22 -04004415
James Smartc90b4482020-03-22 11:12:56 -07004416 /* Seed template for SCSI host registration */
4417 if (dev == &phba->pcidev->dev) {
4418 template = &phba->port_template;
4419
4420 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
4421 /* Seed physical port template */
4422 memcpy(template, &lpfc_template, sizeof(*template));
4423
James Smart7c30bb62020-10-20 13:27:16 -07004424 if (use_no_reset_hba)
James Smartc90b4482020-03-22 11:12:56 -07004425 /* template is for a no reset SCSI Host */
James Smartc90b4482020-03-22 11:12:56 -07004426 template->eh_host_reset_handler = NULL;
James Smartc90b4482020-03-22 11:12:56 -07004427
4428 /* Template for all vports this physical port creates */
4429 memcpy(&phba->vport_template, &lpfc_template,
4430 sizeof(*template));
James Smartc90b4482020-03-22 11:12:56 -07004431 phba->vport_template.shost_attrs = lpfc_vport_attrs;
4432 phba->vport_template.eh_bus_reset_handler = NULL;
4433 phba->vport_template.eh_host_reset_handler = NULL;
4434 phba->vport_template.vendor_id = 0;
4435
4436 /* Initialize the host templates with updated value */
4437 if (phba->sli_rev == LPFC_SLI_REV4) {
4438 template->sg_tablesize = phba->cfg_scsi_seg_cnt;
4439 phba->vport_template.sg_tablesize =
4440 phba->cfg_scsi_seg_cnt;
4441 } else {
4442 template->sg_tablesize = phba->cfg_sg_seg_cnt;
4443 phba->vport_template.sg_tablesize =
4444 phba->cfg_sg_seg_cnt;
4445 }
4446
James Smart895427b2017-02-12 13:52:30 -08004447 } else {
James Smartc90b4482020-03-22 11:12:56 -07004448 /* NVMET is for physical port only */
4449 memcpy(template, &lpfc_template_nvme,
4450 sizeof(*template));
James Smart895427b2017-02-12 13:52:30 -08004451 }
James Smartc90b4482020-03-22 11:12:56 -07004452 } else {
4453 template = &phba->vport_template;
James Smartea4142f2015-04-07 15:07:13 -04004454 }
James Smartc90b4482020-03-22 11:12:56 -07004455
4456 shost = scsi_host_alloc(template, sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05004457 if (!shost)
4458 goto out;
James Smart47a86172007-04-25 09:53:22 -04004459
James Smart2e0fef82007-06-17 19:56:36 -05004460 vport = (struct lpfc_vport *) shost->hostdata;
4461 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05004462 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05004463 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05004464 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04004465 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08004466
James Smartf6e84792019-01-28 11:14:38 -08004467 /* Adjust value in vport */
4468 vport->cfg_enable_fc4_type = phba->cfg_enable_fc4_type;
4469
James Smart2e0fef82007-06-17 19:56:36 -05004470 shost->unique_id = instance;
4471 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04004472 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05004473 shost->this_id = -1;
4474 shost->max_cmd_len = 16;
James Smart6a828b02019-01-28 11:14:31 -08004475
James Smartda0436e2009-05-22 14:51:39 -04004476 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart77ffd342019-08-15 19:36:49 -07004477 if (!phba->cfg_fcp_mq_threshold ||
4478 phba->cfg_fcp_mq_threshold > phba->cfg_hdw_queue)
4479 phba->cfg_fcp_mq_threshold = phba->cfg_hdw_queue;
4480
4481 shost->nr_hw_queues = min_t(int, 2 * num_possible_nodes(),
4482 phba->cfg_fcp_mq_threshold);
James Smart6a828b02019-01-28 11:14:31 -08004483
James Smart28baac72010-02-12 14:42:03 -05004484 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04004485 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartd79c9e92019-08-14 16:57:09 -07004486
4487 if (phba->cfg_xpsgl && !phba->nvmet_support)
4488 shost->sg_tablesize = LPFC_MAX_SG_TABLESIZE;
4489 else
4490 shost->sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smartace44e42019-01-28 11:14:27 -08004491 } else
4492 /* SLI-3 has a limited number of hardware queues (3),
4493 * thus there is only one for FCP processing.
4494 */
4495 shost->nr_hw_queues = 1;
James Smart81301a92008-12-04 22:39:46 -05004496
James Smart47a86172007-04-25 09:53:22 -04004497 /*
James Smart2e0fef82007-06-17 19:56:36 -05004498 * Set initial can_queue value since 0 is no longer supported and
4499 * scsi_add_host will fail. This will be adjusted later based on the
4500 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04004501 */
James Smart2e0fef82007-06-17 19:56:36 -05004502 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04004503 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05004504 shost->transportt = lpfc_vport_transport_template;
4505 vport->port_type = LPFC_NPIV_PORT;
4506 } else {
4507 shost->transportt = lpfc_transport_template;
4508 vport->port_type = LPFC_PHYSICAL_PORT;
4509 }
James Smart47a86172007-04-25 09:53:22 -04004510
James Smartc90b4482020-03-22 11:12:56 -07004511 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4512 "9081 CreatePort TMPLATE type %x TBLsize %d "
4513 "SEGcnt %d/%d\n",
4514 vport->port_type, shost->sg_tablesize,
4515 phba->cfg_scsi_seg_cnt, phba->cfg_sg_seg_cnt);
4516
Gaurav Srivastava5e633302021-06-08 10:05:49 +05304517 /* Allocate the resources for VMID */
4518 rc = lpfc_vmid_res_alloc(phba, vport);
4519
4520 if (rc)
4521 goto out;
4522
James Smart2e0fef82007-06-17 19:56:36 -05004523 /* Initialize all internally managed lists. */
4524 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04004525 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05004526 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04004527
Kees Cookf22eb4d2017-09-06 20:24:26 -07004528 timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
James Smart47a86172007-04-25 09:53:22 -04004529
Kees Cookf22eb4d2017-09-06 20:24:26 -07004530 timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
James Smart92494142011-02-16 12:39:44 -05004531
Kees Cookf22eb4d2017-09-06 20:24:26 -07004532 timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
James Smart92494142011-02-16 12:39:44 -05004533
James Smartaa6ff302019-05-21 17:49:09 -07004534 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4535 lpfc_setup_bg(phba, shost);
4536
James Bottomleyd139b9b2009-11-05 13:33:12 -06004537 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004538 if (error)
4539 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04004540
James Smart523128e2018-09-10 10:30:46 -07004541 spin_lock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004542 list_add_tail(&vport->listentry, &phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07004543 spin_unlock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004544 return vport;
James Smart47a86172007-04-25 09:53:22 -04004545
James Smart2e0fef82007-06-17 19:56:36 -05004546out_put_shost:
Gaurav Srivastava5e633302021-06-08 10:05:49 +05304547 kfree(vport->vmid);
4548 bitmap_free(vport->vmid_priority_range);
James Smart2e0fef82007-06-17 19:56:36 -05004549 scsi_host_put(shost);
4550out:
4551 return NULL;
James Smart47a86172007-04-25 09:53:22 -04004552}
4553
James Smarte59058c2008-08-24 21:49:00 -04004554/**
James Smart3621a712009-04-06 18:47:14 -04004555 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04004556 * @vport: pointer to an lpfc virtual N_Port data structure.
4557 *
4558 * This routine destroys a FC port from the upper layer protocol. All the
4559 * resources associated with the port are released.
4560 **/
James Smart2e0fef82007-06-17 19:56:36 -05004561void
4562destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04004563{
James Smart92d7f7b2007-06-17 19:56:38 -05004564 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4565 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004566
James Smart858c9f62007-06-17 19:56:39 -05004567 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05004568 fc_remove_host(shost);
4569 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04004570
James Smart523128e2018-09-10 10:30:46 -07004571 spin_lock_irq(&phba->port_list_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004572 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -07004573 spin_unlock_irq(&phba->port_list_lock);
James Smart47a86172007-04-25 09:53:22 -04004574
James Smart92d7f7b2007-06-17 19:56:38 -05004575 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04004576 return;
James Smart47a86172007-04-25 09:53:22 -04004577}
4578
James Smarte59058c2008-08-24 21:49:00 -04004579/**
James Smart3621a712009-04-06 18:47:14 -04004580 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04004581 *
4582 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4583 * uses the kernel idr facility to perform the task.
4584 *
4585 * Return codes:
4586 * instance - a unique integer ID allocated as the new instance.
4587 * -1 - lpfc get instance failed.
4588 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004589int
4590lpfc_get_instance(void)
4591{
Tejun Heoab516032013-02-27 17:04:44 -08004592 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004593
Tejun Heoab516032013-02-27 17:04:44 -08004594 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4595 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004596}
4597
James Smarte59058c2008-08-24 21:49:00 -04004598/**
James Smart3621a712009-04-06 18:47:14 -04004599 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04004600 * @shost: pointer to SCSI host data structure.
4601 * @time: elapsed time of the scan in jiffies.
4602 *
4603 * This routine is called by the SCSI layer with a SCSI host to determine
4604 * whether the scan host is finished.
4605 *
4606 * Note: there is no scan_start function as adapter initialization will have
4607 * asynchronously kicked off the link initialization.
4608 *
4609 * Return codes
4610 * 0 - SCSI host scan is not over yet.
4611 * 1 - SCSI host scan is over.
4612 **/
James Smart47a86172007-04-25 09:53:22 -04004613int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4614{
James Smart2e0fef82007-06-17 19:56:36 -05004615 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4616 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05004617 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04004618
James Smart858c9f62007-06-17 19:56:39 -05004619 spin_lock_irq(shost->host_lock);
4620
James Smart51ef4c22007-08-02 11:10:31 -04004621 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05004622 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004623 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05004624 }
James Smart256ec0d2013-04-17 20:14:58 -04004625 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05004626 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004627 "0461 Scanning longer than 30 "
4628 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004629 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004630 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004631 }
James Smart256ec0d2013-04-17 20:14:58 -04004632 if (time >= msecs_to_jiffies(15 * 1000) &&
4633 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05004634 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004635 "0465 Link down longer than 15 "
4636 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004637 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004638 goto finished;
James Smart47a86172007-04-25 09:53:22 -04004639 }
4640
James Smart2e0fef82007-06-17 19:56:36 -05004641 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05004642 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004643 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05004644 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04004645 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05004646 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004647 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05004648 goto finished;
4649
4650 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004651
4652finished:
James Smart858c9f62007-06-17 19:56:39 -05004653 spin_unlock_irq(shost->host_lock);
4654 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05004655}
4656
Bart Van Assche3999df72019-03-28 11:06:16 -07004657static void lpfc_host_supported_speeds_set(struct Scsi_Host *shost)
James Smartcd713482018-10-23 13:41:01 -07004658{
4659 struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4660 struct lpfc_hba *phba = vport->phba;
4661
4662 fc_host_supported_speeds(shost) = 0;
Dick Kennedya1e4d3d2020-08-03 14:02:22 -07004663 /*
4664 * Avoid reporting supported link speed for FCoE as it can't be
4665 * controlled via FCoE.
4666 */
4667 if (phba->hba_flag & HBA_FCOE_MODE)
4668 return;
4669
James Smart1dc5ec22018-10-23 13:41:11 -07004670 if (phba->lmt & LMT_128Gb)
4671 fc_host_supported_speeds(shost) |= FC_PORTSPEED_128GBIT;
James Smartcd713482018-10-23 13:41:01 -07004672 if (phba->lmt & LMT_64Gb)
4673 fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
4674 if (phba->lmt & LMT_32Gb)
4675 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4676 if (phba->lmt & LMT_16Gb)
4677 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4678 if (phba->lmt & LMT_10Gb)
4679 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4680 if (phba->lmt & LMT_8Gb)
4681 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4682 if (phba->lmt & LMT_4Gb)
4683 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4684 if (phba->lmt & LMT_2Gb)
4685 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4686 if (phba->lmt & LMT_1Gb)
4687 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4688}
4689
James Smarte59058c2008-08-24 21:49:00 -04004690/**
James Smart3621a712009-04-06 18:47:14 -04004691 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04004692 * @shost: pointer to SCSI host data structure.
4693 *
4694 * This routine initializes a given SCSI host attributes on a FC port. The
4695 * SCSI host can be either on top of a physical port or a virtual port.
4696 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004697void lpfc_host_attrib_init(struct Scsi_Host *shost)
4698{
4699 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4700 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004701 /*
James Smart2e0fef82007-06-17 19:56:36 -05004702 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04004703 */
4704
James Smart2e0fef82007-06-17 19:56:36 -05004705 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4706 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04004707 fc_host_supported_classes(shost) = FC_COS_CLASS3;
4708
4709 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004710 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004711 fc_host_supported_fc4s(shost)[2] = 1;
4712 fc_host_supported_fc4s(shost)[7] = 1;
4713
James Smart92d7f7b2007-06-17 19:56:38 -05004714 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4715 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04004716
James Smartcd713482018-10-23 13:41:01 -07004717 lpfc_host_supported_speeds_set(shost);
James Smart47a86172007-04-25 09:53:22 -04004718
4719 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05004720 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4721 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04004722
Mike Christie0af5d702010-09-15 16:52:31 -05004723 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4724
James Smart47a86172007-04-25 09:53:22 -04004725 /* This value is also unchanging */
4726 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004727 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004728 fc_host_active_fc4s(shost)[2] = 1;
4729 fc_host_active_fc4s(shost)[7] = 1;
4730
James Smart92d7f7b2007-06-17 19:56:38 -05004731 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04004732 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04004733 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04004734 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04004735}
dea31012005-04-17 16:05:31 -05004736
James Smarte59058c2008-08-24 21:49:00 -04004737/**
James Smartda0436e2009-05-22 14:51:39 -04004738 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04004739 * @phba: pointer to lpfc hba data structure.
4740 *
James Smartda0436e2009-05-22 14:51:39 -04004741 * This routine is invoked to stop an SLI3 device port, it stops the device
4742 * from generating interrupts and stops the device driver's timers for the
4743 * device.
James Smarte59058c2008-08-24 21:49:00 -04004744 **/
James Smartdb2378e2008-02-08 18:49:51 -05004745static void
James Smartda0436e2009-05-22 14:51:39 -04004746lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05004747{
James Smartda0436e2009-05-22 14:51:39 -04004748 /* Clear all interrupt enable conditions */
4749 writel(0, phba->HCregaddr);
4750 readl(phba->HCregaddr); /* flush */
4751 /* Clear all pending interrupts */
4752 writel(0xffffffff, phba->HAregaddr);
4753 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004754
James Smartda0436e2009-05-22 14:51:39 -04004755 /* Reset some HBA SLI setup states */
4756 lpfc_stop_hba_timers(phba);
4757 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004758}
4759
James Smarte59058c2008-08-24 21:49:00 -04004760/**
James Smartda0436e2009-05-22 14:51:39 -04004761 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004762 * @phba: pointer to lpfc hba data structure.
4763 *
James Smartda0436e2009-05-22 14:51:39 -04004764 * This routine is invoked to stop an SLI4 device port, it stops the device
4765 * from generating interrupts and stops the device driver's timers for the
4766 * device.
4767 **/
4768static void
4769lpfc_stop_port_s4(struct lpfc_hba *phba)
4770{
4771 /* Reset some HBA SLI4 setup states */
4772 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -08004773 if (phba->pport)
4774 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004775 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004776}
4777
4778/**
4779 * lpfc_stop_port - Wrapper function for stopping hba port
4780 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004781 *
James Smartda0436e2009-05-22 14:51:39 -04004782 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4783 * the API jump table function pointer from the lpfc_hba struct.
4784 **/
4785void
4786lpfc_stop_port(struct lpfc_hba *phba)
4787{
4788 phba->lpfc_stop_port(phba);
Dick Kennedyf485c182017-09-29 17:34:34 -07004789
4790 if (phba->wq)
4791 flush_workqueue(phba->wq);
James Smartda0436e2009-05-22 14:51:39 -04004792}
4793
4794/**
James Smartecfd03c2010-02-12 14:41:27 -05004795 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4796 * @phba: Pointer to hba for which this call is being executed.
4797 *
4798 * This routine starts the timer waiting for the FCF rediscovery to complete.
4799 **/
4800void
4801lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4802{
4803 unsigned long fcf_redisc_wait_tmo =
4804 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4805 /* Start fcf rediscovery wait period timer */
4806 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4807 spin_lock_irq(&phba->hbalock);
4808 /* Allow action to new fcf asynchronous event */
4809 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4810 /* Mark the FCF rediscovery pending state */
4811 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4812 spin_unlock_irq(&phba->hbalock);
4813}
4814
4815/**
4816 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
Lee Jonesfe614ac2020-07-23 13:24:22 +01004817 * @t: Timer context used to obtain the pointer to lpfc hba data structure.
James Smartecfd03c2010-02-12 14:41:27 -05004818 *
4819 * This routine is invoked when waiting for FCF table rediscover has been
4820 * timed out. If new FCF record(s) has (have) been discovered during the
4821 * wait period, a new FCF event shall be added to the FCOE async event
4822 * list, and then worker thread shall be waked up for processing from the
4823 * worker thread context.
4824 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004825static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07004826lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
James Smartecfd03c2010-02-12 14:41:27 -05004827{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004828 struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
James Smartecfd03c2010-02-12 14:41:27 -05004829
4830 /* Don't send FCF rediscovery event if timer cancelled */
4831 spin_lock_irq(&phba->hbalock);
4832 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4833 spin_unlock_irq(&phba->hbalock);
4834 return;
4835 }
4836 /* Clear FCF rediscovery timer pending flag */
4837 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4838 /* FCF rediscovery event to worker thread */
4839 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4840 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004841 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004842 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004843 /* wake up worker thread */
4844 lpfc_worker_wake_up(phba);
4845}
4846
4847/**
Gaurav Srivastava20397172021-06-08 10:05:54 +05304848 * lpfc_vmid_poll - VMID timeout detection
James Smart50baa152021-07-07 11:43:35 -07004849 * @t: Timer context used to obtain the pointer to lpfc hba data structure.
Gaurav Srivastava20397172021-06-08 10:05:54 +05304850 *
4851 * This routine is invoked when there is no I/O on by a VM for the specified
4852 * amount of time. When this situation is detected, the VMID has to be
4853 * deregistered from the switch and all the local resources freed. The VMID
4854 * will be reassigned to the VM once the I/O begins.
4855 **/
4856static void
4857lpfc_vmid_poll(struct timer_list *t)
4858{
4859 struct lpfc_hba *phba = from_timer(phba, t, inactive_vmid_poll);
4860 u32 wake_up = 0;
4861
4862 /* check if there is a need to issue QFPA */
4863 if (phba->pport->vmid_priority_tagging) {
4864 wake_up = 1;
4865 phba->pport->work_port_events |= WORKER_CHECK_VMID_ISSUE_QFPA;
4866 }
4867
4868 /* Is the vmid inactivity timer enabled */
4869 if (phba->pport->vmid_inactivity_timeout ||
4870 phba->pport->load_flag & FC_DEREGISTER_ALL_APP_ID) {
4871 wake_up = 1;
4872 phba->pport->work_port_events |= WORKER_CHECK_INACTIVE_VMID;
4873 }
4874
4875 if (wake_up)
4876 lpfc_worker_wake_up(phba);
4877
4878 /* restart the timer for the next iteration */
4879 mod_timer(&phba->inactive_vmid_poll, jiffies + msecs_to_jiffies(1000 *
4880 LPFC_VMID_TIMER));
4881}
4882
4883/**
James Smartda0436e2009-05-22 14:51:39 -04004884 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4885 * @phba: pointer to lpfc hba data structure.
4886 * @acqe_link: pointer to the async link completion queue entry.
4887 *
James Smart23288b72018-05-04 20:37:53 -07004888 * This routine is to parse the SLI4 link-attention link fault code.
James Smartda0436e2009-05-22 14:51:39 -04004889 **/
James Smart23288b72018-05-04 20:37:53 -07004890static void
James Smartda0436e2009-05-22 14:51:39 -04004891lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4892 struct lpfc_acqe_link *acqe_link)
4893{
James Smartda0436e2009-05-22 14:51:39 -04004894 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4895 case LPFC_ASYNC_LINK_FAULT_NONE:
4896 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4897 case LPFC_ASYNC_LINK_FAULT_REMOTE:
James Smart23288b72018-05-04 20:37:53 -07004898 case LPFC_ASYNC_LINK_FAULT_LR_LRR:
James Smartda0436e2009-05-22 14:51:39 -04004899 break;
4900 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07004901 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart23288b72018-05-04 20:37:53 -07004902 "0398 Unknown link fault code: x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04004903 bf_get(lpfc_acqe_link_fault, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004904 break;
4905 }
James Smartda0436e2009-05-22 14:51:39 -04004906}
4907
4908/**
4909 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4910 * @phba: pointer to lpfc hba data structure.
4911 * @acqe_link: pointer to the async link completion queue entry.
4912 *
4913 * This routine is to parse the SLI4 link attention type and translate it
4914 * into the base driver's link attention type coding.
4915 *
4916 * Return: Link attention type in terms of base driver's coding.
4917 **/
4918static uint8_t
4919lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4920 struct lpfc_acqe_link *acqe_link)
4921{
4922 uint8_t att_type;
4923
4924 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4925 case LPFC_ASYNC_LINK_STATUS_DOWN:
4926 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004927 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004928 break;
4929 case LPFC_ASYNC_LINK_STATUS_UP:
4930 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004931 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004932 break;
4933 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004934 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004935 break;
4936 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07004937 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04004938 "0399 Invalid link attention type: x%x\n",
4939 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004940 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004941 break;
4942 }
4943 return att_type;
4944}
4945
4946/**
James Smart8b68cd52012-09-29 11:32:37 -04004947 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4948 * @phba: pointer to lpfc hba data structure.
4949 *
4950 * This routine is to get an SLI3 FC port's link speed in Mbps.
4951 *
4952 * Return: link speed in terms of Mbps.
4953 **/
4954uint32_t
4955lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4956{
4957 uint32_t link_speed;
4958
4959 if (!lpfc_is_link_up(phba))
4960 return 0;
4961
James Smarta085e872015-12-16 18:12:02 -05004962 if (phba->sli_rev <= LPFC_SLI_REV3) {
4963 switch (phba->fc_linkspeed) {
4964 case LPFC_LINK_SPEED_1GHZ:
4965 link_speed = 1000;
4966 break;
4967 case LPFC_LINK_SPEED_2GHZ:
4968 link_speed = 2000;
4969 break;
4970 case LPFC_LINK_SPEED_4GHZ:
4971 link_speed = 4000;
4972 break;
4973 case LPFC_LINK_SPEED_8GHZ:
4974 link_speed = 8000;
4975 break;
4976 case LPFC_LINK_SPEED_10GHZ:
4977 link_speed = 10000;
4978 break;
4979 case LPFC_LINK_SPEED_16GHZ:
4980 link_speed = 16000;
4981 break;
4982 default:
4983 link_speed = 0;
4984 }
4985 } else {
4986 if (phba->sli4_hba.link_state.logical_speed)
4987 link_speed =
4988 phba->sli4_hba.link_state.logical_speed;
4989 else
4990 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004991 }
4992 return link_speed;
4993}
4994
4995/**
4996 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4997 * @phba: pointer to lpfc hba data structure.
4998 * @evt_code: asynchronous event code.
4999 * @speed_code: asynchronous event link speed code.
5000 *
5001 * This routine is to parse the giving SLI4 async event link speed code into
5002 * value of Mbps for the link speed.
5003 *
5004 * Return: link speed in terms of Mbps.
5005 **/
5006static uint32_t
5007lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
5008 uint8_t speed_code)
5009{
5010 uint32_t port_speed;
5011
5012 switch (evt_code) {
5013 case LPFC_TRAILER_CODE_LINK:
5014 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04005015 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04005016 port_speed = 0;
5017 break;
James Smart26d830e2015-04-07 15:07:17 -04005018 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04005019 port_speed = 10;
5020 break;
James Smart26d830e2015-04-07 15:07:17 -04005021 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04005022 port_speed = 100;
5023 break;
James Smart26d830e2015-04-07 15:07:17 -04005024 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04005025 port_speed = 1000;
5026 break;
James Smart26d830e2015-04-07 15:07:17 -04005027 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04005028 port_speed = 10000;
5029 break;
James Smart26d830e2015-04-07 15:07:17 -04005030 case LPFC_ASYNC_LINK_SPEED_20GBPS:
5031 port_speed = 20000;
5032 break;
5033 case LPFC_ASYNC_LINK_SPEED_25GBPS:
5034 port_speed = 25000;
5035 break;
5036 case LPFC_ASYNC_LINK_SPEED_40GBPS:
5037 port_speed = 40000;
5038 break;
Dick Kennedya1e4d3d2020-08-03 14:02:22 -07005039 case LPFC_ASYNC_LINK_SPEED_100GBPS:
5040 port_speed = 100000;
5041 break;
James Smart8b68cd52012-09-29 11:32:37 -04005042 default:
5043 port_speed = 0;
5044 }
5045 break;
5046 case LPFC_TRAILER_CODE_FC:
5047 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04005048 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04005049 port_speed = 0;
5050 break;
James Smart26d830e2015-04-07 15:07:17 -04005051 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04005052 port_speed = 1000;
5053 break;
James Smart26d830e2015-04-07 15:07:17 -04005054 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04005055 port_speed = 2000;
5056 break;
James Smart26d830e2015-04-07 15:07:17 -04005057 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04005058 port_speed = 4000;
5059 break;
James Smart26d830e2015-04-07 15:07:17 -04005060 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04005061 port_speed = 8000;
5062 break;
James Smart26d830e2015-04-07 15:07:17 -04005063 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04005064 port_speed = 10000;
5065 break;
James Smart26d830e2015-04-07 15:07:17 -04005066 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04005067 port_speed = 16000;
5068 break;
James Smartd38dd522015-08-31 16:48:17 -04005069 case LPFC_FC_LA_SPEED_32G:
5070 port_speed = 32000;
5071 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08005072 case LPFC_FC_LA_SPEED_64G:
5073 port_speed = 64000;
5074 break;
James Smart1dc5ec22018-10-23 13:41:11 -07005075 case LPFC_FC_LA_SPEED_128G:
5076 port_speed = 128000;
5077 break;
James Smart8b68cd52012-09-29 11:32:37 -04005078 default:
5079 port_speed = 0;
5080 }
5081 break;
5082 default:
5083 port_speed = 0;
5084 }
5085 return port_speed;
5086}
5087
5088/**
James Smart70f3c072010-12-15 17:57:33 -05005089 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04005090 * @phba: pointer to lpfc hba data structure.
5091 * @acqe_link: pointer to the async link completion queue entry.
5092 *
James Smart70f3c072010-12-15 17:57:33 -05005093 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04005094 **/
5095static void
5096lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
5097 struct lpfc_acqe_link *acqe_link)
5098{
5099 struct lpfc_dmabuf *mp;
5100 LPFC_MBOXQ_t *pmb;
5101 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05005102 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04005103 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05005104 int rc;
James Smartda0436e2009-05-22 14:51:39 -04005105
5106 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05005107 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04005108 return;
James Smart32b97932009-07-19 10:01:21 -04005109 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005110 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5111 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005112 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04005113 "0395 The mboxq allocation failed\n");
5114 return;
5115 }
5116 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5117 if (!mp) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005118 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04005119 "0396 The lpfc_dmabuf allocation failed\n");
5120 goto out_free_pmb;
5121 }
5122 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
5123 if (!mp->virt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005124 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04005125 "0397 The mbuf allocation failed\n");
5126 goto out_free_dmabuf;
5127 }
5128
5129 /* Cleanup any outstanding ELS commands */
5130 lpfc_els_flush_all_cmd(phba);
5131
5132 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08005133 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04005134
5135 /* Update link event statistics */
5136 phba->sli.slistat.link_event++;
5137
James Smart76a95d72010-11-20 23:11:48 -05005138 /* Create lpfc_handle_latt mailbox command from link ACQE */
5139 lpfc_read_topology(phba, pmb, mp);
5140 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04005141 pmb->vport = phba->pport;
5142
James Smartda0436e2009-05-22 14:51:39 -04005143 /* Keep the link status for extra SLI4 state machine reference */
5144 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04005145 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
5146 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04005147 phba->sli4_hba.link_state.duplex =
5148 bf_get(lpfc_acqe_link_duplex, acqe_link);
5149 phba->sli4_hba.link_state.status =
5150 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05005151 phba->sli4_hba.link_state.type =
5152 bf_get(lpfc_acqe_link_type, acqe_link);
5153 phba->sli4_hba.link_state.number =
5154 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04005155 phba->sli4_hba.link_state.fault =
5156 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05005157 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005158 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
5159
James Smart70f3c072010-12-15 17:57:33 -05005160 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04005161 "2900 Async FC/FCoE Link event - Speed:%dGBit "
5162 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
5163 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05005164 phba->sli4_hba.link_state.speed,
5165 phba->sli4_hba.link_state.topology,
5166 phba->sli4_hba.link_state.status,
5167 phba->sli4_hba.link_state.type,
5168 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04005169 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05005170 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05005171 /*
5172 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
5173 * topology info. Note: Optional for non FC-AL ports.
5174 */
5175 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
5176 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5177 if (rc == MBX_NOT_FINISHED)
5178 goto out_free_dmabuf;
5179 return;
5180 }
5181 /*
5182 * For FCoE Mode: fill in all the topology information we need and call
5183 * the READ_TOPOLOGY completion routine to continue without actually
5184 * sending the READ_TOPOLOGY mailbox command to the port.
5185 */
James Smart23288b72018-05-04 20:37:53 -07005186 /* Initialize completion status */
James Smart76a95d72010-11-20 23:11:48 -05005187 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07005188 mb->mbxStatus = MBX_SUCCESS;
5189
5190 /* Parse port fault information field */
5191 lpfc_sli4_parse_latt_fault(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05005192
5193 /* Parse and translate link attention fields */
5194 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
5195 la->eventTag = acqe_link->event_tag;
5196 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
5197 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05005198 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05005199
5200 /* Fake the the following irrelvant fields */
5201 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
5202 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
5203 bf_set(lpfc_mbx_read_top_il, la, 0);
5204 bf_set(lpfc_mbx_read_top_pb, la, 0);
5205 bf_set(lpfc_mbx_read_top_fa, la, 0);
5206 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04005207
5208 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05005209 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04005210
5211 return;
5212
5213out_free_dmabuf:
5214 kfree(mp);
5215out_free_pmb:
5216 mempool_free(pmb, phba->mbox_mem_pool);
5217}
5218
5219/**
James Smart1dc5ec22018-10-23 13:41:11 -07005220 * lpfc_async_link_speed_to_read_top - Parse async evt link speed code to read
5221 * topology.
5222 * @phba: pointer to lpfc hba data structure.
James Smart1dc5ec22018-10-23 13:41:11 -07005223 * @speed_code: asynchronous event link speed code.
5224 *
5225 * This routine is to parse the giving SLI4 async event link speed code into
5226 * value of Read topology link speed.
5227 *
5228 * Return: link speed in terms of Read topology.
5229 **/
5230static uint8_t
5231lpfc_async_link_speed_to_read_top(struct lpfc_hba *phba, uint8_t speed_code)
5232{
5233 uint8_t port_speed;
5234
5235 switch (speed_code) {
5236 case LPFC_FC_LA_SPEED_1G:
5237 port_speed = LPFC_LINK_SPEED_1GHZ;
5238 break;
5239 case LPFC_FC_LA_SPEED_2G:
5240 port_speed = LPFC_LINK_SPEED_2GHZ;
5241 break;
5242 case LPFC_FC_LA_SPEED_4G:
5243 port_speed = LPFC_LINK_SPEED_4GHZ;
5244 break;
5245 case LPFC_FC_LA_SPEED_8G:
5246 port_speed = LPFC_LINK_SPEED_8GHZ;
5247 break;
5248 case LPFC_FC_LA_SPEED_16G:
5249 port_speed = LPFC_LINK_SPEED_16GHZ;
5250 break;
5251 case LPFC_FC_LA_SPEED_32G:
5252 port_speed = LPFC_LINK_SPEED_32GHZ;
5253 break;
5254 case LPFC_FC_LA_SPEED_64G:
5255 port_speed = LPFC_LINK_SPEED_64GHZ;
5256 break;
5257 case LPFC_FC_LA_SPEED_128G:
5258 port_speed = LPFC_LINK_SPEED_128GHZ;
5259 break;
5260 case LPFC_FC_LA_SPEED_256G:
5261 port_speed = LPFC_LINK_SPEED_256GHZ;
5262 break;
5263 default:
5264 port_speed = 0;
5265 break;
5266 }
5267
5268 return port_speed;
5269}
5270
5271#define trunk_link_status(__idx)\
5272 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5273 ((phba->trunk_link.link##__idx.state == LPFC_LINK_UP) ?\
5274 "Link up" : "Link down") : "NA"
5275/* Did port __idx reported an error */
5276#define trunk_port_fault(__idx)\
5277 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5278 (port_fault & (1 << __idx) ? "YES" : "NO") : "NA"
5279
5280static void
5281lpfc_update_trunk_link_status(struct lpfc_hba *phba,
5282 struct lpfc_acqe_fc_la *acqe_fc)
5283{
5284 uint8_t port_fault = bf_get(lpfc_acqe_fc_la_trunk_linkmask, acqe_fc);
5285 uint8_t err = bf_get(lpfc_acqe_fc_la_trunk_fault, acqe_fc);
5286
5287 phba->sli4_hba.link_state.speed =
5288 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5289 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5290
5291 phba->sli4_hba.link_state.logical_speed =
James Smartb8e6f132019-05-21 17:49:04 -07005292 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart1dc5ec22018-10-23 13:41:11 -07005293 /* We got FC link speed, convert to fc_linkspeed (READ_TOPOLOGY) */
5294 phba->fc_linkspeed =
5295 lpfc_async_link_speed_to_read_top(
5296 phba,
5297 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5298
5299 if (bf_get(lpfc_acqe_fc_la_trunk_config_port0, acqe_fc)) {
5300 phba->trunk_link.link0.state =
5301 bf_get(lpfc_acqe_fc_la_trunk_link_status_port0, acqe_fc)
5302 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005303 phba->trunk_link.link0.fault = port_fault & 0x1 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005304 }
5305 if (bf_get(lpfc_acqe_fc_la_trunk_config_port1, acqe_fc)) {
5306 phba->trunk_link.link1.state =
5307 bf_get(lpfc_acqe_fc_la_trunk_link_status_port1, acqe_fc)
5308 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005309 phba->trunk_link.link1.fault = port_fault & 0x2 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005310 }
5311 if (bf_get(lpfc_acqe_fc_la_trunk_config_port2, acqe_fc)) {
5312 phba->trunk_link.link2.state =
5313 bf_get(lpfc_acqe_fc_la_trunk_link_status_port2, acqe_fc)
5314 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005315 phba->trunk_link.link2.fault = port_fault & 0x4 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005316 }
5317 if (bf_get(lpfc_acqe_fc_la_trunk_config_port3, acqe_fc)) {
5318 phba->trunk_link.link3.state =
5319 bf_get(lpfc_acqe_fc_la_trunk_link_status_port3, acqe_fc)
5320 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005321 phba->trunk_link.link3.fault = port_fault & 0x8 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005322 }
5323
Dick Kennedy372c1872020-06-30 14:50:00 -07005324 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1dc5ec22018-10-23 13:41:11 -07005325 "2910 Async FC Trunking Event - Speed:%d\n"
5326 "\tLogical speed:%d "
5327 "port0: %s port1: %s port2: %s port3: %s\n",
5328 phba->sli4_hba.link_state.speed,
5329 phba->sli4_hba.link_state.logical_speed,
5330 trunk_link_status(0), trunk_link_status(1),
5331 trunk_link_status(2), trunk_link_status(3));
5332
5333 if (port_fault)
Dick Kennedy372c1872020-06-30 14:50:00 -07005334 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1dc5ec22018-10-23 13:41:11 -07005335 "3202 trunk error:0x%x (%s) seen on port0:%s "
5336 /*
5337 * SLI-4: We have only 0xA error codes
5338 * defined as of now. print an appropriate
5339 * message in case driver needs to be updated.
5340 */
5341 "port1:%s port2:%s port3:%s\n", err, err > 0xA ?
5342 "UNDEFINED. update driver." : trunk_errmsg[err],
5343 trunk_port_fault(0), trunk_port_fault(1),
5344 trunk_port_fault(2), trunk_port_fault(3));
5345}
5346
5347
5348/**
James Smart70f3c072010-12-15 17:57:33 -05005349 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
5350 * @phba: pointer to lpfc hba data structure.
5351 * @acqe_fc: pointer to the async fc completion queue entry.
5352 *
5353 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
5354 * that the event was received and then issue a read_topology mailbox command so
5355 * that the rest of the driver will treat it the same as SLI3.
5356 **/
5357static void
5358lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
5359{
5360 struct lpfc_dmabuf *mp;
5361 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07005362 MAILBOX_t *mb;
5363 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05005364 int rc;
5365
5366 if (bf_get(lpfc_trailer_type, acqe_fc) !=
5367 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005368 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05005369 "2895 Non FC link Event detected.(%d)\n",
5370 bf_get(lpfc_trailer_type, acqe_fc));
5371 return;
5372 }
James Smart1dc5ec22018-10-23 13:41:11 -07005373
5374 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5375 LPFC_FC_LA_TYPE_TRUNKING_EVENT) {
5376 lpfc_update_trunk_link_status(phba, acqe_fc);
5377 return;
5378 }
5379
James Smart70f3c072010-12-15 17:57:33 -05005380 /* Keep the link status for extra SLI4 state machine reference */
5381 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04005382 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5383 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05005384 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
5385 phba->sli4_hba.link_state.topology =
5386 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
5387 phba->sli4_hba.link_state.status =
5388 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
5389 phba->sli4_hba.link_state.type =
5390 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
5391 phba->sli4_hba.link_state.number =
5392 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
5393 phba->sli4_hba.link_state.fault =
5394 bf_get(lpfc_acqe_link_fault, acqe_fc);
James Smartb8e6f132019-05-21 17:49:04 -07005395
5396 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5397 LPFC_FC_LA_TYPE_LINK_DOWN)
5398 phba->sli4_hba.link_state.logical_speed = 0;
5399 else if (!phba->sli4_hba.conf_trunk)
5400 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005401 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smartb8e6f132019-05-21 17:49:04 -07005402
James Smart70f3c072010-12-15 17:57:33 -05005403 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5404 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
5405 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
5406 "%dMbps Fault:%d\n",
5407 phba->sli4_hba.link_state.speed,
5408 phba->sli4_hba.link_state.topology,
5409 phba->sli4_hba.link_state.status,
5410 phba->sli4_hba.link_state.type,
5411 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04005412 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05005413 phba->sli4_hba.link_state.fault);
5414 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5415 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005416 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05005417 "2897 The mboxq allocation failed\n");
5418 return;
5419 }
5420 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5421 if (!mp) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005422 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05005423 "2898 The lpfc_dmabuf allocation failed\n");
5424 goto out_free_pmb;
5425 }
5426 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
5427 if (!mp->virt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005428 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05005429 "2899 The mbuf allocation failed\n");
5430 goto out_free_dmabuf;
5431 }
5432
5433 /* Cleanup any outstanding ELS commands */
5434 lpfc_els_flush_all_cmd(phba);
5435
5436 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08005437 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05005438
5439 /* Update link event statistics */
5440 phba->sli.slistat.link_event++;
5441
5442 /* Create lpfc_handle_latt mailbox command from link ACQE */
5443 lpfc_read_topology(phba, pmb, mp);
5444 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
5445 pmb->vport = phba->pport;
5446
James Smart7bdedb32016-07-06 12:36:00 -07005447 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
James Smartae9e28f2017-05-15 15:20:51 -07005448 phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
5449
5450 switch (phba->sli4_hba.link_state.status) {
5451 case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
5452 phba->link_flag |= LS_MDS_LINK_DOWN;
5453 break;
5454 case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
5455 phba->link_flag |= LS_MDS_LOOPBACK;
5456 break;
5457 default:
5458 break;
5459 }
5460
James Smart23288b72018-05-04 20:37:53 -07005461 /* Initialize completion status */
James Smart7bdedb32016-07-06 12:36:00 -07005462 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07005463 mb->mbxStatus = MBX_SUCCESS;
5464
5465 /* Parse port fault information field */
5466 lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
James Smart7bdedb32016-07-06 12:36:00 -07005467
5468 /* Parse and translate link attention fields */
5469 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
5470 la->eventTag = acqe_fc->event_tag;
James Smart7bdedb32016-07-06 12:36:00 -07005471
James Smartaeb3c812017-04-21 16:05:02 -07005472 if (phba->sli4_hba.link_state.status ==
5473 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
5474 bf_set(lpfc_mbx_read_top_att_type, la,
5475 LPFC_FC_LA_TYPE_UNEXP_WWPN);
5476 } else {
5477 bf_set(lpfc_mbx_read_top_att_type, la,
5478 LPFC_FC_LA_TYPE_LINK_DOWN);
5479 }
James Smart7bdedb32016-07-06 12:36:00 -07005480 /* Invoke the mailbox command callback function */
5481 lpfc_mbx_cmpl_read_topology(phba, pmb);
5482
5483 return;
5484 }
5485
James Smart70f3c072010-12-15 17:57:33 -05005486 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5487 if (rc == MBX_NOT_FINISHED)
5488 goto out_free_dmabuf;
5489 return;
5490
5491out_free_dmabuf:
5492 kfree(mp);
5493out_free_pmb:
5494 mempool_free(pmb, phba->mbox_mem_pool);
5495}
5496
5497/**
5498 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
5499 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01005500 * @acqe_sli: pointer to the async SLI completion queue entry.
James Smart70f3c072010-12-15 17:57:33 -05005501 *
5502 * This routine is to handle the SLI4 asynchronous SLI events.
5503 **/
5504static void
5505lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
5506{
James Smart4b8bae02012-06-12 13:55:07 -04005507 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04005508 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04005509 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04005510 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05005511 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04005512 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04005513 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04005514 struct Scsi_Host *shost;
James Smartcd713482018-10-23 13:41:01 -07005515 struct lpfc_vport **vports;
5516 int rc, i;
James Smart4b8bae02012-06-12 13:55:07 -04005517
James Smart946727d2015-04-07 15:07:09 -04005518 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
5519
James Smart448193b2015-12-16 18:12:05 -05005520 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd11ed162019-09-21 20:59:03 -07005521 "2901 Async SLI event - Type:%d, Event Data: x%08x "
5522 "x%08x x%08x x%08x\n", evt_type,
James Smart448193b2015-12-16 18:12:05 -05005523 acqe_sli->event_data1, acqe_sli->event_data2,
James Smartd11ed162019-09-21 20:59:03 -07005524 acqe_sli->reserved, acqe_sli->trailer);
James Smart4b8bae02012-06-12 13:55:07 -04005525
5526 port_name = phba->Port[0];
5527 if (port_name == 0x00)
5528 port_name = '?'; /* get port name is empty */
5529
James Smart946727d2015-04-07 15:07:09 -04005530 switch (evt_type) {
5531 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
5532 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5533 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
5534 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5535
5536 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5537 "3190 Over Temperature:%d Celsius- Port Name %c\n",
5538 acqe_sli->event_data1, port_name);
5539
James Smart310429e2016-07-06 12:35:54 -07005540 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04005541 shost = lpfc_shost_from_vport(phba->pport);
5542 fc_host_post_vendor_event(shost, fc_get_event_number(),
5543 sizeof(temp_event_data),
5544 (char *)&temp_event_data,
5545 SCSI_NL_VID_TYPE_PCI
5546 | PCI_VENDOR_ID_EMULEX);
5547 break;
5548 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
5549 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5550 temp_event_data.event_code = LPFC_NORMAL_TEMP;
5551 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5552
5553 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5554 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
5555 acqe_sli->event_data1, port_name);
5556
5557 shost = lpfc_shost_from_vport(phba->pport);
5558 fc_host_post_vendor_event(shost, fc_get_event_number(),
5559 sizeof(temp_event_data),
5560 (char *)&temp_event_data,
5561 SCSI_NL_VID_TYPE_PCI
5562 | PCI_VENDOR_ID_EMULEX);
5563 break;
5564 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
5565 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04005566 &acqe_sli->event_data1;
5567
James Smart946727d2015-04-07 15:07:09 -04005568 /* fetch the status for this port */
5569 switch (phba->sli4_hba.lnk_info.lnk_no) {
5570 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05005571 status = bf_get(lpfc_sli_misconfigured_port0_state,
5572 &misconfigured->theEvent);
5573 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04005574 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005575 break;
5576 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05005577 status = bf_get(lpfc_sli_misconfigured_port1_state,
5578 &misconfigured->theEvent);
5579 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04005580 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005581 break;
5582 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05005583 status = bf_get(lpfc_sli_misconfigured_port2_state,
5584 &misconfigured->theEvent);
5585 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04005586 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005587 break;
5588 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05005589 status = bf_get(lpfc_sli_misconfigured_port3_state,
5590 &misconfigured->theEvent);
5591 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04005592 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005593 break;
5594 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07005595 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart448193b2015-12-16 18:12:05 -05005596 "3296 "
5597 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
5598 "event: Invalid link %d",
5599 phba->sli4_hba.lnk_info.lnk_no);
5600 return;
James Smart946727d2015-04-07 15:07:09 -04005601 }
James Smart4b8bae02012-06-12 13:55:07 -04005602
James Smart448193b2015-12-16 18:12:05 -05005603 /* Skip if optic state unchanged */
5604 if (phba->sli4_hba.lnk_info.optic_state == status)
5605 return;
5606
James Smart946727d2015-04-07 15:07:09 -04005607 switch (status) {
5608 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05005609 sprintf(message, "Physical Link is functional");
5610 break;
James Smart946727d2015-04-07 15:07:09 -04005611 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
5612 sprintf(message, "Optics faulted/incorrectly "
5613 "installed/not installed - Reseat optics, "
5614 "if issue not resolved, replace.");
5615 break;
5616 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
5617 sprintf(message,
5618 "Optics of two types installed - Remove one "
5619 "optic or install matching pair of optics.");
5620 break;
5621 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
5622 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04005623 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04005624 break;
James Smart448193b2015-12-16 18:12:05 -05005625 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
5626 sprintf(message, "Unqualified optics - Replace with "
5627 "Avago optics for Warranty and Technical "
5628 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005629 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005630 break;
5631 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
5632 sprintf(message, "Uncertified optics - Replace with "
5633 "Avago-certified optics to enable link "
5634 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005635 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005636 break;
James Smart946727d2015-04-07 15:07:09 -04005637 default:
5638 /* firmware is reporting a status we don't know about */
5639 sprintf(message, "Unknown event status x%02x", status);
5640 break;
5641 }
James Smartcd713482018-10-23 13:41:01 -07005642
5643 /* Issue READ_CONFIG mbox command to refresh supported speeds */
5644 rc = lpfc_sli4_read_config(phba);
James Smart3952e912018-10-23 13:41:02 -07005645 if (rc) {
James Smartcd713482018-10-23 13:41:01 -07005646 phba->lmt = 0;
Dick Kennedy372c1872020-06-30 14:50:00 -07005647 lpfc_printf_log(phba, KERN_ERR,
5648 LOG_TRACE_EVENT,
James Smartcd713482018-10-23 13:41:01 -07005649 "3194 Unable to retrieve supported "
James Smart3952e912018-10-23 13:41:02 -07005650 "speeds, rc = 0x%x\n", rc);
James Smartcd713482018-10-23 13:41:01 -07005651 }
5652 vports = lpfc_create_vport_work_array(phba);
5653 if (vports != NULL) {
5654 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5655 i++) {
5656 shost = lpfc_shost_from_vport(vports[i]);
5657 lpfc_host_supported_speeds_set(shost);
5658 }
5659 }
5660 lpfc_destroy_vport_work_array(phba, vports);
5661
James Smart448193b2015-12-16 18:12:05 -05005662 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04005663 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05005664 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04005665 break;
5666 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
5667 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5668 "3192 Remote DPort Test Initiated - "
5669 "Event Data1:x%08x Event Data2: x%08x\n",
5670 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04005671 break;
James Smarte7d85952019-10-18 14:18:29 -07005672 case LPFC_SLI_EVENT_TYPE_MISCONF_FAWWN:
5673 /* Misconfigured WWN. Reports that the SLI Port is configured
5674 * to use FA-WWN, but the attached device doesn’t support it.
5675 * No driver action is required.
5676 * Event Data1 - N.A, Event Data2 - N.A
5677 */
5678 lpfc_log_msg(phba, KERN_WARNING, LOG_SLI,
5679 "2699 Misconfigured FA-WWN - Attached device does "
5680 "not support FA-WWN\n");
5681 break;
James Smartd11ed162019-09-21 20:59:03 -07005682 case LPFC_SLI_EVENT_TYPE_EEPROM_FAILURE:
5683 /* EEPROM failure. No driver action is required */
5684 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5685 "2518 EEPROM failure - "
5686 "Event Data1: x%08x Event Data2: x%08x\n",
5687 acqe_sli->event_data1, acqe_sli->event_data2);
5688 break;
James Smart4b8bae02012-06-12 13:55:07 -04005689 default:
James Smart946727d2015-04-07 15:07:09 -04005690 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd11ed162019-09-21 20:59:03 -07005691 "3193 Unrecognized SLI event, type: 0x%x",
James Smart946727d2015-04-07 15:07:09 -04005692 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04005693 break;
5694 }
James Smart70f3c072010-12-15 17:57:33 -05005695}
5696
5697/**
James Smartfc2b9892010-02-26 14:15:29 -05005698 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
5699 * @vport: pointer to vport data structure.
5700 *
5701 * This routine is to perform Clear Virtual Link (CVL) on a vport in
5702 * response to a CVL event.
5703 *
5704 * Return the pointer to the ndlp with the vport if successful, otherwise
5705 * return NULL.
5706 **/
5707static struct lpfc_nodelist *
5708lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
5709{
5710 struct lpfc_nodelist *ndlp;
5711 struct Scsi_Host *shost;
5712 struct lpfc_hba *phba;
5713
5714 if (!vport)
5715 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05005716 phba = vport->phba;
5717 if (!phba)
5718 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04005719 ndlp = lpfc_findnode_did(vport, Fabric_DID);
5720 if (!ndlp) {
5721 /* Cannot find existing Fabric ndlp, so allocate a new one */
James Smart9d3d3402017-04-21 16:05:00 -07005722 ndlp = lpfc_nlp_init(vport, Fabric_DID);
James Smart78730cf2010-04-06 15:06:30 -04005723 if (!ndlp)
5724 return 0;
James Smart78730cf2010-04-06 15:06:30 -04005725 /* Set the node type */
5726 ndlp->nlp_type |= NLP_FABRIC;
5727 /* Put ndlp onto node list */
5728 lpfc_enqueue_node(vport, ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005729 }
James Smart63e801c2010-11-20 23:14:19 -05005730 if ((phba->pport->port_state < LPFC_FLOGI) &&
5731 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005732 return NULL;
5733 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05005734 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
5735 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005736 return NULL;
5737 shost = lpfc_shost_from_vport(vport);
5738 if (!shost)
5739 return NULL;
5740 lpfc_linkdown_port(vport);
5741 lpfc_cleanup_pending_mbox(vport);
5742 spin_lock_irq(shost->host_lock);
5743 vport->fc_flag |= FC_VPORT_CVL_RCVD;
5744 spin_unlock_irq(shost->host_lock);
5745
5746 return ndlp;
5747}
5748
5749/**
5750 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
Lee Jonesfe614ac2020-07-23 13:24:22 +01005751 * @phba: pointer to lpfc hba data structure.
James Smartfc2b9892010-02-26 14:15:29 -05005752 *
5753 * This routine is to perform Clear Virtual Link (CVL) on all vports in
5754 * response to a FCF dead event.
5755 **/
5756static void
5757lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
5758{
5759 struct lpfc_vport **vports;
5760 int i;
5761
5762 vports = lpfc_create_vport_work_array(phba);
5763 if (vports)
5764 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
5765 lpfc_sli4_perform_vport_cvl(vports[i]);
5766 lpfc_destroy_vport_work_array(phba, vports);
5767}
5768
5769/**
James Smart76a95d72010-11-20 23:11:48 -05005770 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04005771 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01005772 * @acqe_fip: pointer to the async fcoe completion queue entry.
James Smartda0436e2009-05-22 14:51:39 -04005773 *
5774 * This routine is to handle the SLI4 asynchronous fcoe event.
5775 **/
5776static void
James Smart76a95d72010-11-20 23:11:48 -05005777lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05005778 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04005779{
James Smart70f3c072010-12-15 17:57:33 -05005780 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005781 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04005782 struct lpfc_vport *vport;
5783 struct lpfc_nodelist *ndlp;
James Smart695a8142010-01-26 23:08:03 -05005784 int active_vlink_present;
5785 struct lpfc_vport **vports;
5786 int i;
James Smartda0436e2009-05-22 14:51:39 -04005787
James Smart70f3c072010-12-15 17:57:33 -05005788 phba->fc_eventTag = acqe_fip->event_tag;
5789 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005790 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05005791 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
5792 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
5793 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
Dick Kennedy372c1872020-06-30 14:50:00 -07005794 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta93ff372010-10-22 11:06:08 -04005795 "2546 New FCF event, evt_tag:x%x, "
5796 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005797 acqe_fip->event_tag,
5798 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04005799 else
5800 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
5801 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005802 "2788 FCF param modified event, "
5803 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005804 acqe_fip->event_tag,
5805 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04005806 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005807 /*
5808 * During period of FCF discovery, read the FCF
5809 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04005810 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05005811 */
5812 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5813 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005814 "2779 Read FCF (x%x) for updating "
5815 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05005816 acqe_fip->index);
5817 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005818 }
James Smart38b92ef2010-08-04 16:11:39 -04005819
5820 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04005821 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04005822 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04005823 spin_unlock_irq(&phba->hbalock);
5824 break;
5825 }
5826 /* If fast FCF failover rescan event is pending, do nothing */
James Smart036cad12018-10-23 13:41:06 -07005827 if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
James Smart38b92ef2010-08-04 16:11:39 -04005828 spin_unlock_irq(&phba->hbalock);
5829 break;
5830 }
5831
James Smartc2b97122013-05-31 17:05:36 -04005832 /* If the FCF has been in discovered state, do nothing. */
5833 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04005834 spin_unlock_irq(&phba->hbalock);
5835 break;
5836 }
5837 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005838
James Smart0c9ab6f2010-02-26 14:15:57 -05005839 /* Otherwise, scan the entire FCF table and re-discover SAN */
5840 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005841 "2770 Start FCF table scan per async FCF "
5842 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005843 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005844 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5845 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04005846 if (rc)
Dick Kennedy372c1872020-06-30 14:50:00 -07005847 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05005848 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04005849 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005850 break;
5851
James Smart70f3c072010-12-15 17:57:33 -05005852 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
Dick Kennedy372c1872020-06-30 14:50:00 -07005853 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5854 "2548 FCF Table full count 0x%x tag 0x%x\n",
5855 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5856 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005857 break;
5858
James Smart70f3c072010-12-15 17:57:33 -05005859 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05005860 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
Dick Kennedy372c1872020-06-30 14:50:00 -07005861 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5862 "2549 FCF (x%x) disconnected from network, "
5863 "tag:x%x\n", acqe_fip->index,
5864 acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04005865 /*
5866 * If we are in the middle of FCF failover process, clear
5867 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04005868 */
James Smartfc2b9892010-02-26 14:15:29 -05005869 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07005870 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5871 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05005872 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005873 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05005874 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005875 break;
5876 }
James Smart38b92ef2010-08-04 16:11:39 -04005877 spin_unlock_irq(&phba->hbalock);
5878
5879 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05005880 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04005881 break;
5882
5883 /*
5884 * Otherwise, request the port to rediscover the entire FCF
5885 * table for a fast recovery from case that the current FCF
5886 * is no longer valid as we are not in the middle of FCF
5887 * failover process already.
5888 */
James Smartc2b97122013-05-31 17:05:36 -04005889 spin_lock_irq(&phba->hbalock);
5890 /* Mark the fast failover process in progress */
5891 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5892 spin_unlock_irq(&phba->hbalock);
5893
5894 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5895 "2771 Start FCF fast failover process due to "
5896 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5897 "\n", acqe_fip->event_tag, acqe_fip->index);
5898 rc = lpfc_sli4_redisc_fcf_table(phba);
5899 if (rc) {
5900 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
Dick Kennedy372c1872020-06-30 14:50:00 -07005901 LOG_TRACE_EVENT,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005902 "2772 Issue FCF rediscover mailbox "
James Smartc2b97122013-05-31 17:05:36 -04005903 "command failed, fail through to FCF "
5904 "dead event\n");
5905 spin_lock_irq(&phba->hbalock);
5906 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5907 spin_unlock_irq(&phba->hbalock);
5908 /*
5909 * Last resort will fail over by treating this
5910 * as a link down to FCF registration.
5911 */
5912 lpfc_sli4_fcf_dead_failthrough(phba);
5913 } else {
5914 /* Reset FCF roundrobin bmask for new discovery */
5915 lpfc_sli4_clear_fcf_rr_bmask(phba);
5916 /*
5917 * Handling fast FCF failover to a DEAD FCF event is
5918 * considered equalivant to receiving CVL to all vports.
5919 */
5920 lpfc_sli4_perform_all_vport_cvl(phba);
5921 }
James Smartda0436e2009-05-22 14:51:39 -04005922 break;
James Smart70f3c072010-12-15 17:57:33 -05005923 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05005924 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
Dick Kennedy372c1872020-06-30 14:50:00 -07005925 lpfc_printf_log(phba, KERN_ERR,
5926 LOG_TRACE_EVENT,
James Smart6669f9b2009-10-02 15:16:45 -04005927 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05005928 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04005929
James Smart6669f9b2009-10-02 15:16:45 -04005930 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04005931 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005932 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005933 if (!ndlp)
5934 break;
James Smart695a8142010-01-26 23:08:03 -05005935 active_vlink_present = 0;
5936
5937 vports = lpfc_create_vport_work_array(phba);
5938 if (vports) {
5939 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5940 i++) {
5941 if ((!(vports[i]->fc_flag &
5942 FC_VPORT_CVL_RCVD)) &&
5943 (vports[i]->port_state > LPFC_FDISC)) {
5944 active_vlink_present = 1;
5945 break;
5946 }
5947 }
5948 lpfc_destroy_vport_work_array(phba, vports);
5949 }
5950
James Smartcc823552015-05-21 13:55:26 -04005951 /*
5952 * Don't re-instantiate if vport is marked for deletion.
5953 * If we are here first then vport_delete is going to wait
5954 * for discovery to complete.
5955 */
5956 if (!(vport->load_flag & FC_UNLOADING) &&
5957 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05005958 /*
5959 * If there are other active VLinks present,
5960 * re-instantiate the Vlink using FDISC.
5961 */
James Smart256ec0d2013-04-17 20:14:58 -04005962 mod_timer(&ndlp->nlp_delayfunc,
5963 jiffies + msecs_to_jiffies(1000));
James Smartc6adba12020-11-15 11:26:34 -08005964 spin_lock_irq(&ndlp->lock);
James Smart6669f9b2009-10-02 15:16:45 -04005965 ndlp->nlp_flag |= NLP_DELAY_TMO;
James Smartc6adba12020-11-15 11:26:34 -08005966 spin_unlock_irq(&ndlp->lock);
James Smart695a8142010-01-26 23:08:03 -05005967 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5968 vport->port_state = LPFC_FDISC;
5969 } else {
James Smartecfd03c2010-02-12 14:41:27 -05005970 /*
5971 * Otherwise, we request port to rediscover
5972 * the entire FCF table for a fast recovery
5973 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05005974 * is no longer valid if we are not already
5975 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05005976 */
James Smartfc2b9892010-02-26 14:15:29 -05005977 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005978 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05005979 spin_unlock_irq(&phba->hbalock);
5980 break;
5981 }
5982 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05005983 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005984 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005985 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5986 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005987 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05005988 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05005989 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05005990 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005991 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
Dick Kennedy372c1872020-06-30 14:50:00 -07005992 LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05005993 "2774 Issue FCF rediscover "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005994 "mailbox command failed, "
James Smart0c9ab6f2010-02-26 14:15:57 -05005995 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05005996 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005997 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005998 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05005999 /*
6000 * Last resort will be re-try on the
6001 * the current registered FCF entry.
6002 */
6003 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006004 } else
6005 /*
6006 * Reset FCF roundrobin bmask for new
6007 * discovery.
6008 */
James Smart7d791df2011-07-22 18:37:52 -04006009 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04006010 }
6011 break;
James Smartda0436e2009-05-22 14:51:39 -04006012 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07006013 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6014 "0288 Unknown FCoE event type 0x%x event tag "
6015 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04006016 break;
6017 }
6018}
6019
6020/**
6021 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
6022 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01006023 * @acqe_dcbx: pointer to the async dcbx completion queue entry.
James Smartda0436e2009-05-22 14:51:39 -04006024 *
6025 * This routine is to handle the SLI4 asynchronous dcbx event.
6026 **/
6027static void
6028lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
6029 struct lpfc_acqe_dcbx *acqe_dcbx)
6030{
James Smart4d9ab992009-10-02 15:16:39 -04006031 phba->fc_eventTag = acqe_dcbx->event_tag;
Dick Kennedy372c1872020-06-30 14:50:00 -07006032 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04006033 "0290 The SLI4 DCBX asynchronous event is not "
6034 "handled yet\n");
6035}
6036
6037/**
James Smartb19a0612010-04-06 14:48:51 -04006038 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
6039 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01006040 * @acqe_grp5: pointer to the async grp5 completion queue entry.
James Smartb19a0612010-04-06 14:48:51 -04006041 *
6042 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
6043 * is an asynchronous notified of a logical link speed change. The Port
6044 * reports the logical link speed in units of 10Mbps.
6045 **/
6046static void
6047lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
6048 struct lpfc_acqe_grp5 *acqe_grp5)
6049{
6050 uint16_t prev_ll_spd;
6051
6052 phba->fc_eventTag = acqe_grp5->event_tag;
6053 phba->fcoe_eventtag = acqe_grp5->event_tag;
6054 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
6055 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04006056 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04006057 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6058 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04006059 "from %dMbps to %dMbps\n", prev_ll_spd,
6060 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04006061}
6062
6063/**
James Smartda0436e2009-05-22 14:51:39 -04006064 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
6065 * @phba: pointer to lpfc hba data structure.
6066 *
6067 * This routine is invoked by the worker thread to process all the pending
6068 * SLI4 asynchronous events.
6069 **/
6070void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
6071{
6072 struct lpfc_cq_event *cq_event;
James Smarte7dab162020-10-20 13:27:12 -07006073 unsigned long iflags;
James Smartda0436e2009-05-22 14:51:39 -04006074
6075 /* First, declare the async event has been handled */
James Smarte7dab162020-10-20 13:27:12 -07006076 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartda0436e2009-05-22 14:51:39 -04006077 phba->hba_flag &= ~ASYNC_EVENT;
James Smarte7dab162020-10-20 13:27:12 -07006078 spin_unlock_irqrestore(&phba->hbalock, iflags);
6079
James Smartda0436e2009-05-22 14:51:39 -04006080 /* Now, handle all the async events */
James Smarte7dab162020-10-20 13:27:12 -07006081 spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
James Smartda0436e2009-05-22 14:51:39 -04006082 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
James Smartda0436e2009-05-22 14:51:39 -04006083 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
6084 cq_event, struct lpfc_cq_event, list);
James Smarte7dab162020-10-20 13:27:12 -07006085 spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock,
6086 iflags);
6087
James Smartda0436e2009-05-22 14:51:39 -04006088 /* Process the asynchronous event */
6089 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
6090 case LPFC_TRAILER_CODE_LINK:
6091 lpfc_sli4_async_link_evt(phba,
6092 &cq_event->cqe.acqe_link);
6093 break;
6094 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05006095 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04006096 break;
6097 case LPFC_TRAILER_CODE_DCBX:
6098 lpfc_sli4_async_dcbx_evt(phba,
6099 &cq_event->cqe.acqe_dcbx);
6100 break;
James Smartb19a0612010-04-06 14:48:51 -04006101 case LPFC_TRAILER_CODE_GRP5:
6102 lpfc_sli4_async_grp5_evt(phba,
6103 &cq_event->cqe.acqe_grp5);
6104 break;
James Smart70f3c072010-12-15 17:57:33 -05006105 case LPFC_TRAILER_CODE_FC:
6106 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
6107 break;
6108 case LPFC_TRAILER_CODE_SLI:
6109 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
6110 break;
James Smartda0436e2009-05-22 14:51:39 -04006111 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07006112 lpfc_printf_log(phba, KERN_ERR,
6113 LOG_TRACE_EVENT,
Colin Ian King291c2542019-12-18 08:43:01 +00006114 "1804 Invalid asynchronous event code: "
James Smartda0436e2009-05-22 14:51:39 -04006115 "x%x\n", bf_get(lpfc_trailer_code,
6116 &cq_event->cqe.mcqe_cmpl));
6117 break;
6118 }
James Smarte7dab162020-10-20 13:27:12 -07006119
James Smartda0436e2009-05-22 14:51:39 -04006120 /* Free the completion event processed to the free pool */
6121 lpfc_sli4_cq_event_release(phba, cq_event);
James Smarte7dab162020-10-20 13:27:12 -07006122 spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
James Smartda0436e2009-05-22 14:51:39 -04006123 }
James Smarte7dab162020-10-20 13:27:12 -07006124 spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
James Smartda0436e2009-05-22 14:51:39 -04006125}
6126
6127/**
James Smartecfd03c2010-02-12 14:41:27 -05006128 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
6129 * @phba: pointer to lpfc hba data structure.
6130 *
6131 * This routine is invoked by the worker thread to process FCF table
6132 * rediscovery pending completion event.
6133 **/
6134void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
6135{
6136 int rc;
6137
6138 spin_lock_irq(&phba->hbalock);
6139 /* Clear FCF rediscovery timeout event */
6140 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
6141 /* Clear driver fast failover FCF record flag */
6142 phba->fcf.failover_rec.flag = 0;
6143 /* Set state for FCF fast failover */
6144 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
6145 spin_unlock_irq(&phba->hbalock);
6146
6147 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05006148 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04006149 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05006150 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05006151 if (rc)
Dick Kennedy372c1872020-06-30 14:50:00 -07006152 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05006153 "2747 Issue FCF scan read FCF mailbox "
6154 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05006155}
6156
6157/**
James Smartda0436e2009-05-22 14:51:39 -04006158 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
6159 * @phba: pointer to lpfc hba data structure.
6160 * @dev_grp: The HBA PCI-Device group number.
6161 *
6162 * This routine is invoked to set up the per HBA PCI-Device group function
6163 * API jump table entries.
6164 *
6165 * Return: 0 if success, otherwise -ENODEV
6166 **/
6167int
6168lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05006169{
6170 int rc;
6171
James Smartda0436e2009-05-22 14:51:39 -04006172 /* Set up lpfc PCI-device group */
6173 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05006174
James Smartda0436e2009-05-22 14:51:39 -04006175 /* The LPFC_PCI_DEV_OC uses SLI4 */
6176 if (dev_grp == LPFC_PCI_DEV_OC)
6177 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05006178
James Smartda0436e2009-05-22 14:51:39 -04006179 /* Set up device INIT API function jump table */
6180 rc = lpfc_init_api_table_setup(phba, dev_grp);
6181 if (rc)
6182 return -ENODEV;
6183 /* Set up SCSI API function jump table */
6184 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
6185 if (rc)
6186 return -ENODEV;
6187 /* Set up SLI API function jump table */
6188 rc = lpfc_sli_api_table_setup(phba, dev_grp);
6189 if (rc)
6190 return -ENODEV;
6191 /* Set up MBOX API function jump table */
6192 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
6193 if (rc)
6194 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05006195
James Smartda0436e2009-05-22 14:51:39 -04006196 return 0;
James Smart5b75da22008-12-04 22:39:35 -05006197}
6198
6199/**
James Smart3621a712009-04-06 18:47:14 -04006200 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05006201 * @phba: pointer to lpfc hba data structure.
6202 * @intr_mode: active interrupt mode adopted.
6203 *
6204 * This routine it invoked to log the currently used active interrupt mode
6205 * to the device.
James Smart3772a992009-05-22 14:50:54 -04006206 **/
6207static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05006208{
6209 switch (intr_mode) {
6210 case 0:
6211 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6212 "0470 Enable INTx interrupt mode.\n");
6213 break;
6214 case 1:
6215 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6216 "0481 Enabled MSI interrupt mode.\n");
6217 break;
6218 case 2:
6219 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6220 "0480 Enabled MSI-X interrupt mode.\n");
6221 break;
6222 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07006223 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5b75da22008-12-04 22:39:35 -05006224 "0482 Illegal interrupt mode.\n");
6225 break;
6226 }
6227 return;
6228}
6229
James Smart5b75da22008-12-04 22:39:35 -05006230/**
James Smart3772a992009-05-22 14:50:54 -04006231 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05006232 * @phba: pointer to lpfc hba data structure.
6233 *
James Smart3772a992009-05-22 14:50:54 -04006234 * This routine is invoked to enable the PCI device that is common to all
6235 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05006236 *
6237 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006238 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006239 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05006240 **/
James Smart3772a992009-05-22 14:50:54 -04006241static int
6242lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05006243{
James Smart3772a992009-05-22 14:50:54 -04006244 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05006245
James Smart3772a992009-05-22 14:50:54 -04006246 /* Obtain PCI device reference */
6247 if (!phba->pcidev)
6248 goto out_error;
6249 else
6250 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006251 /* Enable PCI device */
6252 if (pci_enable_device_mem(pdev))
6253 goto out_error;
6254 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006255 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04006256 goto out_disable_device;
6257 /* Set up device as PCI master and save state for EEH */
6258 pci_set_master(pdev);
6259 pci_try_set_mwi(pdev);
6260 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006261
James Smart05580562011-05-24 11:40:48 -04006262 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07006263 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04006264 pdev->needs_freset = 1;
6265
James Smart3772a992009-05-22 14:50:54 -04006266 return 0;
James Smart5b75da22008-12-04 22:39:35 -05006267
James Smart3772a992009-05-22 14:50:54 -04006268out_disable_device:
6269 pci_disable_device(pdev);
6270out_error:
Dick Kennedy372c1872020-06-30 14:50:00 -07006271 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006272 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04006273 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05006274}
6275
6276/**
James Smart3772a992009-05-22 14:50:54 -04006277 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05006278 * @phba: pointer to lpfc hba data structure.
6279 *
James Smart3772a992009-05-22 14:50:54 -04006280 * This routine is invoked to disable the PCI device that is common to all
6281 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05006282 **/
6283static void
James Smart3772a992009-05-22 14:50:54 -04006284lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05006285{
James Smart3772a992009-05-22 14:50:54 -04006286 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05006287
James Smart3772a992009-05-22 14:50:54 -04006288 /* Obtain PCI device reference */
6289 if (!phba->pcidev)
6290 return;
6291 else
6292 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006293 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006294 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04006295 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006296
6297 return;
6298}
6299
6300/**
James Smart3772a992009-05-22 14:50:54 -04006301 * lpfc_reset_hba - Reset a hba
6302 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04006303 *
James Smart3772a992009-05-22 14:50:54 -04006304 * This routine is invoked to reset a hba device. It brings the HBA
6305 * offline, performs a board restart, and then brings the board back
6306 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
6307 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04006308 **/
James Smart3772a992009-05-22 14:50:54 -04006309void
6310lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006311{
James Smart3772a992009-05-22 14:50:54 -04006312 /* If resets are disabled then set error state and return. */
6313 if (!phba->cfg_enable_hba_reset) {
6314 phba->link_state = LPFC_HBA_ERROR;
6315 return;
6316 }
James Smart9ec58ec2021-01-04 10:02:35 -08006317
6318 /* If not LPFC_SLI_ACTIVE, force all IO to be flushed */
6319 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE) {
James Smartee620212014-04-04 13:51:53 -04006320 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart9ec58ec2021-01-04 10:02:35 -08006321 } else {
James Smartee620212014-04-04 13:51:53 -04006322 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart9ec58ec2021-01-04 10:02:35 -08006323 lpfc_sli_flush_io_rings(phba);
6324 }
James Smart3772a992009-05-22 14:50:54 -04006325 lpfc_offline(phba);
6326 lpfc_sli_brdrestart(phba);
6327 lpfc_online(phba);
6328 lpfc_unblock_mgmt_io(phba);
6329}
dea31012005-04-17 16:05:31 -05006330
James Smart3772a992009-05-22 14:50:54 -04006331/**
James Smart0a96e972011-07-22 18:37:28 -04006332 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
6333 * @phba: pointer to lpfc hba data structure.
6334 *
6335 * This function enables the PCI SR-IOV virtual functions to a physical
6336 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6337 * enable the number of virtual functions to the physical function. As
6338 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6339 * API call does not considered as an error condition for most of the device.
6340 **/
6341uint16_t
6342lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
6343{
6344 struct pci_dev *pdev = phba->pcidev;
6345 uint16_t nr_virtfn;
6346 int pos;
6347
James Smart0a96e972011-07-22 18:37:28 -04006348 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
6349 if (pos == 0)
6350 return 0;
6351
6352 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
6353 return nr_virtfn;
6354}
6355
6356/**
James Smart912e3ac2011-05-24 11:42:11 -04006357 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
6358 * @phba: pointer to lpfc hba data structure.
6359 * @nr_vfn: number of virtual functions to be enabled.
6360 *
6361 * This function enables the PCI SR-IOV virtual functions to a physical
6362 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6363 * enable the number of virtual functions to the physical function. As
6364 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6365 * API call does not considered as an error condition for most of the device.
6366 **/
6367int
6368lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
6369{
6370 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04006371 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04006372 int rc;
6373
James Smart0a96e972011-07-22 18:37:28 -04006374 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
6375 if (nr_vfn > max_nr_vfn) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006376 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a96e972011-07-22 18:37:28 -04006377 "3057 Requested vfs (%d) greater than "
6378 "supported vfs (%d)", nr_vfn, max_nr_vfn);
6379 return -EINVAL;
6380 }
6381
James Smart912e3ac2011-05-24 11:42:11 -04006382 rc = pci_enable_sriov(pdev, nr_vfn);
6383 if (rc) {
6384 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6385 "2806 Failed to enable sriov on this device "
6386 "with vfn number nr_vf:%d, rc:%d\n",
6387 nr_vfn, rc);
6388 } else
6389 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6390 "2807 Successful enable sriov on this device "
6391 "with vfn number nr_vf:%d\n", nr_vfn);
6392 return rc;
6393}
6394
6395/**
James Smart895427b2017-02-12 13:52:30 -08006396 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04006397 * @phba: pointer to lpfc hba data structure.
6398 *
James Smart895427b2017-02-12 13:52:30 -08006399 * This routine is invoked to set up the driver internal resources before the
6400 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04006401 *
6402 * Return codes
James Smart895427b2017-02-12 13:52:30 -08006403 * 0 - successful
6404 * other values - error
James Smart3772a992009-05-22 14:50:54 -04006405 **/
6406static int
James Smart895427b2017-02-12 13:52:30 -08006407lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04006408{
James Smart895427b2017-02-12 13:52:30 -08006409 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006410
James Smart3772a992009-05-22 14:50:54 -04006411 /*
James Smart895427b2017-02-12 13:52:30 -08006412 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04006413 */
James Smart895427b2017-02-12 13:52:30 -08006414 atomic_set(&phba->fast_event_count, 0);
Dick Kennedy372c1872020-06-30 14:50:00 -07006415 atomic_set(&phba->dbg_log_idx, 0);
6416 atomic_set(&phba->dbg_log_cnt, 0);
6417 atomic_set(&phba->dbg_log_dmping, 0);
James Smart895427b2017-02-12 13:52:30 -08006418 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006419
James Smart523128e2018-09-10 10:30:46 -07006420 /* Initialize port_list spinlock */
6421 spin_lock_init(&phba->port_list_lock);
James Smart895427b2017-02-12 13:52:30 -08006422 INIT_LIST_HEAD(&phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07006423
James Smart895427b2017-02-12 13:52:30 -08006424 INIT_LIST_HEAD(&phba->work_list);
6425 init_waitqueue_head(&phba->wait_4_mlo_m_q);
6426
6427 /* Initialize the wait queue head for the kernel thread */
6428 init_waitqueue_head(&phba->work_waitq);
6429
6430 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08006431 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08006432 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
6433 "SCSI" : " "),
6434 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08006435 "NVME" : " "),
6436 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08006437
James Smart0794d602019-01-28 11:14:19 -08006438 /* Initialize the IO buffer list used by driver for SLI3 SCSI */
6439 spin_lock_init(&phba->scsi_buf_list_get_lock);
6440 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
6441 spin_lock_init(&phba->scsi_buf_list_put_lock);
6442 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart895427b2017-02-12 13:52:30 -08006443
6444 /* Initialize the fabric iocb list */
6445 INIT_LIST_HEAD(&phba->fabric_iocb_list);
6446
6447 /* Initialize list to save ELS buffers */
6448 INIT_LIST_HEAD(&phba->elsbuf);
6449
6450 /* Initialize FCF connection rec list */
6451 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
6452
6453 /* Initialize OAS configuration list */
6454 spin_lock_init(&phba->devicelock);
6455 INIT_LIST_HEAD(&phba->luns);
6456
James Smart3772a992009-05-22 14:50:54 -04006457 /* MBOX heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006458 timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006459 /* Fabric block timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006460 timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006461 /* EA polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006462 timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
James Smart895427b2017-02-12 13:52:30 -08006463 /* Heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006464 timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
James Smart895427b2017-02-12 13:52:30 -08006465
James Smart32517fc2019-01-28 11:14:33 -08006466 INIT_DELAYED_WORK(&phba->eq_delay_work, lpfc_hb_eq_delay_work);
6467
Dick Kennedy317aeb82020-06-30 14:49:59 -07006468 INIT_DELAYED_WORK(&phba->idle_stat_delay_work,
6469 lpfc_idle_stat_delay_work);
6470
James Smart895427b2017-02-12 13:52:30 -08006471 return 0;
6472}
6473
6474/**
6475 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
6476 * @phba: pointer to lpfc hba data structure.
6477 *
6478 * This routine is invoked to set up the driver internal resources specific to
6479 * support the SLI-3 HBA device it attached to.
6480 *
6481 * Return codes
6482 * 0 - successful
6483 * other values - error
6484 **/
6485static int
6486lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
6487{
James Smart0794d602019-01-28 11:14:19 -08006488 int rc, entry_sz;
James Smart895427b2017-02-12 13:52:30 -08006489
6490 /*
6491 * Initialize timers used by driver
6492 */
6493
6494 /* FCP polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006495 timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006496
6497 /* Host attention work mask setup */
6498 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
6499 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
6500
6501 /* Get all the module params for configuring this host */
6502 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08006503 /* Set up phase-1 common device driver resources */
6504
6505 rc = lpfc_setup_driver_resource_phase1(phba);
6506 if (rc)
6507 return -ENODEV;
6508
James Smart49198b32010-04-06 15:04:33 -04006509 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
6510 phba->menlo_flag |= HBA_MENLO_SUPPORT;
6511 /* check for menlo minimum sg count */
6512 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
6513 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
6514 }
6515
James Smart895427b2017-02-12 13:52:30 -08006516 if (!phba->sli.sli3_ring)
Kees Cook6396bb22018-06-12 14:03:40 -07006517 phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
6518 sizeof(struct lpfc_sli_ring),
6519 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006520 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04006521 return -ENOMEM;
6522
James Smart3772a992009-05-22 14:50:54 -04006523 /*
James Smart96f70772013-04-17 20:16:15 -04006524 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04006525 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04006526 */
James Smart3772a992009-05-22 14:50:54 -04006527
James Smart0794d602019-01-28 11:14:19 -08006528 if (phba->sli_rev == LPFC_SLI_REV4)
6529 entry_sz = sizeof(struct sli4_sge);
6530 else
6531 entry_sz = sizeof(struct ulp_bde64);
6532
James Smart96f70772013-04-17 20:16:15 -04006533 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
6534 if (phba->cfg_enable_bg) {
6535 /*
6536 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
6537 * the FCP rsp, and a BDE for each. Sice we have no control
6538 * over how many protection data segments the SCSI Layer
6539 * will hand us (ie: there could be one for every block
6540 * in the IO), we just allocate enough BDEs to accomidate
6541 * our max amount and we need to limit lpfc_sg_seg_cnt to
6542 * minimize the risk of running out.
6543 */
6544 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6545 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006546 (LPFC_MAX_SG_SEG_CNT * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006547
6548 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
6549 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
6550
6551 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
6552 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
6553 } else {
6554 /*
6555 * The scsi_buf for a regular I/O will hold the FCP cmnd,
6556 * the FCP rsp, a BDE for each, and a BDE for up to
6557 * cfg_sg_seg_cnt data segments.
6558 */
6559 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6560 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006561 ((phba->cfg_sg_seg_cnt + 2) * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006562
6563 /* Total BDEs in BPL for scsi_sg_list */
6564 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
6565 }
6566
6567 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
James Smartc90b4482020-03-22 11:12:56 -07006568 "9088 INIT sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
James Smart96f70772013-04-17 20:16:15 -04006569 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
6570 phba->cfg_total_seg_cnt);
6571
James Smart3772a992009-05-22 14:50:54 -04006572 phba->max_vpi = LPFC_MAX_VPI;
6573 /* This will be set to correct value after config_port mbox */
6574 phba->max_vports = 0;
6575
6576 /*
6577 * Initialize the SLI Layer to run with lpfc HBAs.
6578 */
6579 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08006580 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04006581
6582 /* Allocate device driver memory */
6583 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
6584 return -ENOMEM;
6585
James Smartd79c9e92019-08-14 16:57:09 -07006586 phba->lpfc_sg_dma_buf_pool =
6587 dma_pool_create("lpfc_sg_dma_buf_pool",
6588 &phba->pcidev->dev, phba->cfg_sg_dma_buf_size,
6589 BPL_ALIGN_SZ, 0);
6590
6591 if (!phba->lpfc_sg_dma_buf_pool)
6592 goto fail_free_mem;
6593
6594 phba->lpfc_cmd_rsp_buf_pool =
6595 dma_pool_create("lpfc_cmd_rsp_buf_pool",
6596 &phba->pcidev->dev,
6597 sizeof(struct fcp_cmnd) +
6598 sizeof(struct fcp_rsp),
6599 BPL_ALIGN_SZ, 0);
6600
6601 if (!phba->lpfc_cmd_rsp_buf_pool)
6602 goto fail_free_dma_buf_pool;
6603
James Smart912e3ac2011-05-24 11:42:11 -04006604 /*
6605 * Enable sr-iov virtual functions if supported and configured
6606 * through the module parameter.
6607 */
6608 if (phba->cfg_sriov_nr_virtfn > 0) {
6609 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6610 phba->cfg_sriov_nr_virtfn);
6611 if (rc) {
6612 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6613 "2808 Requested number of SR-IOV "
6614 "virtual functions (%d) is not "
6615 "supported\n",
6616 phba->cfg_sriov_nr_virtfn);
6617 phba->cfg_sriov_nr_virtfn = 0;
6618 }
6619 }
6620
James Smart3772a992009-05-22 14:50:54 -04006621 return 0;
James Smartd79c9e92019-08-14 16:57:09 -07006622
6623fail_free_dma_buf_pool:
6624 dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
6625 phba->lpfc_sg_dma_buf_pool = NULL;
6626fail_free_mem:
6627 lpfc_mem_free(phba);
6628 return -ENOMEM;
James Smart3772a992009-05-22 14:50:54 -04006629}
6630
6631/**
6632 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
6633 * @phba: pointer to lpfc hba data structure.
6634 *
6635 * This routine is invoked to unset the driver internal resources set up
6636 * specific for supporting the SLI-3 HBA device it attached to.
6637 **/
6638static void
6639lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
6640{
6641 /* Free device driver memory allocated */
6642 lpfc_mem_free_all(phba);
6643
6644 return;
6645}
6646
6647/**
James Smartda0436e2009-05-22 14:51:39 -04006648 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
6649 * @phba: pointer to lpfc hba data structure.
6650 *
6651 * This routine is invoked to set up the driver internal resources specific to
6652 * support the SLI-4 HBA device it attached to.
6653 *
6654 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006655 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006656 * other values - error
6657 **/
6658static int
6659lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
6660{
James Smart28baac72010-02-12 14:42:03 -05006661 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08006662 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08006663 int rc, i, max_buf_size;
James Smart09294d42013-04-17 20:16:05 -04006664 int longs;
James Smart81e6a632017-11-20 16:00:43 -08006665 int extra;
James Smartf358dd02017-02-12 13:52:34 -08006666 uint64_t wwn;
James Smartb92dc722018-05-24 21:09:01 -07006667 u32 if_type;
6668 u32 if_fam;
James Smartda0436e2009-05-22 14:51:39 -04006669
James Smart895427b2017-02-12 13:52:30 -08006670 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smarteede4972019-11-21 09:55:56 -08006671 phba->sli4_hba.num_possible_cpu = cpumask_last(cpu_possible_mask) + 1;
James Smart895427b2017-02-12 13:52:30 -08006672 phba->sli4_hba.curr_disp_cpu = 0;
6673
James Smart716d3bc2013-09-06 12:18:28 -04006674 /* Get all the module params for configuring this host */
6675 lpfc_get_cfgparam(phba);
6676
James Smart895427b2017-02-12 13:52:30 -08006677 /* Set up phase-1 common device driver resources */
6678 rc = lpfc_setup_driver_resource_phase1(phba);
6679 if (rc)
6680 return -ENODEV;
6681
James Smartda0436e2009-05-22 14:51:39 -04006682 /* Before proceed, wait for POST done and device ready */
6683 rc = lpfc_sli4_post_status_check(phba);
6684 if (rc)
6685 return -ENODEV;
6686
James Smart3cee98d2019-08-14 16:56:34 -07006687 /* Allocate all driver workqueues here */
6688
6689 /* The lpfc_wq workqueue for deferred irq use */
6690 phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
6691
James Smartda0436e2009-05-22 14:51:39 -04006692 /*
6693 * Initialize timers used by driver
6694 */
6695
Kees Cookf22eb4d2017-09-06 20:24:26 -07006696 timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
James Smartda0436e2009-05-22 14:51:39 -04006697
James Smartecfd03c2010-02-12 14:41:27 -05006698 /* FCF rediscover timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006699 timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
James Smartecfd03c2010-02-12 14:41:27 -05006700
James Smartda0436e2009-05-22 14:51:39 -04006701 /*
James Smart7ad20aa2011-05-24 11:44:28 -04006702 * Control structure for handling external multi-buffer mailbox
6703 * command pass-through.
6704 */
6705 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
6706 sizeof(struct lpfc_mbox_ext_buf_ctx));
6707 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
6708
James Smartda0436e2009-05-22 14:51:39 -04006709 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04006710
James Smartda0436e2009-05-22 14:51:39 -04006711 /* This will be set to correct value after the read_config mbox */
6712 phba->max_vports = 0;
6713
6714 /* Program the default value of vlan_id and fc_map */
6715 phba->valid_vlan = 0;
6716 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
6717 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
6718 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
6719
6720 /*
James Smart2a76a282012-08-03 12:35:54 -04006721 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08006722 * we will associate a new ring, for each EQ/CQ/WQ tuple.
6723 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04006724 */
James Smart085c6472010-11-20 23:11:37 -05006725
James Smartda0436e2009-05-22 14:51:39 -04006726 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08006727 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04006728 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
6729 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
6730
Gaurav Srivastava20397172021-06-08 10:05:54 +05306731 /* for VMID idle timeout if VMID is enabled */
6732 if (lpfc_is_vmid_enabled(phba))
6733 timer_setup(&phba->inactive_vmid_poll, lpfc_vmid_poll, 0);
6734
James Smartda0436e2009-05-22 14:51:39 -04006735 /*
6736 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
6737 */
James Smartc00f62e2019-08-14 16:57:11 -07006738 /* Initialize the Abort buffer list used by driver */
6739 spin_lock_init(&phba->sli4_hba.abts_io_buf_list_lock);
6740 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_io_buf_list);
James Smart895427b2017-02-12 13:52:30 -08006741
6742 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6743 /* Initialize the Abort nvme buffer list used by driver */
James Smart5e5b5112019-01-28 11:14:22 -08006744 spin_lock_init(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07006745 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smarta8cf5df2017-05-15 15:20:46 -07006746 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
James Smart79d8c4c2019-05-21 17:48:56 -07006747 spin_lock_init(&phba->sli4_hba.t_active_list_lock);
6748 INIT_LIST_HEAD(&phba->sli4_hba.t_active_ctx_list);
James Smart895427b2017-02-12 13:52:30 -08006749 }
6750
James Smartda0436e2009-05-22 14:51:39 -04006751 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08006752 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smarta8cf5df2017-05-15 15:20:46 -07006753 spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
James Smarte7dab162020-10-20 13:27:12 -07006754 spin_lock_init(&phba->sli4_hba.asynce_list_lock);
6755 spin_lock_init(&phba->sli4_hba.els_xri_abrt_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04006756
6757 /*
James Smart6d368e52011-05-24 11:44:12 -04006758 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04006759 */
6760
6761 /* Driver internel slow-path CQ Event pool */
6762 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
6763 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04006764 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04006765 /* Asynchronous event CQ Event work queue list */
6766 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
James Smartda0436e2009-05-22 14:51:39 -04006767 /* Slow-path XRI aborted CQ Event work queue list */
6768 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
6769 /* Receive queue CQ Event work queue list */
6770 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
6771
James Smart6d368e52011-05-24 11:44:12 -04006772 /* Initialize extent block lists. */
6773 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
6774 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
6775 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
6776 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
6777
James Smartd1f525a2017-04-21 16:04:55 -07006778 /* Initialize mboxq lists. If the early init routines fail
6779 * these lists need to be correctly initialized.
6780 */
6781 INIT_LIST_HEAD(&phba->sli.mboxq);
6782 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
6783
James Smart448193b2015-12-16 18:12:05 -05006784 /* initialize optic_state to 0xFF */
6785 phba->sli4_hba.lnk_info.optic_state = 0xff;
6786
James Smartda0436e2009-05-22 14:51:39 -04006787 /* Allocate device driver memory */
6788 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
6789 if (rc)
6790 return -ENOMEM;
6791
James Smart2fcee4b2010-12-15 17:57:46 -05006792 /* IF Type 2 ports get initialized now. */
James Smart27d6ac02018-02-22 08:18:42 -08006793 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart2fcee4b2010-12-15 17:57:46 -05006794 LPFC_SLI_INTF_IF_TYPE_2) {
6795 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08006796 if (unlikely(rc)) {
6797 rc = -ENODEV;
6798 goto out_free_mem;
6799 }
James Smart946727d2015-04-07 15:07:09 -04006800 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05006801 }
6802
James Smartda0436e2009-05-22 14:51:39 -04006803 /* Create the bootstrap mailbox command */
6804 rc = lpfc_create_bootstrap_mbox(phba);
6805 if (unlikely(rc))
6806 goto out_free_mem;
6807
6808 /* Set up the host's endian order with the device. */
6809 rc = lpfc_setup_endian_order(phba);
6810 if (unlikely(rc))
6811 goto out_free_bsmbx;
6812
6813 /* Set up the hba's configuration parameters. */
6814 rc = lpfc_sli4_read_config(phba);
6815 if (unlikely(rc))
6816 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05006817 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6818 if (unlikely(rc))
6819 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006820
James Smart2fcee4b2010-12-15 17:57:46 -05006821 /* IF Type 0 ports get initialized now. */
6822 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6823 LPFC_SLI_INTF_IF_TYPE_0) {
6824 rc = lpfc_pci_function_reset(phba);
6825 if (unlikely(rc))
6826 goto out_free_bsmbx;
6827 }
James Smartda0436e2009-05-22 14:51:39 -04006828
James Smartcb5172e2010-03-15 11:25:07 -04006829 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6830 GFP_KERNEL);
6831 if (!mboxq) {
6832 rc = -ENOMEM;
6833 goto out_free_bsmbx;
6834 }
6835
James Smartf358dd02017-02-12 13:52:34 -08006836 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08006837 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08006838 if (lpfc_enable_nvmet_cnt) {
6839
6840 /* First get WWN of HBA instance */
6841 lpfc_read_nv(phba, mboxq);
6842 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6843 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006844 lpfc_printf_log(phba, KERN_ERR,
6845 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08006846 "6016 Mailbox failed , mbxCmd x%x "
6847 "READ_NV, mbxStatus x%x\n",
6848 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6849 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smartd1f525a2017-04-21 16:04:55 -07006850 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartf358dd02017-02-12 13:52:34 -08006851 rc = -EIO;
6852 goto out_free_bsmbx;
6853 }
6854 mb = &mboxq->u.mb;
6855 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6856 sizeof(uint64_t));
6857 wwn = cpu_to_be64(wwn);
6858 phba->sli4_hba.wwnn.u.name = wwn;
6859 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6860 sizeof(uint64_t));
6861 /* wwn is WWPN of HBA instance */
6862 wwn = cpu_to_be64(wwn);
6863 phba->sli4_hba.wwpn.u.name = wwn;
6864
6865 /* Check to see if it matches any module parameter */
6866 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6867 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08006868#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smart3c603be2017-05-15 15:20:44 -07006869 if (lpfc_nvmet_mem_alloc(phba))
6870 break;
6871
6872 phba->nvmet_support = 1; /* a match */
6873
Dick Kennedy372c1872020-06-30 14:50:00 -07006874 lpfc_printf_log(phba, KERN_ERR,
6875 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08006876 "6017 NVME Target %016llx\n",
6877 wwn);
James Smart7d708032017-03-08 14:36:01 -08006878#else
Dick Kennedy372c1872020-06-30 14:50:00 -07006879 lpfc_printf_log(phba, KERN_ERR,
6880 LOG_TRACE_EVENT,
James Smart7d708032017-03-08 14:36:01 -08006881 "6021 Can't enable NVME Target."
6882 " NVME_TARGET_FC infrastructure"
6883 " is not in kernel\n");
6884#endif
James Smartc4908502019-01-28 11:14:28 -08006885 /* Not supported for NVMET */
6886 phba->cfg_xri_rebalancing = 0;
Dick Kennedy3048e3e2020-05-01 14:43:06 -07006887 if (phba->irq_chann_mode == NHT_MODE) {
6888 phba->cfg_irq_chann =
6889 phba->sli4_hba.num_present_cpu;
6890 phba->cfg_hdw_queue =
6891 phba->sli4_hba.num_present_cpu;
6892 phba->irq_chann_mode = NORMAL_MODE;
6893 }
James Smart3c603be2017-05-15 15:20:44 -07006894 break;
James Smartf358dd02017-02-12 13:52:34 -08006895 }
6896 }
6897 }
James Smart895427b2017-02-12 13:52:30 -08006898
6899 lpfc_nvme_mod_param_dep(phba);
6900
James Smartfedd3b72011-02-16 12:39:24 -05006901 /*
6902 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04006903 * If this call fails, it isn't critical unless the SLI4 parameters come
6904 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05006905 */
James Smart6d368e52011-05-24 11:44:12 -04006906 rc = lpfc_get_sli4_parameters(phba, mboxq);
6907 if (rc) {
James Smartb92dc722018-05-24 21:09:01 -07006908 if_type = bf_get(lpfc_sli_intf_if_type,
6909 &phba->sli4_hba.sli_intf);
6910 if_fam = bf_get(lpfc_sli_intf_sli_family,
6911 &phba->sli4_hba.sli_intf);
James Smart6d368e52011-05-24 11:44:12 -04006912 if (phba->sli4_hba.extents_in_use &&
6913 phba->sli4_hba.rpi_hdrs_in_use) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006914 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6915 "2999 Unsupported SLI4 Parameters "
6916 "Extents and RPI headers enabled.\n");
James Smartb92dc722018-05-24 21:09:01 -07006917 if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6918 if_fam == LPFC_SLI_INTF_FAMILY_BE2) {
6919 mempool_free(mboxq, phba->mbox_mem_pool);
6920 rc = -EIO;
6921 goto out_free_bsmbx;
6922 }
James Smart6d368e52011-05-24 11:44:12 -04006923 }
James Smartb92dc722018-05-24 21:09:01 -07006924 if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6925 if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
6926 mempool_free(mboxq, phba->mbox_mem_pool);
6927 rc = -EIO;
6928 goto out_free_bsmbx;
6929 }
James Smart6d368e52011-05-24 11:44:12 -04006930 }
James Smart895427b2017-02-12 13:52:30 -08006931
James Smartd79c9e92019-08-14 16:57:09 -07006932 /*
6933 * 1 for cmd, 1 for rsp, NVME adds an extra one
6934 * for boundary conditions in its max_sgl_segment template.
6935 */
6936 extra = 2;
6937 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
6938 extra++;
6939
6940 /*
6941 * It doesn't matter what family our adapter is in, we are
6942 * limited to 2 Pages, 512 SGEs, for our SGL.
6943 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
6944 */
6945 max_buf_size = (2 * SLI4_PAGE_SIZE);
6946
6947 /*
6948 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
6949 * used to create the sg_dma_buf_pool must be calculated.
6950 */
6951 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
6952 /* Both cfg_enable_bg and cfg_external_dif code paths */
6953
6954 /*
6955 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
6956 * the FCP rsp, and a SGE. Sice we have no control
6957 * over how many protection segments the SCSI Layer
6958 * will hand us (ie: there could be one for every block
6959 * in the IO), just allocate enough SGEs to accomidate
6960 * our max amount and we need to limit lpfc_sg_seg_cnt
6961 * to minimize the risk of running out.
6962 */
6963 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6964 sizeof(struct fcp_rsp) + max_buf_size;
6965
6966 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
6967 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
6968
6969 /*
6970 * If supporting DIF, reduce the seg count for scsi to
6971 * allow room for the DIF sges.
6972 */
6973 if (phba->cfg_enable_bg &&
6974 phba->cfg_sg_seg_cnt > LPFC_MAX_BG_SLI4_SEG_CNT_DIF)
6975 phba->cfg_scsi_seg_cnt = LPFC_MAX_BG_SLI4_SEG_CNT_DIF;
6976 else
6977 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
6978
6979 } else {
6980 /*
6981 * The scsi_buf for a regular I/O holds the FCP cmnd,
6982 * the FCP rsp, a SGE for each, and a SGE for up to
6983 * cfg_sg_seg_cnt data segments.
6984 */
6985 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6986 sizeof(struct fcp_rsp) +
6987 ((phba->cfg_sg_seg_cnt + extra) *
6988 sizeof(struct sli4_sge));
6989
6990 /* Total SGEs for scsi_sg_list */
6991 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
6992 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
6993
6994 /*
6995 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
6996 * need to post 1 page for the SGL.
6997 */
6998 }
6999
7000 if (phba->cfg_xpsgl && !phba->nvmet_support)
7001 phba->cfg_sg_dma_buf_size = LPFC_DEFAULT_XPSGL_SIZE;
7002 else if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
7003 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
7004 else
7005 phba->cfg_sg_dma_buf_size =
7006 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
7007
7008 phba->border_sge_num = phba->cfg_sg_dma_buf_size /
7009 sizeof(struct sli4_sge);
7010
7011 /* Limit to LPFC_MAX_NVME_SEG_CNT for NVME. */
7012 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
7013 if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
7014 lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
7015 "6300 Reducing NVME sg segment "
7016 "cnt to %d\n",
7017 LPFC_MAX_NVME_SEG_CNT);
7018 phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
7019 } else
7020 phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
7021 }
7022
James Smartd79c9e92019-08-14 16:57:09 -07007023 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
7024 "9087 sg_seg_cnt:%d dmabuf_size:%d "
7025 "total:%d scsi:%d nvme:%d\n",
7026 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
7027 phba->cfg_total_seg_cnt, phba->cfg_scsi_seg_cnt,
7028 phba->cfg_nvme_seg_cnt);
7029
7030 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
7031 i = phba->cfg_sg_dma_buf_size;
7032 else
7033 i = SLI4_PAGE_SIZE;
7034
7035 phba->lpfc_sg_dma_buf_pool =
7036 dma_pool_create("lpfc_sg_dma_buf_pool",
7037 &phba->pcidev->dev,
7038 phba->cfg_sg_dma_buf_size,
7039 i, 0);
7040 if (!phba->lpfc_sg_dma_buf_pool)
7041 goto out_free_bsmbx;
7042
7043 phba->lpfc_cmd_rsp_buf_pool =
7044 dma_pool_create("lpfc_cmd_rsp_buf_pool",
7045 &phba->pcidev->dev,
7046 sizeof(struct fcp_cmnd) +
7047 sizeof(struct fcp_rsp),
7048 i, 0);
7049 if (!phba->lpfc_cmd_rsp_buf_pool)
7050 goto out_free_sg_dma_buf;
7051
James Smartcb5172e2010-03-15 11:25:07 -04007052 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05007053
7054 /* Verify OAS is supported */
7055 lpfc_sli4_oas_verify(phba);
James Smart1ba981f2014-02-20 09:56:45 -05007056
James Smartd2cc9bc2018-09-10 10:30:50 -07007057 /* Verify RAS support on adapter */
7058 lpfc_sli4_ras_init(phba);
7059
James Smart5350d872011-10-10 21:33:49 -04007060 /* Verify all the SLI4 queues */
7061 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04007062 if (rc)
James Smartd79c9e92019-08-14 16:57:09 -07007063 goto out_free_cmd_rsp_buf;
James Smartda0436e2009-05-22 14:51:39 -04007064
7065 /* Create driver internal CQE event pool */
7066 rc = lpfc_sli4_cq_event_pool_create(phba);
7067 if (rc)
James Smartd79c9e92019-08-14 16:57:09 -07007068 goto out_free_cmd_rsp_buf;
James Smartda0436e2009-05-22 14:51:39 -04007069
James Smart8a9d2e82012-05-09 21:16:12 -04007070 /* Initialize sgl lists per host */
7071 lpfc_init_sgl_list(phba);
7072
7073 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04007074 rc = lpfc_init_active_sgl_array(phba);
7075 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007076 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04007077 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007078 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04007079 }
James Smartda0436e2009-05-22 14:51:39 -04007080 rc = lpfc_sli4_init_rpi_hdrs(phba);
7081 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007082 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04007083 "1432 Failed to initialize rpi headers.\n");
7084 goto out_free_active_sgl;
7085 }
7086
James Smarta93ff372010-10-22 11:06:08 -04007087 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05007088 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07007089 phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart0c9ab6f2010-02-26 14:15:57 -05007090 GFP_KERNEL);
7091 if (!phba->fcf.fcf_rr_bmask) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007092 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05007093 "2759 Failed allocate memory for FCF round "
7094 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04007095 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05007096 goto out_remove_rpi_hdrs;
7097 }
7098
James Smart6a828b02019-01-28 11:14:31 -08007099 phba->sli4_hba.hba_eq_hdl = kcalloc(phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08007100 sizeof(struct lpfc_hba_eq_hdl),
7101 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08007102 if (!phba->sli4_hba.hba_eq_hdl) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007103 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart67d12732012-08-03 12:36:13 -04007104 "2572 Failed allocate memory for "
7105 "fast-path per-EQ handle array\n");
7106 rc = -ENOMEM;
7107 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04007108 }
7109
James Smart222e9232019-01-28 11:14:35 -08007110 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_possible_cpu,
James Smart895427b2017-02-12 13:52:30 -08007111 sizeof(struct lpfc_vector_map_info),
7112 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04007113 if (!phba->sli4_hba.cpu_map) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007114 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7bb03bb2013-04-17 20:19:16 -04007115 "3327 Failed allocate memory for msi-x "
7116 "interrupt vector mapping\n");
7117 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08007118 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04007119 }
James Smartb246de12013-05-31 17:03:07 -04007120
James Smart32517fc2019-01-28 11:14:33 -08007121 phba->sli4_hba.eq_info = alloc_percpu(struct lpfc_eq_intr_info);
7122 if (!phba->sli4_hba.eq_info) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007123 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32517fc2019-01-28 11:14:33 -08007124 "3321 Failed allocation for per_cpu stats\n");
7125 rc = -ENOMEM;
7126 goto out_free_hba_cpu_map;
7127 }
James Smart840eda92020-03-22 11:13:00 -07007128
Dick Kennedy317aeb82020-06-30 14:49:59 -07007129 phba->sli4_hba.idle_stat = kcalloc(phba->sli4_hba.num_possible_cpu,
7130 sizeof(*phba->sli4_hba.idle_stat),
7131 GFP_KERNEL);
7132 if (!phba->sli4_hba.idle_stat) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007133 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -07007134 "3390 Failed allocation for idle_stat\n");
7135 rc = -ENOMEM;
7136 goto out_free_hba_eq_info;
7137 }
7138
James Smart840eda92020-03-22 11:13:00 -07007139#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
7140 phba->sli4_hba.c_stat = alloc_percpu(struct lpfc_hdwq_stat);
7141 if (!phba->sli4_hba.c_stat) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007142 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart840eda92020-03-22 11:13:00 -07007143 "3332 Failed allocating per cpu hdwq stats\n");
7144 rc = -ENOMEM;
Dick Kennedy317aeb82020-06-30 14:49:59 -07007145 goto out_free_hba_idle_stat;
James Smart840eda92020-03-22 11:13:00 -07007146 }
7147#endif
7148
James Smart912e3ac2011-05-24 11:42:11 -04007149 /*
7150 * Enable sr-iov virtual functions if supported and configured
7151 * through the module parameter.
7152 */
7153 if (phba->cfg_sriov_nr_virtfn > 0) {
7154 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
7155 phba->cfg_sriov_nr_virtfn);
7156 if (rc) {
7157 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7158 "3020 Requested number of SR-IOV "
7159 "virtual functions (%d) is not "
7160 "supported\n",
7161 phba->cfg_sriov_nr_virtfn);
7162 phba->cfg_sriov_nr_virtfn = 0;
7163 }
7164 }
7165
James Smart5248a742011-07-22 18:37:06 -04007166 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007167
James Smart840eda92020-03-22 11:13:00 -07007168#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
Dick Kennedy317aeb82020-06-30 14:49:59 -07007169out_free_hba_idle_stat:
7170 kfree(phba->sli4_hba.idle_stat);
7171#endif
James Smart840eda92020-03-22 11:13:00 -07007172out_free_hba_eq_info:
7173 free_percpu(phba->sli4_hba.eq_info);
James Smart32517fc2019-01-28 11:14:33 -08007174out_free_hba_cpu_map:
7175 kfree(phba->sli4_hba.cpu_map);
James Smart895427b2017-02-12 13:52:30 -08007176out_free_hba_eq_hdl:
7177 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05007178out_free_fcf_rr_bmask:
7179 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04007180out_remove_rpi_hdrs:
7181 lpfc_sli4_remove_rpi_hdrs(phba);
7182out_free_active_sgl:
7183 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04007184out_destroy_cq_event_pool:
7185 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartd79c9e92019-08-14 16:57:09 -07007186out_free_cmd_rsp_buf:
7187 dma_pool_destroy(phba->lpfc_cmd_rsp_buf_pool);
7188 phba->lpfc_cmd_rsp_buf_pool = NULL;
7189out_free_sg_dma_buf:
7190 dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
7191 phba->lpfc_sg_dma_buf_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007192out_free_bsmbx:
7193 lpfc_destroy_bootstrap_mbox(phba);
7194out_free_mem:
7195 lpfc_mem_free(phba);
7196 return rc;
7197}
7198
7199/**
7200 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
7201 * @phba: pointer to lpfc hba data structure.
7202 *
7203 * This routine is invoked to unset the driver internal resources set up
7204 * specific for supporting the SLI-4 HBA device it attached to.
7205 **/
7206static void
7207lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
7208{
7209 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
7210
James Smart32517fc2019-01-28 11:14:33 -08007211 free_percpu(phba->sli4_hba.eq_info);
James Smart840eda92020-03-22 11:13:00 -07007212#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
7213 free_percpu(phba->sli4_hba.c_stat);
7214#endif
Dick Kennedy317aeb82020-06-30 14:49:59 -07007215 kfree(phba->sli4_hba.idle_stat);
James Smart32517fc2019-01-28 11:14:33 -08007216
James Smart7bb03bb2013-04-17 20:19:16 -04007217 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
7218 kfree(phba->sli4_hba.cpu_map);
James Smart222e9232019-01-28 11:14:35 -08007219 phba->sli4_hba.num_possible_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04007220 phba->sli4_hba.num_present_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05007221 phba->sli4_hba.curr_disp_cpu = 0;
Dick Kennedy3048e3e2020-05-01 14:43:06 -07007222 cpumask_clear(&phba->sli4_hba.irq_aff_mask);
James Smart7bb03bb2013-04-17 20:19:16 -04007223
James Smartda0436e2009-05-22 14:51:39 -04007224 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08007225 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04007226
7227 /* Free the allocated rpi headers. */
7228 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04007229 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04007230
James Smart0c9ab6f2010-02-26 14:15:57 -05007231 /* Free eligible FCF index bmask */
7232 kfree(phba->fcf.fcf_rr_bmask);
7233
James Smartda0436e2009-05-22 14:51:39 -04007234 /* Free the ELS sgl list */
7235 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007236 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08007237 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04007238
James Smartda0436e2009-05-22 14:51:39 -04007239 /* Free the completion queue EQ event pool */
7240 lpfc_sli4_cq_event_release_all(phba);
7241 lpfc_sli4_cq_event_pool_destroy(phba);
7242
James Smart6d368e52011-05-24 11:44:12 -04007243 /* Release resource identifiers. */
7244 lpfc_sli4_dealloc_resource_identifiers(phba);
7245
James Smartda0436e2009-05-22 14:51:39 -04007246 /* Free the bsmbx region. */
7247 lpfc_destroy_bootstrap_mbox(phba);
7248
7249 /* Free the SLI Layer memory with SLI4 HBAs */
7250 lpfc_mem_free_all(phba);
7251
7252 /* Free the current connect table */
7253 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04007254 &phba->fcf_conn_rec_list, list) {
7255 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04007256 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04007257 }
James Smartda0436e2009-05-22 14:51:39 -04007258
7259 return;
7260}
7261
7262/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007263 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04007264 * @phba: The hba struct for which this call is being executed.
7265 * @dev_grp: The HBA PCI-Device group number.
7266 *
7267 * This routine sets up the device INIT interface API function jump table
7268 * in @phba struct.
7269 *
7270 * Returns: 0 - success, -ENODEV - failure.
7271 **/
7272int
7273lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7274{
James Smart84d1b002010-02-12 14:42:33 -05007275 phba->lpfc_hba_init_link = lpfc_hba_init_link;
7276 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05007277 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04007278 switch (dev_grp) {
7279 case LPFC_PCI_DEV_LP:
7280 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
7281 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
7282 phba->lpfc_stop_port = lpfc_stop_port_s3;
7283 break;
James Smartda0436e2009-05-22 14:51:39 -04007284 case LPFC_PCI_DEV_OC:
7285 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
7286 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
7287 phba->lpfc_stop_port = lpfc_stop_port_s4;
7288 break;
James Smart3772a992009-05-22 14:50:54 -04007289 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07007290 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04007291 "1431 Invalid HBA PCI-device group: 0x%x\n",
7292 dev_grp);
7293 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007294 }
7295 return 0;
7296}
7297
7298/**
James Smart3772a992009-05-22 14:50:54 -04007299 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
7300 * @phba: pointer to lpfc hba data structure.
7301 *
7302 * This routine is invoked to set up the driver internal resources after the
7303 * device specific resource setup to support the HBA device it attached to.
7304 *
7305 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007306 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007307 * other values - error
7308 **/
7309static int
7310lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
7311{
7312 int error;
7313
7314 /* Startup the kernel thread for this host adapter. */
7315 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7316 "lpfc_worker_%d", phba->brd_no);
7317 if (IS_ERR(phba->worker_thread)) {
7318 error = PTR_ERR(phba->worker_thread);
7319 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007320 }
7321
James Smart3772a992009-05-22 14:50:54 -04007322 return 0;
7323}
7324
7325/**
7326 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
7327 * @phba: pointer to lpfc hba data structure.
7328 *
7329 * This routine is invoked to unset the driver internal resources set up after
7330 * the device specific resource setup for supporting the HBA device it
7331 * attached to.
7332 **/
7333static void
7334lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
7335{
Dick Kennedyf485c182017-09-29 17:34:34 -07007336 if (phba->wq) {
7337 flush_workqueue(phba->wq);
7338 destroy_workqueue(phba->wq);
7339 phba->wq = NULL;
7340 }
7341
James Smart3772a992009-05-22 14:50:54 -04007342 /* Stop kernel worker thread */
James Smart0cdb84e2018-04-09 14:24:26 -07007343 if (phba->worker_thread)
7344 kthread_stop(phba->worker_thread);
James Smart3772a992009-05-22 14:50:54 -04007345}
7346
7347/**
7348 * lpfc_free_iocb_list - Free iocb list.
7349 * @phba: pointer to lpfc hba data structure.
7350 *
7351 * This routine is invoked to free the driver's IOCB list and memory.
7352 **/
James Smart6c621a22017-05-15 15:20:45 -07007353void
James Smart3772a992009-05-22 14:50:54 -04007354lpfc_free_iocb_list(struct lpfc_hba *phba)
7355{
7356 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
7357
7358 spin_lock_irq(&phba->hbalock);
7359 list_for_each_entry_safe(iocbq_entry, iocbq_next,
7360 &phba->lpfc_iocb_list, list) {
7361 list_del(&iocbq_entry->list);
7362 kfree(iocbq_entry);
7363 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007364 }
James Smart3772a992009-05-22 14:50:54 -04007365 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007366
James Smart3772a992009-05-22 14:50:54 -04007367 return;
7368}
dea31012005-04-17 16:05:31 -05007369
James Smart3772a992009-05-22 14:50:54 -04007370/**
7371 * lpfc_init_iocb_list - Allocate and initialize iocb list.
7372 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01007373 * @iocb_count: number of requested iocbs
James Smart3772a992009-05-22 14:50:54 -04007374 *
7375 * This routine is invoked to allocate and initizlize the driver's IOCB
7376 * list and set up the IOCB tag array accordingly.
7377 *
7378 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007379 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007380 * other values - error
7381 **/
James Smart6c621a22017-05-15 15:20:45 -07007382int
James Smart3772a992009-05-22 14:50:54 -04007383lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
7384{
7385 struct lpfc_iocbq *iocbq_entry = NULL;
7386 uint16_t iotag;
7387 int i;
dea31012005-04-17 16:05:31 -05007388
7389 /* Initialize and populate the iocb list per host. */
7390 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04007391 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07007392 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05007393 if (iocbq_entry == NULL) {
7394 printk(KERN_ERR "%s: only allocated %d iocbs of "
7395 "expected %d count. Unloading driver.\n",
James Smarta5f73372019-09-21 20:58:50 -07007396 __func__, i, iocb_count);
dea31012005-04-17 16:05:31 -05007397 goto out_free_iocbq;
7398 }
7399
James Bottomley604a3e32005-10-29 10:28:33 -05007400 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
7401 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04007402 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05007403 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04007404 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05007405 goto out_free_iocbq;
7406 }
James Smart6d368e52011-05-24 11:44:12 -04007407 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04007408 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05007409
7410 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007411 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
7412 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05007413 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007414 }
7415
James Smart3772a992009-05-22 14:50:54 -04007416 return 0;
7417
7418out_free_iocbq:
7419 lpfc_free_iocb_list(phba);
7420
7421 return -ENOMEM;
7422}
7423
7424/**
James Smart8a9d2e82012-05-09 21:16:12 -04007425 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007426 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04007427 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007428 *
James Smart8a9d2e82012-05-09 21:16:12 -04007429 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04007430 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007431void
7432lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04007433{
7434 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04007435
7436 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
7437 list_del(&sglq_entry->list);
7438 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
7439 kfree(sglq_entry);
7440 }
7441}
7442
7443/**
7444 * lpfc_free_els_sgl_list - Free els sgl list.
7445 * @phba: pointer to lpfc hba data structure.
7446 *
7447 * This routine is invoked to free the driver's els sgl list and memory.
7448 **/
7449static void
7450lpfc_free_els_sgl_list(struct lpfc_hba *phba)
7451{
James Smartda0436e2009-05-22 14:51:39 -04007452 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007453
James Smart8a9d2e82012-05-09 21:16:12 -04007454 /* Retrieve all els sgls from driver list */
James Smarta7892412021-04-11 18:31:15 -07007455 spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart895427b2017-02-12 13:52:30 -08007456 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
James Smarta7892412021-04-11 18:31:15 -07007457 spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04007458
James Smart8a9d2e82012-05-09 21:16:12 -04007459 /* Now free the sgl list */
7460 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007461}
7462
7463/**
James Smartf358dd02017-02-12 13:52:34 -08007464 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
7465 * @phba: pointer to lpfc hba data structure.
7466 *
7467 * This routine is invoked to free the driver's nvmet sgl list and memory.
7468 **/
7469static void
7470lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
7471{
7472 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7473 LIST_HEAD(sglq_list);
7474
7475 /* Retrieve all nvmet sgls from driver list */
7476 spin_lock_irq(&phba->hbalock);
7477 spin_lock(&phba->sli4_hba.sgl_list_lock);
7478 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
7479 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7480 spin_unlock_irq(&phba->hbalock);
7481
7482 /* Now free the sgl list */
7483 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
7484 list_del(&sglq_entry->list);
7485 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
7486 kfree(sglq_entry);
7487 }
Dick Kennedy4b40d022017-08-23 16:55:38 -07007488
7489 /* Update the nvmet_xri_cnt to reflect no current sgls.
7490 * The next initialization cycle sets the count and allocates
7491 * the sgls over again.
7492 */
7493 phba->sli4_hba.nvmet_xri_cnt = 0;
James Smartf358dd02017-02-12 13:52:34 -08007494}
7495
7496/**
James Smartda0436e2009-05-22 14:51:39 -04007497 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
7498 * @phba: pointer to lpfc hba data structure.
7499 *
7500 * This routine is invoked to allocate the driver's active sgl memory.
7501 * This array will hold the sglq_entry's for active IOs.
7502 **/
7503static int
7504lpfc_init_active_sgl_array(struct lpfc_hba *phba)
7505{
7506 int size;
7507 size = sizeof(struct lpfc_sglq *);
7508 size *= phba->sli4_hba.max_cfg_param.max_xri;
7509
7510 phba->sli4_hba.lpfc_sglq_active_list =
7511 kzalloc(size, GFP_KERNEL);
7512 if (!phba->sli4_hba.lpfc_sglq_active_list)
7513 return -ENOMEM;
7514 return 0;
7515}
7516
7517/**
7518 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
7519 * @phba: pointer to lpfc hba data structure.
7520 *
7521 * This routine is invoked to walk through the array of active sglq entries
7522 * and free all of the resources.
7523 * This is just a place holder for now.
7524 **/
7525static void
7526lpfc_free_active_sgl(struct lpfc_hba *phba)
7527{
7528 kfree(phba->sli4_hba.lpfc_sglq_active_list);
7529}
7530
7531/**
7532 * lpfc_init_sgl_list - Allocate and initialize sgl list.
7533 * @phba: pointer to lpfc hba data structure.
7534 *
7535 * This routine is invoked to allocate and initizlize the driver's sgl
7536 * list and set up the sgl xritag tag array accordingly.
7537 *
James Smartda0436e2009-05-22 14:51:39 -04007538 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007539static void
James Smartda0436e2009-05-22 14:51:39 -04007540lpfc_init_sgl_list(struct lpfc_hba *phba)
7541{
James Smartda0436e2009-05-22 14:51:39 -04007542 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08007543 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04007544 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08007545 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
James Smart86c67372017-04-21 16:05:04 -07007546 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smartda0436e2009-05-22 14:51:39 -04007547
James Smart8a9d2e82012-05-09 21:16:12 -04007548 /* els xri-sgl book keeping */
7549 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007550
James Smart895427b2017-02-12 13:52:30 -08007551 /* nvme xri-buffer book keeping */
James Smart5e5b5112019-01-28 11:14:22 -08007552 phba->sli4_hba.io_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007553}
7554
7555/**
7556 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
7557 * @phba: pointer to lpfc hba data structure.
7558 *
7559 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04007560 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04007561 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04007562 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
7563 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04007564 *
7565 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007566 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04007567 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04007568 **/
7569int
7570lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
7571{
7572 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007573 struct lpfc_rpi_hdr *rpi_hdr;
7574
7575 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05007576 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04007577 return rc;
James Smart6d368e52011-05-24 11:44:12 -04007578 if (phba->sli4_hba.extents_in_use)
7579 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04007580
7581 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
7582 if (!rpi_hdr) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007583 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04007584 "0391 Error during rpi post operation\n");
7585 lpfc_sli4_remove_rpis(phba);
7586 rc = -ENODEV;
7587 }
7588
7589 return rc;
7590}
7591
7592/**
7593 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
7594 * @phba: pointer to lpfc hba data structure.
7595 *
7596 * This routine is invoked to allocate a single 4KB memory region to
7597 * support rpis and stores them in the phba. This single region
7598 * provides support for up to 64 rpis. The region is used globally
7599 * by the device.
7600 *
7601 * Returns:
7602 * A valid rpi hdr on success.
7603 * A NULL pointer on any failure.
7604 **/
7605struct lpfc_rpi_hdr *
7606lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
7607{
7608 uint16_t rpi_limit, curr_rpi_range;
7609 struct lpfc_dmabuf *dmabuf;
7610 struct lpfc_rpi_hdr *rpi_hdr;
7611
James Smart6d368e52011-05-24 11:44:12 -04007612 /*
7613 * If the SLI4 port supports extents, posting the rpi header isn't
7614 * required. Set the expected maximum count and let the actual value
7615 * get set when extents are fully allocated.
7616 */
7617 if (!phba->sli4_hba.rpi_hdrs_in_use)
7618 return NULL;
7619 if (phba->sli4_hba.extents_in_use)
7620 return NULL;
7621
7622 /* The limit on the logical index is just the max_rpi count. */
James Smart845d9e82017-05-15 15:20:38 -07007623 rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007624
7625 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007626 /*
7627 * Establish the starting RPI in this header block. The starting
7628 * rpi is normalized to a zero base because the physical rpi is
7629 * port based.
7630 */
James Smart97f2ecf2012-03-01 22:35:23 -05007631 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007632 spin_unlock_irq(&phba->hbalock);
7633
James Smart845d9e82017-05-15 15:20:38 -07007634 /* Reached full RPI range */
7635 if (curr_rpi_range == rpi_limit)
James Smart6d368e52011-05-24 11:44:12 -04007636 return NULL;
James Smart845d9e82017-05-15 15:20:38 -07007637
James Smartda0436e2009-05-22 14:51:39 -04007638 /*
7639 * First allocate the protocol header region for the port. The
7640 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
7641 */
7642 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7643 if (!dmabuf)
7644 return NULL;
7645
Luis Chamberlain750afb02019-01-04 09:23:09 +01007646 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
7647 LPFC_HDR_TEMPLATE_SIZE,
7648 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007649 if (!dmabuf->virt) {
7650 rpi_hdr = NULL;
7651 goto err_free_dmabuf;
7652 }
7653
James Smartda0436e2009-05-22 14:51:39 -04007654 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
7655 rpi_hdr = NULL;
7656 goto err_free_coherent;
7657 }
7658
7659 /* Save the rpi header data for cleanup later. */
7660 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
7661 if (!rpi_hdr)
7662 goto err_free_coherent;
7663
7664 rpi_hdr->dmabuf = dmabuf;
7665 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
7666 rpi_hdr->page_count = 1;
7667 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007668
7669 /* The rpi_hdr stores the logical index only. */
7670 rpi_hdr->start_rpi = curr_rpi_range;
James Smart845d9e82017-05-15 15:20:38 -07007671 rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04007672 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
7673
James Smartda0436e2009-05-22 14:51:39 -04007674 spin_unlock_irq(&phba->hbalock);
7675 return rpi_hdr;
7676
7677 err_free_coherent:
7678 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
7679 dmabuf->virt, dmabuf->phys);
7680 err_free_dmabuf:
7681 kfree(dmabuf);
7682 return NULL;
7683}
7684
7685/**
7686 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
7687 * @phba: pointer to lpfc hba data structure.
7688 *
7689 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04007690 * to support rpis for SLI4 ports not supporting extents. This routine
7691 * presumes the caller has released all rpis consumed by fabric or port
7692 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04007693 **/
7694void
7695lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
7696{
7697 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
7698
James Smart6d368e52011-05-24 11:44:12 -04007699 if (!phba->sli4_hba.rpi_hdrs_in_use)
7700 goto exit;
7701
James Smartda0436e2009-05-22 14:51:39 -04007702 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
7703 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
7704 list_del(&rpi_hdr->list);
7705 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
7706 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
7707 kfree(rpi_hdr->dmabuf);
7708 kfree(rpi_hdr);
7709 }
James Smart6d368e52011-05-24 11:44:12 -04007710 exit:
7711 /* There are no rpis available to the port now. */
7712 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04007713}
7714
7715/**
James Smart3772a992009-05-22 14:50:54 -04007716 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
7717 * @pdev: pointer to pci device data structure.
7718 *
7719 * This routine is invoked to allocate the driver hba data structure for an
7720 * HBA device. If the allocation is successful, the phba reference to the
7721 * PCI device data structure is set.
7722 *
7723 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007724 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04007725 * NULL - error
7726 **/
7727static struct lpfc_hba *
7728lpfc_hba_alloc(struct pci_dev *pdev)
7729{
7730 struct lpfc_hba *phba;
7731
7732 /* Allocate memory for HBA structure */
7733 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
7734 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02007735 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04007736 return NULL;
7737 }
7738
7739 /* Set reference to PCI device in HBA structure */
7740 phba->pcidev = pdev;
7741
7742 /* Assign an unused board number */
7743 phba->brd_no = lpfc_get_instance();
7744 if (phba->brd_no < 0) {
7745 kfree(phba);
7746 return NULL;
7747 }
James Smart65791f12016-07-06 12:35:56 -07007748 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04007749
James Smart4fede782010-01-26 23:08:55 -05007750 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04007751 INIT_LIST_HEAD(&phba->ct_ev_waiters);
7752
James Smart3772a992009-05-22 14:50:54 -04007753 return phba;
7754}
7755
7756/**
7757 * lpfc_hba_free - Free driver hba data structure with a device.
7758 * @phba: pointer to lpfc hba data structure.
7759 *
7760 * This routine is invoked to free the driver hba data structure with an
7761 * HBA device.
7762 **/
7763static void
7764lpfc_hba_free(struct lpfc_hba *phba)
7765{
James Smart5e5b5112019-01-28 11:14:22 -08007766 if (phba->sli_rev == LPFC_SLI_REV4)
7767 kfree(phba->sli4_hba.hdwq);
7768
James Smart3772a992009-05-22 14:50:54 -04007769 /* Release the driver assigned board number */
7770 idr_remove(&lpfc_hba_index, phba->brd_no);
7771
James Smart895427b2017-02-12 13:52:30 -08007772 /* Free memory allocated with sli3 rings */
7773 kfree(phba->sli.sli3_ring);
7774 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04007775
James Smart3772a992009-05-22 14:50:54 -04007776 kfree(phba);
7777 return;
7778}
7779
7780/**
7781 * lpfc_create_shost - Create hba physical port with associated scsi host.
7782 * @phba: pointer to lpfc hba data structure.
7783 *
7784 * This routine is invoked to create HBA physical port and associate a SCSI
7785 * host with it.
7786 *
7787 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007788 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007789 * other values - error
7790 **/
7791static int
7792lpfc_create_shost(struct lpfc_hba *phba)
7793{
7794 struct lpfc_vport *vport;
7795 struct Scsi_Host *shost;
7796
7797 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05007798 phba->fc_edtov = FF_DEF_EDTOV;
7799 phba->fc_ratov = FF_DEF_RATOV;
7800 phba->fc_altov = FF_DEF_ALTOV;
7801 phba->fc_arbtov = FF_DEF_ARBTOV;
7802
James Smartd7c47992010-06-08 18:31:54 -04007803 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04007804 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05007805 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04007806 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05007807
7808 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007809 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08007810
James Smartf358dd02017-02-12 13:52:34 -08007811 if (phba->nvmet_support) {
7812 /* Only 1 vport (pport) will support NVME target */
James Smartea85a202019-10-18 14:18:25 -07007813 phba->targetport = NULL;
7814 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
7815 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME_DISC,
7816 "6076 NVME Target Found\n");
James Smartf358dd02017-02-12 13:52:34 -08007817 }
7818
James Smart858c9f62007-06-17 19:56:39 -05007819 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04007820 /* Put reference to SCSI host to driver's device private data */
7821 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05007822
James Smart4258e982015-12-16 18:11:58 -05007823 /*
7824 * At this point we are fully registered with PSA. In addition,
7825 * any initial discovery should be completed.
7826 */
7827 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07007828 if (phba->cfg_enable_SmartSAN ||
7829 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05007830
7831 /* Setup appropriate attribute masks */
7832 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07007833 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05007834 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
7835 else
7836 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
7837 }
James Smart3772a992009-05-22 14:50:54 -04007838 return 0;
7839}
dea31012005-04-17 16:05:31 -05007840
James Smart3772a992009-05-22 14:50:54 -04007841/**
7842 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
7843 * @phba: pointer to lpfc hba data structure.
7844 *
7845 * This routine is invoked to destroy HBA physical port and the associated
7846 * SCSI host.
7847 **/
7848static void
7849lpfc_destroy_shost(struct lpfc_hba *phba)
7850{
7851 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04007852
James Smart3772a992009-05-22 14:50:54 -04007853 /* Destroy physical port that associated with the SCSI host */
7854 destroy_port(vport);
7855
7856 return;
7857}
7858
7859/**
7860 * lpfc_setup_bg - Setup Block guard structures and debug areas.
7861 * @phba: pointer to lpfc hba data structure.
7862 * @shost: the shost to be used to detect Block guard settings.
7863 *
7864 * This routine sets up the local Block guard protocol settings for @shost.
7865 * This routine also allocates memory for debugging bg buffers.
7866 **/
7867static void
7868lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
7869{
James Smartbbeb79b2012-06-12 13:54:27 -04007870 uint32_t old_mask;
7871 uint32_t old_guard;
7872
James Smartb3b98b72016-10-13 15:06:06 -07007873 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04007874 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7875 "1478 Registering BlockGuard with the "
7876 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04007877
James Smartb3b98b72016-10-13 15:06:06 -07007878 old_mask = phba->cfg_prot_mask;
7879 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04007880
7881 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07007882 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04007883 SHOST_DIX_TYPE0_PROTECTION |
7884 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07007885 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
7886 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04007887
7888 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07007889 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
7890 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04007891
James Smartb3b98b72016-10-13 15:06:06 -07007892 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7893 if ((old_mask != phba->cfg_prot_mask) ||
7894 (old_guard != phba->cfg_prot_guard))
Dick Kennedy372c1872020-06-30 14:50:00 -07007895 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartbbeb79b2012-06-12 13:54:27 -04007896 "1475 Registering BlockGuard with the "
7897 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07007898 phba->cfg_prot_mask,
7899 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007900
James Smartb3b98b72016-10-13 15:06:06 -07007901 scsi_host_set_prot(shost, phba->cfg_prot_mask);
7902 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007903 } else
Dick Kennedy372c1872020-06-30 14:50:00 -07007904 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartbbeb79b2012-06-12 13:54:27 -04007905 "1479 Not Registering BlockGuard with the SCSI "
7906 "layer, Bad protection parameters: %d %d\n",
7907 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04007908 }
James Smart3772a992009-05-22 14:50:54 -04007909}
7910
7911/**
7912 * lpfc_post_init_setup - Perform necessary device post initialization setup.
7913 * @phba: pointer to lpfc hba data structure.
7914 *
7915 * This routine is invoked to perform all the necessary post initialization
7916 * setup for the device.
7917 **/
7918static void
7919lpfc_post_init_setup(struct lpfc_hba *phba)
7920{
7921 struct Scsi_Host *shost;
7922 struct lpfc_adapter_event_header adapter_event;
7923
7924 /* Get the default values for Model Name and Description */
7925 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7926
7927 /*
7928 * hba setup may have changed the hba_queue_depth so we need to
7929 * adjust the value of can_queue.
7930 */
7931 shost = pci_get_drvdata(phba->pcidev);
7932 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart858c9f62007-06-17 19:56:39 -05007933
7934 lpfc_host_attrib_init(shost);
7935
James Smart2e0fef82007-06-17 19:56:36 -05007936 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7937 spin_lock_irq(shost->host_lock);
7938 lpfc_poll_start_timer(phba);
7939 spin_unlock_irq(shost->host_lock);
7940 }
James Smart8f6d98d2006-08-01 07:34:00 -04007941
James Smart93996272008-08-24 21:50:30 -04007942 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7943 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04007944 /* Send board arrival event to upper layer */
7945 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7946 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7947 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04007948 sizeof(adapter_event),
7949 (char *) &adapter_event,
7950 LPFC_NL_VENDOR_ID);
7951 return;
7952}
7953
7954/**
7955 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7956 * @phba: pointer to lpfc hba data structure.
7957 *
7958 * This routine is invoked to set up the PCI device memory space for device
7959 * with SLI-3 interface spec.
7960 *
7961 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007962 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007963 * other values - error
7964 **/
7965static int
7966lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7967{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007968 struct pci_dev *pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04007969 unsigned long bar0map_len, bar2map_len;
7970 int i, hbq_count;
7971 void *ptr;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007972 int error;
James Smart3772a992009-05-22 14:50:54 -04007973
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007974 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +01007975 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007976
7977 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +01007978 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
7979 if (error)
7980 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
7981 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007982 return error;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007983 error = -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007984
7985 /* Get the bus address of Bar0 and Bar2 and the number of bytes
7986 * required by each mapping.
7987 */
7988 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7989 bar0map_len = pci_resource_len(pdev, 0);
7990
7991 phba->pci_bar2_map = pci_resource_start(pdev, 2);
7992 bar2map_len = pci_resource_len(pdev, 2);
7993
7994 /* Map HBA SLIM to a kernel virtual address. */
7995 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7996 if (!phba->slim_memmap_p) {
7997 dev_printk(KERN_ERR, &pdev->dev,
7998 "ioremap failed for SLIM memory.\n");
7999 goto out;
8000 }
8001
8002 /* Map HBA Control Registers to a kernel virtual address. */
8003 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
8004 if (!phba->ctrl_regs_memmap_p) {
8005 dev_printk(KERN_ERR, &pdev->dev,
8006 "ioremap failed for HBA control registers.\n");
8007 goto out_iounmap_slim;
8008 }
8009
8010 /* Allocate memory for SLI-2 structures */
Luis Chamberlain750afb02019-01-04 09:23:09 +01008011 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
8012 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04008013 if (!phba->slim2p.virt)
8014 goto out_iounmap;
8015
James Smart3772a992009-05-22 14:50:54 -04008016 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04008017 phba->mbox_ext = (phba->slim2p.virt +
8018 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04008019 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
8020 phba->IOCBs = (phba->slim2p.virt +
8021 offsetof(struct lpfc_sli2_slim, IOCBs));
8022
8023 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
8024 lpfc_sli_hbq_size(),
8025 &phba->hbqslimp.phys,
8026 GFP_KERNEL);
8027 if (!phba->hbqslimp.virt)
8028 goto out_free_slim;
8029
8030 hbq_count = lpfc_sli_hbq_count();
8031 ptr = phba->hbqslimp.virt;
8032 for (i = 0; i < hbq_count; ++i) {
8033 phba->hbqs[i].hbq_virt = ptr;
8034 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
8035 ptr += (lpfc_hbq_defs[i]->entry_count *
8036 sizeof(struct lpfc_hbq_entry));
8037 }
8038 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
8039 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
8040
8041 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
8042
James Smart3772a992009-05-22 14:50:54 -04008043 phba->MBslimaddr = phba->slim_memmap_p;
8044 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
8045 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
8046 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
8047 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04008048
dea31012005-04-17 16:05:31 -05008049 return 0;
8050
dea31012005-04-17 16:05:31 -05008051out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04008052 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
8053 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05008054out_iounmap:
8055 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05008056out_iounmap_slim:
dea31012005-04-17 16:05:31 -05008057 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05008058out:
8059 return error;
8060}
8061
James Smarte59058c2008-08-24 21:49:00 -04008062/**
James Smart3772a992009-05-22 14:50:54 -04008063 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
8064 * @phba: pointer to lpfc hba data structure.
8065 *
8066 * This routine is invoked to unset the PCI device memory space for device
8067 * with SLI-3 interface spec.
8068 **/
8069static void
8070lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
8071{
8072 struct pci_dev *pdev;
8073
8074 /* Obtain PCI device reference */
8075 if (!phba->pcidev)
8076 return;
8077 else
8078 pdev = phba->pcidev;
8079
8080 /* Free coherent DMA memory allocated */
8081 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
8082 phba->hbqslimp.virt, phba->hbqslimp.phys);
8083 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
8084 phba->slim2p.virt, phba->slim2p.phys);
8085
8086 /* I/O memory unmap */
8087 iounmap(phba->ctrl_regs_memmap_p);
8088 iounmap(phba->slim_memmap_p);
8089
8090 return;
8091}
8092
8093/**
James Smartda0436e2009-05-22 14:51:39 -04008094 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
8095 * @phba: pointer to lpfc hba data structure.
8096 *
8097 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
8098 * done and check status.
8099 *
8100 * Return 0 if successful, otherwise -ENODEV.
8101 **/
8102int
8103lpfc_sli4_post_status_check(struct lpfc_hba *phba)
8104{
James Smart2fcee4b2010-12-15 17:57:46 -05008105 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
8106 struct lpfc_register reg_data;
8107 int i, port_error = 0;
8108 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04008109
James Smart9940b972011-03-11 16:06:12 -05008110 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
8111 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05008112 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04008113 return -ENODEV;
8114
James Smartda0436e2009-05-22 14:51:39 -04008115 /* Wait up to 30 seconds for the SLI Port POST done and ready */
8116 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05008117 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
8118 &portsmphr_reg.word0) ||
8119 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008120 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04008121 port_error = -ENODEV;
8122 break;
8123 }
James Smart2fcee4b2010-12-15 17:57:46 -05008124 if (LPFC_POST_STAGE_PORT_READY ==
8125 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04008126 break;
James Smartda0436e2009-05-22 14:51:39 -04008127 msleep(10);
8128 }
8129
James Smart2fcee4b2010-12-15 17:57:46 -05008130 /*
8131 * If there was a port error during POST, then don't proceed with
8132 * other register reads as the data may not be valid. Just exit.
8133 */
8134 if (port_error) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008135 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -05008136 "1408 Port Failed POST - portsmphr=0x%x, "
8137 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
8138 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
8139 portsmphr_reg.word0,
8140 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
8141 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
8142 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
8143 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
8144 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
8145 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
8146 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
8147 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
8148 } else {
James Smart28baac72010-02-12 14:42:03 -05008149 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05008150 "2534 Device Info: SLIFamily=0x%x, "
8151 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
8152 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05008153 bf_get(lpfc_sli_intf_sli_family,
8154 &phba->sli4_hba.sli_intf),
8155 bf_get(lpfc_sli_intf_slirev,
8156 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05008157 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05008158 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05008159 bf_get(lpfc_sli_intf_sli_hint1,
8160 &phba->sli4_hba.sli_intf),
8161 bf_get(lpfc_sli_intf_sli_hint2,
8162 &phba->sli4_hba.sli_intf),
8163 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05008164 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05008165 /*
8166 * Check for other Port errors during the initialization
8167 * process. Fail the load if the port did not come up
8168 * correctly.
8169 */
8170 if_type = bf_get(lpfc_sli_intf_if_type,
8171 &phba->sli4_hba.sli_intf);
8172 switch (if_type) {
8173 case LPFC_SLI_INTF_IF_TYPE_0:
8174 phba->sli4_hba.ue_mask_lo =
8175 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
8176 phba->sli4_hba.ue_mask_hi =
8177 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
8178 uerrlo_reg.word0 =
8179 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
8180 uerrhi_reg.word0 =
8181 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
8182 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
8183 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008184 lpfc_printf_log(phba, KERN_ERR,
8185 LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -05008186 "1422 Unrecoverable Error "
8187 "Detected during POST "
8188 "uerr_lo_reg=0x%x, "
8189 "uerr_hi_reg=0x%x, "
8190 "ue_mask_lo_reg=0x%x, "
8191 "ue_mask_hi_reg=0x%x\n",
8192 uerrlo_reg.word0,
8193 uerrhi_reg.word0,
8194 phba->sli4_hba.ue_mask_lo,
8195 phba->sli4_hba.ue_mask_hi);
8196 port_error = -ENODEV;
8197 }
8198 break;
8199 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08008200 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05008201 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05008202 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
8203 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04008204 (bf_get(lpfc_sliport_status_err, &reg_data) &&
8205 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008206 phba->work_status[0] =
8207 readl(phba->sli4_hba.u.if_type2.
8208 ERR1regaddr);
8209 phba->work_status[1] =
8210 readl(phba->sli4_hba.u.if_type2.
8211 ERR2regaddr);
Dick Kennedy372c1872020-06-30 14:50:00 -07008212 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8fcb8ac2012-03-01 22:35:58 -05008213 "2888 Unrecoverable port error "
8214 "following POST: port status reg "
8215 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05008216 "error 1=0x%x, error 2=0x%x\n",
8217 reg_data.word0,
8218 portsmphr_reg.word0,
8219 phba->work_status[0],
8220 phba->work_status[1]);
8221 port_error = -ENODEV;
8222 }
8223 break;
8224 case LPFC_SLI_INTF_IF_TYPE_1:
8225 default:
8226 break;
8227 }
James Smart28baac72010-02-12 14:42:03 -05008228 }
James Smartda0436e2009-05-22 14:51:39 -04008229 return port_error;
8230}
8231
8232/**
8233 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
8234 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05008235 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04008236 *
8237 * This routine is invoked to set up SLI4 BAR0 PCI config space register
8238 * memory map.
8239 **/
8240static void
James Smart2fcee4b2010-12-15 17:57:46 -05008241lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04008242{
James Smart2fcee4b2010-12-15 17:57:46 -05008243 switch (if_type) {
8244 case LPFC_SLI_INTF_IF_TYPE_0:
8245 phba->sli4_hba.u.if_type0.UERRLOregaddr =
8246 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
8247 phba->sli4_hba.u.if_type0.UERRHIregaddr =
8248 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
8249 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
8250 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
8251 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
8252 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
8253 phba->sli4_hba.SLIINTFregaddr =
8254 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
8255 break;
8256 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart0cf07f842017-06-01 21:07:10 -07008257 phba->sli4_hba.u.if_type2.EQDregaddr =
8258 phba->sli4_hba.conf_regs_memmap_p +
8259 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008260 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008261 phba->sli4_hba.conf_regs_memmap_p +
8262 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008263 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008264 phba->sli4_hba.conf_regs_memmap_p +
8265 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008266 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008267 phba->sli4_hba.conf_regs_memmap_p +
8268 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008269 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008270 phba->sli4_hba.conf_regs_memmap_p +
8271 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008272 phba->sli4_hba.SLIINTFregaddr =
8273 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
8274 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008275 phba->sli4_hba.conf_regs_memmap_p +
8276 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008277 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008278 phba->sli4_hba.conf_regs_memmap_p +
8279 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05008280 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008281 phba->sli4_hba.conf_regs_memmap_p +
8282 LPFC_ULP0_WQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008283 phba->sli4_hba.CQDBregaddr =
James Smart2fcee4b2010-12-15 17:57:46 -05008284 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008285 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smart2fcee4b2010-12-15 17:57:46 -05008286 phba->sli4_hba.MQDBregaddr =
8287 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
8288 phba->sli4_hba.BMBXregaddr =
8289 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8290 break;
James Smart27d6ac02018-02-22 08:18:42 -08008291 case LPFC_SLI_INTF_IF_TYPE_6:
8292 phba->sli4_hba.u.if_type2.EQDregaddr =
8293 phba->sli4_hba.conf_regs_memmap_p +
8294 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
8295 phba->sli4_hba.u.if_type2.ERR1regaddr =
8296 phba->sli4_hba.conf_regs_memmap_p +
8297 LPFC_CTL_PORT_ER1_OFFSET;
8298 phba->sli4_hba.u.if_type2.ERR2regaddr =
8299 phba->sli4_hba.conf_regs_memmap_p +
8300 LPFC_CTL_PORT_ER2_OFFSET;
8301 phba->sli4_hba.u.if_type2.CTRLregaddr =
8302 phba->sli4_hba.conf_regs_memmap_p +
8303 LPFC_CTL_PORT_CTL_OFFSET;
8304 phba->sli4_hba.u.if_type2.STATUSregaddr =
8305 phba->sli4_hba.conf_regs_memmap_p +
8306 LPFC_CTL_PORT_STA_OFFSET;
8307 phba->sli4_hba.PSMPHRregaddr =
8308 phba->sli4_hba.conf_regs_memmap_p +
8309 LPFC_CTL_PORT_SEM_OFFSET;
8310 phba->sli4_hba.BMBXregaddr =
8311 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8312 break;
James Smart2fcee4b2010-12-15 17:57:46 -05008313 case LPFC_SLI_INTF_IF_TYPE_1:
8314 default:
8315 dev_printk(KERN_ERR, &phba->pcidev->dev,
8316 "FATAL - unsupported SLI4 interface type - %d\n",
8317 if_type);
8318 break;
8319 }
James Smartda0436e2009-05-22 14:51:39 -04008320}
8321
8322/**
8323 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
8324 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01008325 * @if_type: sli if type to operate on.
James Smartda0436e2009-05-22 14:51:39 -04008326 *
James Smart27d6ac02018-02-22 08:18:42 -08008327 * This routine is invoked to set up SLI4 BAR1 register memory map.
James Smartda0436e2009-05-22 14:51:39 -04008328 **/
8329static void
James Smart27d6ac02018-02-22 08:18:42 -08008330lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04008331{
James Smart27d6ac02018-02-22 08:18:42 -08008332 switch (if_type) {
8333 case LPFC_SLI_INTF_IF_TYPE_0:
8334 phba->sli4_hba.PSMPHRregaddr =
8335 phba->sli4_hba.ctrl_regs_memmap_p +
8336 LPFC_SLIPORT_IF0_SMPHR;
8337 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8338 LPFC_HST_ISR0;
8339 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8340 LPFC_HST_IMR0;
8341 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8342 LPFC_HST_ISCR0;
8343 break;
8344 case LPFC_SLI_INTF_IF_TYPE_6:
8345 phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8346 LPFC_IF6_RQ_DOORBELL;
8347 phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8348 LPFC_IF6_WQ_DOORBELL;
8349 phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8350 LPFC_IF6_CQ_DOORBELL;
8351 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8352 LPFC_IF6_EQ_DOORBELL;
8353 phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8354 LPFC_IF6_MQ_DOORBELL;
8355 break;
8356 case LPFC_SLI_INTF_IF_TYPE_2:
8357 case LPFC_SLI_INTF_IF_TYPE_1:
8358 default:
8359 dev_err(&phba->pcidev->dev,
8360 "FATAL - unsupported SLI4 interface type - %d\n",
8361 if_type);
8362 break;
8363 }
James Smartda0436e2009-05-22 14:51:39 -04008364}
8365
8366/**
8367 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
8368 * @phba: pointer to lpfc hba data structure.
8369 * @vf: virtual function number
8370 *
8371 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
8372 * based on the given viftual function number, @vf.
8373 *
8374 * Return 0 if successful, otherwise -ENODEV.
8375 **/
8376static int
8377lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
8378{
8379 if (vf > LPFC_VIR_FUNC_MAX)
8380 return -ENODEV;
8381
8382 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008383 vf * LPFC_VFR_PAGE_SIZE +
8384 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04008385 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008386 vf * LPFC_VFR_PAGE_SIZE +
8387 LPFC_ULP0_WQ_DOORBELL);
James Smart9dd35422018-02-22 08:18:41 -08008388 phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8389 vf * LPFC_VFR_PAGE_SIZE +
8390 LPFC_EQCQ_DOORBELL);
8391 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smartda0436e2009-05-22 14:51:39 -04008392 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8393 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
8394 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8395 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
8396 return 0;
8397}
8398
8399/**
8400 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
8401 * @phba: pointer to lpfc hba data structure.
8402 *
8403 * This routine is invoked to create the bootstrap mailbox
8404 * region consistent with the SLI-4 interface spec. This
8405 * routine allocates all memory necessary to communicate
8406 * mailbox commands to the port and sets up all alignment
8407 * needs. No locks are expected to be held when calling
8408 * this routine.
8409 *
8410 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008411 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04008412 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04008413 **/
8414static int
8415lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
8416{
8417 uint32_t bmbx_size;
8418 struct lpfc_dmabuf *dmabuf;
8419 struct dma_address *dma_address;
8420 uint32_t pa_addr;
8421 uint64_t phys_addr;
8422
8423 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
8424 if (!dmabuf)
8425 return -ENOMEM;
8426
8427 /*
8428 * The bootstrap mailbox region is comprised of 2 parts
8429 * plus an alignment restriction of 16 bytes.
8430 */
8431 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Luis Chamberlain750afb02019-01-04 09:23:09 +01008432 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, bmbx_size,
8433 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04008434 if (!dmabuf->virt) {
8435 kfree(dmabuf);
8436 return -ENOMEM;
8437 }
James Smartda0436e2009-05-22 14:51:39 -04008438
8439 /*
8440 * Initialize the bootstrap mailbox pointers now so that the register
8441 * operations are simple later. The mailbox dma address is required
8442 * to be 16-byte aligned. Also align the virtual memory as each
8443 * maibox is copied into the bmbx mailbox region before issuing the
8444 * command to the port.
8445 */
8446 phba->sli4_hba.bmbx.dmabuf = dmabuf;
8447 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
8448
8449 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
8450 LPFC_ALIGN_16_BYTE);
8451 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
8452 LPFC_ALIGN_16_BYTE);
8453
8454 /*
8455 * Set the high and low physical addresses now. The SLI4 alignment
8456 * requirement is 16 bytes and the mailbox is posted to the port
8457 * as two 30-bit addresses. The other data is a bit marking whether
8458 * the 30-bit address is the high or low address.
8459 * Upcast bmbx aphys to 64bits so shift instruction compiles
8460 * clean on 32 bit machines.
8461 */
8462 dma_address = &phba->sli4_hba.bmbx.dma_address;
8463 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
8464 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
8465 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
8466 LPFC_BMBX_BIT1_ADDR_HI);
8467
8468 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
8469 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
8470 LPFC_BMBX_BIT1_ADDR_LO);
8471 return 0;
8472}
8473
8474/**
8475 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
8476 * @phba: pointer to lpfc hba data structure.
8477 *
8478 * This routine is invoked to teardown the bootstrap mailbox
8479 * region and release all host resources. This routine requires
8480 * the caller to ensure all mailbox commands recovered, no
8481 * additional mailbox comands are sent, and interrupts are disabled
8482 * before calling this routine.
8483 *
8484 **/
8485static void
8486lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
8487{
8488 dma_free_coherent(&phba->pcidev->dev,
8489 phba->sli4_hba.bmbx.bmbx_size,
8490 phba->sli4_hba.bmbx.dmabuf->virt,
8491 phba->sli4_hba.bmbx.dmabuf->phys);
8492
8493 kfree(phba->sli4_hba.bmbx.dmabuf);
8494 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
8495}
8496
James Smart83c6cb12019-10-18 14:18:30 -07008497static const char * const lpfc_topo_to_str[] = {
8498 "Loop then P2P",
8499 "Loopback",
8500 "P2P Only",
8501 "Unsupported",
8502 "Loop Only",
8503 "Unsupported",
8504 "P2P then Loop",
8505};
8506
Lee Jonesfe614ac2020-07-23 13:24:22 +01008507#define LINK_FLAGS_DEF 0x0
8508#define LINK_FLAGS_P2P 0x1
8509#define LINK_FLAGS_LOOP 0x2
James Smart83c6cb12019-10-18 14:18:30 -07008510/**
8511 * lpfc_map_topology - Map the topology read from READ_CONFIG
8512 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01008513 * @rd_config: pointer to read config data
James Smart83c6cb12019-10-18 14:18:30 -07008514 *
8515 * This routine is invoked to map the topology values as read
8516 * from the read config mailbox command. If the persistent
8517 * topology feature is supported, the firmware will provide the
8518 * saved topology information to be used in INIT_LINK
James Smart83c6cb12019-10-18 14:18:30 -07008519 **/
James Smart83c6cb12019-10-18 14:18:30 -07008520static void
8521lpfc_map_topology(struct lpfc_hba *phba, struct lpfc_mbx_read_config *rd_config)
8522{
8523 u8 ptv, tf, pt;
8524
8525 ptv = bf_get(lpfc_mbx_rd_conf_ptv, rd_config);
8526 tf = bf_get(lpfc_mbx_rd_conf_tf, rd_config);
8527 pt = bf_get(lpfc_mbx_rd_conf_pt, rd_config);
8528
8529 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8530 "2027 Read Config Data : ptv:0x%x, tf:0x%x pt:0x%x",
8531 ptv, tf, pt);
8532 if (!ptv) {
8533 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8534 "2019 FW does not support persistent topology "
8535 "Using driver parameter defined value [%s]",
8536 lpfc_topo_to_str[phba->cfg_topology]);
8537 return;
8538 }
8539 /* FW supports persistent topology - override module parameter value */
8540 phba->hba_flag |= HBA_PERSISTENT_TOPO;
8541 switch (phba->pcidev->device) {
8542 case PCI_DEVICE_ID_LANCER_G7_FC:
James Smart83c6cb12019-10-18 14:18:30 -07008543 case PCI_DEVICE_ID_LANCER_G6_FC:
8544 if (!tf) {
8545 phba->cfg_topology = ((pt == LINK_FLAGS_LOOP)
8546 ? FLAGS_TOPOLOGY_MODE_LOOP
8547 : FLAGS_TOPOLOGY_MODE_PT_PT);
8548 } else {
8549 phba->hba_flag &= ~HBA_PERSISTENT_TOPO;
8550 }
8551 break;
8552 default: /* G5 */
8553 if (tf) {
8554 /* If topology failover set - pt is '0' or '1' */
8555 phba->cfg_topology = (pt ? FLAGS_TOPOLOGY_MODE_PT_LOOP :
8556 FLAGS_TOPOLOGY_MODE_LOOP_PT);
8557 } else {
8558 phba->cfg_topology = ((pt == LINK_FLAGS_P2P)
8559 ? FLAGS_TOPOLOGY_MODE_PT_PT
8560 : FLAGS_TOPOLOGY_MODE_LOOP);
8561 }
8562 break;
8563 }
8564 if (phba->hba_flag & HBA_PERSISTENT_TOPO) {
8565 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8566 "2020 Using persistent topology value [%s]",
8567 lpfc_topo_to_str[phba->cfg_topology]);
8568 } else {
8569 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8570 "2021 Invalid topology values from FW "
8571 "Using driver parameter defined value [%s]",
8572 lpfc_topo_to_str[phba->cfg_topology]);
8573 }
8574}
8575
James Smartda0436e2009-05-22 14:51:39 -04008576/**
8577 * lpfc_sli4_read_config - Get the config parameters.
8578 * @phba: pointer to lpfc hba data structure.
8579 *
8580 * This routine is invoked to read the configuration parameters from the HBA.
8581 * The configuration parameters are used to set the base and maximum values
8582 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
8583 * allocation for the port.
8584 *
8585 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008586 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008587 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008588 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008589 **/
James Smartff78d8f2011-12-13 13:21:35 -05008590int
James Smartda0436e2009-05-22 14:51:39 -04008591lpfc_sli4_read_config(struct lpfc_hba *phba)
8592{
8593 LPFC_MBOXQ_t *pmb;
8594 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04008595 union lpfc_sli4_cfg_shdr *shdr;
8596 uint32_t shdr_status, shdr_add_status;
8597 struct lpfc_mbx_get_func_cfg *get_func_cfg;
8598 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04008599 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07008600 uint16_t forced_link_speed;
James Smart6a828b02019-01-28 11:14:31 -08008601 uint32_t if_type, qmin;
James Smart8aa134a2012-08-14 14:25:29 -04008602 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04008603
8604 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8605 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008606 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008607 "2011 Unable to allocate memory for issuing "
8608 "SLI_CONFIG_SPECIAL mailbox command\n");
8609 return -ENOMEM;
8610 }
8611
8612 lpfc_read_config(phba, pmb);
8613
8614 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8615 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008616 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8617 "2012 Mailbox failed , mbxCmd x%x "
8618 "READ_CONFIG, mbxStatus x%x\n",
8619 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8620 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smartda0436e2009-05-22 14:51:39 -04008621 rc = -EIO;
8622 } else {
8623 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05008624 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
8625 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
8626 phba->sli4_hba.lnk_info.lnk_tp =
8627 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
8628 phba->sli4_hba.lnk_info.lnk_no =
8629 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
8630 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8631 "3081 lnk_type:%d, lnk_numb:%d\n",
8632 phba->sli4_hba.lnk_info.lnk_tp,
8633 phba->sli4_hba.lnk_info.lnk_no);
8634 } else
8635 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8636 "3082 Mailbox (x%x) returned ldv:x0\n",
8637 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart44fd7fe2017-08-23 16:55:47 -07008638 if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
8639 phba->bbcredit_support = 1;
8640 phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
8641 }
8642
James Smart1dc5ec22018-10-23 13:41:11 -07008643 phba->sli4_hba.conf_trunk =
8644 bf_get(lpfc_mbx_rd_conf_trunk, rd_config);
James Smart6d368e52011-05-24 11:44:12 -04008645 phba->sli4_hba.extents_in_use =
8646 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008647 phba->sli4_hba.max_cfg_param.max_xri =
8648 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
James Smart31f06d22019-08-14 16:56:31 -07008649 /* Reduce resource usage in kdump environment */
8650 if (is_kdump_kernel() &&
8651 phba->sli4_hba.max_cfg_param.max_xri > 512)
8652 phba->sli4_hba.max_cfg_param.max_xri = 512;
James Smartda0436e2009-05-22 14:51:39 -04008653 phba->sli4_hba.max_cfg_param.xri_base =
8654 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
8655 phba->sli4_hba.max_cfg_param.max_vpi =
8656 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
James Smart8b47ae62018-11-29 16:09:33 -08008657 /* Limit the max we support */
8658 if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
8659 phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
James Smartda0436e2009-05-22 14:51:39 -04008660 phba->sli4_hba.max_cfg_param.vpi_base =
8661 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
8662 phba->sli4_hba.max_cfg_param.max_rpi =
8663 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
8664 phba->sli4_hba.max_cfg_param.rpi_base =
8665 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
8666 phba->sli4_hba.max_cfg_param.max_vfi =
8667 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
8668 phba->sli4_hba.max_cfg_param.vfi_base =
8669 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
8670 phba->sli4_hba.max_cfg_param.max_fcfi =
8671 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008672 phba->sli4_hba.max_cfg_param.max_eq =
8673 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
8674 phba->sli4_hba.max_cfg_param.max_rq =
8675 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
8676 phba->sli4_hba.max_cfg_param.max_wq =
8677 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
8678 phba->sli4_hba.max_cfg_param.max_cq =
8679 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
8680 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
8681 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
8682 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
8683 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05008684 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
8685 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04008686 phba->max_vports = phba->max_vpi;
James Smart83c6cb12019-10-18 14:18:30 -07008687 lpfc_map_topology(phba, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008688 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04008689 "2003 cfg params Extents? %d "
8690 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04008691 "VPI(B:%d M:%d) "
8692 "VFI(B:%d M:%d) "
8693 "RPI(B:%d M:%d) "
Dick Kennedya1e4d3d2020-08-03 14:02:22 -07008694 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d lmt:x%x\n",
James Smart6d368e52011-05-24 11:44:12 -04008695 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04008696 phba->sli4_hba.max_cfg_param.xri_base,
8697 phba->sli4_hba.max_cfg_param.max_xri,
8698 phba->sli4_hba.max_cfg_param.vpi_base,
8699 phba->sli4_hba.max_cfg_param.max_vpi,
8700 phba->sli4_hba.max_cfg_param.vfi_base,
8701 phba->sli4_hba.max_cfg_param.max_vfi,
8702 phba->sli4_hba.max_cfg_param.rpi_base,
8703 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08008704 phba->sli4_hba.max_cfg_param.max_fcfi,
8705 phba->sli4_hba.max_cfg_param.max_eq,
8706 phba->sli4_hba.max_cfg_param.max_cq,
8707 phba->sli4_hba.max_cfg_param.max_wq,
Dick Kennedya1e4d3d2020-08-03 14:02:22 -07008708 phba->sli4_hba.max_cfg_param.max_rq,
8709 phba->lmt);
James Smart2ea259e2017-02-12 13:52:27 -08008710
James Smartd38f33b2018-05-04 20:37:54 -07008711 /*
James Smart6a828b02019-01-28 11:14:31 -08008712 * Calculate queue resources based on how
8713 * many WQ/CQ/EQs are available.
James Smartd38f33b2018-05-04 20:37:54 -07008714 */
James Smart6a828b02019-01-28 11:14:31 -08008715 qmin = phba->sli4_hba.max_cfg_param.max_wq;
8716 if (phba->sli4_hba.max_cfg_param.max_cq < qmin)
8717 qmin = phba->sli4_hba.max_cfg_param.max_cq;
8718 if (phba->sli4_hba.max_cfg_param.max_eq < qmin)
8719 qmin = phba->sli4_hba.max_cfg_param.max_eq;
8720 /*
8721 * Whats left after this can go toward NVME / FCP.
8722 * The minus 4 accounts for ELS, NVME LS, MBOX
8723 * plus one extra. When configured for
8724 * NVMET, FCP io channel WQs are not created.
8725 */
8726 qmin -= 4;
James Smartd38f33b2018-05-04 20:37:54 -07008727
James Smart6a828b02019-01-28 11:14:31 -08008728 /* Check to see if there is enough for NVME */
8729 if ((phba->cfg_irq_chann > qmin) ||
8730 (phba->cfg_hdw_queue > qmin)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008731 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy9e3e3652020-08-03 14:02:23 -07008732 "2005 Reducing Queues - "
8733 "FW resource limitation: "
James Smart6a828b02019-01-28 11:14:31 -08008734 "WQ %d CQ %d EQ %d: min %d: "
8735 "IRQ %d HDWQ %d\n",
James Smartd38f33b2018-05-04 20:37:54 -07008736 phba->sli4_hba.max_cfg_param.max_wq,
8737 phba->sli4_hba.max_cfg_param.max_cq,
James Smart6a828b02019-01-28 11:14:31 -08008738 phba->sli4_hba.max_cfg_param.max_eq,
8739 qmin, phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08008740 phba->cfg_hdw_queue);
James Smartd38f33b2018-05-04 20:37:54 -07008741
James Smart6a828b02019-01-28 11:14:31 -08008742 if (phba->cfg_irq_chann > qmin)
8743 phba->cfg_irq_chann = qmin;
8744 if (phba->cfg_hdw_queue > qmin)
8745 phba->cfg_hdw_queue = qmin;
James Smartd38f33b2018-05-04 20:37:54 -07008746 }
James Smartda0436e2009-05-22 14:51:39 -04008747 }
James Smart912e3ac2011-05-24 11:42:11 -04008748
8749 if (rc)
8750 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04008751
James Smartc6918162016-10-13 15:06:16 -07008752 /* Update link speed if forced link speed is supported */
8753 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08008754 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartc6918162016-10-13 15:06:16 -07008755 forced_link_speed =
8756 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
8757 if (forced_link_speed) {
8758 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
8759
8760 switch (forced_link_speed) {
8761 case LINK_SPEED_1G:
8762 phba->cfg_link_speed =
8763 LPFC_USER_LINK_SPEED_1G;
8764 break;
8765 case LINK_SPEED_2G:
8766 phba->cfg_link_speed =
8767 LPFC_USER_LINK_SPEED_2G;
8768 break;
8769 case LINK_SPEED_4G:
8770 phba->cfg_link_speed =
8771 LPFC_USER_LINK_SPEED_4G;
8772 break;
8773 case LINK_SPEED_8G:
8774 phba->cfg_link_speed =
8775 LPFC_USER_LINK_SPEED_8G;
8776 break;
8777 case LINK_SPEED_10G:
8778 phba->cfg_link_speed =
8779 LPFC_USER_LINK_SPEED_10G;
8780 break;
8781 case LINK_SPEED_16G:
8782 phba->cfg_link_speed =
8783 LPFC_USER_LINK_SPEED_16G;
8784 break;
8785 case LINK_SPEED_32G:
8786 phba->cfg_link_speed =
8787 LPFC_USER_LINK_SPEED_32G;
8788 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08008789 case LINK_SPEED_64G:
8790 phba->cfg_link_speed =
8791 LPFC_USER_LINK_SPEED_64G;
8792 break;
James Smartc6918162016-10-13 15:06:16 -07008793 case 0xffff:
8794 phba->cfg_link_speed =
8795 LPFC_USER_LINK_SPEED_AUTO;
8796 break;
8797 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07008798 lpfc_printf_log(phba, KERN_ERR,
8799 LOG_TRACE_EVENT,
James Smartc6918162016-10-13 15:06:16 -07008800 "0047 Unrecognized link "
8801 "speed : %d\n",
8802 forced_link_speed);
8803 phba->cfg_link_speed =
8804 LPFC_USER_LINK_SPEED_AUTO;
8805 }
8806 }
8807 }
8808
James Smartda0436e2009-05-22 14:51:39 -04008809 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04008810 length = phba->sli4_hba.max_cfg_param.max_xri -
8811 lpfc_sli4_get_els_iocb_cnt(phba);
8812 if (phba->cfg_hba_queue_depth > length) {
8813 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8814 "3361 HBA queue depth changed from %d to %d\n",
8815 phba->cfg_hba_queue_depth, length);
8816 phba->cfg_hba_queue_depth = length;
8817 }
James Smart912e3ac2011-05-24 11:42:11 -04008818
James Smart27d6ac02018-02-22 08:18:42 -08008819 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart912e3ac2011-05-24 11:42:11 -04008820 LPFC_SLI_INTF_IF_TYPE_2)
8821 goto read_cfg_out;
8822
8823 /* get the pf# and vf# for SLI4 if_type 2 port */
8824 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
8825 sizeof(struct lpfc_sli4_cfg_mhdr));
8826 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
8827 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
8828 length, LPFC_SLI4_MBX_EMBED);
8829
James Smart8aa134a2012-08-14 14:25:29 -04008830 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04008831 shdr = (union lpfc_sli4_cfg_shdr *)
8832 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
8833 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8834 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04008835 if (rc2 || shdr_status || shdr_add_status) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008836 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart912e3ac2011-05-24 11:42:11 -04008837 "3026 Mailbox failed , mbxCmd x%x "
8838 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
8839 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8840 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04008841 goto read_cfg_out;
8842 }
8843
8844 /* search for fc_fcoe resrouce descriptor */
8845 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04008846
James Smart8aa134a2012-08-14 14:25:29 -04008847 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
8848 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
8849 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
8850 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
8851 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
8852 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
8853 goto read_cfg_out;
8854
James Smart912e3ac2011-05-24 11:42:11 -04008855 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04008856 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04008857 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04008858 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04008859 phba->sli4_hba.iov.pf_number =
8860 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
8861 phba->sli4_hba.iov.vf_number =
8862 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
8863 break;
8864 }
8865 }
8866
8867 if (i < LPFC_RSRC_DESC_MAX_NUM)
8868 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8869 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
8870 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
8871 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04008872 else
Dick Kennedy372c1872020-06-30 14:50:00 -07008873 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart912e3ac2011-05-24 11:42:11 -04008874 "3028 GET_FUNCTION_CONFIG: failed to find "
Colin Ian Kingc4dba182018-10-16 18:28:53 +01008875 "Resource Descriptor:x%x\n",
James Smart912e3ac2011-05-24 11:42:11 -04008876 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04008877
8878read_cfg_out:
8879 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008880 return rc;
8881}
8882
8883/**
James Smart2fcee4b2010-12-15 17:57:46 -05008884 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04008885 * @phba: pointer to lpfc hba data structure.
8886 *
James Smart2fcee4b2010-12-15 17:57:46 -05008887 * This routine is invoked to setup the port-side endian order when
8888 * the port if_type is 0. This routine has no function for other
8889 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04008890 *
8891 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008892 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008893 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008894 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008895 **/
8896static int
8897lpfc_setup_endian_order(struct lpfc_hba *phba)
8898{
8899 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05008900 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04008901 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
8902 HOST_ENDIAN_HIGH_WORD1};
8903
James Smart2fcee4b2010-12-15 17:57:46 -05008904 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8905 switch (if_type) {
8906 case LPFC_SLI_INTF_IF_TYPE_0:
8907 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8908 GFP_KERNEL);
8909 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008910 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -05008911 "0492 Unable to allocate memory for "
8912 "issuing SLI_CONFIG_SPECIAL mailbox "
8913 "command\n");
8914 return -ENOMEM;
8915 }
James Smartda0436e2009-05-22 14:51:39 -04008916
James Smart2fcee4b2010-12-15 17:57:46 -05008917 /*
8918 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
8919 * two words to contain special data values and no other data.
8920 */
8921 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
8922 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
8923 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8924 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008925 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -05008926 "0493 SLI_CONFIG_SPECIAL mailbox "
8927 "failed with status x%x\n",
8928 rc);
8929 rc = -EIO;
8930 }
8931 mempool_free(mboxq, phba->mbox_mem_pool);
8932 break;
James Smart27d6ac02018-02-22 08:18:42 -08008933 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05008934 case LPFC_SLI_INTF_IF_TYPE_2:
8935 case LPFC_SLI_INTF_IF_TYPE_1:
8936 default:
8937 break;
James Smartda0436e2009-05-22 14:51:39 -04008938 }
James Smartda0436e2009-05-22 14:51:39 -04008939 return rc;
8940}
8941
8942/**
James Smart895427b2017-02-12 13:52:30 -08008943 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04008944 * @phba: pointer to lpfc hba data structure.
8945 *
James Smart895427b2017-02-12 13:52:30 -08008946 * This routine is invoked to check the user settable queue counts for EQs.
8947 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04008948 * adhere to the constraints of the system's interrupt vectors and the port's
8949 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04008950 *
8951 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008952 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008953 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04008954 **/
8955static int
James Smart5350d872011-10-10 21:33:49 -04008956lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04008957{
James Smartda0436e2009-05-22 14:51:39 -04008958 /*
James Smart67d12732012-08-03 12:36:13 -04008959 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04008960 * device parameters
8961 */
8962
James Smartbcb24f62017-11-20 16:00:36 -08008963 if (phba->nvmet_support) {
James Smart97a9ed32019-10-18 14:18:17 -07008964 if (phba->cfg_hdw_queue < phba->cfg_nvmet_mrq)
8965 phba->cfg_nvmet_mrq = phba->cfg_hdw_queue;
James Smart982ab122019-03-12 16:30:10 -07008966 if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
8967 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
James Smartbcb24f62017-11-20 16:00:36 -08008968 }
James Smart895427b2017-02-12 13:52:30 -08008969
8970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -08008971 "2574 IO channels: hdwQ %d IRQ %d MRQ: %d\n",
8972 phba->cfg_hdw_queue, phba->cfg_irq_chann,
8973 phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -04008974
James Smartda0436e2009-05-22 14:51:39 -04008975 /* Get EQ depth from module parameter, fake the default for now */
8976 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8977 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8978
James Smart5350d872011-10-10 21:33:49 -04008979 /* Get CQ depth from module parameter, fake the default for now */
8980 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8981 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04008982 return 0;
James Smart895427b2017-02-12 13:52:30 -08008983}
8984
8985static int
James Smartc00f62e2019-08-14 16:57:11 -07008986lpfc_alloc_io_wq_cq(struct lpfc_hba *phba, int idx)
James Smart895427b2017-02-12 13:52:30 -08008987{
8988 struct lpfc_queue *qdesc;
James Smartc00f62e2019-08-14 16:57:11 -07008989 u32 wqesize;
James Smartc1a21eb2019-03-12 16:30:29 -07008990 int cpu;
James Smart895427b2017-02-12 13:52:30 -08008991
James Smartc00f62e2019-08-14 16:57:11 -07008992 cpu = lpfc_find_cpu_handle(phba, idx, LPFC_FIND_BY_HDWQ);
8993 /* Create Fast Path IO CQs */
James Smartc176ffa2018-01-30 15:58:46 -08008994 if (phba->enab_exp_wqcq_pages)
James Smarta51e41b2017-12-08 17:18:06 -08008995 /* Increase the CQ size when WQEs contain an embedded cdb */
8996 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8997 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008998 LPFC_CQE_EXP_COUNT, cpu);
James Smarta51e41b2017-12-08 17:18:06 -08008999
9000 else
9001 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9002 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009003 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08009004 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009005 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9006 "0499 Failed allocate fast-path IO CQ (%d)\n",
9007 idx);
James Smart895427b2017-02-12 13:52:30 -08009008 return 1;
9009 }
James Smart7365f6f2018-02-22 08:18:46 -08009010 qdesc->qe_valid = 1;
James Smartc00f62e2019-08-14 16:57:11 -07009011 qdesc->hdwq = idx;
James Smartc1a21eb2019-03-12 16:30:29 -07009012 qdesc->chann = cpu;
James Smartc00f62e2019-08-14 16:57:11 -07009013 phba->sli4_hba.hdwq[idx].io_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08009014
James Smartc00f62e2019-08-14 16:57:11 -07009015 /* Create Fast Path IO WQs */
James Smartc176ffa2018-01-30 15:58:46 -08009016 if (phba->enab_exp_wqcq_pages) {
James Smarta51e41b2017-12-08 17:18:06 -08009017 /* Increase the WQ size when WQEs contain an embedded cdb */
James Smartc176ffa2018-01-30 15:58:46 -08009018 wqesize = (phba->fcp_embed_io) ?
9019 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smarta51e41b2017-12-08 17:18:06 -08009020 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc176ffa2018-01-30 15:58:46 -08009021 wqesize,
James Smartc1a21eb2019-03-12 16:30:29 -07009022 LPFC_WQE_EXP_COUNT, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08009023 } else
James Smarta51e41b2017-12-08 17:18:06 -08009024 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9025 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009026 phba->sli4_hba.wq_ecount, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08009027
James Smart895427b2017-02-12 13:52:30 -08009028 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009029 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartc00f62e2019-08-14 16:57:11 -07009030 "0503 Failed allocate fast-path IO WQ (%d)\n",
9031 idx);
James Smart895427b2017-02-12 13:52:30 -08009032 return 1;
9033 }
James Smartc00f62e2019-08-14 16:57:11 -07009034 qdesc->hdwq = idx;
9035 qdesc->chann = cpu;
9036 phba->sli4_hba.hdwq[idx].io_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08009037 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
9038 return 0;
James Smart5350d872011-10-10 21:33:49 -04009039}
9040
9041/**
9042 * lpfc_sli4_queue_create - Create all the SLI4 queues
9043 * @phba: pointer to lpfc hba data structure.
9044 *
9045 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
9046 * operation. For each SLI4 queue type, the parameters such as queue entry
9047 * count (queue depth) shall be taken from the module parameter. For now,
9048 * we just use some constant number as place holder.
9049 *
9050 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07009051 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04009052 * -ENOMEM - No availble memory
9053 * -EIO - The mailbox failed to complete successfully.
9054 **/
9055int
9056lpfc_sli4_queue_create(struct lpfc_hba *phba)
9057{
9058 struct lpfc_queue *qdesc;
James Smart657add42019-05-21 17:49:06 -07009059 int idx, cpu, eqcpu;
James Smart5e5b5112019-01-28 11:14:22 -08009060 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07009061 struct lpfc_vector_map_info *cpup;
9062 struct lpfc_vector_map_info *eqcpup;
James Smart32517fc2019-01-28 11:14:33 -08009063 struct lpfc_eq_intr_info *eqi;
James Smart5350d872011-10-10 21:33:49 -04009064
9065 /*
James Smart67d12732012-08-03 12:36:13 -04009066 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08009067 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04009068 */
James Smart67d12732012-08-03 12:36:13 -04009069 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
9070 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
9071 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
9072 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
9073 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
9074 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08009075 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
9076 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
9077 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
9078 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04009079
James Smartcdb42be2019-01-28 11:14:21 -08009080 if (!phba->sli4_hba.hdwq) {
James Smart5e5b5112019-01-28 11:14:22 -08009081 phba->sli4_hba.hdwq = kcalloc(
9082 phba->cfg_hdw_queue, sizeof(struct lpfc_sli4_hdw_queue),
9083 GFP_KERNEL);
9084 if (!phba->sli4_hba.hdwq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009085 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5e5b5112019-01-28 11:14:22 -08009086 "6427 Failed allocate memory for "
9087 "fast-path Hardware Queue array\n");
James Smart895427b2017-02-12 13:52:30 -08009088 goto out_error;
9089 }
James Smart5e5b5112019-01-28 11:14:22 -08009090 /* Prepare hardware queues to take IO buffers */
9091 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9092 qp = &phba->sli4_hba.hdwq[idx];
9093 spin_lock_init(&qp->io_buf_list_get_lock);
9094 spin_lock_init(&qp->io_buf_list_put_lock);
9095 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
9096 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
9097 qp->get_io_bufs = 0;
9098 qp->put_io_bufs = 0;
9099 qp->total_io_bufs = 0;
James Smartc00f62e2019-08-14 16:57:11 -07009100 spin_lock_init(&qp->abts_io_buf_list_lock);
9101 INIT_LIST_HEAD(&qp->lpfc_abts_io_buf_list);
James Smart5e5b5112019-01-28 11:14:22 -08009102 qp->abts_scsi_io_bufs = 0;
James Smart5e5b5112019-01-28 11:14:22 -08009103 qp->abts_nvme_io_bufs = 0;
James Smartd79c9e92019-08-14 16:57:09 -07009104 INIT_LIST_HEAD(&qp->sgl_list);
9105 INIT_LIST_HEAD(&qp->cmd_rsp_buf_list);
9106 spin_lock_init(&qp->hdwq_lock);
James Smart895427b2017-02-12 13:52:30 -08009107 }
James Smart67d12732012-08-03 12:36:13 -04009108 }
9109
James Smartcdb42be2019-01-28 11:14:21 -08009110 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -08009111 if (phba->nvmet_support) {
9112 phba->sli4_hba.nvmet_cqset = kcalloc(
9113 phba->cfg_nvmet_mrq,
9114 sizeof(struct lpfc_queue *),
9115 GFP_KERNEL);
9116 if (!phba->sli4_hba.nvmet_cqset) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009117 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009118 "3121 Fail allocate memory for "
9119 "fast-path CQ set array\n");
9120 goto out_error;
9121 }
9122 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
9123 phba->cfg_nvmet_mrq,
9124 sizeof(struct lpfc_queue *),
9125 GFP_KERNEL);
9126 if (!phba->sli4_hba.nvmet_mrq_hdr) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009127 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009128 "3122 Fail allocate memory for "
9129 "fast-path RQ set hdr array\n");
9130 goto out_error;
9131 }
9132 phba->sli4_hba.nvmet_mrq_data = kcalloc(
9133 phba->cfg_nvmet_mrq,
9134 sizeof(struct lpfc_queue *),
9135 GFP_KERNEL);
9136 if (!phba->sli4_hba.nvmet_mrq_data) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009137 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009138 "3124 Fail allocate memory for "
9139 "fast-path RQ set data array\n");
9140 goto out_error;
9141 }
9142 }
James Smart67d12732012-08-03 12:36:13 -04009143 }
James Smartda0436e2009-05-22 14:51:39 -04009144
James Smart895427b2017-02-12 13:52:30 -08009145 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04009146
James Smart895427b2017-02-12 13:52:30 -08009147 /* Create HBA Event Queues (EQs) */
James Smart657add42019-05-21 17:49:06 -07009148 for_each_present_cpu(cpu) {
9149 /* We only want to create 1 EQ per vector, even though
9150 * multiple CPUs might be using that vector. so only
9151 * selects the CPUs that are LPFC_CPU_FIRST_IRQ.
James Smart6a828b02019-01-28 11:14:31 -08009152 */
James Smart657add42019-05-21 17:49:06 -07009153 cpup = &phba->sli4_hba.cpu_map[cpu];
9154 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
James Smart6a828b02019-01-28 11:14:31 -08009155 continue;
James Smart657add42019-05-21 17:49:06 -07009156
9157 /* Get a ptr to the Hardware Queue associated with this CPU */
9158 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
9159
9160 /* Allocate an EQ */
James Smart81b96ed2017-11-20 16:00:29 -08009161 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9162 phba->sli4_hba.eq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009163 phba->sli4_hba.eq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009164 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009165 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart657add42019-05-21 17:49:06 -07009166 "0497 Failed allocate EQ (%d)\n",
9167 cpup->hdwq);
James Smart67d12732012-08-03 12:36:13 -04009168 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009169 }
James Smart7365f6f2018-02-22 08:18:46 -08009170 qdesc->qe_valid = 1;
James Smart657add42019-05-21 17:49:06 -07009171 qdesc->hdwq = cpup->hdwq;
James Smart3ad348d2019-08-14 16:56:43 -07009172 qdesc->chann = cpu; /* First CPU this EQ is affinitized to */
James Smart32517fc2019-01-28 11:14:33 -08009173 qdesc->last_cpu = qdesc->chann;
James Smart657add42019-05-21 17:49:06 -07009174
9175 /* Save the allocated EQ in the Hardware Queue */
9176 qp->hba_eq = qdesc;
9177
James Smart32517fc2019-01-28 11:14:33 -08009178 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, qdesc->last_cpu);
9179 list_add(&qdesc->cpu_list, &eqi->list);
James Smartda0436e2009-05-22 14:51:39 -04009180 }
9181
James Smart657add42019-05-21 17:49:06 -07009182 /* Now we need to populate the other Hardware Queues, that share
9183 * an IRQ vector, with the associated EQ ptr.
9184 */
9185 for_each_present_cpu(cpu) {
9186 cpup = &phba->sli4_hba.cpu_map[cpu];
9187
9188 /* Check for EQ already allocated in previous loop */
9189 if (cpup->flag & LPFC_CPU_FIRST_IRQ)
9190 continue;
9191
9192 /* Check for multiple CPUs per hdwq */
9193 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
9194 if (qp->hba_eq)
9195 continue;
9196
9197 /* We need to share an EQ for this hdwq */
9198 eqcpu = lpfc_find_cpu_handle(phba, cpup->eq, LPFC_FIND_BY_EQ);
9199 eqcpup = &phba->sli4_hba.cpu_map[eqcpu];
9200 qp->hba_eq = phba->sli4_hba.hdwq[eqcpup->hdwq].hba_eq;
9201 }
James Smart895427b2017-02-12 13:52:30 -08009202
James Smartc00f62e2019-08-14 16:57:11 -07009203 /* Allocate IO Path SLI4 CQ/WQs */
James Smart6a828b02019-01-28 11:14:31 -08009204 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smartc00f62e2019-08-14 16:57:11 -07009205 if (lpfc_alloc_io_wq_cq(phba, idx))
James Smart895427b2017-02-12 13:52:30 -08009206 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -08009207 }
James Smart895427b2017-02-12 13:52:30 -08009208
James Smartc00f62e2019-08-14 16:57:11 -07009209 if (phba->nvmet_support) {
9210 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
9211 cpu = lpfc_find_cpu_handle(phba, idx,
9212 LPFC_FIND_BY_HDWQ);
9213 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009214 LPFC_DEFAULT_PAGE_SIZE,
9215 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009216 phba->sli4_hba.cq_ecount,
9217 cpu);
James Smartc00f62e2019-08-14 16:57:11 -07009218 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009219 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcdb42be2019-01-28 11:14:21 -08009220 "3142 Failed allocate NVME "
9221 "CQ Set (%d)\n", idx);
James Smartc00f62e2019-08-14 16:57:11 -07009222 goto out_error;
James Smart2d7dbc42017-02-12 13:52:35 -08009223 }
James Smartc00f62e2019-08-14 16:57:11 -07009224 qdesc->qe_valid = 1;
9225 qdesc->hdwq = idx;
9226 qdesc->chann = cpu;
9227 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
James Smart2d7dbc42017-02-12 13:52:35 -08009228 }
9229 }
9230
James Smartda0436e2009-05-22 14:51:39 -04009231 /*
James Smart67d12732012-08-03 12:36:13 -04009232 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04009233 */
9234
James Smartc1a21eb2019-03-12 16:30:29 -07009235 cpu = lpfc_find_cpu_handle(phba, 0, LPFC_FIND_BY_EQ);
James Smartda0436e2009-05-22 14:51:39 -04009236 /* Create slow-path Mailbox Command Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009237 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9238 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009239 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009240 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009241 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009242 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04009243 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009244 }
James Smart7365f6f2018-02-22 08:18:46 -08009245 qdesc->qe_valid = 1;
James Smartda0436e2009-05-22 14:51:39 -04009246 phba->sli4_hba.mbx_cq = qdesc;
9247
9248 /* Create slow-path ELS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009249 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9250 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009251 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009252 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009253 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009254 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04009255 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009256 }
James Smart7365f6f2018-02-22 08:18:46 -08009257 qdesc->qe_valid = 1;
James Smartc00f62e2019-08-14 16:57:11 -07009258 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -04009259 phba->sli4_hba.els_cq = qdesc;
9260
James Smartda0436e2009-05-22 14:51:39 -04009261
James Smart5350d872011-10-10 21:33:49 -04009262 /*
James Smart67d12732012-08-03 12:36:13 -04009263 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04009264 */
James Smartda0436e2009-05-22 14:51:39 -04009265
9266 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04009267
James Smart81b96ed2017-11-20 16:00:29 -08009268 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9269 phba->sli4_hba.mq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009270 phba->sli4_hba.mq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009271 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009272 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009273 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04009274 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009275 }
James Smartc00f62e2019-08-14 16:57:11 -07009276 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -04009277 phba->sli4_hba.mbx_wq = qdesc;
9278
9279 /*
James Smart67d12732012-08-03 12:36:13 -04009280 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04009281 */
James Smartda0436e2009-05-22 14:51:39 -04009282
9283 /* Create slow-path ELS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009284 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9285 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009286 phba->sli4_hba.wq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009287 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009288 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009289 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04009290 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009291 }
James Smartc00f62e2019-08-14 16:57:11 -07009292 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -04009293 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08009294 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
9295
9296 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9297 /* Create NVME LS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009298 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9299 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009300 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08009301 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009302 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009303 "6079 Failed allocate NVME LS CQ\n");
9304 goto out_error;
9305 }
James Smartc00f62e2019-08-14 16:57:11 -07009306 qdesc->chann = cpu;
James Smart7365f6f2018-02-22 08:18:46 -08009307 qdesc->qe_valid = 1;
James Smart895427b2017-02-12 13:52:30 -08009308 phba->sli4_hba.nvmels_cq = qdesc;
9309
9310 /* Create NVME LS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009311 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9312 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009313 phba->sli4_hba.wq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08009314 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009315 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009316 "6080 Failed allocate NVME LS WQ\n");
9317 goto out_error;
9318 }
James Smartc00f62e2019-08-14 16:57:11 -07009319 qdesc->chann = cpu;
James Smart895427b2017-02-12 13:52:30 -08009320 phba->sli4_hba.nvmels_wq = qdesc;
9321 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
9322 }
James Smartda0436e2009-05-22 14:51:39 -04009323
James Smartda0436e2009-05-22 14:51:39 -04009324 /*
9325 * Create Receive Queue (RQ)
9326 */
James Smartda0436e2009-05-22 14:51:39 -04009327
9328 /* Create Receive Queue for header */
James Smart81b96ed2017-11-20 16:00:29 -08009329 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9330 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009331 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009332 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009333 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009334 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009335 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009336 }
9337 phba->sli4_hba.hdr_rq = qdesc;
9338
9339 /* Create Receive Queue for data */
James Smart81b96ed2017-11-20 16:00:29 -08009340 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9341 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009342 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009343 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009344 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009345 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009346 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009347 }
9348 phba->sli4_hba.dat_rq = qdesc;
9349
James Smartcdb42be2019-01-28 11:14:21 -08009350 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
9351 phba->nvmet_support) {
James Smart2d7dbc42017-02-12 13:52:35 -08009352 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
James Smartc1a21eb2019-03-12 16:30:29 -07009353 cpu = lpfc_find_cpu_handle(phba, idx,
9354 LPFC_FIND_BY_HDWQ);
James Smart2d7dbc42017-02-12 13:52:35 -08009355 /* Create NVMET Receive Queue for header */
9356 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009357 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009358 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009359 LPFC_NVMET_RQE_DEF_COUNT,
9360 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009361 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009362 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009363 "3146 Failed allocate "
9364 "receive HRQ\n");
9365 goto out_error;
9366 }
James Smart5e5b5112019-01-28 11:14:22 -08009367 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009368 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
9369
9370 /* Only needed for header of RQ pair */
James Smartc1a21eb2019-03-12 16:30:29 -07009371 qdesc->rqbp = kzalloc_node(sizeof(*qdesc->rqbp),
9372 GFP_KERNEL,
9373 cpu_to_node(cpu));
James Smart2d7dbc42017-02-12 13:52:35 -08009374 if (qdesc->rqbp == NULL) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009375 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009376 "6131 Failed allocate "
9377 "Header RQBP\n");
9378 goto out_error;
9379 }
9380
Dick Kennedy4b40d022017-08-23 16:55:38 -07009381 /* Put list in known state in case driver load fails. */
9382 INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
9383
James Smart2d7dbc42017-02-12 13:52:35 -08009384 /* Create NVMET Receive Queue for data */
9385 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009386 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009387 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009388 LPFC_NVMET_RQE_DEF_COUNT,
9389 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009390 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009391 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009392 "3156 Failed allocate "
9393 "receive DRQ\n");
9394 goto out_error;
9395 }
James Smart5e5b5112019-01-28 11:14:22 -08009396 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009397 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
9398 }
9399 }
9400
James Smart4c47efc2019-01-28 11:14:25 -08009401 /* Clear NVME stats */
9402 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9403 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9404 memset(&phba->sli4_hba.hdwq[idx].nvme_cstat, 0,
9405 sizeof(phba->sli4_hba.hdwq[idx].nvme_cstat));
9406 }
9407 }
James Smart4c47efc2019-01-28 11:14:25 -08009408
9409 /* Clear SCSI stats */
9410 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
9411 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9412 memset(&phba->sli4_hba.hdwq[idx].scsi_cstat, 0,
9413 sizeof(phba->sli4_hba.hdwq[idx].scsi_cstat));
9414 }
9415 }
9416
James Smartda0436e2009-05-22 14:51:39 -04009417 return 0;
9418
James Smartda0436e2009-05-22 14:51:39 -04009419out_error:
James Smart67d12732012-08-03 12:36:13 -04009420 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009421 return -ENOMEM;
9422}
9423
James Smart895427b2017-02-12 13:52:30 -08009424static inline void
9425__lpfc_sli4_release_queue(struct lpfc_queue **qp)
9426{
9427 if (*qp != NULL) {
9428 lpfc_sli4_queue_free(*qp);
9429 *qp = NULL;
9430 }
9431}
9432
9433static inline void
9434lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
9435{
9436 int idx;
9437
9438 if (*qs == NULL)
9439 return;
9440
9441 for (idx = 0; idx < max; idx++)
9442 __lpfc_sli4_release_queue(&(*qs)[idx]);
9443
9444 kfree(*qs);
9445 *qs = NULL;
9446}
9447
9448static inline void
James Smart6a828b02019-01-28 11:14:31 -08009449lpfc_sli4_release_hdwq(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08009450{
James Smart6a828b02019-01-28 11:14:31 -08009451 struct lpfc_sli4_hdw_queue *hdwq;
James Smart657add42019-05-21 17:49:06 -07009452 struct lpfc_queue *eq;
James Smartcdb42be2019-01-28 11:14:21 -08009453 uint32_t idx;
9454
James Smart6a828b02019-01-28 11:14:31 -08009455 hdwq = phba->sli4_hba.hdwq;
James Smart6a828b02019-01-28 11:14:31 -08009456
James Smart657add42019-05-21 17:49:06 -07009457 /* Loop thru all Hardware Queues */
9458 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9459 /* Free the CQ/WQ corresponding to the Hardware Queue */
James Smartc00f62e2019-08-14 16:57:11 -07009460 lpfc_sli4_queue_free(hdwq[idx].io_cq);
9461 lpfc_sli4_queue_free(hdwq[idx].io_wq);
James Smart821bc882020-01-27 16:23:05 -08009462 hdwq[idx].hba_eq = NULL;
James Smartc00f62e2019-08-14 16:57:11 -07009463 hdwq[idx].io_cq = NULL;
9464 hdwq[idx].io_wq = NULL;
James Smartd79c9e92019-08-14 16:57:09 -07009465 if (phba->cfg_xpsgl && !phba->nvmet_support)
9466 lpfc_free_sgl_per_hdwq(phba, &hdwq[idx]);
9467 lpfc_free_cmd_rsp_buf_per_hdwq(phba, &hdwq[idx]);
James Smart895427b2017-02-12 13:52:30 -08009468 }
James Smart657add42019-05-21 17:49:06 -07009469 /* Loop thru all IRQ vectors */
9470 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
9471 /* Free the EQ corresponding to the IRQ vector */
9472 eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
9473 lpfc_sli4_queue_free(eq);
9474 phba->sli4_hba.hba_eq_hdl[idx].eq = NULL;
9475 }
James Smart895427b2017-02-12 13:52:30 -08009476}
9477
James Smartda0436e2009-05-22 14:51:39 -04009478/**
9479 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
9480 * @phba: pointer to lpfc hba data structure.
9481 *
9482 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
9483 * operation.
9484 *
9485 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009486 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009487 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009488 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009489 **/
James Smart5350d872011-10-10 21:33:49 -04009490void
James Smartda0436e2009-05-22 14:51:39 -04009491lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
9492{
James Smart4645f7b2019-03-12 16:30:14 -07009493 /*
9494 * Set FREE_INIT before beginning to free the queues.
9495 * Wait until the users of queues to acknowledge to
9496 * release queues by clearing FREE_WAIT.
9497 */
9498 spin_lock_irq(&phba->hbalock);
9499 phba->sli.sli_flag |= LPFC_QUEUE_FREE_INIT;
9500 while (phba->sli.sli_flag & LPFC_QUEUE_FREE_WAIT) {
9501 spin_unlock_irq(&phba->hbalock);
9502 msleep(20);
9503 spin_lock_irq(&phba->hbalock);
9504 }
9505 spin_unlock_irq(&phba->hbalock);
9506
James Smart93a4d6f2019-11-04 16:57:05 -08009507 lpfc_sli4_cleanup_poll_list(phba);
9508
James Smart895427b2017-02-12 13:52:30 -08009509 /* Release HBA eqs */
James Smartcdb42be2019-01-28 11:14:21 -08009510 if (phba->sli4_hba.hdwq)
James Smart6a828b02019-01-28 11:14:31 -08009511 lpfc_sli4_release_hdwq(phba);
James Smartda0436e2009-05-22 14:51:39 -04009512
James Smartbcb24f62017-11-20 16:00:36 -08009513 if (phba->nvmet_support) {
9514 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
9515 phba->cfg_nvmet_mrq);
James Smart2d7dbc42017-02-12 13:52:35 -08009516
James Smartbcb24f62017-11-20 16:00:36 -08009517 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
9518 phba->cfg_nvmet_mrq);
9519 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
9520 phba->cfg_nvmet_mrq);
9521 }
James Smart2d7dbc42017-02-12 13:52:35 -08009522
James Smartda0436e2009-05-22 14:51:39 -04009523 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009524 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04009525
9526 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08009527 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
9528
9529 /* Release ELS work queue */
9530 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04009531
9532 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009533 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
9534 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04009535
James Smartda0436e2009-05-22 14:51:39 -04009536 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009537 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
9538
9539 /* Release NVME LS complete queue */
9540 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04009541
9542 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009543 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
9544
9545 /* Everything on this list has been freed */
9546 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart4645f7b2019-03-12 16:30:14 -07009547
9548 /* Done with freeing the queues */
9549 spin_lock_irq(&phba->hbalock);
9550 phba->sli.sli_flag &= ~LPFC_QUEUE_FREE_INIT;
9551 spin_unlock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08009552}
9553
9554int
James Smart895427b2017-02-12 13:52:30 -08009555lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
9556{
9557 struct lpfc_rqb *rqbp;
9558 struct lpfc_dmabuf *h_buf;
9559 struct rqb_dmabuf *rqb_buffer;
9560
9561 rqbp = rq->rqbp;
9562 while (!list_empty(&rqbp->rqb_buffer_list)) {
9563 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
9564 struct lpfc_dmabuf, list);
9565
9566 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
9567 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
9568 rqbp->buffer_count--;
9569 }
9570 return 1;
9571}
9572
9573static int
9574lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
9575 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
9576 int qidx, uint32_t qtype)
9577{
9578 struct lpfc_sli_ring *pring;
9579 int rc;
9580
9581 if (!eq || !cq || !wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009582 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009583 "6085 Fast-path %s (%d) not allocated\n",
9584 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
9585 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009586 }
James Smartda0436e2009-05-22 14:51:39 -04009587
James Smart895427b2017-02-12 13:52:30 -08009588 /* create the Cq first */
9589 rc = lpfc_cq_create(phba, cq, eq,
9590 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
9591 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009592 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9593 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
9594 qidx, (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009595 return rc;
9596 }
9597
9598 if (qtype != LPFC_MBOX) {
James Smartcdb42be2019-01-28 11:14:21 -08009599 /* Setup cq_map for fast lookup */
James Smart895427b2017-02-12 13:52:30 -08009600 if (cq_map)
9601 *cq_map = cq->queue_id;
9602
9603 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9604 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
9605 qidx, cq->queue_id, qidx, eq->queue_id);
9606
9607 /* create the wq */
9608 rc = lpfc_wq_create(phba, wq, cq, qtype);
9609 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009610 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartc835c082019-03-12 16:30:30 -07009611 "4618 Fail setup fastpath WQ (%d), rc = 0x%x\n",
James Smart895427b2017-02-12 13:52:30 -08009612 qidx, (uint32_t)rc);
9613 /* no need to tear down cq - caller will do so */
9614 return rc;
9615 }
9616
9617 /* Bind this CQ/WQ to the NVME ring */
9618 pring = wq->pring;
9619 pring->sli.sli4.wqp = (void *)wq;
9620 cq->pring = pring;
9621
9622 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9623 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
9624 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
9625 } else {
9626 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
9627 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009628 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9629 "0539 Failed setup of slow-path MQ: "
9630 "rc = 0x%x\n", rc);
James Smart895427b2017-02-12 13:52:30 -08009631 /* no need to tear down cq - caller will do so */
9632 return rc;
9633 }
9634
9635 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9636 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
9637 phba->sli4_hba.mbx_wq->queue_id,
9638 phba->sli4_hba.mbx_cq->queue_id);
9639 }
9640
9641 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009642}
9643
9644/**
James Smart6a828b02019-01-28 11:14:31 -08009645 * lpfc_setup_cq_lookup - Setup the CQ lookup table
9646 * @phba: pointer to lpfc hba data structure.
9647 *
9648 * This routine will populate the cq_lookup table by all
9649 * available CQ queue_id's.
9650 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07009651static void
James Smart6a828b02019-01-28 11:14:31 -08009652lpfc_setup_cq_lookup(struct lpfc_hba *phba)
9653{
9654 struct lpfc_queue *eq, *childq;
James Smart6a828b02019-01-28 11:14:31 -08009655 int qidx;
9656
James Smart6a828b02019-01-28 11:14:31 -08009657 memset(phba->sli4_hba.cq_lookup, 0,
9658 (sizeof(struct lpfc_queue *) * (phba->sli4_hba.cq_max + 1)));
James Smart657add42019-05-21 17:49:06 -07009659 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -08009660 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009661 /* Get the EQ corresponding to the IRQ vector */
9662 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart6a828b02019-01-28 11:14:31 -08009663 if (!eq)
9664 continue;
James Smart657add42019-05-21 17:49:06 -07009665 /* Loop through all CQs associated with that EQ */
James Smart6a828b02019-01-28 11:14:31 -08009666 list_for_each_entry(childq, &eq->child_list, list) {
9667 if (childq->queue_id > phba->sli4_hba.cq_max)
9668 continue;
James Smartc00f62e2019-08-14 16:57:11 -07009669 if (childq->subtype == LPFC_IO)
James Smart6a828b02019-01-28 11:14:31 -08009670 phba->sli4_hba.cq_lookup[childq->queue_id] =
9671 childq;
9672 }
9673 }
9674}
9675
9676/**
James Smartda0436e2009-05-22 14:51:39 -04009677 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
9678 * @phba: pointer to lpfc hba data structure.
9679 *
9680 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
9681 * operation.
9682 *
9683 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009684 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009685 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009686 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009687 **/
9688int
9689lpfc_sli4_queue_setup(struct lpfc_hba *phba)
9690{
James Smart962bc512013-01-03 15:44:00 -05009691 uint32_t shdr_status, shdr_add_status;
9692 union lpfc_sli4_cfg_shdr *shdr;
James Smart657add42019-05-21 17:49:06 -07009693 struct lpfc_vector_map_info *cpup;
James Smartcdb42be2019-01-28 11:14:21 -08009694 struct lpfc_sli4_hdw_queue *qp;
James Smart962bc512013-01-03 15:44:00 -05009695 LPFC_MBOXQ_t *mboxq;
James Smart657add42019-05-21 17:49:06 -07009696 int qidx, cpu;
James Smartcb733e32019-01-28 11:14:32 -08009697 uint32_t length, usdelay;
James Smart895427b2017-02-12 13:52:30 -08009698 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05009699
9700 /* Check for dual-ULP support */
9701 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9702 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009703 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -05009704 "3249 Unable to allocate memory for "
9705 "QUERY_FW_CFG mailbox command\n");
9706 return -ENOMEM;
9707 }
9708 length = (sizeof(struct lpfc_mbx_query_fw_config) -
9709 sizeof(struct lpfc_sli4_cfg_mhdr));
9710 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9711 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
9712 length, LPFC_SLI4_MBX_EMBED);
9713
9714 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9715
9716 shdr = (union lpfc_sli4_cfg_shdr *)
9717 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9718 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9719 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9720 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009721 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -05009722 "3250 QUERY_FW_CFG mailbox failed with status "
9723 "x%x add_status x%x, mbx status x%x\n",
9724 shdr_status, shdr_add_status, rc);
James Smart304ee432021-04-11 18:31:17 -07009725 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart962bc512013-01-03 15:44:00 -05009726 rc = -ENXIO;
9727 goto out_error;
9728 }
9729
9730 phba->sli4_hba.fw_func_mode =
9731 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
9732 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
9733 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04009734 phba->sli4_hba.physical_port =
9735 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05009736 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9737 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
9738 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
9739 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
9740
James Smart304ee432021-04-11 18:31:17 -07009741 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04009742
9743 /*
James Smart67d12732012-08-03 12:36:13 -04009744 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04009745 */
James Smartcdb42be2019-01-28 11:14:21 -08009746 qp = phba->sli4_hba.hdwq;
James Smartda0436e2009-05-22 14:51:39 -04009747
James Smart67d12732012-08-03 12:36:13 -04009748 /* Set up HBA event queue */
James Smartcdb42be2019-01-28 11:14:21 -08009749 if (!qp) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009750 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2e90f4b2011-12-13 13:22:37 -05009751 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009752 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009753 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05009754 }
James Smart657add42019-05-21 17:49:06 -07009755
9756 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -08009757 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009758 /* Create HBA Event Queues (EQs) in order */
9759 for_each_present_cpu(cpu) {
9760 cpup = &phba->sli4_hba.cpu_map[cpu];
9761
9762 /* Look for the CPU thats using that vector with
9763 * LPFC_CPU_FIRST_IRQ set.
9764 */
9765 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
9766 continue;
9767 if (qidx != cpup->eq)
9768 continue;
9769
9770 /* Create an EQ for that vector */
9771 rc = lpfc_eq_create(phba, qp[cpup->hdwq].hba_eq,
9772 phba->cfg_fcp_imax);
9773 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009774 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart657add42019-05-21 17:49:06 -07009775 "0523 Failed setup of fast-path"
9776 " EQ (%d), rc = 0x%x\n",
9777 cpup->eq, (uint32_t)rc);
9778 goto out_destroy;
9779 }
9780
9781 /* Save the EQ for that vector in the hba_eq_hdl */
9782 phba->sli4_hba.hba_eq_hdl[cpup->eq].eq =
9783 qp[cpup->hdwq].hba_eq;
9784
9785 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9786 "2584 HBA EQ setup: queue[%d]-id=%d\n",
9787 cpup->eq,
9788 qp[cpup->hdwq].hba_eq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04009789 }
James Smartda0436e2009-05-22 14:51:39 -04009790 }
9791
James Smart657add42019-05-21 17:49:06 -07009792 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08009793 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009794 cpu = lpfc_find_cpu_handle(phba, qidx, LPFC_FIND_BY_HDWQ);
9795 cpup = &phba->sli4_hba.cpu_map[cpu];
9796
9797 /* Create the CQ/WQ corresponding to the Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -08009798 rc = lpfc_create_wq_cq(phba,
James Smart657add42019-05-21 17:49:06 -07009799 phba->sli4_hba.hdwq[cpup->hdwq].hba_eq,
James Smartc00f62e2019-08-14 16:57:11 -07009800 qp[qidx].io_cq,
9801 qp[qidx].io_wq,
9802 &phba->sli4_hba.hdwq[qidx].io_cq_map,
9803 qidx,
9804 LPFC_IO);
James Smartcdb42be2019-01-28 11:14:21 -08009805 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009806 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009807 "0535 Failed to setup fastpath "
James Smartc00f62e2019-08-14 16:57:11 -07009808 "IO WQ/CQ (%d), rc = 0x%x\n",
James Smart895427b2017-02-12 13:52:30 -08009809 qidx, (uint32_t)rc);
James Smartcdb42be2019-01-28 11:14:21 -08009810 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009811 }
James Smart67d12732012-08-03 12:36:13 -04009812 }
James Smartda0436e2009-05-22 14:51:39 -04009813
9814 /*
James Smart895427b2017-02-12 13:52:30 -08009815 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04009816 */
9817
James Smart895427b2017-02-12 13:52:30 -08009818 /* Set up slow-path MBOX CQ/MQ */
9819
9820 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009821 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009822 "0528 %s not allocated\n",
9823 phba->sli4_hba.mbx_cq ?
James Smartd1f525a2017-04-21 16:04:55 -07009824 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05009825 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009826 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009827 }
James Smart895427b2017-02-12 13:52:30 -08009828
James Smartcdb42be2019-01-28 11:14:21 -08009829 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
James Smartd1f525a2017-04-21 16:04:55 -07009830 phba->sli4_hba.mbx_cq,
9831 phba->sli4_hba.mbx_wq,
9832 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04009833 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009834 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009835 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
9836 (uint32_t)rc);
9837 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009838 }
James Smart2d7dbc42017-02-12 13:52:35 -08009839 if (phba->nvmet_support) {
9840 if (!phba->sli4_hba.nvmet_cqset) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009841 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009842 "3165 Fast-path NVME CQ Set "
9843 "array not allocated\n");
9844 rc = -ENOMEM;
9845 goto out_destroy;
9846 }
9847 if (phba->cfg_nvmet_mrq > 1) {
9848 rc = lpfc_cq_create_set(phba,
9849 phba->sli4_hba.nvmet_cqset,
James Smartcdb42be2019-01-28 11:14:21 -08009850 qp,
James Smart2d7dbc42017-02-12 13:52:35 -08009851 LPFC_WCQ, LPFC_NVMET);
9852 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009853 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009854 "3164 Failed setup of NVME CQ "
9855 "Set, rc = 0x%x\n",
9856 (uint32_t)rc);
9857 goto out_destroy;
9858 }
9859 } else {
9860 /* Set up NVMET Receive Complete Queue */
9861 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
James Smartcdb42be2019-01-28 11:14:21 -08009862 qp[0].hba_eq,
James Smart2d7dbc42017-02-12 13:52:35 -08009863 LPFC_WCQ, LPFC_NVMET);
9864 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009865 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009866 "6089 Failed setup NVMET CQ: "
9867 "rc = 0x%x\n", (uint32_t)rc);
9868 goto out_destroy;
9869 }
James Smart81b96ed2017-11-20 16:00:29 -08009870 phba->sli4_hba.nvmet_cqset[0]->chann = 0;
9871
James Smart2d7dbc42017-02-12 13:52:35 -08009872 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9873 "6090 NVMET CQ setup: cq-id=%d, "
9874 "parent eq-id=%d\n",
9875 phba->sli4_hba.nvmet_cqset[0]->queue_id,
James Smartcdb42be2019-01-28 11:14:21 -08009876 qp[0].hba_eq->queue_id);
James Smart2d7dbc42017-02-12 13:52:35 -08009877 }
9878 }
James Smartda0436e2009-05-22 14:51:39 -04009879
James Smart895427b2017-02-12 13:52:30 -08009880 /* Set up slow-path ELS WQ/CQ */
9881 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009882 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009883 "0530 ELS %s not allocated\n",
9884 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05009885 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009886 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009887 }
James Smartcdb42be2019-01-28 11:14:21 -08009888 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9889 phba->sli4_hba.els_cq,
9890 phba->sli4_hba.els_wq,
9891 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04009892 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009893 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcdb42be2019-01-28 11:14:21 -08009894 "0525 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
9895 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009896 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009897 }
9898 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9899 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
9900 phba->sli4_hba.els_wq->queue_id,
9901 phba->sli4_hba.els_cq->queue_id);
9902
James Smartcdb42be2019-01-28 11:14:21 -08009903 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -08009904 /* Set up NVME LS Complete Queue */
9905 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009906 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009907 "6091 LS %s not allocated\n",
9908 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
9909 rc = -ENOMEM;
9910 goto out_destroy;
9911 }
James Smartcdb42be2019-01-28 11:14:21 -08009912 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9913 phba->sli4_hba.nvmels_cq,
9914 phba->sli4_hba.nvmels_wq,
9915 NULL, 0, LPFC_NVME_LS);
James Smart895427b2017-02-12 13:52:30 -08009916 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009917 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcdb42be2019-01-28 11:14:21 -08009918 "0526 Failed setup of NVVME LS WQ/CQ: "
9919 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009920 goto out_destroy;
9921 }
9922
9923 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9924 "6096 ELS WQ setup: wq-id=%d, "
9925 "parent cq-id=%d\n",
9926 phba->sli4_hba.nvmels_wq->queue_id,
9927 phba->sli4_hba.nvmels_cq->queue_id);
9928 }
9929
James Smart2d7dbc42017-02-12 13:52:35 -08009930 /*
9931 * Create NVMET Receive Queue (RQ)
9932 */
9933 if (phba->nvmet_support) {
9934 if ((!phba->sli4_hba.nvmet_cqset) ||
9935 (!phba->sli4_hba.nvmet_mrq_hdr) ||
9936 (!phba->sli4_hba.nvmet_mrq_data)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009937 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009938 "6130 MRQ CQ Queues not "
9939 "allocated\n");
9940 rc = -ENOMEM;
9941 goto out_destroy;
9942 }
9943 if (phba->cfg_nvmet_mrq > 1) {
9944 rc = lpfc_mrq_create(phba,
9945 phba->sli4_hba.nvmet_mrq_hdr,
9946 phba->sli4_hba.nvmet_mrq_data,
9947 phba->sli4_hba.nvmet_cqset,
9948 LPFC_NVMET);
9949 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009950 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009951 "6098 Failed setup of NVMET "
9952 "MRQ: rc = 0x%x\n",
9953 (uint32_t)rc);
9954 goto out_destroy;
9955 }
9956
9957 } else {
9958 rc = lpfc_rq_create(phba,
9959 phba->sli4_hba.nvmet_mrq_hdr[0],
9960 phba->sli4_hba.nvmet_mrq_data[0],
9961 phba->sli4_hba.nvmet_cqset[0],
9962 LPFC_NVMET);
9963 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009964 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009965 "6057 Failed setup of NVMET "
9966 "Receive Queue: rc = 0x%x\n",
9967 (uint32_t)rc);
9968 goto out_destroy;
9969 }
9970
9971 lpfc_printf_log(
9972 phba, KERN_INFO, LOG_INIT,
9973 "6099 NVMET RQ setup: hdr-rq-id=%d, "
9974 "dat-rq-id=%d parent cq-id=%d\n",
9975 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
9976 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
9977 phba->sli4_hba.nvmet_cqset[0]->queue_id);
9978
9979 }
9980 }
9981
James Smartda0436e2009-05-22 14:51:39 -04009982 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009983 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009984 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009985 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009986 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009987 }
James Smart73d91e52011-10-10 21:32:10 -04009988
James Smartda0436e2009-05-22 14:51:39 -04009989 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04009990 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04009991 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009992 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009993 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04009994 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009995 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009996 }
James Smart73d91e52011-10-10 21:32:10 -04009997
James Smartda0436e2009-05-22 14:51:39 -04009998 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9999 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
10000 "parent cq-id=%d\n",
10001 phba->sli4_hba.hdr_rq->queue_id,
10002 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -040010003 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -050010004
James Smartcb733e32019-01-28 11:14:32 -080010005 if (phba->cfg_fcp_imax)
10006 usdelay = LPFC_SEC_TO_USEC / phba->cfg_fcp_imax;
10007 else
10008 usdelay = 0;
10009
James Smart6a828b02019-01-28 11:14:31 -080010010 for (qidx = 0; qidx < phba->cfg_irq_chann;
James Smartcdb42be2019-01-28 11:14:21 -080010011 qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart0cf07f842017-06-01 21:07:10 -070010012 lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
James Smartcb733e32019-01-28 11:14:32 -080010013 usdelay);
James Smart43140ca2017-03-04 09:30:34 -080010014
James Smart6a828b02019-01-28 11:14:31 -080010015 if (phba->sli4_hba.cq_max) {
10016 kfree(phba->sli4_hba.cq_lookup);
10017 phba->sli4_hba.cq_lookup = kcalloc((phba->sli4_hba.cq_max + 1),
10018 sizeof(struct lpfc_queue *), GFP_KERNEL);
10019 if (!phba->sli4_hba.cq_lookup) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010020 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6a828b02019-01-28 11:14:31 -080010021 "0549 Failed setup of CQ Lookup table: "
10022 "size 0x%x\n", phba->sli4_hba.cq_max);
Dan Carpenterfad28e32019-02-11 21:43:00 +030010023 rc = -ENOMEM;
James Smart1ba981f2014-02-20 09:56:45 -050010024 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -080010025 }
James Smart6a828b02019-01-28 11:14:31 -080010026 lpfc_setup_cq_lookup(phba);
James Smart1ba981f2014-02-20 09:56:45 -050010027 }
James Smartda0436e2009-05-22 14:51:39 -040010028 return 0;
10029
James Smart895427b2017-02-12 13:52:30 -080010030out_destroy:
10031 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -040010032out_error:
10033 return rc;
10034}
10035
10036/**
10037 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
10038 * @phba: pointer to lpfc hba data structure.
10039 *
10040 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
10041 * operation.
10042 *
10043 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010044 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010045 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -040010046 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -040010047 **/
10048void
10049lpfc_sli4_queue_unset(struct lpfc_hba *phba)
10050{
James Smartcdb42be2019-01-28 11:14:21 -080010051 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -070010052 struct lpfc_queue *eq;
James Smart895427b2017-02-12 13:52:30 -080010053 int qidx;
James Smartda0436e2009-05-22 14:51:39 -040010054
10055 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -080010056 if (phba->sli4_hba.mbx_wq)
10057 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
10058
10059 /* Unset NVME LS work queue */
10060 if (phba->sli4_hba.nvmels_wq)
10061 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
10062
James Smartda0436e2009-05-22 14:51:39 -040010063 /* Unset ELS work queue */
Colin Ian King019c0d62017-05-06 23:13:55 +010010064 if (phba->sli4_hba.els_wq)
James Smart895427b2017-02-12 13:52:30 -080010065 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
10066
James Smartda0436e2009-05-22 14:51:39 -040010067 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -080010068 if (phba->sli4_hba.hdr_rq)
10069 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
10070 phba->sli4_hba.dat_rq);
10071
James Smartda0436e2009-05-22 14:51:39 -040010072 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -080010073 if (phba->sli4_hba.mbx_cq)
10074 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
10075
James Smartda0436e2009-05-22 14:51:39 -040010076 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -080010077 if (phba->sli4_hba.els_cq)
10078 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
10079
10080 /* Unset NVME LS complete queue */
10081 if (phba->sli4_hba.nvmels_cq)
10082 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
10083
James Smartbcb24f62017-11-20 16:00:36 -080010084 if (phba->nvmet_support) {
10085 /* Unset NVMET MRQ queue */
10086 if (phba->sli4_hba.nvmet_mrq_hdr) {
10087 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
10088 lpfc_rq_destroy(
10089 phba,
James Smart2d7dbc42017-02-12 13:52:35 -080010090 phba->sli4_hba.nvmet_mrq_hdr[qidx],
10091 phba->sli4_hba.nvmet_mrq_data[qidx]);
James Smartbcb24f62017-11-20 16:00:36 -080010092 }
James Smart2d7dbc42017-02-12 13:52:35 -080010093
James Smartbcb24f62017-11-20 16:00:36 -080010094 /* Unset NVMET CQ Set complete queue */
10095 if (phba->sli4_hba.nvmet_cqset) {
10096 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
10097 lpfc_cq_destroy(
10098 phba, phba->sli4_hba.nvmet_cqset[qidx]);
10099 }
James Smart2d7dbc42017-02-12 13:52:35 -080010100 }
10101
James Smartcdb42be2019-01-28 11:14:21 -080010102 /* Unset fast-path SLI4 queues */
10103 if (phba->sli4_hba.hdwq) {
James Smart657add42019-05-21 17:49:06 -070010104 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -080010105 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -070010106 /* Destroy the CQ/WQ corresponding to Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -080010107 qp = &phba->sli4_hba.hdwq[qidx];
James Smartc00f62e2019-08-14 16:57:11 -070010108 lpfc_wq_destroy(phba, qp->io_wq);
10109 lpfc_cq_destroy(phba, qp->io_cq);
James Smart657add42019-05-21 17:49:06 -070010110 }
10111 /* Loop thru all IRQ vectors */
10112 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
10113 /* Destroy the EQ corresponding to the IRQ vector */
10114 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
10115 lpfc_eq_destroy(phba, eq);
James Smartcdb42be2019-01-28 11:14:21 -080010116 }
10117 }
James Smart895427b2017-02-12 13:52:30 -080010118
James Smart6a828b02019-01-28 11:14:31 -080010119 kfree(phba->sli4_hba.cq_lookup);
10120 phba->sli4_hba.cq_lookup = NULL;
10121 phba->sli4_hba.cq_max = 0;
James Smartda0436e2009-05-22 14:51:39 -040010122}
10123
10124/**
10125 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
10126 * @phba: pointer to lpfc hba data structure.
10127 *
10128 * This routine is invoked to allocate and set up a pool of completion queue
10129 * events. The body of the completion queue event is a completion queue entry
10130 * CQE. For now, this pool is used for the interrupt service routine to queue
10131 * the following HBA completion queue events for the worker thread to process:
10132 * - Mailbox asynchronous events
10133 * - Receive queue completion unsolicited events
10134 * Later, this can be used for all the slow-path events.
10135 *
10136 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010137 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010138 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -040010139 **/
10140static int
10141lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
10142{
10143 struct lpfc_cq_event *cq_event;
10144 int i;
10145
10146 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
10147 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
10148 if (!cq_event)
10149 goto out_pool_create_fail;
10150 list_add_tail(&cq_event->list,
10151 &phba->sli4_hba.sp_cqe_event_pool);
10152 }
10153 return 0;
10154
10155out_pool_create_fail:
10156 lpfc_sli4_cq_event_pool_destroy(phba);
10157 return -ENOMEM;
10158}
10159
10160/**
10161 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
10162 * @phba: pointer to lpfc hba data structure.
10163 *
10164 * This routine is invoked to free the pool of completion queue events at
10165 * driver unload time. Note that, it is the responsibility of the driver
10166 * cleanup routine to free all the outstanding completion-queue events
10167 * allocated from this pool back into the pool before invoking this routine
10168 * to destroy the pool.
10169 **/
10170static void
10171lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
10172{
10173 struct lpfc_cq_event *cq_event, *next_cq_event;
10174
10175 list_for_each_entry_safe(cq_event, next_cq_event,
10176 &phba->sli4_hba.sp_cqe_event_pool, list) {
10177 list_del(&cq_event->list);
10178 kfree(cq_event);
10179 }
10180}
10181
10182/**
10183 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
10184 * @phba: pointer to lpfc hba data structure.
10185 *
10186 * This routine is the lock free version of the API invoked to allocate a
10187 * completion-queue event from the free pool.
10188 *
10189 * Return: Pointer to the newly allocated completion-queue event if successful
10190 * NULL otherwise.
10191 **/
10192struct lpfc_cq_event *
10193__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
10194{
10195 struct lpfc_cq_event *cq_event = NULL;
10196
10197 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
10198 struct lpfc_cq_event, list);
10199 return cq_event;
10200}
10201
10202/**
10203 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
10204 * @phba: pointer to lpfc hba data structure.
10205 *
10206 * This routine is the lock version of the API invoked to allocate a
10207 * completion-queue event from the free pool.
10208 *
10209 * Return: Pointer to the newly allocated completion-queue event if successful
10210 * NULL otherwise.
10211 **/
10212struct lpfc_cq_event *
10213lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
10214{
10215 struct lpfc_cq_event *cq_event;
10216 unsigned long iflags;
10217
10218 spin_lock_irqsave(&phba->hbalock, iflags);
10219 cq_event = __lpfc_sli4_cq_event_alloc(phba);
10220 spin_unlock_irqrestore(&phba->hbalock, iflags);
10221 return cq_event;
10222}
10223
10224/**
10225 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
10226 * @phba: pointer to lpfc hba data structure.
10227 * @cq_event: pointer to the completion queue event to be freed.
10228 *
10229 * This routine is the lock free version of the API invoked to release a
10230 * completion-queue event back into the free pool.
10231 **/
10232void
10233__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
10234 struct lpfc_cq_event *cq_event)
10235{
10236 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
10237}
10238
10239/**
10240 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
10241 * @phba: pointer to lpfc hba data structure.
10242 * @cq_event: pointer to the completion queue event to be freed.
10243 *
10244 * This routine is the lock version of the API invoked to release a
10245 * completion-queue event back into the free pool.
10246 **/
10247void
10248lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
10249 struct lpfc_cq_event *cq_event)
10250{
10251 unsigned long iflags;
10252 spin_lock_irqsave(&phba->hbalock, iflags);
10253 __lpfc_sli4_cq_event_release(phba, cq_event);
10254 spin_unlock_irqrestore(&phba->hbalock, iflags);
10255}
10256
10257/**
10258 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
10259 * @phba: pointer to lpfc hba data structure.
10260 *
10261 * This routine is to free all the pending completion-queue events to the
10262 * back into the free pool for device reset.
10263 **/
10264static void
10265lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
10266{
James Smarte7dab162020-10-20 13:27:12 -070010267 LIST_HEAD(cq_event_list);
10268 struct lpfc_cq_event *cq_event;
James Smartda0436e2009-05-22 14:51:39 -040010269 unsigned long iflags;
10270
10271 /* Retrieve all the pending WCQEs from pending WCQE lists */
James Smartda0436e2009-05-22 14:51:39 -040010272
James Smarte7dab162020-10-20 13:27:12 -070010273 /* Pending ELS XRI abort events */
10274 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
10275 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10276 &cq_event_list);
10277 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
10278
10279 /* Pending asynnc events */
10280 spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
10281 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
10282 &cq_event_list);
10283 spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
10284
10285 while (!list_empty(&cq_event_list)) {
10286 list_remove_head(&cq_event_list, cq_event,
10287 struct lpfc_cq_event, list);
10288 lpfc_sli4_cq_event_release(phba, cq_event);
James Smartda0436e2009-05-22 14:51:39 -040010289 }
10290}
10291
10292/**
10293 * lpfc_pci_function_reset - Reset pci function.
10294 * @phba: pointer to lpfc hba data structure.
10295 *
10296 * This routine is invoked to request a PCI function reset. It will destroys
10297 * all resources assigned to the PCI function which originates this request.
10298 *
10299 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010300 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010301 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -040010302 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -040010303 **/
10304int
10305lpfc_pci_function_reset(struct lpfc_hba *phba)
10306{
10307 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -050010308 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -040010309 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -040010310 uint32_t rdy_chk;
10311 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -040010312 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -050010313 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -050010314 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -040010315
James Smart2fcee4b2010-12-15 17:57:46 -050010316 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10317 switch (if_type) {
10318 case LPFC_SLI_INTF_IF_TYPE_0:
10319 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
10320 GFP_KERNEL);
10321 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010322 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050010323 "0494 Unable to allocate memory for "
10324 "issuing SLI_FUNCTION_RESET mailbox "
10325 "command\n");
10326 return -ENOMEM;
10327 }
10328
10329 /* Setup PCI function reset mailbox-ioctl command */
10330 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10331 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
10332 LPFC_SLI4_MBX_EMBED);
10333 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10334 shdr = (union lpfc_sli4_cfg_shdr *)
10335 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
10336 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10337 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
10338 &shdr->response);
James Smart304ee432021-04-11 18:31:17 -070010339 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart2fcee4b2010-12-15 17:57:46 -050010340 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010341 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050010342 "0495 SLI_FUNCTION_RESET mailbox "
10343 "failed with status x%x add_status x%x,"
10344 " mbx status x%x\n",
10345 shdr_status, shdr_add_status, rc);
10346 rc = -ENXIO;
10347 }
10348 break;
10349 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080010350 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2f6fa2c2014-09-03 12:57:08 -040010351wait:
10352 /*
10353 * Poll the Port Status Register and wait for RDY for
10354 * up to 30 seconds. If the port doesn't respond, treat
10355 * it as an error.
10356 */
James Smart77d093f2015-04-07 15:07:08 -040010357 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -040010358 if (lpfc_readl(phba->sli4_hba.u.if_type2.
10359 STATUSregaddr, &reg_data.word0)) {
10360 rc = -ENODEV;
10361 goto out;
10362 }
10363 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
10364 break;
10365 msleep(20);
10366 }
10367
10368 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
10369 phba->work_status[0] = readl(
10370 phba->sli4_hba.u.if_type2.ERR1regaddr);
10371 phba->work_status[1] = readl(
10372 phba->sli4_hba.u.if_type2.ERR2regaddr);
Dick Kennedy372c1872020-06-30 14:50:00 -070010373 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2f6fa2c2014-09-03 12:57:08 -040010374 "2890 Port not ready, port status reg "
10375 "0x%x error 1=0x%x, error 2=0x%x\n",
10376 reg_data.word0,
10377 phba->work_status[0],
10378 phba->work_status[1]);
10379 rc = -ENODEV;
10380 goto out;
10381 }
10382
10383 if (!port_reset) {
10384 /*
10385 * Reset the port now
10386 */
James Smart2fcee4b2010-12-15 17:57:46 -050010387 reg_data.word0 = 0;
10388 bf_set(lpfc_sliport_ctrl_end, &reg_data,
10389 LPFC_SLIPORT_LITTLE_ENDIAN);
10390 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
10391 LPFC_SLIPORT_INIT_PORT);
10392 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
10393 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -050010394 /* flush */
James Smart2b81f942012-03-01 22:37:18 -050010395 pci_read_config_word(phba->pcidev,
10396 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -050010397
James Smart2f6fa2c2014-09-03 12:57:08 -040010398 port_reset = 1;
10399 msleep(20);
10400 goto wait;
10401 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
10402 rc = -ENODEV;
10403 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -050010404 }
10405 break;
James Smart2f6fa2c2014-09-03 12:57:08 -040010406
James Smart2fcee4b2010-12-15 17:57:46 -050010407 case LPFC_SLI_INTF_IF_TYPE_1:
10408 default:
10409 break;
James Smartda0436e2009-05-22 14:51:39 -040010410 }
10411
James Smart73d91e52011-10-10 21:32:10 -040010412out:
James Smart2fcee4b2010-12-15 17:57:46 -050010413 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -040010414 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010415 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart229adb02013-04-17 20:16:51 -040010416 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -040010417 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -050010418 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -040010419 }
James Smart2fcee4b2010-12-15 17:57:46 -050010420
James Smartda0436e2009-05-22 14:51:39 -040010421 return rc;
10422}
10423
10424/**
James Smartda0436e2009-05-22 14:51:39 -040010425 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
10426 * @phba: pointer to lpfc hba data structure.
10427 *
10428 * This routine is invoked to set up the PCI device memory space for device
10429 * with SLI-4 interface spec.
10430 *
10431 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010432 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010433 * other values - error
10434 **/
10435static int
10436lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
10437{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010438 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040010439 unsigned long bar0map_len, bar1map_len, bar2map_len;
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010440 int error;
James Smart2fcee4b2010-12-15 17:57:46 -050010441 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -040010442
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010443 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +010010444 return -ENODEV;
James Smartda0436e2009-05-22 14:51:39 -040010445
10446 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +010010447 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
10448 if (error)
10449 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10450 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010451 return error;
James Smartda0436e2009-05-22 14:51:39 -040010452
James Smart2fcee4b2010-12-15 17:57:46 -050010453 /*
10454 * The BARs and register set definitions and offset locations are
10455 * dependent on the if_type.
10456 */
10457 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
10458 &phba->sli4_hba.sli_intf.word0)) {
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010459 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010460 }
10461
10462 /* There is no SLI3 failback for SLI4 devices. */
10463 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
10464 LPFC_SLI_INTF_VALID) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010465 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050010466 "2894 SLI_INTF reg contents invalid "
10467 "sli_intf reg 0x%x\n",
10468 phba->sli4_hba.sli_intf.word0);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010469 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010470 }
10471
10472 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10473 /*
10474 * Get the bus address of SLI4 device Bar regions and the
10475 * number of bytes required by each mapping. The mapping of the
10476 * particular PCI BARs regions is dependent on the type of
10477 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -040010478 */
James Smartf5ca6f22013-09-06 12:21:09 -040010479 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
10480 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
10481 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -050010482
10483 /*
10484 * Map SLI4 PCI Config Space Register base to a kernel virtual
10485 * addr
10486 */
10487 phba->sli4_hba.conf_regs_memmap_p =
10488 ioremap(phba->pci_bar0_map, bar0map_len);
10489 if (!phba->sli4_hba.conf_regs_memmap_p) {
10490 dev_printk(KERN_ERR, &pdev->dev,
10491 "ioremap failed for SLI4 PCI config "
10492 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010493 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010494 }
James Smartf5ca6f22013-09-06 12:21:09 -040010495 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -050010496 /* Set up BAR0 PCI config space register memory map */
10497 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -050010498 } else {
10499 phba->pci_bar0_map = pci_resource_start(pdev, 1);
10500 bar0map_len = pci_resource_len(pdev, 1);
James Smart27d6ac02018-02-22 08:18:42 -080010501 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart2fcee4b2010-12-15 17:57:46 -050010502 dev_printk(KERN_ERR, &pdev->dev,
10503 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010504 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010505 }
10506 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -040010507 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -050010508 if (!phba->sli4_hba.conf_regs_memmap_p) {
10509 dev_printk(KERN_ERR, &pdev->dev,
10510 "ioremap failed for SLI4 PCI config "
10511 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010512 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010513 }
10514 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -040010515 }
10516
James Smarte4b97942017-11-20 16:00:31 -080010517 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10518 if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
10519 /*
10520 * Map SLI4 if type 0 HBA Control Register base to a
10521 * kernel virtual address and setup the registers.
10522 */
10523 phba->pci_bar1_map = pci_resource_start(pdev,
10524 PCI_64BIT_BAR2);
10525 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10526 phba->sli4_hba.ctrl_regs_memmap_p =
10527 ioremap(phba->pci_bar1_map,
10528 bar1map_len);
10529 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
10530 dev_err(&pdev->dev,
10531 "ioremap failed for SLI4 HBA "
10532 "control registers.\n");
10533 error = -ENOMEM;
10534 goto out_iounmap_conf;
10535 }
10536 phba->pci_bar2_memmap_p =
10537 phba->sli4_hba.ctrl_regs_memmap_p;
James Smart27d6ac02018-02-22 08:18:42 -080010538 lpfc_sli4_bar1_register_memmap(phba, if_type);
James Smarte4b97942017-11-20 16:00:31 -080010539 } else {
10540 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010541 goto out_iounmap_conf;
10542 }
James Smartda0436e2009-05-22 14:51:39 -040010543 }
10544
James Smart27d6ac02018-02-22 08:18:42 -080010545 if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
10546 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
10547 /*
10548 * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
10549 * virtual address and setup the registers.
10550 */
10551 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
10552 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10553 phba->sli4_hba.drbl_regs_memmap_p =
10554 ioremap(phba->pci_bar1_map, bar1map_len);
10555 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10556 dev_err(&pdev->dev,
10557 "ioremap failed for SLI4 HBA doorbell registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010558 error = -ENOMEM;
James Smart27d6ac02018-02-22 08:18:42 -080010559 goto out_iounmap_conf;
10560 }
10561 phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
10562 lpfc_sli4_bar1_register_memmap(phba, if_type);
10563 }
10564
James Smarte4b97942017-11-20 16:00:31 -080010565 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10566 if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10567 /*
10568 * Map SLI4 if type 0 HBA Doorbell Register base to
10569 * a kernel virtual address and setup the registers.
10570 */
10571 phba->pci_bar2_map = pci_resource_start(pdev,
10572 PCI_64BIT_BAR4);
10573 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10574 phba->sli4_hba.drbl_regs_memmap_p =
10575 ioremap(phba->pci_bar2_map,
10576 bar2map_len);
10577 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10578 dev_err(&pdev->dev,
10579 "ioremap failed for SLI4 HBA"
10580 " doorbell registers.\n");
10581 error = -ENOMEM;
10582 goto out_iounmap_ctrl;
10583 }
10584 phba->pci_bar4_memmap_p =
10585 phba->sli4_hba.drbl_regs_memmap_p;
10586 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
10587 if (error)
10588 goto out_iounmap_all;
10589 } else {
10590 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010591 goto out_iounmap_all;
James Smarte4b97942017-11-20 16:00:31 -080010592 }
James Smartda0436e2009-05-22 14:51:39 -040010593 }
10594
James Smart1351e692018-02-22 08:18:43 -080010595 if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
10596 pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10597 /*
10598 * Map SLI4 if type 6 HBA DPP Register base to a kernel
10599 * virtual address and setup the registers.
10600 */
10601 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
10602 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10603 phba->sli4_hba.dpp_regs_memmap_p =
10604 ioremap(phba->pci_bar2_map, bar2map_len);
10605 if (!phba->sli4_hba.dpp_regs_memmap_p) {
10606 dev_err(&pdev->dev,
10607 "ioremap failed for SLI4 HBA dpp registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010608 error = -ENOMEM;
James Smart1351e692018-02-22 08:18:43 -080010609 goto out_iounmap_ctrl;
10610 }
10611 phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
10612 }
10613
James Smartb71413d2018-02-22 08:18:40 -080010614 /* Set up the EQ/CQ register handeling functions now */
James Smart27d6ac02018-02-22 08:18:42 -080010615 switch (if_type) {
10616 case LPFC_SLI_INTF_IF_TYPE_0:
10617 case LPFC_SLI_INTF_IF_TYPE_2:
James Smartb71413d2018-02-22 08:18:40 -080010618 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010619 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_write_eq_db;
10620 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010621 break;
10622 case LPFC_SLI_INTF_IF_TYPE_6:
10623 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010624 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_if6_write_eq_db;
10625 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_if6_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010626 break;
10627 default:
10628 break;
James Smartb71413d2018-02-22 08:18:40 -080010629 }
10630
James Smartda0436e2009-05-22 14:51:39 -040010631 return 0;
10632
10633out_iounmap_all:
10634 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10635out_iounmap_ctrl:
10636 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10637out_iounmap_conf:
10638 iounmap(phba->sli4_hba.conf_regs_memmap_p);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010639
James Smartda0436e2009-05-22 14:51:39 -040010640 return error;
10641}
10642
10643/**
10644 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
10645 * @phba: pointer to lpfc hba data structure.
10646 *
10647 * This routine is invoked to unset the PCI device memory space for device
10648 * with SLI-4 interface spec.
10649 **/
10650static void
10651lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
10652{
James Smart2e90f4b2011-12-13 13:22:37 -050010653 uint32_t if_type;
10654 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -040010655
James Smart2e90f4b2011-12-13 13:22:37 -050010656 switch (if_type) {
10657 case LPFC_SLI_INTF_IF_TYPE_0:
10658 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10659 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10660 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10661 break;
10662 case LPFC_SLI_INTF_IF_TYPE_2:
10663 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10664 break;
James Smart27d6ac02018-02-22 08:18:42 -080010665 case LPFC_SLI_INTF_IF_TYPE_6:
10666 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10667 iounmap(phba->sli4_hba.conf_regs_memmap_p);
James Smart0b439192019-12-18 15:58:05 -080010668 if (phba->sli4_hba.dpp_regs_memmap_p)
10669 iounmap(phba->sli4_hba.dpp_regs_memmap_p);
James Smart27d6ac02018-02-22 08:18:42 -080010670 break;
James Smart2e90f4b2011-12-13 13:22:37 -050010671 case LPFC_SLI_INTF_IF_TYPE_1:
10672 default:
10673 dev_printk(KERN_ERR, &phba->pcidev->dev,
10674 "FATAL - unsupported SLI4 interface type - %d\n",
10675 if_type);
10676 break;
10677 }
James Smartda0436e2009-05-22 14:51:39 -040010678}
10679
10680/**
James Smart3772a992009-05-22 14:50:54 -040010681 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
10682 * @phba: pointer to lpfc hba data structure.
10683 *
10684 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010685 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -040010686 *
10687 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010688 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010689 * other values - error
10690 **/
10691static int
10692lpfc_sli_enable_msix(struct lpfc_hba *phba)
10693{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010694 int rc;
James Smart3772a992009-05-22 14:50:54 -040010695 LPFC_MBOXQ_t *pmb;
10696
10697 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010698 rc = pci_alloc_irq_vectors(phba->pcidev,
10699 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
10700 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -040010701 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10702 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010703 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -040010704 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010705
James Smart3772a992009-05-22 14:50:54 -040010706 /*
10707 * Assign MSI-X vectors to interrupt handlers
10708 */
10709
10710 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010711 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -040010712 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010713 LPFC_SP_DRIVER_HANDLER_NAME, phba);
10714 if (rc) {
10715 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10716 "0421 MSI-X slow-path request_irq failed "
10717 "(%d)\n", rc);
10718 goto msi_fail_out;
10719 }
10720
10721 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010722 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -040010723 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010724 LPFC_FP_DRIVER_HANDLER_NAME, phba);
10725
10726 if (rc) {
10727 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10728 "0429 MSI-X fast-path request_irq failed "
10729 "(%d)\n", rc);
10730 goto irq_fail_out;
10731 }
10732
10733 /*
10734 * Configure HBA MSI-X attention conditions to messages
10735 */
10736 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10737
10738 if (!pmb) {
10739 rc = -ENOMEM;
Dick Kennedy372c1872020-06-30 14:50:00 -070010740 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040010741 "0474 Unable to allocate memory for issuing "
10742 "MBOX_CONFIG_MSI command\n");
10743 goto mem_fail_out;
10744 }
10745 rc = lpfc_config_msi(phba, pmb);
10746 if (rc)
10747 goto mbx_fail_out;
10748 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
10749 if (rc != MBX_SUCCESS) {
10750 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
10751 "0351 Config MSI mailbox command failed, "
10752 "mbxCmd x%x, mbxStatus x%x\n",
10753 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
10754 goto mbx_fail_out;
10755 }
10756
10757 /* Free memory allocated for mailbox command */
10758 mempool_free(pmb, phba->mbox_mem_pool);
10759 return rc;
10760
10761mbx_fail_out:
10762 /* Free memory allocated for mailbox command */
10763 mempool_free(pmb, phba->mbox_mem_pool);
10764
10765mem_fail_out:
10766 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010767 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -040010768
10769irq_fail_out:
10770 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010771 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -040010772
10773msi_fail_out:
10774 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010775 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010776
10777vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -040010778 return rc;
10779}
10780
10781/**
James Smart3772a992009-05-22 14:50:54 -040010782 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
10783 * @phba: pointer to lpfc hba data structure.
10784 *
10785 * This routine is invoked to enable the MSI interrupt mode to device with
10786 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
10787 * enable the MSI vector. The device driver is responsible for calling the
10788 * request_irq() to register MSI vector with a interrupt the handler, which
10789 * is done in this function.
10790 *
10791 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010792 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010793 * other values - error
10794 */
10795static int
10796lpfc_sli_enable_msi(struct lpfc_hba *phba)
10797{
10798 int rc;
10799
10800 rc = pci_enable_msi(phba->pcidev);
10801 if (!rc)
10802 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10803 "0462 PCI enable MSI mode success.\n");
10804 else {
10805 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10806 "0471 PCI enable MSI mode failed (%d)\n", rc);
10807 return rc;
10808 }
10809
10810 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040010811 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -040010812 if (rc) {
10813 pci_disable_msi(phba->pcidev);
10814 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10815 "0478 MSI request_irq failed (%d)\n", rc);
10816 }
10817 return rc;
10818}
10819
10820/**
James Smart3772a992009-05-22 14:50:54 -040010821 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
10822 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +010010823 * @cfg_mode: Interrupt configuration mode (INTx, MSI or MSI-X).
James Smart3772a992009-05-22 14:50:54 -040010824 *
10825 * This routine is invoked to enable device interrupt and associate driver's
10826 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
10827 * spec. Depends on the interrupt mode configured to the driver, the driver
10828 * will try to fallback from the configured interrupt mode to an interrupt
10829 * mode which is supported by the platform, kernel, and device in the order
10830 * of:
10831 * MSI-X -> MSI -> IRQ.
10832 *
10833 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010834 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010835 * other values - error
10836 **/
10837static uint32_t
10838lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10839{
10840 uint32_t intr_mode = LPFC_INTR_ERROR;
10841 int retval;
10842
James Smartd2f25472021-01-04 10:02:27 -080010843 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
10844 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
10845 if (retval)
10846 return intr_mode;
10847 phba->hba_flag &= ~HBA_NEEDS_CFG_PORT;
10848
James Smart3772a992009-05-22 14:50:54 -040010849 if (cfg_mode == 2) {
James Smartd2f25472021-01-04 10:02:27 -080010850 /* Now, try to enable MSI-X interrupt mode */
10851 retval = lpfc_sli_enable_msix(phba);
James Smart3772a992009-05-22 14:50:54 -040010852 if (!retval) {
James Smartd2f25472021-01-04 10:02:27 -080010853 /* Indicate initialization to MSI-X mode */
10854 phba->intr_type = MSIX;
10855 intr_mode = 2;
James Smart3772a992009-05-22 14:50:54 -040010856 }
10857 }
10858
10859 /* Fallback to MSI if MSI-X initialization failed */
10860 if (cfg_mode >= 1 && phba->intr_type == NONE) {
10861 retval = lpfc_sli_enable_msi(phba);
10862 if (!retval) {
10863 /* Indicate initialization to MSI mode */
10864 phba->intr_type = MSI;
10865 intr_mode = 1;
10866 }
10867 }
10868
10869 /* Fallback to INTx if both MSI-X/MSI initalization failed */
10870 if (phba->intr_type == NONE) {
10871 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
10872 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10873 if (!retval) {
10874 /* Indicate initialization to INTx mode */
10875 phba->intr_type = INTx;
10876 intr_mode = 0;
10877 }
10878 }
10879 return intr_mode;
10880}
10881
10882/**
10883 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
10884 * @phba: pointer to lpfc hba data structure.
10885 *
10886 * This routine is invoked to disable device interrupt and disassociate the
10887 * driver's interrupt handler(s) from interrupt vector(s) to device with
10888 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
10889 * release the interrupt vector(s) for the message signaled interrupt.
10890 **/
10891static void
10892lpfc_sli_disable_intr(struct lpfc_hba *phba)
10893{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010894 int nr_irqs, i;
10895
James Smart3772a992009-05-22 14:50:54 -040010896 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010897 nr_irqs = LPFC_MSIX_VECTORS;
10898 else
10899 nr_irqs = 1;
10900
10901 for (i = 0; i < nr_irqs; i++)
10902 free_irq(pci_irq_vector(phba->pcidev, i), phba);
10903 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -040010904
10905 /* Reset interrupt management states */
10906 phba->intr_type = NONE;
10907 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -040010908}
10909
10910/**
James Smart657add42019-05-21 17:49:06 -070010911 * lpfc_find_cpu_handle - Find the CPU that corresponds to the specified Queue
James Smart6a828b02019-01-28 11:14:31 -080010912 * @phba: pointer to lpfc hba data structure.
10913 * @id: EQ vector index or Hardware Queue index
10914 * @match: LPFC_FIND_BY_EQ = match by EQ
10915 * LPFC_FIND_BY_HDWQ = match by Hardware Queue
James Smart657add42019-05-21 17:49:06 -070010916 * Return the CPU that matches the selection criteria
James Smart6a828b02019-01-28 11:14:31 -080010917 */
10918static uint16_t
10919lpfc_find_cpu_handle(struct lpfc_hba *phba, uint16_t id, int match)
10920{
10921 struct lpfc_vector_map_info *cpup;
10922 int cpu;
10923
James Smart657add42019-05-21 17:49:06 -070010924 /* Loop through all CPUs */
James Smart222e9232019-01-28 11:14:35 -080010925 for_each_present_cpu(cpu) {
10926 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart657add42019-05-21 17:49:06 -070010927
10928 /* If we are matching by EQ, there may be multiple CPUs using
10929 * using the same vector, so select the one with
10930 * LPFC_CPU_FIRST_IRQ set.
10931 */
James Smart6a828b02019-01-28 11:14:31 -080010932 if ((match == LPFC_FIND_BY_EQ) &&
James Smart657add42019-05-21 17:49:06 -070010933 (cpup->flag & LPFC_CPU_FIRST_IRQ) &&
James Smart6a828b02019-01-28 11:14:31 -080010934 (cpup->eq == id))
10935 return cpu;
James Smart657add42019-05-21 17:49:06 -070010936
10937 /* If matching by HDWQ, select the first CPU that matches */
James Smart6a828b02019-01-28 11:14:31 -080010938 if ((match == LPFC_FIND_BY_HDWQ) && (cpup->hdwq == id))
10939 return cpu;
James Smart6a828b02019-01-28 11:14:31 -080010940 }
10941 return 0;
10942}
10943
James Smart6a828b02019-01-28 11:14:31 -080010944#ifdef CONFIG_X86
10945/**
10946 * lpfc_find_hyper - Determine if the CPU map entry is hyper-threaded
10947 * @phba: pointer to lpfc hba data structure.
10948 * @cpu: CPU map index
10949 * @phys_id: CPU package physical id
10950 * @core_id: CPU core id
10951 */
10952static int
10953lpfc_find_hyper(struct lpfc_hba *phba, int cpu,
10954 uint16_t phys_id, uint16_t core_id)
10955{
10956 struct lpfc_vector_map_info *cpup;
10957 int idx;
10958
James Smart222e9232019-01-28 11:14:35 -080010959 for_each_present_cpu(idx) {
10960 cpup = &phba->sli4_hba.cpu_map[idx];
James Smart6a828b02019-01-28 11:14:31 -080010961 /* Does the cpup match the one we are looking for */
10962 if ((cpup->phys_id == phys_id) &&
10963 (cpup->core_id == core_id) &&
James Smart222e9232019-01-28 11:14:35 -080010964 (cpu != idx))
James Smart6a828b02019-01-28 11:14:31 -080010965 return 1;
James Smart6a828b02019-01-28 11:14:31 -080010966 }
10967 return 0;
10968}
10969#endif
10970
James Smartdcaa2132019-11-04 16:57:06 -080010971/*
10972 * lpfc_assign_eq_map_info - Assigns eq for vector_map structure
10973 * @phba: pointer to lpfc hba data structure.
10974 * @eqidx: index for eq and irq vector
10975 * @flag: flags to set for vector_map structure
10976 * @cpu: cpu used to index vector_map structure
10977 *
10978 * The routine assigns eq info into vector_map structure
10979 */
10980static inline void
10981lpfc_assign_eq_map_info(struct lpfc_hba *phba, uint16_t eqidx, uint16_t flag,
10982 unsigned int cpu)
10983{
10984 struct lpfc_vector_map_info *cpup = &phba->sli4_hba.cpu_map[cpu];
10985 struct lpfc_hba_eq_hdl *eqhdl = lpfc_get_eq_hdl(eqidx);
10986
10987 cpup->eq = eqidx;
10988 cpup->flag |= flag;
10989
10990 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10991 "3336 Set Affinity: CPU %d irq %d eq %d flag x%x\n",
10992 cpu, eqhdl->irq, cpup->eq, cpup->flag);
10993}
10994
10995/**
10996 * lpfc_cpu_map_array_init - Initialize cpu_map structure
10997 * @phba: pointer to lpfc hba data structure.
10998 *
10999 * The routine initializes the cpu_map array structure
11000 */
11001static void
11002lpfc_cpu_map_array_init(struct lpfc_hba *phba)
11003{
11004 struct lpfc_vector_map_info *cpup;
11005 struct lpfc_eq_intr_info *eqi;
11006 int cpu;
11007
11008 for_each_possible_cpu(cpu) {
11009 cpup = &phba->sli4_hba.cpu_map[cpu];
11010 cpup->phys_id = LPFC_VECTOR_MAP_EMPTY;
11011 cpup->core_id = LPFC_VECTOR_MAP_EMPTY;
11012 cpup->hdwq = LPFC_VECTOR_MAP_EMPTY;
11013 cpup->eq = LPFC_VECTOR_MAP_EMPTY;
11014 cpup->flag = 0;
11015 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, cpu);
11016 INIT_LIST_HEAD(&eqi->list);
11017 eqi->icnt = 0;
11018 }
11019}
11020
11021/**
11022 * lpfc_hba_eq_hdl_array_init - Initialize hba_eq_hdl structure
11023 * @phba: pointer to lpfc hba data structure.
11024 *
11025 * The routine initializes the hba_eq_hdl array structure
11026 */
11027static void
11028lpfc_hba_eq_hdl_array_init(struct lpfc_hba *phba)
11029{
11030 struct lpfc_hba_eq_hdl *eqhdl;
11031 int i;
11032
11033 for (i = 0; i < phba->cfg_irq_chann; i++) {
11034 eqhdl = lpfc_get_eq_hdl(i);
11035 eqhdl->irq = LPFC_VECTOR_MAP_EMPTY;
11036 eqhdl->phba = phba;
11037 }
11038}
11039
James Smart6a828b02019-01-28 11:14:31 -080011040/**
James Smart895427b2017-02-12 13:52:30 -080011041 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -040011042 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -080011043 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -040011044 *
James Smart895427b2017-02-12 13:52:30 -080011045 * The routine will figure out the CPU affinity assignment for every
James Smart6a828b02019-01-28 11:14:31 -080011046 * MSI-X vector allocated for the HBA.
James Smart895427b2017-02-12 13:52:30 -080011047 * In addition, the CPU to IO channel mapping will be calculated
11048 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -040011049 */
James Smart895427b2017-02-12 13:52:30 -080011050static void
11051lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -040011052{
James Smart3ad348d2019-08-14 16:56:43 -070011053 int i, cpu, idx, next_idx, new_cpu, start_cpu, first_cpu;
James Smart6a828b02019-01-28 11:14:31 -080011054 int max_phys_id, min_phys_id;
11055 int max_core_id, min_core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040011056 struct lpfc_vector_map_info *cpup;
James Smartd9954a22019-05-21 17:49:05 -070011057 struct lpfc_vector_map_info *new_cpup;
James Smart7bb03bb2013-04-17 20:19:16 -040011058#ifdef CONFIG_X86
11059 struct cpuinfo_x86 *cpuinfo;
11060#endif
James Smart840eda92020-03-22 11:13:00 -070011061#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
11062 struct lpfc_hdwq_stat *c_stat;
11063#endif
James Smart7bb03bb2013-04-17 20:19:16 -040011064
James Smart6a828b02019-01-28 11:14:31 -080011065 max_phys_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070011066 min_phys_id = LPFC_VECTOR_MAP_EMPTY;
James Smart6a828b02019-01-28 11:14:31 -080011067 max_core_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070011068 min_core_id = LPFC_VECTOR_MAP_EMPTY;
James Smart7bb03bb2013-04-17 20:19:16 -040011069
11070 /* Update CPU map with physical id and core id of each CPU */
James Smart222e9232019-01-28 11:14:35 -080011071 for_each_present_cpu(cpu) {
11072 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart7bb03bb2013-04-17 20:19:16 -040011073#ifdef CONFIG_X86
11074 cpuinfo = &cpu_data(cpu);
11075 cpup->phys_id = cpuinfo->phys_proc_id;
11076 cpup->core_id = cpuinfo->cpu_core_id;
James Smartd9954a22019-05-21 17:49:05 -070011077 if (lpfc_find_hyper(phba, cpu, cpup->phys_id, cpup->core_id))
11078 cpup->flag |= LPFC_CPU_MAP_HYPER;
James Smart7bb03bb2013-04-17 20:19:16 -040011079#else
11080 /* No distinction between CPUs for other platforms */
11081 cpup->phys_id = 0;
James Smart6a828b02019-01-28 11:14:31 -080011082 cpup->core_id = cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040011083#endif
James Smart7bb03bb2013-04-17 20:19:16 -040011084
James Smartb3295c22019-01-28 11:14:30 -080011085 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart3ad348d2019-08-14 16:56:43 -070011086 "3328 CPU %d physid %d coreid %d flag x%x\n",
11087 cpu, cpup->phys_id, cpup->core_id, cpup->flag);
James Smart6a828b02019-01-28 11:14:31 -080011088
11089 if (cpup->phys_id > max_phys_id)
11090 max_phys_id = cpup->phys_id;
11091 if (cpup->phys_id < min_phys_id)
11092 min_phys_id = cpup->phys_id;
11093
11094 if (cpup->core_id > max_core_id)
11095 max_core_id = cpup->core_id;
11096 if (cpup->core_id < min_core_id)
11097 min_core_id = cpup->core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040011098 }
James Smartb3295c22019-01-28 11:14:30 -080011099
James Smartd9954a22019-05-21 17:49:05 -070011100 /* After looking at each irq vector assigned to this pcidev, its
11101 * possible to see that not ALL CPUs have been accounted for.
James Smart657add42019-05-21 17:49:06 -070011102 * Next we will set any unassigned (unaffinitized) cpu map
11103 * entries to a IRQ on the same phys_id.
James Smartd9954a22019-05-21 17:49:05 -070011104 */
11105 first_cpu = cpumask_first(cpu_present_mask);
11106 start_cpu = first_cpu;
11107
11108 for_each_present_cpu(cpu) {
11109 cpup = &phba->sli4_hba.cpu_map[cpu];
11110
11111 /* Is this CPU entry unassigned */
11112 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
11113 /* Mark CPU as IRQ not assigned by the kernel */
11114 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
11115
James Smart657add42019-05-21 17:49:06 -070011116 /* If so, find a new_cpup thats on the the SAME
James Smartd9954a22019-05-21 17:49:05 -070011117 * phys_id as cpup. start_cpu will start where we
11118 * left off so all unassigned entries don't get assgined
11119 * the IRQ of the first entry.
11120 */
11121 new_cpu = start_cpu;
11122 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
11123 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
11124 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
James Smartdcaa2132019-11-04 16:57:06 -080011125 (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY) &&
James Smartd9954a22019-05-21 17:49:05 -070011126 (new_cpup->phys_id == cpup->phys_id))
11127 goto found_same;
11128 new_cpu = cpumask_next(
11129 new_cpu, cpu_present_mask);
11130 if (new_cpu == nr_cpumask_bits)
11131 new_cpu = first_cpu;
11132 }
11133 /* At this point, we leave the CPU as unassigned */
11134 continue;
11135found_same:
11136 /* We found a matching phys_id, so copy the IRQ info */
11137 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070011138
11139 /* Bump start_cpu to the next slot to minmize the
11140 * chance of having multiple unassigned CPU entries
11141 * selecting the same IRQ.
11142 */
11143 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
11144 if (start_cpu == nr_cpumask_bits)
11145 start_cpu = first_cpu;
11146
James Smart657add42019-05-21 17:49:06 -070011147 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070011148 "3337 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080011149 "eq %d from peer cpu %d same "
James Smartd9954a22019-05-21 17:49:05 -070011150 "phys_id (%d)\n",
James Smartdcaa2132019-11-04 16:57:06 -080011151 cpu, cpup->eq, new_cpu,
11152 cpup->phys_id);
James Smartd9954a22019-05-21 17:49:05 -070011153 }
11154 }
11155
11156 /* Set any unassigned cpu map entries to a IRQ on any phys_id */
11157 start_cpu = first_cpu;
11158
11159 for_each_present_cpu(cpu) {
11160 cpup = &phba->sli4_hba.cpu_map[cpu];
11161
11162 /* Is this entry unassigned */
11163 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
11164 /* Mark it as IRQ not assigned by the kernel */
11165 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
11166
James Smart657add42019-05-21 17:49:06 -070011167 /* If so, find a new_cpup thats on ANY phys_id
James Smartd9954a22019-05-21 17:49:05 -070011168 * as the cpup. start_cpu will start where we
11169 * left off so all unassigned entries don't get
11170 * assigned the IRQ of the first entry.
11171 */
11172 new_cpu = start_cpu;
11173 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
11174 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
11175 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
James Smartdcaa2132019-11-04 16:57:06 -080011176 (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY))
James Smartd9954a22019-05-21 17:49:05 -070011177 goto found_any;
11178 new_cpu = cpumask_next(
11179 new_cpu, cpu_present_mask);
11180 if (new_cpu == nr_cpumask_bits)
11181 new_cpu = first_cpu;
11182 }
11183 /* We should never leave an entry unassigned */
11184 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11185 "3339 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080011186 "eq %d UNASSIGNED\n",
11187 cpup->hdwq, cpup->eq);
James Smartd9954a22019-05-21 17:49:05 -070011188 continue;
11189found_any:
11190 /* We found an available entry, copy the IRQ info */
11191 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070011192
11193 /* Bump start_cpu to the next slot to minmize the
11194 * chance of having multiple unassigned CPU entries
11195 * selecting the same IRQ.
11196 */
11197 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
11198 if (start_cpu == nr_cpumask_bits)
11199 start_cpu = first_cpu;
11200
James Smart657add42019-05-21 17:49:06 -070011201 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070011202 "3338 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080011203 "eq %d from peer cpu %d (%d/%d)\n",
11204 cpu, cpup->eq, new_cpu,
James Smartd9954a22019-05-21 17:49:05 -070011205 new_cpup->phys_id, new_cpup->core_id);
11206 }
11207 }
James Smart657add42019-05-21 17:49:06 -070011208
James Smart3ad348d2019-08-14 16:56:43 -070011209 /* Assign hdwq indices that are unique across all cpus in the map
11210 * that are also FIRST_CPUs.
11211 */
11212 idx = 0;
11213 for_each_present_cpu(cpu) {
11214 cpup = &phba->sli4_hba.cpu_map[cpu];
11215
11216 /* Only FIRST IRQs get a hdwq index assignment. */
11217 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
11218 continue;
11219
11220 /* 1 to 1, the first LPFC_CPU_FIRST_IRQ cpus to a unique hdwq */
11221 cpup->hdwq = idx;
11222 idx++;
Anton Blanchardbc2736e2020-07-13 18:39:08 +100011223 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart3ad348d2019-08-14 16:56:43 -070011224 "3333 Set Affinity: CPU %d (phys %d core %d): "
James Smartdcaa2132019-11-04 16:57:06 -080011225 "hdwq %d eq %d flg x%x\n",
James Smart3ad348d2019-08-14 16:56:43 -070011226 cpu, cpup->phys_id, cpup->core_id,
James Smartdcaa2132019-11-04 16:57:06 -080011227 cpup->hdwq, cpup->eq, cpup->flag);
James Smart3ad348d2019-08-14 16:56:43 -070011228 }
James Smartbc227dd2019-11-11 15:03:59 -080011229 /* Associate a hdwq with each cpu_map entry
James Smart657add42019-05-21 17:49:06 -070011230 * This will be 1 to 1 - hdwq to cpu, unless there are less
11231 * hardware queues then CPUs. For that case we will just round-robin
11232 * the available hardware queues as they get assigned to CPUs.
James Smart3ad348d2019-08-14 16:56:43 -070011233 * The next_idx is the idx from the FIRST_CPU loop above to account
11234 * for irq_chann < hdwq. The idx is used for round-robin assignments
11235 * and needs to start at 0.
James Smart657add42019-05-21 17:49:06 -070011236 */
James Smart3ad348d2019-08-14 16:56:43 -070011237 next_idx = idx;
James Smart657add42019-05-21 17:49:06 -070011238 start_cpu = 0;
James Smart3ad348d2019-08-14 16:56:43 -070011239 idx = 0;
James Smart657add42019-05-21 17:49:06 -070011240 for_each_present_cpu(cpu) {
11241 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart657add42019-05-21 17:49:06 -070011242
James Smart3ad348d2019-08-14 16:56:43 -070011243 /* FIRST cpus are already mapped. */
11244 if (cpup->flag & LPFC_CPU_FIRST_IRQ)
11245 continue;
James Smart657add42019-05-21 17:49:06 -070011246
James Smart3ad348d2019-08-14 16:56:43 -070011247 /* If the cfg_irq_chann < cfg_hdw_queue, set the hdwq
11248 * of the unassigned cpus to the next idx so that all
11249 * hdw queues are fully utilized.
11250 */
11251 if (next_idx < phba->cfg_hdw_queue) {
11252 cpup->hdwq = next_idx;
11253 next_idx++;
11254 continue;
James Smart657add42019-05-21 17:49:06 -070011255 }
James Smart3ad348d2019-08-14 16:56:43 -070011256
11257 /* Not a First CPU and all hdw_queues are used. Reuse a
11258 * Hardware Queue for another CPU, so be smart about it
11259 * and pick one that has its IRQ/EQ mapped to the same phys_id
11260 * (CPU package) and core_id.
11261 */
11262 new_cpu = start_cpu;
11263 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
11264 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
11265 if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
11266 new_cpup->phys_id == cpup->phys_id &&
11267 new_cpup->core_id == cpup->core_id) {
11268 goto found_hdwq;
11269 }
11270 new_cpu = cpumask_next(new_cpu, cpu_present_mask);
11271 if (new_cpu == nr_cpumask_bits)
11272 new_cpu = first_cpu;
11273 }
11274
11275 /* If we can't match both phys_id and core_id,
11276 * settle for just a phys_id match.
11277 */
11278 new_cpu = start_cpu;
11279 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
11280 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
11281 if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
11282 new_cpup->phys_id == cpup->phys_id)
11283 goto found_hdwq;
11284
11285 new_cpu = cpumask_next(new_cpu, cpu_present_mask);
11286 if (new_cpu == nr_cpumask_bits)
11287 new_cpu = first_cpu;
11288 }
11289
11290 /* Otherwise just round robin on cfg_hdw_queue */
11291 cpup->hdwq = idx % phba->cfg_hdw_queue;
11292 idx++;
11293 goto logit;
11294 found_hdwq:
11295 /* We found an available entry, copy the IRQ info */
11296 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
11297 if (start_cpu == nr_cpumask_bits)
11298 start_cpu = first_cpu;
11299 cpup->hdwq = new_cpup->hdwq;
11300 logit:
Anton Blanchardbc2736e2020-07-13 18:39:08 +100011301 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart657add42019-05-21 17:49:06 -070011302 "3335 Set Affinity: CPU %d (phys %d core %d): "
James Smartdcaa2132019-11-04 16:57:06 -080011303 "hdwq %d eq %d flg x%x\n",
James Smart657add42019-05-21 17:49:06 -070011304 cpu, cpup->phys_id, cpup->core_id,
James Smartdcaa2132019-11-04 16:57:06 -080011305 cpup->hdwq, cpup->eq, cpup->flag);
James Smart657add42019-05-21 17:49:06 -070011306 }
11307
James Smartbc227dd2019-11-11 15:03:59 -080011308 /*
11309 * Initialize the cpu_map slots for not-present cpus in case
11310 * a cpu is hot-added. Perform a simple hdwq round robin assignment.
11311 */
11312 idx = 0;
11313 for_each_possible_cpu(cpu) {
11314 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart840eda92020-03-22 11:13:00 -070011315#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
11316 c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, cpu);
11317 c_stat->hdwq_no = cpup->hdwq;
11318#endif
James Smartbc227dd2019-11-11 15:03:59 -080011319 if (cpup->hdwq != LPFC_VECTOR_MAP_EMPTY)
11320 continue;
11321
11322 cpup->hdwq = idx++ % phba->cfg_hdw_queue;
James Smart840eda92020-03-22 11:13:00 -070011323#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
11324 c_stat->hdwq_no = cpup->hdwq;
11325#endif
James Smartbc227dd2019-11-11 15:03:59 -080011326 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11327 "3340 Set Affinity: not present "
11328 "CPU %d hdwq %d\n",
11329 cpu, cpup->hdwq);
James Smart657add42019-05-21 17:49:06 -070011330 }
11331
11332 /* The cpu_map array will be used later during initialization
11333 * when EQ / CQ / WQs are allocated and configured.
11334 */
James Smartb3295c22019-01-28 11:14:30 -080011335 return;
James Smart7bb03bb2013-04-17 20:19:16 -040011336}
James Smart7bb03bb2013-04-17 20:19:16 -040011337
11338/**
James Smart93a4d6f2019-11-04 16:57:05 -080011339 * lpfc_cpuhp_get_eq
11340 *
11341 * @phba: pointer to lpfc hba data structure.
11342 * @cpu: cpu going offline
Lee Jonesfe614ac2020-07-23 13:24:22 +010011343 * @eqlist: eq list to append to
James Smart93a4d6f2019-11-04 16:57:05 -080011344 */
James Smarta99c8072020-01-27 16:23:06 -080011345static int
James Smart93a4d6f2019-11-04 16:57:05 -080011346lpfc_cpuhp_get_eq(struct lpfc_hba *phba, unsigned int cpu,
11347 struct list_head *eqlist)
11348{
James Smart93a4d6f2019-11-04 16:57:05 -080011349 const struct cpumask *maskp;
11350 struct lpfc_queue *eq;
James Smarta99c8072020-01-27 16:23:06 -080011351 struct cpumask *tmp;
James Smart93a4d6f2019-11-04 16:57:05 -080011352 u16 idx;
11353
James Smarta99c8072020-01-27 16:23:06 -080011354 tmp = kzalloc(cpumask_size(), GFP_KERNEL);
11355 if (!tmp)
11356 return -ENOMEM;
11357
James Smart93a4d6f2019-11-04 16:57:05 -080011358 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
11359 maskp = pci_irq_get_affinity(phba->pcidev, idx);
11360 if (!maskp)
11361 continue;
11362 /*
11363 * if irq is not affinitized to the cpu going
11364 * then we don't need to poll the eq attached
11365 * to it.
11366 */
James Smarta99c8072020-01-27 16:23:06 -080011367 if (!cpumask_and(tmp, maskp, cpumask_of(cpu)))
James Smart93a4d6f2019-11-04 16:57:05 -080011368 continue;
11369 /* get the cpus that are online and are affini-
11370 * tized to this irq vector. If the count is
11371 * more than 1 then cpuhp is not going to shut-
11372 * down this vector. Since this cpu has not
11373 * gone offline yet, we need >1.
11374 */
James Smarta99c8072020-01-27 16:23:06 -080011375 cpumask_and(tmp, maskp, cpu_online_mask);
11376 if (cpumask_weight(tmp) > 1)
James Smart93a4d6f2019-11-04 16:57:05 -080011377 continue;
11378
11379 /* Now that we have an irq to shutdown, get the eq
11380 * mapped to this irq. Note: multiple hdwq's in
11381 * the software can share an eq, but eventually
11382 * only eq will be mapped to this vector
11383 */
James Smartdcaa2132019-11-04 16:57:06 -080011384 eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
11385 list_add(&eq->_poll_list, eqlist);
James Smart93a4d6f2019-11-04 16:57:05 -080011386 }
James Smarta99c8072020-01-27 16:23:06 -080011387 kfree(tmp);
11388 return 0;
James Smart93a4d6f2019-11-04 16:57:05 -080011389}
11390
11391static void __lpfc_cpuhp_remove(struct lpfc_hba *phba)
11392{
11393 if (phba->sli_rev != LPFC_SLI_REV4)
11394 return;
11395
11396 cpuhp_state_remove_instance_nocalls(lpfc_cpuhp_state,
11397 &phba->cpuhp);
11398 /*
11399 * unregistering the instance doesn't stop the polling
11400 * timer. Wait for the poll timer to retire.
11401 */
11402 synchronize_rcu();
11403 del_timer_sync(&phba->cpuhp_poll_timer);
11404}
11405
11406static void lpfc_cpuhp_remove(struct lpfc_hba *phba)
11407{
11408 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
11409 return;
11410
11411 __lpfc_cpuhp_remove(phba);
11412}
11413
11414static void lpfc_cpuhp_add(struct lpfc_hba *phba)
11415{
11416 if (phba->sli_rev != LPFC_SLI_REV4)
11417 return;
11418
11419 rcu_read_lock();
11420
James Smartf861f592020-03-22 11:12:54 -070011421 if (!list_empty(&phba->poll_list))
James Smart93a4d6f2019-11-04 16:57:05 -080011422 mod_timer(&phba->cpuhp_poll_timer,
11423 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
James Smart93a4d6f2019-11-04 16:57:05 -080011424
11425 rcu_read_unlock();
11426
11427 cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state,
11428 &phba->cpuhp);
11429}
11430
11431static int __lpfc_cpuhp_checks(struct lpfc_hba *phba, int *retval)
11432{
11433 if (phba->pport->load_flag & FC_UNLOADING) {
11434 *retval = -EAGAIN;
11435 return true;
11436 }
11437
11438 if (phba->sli_rev != LPFC_SLI_REV4) {
11439 *retval = 0;
11440 return true;
11441 }
11442
11443 /* proceed with the hotplug */
11444 return false;
11445}
11446
James Smartdcaa2132019-11-04 16:57:06 -080011447/**
11448 * lpfc_irq_set_aff - set IRQ affinity
11449 * @eqhdl: EQ handle
11450 * @cpu: cpu to set affinity
11451 *
11452 **/
11453static inline void
11454lpfc_irq_set_aff(struct lpfc_hba_eq_hdl *eqhdl, unsigned int cpu)
11455{
11456 cpumask_clear(&eqhdl->aff_mask);
11457 cpumask_set_cpu(cpu, &eqhdl->aff_mask);
11458 irq_set_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
11459 irq_set_affinity_hint(eqhdl->irq, &eqhdl->aff_mask);
11460}
11461
11462/**
11463 * lpfc_irq_clear_aff - clear IRQ affinity
11464 * @eqhdl: EQ handle
11465 *
11466 **/
11467static inline void
11468lpfc_irq_clear_aff(struct lpfc_hba_eq_hdl *eqhdl)
11469{
11470 cpumask_clear(&eqhdl->aff_mask);
11471 irq_clear_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
James Smartdcaa2132019-11-04 16:57:06 -080011472}
11473
11474/**
11475 * lpfc_irq_rebalance - rebalances IRQ affinity according to cpuhp event
11476 * @phba: pointer to HBA context object.
11477 * @cpu: cpu going offline/online
11478 * @offline: true, cpu is going offline. false, cpu is coming online.
11479 *
11480 * If cpu is going offline, we'll try our best effort to find the next
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011481 * online cpu on the phba's original_mask and migrate all offlining IRQ
11482 * affinities.
James Smartdcaa2132019-11-04 16:57:06 -080011483 *
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011484 * If cpu is coming online, reaffinitize the IRQ back to the onlining cpu.
James Smartdcaa2132019-11-04 16:57:06 -080011485 *
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011486 * Note: Call only if NUMA or NHT mode is enabled, otherwise rely on
James Smartdcaa2132019-11-04 16:57:06 -080011487 * PCI_IRQ_AFFINITY to auto-manage IRQ affinity.
11488 *
11489 **/
11490static void
11491lpfc_irq_rebalance(struct lpfc_hba *phba, unsigned int cpu, bool offline)
11492{
11493 struct lpfc_vector_map_info *cpup;
11494 struct cpumask *aff_mask;
11495 unsigned int cpu_select, cpu_next, idx;
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011496 const struct cpumask *orig_mask;
James Smartdcaa2132019-11-04 16:57:06 -080011497
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011498 if (phba->irq_chann_mode == NORMAL_MODE)
James Smartdcaa2132019-11-04 16:57:06 -080011499 return;
11500
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011501 orig_mask = &phba->sli4_hba.irq_aff_mask;
James Smartdcaa2132019-11-04 16:57:06 -080011502
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011503 if (!cpumask_test_cpu(cpu, orig_mask))
James Smartdcaa2132019-11-04 16:57:06 -080011504 return;
11505
11506 cpup = &phba->sli4_hba.cpu_map[cpu];
11507
11508 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
11509 return;
11510
11511 if (offline) {
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011512 /* Find next online CPU on original mask */
11513 cpu_next = cpumask_next_wrap(cpu, orig_mask, cpu, true);
11514 cpu_select = lpfc_next_online_cpu(orig_mask, cpu_next);
James Smartdcaa2132019-11-04 16:57:06 -080011515
11516 /* Found a valid CPU */
11517 if ((cpu_select < nr_cpu_ids) && (cpu_select != cpu)) {
11518 /* Go through each eqhdl and ensure offlining
11519 * cpu aff_mask is migrated
11520 */
11521 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
11522 aff_mask = lpfc_get_aff_mask(idx);
11523
11524 /* Migrate affinity */
11525 if (cpumask_test_cpu(cpu, aff_mask))
11526 lpfc_irq_set_aff(lpfc_get_eq_hdl(idx),
11527 cpu_select);
11528 }
11529 } else {
11530 /* Rely on irqbalance if no online CPUs left on NUMA */
11531 for (idx = 0; idx < phba->cfg_irq_chann; idx++)
11532 lpfc_irq_clear_aff(lpfc_get_eq_hdl(idx));
11533 }
11534 } else {
11535 /* Migrate affinity back to this CPU */
11536 lpfc_irq_set_aff(lpfc_get_eq_hdl(cpup->eq), cpu);
11537 }
11538}
11539
James Smart93a4d6f2019-11-04 16:57:05 -080011540static int lpfc_cpu_offline(unsigned int cpu, struct hlist_node *node)
11541{
11542 struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
11543 struct lpfc_queue *eq, *next;
11544 LIST_HEAD(eqlist);
11545 int retval;
11546
11547 if (!phba) {
11548 WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
11549 return 0;
11550 }
11551
11552 if (__lpfc_cpuhp_checks(phba, &retval))
11553 return retval;
11554
James Smartdcaa2132019-11-04 16:57:06 -080011555 lpfc_irq_rebalance(phba, cpu, true);
11556
James Smarta99c8072020-01-27 16:23:06 -080011557 retval = lpfc_cpuhp_get_eq(phba, cpu, &eqlist);
11558 if (retval)
11559 return retval;
James Smart93a4d6f2019-11-04 16:57:05 -080011560
11561 /* start polling on these eq's */
11562 list_for_each_entry_safe(eq, next, &eqlist, _poll_list) {
11563 list_del_init(&eq->_poll_list);
11564 lpfc_sli4_start_polling(eq);
11565 }
11566
11567 return 0;
11568}
11569
11570static int lpfc_cpu_online(unsigned int cpu, struct hlist_node *node)
11571{
11572 struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
11573 struct lpfc_queue *eq, *next;
11574 unsigned int n;
11575 int retval;
11576
11577 if (!phba) {
11578 WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
11579 return 0;
11580 }
11581
11582 if (__lpfc_cpuhp_checks(phba, &retval))
11583 return retval;
11584
James Smartdcaa2132019-11-04 16:57:06 -080011585 lpfc_irq_rebalance(phba, cpu, false);
11586
James Smart93a4d6f2019-11-04 16:57:05 -080011587 list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list) {
11588 n = lpfc_find_cpu_handle(phba, eq->hdwq, LPFC_FIND_BY_HDWQ);
11589 if (n == cpu)
11590 lpfc_sli4_stop_polling(eq);
11591 }
11592
11593 return 0;
11594}
11595
11596/**
James Smartda0436e2009-05-22 14:51:39 -040011597 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
11598 * @phba: pointer to lpfc hba data structure.
11599 *
11600 * This routine is invoked to enable the MSI-X interrupt vectors to device
James Smartdcaa2132019-11-04 16:57:06 -080011601 * with SLI-4 interface spec. It also allocates MSI-X vectors and maps them
11602 * to cpus on the system.
11603 *
11604 * When cfg_irq_numa is enabled, the adapter will only allocate vectors for
11605 * the number of cpus on the same numa node as this adapter. The vectors are
11606 * allocated without requesting OS affinity mapping. A vector will be
11607 * allocated and assigned to each online and offline cpu. If the cpu is
11608 * online, then affinity will be set to that cpu. If the cpu is offline, then
11609 * affinity will be set to the nearest peer cpu within the numa node that is
11610 * online. If there are no online cpus within the numa node, affinity is not
11611 * assigned and the OS may do as it pleases. Note: cpu vector affinity mapping
11612 * is consistent with the way cpu online/offline is handled when cfg_irq_numa is
11613 * configured.
11614 *
11615 * If numa mode is not enabled and there is more than 1 vector allocated, then
11616 * the driver relies on the managed irq interface where the OS assigns vector to
11617 * cpu affinity. The driver will then use that affinity mapping to setup its
11618 * cpu mapping table.
James Smartda0436e2009-05-22 14:51:39 -040011619 *
11620 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011621 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011622 * other values - error
11623 **/
11624static int
11625lpfc_sli4_enable_msix(struct lpfc_hba *phba)
11626{
James Smart75baf692010-06-08 18:31:21 -040011627 int vectors, rc, index;
James Smartb83d0052017-06-01 21:07:05 -070011628 char *name;
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011629 const struct cpumask *aff_mask = NULL;
James Smartdcaa2132019-11-04 16:57:06 -080011630 unsigned int cpu = 0, cpu_cnt = 0, cpu_select = nr_cpu_ids;
Dick Kennedy17105d92020-07-06 13:42:30 -070011631 struct lpfc_vector_map_info *cpup;
James Smartdcaa2132019-11-04 16:57:06 -080011632 struct lpfc_hba_eq_hdl *eqhdl;
11633 const struct cpumask *maskp;
James Smartdcaa2132019-11-04 16:57:06 -080011634 unsigned int flags = PCI_IRQ_MSIX;
James Smartda0436e2009-05-22 14:51:39 -040011635
11636 /* Set up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080011637 vectors = phba->cfg_irq_chann;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011638
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011639 if (phba->irq_chann_mode != NORMAL_MODE)
11640 aff_mask = &phba->sli4_hba.irq_aff_mask;
11641
11642 if (aff_mask) {
11643 cpu_cnt = cpumask_weight(aff_mask);
James Smartdcaa2132019-11-04 16:57:06 -080011644 vectors = min(phba->cfg_irq_chann, cpu_cnt);
11645
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011646 /* cpu: iterates over aff_mask including offline or online
11647 * cpu_select: iterates over online aff_mask to set affinity
James Smartdcaa2132019-11-04 16:57:06 -080011648 */
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011649 cpu = cpumask_first(aff_mask);
11650 cpu_select = lpfc_next_online_cpu(aff_mask, cpu);
James Smartdcaa2132019-11-04 16:57:06 -080011651 } else {
11652 flags |= PCI_IRQ_AFFINITY;
11653 }
11654
11655 rc = pci_alloc_irq_vectors(phba->pcidev, 1, vectors, flags);
Alexander Gordeev4f871e12014-09-03 12:56:29 -040011656 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -040011657 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11658 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020011659 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -040011660 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -040011661 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -040011662
James Smart7bb03bb2013-04-17 20:19:16 -040011663 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -040011664 for (index = 0; index < vectors; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080011665 eqhdl = lpfc_get_eq_hdl(index);
11666 name = eqhdl->handler_name;
James Smartb83d0052017-06-01 21:07:05 -070011667 memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
11668 snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -040011669 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -040011670
James Smartdcaa2132019-11-04 16:57:06 -080011671 eqhdl->idx = index;
James Smart7370d102019-01-28 11:14:20 -080011672 rc = request_irq(pci_irq_vector(phba->pcidev, index),
11673 &lpfc_sli4_hba_intr_handler, 0,
James Smartdcaa2132019-11-04 16:57:06 -080011674 name, eqhdl);
James Smartda0436e2009-05-22 14:51:39 -040011675 if (rc) {
11676 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11677 "0486 MSI-X fast-path (%d) "
11678 "request_irq failed (%d)\n", index, rc);
11679 goto cfg_fail_out;
11680 }
James Smartdcaa2132019-11-04 16:57:06 -080011681
11682 eqhdl->irq = pci_irq_vector(phba->pcidev, index);
11683
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011684 if (aff_mask) {
James Smartdcaa2132019-11-04 16:57:06 -080011685 /* If found a neighboring online cpu, set affinity */
11686 if (cpu_select < nr_cpu_ids)
11687 lpfc_irq_set_aff(eqhdl, cpu_select);
11688
11689 /* Assign EQ to cpu_map */
11690 lpfc_assign_eq_map_info(phba, index,
11691 LPFC_CPU_FIRST_IRQ,
11692 cpu);
11693
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011694 /* Iterate to next offline or online cpu in aff_mask */
11695 cpu = cpumask_next(cpu, aff_mask);
James Smartdcaa2132019-11-04 16:57:06 -080011696
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011697 /* Find next online cpu in aff_mask to set affinity */
11698 cpu_select = lpfc_next_online_cpu(aff_mask, cpu);
James Smartdcaa2132019-11-04 16:57:06 -080011699 } else if (vectors == 1) {
11700 cpu = cpumask_first(cpu_present_mask);
11701 lpfc_assign_eq_map_info(phba, index, LPFC_CPU_FIRST_IRQ,
11702 cpu);
11703 } else {
11704 maskp = pci_irq_get_affinity(phba->pcidev, index);
11705
James Smartdcaa2132019-11-04 16:57:06 -080011706 /* Loop through all CPUs associated with vector index */
11707 for_each_cpu_and(cpu, maskp, cpu_present_mask) {
Dick Kennedy17105d92020-07-06 13:42:30 -070011708 cpup = &phba->sli4_hba.cpu_map[cpu];
11709
James Smartdcaa2132019-11-04 16:57:06 -080011710 /* If this is the first CPU thats assigned to
11711 * this vector, set LPFC_CPU_FIRST_IRQ.
Dick Kennedy17105d92020-07-06 13:42:30 -070011712 *
11713 * With certain platforms its possible that irq
11714 * vectors are affinitized to all the cpu's.
11715 * This can result in each cpu_map.eq to be set
11716 * to the last vector, resulting in overwrite
11717 * of all the previous cpu_map.eq. Ensure that
11718 * each vector receives a place in cpu_map.
11719 * Later call to lpfc_cpu_affinity_check will
11720 * ensure we are nicely balanced out.
James Smartdcaa2132019-11-04 16:57:06 -080011721 */
Dick Kennedy17105d92020-07-06 13:42:30 -070011722 if (cpup->eq != LPFC_VECTOR_MAP_EMPTY)
11723 continue;
James Smartdcaa2132019-11-04 16:57:06 -080011724 lpfc_assign_eq_map_info(phba, index,
Dick Kennedy17105d92020-07-06 13:42:30 -070011725 LPFC_CPU_FIRST_IRQ,
James Smartdcaa2132019-11-04 16:57:06 -080011726 cpu);
Dick Kennedy17105d92020-07-06 13:42:30 -070011727 break;
James Smartdcaa2132019-11-04 16:57:06 -080011728 }
11729 }
James Smartda0436e2009-05-22 14:51:39 -040011730 }
11731
James Smart6a828b02019-01-28 11:14:31 -080011732 if (vectors != phba->cfg_irq_chann) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011733 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart82c3e9b2012-09-29 11:29:50 -040011734 "3238 Reducing IO channels to match number of "
11735 "MSI-X vectors, requested %d got %d\n",
James Smart6a828b02019-01-28 11:14:31 -080011736 phba->cfg_irq_chann, vectors);
11737 if (phba->cfg_irq_chann > vectors)
11738 phba->cfg_irq_chann = vectors;
James Smart82c3e9b2012-09-29 11:29:50 -040011739 }
James Smart7bb03bb2013-04-17 20:19:16 -040011740
James Smartda0436e2009-05-22 14:51:39 -040011741 return rc;
11742
11743cfg_fail_out:
11744 /* free the irq already requested */
James Smartdcaa2132019-11-04 16:57:06 -080011745 for (--index; index >= 0; index--) {
11746 eqhdl = lpfc_get_eq_hdl(index);
11747 lpfc_irq_clear_aff(eqhdl);
11748 irq_set_affinity_hint(eqhdl->irq, NULL);
11749 free_irq(eqhdl->irq, eqhdl);
11750 }
James Smartda0436e2009-05-22 14:51:39 -040011751
James Smartda0436e2009-05-22 14:51:39 -040011752 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011753 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020011754
11755vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -040011756 return rc;
11757}
11758
11759/**
James Smartda0436e2009-05-22 14:51:39 -040011760 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
11761 * @phba: pointer to lpfc hba data structure.
11762 *
11763 * This routine is invoked to enable the MSI interrupt mode to device with
James Smart07b1b912019-08-14 16:56:58 -070011764 * SLI-4 interface spec. The kernel function pci_alloc_irq_vectors() is
11765 * called to enable the MSI vector. The device driver is responsible for
11766 * calling the request_irq() to register MSI vector with a interrupt the
11767 * handler, which is done in this function.
James Smartda0436e2009-05-22 14:51:39 -040011768 *
11769 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011770 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011771 * other values - error
11772 **/
11773static int
11774lpfc_sli4_enable_msi(struct lpfc_hba *phba)
11775{
11776 int rc, index;
James Smartdcaa2132019-11-04 16:57:06 -080011777 unsigned int cpu;
11778 struct lpfc_hba_eq_hdl *eqhdl;
James Smartda0436e2009-05-22 14:51:39 -040011779
James Smart07b1b912019-08-14 16:56:58 -070011780 rc = pci_alloc_irq_vectors(phba->pcidev, 1, 1,
11781 PCI_IRQ_MSI | PCI_IRQ_AFFINITY);
11782 if (rc > 0)
James Smartda0436e2009-05-22 14:51:39 -040011783 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11784 "0487 PCI enable MSI mode success.\n");
11785 else {
11786 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11787 "0488 PCI enable MSI mode failed (%d)\n", rc);
James Smart07b1b912019-08-14 16:56:58 -070011788 return rc ? rc : -1;
James Smartda0436e2009-05-22 14:51:39 -040011789 }
11790
11791 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040011792 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -040011793 if (rc) {
James Smart07b1b912019-08-14 16:56:58 -070011794 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040011795 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11796 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -040011797 return rc;
James Smartda0436e2009-05-22 14:51:39 -040011798 }
11799
James Smartdcaa2132019-11-04 16:57:06 -080011800 eqhdl = lpfc_get_eq_hdl(0);
11801 eqhdl->irq = pci_irq_vector(phba->pcidev, 0);
11802
11803 cpu = cpumask_first(cpu_present_mask);
11804 lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ, cpu);
11805
James Smart6a828b02019-01-28 11:14:31 -080011806 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080011807 eqhdl = lpfc_get_eq_hdl(index);
11808 eqhdl->idx = index;
James Smartda0436e2009-05-22 14:51:39 -040011809 }
11810
James Smart75baf692010-06-08 18:31:21 -040011811 return 0;
James Smartda0436e2009-05-22 14:51:39 -040011812}
11813
11814/**
James Smartda0436e2009-05-22 14:51:39 -040011815 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
11816 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +010011817 * @cfg_mode: Interrupt configuration mode (INTx, MSI or MSI-X).
James Smartda0436e2009-05-22 14:51:39 -040011818 *
11819 * This routine is invoked to enable device interrupt and associate driver's
11820 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
11821 * interface spec. Depends on the interrupt mode configured to the driver,
11822 * the driver will try to fallback from the configured interrupt mode to an
11823 * interrupt mode which is supported by the platform, kernel, and device in
11824 * the order of:
11825 * MSI-X -> MSI -> IRQ.
11826 *
11827 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011828 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011829 * other values - error
11830 **/
11831static uint32_t
11832lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
11833{
11834 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -080011835 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -040011836
11837 if (cfg_mode == 2) {
11838 /* Preparation before conf_msi mbox cmd */
11839 retval = 0;
11840 if (!retval) {
11841 /* Now, try to enable MSI-X interrupt mode */
11842 retval = lpfc_sli4_enable_msix(phba);
11843 if (!retval) {
11844 /* Indicate initialization to MSI-X mode */
11845 phba->intr_type = MSIX;
11846 intr_mode = 2;
11847 }
11848 }
11849 }
11850
11851 /* Fallback to MSI if MSI-X initialization failed */
11852 if (cfg_mode >= 1 && phba->intr_type == NONE) {
11853 retval = lpfc_sli4_enable_msi(phba);
11854 if (!retval) {
11855 /* Indicate initialization to MSI mode */
11856 phba->intr_type = MSI;
11857 intr_mode = 1;
11858 }
11859 }
11860
11861 /* Fallback to INTx if both MSI-X/MSI initalization failed */
11862 if (phba->intr_type == NONE) {
11863 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
11864 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
11865 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -080011866 struct lpfc_hba_eq_hdl *eqhdl;
James Smartdcaa2132019-11-04 16:57:06 -080011867 unsigned int cpu;
James Smart895427b2017-02-12 13:52:30 -080011868
James Smartda0436e2009-05-22 14:51:39 -040011869 /* Indicate initialization to INTx mode */
11870 phba->intr_type = INTx;
11871 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -080011872
James Smartdcaa2132019-11-04 16:57:06 -080011873 eqhdl = lpfc_get_eq_hdl(0);
11874 eqhdl->irq = pci_irq_vector(phba->pcidev, 0);
11875
11876 cpu = cpumask_first(cpu_present_mask);
11877 lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ,
11878 cpu);
James Smart6a828b02019-01-28 11:14:31 -080011879 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
James Smartdcaa2132019-11-04 16:57:06 -080011880 eqhdl = lpfc_get_eq_hdl(idx);
James Smart895427b2017-02-12 13:52:30 -080011881 eqhdl->idx = idx;
James Smart1ba981f2014-02-20 09:56:45 -050011882 }
James Smartda0436e2009-05-22 14:51:39 -040011883 }
11884 }
11885 return intr_mode;
11886}
11887
11888/**
11889 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
11890 * @phba: pointer to lpfc hba data structure.
11891 *
11892 * This routine is invoked to disable device interrupt and disassociate
11893 * the driver's interrupt handler(s) from interrupt vector(s) to device
11894 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
11895 * will release the interrupt vector(s) for the message signaled interrupt.
11896 **/
11897static void
11898lpfc_sli4_disable_intr(struct lpfc_hba *phba)
11899{
11900 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011901 if (phba->intr_type == MSIX) {
11902 int index;
James Smartdcaa2132019-11-04 16:57:06 -080011903 struct lpfc_hba_eq_hdl *eqhdl;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011904
11905 /* Free up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080011906 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080011907 eqhdl = lpfc_get_eq_hdl(index);
11908 lpfc_irq_clear_aff(eqhdl);
11909 irq_set_affinity_hint(eqhdl->irq, NULL);
11910 free_irq(eqhdl->irq, eqhdl);
James Smartb3295c22019-01-28 11:14:30 -080011911 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011912 } else {
James Smartda0436e2009-05-22 14:51:39 -040011913 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011914 }
11915
11916 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040011917
11918 /* Reset interrupt management states */
11919 phba->intr_type = NONE;
11920 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -040011921}
11922
11923/**
James Smart3772a992009-05-22 14:50:54 -040011924 * lpfc_unset_hba - Unset SLI3 hba device initialization
11925 * @phba: pointer to lpfc hba data structure.
11926 *
11927 * This routine is invoked to unset the HBA device initialization steps to
11928 * a device with SLI-3 interface spec.
11929 **/
11930static void
11931lpfc_unset_hba(struct lpfc_hba *phba)
11932{
11933 struct lpfc_vport *vport = phba->pport;
11934 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
11935
11936 spin_lock_irq(shost->host_lock);
11937 vport->load_flag |= FC_UNLOADING;
11938 spin_unlock_irq(shost->host_lock);
11939
James Smart72859902012-01-18 16:25:38 -050011940 kfree(phba->vpi_bmask);
11941 kfree(phba->vpi_ids);
11942
James Smart3772a992009-05-22 14:50:54 -040011943 lpfc_stop_hba_timers(phba);
11944
11945 phba->pport->work_port_events = 0;
11946
11947 lpfc_sli_hba_down(phba);
11948
11949 lpfc_sli_brdrestart(phba);
11950
11951 lpfc_sli_disable_intr(phba);
11952
11953 return;
11954}
11955
11956/**
James Smart5af5eee2010-10-22 11:06:38 -040011957 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
11958 * @phba: Pointer to HBA context object.
11959 *
11960 * This function is called in the SLI4 code path to wait for completion
11961 * of device's XRIs exchange busy. It will check the XRI exchange busy
11962 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
11963 * that, it will check the XRI exchange busy on outstanding FCP and ELS
11964 * I/Os every 30 seconds, log error message, and wait forever. Only when
11965 * all XRI exchange busy complete, the driver unload shall proceed with
11966 * invoking the function reset ioctl mailbox command to the CNA and the
11967 * the rest of the driver unload resource release.
11968 **/
11969static void
11970lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
11971{
James Smart5e5b5112019-01-28 11:14:22 -080011972 struct lpfc_sli4_hdw_queue *qp;
James Smartc00f62e2019-08-14 16:57:11 -070011973 int idx, ccnt;
James Smart5af5eee2010-10-22 11:06:38 -040011974 int wait_time = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011975 int io_xri_cmpl = 1;
James Smart86c67372017-04-21 16:05:04 -070011976 int nvmet_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -040011977 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
11978
James Smartc3725bd2017-11-20 16:00:42 -080011979 /* Driver just aborted IOs during the hba_unset process. Pause
11980 * here to give the HBA time to complete the IO and get entries
11981 * into the abts lists.
11982 */
11983 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
11984
11985 /* Wait for NVME pending IO to flush back to transport. */
11986 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11987 lpfc_nvme_wait_for_io_drain(phba);
11988
James Smart5e5b5112019-01-28 11:14:22 -080011989 ccnt = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011990 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11991 qp = &phba->sli4_hba.hdwq[idx];
James Smartc00f62e2019-08-14 16:57:11 -070011992 io_xri_cmpl = list_empty(&qp->lpfc_abts_io_buf_list);
11993 if (!io_xri_cmpl) /* if list is NOT empty */
11994 ccnt++;
James Smart5e5b5112019-01-28 11:14:22 -080011995 }
11996 if (ccnt)
11997 io_xri_cmpl = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011998
James Smart86c67372017-04-21 16:05:04 -070011999 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070012000 nvmet_xri_cmpl =
12001 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
12002 }
James Smart895427b2017-02-12 13:52:30 -080012003
James Smartc00f62e2019-08-14 16:57:11 -070012004 while (!els_xri_cmpl || !io_xri_cmpl || !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -040012005 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart68c9b552018-06-26 08:24:25 -070012006 if (!nvmet_xri_cmpl)
Dick Kennedy372c1872020-06-30 14:50:00 -070012007 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart68c9b552018-06-26 08:24:25 -070012008 "6424 NVMET XRI exchange busy "
12009 "wait time: %d seconds.\n",
12010 wait_time/1000);
James Smart5e5b5112019-01-28 11:14:22 -080012011 if (!io_xri_cmpl)
Dick Kennedy372c1872020-06-30 14:50:00 -070012012 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartc00f62e2019-08-14 16:57:11 -070012013 "6100 IO XRI exchange busy "
James Smart5af5eee2010-10-22 11:06:38 -040012014 "wait time: %d seconds.\n",
12015 wait_time/1000);
12016 if (!els_xri_cmpl)
Dick Kennedy372c1872020-06-30 14:50:00 -070012017 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5af5eee2010-10-22 11:06:38 -040012018 "2878 ELS XRI exchange busy "
12019 "wait time: %d seconds.\n",
12020 wait_time/1000);
12021 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
12022 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
12023 } else {
12024 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
12025 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
12026 }
James Smart5e5b5112019-01-28 11:14:22 -080012027
12028 ccnt = 0;
James Smart5e5b5112019-01-28 11:14:22 -080012029 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
12030 qp = &phba->sli4_hba.hdwq[idx];
James Smartc00f62e2019-08-14 16:57:11 -070012031 io_xri_cmpl = list_empty(
12032 &qp->lpfc_abts_io_buf_list);
12033 if (!io_xri_cmpl) /* if list is NOT empty */
12034 ccnt++;
James Smart5e5b5112019-01-28 11:14:22 -080012035 }
12036 if (ccnt)
12037 io_xri_cmpl = 0;
James Smart5e5b5112019-01-28 11:14:22 -080012038
James Smart86c67372017-04-21 16:05:04 -070012039 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070012040 nvmet_xri_cmpl = list_empty(
12041 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
12042 }
James Smart5af5eee2010-10-22 11:06:38 -040012043 els_xri_cmpl =
12044 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -080012045
James Smart5af5eee2010-10-22 11:06:38 -040012046 }
12047}
12048
12049/**
James Smartda0436e2009-05-22 14:51:39 -040012050 * lpfc_sli4_hba_unset - Unset the fcoe hba
12051 * @phba: Pointer to HBA context object.
12052 *
12053 * This function is called in the SLI4 code path to reset the HBA's FCoE
12054 * function. The caller is not required to hold any lock. This routine
12055 * issues PCI function reset mailbox command to reset the FCoE function.
12056 * At the end of the function, it calls lpfc_hba_down_post function to
12057 * free any pending commands.
12058 **/
12059static void
12060lpfc_sli4_hba_unset(struct lpfc_hba *phba)
12061{
12062 int wait_cnt = 0;
12063 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040012064 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040012065
12066 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -080012067 if (phba->pport)
12068 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -040012069
12070 /*
12071 * Gracefully wait out the potential current outstanding asynchronous
12072 * mailbox command.
12073 */
12074
12075 /* First, block any pending async mailbox command from posted */
12076 spin_lock_irq(&phba->hbalock);
12077 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
12078 spin_unlock_irq(&phba->hbalock);
12079 /* Now, trying to wait it out if we can */
12080 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
12081 msleep(10);
12082 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
12083 break;
12084 }
12085 /* Forcefully release the outstanding mailbox command if timed out */
12086 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
12087 spin_lock_irq(&phba->hbalock);
12088 mboxq = phba->sli.mbox_active;
12089 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
12090 __lpfc_mbox_cmpl_put(phba, mboxq);
12091 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12092 phba->sli.mbox_active = NULL;
12093 spin_unlock_irq(&phba->hbalock);
12094 }
12095
James Smart5af5eee2010-10-22 11:06:38 -040012096 /* Abort all iocbs associated with the hba */
12097 lpfc_sli_hba_iocb_abort(phba);
12098
12099 /* Wait for completion of device XRI exchange busy */
12100 lpfc_sli4_xri_exchange_busy_wait(phba);
12101
James Smart93a4d6f2019-11-04 16:57:05 -080012102 /* per-phba callback de-registration for hotplug event */
SeongJae Park46da5472020-06-23 10:41:22 +020012103 if (phba->pport)
12104 lpfc_cpuhp_remove(phba);
James Smart93a4d6f2019-11-04 16:57:05 -080012105
James Smartda0436e2009-05-22 14:51:39 -040012106 /* Disable PCI subsystem interrupt */
12107 lpfc_sli4_disable_intr(phba);
12108
James Smart912e3ac2011-05-24 11:42:11 -040012109 /* Disable SR-IOV if enabled */
12110 if (phba->cfg_sriov_nr_virtfn)
12111 pci_disable_sriov(pdev);
12112
James Smartda0436e2009-05-22 14:51:39 -040012113 /* Stop kthread signal shall trigger work_done one more time */
12114 kthread_stop(phba->worker_thread);
12115
James Smartd2cc9bc2018-09-10 10:30:50 -070012116 /* Disable FW logging to host memory */
James Smart1165a5c2018-11-29 16:09:39 -080012117 lpfc_ras_stop_fwlog(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070012118
James Smartd1f525a2017-04-21 16:04:55 -070012119 /* Unset the queues shared with the hardware then release all
12120 * allocated resources.
12121 */
12122 lpfc_sli4_queue_unset(phba);
12123 lpfc_sli4_queue_destroy(phba);
12124
James Smart3677a3a2010-09-29 11:19:14 -040012125 /* Reset SLI4 HBA FCoE function */
12126 lpfc_pci_function_reset(phba);
12127
James Smart1165a5c2018-11-29 16:09:39 -080012128 /* Free RAS DMA memory */
12129 if (phba->ras_fwlog.ras_enabled)
12130 lpfc_sli4_ras_dma_free(phba);
12131
James Smartda0436e2009-05-22 14:51:39 -040012132 /* Stop the SLI4 device port */
James Smart1ffdd2c2019-03-04 15:27:51 -080012133 if (phba->pport)
12134 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -040012135}
12136
12137/**
James Smartfedd3b72011-02-16 12:39:24 -050012138 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
12139 * @phba: Pointer to HBA context object.
12140 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
12141 *
12142 * This function is called in the SLI4 code path to read the port's
12143 * sli4 capabilities.
12144 *
12145 * This function may be be called from any context that can block-wait
12146 * for the completion. The expectation is that this routine is called
12147 * typically from probe_one or from the online routine.
12148 **/
12149int
12150lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12151{
12152 int rc;
12153 struct lpfc_mqe *mqe = &mboxq->u.mqe;
12154 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040012155 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050012156 int length;
James Smartbf316c72018-04-09 14:24:28 -070012157 bool exp_wqcq_pages = true;
James Smartfedd3b72011-02-16 12:39:24 -050012158 struct lpfc_sli4_parameters *mbx_sli4_parameters;
12159
James Smart6d368e52011-05-24 11:44:12 -040012160 /*
12161 * By default, the driver assumes the SLI4 port requires RPI
12162 * header postings. The SLI4_PARAM response will correct this
12163 * assumption.
12164 */
12165 phba->sli4_hba.rpi_hdrs_in_use = 1;
12166
James Smartfedd3b72011-02-16 12:39:24 -050012167 /* Read the port's SLI4 Config Parameters */
12168 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
12169 sizeof(struct lpfc_sli4_cfg_mhdr));
12170 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
12171 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
12172 length, LPFC_SLI4_MBX_EMBED);
12173 if (!phba->sli4_hba.intr_enable)
12174 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040012175 else {
12176 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
12177 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
12178 }
James Smartfedd3b72011-02-16 12:39:24 -050012179 if (unlikely(rc))
12180 return rc;
12181 sli4_params = &phba->sli4_hba.pc_sli4_params;
12182 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
12183 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
12184 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
12185 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
12186 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
12187 mbx_sli4_parameters);
12188 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
12189 mbx_sli4_parameters);
12190 if (bf_get(cfg_phwq, mbx_sli4_parameters))
12191 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
12192 else
12193 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
12194 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
James Smartb62232b2021-04-11 18:31:22 -070012195 sli4_params->loopbk_scope = bf_get(cfg_loopbk_scope,
12196 mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050012197 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050012198 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
12199 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
12200 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
12201 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart7365f6f2018-02-22 08:18:46 -080012202 sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
12203 sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040012204 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smart66e9e6b2018-06-26 08:24:27 -070012205 sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
James Smart83c6cb12019-10-18 14:18:30 -070012206 sli4_params->pls = bf_get(cfg_pvl, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050012207 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
12208 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080012209 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050012210 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
12211 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040012212 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
12213 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080012214
James Smartd79c9e92019-08-14 16:57:09 -070012215 /* Check for Extended Pre-Registered SGL support */
12216 phba->cfg_xpsgl = bf_get(cfg_xpsgl, mbx_sli4_parameters);
12217
James Smartc15e0702019-05-21 17:49:02 -070012218 /* Check for firmware nvme support */
12219 rc = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
12220 bf_get(cfg_xib, mbx_sli4_parameters));
James Smart895427b2017-02-12 13:52:30 -080012221
James Smartc15e0702019-05-21 17:49:02 -070012222 if (rc) {
12223 /* Save this to indicate the Firmware supports NVME */
12224 sli4_params->nvme = 1;
12225
12226 /* Firmware NVME support, check driver FC4 NVME support */
12227 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) {
12228 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
12229 "6133 Disabling NVME support: "
12230 "FC4 type not supported: x%x\n",
12231 phba->cfg_enable_fc4_type);
12232 goto fcponly;
12233 }
12234 } else {
12235 /* No firmware NVME support, check driver FC4 NVME support */
12236 sli4_params->nvme = 0;
12237 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
12238 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
12239 "6101 Disabling NVME support: Not "
12240 "supported by firmware (%d %d) x%x\n",
12241 bf_get(cfg_nvme, mbx_sli4_parameters),
12242 bf_get(cfg_xib, mbx_sli4_parameters),
12243 phba->cfg_enable_fc4_type);
12244fcponly:
James Smartc15e0702019-05-21 17:49:02 -070012245 phba->nvmet_support = 0;
12246 phba->cfg_nvmet_mrq = 0;
James Smart6a224b42019-08-14 16:56:57 -070012247 phba->cfg_nvme_seg_cnt = 0;
James Smartc15e0702019-05-21 17:49:02 -070012248
12249 /* If no FC4 type support, move to just SCSI support */
12250 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
12251 return -ENODEV;
12252 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
12253 }
James Smart895427b2017-02-12 13:52:30 -080012254 }
James Smart05580562011-05-24 11:40:48 -040012255
James Smartc26c2652019-08-14 16:56:49 -070012256 /* If the NVME FC4 type is enabled, scale the sg_seg_cnt to
James Smarta5f73372019-09-21 20:58:50 -070012257 * accommodate 512K and 1M IOs in a single nvme buf.
James Smartc26c2652019-08-14 16:56:49 -070012258 */
James Smarta5f73372019-09-21 20:58:50 -070012259 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
James Smartc26c2652019-08-14 16:56:49 -070012260 phba->cfg_sg_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
James Smartc26c2652019-08-14 16:56:49 -070012261
James Smart414abe02018-06-26 08:24:26 -070012262 /* Only embed PBDE for if_type 6, PBDE support requires xib be set */
12263 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
12264 LPFC_SLI_INTF_IF_TYPE_6) || (!bf_get(cfg_xib, mbx_sli4_parameters)))
12265 phba->cfg_enable_pbde = 0;
James Smart0bc2b7c2018-02-22 08:18:48 -080012266
James Smart20aefac2018-01-30 15:58:58 -080012267 /*
12268 * To support Suppress Response feature we must satisfy 3 conditions.
12269 * lpfc_suppress_rsp module parameter must be set (default).
12270 * In SLI4-Parameters Descriptor:
12271 * Extended Inline Buffers (XIB) must be supported.
12272 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
12273 * (double negative).
12274 */
12275 if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
12276 !(bf_get(cfg_nosr, mbx_sli4_parameters)))
James Smartf358dd02017-02-12 13:52:34 -080012277 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
James Smart20aefac2018-01-30 15:58:58 -080012278 else
12279 phba->cfg_suppress_rsp = 0;
James Smartf358dd02017-02-12 13:52:34 -080012280
James Smart0cf07f842017-06-01 21:07:10 -070012281 if (bf_get(cfg_eqdr, mbx_sli4_parameters))
12282 phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
12283
James Smart05580562011-05-24 11:40:48 -040012284 /* Make sure that sge_supp_len can be handled by the driver */
12285 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
12286 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
12287
James Smartb5c53952016-03-31 14:12:30 -070012288 /*
James Smartc176ffa2018-01-30 15:58:46 -080012289 * Check whether the adapter supports an embedded copy of the
12290 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
12291 * to use this option, 128-byte WQEs must be used.
James Smartb5c53952016-03-31 14:12:30 -070012292 */
12293 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
12294 phba->fcp_embed_io = 1;
12295 else
12296 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070012297
James Smart0bc2b7c2018-02-22 08:18:48 -080012298 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
James Smart414abe02018-06-26 08:24:26 -070012299 "6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
James Smart0bc2b7c2018-02-22 08:18:48 -080012300 bf_get(cfg_xib, mbx_sli4_parameters),
James Smart414abe02018-06-26 08:24:26 -070012301 phba->cfg_enable_pbde,
James Smartae463b62021-07-07 11:43:32 -070012302 phba->fcp_embed_io, sli4_params->nvme,
James Smart4e565cf2018-02-22 08:18:50 -080012303 phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
James Smart0bc2b7c2018-02-22 08:18:48 -080012304
James Smartbf316c72018-04-09 14:24:28 -070012305 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
12306 LPFC_SLI_INTF_IF_TYPE_2) &&
12307 (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
James Smartc2217682018-05-24 21:09:00 -070012308 LPFC_SLI_INTF_FAMILY_LNCR_A0))
James Smartbf316c72018-04-09 14:24:28 -070012309 exp_wqcq_pages = false;
12310
James Smartc176ffa2018-01-30 15:58:46 -080012311 if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
12312 (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
James Smartbf316c72018-04-09 14:24:28 -070012313 exp_wqcq_pages &&
James Smartc176ffa2018-01-30 15:58:46 -080012314 (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
12315 phba->enab_exp_wqcq_pages = 1;
12316 else
12317 phba->enab_exp_wqcq_pages = 0;
James Smart7bdedb32016-07-06 12:36:00 -070012318 /*
12319 * Check if the SLI port supports MDS Diagnostics
12320 */
12321 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
12322 phba->mds_diags_support = 1;
12323 else
12324 phba->mds_diags_support = 0;
James Smartd2cc9bc2018-09-10 10:30:50 -070012325
James Smart0d8af092019-08-14 16:57:10 -070012326 /*
12327 * Check if the SLI port supports NSLER
12328 */
12329 if (bf_get(cfg_nsler, mbx_sli4_parameters))
12330 phba->nsler = 1;
12331 else
12332 phba->nsler = 0;
12333
James Smart8aaa7bc2020-10-20 13:27:17 -070012334 /* Save PB info for use during HBA setup */
12335 sli4_params->mi_ver = bf_get(cfg_mi_ver, mbx_sli4_parameters);
12336 sli4_params->mib_bde_cnt = bf_get(cfg_mib_bde_cnt, mbx_sli4_parameters);
12337 sli4_params->mib_size = mbx_sli4_parameters->mib_size;
12338 sli4_params->mi_value = LPFC_DFLT_MIB_VAL;
12339
12340 /* Next we check for Vendor MIB support */
12341 if (sli4_params->mi_ver && phba->cfg_enable_mi)
12342 phba->cfg_fdmi_on = LPFC_FDMI_SUPPORT;
12343
12344 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12345 "6461 MIB attr %d enable %d FDMI %d buf %d:%d\n",
12346 sli4_params->mi_ver, phba->cfg_enable_mi,
12347 sli4_params->mi_value, sli4_params->mib_bde_cnt,
12348 sli4_params->mib_size);
James Smartfedd3b72011-02-16 12:39:24 -050012349 return 0;
12350}
12351
12352/**
James Smart3772a992009-05-22 14:50:54 -040012353 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
12354 * @pdev: pointer to PCI device
12355 * @pid: pointer to PCI device identifier
12356 *
12357 * This routine is to be called to attach a device with SLI-3 interface spec
12358 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
12359 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
12360 * information of the device and driver to see if the driver state that it can
12361 * support this kind of device. If the match is successful, the driver core
12362 * invokes this routine. If this routine determines it can claim the HBA, it
12363 * does all the initialization that it needs to do to handle the HBA properly.
12364 *
12365 * Return code
12366 * 0 - driver can claim the device
12367 * negative value - driver can not claim the device
12368 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012369static int
James Smart3772a992009-05-22 14:50:54 -040012370lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
12371{
12372 struct lpfc_hba *phba;
12373 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040012374 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040012375 int error;
12376 uint32_t cfg_mode, intr_mode;
12377
12378 /* Allocate memory for HBA structure */
12379 phba = lpfc_hba_alloc(pdev);
12380 if (!phba)
12381 return -ENOMEM;
12382
12383 /* Perform generic PCI device enabling operation */
12384 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040012385 if (error)
James Smart3772a992009-05-22 14:50:54 -040012386 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040012387
12388 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
12389 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
12390 if (error)
12391 goto out_disable_pci_dev;
12392
12393 /* Set up SLI-3 specific device PCI memory space */
12394 error = lpfc_sli_pci_mem_setup(phba);
12395 if (error) {
12396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12397 "1402 Failed to set up pci memory space.\n");
12398 goto out_disable_pci_dev;
12399 }
12400
James Smart3772a992009-05-22 14:50:54 -040012401 /* Set up SLI-3 specific device driver resources */
12402 error = lpfc_sli_driver_resource_setup(phba);
12403 if (error) {
12404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12405 "1404 Failed to set up driver resource.\n");
12406 goto out_unset_pci_mem_s3;
12407 }
12408
12409 /* Initialize and populate the iocb list per host */
James Smartd1f525a2017-04-21 16:04:55 -070012410
James Smart3772a992009-05-22 14:50:54 -040012411 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
12412 if (error) {
12413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12414 "1405 Failed to initialize iocb list.\n");
12415 goto out_unset_driver_resource_s3;
12416 }
12417
12418 /* Set up common device driver resources */
12419 error = lpfc_setup_driver_resource_phase2(phba);
12420 if (error) {
12421 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12422 "1406 Failed to set up driver resource.\n");
12423 goto out_free_iocb_list;
12424 }
12425
James Smart079b5c92011-08-21 21:48:49 -040012426 /* Get the default values for Model Name and Description */
12427 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
12428
James Smart3772a992009-05-22 14:50:54 -040012429 /* Create SCSI host to the physical port */
12430 error = lpfc_create_shost(phba);
12431 if (error) {
12432 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12433 "1407 Failed to create scsi host.\n");
12434 goto out_unset_driver_resource;
12435 }
12436
12437 /* Configure sysfs attributes */
12438 vport = phba->pport;
12439 error = lpfc_alloc_sysfs_attr(vport);
12440 if (error) {
12441 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12442 "1476 Failed to allocate sysfs attr\n");
12443 goto out_destroy_shost;
12444 }
12445
James Smart6669f9b2009-10-02 15:16:45 -040012446 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040012447 /* Now, trying to enable interrupt and bring up the device */
12448 cfg_mode = phba->cfg_use_msi;
12449 while (true) {
12450 /* Put device to a known state before enabling interrupt */
12451 lpfc_stop_port(phba);
12452 /* Configure and enable interrupt */
12453 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
12454 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012455 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040012456 "0431 Failed to enable interrupt.\n");
12457 error = -ENODEV;
12458 goto out_free_sysfs_attr;
12459 }
12460 /* SLI-3 HBA setup */
12461 if (lpfc_sli_hba_setup(phba)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012462 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040012463 "1477 Failed to set up hba\n");
12464 error = -ENODEV;
12465 goto out_remove_device;
12466 }
12467
12468 /* Wait 50ms for the interrupts of previous mailbox commands */
12469 msleep(50);
12470 /* Check active interrupts on message signaled interrupts */
12471 if (intr_mode == 0 ||
12472 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
12473 /* Log the current active interrupt mode */
12474 phba->intr_mode = intr_mode;
12475 lpfc_log_intr_mode(phba, intr_mode);
12476 break;
12477 } else {
12478 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12479 "0447 Configure interrupt mode (%d) "
12480 "failed active interrupt test.\n",
12481 intr_mode);
12482 /* Disable the current interrupt mode */
12483 lpfc_sli_disable_intr(phba);
12484 /* Try next level of interrupt mode */
12485 cfg_mode = --intr_mode;
12486 }
12487 }
12488
12489 /* Perform post initialization setup */
12490 lpfc_post_init_setup(phba);
12491
12492 /* Check if there are static vports to be created. */
12493 lpfc_create_static_vport(phba);
12494
12495 return 0;
12496
12497out_remove_device:
12498 lpfc_unset_hba(phba);
12499out_free_sysfs_attr:
12500 lpfc_free_sysfs_attr(vport);
12501out_destroy_shost:
12502 lpfc_destroy_shost(phba);
12503out_unset_driver_resource:
12504 lpfc_unset_driver_resource_phase2(phba);
12505out_free_iocb_list:
12506 lpfc_free_iocb_list(phba);
12507out_unset_driver_resource_s3:
12508 lpfc_sli_driver_resource_unset(phba);
12509out_unset_pci_mem_s3:
12510 lpfc_sli_pci_mem_unset(phba);
12511out_disable_pci_dev:
12512 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040012513 if (shost)
12514 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040012515out_free_phba:
12516 lpfc_hba_free(phba);
12517 return error;
12518}
12519
12520/**
12521 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040012522 * @pdev: pointer to PCI device
12523 *
James Smart3772a992009-05-22 14:50:54 -040012524 * This routine is to be called to disattach a device with SLI-3 interface
12525 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
12526 * removed from PCI bus, it performs all the necessary cleanup for the HBA
12527 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040012528 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012529static void
James Smart3772a992009-05-22 14:50:54 -040012530lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050012531{
James Smart2e0fef82007-06-17 19:56:36 -050012532 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12533 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050012534 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050012535 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050012536 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050012537
James Smart549e55c2007-08-02 11:09:51 -040012538 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040012539 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040012540 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050012541
James Smart858c9f62007-06-17 19:56:39 -050012542 lpfc_free_sysfs_attr(vport);
12543
James Smarteada2722008-12-04 22:39:13 -050012544 /* Release all the vports against this physical port */
12545 vports = lpfc_create_vport_work_array(phba);
12546 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040012547 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
12548 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
12549 continue;
James Smarteada2722008-12-04 22:39:13 -050012550 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040012551 }
James Smarteada2722008-12-04 22:39:13 -050012552 lpfc_destroy_vport_work_array(phba, vports);
12553
James Smart95f0ef82020-11-15 11:26:32 -080012554 /* Remove FC host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050012555 fc_remove_host(shost);
James Smarte9b11082020-11-15 11:26:33 -080012556 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080012557
James Smart95f0ef82020-11-15 11:26:32 -080012558 /* Clean up all nodes, mailboxes and IOs. */
James Smart87af33f2007-10-27 13:37:43 -040012559 lpfc_cleanup(vport);
12560
James Smart2e0fef82007-06-17 19:56:36 -050012561 /*
12562 * Bring down the SLI Layer. This step disable all interrupts,
12563 * clears the rings, discards all mailbox commands, and resets
12564 * the HBA.
12565 */
James Smarta257bf92009-04-06 18:48:10 -040012566
Justin P. Mattock48e34d02010-12-30 15:07:58 -080012567 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050012568 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040012569 /* Stop kthread signal shall trigger work_done one more time */
12570 kthread_stop(phba->worker_thread);
12571 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050012572 lpfc_sli_brdrestart(phba);
12573
James Smart72859902012-01-18 16:25:38 -050012574 kfree(phba->vpi_bmask);
12575 kfree(phba->vpi_ids);
12576
James Smart3772a992009-05-22 14:50:54 -040012577 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070012578 spin_lock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050012579 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070012580 spin_unlock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050012581
James Smart858c9f62007-06-17 19:56:39 -050012582 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050012583
James Smart912e3ac2011-05-24 11:42:11 -040012584 /* Disable SR-IOV if enabled */
12585 if (phba->cfg_sriov_nr_virtfn)
12586 pci_disable_sriov(pdev);
12587
James Smart5b75da22008-12-04 22:39:35 -050012588 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012589 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050012590
James Smart858c9f62007-06-17 19:56:39 -050012591 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050012592
12593 /*
12594 * Call scsi_free before mem_free since scsi bufs are released to their
12595 * corresponding pools here.
12596 */
12597 lpfc_scsi_free(phba);
James Smart0794d602019-01-28 11:14:19 -080012598 lpfc_free_iocb_list(phba);
12599
James Smart3772a992009-05-22 14:50:54 -040012600 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050012601
James Smart34b02dc2008-08-24 21:49:55 -040012602 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
12603 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050012604
James Smart2e0fef82007-06-17 19:56:36 -050012605 /* Free resources associated with SLI2 interface */
12606 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040012607 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050012608
12609 /* unmap adapter SLIM and Control Registers */
12610 iounmap(phba->ctrl_regs_memmap_p);
12611 iounmap(phba->slim_memmap_p);
12612
James Smart3772a992009-05-22 14:50:54 -040012613 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050012614
Johannes Thumshirne0c04832016-06-07 09:44:03 +020012615 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050012616 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050012617}
12618
Linas Vepstas8d63f372007-02-14 14:28:36 -060012619/**
James Smart3772a992009-05-22 14:50:54 -040012620 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053012621 * @dev_d: pointer to device
James Smart3a55b532008-12-04 22:38:54 -050012622 *
James Smart3772a992009-05-22 14:50:54 -040012623 * This routine is to be called from the kernel's PCI subsystem to support
12624 * system Power Management (PM) to device with SLI-3 interface spec. When
12625 * PM invokes this method, it quiesces the device by stopping the driver's
12626 * worker thread for the device, turning off device's interrupt and DMA,
12627 * and bring the device offline. Note that as the driver implements the
12628 * minimum PM requirements to a power-aware driver's PM support for the
12629 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
12630 * to the suspend() method call will be treated as SUSPEND and the driver will
12631 * fully reinitialize its device during resume() method call, the driver will
12632 * set device to PCI_D3hot state in PCI config space instead of setting it
12633 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050012634 *
12635 * Return code
James Smart3772a992009-05-22 14:50:54 -040012636 * 0 - driver suspended the device
12637 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050012638 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053012639static int __maybe_unused
12640lpfc_pci_suspend_one_s3(struct device *dev_d)
James Smart3a55b532008-12-04 22:38:54 -050012641{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053012642 struct Scsi_Host *shost = dev_get_drvdata(dev_d);
James Smart3a55b532008-12-04 22:38:54 -050012643 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12644
12645 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12646 "0473 PCI device Power Management suspend.\n");
12647
12648 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040012649 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050012650 lpfc_offline(phba);
12651 kthread_stop(phba->worker_thread);
12652
12653 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040012654 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050012655
James Smart3a55b532008-12-04 22:38:54 -050012656 return 0;
12657}
12658
12659/**
James Smart3772a992009-05-22 14:50:54 -040012660 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053012661 * @dev_d: pointer to device
James Smart3a55b532008-12-04 22:38:54 -050012662 *
James Smart3772a992009-05-22 14:50:54 -040012663 * This routine is to be called from the kernel's PCI subsystem to support
12664 * system Power Management (PM) to device with SLI-3 interface spec. When PM
12665 * invokes this method, it restores the device's PCI config space state and
12666 * fully reinitializes the device and brings it online. Note that as the
12667 * driver implements the minimum PM requirements to a power-aware driver's
12668 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
12669 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
12670 * driver will fully reinitialize its device during resume() method call,
12671 * the device will be set to PCI_D0 directly in PCI config space before
12672 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050012673 *
12674 * Return code
James Smart3772a992009-05-22 14:50:54 -040012675 * 0 - driver suspended the device
12676 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050012677 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053012678static int __maybe_unused
12679lpfc_pci_resume_one_s3(struct device *dev_d)
James Smart3a55b532008-12-04 22:38:54 -050012680{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053012681 struct Scsi_Host *shost = dev_get_drvdata(dev_d);
James Smart3a55b532008-12-04 22:38:54 -050012682 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050012683 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050012684 int error;
12685
12686 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12687 "0452 PCI device Power Management resume.\n");
12688
James Smart3a55b532008-12-04 22:38:54 -050012689 /* Startup the kernel thread for this host adapter. */
12690 phba->worker_thread = kthread_run(lpfc_do_work, phba,
12691 "lpfc_worker_%d", phba->brd_no);
12692 if (IS_ERR(phba->worker_thread)) {
12693 error = PTR_ERR(phba->worker_thread);
12694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12695 "0434 PM resume failed to start worker "
12696 "thread: error=x%x.\n", error);
12697 return error;
12698 }
12699
James Smart5b75da22008-12-04 22:39:35 -050012700 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012701 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050012702 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012703 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5b75da22008-12-04 22:39:35 -050012704 "0430 PM resume Failed to enable interrupt\n");
12705 return -EIO;
12706 } else
12707 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050012708
12709 /* Restart HBA and bring it online */
12710 lpfc_sli_brdrestart(phba);
12711 lpfc_online(phba);
12712
James Smart5b75da22008-12-04 22:39:35 -050012713 /* Log the current active interrupt mode */
12714 lpfc_log_intr_mode(phba, phba->intr_mode);
12715
James Smart3a55b532008-12-04 22:38:54 -050012716 return 0;
12717}
12718
12719/**
James Smart891478a2009-11-18 15:40:23 -050012720 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
12721 * @phba: pointer to lpfc hba data structure.
12722 *
12723 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040012724 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050012725 **/
12726static void
12727lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
12728{
Dick Kennedy372c1872020-06-30 14:50:00 -070012729 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart891478a2009-11-18 15:40:23 -050012730 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040012731
12732 /*
12733 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
12734 * and let the SCSI mid-layer to retry them to recover.
12735 */
James Smartdb55fba2014-04-04 13:52:02 -040012736 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050012737}
12738
12739/**
James Smart0d878412009-10-02 15:16:56 -040012740 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
12741 * @phba: pointer to lpfc hba data structure.
12742 *
12743 * This routine is called to prepare the SLI3 device for PCI slot reset. It
12744 * disables the device interrupt and pci device, and aborts the internal FCP
12745 * pending I/Os.
12746 **/
12747static void
12748lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
12749{
Dick Kennedy372c1872020-06-30 14:50:00 -070012750 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart891478a2009-11-18 15:40:23 -050012751 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040012752
James Smart75baf692010-06-08 18:31:21 -040012753 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040012754 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040012755
James Smarte2af0d22010-03-15 11:25:32 -040012756 /* Block all SCSI devices' I/Os on the host */
12757 lpfc_scsi_dev_block(phba);
12758
James Smartea714f32013-04-17 20:18:47 -040012759 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
James Smartc00f62e2019-08-14 16:57:11 -070012760 lpfc_sli_flush_io_rings(phba);
James Smartea714f32013-04-17 20:18:47 -040012761
James Smarte2af0d22010-03-15 11:25:32 -040012762 /* stop all timers */
12763 lpfc_stop_hba_timers(phba);
12764
James Smart0d878412009-10-02 15:16:56 -040012765 /* Disable interrupt and pci device */
12766 lpfc_sli_disable_intr(phba);
12767 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040012768}
12769
12770/**
12771 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
12772 * @phba: pointer to lpfc hba data structure.
12773 *
12774 * This routine is called to prepare the SLI3 device for PCI slot permanently
12775 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
12776 * pending I/Os.
12777 **/
12778static void
James Smart75baf692010-06-08 18:31:21 -040012779lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040012780{
Dick Kennedy372c1872020-06-30 14:50:00 -070012781 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart891478a2009-11-18 15:40:23 -050012782 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040012783 /* Block all SCSI devices' I/Os on the host */
12784 lpfc_scsi_dev_block(phba);
12785
12786 /* stop all timers */
12787 lpfc_stop_hba_timers(phba);
12788
James Smart0d878412009-10-02 15:16:56 -040012789 /* Clean up all driver's outstanding SCSI I/Os */
James Smartc00f62e2019-08-14 16:57:11 -070012790 lpfc_sli_flush_io_rings(phba);
James Smart0d878412009-10-02 15:16:56 -040012791}
12792
12793/**
James Smart3772a992009-05-22 14:50:54 -040012794 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040012795 * @pdev: pointer to PCI device.
12796 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012797 *
James Smart3772a992009-05-22 14:50:54 -040012798 * This routine is called from the PCI subsystem for I/O error handling to
12799 * device with SLI-3 interface spec. This function is called by the PCI
12800 * subsystem after a PCI bus error affecting this device has been detected.
12801 * When this function is invoked, it will need to stop all the I/Os and
12802 * interrupt(s) to the device. Once that is done, it will return
12803 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
12804 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040012805 *
12806 * Return codes
James Smart0d878412009-10-02 15:16:56 -040012807 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040012808 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12809 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040012810 **/
James Smart3772a992009-05-22 14:50:54 -040012811static pci_ers_result_t
12812lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012813{
James Smart51ef4c22007-08-02 11:10:31 -040012814 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12815 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012816
James Smart0d878412009-10-02 15:16:56 -040012817 switch (state) {
12818 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050012819 /* Non-fatal error, prepare for recovery */
12820 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040012821 return PCI_ERS_RESULT_CAN_RECOVER;
12822 case pci_channel_io_frozen:
12823 /* Fatal error, prepare for slot reset */
12824 lpfc_sli_prep_dev_for_reset(phba);
12825 return PCI_ERS_RESULT_NEED_RESET;
12826 case pci_channel_io_perm_failure:
12827 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040012828 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012829 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040012830 default:
12831 /* Unknown state, prepare and request slot reset */
Dick Kennedy372c1872020-06-30 14:50:00 -070012832 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0d878412009-10-02 15:16:56 -040012833 "0472 Unknown PCI error state: x%x\n", state);
12834 lpfc_sli_prep_dev_for_reset(phba);
12835 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040012836 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060012837}
12838
12839/**
James Smart3772a992009-05-22 14:50:54 -040012840 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040012841 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012842 *
James Smart3772a992009-05-22 14:50:54 -040012843 * This routine is called from the PCI subsystem for error handling to
12844 * device with SLI-3 interface spec. This is called after PCI bus has been
12845 * reset to restart the PCI card from scratch, as if from a cold-boot.
12846 * During the PCI subsystem error recovery, after driver returns
12847 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
12848 * recovery and then call this routine before calling the .resume method
12849 * to recover the device. This function will initialize the HBA device,
12850 * enable the interrupt, but it will just put the HBA to offline state
12851 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040012852 *
12853 * Return codes
James Smart3772a992009-05-22 14:50:54 -040012854 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
12855 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060012856 */
James Smart3772a992009-05-22 14:50:54 -040012857static pci_ers_result_t
12858lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012859{
James Smart51ef4c22007-08-02 11:10:31 -040012860 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12861 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012862 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050012863 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012864
12865 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110012866 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060012867 printk(KERN_ERR "lpfc: Cannot re-enable "
12868 "PCI device after reset.\n");
12869 return PCI_ERS_RESULT_DISCONNECT;
12870 }
12871
James Smart97207482008-12-04 22:39:19 -050012872 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050012873
12874 /*
12875 * As the new kernel behavior of pci_restore_state() API call clears
12876 * device saved_state flag, need to save the restored state again.
12877 */
12878 pci_save_state(pdev);
12879
James Smart97207482008-12-04 22:39:19 -050012880 if (pdev->is_busmaster)
12881 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012882
James Smart92d7f7b2007-06-17 19:56:38 -050012883 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012884 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050012885 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012886
James Smart5b75da22008-12-04 22:39:35 -050012887 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012888 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050012889 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012890 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5b75da22008-12-04 22:39:35 -050012891 "0427 Cannot re-enable interrupt after "
12892 "slot reset.\n");
12893 return PCI_ERS_RESULT_DISCONNECT;
12894 } else
12895 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012896
James Smart75baf692010-06-08 18:31:21 -040012897 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040012898 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012899 lpfc_offline(phba);
12900 lpfc_sli_brdrestart(phba);
12901
James Smart5b75da22008-12-04 22:39:35 -050012902 /* Log the current active interrupt mode */
12903 lpfc_log_intr_mode(phba, phba->intr_mode);
12904
Linas Vepstas8d63f372007-02-14 14:28:36 -060012905 return PCI_ERS_RESULT_RECOVERED;
12906}
12907
12908/**
James Smart3772a992009-05-22 14:50:54 -040012909 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040012910 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060012911 *
James Smart3772a992009-05-22 14:50:54 -040012912 * This routine is called from the PCI subsystem for error handling to device
12913 * with SLI-3 interface spec. It is called when kernel error recovery tells
12914 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
12915 * error recovery. After this call, traffic can start to flow from this device
12916 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012917 */
James Smart3772a992009-05-22 14:50:54 -040012918static void
12919lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012920{
James Smart51ef4c22007-08-02 11:10:31 -040012921 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12922 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012923
James Smarte2af0d22010-03-15 11:25:32 -040012924 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040012925 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012926}
12927
James Smart3772a992009-05-22 14:50:54 -040012928/**
James Smartda0436e2009-05-22 14:51:39 -040012929 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
12930 * @phba: pointer to lpfc hba data structure.
12931 *
12932 * returns the number of ELS/CT IOCBs to reserve
12933 **/
12934int
12935lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
12936{
12937 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
12938
James Smartf1126682009-06-10 17:22:44 -040012939 if (phba->sli_rev == LPFC_SLI_REV4) {
12940 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040012941 return 10;
James Smartf1126682009-06-10 17:22:44 -040012942 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040012943 return 25;
James Smartf1126682009-06-10 17:22:44 -040012944 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040012945 return 50;
James Smartf1126682009-06-10 17:22:44 -040012946 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040012947 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040012948 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040012949 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040012950 else if (max_xri <= 2048)
12951 return 200;
12952 else
12953 return 250;
James Smartf1126682009-06-10 17:22:44 -040012954 } else
12955 return 0;
James Smartda0436e2009-05-22 14:51:39 -040012956}
12957
12958/**
James Smart895427b2017-02-12 13:52:30 -080012959 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
12960 * @phba: pointer to lpfc hba data structure.
12961 *
James Smartf358dd02017-02-12 13:52:34 -080012962 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080012963 **/
12964int
12965lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
12966{
12967 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
12968
James Smartf358dd02017-02-12 13:52:34 -080012969 if (phba->nvmet_support)
12970 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080012971 return max_xri;
12972}
12973
12974
James Smart0a5ce732019-10-18 14:18:18 -070012975static int
James Smart1feb8202018-02-22 08:18:47 -080012976lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
12977 uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
12978 const struct firmware *fw)
12979{
James Smart0a5ce732019-10-18 14:18:18 -070012980 int rc;
James Smart1feb8202018-02-22 08:18:47 -080012981
James Smart0a5ce732019-10-18 14:18:18 -070012982 /* Three cases: (1) FW was not supported on the detected adapter.
12983 * (2) FW update has been locked out administratively.
12984 * (3) Some other error during FW update.
12985 * In each case, an unmaskable message is written to the console
12986 * for admin diagnosis.
12987 */
12988 if (offset == ADD_STATUS_FW_NOT_SUPPORTED ||
James Smart1feb8202018-02-22 08:18:47 -080012989 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC &&
James Smart5792a0e2019-10-25 11:43:42 -070012990 magic_number != MAGIC_NUMBER_G6) ||
James Smart1feb8202018-02-22 08:18:47 -080012991 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G7_FC &&
James Smart5792a0e2019-10-25 11:43:42 -070012992 magic_number != MAGIC_NUMBER_G7)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012993 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070012994 "3030 This firmware version is not supported on"
12995 " this HBA model. Device:%x Magic:%x Type:%x "
12996 "ID:%x Size %d %zd\n",
12997 phba->pcidev->device, magic_number, ftype, fid,
12998 fsize, fw->size);
12999 rc = -EINVAL;
13000 } else if (offset == ADD_STATUS_FW_DOWNLOAD_HW_DISABLED) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013001 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070013002 "3021 Firmware downloads have been prohibited "
13003 "by a system configuration setting on "
13004 "Device:%x Magic:%x Type:%x ID:%x Size %d "
13005 "%zd\n",
13006 phba->pcidev->device, magic_number, ftype, fid,
13007 fsize, fw->size);
13008 rc = -EACCES;
13009 } else {
Dick Kennedy372c1872020-06-30 14:50:00 -070013010 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070013011 "3022 FW Download failed. Add Status x%x "
13012 "Device:%x Magic:%x Type:%x ID:%x Size %d "
13013 "%zd\n",
13014 offset, phba->pcidev->device, magic_number,
13015 ftype, fid, fsize, fw->size);
13016 rc = -EIO;
13017 }
13018 return rc;
James Smart1feb8202018-02-22 08:18:47 -080013019}
James Smart1feb8202018-02-22 08:18:47 -080013020
James Smart895427b2017-02-12 13:52:30 -080013021/**
James Smart52d52442011-05-24 11:42:45 -040013022 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040013023 * @fw: pointer to firmware image returned from request_firmware.
James Smart0a5ce732019-10-18 14:18:18 -070013024 * @context: pointer to firmware image returned from request_firmware.
James Smart52d52442011-05-24 11:42:45 -040013025 *
James Smart52d52442011-05-24 11:42:45 -040013026 **/
James Smartce396282012-09-29 11:30:56 -040013027static void
13028lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040013029{
James Smartce396282012-09-29 11:30:56 -040013030 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050013031 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040013032 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040013033 struct list_head dma_buffer_list;
13034 int i, rc = 0;
13035 struct lpfc_dmabuf *dmabuf, *next;
13036 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070013037 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040013038
James Smartc71ab862012-10-31 14:44:33 -040013039 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040013040 if (!fw) {
13041 rc = -ENXIO;
13042 goto out;
13043 }
13044 image = (struct lpfc_grp_hdr *)fw->data;
13045
James Smart6b6ef5d2016-10-13 15:06:17 -070013046 magic_number = be32_to_cpu(image->magic_number);
13047 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
James Smart1feb8202018-02-22 08:18:47 -080013048 fid = bf_get_be32(lpfc_grp_hdr_id, image);
James Smart6b6ef5d2016-10-13 15:06:17 -070013049 fsize = be32_to_cpu(image->size);
13050
James Smart52d52442011-05-24 11:42:45 -040013051 INIT_LIST_HEAD(&dma_buffer_list);
James Smart52d52442011-05-24 11:42:45 -040013052 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040013053 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013054 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartce396282012-09-29 11:30:56 -040013055 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040013056 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040013057 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040013058 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
13059 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
13060 GFP_KERNEL);
13061 if (!dmabuf) {
13062 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040013063 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040013064 }
13065 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
13066 SLI4_PAGE_SIZE,
13067 &dmabuf->phys,
13068 GFP_KERNEL);
13069 if (!dmabuf->virt) {
13070 kfree(dmabuf);
13071 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040013072 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040013073 }
13074 list_add_tail(&dmabuf->list, &dma_buffer_list);
13075 }
13076 while (offset < fw->size) {
13077 temp_offset = offset;
13078 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040013079 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040013080 memcpy(dmabuf->virt,
13081 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040013082 fw->size - temp_offset);
13083 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040013084 break;
13085 }
James Smart52d52442011-05-24 11:42:45 -040013086 memcpy(dmabuf->virt, fw->data + temp_offset,
13087 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040013088 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040013089 }
13090 rc = lpfc_wr_object(phba, &dma_buffer_list,
13091 (fw->size - offset), &offset);
James Smart1feb8202018-02-22 08:18:47 -080013092 if (rc) {
James Smart0a5ce732019-10-18 14:18:18 -070013093 rc = lpfc_log_write_firmware_error(phba, offset,
13094 magic_number,
13095 ftype,
13096 fid,
13097 fsize,
13098 fw);
James Smartce396282012-09-29 11:30:56 -040013099 goto release_out;
James Smart1feb8202018-02-22 08:18:47 -080013100 }
James Smart52d52442011-05-24 11:42:45 -040013101 }
13102 rc = offset;
James Smart1feb8202018-02-22 08:18:47 -080013103 } else
Dick Kennedy372c1872020-06-30 14:50:00 -070013104 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1feb8202018-02-22 08:18:47 -080013105 "3029 Skipped Firmware update, Current "
13106 "Version:%s New Version:%s\n",
13107 fwrev, image->revision);
James Smartce396282012-09-29 11:30:56 -040013108
13109release_out:
James Smart52d52442011-05-24 11:42:45 -040013110 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
13111 list_del(&dmabuf->list);
13112 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
13113 dmabuf->virt, dmabuf->phys);
13114 kfree(dmabuf);
13115 }
James Smartce396282012-09-29 11:30:56 -040013116 release_firmware(fw);
13117out:
James Smart0a5ce732019-10-18 14:18:18 -070013118 if (rc < 0)
Dick Kennedy372c1872020-06-30 14:50:00 -070013119 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070013120 "3062 Firmware update error, status %d.\n", rc);
13121 else
Dick Kennedy372c1872020-06-30 14:50:00 -070013122 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070013123 "3024 Firmware update success: size %d.\n", rc);
James Smart52d52442011-05-24 11:42:45 -040013124}
13125
13126/**
James Smartc71ab862012-10-31 14:44:33 -040013127 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
13128 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +010013129 * @fw_upgrade: which firmware to update.
James Smartc71ab862012-10-31 14:44:33 -040013130 *
13131 * This routine is called to perform Linux generic firmware upgrade on device
13132 * that supports such feature.
13133 **/
13134int
13135lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
13136{
13137 uint8_t file_name[ELX_MODEL_NAME_SIZE];
13138 int ret;
13139 const struct firmware *fw;
13140
13141 /* Only supported on SLI4 interface type 2 for now */
James Smart27d6ac02018-02-22 08:18:42 -080013142 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smartc71ab862012-10-31 14:44:33 -040013143 LPFC_SLI_INTF_IF_TYPE_2)
13144 return -EPERM;
13145
13146 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
13147
13148 if (fw_upgrade == INT_FW_UPGRADE) {
Shawn Guo0733d832021-04-25 10:00:24 +080013149 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT,
James Smartc71ab862012-10-31 14:44:33 -040013150 file_name, &phba->pcidev->dev,
13151 GFP_KERNEL, (void *)phba,
13152 lpfc_write_firmware);
13153 } else if (fw_upgrade == RUN_FW_UPGRADE) {
13154 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
13155 if (!ret)
13156 lpfc_write_firmware(fw, (void *)phba);
13157 } else {
13158 ret = -EINVAL;
13159 }
13160
13161 return ret;
13162}
13163
13164/**
James Smartda0436e2009-05-22 14:51:39 -040013165 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
13166 * @pdev: pointer to PCI device
13167 * @pid: pointer to PCI device identifier
13168 *
13169 * This routine is called from the kernel's PCI subsystem to device with
13170 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
13171 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
13172 * information of the device and driver to see if the driver state that it
13173 * can support this kind of device. If the match is successful, the driver
13174 * core invokes this routine. If this routine determines it can claim the HBA,
13175 * it does all the initialization that it needs to do to handle the HBA
13176 * properly.
13177 *
13178 * Return code
13179 * 0 - driver can claim the device
13180 * negative value - driver can not claim the device
13181 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013182static int
James Smartda0436e2009-05-22 14:51:39 -040013183lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
13184{
13185 struct lpfc_hba *phba;
13186 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040013187 struct Scsi_Host *shost = NULL;
James Smart6c621a22017-05-15 15:20:45 -070013188 int error;
James Smartda0436e2009-05-22 14:51:39 -040013189 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040013190
13191 /* Allocate memory for HBA structure */
13192 phba = lpfc_hba_alloc(pdev);
13193 if (!phba)
13194 return -ENOMEM;
13195
13196 /* Perform generic PCI device enabling operation */
13197 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040013198 if (error)
James Smartda0436e2009-05-22 14:51:39 -040013199 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040013200
13201 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
13202 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
13203 if (error)
13204 goto out_disable_pci_dev;
13205
13206 /* Set up SLI-4 specific device PCI memory space */
13207 error = lpfc_sli4_pci_mem_setup(phba);
13208 if (error) {
13209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13210 "1410 Failed to set up pci memory space.\n");
13211 goto out_disable_pci_dev;
13212 }
13213
James Smartda0436e2009-05-22 14:51:39 -040013214 /* Set up SLI-4 Specific device driver resources */
13215 error = lpfc_sli4_driver_resource_setup(phba);
13216 if (error) {
13217 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13218 "1412 Failed to set up driver resource.\n");
13219 goto out_unset_pci_mem_s4;
13220 }
13221
James Smart19ca7602010-11-20 23:11:55 -050013222 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040013223 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050013224
James Smartda0436e2009-05-22 14:51:39 -040013225 /* Set up common device driver resources */
13226 error = lpfc_setup_driver_resource_phase2(phba);
13227 if (error) {
13228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13229 "1414 Failed to set up driver resource.\n");
James Smart6c621a22017-05-15 15:20:45 -070013230 goto out_unset_driver_resource_s4;
James Smartda0436e2009-05-22 14:51:39 -040013231 }
13232
James Smart079b5c92011-08-21 21:48:49 -040013233 /* Get the default values for Model Name and Description */
13234 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
13235
James Smartda0436e2009-05-22 14:51:39 -040013236 /* Now, trying to enable interrupt and bring up the device */
13237 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040013238
James Smart7b15db32013-01-03 15:43:29 -050013239 /* Put device to a known state before enabling interrupt */
James Smartcdb42be2019-01-28 11:14:21 -080013240 phba->pport = NULL;
James Smart7b15db32013-01-03 15:43:29 -050013241 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080013242
James Smartdcaa2132019-11-04 16:57:06 -080013243 /* Init cpu_map array */
13244 lpfc_cpu_map_array_init(phba);
13245
13246 /* Init hba_eq_hdl array */
13247 lpfc_hba_eq_hdl_array_init(phba);
13248
James Smart7b15db32013-01-03 15:43:29 -050013249 /* Configure and enable interrupt */
13250 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
13251 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013252 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7b15db32013-01-03 15:43:29 -050013253 "0426 Failed to enable interrupt.\n");
13254 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080013255 goto out_unset_driver_resource;
James Smartda0436e2009-05-22 14:51:39 -040013256 }
James Smart7b15db32013-01-03 15:43:29 -050013257 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080013258 if (phba->intr_type != MSIX) {
James Smart6a828b02019-01-28 11:14:31 -080013259 phba->cfg_irq_chann = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080013260 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -080013261 if (phba->nvmet_support)
13262 phba->cfg_nvmet_mrq = 1;
13263 }
James Smartcdb42be2019-01-28 11:14:21 -080013264 }
James Smart6a828b02019-01-28 11:14:31 -080013265 lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
James Smartcdb42be2019-01-28 11:14:21 -080013266
13267 /* Create SCSI host to the physical port */
13268 error = lpfc_create_shost(phba);
13269 if (error) {
13270 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13271 "1415 Failed to create scsi host.\n");
13272 goto out_disable_intr;
13273 }
13274 vport = phba->pport;
13275 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
13276
13277 /* Configure sysfs attributes */
13278 error = lpfc_alloc_sysfs_attr(vport);
13279 if (error) {
13280 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13281 "1416 Failed to allocate sysfs attr\n");
13282 goto out_destroy_shost;
James Smart895427b2017-02-12 13:52:30 -080013283 }
13284
James Smart7b15db32013-01-03 15:43:29 -050013285 /* Set up SLI-4 HBA */
13286 if (lpfc_sli4_hba_setup(phba)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013287 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7b15db32013-01-03 15:43:29 -050013288 "1421 Failed to set up hba\n");
13289 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080013290 goto out_free_sysfs_attr;
James Smart7b15db32013-01-03 15:43:29 -050013291 }
13292
13293 /* Log the current active interrupt mode */
13294 phba->intr_mode = intr_mode;
13295 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040013296
13297 /* Perform post initialization setup */
13298 lpfc_post_init_setup(phba);
13299
James Smart01649562017-02-12 13:52:32 -080013300 /* NVME support in FW earlier in the driver load corrects the
13301 * FC4 type making a check for nvme_support unnecessary.
13302 */
James Smart0794d602019-01-28 11:14:19 -080013303 if (phba->nvmet_support == 0) {
13304 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
13305 /* Create NVME binding with nvme_fc_transport. This
13306 * ensures the vport is initialized. If the localport
13307 * create fails, it should not unload the driver to
13308 * support field issues.
13309 */
13310 error = lpfc_nvme_create_localport(vport);
13311 if (error) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013312 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080013313 "6004 NVME registration "
13314 "failed, error x%x\n",
13315 error);
13316 }
James Smart01649562017-02-12 13:52:32 -080013317 }
13318 }
James Smart895427b2017-02-12 13:52:30 -080013319
James Smartc71ab862012-10-31 14:44:33 -040013320 /* check for firmware upgrade or downgrade */
13321 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040013322 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040013323
James Smart1c6834a2009-07-19 10:01:26 -040013324 /* Check if there are static vports to be created. */
13325 lpfc_create_static_vport(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070013326
13327 /* Enable RAS FW log support */
13328 lpfc_sli4_ras_setup(phba);
13329
James Smart93a4d6f2019-11-04 16:57:05 -080013330 INIT_LIST_HEAD(&phba->poll_list);
James Smartf861f592020-03-22 11:12:54 -070013331 timer_setup(&phba->cpuhp_poll_timer, lpfc_sli4_poll_hbtimer, 0);
James Smart93a4d6f2019-11-04 16:57:05 -080013332 cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state, &phba->cpuhp);
13333
James Smartda0436e2009-05-22 14:51:39 -040013334 return 0;
13335
James Smartda0436e2009-05-22 14:51:39 -040013336out_free_sysfs_attr:
13337 lpfc_free_sysfs_attr(vport);
13338out_destroy_shost:
13339 lpfc_destroy_shost(phba);
James Smartcdb42be2019-01-28 11:14:21 -080013340out_disable_intr:
13341 lpfc_sli4_disable_intr(phba);
James Smartda0436e2009-05-22 14:51:39 -040013342out_unset_driver_resource:
13343 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040013344out_unset_driver_resource_s4:
13345 lpfc_sli4_driver_resource_unset(phba);
13346out_unset_pci_mem_s4:
13347 lpfc_sli4_pci_mem_unset(phba);
13348out_disable_pci_dev:
13349 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040013350 if (shost)
13351 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040013352out_free_phba:
13353 lpfc_hba_free(phba);
13354 return error;
13355}
13356
13357/**
13358 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
13359 * @pdev: pointer to PCI device
13360 *
13361 * This routine is called from the kernel's PCI subsystem to device with
13362 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
13363 * removed from PCI bus, it performs all the necessary cleanup for the HBA
13364 * device to be removed from the PCI subsystem properly.
13365 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013366static void
James Smartda0436e2009-05-22 14:51:39 -040013367lpfc_pci_remove_one_s4(struct pci_dev *pdev)
13368{
13369 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13370 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
13371 struct lpfc_vport **vports;
13372 struct lpfc_hba *phba = vport->phba;
13373 int i;
13374
13375 /* Mark the device unloading flag */
13376 spin_lock_irq(&phba->hbalock);
13377 vport->load_flag |= FC_UNLOADING;
13378 spin_unlock_irq(&phba->hbalock);
13379
James Smartda0436e2009-05-22 14:51:39 -040013380 lpfc_free_sysfs_attr(vport);
13381
13382 /* Release all the vports against this physical port */
13383 vports = lpfc_create_vport_work_array(phba);
13384 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040013385 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
13386 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
13387 continue;
James Smartda0436e2009-05-22 14:51:39 -040013388 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040013389 }
James Smartda0436e2009-05-22 14:51:39 -040013390 lpfc_destroy_vport_work_array(phba, vports);
13391
James Smart95f0ef82020-11-15 11:26:32 -080013392 /* Remove FC host with the physical port */
James Smartda0436e2009-05-22 14:51:39 -040013393 fc_remove_host(shost);
James Smarte9b11082020-11-15 11:26:33 -080013394 scsi_remove_host(shost);
James Smartda0436e2009-05-22 14:51:39 -040013395
James Smartda0436e2009-05-22 14:51:39 -040013396 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
13397 * localports are destroyed after to cleanup all transport memory.
13398 */
13399 lpfc_cleanup(vport);
13400 lpfc_nvmet_destroy_targetport(phba);
13401 lpfc_nvme_destroy_localport(vport);
13402
James Smartc4908502019-01-28 11:14:28 -080013403 /* De-allocate multi-XRI pools */
13404 if (phba->cfg_xri_rebalancing)
13405 lpfc_destroy_multixri_pools(phba);
13406
James Smart281d6192018-01-30 15:58:47 -080013407 /*
13408 * Bring down the SLI Layer. This step disables all interrupts,
13409 * clears the rings, discards all mailbox commands, and resets
13410 * the HBA FCoE function.
13411 */
13412 lpfc_debugfs_terminate(vport);
James Smartda0436e2009-05-22 14:51:39 -040013413
Dick Kennedy19017622017-09-29 17:34:27 -070013414 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070013415 spin_lock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040013416 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070013417 spin_unlock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040013418
James Smart3677a3a2010-09-29 11:19:14 -040013419 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040013420 * buffers are released to their corresponding pools here.
13421 */
James Smart5e5b5112019-01-28 11:14:22 -080013422 lpfc_io_free(phba);
James Smart01649562017-02-12 13:52:32 -080013423 lpfc_free_iocb_list(phba);
James Smart5e5b5112019-01-28 11:14:22 -080013424 lpfc_sli4_hba_unset(phba);
James Smart67d12732012-08-03 12:36:13 -040013425
James Smart0cdb84e2018-04-09 14:24:26 -070013426 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040013427 lpfc_sli4_driver_resource_unset(phba);
13428
13429 /* Unmap adapter Control and Doorbell registers */
13430 lpfc_sli4_pci_mem_unset(phba);
13431
13432 /* Release PCI resources and disable device's PCI function */
13433 scsi_host_put(shost);
13434 lpfc_disable_pci_dev(phba);
13435
13436 /* Finally, free the driver's device data structure */
13437 lpfc_hba_free(phba);
13438
13439 return;
13440}
13441
13442/**
13443 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013444 * @dev_d: pointer to device
James Smartda0436e2009-05-22 14:51:39 -040013445 *
13446 * This routine is called from the kernel's PCI subsystem to support system
13447 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
13448 * this method, it quiesces the device by stopping the driver's worker
13449 * thread for the device, turning off device's interrupt and DMA, and bring
13450 * the device offline. Note that as the driver implements the minimum PM
13451 * requirements to a power-aware driver's PM support for suspend/resume -- all
13452 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
13453 * method call will be treated as SUSPEND and the driver will fully
13454 * reinitialize its device during resume() method call, the driver will set
13455 * device to PCI_D3hot state in PCI config space instead of setting it
13456 * according to the @msg provided by the PM.
13457 *
13458 * Return code
13459 * 0 - driver suspended the device
13460 * Error otherwise
13461 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013462static int __maybe_unused
13463lpfc_pci_suspend_one_s4(struct device *dev_d)
James Smartda0436e2009-05-22 14:51:39 -040013464{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013465 struct Scsi_Host *shost = dev_get_drvdata(dev_d);
James Smartda0436e2009-05-22 14:51:39 -040013466 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13467
13468 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040013469 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040013470
13471 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040013472 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040013473 lpfc_offline(phba);
13474 kthread_stop(phba->worker_thread);
13475
13476 /* Disable interrupt from device */
13477 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040013478 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040013479
James Smartda0436e2009-05-22 14:51:39 -040013480 return 0;
13481}
13482
13483/**
13484 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013485 * @dev_d: pointer to device
James Smartda0436e2009-05-22 14:51:39 -040013486 *
13487 * This routine is called from the kernel's PCI subsystem to support system
13488 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
13489 * this method, it restores the device's PCI config space state and fully
13490 * reinitializes the device and brings it online. Note that as the driver
13491 * implements the minimum PM requirements to a power-aware driver's PM for
13492 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
13493 * to the suspend() method call will be treated as SUSPEND and the driver
13494 * will fully reinitialize its device during resume() method call, the device
13495 * will be set to PCI_D0 directly in PCI config space before restoring the
13496 * state.
13497 *
13498 * Return code
13499 * 0 - driver suspended the device
13500 * Error otherwise
13501 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013502static int __maybe_unused
13503lpfc_pci_resume_one_s4(struct device *dev_d)
James Smartda0436e2009-05-22 14:51:39 -040013504{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013505 struct Scsi_Host *shost = dev_get_drvdata(dev_d);
James Smartda0436e2009-05-22 14:51:39 -040013506 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13507 uint32_t intr_mode;
13508 int error;
13509
13510 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13511 "0292 PCI device Power Management resume.\n");
13512
James Smartda0436e2009-05-22 14:51:39 -040013513 /* Startup the kernel thread for this host adapter. */
13514 phba->worker_thread = kthread_run(lpfc_do_work, phba,
13515 "lpfc_worker_%d", phba->brd_no);
13516 if (IS_ERR(phba->worker_thread)) {
13517 error = PTR_ERR(phba->worker_thread);
13518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13519 "0293 PM resume failed to start worker "
13520 "thread: error=x%x.\n", error);
13521 return error;
13522 }
13523
13524 /* Configure and enable interrupt */
13525 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
13526 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013527 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -040013528 "0294 PM resume Failed to enable interrupt\n");
13529 return -EIO;
13530 } else
13531 phba->intr_mode = intr_mode;
13532
13533 /* Restart HBA and bring it online */
13534 lpfc_sli_brdrestart(phba);
13535 lpfc_online(phba);
13536
13537 /* Log the current active interrupt mode */
13538 lpfc_log_intr_mode(phba, phba->intr_mode);
13539
13540 return 0;
13541}
13542
13543/**
James Smart75baf692010-06-08 18:31:21 -040013544 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
13545 * @phba: pointer to lpfc hba data structure.
13546 *
13547 * This routine is called to prepare the SLI4 device for PCI slot recover. It
13548 * aborts all the outstanding SCSI I/Os to the pci device.
13549 **/
13550static void
13551lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
13552{
Dick Kennedy372c1872020-06-30 14:50:00 -070013553 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040013554 "2828 PCI channel I/O abort preparing for recovery\n");
13555 /*
13556 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
13557 * and let the SCSI mid-layer to retry them to recover.
13558 */
James Smartdb55fba2014-04-04 13:52:02 -040013559 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040013560}
13561
13562/**
13563 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
13564 * @phba: pointer to lpfc hba data structure.
13565 *
13566 * This routine is called to prepare the SLI4 device for PCI slot reset. It
13567 * disables the device interrupt and pci device, and aborts the internal FCP
13568 * pending I/Os.
13569 **/
13570static void
13571lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
13572{
Dick Kennedy372c1872020-06-30 14:50:00 -070013573 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040013574 "2826 PCI channel disable preparing for reset\n");
13575
13576 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040013577 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040013578
13579 /* Block all SCSI devices' I/Os on the host */
13580 lpfc_scsi_dev_block(phba);
13581
James Smartc00f62e2019-08-14 16:57:11 -070013582 /* Flush all driver's outstanding I/Os as we are to reset */
13583 lpfc_sli_flush_io_rings(phba);
James Smartc3725bd2017-11-20 16:00:42 -080013584
James Smart75baf692010-06-08 18:31:21 -040013585 /* stop all timers */
13586 lpfc_stop_hba_timers(phba);
13587
13588 /* Disable interrupt and pci device */
13589 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040013590 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040013591 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040013592}
13593
13594/**
13595 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
13596 * @phba: pointer to lpfc hba data structure.
13597 *
13598 * This routine is called to prepare the SLI4 device for PCI slot permanently
13599 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
13600 * pending I/Os.
13601 **/
13602static void
13603lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
13604{
Dick Kennedy372c1872020-06-30 14:50:00 -070013605 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040013606 "2827 PCI channel permanent disable for failure\n");
13607
13608 /* Block all SCSI devices' I/Os on the host */
13609 lpfc_scsi_dev_block(phba);
13610
13611 /* stop all timers */
13612 lpfc_stop_hba_timers(phba);
13613
James Smartc00f62e2019-08-14 16:57:11 -070013614 /* Clean up all driver's outstanding I/Os */
13615 lpfc_sli_flush_io_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040013616}
13617
13618/**
James Smartda0436e2009-05-22 14:51:39 -040013619 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
13620 * @pdev: pointer to PCI device.
13621 * @state: the current PCI connection state.
13622 *
13623 * This routine is called from the PCI subsystem for error handling to device
13624 * with SLI-4 interface spec. This function is called by the PCI subsystem
13625 * after a PCI bus error affecting this device has been detected. When this
13626 * function is invoked, it will need to stop all the I/Os and interrupt(s)
13627 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
13628 * for the PCI subsystem to perform proper recovery as desired.
13629 *
13630 * Return codes
13631 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
13632 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13633 **/
13634static pci_ers_result_t
13635lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
13636{
James Smart75baf692010-06-08 18:31:21 -040013637 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13638 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13639
13640 switch (state) {
13641 case pci_channel_io_normal:
13642 /* Non-fatal error, prepare for recovery */
13643 lpfc_sli4_prep_dev_for_recover(phba);
13644 return PCI_ERS_RESULT_CAN_RECOVER;
13645 case pci_channel_io_frozen:
13646 /* Fatal error, prepare for slot reset */
13647 lpfc_sli4_prep_dev_for_reset(phba);
13648 return PCI_ERS_RESULT_NEED_RESET;
13649 case pci_channel_io_perm_failure:
13650 /* Permanent failure, prepare for device down */
13651 lpfc_sli4_prep_dev_for_perm_failure(phba);
13652 return PCI_ERS_RESULT_DISCONNECT;
13653 default:
13654 /* Unknown state, prepare and request slot reset */
Dick Kennedy372c1872020-06-30 14:50:00 -070013655 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040013656 "2825 Unknown PCI error state: x%x\n", state);
13657 lpfc_sli4_prep_dev_for_reset(phba);
13658 return PCI_ERS_RESULT_NEED_RESET;
13659 }
James Smartda0436e2009-05-22 14:51:39 -040013660}
13661
13662/**
13663 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
13664 * @pdev: pointer to PCI device.
13665 *
13666 * This routine is called from the PCI subsystem for error handling to device
13667 * with SLI-4 interface spec. It is called after PCI bus has been reset to
13668 * restart the PCI card from scratch, as if from a cold-boot. During the
13669 * PCI subsystem error recovery, after the driver returns
13670 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
13671 * recovery and then call this routine before calling the .resume method to
13672 * recover the device. This function will initialize the HBA device, enable
13673 * the interrupt, but it will just put the HBA to offline state without
13674 * passing any I/O traffic.
13675 *
13676 * Return codes
13677 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
13678 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13679 */
13680static pci_ers_result_t
13681lpfc_io_slot_reset_s4(struct pci_dev *pdev)
13682{
James Smart75baf692010-06-08 18:31:21 -040013683 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13684 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13685 struct lpfc_sli *psli = &phba->sli;
13686 uint32_t intr_mode;
13687
13688 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
13689 if (pci_enable_device_mem(pdev)) {
13690 printk(KERN_ERR "lpfc: Cannot re-enable "
13691 "PCI device after reset.\n");
13692 return PCI_ERS_RESULT_DISCONNECT;
13693 }
13694
13695 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040013696
13697 /*
13698 * As the new kernel behavior of pci_restore_state() API call clears
13699 * device saved_state flag, need to save the restored state again.
13700 */
13701 pci_save_state(pdev);
13702
James Smart75baf692010-06-08 18:31:21 -040013703 if (pdev->is_busmaster)
13704 pci_set_master(pdev);
13705
13706 spin_lock_irq(&phba->hbalock);
13707 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
13708 spin_unlock_irq(&phba->hbalock);
13709
13710 /* Configure and enable interrupt */
13711 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
13712 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013713 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040013714 "2824 Cannot re-enable interrupt after "
13715 "slot reset.\n");
13716 return PCI_ERS_RESULT_DISCONNECT;
13717 } else
13718 phba->intr_mode = intr_mode;
13719
13720 /* Log the current active interrupt mode */
13721 lpfc_log_intr_mode(phba, phba->intr_mode);
13722
James Smartda0436e2009-05-22 14:51:39 -040013723 return PCI_ERS_RESULT_RECOVERED;
13724}
13725
13726/**
13727 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
13728 * @pdev: pointer to PCI device
13729 *
13730 * This routine is called from the PCI subsystem for error handling to device
13731 * with SLI-4 interface spec. It is called when kernel error recovery tells
13732 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
13733 * error recovery. After this call, traffic can start to flow from this device
13734 * again.
13735 **/
13736static void
13737lpfc_io_resume_s4(struct pci_dev *pdev)
13738{
James Smart75baf692010-06-08 18:31:21 -040013739 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13740 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13741
13742 /*
13743 * In case of slot reset, as function reset is performed through
13744 * mailbox command which needs DMA to be enabled, this operation
13745 * has to be moved to the io resume phase. Taking device offline
13746 * will perform the necessary cleanup.
13747 */
13748 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
13749 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040013750 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040013751 lpfc_offline(phba);
13752 lpfc_sli_brdrestart(phba);
13753 /* Bring the device back online */
13754 lpfc_online(phba);
13755 }
James Smartda0436e2009-05-22 14:51:39 -040013756}
13757
13758/**
James Smart3772a992009-05-22 14:50:54 -040013759 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
13760 * @pdev: pointer to PCI device
13761 * @pid: pointer to PCI device identifier
13762 *
13763 * This routine is to be registered to the kernel's PCI subsystem. When an
13764 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
13765 * at PCI device-specific information of the device and driver to see if the
13766 * driver state that it can support this kind of device. If the match is
13767 * successful, the driver core invokes this routine. This routine dispatches
13768 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
13769 * do all the initialization that it needs to do to handle the HBA device
13770 * properly.
13771 *
13772 * Return code
13773 * 0 - driver can claim the device
13774 * negative value - driver can not claim the device
13775 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013776static int
James Smart3772a992009-05-22 14:50:54 -040013777lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
13778{
13779 int rc;
James Smart8fa38512009-07-19 10:01:03 -040013780 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040013781
James Smart28baac72010-02-12 14:42:03 -050013782 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040013783 return -ENODEV;
13784
James Smart8fa38512009-07-19 10:01:03 -040013785 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050013786 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040013787 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040013788 else
James Smart3772a992009-05-22 14:50:54 -040013789 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040013790
James Smart3772a992009-05-22 14:50:54 -040013791 return rc;
13792}
13793
13794/**
13795 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
13796 * @pdev: pointer to PCI device
13797 *
13798 * This routine is to be registered to the kernel's PCI subsystem. When an
13799 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
13800 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
13801 * remove routine, which will perform all the necessary cleanup for the
13802 * device to be removed from the PCI subsystem properly.
13803 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013804static void
James Smart3772a992009-05-22 14:50:54 -040013805lpfc_pci_remove_one(struct pci_dev *pdev)
13806{
13807 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13808 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13809
13810 switch (phba->pci_dev_grp) {
13811 case LPFC_PCI_DEV_LP:
13812 lpfc_pci_remove_one_s3(pdev);
13813 break;
James Smartda0436e2009-05-22 14:51:39 -040013814 case LPFC_PCI_DEV_OC:
13815 lpfc_pci_remove_one_s4(pdev);
13816 break;
James Smart3772a992009-05-22 14:50:54 -040013817 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013818 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013819 "1424 Invalid PCI device group: 0x%x\n",
13820 phba->pci_dev_grp);
13821 break;
13822 }
13823 return;
13824}
13825
13826/**
13827 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013828 * @dev: pointer to device
James Smart3772a992009-05-22 14:50:54 -040013829 *
13830 * This routine is to be registered to the kernel's PCI subsystem to support
13831 * system Power Management (PM). When PM invokes this method, it dispatches
13832 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
13833 * suspend the device.
13834 *
13835 * Return code
13836 * 0 - driver suspended the device
13837 * Error otherwise
13838 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013839static int __maybe_unused
13840lpfc_pci_suspend_one(struct device *dev)
James Smart3772a992009-05-22 14:50:54 -040013841{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013842 struct Scsi_Host *shost = dev_get_drvdata(dev);
James Smart3772a992009-05-22 14:50:54 -040013843 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13844 int rc = -ENODEV;
13845
13846 switch (phba->pci_dev_grp) {
13847 case LPFC_PCI_DEV_LP:
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013848 rc = lpfc_pci_suspend_one_s3(dev);
James Smart3772a992009-05-22 14:50:54 -040013849 break;
James Smartda0436e2009-05-22 14:51:39 -040013850 case LPFC_PCI_DEV_OC:
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013851 rc = lpfc_pci_suspend_one_s4(dev);
James Smartda0436e2009-05-22 14:51:39 -040013852 break;
James Smart3772a992009-05-22 14:50:54 -040013853 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013854 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013855 "1425 Invalid PCI device group: 0x%x\n",
13856 phba->pci_dev_grp);
13857 break;
13858 }
13859 return rc;
13860}
13861
13862/**
13863 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013864 * @dev: pointer to device
James Smart3772a992009-05-22 14:50:54 -040013865 *
13866 * This routine is to be registered to the kernel's PCI subsystem to support
13867 * system Power Management (PM). When PM invokes this method, it dispatches
13868 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
13869 * resume the device.
13870 *
13871 * Return code
13872 * 0 - driver suspended the device
13873 * Error otherwise
13874 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013875static int __maybe_unused
13876lpfc_pci_resume_one(struct device *dev)
James Smart3772a992009-05-22 14:50:54 -040013877{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013878 struct Scsi_Host *shost = dev_get_drvdata(dev);
James Smart3772a992009-05-22 14:50:54 -040013879 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13880 int rc = -ENODEV;
13881
13882 switch (phba->pci_dev_grp) {
13883 case LPFC_PCI_DEV_LP:
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013884 rc = lpfc_pci_resume_one_s3(dev);
James Smart3772a992009-05-22 14:50:54 -040013885 break;
James Smartda0436e2009-05-22 14:51:39 -040013886 case LPFC_PCI_DEV_OC:
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053013887 rc = lpfc_pci_resume_one_s4(dev);
James Smartda0436e2009-05-22 14:51:39 -040013888 break;
James Smart3772a992009-05-22 14:50:54 -040013889 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013890 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013891 "1426 Invalid PCI device group: 0x%x\n",
13892 phba->pci_dev_grp);
13893 break;
13894 }
13895 return rc;
13896}
13897
13898/**
13899 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
13900 * @pdev: pointer to PCI device.
13901 * @state: the current PCI connection state.
13902 *
13903 * This routine is registered to the PCI subsystem for error handling. This
13904 * function is called by the PCI subsystem after a PCI bus error affecting
13905 * this device has been detected. When this routine is invoked, it dispatches
13906 * the action to the proper SLI-3 or SLI-4 device error detected handling
13907 * routine, which will perform the proper error detected operation.
13908 *
13909 * Return codes
13910 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
13911 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13912 **/
13913static pci_ers_result_t
13914lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
13915{
13916 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13917 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13918 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13919
13920 switch (phba->pci_dev_grp) {
13921 case LPFC_PCI_DEV_LP:
13922 rc = lpfc_io_error_detected_s3(pdev, state);
13923 break;
James Smartda0436e2009-05-22 14:51:39 -040013924 case LPFC_PCI_DEV_OC:
13925 rc = lpfc_io_error_detected_s4(pdev, state);
13926 break;
James Smart3772a992009-05-22 14:50:54 -040013927 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013928 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013929 "1427 Invalid PCI device group: 0x%x\n",
13930 phba->pci_dev_grp);
13931 break;
13932 }
13933 return rc;
13934}
13935
13936/**
13937 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
13938 * @pdev: pointer to PCI device.
13939 *
13940 * This routine is registered to the PCI subsystem for error handling. This
13941 * function is called after PCI bus has been reset to restart the PCI card
13942 * from scratch, as if from a cold-boot. When this routine is invoked, it
13943 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
13944 * routine, which will perform the proper device reset.
13945 *
13946 * Return codes
13947 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
13948 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13949 **/
13950static pci_ers_result_t
13951lpfc_io_slot_reset(struct pci_dev *pdev)
13952{
13953 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13954 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13955 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13956
13957 switch (phba->pci_dev_grp) {
13958 case LPFC_PCI_DEV_LP:
13959 rc = lpfc_io_slot_reset_s3(pdev);
13960 break;
James Smartda0436e2009-05-22 14:51:39 -040013961 case LPFC_PCI_DEV_OC:
13962 rc = lpfc_io_slot_reset_s4(pdev);
13963 break;
James Smart3772a992009-05-22 14:50:54 -040013964 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013965 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013966 "1428 Invalid PCI device group: 0x%x\n",
13967 phba->pci_dev_grp);
13968 break;
13969 }
13970 return rc;
13971}
13972
13973/**
13974 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
13975 * @pdev: pointer to PCI device
13976 *
13977 * This routine is registered to the PCI subsystem for error handling. It
13978 * is called when kernel error recovery tells the lpfc driver that it is
13979 * OK to resume normal PCI operation after PCI bus error recovery. When
13980 * this routine is invoked, it dispatches the action to the proper SLI-3
13981 * or SLI-4 device io_resume routine, which will resume the device operation.
13982 **/
13983static void
13984lpfc_io_resume(struct pci_dev *pdev)
13985{
13986 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13987 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13988
13989 switch (phba->pci_dev_grp) {
13990 case LPFC_PCI_DEV_LP:
13991 lpfc_io_resume_s3(pdev);
13992 break;
James Smartda0436e2009-05-22 14:51:39 -040013993 case LPFC_PCI_DEV_OC:
13994 lpfc_io_resume_s4(pdev);
13995 break;
James Smart3772a992009-05-22 14:50:54 -040013996 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013997 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013998 "1429 Invalid PCI device group: 0x%x\n",
13999 phba->pci_dev_grp);
14000 break;
14001 }
14002 return;
14003}
14004
James Smart1ba981f2014-02-20 09:56:45 -050014005/**
14006 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
14007 * @phba: pointer to lpfc hba data structure.
14008 *
14009 * This routine checks to see if OAS is supported for this adapter. If
14010 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
14011 * the enable oas flag is cleared and the pool created for OAS device data
14012 * is destroyed.
14013 *
14014 **/
YueHaibingc7092972019-04-17 23:05:54 +080014015static void
James Smart1ba981f2014-02-20 09:56:45 -050014016lpfc_sli4_oas_verify(struct lpfc_hba *phba)
14017{
14018
14019 if (!phba->cfg_EnableXLane)
14020 return;
14021
14022 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
14023 phba->cfg_fof = 1;
14024 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040014025 phba->cfg_fof = 0;
Saurav Girepunjec3e5aac2019-10-27 01:17:17 +053014026 mempool_destroy(phba->device_data_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -050014027 phba->device_data_mem_pool = NULL;
14028 }
14029
14030 return;
14031}
14032
14033/**
James Smartd2cc9bc2018-09-10 10:30:50 -070014034 * lpfc_sli4_ras_init - Verify RAS-FW log is supported by this adapter
14035 * @phba: pointer to lpfc hba data structure.
14036 *
14037 * This routine checks to see if RAS is supported by the adapter. Check the
14038 * function through which RAS support enablement is to be done.
14039 **/
14040void
14041lpfc_sli4_ras_init(struct lpfc_hba *phba)
14042{
14043 switch (phba->pcidev->device) {
14044 case PCI_DEVICE_ID_LANCER_G6_FC:
14045 case PCI_DEVICE_ID_LANCER_G7_FC:
14046 phba->ras_fwlog.ras_hwsupport = true;
James Smartcb349902018-11-29 16:09:27 -080014047 if (phba->cfg_ras_fwlog_func == PCI_FUNC(phba->pcidev->devfn) &&
14048 phba->cfg_ras_fwlog_buffsize)
James Smartd2cc9bc2018-09-10 10:30:50 -070014049 phba->ras_fwlog.ras_enabled = true;
14050 else
14051 phba->ras_fwlog.ras_enabled = false;
14052 break;
14053 default:
14054 phba->ras_fwlog.ras_hwsupport = false;
14055 }
14056}
14057
James Smart1ba981f2014-02-20 09:56:45 -050014058
dea31012005-04-17 16:05:31 -050014059MODULE_DEVICE_TABLE(pci, lpfc_id_table);
14060
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070014061static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060014062 .error_detected = lpfc_io_error_detected,
14063 .slot_reset = lpfc_io_slot_reset,
14064 .resume = lpfc_io_resume,
14065};
14066
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014067static SIMPLE_DEV_PM_OPS(lpfc_pci_pm_ops_one,
14068 lpfc_pci_suspend_one,
14069 lpfc_pci_resume_one);
14070
dea31012005-04-17 16:05:31 -050014071static struct pci_driver lpfc_driver = {
14072 .name = LPFC_DRIVER_NAME,
14073 .id_table = lpfc_id_table,
14074 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080014075 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110014076 .shutdown = lpfc_pci_remove_one,
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014077 .driver.pm = &lpfc_pci_pm_ops_one,
James Smart2e0fef82007-06-17 19:56:36 -050014078 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050014079};
14080
James Smart3ef6d242012-01-18 16:23:48 -050014081static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040014082 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050014083};
14084
14085static struct miscdevice lpfc_mgmt_dev = {
14086 .minor = MISC_DYNAMIC_MINOR,
14087 .name = "lpfcmgmt",
14088 .fops = &lpfc_mgmt_fop,
14089};
14090
James Smarte59058c2008-08-24 21:49:00 -040014091/**
James Smart3621a712009-04-06 18:47:14 -040014092 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040014093 *
14094 * This routine is to be invoked when the lpfc module is loaded into the
14095 * kernel. The special kernel macro module_init() is used to indicate the
14096 * role of this routine to the kernel as lpfc module entry point.
14097 *
14098 * Return codes
14099 * 0 - successful
14100 * -ENOMEM - FC attach transport failed
14101 * all others - failed
14102 */
dea31012005-04-17 16:05:31 -050014103static int __init
14104lpfc_init(void)
14105{
14106 int error = 0;
14107
Anton Blanchardbc2736e2020-07-13 18:39:08 +100014108 pr_info(LPFC_MODULE_DESC "\n");
14109 pr_info(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050014110
James Smart3ef6d242012-01-18 16:23:48 -050014111 error = misc_register(&lpfc_mgmt_dev);
14112 if (error)
14113 printk(KERN_ERR "Could not register lpfcmgmt device, "
14114 "misc_register returned with status %d", error);
14115
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080014116 error = -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070014117 lpfc_transport_functions.vport_create = lpfc_vport_create;
14118 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050014119 lpfc_transport_template =
14120 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040014121 if (lpfc_transport_template == NULL)
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080014122 goto unregister;
James Smart458c0832016-07-06 12:36:07 -070014123 lpfc_vport_transport_template =
14124 fc_attach_transport(&lpfc_vport_transport_functions);
14125 if (lpfc_vport_transport_template == NULL) {
14126 fc_release_transport(lpfc_transport_template);
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080014127 goto unregister;
James Smart7ee5d432007-10-27 13:37:17 -040014128 }
James Smart840a4702020-11-15 11:26:40 -080014129 lpfc_wqe_cmd_template();
James Smartbd3061b2018-03-05 12:04:05 -080014130 lpfc_nvmet_cmd_template();
James Smart7bb03bb2013-04-17 20:19:16 -040014131
14132 /* Initialize in case vector mapping is needed */
James Smart2ea259e2017-02-12 13:52:27 -080014133 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040014134
James Smart93a4d6f2019-11-04 16:57:05 -080014135 error = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
14136 "lpfc/sli4:online",
14137 lpfc_cpu_online, lpfc_cpu_offline);
14138 if (error < 0)
14139 goto cpuhp_failure;
14140 lpfc_cpuhp_state = error;
14141
dea31012005-04-17 16:05:31 -050014142 error = pci_register_driver(&lpfc_driver);
James Smart93a4d6f2019-11-04 16:57:05 -080014143 if (error)
14144 goto unwind;
14145
14146 return error;
14147
14148unwind:
14149 cpuhp_remove_multi_state(lpfc_cpuhp_state);
14150cpuhp_failure:
14151 fc_release_transport(lpfc_transport_template);
14152 fc_release_transport(lpfc_vport_transport_template);
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080014153unregister:
14154 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050014155
14156 return error;
14157}
14158
Dick Kennedy372c1872020-06-30 14:50:00 -070014159void lpfc_dmp_dbg(struct lpfc_hba *phba)
14160{
14161 unsigned int start_idx;
14162 unsigned int dbg_cnt;
14163 unsigned int temp_idx;
14164 int i;
14165 int j = 0;
James Smarte8613082021-07-07 11:43:33 -070014166 unsigned long rem_nsec, iflags;
14167 bool log_verbose = false;
14168 struct lpfc_vport *port_iterator;
Dick Kennedy372c1872020-06-30 14:50:00 -070014169
James Smart0b3ad322021-01-04 10:02:39 -080014170 /* Don't dump messages if we explicitly set log_verbose for the
14171 * physical port or any vport.
14172 */
Dick Kennedy372c1872020-06-30 14:50:00 -070014173 if (phba->cfg_log_verbose)
14174 return;
14175
James Smarte8613082021-07-07 11:43:33 -070014176 spin_lock_irqsave(&phba->port_list_lock, iflags);
14177 list_for_each_entry(port_iterator, &phba->port_list, listentry) {
14178 if (port_iterator->load_flag & FC_UNLOADING)
14179 continue;
14180 if (scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
14181 if (port_iterator->cfg_log_verbose)
14182 log_verbose = true;
14183
14184 scsi_host_put(lpfc_shost_from_vport(port_iterator));
14185
14186 if (log_verbose) {
14187 spin_unlock_irqrestore(&phba->port_list_lock,
14188 iflags);
James Smart0b3ad322021-01-04 10:02:39 -080014189 return;
14190 }
14191 }
14192 }
James Smarte8613082021-07-07 11:43:33 -070014193 spin_unlock_irqrestore(&phba->port_list_lock, iflags);
James Smart0b3ad322021-01-04 10:02:39 -080014194
Dick Kennedy372c1872020-06-30 14:50:00 -070014195 if (atomic_cmpxchg(&phba->dbg_log_dmping, 0, 1) != 0)
14196 return;
14197
14198 start_idx = (unsigned int)atomic_read(&phba->dbg_log_idx) % DBG_LOG_SZ;
14199 dbg_cnt = (unsigned int)atomic_read(&phba->dbg_log_cnt);
James Smart0b3ad322021-01-04 10:02:39 -080014200 if (!dbg_cnt)
14201 goto out;
Dick Kennedy372c1872020-06-30 14:50:00 -070014202 temp_idx = start_idx;
14203 if (dbg_cnt >= DBG_LOG_SZ) {
14204 dbg_cnt = DBG_LOG_SZ;
14205 temp_idx -= 1;
14206 } else {
14207 if ((start_idx + dbg_cnt) > (DBG_LOG_SZ - 1)) {
14208 temp_idx = (start_idx + dbg_cnt) % DBG_LOG_SZ;
14209 } else {
Dick Kennedy77dd7d72020-07-06 13:42:46 -070014210 if (start_idx < dbg_cnt)
Dick Kennedy372c1872020-06-30 14:50:00 -070014211 start_idx = DBG_LOG_SZ - (dbg_cnt - start_idx);
Dick Kennedy77dd7d72020-07-06 13:42:46 -070014212 else
Dick Kennedy372c1872020-06-30 14:50:00 -070014213 start_idx -= dbg_cnt;
Dick Kennedy372c1872020-06-30 14:50:00 -070014214 }
14215 }
14216 dev_info(&phba->pcidev->dev, "start %d end %d cnt %d\n",
14217 start_idx, temp_idx, dbg_cnt);
14218
14219 for (i = 0; i < dbg_cnt; i++) {
14220 if ((start_idx + i) < DBG_LOG_SZ)
Dick Kennedy77dd7d72020-07-06 13:42:46 -070014221 temp_idx = (start_idx + i) % DBG_LOG_SZ;
Dick Kennedy372c1872020-06-30 14:50:00 -070014222 else
14223 temp_idx = j++;
14224 rem_nsec = do_div(phba->dbg_log[temp_idx].t_ns, NSEC_PER_SEC);
14225 dev_info(&phba->pcidev->dev, "%d: [%5lu.%06lu] %s",
14226 temp_idx,
14227 (unsigned long)phba->dbg_log[temp_idx].t_ns,
14228 rem_nsec / 1000,
14229 phba->dbg_log[temp_idx].log);
14230 }
James Smart0b3ad322021-01-04 10:02:39 -080014231out:
Dick Kennedy372c1872020-06-30 14:50:00 -070014232 atomic_set(&phba->dbg_log_cnt, 0);
14233 atomic_set(&phba->dbg_log_dmping, 0);
14234}
14235
Lee Jones7fa03c72020-07-23 13:24:21 +010014236__printf(2, 3)
Dick Kennedy372c1872020-06-30 14:50:00 -070014237void lpfc_dbg_print(struct lpfc_hba *phba, const char *fmt, ...)
14238{
14239 unsigned int idx;
14240 va_list args;
14241 int dbg_dmping = atomic_read(&phba->dbg_log_dmping);
14242 struct va_format vaf;
14243
14244
14245 va_start(args, fmt);
14246 if (unlikely(dbg_dmping)) {
14247 vaf.fmt = fmt;
14248 vaf.va = &args;
14249 dev_info(&phba->pcidev->dev, "%pV", &vaf);
14250 va_end(args);
14251 return;
14252 }
14253 idx = (unsigned int)atomic_fetch_add(1, &phba->dbg_log_idx) %
14254 DBG_LOG_SZ;
14255
14256 atomic_inc(&phba->dbg_log_cnt);
14257
14258 vscnprintf(phba->dbg_log[idx].log,
14259 sizeof(phba->dbg_log[idx].log), fmt, args);
14260 va_end(args);
14261
14262 phba->dbg_log[idx].t_ns = local_clock();
14263}
14264
James Smarte59058c2008-08-24 21:49:00 -040014265/**
James Smart3621a712009-04-06 18:47:14 -040014266 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040014267 *
14268 * This routine is invoked when the lpfc module is removed from the kernel.
14269 * The special kernel macro module_exit() is used to indicate the role of
14270 * this routine to the kernel as lpfc module exit point.
14271 */
dea31012005-04-17 16:05:31 -050014272static void __exit
14273lpfc_exit(void)
14274{
James Smart3ef6d242012-01-18 16:23:48 -050014275 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050014276 pci_unregister_driver(&lpfc_driver);
James Smart93a4d6f2019-11-04 16:57:05 -080014277 cpuhp_remove_multi_state(lpfc_cpuhp_state);
dea31012005-04-17 16:05:31 -050014278 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070014279 fc_release_transport(lpfc_vport_transport_template);
Johannes Thumshirn79739672015-08-31 16:48:11 -040014280 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050014281}
14282
14283module_init(lpfc_init);
14284module_exit(lpfc_exit);
14285MODULE_LICENSE("GPL");
14286MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080014287MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050014288MODULE_VERSION("0:" LPFC_DRIVER_VERSION);